File: JXGrabKey.cpp

package info (click to toggle)
jxgrabkey 0.3.2-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 388 kB
  • sloc: java: 1,558; xml: 850; cpp: 425; makefile: 47
file content (493 lines) | stat: -rw-r--r-- 13,725 bytes parent folder | download | duplicates (4)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*	Copyright 2008  Edwin Stang (edwinstang@gmail.com), 
 *
 *  This file is part of JXGrabKey.
 *
 *  JXGrabKey is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  JXGrabKey is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with JXGrabKey.  If not, see <http://www.gnu.org/licenses/>.
 */

#define SLEEP_TIME_MS 100
#define MICROSECOND_TO_MILLISECOND_MULTIPLICATOR 1000

#include "JXGrabKey.h"

#include <X11/Xlib.h>
#include <X11/keysym.h>

#include <pthread.h>
#include <streambuf>
#include <iostream>
#include <sstream>
#include <vector>
#include <unistd.h>

using namespace std;

Display *dpy = NULL;
vector<KeyStruct> keys;
bool debug = false;
bool isListening = false;
bool errorInListen = false;
bool doListen = true;
bool registerHotkeyIsWaitingForException = false;
bool registerHotkeyHasException = false;

pthread_spinlock_t x_lock;

unsigned int numlock_mask = 0;
unsigned int scrolllock_mask = 0;
unsigned int capslock_mask = 0;

JNIEXPORT void JNICALL Java_jxgrabkey_JXGrabKey_clean(JNIEnv *_env,
		jobject _obj) {

	while (!isListening && !errorInListen) {
		if (debug) {
			ostringstream sout;
			sout << "clean() - sleeping " << std::dec << SLEEP_TIME_MS
					<< " ms for listen() to be ready";
			printToDebugCallback(_env, sout.str().c_str());
		}
		usleep(SLEEP_TIME_MS * MICROSECOND_TO_MILLISECOND_MULTIPLICATOR);
	}
	if (errorInListen) {
		if (debug) {
			ostringstream sout;
			sout << "clean() - WARNING: aborting because of error in listen()";
			printToDebugCallback(_env, sout.str().c_str());
		}
		return;
	}

	pthread_spin_lock(&x_lock);
	for (int i = 0; i < keys.size(); i++) {
		Java_jxgrabkey_JXGrabKey_unregisterHotKey(_env, _obj, keys.at(i).id);
	}
	pthread_spin_unlock(&x_lock);

	doListen = false;
}

JNIEXPORT void JNICALL Java_jxgrabkey_JXGrabKey_registerHotkey__III(
		JNIEnv *_env, jobject _obj, jint _id, jint _mask, jint _key) {

	if (debug) {
		ostringstream sout;
		sout << "++ registerHotkey(" << std::dec << _id << ", 0x" << std::hex
				<< _mask << ", 0x" << std::hex << _key << ")";
		printToDebugCallback(_env, sout.str().c_str());
	}

	while (!isListening && !errorInListen) {
		if (debug) {
			ostringstream sout;
			sout << "registerHotkey() - sleeping " << std::dec << SLEEP_TIME_MS
					<< " ms for listen() to be ready";
			printToDebugCallback(_env, sout.str().c_str());
		}
		usleep(SLEEP_TIME_MS * MICROSECOND_TO_MILLISECOND_MULTIPLICATOR);
	}

	if (errorInListen) {
		if (debug) {
			ostringstream sout;
			sout
					<< "registerHotkey() - WARNING: aborting because of error in listen(): errorInListen = "
					<< std::dec << errorInListen;
			printToDebugCallback(_env, sout.str().c_str());
		}
		return;
	}

	pthread_spin_lock(&x_lock);
	for (int i = 0; i < keys.size(); i++) {
		if (keys[i].id == _id) {
			Java_jxgrabkey_JXGrabKey_unregisterHotKey(_env, _obj, _id);
			break;
		}
	}

	struct KeyStruct key;
	key.id = _id;
	key.key = XKeysymToKeycode(dpy, _key);
	key.mask = _mask;
	keys.push_back(key);
	pthread_spin_unlock(&x_lock);

	if (debug) {
		ostringstream sout;

		sout << "registerHotkey() - found in X11 mask (0x" << std::hex
				<< key.mask << "): ";

		if (key.mask & ShiftMask) {
			sout << "'Shift' ";
		}
		if (key.mask & LockMask) {
			sout << "'Lock' ";
		}
		if (key.mask & ControlMask) {
			sout << "'Control' ";
		}
		if (key.mask & Mod1Mask) {
			sout << "'Mod1' ";
		}
		if (key.mask & Mod2Mask) {
			sout << "'Mod2' ";
		}
		if (key.mask & Mod3Mask) {
			sout << "'Mod3' ";
		}
		if (key.mask & Mod4Mask) {
			sout << "'Mod4' ";
		}
		if (key.mask & Mod5Mask) {
			sout << "'Mod5' ";
		}

		sout << endl;
		sout << "registerHotkey() - converted X11 keysym '" << XKeysymToString(
				_key) << "' (0x" << std::hex << _key << ") to X11 key (0x"
				<< std::hex << (int) key.key << ")";

		printToDebugCallback(_env, sout.str().c_str());
	}

	grabKey(_env, key);

	//Flush X to receive errors
	registerHotkeyIsWaitingForException = true;
	XSync(dpy, false);
	if (registerHotkeyHasException) {
		ostringstream message;
		message << "Unable to register hotkey with id = " << _id
				<< ". Each hotkey combination can only be grabbed by one application at a time!";

		jclass cls = _env->FindClass("jxgrabkey/HotkeyConflictException");
		if (cls != NULL) {
			_env->ThrowNew(cls, message.str().c_str());
			//Don't print anything to console until function return,
			//or else java will not register the exception!
		} else {
			if (debug) {
				ostringstream sout;
				sout
						<< "registerHotkey() - WARNING: Unable to throw HotkeyConflictException, class not found!";
				sout << " - " << message.str().c_str();
				printToDebugCallback(NULL, sout.str().c_str());
			}
		}
	}
	registerHotkeyIsWaitingForException = false;
	if (registerHotkeyHasException) {
		registerHotkeyHasException = false;
		return;
	}

	if (debug) {
		ostringstream sout;
		sout << "-- registerHotkey()";
		printToDebugCallback(_env, sout.str().c_str());
	}
}

JNIEXPORT void JNICALL Java_jxgrabkey_JXGrabKey_unregisterHotKey(JNIEnv *_env,
		jobject _obj, jint _id) {

	if (debug) {
		ostringstream sout;
		sout << "++ unregisterHotkey(" << std::dec << _id << ")";
		printToDebugCallback(_env, sout.str().c_str());
	}

	while (!isListening && !errorInListen) {
		if (debug) {
			ostringstream sout;
			sout << "unregisterHotkey() - sleeping " << std::dec
					<< SLEEP_TIME_MS << " ms for listen() to be ready";
			printToDebugCallback(_env, sout.str().c_str());
		}
		usleep(SLEEP_TIME_MS * MICROSECOND_TO_MILLISECOND_MULTIPLICATOR);
	}
	if (errorInListen) {
		if (debug) {
			ostringstream sout;
			sout
					<< "unregisterHotkey() - WARNING: aborting because of error in listen()";
			printToDebugCallback(_env, sout.str().c_str());
		}
		return;
	}

	pthread_spin_lock(&x_lock);
	for (int i = 0; i < keys.size(); i++) {
		if (keys.at(i).id == _id) {
			ungrabKey(_env, keys.at(i));
			keys.erase(keys.begin() + i);
			break;
		}
	}
	pthread_spin_unlock(&x_lock);

	if (debug) {
		ostringstream sout;
		sout << "-- unregisterHotkey()";
		printToDebugCallback(_env, sout.str().c_str());
	}
}

JNIEXPORT void JNICALL Java_jxgrabkey_JXGrabKey_listen(JNIEnv *_env,
		jobject _obj) {

	if (debug) {
		ostringstream sout;
		sout << "++ listen()";
		printToDebugCallback(_env, sout.str().c_str());
	}

	if (isListening) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - WARNING: already listening, aborting";
			printToDebugCallback(_env, sout.str().c_str());
		}
		return;
	}

	jclass cls = _env->FindClass("jxgrabkey/JXGrabKey");
	if (cls == NULL) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - ERROR: cannot find class jxgrabkey.JXGrabKey";
			printToDebugCallback(_env, sout.str().c_str());
		}
		errorInListen = true;
		return;
	}

	jmethodID mid = _env->GetStaticMethodID(cls, "fireKeyEvent", "(I)V");
	if (mid == NULL) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - ERROR: cannot find method fireKeyEvent(int)";
			printToDebugCallback(_env, sout.str().c_str());
		}
		errorInListen = true;
		return;
	}

	dpy = XOpenDisplay(NULL);

	if (!dpy) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - ERROR: cannot open display " << XDisplayName(
					NULL);
			printToDebugCallback(_env, sout.str().c_str());
		}
		errorInListen = true;
		return;
	}

	getOffendingModifiers(dpy);

	int ret = XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
	if (debug && !ret) {
		ostringstream sout;
		sout << "listen() - WARNING: XAllowEvents() returned false";
		printToDebugCallback(_env, sout.str().c_str());
	}

	for (int screen = 0; screen < ScreenCount(dpy); screen++) {
		ret = XSelectInput(dpy, RootWindow(dpy, screen), KeyPressMask);
		if (debug && !ret) {
			ostringstream sout;
			sout << "listen() - WARNING: XSelectInput() on screen " << std::dec
					<< screen << " returned false";
			printToDebugCallback(_env, sout.str().c_str());
		}
	}

	XSetErrorHandler((XErrorHandler) xErrorHandler);
	pthread_spin_init(&x_lock, NULL); // init here bcoz of the returns

	doListen = true;
	isListening = true;

	XEvent ev;

	while (doListen) {

		while (!XPending(dpy) && doListen) { //Don't block on XNextEvent(), this breaks XGrabKey()!
			usleep(SLEEP_TIME_MS * MICROSECOND_TO_MILLISECOND_MULTIPLICATOR);
		}

		if (doListen) {
			XNextEvent(dpy, &ev);

			if (ev.type == KeyPress) {
				pthread_spin_lock(&x_lock);
				for (int i = 0; i < keys.size(); i++) {
					ev.xkey.state &= ~(numlock_mask | capslock_mask
							| scrolllock_mask); //Filter offending modifiers
					if (ev.xkey.keycode == keys.at(i).key && ev.xkey.state
							== keys.at(i).mask) {
						if (debug) {
							ostringstream sout;
							sout << "listen() - received: id = " << std::dec
									<< keys.at(i).id
									<< "; type = KeyPress; x11Keycode = '"
									<< XKeysymToString(XKeycodeToKeysym(dpy,
											ev.xkey.keycode, 0)) << "' (0x"
									<< std::hex << ev.xkey.keycode
									<< "); x11Mask = 0x" << std::hex
									<< ev.xkey.state << endl;
							printToDebugCallback(_env, sout.str().c_str());
						}
						_env->CallStaticVoidMethod(cls, mid, keys.at(i).id);
						break;
					}
				}
				pthread_spin_unlock(&x_lock);
			}
		}
	}

	isListening = false;

	pthread_spin_destroy(&x_lock);
	XCloseDisplay(dpy);
}

JNIEXPORT void JNICALL Java_jxgrabkey_JXGrabKey_setDebug(JNIEnv *_env,
		jobject _obj, jboolean _debug) {
	debug = _debug;
}

void getOffendingModifiers(Display* _dpy) {

	//Took this code from project xbindkeys
	int i;
	XModifierKeymap *modmap;
	KeyCode nlock, slock;
	static int mask_table[8] = { ShiftMask, LockMask, ControlMask, Mod1Mask,
			Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask };

	nlock = XKeysymToKeycode(_dpy, XK_Num_Lock);
	slock = XKeysymToKeycode(_dpy, XK_Scroll_Lock);

	modmap = XGetModifierMapping(_dpy);

	if (modmap != NULL && modmap->max_keypermod > 0) {
		for (i = 0; i < 8 * modmap->max_keypermod; i++) {
			if (modmap->modifiermap[i] == nlock && nlock != 0)
				numlock_mask = mask_table[i / modmap->max_keypermod];
			else if (modmap->modifiermap[i] == slock && slock != 0)
				scrolllock_mask = mask_table[i / modmap->max_keypermod];
		}
	}

	capslock_mask = LockMask;

	if (modmap)
		XFreeModifiermap(modmap);
}

void printToDebugCallback(JNIEnv *_env, const char* message) {
	if (debug) {
		static JNIEnv *env = _env;
		if (env != NULL) {
			static jclass cls = env->FindClass("jxgrabkey/JXGrabKey");
			static jmethodID mid = env->GetStaticMethodID(cls, "debugCallback",
					"(Ljava/lang/String;)V");

			if (mid != NULL) {
				env->CallStaticVoidMethod(cls, mid, env->NewStringUTF(message));
			} else {
				cout << "JAVA DEBUG CALLBACK NOT FOUND - " << message << endl;
				fflush(stdout);
			}
		} else {
			cout << "JAVA DEBUG CALLBACK NOT INITIALIZED - " << message << endl;
			fflush(stdout);
		}
	}
}

static int *xErrorHandler(Display *_dpy, XErrorEvent *_event) {
	if (registerHotkeyIsWaitingForException) {
		//The exception has to be thrown in registerHotkey,
		//coz this functions runs in a different thread!
		registerHotkeyHasException = true;
	}

	if (debug) {
		ostringstream sout;
		sout << "xErrorHandler() - Caught error: serial = " << std::dec
				<< _event->serial;
		sout << "; resourceid = " << std::dec << _event->resourceid;
		sout << "; type = " << std::dec << _event->type;
		sout << "; error_code = " << std::dec << (int) _event->error_code;
		sout << "; request_code = " << std::dec << (int) _event->request_code;
		sout << "; minor_code = " << std::dec << (int) _event->minor_code;
		printToDebugCallback(NULL, sout.str().c_str());
	}

	return NULL;
}

void grabKey(JNIEnv *_env, KeyStruct key) {
	Mask modifierCombinations[] = { key.mask, key.mask | numlock_mask, key.mask
			| scrolllock_mask, key.mask | capslock_mask, key.mask
			| numlock_mask | scrolllock_mask, key.mask | numlock_mask
			| capslock_mask, key.mask | scrolllock_mask | capslock_mask,
			key.mask | numlock_mask | scrolllock_mask | capslock_mask };

	for (int screen = 0; screen < ScreenCount(dpy); screen++) {
		for (int m = 0; m < 8; m++) {
			int ret =
					XGrabKey(dpy, key.key, modifierCombinations[m],
							RootWindow(dpy, screen), True, GrabModeAsync,
							GrabModeAsync);
			if (debug && !ret) {
				ostringstream sout;
				sout << "grabKey() - WARNING: XGrabKey() on screen "
						<< std::dec << screen << " for mask combination "
						<< std::dec << m << " returned false";
				printToDebugCallback(_env, sout.str().c_str());
			}
		}
	}
}

void ungrabKey(JNIEnv *_env, KeyStruct key) {
	Mask modifierCombinations[] = { key.mask, key.mask | numlock_mask, key.mask
			| scrolllock_mask, key.mask | capslock_mask, key.mask
			| numlock_mask | scrolllock_mask, key.mask | numlock_mask
			| capslock_mask, key.mask | scrolllock_mask | capslock_mask,
			key.mask | numlock_mask | scrolllock_mask | capslock_mask };

	for (int screen = 0; screen < ScreenCount(dpy); screen++) {
		for (int m = 0; m < 8; m++) {
			int ret = XUngrabKey(dpy, key.key, modifierCombinations[m],
					RootWindow(dpy, screen));
			if (debug && !ret) {
				ostringstream sout;
				sout << "ungrabKey() - WARNING: XUngrabKey() on screen "
						<< std::dec << screen << " for mask combination "
						<< std::dec << m << " returned false";
				printToDebugCallback(_env, sout.str().c_str());
			}
		}
	}
}