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