]> code.delx.au - spectrwm/blob - scrotwm.c
572be960929a8e78c1b9150886e872c9c9d13b87
[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 static const char *cvstag = "$scrotwm$";
52
53 #define SWM_VERSION "0.7"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <locale.h>
61 #include <unistd.h>
62 #include <time.h>
63 #include <signal.h>
64 #include <string.h>
65 #include <util.h>
66 #include <pwd.h>
67 #include <ctype.h>
68
69 #include <sys/types.h>
70 #include <sys/time.h>
71 #include <sys/stat.h>
72 #include <sys/wait.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
76
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
80 #include <X11/Xlib.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
84
85 #if RANDR_MAJOR < 1
86 # error XRandR versions less than 1.0 are not supported
87 #endif
88
89 #if RANDR_MAJOR >= 1
90 #if RANDR_MINOR >= 2
91 #define SWM_XRR_HAS_CRTC
92 #endif
93 #endif
94
95 /* #define SWM_DEBUG */
96 #ifdef SWM_DEBUG
97 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
98 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
99 #define SWM_D_MISC 0x0001
100 #define SWM_D_EVENT 0x0002
101 #define SWM_D_WS 0x0004
102 #define SWM_D_FOCUS 0x0008
103 #define SWM_D_MOVE 0x0010
104 #define SWM_D_STACK 0x0020
105 #define SWM_D_MOUSE 0x0040
106 #define SWM_D_PROP 0x0080
107 #define SWM_D_CLASS 0x0100
108
109 u_int32_t swm_debug = 0
110 | SWM_D_MISC
111 | SWM_D_EVENT
112 | SWM_D_WS
113 | SWM_D_FOCUS
114 | SWM_D_MOVE
115 | SWM_D_STACK
116 | SWM_D_MOUSE
117 | SWM_D_PROP
118 | SWM_D_CLASS
119 ;
120 #else
121 #define DPRINTF(x...)
122 #define DNPRINTF(n,x...)
123 #endif
124
125 #define LENGTH(x) (sizeof x / sizeof x[0])
126 #define MODKEY Mod1Mask
127 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
128 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
129 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
130 #define SWM_PROPLEN (16)
131 #define X(r) (r)->g.x
132 #define Y(r) (r)->g.y
133 #define WIDTH(r) (r)->g.w
134 #define HEIGHT(r) (r)->g.h
135
136 #ifndef SWM_LIB
137 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
138 #endif
139
140 char **start_argv;
141 Atom astate;
142 int (*xerrorxlib)(Display *, XErrorEvent *);
143 int other_wm;
144 int running = 1;
145 int ss_enabled = 0;
146 int xrandr_eventbase;
147 int ignore_enter = 0;
148 unsigned int numlockmask = 0;
149 Display *display;
150
151 int cycle_empty = 0;
152 int cycle_visible = 0;
153
154 /* dialog windows */
155 double dialog_ratio = .6;
156 /* status bar */
157 #define SWM_BAR_MAX (128)
158 char *bar_argv[] = { NULL, NULL };
159 int bar_pipe[2];
160 char bar_ext[SWM_BAR_MAX];
161 char bar_vertext[SWM_BAR_MAX];
162 int bar_version = 0;
163 sig_atomic_t bar_alarm = 0;
164 int bar_delay = 30;
165 int bar_enabled = 1;
166 int bar_extra = 1;
167 int bar_extra_running = 0;
168 int bar_verbose = 1;
169 int bar_height = 0;
170 pid_t bar_pid;
171 GC bar_gc;
172 XGCValues bar_gcv;
173 int bar_fidx = 0;
174 XFontStruct *bar_fs;
175 char *bar_fonts[] = {
176 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
177 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
178 NULL
179 };
180
181 /* terminal + args */
182 char *spawn_term[] = { "xterm", NULL };
183 char *spawn_screenshot[] = { "screenshot.sh", NULL, NULL };
184 char *spawn_lock[] = { "xlock", NULL };
185 char *spawn_menu[] = { "dmenu_run", "-fn", NULL, "-nb", NULL,
186 "-nf", NULL, "-sb", NULL, "-sf", NULL, NULL };
187
188 #define SWM_MENU_FN (2)
189 #define SWM_MENU_NB (4)
190 #define SWM_MENU_NF (6)
191 #define SWM_MENU_SB (8)
192 #define SWM_MENU_SF (10)
193
194 /* layout manager data */
195 struct swm_geometry {
196 int x;
197 int y;
198 int w;
199 int h;
200 };
201
202 struct swm_screen;
203 struct workspace;
204
205 /* virtual "screens" */
206 struct swm_region {
207 TAILQ_ENTRY(swm_region) entry;
208 struct swm_geometry g;
209 struct workspace *ws; /* current workspace on this region */
210 struct swm_screen *s; /* screen idx */
211 Window bar_window;
212 };
213 TAILQ_HEAD(swm_region_list, swm_region);
214
215 struct ws_win {
216 TAILQ_ENTRY(ws_win) entry;
217 Window id;
218 struct swm_geometry g;
219 int got_focus;
220 int floating;
221 int transient;
222 int manual;
223 struct workspace *ws; /* always valid */
224 struct swm_screen *s; /* always valid, never changes */
225 XWindowAttributes wa;
226 XSizeHints sh;
227 XClassHint ch;
228 };
229 TAILQ_HEAD(ws_win_list, ws_win);
230
231 /* layout handlers */
232 void stack(void);
233 void vertical_config(struct workspace *, int);
234 void vertical_stack(struct workspace *, struct swm_geometry *);
235 void horizontal_config(struct workspace *, int);
236 void horizontal_stack(struct workspace *, struct swm_geometry *);
237 void max_stack(struct workspace *, struct swm_geometry *);
238
239 void grabbuttons(struct ws_win *, int);
240
241 struct layout {
242 void (*l_stack)(struct workspace *, struct swm_geometry *);
243 void (*l_config)(struct workspace *, int);
244 } layouts[] = {
245 /* stack, configure */
246 { vertical_stack, vertical_config},
247 { horizontal_stack, horizontal_config},
248 { max_stack, NULL},
249 { NULL, NULL},
250 };
251
252 #define SWM_H_SLICE (32)
253 #define SWM_V_SLICE (32)
254
255 /* define work spaces */
256 struct workspace {
257 int idx; /* workspace index */
258 int restack; /* restack on switch */
259 struct layout *cur_layout; /* current layout handlers */
260 struct ws_win *focus; /* may be NULL */
261 struct swm_region *r; /* may be NULL */
262 struct ws_win_list winlist; /* list of windows in ws */
263
264 /* stacker state */
265 struct {
266 int horizontal_msize;
267 int horizontal_mwin;
268 int vertical_msize;
269 int vertical_mwin;
270 } l_state;
271 };
272
273 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
274 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
275
276 /* physical screen mapping */
277 #define SWM_WS_MAX (10) /* XXX Too small? */
278 struct swm_screen {
279 int idx; /* screen index */
280 struct swm_region_list rl; /* list of regions on this screen */
281 Window root;
282 int xrandr_support;
283 struct workspace ws[SWM_WS_MAX];
284
285 /* colors */
286 struct {
287 unsigned long color;
288 char *name;
289 } c[SWM_S_COLOR_MAX];
290 };
291 struct swm_screen *screens;
292 int num_screens;
293
294 struct ws_win *cur_focus = NULL;
295
296 /* args to functions */
297 union arg {
298 int id;
299 #define SWM_ARG_ID_FOCUSNEXT (0)
300 #define SWM_ARG_ID_FOCUSPREV (1)
301 #define SWM_ARG_ID_FOCUSMAIN (2)
302 #define SWM_ARG_ID_SWAPNEXT (3)
303 #define SWM_ARG_ID_SWAPPREV (4)
304 #define SWM_ARG_ID_SWAPMAIN (5)
305 #define SWM_ARG_ID_MASTERSHRINK (6)
306 #define SWM_ARG_ID_MASTERGROW (7)
307 #define SWM_ARG_ID_MASTERADD (8)
308 #define SWM_ARG_ID_MASTERDEL (9)
309 #define SWM_ARG_ID_STACKRESET (10)
310 #define SWM_ARG_ID_STACKINIT (11)
311 #define SWM_ARG_ID_CYCLEWS_UP (12)
312 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
313 #define SWM_ARG_ID_CYCLESC_UP (14)
314 #define SWM_ARG_ID_CYCLESC_DOWN (15)
315 #define SWM_ARG_ID_SS_ALL (0)
316 #define SWM_ARG_ID_SS_WINDOW (1)
317 #define SWM_ARG_ID_DONTCENTER (0)
318 #define SWM_ARG_ID_CENTER (1)
319 char **argv;
320 };
321
322 /* quirks */
323 struct quirk {
324 char *class;
325 char *name;
326 unsigned long quirk;
327 #define SWM_Q_FLOAT (1<<0)
328 } quirks[] = {
329 { "MPlayer", "xv", SWM_Q_FLOAT },
330 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT },
331 { NULL, NULL, 0},
332 };
333
334 /* events */
335 void expose(XEvent *);
336 void keypress(XEvent *);
337 void buttonpress(XEvent *);
338 void configurerequest(XEvent *);
339 void configurenotify(XEvent *);
340 void destroynotify(XEvent *);
341 void enternotify(XEvent *);
342 void focusin(XEvent *);
343 void mappingnotify(XEvent *);
344 void maprequest(XEvent *);
345 void propertynotify(XEvent *);
346 void unmapnotify(XEvent *);
347 void visibilitynotify(XEvent *);
348
349 void (*handler[LASTEvent])(XEvent *) = {
350 [Expose] = expose,
351 [KeyPress] = keypress,
352 [ButtonPress] = buttonpress,
353 [ConfigureRequest] = configurerequest,
354 [ConfigureNotify] = configurenotify,
355 [DestroyNotify] = destroynotify,
356 [EnterNotify] = enternotify,
357 [FocusIn] = focusin,
358 [MappingNotify] = mappingnotify,
359 [MapRequest] = maprequest,
360 [PropertyNotify] = propertynotify,
361 [UnmapNotify] = unmapnotify,
362 [VisibilityNotify] = visibilitynotify,
363 };
364
365 unsigned long
366 name_to_color(char *colorname)
367 {
368 Colormap cmap;
369 Status status;
370 XColor screen_def, exact_def;
371 unsigned long result = 0;
372 char cname[32] = "#";
373
374 cmap = DefaultColormap(display, screens[0].idx);
375 status = XAllocNamedColor(display, cmap, colorname,
376 &screen_def, &exact_def);
377 if (!status) {
378 strlcat(cname, colorname + 2, sizeof cname - 1);
379 status = XAllocNamedColor(display, cmap, cname, &screen_def,
380 &exact_def);
381 }
382 if (status)
383 result = screen_def.pixel;
384 else
385 fprintf(stderr, "color '%s' not found.\n", colorname);
386
387 return (result);
388 }
389
390 void
391 setscreencolor(char *val, int i, int c)
392 {
393 if (i > 0 && i <= ScreenCount(display)) {
394 screens[i - 1].c[c].color = name_to_color(val);
395 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
396 errx(1, "strdup");
397 } else if (i == -1) {
398 for (i = 0; i < ScreenCount(display); i++)
399 screens[i].c[c].color = name_to_color(val);
400 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
401 errx(1, "strdup");
402 } else
403 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
404 i, ScreenCount(display));
405 }
406
407 void new_region(struct swm_screen *, struct workspace *,
408 int, int, int, int);
409
410 void
411 custom_region(char *val)
412 {
413 unsigned int sidx, x, y, w, h;
414
415 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
416 errx(1, "invalid custom region, "
417 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
418 if (sidx < 1 || sidx > ScreenCount(display))
419 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
420 sidx, ScreenCount(display));
421 sidx--;
422
423 if (w < 1 || h < 1)
424 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
425
426 if (x < 0 || x > DisplayWidth(display, sidx) ||
427 y < 0 || y > DisplayHeight(display, sidx) ||
428 w + x > DisplayWidth(display, sidx) ||
429 h + y > DisplayHeight(display, sidx))
430 errx(1, "region %ux%u+%u+%u not within screen boundaries "
431 "(%ux%u)\n", w, h, x, y,
432 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
433
434 new_region(&screens[sidx], NULL, x, y, w, h);
435 }
436
437 int
438 varmatch(char *var, char *name, int *index)
439 {
440 char *p, buf[5];
441 int i;
442
443 i = strncmp(var, name, 255);
444 if (index == NULL)
445 return (i);
446
447 *index = -1;
448 if (i <= 0)
449 return (i);
450 p = var + strlen(name);
451 if (*p++ != '[')
452 return (i);
453 bzero(buf, sizeof buf);
454 i = 0;
455 while (isdigit(*p) && i < sizeof buf)
456 buf[i++] = *p++;
457 if (i == 0 || i >= sizeof buf || *p != ']')
458 return (1);
459 *index = strtonum(buf, 0, 99, NULL);
460 return (0);
461 }
462
463 /* conf file stuff */
464 #define SWM_CONF_WS "\n= \t"
465 #define SWM_CONF_FILE "scrotwm.conf"
466 int
467 conf_load(char *filename)
468 {
469 FILE *config;
470 char *line, *cp, *var, *val;
471 size_t len, lineno = 0;
472 int i, sc;
473
474 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
475
476 if (filename == NULL)
477 return (1);
478
479 if ((config = fopen(filename, "r")) == NULL)
480 return (1);
481
482 for (sc = ScreenCount(display);;) {
483 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
484 if (feof(config))
485 break;
486 cp = line;
487 cp += (long)strspn(cp, SWM_CONF_WS);
488 if (cp[0] == '\0') {
489 /* empty line */
490 free(line);
491 continue;
492 }
493 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
494 break;
495 cp += (long)strspn(cp, SWM_CONF_WS);
496 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
497 break;
498
499 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
500 switch (var[0]) {
501 case 'b':
502 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
503 bar_enabled = atoi(val);
504 else if (!varmatch(var, "bar_border", &i))
505 setscreencolor(val, i, SWM_S_COLOR_BAR_BORDER);
506 else if (!varmatch(var, "bar_color", &i))
507 setscreencolor(val, i, SWM_S_COLOR_BAR);
508 else if (!varmatch(var, "bar_font_color", &i))
509 setscreencolor(val, i, SWM_S_COLOR_BAR_FONT);
510 else if (!strncmp(var, "bar_font", strlen("bar_font")))
511 asprintf(&bar_fonts[0], "%s", val);
512 else if (!strncmp(var, "bar_action", strlen("bar_action")))
513 asprintf(&bar_argv[0], "%s", val);
514 else if (!strncmp(var, "bar_delay", strlen("bar_delay")))
515 bar_delay = atoi(val);
516 else
517 goto bad;
518 break;
519
520 case 'c':
521 if (!varmatch(var, "color_focus", &i))
522 setscreencolor(val, i, SWM_S_COLOR_FOCUS);
523 else if (!varmatch(var, "color_unfocus", &i))
524 setscreencolor(val, i, SWM_S_COLOR_UNFOCUS);
525 else if (!strncmp(var, "cycle_empty", strlen("cycle_empty")))
526 cycle_visible = atoi(val);
527 else if (!strncmp(var, "cycle_visible", strlen("cycle_visible")))
528 cycle_visible = atoi(val);
529 else
530 goto bad;
531 break;
532
533 case 'd':
534 if (!strncmp(var, "dialog_ratio",
535 strlen("dialog_ratio"))) {
536 dialog_ratio = atof(val);
537 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
538 dialog_ratio = .6;
539 } else
540 goto bad;
541 break;
542
543 case 'r':
544 if (!strncmp(var, "region", strlen("region")))
545 custom_region(val);
546 else
547 goto bad;
548 break;
549
550 case 's':
551 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
552 asprintf(&spawn_term[0], "%s", val);
553 if (!strncmp(var, "screenshot_enabled",
554 strlen("screenshot_enabled")))
555 ss_enabled = atoi(val);
556 if (!strncmp(var, "screenshot_app",
557 strlen("screenshot_app")))
558 asprintf(&spawn_screenshot[0], "%s", val);
559 break;
560 default:
561 goto bad;
562 }
563 free(line);
564 }
565
566 fclose(config);
567 return (0);
568
569 bad:
570 errx(1, "invalid conf file entry: %s=%s", var, val);
571 }
572
573 void
574 socket_setnonblock(int fd)
575 {
576 int flags;
577
578 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
579 err(1, "fcntl F_GETFL");
580 flags |= O_NONBLOCK;
581 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
582 err(1, "fcntl F_SETFL");
583 }
584
585 void
586 bar_print(struct swm_region *r, char *s)
587 {
588 XClearWindow(display, r->bar_window);
589 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
590 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
591 strlen(s));
592 }
593
594 void
595 bar_extra_stop(void)
596 {
597 if (bar_pipe[0]) {
598 close(bar_pipe[0]);
599 bzero(bar_pipe, sizeof bar_pipe);
600 }
601 if (bar_pid) {
602 kill(bar_pid, SIGTERM);
603 bar_pid = 0;
604 }
605 strlcpy(bar_ext, "", sizeof bar_ext);
606 bar_extra = 0;
607 }
608
609 void
610 bar_update(void)
611 {
612 time_t tmt;
613 struct tm tm;
614 struct swm_region *r;
615 int i, x;
616 size_t len;
617 char s[SWM_BAR_MAX];
618 char loc[SWM_BAR_MAX];
619 char *b;
620
621 if (bar_enabled == 0)
622 return;
623
624 if (bar_extra && bar_extra_running) {
625 /* ignore short reads; it'll correct itself */
626 while ((b = fgetln(stdin, &len)) != NULL)
627 if (b && b[len - 1] == '\n') {
628 b[len - 1] = '\0';
629 strlcpy(bar_ext, b, sizeof bar_ext);
630 }
631 if (b == NULL && errno != EAGAIN) {
632 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
633 errno, strerror(errno));
634 bar_extra_stop();
635 }
636 } else
637 strlcpy(bar_ext, "", sizeof bar_ext);
638
639 time(&tmt);
640 localtime_r(&tmt, &tm);
641 strftime(s, sizeof s, "%a %b %d %R %Z %Y", &tm);
642 for (i = 0; i < ScreenCount(display); i++) {
643 x = 1;
644 TAILQ_FOREACH(r, &screens[i].rl, entry) {
645 snprintf(loc, sizeof loc, "%s %d:%d %s %s",
646 s, x++, r->ws->idx + 1, bar_ext, bar_vertext);
647 bar_print(r, loc);
648 }
649 }
650 XSync(display, False);
651 alarm(bar_delay);
652 }
653
654 void
655 bar_signal(int sig)
656 {
657 bar_alarm = 1;
658 }
659
660 void
661 bar_toggle(struct swm_region *r, union arg *args)
662 {
663 struct swm_region *tmpr;
664 int i, j, sc = ScreenCount(display);
665
666 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
667
668 if (bar_enabled) {
669 for (i = 0; i < sc; i++)
670 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
671 XUnmapWindow(display, tmpr->bar_window);
672 } else {
673 for (i = 0; i < sc; i++)
674 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
675 XMapRaised(display, tmpr->bar_window);
676 }
677 bar_enabled = !bar_enabled;
678 XSync(display, False);
679 for (i = 0; i < sc; i++)
680 for (j = 0; j < SWM_WS_MAX; j++)
681 screens[i].ws[j].restack = 1;
682
683 stack();
684 /* must be after stack */
685 for (i = 0; i < sc; i++)
686 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
687 bar_update();
688 }
689
690 void
691 bar_refresh(void)
692 {
693 XSetWindowAttributes wa;
694 struct swm_region *r;
695 int i;
696
697 /* do this here because the conf file is in memory */
698 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
699 /* launch external status app */
700 bar_extra_running = 1;
701 if (pipe(bar_pipe) == -1)
702 err(1, "pipe error");
703 socket_setnonblock(bar_pipe[0]);
704 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
705 if (dup2(bar_pipe[0], 0) == -1)
706 errx(1, "dup2");
707 if (dup2(bar_pipe[1], 1) == -1)
708 errx(1, "dup2");
709 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
710 err(1, "could not disable SIGPIPE");
711 switch (bar_pid = fork()) {
712 case -1:
713 err(1, "cannot fork");
714 break;
715 case 0: /* child */
716 close(bar_pipe[0]);
717 execvp(bar_argv[0], bar_argv);
718 err(1, "%s external app failed", bar_argv[0]);
719 break;
720 default: /* parent */
721 close(bar_pipe[1]);
722 break;
723 }
724 }
725
726 bzero(&wa, sizeof wa);
727 for (i = 0; i < ScreenCount(display); i++)
728 TAILQ_FOREACH(r, &screens[i].rl, entry) {
729 wa.border_pixel =
730 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
731 wa.background_pixel =
732 screens[i].c[SWM_S_COLOR_BAR].color;
733 XChangeWindowAttributes(display, r->bar_window,
734 CWBackPixel | CWBorderPixel, &wa);
735 }
736 bar_update();
737 }
738
739 void
740 bar_setup(struct swm_region *r)
741 {
742 int i;
743
744 for (i = 0; bar_fonts[i] != NULL; i++) {
745 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
746 if (bar_fs) {
747 bar_fidx = i;
748 break;
749 }
750 }
751 if (bar_fonts[i] == NULL)
752 errx(1, "couldn't load font");
753 bar_height = bar_fs->ascent + bar_fs->descent + 3;
754
755 r->bar_window = XCreateSimpleWindow(display,
756 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
757 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
758 r->s->c[SWM_S_COLOR_BAR].color);
759 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
760 XSetFont(display, bar_gc, bar_fs->fid);
761 XSelectInput(display, r->bar_window, VisibilityChangeMask);
762 if (bar_enabled)
763 XMapRaised(display, r->bar_window);
764 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
765
766 if (signal(SIGALRM, bar_signal) == SIG_ERR)
767 err(1, "could not install bar_signal");
768 bar_refresh();
769 }
770
771 void
772 version(struct swm_region *r, union arg *args)
773 {
774 bar_version = !bar_version;
775 if (bar_version)
776 strlcpy(bar_vertext, cvstag, sizeof bar_vertext);
777 else
778 strlcpy(bar_vertext, "", sizeof bar_vertext);
779 bar_update();
780 }
781
782 void
783 config_win(struct ws_win *win)
784 {
785 XConfigureEvent ce;
786
787 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
788 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
789 ce.type = ConfigureNotify;
790 ce.display = display;
791 ce.event = win->id;
792 ce.window = win->id;
793 ce.x = win->g.x;
794 ce.y = win->g.y;
795 ce.width = win->g.w;
796 ce.height = win->g.h;
797 ce.border_width = 1; /* XXX store this! */
798 ce.above = None;
799 ce.override_redirect = False;
800 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
801 }
802
803 int
804 count_win(struct workspace *ws, int count_transient)
805 {
806 struct ws_win *win;
807 int count = 0;
808
809 TAILQ_FOREACH(win, &ws->winlist, entry) {
810 if (count_transient == 0 && win->floating)
811 continue;
812 if (count_transient == 0 && win->transient)
813 continue;
814 count++;
815 }
816 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
817
818 return (count);
819 }
820
821 void
822 quit(struct swm_region *r, union arg *args)
823 {
824 DNPRINTF(SWM_D_MISC, "quit\n");
825 running = 0;
826 }
827
828 void
829 unmap_all(void)
830 {
831 struct ws_win *win;
832 int i, j;
833
834 for (i = 0; i < ScreenCount(display); i++)
835 for (j = 0; j < SWM_WS_MAX; j++)
836 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
837 XUnmapWindow(display, win->id);
838 }
839
840 void
841 restart(struct swm_region *r, union arg *args)
842 {
843 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
844
845 /* disable alarm because the following code may not be interrupted */
846 alarm(0);
847 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
848 errx(1, "can't disable alarm");
849
850 bar_extra_stop();
851 bar_extra = 1;
852 unmap_all();
853 XCloseDisplay(display);
854 execvp(start_argv[0], start_argv);
855 fprintf(stderr, "execvp failed\n");
856 perror(" failed");
857 quit(NULL, NULL);
858 }
859
860 struct swm_region *
861 root_to_region(Window root)
862 {
863 struct swm_region *r;
864 Window rr, cr;
865 int i, x, y, wx, wy;
866 unsigned int mask;
867
868 for (i = 0; i < ScreenCount(display); i++)
869 if (screens[i].root == root)
870 break;
871
872 if (XQueryPointer(display, screens[i].root,
873 &rr, &cr, &x, &y, &wx, &wy, &mask) == False) {
874 /* if we can't query the pointer, grab the first region */
875 r = TAILQ_FIRST(&screens[i].rl);
876 } else {
877 /* otherwise, choose a region based on pointer location */
878 TAILQ_FOREACH(r, &screens[i].rl, entry) {
879 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
880 y >= Y(r) && y <= Y(r) + HEIGHT(r))
881 break;
882 }
883
884 if (r == NULL)
885 r = TAILQ_FIRST(&screens[i].rl);
886 }
887 return (r);
888 }
889
890 struct ws_win *
891 find_window(Window id)
892 {
893 struct ws_win *win;
894 int i, j;
895
896 for (i = 0; i < ScreenCount(display); i++)
897 for (j = 0; j < SWM_WS_MAX; j++)
898 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
899 if (id == win->id)
900 return (win);
901 return (NULL);
902 }
903
904 void
905 spawn(struct swm_region *r, union arg *args)
906 {
907 char *ret;
908 int si;
909
910 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
911 /*
912 * The double-fork construct avoids zombie processes and keeps the code
913 * clean from stupid signal handlers.
914 */
915 if (fork() == 0) {
916 if (fork() == 0) {
917 if (display)
918 close(ConnectionNumber(display));
919 setenv("LD_PRELOAD", SWM_LIB, 1);
920 if (asprintf(&ret, "%d", r->ws->idx)) {
921 setenv("_SWM_WS", ret, 1);
922 free(ret);
923 }
924 if (asprintf(&ret, "%d", getpid())) {
925 setenv("_SWM_PID", ret, 1);
926 free(ret);
927 }
928 setsid();
929 /* kill stdin, mplayer, ssh-add etc. need that */
930 si = open("/dev/null", O_RDONLY, 0);
931 if (si == -1)
932 err(1, "open /dev/null");
933 if (dup2(si, 0) == -1)
934 err(1, "dup2 /dev/null");
935 execvp(args->argv[0], args->argv);
936 fprintf(stderr, "execvp failed\n");
937 perror(" failed");
938 }
939 exit(0);
940 }
941 wait(0);
942 }
943
944 void
945 spawnmenu(struct swm_region *r, union arg *args)
946 {
947 DNPRINTF(SWM_D_MISC, "spawnmenu\n");
948
949 spawn_menu[SWM_MENU_FN] = bar_fonts[bar_fidx];
950 spawn_menu[SWM_MENU_NB] = r->s->c[SWM_S_COLOR_BAR].name;
951 spawn_menu[SWM_MENU_NF] = r->s->c[SWM_S_COLOR_BAR_FONT].name;
952 spawn_menu[SWM_MENU_SB] = r->s->c[SWM_S_COLOR_BAR_BORDER].name;
953 spawn_menu[SWM_MENU_SF] = r->s->c[SWM_S_COLOR_BAR].name;
954
955 spawn(r, args);
956 }
957
958 void
959 unfocus_all(void)
960 {
961 struct ws_win *win;
962 int i, j;
963
964 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
965
966 for (i = 0; i < ScreenCount(display); i++)
967 for (j = 0; j < SWM_WS_MAX; j++)
968 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry) {
969 if (win->ws->r == NULL)
970 continue;
971 grabbuttons(win, 0);
972 XSetWindowBorder(display, win->id,
973 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
974 win->got_focus = 0;
975 win->ws->focus = NULL;
976 cur_focus = NULL;
977 }
978 }
979
980 void
981 focus_win(struct ws_win *win)
982 {
983 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
984
985 if (win == NULL)
986 return;
987
988 unfocus_all();
989 win->ws->focus = win;
990 if (win->ws->r != NULL) {
991 cur_focus = win;
992 if (!win->got_focus) {
993 XSetWindowBorder(display, win->id,
994 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
995 grabbuttons(win, 1);
996 }
997 win->got_focus = 1;
998 XSetInputFocus(display, win->id,
999 RevertToPointerRoot, CurrentTime);
1000 }
1001 }
1002
1003 void
1004 switchws(struct swm_region *r, union arg *args)
1005 {
1006 int wsid = args->id;
1007 struct swm_region *this_r, *other_r;
1008 struct ws_win *win;
1009 struct workspace *new_ws, *old_ws;
1010
1011 this_r = r;
1012 old_ws = this_r->ws;
1013 new_ws = &this_r->s->ws[wsid];
1014
1015 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1016 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1017 old_ws->idx, wsid);
1018
1019 if (new_ws == old_ws)
1020 return;
1021
1022 other_r = new_ws->r;
1023 if (!other_r) {
1024 /* if the other workspace is hidden, switch windows */
1025 /* map new window first to prevent ugly blinking */
1026 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1027 XMapRaised(display, win->id);
1028
1029 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1030 XUnmapWindow(display, win->id);
1031 old_ws->r = NULL;
1032 old_ws->restack = 1;
1033 } else {
1034 other_r->ws = old_ws;
1035 old_ws->r = other_r;
1036 }
1037 this_r->ws = new_ws;
1038 new_ws->r = this_r;
1039
1040 ignore_enter = 1;
1041 /* set focus */
1042 if (new_ws->focus)
1043 focus_win(new_ws->focus);
1044 stack();
1045 bar_update();
1046 }
1047
1048 void
1049 cyclews(struct swm_region *r, union arg *args)
1050 {
1051 union arg a;
1052 struct swm_screen *s = r->s;
1053
1054 DNPRINTF(SWM_D_WS, "cyclews id %d "
1055 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1056 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1057
1058 a.id = r->ws->idx;
1059 do {
1060 switch (args->id) {
1061 case SWM_ARG_ID_CYCLEWS_UP:
1062 if (a.id < SWM_WS_MAX - 1)
1063 a.id++;
1064 else
1065 a.id = 0;
1066 break;
1067 case SWM_ARG_ID_CYCLEWS_DOWN:
1068 if (a.id > 0)
1069 a.id--;
1070 else
1071 a.id = SWM_WS_MAX - 1;
1072 break;
1073 default:
1074 return;
1075 };
1076
1077 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1078 continue;
1079 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1080 continue;
1081
1082 switchws(r, &a);
1083 } while (a.id != r->ws->idx);
1084 }
1085
1086 void
1087 cyclescr(struct swm_region *r, union arg *args)
1088 {
1089 struct swm_region *rr;
1090 int i;
1091
1092 i = r->s->idx;
1093 switch (args->id) {
1094 case SWM_ARG_ID_CYCLESC_UP:
1095 rr = TAILQ_NEXT(r, entry);
1096 if (rr == NULL)
1097 rr = TAILQ_FIRST(&screens[i].rl);
1098 break;
1099 case SWM_ARG_ID_CYCLESC_DOWN:
1100 rr = TAILQ_PREV(r, swm_region_list, entry);
1101 if (rr == NULL)
1102 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1103 break;
1104 default:
1105 return;
1106 };
1107 unfocus_all();
1108 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1109 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1110 rr->g.y + bar_enabled ? bar_height : 0);
1111 }
1112
1113 void
1114 swapwin(struct swm_region *r, union arg *args)
1115 {
1116 struct ws_win *target;
1117 struct ws_win_list *wl;
1118
1119
1120 DNPRINTF(SWM_D_WS, "swapwin id %d "
1121 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1122 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1123 if (cur_focus == NULL)
1124 return;
1125
1126 wl = &cur_focus->ws->winlist;
1127
1128 switch (args->id) {
1129 case SWM_ARG_ID_SWAPPREV:
1130 target = TAILQ_PREV(cur_focus, ws_win_list, entry);
1131 TAILQ_REMOVE(wl, cur_focus, entry);
1132 if (target == NULL)
1133 TAILQ_INSERT_TAIL(wl, cur_focus, entry);
1134 else
1135 TAILQ_INSERT_BEFORE(target, cur_focus, entry);
1136 break;
1137 case SWM_ARG_ID_SWAPNEXT:
1138 target = TAILQ_NEXT(cur_focus, entry);
1139 TAILQ_REMOVE(wl, cur_focus, entry);
1140 if (target == NULL)
1141 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1142 else
1143 TAILQ_INSERT_AFTER(wl, target, cur_focus, entry);
1144 break;
1145 case SWM_ARG_ID_SWAPMAIN:
1146 target = TAILQ_FIRST(wl);
1147 if (target == cur_focus)
1148 return;
1149 TAILQ_REMOVE(wl, target, entry);
1150 TAILQ_INSERT_BEFORE(cur_focus, target, entry);
1151 TAILQ_REMOVE(wl, cur_focus, entry);
1152 TAILQ_INSERT_HEAD(wl, cur_focus, entry);
1153 break;
1154 default:
1155 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1156 return;
1157 }
1158
1159 ignore_enter = 2;
1160 stack();
1161 }
1162
1163 void
1164 focus(struct swm_region *r, union arg *args)
1165 {
1166 struct ws_win *winfocus, *winlostfocus;
1167 struct ws_win_list *wl;
1168
1169 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1170 if (cur_focus == NULL)
1171 return;
1172
1173 wl = &cur_focus->ws->winlist;
1174
1175 winlostfocus = cur_focus;
1176
1177 switch (args->id) {
1178 case SWM_ARG_ID_FOCUSPREV:
1179 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1180 if (winfocus == NULL)
1181 winfocus = TAILQ_LAST(wl, ws_win_list);
1182 break;
1183
1184 case SWM_ARG_ID_FOCUSNEXT:
1185 winfocus = TAILQ_NEXT(cur_focus, entry);
1186 if (winfocus == NULL)
1187 winfocus = TAILQ_FIRST(wl);
1188 break;
1189
1190 case SWM_ARG_ID_FOCUSMAIN:
1191 winfocus = TAILQ_FIRST(wl);
1192 break;
1193
1194 default:
1195 return;
1196 }
1197
1198 if (winfocus == winlostfocus || winfocus == NULL)
1199 return;
1200
1201 XMapRaised(display, winfocus->id);
1202 focus_win(winfocus);
1203 XSync(display, False);
1204 }
1205
1206 void
1207 cycle_layout(struct swm_region *r, union arg *args)
1208 {
1209 struct workspace *ws = r->ws;
1210
1211 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1212
1213 ws->cur_layout++;
1214 if (ws->cur_layout->l_stack == NULL)
1215 ws->cur_layout = &layouts[0];
1216 ignore_enter = 1;
1217 stack();
1218 }
1219
1220 void
1221 stack_config(struct swm_region *r, union arg *args)
1222 {
1223 struct workspace *ws = r->ws;
1224
1225 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1226 args->id, ws->idx);
1227
1228 if (ws->cur_layout->l_config != NULL)
1229 ws->cur_layout->l_config(ws, args->id);
1230
1231 if (args->id != SWM_ARG_ID_STACKINIT);
1232 stack();
1233 }
1234
1235 void
1236 stack(void) {
1237 struct swm_geometry g;
1238 struct swm_region *r;
1239 int i, j;
1240
1241 DNPRINTF(SWM_D_STACK, "stack\n");
1242
1243 for (i = 0; i < ScreenCount(display); i++) {
1244 j = 0;
1245 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1246 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1247 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1248
1249 /* start with screen geometry, adjust for bar */
1250 g = r->g;
1251 g.w -= 2;
1252 g.h -= 2;
1253 if (bar_enabled) {
1254 g.y += bar_height;
1255 g.h -= bar_height;
1256 }
1257
1258 r->ws->restack = 0;
1259 r->ws->cur_layout->l_stack(r->ws, &g);
1260 }
1261 }
1262 XSync(display, False);
1263 }
1264
1265 void
1266 stack_floater(struct ws_win *win, struct swm_region *r)
1267 {
1268 unsigned int mask;
1269 XWindowChanges wc;
1270
1271 bzero(&wc, sizeof wc);
1272 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1273 wc.border_width = 1;
1274 if (win->transient) {
1275 win->g.w = (double)WIDTH(r) * dialog_ratio;
1276 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1277 }
1278 wc.width = win->g.w;
1279 wc.height = win->g.h;
1280 if (win->manual) {
1281 wc.x = win->g.x;
1282 wc.y = win->g.y;
1283 } else {
1284 wc.x = (WIDTH(r) - win->g.w) / 2;
1285 wc.y = (HEIGHT(r) - win->g.h) / 2;
1286 }
1287
1288 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1289 win->id, wc.x, wc.y, wc.width, wc.height);
1290
1291 XConfigureWindow(display, win->id, mask, &wc);
1292 }
1293
1294 #define SWAPXY(g) do { \
1295 int tmp; \
1296 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1297 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1298 } while (0)
1299 void
1300 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1301 {
1302 XWindowChanges wc;
1303 struct swm_geometry win_g, r_g = *g;
1304 struct ws_win *win, *winfocus;
1305 int i, j, w_inc, h_inc, w_base, h_base;
1306 int hrh, extra, h_slice, last_h = 0;
1307 int split, colno, winno, mwin, msize, mscale;
1308 int remain, missing, v_slice;;
1309 unsigned int mask;
1310
1311 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1312 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1313
1314 if ((winno = count_win(ws, 0)) == 0)
1315 return;
1316
1317 if (ws->focus == NULL)
1318 ws->focus = TAILQ_FIRST(&ws->winlist);
1319 winfocus = cur_focus ? cur_focus : ws->focus;
1320
1321 win = TAILQ_FIRST(&ws->winlist);
1322 if (rot) {
1323 w_inc = win->sh.width_inc;
1324 w_base = win->sh.base_width;
1325 mwin = ws->l_state.horizontal_mwin;
1326 mscale = ws->l_state.horizontal_msize;
1327 SWAPXY(&r_g);
1328 } else {
1329 w_inc = win->sh.height_inc;
1330 w_base = win->sh.base_height;
1331 mwin = ws->l_state.vertical_mwin;
1332 mscale = ws->l_state.vertical_msize;
1333 }
1334 win_g = r_g;
1335
1336 h_slice = r_g.h / SWM_H_SLICE;
1337 if (mwin && winno > mwin) {
1338 v_slice = r_g.w / SWM_V_SLICE;
1339
1340 split = mwin;
1341 colno = split;
1342 msize = v_slice * mscale;
1343
1344 if (w_inc > 1 && w_inc < v_slice) {
1345 /* adjust for window's requested size increment */
1346 remain = (win_g.w - w_base) % w_inc;
1347 missing = w_inc - remain;
1348
1349 if (missing <= extra || j == 0) {
1350 extra -= missing;
1351 win_g.w += missing;
1352 } else {
1353 win_g.w -= remain;
1354 extra += remain;
1355 }
1356 }
1357
1358 win_g.w = msize;
1359 if (flip)
1360 win_g.x += r_g.w - msize;
1361 } else {
1362 colno = winno;
1363 split = 0;
1364 }
1365 hrh = r_g.h / colno;
1366 extra = r_g.h - (colno * hrh);
1367 win_g.h = hrh - 2;
1368
1369 /* stack all the tiled windows */
1370 i = j = 0;
1371 TAILQ_FOREACH(win, &ws->winlist, entry) {
1372 if (win->transient != 0 || win->floating != 0)
1373 continue;
1374
1375 if (split && i == split) {
1376 colno = winno - split;
1377 hrh = (r_g.h / colno);
1378 extra = r_g.h - (colno * hrh);
1379 if (flip)
1380 win_g.x = r_g.x;
1381 else
1382 win_g.x += msize + 2;
1383 win_g.w = r_g.w - (msize + 2);
1384 j = 0;
1385 }
1386 win_g.h = hrh - 2;
1387 if (rot) {
1388 h_inc = win->sh.width_inc;
1389 h_base = win->sh.base_width;
1390 } else {
1391 h_inc = win->sh.height_inc;
1392 h_base = win->sh.base_height;
1393 }
1394 if (j == colno - 1) {
1395 win_g.h = hrh + extra;
1396 } else if (h_inc > 1 && h_inc < h_slice) {
1397 /* adjust for window's requested size increment */
1398 remain = (win_g.h - h_base) % h_inc;
1399 missing = h_inc - remain;
1400
1401 if (missing <= extra || j == 0) {
1402 extra -= missing;
1403 win_g.h += missing;
1404 } else {
1405 win_g.h -= remain;
1406 extra += remain;
1407 }
1408 }
1409
1410 if (j == 0)
1411 win_g.y = r_g.y;
1412 else
1413 win_g.y += last_h + 2;
1414
1415 bzero(&wc, sizeof wc);
1416 wc.border_width = 1;
1417 if (rot) {
1418 win->g.x = wc.x = win_g.y;
1419 win->g.y = wc.y = win_g.x;
1420 win->g.w = wc.width = win_g.h;
1421 win->g.h = wc.height = win_g.w;
1422 } else {
1423 win->g.x = wc.x = win_g.x;
1424 win->g.y = wc.y = win_g.y;
1425 win->g.w = wc.width = win_g.w;
1426 win->g.h = wc.height = win_g.h;
1427 }
1428 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1429 XConfigureWindow(display, win->id, mask, &wc);
1430 XMapRaised(display, win->id);
1431 /*
1432 fprintf(stderr, "vertical_stack: win %d x %d y %d w %d h %d bw %d\n", win->id, win->g.x, win->g.y, win->g.w , win->g.h, wc.border_width);
1433 */
1434
1435 last_h = win_g.h;
1436 i++;
1437 j++;
1438 }
1439
1440 /* now, stack all the floaters and transients */
1441 TAILQ_FOREACH(win, &ws->winlist, entry) {
1442 if (win->transient == 0 && win->floating == 0)
1443 continue;
1444
1445 stack_floater(win, ws->r);
1446 XMapRaised(display, win->id);
1447 }
1448
1449 if (winfocus)
1450 focus_win(winfocus); /* has to be done outside of the loop */
1451 }
1452
1453 void
1454 vertical_config(struct workspace *ws, int id)
1455 {
1456 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1457
1458 switch (id) {
1459 case SWM_ARG_ID_STACKRESET:
1460 case SWM_ARG_ID_STACKINIT:
1461 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1462 ws->l_state.vertical_mwin = 1;
1463 break;
1464 case SWM_ARG_ID_MASTERSHRINK:
1465 if (ws->l_state.vertical_msize > 1)
1466 ws->l_state.vertical_msize--;
1467 break;
1468 case SWM_ARG_ID_MASTERGROW:
1469 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1470 ws->l_state.vertical_msize++;
1471 break;
1472 case SWM_ARG_ID_MASTERADD:
1473 ws->l_state.vertical_mwin++;
1474 break;
1475 case SWM_ARG_ID_MASTERDEL:
1476 if (ws->l_state.vertical_mwin > 0)
1477 ws->l_state.vertical_mwin--;
1478 break;
1479 default:
1480 return;
1481 }
1482 }
1483
1484 void
1485 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1486 {
1487 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1488
1489 stack_master(ws, g, 0, 0);
1490 }
1491
1492 void
1493 horizontal_config(struct workspace *ws, int id)
1494 {
1495 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1496
1497 switch (id) {
1498 case SWM_ARG_ID_STACKRESET:
1499 case SWM_ARG_ID_STACKINIT:
1500 ws->l_state.horizontal_mwin = 1;
1501 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1502 break;
1503 case SWM_ARG_ID_MASTERSHRINK:
1504 if (ws->l_state.horizontal_msize > 1)
1505 ws->l_state.horizontal_msize--;
1506 break;
1507 case SWM_ARG_ID_MASTERGROW:
1508 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1509 ws->l_state.horizontal_msize++;
1510 break;
1511 case SWM_ARG_ID_MASTERADD:
1512 ws->l_state.horizontal_mwin++;
1513 break;
1514 case SWM_ARG_ID_MASTERDEL:
1515 if (ws->l_state.horizontal_mwin > 0)
1516 ws->l_state.horizontal_mwin--;
1517 break;
1518 default:
1519 return;
1520 }
1521 }
1522
1523 void
1524 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1525 {
1526 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1527
1528 stack_master(ws, g, 1, 0);
1529 }
1530
1531 /* fullscreen view */
1532 void
1533 max_stack(struct workspace *ws, struct swm_geometry *g) {
1534 XWindowChanges wc;
1535 struct swm_geometry gg = *g;
1536 struct ws_win *win, *winfocus;
1537 unsigned int mask;
1538
1539 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1540
1541 if (count_win(ws, 0) == 0)
1542 return;
1543
1544 if (ws->focus == NULL)
1545 ws->focus = TAILQ_FIRST(&ws->winlist);
1546 winfocus = cur_focus ? cur_focus : ws->focus;
1547
1548 TAILQ_FOREACH(win, &ws->winlist, entry) {
1549 if (win->transient != 0 || win->floating != 0) {
1550 if (win == ws->focus) {
1551 /* XXX maximize? */
1552 stack_floater(win, ws->r);
1553 XMapRaised(display, win->id);
1554 } else
1555 XUnmapWindow(display, win->id);
1556 } else {
1557 bzero(&wc, sizeof wc);
1558 wc.border_width = 1;
1559 win->g.x = wc.x = gg.x;
1560 win->g.y = wc.y = gg.y;
1561 win->g.w = wc.width = gg.w;
1562 win->g.h = wc.height = gg.h;
1563 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1564 XConfigureWindow(display, win->id, mask, &wc);
1565
1566 if (win == ws->focus) {
1567 XMapRaised(display, win->id);
1568 } else
1569 XUnmapWindow(display, win->id);
1570 }
1571 }
1572
1573 if (winfocus)
1574 focus_win(winfocus); /* has to be done outside of the loop */
1575 }
1576
1577 void
1578 send_to_ws(struct swm_region *r, union arg *args)
1579 {
1580 int wsid = args->id;
1581 struct ws_win *win = cur_focus;
1582 struct workspace *ws, *nws;
1583 Atom ws_idx_atom = 0;
1584 unsigned char ws_idx_str[SWM_PROPLEN];
1585
1586 if (win == NULL)
1587 return;
1588
1589 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1590
1591 ws = win->ws;
1592 nws = &win->s->ws[wsid];
1593
1594 XUnmapWindow(display, win->id);
1595
1596 /* find a window to focus */
1597 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1598 if (ws->focus == NULL)
1599 ws->focus = TAILQ_FIRST(&ws->winlist);
1600 if (ws->focus == win)
1601 ws->focus = NULL;
1602
1603 TAILQ_REMOVE(&ws->winlist, win, entry);
1604
1605 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1606 win->ws = nws;
1607
1608 /* Try to update the window's workspace property */
1609 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1610 if (ws_idx_atom &&
1611 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1612 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1613 ws_idx_str);
1614 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1615 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1616 }
1617
1618 if (count_win(nws, 1) == 1)
1619 nws->focus = win;
1620 ws->restack = 1;
1621 nws->restack = 1;
1622
1623 stack();
1624 }
1625
1626 void
1627 wkill(struct swm_region *r, union arg *args)
1628 {
1629 DNPRINTF(SWM_D_MISC, "wkill\n");
1630 if(r->ws->focus != NULL)
1631 XKillClient(display, r->ws->focus->id);
1632 }
1633
1634 void
1635 screenshot(struct swm_region *r, union arg *args)
1636 {
1637 union arg a;
1638
1639 DNPRINTF(SWM_D_MISC, "screenshot\n");
1640
1641 if (ss_enabled == 0)
1642 return;
1643
1644 switch (args->id) {
1645 case SWM_ARG_ID_SS_ALL:
1646 spawn_screenshot[1] = "full";
1647 break;
1648 case SWM_ARG_ID_SS_WINDOW:
1649 spawn_screenshot[1] = "window";
1650 break;
1651 default:
1652 return;
1653 }
1654 a.argv = spawn_screenshot;
1655 spawn(r, &a);
1656 }
1657
1658 void
1659 floating_toggle(struct swm_region *r, union arg *args)
1660 {
1661 struct ws_win *win = cur_focus;
1662
1663 if (win == NULL)
1664 return;
1665
1666 win->floating = !win->floating;
1667 win->manual = 0;
1668 stack();
1669 focus_win(win);
1670 }
1671
1672 /* key definitions */
1673 struct key {
1674 unsigned int mod;
1675 KeySym keysym;
1676 void (*func)(struct swm_region *r, union arg *);
1677 union arg args;
1678 } keys[] = {
1679 /* modifier key function argument */
1680 { MODKEY, XK_space, cycle_layout, {0} },
1681 { MODKEY | ShiftMask, XK_space, stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1682 { MODKEY, XK_h, stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1683 { MODKEY, XK_l, stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1684 { MODKEY, XK_comma, stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1685 { MODKEY, XK_period, stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1686 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1687 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1688 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1689 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1690 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1691 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
1692 { MODKEY, XK_p, spawnmenu, {.argv = spawn_menu} },
1693 { MODKEY | ShiftMask, XK_q, quit, {0} },
1694 { MODKEY, XK_q, restart, {0} },
1695 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1696 { MODKEY, XK_1, switchws, {.id = 0} },
1697 { MODKEY, XK_2, switchws, {.id = 1} },
1698 { MODKEY, XK_3, switchws, {.id = 2} },
1699 { MODKEY, XK_4, switchws, {.id = 3} },
1700 { MODKEY, XK_5, switchws, {.id = 4} },
1701 { MODKEY, XK_6, switchws, {.id = 5} },
1702 { MODKEY, XK_7, switchws, {.id = 6} },
1703 { MODKEY, XK_8, switchws, {.id = 7} },
1704 { MODKEY, XK_9, switchws, {.id = 8} },
1705 { MODKEY, XK_0, switchws, {.id = 9} },
1706 { MODKEY, XK_Right, cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1707 { MODKEY, XK_Left, cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1708 { MODKEY | ShiftMask, XK_Right, cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1709 { MODKEY | ShiftMask, XK_Left, cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1710 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1711 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1712 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1713 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1714 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1715 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1716 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1717 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1718 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1719 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1720 { MODKEY, XK_b, bar_toggle, {0} },
1721 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1722 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1723 { MODKEY | ShiftMask, XK_x, wkill, {0} },
1724 { MODKEY, XK_s, screenshot, {.id = SWM_ARG_ID_SS_ALL} },
1725 { MODKEY | ShiftMask, XK_s, screenshot, {.id = SWM_ARG_ID_SS_WINDOW} },
1726 { MODKEY, XK_t, floating_toggle,{0} },
1727 { MODKEY | ShiftMask, XK_v, version, {0} },
1728 { MODKEY | ShiftMask, XK_Delete, spawn, {.argv = spawn_lock} },
1729 };
1730
1731 void
1732 resize_window(struct ws_win *win, int center)
1733 {
1734 unsigned int mask;
1735 XWindowChanges wc;
1736 struct swm_region *r;
1737
1738 r = root_to_region(win->wa.root);
1739 bzero(&wc, sizeof wc);
1740 mask = CWBorderWidth | CWWidth | CWHeight;
1741 wc.border_width = 1;
1742 wc.width = win->g.w;
1743 wc.height = win->g.h;
1744 if (center == SWM_ARG_ID_CENTER) {
1745 wc.x = (WIDTH(r) - win->g.w) / 2;
1746 wc.y = (HEIGHT(r) - win->g.h) / 2;
1747 mask |= CWX | CWY;
1748 }
1749
1750 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1751 win->id, wc.x, wc.y, wc.width, wc.height);
1752
1753 XConfigureWindow(display, win->id, mask, &wc);
1754 config_win(win);
1755 }
1756
1757 void
1758 resize(struct ws_win *win, union arg *args)
1759 {
1760 XEvent ev;
1761 Time time = 0;
1762
1763 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1764 win->id, win->floating, win->transient);
1765
1766 if (!(win->transient != 0 || win->floating != 0))
1767 return;
1768
1769 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1770 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1771 return;
1772 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1773 do {
1774 XMaskEvent(display, MOUSEMASK | ExposureMask |
1775 SubstructureRedirectMask, &ev);
1776 switch(ev.type) {
1777 case ConfigureRequest:
1778 case Expose:
1779 case MapRequest:
1780 handler[ev.type](&ev);
1781 break;
1782 case MotionNotify:
1783 if (ev.xmotion.x < 0)
1784 ev.xmotion.x = 0;
1785 if (ev.xmotion.y < 0)
1786 ev.xmotion.y = 0;
1787 win->g.w = ev.xmotion.x;
1788 win->g.h = ev.xmotion.y;
1789
1790 /* not free, don't sync more than 60 times / second */
1791 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1792 time = ev.xmotion.time;
1793 XSync(display, False);
1794 resize_window(win, args->id);
1795 }
1796 break;
1797 }
1798 } while (ev.type != ButtonRelease);
1799 if (time) {
1800 XSync(display, False);
1801 resize_window(win, args->id);
1802 }
1803 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1804 win->g.h - 1);
1805 XUngrabPointer(display, CurrentTime);
1806
1807 /* drain events */
1808 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1809 }
1810
1811 void
1812 move_window(struct ws_win *win)
1813 {
1814 unsigned int mask;
1815 XWindowChanges wc;
1816 struct swm_region *r;
1817
1818 r = root_to_region(win->wa.root);
1819 bzero(&wc, sizeof wc);
1820 mask = CWX | CWY;
1821 wc.x = win->g.x;
1822 wc.y = win->g.y;
1823
1824 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1825 win->id, wc.x, wc.y, wc.width, wc.height);
1826
1827 XConfigureWindow(display, win->id, mask, &wc);
1828 config_win(win);
1829 }
1830
1831 void
1832 move(struct ws_win *win, union arg *args)
1833 {
1834 XEvent ev;
1835 Time time = 0;
1836 int restack = 0;
1837
1838 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
1839 win->id, win->floating, win->transient);
1840
1841 if (win->floating == 0) {
1842 win->floating = 1;
1843 win->manual = 1;
1844 restack = 1;
1845 }
1846
1847 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1848 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1849 return;
1850 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1851 do {
1852 XMaskEvent(display, MOUSEMASK | ExposureMask |
1853 SubstructureRedirectMask, &ev);
1854 switch(ev.type) {
1855 case ConfigureRequest:
1856 case Expose:
1857 case MapRequest:
1858 handler[ev.type](&ev);
1859 break;
1860 case MotionNotify:
1861 win->g.x = ev.xmotion.x_root;
1862 win->g.y = ev.xmotion.y_root;
1863
1864 /* not free, don't sync more than 60 times / second */
1865 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1866 time = ev.xmotion.time;
1867 XSync(display, False);
1868 move_window(win);
1869 }
1870 break;
1871 }
1872 } while (ev.type != ButtonRelease);
1873 if (time) {
1874 XSync(display, False);
1875 move_window(win);
1876 }
1877 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1878 XUngrabPointer(display, CurrentTime);
1879 if (restack)
1880 stack();
1881
1882 /* drain events */
1883 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1884 }
1885
1886 /* mouse */
1887 enum { client_click, root_click };
1888 struct button {
1889 unsigned int action;
1890 unsigned int mask;
1891 unsigned int button;
1892 void (*func)(struct ws_win *, union arg *);
1893 union arg args;
1894 } buttons[] = {
1895 /* action key mouse button func args */
1896 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
1897 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
1898 { client_click, MODKEY, Button1, move, {0} },
1899 };
1900
1901 void
1902 updatenumlockmask(void)
1903 {
1904 unsigned int i, j;
1905 XModifierKeymap *modmap;
1906
1907 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1908 numlockmask = 0;
1909 modmap = XGetModifierMapping(display);
1910 for (i = 0; i < 8; i++)
1911 for (j = 0; j < modmap->max_keypermod; j++)
1912 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1913 == XKeysymToKeycode(display, XK_Num_Lock))
1914 numlockmask = (1 << i);
1915
1916 XFreeModifiermap(modmap);
1917 }
1918
1919 void
1920 grabkeys(void)
1921 {
1922 unsigned int i, j, k;
1923 KeyCode code;
1924 unsigned int modifiers[] =
1925 { 0, LockMask, numlockmask, numlockmask | LockMask };
1926
1927 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1928 updatenumlockmask();
1929
1930 for (k = 0; k < ScreenCount(display); k++) {
1931 if (TAILQ_EMPTY(&screens[k].rl))
1932 continue;
1933 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
1934 for (i = 0; i < LENGTH(keys); i++) {
1935 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
1936 for (j = 0; j < LENGTH(modifiers); j++)
1937 XGrabKey(display, code,
1938 keys[i].mod | modifiers[j],
1939 screens[k].root, True,
1940 GrabModeAsync, GrabModeAsync);
1941 }
1942 }
1943 }
1944
1945 void
1946 grabbuttons(struct ws_win *win, int focused)
1947 {
1948 unsigned int i, j;
1949 unsigned int modifiers[] =
1950 { 0, LockMask, numlockmask, numlockmask|LockMask };
1951
1952 updatenumlockmask();
1953 XUngrabButton(display, AnyButton, AnyModifier, win->id);
1954 if(focused) {
1955 for (i = 0; i < LENGTH(buttons); i++)
1956 if (buttons[i].action == client_click)
1957 for (j = 0; j < LENGTH(modifiers); j++)
1958 XGrabButton(display, buttons[i].button,
1959 buttons[i].mask | modifiers[j],
1960 win->id, False, BUTTONMASK,
1961 GrabModeAsync, GrabModeSync, None,
1962 None);
1963 } else
1964 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
1965 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1966 }
1967
1968 void
1969 expose(XEvent *e)
1970 {
1971 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1972 }
1973
1974 void
1975 keypress(XEvent *e)
1976 {
1977 unsigned int i;
1978 KeySym keysym;
1979 XKeyEvent *ev = &e->xkey;
1980
1981 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1982
1983 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1984 for (i = 0; i < LENGTH(keys); i++)
1985 if (keysym == keys[i].keysym
1986 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1987 && keys[i].func)
1988 keys[i].func(root_to_region(ev->root),
1989 &(keys[i].args));
1990 }
1991
1992 void
1993 buttonpress(XEvent *e)
1994 {
1995 XButtonPressedEvent *ev = &e->xbutton;
1996
1997 struct ws_win *win;
1998 int i, action;
1999
2000 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2001
2002 action = root_click;
2003 if ((win = find_window(ev->window)) == NULL)
2004 return;
2005 else {
2006 focus_win(win);
2007 action = client_click;
2008 }
2009
2010 for (i = 0; i < LENGTH(buttons); i++)
2011 if (action == buttons[i].action && buttons[i].func &&
2012 buttons[i].button == ev->button &&
2013 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2014 buttons[i].func(win, &buttons[i].args);
2015 }
2016
2017 void
2018 set_win_state(struct ws_win *win, long state)
2019 {
2020 long data[] = {state, None};
2021
2022 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2023
2024 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2025 (unsigned char *)data, 2);
2026 }
2027
2028 struct ws_win *
2029 manage_window(Window id)
2030 {
2031 Window trans;
2032 struct workspace *ws;
2033 struct ws_win *win;
2034 int format, i, ws_idx;
2035 unsigned long nitems, bytes;
2036 Atom ws_idx_atom = 0, type;
2037 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
2038 struct swm_region *r;
2039 long mask;
2040 const char *errstr;
2041
2042 if ((win = find_window(id)) != NULL)
2043 return (win); /* already being managed */
2044
2045 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
2046 errx(1, "calloc: failed to allocate memory for new window");
2047
2048 /* Get all the window data in one shot */
2049 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2050 if (ws_idx_atom)
2051 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
2052 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
2053 XGetWindowAttributes(display, id, &win->wa);
2054 XGetTransientForHint(display, id, &trans);
2055 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
2056 if (trans) {
2057 win->transient = trans;
2058 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
2059 (unsigned)win->id, win->transient);
2060 }
2061
2062 /*
2063 * Figure out where to put the window. If it was previously assigned to
2064 * a workspace (either by spawn() or manually moving), and isn't
2065 * transient, * put it in the same workspace
2066 */
2067 r = root_to_region(win->wa.root);
2068 if (prop && win->transient == 0) {
2069 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
2070 ws_idx = strtonum(prop, 0, 9, &errstr);
2071 if (errstr) {
2072 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
2073 errstr, prop);
2074 }
2075 ws = &r->s->ws[ws_idx];
2076 } else
2077 ws = r->ws;
2078
2079 /* set up the window layout */
2080 win->id = id;
2081 win->ws = ws;
2082 win->s = r->s; /* this never changes */
2083 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
2084
2085 win->g.w = win->wa.width;
2086 win->g.h = win->wa.height;
2087 win->g.x = win->wa.x;
2088 win->g.y = win->wa.y;
2089
2090 /* Set window properties so we can remember this after reincarnation */
2091 if (ws_idx_atom && prop == NULL &&
2092 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
2093 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2094 ws_idx_str);
2095 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2096 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2097 }
2098 XFree(prop);
2099
2100 /*
2101 fprintf(stderr, "manage window: %d x %d y %d w %d h %d\n", win->id, win->g.x, win->g.y, win->g.w, win->g.h);
2102 */
2103
2104 if (XGetClassHint(display, win->id, &win->ch)) {
2105 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
2106 win->ch.res_class, win->ch.res_name);
2107 for (i = 0; quirks[i].class != NULL && quirks[i].name != NULL &&
2108 quirks[i].quirk != 0; i++){
2109 if (!strcmp(win->ch.res_class, quirks[i].class) &&
2110 !strcmp(win->ch.res_name, quirks[i].name)) {
2111 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
2112 win->ch.res_class, win->ch.res_name);
2113 if (quirks[i].quirk & SWM_Q_FLOAT)
2114 win->floating = 1;
2115 }
2116 }
2117 }
2118
2119 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
2120 PropertyChangeMask | StructureNotifyMask);
2121
2122 set_win_state(win, NormalState);
2123
2124 /* make new win focused */
2125 focus_win(win);
2126
2127 return (win);
2128 }
2129
2130 void
2131 configurerequest(XEvent *e)
2132 {
2133 XConfigureRequestEvent *ev = &e->xconfigurerequest;
2134 struct ws_win *win;
2135 int new = 0;
2136 XWindowChanges wc;
2137
2138 if ((win = find_window(ev->window)) == NULL)
2139 new = 1;
2140
2141 if (new) {
2142 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
2143 ev->window);
2144 /*
2145 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
2146 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
2147 */
2148 bzero(&wc, sizeof wc);
2149 wc.x = ev->x;
2150 wc.y = ev->y;
2151 wc.width = ev->width;
2152 wc.height = ev->height;
2153 wc.border_width = ev->border_width;
2154 wc.sibling = ev->above;
2155 wc.stack_mode = ev->detail;
2156 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
2157 } else {
2158 /*
2159 fprintf(stderr, "configurerequest: change window: %lu\n",
2160 ev->window);
2161 */
2162 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
2163 ev->window);
2164 if (win->floating) {
2165 if (ev->value_mask & CWX)
2166 win->g.x = ev->x;
2167 if (ev->value_mask & CWY)
2168 win->g.y = ev->y;
2169 if (ev->value_mask & CWWidth)
2170 win->g.w = ev->width;
2171 if (ev->value_mask & CWHeight)
2172 win->g.h = ev->height;
2173 if (win->ws->r != NULL) {
2174 /* this seems to be full screen */
2175 if (win->g.w >= WIDTH(win->ws->r)) {
2176 win->g.x = -1;
2177 win->g.w = WIDTH(win->ws->r);
2178 ev->value_mask |= CWX | CWWidth;
2179 }
2180 if (win->g.h >= HEIGHT(win->ws->r)) {
2181 /* kill border */
2182 win->g.y = -1;
2183 win->g.h = HEIGHT(win->ws->r);
2184 ev->value_mask |= CWY | CWHeight;
2185 }
2186 }
2187 if ((ev->value_mask & (CWX|CWY)) &&
2188 !(ev->value_mask & (CWWidth|CWHeight)))
2189 config_win(win);
2190 XMoveResizeWindow(display, win->id,
2191 win->g.x, win->g.y, win->g.w, win->g.h);
2192 } else
2193 config_win(win);
2194 }
2195 }
2196
2197 void
2198 configurenotify(XEvent *e)
2199 {
2200 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
2201 e->xconfigure.window);
2202 }
2203
2204 void
2205 destroynotify(XEvent *e)
2206 {
2207 struct ws_win *win;
2208 XDestroyWindowEvent *ev = &e->xdestroywindow;
2209 struct workspace *ws;
2210
2211 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
2212
2213 if ((win = find_window(ev->window)) != NULL) {
2214 ws = win->ws;
2215 /* find a window to focus */
2216 if (ws->focus == win)
2217 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
2218 if (ws->focus == NULL)
2219 ws->focus = TAILQ_FIRST(&ws->winlist);
2220 if (ws->focus == NULL || ws->focus == win) {
2221 ws->focus = NULL;
2222 unfocus_all();
2223 } else
2224 focus_win(ws->focus);
2225 TAILQ_REMOVE(&ws->winlist, win, entry);
2226 set_win_state(win, WithdrawnState);
2227 if (win->ch.res_class)
2228 XFree(win->ch.res_class);
2229 if (win->ch.res_name)
2230 XFree(win->ch.res_name);
2231 free(win);
2232 stack();
2233 }
2234 }
2235
2236 void
2237 enternotify(XEvent *e)
2238 {
2239 XCrossingEvent *ev = &e->xcrossing;
2240 struct ws_win *win;
2241
2242 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
2243
2244 if (ignore_enter) {
2245 /* eat event(r) to prevent autofocus */
2246 ignore_enter--;
2247 return;
2248 }
2249
2250 if ((win = find_window(ev->window)) != NULL)
2251 focus_win(win);
2252 }
2253
2254 void
2255 focusin(XEvent *e)
2256 {
2257 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
2258 }
2259
2260 void
2261 mappingnotify(XEvent *e)
2262 {
2263 XMappingEvent *ev = &e->xmapping;
2264
2265 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
2266
2267 XRefreshKeyboardMapping(ev);
2268 if (ev->request == MappingKeyboard)
2269 grabkeys();
2270 }
2271
2272 void
2273 maprequest(XEvent *e)
2274 {
2275 XMapRequestEvent *ev = &e->xmaprequest;
2276 XWindowAttributes wa;
2277
2278 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
2279 e->xmaprequest.window);
2280
2281 if (!XGetWindowAttributes(display, ev->window, &wa))
2282 return;
2283 if (wa.override_redirect)
2284 return;
2285 manage_window(e->xmaprequest.window);
2286 stack();
2287 }
2288
2289 void
2290 propertynotify(XEvent *e)
2291 {
2292 struct ws_win *win;
2293 XPropertyEvent *ev = &e->xproperty;
2294
2295 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
2296 ev->window);
2297
2298 if (ev->state == PropertyDelete)
2299 return; /* ignore */
2300 win = find_window(ev->window);
2301 if (win == NULL)
2302 return;
2303
2304 switch (ev->atom) {
2305 case XA_WM_NORMAL_HINTS:
2306 #if 0
2307 long mask;
2308 XGetWMNormalHints(display, win->id, &win->sh, &mask);
2309 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
2310 if (win->sh.flags & PMinSize) {
2311 win->g.w = win->sh.min_width;
2312 win->g.h = win->sh.min_height;
2313 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
2314 }
2315 XMoveResizeWindow(display, win->id,
2316 win->g.x, win->g.y, win->g.w, win->g.h);
2317 #endif
2318 break;
2319 default:
2320 break;
2321 }
2322 }
2323
2324 void
2325 unmapnotify(XEvent *e)
2326 {
2327 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
2328 }
2329
2330 void
2331 visibilitynotify(XEvent *e)
2332 {
2333 int i;
2334 struct swm_region *r;
2335
2336 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
2337 e->xvisibility.window);
2338 if (e->xvisibility.state == VisibilityUnobscured)
2339 for (i = 0; i < ScreenCount(display); i++)
2340 TAILQ_FOREACH(r, &screens[i].rl, entry)
2341 if (e->xvisibility.window == r->bar_window)
2342 bar_update();
2343 }
2344
2345 int
2346 xerror_start(Display *d, XErrorEvent *ee)
2347 {
2348 other_wm = 1;
2349 return (-1);
2350 }
2351
2352 int
2353 xerror(Display *d, XErrorEvent *ee)
2354 {
2355 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2356 return (-1);
2357 }
2358
2359 int
2360 active_wm(void)
2361 {
2362 other_wm = 0;
2363 xerrorxlib = XSetErrorHandler(xerror_start);
2364
2365 /* this causes an error if some other window manager is running */
2366 XSelectInput(display, DefaultRootWindow(display),
2367 SubstructureRedirectMask);
2368 XSync(display, False);
2369 if (other_wm)
2370 return (1);
2371
2372 XSetErrorHandler(xerror);
2373 XSync(display, False);
2374 return (0);
2375 }
2376
2377 long
2378 getstate(Window w)
2379 {
2380 int format, status;
2381 long result = -1;
2382 unsigned char *p = NULL;
2383 unsigned long n, extra;
2384 Atom real;
2385
2386 astate = XInternAtom(display, "WM_STATE", False);
2387 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
2388 &real, &format, &n, &extra, (unsigned char **)&p);
2389 if (status != Success)
2390 return (-1);
2391 if (n != 0)
2392 result = *p;
2393 XFree(p);
2394 return (result);
2395 }
2396
2397 void
2398 remove_region(struct swm_region *r)
2399 {
2400 struct swm_screen *s = r->s;
2401 struct ws_win *win;
2402
2403 DNPRINTF(SWM_D_MISC, "removing region: screen[%d]:%dx%d+%d+%d\n",
2404 s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r));
2405
2406 TAILQ_FOREACH(win, &r->ws->winlist, entry)
2407 XUnmapWindow(display, win->id);
2408 r->ws->r = NULL;
2409
2410 XDestroyWindow(display, r->bar_window);
2411 TAILQ_REMOVE(&s->rl, r, entry);
2412 free(r);
2413 }
2414
2415 void
2416 new_region(struct swm_screen *s, struct workspace *ws,
2417 int x, int y, int w, int h)
2418 {
2419 struct swm_region *r, *n;
2420 int i;
2421
2422 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
2423 s->idx, w, h, x, y);
2424
2425 /* remove any conflicting regions */
2426 n = TAILQ_FIRST(&s->rl);
2427 while (n) {
2428 r = n;
2429 n = TAILQ_NEXT(r, entry);
2430 if (X(r) < (x + w) &&
2431 (X(r) + WIDTH(r)) > x &&
2432 Y(r) < (y + h) &&
2433 (Y(r) + HEIGHT(r)) > y) {
2434 if (ws == NULL)
2435 ws = r->ws;
2436 remove_region(r);
2437 }
2438 }
2439
2440 /* pick an appropriate workspace */
2441 if (ws == NULL) {
2442 for (i = 0; i < SWM_WS_MAX; i++)
2443 if (s->ws[i].r == NULL) {
2444 ws = &s->ws[i];
2445 break;
2446 }
2447
2448 if (ws == NULL)
2449 errx(1, "no free regions\n");
2450 }
2451
2452 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
2453 errx(1, "calloc: failed to allocate memory for screen");
2454
2455 X(r) = x;
2456 Y(r) = y;
2457 WIDTH(r) = w;
2458 HEIGHT(r) = h;
2459 r->s = s;
2460 r->ws = ws;
2461 ws->r = r;
2462 TAILQ_INSERT_TAIL(&s->rl, r, entry);
2463 bar_setup(r);
2464 }
2465
2466 void
2467 setup_screens(void)
2468 {
2469 #ifdef SWM_XRR_HAS_CRTC
2470 XRRCrtcInfo *ci;
2471 XRRScreenResources *sr;
2472 int c;
2473 #endif /* SWM_XRR_HAS_CRTC */
2474 Window d1, d2, *wins = NULL;
2475 XWindowAttributes wa;
2476 struct swm_region *r;
2477 unsigned int no;
2478 int errorbase, major, minor;
2479 int ncrtc = 0, w = 0;
2480 int i, j, k;
2481 struct workspace *ws;
2482 int ws_idx_atom;
2483
2484
2485 if ((screens = calloc(ScreenCount(display),
2486 sizeof(struct swm_screen))) == NULL)
2487 errx(1, "calloc: screens");
2488
2489 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2490
2491
2492 /* map physical screens */
2493 for (i = 0; i < ScreenCount(display); i++) {
2494 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
2495 screens[i].idx = i;
2496 TAILQ_INIT(&screens[i].rl);
2497 screens[i].root = RootWindow(display, i);
2498
2499 /* set default colors */
2500 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
2501 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
2502 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
2503 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
2504 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
2505
2506 /* init all workspaces */
2507 for (j = 0; j < SWM_WS_MAX; j++) {
2508 ws = &screens[i].ws[j];
2509 ws->idx = j;
2510 ws->restack = 1;
2511 ws->focus = NULL;
2512 ws->r = NULL;
2513 TAILQ_INIT(&ws->winlist);
2514
2515 for (k = 0; layouts[k].l_stack != NULL; k++)
2516 if (layouts[k].l_config != NULL)
2517 layouts[k].l_config(ws,
2518 SWM_ARG_ID_STACKINIT);
2519 ws->cur_layout = &layouts[0];
2520 }
2521
2522 /* map virtual screens onto physical screens */
2523 screens[i].xrandr_support = XRRQueryExtension(display,
2524 &xrandr_eventbase, &errorbase);
2525 if (screens[i].xrandr_support)
2526 if (XRRQueryVersion(display, &major, &minor) &&
2527 major < 1)
2528 screens[i].xrandr_support = 0;
2529
2530 #if 0 /* not ready for dynamic screen changes */
2531 if (screens[i].xrandr_support)
2532 XRRSelectInput(display,
2533 screens[r->s].root,
2534 RRScreenChangeNotifyMask);
2535 #endif
2536
2537 /* grab existing windows (before we build the bars)*/
2538 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
2539 continue;
2540
2541 #ifdef SWM_XRR_HAS_CRTC
2542 sr = XRRGetScreenResources(display, screens[i].root);
2543 if (sr == NULL)
2544 new_region(&screens[i], &screens[i].ws[w],
2545 0, 0, DisplayWidth(display, i),
2546 DisplayHeight(display, i));
2547 else
2548 ncrtc = sr->ncrtc;
2549
2550 for (c = 0, ci = NULL; c < ncrtc; c++) {
2551 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
2552 if (ci->noutput == 0)
2553 continue;
2554
2555 if (ci != NULL && ci->mode == None)
2556 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2557 DisplayWidth(display, i),
2558 DisplayHeight(display, i));
2559 else
2560 new_region(&screens[i], &screens[i].ws[w],
2561 ci->x, ci->y, ci->width, ci->height);
2562 w++;
2563 }
2564 if (ci)
2565 XRRFreeCrtcInfo(ci);
2566 XRRFreeScreenResources(sr);
2567 #else
2568 new_region(&screens[i], &screens[i].ws[w], 0, 0,
2569 DisplayWidth(display, i),
2570 DisplayHeight(display, i));
2571 #endif /* SWM_XRR_HAS_CRTC */
2572
2573 /* attach windows to a region */
2574 /* normal windows */
2575 if ((r = TAILQ_FIRST(&screens[i].rl)) == NULL)
2576 errx(1, "no regions on screen %d", i);
2577
2578 for (i = 0; i < no; i++) {
2579 XGetWindowAttributes(display, wins[i], &wa);
2580 if (!XGetWindowAttributes(display, wins[i], &wa) ||
2581 wa.override_redirect ||
2582 XGetTransientForHint(display, wins[i], &d1))
2583 continue;
2584
2585 if (wa.map_state == IsViewable ||
2586 getstate(wins[i]) == NormalState)
2587 manage_window(wins[i]);
2588 }
2589 /* transient windows */
2590 for (i = 0; i < no; i++) {
2591 if (!XGetWindowAttributes(display, wins[i], &wa))
2592 continue;
2593
2594 if (XGetTransientForHint(display, wins[i], &d1) &&
2595 (wa.map_state == IsViewable || getstate(wins[i]) ==
2596 NormalState))
2597 manage_window(wins[i]);
2598 }
2599 if (wins) {
2600 XFree(wins);
2601 wins = NULL;
2602 }
2603 }
2604 }
2605
2606 int
2607 main(int argc, char *argv[])
2608 {
2609 struct passwd *pwd;
2610 char conf[PATH_MAX], *cfile = NULL;
2611 struct stat sb;
2612 XEvent e;
2613 int xfd;
2614 fd_set rd;
2615
2616 start_argv = argv;
2617 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
2618 SWM_VERSION, cvstag);
2619 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2620 warnx("no locale support");
2621
2622 if (!(display = XOpenDisplay(0)))
2623 errx(1, "can not open display");
2624
2625 if (active_wm())
2626 errx(1, "other wm running");
2627
2628 astate = XInternAtom(display, "WM_STATE", False);
2629
2630 /* look for local and global conf file */
2631 pwd = getpwuid(getuid());
2632 if (pwd == NULL)
2633 errx(1, "invalid user %d", getuid());
2634
2635 setup_screens();
2636
2637 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
2638 if (stat(conf, &sb) != -1) {
2639 if (S_ISREG(sb.st_mode))
2640 cfile = conf;
2641 } else {
2642 /* try global conf file */
2643 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
2644 if (!stat(conf, &sb))
2645 if (S_ISREG(sb.st_mode))
2646 cfile = conf;
2647 }
2648 if (cfile)
2649 conf_load(cfile);
2650 bar_refresh();
2651
2652 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2653
2654 grabkeys();
2655 stack();
2656
2657 xfd = ConnectionNumber(display);
2658 while (running) {
2659 FD_ZERO(&rd);
2660 FD_SET(xfd, &rd);
2661 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
2662 if (errno != EINTR)
2663 errx(1, "select failed");
2664 if (bar_alarm) {
2665 bar_alarm = 0;
2666 bar_update();
2667 }
2668 while(XPending(display)) {
2669 XNextEvent(display, &e);
2670 if (handler[e.type])
2671 handler[e.type](&e);
2672 }
2673 }
2674
2675 XCloseDisplay(display);
2676
2677 return (0);
2678 }