aboutsummaryrefslogtreecommitdiffstats
path: root/enable_X11_numlock.c
blob: 64ccc350630f997ee8c503729416b0bc32076c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/****************************************************************************

 NumlockX -  (C) 2000 Lubos Lunak <l.lunak@email.cz>
 Released under the terms of the GNU General Public License

 main.c  -
 
 $Id$

****************************************************************************/

/* The NumLock state detection code is originally from KLeds by
   Hans Matzen <hans@tm.informatik.uni-frankfurt.de>  */

#define __main_C

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define HAS_XKB 1
#ifdef HAS_XKB
#include <X11/XKBlib.h>
#endif

void usage( const char* argv0 )
    {
    printf( "NumlockX - (C) 2000 Lubos Lunak <l.lunak@email.cz>\n\n"
#ifdef HAS_XKB
        "Usage : %s [on|off|switch]\n"
        "on     - sets NumLock on in X (default)\n"
        "off    - sets NumLock off in X\n"
        "switch - changes NumLock state in X\n"
#else
        "Usage : %s\n"
        "Changes NumLock state in X\n"
        "( NumLock state detection not available,"
        " compiled without XKB )\n"
#endif
        "\n"
        , argv0 );
    }

Display* disp;

#ifdef HAS_XKB
int get_numlock_state()
    {
    unsigned int states;
    if( XkbGetIndicatorState( disp, XkbUseCoreKbd, &states) != Success )
        {
        printf("Error while reading Indicator status\n");
        XCloseDisplay( disp );
        exit( 3 );
        }
    return states & 0x02; /* NumLock appears to be bit1 */
    }
#endif

void change_numlock()
    {
    XTestFakeKeyEvent( disp, XKeysymToKeycode( disp, XK_Num_Lock ), True, CurrentTime );
    XTestFakeKeyEvent( disp, XKeysymToKeycode( disp, XK_Num_Lock ), False, CurrentTime );
    }

#ifdef HAS_XKB
void set_on()
    {
    if( !get_numlock_state())
        change_numlock();
    }

void set_off()
    {
    if( get_numlock_state())
        change_numlock();
    }
#endif

int main( int argc, char* argv[] )
    {
	if( argc > 2 )
	    {
        usage( argv[ 0 ] );
        return 1;
        }
    disp = XOpenDisplay( NULL );
    if( disp == NULL )
        {
        printf( "Error opening display\n" );
        return 2;
        }
    if( argc == 1 )
#if HAS_XKB
        set_on();
    else if( strncmp( argv[ 1 ], "on", 2 ) == 0 )
        set_on();
    else if( strncmp( argv[ 1 ], "of", 2 ) == 0 )
        set_off();
#else
        change_numlock(); /* if( argc == 1 ) */
#endif
    else if( strncmp( argv[ 1 ], "switch", 6 ) == 0 )
        change_numlock();
    else
        {
        usage( argv[ 0 ] );
        XCloseDisplay( disp );
        return 1;
        }
    XCloseDisplay( disp );
    return 0;
    }