]> code.delx.au - spectrwm/blob - scrotwm.c
8b59687522a9133b01b38d6ef7b6aa630eaec0f2
[spectrwm] / scrotwm.c
1 /* $scrotwm$ */
2 /*
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 /*
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
21 *
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
31 *
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
38 *
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
49 */
50
51 #define SWM_VERSION "0.5"
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <locale.h>
58 #include <unistd.h>
59 #include <time.h>
60 #include <signal.h>
61 #include <string.h>
62 #include <util.h>
63 #include <pwd.h>
64 #include <ctype.h>
65
66 #include <sys/types.h>
67 #include <sys/time.h>
68 #include <sys/stat.h>
69 #include <sys/wait.h>
70 #include <sys/queue.h>
71 #include <sys/param.h>
72 #include <sys/select.h>
73
74 #include <X11/cursorfont.h>
75 #include <X11/keysym.h>
76 #include <X11/Xatom.h>
77 #include <X11/Xlib.h>
78 #include <X11/Xproto.h>
79 #include <X11/Xutil.h>
80 #include <X11/extensions/Xrandr.h>
81
82 #if RANDR_MAJOR < 1
83 # error XRandR versions less than 1.0 are not supported
84 #endif
85
86 #if RANDR_MAJOR >= 1
87 #if RANDR_MINOR >= 2
88 #define SWM_XRR_HAS_CRTC
89 #endif
90 #endif
91
92 /* #define SWM_DEBUG */
93 #ifdef SWM_DEBUG
94 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
95 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
96 #define SWM_D_MISC 0x0001
97 #define SWM_D_EVENT 0x0002
98 #define SWM_D_WS 0x0004
99 #define SWM_D_FOCUS 0x0008
100 #define SWM_D_MOVE 0x0010
101 #define SWM_D_STACK 0x0020
102 #define SWM_D_MOUSE 0x0040
103
104 u_int32_t swm_debug = 0
105 | SWM_D_MISC
106 | SWM_D_EVENT
107 | SWM_D_WS
108 | SWM_D_FOCUS
109 | SWM_D_MOVE
110 | SWM_D_STACK
111 ;
112 #else
113 #define DPRINTF(x...)
114 #define DNPRINTF(n,x...)
115 #endif
116
117 #define LENGTH(x) (sizeof x / sizeof x[0])
118 #define MODKEY Mod1Mask
119 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
120 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
121
122 #define X(r) (r)->g.x
123 #define Y(r) (r)->g.y
124 #define WIDTH(r) (r)->g.w
125 #define HEIGHT(r) (r)->g.h
126
127 char **start_argv;
128 Atom astate;
129 int (*xerrorxlib)(Display *, XErrorEvent *);
130 int other_wm;
131 int running = 1;
132 int xrandr_eventbase;
133 int ignore_enter = 0;
134 unsigned int numlockmask = 0;
135 Display *display;
136
137 int cycle_empty = 0;
138 int cycle_visible = 0;
139
140 /* dialog windows */
141 double dialog_ratio = .6;
142 /* status bar */
143 #define SWM_BAR_MAX (128)
144 sig_atomic_t bar_alarm = 0;
145 int bar_enabled = 1;
146 int bar_verbose = 1;
147 int bar_height = 0;
148 GC bar_gc;
149 XGCValues bar_gcv;
150 int bar_fidx = 0;
151 XFontStruct *bar_fs;
152 char *bar_fonts[] = {
153 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
154 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
155 NULL
156 };
157
158
159
160 /* terminal + args */
161 char *spawn_term[] = { "xterm", NULL };
162 char *spawn_menu[] = { "dmenu_run", "-fn", NULL,
163 "-nb", NULL, "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
164
165 #define SWM_MENU_FN (2)
166 #define SWM_MENU_NB (4)
167 #define SWM_MENU_NF (6)
168 #define SWM_MENU_SB (8)
169 #define SWM_MENU_SF (10)
170
171 /* layout manager data */
172 struct swm_geometry {
173 int x;
174 int y;
175 int w;
176 int h;
177 };
178
179 struct swm_screen;
180 struct workspace;
181
182 /* virtual "screens" */
183 struct swm_region {
184 TAILQ_ENTRY(swm_region) entry;
185 struct swm_geometry g;
186 Window bar_window;
187 struct workspace *ws; /* current workspace on this region */
188 struct swm_screen *s; /* screen idx */
189 };
190 TAILQ_HEAD(swm_region_list, swm_region);
191
192
193 struct ws_win {
194 TAILQ_ENTRY(ws_win) entry;
195 Window id;
196 struct swm_geometry g;
197 int got_focus;
198 int floating;
199 int transient;
200 struct workspace *ws; /* always valid */
201 struct swm_screen *s; /* always valid, never changes */
202 XWindowAttributes wa;
203 };
204 TAILQ_HEAD(ws_win_list, ws_win);
205
206 /* layout handlers */
207 void stack(void);
208 void vertical_config(struct workspace *, int);
209 void vertical_stack(struct workspace *, struct swm_geometry *);
210 void horizontal_config(struct workspace *, int);
211 void horizontal_stack(struct workspace *, struct swm_geometry *);
212 void max_stack(struct workspace *, struct swm_geometry *);
213
214 void grabbuttons(struct ws_win *, int);
215
216 struct layout {
217 void (*l_stack)(struct workspace *, struct swm_geometry *);
218 void (*l_config)(struct workspace *, int);
219 } layouts[] = {
220 /* stack, configure */
221 { vertical_stack, vertical_config},
222 { horizontal_stack, horizontal_config},
223 { max_stack, NULL},
224 { NULL, NULL},
225 };
226
227 #define SWM_H_SLICE (32)
228 #define SWM_V_SLICE (32)
229
230 /* define work spaces */
231 struct workspace {
232 int idx; /* workspace index */
233 int restack; /* restack on switch */
234 struct layout *cur_layout; /* current layout handlers */
235 struct ws_win *focus; /* may be NULL */
236 struct swm_region *r; /* may be NULL */
237 struct ws_win_list winlist; /* list of windows in ws */
238
239 /* stacker state */
240 struct {
241 int horizontal_msize;
242 int horizontal_mwin;
243 int vertical_msize;
244 int vertical_mwin;
245 } l_state;
246 };
247
248
249 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
250 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
251
252 /* physical screen mapping */
253 #define SWM_WS_MAX (10) /* XXX Too small? */
254 struct swm_screen {
255 int idx; /* screen index */
256 struct swm_region_list rl; /* list of regions on this screen */
257 Window root;
258 int xrandr_support;
259 struct workspace ws[SWM_WS_MAX];
260
261 /* colors */
262 struct {
263 unsigned long color;
264 char *name;
265 } c[SWM_S_COLOR_MAX];
266 };
267 struct swm_screen *screens;
268 int num_screens;
269
270 struct ws_win *cur_focus = NULL;
271
272 /* args to functions */
273 union arg {
274 int id;
275 #define SWM_ARG_ID_FOCUSNEXT (0)
276 #define SWM_ARG_ID_FOCUSPREV (1)
277 #define SWM_ARG_ID_FOCUSMAIN (2)
278 #define SWM_ARG_ID_SWAPNEXT (3)
279 #define SWM_ARG_ID_SWAPPREV (4)
280 #define SWM_ARG_ID_SWAPMAIN (5)
281 #define SWM_ARG_ID_MASTERSHRINK (6)
282 #define SWM_ARG_ID_MASTERGROW (7)
283 #define SWM_ARG_ID_MASTERADD (8)
284 #define SWM_ARG_ID_MASTERDEL (9)
285 #define SWM_ARG_ID_STACKRESET (10)
286 #define SWM_ARG_ID_STACKINIT (11)
287 #define SWM_ARG_ID_CYCLEWS_UP (12)
288 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
289 char **argv;
290 };
291
292 unsigned long
293 name_to_color(char *colorname)
294 {
295 Colormap cmap;
296 Status status;
297 XColor screen_def, exact_def;
298 unsigned long result = 0;
299 char cname[32] = "#";
300
301 cmap = DefaultColormap(display, screens[0].idx);
302 status = XAllocNamedColor(display, cmap, colorname,
303 &screen_def, &exact_def);
304 if (!status) {
305 strlcat(cname, colorname + 2, sizeof cname - 1);
306 status = XAllocNamedColor(display, cmap, cname, &screen_def,
307 &exact_def);
308 }
309 if (status)
310 result = screen_def.pixel;
311 else
312 fprintf(stderr, "color '%s' not found.\n", colorname);
313
314 return (result);
315 }
316
317 void
318 setscreencolor(char *val, int i, int c)
319 {
320 if (i > 0 && i <= ScreenCount(display)) {
321 screens[i - 1].c[c].color = name_to_color(val);
322 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
323 errx(1, "strdup");
324 } else if (i == -1) {
325 for (i = 0; i < ScreenCount(display); i++)
326 screens[i].c[c].color = name_to_color(val);
327 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
328 errx(1, "strdup");
329 } else
330 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
331 i, ScreenCount(display));
332 }
333
334 int
335 varmatch(char *var, char *name, int *index)
336 {
337 char *p, buf[5];
338 int i;
339
340 i = strncmp(var, name, 255);
341 if (index == NULL)
342 return (i);
343
344 *index = -1;
345 if (i <= 0)
346 return (i);
347 p = var + strlen(name);
348 if (*p++ != '[')
349 return (i);
350 bzero(buf, sizeof buf);
351 i = 0;
352 while (isdigit(*p) && i < sizeof buf)
353 buf[i++] = *p++;
354 if (i == 0 || i >= sizeof buf || *p != ']')
355 return (1);
356 *index = strtonum(buf, 0, 99, NULL);
357 return (0);
358 }
359
360 /* conf file stuff */
361 #define SWM_CONF_WS "\n= \t"
362 #define SWM_CONF_FILE "scrotwm.conf"
363 int
364 conf_load(char *filename)
365 {
366 FILE *config;
367 char *line, *cp, *var, *val;
368 size_t len, lineno = 0;
369 int i, sc;
370
371 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
372
373 if (filename == NULL)
374 return (1);
375
376 if ((config = fopen(filename, "r")) == NULL)
377 return (1);
378
379 for (sc = ScreenCount(display);;) {
380 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
381 if (feof(config))
382 break;
383 cp = line;
384 cp += (long)strspn(cp, SWM_CONF_WS);
385 if (cp[0] == '\0') {
386 /* empty line */
387 free(line);
388 continue;
389 }
390 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
391 break;
392 cp += (long)strspn(cp, SWM_CONF_WS);
393 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
394 break;
395
396 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
397 switch (var[0]) {
398 case 'b':
399 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
400 bar_enabled = atoi(val);
401 else if (!varmatch(var, "bar_border", &i))
402 setscreencolor(var, i, SWM_S_COLOR_BAR_BORDER);
403 else if (!varmatch(var, "bar_color", &i))
404 setscreencolor(var, i, SWM_S_COLOR_BAR);
405 else if (!varmatch(var, "bar_font_color", &i))
406 setscreencolor(var, i, SWM_S_COLOR_BAR_FONT);
407 else if (!strncmp(var, "bar_font", strlen("bar_font")))
408 asprintf(&bar_fonts[0], "%s", val);
409 else
410 goto bad;
411 break;
412
413 case 'c':
414 if (!varmatch(var, "color_focus", &i))
415 setscreencolor(var, i, SWM_S_COLOR_FOCUS);
416 else if (!varmatch(var, "color_unfocus", &i))
417 setscreencolor(var, i, SWM_S_COLOR_UNFOCUS);
418 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
419 cycle_visible = atoi(val);
420 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
421 cycle_visible = atoi(val);
422 else
423 goto bad;
424 break;
425
426 case 'd':
427 if (!strncmp(var, "dialog_ratio",
428 strlen("dialog_ratio"))) {
429 dialog_ratio = atof(val);
430 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
431 dialog_ratio = .6;
432 } else
433 goto bad;
434 break;
435
436 case 's':
437 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
438 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
439 break;
440 default:
441 goto bad;
442 }
443 free(line);
444 }
445
446 fclose(config);
447 return (0);
448
449 bad:
450 errx(1, "invalid conf file entry: %s=%s", var, val);
451 }
452
453 void
454 bar_print(struct swm_region *r, char *s)
455 {
456 XClearWindow(display, r->bar_window);
457 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
458 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
459 strlen(s));
460 }
461
462 void
463 bar_update(void)
464 {
465 time_t tmt;
466 struct tm tm;
467 struct swm_region *r;
468 int i, x;
469 char s[SWM_BAR_MAX];
470 char e[SWM_BAR_MAX];
471
472 if (bar_enabled == 0)
473 return;
474
475 time(&tmt);
476 localtime_r(&tmt, &tm);
477 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
478 for (i = 0; i < ScreenCount(display); i++) {
479 x = 1;
480 TAILQ_FOREACH(r, &screens[i].rl, entry) {
481 snprintf(e, sizeof e, "%s %d:%d",
482 s, x++, r->ws->idx + 1);
483 bar_print(r, e);
484 }
485 }
486 XSync(display, False);
487 alarm(60);
488 }
489
490 void
491 bar_signal(int sig)
492 {
493 bar_alarm = 1;
494 }
495
496 void
497 bar_toggle(struct swm_region *r, union arg *args)
498 {
499 struct swm_region *tmpr;
500 int i, j, sc = ScreenCount(display);
501
502 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
503
504 if (bar_enabled) {
505 for (i = 0; i < sc; i++)
506 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
507 XUnmapWindow(display, tmpr->bar_window);
508 } else {
509 for (i = 0; i < sc; i++)
510 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
511 XMapRaised(display, tmpr->bar_window);
512 }
513 bar_enabled = !bar_enabled;
514 XSync(display, False);
515 for (i = 0; i < sc; i++)
516 for (j = 0; j < SWM_WS_MAX; j++)
517 screens[i].ws[j].restack = 1;
518
519 stack();
520 /* must be after stack */
521 for (i = 0; i < sc; i++)
522 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
523 bar_update();
524 }
525
526 void
527 bar_setup(struct swm_region *r)
528 {
529 int i;
530
531 for (i = 0; bar_fonts[i] != NULL; i++) {
532 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
533 if (bar_fs) {
534 bar_fidx = i;
535 break;
536 }
537 }
538 if (bar_fonts[i] == NULL)
539 errx(1, "couldn't load font");
540 bar_height = bar_fs->ascent + bar_fs->descent + 3;
541
542 r->bar_window = XCreateSimpleWindow(display,
543 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
544 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
545 r->s->c[SWM_S_COLOR_BAR].color);
546 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
547 XSetFont(display, bar_gc, bar_fs->fid);
548 XSelectInput(display, r->bar_window, VisibilityChangeMask);
549 if (bar_enabled)
550 XMapRaised(display, r->bar_window);
551 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
552
553 if (signal(SIGALRM, bar_signal) == SIG_ERR)
554 err(1, "could not install bar_signal");
555 bar_update();
556 }
557
558 void
559 bar_refresh(void)
560 {
561 XSetWindowAttributes wa;
562 struct swm_region *r;
563 int i;
564
565 bzero(&wa, sizeof wa);
566 for (i = 0; i < ScreenCount(display); i++)
567 TAILQ_FOREACH(r, &screens[i].rl, entry) {
568 wa.border_pixel =
569 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
570 wa.background_pixel =
571 screens[i].c[SWM_S_COLOR_BAR].color;
572 XChangeWindowAttributes(display, r->bar_window,
573 CWBackPixel | CWBorderPixel, &wa);
574 }
575 bar_update();
576 }
577
578 void
579 config_win(struct ws_win *win)
580 {
581 XConfigureEvent ce;
582
583 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
584 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
585 ce.type = ConfigureNotify;
586 ce.display = display;
587 ce.event = win->id;
588 ce.window = win->id;
589 ce.x = win->g.x;
590 ce.y = win->g.y;
591 ce.width = win->g.w;
592 ce.height = win->g.h;
593 ce.border_width = 1; /* XXX store this! */
594 ce.above = None;
595 ce.override_redirect = False;
596 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
597 }
598
599 int
600 count_win(struct workspace *ws, int count_transient)
601 {
602 struct ws_win *win;
603 int count = 0;
604
605 TAILQ_FOREACH(win, &ws->winlist, entry) {
606 if (count_transient == 0 && win->floating)
607 continue;
608 if (count_transient == 0 && win->transient)
609 continue;
610 count++;
611 }
612 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
613
614 return (count);
615 }
616
617 void
618 quit(struct swm_region *r, union arg *args)
619 {
620 DNPRINTF(SWM_D_MISC, "quit\n");
621 running = 0;
622 }
623
624 void
625 restart(struct swm_region *r, union arg *args)
626 {
627 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
628
629 /* disable alarm because the following code may not be interrupted */
630 alarm(0);
631 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
632 errx(1, "can't disable alarm");
633
634 XCloseDisplay(display);
635 execvp(start_argv[0], start_argv);
636 fprintf(stderr, "execvp failed\n");
637 perror(" failed");
638 quit(NULL, NULL);
639 }
640
641 struct swm_region *
642 root_to_region(Window root)
643 {
644 struct swm_region *r;
645 Window rr, cr;
646 int i, x, y, wx, wy;
647 unsigned int mask;
648
649 for (i = 0; i < ScreenCount(display); i++)
650 if (screens[i].root == root)
651 break;
652
653 if (XQueryPointer(display, screens[i].root,
654 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
655 /* if we can't query the pointer, grab the first region */
656 r = TAILQ_FIRST(&screens[i].rl);
657 } else {
658 /* otherwise, choose a region based on pointer location */
659 TAILQ_FOREACH(r, &screens[i].rl, entry) {
660 if (x > X(r) && x < X(r) + WIDTH(r) &&
661 y > Y(r) && y < Y(r) + HEIGHT(r))
662 break;
663 }
664
665 if (r == NULL)
666 r = TAILQ_FIRST(&screens[i].rl);
667 }
668 return (r);
669 }
670
671 struct ws_win *
672 find_window(Window id)
673 {
674 struct ws_win *win;
675 int i, j;
676
677 for (i = 0; i < ScreenCount(display); i++)
678 for (j = 0; j < SWM_WS_MAX; j++)
679 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
680 if (id == win->id)
681 return (win);
682 return (NULL);
683 }
684
685 void
686 spawn(struct swm_region *r, union arg *args)
687 {
688 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
689 /*
690 * The double-fork construct avoids zombie processes and keeps the code
691 * clean from stupid signal handlers.
692 */
693 if (fork() == 0) {
694 if (fork() == 0) {
695 if (display)
696 close(ConnectionNumber(display));
697 setsid();
698 execvp(args->argv[0], args->argv);
699 fprintf(stderr, "execvp failed\n");
700 perror(" failed");
701 }
702 exit(0);
703 }
704 wait(0);
705 }
706
707
708 void
709 spawnmenu(struct swm_region *r, union arg *args)
710 {
711 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
712
713 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
714 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
715 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
716 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
717 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
718
719 spawn(r, args);
720 }
721
722 void
723 unfocus_all(void)
724 {
725 struct ws_win *win;
726 int i, j;
727
728 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
729
730 for (i = 0; i < ScreenCount(display); i++)
731 for (j = 0; j < SWM_WS_MAX; j++)
732 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
733 if (win->ws->r == NULL)
734 continue;
735 grabbuttons(win, 0);
736 XSetWindowBorder(display, win->id,
737 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
738 win->got_focus = 0;
739 win->ws->focus = NULL;
740 cur_focus = NULL;
741 }
742 }
743
744 void
745 focus_win(struct ws_win *win)
746 {
747 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
748
749 if (win == NULL)
750 return;
751
752 unfocus_all();
753 win->ws->focus = win;
754 if (win->ws->r != NULL) {
755 cur_focus = win;
756 if (!win->got_focus) {
757 XSetWindowBorder(display, win->id,
758 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
759 grabbuttons(win, 1);
760 }
761 win->got_focus = 1;
762 XSetInputFocus(display, win->id,
763 RevertToPointerRoot, CurrentTime);
764 }
765 }
766
767 void
768 switchws(struct swm_region *r, union arg *args)
769 {
770 int wsid = args->id;
771 struct swm_region *this_r, *other_r;
772 struct ws_win *win;
773 struct workspace *new_ws, *old_ws;
774
775 this_r = r;
776 old_ws = this_r->ws;
777 new_ws = &this_r->s->ws[wsid];
778
779 DNPRINTF(SWM_D_WS, "switchws screen %d region %dx%d+%d+%d: "
780 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
781 old_ws->idx, wsid);
782
783 if (new_ws == old_ws)
784 return;
785
786 other_r = new_ws->r;
787 if (!other_r) {
788 /* if the other workspace is hidden, switch windows */
789 /* map new window first to prevent ugly blinking */
790 TAILQ_FOREACH(win, &new_ws->winlist, entry)
791 XMapRaised(display, win->id);
792
793 TAILQ_FOREACH(win, &old_ws->winlist, entry)
794 XUnmapWindow(display, win->id);
795
796 old_ws->r = NULL;
797 old_ws->restack = 1;
798 } else {
799 other_r->ws = old_ws;
800 old_ws->r = other_r;
801 }
802 this_r->ws = new_ws;
803 new_ws->r = this_r;
804
805 ignore_enter = 1;
806 /* set focus */
807 if (new_ws->focus)
808 focus_win(new_ws->focus);
809 stack();
810 bar_update();
811 }
812
813 void
814 cyclews(struct swm_region *r, union arg *args)
815 {
816 union arg a;
817 struct swm_screen *s = r->s;
818
819 DNPRINTF(SWM_D_WS, "cyclews id %d "
820 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
821 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
822
823 a.id = r->ws->idx;
824
825 do {
826 switch (args->id) {
827 case SWM_ARG_ID_CYCLEWS_UP:
828 if (a.id < SWM_WS_MAX - 1)
829 a.id++;
830 else
831 a.id = 0;
832 break;
833 case SWM_ARG_ID_CYCLEWS_DOWN:
834 if (a.id > 0)
835 a.id--;
836 else
837 a.id = SWM_WS_MAX - 1;
838 break;
839 default:
840 return;
841 };
842
843 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
844 continue;
845 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
846 continue;
847
848 switchws(r, &a);
849 } while (a.id != r->ws->idx);
850 }
851
852 void
853 swapwin(struct swm_region *r, union arg *args)
854 {
855 struct ws_win *target;
856 struct ws_win_list *wl;
857
858
859 DNPRINTF(SWM_D_WS, "swapwin id %d "
860 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
861 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
862 if (cur_focus == NULL)
863 return;
864
865 wl = &cur_focus->ws->winlist;
866
867 switch (args->id) {
868 case SWM_ARG_ID_SWAPPREV:
869 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
870 TAILQ_REMOVE(wl, cur_focus, entry);
871 if (target == NULL)
872 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
873 else
874 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
875 break;
876 case SWM_ARG_ID_SWAPNEXT:
877 target = TAILQ_NEXT(cur_focus, entry);
878 TAILQ_REMOVE(wl, cur_focus, entry);
879 if (target == NULL)
880 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
881 else
882 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
883 break;
884 case SWM_ARG_ID_SWAPMAIN:
885 target = TAILQ_FIRST(wl);
886 if (target == cur_focus)
887 return;
888 TAILQ_REMOVE(wl, target, entry);
889 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
890 TAILQ_REMOVE(wl, cur_focus, entry);
891 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
892 break;
893 default:
894 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
895 return;
896 }
897
898 ignore_enter = 2;
899 stack();
900 }
901
902 void
903 focus(struct swm_region *r, union arg *args)
904 {
905 struct ws_win *winfocus, *winlostfocus;
906 struct ws_win_list *wl;
907
908 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
909 if (cur_focus == NULL)
910 return;
911
912 wl = &cur_focus->ws->winlist;
913
914 winlostfocus = cur_focus;
915
916 switch (args->id) {
917 case SWM_ARG_ID_FOCUSPREV:
918 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
919 if (winfocus == NULL)
920 winfocus = TAILQ_LAST(wl, ws_win_list);
921 break;
922
923 case SWM_ARG_ID_FOCUSNEXT:
924 winfocus = TAILQ_NEXT(cur_focus, entry);
925 if (winfocus == NULL)
926 winfocus = TAILQ_FIRST(wl);
927 break;
928
929 case SWM_ARG_ID_FOCUSMAIN:
930 winfocus = TAILQ_FIRST(wl);
931 break;
932
933 default:
934 return;
935 }
936
937 if (winfocus == winlostfocus)
938 return;
939
940 focus_win(winfocus);
941 XSync(display, False);
942 }
943
944 void
945 cycle_layout(struct swm_region *r, union arg *args)
946 {
947 struct workspace *ws = r->ws;
948
949 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
950
951 ws->cur_layout++;
952 if (ws->cur_layout->l_stack == NULL)
953 ws->cur_layout = &layouts[0];
954 ignore_enter = 1;
955
956 stack();
957 }
958
959 void
960 stack_config(struct swm_region *r, union arg *args)
961 {
962 struct workspace *ws = r->ws;
963
964 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
965 args->id, ws->idx);
966
967 if (ws->cur_layout->l_config != NULL)
968 ws->cur_layout->l_config(ws, args->id);
969
970 if (args->id != SWM_ARG_ID_STACKINIT);
971 stack();
972 }
973
974 void
975 stack(void) {
976 struct swm_geometry g;
977 struct swm_region *r;
978 int i, j;
979
980 DNPRINTF(SWM_D_STACK, "stack\n");
981
982 for (i = 0; i < ScreenCount(display); i++) {
983 j = 0;
984 TAILQ_FOREACH(r, &screens[i].rl, entry) {
985 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
986 "(screen %d, region %d)\n", r->ws->idx, i, j++);
987
988 /* start with screen geometry, adjust for bar */
989 g = r->g;
990 g.w -= 2;
991 g.h -= 2;
992 if (bar_enabled) {
993 g.y += bar_height;
994 g.h -= bar_height;
995 }
996
997 r->ws->restack = 0;
998 r->ws->cur_layout->l_stack(r->ws, &g);
999 }
1000 }
1001 XSync(display, False);
1002 }
1003
1004 void
1005 stack_floater(struct ws_win *win, struct swm_region *r)
1006 {
1007 unsigned int mask;
1008 XWindowChanges wc;
1009
1010 bzero(&wc, sizeof wc);
1011 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1012 wc.border_width = 1;
1013 if (win->transient) {
1014 win->g.w = (double)WIDTH(r) * dialog_ratio;
1015 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1016 }
1017 wc.width = win->g.w;
1018 wc.height = win->g.h;
1019 wc.x = (WIDTH(r) - win->g.w) / 2;
1020 wc.y = (HEIGHT(r) - win->g.h) / 2;
1021
1022 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1023 win->id, wc.x, wc.y, wc.width, wc.height);
1024
1025 XConfigureWindow(display, win->id, mask, &wc);
1026 }
1027
1028
1029 void
1030 vertical_config(struct workspace *ws, int id)
1031 {
1032 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1033
1034 switch (id) {
1035 case SWM_ARG_ID_STACKRESET:
1036 case SWM_ARG_ID_STACKINIT:
1037 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1038 ws->l_state.vertical_mwin = 1;
1039 break;
1040 case SWM_ARG_ID_MASTERSHRINK:
1041 if (ws->l_state.vertical_msize > 1)
1042 ws->l_state.vertical_msize--;
1043 break;
1044 case SWM_ARG_ID_MASTERGROW:
1045 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1046 ws->l_state.vertical_msize++;
1047 break;
1048 case SWM_ARG_ID_MASTERADD:
1049 ws->l_state.vertical_mwin++;
1050 break;
1051 case SWM_ARG_ID_MASTERDEL:
1052 if (ws->l_state.vertical_mwin > 0)
1053 ws->l_state.vertical_mwin--;
1054 break;
1055 default:
1056 return;
1057 }
1058 }
1059
1060 void
1061 vertical_stack(struct workspace *ws, struct swm_geometry *g) {
1062 XWindowChanges wc;
1063 struct swm_geometry gg = *g;
1064 struct ws_win *win, *winfocus;
1065 int i, j, split, colno, hrh, winno, main_width;
1066 unsigned int mask;
1067
1068 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1069
1070 if ((winno = count_win(ws, 0)) == 0)
1071 return;
1072
1073 if (ws->focus == NULL)
1074 ws->focus = TAILQ_FIRST(&ws->winlist);
1075 winfocus = cur_focus ? cur_focus : ws->focus;
1076
1077 if (ws->l_state.vertical_mwin &&
1078 winno > ws->l_state.vertical_mwin) {
1079 split = ws->l_state.vertical_mwin;
1080 colno = split;
1081 main_width = (g->w / SWM_V_SLICE) *
1082 ws->l_state.vertical_msize;
1083 gg.w = main_width;
1084 } else {
1085 colno = winno;
1086 split = 0;
1087 }
1088 hrh = g->h / colno;
1089 gg.h = hrh - 2;
1090
1091 i = j = 0;
1092 TAILQ_FOREACH(win, &ws->winlist, entry) {
1093 if (split && i == split) {
1094 colno = winno - split;
1095 hrh = (g->h / colno);
1096 gg.x += main_width + 2;
1097 gg.w = g->w - (main_width + 2);
1098 gg.h = hrh - 2;
1099 j = 0;
1100 }
1101 if (j == colno - 1)
1102 gg.h = (hrh + (g->h - (colno * hrh)));
1103
1104 if (j == 0)
1105 gg.y = g->y;
1106 else
1107 gg.y += hrh;
1108
1109
1110 if (win->transient != 0 || win->floating != 0)
1111 stack_floater(win, ws->r);
1112 else {
1113 bzero(&wc, sizeof wc);
1114 wc.border_width = 1;
1115 win->g.x = wc.x = gg.x;
1116 win->g.y = wc.y = gg.y;
1117 win->g.w = wc.width = gg.w;
1118 win->g.h = wc.height = gg.h;
1119 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1120 XConfigureWindow(display, win->id, mask, &wc);
1121 }
1122
1123 XMapRaised(display, win->id);
1124 i++;
1125 j++;
1126 }
1127
1128 if (winfocus)
1129 focus_win(winfocus); /* has to be done outside of the loop */
1130 }
1131
1132
1133 void
1134 horizontal_config(struct workspace *ws, int id)
1135 {
1136 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1137
1138 switch (id) {
1139 case SWM_ARG_ID_STACKRESET:
1140 case SWM_ARG_ID_STACKINIT:
1141 ws->l_state.horizontal_mwin = 1;
1142 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1143 break;
1144 case SWM_ARG_ID_MASTERSHRINK:
1145 if (ws->l_state.horizontal_msize > 1)
1146 ws->l_state.horizontal_msize--;
1147 break;
1148 case SWM_ARG_ID_MASTERGROW:
1149 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1150 ws->l_state.horizontal_msize++;
1151 break;
1152 case SWM_ARG_ID_MASTERADD:
1153 ws->l_state.horizontal_mwin++;
1154 break;
1155 case SWM_ARG_ID_MASTERDEL:
1156 if (ws->l_state.horizontal_mwin > 0)
1157 ws->l_state.horizontal_mwin--;
1158 break;
1159 default:
1160 return;
1161 }
1162 }
1163
1164 void
1165 horizontal_stack(struct workspace *ws, struct swm_geometry *g) {
1166 XWindowChanges wc;
1167 struct swm_geometry gg = *g;
1168 struct ws_win *win, *winfocus;
1169 int i, j, split, rowno, hrw, winno, main_height;
1170 unsigned int mask;
1171
1172 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
1173
1174 if ((winno = count_win(ws, 0)) == 0)
1175 return;
1176
1177 if (ws->focus == NULL)
1178 ws->focus = TAILQ_FIRST(&ws->winlist);
1179 winfocus = cur_focus ? cur_focus : ws->focus;
1180
1181 if (ws->l_state.horizontal_mwin &&
1182 winno > ws->l_state.horizontal_mwin) {
1183 split = ws->l_state.horizontal_mwin;
1184 rowno = split;
1185 main_height = (g->h / SWM_V_SLICE) *
1186 ws->l_state.horizontal_msize;
1187 gg.h = main_height;
1188 } else {
1189 rowno = winno;
1190 split = 0;
1191 }
1192 hrw = g->w / rowno;
1193 gg.w = hrw - 2;
1194
1195 i = j = 0;
1196 TAILQ_FOREACH(win, &ws->winlist, entry) {
1197 if (split && i == split) {
1198 rowno = winno - split;
1199 hrw = (g->w / rowno);
1200 gg.y += main_height + 2;
1201 gg.h = g->h - (main_height + 2);
1202 gg.w = hrw - 2;
1203 j = 0;
1204 }
1205 if (j == rowno - 1)
1206 gg.w = (hrw + (g->w - (rowno * hrw)));
1207
1208 if (j == 0)
1209 gg.x = g->x;
1210 else
1211 gg.x += hrw;
1212
1213 if (win->transient != 0 || win->floating != 0)
1214 stack_floater(win, ws->r);
1215 else {
1216 bzero(&wc, sizeof wc);
1217 wc.border_width = 1;
1218 win->g.x = wc.x = gg.x;
1219 win->g.y = wc.y = gg.y;
1220 win->g.w = wc.width = gg.w;
1221 win->g.h = wc.height = gg.h;
1222 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1223 XConfigureWindow(display, win->id, mask, &wc);
1224 }
1225
1226 XMapRaised(display, win->id);
1227 j++;
1228 i++;
1229 }
1230
1231 if (winfocus)
1232 focus_win(winfocus); /* this has to be done outside of the loop */
1233 }
1234
1235 /* fullscreen view */
1236 void
1237 max_stack(struct workspace *ws, struct swm_geometry *g) {
1238 XWindowChanges wc;
1239 struct swm_geometry gg = *g;
1240 struct ws_win *win, *winfocus;
1241 unsigned int mask;
1242
1243 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1244
1245 if (count_win(ws, 0) == 0)
1246 return;
1247
1248 if (ws->focus == NULL)
1249 ws->focus = TAILQ_FIRST(&ws->winlist);
1250 winfocus = cur_focus ? cur_focus : ws->focus;
1251
1252 TAILQ_FOREACH(win, &ws->winlist, entry) {
1253 if (win->transient != 0 || win->floating != 0) {
1254 if (win == ws->focus) {
1255 /* XXX maximize? */
1256 stack_floater(win, ws->r);
1257 XMapRaised(display, win->id);
1258 } else
1259 XUnmapWindow(display, win->id);
1260 } else {
1261 bzero(&wc, sizeof wc);
1262 wc.border_width = 1;
1263 win->g.x = wc.x = gg.x;
1264 win->g.y = wc.y = gg.y;
1265 win->g.w = wc.width = gg.w;
1266 win->g.h = wc.height = gg.h;
1267 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1268 XConfigureWindow(display, win->id, mask, &wc);
1269
1270 if (win == ws->focus) {
1271 XMapRaised(display, win->id);
1272 } else
1273 XUnmapWindow(display, win->id);
1274 }
1275 }
1276
1277 if (winfocus)
1278 focus_win(winfocus); /* has to be done outside of the loop */
1279 }
1280
1281 void
1282 send_to_ws(struct swm_region *r, union arg *args)
1283 {
1284 int wsid = args->id;
1285 struct ws_win *win = cur_focus;
1286 struct workspace *ws, *nws;
1287
1288 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1289
1290 ws = win->ws;
1291 nws = &win->s->ws[wsid];
1292
1293 XUnmapWindow(display, win->id);
1294
1295 /* find a window to focus */
1296 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1297 if (ws->focus == NULL)
1298 ws->focus = TAILQ_FIRST(&ws->winlist);
1299 if (ws->focus == win)
1300 ws->focus = NULL;
1301
1302 TAILQ_REMOVE(&ws->winlist, win, entry);
1303
1304 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1305 win->ws = nws;
1306
1307 if (count_win(nws, 1) == 1)
1308 nws->focus = win;
1309 ws->restack = 1;
1310 nws->restack = 1;
1311
1312 stack();
1313 }
1314
1315 /* key definitions */
1316 struct key {
1317 unsigned int mod;
1318 KeySym keysym;
1319 void (*func)(struct swm_region *r, union arg *);
1320 union arg args;
1321 } keys[] = {
1322 /* modifier key function argument */
1323 { MODKEY, XK_space, cycle_layout, {0} },
1324 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1325 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1326 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1327 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1328 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1329 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1330 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1331 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1332 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1333 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1334 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1335 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1336 { MODKEY | ShiftMask, XK_q, quit, {0} },
1337 { MODKEY, XK_q, restart, {0} },
1338 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1339 { MODKEY, XK_1, switchws, {.id = 0} },
1340 { MODKEY, XK_2, switchws, {.id = 1} },
1341 { MODKEY, XK_3, switchws, {.id = 2} },
1342 { MODKEY, XK_4, switchws, {.id = 3} },
1343 { MODKEY, XK_5, switchws, {.id = 4} },
1344 { MODKEY, XK_6, switchws, {.id = 5} },
1345 { MODKEY, XK_7, switchws, {.id = 6} },
1346 { MODKEY, XK_8, switchws, {.id = 7} },
1347 { MODKEY, XK_9, switchws, {.id = 8} },
1348 { MODKEY, XK_0, switchws, {.id = 9} },
1349 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1350 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1351 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1352 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1353 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1354 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1355 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1356 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1357 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1358 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1359 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1360 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1361 { MODKEY, XK_b, bar_toggle, {0} },
1362 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1363 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1364 };
1365
1366 void
1367 click(struct swm_region *r, union arg *args)
1368 {
1369 DNPRINTF(SWM_D_MOUSE, "click: button: %d\n", args->id);
1370
1371 switch (args->id) {
1372 case Button1:
1373 break;
1374 case Button2:
1375 break;
1376 case Button3:
1377 break;
1378 default:
1379 return;
1380 }
1381 }
1382
1383 /* mouse */
1384 enum { client_click, root_click };
1385 struct button {
1386 unsigned int action;
1387 unsigned int mask;
1388 unsigned int button;
1389 void (*func)(struct swm_region *r, union arg *);
1390 union arg args;
1391 } buttons[] = {
1392 /* action key mouse button func args */
1393 { client_click, MODKEY, Button1, click, {.id=Button1} },
1394 };
1395
1396 void
1397 updatenumlockmask(void)
1398 {
1399 unsigned int i, j;
1400 XModifierKeymap *modmap;
1401
1402 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1403 numlockmask = 0;
1404 modmap = XGetModifierMapping(display);
1405 for (i = 0; i < 8; i++)
1406 for (j = 0; j < modmap->max_keypermod; j++)
1407 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1408 == XKeysymToKeycode(display, XK_Num_Lock))
1409 numlockmask = (1 << i);
1410
1411 XFreeModifiermap(modmap);
1412 }
1413
1414 void
1415 grabkeys(void)
1416 {
1417 unsigned int i, j, k;
1418 KeyCode code;
1419 unsigned int modifiers[] =
1420 { 0, LockMask, numlockmask, numlockmask | LockMask };
1421
1422 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1423 updatenumlockmask();
1424
1425 for (k = 0; k < ScreenCount(display); k++) {
1426 if (TAILQ_EMPTY(&screens[k].rl))
1427 continue;
1428 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1429 for (i = 0; i < LENGTH(keys); i++) {
1430 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1431 for (j = 0; j < LENGTH(modifiers); j++)
1432 XGrabKey(display, code,
1433 keys[i].mod | modifiers[j],
1434 screens[k].root, True,
1435 GrabModeAsync, GrabModeAsync);
1436 }
1437 }
1438 }
1439
1440 void
1441 grabbuttons(struct ws_win *win, int focused)
1442 {
1443 unsigned int i, j;
1444 unsigned int modifiers[] =
1445 { 0, LockMask, numlockmask, numlockmask|LockMask };
1446
1447 updatenumlockmask();
1448 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1449 if(focused) {
1450 for (i = 0; i < LENGTH(buttons); i++)
1451 if (buttons[i].action == client_click)
1452 for (j = 0; j < LENGTH(modifiers); j++)
1453 XGrabButton(display, buttons[i].button,
1454 buttons[i].mask | modifiers[j],
1455 win->id, False, BUTTONMASK,
1456 GrabModeAsync, GrabModeSync, None,
1457 None);
1458 } else
1459 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1460 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1461 }
1462
1463 void
1464 expose(XEvent *e)
1465 {
1466 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1467 }
1468
1469 void
1470 keypress(XEvent *e)
1471 {
1472 unsigned int i;
1473 KeySym keysym;
1474 XKeyEvent *ev = &e->xkey;
1475
1476 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1477
1478 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1479 for (i = 0; i < LENGTH(keys); i++)
1480 if (keysym == keys[i].keysym
1481 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1482 && keys[i].func)
1483 keys[i].func(root_to_region(ev->root),
1484 &(keys[i].args));
1485 }
1486
1487 void
1488 buttonpress(XEvent *e)
1489 {
1490 XButtonPressedEvent *ev = &e->xbutton;
1491
1492 struct ws_win *win;
1493 int i, action;
1494
1495 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1496
1497 action = root_click;
1498 if ((win = find_window(ev->window)) == NULL)
1499 return;
1500 else {
1501 focus_win(win);
1502 action = client_click;
1503 }
1504
1505 for (i = 0; i < LENGTH(buttons); i++)
1506 if (action == buttons[i].action && buttons[i].func &&
1507 buttons[i].button == ev->button &&
1508 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
1509 buttons[i].func(root_to_region(ev->root),
1510 &buttons[i].args);
1511 }
1512
1513 void
1514 set_win_state(struct ws_win *win, long state)
1515 {
1516 long data[] = {state, None};
1517
1518 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1519
1520 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1521 (unsigned char *)data, 2);
1522 }
1523
1524 struct ws_win *
1525 manage_window(Window id, struct workspace *ws)
1526 {
1527 Window trans;
1528 struct ws_win *win;
1529 XClassHint ch;
1530
1531 TAILQ_FOREACH(win, &ws->winlist, entry) {
1532 if (win->id == id)
1533 return (win); /* already being managed */
1534 }
1535
1536 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1537 errx(1, "calloc: failed to allocate memory for new window");
1538
1539 win->id = id;
1540 win->ws = ws;
1541 win->s = ws->r->s; /* this never changes */
1542 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
1543
1544 /* make new win focused */
1545 focus_win(win);
1546
1547 XGetTransientForHint(display, win->id, &trans);
1548 if (trans) {
1549 win->transient = trans;
1550 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
1551 (unsigned)win->id, win->transient);
1552 }
1553 XGetWindowAttributes(display, id, &win->wa);
1554 win->g.w = win->wa.width;
1555 win->g.h = win->wa.height;
1556 win->g.x = win->wa.x;
1557 win->g.y = win->wa.y;
1558
1559 /* XXX make this a table */
1560 bzero(&ch, sizeof ch);
1561 if (XGetClassHint(display, win->id, &ch)) {
1562 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1563 if (!strcmp(ch.res_class, "MPlayer") && !strcmp(ch.res_name, "xv")) {
1564 win->floating = 1;
1565 }
1566 if (ch.res_class)
1567 XFree(ch.res_class);
1568 if (ch.res_name)
1569 XFree(ch.res_name);
1570 }
1571
1572 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
1573 PropertyChangeMask | StructureNotifyMask);
1574
1575 set_win_state(win, NormalState);
1576
1577 return (win);
1578 }
1579
1580 void
1581 configurerequest(XEvent *e)
1582 {
1583 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1584 struct ws_win *win;
1585 int new = 1;
1586 XWindowChanges wc;
1587
1588 if ((win = find_window(ev->window)) == NULL)
1589 new = 1;
1590
1591 if (new) {
1592 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
1593 ev->window);
1594 bzero(&wc, sizeof wc);
1595 wc.x = ev->x;
1596 wc.y = ev->y;
1597 wc.width = ev->width;
1598 wc.height = ev->height;
1599 wc.border_width = ev->border_width;
1600 wc.sibling = ev->above;
1601 wc.stack_mode = ev->detail;
1602 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
1603 } else {
1604 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
1605 ev->window);
1606 if (win->floating) {
1607 if (ev->value_mask & CWX)
1608 win->g.x = ev->x;
1609 if (ev->value_mask & CWY)
1610 win->g.y = ev->y;
1611 if (ev->value_mask & CWWidth)
1612 win->g.w = ev->width;
1613 if (ev->value_mask & CWHeight)
1614 win->g.h = ev->height;
1615 if (win->ws->r != NULL) {
1616 /* this seems to be full screen */
1617 if (win->g.w > WIDTH(win->ws->r)) {
1618 /* kill border */
1619 win->g.x -= 1;
1620 win->g.w += 1;
1621 }
1622 if (win->g.h > HEIGHT(win->ws->r)) {
1623 /* kill border */
1624 win->g.y -= 1;
1625 win->g.h += 1;
1626 }
1627 }
1628 if ((ev->value_mask & (CWX|CWY)) &&
1629 !(ev->value_mask & (CWWidth|CWHeight)))
1630 config_win(win);
1631 XMoveResizeWindow(display, win->id,
1632 win->g.x, win->g.y, win->g.w, win->g.h);
1633 } else
1634 config_win(win);
1635 }
1636 }
1637
1638 void
1639 configurenotify(XEvent *e)
1640 {
1641 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1642 e->xconfigure.window);
1643 }
1644
1645 void
1646 destroynotify(XEvent *e)
1647 {
1648 struct ws_win *win;
1649 XDestroyWindowEvent *ev = &e->xdestroywindow;
1650 struct workspace *ws;
1651
1652 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1653
1654 if ((win = find_window(ev->window)) != NULL) {
1655 ws = win->ws;
1656 /* find a window to focus */
1657 if (ws->focus == win)
1658 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1659 if (ws->focus == NULL)
1660 ws->focus = TAILQ_FIRST(&ws->winlist);
1661 if (ws->focus == win)
1662 ws->focus = NULL;
1663 if (cur_focus == win)
1664 focus_win(ws->focus);
1665
1666 TAILQ_REMOVE(&ws->winlist, win, entry);
1667 set_win_state(win, WithdrawnState);
1668 free(win);
1669 }
1670 stack();
1671 }
1672
1673 void
1674 enternotify(XEvent *e)
1675 {
1676 XCrossingEvent *ev = &e->xcrossing;
1677 struct ws_win *win;
1678
1679 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1680
1681 if (ignore_enter) {
1682 /* eat event(r) to prevent autofocus */
1683 ignore_enter--;
1684 return;
1685 }
1686
1687 if ((win = find_window(ev->window)) != NULL)
1688 focus_win(win);
1689 }
1690
1691 void
1692 focusin(XEvent *e)
1693 {
1694 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
1695 }
1696
1697 void
1698 mappingnotify(XEvent *e)
1699 {
1700 XMappingEvent *ev = &e->xmapping;
1701
1702 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1703
1704 XRefreshKeyboardMapping(ev);
1705 if (ev->request == MappingKeyboard)
1706 grabkeys();
1707 }
1708
1709 void
1710 maprequest(XEvent *e)
1711 {
1712 XMapRequestEvent *ev = &e->xmaprequest;
1713 XWindowAttributes wa;
1714 struct swm_region *r;
1715
1716 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1717 e->xmaprequest.window);
1718
1719 if (!XGetWindowAttributes(display, ev->window, &wa))
1720 return;
1721 if (wa.override_redirect)
1722 return;
1723 r = root_to_region(wa.root);
1724 manage_window(e->xmaprequest.window, r->ws);
1725 stack();
1726 }
1727
1728 void
1729 propertynotify(XEvent *e)
1730 {
1731 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1732 e->xproperty.window);
1733 }
1734
1735 void
1736 unmapnotify(XEvent *e)
1737 {
1738 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1739 }
1740
1741 void
1742 visibilitynotify(XEvent *e)
1743 {
1744 int i;
1745 struct swm_region *r;
1746
1747 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
1748 e->xvisibility.window);
1749 if (e->xvisibility.state == VisibilityUnobscured)
1750 for (i = 0; i < ScreenCount(display); i++)
1751 TAILQ_FOREACH(r, &screens[i].rl, entry)
1752 if (e->xvisibility.window == r->bar_window)
1753 bar_update();
1754 }
1755
1756 void (*handler[LASTEvent])(XEvent *) = {
1757 [Expose] = expose,
1758 [KeyPress] = keypress,
1759 [ButtonPress] = buttonpress,
1760 [ConfigureRequest] = configurerequest,
1761 [ConfigureNotify] = configurenotify,
1762 [DestroyNotify] = destroynotify,
1763 [EnterNotify] = enternotify,
1764 [FocusIn] = focusin,
1765 [MappingNotify] = mappingnotify,
1766 [MapRequest] = maprequest,
1767 [PropertyNotify] = propertynotify,
1768 [UnmapNotify] = unmapnotify,
1769 [VisibilityNotify] = visibilitynotify,
1770 };
1771
1772 int
1773 xerror_start(Display *d, XErrorEvent *ee)
1774 {
1775 other_wm = 1;
1776 return (-1);
1777 }
1778
1779 int
1780 xerror(Display *d, XErrorEvent *ee)
1781 {
1782 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1783 return (-1);
1784 }
1785
1786 int
1787 active_wm(void)
1788 {
1789 other_wm = 0;
1790 xerrorxlib = XSetErrorHandler(xerror_start);
1791
1792 /* this causes an error if some other window manager is running */
1793 XSelectInput(display, DefaultRootWindow(display),
1794 SubstructureRedirectMask);
1795 XSync(display, False);
1796 if (other_wm)
1797 return (1);
1798
1799 XSetErrorHandler(xerror);
1800 XSync(display, False);
1801 return (0);
1802 }
1803
1804 long
1805 getstate(Window w)
1806 {
1807 int format, status;
1808 long result = -1;
1809 unsigned char *p = NULL;
1810 unsigned long n, extra;
1811 Atom real;
1812
1813 astate = XInternAtom(display, "WM_STATE", False);
1814 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
1815 &real, &format, &n, &extra, (unsigned char **)&p);
1816 if (status != Success)
1817 return (-1);
1818 if (n != 0)
1819 result = *p;
1820 XFree(p);
1821 return (result);
1822 }
1823
1824 void
1825 new_region(struct swm_screen *s, struct workspace *ws,
1826 int x, int y, int w, int h)
1827 {
1828 struct swm_region *r;
1829
1830 DNPRINTF(SWM_D_MISC, "new region on screen %d: %dx%d (%d, %d)\n",
1831 s->idx, x, y, w, h);
1832
1833 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
1834 errx(1, "calloc: failed to allocate memory for screen");
1835
1836 X(r) = x;
1837 Y(r) = y;
1838 WIDTH(r) = w;
1839 HEIGHT(r) = h;
1840 r->s = s;
1841 r->ws = ws;
1842 ws->r = r;
1843 TAILQ_INSERT_TAIL(&s->rl, r, entry);
1844 bar_setup(r);
1845 }
1846
1847 void
1848 setup_screens(void)
1849 {
1850 #ifdef SWM_XRR_HAS_CRTC
1851 XRRCrtcInfo *ci;
1852 XRRScreenResources *sr;
1853 int c;
1854 #endif /* SWM_XRR_HAS_CRTC */
1855 Window d1, d2, *wins = NULL;
1856 XWindowAttributes wa;
1857 struct swm_region *r;
1858 unsigned int no;
1859 int errorbase, major, minor;
1860 int ncrtc = 0, w = 0;
1861 int i, j, k;
1862 struct workspace *ws;
1863
1864 if ((screens = calloc(ScreenCount(display),
1865 sizeof(struct swm_screen))) == NULL)
1866 errx(1, "calloc: screens");
1867
1868 /* map physical screens */
1869 for (i = 0; i < ScreenCount(display); i++) {
1870 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
1871 screens[i].idx = i;
1872 TAILQ_INIT(&screens[i].rl);
1873 screens[i].root = RootWindow(display, i);
1874
1875 /* set default colors */
1876 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
1877 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
1878 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
1879 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
1880 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
1881
1882 /* init all workspaces */
1883 for (j = 0; j < SWM_WS_MAX; j++) {
1884 ws = &screens[i].ws[j];
1885 ws->idx = j;
1886 ws->restack = 1;
1887 ws->focus = NULL;
1888 ws->r = NULL;
1889 TAILQ_INIT(&ws->winlist);
1890
1891 for (k = 0; layouts[k].l_stack != NULL; k++)
1892 if (layouts[k].l_config != NULL)
1893 layouts[k].l_config(ws,
1894 SWM_ARG_ID_STACKINIT);
1895 ws->cur_layout = &layouts[0];
1896 }
1897
1898 /* map virtual screens onto physical screens */
1899 screens[i].xrandr_support = XRRQueryExtension(display,
1900 &xrandr_eventbase, &errorbase);
1901 if (screens[i].xrandr_support)
1902 if (XRRQueryVersion(display, &major, &minor) &&
1903 major < 1)
1904 screens[i].xrandr_support = 0;
1905
1906 #if 0 /* not ready for dynamic screen changes */
1907 if (screens[i].xrandr_support)
1908 XRRSelectInput(display,
1909 screens[r->s].root,
1910 RRScreenChangeNotifyMask);
1911 #endif
1912
1913 /* grab existing windows (before we build the bars)*/
1914 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
1915 continue;
1916
1917 #ifdef SWM_XRR_HAS_CRTC
1918 sr = XRRGetScreenResources(display, screens[i].root);
1919 if (sr == NULL)
1920 new_region(&screens[i], &screens[i].ws[w],
1921 0, 0, DisplayWidth(display, i),
1922 DisplayHeight(display, i));
1923 else
1924 ncrtc = sr->ncrtc;
1925
1926 for (c = 0; c < ncrtc; c++) {
1927 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
1928 if (ci->noutput == 0)
1929 continue;
1930
1931 if (ci != NULL && ci->mode == None)
1932 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1933 DisplayWidth(display, i),
1934 DisplayHeight(display, i));
1935 else
1936 new_region(&screens[i], &screens[i].ws[w],
1937 ci->x, ci->y, ci->width, ci->height);
1938 w++;
1939 }
1940 XRRFreeCrtcInfo(ci);
1941 XRRFreeScreenResources(sr);
1942 #else
1943 new_region(&screens[i], &screens[i].ws[w], 0, 0,
1944 DisplayWidth(display, i),
1945 DisplayHeight(display, i));
1946 #endif /* SWM_XRR_HAS_CRTC */
1947
1948 /* attach windows to a region */
1949 /* normal windows */
1950 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
1951 errx(1, "no regions on screen %d", i);
1952
1953 for (i = 0; i < no; i++) {
1954 XGetWindowAttributes(display, wins[i], &wa);
1955 if (!XGetWindowAttributes(display, wins[i], &wa) ||
1956 wa.override_redirect ||
1957 XGetTransientForHint(display, wins[i], &d1))
1958 continue;
1959
1960 if (wa.map_state == IsViewable ||
1961 getstate(wins[i]) == NormalState)
1962 manage_window(wins[i], r->ws);
1963 }
1964 /* transient windows */
1965 for (i = 0; i < no; i++) {
1966 if (!XGetWindowAttributes(display, wins[i], &wa))
1967 continue;
1968
1969 if (XGetTransientForHint(display, wins[i], &d1) &&
1970 (wa.map_state == IsViewable || getstate(wins[i]) ==
1971 NormalState))
1972 manage_window(wins[i], r->ws);
1973 }
1974 if (wins) {
1975 XFree(wins);
1976 wins = NULL;
1977 }
1978 }
1979 }
1980
1981 int
1982 main(int argc, char *argv[])
1983 {
1984 struct passwd *pwd;
1985 char conf[PATH_MAX], *cfile = NULL;
1986 struct stat sb;
1987 XEvent e;
1988 int xfd;
1989 fd_set rd;
1990
1991 start_argv = argv;
1992 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1993 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1994 warnx("no locale support");
1995
1996 if (!(display = XOpenDisplay(0)))
1997 errx(1, "can not open display");
1998
1999 if (active_wm())
2000 errx(1, "other wm running");
2001
2002 astate = XInternAtom(display, "WM_STATE", False);
2003
2004 /* look for local and global conf file */
2005 pwd = getpwuid(getuid());
2006 if (pwd == NULL)
2007 errx(1, "invalid user %d", getuid());
2008
2009 setup_screens();
2010
2011 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2012 if (stat(conf, &sb) != -1) {
2013 if (S_ISREG(sb.st_mode))
2014 cfile = conf;
2015 } else {
2016 /* try global conf file */
2017 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2018 if (!stat(conf, &sb))
2019 if (S_ISREG(sb.st_mode))
2020 cfile = conf;
2021 }
2022 if (cfile)
2023 conf_load(cfile);
2024 bar_refresh();
2025
2026 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2027
2028 grabkeys();
2029 stack();
2030
2031 xfd = ConnectionNumber(display);
2032 while (running) {
2033 FD_ZERO(&rd);
2034 FD_SET(xfd, &rd);
2035 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2036 if (errno != EINTR)
2037 errx(1, "select failed");
2038 if (bar_alarm) {
2039 bar_alarm = 0;
2040 bar_update();
2041 }
2042 while(XPending(display)) {
2043 XNextEvent(display, &e);
2044 if (handler[e.type])
2045 handler[e.type](&e);
2046 }
2047 }
2048
2049 XCloseDisplay(display);
2050
2051 return (0);
2052 }