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