]> code.delx.au - spectrwm/blob - scrotwm.c
Remove an unused var.
[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 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20 /*
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
23 *
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
33 *
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
40 *
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
43 *
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
51 */
52
53 static const char *cvstag = "$scrotwm$";
54
55 #define SWM_VERSION "0.9.7"
56
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <locale.h>
63 #include <unistd.h>
64 #include <time.h>
65 #include <signal.h>
66 #include <string.h>
67 #include <util.h>
68 #include <pwd.h>
69 #include <ctype.h>
70
71 #include <sys/types.h>
72 #include <sys/time.h>
73 #include <sys/stat.h>
74 #include <sys/wait.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
78
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
82 #include <X11/Xlib.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
86
87 #if RANDR_MAJOR < 1
88 # error XRandR versions less than 1.0 are not supported
89 #endif
90
91 #if RANDR_MAJOR >= 1
92 #if RANDR_MINOR >= 2
93 #define SWM_XRR_HAS_CRTC
94 #endif
95 #endif
96
97 /* #define SWM_DEBUG */
98 #ifdef SWM_DEBUG
99 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
100 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
101 #define SWM_D_MISC 0x0001
102 #define SWM_D_EVENT 0x0002
103 #define SWM_D_WS 0x0004
104 #define SWM_D_FOCUS 0x0008
105 #define SWM_D_MOVE 0x0010
106 #define SWM_D_STACK 0x0020
107 #define SWM_D_MOUSE 0x0040
108 #define SWM_D_PROP 0x0080
109 #define SWM_D_CLASS 0x0100
110 #define SWM_D_KEY 0x0200
111 #define SWM_D_QUIRK 0x0400
112 #define SWM_D_SPAWN 0x0800
113
114 u_int32_t swm_debug = 0
115 | SWM_D_MISC
116 | SWM_D_EVENT
117 | SWM_D_WS
118 | SWM_D_FOCUS
119 | SWM_D_MOVE
120 | SWM_D_STACK
121 | SWM_D_MOUSE
122 | SWM_D_PROP
123 | SWM_D_CLASS
124 | SWM_D_KEY
125 | SWM_D_QUIRK
126 | SWM_D_SPAWN
127 ;
128 #else
129 #define DPRINTF(x...)
130 #define DNPRINTF(n,x...)
131 #endif
132
133 #define LENGTH(x) (sizeof x / sizeof x[0])
134 #define MODKEY Mod1Mask
135 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
136 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
137 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
138 #define SWM_PROPLEN (16)
139 #define SWM_FUNCNAME_LEN (32)
140 #define SWM_KEYS_LEN (255)
141 #define SWM_QUIRK_LEN (64)
142 #define X(r) (r)->g.x
143 #define Y(r) (r)->g.y
144 #define WIDTH(r) (r)->g.w
145 #define HEIGHT(r) (r)->g.h
146 #define SWM_MAX_FONT_STEPS (3)
147
148 #ifndef SWM_LIB
149 #define SWM_LIB "/usr/local/lib/libswmhack.so.0.0"
150 #endif
151
152 char **start_argv;
153 Atom astate;
154 Atom aprot;
155 Atom adelete;
156 int (*xerrorxlib)(Display *, XErrorEvent *);
157 int other_wm;
158 int running = 1;
159 int ss_enabled = 0;
160 int xrandr_support;
161 int xrandr_eventbase;
162 int ignore_enter = 0;
163 unsigned int numlockmask = 0;
164 Display *display;
165
166 int cycle_empty = 0;
167 int cycle_visible = 0;
168 int term_width = 0;
169 int font_adjusted = 0;
170 unsigned int mod_key = MODKEY;
171
172 /* dialog windows */
173 double dialog_ratio = .6;
174 /* status bar */
175 #define SWM_BAR_MAX (256)
176 char *bar_argv[] = { NULL, NULL };
177 int bar_pipe[2];
178 char bar_ext[SWM_BAR_MAX];
179 char bar_vertext[SWM_BAR_MAX];
180 int bar_version = 0;
181 sig_atomic_t bar_alarm = 0;
182 int bar_delay = 30;
183 int bar_enabled = 1;
184 int bar_extra = 1;
185 int bar_extra_running = 0;
186 int bar_verbose = 1;
187 int bar_height = 0;
188 int clock_enabled = 1;
189 int title_name_enabled = 0;
190 int title_class_enabled = 0;
191 pid_t bar_pid;
192 GC bar_gc;
193 XGCValues bar_gcv;
194 int bar_fidx = 0;
195 XFontStruct *bar_fs;
196 char *bar_fonts[] = { NULL, NULL, NULL }; /* XXX Make fully dynamic */
197 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
198
199 #define SWM_MENU_FN (2)
200 #define SWM_MENU_NB (4)
201 #define SWM_MENU_NF (6)
202 #define SWM_MENU_SB (8)
203 #define SWM_MENU_SF (10)
204
205 /* layout manager data */
206 struct swm_geometry {
207 int x;
208 int y;
209 int w;
210 int h;
211 };
212
213 struct swm_screen;
214 struct workspace;
215
216 /* virtual "screens" */
217 struct swm_region {
218 TAILQ_ENTRY(swm_region) entry;
219 struct swm_geometry g;
220 struct workspace *ws; /* current workspace on this region */
221 struct swm_screen *s; /* screen idx */
222 Window bar_window;
223 };
224 TAILQ_HEAD(swm_region_list, swm_region);
225
226 struct ws_win {
227 TAILQ_ENTRY(ws_win) entry;
228 Window id;
229 struct swm_geometry g;
230 int got_focus;
231 int floating;
232 int transient;
233 int manual;
234 int font_size_boundary[SWM_MAX_FONT_STEPS];
235 int font_steps;
236 int last_inc;
237 int can_delete;
238 unsigned long quirks;
239 struct workspace *ws; /* always valid */
240 struct swm_screen *s; /* always valid, never changes */
241 XWindowAttributes wa;
242 XSizeHints sh;
243 XClassHint ch;
244 };
245 TAILQ_HEAD(ws_win_list, ws_win);
246
247 /* user/key callable function IDs */
248 enum keyfuncid {
249 kf_cycle_layout,
250 kf_stack_reset,
251 kf_master_shrink,
252 kf_master_grow,
253 kf_master_add,
254 kf_master_del,
255 kf_stack_inc,
256 kf_stack_dec,
257 kf_swap_main,
258 kf_focus_next,
259 kf_focus_prev,
260 kf_swap_next,
261 kf_swap_prev,
262 kf_spawn_term,
263 kf_spawn_menu,
264 kf_quit,
265 kf_restart,
266 kf_focus_main,
267 kf_ws_1,
268 kf_ws_2,
269 kf_ws_3,
270 kf_ws_4,
271 kf_ws_5,
272 kf_ws_6,
273 kf_ws_7,
274 kf_ws_8,
275 kf_ws_9,
276 kf_ws_10,
277 kf_ws_next,
278 kf_ws_prev,
279 kf_screen_next,
280 kf_screen_prev,
281 kf_mvws_1,
282 kf_mvws_2,
283 kf_mvws_3,
284 kf_mvws_4,
285 kf_mvws_5,
286 kf_mvws_6,
287 kf_mvws_7,
288 kf_mvws_8,
289 kf_mvws_9,
290 kf_mvws_10,
291 kf_bar_toggle,
292 kf_wind_kill,
293 kf_wind_del,
294 kf_screenshot_all,
295 kf_screenshot_wind,
296 kf_float_toggle,
297 kf_version,
298 kf_spawn_lock,
299 kf_spawn_initscr,
300 kf_spawn_custom,
301 kf_invalid
302 };
303
304 /* layout handlers */
305 void stack(void);
306 void vertical_config(struct workspace *, int);
307 void vertical_stack(struct workspace *, struct swm_geometry *);
308 void horizontal_config(struct workspace *, int);
309 void horizontal_stack(struct workspace *, struct swm_geometry *);
310 void max_stack(struct workspace *, struct swm_geometry *);
311
312 void grabbuttons(struct ws_win *, int);
313 void new_region(struct swm_screen *, int, int, int, int);
314
315 struct layout {
316 void (*l_stack)(struct workspace *, struct swm_geometry *);
317 void (*l_config)(struct workspace *, int);
318 } layouts[] = {
319 /* stack, configure */
320 { vertical_stack, vertical_config},
321 { horizontal_stack, horizontal_config},
322 { max_stack, NULL},
323 { NULL, NULL},
324 };
325
326 #define SWM_H_SLICE (32)
327 #define SWM_V_SLICE (32)
328
329 /* define work spaces */
330 struct workspace {
331 int idx; /* workspace index */
332 int restack; /* restack on switch */
333 struct layout *cur_layout; /* current layout handlers */
334 struct ws_win *focus; /* may be NULL */
335 struct ws_win *focus_prev; /* may be NULL */
336 struct swm_region *r; /* may be NULL */
337 struct ws_win_list winlist; /* list of windows in ws */
338
339 /* stacker state */
340 struct {
341 int horizontal_msize;
342 int horizontal_mwin;
343 int horizontal_stacks;
344 int vertical_msize;
345 int vertical_mwin;
346 int vertical_stacks;
347 } l_state;
348 };
349
350 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
351 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
352
353 /* physical screen mapping */
354 #define SWM_WS_MAX (10) /* XXX Too small? */
355 struct swm_screen {
356 int idx; /* screen index */
357 struct swm_region_list rl; /* list of regions on this screen */
358 struct swm_region_list orl; /* list of old regions */
359 Window root;
360 struct workspace ws[SWM_WS_MAX];
361
362 /* colors */
363 struct {
364 unsigned long color;
365 char *name;
366 } c[SWM_S_COLOR_MAX];
367 };
368 struct swm_screen *screens;
369 int num_screens;
370
371 struct ws_win *cur_focus = NULL;
372
373 /* args to functions */
374 union arg {
375 int id;
376 #define SWM_ARG_ID_FOCUSNEXT (0)
377 #define SWM_ARG_ID_FOCUSPREV (1)
378 #define SWM_ARG_ID_FOCUSMAIN (2)
379 #define SWM_ARG_ID_SWAPNEXT (3)
380 #define SWM_ARG_ID_SWAPPREV (4)
381 #define SWM_ARG_ID_SWAPMAIN (5)
382 #define SWM_ARG_ID_MASTERSHRINK (6)
383 #define SWM_ARG_ID_MASTERGROW (7)
384 #define SWM_ARG_ID_MASTERADD (8)
385 #define SWM_ARG_ID_MASTERDEL (9)
386 #define SWM_ARG_ID_STACKRESET (10)
387 #define SWM_ARG_ID_STACKINIT (11)
388 #define SWM_ARG_ID_CYCLEWS_UP (12)
389 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
390 #define SWM_ARG_ID_CYCLESC_UP (14)
391 #define SWM_ARG_ID_CYCLESC_DOWN (15)
392 #define SWM_ARG_ID_STACKINC (16)
393 #define SWM_ARG_ID_STACKDEC (17)
394 #define SWM_ARG_ID_SS_ALL (0)
395 #define SWM_ARG_ID_SS_WINDOW (1)
396 #define SWM_ARG_ID_DONTCENTER (0)
397 #define SWM_ARG_ID_CENTER (1)
398 #define SWM_ARG_ID_KILLWINDOW (0)
399 #define SWM_ARG_ID_DELETEWINDOW (1)
400 char **argv;
401 };
402
403 /* quirks */
404 struct quirk {
405 char *class;
406 char *name;
407 unsigned long quirk;
408 #define SWM_Q_FLOAT (1<<0) /* float this window */
409 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
410 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
411 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
412 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
413 };
414 int quirks_size = 0, quirks_length = 0;
415 struct quirk *quirks = NULL;
416
417 /* events */
418 void expose(XEvent *);
419 void keypress(XEvent *);
420 void buttonpress(XEvent *);
421 void configurerequest(XEvent *);
422 void configurenotify(XEvent *);
423 void destroynotify(XEvent *);
424 void enternotify(XEvent *);
425 void focusin(XEvent *);
426 void focusout(XEvent *);
427 void mappingnotify(XEvent *);
428 void maprequest(XEvent *);
429 void propertynotify(XEvent *);
430 void unmapnotify(XEvent *);
431 void visibilitynotify(XEvent *);
432
433 void (*handler[LASTEvent])(XEvent *) = {
434 [Expose] = expose,
435 [KeyPress] = keypress,
436 [ButtonPress] = buttonpress,
437 [ConfigureRequest] = configurerequest,
438 [ConfigureNotify] = configurenotify,
439 [DestroyNotify] = destroynotify,
440 [EnterNotify] = enternotify,
441 [FocusIn] = focusin,
442 [FocusOut] = focusout,
443 [MappingNotify] = mappingnotify,
444 [MapRequest] = maprequest,
445 [PropertyNotify] = propertynotify,
446 [UnmapNotify] = unmapnotify,
447 [VisibilityNotify] = visibilitynotify,
448 };
449
450 unsigned long
451 name_to_color(char *colorname)
452 {
453 Colormap cmap;
454 Status status;
455 XColor screen_def, exact_def;
456 unsigned long result = 0;
457 char cname[32] = "#";
458
459 cmap = DefaultColormap(display, screens[0].idx);
460 status = XAllocNamedColor(display, cmap, colorname,
461 &screen_def, &exact_def);
462 if (!status) {
463 strlcat(cname, colorname + 2, sizeof cname - 1);
464 status = XAllocNamedColor(display, cmap, cname, &screen_def,
465 &exact_def);
466 }
467 if (status)
468 result = screen_def.pixel;
469 else
470 fprintf(stderr, "color '%s' not found.\n", colorname);
471
472 return (result);
473 }
474
475 void
476 setscreencolor(char *val, int i, int c)
477 {
478 if (i > 0 && i <= ScreenCount(display)) {
479 screens[i - 1].c[c].color = name_to_color(val);
480 free(screens[i - 1].c[c].name);
481 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
482 errx(1, "strdup");
483 } else if (i == -1) {
484 for (i = 0; i < ScreenCount(display); i++) {
485 screens[i].c[c].color = name_to_color(val);
486 free(screens[i].c[c].name);
487 if ((screens[i].c[c].name = strdup(val)) == NULL)
488 errx(1, "strdup");
489 }
490 } else
491 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
492 i, ScreenCount(display));
493 }
494
495 void
496 custom_region(char *val)
497 {
498 unsigned int sidx, x, y, w, h;
499
500 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
501 errx(1, "invalid custom region, "
502 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
503 if (sidx < 1 || sidx > ScreenCount(display))
504 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
505 sidx, ScreenCount(display));
506 sidx--;
507
508 if (w < 1 || h < 1)
509 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
510
511 if (x < 0 || x > DisplayWidth(display, sidx) ||
512 y < 0 || y > DisplayHeight(display, sidx) ||
513 w + x > DisplayWidth(display, sidx) ||
514 h + y > DisplayHeight(display, sidx))
515 errx(1, "region %ux%u+%u+%u not within screen boundaries "
516 "(%ux%u)\n", w, h, x, y,
517 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
518
519 new_region(&screens[sidx], x, y, w, h);
520 }
521
522 void
523 socket_setnonblock(int fd)
524 {
525 int flags;
526
527 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
528 err(1, "fcntl F_GETFL");
529 flags |= O_NONBLOCK;
530 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
531 err(1, "fcntl F_SETFL");
532 }
533
534 void
535 bar_print(struct swm_region *r, char *s)
536 {
537 XClearWindow(display, r->bar_window);
538 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
539 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
540 strlen(s));
541 }
542
543 void
544 bar_extra_stop(void)
545 {
546 if (bar_pipe[0]) {
547 close(bar_pipe[0]);
548 bzero(bar_pipe, sizeof bar_pipe);
549 }
550 if (bar_pid) {
551 kill(bar_pid, SIGTERM);
552 bar_pid = 0;
553 }
554 strlcpy(bar_ext, "", sizeof bar_ext);
555 bar_extra = 0;
556 }
557
558 void
559 bar_update(void)
560 {
561 time_t tmt;
562 struct tm tm;
563 struct swm_region *r;
564 int i, x, do_class, do_name;
565 size_t len;
566 char s[SWM_BAR_MAX];
567 char loc[SWM_BAR_MAX];
568 char *b;
569 XClassHint *xch;
570 Status status;
571
572 if (bar_enabled == 0)
573 return;
574 if (bar_extra && bar_extra_running) {
575 /* ignore short reads; it'll correct itself */
576 while ((b = fgetln(stdin, &len)) != NULL)
577 if (b && b[len - 1] == '\n') {
578 b[len - 1] = '\0';
579 strlcpy(bar_ext, b, sizeof bar_ext);
580 }
581 if (b == NULL && errno != EAGAIN) {
582 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
583 errno, strerror(errno));
584 bar_extra_stop();
585 }
586 } else
587 strlcpy(bar_ext, "", sizeof bar_ext);
588
589 if (clock_enabled == 0)
590 strlcpy(s, "", sizeof s);
591 else {
592 time(&tmt);
593 localtime_r(&tmt, &tm);
594 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
595 }
596 xch = NULL;
597 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
598 cur_focus != NULL) {
599 if ((xch = XAllocClassHint()) == NULL)
600 goto out;
601 status = XGetClassHint(display, cur_focus->id, xch);
602 if (status == BadWindow || status == BadAlloc)
603 goto out;
604 do_class = (title_class_enabled && xch->res_class != NULL);
605 do_name = (title_name_enabled && xch->res_name != NULL);
606 if (do_class)
607 strlcat(s, xch->res_class, sizeof s);
608 if (do_class && do_name)
609 strlcat(s, ":", sizeof s);
610 if (do_name)
611 strlcat(s, xch->res_name, sizeof s);
612 }
613 out:
614 if (xch)
615 XFree(xch);
616 for (i = 0; i < ScreenCount(display); i++) {
617 x = 1;
618 TAILQ_FOREACH(r, &screens[i].rl, entry) {
619 snprintf(loc, sizeof loc, "%d:%d %s %s %s",
620 x++, r->ws->idx + 1, s, bar_ext, bar_vertext);
621 bar_print(r, loc);
622 }
623 }
624 XSync(display, False);
625 alarm(bar_delay);
626 }
627
628 void
629 bar_signal(int sig)
630 {
631 bar_alarm = 1;
632 }
633
634 void
635 bar_toggle(struct swm_region *r, union arg *args)
636 {
637 struct swm_region *tmpr;
638 int i, j, sc = ScreenCount(display);
639
640 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
641
642 if (bar_enabled)
643 for (i = 0; i < sc; i++)
644 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
645 XUnmapWindow(display, tmpr->bar_window);
646 else
647 for (i = 0; i < sc; i++)
648 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
649 XMapRaised(display, tmpr->bar_window);
650
651 bar_enabled = !bar_enabled;
652 for (i = 0; i < sc; i++)
653 for (j = 0; j < SWM_WS_MAX; j++)
654 screens[i].ws[j].restack = 1;
655
656 stack();
657 /* must be after stack */
658 bar_update();
659 }
660
661 void
662 bar_refresh(void)
663 {
664 XSetWindowAttributes wa;
665 struct swm_region *r;
666 int i;
667
668 /* do this here because the conf file is in memory */
669 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
670 /* launch external status app */
671 bar_extra_running = 1;
672 if (pipe(bar_pipe) == -1)
673 err(1, "pipe error");
674 socket_setnonblock(bar_pipe[0]);
675 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
676 if (dup2(bar_pipe[0], 0) == -1)
677 errx(1, "dup2");
678 if (dup2(bar_pipe[1], 1) == -1)
679 errx(1, "dup2");
680 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
681 err(1, "could not disable SIGPIPE");
682 switch (bar_pid = fork()) {
683 case -1:
684 err(1, "cannot fork");
685 break;
686 case 0: /* child */
687 close(bar_pipe[0]);
688 execvp(bar_argv[0], bar_argv);
689 err(1, "%s external app failed", bar_argv[0]);
690 break;
691 default: /* parent */
692 close(bar_pipe[1]);
693 break;
694 }
695 }
696
697 bzero(&wa, sizeof wa);
698 for (i = 0; i < ScreenCount(display); i++)
699 TAILQ_FOREACH(r, &screens[i].rl, entry) {
700 wa.border_pixel =
701 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
702 wa.background_pixel =
703 screens[i].c[SWM_S_COLOR_BAR].color;
704 XChangeWindowAttributes(display, r->bar_window,
705 CWBackPixel | CWBorderPixel, &wa);
706 }
707 bar_update();
708 }
709
710 void
711 bar_setup(struct swm_region *r)
712 {
713 int i;
714
715 for (i = 0; bar_fonts[i] != NULL; i++) {
716 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
717 if (bar_fs) {
718 bar_fidx = i;
719 break;
720 }
721 }
722 if (bar_fonts[i] == NULL)
723 errx(1, "couldn't load font");
724 bar_height = bar_fs->ascent + bar_fs->descent + 3;
725
726 r->bar_window = XCreateSimpleWindow(display,
727 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
728 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
729 r->s->c[SWM_S_COLOR_BAR].color);
730 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
731 XSetFont(display, bar_gc, bar_fs->fid);
732 XSelectInput(display, r->bar_window, VisibilityChangeMask);
733 if (bar_enabled)
734 XMapRaised(display, r->bar_window);
735 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
736
737 if (signal(SIGALRM, bar_signal) == SIG_ERR)
738 err(1, "could not install bar_signal");
739 bar_refresh();
740 }
741
742 void
743 version(struct swm_region *r, union arg *args)
744 {
745 bar_version = !bar_version;
746 if (bar_version)
747 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
748 SWM_VERSION, cvstag);
749 else
750 strlcpy(bar_vertext, "", sizeof bar_vertext);
751 bar_update();
752 }
753
754 void
755 client_msg(struct ws_win *win, Atom a)
756 {
757 XClientMessageEvent cm;
758
759 bzero(&cm, sizeof cm);
760 cm.type = ClientMessage;
761 cm.window = win->id;
762 cm.message_type = aprot;
763 cm.format = 32;
764 cm.data.l[0] = a;
765 cm.data.l[1] = CurrentTime;
766 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
767 }
768
769 void
770 config_win(struct ws_win *win)
771 {
772 XConfigureEvent ce;
773
774 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
775 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
776 ce.type = ConfigureNotify;
777 ce.display = display;
778 ce.event = win->id;
779 ce.window = win->id;
780 ce.x = win->g.x;
781 ce.y = win->g.y;
782 ce.width = win->g.w;
783 ce.height = win->g.h;
784 ce.border_width = 1; /* XXX store this! */
785 ce.above = None;
786 ce.override_redirect = False;
787 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
788 }
789
790 int
791 count_win(struct workspace *ws, int count_transient)
792 {
793 struct ws_win *win;
794 int count = 0;
795
796 TAILQ_FOREACH(win, &ws->winlist, entry) {
797 if (count_transient == 0 && win->floating)
798 continue;
799 if (count_transient == 0 && win->transient)
800 continue;
801 count++;
802 }
803 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
804
805 return (count);
806 }
807
808 void
809 quit(struct swm_region *r, union arg *args)
810 {
811 DNPRINTF(SWM_D_MISC, "quit\n");
812 running = 0;
813 }
814
815 void
816 unmap_all(void)
817 {
818 struct ws_win *win;
819 int i, j;
820
821 for (i = 0; i < ScreenCount(display); i++)
822 for (j = 0; j < SWM_WS_MAX; j++)
823 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
824 XUnmapWindow(display, win->id);
825 }
826
827 void
828 fake_keypress(struct ws_win *win, int keysym, int modifiers)
829 {
830 XKeyEvent event;
831
832 event.display = display; /* Ignored, but what the hell */
833 event.window = win->id;
834 event.root = win->s->root;
835 event.subwindow = None;
836 event.time = CurrentTime;
837 event.x = win->g.x;
838 event.y = win->g.y;
839 event.x_root = 1;
840 event.y_root = 1;
841 event.same_screen = True;
842 event.keycode = XKeysymToKeycode(display, keysym);
843 event.state = modifiers;
844
845 event.type = KeyPress;
846 XSendEvent(event.display, event.window, True,
847 KeyPressMask, (XEvent *)&event);
848
849 event.type = KeyRelease;
850 XSendEvent(event.display, event.window, True,
851 KeyPressMask, (XEvent *)&event);
852
853 }
854
855 void
856 restart(struct swm_region *r, union arg *args)
857 {
858 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
859
860 /* disable alarm because the following code may not be interrupted */
861 alarm(0);
862 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
863 errx(1, "can't disable alarm");
864
865 bar_extra_stop();
866 bar_extra = 1;
867 unmap_all();
868 XCloseDisplay(display);
869 execvp(start_argv[0], start_argv);
870 fprintf(stderr, "execvp failed\n");
871 perror(" failed");
872 quit(NULL, NULL);
873 }
874
875 struct swm_region *
876 root_to_region(Window root)
877 {
878 struct swm_region *r = NULL;
879 Window rr, cr;
880 int i, x, y, wx, wy;
881 unsigned int mask;
882
883 for (i = 0; i < ScreenCount(display); i++)
884 if (screens[i].root == root)
885 break;
886
887 if (XQueryPointer(display, screens[i].root,
888 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
889 /* choose a region based on pointer location */
890 TAILQ_FOREACH(r, &screens[i].rl, entry)
891 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
892 y >= Y(r) && y <= Y(r) + HEIGHT(r))
893 break;
894 }
895
896 if (r == NULL)
897 r = TAILQ_FIRST(&screens[i].rl);
898
899 return (r);
900 }
901
902 struct ws_win *
903 find_window(Window id)
904 {
905 struct ws_win *win;
906 int i, j;
907
908 for (i = 0; i < ScreenCount(display); i++)
909 for (j = 0; j < SWM_WS_MAX; j++)
910 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
911 if (id == win->id)
912 return (win);
913 return (NULL);
914 }
915
916 void
917 spawn(struct swm_region *r, union arg *args)
918 {
919 char *ret;
920 int si;
921
922 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
923 /*
924 * The double-fork construct avoids zombie processes and keeps the code
925 * clean from stupid signal handlers.
926 */
927 if (fork() == 0) {
928 if (fork() == 0) {
929 if (display)
930 close(ConnectionNumber(display));
931 setenv("LD_PRELOAD", SWM_LIB, 1);
932 if (asprintf(&ret, "%d", r->ws->idx)) {
933 setenv("_SWM_WS", ret, 1);
934 free(ret);
935 }
936 if (asprintf(&ret, "%d", getpid())) {
937 setenv("_SWM_PID", ret, 1);
938 free(ret);
939 }
940 setsid();
941 /* kill stdin, mplayer, ssh-add etc. need that */
942 si = open("/dev/null", O_RDONLY, 0);
943 if (si == -1)
944 err(1, "open /dev/null");
945 if (dup2(si, 0) == -1)
946 err(1, "dup2 /dev/null");
947 execvp(args->argv[0], args->argv);
948 fprintf(stderr, "execvp failed\n");
949 perror(" failed");
950 }
951 exit(0);
952 }
953 wait(0);
954 }
955
956 void
957 spawnterm(struct swm_region *r, union arg *args)
958 {
959 DNPRINTF(SWM_D_MISC, "spawnterm\n");
960
961 if (term_width)
962 setenv("_SWM_XTERM_FONTADJ", "", 1);
963 spawn(r, args);
964 }
965
966 void
967 unfocus_win(struct ws_win *win)
968 {
969 if (win == NULL)
970 return;
971
972 if (win->ws->focus != win && win->ws->focus != NULL)
973 win->ws->focus_prev = win->ws->focus;
974
975 if (win->ws->r == NULL)
976 return;
977
978 grabbuttons(win, 0);
979 XSetWindowBorder(display, win->id,
980 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
981 win->got_focus = 0;
982 if (win->ws->focus == win)
983 win->ws->focus = NULL;
984 if (cur_focus == win)
985 cur_focus = NULL;
986 }
987
988 void
989 unfocus_all(void)
990 {
991 struct ws_win *win;
992 int i, j;
993
994 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
995
996 for (i = 0; i < ScreenCount(display); i++)
997 for (j = 0; j < SWM_WS_MAX; j++)
998 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
999 unfocus_win(win);
1000 }
1001
1002 void
1003 focus_win(struct ws_win *win)
1004 {
1005 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1006
1007 if (win == NULL)
1008 return;
1009
1010 if (cur_focus)
1011 unfocus_win(cur_focus);
1012 if (win->ws->focus) {
1013 /* probably shouldn't happen due to the previous unfocus_win */
1014 DNPRINTF(SWM_D_FOCUS, "unfocusing win->ws->focus: %lu\n",
1015 win->ws->focus->id);
1016 unfocus_win(win->ws->focus);
1017 }
1018 win->ws->focus = win;
1019 if (win->ws->r != NULL) {
1020 cur_focus = win;
1021 if (!win->got_focus) {
1022 XSetWindowBorder(display, win->id,
1023 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1024 grabbuttons(win, 1);
1025 }
1026 win->got_focus = 1;
1027 XSetInputFocus(display, win->id,
1028 RevertToPointerRoot, CurrentTime);
1029 }
1030 }
1031
1032 void
1033 switchws(struct swm_region *r, union arg *args)
1034 {
1035 int wsid = args->id;
1036 struct swm_region *this_r, *other_r;
1037 struct ws_win *win;
1038 struct workspace *new_ws, *old_ws;
1039
1040 this_r = r;
1041 old_ws = this_r->ws;
1042 new_ws = &this_r->s->ws[wsid];
1043
1044 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1045 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1046 old_ws->idx, wsid);
1047
1048 if (new_ws == old_ws)
1049 return;
1050
1051 other_r = new_ws->r;
1052 if (!other_r) {
1053 /* if the other workspace is hidden, switch windows */
1054 /* map new window first to prevent ugly blinking */
1055 old_ws->r = NULL;
1056 old_ws->restack = 1;
1057
1058 TAILQ_FOREACH(win, &new_ws->winlist, entry)
1059 XMapRaised(display, win->id);
1060
1061 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1062 XUnmapWindow(display, win->id);
1063 } else {
1064 other_r->ws = old_ws;
1065 old_ws->r = other_r;
1066 }
1067 this_r->ws = new_ws;
1068 new_ws->r = this_r;
1069
1070 ignore_enter = 1;
1071 /* set focus */
1072 if (new_ws->focus == NULL)
1073 new_ws->focus = TAILQ_FIRST(&new_ws->winlist);
1074 if (new_ws->focus)
1075 focus_win(new_ws->focus);
1076 stack();
1077 bar_update();
1078 }
1079
1080 void
1081 cyclews(struct swm_region *r, union arg *args)
1082 {
1083 union arg a;
1084 struct swm_screen *s = r->s;
1085
1086 DNPRINTF(SWM_D_WS, "cyclews id %d "
1087 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1088 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1089
1090 a.id = r->ws->idx;
1091 do {
1092 switch (args->id) {
1093 case SWM_ARG_ID_CYCLEWS_UP:
1094 if (a.id < SWM_WS_MAX - 1)
1095 a.id++;
1096 else
1097 a.id = 0;
1098 break;
1099 case SWM_ARG_ID_CYCLEWS_DOWN:
1100 if (a.id > 0)
1101 a.id--;
1102 else
1103 a.id = SWM_WS_MAX - 1;
1104 break;
1105 default:
1106 return;
1107 };
1108
1109 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1110 continue;
1111 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1112 continue;
1113
1114 switchws(r, &a);
1115 } while (a.id != r->ws->idx);
1116 }
1117
1118 void
1119 cyclescr(struct swm_region *r, union arg *args)
1120 {
1121 struct swm_region *rr;
1122 int i;
1123
1124 i = r->s->idx;
1125 switch (args->id) {
1126 case SWM_ARG_ID_CYCLESC_UP:
1127 rr = TAILQ_NEXT(r, entry);
1128 if (rr == NULL)
1129 rr = TAILQ_FIRST(&screens[i].rl);
1130 break;
1131 case SWM_ARG_ID_CYCLESC_DOWN:
1132 rr = TAILQ_PREV(r, swm_region_list, entry);
1133 if (rr == NULL)
1134 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1135 break;
1136 default:
1137 return;
1138 };
1139 unfocus_all();
1140 XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);
1141 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, rr->g.x,
1142 rr->g.y + bar_enabled ? bar_height : 0);
1143 }
1144
1145 void
1146 swapwin(struct swm_region *r, union arg *args)
1147 {
1148 struct ws_win *target, *source;
1149 struct ws_win_list *wl;
1150
1151
1152 DNPRINTF(SWM_D_WS, "swapwin id %d "
1153 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1154 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1155 if (cur_focus == NULL)
1156 return;
1157
1158 source = cur_focus;
1159 wl = &source->ws->winlist;
1160
1161 switch (args->id) {
1162 case SWM_ARG_ID_SWAPPREV:
1163 target = TAILQ_PREV(source, ws_win_list, entry);
1164 TAILQ_REMOVE(wl, cur_focus, entry);
1165 if (target == NULL)
1166 TAILQ_INSERT_TAIL(wl, source, entry);
1167 else
1168 TAILQ_INSERT_BEFORE(target, source, entry);
1169 break;
1170 case SWM_ARG_ID_SWAPNEXT:
1171 target = TAILQ_NEXT(source, entry);
1172 TAILQ_REMOVE(wl, source, entry);
1173 if (target == NULL)
1174 TAILQ_INSERT_HEAD(wl, source, entry);
1175 else
1176 TAILQ_INSERT_AFTER(wl, target, source, entry);
1177 break;
1178 case SWM_ARG_ID_SWAPMAIN:
1179 target = TAILQ_FIRST(wl);
1180 if (target == source) {
1181 if (source->ws->focus_prev != NULL &&
1182 source->ws->focus_prev != target)
1183
1184 source = source->ws->focus_prev;
1185 else
1186 return;
1187 }
1188 source->ws->focus_prev = target;
1189 TAILQ_REMOVE(wl, target, entry);
1190 TAILQ_INSERT_BEFORE(source, target, entry);
1191 TAILQ_REMOVE(wl, source, entry);
1192 TAILQ_INSERT_HEAD(wl, source, entry);
1193 break;
1194 default:
1195 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1196 return;
1197 }
1198
1199 ignore_enter = 1;
1200 stack();
1201 }
1202
1203 void
1204 focus(struct swm_region *r, union arg *args)
1205 {
1206 struct ws_win *winfocus, *winlostfocus;
1207 struct ws_win_list *wl;
1208
1209 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1210 if (cur_focus == NULL)
1211 return;
1212
1213 wl = &cur_focus->ws->winlist;
1214
1215 winlostfocus = cur_focus;
1216
1217 switch (args->id) {
1218 case SWM_ARG_ID_FOCUSPREV:
1219 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1220 if (winfocus == NULL)
1221 winfocus = TAILQ_LAST(wl, ws_win_list);
1222 break;
1223
1224 case SWM_ARG_ID_FOCUSNEXT:
1225 winfocus = TAILQ_NEXT(cur_focus, entry);
1226 if (winfocus == NULL)
1227 winfocus = TAILQ_FIRST(wl);
1228 break;
1229
1230 case SWM_ARG_ID_FOCUSMAIN:
1231 winfocus = TAILQ_FIRST(wl);
1232 if (winfocus == cur_focus)
1233 winfocus = cur_focus->ws->focus_prev;
1234 if (winfocus == NULL)
1235 return;
1236 break;
1237
1238 default:
1239 return;
1240 }
1241
1242 if (winfocus == winlostfocus || winfocus == NULL)
1243 return;
1244
1245 XMapRaised(display, winfocus->id);
1246 focus_win(winfocus);
1247 XSync(display, False);
1248 }
1249
1250 void
1251 cycle_layout(struct swm_region *r, union arg *args)
1252 {
1253 struct workspace *ws = r->ws;
1254
1255 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1256
1257 ws->cur_layout++;
1258 if (ws->cur_layout->l_stack == NULL)
1259 ws->cur_layout = &layouts[0];
1260 ignore_enter = 1;
1261 stack();
1262 }
1263
1264 void
1265 stack_config(struct swm_region *r, union arg *args)
1266 {
1267 struct workspace *ws = r->ws;
1268
1269 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1270 args->id, ws->idx);
1271
1272 if (ws->cur_layout->l_config != NULL)
1273 ws->cur_layout->l_config(ws, args->id);
1274
1275 if (args->id != SWM_ARG_ID_STACKINIT);
1276 stack();
1277 }
1278
1279 void
1280 stack(void) {
1281 struct swm_geometry g;
1282 struct swm_region *r;
1283 int i, j;
1284
1285 DNPRINTF(SWM_D_STACK, "stack\n");
1286
1287 for (i = 0; i < ScreenCount(display); i++) {
1288 j = 0;
1289 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1290 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1291 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1292
1293 /* start with screen geometry, adjust for bar */
1294 g = r->g;
1295 g.w -= 2;
1296 g.h -= 2;
1297 if (bar_enabled) {
1298 g.y += bar_height;
1299 g.h -= bar_height;
1300 }
1301
1302 r->ws->restack = 0;
1303 r->ws->cur_layout->l_stack(r->ws, &g);
1304 }
1305 }
1306 if (font_adjusted)
1307 font_adjusted--;
1308 XSync(display, False);
1309 }
1310
1311 void
1312 stack_floater(struct ws_win *win, struct swm_region *r)
1313 {
1314 unsigned int mask;
1315 XWindowChanges wc;
1316
1317 bzero(&wc, sizeof wc);
1318 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1319 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w == WIDTH(r)) &&
1320 (win->g.h == HEIGHT(r)))
1321 wc.border_width = 0;
1322 else
1323 wc.border_width = 1;
1324 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1325 win->g.w = (double)WIDTH(r) * dialog_ratio;
1326 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1327 }
1328 wc.width = win->g.w;
1329 wc.height = win->g.h;
1330 if (win->manual) {
1331 wc.x = win->g.x;
1332 wc.y = win->g.y;
1333 } else {
1334 wc.x = (WIDTH(r) - win->g.w) / 2;
1335 wc.y = (HEIGHT(r) - win->g.h) / 2;
1336 }
1337
1338 DNPRINTF(SWM_D_STACK, "stack_floater: win %lu x %d y %d w %d h %d\n",
1339 win->id, wc.x, wc.y, wc.width, wc.height);
1340
1341 XConfigureWindow(display, win->id, mask, &wc);
1342 }
1343
1344 /*
1345 * Send keystrokes to terminal to decrease/increase the font size as the
1346 * window size changes.
1347 */
1348 void
1349 adjust_font(struct ws_win *win)
1350 {
1351 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1352 win->floating || win->transient)
1353 return;
1354
1355 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1356 win->g.w / win->sh.width_inc < term_width &&
1357 win->font_steps < SWM_MAX_FONT_STEPS) {
1358 win->font_size_boundary[win->font_steps] =
1359 (win->sh.width_inc * term_width) + win->sh.base_width;
1360 win->font_steps++;
1361 font_adjusted++;
1362 win->last_inc = win->sh.width_inc;
1363 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1364 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1365 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1366 win->font_steps--;
1367 font_adjusted++;
1368 win->last_inc = win->sh.width_inc;
1369 fake_keypress(win, XK_KP_Add, ShiftMask);
1370 }
1371 }
1372
1373 #define SWAPXY(g) do { \
1374 int tmp; \
1375 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1376 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1377 } while (0)
1378 void
1379 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1380 {
1381 XWindowChanges wc;
1382 struct swm_geometry win_g, r_g = *g;
1383 struct ws_win *win, *winfocus;
1384 int i, j, s, stacks;
1385 int w_inc = 1, h_inc, w_base = 1, h_base;
1386 int hrh, extra = 0, h_slice, last_h = 0;
1387 int split, colno, winno, mwin, msize, mscale;
1388 int remain, missing, v_slice;
1389 unsigned int mask;
1390
1391 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1392 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1393
1394 winno = count_win(ws, 0);
1395 if (winno == 0 && count_win(ws, 1) == 0)
1396 return;
1397
1398 if (ws->focus == NULL)
1399 ws->focus = TAILQ_FIRST(&ws->winlist);
1400 winfocus = cur_focus ? cur_focus : ws->focus;
1401
1402 TAILQ_FOREACH(win, &ws->winlist, entry)
1403 if (win->transient == 0 && win->floating == 0)
1404 break;
1405
1406 if (win == NULL)
1407 goto notiles;
1408
1409 if (rot) {
1410 w_inc = win->sh.width_inc;
1411 w_base = win->sh.base_width;
1412 mwin = ws->l_state.horizontal_mwin;
1413 mscale = ws->l_state.horizontal_msize;
1414 stacks = ws->l_state.horizontal_stacks;
1415 SWAPXY(&r_g);
1416 } else {
1417 w_inc = win->sh.height_inc;
1418 w_base = win->sh.base_height;
1419 mwin = ws->l_state.vertical_mwin;
1420 mscale = ws->l_state.vertical_msize;
1421 stacks = ws->l_state.vertical_stacks;
1422 }
1423 win_g = r_g;
1424
1425 if (stacks > winno - mwin)
1426 stacks = winno - mwin;
1427 if (stacks < 1)
1428 stacks = 1;
1429
1430 h_slice = r_g.h / SWM_H_SLICE;
1431 if (mwin && winno > mwin) {
1432 v_slice = r_g.w / SWM_V_SLICE;
1433
1434 split = mwin;
1435 colno = split;
1436 win_g.w = v_slice * mscale;
1437
1438 if (w_inc > 1 && w_inc < v_slice) {
1439 /* adjust for window's requested size increment */
1440 remain = (win_g.w - w_base) % w_inc;
1441 missing = w_inc - remain;
1442 win_g.w -= remain;
1443 extra += remain;
1444 }
1445
1446 msize = win_g.w;
1447 if (flip)
1448 win_g.x += r_g.w - msize;
1449 } else {
1450 msize = -2;
1451 colno = split = winno / stacks;
1452 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1453 }
1454 hrh = r_g.h / colno;
1455 extra = r_g.h - (colno * hrh);
1456 win_g.h = hrh - 2;
1457
1458 /* stack all the tiled windows */
1459 i = j = 0, s = stacks;
1460 TAILQ_FOREACH(win, &ws->winlist, entry) {
1461 if (win->transient != 0 || win->floating != 0)
1462 continue;
1463
1464 if (split && i == split) {
1465 colno = (winno - mwin) / stacks;
1466 if (s <= (winno - mwin) % stacks)
1467 colno++;
1468 split = split + colno;
1469 hrh = (r_g.h / colno);
1470 extra = r_g.h - (colno * hrh);
1471 if (flip)
1472 win_g.x = r_g.x;
1473 else
1474 win_g.x += win_g.w + 2;
1475 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1476 if (s == 1)
1477 win_g.w += (r_g.w - msize - (stacks * 2)) %
1478 stacks;
1479 s--;
1480 j = 0;
1481 }
1482 win_g.h = hrh - 2;
1483 if (rot) {
1484 h_inc = win->sh.width_inc;
1485 h_base = win->sh.base_width;
1486 } else {
1487 h_inc = win->sh.height_inc;
1488 h_base = win->sh.base_height;
1489 }
1490 if (j == colno - 1) {
1491 win_g.h = hrh + extra;
1492 } else if (h_inc > 1 && h_inc < h_slice) {
1493 /* adjust for window's requested size increment */
1494 remain = (win_g.h - h_base) % h_inc;
1495 missing = h_inc - remain;
1496
1497 if (missing <= extra || j == 0) {
1498 extra -= missing;
1499 win_g.h += missing;
1500 } else {
1501 win_g.h -= remain;
1502 extra += remain;
1503 }
1504 }
1505
1506 if (j == 0)
1507 win_g.y = r_g.y;
1508 else
1509 win_g.y += last_h + 2;
1510
1511 bzero(&wc, sizeof wc);
1512 wc.border_width = 1;
1513 if (rot) {
1514 win->g.x = wc.x = win_g.y;
1515 win->g.y = wc.y = win_g.x;
1516 win->g.w = wc.width = win_g.h;
1517 win->g.h = wc.height = win_g.w;
1518 } else {
1519 win->g.x = wc.x = win_g.x;
1520 win->g.y = wc.y = win_g.y;
1521 win->g.w = wc.width = win_g.w;
1522 win->g.h = wc.height = win_g.h;
1523 }
1524 adjust_font(win);
1525 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1526 XConfigureWindow(display, win->id, mask, &wc);
1527 XMapRaised(display, win->id);
1528
1529 last_h = win_g.h;
1530 i++;
1531 j++;
1532 }
1533
1534 notiles:
1535 /* now, stack all the floaters and transients */
1536 TAILQ_FOREACH(win, &ws->winlist, entry) {
1537 if (win->transient == 0 && win->floating == 0)
1538 continue;
1539
1540 stack_floater(win, ws->r);
1541 XMapRaised(display, win->id);
1542 }
1543
1544 if (winfocus)
1545 focus_win(winfocus); /* has to be done outside of the loop */
1546 }
1547
1548 void
1549 vertical_config(struct workspace *ws, int id)
1550 {
1551 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
1552
1553 switch (id) {
1554 case SWM_ARG_ID_STACKRESET:
1555 case SWM_ARG_ID_STACKINIT:
1556 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
1557 ws->l_state.vertical_mwin = 1;
1558 ws->l_state.vertical_stacks = 1;
1559 break;
1560 case SWM_ARG_ID_MASTERSHRINK:
1561 if (ws->l_state.vertical_msize > 1)
1562 ws->l_state.vertical_msize--;
1563 break;
1564 case SWM_ARG_ID_MASTERGROW:
1565 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
1566 ws->l_state.vertical_msize++;
1567 break;
1568 case SWM_ARG_ID_MASTERADD:
1569 ws->l_state.vertical_mwin++;
1570 break;
1571 case SWM_ARG_ID_MASTERDEL:
1572 if (ws->l_state.vertical_mwin > 0)
1573 ws->l_state.vertical_mwin--;
1574 break;
1575 case SWM_ARG_ID_STACKINC:
1576 ws->l_state.vertical_stacks++;
1577 break;
1578 case SWM_ARG_ID_STACKDEC:
1579 if (ws->l_state.vertical_stacks > 1)
1580 ws->l_state.vertical_stacks--;
1581 break;
1582 default:
1583 return;
1584 }
1585 }
1586
1587 void
1588 vertical_stack(struct workspace *ws, struct swm_geometry *g)
1589 {
1590 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1591
1592 stack_master(ws, g, 0, 0);
1593 }
1594
1595 void
1596 horizontal_config(struct workspace *ws, int id)
1597 {
1598 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
1599
1600 switch (id) {
1601 case SWM_ARG_ID_STACKRESET:
1602 case SWM_ARG_ID_STACKINIT:
1603 ws->l_state.horizontal_mwin = 1;
1604 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
1605 ws->l_state.horizontal_stacks = 1;
1606 break;
1607 case SWM_ARG_ID_MASTERSHRINK:
1608 if (ws->l_state.horizontal_msize > 1)
1609 ws->l_state.horizontal_msize--;
1610 break;
1611 case SWM_ARG_ID_MASTERGROW:
1612 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
1613 ws->l_state.horizontal_msize++;
1614 break;
1615 case SWM_ARG_ID_MASTERADD:
1616 ws->l_state.horizontal_mwin++;
1617 break;
1618 case SWM_ARG_ID_MASTERDEL:
1619 if (ws->l_state.horizontal_mwin > 0)
1620 ws->l_state.horizontal_mwin--;
1621 break;
1622 case SWM_ARG_ID_STACKINC:
1623 ws->l_state.horizontal_stacks++;
1624 break;
1625 case SWM_ARG_ID_STACKDEC:
1626 if (ws->l_state.horizontal_stacks > 1)
1627 ws->l_state.horizontal_stacks--;
1628 break;
1629 default:
1630 return;
1631 }
1632 }
1633
1634 void
1635 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
1636 {
1637 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
1638
1639 stack_master(ws, g, 1, 0);
1640 }
1641
1642 /* fullscreen view */
1643 void
1644 max_stack(struct workspace *ws, struct swm_geometry *g)
1645 {
1646 XWindowChanges wc;
1647 struct swm_geometry gg = *g;
1648 struct ws_win *win, *winfocus;
1649 unsigned int mask;
1650 int winno;
1651
1652 /* XXX this function needs to be rewritten it sucks crap */
1653
1654 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
1655
1656 winno = count_win(ws, 0);
1657 if (winno == 0 && count_win(ws, 1) == 0)
1658 return;
1659
1660 if (ws->focus == NULL)
1661 ws->focus = TAILQ_FIRST(&ws->winlist);
1662 winfocus = cur_focus ? cur_focus : ws->focus;
1663
1664 TAILQ_FOREACH(win, &ws->winlist, entry) {
1665 if (win->transient != 0 || win->floating != 0) {
1666 if (win == ws->focus) {
1667 /* XXX maximize? */
1668 stack_floater(win, ws->r);
1669 XMapRaised(display, win->id);
1670 } else {
1671 /* XXX this sucks */
1672 XUnmapWindow(display, win->id);
1673 }
1674 } else {
1675 bzero(&wc, sizeof wc);
1676 wc.border_width = 1;
1677 win->g.x = wc.x = gg.x;
1678 win->g.y = wc.y = gg.y;
1679 win->g.w = wc.width = gg.w;
1680 win->g.h = wc.height = gg.h;
1681 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1682 XConfigureWindow(display, win->id, mask, &wc);
1683
1684 if (win == ws->focus) {
1685 XMapRaised(display, win->id);
1686 } else
1687 XUnmapWindow(display, win->id);
1688 }
1689 }
1690
1691 if (winfocus)
1692 focus_win(winfocus); /* has to be done outside of the loop */
1693 }
1694
1695 void
1696 send_to_ws(struct swm_region *r, union arg *args)
1697 {
1698 int wsid = args->id;
1699 struct ws_win *win = cur_focus;
1700 struct workspace *ws, *nws;
1701 Atom ws_idx_atom = 0;
1702 unsigned char ws_idx_str[SWM_PROPLEN];
1703
1704 if (win == NULL)
1705 return;
1706
1707 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
1708
1709 ws = win->ws;
1710 nws = &win->s->ws[wsid];
1711
1712 XUnmapWindow(display, win->id);
1713
1714 /* find a window to focus */
1715 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
1716 if (ws->focus == NULL)
1717 ws->focus = TAILQ_FIRST(&ws->winlist);
1718 if (ws->focus == win)
1719 ws->focus = NULL;
1720
1721 TAILQ_REMOVE(&ws->winlist, win, entry);
1722
1723 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
1724 win->ws = nws;
1725
1726 /* Try to update the window's workspace property */
1727 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
1728 if (ws_idx_atom &&
1729 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
1730 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
1731 ws_idx_str);
1732 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
1733 PropModeReplace, ws_idx_str, SWM_PROPLEN);
1734 }
1735
1736 if (count_win(nws, 1) == 1)
1737 nws->focus = win;
1738 ws->restack = 1;
1739 nws->restack = 1;
1740
1741 stack();
1742 }
1743
1744 void
1745 wkill(struct swm_region *r, union arg *args)
1746 {
1747 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
1748
1749 if(r->ws->focus == NULL)
1750 return;
1751
1752 if (args->id == SWM_ARG_ID_KILLWINDOW)
1753 XKillClient(display, r->ws->focus->id);
1754 else
1755 if (r->ws->focus->can_delete)
1756 client_msg(r->ws->focus, adelete);
1757 }
1758
1759 void
1760 floating_toggle(struct swm_region *r, union arg *args)
1761 {
1762 struct ws_win *win = cur_focus;
1763
1764 if (win == NULL)
1765 return;
1766
1767 win->floating = !win->floating;
1768 win->manual = 0;
1769 stack();
1770 focus_win(win);
1771 }
1772
1773 void
1774 resize_window(struct ws_win *win, int center)
1775 {
1776 unsigned int mask;
1777 XWindowChanges wc;
1778 struct swm_region *r;
1779
1780 r = root_to_region(win->wa.root);
1781 bzero(&wc, sizeof wc);
1782 mask = CWBorderWidth | CWWidth | CWHeight;
1783 wc.border_width = 1;
1784 wc.width = win->g.w;
1785 wc.height = win->g.h;
1786 if (center == SWM_ARG_ID_CENTER) {
1787 wc.x = (WIDTH(r) - win->g.w) / 2;
1788 wc.y = (HEIGHT(r) - win->g.h) / 2;
1789 mask |= CWX | CWY;
1790 }
1791
1792 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
1793 win->id, wc.x, wc.y, wc.width, wc.height);
1794
1795 XConfigureWindow(display, win->id, mask, &wc);
1796 config_win(win);
1797 }
1798
1799 void
1800 resize(struct ws_win *win, union arg *args)
1801 {
1802 XEvent ev;
1803 Time time = 0;
1804
1805 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %d\n",
1806 win->id, win->floating, win->transient);
1807
1808 if (!(win->transient != 0 || win->floating != 0))
1809 return;
1810
1811 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1812 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1813 return;
1814 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
1815 do {
1816 XMaskEvent(display, MOUSEMASK | ExposureMask |
1817 SubstructureRedirectMask, &ev);
1818 switch(ev.type) {
1819 case ConfigureRequest:
1820 case Expose:
1821 case MapRequest:
1822 handler[ev.type](&ev);
1823 break;
1824 case MotionNotify:
1825 if (ev.xmotion.x <= 1)
1826 ev.xmotion.x = 1;
1827 if (ev.xmotion.y <= 1)
1828 ev.xmotion.y = 1;
1829 win->g.w = ev.xmotion.x;
1830 win->g.h = ev.xmotion.y;
1831
1832 /* not free, don't sync more than 60 times / second */
1833 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1834 time = ev.xmotion.time;
1835 XSync(display, False);
1836 resize_window(win, args->id);
1837 }
1838 break;
1839 }
1840 } while (ev.type != ButtonRelease);
1841 if (time) {
1842 XSync(display, False);
1843 resize_window(win, args->id);
1844 }
1845 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
1846 win->g.h - 1);
1847 XUngrabPointer(display, CurrentTime);
1848
1849 /* drain events */
1850 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1851 }
1852
1853 void
1854 move_window(struct ws_win *win)
1855 {
1856 unsigned int mask;
1857 XWindowChanges wc;
1858 struct swm_region *r;
1859
1860 r = root_to_region(win->wa.root);
1861 bzero(&wc, sizeof wc);
1862 mask = CWX | CWY;
1863 wc.x = win->g.x;
1864 wc.y = win->g.y;
1865
1866 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
1867 win->id, wc.x, wc.y, wc.width, wc.height);
1868
1869 XConfigureWindow(display, win->id, mask, &wc);
1870 config_win(win);
1871 }
1872
1873 void
1874 move(struct ws_win *win, union arg *args)
1875 {
1876 XEvent ev;
1877 Time time = 0;
1878 int restack = 0;
1879
1880 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %d\n",
1881 win->id, win->floating, win->transient);
1882
1883 if (win->floating == 0) {
1884 win->floating = 1;
1885 win->manual = 1;
1886 restack = 1;
1887 }
1888
1889 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
1890 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
1891 return;
1892 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1893 do {
1894 XMaskEvent(display, MOUSEMASK | ExposureMask |
1895 SubstructureRedirectMask, &ev);
1896 switch(ev.type) {
1897 case ConfigureRequest:
1898 case Expose:
1899 case MapRequest:
1900 handler[ev.type](&ev);
1901 break;
1902 case MotionNotify:
1903 win->g.x = ev.xmotion.x_root;
1904 win->g.y = ev.xmotion.y_root;
1905
1906 /* not free, don't sync more than 60 times / second */
1907 if ((ev.xmotion.time - time) > (1000 / 60) ) {
1908 time = ev.xmotion.time;
1909 XSync(display, False);
1910 move_window(win);
1911 }
1912 break;
1913 }
1914 } while (ev.type != ButtonRelease);
1915 if (time) {
1916 XSync(display, False);
1917 move_window(win);
1918 }
1919 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
1920 XUngrabPointer(display, CurrentTime);
1921 if (restack)
1922 stack();
1923
1924 /* drain events */
1925 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
1926 }
1927
1928 /* key definitions */
1929 void dummykeyfunc(struct swm_region *r, union arg *args) {};
1930 void legacyfunc(struct swm_region *r, union arg *args) {};
1931
1932 struct keyfunc {
1933 char name[SWM_FUNCNAME_LEN];
1934 void (*func)(struct swm_region *r, union arg *);
1935 union arg args;
1936 } keyfuncs[kf_invalid + 1] = {
1937 /* name function argument */
1938 { "cycle_layout", cycle_layout, {0} },
1939 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
1940 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
1941 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
1942 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
1943 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
1944 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
1945 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
1946 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
1947 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
1948 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
1949 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
1950 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
1951 { "spawn_term", spawnterm, {.argv = spawn_term} },
1952 { "spawn_menu", legacyfunc, {0} },
1953 { "quit", quit, {0} },
1954 { "restart", restart, {0} },
1955 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
1956 { "ws_1", switchws, {.id = 0} },
1957 { "ws_2", switchws, {.id = 1} },
1958 { "ws_3", switchws, {.id = 2} },
1959 { "ws_4", switchws, {.id = 3} },
1960 { "ws_5", switchws, {.id = 4} },
1961 { "ws_6", switchws, {.id = 5} },
1962 { "ws_7", switchws, {.id = 6} },
1963 { "ws_8", switchws, {.id = 7} },
1964 { "ws_9", switchws, {.id = 8} },
1965 { "ws_10", switchws, {.id = 9} },
1966 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
1967 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
1968 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
1969 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
1970 { "mvws_1", send_to_ws, {.id = 0} },
1971 { "mvws_2", send_to_ws, {.id = 1} },
1972 { "mvws_3", send_to_ws, {.id = 2} },
1973 { "mvws_4", send_to_ws, {.id = 3} },
1974 { "mvws_5", send_to_ws, {.id = 4} },
1975 { "mvws_6", send_to_ws, {.id = 5} },
1976 { "mvws_7", send_to_ws, {.id = 6} },
1977 { "mvws_8", send_to_ws, {.id = 7} },
1978 { "mvws_9", send_to_ws, {.id = 8} },
1979 { "mvws_10", send_to_ws, {.id = 9} },
1980 { "bar_toggle", bar_toggle, {0} },
1981 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
1982 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
1983 { "screenshot_all", legacyfunc, {0} },
1984 { "screenshot_wind", legacyfunc, {0} },
1985 { "float_toggle", floating_toggle,{0} },
1986 { "version", version, {0} },
1987 { "spawn_lock", legacyfunc, {0} },
1988 { "spawn_initscr", legacyfunc, {0} },
1989 { "spawn_custom", dummykeyfunc, {0} },
1990 { "invalid key func", NULL, {0} },
1991 };
1992 struct key {
1993 unsigned int mod;
1994 KeySym keysym;
1995 enum keyfuncid funcid;
1996 char *spawn_name;
1997 };
1998 int keys_size = 0, keys_length = 0;
1999 struct key *keys = NULL;
2000
2001 /* mouse */
2002 enum { client_click, root_click };
2003 struct button {
2004 unsigned int action;
2005 unsigned int mask;
2006 unsigned int button;
2007 void (*func)(struct ws_win *, union arg *);
2008 union arg args;
2009 } buttons[] = {
2010 /* action key mouse button func args */
2011 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2012 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2013 { client_click, MODKEY, Button1, move, {0} },
2014 };
2015
2016 void
2017 update_modkey(unsigned int mod)
2018 {
2019 int i;
2020
2021 mod_key = mod;
2022 for (i = 0; i < keys_length; i++)
2023 if (keys[i].mod & ShiftMask)
2024 keys[i].mod = mod | ShiftMask;
2025 else
2026 keys[i].mod = mod;
2027
2028 for (i = 0; i < LENGTH(buttons); i++)
2029 if (buttons[i].mask & ShiftMask)
2030 buttons[i].mask = mod | ShiftMask;
2031 else
2032 buttons[i].mask = mod;
2033 }
2034
2035 /* spawn */
2036 struct spawn_prog {
2037 char *name;
2038 int argc;
2039 char **argv;
2040 };
2041
2042 int spawns_size = 0, spawns_length = 0;
2043 struct spawn_prog *spawns = NULL;
2044
2045 void
2046 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2047 {
2048 union arg a;
2049 struct spawn_prog *prog = NULL;
2050 int i;
2051 char *ap, **real_args;
2052
2053 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2054
2055 /* find program */
2056 for (i = 0; i < spawns_length; i++) {
2057 if (!strcasecmp(spawn_name, spawns[i].name))
2058 prog = &spawns[i];
2059 }
2060 if (prog == NULL) {
2061 fprintf(stderr, "spawn_custom: program %s not found\n",
2062 spawn_name);
2063 return;
2064 }
2065
2066 /* make room for expanded args */
2067 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2068 err(1, "spawn_custom: calloc real_args");
2069
2070 /* expand spawn_args into real_args */
2071 for (i = 0; i < prog->argc; i++) {
2072 ap = prog->argv[i];
2073 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2074 if (!strcasecmp(ap, "$bar_border")) {
2075 if ((real_args[i] =
2076 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2077 == NULL)
2078 err(1, "spawn_custom border color");
2079 } else if (!strcasecmp(ap, "$bar_color")) {
2080 if ((real_args[i] =
2081 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2082 == NULL)
2083 err(1, "spawn_custom bar color");
2084 } else if (!strcasecmp(ap, "$bar_font")) {
2085 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2086 == NULL)
2087 err(1, "spawn_custom bar fonts");
2088 } else if (!strcasecmp(ap, "$bar_font_color")) {
2089 if ((real_args[i] =
2090 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2091 == NULL)
2092 err(1, "spawn_custom color font");
2093 } else if (!strcasecmp(ap, "$color_focus")) {
2094 if ((real_args[i] =
2095 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2096 == NULL)
2097 err(1, "spawn_custom color focus");
2098 } else if (!strcasecmp(ap, "$color_unfocus")) {
2099 if ((real_args[i] =
2100 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2101 == NULL)
2102 err(1, "spawn_custom color unfocus");
2103 } else {
2104 /* no match --> copy as is */
2105 if ((real_args[i] = strdup(ap)) == NULL)
2106 err(1, "spawn_custom strdup(ap)");
2107 }
2108 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2109 real_args[i]);
2110 }
2111
2112 #ifdef SWM_DEBUG
2113 if ((swm_debug & SWM_D_SPAWN) != 0) {
2114 fprintf(stderr, "spawn_custom: result = ");
2115 for (i = 0; i < prog->argc; i++)
2116 fprintf(stderr, "\"%s\" ", real_args[i]);
2117 fprintf(stderr, "\n");
2118 }
2119 #endif
2120
2121 a.argv = real_args;
2122 spawn(r, &a);
2123 for (i = 0; i < prog->argc; i++)
2124 free(real_args[i]);
2125 free(real_args);
2126 }
2127
2128 void
2129 setspawn(struct spawn_prog *prog)
2130 {
2131 int i, j;
2132
2133 if (prog == NULL || prog->name == NULL)
2134 return;
2135
2136 /* find existing */
2137 for (i = 0; i < spawns_length; i++) {
2138 if (!strcmp(spawns[i].name, prog->name)) {
2139 /* found */
2140 if (prog->argv == NULL) {
2141 /* delete */
2142 DNPRINTF(SWM_D_SPAWN,
2143 "setspawn: delete #%d %s\n",
2144 i, spawns[i].name);
2145 free(spawns[i].name);
2146 for (j = 0; j < spawns[i].argc; j++)
2147 free(spawns[i].argv[j]);
2148 free(spawns[i].argv);
2149 j = spawns_length - 1;
2150 if (i < j)
2151 spawns[i] = spawns[j];
2152 spawns_length--;
2153 free(prog->name);
2154 } else {
2155 /* replace */
2156 DNPRINTF(SWM_D_SPAWN,
2157 "setspawn: replace #%d %s\n",
2158 i, spawns[i].name);
2159 free(spawns[i].name);
2160 for (j = 0; j < spawns[i].argc; j++)
2161 free(spawns[i].argv[j]);
2162 free(spawns[i].argv);
2163 spawns[i] = *prog;
2164 }
2165 /* found case handled */
2166 free(prog);
2167 return;
2168 }
2169 }
2170
2171 if (prog->argv == NULL) {
2172 fprintf(stderr,
2173 "error: setspawn: cannot find program %s", prog->name);
2174 free(prog);
2175 return;
2176 }
2177
2178 /* not found: add */
2179 if (spawns_size == 0 || spawns == NULL) {
2180 spawns_size = 4;
2181 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2182 spawns = malloc((size_t)spawns_size *
2183 sizeof(struct spawn_prog));
2184 if (spawns == NULL) {
2185 fprintf(stderr, "setspawn: malloc failed\n");
2186 perror(" failed");
2187 quit(NULL, NULL);
2188 }
2189 } else if (spawns_length == spawns_size) {
2190 spawns_size *= 2;
2191 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2192 spawns = realloc(spawns, (size_t)spawns_size *
2193 sizeof(struct spawn_prog));
2194 if (spawns == NULL) {
2195 fprintf(stderr, "setspawn: realloc failed\n");
2196 perror(" failed");
2197 quit(NULL, NULL);
2198 }
2199 }
2200
2201 if (spawns_length < spawns_size) {
2202 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2203 spawns_length, prog->name);
2204 i = spawns_length++;
2205 spawns[i] = *prog;
2206 } else {
2207 fprintf(stderr, "spawns array problem?\n");
2208 if (spawns == NULL) {
2209 fprintf(stderr, "spawns array is NULL!\n");
2210 quit(NULL, NULL);
2211 }
2212 }
2213 free(prog);
2214 }
2215
2216 int
2217 setconfspawn(char *selector, char *value, int flags)
2218 {
2219 struct spawn_prog *prog;
2220 char *vp, *cp, *word;
2221
2222 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2223 if ((prog = calloc(1, sizeof *prog)) == NULL)
2224 err(1, "setconfspawn: calloc prog");
2225 prog->name = strdup(selector);
2226 if (prog->name == NULL)
2227 err(1, "setconfspawn prog->name");
2228 if ((cp = vp = strdup(value)) == NULL)
2229 err(1, "setconfspawn: strdup(value) ");
2230 while ((word = strsep(&cp, " \t")) != NULL) {
2231 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2232 if (cp)
2233 cp += (long)strspn(cp, " \t");
2234 if (strlen(word) > 0) {
2235 prog->argc++;
2236 prog->argv = realloc(prog->argv,
2237 prog->argc * sizeof(char *));
2238 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2239 err(1, "setconfspawn: strdup");
2240 }
2241 }
2242 free(vp);
2243
2244 setspawn(prog);
2245
2246 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2247 return (0);
2248 }
2249
2250 void
2251 setup_spawn(void)
2252 {
2253 setconfspawn("term", "xterm", 0);
2254 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2255 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2256 setconfspawn("lock", "xlock", 0);
2257 setconfspawn("initscr", "initscreen.sh", 0);
2258 setconfspawn("menu", "dmenu_run"
2259 " -fn $bar_font"
2260 " -nb $bar_color"
2261 " -nf $bar_font_color"
2262 " -sb $bar_border"
2263 " -sf $bar_color", 0);
2264 }
2265
2266 /* key bindings */
2267 #define SWM_MODNAME_SIZE 32
2268 #define SWM_KEY_WS "\n+ \t"
2269 int
2270 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2271 {
2272 char *cp, *name;
2273 KeySym uks;
2274 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2275 if (mod == NULL || ks == NULL) {
2276 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2277 return (1);
2278 }
2279 if (keystr == NULL || strlen(keystr) == 0) {
2280 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2281 return (1);
2282 }
2283 cp = keystr;
2284 *ks = NoSymbol;
2285 *mod = 0;
2286 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2287 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2288 if (cp)
2289 cp += (long)strspn(cp, SWM_KEY_WS);
2290 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2291 *mod |= currmod;
2292 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2293 *mod |= Mod1Mask;
2294 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2295 *mod += Mod2Mask;
2296 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2297 *mod |= Mod3Mask;
2298 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2299 *mod |= Mod4Mask;
2300 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2301 *mod |= ShiftMask;
2302 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2303 *mod |= ControlMask;
2304 else {
2305 *ks = XStringToKeysym(name);
2306 XConvertCase(*ks, ks, &uks);
2307 if (ks == NoSymbol) {
2308 DNPRINTF(SWM_D_KEY,
2309 "parsekeys: invalid key %s\n",
2310 name);
2311 return (1);
2312 }
2313 }
2314 }
2315 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2316 return (0);
2317 }
2318 char *
2319 strdupsafe(char *str)
2320 {
2321 if (str == NULL)
2322 return (NULL);
2323 else
2324 return (strdup(str));
2325 }
2326 void
2327 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2328 {
2329 int i, j;
2330 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2331 keyfuncs[kfid].name, spawn_name);
2332 /* find existing */
2333 for (i = 0; i < keys_length; i++) {
2334 if (keys[i].mod == mod && keys[i].keysym == ks) {
2335 if (kfid == kf_invalid) {
2336 /* found: delete */
2337 DNPRINTF(SWM_D_KEY,
2338 "setkeybinding: delete #%d %s\n",
2339 i, keyfuncs[keys[i].funcid].name);
2340 free(keys[i].spawn_name);
2341 j = keys_length - 1;
2342 if (i < j)
2343 keys[i] = keys[j];
2344 keys_length--;
2345 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2346 return;
2347 } else {
2348 /* found: replace */
2349 DNPRINTF(SWM_D_KEY,
2350 "setkeybinding: replace #%d %s %s\n",
2351 i, keyfuncs[keys[i].funcid].name,
2352 spawn_name);
2353 free(keys[i].spawn_name);
2354 keys[i].mod = mod;
2355 keys[i].keysym = ks;
2356 keys[i].funcid = kfid;
2357 keys[i].spawn_name = strdupsafe(spawn_name);
2358 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2359 return;
2360 }
2361 }
2362 }
2363 if (kfid == kf_invalid) {
2364 fprintf(stderr,
2365 "error: setkeybinding: cannot find mod/key combination");
2366 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2367 return;
2368 }
2369 /* not found: add */
2370 if (keys_size == 0 || keys == NULL) {
2371 keys_size = 4;
2372 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2373 keys = malloc((size_t)keys_size * sizeof(struct key));
2374 if (!keys) {
2375 fprintf(stderr, "malloc failed\n");
2376 perror(" failed");
2377 quit(NULL, NULL);
2378 }
2379 } else if (keys_length == keys_size) {
2380 keys_size *= 2;
2381 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2382 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2383 if (!keys) {
2384 fprintf(stderr, "realloc failed\n");
2385 perror(" failed");
2386 quit(NULL, NULL);
2387 }
2388 }
2389 if (keys_length < keys_size) {
2390 j = keys_length++;
2391 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2392 j, keyfuncs[kfid].name, spawn_name);
2393 keys[j].mod = mod;
2394 keys[j].keysym = ks;
2395 keys[j].funcid = kfid;
2396 keys[j].spawn_name = strdupsafe(spawn_name);
2397 } else {
2398 fprintf(stderr, "keys array problem?\n");
2399 if (!keys) {
2400 fprintf(stderr, "keys array problem\n");
2401 quit(NULL, NULL);
2402 }
2403 }
2404 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2405 }
2406 int
2407 setconfbinding(char *selector, char *value, int flags)
2408 {
2409 enum keyfuncid kfid;
2410 unsigned int mod;
2411 KeySym ks;
2412 int i;
2413 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2414 if (selector == NULL) {
2415 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2416 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2417 kfid = kf_invalid;
2418 setkeybinding(mod, ks, kfid, NULL);
2419 return (0);
2420 } else
2421 return (1);
2422 }
2423 /* search by key function name */
2424 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2425 if (strncasecmp(selector, keyfuncs[kfid].name,
2426 SWM_FUNCNAME_LEN) == 0) {
2427 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2428 selector);
2429 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2430 setkeybinding(mod, ks, kfid, NULL);
2431 return (0);
2432 } else
2433 return (1);
2434 }
2435 }
2436 /* search by custom spawn name */
2437 for (i = 0; i < spawns_length; i++) {
2438 if (strcasecmp(selector, spawns[i].name) == 0) {
2439 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2440 selector);
2441 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2442 setkeybinding(mod, ks, kf_spawn_custom,
2443 spawns[i].name);
2444 return (0);
2445 } else
2446 return (1);
2447 }
2448 }
2449 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
2450 return (1);
2451 }
2452 void
2453 setup_keys(void)
2454 {
2455 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
2456 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
2457 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
2458 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
2459 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
2460 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
2461 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
2462 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
2463 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
2464 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
2465 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
2466 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
2467 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
2468 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
2469 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
2470 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
2471 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
2472 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
2473 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
2474 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
2475 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
2476 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
2477 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
2478 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
2479 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
2480 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
2481 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
2482 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
2483 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
2484 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
2485 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
2486 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
2487 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
2488 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
2489 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
2490 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
2491 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
2492 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
2493 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
2494 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
2495 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
2496 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
2497 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
2498 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
2499 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
2500 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
2501 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
2502 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
2503 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
2504 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
2505 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
2506 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
2507 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
2508 }
2509 void
2510 updatenumlockmask(void)
2511 {
2512 unsigned int i, j;
2513 XModifierKeymap *modmap;
2514
2515 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
2516 numlockmask = 0;
2517 modmap = XGetModifierMapping(display);
2518 for (i = 0; i < 8; i++)
2519 for (j = 0; j < modmap->max_keypermod; j++)
2520 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2521 == XKeysymToKeycode(display, XK_Num_Lock))
2522 numlockmask = (1 << i);
2523
2524 XFreeModifiermap(modmap);
2525 }
2526
2527 void
2528 grabkeys(void)
2529 {
2530 unsigned int i, j, k;
2531 KeyCode code;
2532 unsigned int modifiers[] =
2533 { 0, LockMask, numlockmask, numlockmask | LockMask };
2534
2535 DNPRINTF(SWM_D_MISC, "grabkeys\n");
2536 updatenumlockmask();
2537
2538 for (k = 0; k < ScreenCount(display); k++) {
2539 if (TAILQ_EMPTY(&screens[k].rl))
2540 continue;
2541 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
2542 for (i = 0; i < keys_length; i++) {
2543 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
2544 for (j = 0; j < LENGTH(modifiers); j++)
2545 XGrabKey(display, code,
2546 keys[i].mod | modifiers[j],
2547 screens[k].root, True,
2548 GrabModeAsync, GrabModeAsync);
2549 }
2550 }
2551 }
2552
2553 void
2554 grabbuttons(struct ws_win *win, int focused)
2555 {
2556 unsigned int i, j;
2557 unsigned int modifiers[] =
2558 { 0, LockMask, numlockmask, numlockmask|LockMask };
2559
2560 updatenumlockmask();
2561 XUngrabButton(display, AnyButton, AnyModifier, win->id);
2562 if(focused) {
2563 for (i = 0; i < LENGTH(buttons); i++)
2564 if (buttons[i].action == client_click)
2565 for (j = 0; j < LENGTH(modifiers); j++)
2566 XGrabButton(display, buttons[i].button,
2567 buttons[i].mask | modifiers[j],
2568 win->id, False, BUTTONMASK,
2569 GrabModeAsync, GrabModeSync, None,
2570 None);
2571 } else
2572 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
2573 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
2574 }
2575
2576 void
2577 expose(XEvent *e)
2578 {
2579 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
2580 }
2581
2582 void
2583 keypress(XEvent *e)
2584 {
2585 unsigned int i;
2586 KeySym keysym;
2587 XKeyEvent *ev = &e->xkey;
2588
2589 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
2590
2591 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
2592 for (i = 0; i < keys_length; i++)
2593 if (keysym == keys[i].keysym
2594 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
2595 && keyfuncs[keys[i].funcid].func) {
2596 if (keys[i].funcid == kf_spawn_custom)
2597 spawn_custom(
2598 root_to_region(ev->root),
2599 &(keyfuncs[keys[i].funcid].args),
2600 keys[i].spawn_name
2601 );
2602 else
2603 keyfuncs[keys[i].funcid].func(
2604 root_to_region(ev->root),
2605 &(keyfuncs[keys[i].funcid].args)
2606 );
2607 }
2608 }
2609
2610 void
2611 buttonpress(XEvent *e)
2612 {
2613 XButtonPressedEvent *ev = &e->xbutton;
2614
2615 struct ws_win *win;
2616 int i, action;
2617
2618 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
2619
2620 action = root_click;
2621 if ((win = find_window(ev->window)) == NULL)
2622 return;
2623 else {
2624 focus_win(win);
2625 action = client_click;
2626 }
2627
2628 for (i = 0; i < LENGTH(buttons); i++)
2629 if (action == buttons[i].action && buttons[i].func &&
2630 buttons[i].button == ev->button &&
2631 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
2632 buttons[i].func(win, &buttons[i].args);
2633 }
2634
2635 void
2636 set_win_state(struct ws_win *win, long state)
2637 {
2638 long data[] = {state, None};
2639
2640 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
2641
2642 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
2643 (unsigned char *)data, 2);
2644 }
2645
2646 const char *quirkname[] = {
2647 "NONE", /* config string for "no value" */
2648 "FLOAT",
2649 "TRANSSZ",
2650 "ANYWHERE",
2651 "XTERM_FONTADJ",
2652 "FULLSCREEN",
2653 };
2654
2655 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
2656 #define SWM_Q_WS "\n|+ \t"
2657 int
2658 parsequirks(char *qstr, unsigned long *quirk)
2659 {
2660 char *cp, *name;
2661 int i;
2662 if (quirk == NULL)
2663 return (1);
2664 cp = qstr;
2665 *quirk = 0;
2666 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
2667 if (cp)
2668 cp += (long)strspn(cp, SWM_Q_WS);
2669 for (i = 0; i < LENGTH(quirkname); i++) {
2670 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
2671 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
2672 if (i == 0) {
2673 *quirk = 0;
2674 return (0);
2675 }
2676 *quirk |= 1 << (i-1);
2677 break;
2678 }
2679 }
2680 if (i >= LENGTH(quirkname)) {
2681 DNPRINTF(SWM_D_QUIRK,
2682 "parsequirks: invalid quirk [%s]\n", name);
2683 return (1);
2684 }
2685 }
2686 return (0);
2687 }
2688 void
2689 setquirk(const char *class, const char *name, const int quirk)
2690 {
2691 int i, j;
2692 /* find existing */
2693 for (i = 0; i < quirks_length; i++) {
2694 if (!strcmp(quirks[i].class, class) &&
2695 !strcmp(quirks[i].name, name)) {
2696 if (!quirk) {
2697 /* found: delete */
2698 DNPRINTF(SWM_D_QUIRK,
2699 "setquirk: delete #%d %s:%s\n",
2700 i, quirks[i].class, quirks[i].name);
2701 free(quirks[i].class);
2702 free(quirks[i].name);
2703 j = quirks_length - 1;
2704 if (i < j)
2705 quirks[i] = quirks[j];
2706 quirks_length--;
2707 return;
2708 } else {
2709 /* found: replace */
2710 DNPRINTF(SWM_D_QUIRK,
2711 "setquirk: replace #%d %s:%s\n",
2712 i, quirks[i].class, quirks[i].name);
2713 free(quirks[i].class);
2714 free(quirks[i].name);
2715 quirks[i].class = strdup(class);
2716 quirks[i].name = strdup(name);
2717 quirks[i].quirk = quirk;
2718 return;
2719 }
2720 }
2721 }
2722 if (!quirk) {
2723 fprintf(stderr,
2724 "error: setquirk: cannot find class/name combination");
2725 return;
2726 }
2727 /* not found: add */
2728 if (quirks_size == 0 || quirks == NULL) {
2729 quirks_size = 4;
2730 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
2731 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
2732 if (!quirks) {
2733 fprintf(stderr, "setquirk: malloc failed\n");
2734 perror(" failed");
2735 quit(NULL, NULL);
2736 }
2737 } else if (quirks_length == quirks_size) {
2738 quirks_size *= 2;
2739 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
2740 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
2741 if (!quirks) {
2742 fprintf(stderr, "setquirk: realloc failed\n");
2743 perror(" failed");
2744 quit(NULL, NULL);
2745 }
2746 }
2747 if (quirks_length < quirks_size) {
2748 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
2749 j = quirks_length++;
2750 quirks[j].class = strdup(class);
2751 quirks[j].name = strdup(name);
2752 quirks[j].quirk = quirk;
2753 } else {
2754 fprintf(stderr, "quirks array problem?\n");
2755 if (!quirks) {
2756 fprintf(stderr, "quirks array problem!\n");
2757 quit(NULL, NULL);
2758 }
2759 }
2760 }
2761 int
2762 setconfquirk(char *selector, char *value, int flags)
2763 {
2764 char *cp, *class, *name;
2765 int retval;
2766 unsigned long quirks;
2767 if (selector == NULL)
2768 return (0);
2769 if ((cp = strchr(selector, ':')) == NULL)
2770 return (0);
2771 *cp = '\0';
2772 class = selector;
2773 name = cp + 1;
2774 if ((retval = parsequirks(value, &quirks)) == 0)
2775 setquirk(class, name, quirks);
2776 return (retval);
2777 }
2778
2779 void
2780 setup_quirks(void)
2781 {
2782 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
2783 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
2784 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
2785 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
2786 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
2787 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2788 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
2789 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2790 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2791 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2792 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
2793 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
2794 setquirk("pcb", "pcb", SWM_Q_FLOAT);
2795 }
2796
2797 /* conf file stuff */
2798 #define SWM_CONF_FILE "scrotwm.conf"
2799
2800 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_CLOCK_ENABLED,
2801 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
2802 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED,
2803 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP,
2804 SWM_S_DIALOG_RATIO };
2805
2806 int
2807 setconfvalue(char *selector, char *value, int flags)
2808 {
2809 switch (flags) {
2810 case SWM_S_BAR_DELAY:
2811 bar_delay = atoi(value);
2812 break;
2813 case SWM_S_BAR_ENABLED:
2814 bar_enabled = atoi(value);
2815 break;
2816 case SWM_S_CLOCK_ENABLED:
2817 clock_enabled = atoi(value);
2818 break;
2819 case SWM_S_CYCLE_EMPTY:
2820 cycle_empty = atoi(value);
2821 break;
2822 case SWM_S_CYCLE_VISIBLE:
2823 cycle_visible = atoi(value);
2824 break;
2825 case SWM_S_SS_ENABLED:
2826 ss_enabled = atoi(value);
2827 break;
2828 case SWM_S_TERM_WIDTH:
2829 term_width = atoi(value);
2830 break;
2831 case SWM_S_TITLE_CLASS_ENABLED:
2832 title_class_enabled = atoi(value);
2833 break;
2834 case SWM_S_TITLE_NAME_ENABLED:
2835 title_name_enabled = atoi(value);
2836 break;
2837 case SWM_S_BAR_FONT:
2838 free(bar_fonts[0]);
2839 if ((bar_fonts[0] = strdup(value)) == NULL)
2840 err(1, "setconfvalue: bar_font");
2841 break;
2842 case SWM_S_BAR_ACTION:
2843 free(bar_argv[0]);
2844 if ((bar_argv[0] = strdup(value)) == NULL)
2845 err(1, "setconfvalue: bar_action");
2846 break;
2847 case SWM_S_SPAWN_TERM:
2848 free(spawn_term[0]);
2849 if ((spawn_term[0] = strdup(value)) == NULL)
2850 err(1, "setconfvalue: spawn_term");
2851 break;
2852 case SWM_S_SS_APP:
2853 break;
2854 case SWM_S_DIALOG_RATIO:
2855 dialog_ratio = atof(value);
2856 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
2857 dialog_ratio = .6;
2858 break;
2859 default:
2860 return (1);
2861 }
2862 return (0);
2863 }
2864
2865 int
2866 setconfmodkey(char *selector, char *value, int flags)
2867 {
2868 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
2869 update_modkey(Mod1Mask);
2870 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
2871 update_modkey(Mod2Mask);
2872 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
2873 update_modkey(Mod3Mask);
2874 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
2875 update_modkey(Mod4Mask);
2876 else
2877 return (1);
2878 return (0);
2879 }
2880
2881 int
2882 setconfcolor(char *selector, char *value, int flags)
2883 {
2884 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
2885 return (0);
2886 }
2887
2888 int
2889 setconfregion(char *selector, char *value, int flags)
2890 {
2891 custom_region(value);
2892 return (0);
2893 }
2894
2895 /* config options */
2896 struct config_option {
2897 char *optname;
2898 int (*func)(char*, char*, int);
2899 int funcflags;
2900 };
2901 struct config_option configopt[] = {
2902 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
2903 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
2904 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
2905 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
2906 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
2907 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
2908 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
2909 { "bind", setconfbinding, 0 },
2910 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
2911 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
2912 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
2913 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
2914 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
2915 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
2916 { "modkey", setconfmodkey, 0 },
2917 { "program", setconfspawn, 0 },
2918 { "quirk", setconfquirk, 0 },
2919 { "region", setconfregion, 0 },
2920 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
2921 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
2922 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
2923 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
2924 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
2925 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
2926 };
2927
2928
2929 int
2930 conf_load(char *filename)
2931 {
2932 FILE *config;
2933 char *line, *cp, *optsub, *optval;
2934 size_t linelen, lineno = 0;
2935 int wordlen, i, optind;
2936 struct config_option *opt;
2937 DPRINTF("conf_load begin\n");
2938 if (filename == NULL) {
2939 fprintf(stderr, "conf_load: no filename\n");
2940 return (1);
2941 }
2942 if ((config = fopen(filename, "r")) == NULL) {
2943 warn("conf_load: fopen");
2944 return (1);
2945 }
2946 while (!feof(config)) {
2947 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
2948 == NULL) {
2949 if (ferror(config))
2950 err(1, "%s", filename);
2951 else
2952 continue;
2953 }
2954 cp = line;
2955 cp += strspn(cp, " \t\n"); /* eat whitespace */
2956 if (cp[0] == '\0') {
2957 /* empty line */
2958 free(line);
2959 continue;
2960 }
2961 /* get config option */
2962 wordlen = strcspn(cp, "=[ \t\n");
2963 if (wordlen == 0) {
2964 warnx("%s: line %zd: no option found",
2965 filename, lineno);
2966 return (1);
2967 }
2968 optind = -1;
2969 for (i = 0; i < LENGTH(configopt); i++) {
2970 opt = &configopt[i];
2971 if (!strncasecmp(cp, opt->optname, wordlen) &&
2972 strlen(opt->optname) == wordlen) {
2973 optind = i;
2974 break;
2975 }
2976 }
2977 if (optind == -1) {
2978 warnx("%s: line %zd: unknown option %.*s",
2979 filename, lineno, wordlen, cp);
2980 return (1);
2981 }
2982 cp += wordlen;
2983 cp += strspn(cp, " \t\n"); /* eat whitespace */
2984 /* get [selector] if any */
2985 optsub = NULL;
2986 if (*cp == '[') {
2987 cp++;
2988 wordlen = strcspn(cp, "]");
2989 if (*cp != ']') {
2990 if (wordlen == 0) {
2991 warnx("%s: line %zd: syntax error",
2992 filename, lineno);
2993 return (1);
2994 }
2995 asprintf(&optsub, "%.*s", wordlen, cp);
2996 }
2997 cp += wordlen;
2998 cp += strspn(cp, "] \t\n"); /* eat trailing */
2999 }
3000 cp += strspn(cp, "= \t\n"); /* eat trailing */
3001 /* get RHS value */
3002 optval = strdup(cp);
3003 /* call function to deal with it all */
3004 if (configopt[optind].func(optsub, optval,
3005 configopt[optind].funcflags) != 0) {
3006 fprintf(stderr, "%s line %zd: %s\n",
3007 filename, lineno, line);
3008 errx(1, "%s: line %zd: invalid data for %s",
3009 filename, lineno, configopt[optind].optname);
3010 }
3011 free(optval);
3012 free(optsub);
3013 free(line);
3014 }
3015 fclose(config);
3016 DPRINTF("conf_load end\n");
3017 return (0);
3018 }
3019
3020 struct ws_win *
3021 manage_window(Window id)
3022 {
3023 Window trans;
3024 struct workspace *ws;
3025 struct ws_win *win;
3026 int format, i, ws_idx, n;
3027 unsigned long nitems, bytes;
3028 Atom ws_idx_atom = 0, type;
3029 Atom *prot = NULL, *pp;
3030 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3031 struct swm_region *r;
3032 long mask;
3033 const char *errstr;
3034 XWindowChanges wc;
3035
3036 if ((win = find_window(id)) != NULL)
3037 return (win); /* already being managed */
3038
3039 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3040 errx(1, "calloc: failed to allocate memory for new window");
3041
3042 /* Get all the window data in one shot */
3043 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3044 if (ws_idx_atom)
3045 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3046 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3047 XGetWindowAttributes(display, id, &win->wa);
3048 XGetTransientForHint(display, id, &trans);
3049 XGetWMNormalHints(display, id, &win->sh, &mask); /* XXX function? */
3050 if (trans) {
3051 win->transient = trans;
3052 DNPRINTF(SWM_D_MISC, "manage_window: win %u transient %u\n",
3053 (unsigned)win->id, win->transient);
3054 }
3055 /* get supported protocols */
3056 if (XGetWMProtocols(display, id, &prot, &n)) {
3057 for (i = 0, pp = prot; i < n; i++, pp++)
3058 if (*pp == adelete)
3059 win->can_delete = 1;
3060 if (prot)
3061 XFree(prot);
3062 }
3063
3064 /*
3065 * Figure out where to put the window. If it was previously assigned to
3066 * a workspace (either by spawn() or manually moving), and isn't
3067 * transient, * put it in the same workspace
3068 */
3069 r = root_to_region(win->wa.root);
3070 if (prop && win->transient == 0) {
3071 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3072 ws_idx = strtonum(prop, 0, 9, &errstr);
3073 if (errstr) {
3074 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3075 errstr, prop);
3076 }
3077 ws = &r->s->ws[ws_idx];
3078 } else
3079 ws = r->ws;
3080
3081 /* set up the window layout */
3082 win->id = id;
3083 win->ws = ws;
3084 win->s = r->s; /* this never changes */
3085 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3086
3087 win->g.w = win->wa.width;
3088 win->g.h = win->wa.height;
3089 win->g.x = win->wa.x;
3090 win->g.y = win->wa.y;
3091
3092 /* Set window properties so we can remember this after reincarnation */
3093 if (ws_idx_atom && prop == NULL &&
3094 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3095 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3096 ws_idx_str);
3097 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3098 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3099 }
3100 XFree(prop);
3101
3102 if (XGetClassHint(display, win->id, &win->ch)) {
3103 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3104 win->ch.res_class, win->ch.res_name);
3105 for (i = 0; i < quirks_length; i++){
3106 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3107 !strcmp(win->ch.res_name, quirks[i].name)) {
3108 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3109 win->ch.res_class, win->ch.res_name);
3110 if (quirks[i].quirk & SWM_Q_FLOAT)
3111 win->floating = 1;
3112 win->quirks = quirks[i].quirk;
3113 }
3114 }
3115 }
3116
3117 /* alter window position if quirky */
3118 if (win->quirks & SWM_Q_ANYWHERE) {
3119 win->manual = 1; /* don't center the quirky windows */
3120 bzero(&wc, sizeof wc);
3121 mask = 0;
3122 if (win->g.y < bar_height) {
3123 win->g.y = wc.y = bar_height;
3124 mask |= CWY;
3125 }
3126 if (win->g.w + win->g.x > WIDTH(r)) {
3127 win->g.x = wc.x = WIDTH(win->ws->r) - win->g.w - 2;
3128 mask |= CWX;
3129 }
3130 wc.border_width = 1;
3131 mask |= CWBorderWidth;
3132 XConfigureWindow(display, win->id, mask, &wc);
3133 }
3134
3135 /* Reset font sizes (the bruteforce way; no default keybinding). */
3136 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3137 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3138 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3139 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3140 fake_keypress(win, XK_KP_Add, ShiftMask);
3141 }
3142
3143 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3144 PropertyChangeMask | StructureNotifyMask);
3145
3146 set_win_state(win, NormalState);
3147
3148 /* floaters need to be mapped if they are in the current workspace */
3149 if (win->floating && (ws->idx == r->ws->idx))
3150 XMapRaised(display, win->id);
3151
3152 /* make new win focused */
3153 focus_win(win);
3154
3155 return (win);
3156 }
3157
3158 void
3159 unmanage_window(struct ws_win *win)
3160 {
3161 struct workspace *ws;
3162
3163 if (win == NULL)
3164 return;
3165
3166 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3167
3168 ws = win->ws;
3169
3170 /* find a window to focus */
3171 if (ws->focus == win)
3172 ws->focus = TAILQ_PREV(win, ws_win_list, entry);
3173 if (ws->focus == NULL)
3174 ws->focus = TAILQ_FIRST(&ws->winlist);
3175 if (ws->focus == NULL || ws->focus == win) {
3176 ws->focus = NULL;
3177 unfocus_win(win);
3178 } else
3179 focus_win(ws->focus);
3180 if (ws->focus_prev == win)
3181 ws->focus_prev = NULL;
3182
3183 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3184 set_win_state(win, WithdrawnState);
3185 if (win->ch.res_class)
3186 XFree(win->ch.res_class);
3187 if (win->ch.res_name)
3188 XFree(win->ch.res_name);
3189 free(win);
3190 }
3191
3192 void
3193 configurerequest(XEvent *e)
3194 {
3195 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3196 struct ws_win *win;
3197 int new = 0;
3198 XWindowChanges wc;
3199
3200 if ((win = find_window(ev->window)) == NULL)
3201 new = 1;
3202
3203 if (new) {
3204 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3205 ev->window);
3206 bzero(&wc, sizeof wc);
3207 wc.x = ev->x;
3208 wc.y = ev->y;
3209 wc.width = ev->width;
3210 wc.height = ev->height;
3211 wc.border_width = ev->border_width;
3212 wc.sibling = ev->above;
3213 wc.stack_mode = ev->detail;
3214 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3215 } else {
3216 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3217 ev->window);
3218 if (win->floating) {
3219 if (ev->value_mask & CWX)
3220 win->g.x = ev->x;
3221 if (ev->value_mask & CWY)
3222 win->g.y = ev->y;
3223 if (ev->value_mask & CWWidth)
3224 win->g.w = ev->width;
3225 if (ev->value_mask & CWHeight)
3226 win->g.h = ev->height;
3227 if (win->ws->r != NULL) {
3228 /* this seems to be full screen */
3229 if (win->g.w >= WIDTH(win->ws->r)) {
3230 win->g.x = 0;
3231 win->g.w = WIDTH(win->ws->r);
3232 ev->value_mask |= CWX | CWWidth;
3233 }
3234 if (win->g.h >= HEIGHT(win->ws->r)) {
3235 /* kill border */
3236 win->g.y = 0;
3237 win->g.h = HEIGHT(win->ws->r);
3238 ev->value_mask |= CWY | CWHeight;
3239 }
3240 }
3241 if ((ev->value_mask & (CWX | CWY)) &&
3242 !(ev->value_mask & (CWWidth | CWHeight)))
3243 config_win(win);
3244 XMoveResizeWindow(display, win->id,
3245 win->g.x, win->g.y, win->g.w, win->g.h);
3246 } else
3247 config_win(win);
3248 }
3249 }
3250
3251 void
3252 configurenotify(XEvent *e)
3253 {
3254 struct ws_win *win;
3255 long mask;
3256
3257 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3258 e->xconfigure.window);
3259
3260 XMapWindow(display, e->xconfigure.window);
3261 win = find_window(e->xconfigure.window);
3262 if (win) {
3263 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3264 adjust_font(win);
3265 XMapWindow(display, win->id);
3266 if (font_adjusted)
3267 stack();
3268 }
3269 }
3270
3271 void
3272 destroynotify(XEvent *e)
3273 {
3274 struct ws_win *win;
3275 XDestroyWindowEvent *ev = &e->xdestroywindow;
3276
3277 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3278
3279 if ((win = find_window(ev->window)) != NULL) {
3280 unmanage_window(win);
3281 stack();
3282 }
3283 }
3284
3285 void
3286 enternotify(XEvent *e)
3287 {
3288 XCrossingEvent *ev = &e->xcrossing;
3289 struct ws_win *win;
3290
3291 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3292
3293 if (ignore_enter) {
3294 /* eat event(r) to prevent autofocus */
3295 ignore_enter--;
3296 return;
3297 }
3298
3299 if ((win = find_window(ev->window)) != NULL)
3300 focus_win(win);
3301 }
3302
3303 void
3304 focusin(XEvent *e)
3305 {
3306 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3307 }
3308
3309 void
3310 focusout(XEvent *e)
3311 {
3312 struct swm_screen *s;
3313 Window rr, cr;
3314 int x, y, wx, wy;
3315 unsigned int mask;
3316
3317 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3318
3319 if (cur_focus && cur_focus->ws->r &&
3320 cur_focus->id == e->xfocus.window) {
3321 s = cur_focus->ws->r->s;
3322 if (XQueryPointer(display, cur_focus->id,
3323 &rr, &cr, &x, &y, &wx, &wy, &mask) != False &&
3324 cr == 0 && !mask &&
3325 x == DisplayWidth(display, s->idx) / 2 &&
3326 y == DisplayHeight(display, s->idx) / 2)
3327 unfocus_win(cur_focus);
3328 }
3329 }
3330
3331 void
3332 mappingnotify(XEvent *e)
3333 {
3334 XMappingEvent *ev = &e->xmapping;
3335
3336 DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window);
3337
3338 XRefreshKeyboardMapping(ev);
3339 if (ev->request == MappingKeyboard)
3340 grabkeys();
3341 }
3342
3343 void
3344 maprequest(XEvent *e)
3345 {
3346 XMapRequestEvent *ev = &e->xmaprequest;
3347 XWindowAttributes wa;
3348
3349 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3350 e->xmaprequest.window);
3351
3352 if (!XGetWindowAttributes(display, ev->window, &wa))
3353 return;
3354 if (wa.override_redirect)
3355 return;
3356 manage_window(e->xmaprequest.window);
3357
3358 stack();
3359 }
3360
3361 void
3362 propertynotify(XEvent *e)
3363 {
3364 struct ws_win *win;
3365 XPropertyEvent *ev = &e->xproperty;
3366
3367 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3368 ev->window);
3369
3370 if (ev->state == PropertyDelete)
3371 return; /* ignore */
3372 win = find_window(ev->window);
3373 if (win == NULL)
3374 return;
3375
3376 switch (ev->atom) {
3377 case XA_WM_NORMAL_HINTS:
3378 #if 0
3379 long mask;
3380 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3381 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3382 if (win->sh.flags & PMinSize) {
3383 win->g.w = win->sh.min_width;
3384 win->g.h = win->sh.min_height;
3385 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3386 }
3387 XMoveResizeWindow(display, win->id,
3388 win->g.x, win->g.y, win->g.w, win->g.h);
3389 #endif
3390 break;
3391 default:
3392 break;
3393 }
3394 }
3395
3396 void
3397 unmapnotify(XEvent *e)
3398 {
3399 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3400 }
3401
3402 void
3403 visibilitynotify(XEvent *e)
3404 {
3405 int i;
3406 struct swm_region *r;
3407
3408 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
3409 e->xvisibility.window);
3410 if (e->xvisibility.state == VisibilityUnobscured)
3411 for (i = 0; i < ScreenCount(display); i++)
3412 TAILQ_FOREACH(r, &screens[i].rl, entry)
3413 if (e->xvisibility.window == r->bar_window)
3414 bar_update();
3415 }
3416
3417 int
3418 xerror_start(Display *d, XErrorEvent *ee)
3419 {
3420 other_wm = 1;
3421 return (-1);
3422 }
3423
3424 int
3425 xerror(Display *d, XErrorEvent *ee)
3426 {
3427 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3428 return (-1);
3429 }
3430
3431 int
3432 active_wm(void)
3433 {
3434 other_wm = 0;
3435 xerrorxlib = XSetErrorHandler(xerror_start);
3436
3437 /* this causes an error if some other window manager is running */
3438 XSelectInput(display, DefaultRootWindow(display),
3439 SubstructureRedirectMask);
3440 XSync(display, False);
3441 if (other_wm)
3442 return (1);
3443
3444 XSetErrorHandler(xerror);
3445 XSync(display, False);
3446 return (0);
3447 }
3448
3449 long
3450 getstate(Window w)
3451 {
3452 int format, status;
3453 long result = -1;
3454 unsigned char *p = NULL;
3455 unsigned long n, extra;
3456 Atom real;
3457
3458 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
3459 &real, &format, &n, &extra, (unsigned char **)&p);
3460 if (status != Success)
3461 return (-1);
3462 if (n != 0)
3463 result = *((long *)p);
3464 XFree(p);
3465 return (result);
3466 }
3467
3468 void
3469 new_region(struct swm_screen *s, int x, int y, int w, int h)
3470 {
3471 struct swm_region *r, *n;
3472 struct workspace *ws = NULL;
3473 int i;
3474
3475 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
3476 s->idx, w, h, x, y);
3477
3478 /* remove any conflicting regions */
3479 n = TAILQ_FIRST(&s->rl);
3480 while (n) {
3481 r = n;
3482 n = TAILQ_NEXT(r, entry);
3483 if (X(r) < (x + w) &&
3484 (X(r) + WIDTH(r)) > x &&
3485 Y(r) < (y + h) &&
3486 (Y(r) + HEIGHT(r)) > y) {
3487 XDestroyWindow(display, r->bar_window);
3488 TAILQ_REMOVE(&s->rl, r, entry);
3489 TAILQ_INSERT_TAIL(&s->orl, r, entry);
3490 }
3491 }
3492
3493 /* search old regions for one to reuse */
3494
3495 /* size + location match */
3496 TAILQ_FOREACH(r, &s->orl, entry)
3497 if (X(r) == x && Y(r) == y &&
3498 HEIGHT(r) == h && WIDTH(r) == w)
3499 break;
3500
3501 /* size match */
3502 TAILQ_FOREACH(r, &s->orl, entry)
3503 if (HEIGHT(r) == h && WIDTH(r) == w)
3504 break;
3505
3506 if (r != NULL) {
3507 TAILQ_REMOVE(&s->orl, r, entry);
3508 /* try to use old region's workspace */
3509 if (r->ws->r == NULL)
3510 ws = r->ws;
3511 } else
3512 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
3513 errx(1, "calloc: failed to allocate memory for screen");
3514
3515 /* if we don't have a workspace already, find one */
3516 if (ws == NULL) {
3517 for (i = 0; i < SWM_WS_MAX; i++)
3518 if (s->ws[i].r == NULL) {
3519 ws = &s->ws[i];
3520 break;
3521 }
3522 }
3523
3524 if (ws == NULL)
3525 errx(1, "no free workspaces\n");
3526
3527 X(r) = x;
3528 Y(r) = y;
3529 WIDTH(r) = w;
3530 HEIGHT(r) = h;
3531 r->s = s;
3532 r->ws = ws;
3533 ws->r = r;
3534 TAILQ_INSERT_TAIL(&s->rl, r, entry);
3535 }
3536
3537 void
3538 scan_xrandr(int i)
3539 {
3540 #ifdef SWM_XRR_HAS_CRTC
3541 XRRCrtcInfo *ci;
3542 XRRScreenResources *sr;
3543 int c;
3544 int ncrtc = 0;
3545 #endif /* SWM_XRR_HAS_CRTC */
3546 struct swm_region *r;
3547
3548
3549 if (i >= ScreenCount(display))
3550 errx(1, "invalid screen");
3551
3552 /* remove any old regions */
3553 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
3554 r->ws->r = NULL;
3555 XDestroyWindow(display, r->bar_window);
3556 TAILQ_REMOVE(&screens[i].rl, r, entry);
3557 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
3558 }
3559
3560 /* map virtual screens onto physical screens */
3561 #ifdef SWM_XRR_HAS_CRTC
3562 if (xrandr_support) {
3563 sr = XRRGetScreenResources(display, screens[i].root);
3564 if (sr == NULL)
3565 new_region(&screens[i], 0, 0,
3566 DisplayWidth(display, i),
3567 DisplayHeight(display, i));
3568 else
3569 ncrtc = sr->ncrtc;
3570
3571 for (c = 0, ci = NULL; c < ncrtc; c++) {
3572 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
3573 if (ci->noutput == 0)
3574 continue;
3575
3576 if (ci != NULL && ci->mode == None)
3577 new_region(&screens[i], 0, 0,
3578 DisplayWidth(display, i),
3579 DisplayHeight(display, i));
3580 else
3581 new_region(&screens[i],
3582 ci->x, ci->y, ci->width, ci->height);
3583 }
3584 if (ci)
3585 XRRFreeCrtcInfo(ci);
3586 XRRFreeScreenResources(sr);
3587 } else
3588 #endif /* SWM_XRR_HAS_CRTC */
3589 {
3590 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
3591 DisplayHeight(display, i));
3592 }
3593 }
3594
3595 void
3596 screenchange(XEvent *e) {
3597 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
3598 struct swm_region *r;
3599 struct ws_win *win;
3600 int i;
3601
3602 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
3603
3604 if (!XRRUpdateConfiguration(e))
3605 return;
3606
3607 /* silly event doesn't include the screen index */
3608 for (i = 0; i < ScreenCount(display); i++)
3609 if (screens[i].root == xe->root)
3610 break;
3611 if (i >= ScreenCount(display))
3612 errx(1, "screenchange: screen not found\n");
3613
3614 /* brute force for now, just re-enumerate the regions */
3615 scan_xrandr(i);
3616
3617 /* hide any windows that went away */
3618 TAILQ_FOREACH(r, &screens[i].rl, entry)
3619 TAILQ_FOREACH(win, &r->ws->winlist, entry)
3620 XUnmapWindow(display, win->id);
3621
3622 /* add bars to all regions */
3623 for (i = 0; i < ScreenCount(display); i++)
3624 TAILQ_FOREACH(r, &screens[i].rl, entry)
3625 bar_setup(r);
3626 stack();
3627 }
3628
3629 void
3630 setup_screens(void)
3631 {
3632 Window d1, d2, *wins = NULL;
3633 XWindowAttributes wa;
3634 unsigned int no;
3635 int i, j, k;
3636 int errorbase, major, minor;
3637 struct workspace *ws;
3638 int ws_idx_atom;
3639
3640
3641 if ((screens = calloc(ScreenCount(display),
3642 sizeof(struct swm_screen))) == NULL)
3643 errx(1, "calloc: screens");
3644
3645 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3646
3647 /* initial Xrandr setup */
3648 xrandr_support = XRRQueryExtension(display,
3649 &xrandr_eventbase, &errorbase);
3650 if (xrandr_support)
3651 if (XRRQueryVersion(display, &major, &minor) && major < 1)
3652 xrandr_support = 0;
3653
3654 /* map physical screens */
3655 for (i = 0; i < ScreenCount(display); i++) {
3656 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
3657 screens[i].idx = i;
3658 TAILQ_INIT(&screens[i].rl);
3659 TAILQ_INIT(&screens[i].orl);
3660 screens[i].root = RootWindow(display, i);
3661
3662 /* set default colors */
3663 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
3664 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
3665 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
3666 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
3667 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
3668
3669 /* init all workspaces */
3670 /* XXX these should be dynamically allocated too */
3671 for (j = 0; j < SWM_WS_MAX; j++) {
3672 ws = &screens[i].ws[j];
3673 ws->idx = j;
3674 ws->restack = 1;
3675 ws->focus = NULL;
3676 ws->r = NULL;
3677 TAILQ_INIT(&ws->winlist);
3678
3679 for (k = 0; layouts[k].l_stack != NULL; k++)
3680 if (layouts[k].l_config != NULL)
3681 layouts[k].l_config(ws,
3682 SWM_ARG_ID_STACKINIT);
3683 ws->cur_layout = &layouts[0];
3684 }
3685 /* grab existing windows (before we build the bars)*/
3686 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
3687 continue;
3688
3689 scan_xrandr(i);
3690
3691 if (xrandr_support)
3692 XRRSelectInput(display, screens[i].root,
3693 RRScreenChangeNotifyMask);
3694
3695 /* attach windows to a region */
3696 /* normal windows */
3697 for (j = 0; j < no; j++) {
3698 XGetWindowAttributes(display, wins[j], &wa);
3699 if (!XGetWindowAttributes(display, wins[j], &wa) ||
3700 wa.override_redirect ||
3701 XGetTransientForHint(display, wins[j], &d1))
3702 continue;
3703
3704 if (wa.map_state == IsViewable ||
3705 getstate(wins[j]) == NormalState)
3706 manage_window(wins[j]);
3707 }
3708 /* transient windows */
3709 for (j = 0; j < no; j++) {
3710 if (!XGetWindowAttributes(display, wins[j], &wa))
3711 continue;
3712
3713 if (XGetTransientForHint(display, wins[j], &d1) &&
3714 (wa.map_state == IsViewable || getstate(wins[j]) ==
3715 NormalState))
3716 manage_window(wins[j]);
3717 }
3718 if (wins) {
3719 XFree(wins);
3720 wins = NULL;
3721 }
3722 }
3723 }
3724 void
3725 setup_globals(void)
3726 {
3727 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
3728 == NULL)
3729 err(1, "setup_globals: strdup");
3730 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
3731 == NULL)
3732 err(1, "setup_globals: strdup");
3733 if ((spawn_term[0] = strdup("xterm")) == NULL)
3734 err(1, "setup_globals: strdup");
3735 }
3736
3737 void
3738 workaround(void)
3739 {
3740 int i;
3741 Atom netwmcheck, netwmname, utf8_string;
3742 Window root;
3743
3744 /* work around sun jdk bugs, code from wmname */
3745 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
3746 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
3747 utf8_string = XInternAtom(display, "UTF8_STRING", False);
3748 for (i = 0; i < ScreenCount(display); i++) {
3749 root = screens[i].root;
3750 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
3751 PropModeReplace, (unsigned char *)&root, 1);
3752 XChangeProperty(display, root, netwmname, utf8_string, 8,
3753 PropModeReplace, "LG3D", strlen("LG3D"));
3754 }
3755 }
3756
3757 int
3758 main(int argc, char *argv[])
3759 {
3760 struct passwd *pwd;
3761 struct swm_region *r;
3762 char conf[PATH_MAX], *cfile = NULL;
3763 struct stat sb;
3764 XEvent e;
3765 int xfd, i;
3766 fd_set rd;
3767
3768 start_argv = argv;
3769 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
3770 SWM_VERSION, cvstag);
3771 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
3772 warnx("no locale support");
3773
3774 if (!(display = XOpenDisplay(0)))
3775 errx(1, "can not open display");
3776
3777 if (active_wm())
3778 errx(1, "other wm running");
3779
3780 astate = XInternAtom(display, "WM_STATE", False);
3781 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
3782 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
3783
3784 /* look for local and global conf file */
3785 pwd = getpwuid(getuid());
3786 if (pwd == NULL)
3787 errx(1, "invalid user %d", getuid());
3788
3789 setup_screens();
3790 setup_globals();
3791 setup_keys();
3792 setup_quirks();
3793 setup_spawn();
3794
3795 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
3796 if (stat(conf, &sb) != -1) {
3797 if (S_ISREG(sb.st_mode))
3798 cfile = conf;
3799 } else {
3800 /* try global conf file */
3801 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
3802 if (!stat(conf, &sb))
3803 if (S_ISREG(sb.st_mode))
3804 cfile = conf;
3805 }
3806 if (cfile)
3807 conf_load(cfile);
3808
3809 /* setup all bars */
3810 for (i = 0; i < ScreenCount(display); i++)
3811 TAILQ_FOREACH(r, &screens[i].rl, entry)
3812 bar_setup(r);
3813
3814 /* set some values to work around bad programs */
3815 workaround();
3816
3817 grabkeys();
3818 stack();
3819
3820 xfd = ConnectionNumber(display);
3821 while (running) {
3822 FD_ZERO(&rd);
3823 FD_SET(xfd, &rd);
3824 if (select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
3825 if (errno != EINTR)
3826 errx(1, "select failed");
3827 if (bar_alarm) {
3828 bar_alarm = 0;
3829 bar_update();
3830 }
3831 while (XPending(display)) {
3832 XNextEvent(display, &e);
3833 if (e.type < LASTEvent) {
3834 if (handler[e.type])
3835 handler[e.type](&e);
3836 else
3837 DNPRINTF(SWM_D_EVENT,
3838 "win: %lu unknown event: %d\n",
3839 e.xany.window, e.type);
3840 } else {
3841 switch (e.type - xrandr_eventbase) {
3842 case RRScreenChangeNotify:
3843 screenchange(&e);
3844 break;
3845 default:
3846 DNPRINTF(SWM_D_EVENT,
3847 "win: %lu unknown xrandr event: "
3848 "%d\n", e.xany.window, e.type);
3849 break;
3850 }
3851 }
3852 }
3853 }
3854
3855 XCloseDisplay(display);
3856
3857 return (0);
3858 }