]> code.delx.au - spectrwm/blob - scrotwm.c
Make max (maximized single window) layout handler mostly work.
[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
77 /* #define SWM_DEBUG */
78 #ifdef SWM_DEBUG
79 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_MISC 0x0001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_MOVE 0x0010
86
87 u_int32_t swm_debug = 0
88 | SWM_D_MISC
89 | SWM_D_EVENT
90 | SWM_D_WS
91 | SWM_D_FOCUS
92 | SWM_D_MOVE
93 ;
94 #else
95 #define DPRINTF(x...)
96 #define DNPRINTF(n,x...)
97 #endif
98
99 #define LENGTH(x) (sizeof x / sizeof x[0])
100 #define MODKEY Mod1Mask
101 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
102
103 int (*xerrorxlib)(Display *, XErrorEvent *);
104 int other_wm;
105 int screen;
106 int running = 1;
107 int ignore_enter = 0;
108 unsigned int numlockmask = 0;
109 unsigned long color_focus = 0xff0000; /* XXX should this be per ws? */
110 unsigned long color_unfocus = 0x888888;
111 Display *display;
112 Window root;
113
114 /* status bar */
115 int bar_enabled = 1;
116 int bar_height = 0;
117 unsigned long bar_border = 0x008080;
118 unsigned long bar_color = 0x000000;
119 unsigned long bar_font_color = 0xa0a0a0;
120 Window bar_window;
121 GC bar_gc;
122 XGCValues bar_gcv;
123 XFontStruct *bar_fs;
124 char bar_text[128];
125 char *bar_fonts[] = {
126 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
127 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
128 NULL
129 };
130
131 /* terminal + args */
132 char *spawn_term[] = { "xterm", NULL };
133 char *spawn_menu[] = { "dmenu_run", NULL };
134 char *spawn_scrotwm[] = { "scrotwm", NULL };
135
136 /* layout manager data */
137 struct swm_geometry {
138 int x;
139 int y;
140 int w;
141 int h;
142 };
143
144
145 struct ws_win {
146 TAILQ_ENTRY(ws_win) entry;
147 Window id;
148 struct swm_geometry g;
149 int floating;
150 int transient;
151 XWindowAttributes wa;
152 XSizeHints sh;
153 };
154 TAILQ_HEAD(ws_win_list, ws_win);
155
156
157 /* layout handlers */
158 void stack(void);
159 void vertical_init(int);
160 void vertical_resize(int);
161 void vertical_stack(struct swm_geometry *);
162 void horizontal_init(int);
163 void horizontal_resize(int);
164 void horizontal_stack(struct swm_geometry *);
165 void max_init(int);
166 void max_focus(struct ws_win *);
167 void max_stack(struct swm_geometry *);
168
169 struct layout {
170 void (*l_init)(int); /* init/reset */
171 void (*l_stack)(struct swm_geometry *);
172 void (*l_resize)(int);
173 void (*l_focus)(struct ws_win *);
174 } layouts[] = {
175 /* init stack, resize */
176 { vertical_init, vertical_stack, vertical_resize, NULL},
177 { horizontal_init, horizontal_stack, horizontal_resize, NULL},
178 { NULL, max_stack, NULL, max_focus},
179 { NULL, NULL, NULL, NULL},
180 };
181
182
183 /* define work spaces */
184 #define SWM_WS_MAX (10)
185 struct workspace {
186 int visible; /* workspace visible */
187 int restack; /* restack on switch */
188 struct swm_geometry g;
189 struct layout *cur_layout; /* current layout handlers */
190 struct ws_win *focus; /* which win has focus */
191 struct ws_win_list winlist; /* list of windows in ws */
192 } ws[SWM_WS_MAX];
193 int current_ws = 0;
194
195 /* args to functions */
196 union arg {
197 int id;
198 #define SWM_ARG_ID_FOCUSNEXT (0)
199 #define SWM_ARG_ID_FOCUSPREV (1)
200 #define SWM_ARG_ID_FOCUSMAIN (2)
201 #define SWM_ARG_ID_SWAPNEXT (3)
202 #define SWM_ARG_ID_SWAPPREV (4)
203 #define SWM_ARG_ID_SWAPMAIN (5)
204 #define SWM_ARG_ID_MASTERSHRINK (6)
205 #define SWM_ARG_ID_MASTERGROW (7)
206 char **argv;
207 };
208
209 /* conf file stuff */
210 #define SWM_CONF_WS "\n= \t"
211 #define SWM_CONF_FILE "scrotwm.conf"
212 int
213 conf_load(char *filename)
214 {
215 FILE *config;
216 char *line, *cp, *var, *val;
217 size_t len, lineno = 0;
218
219 DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
220
221 if (filename == NULL)
222 return (1);
223
224 if ((config = fopen(filename, "r")) == NULL)
225 return (1);
226
227 for (;;) {
228 if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
229 if (feof(config))
230 break;
231 cp = line;
232 cp += (long)strspn(cp, SWM_CONF_WS);
233 if (cp[0] == '\0') {
234 /* empty line */
235 free(line);
236 continue;
237 }
238 if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
239 break;
240 cp += (long)strspn(cp, SWM_CONF_WS);
241 if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
242 break;
243
244 DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
245 switch (var[0]) {
246 case 'b':
247 if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
248 bar_enabled = atoi(val);
249 else if (!strncmp(var, "bar_border",
250 strlen("bar_border")))
251 bar_border = strtol(val, NULL, 16);
252 else if (!strncmp(var, "bar_color",
253 strlen("bar_color")))
254 bar_color = strtol(val, NULL, 16);
255 else if (!strncmp(var, "bar_font_color",
256 strlen("bar_font_color")))
257 bar_font_color = strtol(val, NULL, 16);
258 else if (!strncmp(var, "bar_font", strlen("bar_font")))
259 asprintf(&bar_fonts[0], "%s", val);
260 else
261 goto bad;
262 break;
263
264 case 'c':
265 if (!strncmp(var, "color_focus", strlen("color_focus")))
266 color_focus = strtol(val, NULL, 16);
267 else if (!strncmp(var, "color_unfocus",
268 strlen("color_unfocus")))
269 color_unfocus = strtol(val, NULL, 16);
270 else
271 goto bad;
272 break;
273
274 case 's':
275 if (!strncmp(var, "spawn_term", strlen("spawn_term")))
276 asprintf(&spawn_term[0], "%s", val); /* XXX args? */
277 break;
278 default:
279 goto bad;
280 }
281 free(line);
282 }
283
284 fclose(config);
285 return (0);
286 bad:
287 errx(1, "invalid conf file entry: %s=%s", var, val);
288 }
289
290 void
291 bar_print(void)
292 {
293 time_t tmt;
294 struct tm tm;
295
296 if (bar_enabled == 0)
297 return;
298
299 /* clear old text */
300 XSetForeground(display, bar_gc, bar_color);
301 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
302 strlen(bar_text));
303
304 /* draw new text */
305 time(&tmt);
306 localtime_r(&tmt, &tm);
307 strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
308 XSetForeground(display, bar_gc, bar_font_color);
309 XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
310 strlen(bar_text));
311 XSync(display, False);
312
313 alarm(60);
314 }
315
316 void
317 bar_signal(int sig)
318 {
319 /* XXX yeah yeah byte me */
320 bar_print();
321 }
322
323 void
324 bar_toggle(union arg *args)
325 {
326 int i;
327
328 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
329
330 if (bar_enabled) {
331 bar_enabled = 0;
332 XUnmapWindow(display, bar_window);
333 } else {
334 bar_enabled = 1;
335 XMapWindow(display, bar_window);
336 }
337 XSync(display, False);
338 for (i = 0; i < SWM_WS_MAX; i++)
339 ws[i].restack = 1;
340
341 stack();
342 bar_print(); /* must be after stack */
343 }
344
345 void
346 bar_setup(void)
347 {
348 int i;
349
350 for (i = 0; bar_fonts[i] != NULL; i++) {
351 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
352 if (bar_fs)
353 break;
354 }
355 if (bar_fonts[i] == NULL)
356 errx(1, "couldn't load font");
357 bar_height = bar_fs->ascent + bar_fs->descent + 3;
358
359 bar_window = XCreateSimpleWindow(display, root, 0, 0,
360 ws[current_ws].g.w, bar_height - 2, 1, bar_border, bar_color);
361 bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
362 XSetFont(display, bar_gc, bar_fs->fid);
363 XSelectInput(display, bar_window, VisibilityChangeMask);
364 if (bar_enabled) {
365 XMapWindow(display, bar_window);
366 }
367 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
368
369 if (signal(SIGALRM, bar_signal) == SIG_ERR)
370 err(1, "could not install bar_signal");
371 bar_print();
372 }
373
374 int
375 count_win(int wsid, int count_transient)
376 {
377 struct ws_win *win;
378 int count = 0;
379
380 TAILQ_FOREACH (win, &ws[wsid].winlist, entry) {
381 if (count_transient == 0 && win->transient)
382 continue;
383 count++;
384 }
385 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
386
387 return (count);
388 }
389 void
390 quit(union arg *args)
391 {
392 DNPRINTF(SWM_D_MISC, "quit\n");
393 running = 0;
394 }
395
396
397 void
398 restart(union arg *args)
399 {
400 XCloseDisplay(display);
401 execvp(args->argv[0], args->argv);
402 fprintf(stderr, "execvp failed\n");
403 perror(" failed");
404 quit(NULL);
405 }
406
407 void
408 spawn(union arg *args)
409 {
410 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
411 /*
412 * The double-fork construct avoids zombie processes and keeps the code
413 * clean from stupid signal handlers.
414 */
415 if(fork() == 0) {
416 if(fork() == 0) {
417 if(display)
418 close(ConnectionNumber(display));
419 setsid();
420 execvp(args->argv[0], args->argv);
421 fprintf(stderr, "execvp failed\n");
422 perror(" failed");
423 }
424 exit(0);
425 }
426 wait(0);
427 }
428
429 void
430 focus_win(struct ws_win *win)
431 {
432 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id);
433 XSetWindowBorder(display, win->id, color_focus);
434 XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime);
435 ws[current_ws].focus = win;
436 }
437
438 void
439 unfocus_win(struct ws_win *win)
440 {
441 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id);
442 XSetWindowBorder(display, win->id, color_unfocus);
443 if (ws[current_ws].focus == win)
444 ws[current_ws].focus = NULL;
445 }
446
447 void
448 switchws(union arg *args)
449 {
450 int wsid = args->id;
451 struct ws_win *win;
452
453 DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1);
454
455 if (wsid == current_ws)
456 return;
457
458 /* map new window first to prevent ugly blinking */
459 TAILQ_FOREACH (win, &ws[wsid].winlist, entry)
460 XMapRaised(display, win->id);
461 ws[wsid].visible = 1;
462
463 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry)
464 XUnmapWindow(display, win->id);
465 ws[current_ws].visible = 0;
466
467 current_ws = wsid;
468
469 ignore_enter = 1;
470 if (ws[wsid].restack) {
471 stack();
472 bar_print();
473 } else {
474 if (ws[wsid].focus != NULL)
475 focus_win(ws[wsid].focus);
476 XSync(display, False);
477 }
478 }
479
480 void
481 swapwin(union arg *args)
482 {
483 struct ws_win *target;
484
485 DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id);
486 if (ws[current_ws].focus == NULL)
487 return;
488
489 switch (args->id) {
490 case SWM_ARG_ID_SWAPPREV:
491 target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
492 TAILQ_REMOVE(&ws[current_ws].winlist,
493 ws[current_ws].focus, entry);
494 if (target == NULL)
495 TAILQ_INSERT_TAIL(&ws[current_ws].winlist,
496 ws[current_ws].focus, entry);
497 else
498 TAILQ_INSERT_BEFORE(target, ws[current_ws].focus,
499 entry);
500 break;
501 case SWM_ARG_ID_SWAPNEXT:
502 target = TAILQ_NEXT(ws[current_ws].focus, entry);
503 TAILQ_REMOVE(&ws[current_ws].winlist,
504 ws[current_ws].focus, entry);
505 if (target == NULL)
506 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
507 ws[current_ws].focus, entry);
508 else
509 TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target,
510 ws[current_ws].focus, entry);
511 break;
512 case SWM_ARG_ID_SWAPMAIN:
513 target = TAILQ_FIRST(&ws[current_ws].winlist);
514 if (target == ws[current_ws].focus)
515 return;
516 TAILQ_REMOVE(&ws[current_ws].winlist, target, entry);
517 TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry);
518 TAILQ_REMOVE(&ws[current_ws].winlist,
519 ws[current_ws].focus, entry);
520 TAILQ_INSERT_HEAD(&ws[current_ws].winlist,
521 ws[current_ws].focus, entry);
522 break;
523 default:
524 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
525 return;
526 }
527
528 ignore_enter = 2;
529 stack();
530 }
531
532 void
533 focus(union arg *args)
534 {
535 struct ws_win *winfocus, *winlostfocus;
536
537 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
538 if (ws[current_ws].focus == NULL)
539 return;
540
541 winlostfocus = ws[current_ws].focus;
542
543 switch (args->id) {
544 case SWM_ARG_ID_FOCUSPREV:
545 winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry);
546 if (winfocus == NULL)
547 winfocus =
548 TAILQ_LAST(&ws[current_ws].winlist, ws_win_list);
549 break;
550
551 case SWM_ARG_ID_FOCUSNEXT:
552 winfocus = TAILQ_NEXT(ws[current_ws].focus, entry);
553 if (winfocus == NULL)
554 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
555 break;
556
557 case SWM_ARG_ID_FOCUSMAIN:
558 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
559 break;
560
561 default:
562 return;
563 }
564
565 if (winfocus == winlostfocus)
566 return;
567
568 unfocus_win(winlostfocus);
569 focus_win(winfocus);
570 /* XXX if we hook in focus_win(), we get a nasty cycle */
571 if (ws[current_ws].cur_layout->l_focus != NULL)
572 ws[current_ws].cur_layout->l_focus(winfocus);
573 XSync(display, False);
574 }
575
576 void
577 cycle_layout(union arg *args)
578 {
579 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws);
580
581 ws[current_ws].cur_layout++;
582 if (ws[current_ws].cur_layout->l_stack == NULL)
583 ws[current_ws].cur_layout = &layouts[0];
584
585 stack();
586 }
587
588 void
589 resize_master(union arg *args)
590 {
591 DNPRINTF(SWM_D_EVENT, "resize_master: id %d\n", args->id);
592
593 if (ws[current_ws].cur_layout->l_resize != NULL)
594 ws[current_ws].cur_layout->l_resize(args->id);
595 }
596
597 void
598 stack_reset(union arg *args)
599 {
600 DNPRINTF(SWM_D_EVENT, "stack_reset: ws %d\n", current_ws);
601
602 if (ws[current_ws].cur_layout->l_init != NULL) {
603 ws[current_ws].cur_layout->l_init(current_ws);
604 stack();
605 }
606 }
607
608 void
609 stack(void) {
610 struct swm_geometry g;
611 DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws);
612
613 /* start with workspace geometry, adjust for bar */
614 g = ws[current_ws].g;
615 if (bar_enabled) {
616 g.y += bar_height;
617 g.h -= bar_height;
618 }
619
620 ws[current_ws].restack = 0;
621 ws[current_ws].cur_layout->l_stack(&g);
622 XSync(display, False);
623 }
624
625 void
626 stack_floater(struct ws_win *win)
627 {
628 unsigned int mask;
629 XWindowChanges wc;
630
631 bzero(&wc, sizeof wc);
632 wc.border_width = 1;
633 mask = CWX | CWY | CWBorderWidth;
634
635 /* use obsolete width height */
636 if (win->sh.flags & USPosition) {
637 win->g.w = wc.width = win->sh.width;
638 win->g.h = wc.height = win->sh.height;
639 mask |= CWWidth | CWHeight;
640 }
641
642 /* try min max */
643 if (win->sh.flags & PMinSize) {
644 /* some hints are retarded */
645 if (win->sh.min_width < ws[current_ws].g.w / 10)
646 win->sh.min_width = ws[current_ws].g.w / 3;
647 if (win->sh.min_height < ws[current_ws].g.h / 10)
648 win->wa.height = ws[current_ws].g.h / 3;
649
650 win->g.w = wc.width = win->sh.min_width * 2;
651 win->g.h = wc.height = win->sh.min_height * 2;
652 mask |= CWWidth | CWHeight;
653 }
654 if (win->sh.flags & PMaxSize) {
655 /* potentially override min values */
656 if (win->sh.max_width < ws[current_ws].g.w) {
657 win->g.w = wc.width = win->sh.max_width;
658 mask |= CWWidth;
659 }
660 if (win->sh.max_height < ws[current_ws].g.h) {
661 win->g.h = wc.height = win->sh.max_height;
662 mask |= CWHeight;
663 }
664 }
665
666 /* make sure we don't clobber the screen */
667 if ((mask & CWWidth) && win->wa.width > ws[current_ws].g.w)
668 win->wa.width = ws[current_ws].g.w - 4;
669 if ((mask & CWHeight) && win->wa.height > ws[current_ws].g.h)
670 win->wa.height = ws[current_ws].g.h - 4;
671
672 /* supposed to be obsolete */
673 if (win->sh.flags & USPosition) {
674 win->g.x = wc.x = win->sh.x;
675 win->g.y = wc.y = win->sh.y;
676 } else {
677 win->g.x = wc.x = (ws[current_ws].g.w - win->wa.width) / 2;
678 win->g.y = wc.y = (ws[current_ws].g.h - win->wa.height) / 2;
679 }
680 DNPRINTF(SWM_D_EVENT, "stack_floater: win %d x %d y %d w %d h %d\n",
681 win, wc.x, wc.y, wc.width, wc.height);
682
683 XConfigureWindow(display, win->id, mask, &wc);
684 }
685
686
687 int vertical_msize[SWM_WS_MAX];
688
689 void
690 vertical_init(int ws_idx)
691 {
692 DNPRINTF(SWM_D_MISC, "vertical_init: workspace: %d\n", current_ws);
693
694 vertical_msize[ws_idx] = ws[ws_idx].g.w / 2;
695 }
696
697 void
698 vertical_resize(int id)
699 {
700 DNPRINTF(SWM_D_MISC, "vertical_resize: workspace: %d\n", current_ws);
701
702 switch (id) {
703 case SWM_ARG_ID_MASTERSHRINK:
704 vertical_msize[current_ws] -= ws[current_ws].g.w / 32;
705 if ( vertical_msize[current_ws] < ws[current_ws].g.w / 16)
706 vertical_msize[current_ws] = ws[current_ws].g.w / 16;
707 break;
708 case SWM_ARG_ID_MASTERGROW:
709 vertical_msize[current_ws] += ws[current_ws].g.w / 32;
710 if ( vertical_msize[current_ws] >
711 (ws[current_ws].g.w - (ws[current_ws].g.w / 16)))
712 vertical_msize[current_ws] =
713 ws[current_ws].g.w - ws[current_ws].g.w / 16;
714 break;
715 default:
716 return;
717 }
718 stack();
719 }
720
721 /* I know this sucks but it works well enough */
722 void
723 vertical_stack(struct swm_geometry *g) {
724 XWindowChanges wc;
725 struct swm_geometry gg = *g;
726 struct ws_win wf, *win, *winfocus = &wf;
727 int i, hrh, winno;
728 unsigned int mask;
729
730 DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws);
731
732 winfocus->id = root;
733
734 winno = count_win(current_ws, 0);
735 if (winno == 0)
736 return;
737
738 if (winno > 1)
739 gg.w = vertical_msize[current_ws];
740
741 if (winno > 2)
742 hrh = g->h / (winno - 1);
743 else
744 hrh = 0;
745
746 i = 0;
747 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
748 if (i == 1) {
749 gg.x += vertical_msize[current_ws] + 2;
750 gg.w = g->w - (vertical_msize[current_ws] + 2);
751 }
752 if (i != 0 && hrh != 0) {
753 /* correct the last window for lost pixels */
754 if (win == TAILQ_LAST(&ws[current_ws].winlist,
755 ws_win_list)) {
756 gg.h = hrh + (g->h - (i * hrh));
757 gg.y += hrh;
758 } else {
759 gg.h = hrh - 2;
760 /* leave first right hand window at y = 0 */
761 if (i > 1)
762 gg.y += gg.h + 2;
763 }
764 }
765
766 if (win->transient != 0 || win->floating != 0)
767 stack_floater(win);
768 else {
769 bzero(&wc, sizeof wc);
770 wc.border_width = 1;
771 win->g.x = wc.x = gg.x;
772 win->g.y = wc.y = gg.y;
773 win->g.w = wc.width = gg.w;
774 win->g.h = wc.height = gg.h;
775 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
776 XConfigureWindow(display, win->id, mask, &wc);
777 }
778
779 if (win == ws[current_ws].focus)
780 winfocus = win;
781 else
782 unfocus_win(win);
783 XMapRaised(display, win->id);
784 i++;
785 }
786
787 focus_win(winfocus); /* this has to be done outside of the loop */
788 }
789
790 int horizontal_msize[SWM_WS_MAX];
791
792 void
793 horizontal_init(int ws_idx)
794 {
795 DNPRINTF(SWM_D_MISC, "horizontal_init: workspace: %d\n", current_ws);
796
797 horizontal_msize[ws_idx] = ws[ws_idx].g.h / 2;
798 }
799
800 void
801 horizontal_resize(int id)
802 {
803 DNPRINTF(SWM_D_MISC, "horizontal_resize: workspace: %d\n", current_ws);
804
805 switch (id) {
806 case SWM_ARG_ID_MASTERSHRINK:
807 horizontal_msize[current_ws] -= ws[current_ws].g.h / 32;
808 if ( horizontal_msize[current_ws] < ws[current_ws].g.h / 16)
809 horizontal_msize[current_ws] = ws[current_ws].g.h / 16;
810 break;
811 case SWM_ARG_ID_MASTERGROW:
812 horizontal_msize[current_ws] += ws[current_ws].g.h / 32;
813 if ( horizontal_msize[current_ws] >
814 (ws[current_ws].g.h - (ws[current_ws].g.h / 16)))
815 horizontal_msize[current_ws] =
816 ws[current_ws].g.h - ws[current_ws].g.h / 16;
817 break;
818 default:
819 return;
820 }
821 stack();
822 }
823
824 void
825 horizontal_stack(struct swm_geometry *g) {
826 XWindowChanges wc;
827 struct swm_geometry gg = *g;
828 struct ws_win wf, *win, *winfocus = &wf;
829 int i, hrw, winno;
830 unsigned int mask;
831
832 DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws);
833
834 winfocus->id = root;
835
836 winno = count_win(current_ws, 0);
837 if (winno == 0)
838 return;
839
840 if (winno > 1)
841 gg.h = horizontal_msize[current_ws];
842
843 if (winno > 2)
844 hrw = g->w / (winno - 1);
845 else
846 hrw = 0;
847
848 i = 0;
849 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
850 if (i == 1) {
851 gg.y += horizontal_msize[current_ws] + 2;
852 gg.h = g->h - (horizontal_msize[current_ws] + 2);
853 }
854 if (i != 0 && hrw != 0) {
855 /* correct the last window for lost pixels */
856 if (win == TAILQ_LAST(&ws[current_ws].winlist,
857 ws_win_list)) {
858 gg.w = hrw + (g->w - (i * hrw));
859 gg.x += hrw;
860 } else {
861 gg.w = hrw - 2;
862 /* leave first bottom window at x = 0 */
863 if (i > 1)
864 gg.x += gg.w + 2;
865 }
866 }
867
868 if (win->transient != 0 || win->floating != 0)
869 stack_floater(win);
870 else {
871 bzero(&wc, sizeof wc);
872 wc.border_width = 1;
873 win->g.x = wc.x = gg.x;
874 win->g.y = wc.y = gg.y;
875 win->g.w = wc.width = gg.w;
876 win->g.h = wc.height = gg.h;
877 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
878 XConfigureWindow(display, win->id, mask, &wc);
879 }
880
881 if (win == ws[current_ws].focus)
882 winfocus = win;
883 else
884 unfocus_win(win);
885 XMapRaised(display, win->id);
886 i++;
887 }
888
889 focus_win(winfocus); /* this has to be done outside of the loop */
890 }
891
892 void
893 max_focus(struct ws_win *fwin)
894 {
895 struct ws_win *win;
896
897 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
898 if (win->transient == 0 && win->floating == 0 && win == fwin)
899 XMapRaised(display, win->id);
900 else
901 XUnmapWindow(display, win->id);
902 }
903 }
904
905 /* fullscreen view */
906 void
907 max_stack(struct swm_geometry *g) {
908 XWindowChanges wc;
909 struct swm_geometry gg = *g;
910 struct ws_win *win, *winfocus;
911 int i, found = 0, winno;
912 unsigned int mask;
913
914 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
915
916 winno = count_win(current_ws, 0);
917 if (winno == 0)
918 return;
919
920 winfocus = TAILQ_FIRST(&ws[current_ws].winlist);
921
922 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
923 if (win->transient != 0 || win->floating != 0) {
924 stack_floater(win);
925 } else {
926 bzero(&wc, sizeof wc);
927 wc.border_width = 1;
928 win->g.x = wc.x = gg.x;
929 win->g.y = wc.y = gg.y;
930 win->g.w = wc.width = gg.w;
931 win->g.h = wc.height = gg.h;
932 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
933 XConfigureWindow(display, win->id, mask, &wc);
934
935 if (!found) {
936 found = 1;
937 XMapRaised(display, win->id);
938 } else {
939 /* hide all but the master window */
940 XUnmapWindow(display, win->id);
941 }
942 }
943 if (win == ws[current_ws].focus)
944 winfocus = win;
945 else
946 unfocus_win(win);
947 i++;
948 }
949
950 focus_win(winfocus); /* this has to be done outside of the loop */
951 }
952
953 void
954 send_to_ws(union arg *args)
955 {
956 int wsid = args->id;
957 struct ws_win *win = ws[current_ws].focus;
958
959 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
960
961 XUnmapWindow(display, win->id);
962
963 /* find a window to focus */
964 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
965 if (ws[current_ws].focus == NULL)
966 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
967 if (ws[current_ws].focus == win)
968 ws[current_ws].focus = NULL;
969
970 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
971
972 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
973 if (count_win(wsid, 1) == 1)
974 ws[wsid].focus = win;
975 ws[wsid].restack = 1;
976
977 stack();
978 }
979
980 /* key definitions */
981 struct key {
982 unsigned int mod;
983 KeySym keysym;
984 void (*func)(union arg *);
985 union arg args;
986 } keys[] = {
987 /* modifier key function argument */
988 { MODKEY, XK_space, cycle_layout, {0} },
989 { MODKEY | ShiftMask, XK_space, stack_reset, {0} },
990 { MODKEY, XK_h, resize_master, {.id = SWM_ARG_ID_MASTERSHRINK} },
991 { MODKEY, XK_l, resize_master, {.id = SWM_ARG_ID_MASTERGROW} },
992 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
993 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
994 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
995 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
996 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
997 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
998 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
999 { MODKEY | ShiftMask, XK_q, quit, {0} },
1000 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
1001 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1002 { MODKEY, XK_1, switchws, {.id = 0} },
1003 { MODKEY, XK_2, switchws, {.id = 1} },
1004 { MODKEY, XK_3, switchws, {.id = 2} },
1005 { MODKEY, XK_4, switchws, {.id = 3} },
1006 { MODKEY, XK_5, switchws, {.id = 4} },
1007 { MODKEY, XK_6, switchws, {.id = 5} },
1008 { MODKEY, XK_7, switchws, {.id = 6} },
1009 { MODKEY, XK_8, switchws, {.id = 7} },
1010 { MODKEY, XK_9, switchws, {.id = 8} },
1011 { MODKEY, XK_0, switchws, {.id = 9} },
1012 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
1013 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
1014 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
1015 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
1016 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
1017 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
1018 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
1019 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
1020 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
1021 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
1022 { MODKEY, XK_b, bar_toggle, {0} },
1023 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1024 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1025 };
1026
1027 void
1028 updatenumlockmask(void)
1029 {
1030 unsigned int i, j;
1031 XModifierKeymap *modmap;
1032
1033 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
1034 numlockmask = 0;
1035 modmap = XGetModifierMapping(display);
1036 for (i = 0; i < 8; i++)
1037 for (j = 0; j < modmap->max_keypermod; j++)
1038 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1039 == XKeysymToKeycode(display, XK_Num_Lock))
1040 numlockmask = (1 << i);
1041
1042 XFreeModifiermap(modmap);
1043 }
1044
1045 void
1046 grabkeys(void)
1047 {
1048 unsigned int i, j;
1049 KeyCode code;
1050 unsigned int modifiers[] =
1051 { 0, LockMask, numlockmask, numlockmask | LockMask };
1052
1053 DNPRINTF(SWM_D_MISC, "grabkeys\n");
1054 updatenumlockmask();
1055
1056 XUngrabKey(display, AnyKey, AnyModifier, root);
1057 for(i = 0; i < LENGTH(keys); i++) {
1058 if((code = XKeysymToKeycode(display, keys[i].keysym)))
1059 for(j = 0; j < LENGTH(modifiers); j++)
1060 XGrabKey(display, code,
1061 keys[i].mod | modifiers[j], root,
1062 True, GrabModeAsync, GrabModeAsync);
1063 }
1064 }
1065 void
1066 expose(XEvent *e)
1067 {
1068 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
1069 }
1070
1071 void
1072 keypress(XEvent *e)
1073 {
1074 unsigned int i;
1075 KeySym keysym;
1076 XKeyEvent *ev = &e->xkey;
1077
1078 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
1079
1080 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
1081 for(i = 0; i < LENGTH(keys); i++)
1082 if(keysym == keys[i].keysym
1083 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1084 && keys[i].func)
1085 keys[i].func(&(keys[i].args));
1086 }
1087
1088 void
1089 buttonpress(XEvent *e)
1090 {
1091 XButtonPressedEvent *ev = &e->xbutton;
1092 #ifdef SWM_CLICKTOFOCUS
1093 struct ws_win *win;
1094 #endif
1095
1096
1097 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
1098
1099 if (ev->window == root)
1100 return;
1101 if (ev->window == ws[current_ws].focus->id)
1102 return;
1103 #ifdef SWM_CLICKTOFOCUS
1104 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
1105 if (win->id == ev->window) {
1106 /* focus in the clicked window */
1107 XSetWindowBorder(display, ev->window, 0xff0000);
1108 XSetWindowBorder(display,
1109 ws[current_ws].focus->id, 0x888888);
1110 XSetInputFocus(display, ev->window, RevertToPointerRoot,
1111 CurrentTime);
1112 ws[current_ws].focus = win;
1113 XSync(display, False);
1114 break;
1115 }
1116 #endif
1117 }
1118
1119 struct ws_win *
1120 manage_window(Window id)
1121 {
1122 struct ws_win *win;
1123
1124 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1125 if (win->id == id)
1126 return (win); /* already being managed */
1127 }
1128
1129 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1130 errx(1, "calloc: failed to allocate memory for new window");
1131
1132 win->id = id;
1133 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1134
1135 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1136 FocusChangeMask | ExposureMask);
1137
1138 return win;
1139 }
1140
1141 void
1142 configurerequest(XEvent *e)
1143 {
1144 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1145 Window trans;
1146 struct ws_win *win;
1147
1148 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1149
1150
1151 win = manage_window(ev->window);
1152 ws[current_ws].focus = win; /* make new win focused */
1153
1154 XGetTransientForHint(display, win->id, &trans);
1155 if (trans) {
1156 win->transient = trans;
1157 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
1158 (unsigned)win->id, win->transient);
1159 }
1160 XGetWindowAttributes(display, win->id, &win->wa);
1161 XGetNormalHints(display, win->id, &win->sh);
1162 #if 0
1163 XClassHint ch = { 0 };
1164 if(XGetClassHint(display, win->id, &ch)) {
1165 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1166 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1167 win->floating = 0;
1168 }
1169 if(ch.res_class)
1170 XFree(ch.res_class);
1171 if(ch.res_name)
1172 XFree(ch.res_name);
1173 }
1174 #endif
1175 stack();
1176 }
1177
1178 void
1179 configurenotify(XEvent *e)
1180 {
1181
1182 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1183 e->xconfigure.window);
1184 }
1185
1186 void
1187 destroynotify(XEvent *e)
1188 {
1189 struct ws_win *win;
1190 XDestroyWindowEvent *ev = &e->xdestroywindow;
1191
1192 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1193
1194 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1195 if (ev->window == win->id) {
1196 /* find a window to focus */
1197 ws[current_ws].focus = TAILQ_PREV(win,
1198 ws_win_list, entry);
1199 if (ws[current_ws].focus == NULL)
1200 ws[current_ws].focus =
1201 TAILQ_FIRST(&ws[current_ws].winlist);
1202 if (win == ws[current_ws].focus)
1203 ws[current_ws].focus = NULL;
1204
1205 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1206 free(win);
1207 break;
1208 }
1209 }
1210
1211 stack();
1212 }
1213
1214 void
1215 enternotify(XEvent *e)
1216 {
1217 XCrossingEvent *ev = &e->xcrossing;
1218 struct ws_win *win;
1219
1220 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1221
1222 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1223 ev->window != root)
1224 return;
1225 if (ignore_enter) {
1226 /* eat event(s) to prevent autofocus */
1227 ignore_enter--;
1228 return;
1229 }
1230 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1231 if (win->id == ev->window)
1232 focus_win(win);
1233 else
1234 unfocus_win(win);
1235 }
1236 }
1237
1238 void
1239 focusin(XEvent *e)
1240 {
1241 XFocusChangeEvent *ev = &e->xfocus;
1242
1243 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1244
1245 XSync(display, False); /* not sure this helps redrawing graphic apps */
1246
1247 if (ev->window == root)
1248 return;
1249 /*
1250 * kill grab for now so that we can cut and paste , this screws up
1251 * click to focus
1252 */
1253 /*
1254 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1255 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1256 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1257 */
1258 }
1259
1260 void
1261 mappingnotify(XEvent *e)
1262 {
1263 XMappingEvent *ev = &e->xmapping;
1264
1265 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1266
1267 XRefreshKeyboardMapping(ev);
1268 if(ev->request == MappingKeyboard)
1269 grabkeys();
1270 }
1271
1272 void
1273 maprequest(XEvent *e)
1274 {
1275 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1276 e->xmaprequest.window);
1277
1278 manage_window(e->xmaprequest.window);
1279 stack();
1280 }
1281
1282 void
1283 propertynotify(XEvent *e)
1284 {
1285 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1286 e->xproperty.window);
1287 }
1288
1289 void
1290 unmapnotify(XEvent *e)
1291 {
1292 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1293 }
1294
1295 void
1296 visibilitynotify(XEvent *e)
1297 {
1298 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1299
1300 if (e->xvisibility.window == bar_window &&
1301 e->xvisibility.state == VisibilityUnobscured)
1302 bar_print();
1303 }
1304
1305 void (*handler[LASTEvent])(XEvent *) = {
1306 [Expose] = expose,
1307 [KeyPress] = keypress,
1308 [ButtonPress] = buttonpress,
1309 [ConfigureRequest] = configurerequest,
1310 [ConfigureNotify] = configurenotify,
1311 [DestroyNotify] = destroynotify,
1312 [EnterNotify] = enternotify,
1313 [FocusIn] = focusin,
1314 [MappingNotify] = mappingnotify,
1315 [MapRequest] = maprequest,
1316 [PropertyNotify] = propertynotify,
1317 [UnmapNotify] = unmapnotify,
1318 [VisibilityNotify] = visibilitynotify,
1319 };
1320
1321 int
1322 xerror_start(Display *d, XErrorEvent *ee)
1323 {
1324 other_wm = 1;
1325 return (-1);
1326 }
1327
1328 int
1329 xerror(Display *d, XErrorEvent *ee)
1330 {
1331 fprintf(stderr, "error: %p %p\n", display, ee);
1332
1333 return (-1);
1334 }
1335
1336 int
1337 active_wm(void)
1338 {
1339 other_wm = 0;
1340 xerrorxlib = XSetErrorHandler(xerror_start);
1341
1342 /* this causes an error if some other window manager is running */
1343 XSelectInput(display, DefaultRootWindow(display),
1344 SubstructureRedirectMask);
1345 XSync(display, False);
1346 if(other_wm)
1347 return (1);
1348
1349 XSetErrorHandler(xerror);
1350 XSync(display, False);
1351 return (0);
1352 }
1353
1354 int
1355 main(int argc, char *argv[])
1356 {
1357 struct passwd *pwd;
1358 char conf[PATH_MAX], *cfile = NULL;
1359 struct stat sb;
1360 XEvent e;
1361 unsigned int i, j, num;
1362 Window d1, d2, *wins = NULL;
1363 XWindowAttributes wa;
1364
1365 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1366 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1367 warnx("no locale support");
1368
1369 if(!(display = XOpenDisplay(0)))
1370 errx(1, "can not open display");
1371
1372 if (active_wm())
1373 errx(1, "other wm running");
1374
1375 screen = DefaultScreen(display);
1376 root = RootWindow(display, screen);
1377
1378 /* look for local and global conf file */
1379 pwd = getpwuid(getuid());
1380 if (pwd == NULL)
1381 errx(1, "invalid user %d", getuid());
1382
1383 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1384 if (stat(conf, &sb) != -1) {
1385 if (S_ISREG(sb.st_mode))
1386 cfile = conf;
1387 } else {
1388 /* try global conf file */
1389 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1390 if (!stat(conf, &sb))
1391 if (S_ISREG(sb.st_mode))
1392 cfile = conf;
1393 }
1394 if (cfile)
1395 conf_load(cfile);
1396
1397 /* init all workspaces */
1398 for (i = 0; i < SWM_WS_MAX; i++) {
1399 ws[i].visible = 0;
1400 ws[i].restack = 1;
1401 ws[i].focus = NULL;
1402 ws[i].g.x = 0;
1403 ws[i].g.y = 0;
1404 ws[i].g.w = DisplayWidth(display, screen) - 2;
1405 ws[i].g.h = DisplayHeight(display, screen) - 2;
1406 TAILQ_INIT(&ws[i].winlist);
1407
1408 for (j = 0; layouts[j].l_stack != NULL; j++) {
1409 if (layouts[j].l_init != NULL)
1410 layouts[j].l_init(i);
1411 }
1412 ws[i].cur_layout = &layouts[0];
1413 }
1414 /* make work space 1 active */
1415 ws[0].visible = 1;
1416
1417 /* grab existing windows */
1418 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1419 for (i = 0; i < num; i++) {
1420 if (!XGetWindowAttributes(display, wins[i], &wa)
1421 || wa.override_redirect ||
1422 XGetTransientForHint(display, wins[i], &d1))
1423 continue;
1424 manage_window(wins[i]);
1425 }
1426 if(wins)
1427 XFree(wins);
1428 }
1429 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1430
1431 /* setup status bar */
1432 bar_setup();
1433
1434 XSelectInput(display, root, SubstructureRedirectMask |
1435 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1436 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1437 FocusChangeMask | PropertyChangeMask | ExposureMask);
1438
1439 grabkeys();
1440 stack();
1441
1442 while (running) {
1443 XNextEvent(display, &e);
1444 if (handler[e.type])
1445 handler[e.type](&e);
1446 }
1447
1448 XCloseDisplay(display);
1449
1450 return (0);
1451 }