/
alttab.c
635 lines (577 loc) · 19.8 KB
/
alttab.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
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
/*
Parsing options/resources, top-level keygrab functions and main().
Copyright 2017-2021 Alexander Kulak.
This file is part of alttab program.
alttab 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 3 of the License, or
(at your option) any later version.
alttab 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with alttab. If not, see <http://www.gnu.org/licenses/>.
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xft/Xft.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include "alttab.h"
#include "util.h"
#include "config.h"
// PUBLIC
Globals g;
// globals common for alttab, util and icon
Display *dpy;
int scr;
Window root;
// PRIVATE
static XrmDatabase db;
//
// help and exit
//
static void helpexit()
{
msg(-1, "the task switcher, v%s\n\
Options:\n\
-w N window manager: 0=no, 1=ewmh-compatible, 2=ratpoison, 3=old fashion\n\
-d N desktop: 0=current 1=all, 2=all but special, 3=all but current\n\
-sc N screen: 0=current 1=all\n\
-kk str keysym of main key\n\
-mk str keysym of main modifier\n\
-bk str keysym of backscroll modifier\n\
-pk str keysym of 'prev' key\n\
-nk str keysym of 'next' key\n\
-mm N (obsoleted) main modifier mask\n\
-bm N (obsoleted) backward scroll modifier mask\n\
-t NxM tile geometry\n\
-i NxM icon geometry\n\
-vp str switcher viewport: focus, pointer, total, WxH+X+Y\n\
-p str switcher position: center, none, +X+Y\n\
-s N icon source: 0=X11 only, 1=fallback to files, 2=best size, 3=files only\n\
-theme name icon theme\n\
-bg color background color\n\
-fg color foreground color\n\
-frame color active frame color\n\
-font name font name in the form xft:fontconfig_pattern\n\
-v|-vv verbose\n\
-h help\n\
See man alttab for details.\n", PACKAGE_VERSION);
exit(0);
}
//
// initialize globals based on executable agruments and Xresources
// return 1 if success, 0 otherwise
// on fatal failure, calls die/exit
//
static int use_args_and_xrm(int *argc, char **argv)
{
// set debug level early
g.debug = 0;
char *errmsg;
int ksi;
KeyCode BC;
unsigned int wmindex, dsindex, scindex, isrc;
char *gtile, *gicon, *gview, *gpos;
int x, y;
unsigned int w, h;
int xpg;
char *s;
char *rm;
char *empty = "";
int uo;
Atom nwm_prop, atype;
unsigned char *nwm;
int form;
unsigned long remain, len;
XrmOptionDescRec xrmTable[] = {
{"-w", "*windowmanager", XrmoptionSepArg, NULL},
{"-d", "*desktops", XrmoptionSepArg, NULL},
{"-sc", "*screens", XrmoptionSepArg, NULL},
{"-mm", "*modifier.mask", XrmoptionSepArg, NULL},
{"-bm", "*backscroll.mask", XrmoptionSepArg, NULL},
{"-mk", "*modifier.keysym", XrmoptionSepArg, NULL},
{"-kk", "*key.keysym", XrmoptionSepArg, NULL},
{"-bk", "*backscroll.keysym", XrmoptionSepArg, NULL},
{"-pk", "*prevkey.keysym", XrmoptionSepArg, NULL},
{"-nk", "*nextkey.keysym", XrmoptionSepArg, NULL},
{"-ck", "*cancelkey.keysym", XrmoptionSepArg, NULL},
{"-t", "*tile.geometry", XrmoptionSepArg, NULL},
{"-i", "*icon.geometry", XrmoptionSepArg, NULL},
{"-vp", "*viewport", XrmoptionSepArg, NULL},
{"-p", "*position", XrmoptionSepArg, NULL},
{"-s", "*icon.source", XrmoptionSepArg, NULL},
{"-theme", "*theme", XrmoptionSepArg, NULL},
{"-bg", "*background", XrmoptionSepArg, NULL},
{"-fg", "*foreground", XrmoptionSepArg, NULL},
{"-frame", "*framecolor", XrmoptionSepArg, NULL},
{"-font", "*font", XrmoptionSepArg, NULL},
};
const char *inv = "invalid %s, use -h for help\n";
const char *rmb = "can't figure out modmask from keycode 0x%x\n";
// not using getopt() because of need for "-v" before Xrm
int arg;
for (arg = 0; arg < (*argc); arg++) {
if ((strcmp(argv[arg], "-v") == 0)) {
g.debug = 1;
remove_arg(argc, argv, arg);
} else if ((strcmp(argv[arg], "-vv") == 0)) {
g.debug = 2;
remove_arg(argc, argv, arg);
} else if ((strcmp(argv[arg], "-h") == 0)) {
helpexit();
remove_arg(argc, argv, arg);
}
}
msg(0, "%s\n", PACKAGE_STRING);
msg(0, "debug level %d\n", g.debug);
XrmInitialize();
rm = XResourceManagerString(dpy);
msg(1, "resource manager: \"%s\"\n", rm);
if (!rm) {
msg(0, "can't get resource manager, using empty db\n");
//return 0; // we can do it
//db = XrmGetDatabase (dpy);
rm = empty;
}
db = XrmGetStringDatabase(rm);
if (!db) {
msg(-1, "can't get resource database\n");
return 0;
}
XrmParseCommand(&db, xrmTable, sizeof(xrmTable) / sizeof(xrmTable[0]),
XRMAPPNAME, argc, argv);
if ((*argc) > 1) {
g.debug = 1;
msg(-1, "unknown options or wrong arguments:");
for (uo = 1; uo < (*argc); uo++) {
msg(0, " \"%s\"", argv[uo]);
}
msg(0, ", use -h for help\n");
exit(1);
}
switch (xresource_load_int(&db, XRMAPPNAME, "windowmanager", &wmindex)) {
case 1:
if (wmindex >= WM_MIN && wmindex <= WM_MAX) {
g.option_wm = wmindex;
goto wmDone;
} else {
die(inv, "windowmanager argument range");
}
break;
case 0:
msg(0, "no WM index or unknown, guessing\n");
break;
case -1:
die(inv, "windowmanager argument");
break;
}
// EWMH?
if (ewmh_detectFeatures(&(g.ewmh))) {
msg(0, "EWMH-compatible WM detected: %s\n", g.ewmh.wmname);
g.option_wm = WM_EWMH;
goto wmDone;
}
// ratpoison?
nwm_prop = XInternAtom(dpy, "_NET_WM_NAME", false);
if (XGetWindowProperty(dpy, root, nwm_prop, 0, MAXNAMESZ, false,
AnyPropertyType, &atype, &form, &len, &remain,
&nwm) == Success && nwm) {
msg(0, "_NET_WM_NAME root property present: %s\n", nwm);
if (strstr((char *)nwm, "ratpoison") != NULL) {
g.option_wm = WM_RATPOISON;
XFree(nwm);
goto wmDone;
}
XFree(nwm);
}
msg(0, "unknown WM, using WM_TWM\n");
g.option_wm = WM_TWM;
wmDone:
msg(0, "WM: %d\n", g.option_wm);
switch (xresource_load_int(&db, XRMAPPNAME, "desktops", &dsindex)) {
case 1:
if (dsindex >= DESK_MIN && dsindex <= DESK_MAX)
g.option_desktop = dsindex;
else
die(inv, "desktops argument range");
break;
case 0:
g.option_desktop = DESK_DEFAULT;
break;
case -1:
die(inv, "desktops argument");
break;
}
msg(0, "desktops: %d\n", g.option_desktop);
switch (xresource_load_int(&db, XRMAPPNAME, "screens", &scindex)) {
case 1:
if (scindex >= SCR_MIN && scindex <= SCR_MAX)
g.option_screen = scindex;
else
die(inv, "screens argument range");
break;
case 0:
g.option_screen = SCR_DEFAULT;
break;
case -1:
die(inv, "screens argument");
break;
}
msg(0, "screens: %d\n", g.option_screen);
#define MC g.option_modCode
#define KC g.option_keyCode
#define prevC g.option_prevCode
#define nextC g.option_nextCode
#define cancelC g.option_cancelCode
#define GMM g.option_modMask
#define GBM g.option_backMask
ksi = ksym_option_to_keycode(&db, XRMAPPNAME, "modifier", &errmsg);
if (ksi == -1)
die("%s\n", errmsg);
MC = ksi != 0 ? ksi : XKeysymToKeycode(dpy, DEFMODKS);
ksi = ksym_option_to_keycode(&db, XRMAPPNAME, "key", &errmsg);
if (ksi == -1)
die("%s\n", errmsg);
KC = ksi != 0 ? ksi : XKeysymToKeycode(dpy, DEFKEYKS);
ksi = ksym_option_to_keycode(&db, XRMAPPNAME, "prevkey", &errmsg);
if (ksi == -1)
die("%s\n", errmsg);
prevC = ksi != 0 ? ksi : XKeysymToKeycode(dpy, DEFPREVKEYKS);
ksi = ksym_option_to_keycode(&db, XRMAPPNAME, "nextkey", &errmsg);
if (ksi == -1)
die("%s\n", errmsg);
nextC = ksi != 0 ? ksi : XKeysymToKeycode(dpy, DEFNEXTKEYKS);
ksi = ksym_option_to_keycode(&db, XRMAPPNAME, "cancelkey", &errmsg);
if (ksi == -1)
die("%s\n", errmsg);
cancelC = ksi != 0 ? ksi : XKeysymToKeycode(dpy, DEFCANCELKS);
switch (xresource_load_int(&db, XRMAPPNAME, "modifier.mask", &(GMM))) {
case 1:
msg(-1,
"Using obsoleted -mm option or modifier.mask resource, see man page for upgrade\n");
break;
case 0:
GMM = keycode_to_modmask(MC);
if (GMM == 0)
die(rmb, MC);
break;
case -1:
die(inv, "modifier mask");
break;
}
switch (xresource_load_int(&db, XRMAPPNAME, "backscroll.mask", &(GBM))) {
case 1:
msg(-1,
"Using obsoleted -bm option or backscroll.mask resource, see man page for upgrade\n");
break;
case 0:
BC = ksym_option_to_keycode(&db, XRMAPPNAME, "backscroll", &errmsg);
if (BC != 0) {
GBM = keycode_to_modmask(BC);
if (GBM == 0)
die(rmb, BC);
} else {
GBM = DEFBACKMASK;
}
break;
case -1:
die(inv, "backscroll mask");
break;
}
msg(0, "modMask %d, backMask %d, modCode %d, keyCode %d\n",
GMM, GBM, MC, KC);
g.option_tileW = DEFTILEW;
g.option_tileH = DEFTILEH;
gtile = xresource_load_string(&db, XRMAPPNAME, "tile.geometry");
if (gtile != NULL) {
xpg = XParseGeometry(gtile, &x, &y, &w, &h);
if (xpg & WidthValue)
g.option_tileW = w;
else
die(inv, "tile width");
if (xpg & HeightValue)
g.option_tileH = h;
else
die(inv, "tile height");
}
g.option_iconW = DEFICONW;
g.option_iconH = DEFICONH;
gicon = xresource_load_string(&db, XRMAPPNAME, "icon.geometry");
if (gicon) {
xpg = XParseGeometry(gicon, &x, &y, &w, &h);
if (xpg & WidthValue)
g.option_iconW = w;
else
die(inv, "icon width");
if (xpg & HeightValue)
g.option_iconH = h;
else
die(inv, "icon height");
}
msg(0, "%dx%d tile, %dx%d icon\n",
g.option_tileW, g.option_tileH, g.option_iconW, g.option_iconH);
bzero(&(g.option_vp), sizeof(g.option_vp));
g.option_vp_mode = VP_DEFAULT;
gview = xresource_load_string(&db, XRMAPPNAME, "viewport");
if (gview) {
if (strncmp(gview, "focus", 6) == 0) {
g.option_vp_mode = VP_FOCUS;
} else if (strncmp(gview, "pointer", 8) == 0) {
g.option_vp_mode = VP_POINTER;
} else if (strncmp(gview, "total", 6) == 0) {
g.option_vp_mode = VP_TOTAL;
} else {
g.option_vp_mode = VP_SPECIFIC;
xpg = XParseGeometry(gview, &x, &y, &w, &h);
if (xpg & (XValue | YValue | WidthValue | HeightValue)) {
g.option_vp.w = w;
g.option_vp.h = h;
g.option_vp.x = x;
g.option_vp.y = y;
} else {
die(inv, "viewport");
}
}
}
msg(0, "viewport: mode %d, %dx%d+%d+%d\n",
g.option_vp_mode,
g.option_vp.w, g.option_vp.h, g.option_vp.x, g.option_vp.y);
g.option_positioning = POS_DEFAULT;
g.option_posX = 0;
g.option_posY = 0;
gpos = xresource_load_string(&db, XRMAPPNAME, "position");
if (gpos) {
if (strncmp(gpos, "center", 7) == 0) {
g.option_positioning = POS_CENTER;
} else if (strncmp(gpos, "none", 5) == 0) {
g.option_positioning = POS_NONE;
} else {
g.option_positioning = POS_SPECIFIC;
xpg = XParseGeometry(gpos, &x, &y, &w, &h);
if (xpg & (XValue | YValue)) {
g.option_posX = x;
g.option_posY = y;
} else {
die(inv, "position");
}
}
}
msg(0, "positioning policy: %d, position: +%d+%d\n",
g.option_positioning, g.option_posX, g.option_posY);
g.option_iconSrc = ISRC_DEFAULT;
switch (xresource_load_int(&db, XRMAPPNAME, "icon.source", &isrc)) {
case 1:
if (isrc >= ISRC_MIN && isrc <= ISRC_MAX)
g.option_iconSrc = isrc;
else
die("icon source argument must be from %d to %d\n",
ISRC_MIN, ISRC_MAX);
break;
case 0:
g.option_iconSrc = ISRC_DEFAULT;
break;
case -1:
die(inv, "icon source");
break;
}
msg(0, "icon source: %d\n", g.option_iconSrc);
s = xresource_load_string(&db, XRMAPPNAME, "theme");
g.option_theme = s ? s : DEFTHEME;
msg(0, "icon theme: %s\n", g.option_theme);
s = xresource_load_string(&db, XRMAPPNAME, "background");
g.color[COLBG].name = s ? s : DEFCOLBG;
s = xresource_load_string(&db, XRMAPPNAME, "foreground");
g.color[COLFG].name = s ? s : DEFCOLFG;
s = xresource_load_string(&db, XRMAPPNAME, "framecolor");
g.color[COLFRAME].name = s ? s : DEFCOLFRAME;
s = xresource_load_string(&db, XRMAPPNAME, "font");
if (s) {
if ((strncmp(s, "xft:", 4) == 0)
&& (*(s + 4) != '\0')) {
g.option_font = s + 4;
} else {
// resource may indeed be valid but non-xft
msg(-1, "invalid font: %s, using default: %s\n", s, DEFFONT);
g.option_font = DEFFONT + 4;
}
} else {
g.option_font = DEFFONT + 4;
}
// max recursion for searching windows
// -1 is "everything"
// in raw X this returns too much windows, "1" is probably sufficient
// no need for an option
g.option_max_reclevel = (g.option_wm == WM_NO) ? 1 : -1;
return 1;
}
//
// grab Alt-Tab and Alt-Shift-Tab
// note: exit() on failure
//
static int grabKeysAtStartup(bool grabUngrab)
{
g.ignored_modmask = getOffendingModifiersMask(dpy); // or 0 for g.debug
char *grabhint =
"Error while (un)grabbing key 0x%x with mask 0x%x/0x%x.\nProbably other program already grabbed this combination.\nCheck: xdotool keydown alt+Tab; xdotool key XF86LogGrabInfo; xdotool keyup Tab; sleep 1; xdotool keyup alt\nand then look for active device grabs in /var/log/Xorg.0.log\nOr try Ctrl-Tab instead of Alt-Tab: alttab -mk Control_L\n";
// attempt XF86Ungrab? probably too invasive
if (!changeKeygrab
(root, grabUngrab, g.option_keyCode, g.option_modMask,
g.ignored_modmask)) {
die(grabhint, g.option_keyCode, g.option_modMask, g.ignored_modmask);
}
if (!changeKeygrab
(root, grabUngrab, g.option_keyCode,
g.option_modMask | g.option_backMask, g.ignored_modmask)) {
die(grabhint, g.option_keyCode,
g.option_modMask | g.option_backMask, g.ignored_modmask);
}
return 1;
}
//
// Returns 0 if not an extra prev/next keycode, 1 if extra prev keycode, and 2 if extra next keycode.
//
static int isPrevNextKey(unsigned int keycode)
{
if (keycode == g.option_prevCode) {
return 1;
}
if (keycode == g.option_nextCode) {
return 2;
}
// if here then is neither
return 0;
}
int main(int argc, char **argv)
{
XEvent ev;
dpy = XOpenDisplay(NULL);
if (!dpy)
die("can't open display");
scr = DefaultScreen(dpy);
root = DefaultRootWindow(dpy);
ee_complain = true;
//hnd = (XErrorHandler)0;
XErrorHandler hnd = XSetErrorHandler(zeroErrorHandler); // for entire program
if (hnd) ;; // make -Wunused happy
signal(SIGUSR1, sighandler);
if (!use_args_and_xrm(&argc, argv))
die("use_args_and_xrm failed");
if (!startupWintasks())
die("startupWintasks failed");
if (!startupGUItasks())
die("startupGUItasks failed");
grabKeysAtStartup(true);
g.uiShowHasRun = false;
struct timespec nanots;
nanots.tv_sec = 0;
nanots.tv_nsec = 1E7;
char keys_pressed[32];
int octet = g.option_modCode / 8;
int kmask = 1 << (g.option_modCode - octet * 8);
while (true) {
memset(&(ev.xkey), 0, sizeof(ev.xkey));
if (g.uiShowHasRun) {
// poll: lag and consume cpu, but necessary because of bug #1 and #2
XQueryKeymap(dpy, keys_pressed);
if (!(keys_pressed[octet] & kmask)) { // Alt released
uiHide();
continue;
}
if (!XCheckIfEvent(dpy, &ev, *predproc_true, NULL)) {
nanosleep(&nanots, NULL);
continue;
}
} else {
// event: immediate, when we don't care about Alt release
XNextEvent(dpy, &ev);
}
switch (ev.type) {
case KeyPress:
msg(1, "Press %lx: %d-%d\n",
ev.xkey.window, ev.xkey.state, ev.xkey.keycode);
if (ev.xkey.state & g.option_modMask) { // alt
if (ev.xkey.keycode == g.option_keyCode) { // tab
// additional check, see #97
XQueryKeymap(dpy, keys_pressed);
if (!(keys_pressed[octet] & kmask)) {
msg(1, "Wrong modifier, skip event\n");
continue;
}
if (!g.uiShowHasRun) {
uiShow((ev.xkey.state & g.option_backMask));
} else {
if (ev.xkey.state & g.option_backMask) {
uiPrevWindow();
} else {
uiNextWindow();
}
}
} else if (ev.xkey.keycode == g.option_cancelCode) { // escape
// additional check, see #97
XQueryKeymap(dpy, keys_pressed);
if (!(keys_pressed[octet] & kmask)) {
msg(1, "Wrong modifier, skip event\n");
continue;
}
uiSelectWindow(0);
} else { // non-tab
switch (isPrevNextKey(ev.xkey.keycode)) {
case 1:
uiPrevWindow();
break;
case 2:
uiNextWindow();
break;
}
}
}
break;
case KeyRelease:
msg(1, "Release %lx: %d-%d\n",
ev.xkey.window, ev.xkey.state, ev.xkey.keycode);
// interested only in "final" release
if (!((ev.xkey.state & g.option_modMask)
&& ev.xkey.keycode == g.option_modCode && g.uiShowHasRun)) {
break;
}
uiHide();
break;
case Expose:
if (g.uiShowHasRun) {
uiExpose();
}
break;
case ButtonPress:
case ButtonRelease:
uiButtonEvent(ev.xbutton);
break;
case PropertyNotify:
winPropChangeEvent(ev.xproperty);
break;
case DestroyNotify:
winDestroyEvent(ev.xdestroywindow);
break;
case FocusIn:
winFocusChangeEvent(ev.xfocus);
break;
default:
msg(1, "Event type %d\n", ev.type);
break;
}
}
// this is probably never reached
shutdownWin();
shutdownGUI();
XrmDestroyDatabase(db);
grabKeysAtStartup(false);
// not restoring error handler
XCloseDisplay(dpy);
return 0;
} // main