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