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