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