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