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