Adding upstream version 0.4.1.
[debian/lxdm.git] / src / numlock.c
1 #include <X11/Xlib.h>
2 #include <X11/XKBlib.h>
3 #include <string.h> // strcmp
4
5 void numlock(Display *c,int onoff)
6 {
7 XkbDescPtr xkb;
8 unsigned int mask;
9 int i;
10 xkb = XkbGetKeyboard(c, XkbAllComponentsMask, XkbUseCoreKbd );
11 if(!xkb) return;
12 if(!xkb->names)
13 {
14 XkbFreeKeyboard(xkb,0,True);
15 return;
16 }
17 for(i = 0; i < XkbNumVirtualMods; i++)
18 {
19 char *s=XGetAtomName( xkb->dpy, xkb->names->vmods[i]);
20 if(!s) continue;
21 if(strcmp(s,"NumLock")) continue;
22 XkbVirtualModsToReal( xkb, 1 << i, &mask );
23 break;
24 }
25 XkbFreeKeyboard( xkb, 0, True );
26 XkbLockModifiers ( c, XkbUseCoreKbd, mask, (onoff?mask:0));
27 }
28
29 int main(int arc,char *arg[])
30 {
31 Display *c;
32 if(arc!=2)
33 return -1;
34 c=XOpenDisplay(0);
35 if(!c)
36 return -1;
37 if(!strcmp(arg[1],"on") || !strcmp(arg[1],"1"))
38 numlock(c,1);
39 else
40 numlock(c,0);
41 XCloseDisplay(c);
42 return 0;
43 }
44