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