-
Notifications
You must be signed in to change notification settings - Fork 1
/
setcapslock.c
90 lines (71 loc) · 1.76 KB
/
setcapslock.c
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
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
void set_modifier(int mod, int on)
{
Display* display = XOpenDisplay(NULL);
XkbLockModifiers(display, XkbUseCoreKbd, mod, on ? mod : 0);
XCloseDisplay(display);
}
int get_modifier(int mod)
{
XkbStateRec xkbState;
Display* display = XOpenDisplay(NULL);
XkbGetState(display, XkbUseCoreKbd, &xkbState);
XCloseDisplay(display);
return xkbState.locked_mods & mod;
}
void usage(const char* program_name)
{
printf("Usage: %s (on|off|toggle|get) [MOD]\n\n", program_name);
}
int main(int argc, char** argv)
{
enum {
SHOW_USAGE,
SETCAPSLOCK_ON,
SETCAPSLOCK_OFF,
SETCAPSLOCK_TOGGLE,
GETCAPSLOCK
};
int mod = LockMask; // see <X11/X.h>
int action = SHOW_USAGE;
char* cmd;
if (argc == 3) {
mod = 1<<atoi(argv[2]);
}
if (argc >= 2) {
cmd = argv[1];
if (strcasecmp(cmd, "on") == 0) {
action = SETCAPSLOCK_ON;
}
else if (strcasecmp(cmd, "off") == 0) {
action = SETCAPSLOCK_OFF;
}
else if (strcasecmp(cmd, "toggle") == 0) {
action = SETCAPSLOCK_TOGGLE;
}
else if (strcasecmp(cmd, "get") == 0) {
action = GETCAPSLOCK;
}
}
if (action == SHOW_USAGE) {
usage(argv[0]);
return 1;
}
if (action == SETCAPSLOCK_ON) {
set_modifier(mod, 1);
}
if (action == SETCAPSLOCK_OFF) {
set_modifier(mod, 0);
}
if (action == SETCAPSLOCK_TOGGLE) {
set_modifier(mod, !get_modifier(mod));
}
if (action == GETCAPSLOCK) {
printf("%s\n", get_modifier(mod) ? "on" : "off");
}
return 0;
}