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