]> code.delx.au - spectrwm/blob - scrotwm.c
Off by one pixel in horizontal stack. Some spacing.
[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 fprintf(stderr, "h: %d mod: %d\n", h, height % 2);
720 }
721 if (i != 0 && hrw != 0) {
722 /* correct the last window for lost pixels */
723 if (win == TAILQ_LAST(&ws[current_ws].winlist,
724 ws_win_list)) {
725 w = width - (i * hrw);
726 if (w == 0)
727 w = hrw;
728 else
729 w += hrw;
730 x += hrw;
731 } else {
732 w = hrw - 2;
733 /* leave first bottom window at x = 0 */
734 if (i > 1)
735 x += w + 2;
736 }
737 }
738
739 if (win->transient != 0 || win->floating != 0)
740 floater = 1;
741 else
742 floater = 0;
743
744 bzero(&wc, sizeof wc);
745 wc.border_width = 1;
746 if (floater == 0) {
747 win->x = wc.x = x;
748 win->y = wc.y = y;
749 win->width = wc.width = w;
750 win->height = wc.height = h;
751 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
752 } else {
753 /* make sure we don't clobber the screen */
754 if (win->wa.width > width)
755 win->wa.width = width;
756 if (win->wa.height > height)
757 win->wa.width = height;
758 win->x = wc.x = (width - win->wa.width) / 2;
759 win->y = wc.y = (height - win->wa.height) / 2;
760 mask = CWX | CWY | CWBorderWidth;
761 }
762 XConfigureWindow(display, win->id, mask, &wc);
763
764 if (win == ws[current_ws].focus)
765 winfocus = win;
766 else
767 unfocus_win(win);
768 XMapRaised(display, win->id);
769 i++;
770 }
771
772 focus_win(winfocus); /* this has to be done outside of the loop */
773 }
774
775 /* fullscreen view */
776 void
777 max_init(void)
778 {
779 DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", current_ws);
780 }
781
782 void
783 max_stack(void) {
784 XWindowChanges wc;
785 struct ws_win wf, *win, *winfocus = &wf;
786 int i, h, w, x, y, winno;
787 unsigned int mask;
788
789 DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws);
790
791 winfocus->id = root;
792
793 winno = count_win(current_ws, 0);
794 if (winno == 0)
795 return;
796
797 x = 0;
798 y = bar_enabled ? bar_height : 0;
799 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
800 if (i == 1 && win->transient == 0 && win->floating != 0) {
801 h = height - 2;
802 w = width - 2;
803
804 winfocus = win;
805
806 bzero(&wc, sizeof wc);
807 wc.border_width = 1;
808 win->x = wc.x = x;
809 win->y = wc.y = y;
810 win->width = wc.width = w;
811 win->height = wc.height = h;
812 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
813 XConfigureWindow(display, win->id, mask, &wc);
814
815 XMapRaised(display, win->id);
816 } else {
817 /* hide all but the master window */
818 XUnmapWindow(display, win->id);
819 }
820 i++;
821 }
822
823 focus_win(winfocus); /* this has to be done outside of the loop */
824 }
825
826 void
827 send_to_ws(union arg *args)
828 {
829 int wsid = args->id;
830 struct ws_win *win = ws[current_ws].focus;
831
832 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
833
834 XUnmapWindow(display, win->id);
835
836 /* find a window to focus */
837 ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry);
838 if (ws[current_ws].focus == NULL)
839 ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist);
840 if (ws[current_ws].focus == win)
841 ws[current_ws].focus = NULL;
842
843 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
844
845 TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry);
846 if (count_win(wsid, 1) == 0)
847 ws[wsid].focus = win;
848 ws[wsid].restack = 1;
849
850 stack();
851 }
852
853 /* key definitions */
854 struct key {
855 unsigned int mod;
856 KeySym keysym;
857 void (*func)(union arg *);
858 union arg args;
859 } keys[] = {
860 /* modifier key function argument */
861 /* XXX alt-c is temporary, until I figure out how to grab spacebar */
862 { MODKEY, XK_c, cycle_layout, {0} },
863 { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
864 { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
865 { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
866 { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
867 { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
868 { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} },
869 { MODKEY, XK_p, spawn, {.argv = spawn_menu} },
870 { MODKEY | ShiftMask, XK_q, quit, {0} },
871 { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } },
872 { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
873 { MODKEY, XK_1, switchws, {.id = 0} },
874 { MODKEY, XK_2, switchws, {.id = 1} },
875 { MODKEY, XK_3, switchws, {.id = 2} },
876 { MODKEY, XK_4, switchws, {.id = 3} },
877 { MODKEY, XK_5, switchws, {.id = 4} },
878 { MODKEY, XK_6, switchws, {.id = 5} },
879 { MODKEY, XK_7, switchws, {.id = 6} },
880 { MODKEY, XK_8, switchws, {.id = 7} },
881 { MODKEY, XK_9, switchws, {.id = 8} },
882 { MODKEY, XK_0, switchws, {.id = 9} },
883 { MODKEY | ShiftMask, XK_1, send_to_ws, {.id = 0} },
884 { MODKEY | ShiftMask, XK_2, send_to_ws, {.id = 1} },
885 { MODKEY | ShiftMask, XK_3, send_to_ws, {.id = 2} },
886 { MODKEY | ShiftMask, XK_4, send_to_ws, {.id = 3} },
887 { MODKEY | ShiftMask, XK_5, send_to_ws, {.id = 4} },
888 { MODKEY | ShiftMask, XK_6, send_to_ws, {.id = 5} },
889 { MODKEY | ShiftMask, XK_7, send_to_ws, {.id = 6} },
890 { MODKEY | ShiftMask, XK_8, send_to_ws, {.id = 7} },
891 { MODKEY | ShiftMask, XK_9, send_to_ws, {.id = 8} },
892 { MODKEY | ShiftMask, XK_0, send_to_ws, {.id = 9} },
893 { MODKEY, XK_b, bar_toggle, {0} },
894 { MODKEY, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
895 { MODKEY | ShiftMask, XK_Tab, focus, {.id = SWM_ARG_ID_FOCUSPREV} },
896 };
897
898 void
899 updatenumlockmask(void)
900 {
901 unsigned int i, j;
902 XModifierKeymap *modmap;
903
904 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
905 numlockmask = 0;
906 modmap = XGetModifierMapping(display);
907 for (i = 0; i < 8; i++)
908 for (j = 0; j < modmap->max_keypermod; j++)
909 if (modmap->modifiermap[i * modmap->max_keypermod + j]
910 == XKeysymToKeycode(display, XK_Num_Lock))
911 numlockmask = (1 << i);
912
913 XFreeModifiermap(modmap);
914 }
915
916 void
917 grabkeys(void)
918 {
919 unsigned int i, j;
920 KeyCode code;
921 unsigned int modifiers[] =
922 { 0, LockMask, numlockmask, numlockmask | LockMask };
923
924 DNPRINTF(SWM_D_MISC, "grabkeys\n");
925 updatenumlockmask();
926
927 XUngrabKey(display, AnyKey, AnyModifier, root);
928 for(i = 0; i < LENGTH(keys); i++) {
929 if((code = XKeysymToKeycode(display, keys[i].keysym)))
930 for(j = 0; j < LENGTH(modifiers); j++)
931 XGrabKey(display, code,
932 keys[i].mod | modifiers[j], root,
933 True, GrabModeAsync, GrabModeAsync);
934 }
935 }
936 void
937 expose(XEvent *e)
938 {
939 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
940 }
941
942 void
943 keypress(XEvent *e)
944 {
945 unsigned int i;
946 KeySym keysym;
947 XKeyEvent *ev = &e->xkey;
948
949 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
950
951 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
952 for(i = 0; i < LENGTH(keys); i++)
953 if(keysym == keys[i].keysym
954 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
955 && keys[i].func)
956 keys[i].func(&(keys[i].args));
957 }
958
959 void
960 buttonpress(XEvent *e)
961 {
962 XButtonPressedEvent *ev = &e->xbutton;
963 #ifdef SWM_CLICKTOFOCUS
964 struct ws_win *win;
965 #endif
966
967
968 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
969
970 if (ev->window == root)
971 return;
972 if (ev->window == ws[current_ws].focus->id)
973 return;
974 #ifdef SWM_CLICKTOFOCUS
975 TAILQ_FOREACH(win, &ws[current_ws].winlist, entry)
976 if (win->id == ev->window) {
977 /* focus in the clicked window */
978 XSetWindowBorder(display, ev->window, 0xff0000);
979 XSetWindowBorder(display,
980 ws[current_ws].focus->id, 0x888888);
981 XSetInputFocus(display, ev->window, RevertToPointerRoot,
982 CurrentTime);
983 ws[current_ws].focus = win;
984 XSync(display, False);
985 break;
986 }
987 #endif
988 }
989
990 struct ws_win *
991 manage_window(Window id)
992 {
993 struct ws_win *win;
994
995 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
996 if (win->id == id)
997 return (win); /* already being managed */
998 }
999
1000 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
1001 errx(1, "calloc: failed to allocate memory for new window");
1002
1003 win->id = id;
1004 TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry);
1005
1006 XSelectInput(display, id, ButtonPressMask | EnterWindowMask |
1007 FocusChangeMask | ExposureMask);
1008
1009 return win;
1010 }
1011
1012
1013 void
1014 configurerequest(XEvent *e)
1015 {
1016 XConfigureRequestEvent *ev = &e->xconfigurerequest;
1017 Window trans;
1018 struct ws_win *win;
1019
1020 DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window);
1021
1022
1023 win = manage_window(ev->window);
1024 ws[current_ws].focus = win; /* make new win focused */
1025
1026 XGetTransientForHint(display, win->id, &trans);
1027 if (trans) {
1028 win->transient = trans;
1029 DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n",
1030 (unsigned)win->id, win->transient);
1031 }
1032 XGetWindowAttributes(display, win->id, &win->wa);
1033 #if 0
1034 XClassHint ch = { 0 };
1035 if(XGetClassHint(display, win->id, &ch)) {
1036 fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name);
1037 if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) {
1038 win->floating = 0;
1039 }
1040 if(ch.res_class)
1041 XFree(ch.res_class);
1042 if(ch.res_name)
1043 XFree(ch.res_name);
1044 }
1045 #endif
1046 stack();
1047 }
1048
1049 void
1050 configurenotify(XEvent *e)
1051 {
1052 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
1053 e->xconfigure.window);
1054 }
1055
1056 void
1057 destroynotify(XEvent *e)
1058 {
1059 struct ws_win *win;
1060 XDestroyWindowEvent *ev = &e->xdestroywindow;
1061
1062 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
1063
1064 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1065 if (ev->window == win->id) {
1066 /* find a window to focus */
1067 ws[current_ws].focus = TAILQ_PREV(win,
1068 ws_win_list, entry);
1069 if (ws[current_ws].focus == NULL)
1070 ws[current_ws].focus =
1071 TAILQ_FIRST(&ws[current_ws].winlist);
1072 if (win == ws[current_ws].focus)
1073 ws[current_ws].focus = NULL;
1074
1075 TAILQ_REMOVE(&ws[current_ws].winlist, win, entry);
1076 free(win);
1077 break;
1078 }
1079 }
1080
1081 stack();
1082 }
1083
1084 void
1085 enternotify(XEvent *e)
1086 {
1087 XCrossingEvent *ev = &e->xcrossing;
1088 struct ws_win *win;
1089
1090 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
1091
1092 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) &&
1093 ev->window != root)
1094 return;
1095 if (ignore_enter) {
1096 /* eat event(s) to prevent autofocus */
1097 ignore_enter--;
1098 return;
1099 }
1100 TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) {
1101 if (win->id == ev->window)
1102 focus_win(win);
1103 else
1104 unfocus_win(win);
1105 }
1106 }
1107
1108 void
1109 focusin(XEvent *e)
1110 {
1111 XFocusChangeEvent *ev = &e->xfocus;
1112
1113 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window);
1114
1115 XSync(display, False); /* not sure this helps redrawing graphic apps */
1116
1117 if (ev->window == root)
1118 return;
1119 /*
1120 * kill grab for now so that we can cut and paste , this screws up
1121 * click to focus
1122 */
1123 /*
1124 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1125 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1126 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1127 */
1128 }
1129
1130 void
1131 mappingnotify(XEvent *e)
1132 {
1133 XMappingEvent *ev = &e->xmapping;
1134
1135 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
1136
1137 XRefreshKeyboardMapping(ev);
1138 if(ev->request == MappingKeyboard)
1139 grabkeys();
1140 }
1141
1142 void
1143 maprequest(XEvent *e)
1144 {
1145 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
1146 e->xmaprequest.window);
1147
1148 manage_window(e->xmaprequest.window);
1149 stack();
1150 }
1151
1152 void
1153 propertynotify(XEvent *e)
1154 {
1155 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
1156 e->xproperty.window);
1157 }
1158
1159 void
1160 unmapnotify(XEvent *e)
1161 {
1162 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
1163 }
1164
1165 void
1166 visibilitynotify(XEvent *e)
1167 {
1168 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window);
1169
1170 if (e->xvisibility.window == bar_window &&
1171 e->xvisibility.state == VisibilityUnobscured)
1172 bar_print();
1173 }
1174
1175 void (*handler[LASTEvent])(XEvent *) = {
1176 [Expose] = expose,
1177 [KeyPress] = keypress,
1178 [ButtonPress] = buttonpress,
1179 [ConfigureRequest] = configurerequest,
1180 [ConfigureNotify] = configurenotify,
1181 [DestroyNotify] = destroynotify,
1182 [EnterNotify] = enternotify,
1183 [FocusIn] = focusin,
1184 [MappingNotify] = mappingnotify,
1185 [MapRequest] = maprequest,
1186 [PropertyNotify] = propertynotify,
1187 [UnmapNotify] = unmapnotify,
1188 [VisibilityNotify] = visibilitynotify,
1189 };
1190
1191 int
1192 xerror_start(Display *d, XErrorEvent *ee)
1193 {
1194 other_wm = 1;
1195 return (-1);
1196 }
1197
1198 int
1199 xerror(Display *d, XErrorEvent *ee)
1200 {
1201 fprintf(stderr, "error: %p %p\n", display, ee);
1202
1203 return (-1);
1204 }
1205
1206 int
1207 active_wm(void)
1208 {
1209 other_wm = 0;
1210 xerrorxlib = XSetErrorHandler(xerror_start);
1211
1212 /* this causes an error if some other window manager is running */
1213 XSelectInput(display, DefaultRootWindow(display),
1214 SubstructureRedirectMask);
1215 XSync(display, False);
1216 if(other_wm)
1217 return (1);
1218
1219 XSetErrorHandler(xerror);
1220 XSync(display, False);
1221 return (0);
1222 }
1223
1224 int
1225 main(int argc, char *argv[])
1226 {
1227 struct passwd *pwd;
1228 char conf[PATH_MAX], *cfile = NULL;
1229 struct stat sb;
1230 XEvent e;
1231 unsigned int i, num;
1232 Window d1, d2, *wins = NULL;
1233 XWindowAttributes wa;
1234
1235 fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION);
1236 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
1237 warnx("no locale support");
1238
1239 if(!(display = XOpenDisplay(0)))
1240 errx(1, "can not open display");
1241
1242 if (active_wm())
1243 errx(1, "other wm running");
1244
1245 screen = DefaultScreen(display);
1246 root = RootWindow(display, screen);
1247 width = DisplayWidth(display, screen) - 2;
1248 height = DisplayHeight(display, screen) - 2;
1249
1250 /* look for local and global conf file */
1251 pwd = getpwuid(getuid());
1252 if (pwd == NULL)
1253 errx(1, "invalid user %d", getuid());
1254
1255 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
1256 if (stat(conf, &sb) != -1) {
1257 if (S_ISREG(sb.st_mode))
1258 cfile = conf;
1259 } else {
1260 /* try global conf file */
1261 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
1262 if (!stat(conf, &sb))
1263 if (S_ISREG(sb.st_mode))
1264 cfile = conf;
1265 }
1266 if (cfile)
1267 conf_load(cfile);
1268
1269 /* init all workspaces */
1270 for (i = 0; i < SWM_WS_MAX; i++) {
1271 ws[i].visible = 0;
1272 ws[i].restack = 1;
1273 ws[i].focus = NULL;
1274 TAILQ_INIT(&ws[i].winlist);
1275 ws[i].cur_layout = &layouts[0];
1276 }
1277 /* make work space 1 active */
1278 ws[0].visible = 1;
1279
1280 /* grab existing windows */
1281 if (XQueryTree(display, root, &d1, &d2, &wins, &num)) {
1282 for (i = 0; i < num; i++) {
1283 if (!XGetWindowAttributes(display, wins[i], &wa)
1284 || wa.override_redirect ||
1285 XGetTransientForHint(display, wins[i], &d1))
1286 continue;
1287 manage_window(wins[i]);
1288 }
1289 if(wins)
1290 XFree(wins);
1291 }
1292 ws[0].focus = TAILQ_FIRST(&ws[0].winlist);
1293
1294 /* setup status bar */
1295 bar_setup();
1296
1297 XSelectInput(display, root, SubstructureRedirectMask |
1298 SubstructureNotifyMask | ButtonPressMask | KeyPressMask |
1299 EnterWindowMask | LeaveWindowMask | StructureNotifyMask |
1300 FocusChangeMask | PropertyChangeMask | ExposureMask);
1301
1302 grabkeys();
1303 stack();
1304
1305 while (running) {
1306 XNextEvent(display, &e);
1307 if (handler[e.type])
1308 handler[e.type](&e);
1309 }
1310
1311 XCloseDisplay(display);
1312
1313 return (0);
1314 }