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