-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkeys.c
195 lines (171 loc) · 5.58 KB
/
xkeys.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/**
* @author Martin Gropp
* @note Contains code from xbindkeys, (C) 2001 by Philippe Brochard
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include "xkeys.h"
static unsigned int numLockMask = 0;
static unsigned int scrollLockMask = 0;
static unsigned int capsLockMask = 0;
/**
* "Find out the masks for the NumLock and ScrollLock modifiers,
* so that we can bind the grabs for when they are enabled too."
*/
void initMasks(Display* display) {
KeyCode nlock = XKeysymToKeycode(display, XK_Num_Lock);
KeyCode slock = XKeysymToKeycode(display, XK_Scroll_Lock);
static int mask_table[8] = {
ShiftMask, LockMask, ControlMask, Mod1Mask,
Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
};
XModifierKeymap* modmap = XGetModifierMapping(display);
if (modmap != NULL && modmap->max_keypermod > 0) {
for (int i = 0; i < 8 * modmap->max_keypermod; i++) {
if (modmap->modifiermap[i] == nlock && nlock != 0) {
numLockMask = mask_table[i / modmap->max_keypermod];
} else if (modmap->modifiermap[i] == slock && slock != 0) {
scrollLockMask = mask_table[i / modmap->max_keypermod];
}
}
}
capsLockMask = LockMask;
if (modmap) {
XFreeModifiermap(modmap);
}
}
/**
* Actually grab a key (and its *Lock combinations).
*/
void grabKey(Display* display, Window rootWindow, int keycode, int modifier) {
XGrabKey(display, keycode, modifier, rootWindow, False, GrabModeAsync, GrabModeAsync);
if (modifier != AnyModifier) {
// Also grab with NumLock, CapsLock, ScrollLock
if (numLockMask) {
XGrabKey(display, keycode, modifier | numLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
if (capsLockMask) {
XGrabKey(display, keycode, modifier | capsLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
if (scrollLockMask) {
XGrabKey(display, keycode, modifier | scrollLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
if (numLockMask && capsLockMask) {
XGrabKey(display, keycode, modifier | numLockMask | capsLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
if (numLockMask && scrollLockMask) {
XGrabKey(display, keycode, modifier | numLockMask | scrollLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
if (capsLockMask && scrollLockMask) {
XGrabKey(display, keycode, modifier | capsLockMask | scrollLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
if (numLockMask && capsLockMask && scrollLockMask) {
XGrabKey(display, keycode, modifier | numLockMask | capsLockMask | scrollLockMask, rootWindow, False, GrabModeAsync, GrabModeAsync);
}
}
}
/**
* Initialize X connection, establish grab.
*/
Display* initX(const char* keyName, int keyModifier, int* keyCode) {
KeySym sym = XStringToKeysym(keyName);
if (sym == NoSymbol) {
fprintf(stderr, "ERROR: Unknown key: %s\n", keyName);
exit(2);
}
Display* display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "ERROR: Could not open display.\n");
exit(3);
}
*keyCode = XKeysymToKeycode(display, sym);
Window rootWindow = DefaultRootWindow(display);
initMasks(display);
Bool detectableAutoRepeatSupported;
XkbSetDetectableAutoRepeat(display, True, &detectableAutoRepeatSupported);
if (!detectableAutoRepeatSupported) {
fprintf(stderr, "ERROR: Detectable auto repeat is not supported.\n");
exit(4);
}
// Grab
grabKey(display, rootWindow, *keyCode, keyModifier);
// Enable events
XAllowEvents(display, AsyncBoth, CurrentTime);
XSelectInput(display, rootWindow, KeyPressMask | KeyReleaseMask);
return display;
}
/**
* Ungrab key and close display.
*/
void cleanupX(Display* display) {
if (display != NULL) {
XUngrabKey(display, AnyKey, AnyModifier, DefaultRootWindow(display));
XCloseDisplay(display);
}
}
/**
* Fake a key release to cancel a blocking XNextEvent.
*/
void fakeKeyRelease(int keyCode, int keyModifier) {
Display* display = XOpenDisplay(NULL);
Window rootWindow = DefaultRootWindow(display);
XKeyEvent event;
memset(&event, 0, sizeof(XKeyEvent));
event.type = KeyRelease;
event.time = CurrentTime;
event.send_event = True;
event.display = display;
event.window = rootWindow;
event.root = rootWindow;
event.state = keyModifier;
event.keycode = keyCode;
event.same_screen = True;
XSendEvent(
display,
rootWindow,
True,
KeyReleaseMask,
(XEvent*)&event
);
XCloseDisplay(display);
}
/**
* Translate a key modifier bitmap to a char*.
*/
char* mod2str(int modifier) {
// data segment is better than dynamic allocation
switch (modifier & (ControlMask | Mod1Mask | ShiftMask)) {
case 0:
return "";
case ControlMask:
return "Ctrl+";
case Mod1Mask:
return "Alt+";
case ShiftMask:
return "Shift+";
case ControlMask | Mod1Mask:
return "Ctrl+Alt+";
case ControlMask | ShiftMask:
return "Ctrl+Shift+";
case Mod1Mask | ShiftMask:
return "Alt+Shift+";
case ControlMask | Mod1Mask | ShiftMask:
return "Ctrl+Alt+Shift+";
default:
return "???";
}
}