File: irxevent.cpp

package info (click to toggle)
lirc 0.10.2-0.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,268 kB
  • sloc: ansic: 26,981; cpp: 9,187; sh: 5,875; python: 4,507; makefile: 1,049; xml: 63
file content (742 lines) | stat: -rw-r--r-- 19,880 bytes parent folder | download | duplicates (5)
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/****************************************************************************
** irxevent.c **************************************************************
****************************************************************************
*
* irxevent  - infra-red xevent sender
*
* Heinrich Langos  <heinrich@null.net>
* small modifications by Christoph Bartelmus <lirc@bartelmus.de>
*
* irxevent is based on irexec (Copyright (C) 1998 Trent Piepho)
* and irx.c (no copyright notice found)
*
*  =======
*  HISTORY
*  =======
*
* 0.1
*     -Initial Release
*
* 0.2
*     -no more XWarpPointer... sending Buttonclicks to off-screen
*      applications works becaus i also fake the EnterNotify and LeaveNotify
*     -support for keysymbols rather than characters... so you can use
*      Up or Insert or Control_L ... maybe you could play xquake than :*)
*
* 0.3
*     -bugfix for looking for subwindows of non existing windows
*     -finaly a README file
*
* 0.3a (done by Christoph Bartelmus)
*     -read from a shared .lircrc file
*     -changes to comments
*     (chris, was that all you changed?)
*
* 0.4
*     -fake_timestamp() to solve gqmpeg problems
*     -Shift Control and other mod-keys may work. (can't check it right now)
*      try ctrl-c or shift-Page_up or whatever ...
*      modifiers: shift, caps, ctrl, alt, meta, numlock, mod3, mod4, scrlock
*     -size of 'char *keyname' changed from 64 to 128 to allow all mod-keys.
*     -updated irxevent.README
*
* 0.4.1
*     -started to make smaller version steps :-)
*     -Use "CurrentWindow" as window name to send events to the window
*      that -you guessed it- currently has the focus.
*
* 0.4.2
*     -fixed a stupid string bug in key sending.
*     -updated irxevent.README to be up to date with the .lircrc format.
*
* 0.4.3
*     -changed DEBUG functions to actually produce some output :)
*
* 0.5.0
*     -fixed finding subwindows recursively
*     -added xy_Key (though xterm and xemacs still don't like me)
*     -added compilation patch from Ben Hochstedler
*      <benh@eeyore.moneng.mei.com> for compiling on systems
*       without strsep() (like some solaris)
*
*
* see http://www.wh9.tu-dresden.de/~heinrich/lirc/irxevent/irxevent.keys
* for a the key names. (this one is for you Pablo :-) )
*
* for more information see the irxevent.README file
*
*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/time.h>
#include <unistd.h>

#include "lirc_client.h"
#include "lirc_log.h"

static const logchannel_t logchannel = LOG_APP;

static int bDaemon = 0;
static int bInError = 0;

struct keymodlist_t {
	const char* name;
	Mask mask;
};
static struct keymodlist_t keymodlist[] = {
	{ "SHIFT",   ShiftMask	 },
	{ "CAPS",    LockMask	 },
	{ "CTRL",    ControlMask },
	{ "ALT",     Mod1Mask	 },
	{ "META",    Mod1Mask	 },
	{ "NUMLOCK", Mod2Mask	 },
	{ "MOD3",    Mod3Mask	 },     /* I don't have a clue what key maps to this. */
	{ "MOD4",    Mod4Mask	 },     /* I don't have a clue what key maps to this. */
	{ "MOD5",    Mod5Mask	 },
	{ "ALTGR",   Mod5Mask	 },
	{ "SCRLOCK", Mod5Mask	 },
	{ NULL,	     0		 },
};

static const char* key_delimiter = "-";
static const char* active_window_name = "CurrentWindow";
static const char* root_window_name = "RootWindow";

static const char* prog = "irxevent";
static Display* dpy;
static Window root;
static XEvent xev;
static Window subw;

static Time fake_timestamp(void)
/*seems that xfree86 computes the timestamps like this     */
/*strange but it relies on the *1000-32bit-wrap-around     */
/*if anybody knows exactly how to do it, please contact me */
{
	int tint;
	struct timeval tv;
	struct timezone tz;     /* is not used since ages */

	gettimeofday(&tv, &tz);
	tint = (int) tv.tv_sec * 1000;
	tint = tint / 1000 * 1000;
	tint = tint + tv.tv_usec / 1000;
	return (Time)tint;
}

static Window find_window(Window top, char* name)
{
	char* wname;
	char* iname;
	XClassHint xch;
	Window* children;
	Window foo;
	int revert_to_return;
	unsigned int nc;

	if (!strcmp(active_window_name, name)) {
		XGetInputFocus(dpy, &foo, &revert_to_return);
		return foo;
	} else if (!strcmp(root_window_name, name)) {
		return root;
	}
	/* First the base case */
	if (XFetchName(dpy, top, &wname)) {
		if (!strncmp(wname, name, strlen(name))) {
			XFree(wname);
			log_debug("found it by wname 0x%x\n", top);
			return top;     /* found it! */
		}
		;
		XFree(wname);
	}
	;

	if (XGetIconName(dpy, top, &iname)) {
		if (!strncmp(iname, name, strlen(name))) {
			XFree(iname);
			log_debug("found it by iname 0x%x\n", top);
			return top;     /* found it! */
		}
		;
		XFree(iname);
	}
	;

	if (XGetClassHint(dpy, top, &xch)) {
		if (!strcmp(xch.res_class, name)) {
			XFree(xch.res_name);
			XFree(xch.res_class);
			log_debug("res_class '%s' res_name '%s' 0x%x\n",
				  xch.res_class, xch.res_name, top);
			return top;     /* found it! */
		}
		;
		if (!strcmp(xch.res_name, name)) {
			XFree(xch.res_name);
			XFree(xch.res_class);
			log_debug("res_class '%s' res_name '%s' 0x%x\n",
				  xch.res_class, xch.res_name, top);
			return top;     /* found it! */
		}
		;
		XFree(xch.res_name);
		XFree(xch.res_class);
	}
	;

	if (!XQueryTree(dpy, top, &foo, &foo, &children, &nc) || children == NULL)
		return 0;       /* no more windows here */
	;

	/* check all the sub windows */
	for (; nc > 0; nc--) {
		top = find_window(children[nc - 1], name);
		if (top)
			break;  /* we found it somewhere */
	}
	;
	if (children != NULL)
		XFree(children);
	return top;
}

static Window find_sub_sub_window(Window top, int* x, int* y)
{
	Window base;
	Window* children;
	Window foo;
	Window target = 0;
	int rel_x, rel_y, new_x = 1, new_y = 1;
	int targetsize = 1000000;
	unsigned int nc, width, height, border, depth;

	base = top;
	if (!base)
		return base;
	;
	if (!XQueryTree(dpy, base, &foo, &foo, &children, &nc) || children == NULL)
		return base;    /* no more windows here */
	;
	log_debug("found subwindows %d\n", nc);

	/* check if we hit a sub window and find the smallest one */
	for (; nc > 0; nc--) {
		if (XGetGeometry(dpy, children[nc - 1],
				 &foo, &rel_x, &rel_y, &width, &height, &border, &depth)) {
			if ((rel_x <= *x)
			    && (*x <= (int) (rel_x + width))
			    && (rel_y <= *y) && (*y <= (int) (rel_y + height))) {
				log_debug("found a subwindow 0x%x +%d +%d  %d x %d\n",
					  children[nc - 1], rel_x,
					  rel_y, width, height);
				if ((int) (width * height) < targetsize) {
					target = children[nc - 1];
					targetsize = width * height;
					new_x = *x - rel_x;
					new_y = *y - rel_y;
					/*bull's eye ... */
					target = find_sub_sub_window(target,
								     &new_x,
								     &new_y);
				}
			}
		}
	}
	;
	if (children != NULL)
		XFree(children);
	if (target) {
		*x = new_x;
		*y = new_y;
		return target;
	} else {
		return base;
	}
}

static Window find_sub_window(Window top, char* name, int* x, int* y)
{
	Window base;
	Window* children;
	Window foo;
	Window target = 0;
	int rel_x, rel_y, new_x = 1, new_y = 1;
	unsigned int nc, width, height, border, depth, targetsize = 1000000;

	base = find_window(top, name);
	if (!base)
		return base;
	;
	if (!XQueryTree(dpy, base, &foo, &foo, &children, &nc) || children == NULL)
		return base;    /* no more windows here */
	;
	log_debug("found subwindows %d\n", nc);

	/* check if we hit a sub window and find the smallest one */
	for (; nc > 0; nc--) {
		if (XGetGeometry(dpy, children[nc - 1],
				 &foo, &rel_x, &rel_y,
				 &width, &height, &border, &depth)) {
			if ((rel_x <= *x) && (*x <= (int) (rel_x + width))
			    && (rel_y <= *y) && (*y <= (int) (rel_y + height))) {
				log_debug("found a subwindow 0x%x +%d +%d  %d x %d\n",
					  children[nc - 1], rel_x,
					  rel_y, width, height);
				if ((width * height) < targetsize) {
					target = children[nc - 1];
					targetsize = width * height;
					new_x = *x - rel_x;
					new_y = *y - rel_y;
					/*bull's eye ... */
					target = find_sub_sub_window(target, &new_x, &new_y);
				}
			}
		}
	}
	;
	if (children != NULL)
		XFree(children);
	if (target) {
		*x = new_x;
		*y = new_y;
		return target;
	} else {
		return base;
	}
}

static Window find_window_focused(Window top, char* name)
{
	int tmp;
	Window w;
	Window cur;
	Window* children;
	Window foo;
	unsigned int n;

	/* return the currently focused window if it is a direct match or a
	 * subwindow of the named window */

	w = find_window(top, name);
	if (w) {
		XGetInputFocus(dpy, &cur, &tmp);
		log_debug("current window: 0x%x named window: 0x%x\n", cur, w);

		if (w == cur) {
			/* window matched */
			return cur;
		} else if (XQueryTree(dpy, w, &foo, &foo, &children, &n) && children != NULL) {
			/* check all the sub windows of named window */
			for (; n > 0; n--) {
				if (children[n - 1] == cur) {
					XFree(children);
					return cur;
				}
			}
			XFree(children);
		}
	}

	return 0;
}

static void make_button(int button, int x, int y, XButtonEvent* xev)
{
	xev->type = ButtonPress;
	xev->display = dpy;
	xev->root = root;
	xev->subwindow = None;
	xev->time = fake_timestamp();
	xev->x = x;
	xev->y = y;
	xev->x_root = 1;
	xev->y_root = 1;
	xev->state = 0;
	xev->button = button;
	xev->same_screen = True;
}

static void make_key(char* keyname, int x, int y, XKeyEvent* xev)
{
	char* part;
	char* part2;
	char* sep_part;
	struct keymodlist_t* kmlptr;
	KeySym ks;
	KeyCode kc;

	part2 = (char*) malloc(128);

	xev->type = KeyPress;
	xev->display = dpy;
	xev->root = root;
	xev->subwindow = None;
	xev->time = fake_timestamp();
	xev->x = x;
	xev->y = y;
	xev->x_root = 1;
	xev->y_root = 1;
	xev->same_screen = True;

	xev->state = 0;
#ifdef HAVE_STRSEP
	while ((part = strsep(&keyname, key_delimiter)))
#else
	while ((part = strtok(keyname, key_delimiter)) && ((keyname = NULL) == NULL))
#endif
	{
		part2 = strncpy(part2, part, 128);
		//      log_debug("-   %s\n",part);
		kmlptr = keymodlist;
		while (kmlptr->name) {
			//      log_debug("--  %s %s\n", kmlptr->name, part);
			if (!strcasecmp(kmlptr->name, part))
				xev->state |= kmlptr->mask;
			kmlptr++;
		}
		//      log_debug("--- %s\n",part);
	}
	//  log_debug("*** %s\n",part);
	//  log_debug("*** %s\n",part2);

	/*
	 * New code 14-June-2005 by Warren Melnick, C.A.C. Media
	 * Uses the KeySym: and KeyCode: prefixes on the Key lines to allow for
	 * numeric keysym and keycode numbers to be used in place of X keysyms.
	 * Example 1: config = Key KeyCode:127 CurrentWindow
	 * Example 2: config = Key KeySym:0xFFF0 CurrentWindow
	 */
	ks = 0;
	kc = 0;
	if (strncmp(part2, "KeySym:", 7) == 0) {
		sep_part = part2 + 7;
		ks = strtoul(sep_part, NULL, 0);
		kc = XKeysymToKeycode(dpy, ks);
		log_debug("KeySym String: %s, KeySym: %ld KeyCode: %d\n", part2, ks, kc);
	} else if (strncmp(part2, "KeyCode:", 8) == 0) {
		sep_part = part2 + 8;
		kc = (KeyCode)strtoul(sep_part, NULL, 0);
		log_debug("KeyCode String: %s, KeyCode: %d\n", part2, kc);
	}
	if ((ks == 0) && (kc == 0)) {
		ks = XStringToKeysym(part2);
		kc = XKeysymToKeycode(dpy, ks);
		log_debug("Unmodified String: %s, KeySym: %d KeyCode: %d\n", part2, ks, kc);
	}
	xev->keycode = kc;
	log_debug("state 0x%x, keycode 0x%x\n", xev->state, xev->keycode);
	free(part2);
}

static void sendfocus(Window w, int in_out)
{
	XFocusChangeEvent focev;

	focev.display = dpy;
	focev.type = in_out;
	focev.window = w;
	focev.mode = NotifyNormal;
	focev.detail = NotifyPointer;
	XSendEvent(dpy, w, True, FocusChangeMask, (XEvent*)&focev);
	XSync(dpy, True);
}

static void sendpointer_enter_or_leave(Window w, int in_out)
{
	XCrossingEvent crossev;

	crossev.type = in_out;
	crossev.display = dpy;
	crossev.window = w;
	crossev.root = root;
	crossev.subwindow = None;
	crossev.time = fake_timestamp();
	crossev.x = 1;
	crossev.y = 1;
	crossev.x_root = 1;
	crossev.y_root = 1;
	crossev.mode = NotifyNormal;
	crossev.detail = NotifyNonlinear;
	crossev.same_screen = True;
	crossev.focus = True;
	crossev.state = 0;
	XSendEvent(dpy, w, True, EnterWindowMask | LeaveWindowMask, (XEvent*)&crossev);
	XSync(dpy, True);
}

static void sendkey(char* keyname, int x, int y, Window w, Window s)
{
	make_key(keyname, x, y, (XKeyEvent*)&xev);
	xev.xkey.window = w;
	xev.xkey.subwindow = s;

	if (s)
		sendfocus(s, FocusIn);

	XSendEvent(dpy, w, True, KeyPressMask, &xev);
	XFlush(dpy);

	xev.type = KeyRelease;
	usleep(20000);
	xev.xkey.time = fake_timestamp();
	if (s)
		sendfocus(s, FocusOut);
	XSendEvent(dpy, w, True, KeyReleaseMask, &xev);
	XSync(dpy, True);
}

static void sendbutton(int button, int x, int y, Window w, Window s)
{
	make_button(button, x, y, (XButtonEvent*)&xev);
	xev.xbutton.window = w;
	xev.xbutton.subwindow = s;
	sendpointer_enter_or_leave(w, EnterNotify);
	sendpointer_enter_or_leave(s, EnterNotify);

	XSendEvent(dpy, w, True, ButtonPressMask, &xev);
	XSync(dpy, True);
	xev.type = ButtonRelease;
	xev.xkey.state |= 0x100;
	usleep(1000);
	xev.xkey.time = fake_timestamp();
	XSendEvent(dpy, w, True, ButtonReleaseMask, &xev);
	sendpointer_enter_or_leave(s, LeaveNotify);
	sendpointer_enter_or_leave(w, LeaveNotify);
	XSync(dpy, True);
}

int errorHandler(Display* di, XErrorEvent* ev)
{
	char buff[512];

	buff[0] = 0;
	if (bInError || ev == NULL || di == NULL)
		return 1;       // only 1 msg per key
	XGetErrorText(di, ev->error_code, buff, sizeof(buff) - 1);
	if (buff[0]) {
		if (!bDaemon)
			fprintf(stderr, "X11 error: %s\n", buff);
		bInError = 1;
	}
	return 1;
}

int check(char* s)
{
	int d;
	char* buffer;

	buffer = (char*) malloc(strlen(s) + 1);
	if (buffer == NULL) {
		fprintf(stderr, "%s: out of memory\n", prog);
		return -1;
	}

	if (2 != sscanf(s, "Key %s Focus %s %s", buffer, buffer, buffer) &&
	    2 != sscanf(s, "Key %s WindowID %i %s", buffer, &d, buffer) &&
	    2 != sscanf(s, "Key %s Focus WindowID %i %s", buffer, &d, buffer) &&
	    2 != sscanf(s, "Key %s %s %s", buffer, buffer, buffer) &&
	    4 != sscanf(s, "Button %d %d %d Focus %s %s", &d, &d, &d, buffer, buffer) &&
	    4 != sscanf(s, "Button %d %d %d WindowID %i %s", &d, &d, &d, &d, buffer) &&
	    4 != sscanf(s, "Button %d %d %d Focus WindowID %i %s", &d, &d, &d, &d, buffer) &&
	    4 != sscanf(s, "Button %d %d %d %s %s", &d, &d, &d, buffer, buffer) &&
	    4 != sscanf(s, "xy_Key %d %d %s Focus %s %s", &d, &d, buffer, buffer, buffer) &&
	    4 != sscanf(s, "xy_Key %d %d %s WindowID %i %s", &d, &d, buffer, &d, buffer) &&
	    4 != sscanf(s, "xy_Key %d %d %s Focus WindowID %i %s", &d, &d, buffer, &d, buffer) &&
	    4 != sscanf(s, "xy_Key %d %d %s %s", &d, &d, buffer, buffer)) {
		fprintf(stderr, "%s: bad config string \"%s\"\n", prog, s);
		free(buffer);
		return -1;
	}
	free(buffer);
	return 0;
}

static struct option long_options[] = {
	{ "daemon",  no_argument, NULL, 'd' },
	{ "help",    no_argument, NULL, 'h' },
	{ "version", no_argument, NULL, 'V' },
	{ 0,	     0,		  0,	0   }
};

int main(int argc, char* argv[])
{
	char keyname[128];
	int pointer_button, pointer_x, pointer_y;
	char windowname[64];
	struct lirc_config* config;
	char* config_file = NULL;
	int c;
	unsigned int WindowID;

	while ((c = getopt_long(argc, argv, "dhV", long_options, NULL)) != EOF) {
		switch (c) {
		case 'd':
			bDaemon = 1;
			continue;
		case 'h':
			printf("Usage: %s [option]... [config file]\n"
			       "       -d --daemon     fork and run in background\n"
			       "       -h --help       display usage summary\n"
			       "       -V --version    display version\n", prog);
			return EXIT_SUCCESS;
		case 'V':
			printf("%s %s\n", prog, VERSION);
			return EXIT_SUCCESS;
		case '?':
			fprintf(stderr, "unrecognized option: -%c\n", optopt);
			fprintf(stderr, "Try `%s --help' for more information.\n", prog);
			return EXIT_FAILURE;
		}
	}

	if (argc == optind + 1) {
		config_file = argv[optind];
	} else if (argc > optind + 1) {
		fprintf(stderr, "%s: incorrect number of arguments.\n", prog);
		fprintf(stderr, "Try `%s --help' for more information.\n", prog);
		return EXIT_FAILURE;
	}

	dpy = XOpenDisplay(NULL);
	if (dpy == NULL) {
		fprintf(stderr, "Can't open DISPLAY.\n");
		exit(1);
	}
	root = RootWindow(dpy, DefaultScreen(dpy));

	// windows may get closed at wrong time. Override default error handler...
	XSetErrorHandler(errorHandler);

	if (lirc_init("irxevent", 1) == -1)
		exit(EXIT_FAILURE);

	if (lirc_readconfig(config_file, &config, check) == 0) {
		char* ir;
		char* c;
		int ret;

		if (bDaemon) {
			if (daemon(1, 0) < 0) {
				perror("Failed to run as daemon");
				exit(EXIT_FAILURE);
			}
		}

		while (lirc_nextcode(&ir) == 0) {
			if (ir == NULL)
				continue;
			while ((ret = lirc_code2char(config, ir, &c)) == 0 && c != NULL) {
				log_debug("Received code: %s Sending:\n", ir);
				bInError = 0;   // reset error state, want to see error msg

				*windowname = 0;
				if (2 == sscanf(c, "Key %s Focus WindowID %i", keyname, &WindowID) ||
				    4 == sscanf(c, "Button %d %d %d Focus WindowID %i", &pointer_button, &pointer_x,
						&pointer_y, &WindowID)
				    || 4 == sscanf(c, "xy_Key %d %d %s Focus WindowID %i", &pointer_x, &pointer_y,
						   keyname, &WindowID)
				    || 2 == sscanf(c, "Key %s Focus %s", keyname, windowname)
				    || 4 == sscanf(c, "Button %d %d %d Focus %s", &pointer_button, &pointer_x,
						   &pointer_y, windowname)
				    || 4 == sscanf(c, "xy_Key %d %d %s Focus %s", &pointer_x, &pointer_y, keyname,
						   windowname)) {
					log_debug("Focus\n");
					/* focussed ? */
					if (*windowname) {
						WindowID = find_window_focused(root, windowname);
						if (!WindowID) {
							log_debug("target window '%s' doesn't have focus\n",
								  windowname);
							continue;
						}
						log_debug("focused:  %s\n", windowname);
					} else {
						Window cur;
						int tmp;

						XGetInputFocus(dpy, &cur, &tmp);
						if (WindowID != cur) {
							log_debug("target window '0x%x' doesn't have focus\n",
								  WindowID);
							continue;
						}
						log_debug("focused:  0x%x\n", WindowID);
					}
				} else if (2 == sscanf(c, "Key %s WindowID %i", keyname, &WindowID) ||
					   4 == sscanf(c, "Button %d %d %d WindowID %i", &pointer_button, &pointer_x,
						       &pointer_y, &WindowID)
					   || 4 == sscanf(c, "xy_Key %d %d %s WindowID %i", &pointer_x, &pointer_y,
							  keyname, &WindowID)) {
					log_debug("WindowID:  0x%x\n", WindowID);
					/* WindowID passed */
				} else if (2 == sscanf(c, "Key %s %s", keyname, windowname) ||
					   4 == sscanf(c, "Button %d %d %d %s", &pointer_button, &pointer_x, &pointer_y,
						       windowname)
					   || 4 == sscanf(c, "xy_Key %d %d %s %s\n", &pointer_x, &pointer_y, keyname,
							  windowname)) {
					log_debug("name: %s\n", windowname);
					WindowID = find_window(root, windowname);
					if (WindowID == 0) {
						log_debug("target window '%s' not found\n", windowname);
						continue;
					}
				}

				switch (c[0]) {
				case 'K':       // Key
					log_debug("keyname: %s \t WindowID: 0x%x\n", keyname, WindowID);
					log_debug("%s\n", c);
					sendkey(keyname, 1, 1, (Window)WindowID, 0);
					break;

				case 'B':       // Button
				case 'x':       // xy_Key
					subw = find_sub_window(root, windowname, &pointer_x, &pointer_y);
					if (subw) {
						if (WindowID == subw)
							subw = 0;
						log_debug("%s\n", c);
						switch (c[0]) {
						case 'B':
							/* FIXME: pointer_button potentially uninititalzed. */
							sendbutton(pointer_button, pointer_x, pointer_y, WindowID,
								   subw);
							break;
						case 'x':
							sendkey(keyname, pointer_x, pointer_y, WindowID, subw);
							break;
						}
					}
					break;
				}
			}
			free(ir);
			if (ret == -1)
				break;
		}
		lirc_freeconfig(config);
	}

	lirc_deinit();

	exit(0);
}