/[soft]/numlock/branches/1/enable_X11_numlock.c
ViewVC logotype

Contents of /numlock/branches/1/enable_X11_numlock.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1637 - (show annotations) (download)
Thu Jun 2 20:45:18 2011 UTC (13 years, 9 months ago) by dmorgan
File MIME type: text/plain
File size: 2725 byte(s)
Branch for updates
1 /****************************************************************************
2
3 NumlockX - (C) 2000 Lubos Lunak <l.lunak@email.cz>
4 Released under the terms of the GNU General Public License
5
6 main.c -
7
8 $Id: enable_X11_numlock.c 158725 2001-03-29 11:17:40Z gc $
9
10 ****************************************************************************/
11
12 /* The NumLock state detection code is originally from KLeds by
13 Hans Matzen <hans@tm.informatik.uni-frankfurt.de> */
14
15 #define __main_C
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <X11/extensions/XTest.h>
22 #include <X11/keysym.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #define HAS_XKB 1
28 #ifdef HAS_XKB
29 #include <X11/XKBlib.h>
30 #endif
31
32 void usage( const char* argv0 )
33 {
34 printf( "NumlockX - (C) 2000 Lubos Lunak <l.lunak@email.cz>\n\n"
35 #ifdef HAS_XKB
36 "Usage : %s [on|off|switch]\n"
37 "on - sets NumLock on in X (default)\n"
38 "off - sets NumLock off in X\n"
39 "switch - changes NumLock state in X\n"
40 #else
41 "Usage : %s\n"
42 "Changes NumLock state in X\n"
43 "( NumLock state detection not available,"
44 " compiled without XKB )\n"
45 #endif
46 "\n"
47 , argv0 );
48 }
49
50 Display* disp;
51
52 #ifdef HAS_XKB
53 int get_numlock_state()
54 {
55 unsigned int states;
56 if( XkbGetIndicatorState( disp, XkbUseCoreKbd, &states) != Success )
57 {
58 printf("Error while reading Indicator status\n");
59 XCloseDisplay( disp );
60 exit( 3 );
61 }
62 return states & 0x02; /* NumLock appears to be bit1 */
63 }
64 #endif
65
66 void change_numlock()
67 {
68 XTestFakeKeyEvent( disp, XKeysymToKeycode( disp, XK_Num_Lock ), True, CurrentTime );
69 XTestFakeKeyEvent( disp, XKeysymToKeycode( disp, XK_Num_Lock ), False, CurrentTime );
70 }
71
72 #ifdef HAS_XKB
73 void set_on()
74 {
75 if( !get_numlock_state())
76 change_numlock();
77 }
78
79 void set_off()
80 {
81 if( get_numlock_state())
82 change_numlock();
83 }
84 #endif
85
86 int main( int argc, char* argv[] )
87 {
88 if( argc > 2 )
89 {
90 usage( argv[ 0 ] );
91 return 1;
92 }
93 disp = XOpenDisplay( NULL );
94 if( disp == NULL )
95 {
96 printf( "Error opening display\n" );
97 return 2;
98 }
99 if( argc == 1 )
100 #if HAS_XKB
101 set_on();
102 else if( strncmp( argv[ 1 ], "on", 2 ) == 0 )
103 set_on();
104 else if( strncmp( argv[ 1 ], "of", 2 ) == 0 )
105 set_off();
106 #else
107 change_numlock(); /* if( argc == 1 ) */
108 #endif
109 else if( strncmp( argv[ 1 ], "switch", 6 ) == 0 )
110 change_numlock();
111 else
112 {
113 usage( argv[ 0 ] );
114 XCloseDisplay( disp );
115 return 1;
116 }
117 XCloseDisplay( disp );
118 return 0;
119 }

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.30