]> code.delx.au - spectrwm/blob - scrotwm.c
undo previous diff and create a quirk instead. the previous change was
[spectrwm] / scrotwm.c
1 /* $scrotwm$ */
2 /*
3 * Copyright (c) 2009-2010-2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009-2010-2011 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 * Copyright (c) 2011 Jason L. Wright <jason@thought.net>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21 /*
22 * Much code and ideas taken from dwm under the following license:
23 * MIT/X Consortium License
24 *
25 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
26 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
27 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
28 * 2007 Premysl Hruby <dfenze at gmail dot com>
29 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
30 * 2007 Christof Musik <christof at sendfax dot de>
31 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
32 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
33 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 *
35 * Permission is hereby granted, free of charge, to any person obtaining a
36 * copy of this software and associated documentation files (the "Software"),
37 * to deal in the Software without restriction, including without limitation
38 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
39 * and/or sell copies of the Software, and to permit persons to whom the
40 * Software is furnished to do so, subject to the following conditions:
41 *
42 * The above copyright notice and this permission notice shall be included in
43 * all copies or substantial portions of the Software.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51 * DEALINGS IN THE SOFTWARE.
52 */
53
54 static const char *cvstag = "$scrotwm$";
55
56 #define SWM_VERSION "0.9.30"
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <locale.h>
64 #include <unistd.h>
65 #include <time.h>
66 #include <signal.h>
67 #include <string.h>
68 #include <util.h>
69 #include <pwd.h>
70 #include <paths.h>
71 #include <ctype.h>
72
73 #include <sys/types.h>
74 #include <sys/time.h>
75 #include <sys/stat.h>
76 #include <sys/wait.h>
77 #include <sys/queue.h>
78 #include <sys/param.h>
79 #include <sys/select.h>
80
81 #include <X11/cursorfont.h>
82 #include <X11/keysym.h>
83 #include <X11/Xatom.h>
84 #include <X11/Xlib.h>
85 #include <X11/Xproto.h>
86 #include <X11/Xutil.h>
87 #include <X11/extensions/Xrandr.h>
88
89 #ifdef __OSX__
90 #include <osx.h>
91 #endif
92
93 #if RANDR_MAJOR < 1
94 # error XRandR versions less than 1.0 are not supported
95 #endif
96
97 #if RANDR_MAJOR >= 1
98 #if RANDR_MINOR >= 2
99 #define SWM_XRR_HAS_CRTC
100 #endif
101 #endif
102
103 /*#define SWM_DEBUG*/
104 #ifdef SWM_DEBUG
105 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
106 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
107 #define SWM_D_MISC 0x0001
108 #define SWM_D_EVENT 0x0002
109 #define SWM_D_WS 0x0004
110 #define SWM_D_FOCUS 0x0008
111 #define SWM_D_MOVE 0x0010
112 #define SWM_D_STACK 0x0020
113 #define SWM_D_MOUSE 0x0040
114 #define SWM_D_PROP 0x0080
115 #define SWM_D_CLASS 0x0100
116 #define SWM_D_KEY 0x0200
117 #define SWM_D_QUIRK 0x0400
118 #define SWM_D_SPAWN 0x0800
119 #define SWM_D_EVENTQ 0x1000
120 #define SWM_D_CONF 0x2000
121
122 u_int32_t swm_debug = 0
123 | SWM_D_MISC
124 | SWM_D_EVENT
125 | SWM_D_WS
126 | SWM_D_FOCUS
127 | SWM_D_MOVE
128 | SWM_D_STACK
129 | SWM_D_MOUSE
130 | SWM_D_PROP
131 | SWM_D_CLASS
132 | SWM_D_KEY
133 | SWM_D_QUIRK
134 | SWM_D_SPAWN
135 | SWM_D_EVENTQ
136 | SWM_D_CONF
137 ;
138 #else
139 #define DPRINTF(x...)
140 #define DNPRINTF(n,x...)
141 #endif
142
143 #define LENGTH(x) (sizeof x / sizeof x[0])
144 #define MODKEY Mod1Mask
145 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
146 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
147 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
148 #define SWM_PROPLEN (16)
149 #define SWM_FUNCNAME_LEN (32)
150 #define SWM_KEYS_LEN (255)
151 #define SWM_QUIRK_LEN (64)
152 #define X(r) (r)->g.x
153 #define Y(r) (r)->g.y
154 #define WIDTH(r) (r)->g.w
155 #define HEIGHT(r) (r)->g.h
156 #define SWM_MAX_FONT_STEPS (3)
157 #define WINID(w) (w ? w->id : 0)
158
159 #define SWM_FOCUS_DEFAULT (0)
160 #define SWM_FOCUS_SYNERGY (1)
161 #define SWM_FOCUS_FOLLOW (2)
162
163 #ifndef SWM_LIB
164 #define SWM_LIB "/usr/local/lib/libswmhack.so"
165 #endif
166
167 char **start_argv;
168 Atom astate;
169 Atom aprot;
170 Atom adelete;
171 Atom takefocus;
172 Atom a_wmname;
173 Atom a_utf8_string;
174 Atom a_string;
175 Atom a_swm_iconic;
176 volatile sig_atomic_t running = 1;
177 volatile sig_atomic_t restart_wm = 0;
178 int outputs = 0;
179 int last_focus_event = FocusOut;
180 int (*xerrorxlib)(Display *, XErrorEvent *);
181 int other_wm;
182 int ss_enabled = 0;
183 int xrandr_support;
184 int xrandr_eventbase;
185 unsigned int numlockmask = 0;
186 Display *display;
187
188 int cycle_empty = 0;
189 int cycle_visible = 0;
190 int term_width = 0;
191 int font_adjusted = 0;
192 unsigned int mod_key = MODKEY;
193
194 /* dmenu search */
195 struct swm_region *search_r;
196 int select_list_pipe[2];
197 int select_resp_pipe[2];
198 pid_t searchpid;
199 volatile sig_atomic_t search_resp;
200
201 /* dialog windows */
202 double dialog_ratio = .6;
203 /* status bar */
204 #define SWM_BAR_MAX (256)
205 char *bar_argv[] = { NULL, NULL };
206 int bar_pipe[2];
207 char bar_ext[SWM_BAR_MAX];
208 char bar_vertext[SWM_BAR_MAX];
209 int bar_version = 0;
210 sig_atomic_t bar_alarm = 0;
211 int bar_delay = 30;
212 int bar_enabled = 1;
213 int bar_border_width = 1;
214 int bar_at_bottom = 0;
215 int bar_extra = 1;
216 int bar_extra_running = 0;
217 int bar_verbose = 1;
218 int bar_height = 0;
219 int stack_enabled = 1;
220 int clock_enabled = 1;
221 char *clock_format = NULL;
222 int title_name_enabled = 0;
223 int title_class_enabled = 0;
224 int window_name_enabled = 0;
225 int focus_mode = SWM_FOCUS_DEFAULT;
226 int disable_border = 0;
227 int border_width = 1;
228 pid_t bar_pid;
229 GC bar_gc;
230 XGCValues bar_gcv;
231 int bar_fidx = 0;
232 XFontStruct *bar_fs;
233 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
234 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
235
236 #define SWM_MENU_FN (2)
237 #define SWM_MENU_NB (4)
238 #define SWM_MENU_NF (6)
239 #define SWM_MENU_SB (8)
240 #define SWM_MENU_SF (10)
241
242 /* layout manager data */
243 struct swm_geometry {
244 int x;
245 int y;
246 int w;
247 int h;
248 };
249
250 struct swm_screen;
251 struct workspace;
252
253 /* virtual "screens" */
254 struct swm_region {
255 TAILQ_ENTRY(swm_region) entry;
256 struct swm_geometry g;
257 struct workspace *ws; /* current workspace on this region */
258 struct workspace *ws_prior; /* prior workspace on this region */
259 struct swm_screen *s; /* screen idx */
260 Window bar_window;
261 };
262 TAILQ_HEAD(swm_region_list, swm_region);
263
264 struct ws_win {
265 TAILQ_ENTRY(ws_win) entry;
266 Window id;
267 Window transient;
268 struct ws_win *child_trans; /* transient child window */
269 struct swm_geometry g; /* current geometry */
270 struct swm_geometry g_float; /* geometry when floating */
271 struct swm_geometry rg_float; /* region geom when floating */
272 int g_floatvalid; /* flag: geometry in g_float is valid */
273 int floatmaxed; /* flag: floater was maxed in max_stack */
274 int floating;
275 int manual;
276 int iconic;
277 unsigned int ewmh_flags;
278 int font_size_boundary[SWM_MAX_FONT_STEPS];
279 int font_steps;
280 int last_inc;
281 int can_delete;
282 int take_focus;
283 int java;
284 unsigned long quirks;
285 struct workspace *ws; /* always valid */
286 struct swm_screen *s; /* always valid, never changes */
287 XWindowAttributes wa;
288 XSizeHints sh;
289 XClassHint ch;
290 XWMHints *hints;
291 };
292 TAILQ_HEAD(ws_win_list, ws_win);
293
294 /* layout handlers */
295 void stack(void);
296 void vertical_config(struct workspace *, int);
297 void vertical_stack(struct workspace *, struct swm_geometry *);
298 void horizontal_config(struct workspace *, int);
299 void horizontal_stack(struct workspace *, struct swm_geometry *);
300 void max_stack(struct workspace *, struct swm_geometry *);
301
302 struct ws_win *find_window(Window);
303
304 void grabbuttons(struct ws_win *, int);
305 void new_region(struct swm_screen *, int, int, int, int);
306 void unmanage_window(struct ws_win *);
307 long getstate(Window);
308
309 struct layout {
310 void (*l_stack)(struct workspace *, struct swm_geometry *);
311 void (*l_config)(struct workspace *, int);
312 u_int32_t flags;
313 #define SWM_L_FOCUSPREV (1<<0)
314 #define SWM_L_MAPONFOCUS (1<<1)
315 char *name;
316 } layouts[] = {
317 /* stack, configure */
318 { vertical_stack, vertical_config, 0, "[|]" },
319 { horizontal_stack, horizontal_config, 0, "[-]" },
320 { max_stack, NULL,
321 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
322 { NULL, NULL, 0, NULL },
323 };
324
325 /* position of max_stack mode in the layouts array */
326 #define SWM_MAX_STACK 2
327
328 #define SWM_H_SLICE (32)
329 #define SWM_V_SLICE (32)
330
331 /* define work spaces */
332 struct workspace {
333 int idx; /* workspace index */
334 struct layout *cur_layout; /* current layout handlers */
335 struct ws_win *focus; /* may be NULL */
336 struct ws_win *focus_prev; /* may be NULL */
337 struct swm_region *r; /* may be NULL */
338 struct swm_region *old_r; /* may be NULL */
339 struct ws_win_list winlist; /* list of windows in ws */
340 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
341
342 /* stacker state */
343 struct {
344 int horizontal_msize;
345 int horizontal_mwin;
346 int horizontal_stacks;
347 int vertical_msize;
348 int vertical_mwin;
349 int vertical_stacks;
350 } l_state;
351 };
352
353 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
354 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
355
356 /* physical screen mapping */
357 #define SWM_WS_MAX (10)
358 struct swm_screen {
359 int idx; /* screen index */
360 struct swm_region_list rl; /* list of regions on this screen */
361 struct swm_region_list orl; /* list of old regions */
362 Window root;
363 struct workspace ws[SWM_WS_MAX];
364
365 /* colors */
366 struct {
367 unsigned long color;
368 char *name;
369 } c[SWM_S_COLOR_MAX];
370 };
371 struct swm_screen *screens;
372 int num_screens;
373
374 /* args to functions */
375 union arg {
376 int id;
377 #define SWM_ARG_ID_FOCUSNEXT (0)
378 #define SWM_ARG_ID_FOCUSPREV (1)
379 #define SWM_ARG_ID_FOCUSMAIN (2)
380 #define SWM_ARG_ID_FOCUSCUR (4)
381 #define SWM_ARG_ID_SWAPNEXT (10)
382 #define SWM_ARG_ID_SWAPPREV (11)
383 #define SWM_ARG_ID_SWAPMAIN (12)
384 #define SWM_ARG_ID_MOVELAST (13)
385 #define SWM_ARG_ID_MASTERSHRINK (20)
386 #define SWM_ARG_ID_MASTERGROW (21)
387 #define SWM_ARG_ID_MASTERADD (22)
388 #define SWM_ARG_ID_MASTERDEL (23)
389 #define SWM_ARG_ID_STACKRESET (30)
390 #define SWM_ARG_ID_STACKINIT (31)
391 #define SWM_ARG_ID_CYCLEWS_UP (40)
392 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
393 #define SWM_ARG_ID_CYCLESC_UP (42)
394 #define SWM_ARG_ID_CYCLESC_DOWN (43)
395 #define SWM_ARG_ID_STACKINC (50)
396 #define SWM_ARG_ID_STACKDEC (51)
397 #define SWM_ARG_ID_SS_ALL (60)
398 #define SWM_ARG_ID_SS_WINDOW (61)
399 #define SWM_ARG_ID_DONTCENTER (70)
400 #define SWM_ARG_ID_CENTER (71)
401 #define SWM_ARG_ID_KILLWINDOW (80)
402 #define SWM_ARG_ID_DELETEWINDOW (81)
403 char **argv;
404 };
405
406 void focus(struct swm_region *, union arg *);
407 void focus_magic(struct ws_win *);
408
409 /* quirks */
410 struct quirk {
411 char *class;
412 char *name;
413 unsigned long quirk;
414 #define SWM_Q_FLOAT (1<<0) /* float this window */
415 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
416 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
417 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
418 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
419 #define SWM_Q_FOCUSPREV (1<<5) /* focus on caller */
420 };
421 int quirks_size = 0, quirks_length = 0;
422 struct quirk *quirks = NULL;
423
424 /*
425 * Supported EWMH hints should be added to
426 * both the enum and the ewmh array
427 */
428 enum { _NET_ACTIVE_WINDOW, _NET_MOVERESIZE_WINDOW, _NET_CLOSE_WINDOW,
429 _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DOCK,
430 _NET_WM_WINDOW_TYPE_TOOLBAR, _NET_WM_WINDOW_TYPE_UTILITY,
431 _NET_WM_WINDOW_TYPE_SPLASH, _NET_WM_WINDOW_TYPE_DIALOG,
432 _NET_WM_WINDOW_TYPE_NORMAL, _NET_WM_STATE,
433 _NET_WM_STATE_MAXIMIZED_HORZ, _NET_WM_STATE_MAXIMIZED_VERT,
434 _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_SKIP_PAGER,
435 _NET_WM_STATE_HIDDEN, _NET_WM_STATE_ABOVE, _SWM_WM_STATE_MANUAL,
436 _NET_WM_STATE_FULLSCREEN, _NET_WM_ALLOWED_ACTIONS, _NET_WM_ACTION_MOVE,
437 _NET_WM_ACTION_RESIZE, _NET_WM_ACTION_FULLSCREEN, _NET_WM_ACTION_CLOSE,
438 SWM_EWMH_HINT_MAX };
439
440 struct ewmh_hint {
441 char *name;
442 Atom atom;
443 } ewmh[SWM_EWMH_HINT_MAX] = {
444 /* must be in same order as in the enum */
445 {"_NET_ACTIVE_WINDOW", None},
446 {"_NET_MOVERESIZE_WINDOW", None},
447 {"_NET_CLOSE_WINDOW", None},
448 {"_NET_WM_WINDOW_TYPE", None},
449 {"_NET_WM_WINDOW_TYPE_DOCK", None},
450 {"_NET_WM_WINDOW_TYPE_TOOLBAR", None},
451 {"_NET_WM_WINDOW_TYPE_UTILITY", None},
452 {"_NET_WM_WINDOW_TYPE_SPLASH", None},
453 {"_NET_WM_WINDOW_TYPE_DIALOG", None},
454 {"_NET_WM_WINDOW_TYPE_NORMAL", None},
455 {"_NET_WM_STATE", None},
456 {"_NET_WM_STATE_MAXIMIZED_HORZ", None},
457 {"_NET_WM_STATE_MAXIMIZED_VERT", None},
458 {"_NET_WM_STATE_SKIP_TASKBAR", None},
459 {"_NET_WM_STATE_SKIP_PAGER", None},
460 {"_NET_WM_STATE_HIDDEN", None},
461 {"_NET_WM_STATE_ABOVE", None},
462 {"_SWM_WM_STATE_MANUAL", None},
463 {"_NET_WM_STATE_FULLSCREEN", None},
464 {"_NET_WM_ALLOWED_ACTIONS", None},
465 {"_NET_WM_ACTION_MOVE", None},
466 {"_NET_WM_ACTION_RESIZE", None},
467 {"_NET_WM_ACTION_FULLSCREEN", None},
468 {"_NET_WM_ACTION_CLOSE", None},
469 };
470
471 void store_float_geom(struct ws_win *win, struct swm_region *r);
472 int floating_toggle_win(struct ws_win *win);
473 void spawn_select(struct swm_region *, union arg *, char *, int *);
474
475 int
476 get_property(Window id, Atom atom, long count, Atom type,
477 unsigned long *n, unsigned char **data)
478 {
479 int format, status;
480 unsigned long tmp, extra;
481 unsigned long *nitems;
482 Atom real;
483
484 nitems = n != NULL ? n : &tmp;
485 status = XGetWindowProperty(display, id, atom, 0L, count, False, type,
486 &real, &format, nitems, &extra, data);
487
488 if (status != Success)
489 return False;
490 if (real != type)
491 return False;
492
493 return True;
494 }
495
496 void
497 update_iconic(struct ws_win *win, int newv)
498 {
499 int32_t v = newv;
500 Atom iprop;
501
502 win->iconic = newv;
503
504 iprop = XInternAtom(display, "_SWM_ICONIC", False);
505 if (!iprop)
506 return;
507 if (newv)
508 XChangeProperty(display, win->id, iprop, XA_INTEGER, 32,
509 PropModeReplace, (unsigned char *)&v, 1);
510 else
511 XDeleteProperty(display, win->id, iprop);
512 }
513
514 int
515 get_iconic(struct ws_win *win)
516 {
517 int32_t v = 0;
518 int retfmt, status;
519 Atom iprop, rettype;
520 unsigned long nitems, extra;
521 unsigned char *prop = NULL;
522
523 iprop = XInternAtom(display, "_SWM_ICONIC", False);
524 if (!iprop)
525 goto out;
526 status = XGetWindowProperty(display, win->id, iprop, 0L, 1L,
527 False, XA_INTEGER, &rettype, &retfmt, &nitems, &extra, &prop);
528 if (status != Success)
529 goto out;
530 if (rettype != XA_INTEGER || retfmt != 32)
531 goto out;
532 if (nitems != 1)
533 goto out;
534 v = *((int32_t *)prop);
535
536 out:
537 if (prop != NULL)
538 XFree(prop);
539 return (v);
540 }
541
542 void
543 setup_ewmh(void)
544 {
545 int i,j;
546 Atom sup_list;
547
548 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
549
550 for (i = 0; i < LENGTH(ewmh); i++)
551 ewmh[i].atom = XInternAtom(display, ewmh[i].name, False);
552
553 for (i = 0; i < ScreenCount(display); i++) {
554 /* Support check window will be created by workaround(). */
555
556 /* Report supported atoms */
557 XDeleteProperty(display, screens[i].root, sup_list);
558 for (j = 0; j < LENGTH(ewmh); j++)
559 XChangeProperty(display, screens[i].root, sup_list, XA_ATOM, 32,
560 PropModeAppend, (unsigned char *)&ewmh[j].atom,1);
561 }
562 }
563
564 void
565 teardown_ewmh(void)
566 {
567 int i, success;
568 unsigned char *data = NULL;
569 unsigned long n;
570 Atom sup_check, sup_list;
571 Window id;
572
573 sup_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
574 sup_list = XInternAtom(display, "_NET_SUPPORTED", False);
575
576 for (i = 0; i < ScreenCount(display); i++) {
577 /* Get the support check window and destroy it */
578 success = get_property(screens[i].root, sup_check, 1, XA_WINDOW,
579 &n, &data);
580
581 if (success) {
582 id = data[0];
583 XDestroyWindow(display, id);
584 XDeleteProperty(display, screens[i].root, sup_check);
585 XDeleteProperty(display, screens[i].root, sup_list);
586 }
587
588 XFree(data);
589 }
590 }
591
592 void
593 ewmh_autoquirk(struct ws_win *win)
594 {
595 int success, i;
596 unsigned long *data = NULL;
597 unsigned long n;
598 Atom type;
599
600 success = get_property(win->id, ewmh[_NET_WM_WINDOW_TYPE].atom, (~0L),
601 XA_ATOM, &n, (unsigned char **)&data);
602
603 if (!success) {
604 XFree(data);
605 return;
606 }
607
608 for (i = 0; i < n; i++) {
609 type = data[i];
610 if (type == ewmh[_NET_WM_WINDOW_TYPE_NORMAL].atom)
611 break;
612 if (type == ewmh[_NET_WM_WINDOW_TYPE_DOCK].atom ||
613 type == ewmh[_NET_WM_WINDOW_TYPE_TOOLBAR].atom ||
614 type == ewmh[_NET_WM_WINDOW_TYPE_UTILITY].atom) {
615 win->floating = 1;
616 win->quirks = SWM_Q_FLOAT | SWM_Q_ANYWHERE;
617 break;
618 }
619 if (type == ewmh[_NET_WM_WINDOW_TYPE_SPLASH].atom ||
620 type == ewmh[_NET_WM_WINDOW_TYPE_DIALOG].atom) {
621 win->floating = 1;
622 win->quirks = SWM_Q_FLOAT;
623 break;
624 }
625 }
626
627 XFree(data);
628 }
629
630 #define SWM_EWMH_ACTION_COUNT_MAX (6)
631 #define EWMH_F_FULLSCREEN (1<<0)
632 #define EWMH_F_ABOVE (1<<1)
633 #define EWMH_F_HIDDEN (1<<2)
634 #define EWMH_F_SKIP_PAGER (1<<3)
635 #define EWMH_F_SKIP_TASKBAR (1<<4)
636 #define SWM_F_MANUAL (1<<5)
637
638 int
639 ewmh_set_win_fullscreen(struct ws_win *win, int fs)
640 {
641 struct swm_geometry rg;
642
643 if (!win->ws->r)
644 return 0;
645
646 if (!win->floating)
647 return 0;
648
649 DNPRINTF(SWM_D_MISC, "ewmh_set_win_fullscreen: win 0x%lx fs: %d\n",
650 win->id, fs);
651
652 rg = win->ws->r->g;
653
654 if (fs) {
655 store_float_geom(win, win->ws->r);
656
657 win->g.x = rg.x;
658 win->g.y = rg.y;
659 win->g.w = rg.w;
660 win->g.h = rg.h;
661 } else {
662 if (win->g_floatvalid) {
663 /* refloat at last floating relative position */
664 win->g.x = win->g_float.x - win->rg_float.x + rg.x;
665 win->g.y = win->g_float.y - win->rg_float.y + rg.y;
666 win->g.w = win->g_float.w;
667 win->g.h = win->g_float.h;
668 }
669 }
670
671 return 1;
672 }
673
674 void
675 ewmh_update_actions(struct ws_win *win)
676 {
677 Atom actions[SWM_EWMH_ACTION_COUNT_MAX];
678 int n = 0;
679
680 if (win == NULL)
681 return;
682
683 actions[n++] = ewmh[_NET_WM_ACTION_CLOSE].atom;
684
685 if (win->floating) {
686 actions[n++] = ewmh[_NET_WM_ACTION_MOVE].atom;
687 actions[n++] = ewmh[_NET_WM_ACTION_RESIZE].atom;
688 }
689
690 XChangeProperty(display, win->id, ewmh[_NET_WM_ALLOWED_ACTIONS].atom,
691 XA_ATOM, 32, PropModeReplace, (unsigned char *)actions, n);
692 }
693
694 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
695 #define _NET_WM_STATE_ADD 1 /* add/set property */
696 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
697
698 void
699 ewmh_update_win_state(struct ws_win *win, long state, long action)
700 {
701 unsigned int mask = 0;
702 unsigned int changed = 0;
703 unsigned int orig_flags;
704
705 if (win == NULL)
706 return;
707
708 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
709 mask = EWMH_F_FULLSCREEN;
710 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
711 mask = EWMH_F_ABOVE;
712 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
713 mask = SWM_F_MANUAL;
714 if (state == ewmh[_NET_WM_STATE_SKIP_PAGER].atom)
715 mask = EWMH_F_SKIP_PAGER;
716 if (state == ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom)
717 mask = EWMH_F_SKIP_TASKBAR;
718
719
720 orig_flags = win->ewmh_flags;
721
722 switch (action) {
723 case _NET_WM_STATE_REMOVE:
724 win->ewmh_flags &= ~mask;
725 break;
726 case _NET_WM_STATE_ADD:
727 win->ewmh_flags |= mask;
728 break;
729 case _NET_WM_STATE_TOGGLE:
730 win->ewmh_flags ^= mask;
731 break;
732 }
733
734 changed = (win->ewmh_flags & mask) ^ (orig_flags & mask) ? 1 : 0;
735
736 if (state == ewmh[_NET_WM_STATE_ABOVE].atom)
737 if (changed)
738 if (!floating_toggle_win(win))
739 win->ewmh_flags = orig_flags; /* revert */
740 if (state == ewmh[_SWM_WM_STATE_MANUAL].atom)
741 if (changed)
742 win->manual = (win->ewmh_flags & SWM_F_MANUAL) != 0;
743 if (state == ewmh[_NET_WM_STATE_FULLSCREEN].atom)
744 if (changed)
745 if (!ewmh_set_win_fullscreen(win, win->ewmh_flags & EWMH_F_FULLSCREEN))
746 win->ewmh_flags = orig_flags; /* revert */
747
748 XDeleteProperty(display, win->id, ewmh[_NET_WM_STATE].atom);
749
750 if (win->ewmh_flags & EWMH_F_FULLSCREEN)
751 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
752 XA_ATOM, 32, PropModeAppend,
753 (unsigned char *)&ewmh[_NET_WM_STATE_FULLSCREEN].atom, 1);
754 if (win->ewmh_flags & EWMH_F_SKIP_PAGER)
755 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
756 XA_ATOM, 32, PropModeAppend,
757 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_PAGER].atom, 1);
758 if (win->ewmh_flags & EWMH_F_SKIP_TASKBAR)
759 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
760 XA_ATOM, 32, PropModeAppend,
761 (unsigned char *)&ewmh[_NET_WM_STATE_SKIP_TASKBAR].atom, 1);
762 if (win->ewmh_flags & EWMH_F_ABOVE)
763 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
764 XA_ATOM, 32, PropModeAppend,
765 (unsigned char *)&ewmh[_NET_WM_STATE_ABOVE].atom, 1);
766 if (win->ewmh_flags & SWM_F_MANUAL)
767 XChangeProperty(display, win->id, ewmh[_NET_WM_STATE].atom,
768 XA_ATOM, 32, PropModeAppend,
769 (unsigned char *)&ewmh[_SWM_WM_STATE_MANUAL].atom, 1);
770 }
771
772 void
773 ewmh_get_win_state(struct ws_win *win)
774 {
775 int success, i;
776 unsigned long n;
777 Atom *states;
778
779 if (win == NULL)
780 return;
781
782 win->ewmh_flags = 0;
783 if (win->floating)
784 win->ewmh_flags |= EWMH_F_ABOVE;
785 if (win->manual)
786 win->ewmh_flags |= SWM_F_MANUAL;
787
788 success = get_property(win->id, ewmh[_NET_WM_STATE].atom, (~0L), XA_ATOM,
789 &n, (unsigned char **)&states);
790
791 if (!success)
792 return;
793
794 for (i = 0; i < n; i++)
795 ewmh_update_win_state(win, states[i], _NET_WM_STATE_ADD);
796
797 XFree(states);
798 }
799
800 /* events */
801 #ifdef SWM_DEBUG
802 void
803 dumpevent(XEvent *e)
804 {
805 char *name = NULL;
806
807 switch (e->type) {
808 case KeyPress:
809 name = "KeyPress";
810 break;
811 case KeyRelease:
812 name = "KeyRelease";
813 break;
814 case ButtonPress:
815 name = "ButtonPress";
816 break;
817 case ButtonRelease:
818 name = "ButtonRelease";
819 break;
820 case MotionNotify:
821 name = "MotionNotify";
822 break;
823 case EnterNotify:
824 name = "EnterNotify";
825 break;
826 case LeaveNotify:
827 name = "LeaveNotify";
828 break;
829 case FocusIn:
830 name = "FocusIn";
831 break;
832 case FocusOut:
833 name = "FocusOut";
834 break;
835 case KeymapNotify:
836 name = "KeymapNotify";
837 break;
838 case Expose:
839 name = "Expose";
840 break;
841 case GraphicsExpose:
842 name = "GraphicsExpose";
843 break;
844 case NoExpose:
845 name = "NoExpose";
846 break;
847 case VisibilityNotify:
848 name = "VisibilityNotify";
849 break;
850 case CreateNotify:
851 name = "CreateNotify";
852 break;
853 case DestroyNotify:
854 name = "DestroyNotify";
855 break;
856 case UnmapNotify:
857 name = "UnmapNotify";
858 break;
859 case MapNotify:
860 name = "MapNotify";
861 break;
862 case MapRequest:
863 name = "MapRequest";
864 break;
865 case ReparentNotify:
866 name = "ReparentNotify";
867 break;
868 case ConfigureNotify:
869 name = "ConfigureNotify";
870 break;
871 case ConfigureRequest:
872 name = "ConfigureRequest";
873 break;
874 case GravityNotify:
875 name = "GravityNotify";
876 break;
877 case ResizeRequest:
878 name = "ResizeRequest";
879 break;
880 case CirculateNotify:
881 name = "CirculateNotify";
882 break;
883 case CirculateRequest:
884 name = "CirculateRequest";
885 break;
886 case PropertyNotify:
887 name = "PropertyNotify";
888 break;
889 case SelectionClear:
890 name = "SelectionClear";
891 break;
892 case SelectionRequest:
893 name = "SelectionRequest";
894 break;
895 case SelectionNotify:
896 name = "SelectionNotify";
897 break;
898 case ColormapNotify:
899 name = "ColormapNotify";
900 break;
901 case ClientMessage:
902 name = "ClientMessage";
903 break;
904 case MappingNotify:
905 name = "MappingNotify";
906 break;
907 }
908
909 if (name)
910 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
911 "remaining\n",
912 e->xany.window, name, e->type, QLength(display));
913 else
914 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
915 "remaining\n",
916 e->xany.window, e->type, QLength(display));
917 }
918
919 void
920 dumpwins(struct swm_region *r, union arg *args)
921 {
922 struct ws_win *win;
923 unsigned int state;
924 XWindowAttributes wa;
925
926 if (r->ws == NULL) {
927 fprintf(stderr, "invalid workspace\n");
928 return;
929 }
930
931 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
932
933 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
934 state = getstate(win->id);
935 if (!XGetWindowAttributes(display, win->id, &wa))
936 fprintf(stderr, "window: %lu failed "
937 "XGetWindowAttributes\n", win->id);
938 fprintf(stderr, "window: %lu map_state: %d state: %d "
939 "transient: %lu\n",
940 win->id, wa.map_state, state, win->transient);
941 }
942
943 fprintf(stderr, "===== unmanaged window list =====\n");
944 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
945 state = getstate(win->id);
946 if (!XGetWindowAttributes(display, win->id, &wa))
947 fprintf(stderr, "window: %lu failed "
948 "XGetWindowAttributes\n", win->id);
949 fprintf(stderr, "window: %lu map_state: %d state: %d "
950 "transient: %lu\n",
951 win->id, wa.map_state, state, win->transient);
952 }
953
954 fprintf(stderr, "=================================\n");
955 }
956 #else
957 #define dumpevent(e)
958 void
959 dumpwins(struct swm_region *r, union arg *args)
960 {
961 }
962 #endif /* SWM_DEBUG */
963
964 void expose(XEvent *);
965 void keypress(XEvent *);
966 void buttonpress(XEvent *);
967 void configurerequest(XEvent *);
968 void configurenotify(XEvent *);
969 void destroynotify(XEvent *);
970 void enternotify(XEvent *);
971 void focusevent(XEvent *);
972 void mapnotify(XEvent *);
973 void mappingnotify(XEvent *);
974 void maprequest(XEvent *);
975 void propertynotify(XEvent *);
976 void unmapnotify(XEvent *);
977 void visibilitynotify(XEvent *);
978 void clientmessage(XEvent *);
979
980 void (*handler[LASTEvent])(XEvent *) = {
981 [Expose] = expose,
982 [KeyPress] = keypress,
983 [ButtonPress] = buttonpress,
984 [ConfigureRequest] = configurerequest,
985 [ConfigureNotify] = configurenotify,
986 [DestroyNotify] = destroynotify,
987 [EnterNotify] = enternotify,
988 [FocusIn] = focusevent,
989 [FocusOut] = focusevent,
990 [MapNotify] = mapnotify,
991 [MappingNotify] = mappingnotify,
992 [MapRequest] = maprequest,
993 [PropertyNotify] = propertynotify,
994 [UnmapNotify] = unmapnotify,
995 [VisibilityNotify] = visibilitynotify,
996 [ClientMessage] = clientmessage,
997 };
998
999 void
1000 sighdlr(int sig)
1001 {
1002 int saved_errno, status;
1003 pid_t pid;
1004
1005 saved_errno = errno;
1006
1007 switch (sig) {
1008 case SIGCHLD:
1009 while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) != 0) {
1010 if (pid == -1) {
1011 if (errno == EINTR)
1012 continue;
1013 #ifdef SWM_DEBUG
1014 if (errno != ECHILD)
1015 warn("sighdlr: waitpid");
1016 #endif /* SWM_DEBUG */
1017 break;
1018 }
1019 if (pid == searchpid)
1020 search_resp = 1;
1021
1022 #ifdef SWM_DEBUG
1023 if (WIFEXITED(status)) {
1024 if (WEXITSTATUS(status) != 0)
1025 warnx("sighdlr: child exit status: %d",
1026 WEXITSTATUS(status));
1027 } else
1028 warnx("sighdlr: child is terminated "
1029 "abnormally");
1030 #endif /* SWM_DEBUG */
1031 }
1032 break;
1033
1034 case SIGHUP:
1035 restart_wm = 1;
1036 break;
1037 case SIGINT:
1038 case SIGTERM:
1039 case SIGQUIT:
1040 running = 0;
1041 break;
1042 }
1043
1044 errno = saved_errno;
1045 }
1046
1047 unsigned long
1048 name_to_color(char *colorname)
1049 {
1050 Colormap cmap;
1051 Status status;
1052 XColor screen_def, exact_def;
1053 unsigned long result = 0;
1054 char cname[32] = "#";
1055
1056 cmap = DefaultColormap(display, screens[0].idx);
1057 status = XAllocNamedColor(display, cmap, colorname,
1058 &screen_def, &exact_def);
1059 if (!status) {
1060 strlcat(cname, colorname + 2, sizeof cname - 1);
1061 status = XAllocNamedColor(display, cmap, cname, &screen_def,
1062 &exact_def);
1063 }
1064 if (status)
1065 result = screen_def.pixel;
1066 else
1067 fprintf(stderr, "color '%s' not found.\n", colorname);
1068
1069 return (result);
1070 }
1071
1072 void
1073 setscreencolor(char *val, int i, int c)
1074 {
1075 if (i > 0 && i <= ScreenCount(display)) {
1076 screens[i - 1].c[c].color = name_to_color(val);
1077 free(screens[i - 1].c[c].name);
1078 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
1079 errx(1, "strdup");
1080 } else if (i == -1) {
1081 for (i = 0; i < ScreenCount(display); i++) {
1082 screens[i].c[c].color = name_to_color(val);
1083 free(screens[i].c[c].name);
1084 if ((screens[i].c[c].name = strdup(val)) == NULL)
1085 errx(1, "strdup");
1086 }
1087 } else
1088 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1089 i, ScreenCount(display));
1090 }
1091
1092 void
1093 custom_region(char *val)
1094 {
1095 unsigned int sidx, x, y, w, h;
1096
1097 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
1098 errx(1, "invalid custom region, "
1099 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
1100 if (sidx < 1 || sidx > ScreenCount(display))
1101 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
1102 sidx, ScreenCount(display));
1103 sidx--;
1104
1105 if (w < 1 || h < 1)
1106 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
1107
1108 if (x < 0 || x > DisplayWidth(display, sidx) ||
1109 y < 0 || y > DisplayHeight(display, sidx) ||
1110 w + x > DisplayWidth(display, sidx) ||
1111 h + y > DisplayHeight(display, sidx)) {
1112 fprintf(stderr, "ignoring region %ux%u+%u+%u - not within screen boundaries "
1113 "(%ux%u)\n", w, h, x, y,
1114 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
1115 return;
1116 }
1117
1118 new_region(&screens[sidx], x, y, w, h);
1119 }
1120
1121 void
1122 socket_setnonblock(int fd)
1123 {
1124 int flags;
1125
1126 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
1127 err(1, "fcntl F_GETFL");
1128 flags |= O_NONBLOCK;
1129 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
1130 err(1, "fcntl F_SETFL");
1131 }
1132
1133 void
1134 bar_print(struct swm_region *r, char *s)
1135 {
1136 XClearWindow(display, r->bar_window);
1137 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
1138 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
1139 strlen(s));
1140 }
1141
1142 void
1143 bar_extra_stop(void)
1144 {
1145 if (bar_pipe[0]) {
1146 close(bar_pipe[0]);
1147 bzero(bar_pipe, sizeof bar_pipe);
1148 }
1149 if (bar_pid) {
1150 kill(bar_pid, SIGTERM);
1151 bar_pid = 0;
1152 }
1153 strlcpy(bar_ext, "", sizeof bar_ext);
1154 bar_extra = 0;
1155 }
1156
1157 void
1158 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1159 {
1160 int do_class, do_name;
1161 Status status;
1162 XClassHint *xch = NULL;
1163
1164 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
1165 cur_focus != NULL) {
1166 if ((xch = XAllocClassHint()) == NULL)
1167 goto out;
1168 status = XGetClassHint(display, cur_focus->id, xch);
1169 if (status == BadWindow || status == BadAlloc)
1170 goto out;
1171 do_class = (title_class_enabled && xch->res_class != NULL);
1172 do_name = (title_name_enabled && xch->res_name != NULL);
1173 if (do_class)
1174 strlcat(s, xch->res_class, sz);
1175 if (do_class && do_name)
1176 strlcat(s, ":", sz);
1177 if (do_name)
1178 strlcat(s, xch->res_name, sz);
1179 strlcat(s, " ", sz);
1180 }
1181 out:
1182 if (xch)
1183 XFree(xch);
1184 }
1185
1186 void
1187 bar_window_name(char *s, ssize_t sz, struct ws_win *cur_focus)
1188 {
1189 char *title;
1190
1191 if (window_name_enabled && cur_focus != NULL) {
1192 XFetchName(display, cur_focus->id, &title);
1193 if (title) {
1194 if (cur_focus->floating)
1195 strlcat(s, "(f) ", sz);
1196 strlcat(s, title, sz);
1197 strlcat(s, " ", sz);
1198 XFree(title);
1199 }
1200 }
1201 }
1202
1203 void
1204 bar_update(void)
1205 {
1206 time_t tmt;
1207 struct tm tm;
1208 struct swm_region *r;
1209 int i, x;
1210 size_t len;
1211 char s[SWM_BAR_MAX];
1212 char cn[SWM_BAR_MAX];
1213 char loc[SWM_BAR_MAX];
1214 char *b;
1215 char *stack = "";
1216
1217 if (bar_enabled == 0)
1218 return;
1219 if (bar_extra && bar_extra_running) {
1220 /* ignore short reads; it'll correct itself */
1221 while ((b = fgetln(stdin, &len)) != NULL)
1222 if (b && b[len - 1] == '\n') {
1223 b[len - 1] = '\0';
1224 strlcpy(bar_ext, b, sizeof bar_ext);
1225 }
1226 if (b == NULL && errno != EAGAIN) {
1227 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
1228 errno, strerror(errno));
1229 bar_extra_stop();
1230 }
1231 } else
1232 strlcpy(bar_ext, "", sizeof bar_ext);
1233
1234 if (clock_enabled == 0)
1235 strlcpy(s, "", sizeof s);
1236 else {
1237 time(&tmt);
1238 localtime_r(&tmt, &tm);
1239 strftime(s, sizeof s, clock_format, &tm);
1240 strlcat(s, " ", sizeof s);
1241 }
1242
1243 for (i = 0; i < ScreenCount(display); i++) {
1244 x = 1;
1245 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1246 strlcpy(cn, "", sizeof cn);
1247 if (r && r->ws) {
1248 bar_class_name(cn, sizeof cn, r->ws->focus);
1249 bar_window_name(cn, sizeof cn, r->ws->focus);
1250 }
1251
1252 if (stack_enabled)
1253 stack = r->ws->cur_layout->name;
1254
1255 snprintf(loc, sizeof loc, "%d:%d %s %s%s %s %s",
1256 x++, r->ws->idx + 1, stack, s, cn, bar_ext,
1257 bar_vertext);
1258 bar_print(r, loc);
1259 }
1260 }
1261 alarm(bar_delay);
1262 }
1263
1264 void
1265 bar_signal(int sig)
1266 {
1267 bar_alarm = 1;
1268 }
1269
1270 void
1271 bar_toggle(struct swm_region *r, union arg *args)
1272 {
1273 struct swm_region *tmpr;
1274 int i, sc = ScreenCount(display);
1275
1276 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
1277
1278 if (bar_enabled)
1279 for (i = 0; i < sc; i++)
1280 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1281 XUnmapWindow(display, tmpr->bar_window);
1282 else
1283 for (i = 0; i < sc; i++)
1284 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
1285 XMapRaised(display, tmpr->bar_window);
1286
1287 bar_enabled = !bar_enabled;
1288
1289 stack();
1290 /* must be after stack */
1291 bar_update();
1292 }
1293
1294 void
1295 bar_refresh(void)
1296 {
1297 XSetWindowAttributes wa;
1298 struct swm_region *r;
1299 int i;
1300
1301 /* do this here because the conf file is in memory */
1302 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
1303 /* launch external status app */
1304 bar_extra_running = 1;
1305 if (pipe(bar_pipe) == -1)
1306 err(1, "pipe error");
1307 socket_setnonblock(bar_pipe[0]);
1308 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
1309 if (dup2(bar_pipe[0], 0) == -1)
1310 errx(1, "dup2");
1311 if (dup2(bar_pipe[1], 1) == -1)
1312 errx(1, "dup2");
1313 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1314 err(1, "could not disable SIGPIPE");
1315 switch (bar_pid = fork()) {
1316 case -1:
1317 err(1, "cannot fork");
1318 break;
1319 case 0: /* child */
1320 close(bar_pipe[0]);
1321 execvp(bar_argv[0], bar_argv);
1322 err(1, "%s external app failed", bar_argv[0]);
1323 break;
1324 default: /* parent */
1325 close(bar_pipe[1]);
1326 break;
1327 }
1328 }
1329
1330 bzero(&wa, sizeof wa);
1331 for (i = 0; i < ScreenCount(display); i++)
1332 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1333 wa.border_pixel =
1334 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
1335 wa.background_pixel =
1336 screens[i].c[SWM_S_COLOR_BAR].color;
1337 XChangeWindowAttributes(display, r->bar_window,
1338 CWBackPixel | CWBorderPixel, &wa);
1339 }
1340 bar_update();
1341 }
1342
1343 void
1344 bar_setup(struct swm_region *r)
1345 {
1346 int i, x, y;
1347
1348 if (bar_fs) {
1349 XFreeFont(display, bar_fs);
1350 bar_fs = NULL;
1351 }
1352
1353 for (i = 0; bar_fonts[i] != NULL; i++) {
1354 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
1355 if (bar_fs) {
1356 bar_fidx = i;
1357 break;
1358 }
1359 }
1360 if (bar_fonts[i] == NULL)
1361 errx(1, "couldn't load font");
1362 if (bar_fs == NULL)
1363 errx(1, "couldn't create font structure");
1364
1365 bar_height = bar_fs->ascent + bar_fs->descent + 1 + 2 * bar_border_width;
1366 x = X(r);
1367 y = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
1368
1369 r->bar_window = XCreateSimpleWindow(display,
1370 r->s->root, x, y, WIDTH(r) - 2 * bar_border_width, bar_height - 2 * bar_border_width,
1371 bar_border_width, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
1372 r->s->c[SWM_S_COLOR_BAR].color);
1373 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
1374 XSetFont(display, bar_gc, bar_fs->fid);
1375 XSelectInput(display, r->bar_window, VisibilityChangeMask);
1376 if (bar_enabled)
1377 XMapRaised(display, r->bar_window);
1378 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
1379
1380 if (signal(SIGALRM, bar_signal) == SIG_ERR)
1381 err(1, "could not install bar_signal");
1382 bar_refresh();
1383 }
1384
1385 void
1386 drain_enter_notify(void)
1387 {
1388 int i = 0;
1389 XEvent cne;
1390
1391 while (XCheckMaskEvent(display, EnterWindowMask, &cne))
1392 i++;
1393
1394 DNPRINTF(SWM_D_MISC, "drain_enter_notify: drained %d\n", i);
1395 }
1396
1397 void
1398 set_win_state(struct ws_win *win, long state)
1399 {
1400 long data[] = {state, None};
1401
1402 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
1403
1404 if (win == NULL)
1405 return;
1406
1407 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
1408 (unsigned char *)data, 2);
1409 }
1410
1411 long
1412 getstate(Window w)
1413 {
1414 long result = -1;
1415 unsigned char *p = NULL;
1416 unsigned long n;
1417
1418 if (!get_property(w, astate, 2L, astate, &n, &p))
1419 return (-1);
1420 if (n != 0)
1421 result = *((long *)p);
1422 XFree(p);
1423 return (result);
1424 }
1425
1426 void
1427 version(struct swm_region *r, union arg *args)
1428 {
1429 bar_version = !bar_version;
1430 if (bar_version)
1431 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1432 SWM_VERSION, cvstag);
1433 else
1434 strlcpy(bar_vertext, "", sizeof bar_vertext);
1435 bar_update();
1436 }
1437
1438 void
1439 client_msg(struct ws_win *win, Atom a)
1440 {
1441 XClientMessageEvent cm;
1442
1443 if (win == NULL)
1444 return;
1445
1446 bzero(&cm, sizeof cm);
1447 cm.type = ClientMessage;
1448 cm.window = win->id;
1449 cm.message_type = aprot;
1450 cm.format = 32;
1451 cm.data.l[0] = a;
1452 cm.data.l[1] = CurrentTime;
1453 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1454 }
1455
1456 void
1457 config_win(struct ws_win *win, XConfigureRequestEvent *ev)
1458 {
1459 XConfigureEvent ce;
1460
1461 if (win == NULL)
1462 return;
1463
1464 if (ev == NULL) {
1465 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1466 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1467
1468 ce.type = ConfigureNotify;
1469 ce.display = display;
1470 ce.event = win->id;
1471 ce.window = win->id;
1472 ce.x = win->g.x;
1473 ce.y = win->g.y;
1474 ce.width = win->g.w;
1475 ce.height = win->g.h;
1476 ce.border_width = border_width;
1477 ce.above = None;
1478 ce.override_redirect = False;
1479 } else {
1480 DNPRINTF(SWM_D_MISC, "config_win: ev win %lu x %d y %d w %d h %d\n",
1481 ev->window, ev->x, ev->y, ev->width, ev->height);
1482 ce.type = ConfigureNotify;
1483 ce.display = ev->display;
1484 ce.event = ev->window;
1485 ce.window = ev->window;
1486 ce.x = ev->x;
1487 ce.y = ev->y;
1488 ce.width = ev->width;
1489 ce.height = ev->height;
1490 ce.border_width = ev->border_width;
1491 ce.above = ev->above;
1492 ce.override_redirect = False;
1493 }
1494
1495 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1496 }
1497
1498 int
1499 count_win(struct workspace *ws, int count_transient)
1500 {
1501 struct ws_win *win;
1502 int count = 0;
1503
1504 TAILQ_FOREACH(win, &ws->winlist, entry) {
1505 if (count_transient == 0 && win->floating)
1506 continue;
1507 if (count_transient == 0 && win->transient)
1508 continue;
1509 if (win->iconic)
1510 continue;
1511 count++;
1512 }
1513 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1514
1515 return (count);
1516 }
1517
1518 void
1519 quit(struct swm_region *r, union arg *args)
1520 {
1521 DNPRINTF(SWM_D_MISC, "quit\n");
1522 running = 0;
1523 }
1524
1525 void
1526 unmap_window(struct ws_win *win)
1527 {
1528 if (win == NULL)
1529 return;
1530
1531 /* don't unmap again */
1532 if (getstate(win->id) == IconicState)
1533 return;
1534
1535 set_win_state(win, IconicState);
1536
1537 XUnmapWindow(display, win->id);
1538 XSetWindowBorder(display, win->id,
1539 win->s->c[SWM_S_COLOR_UNFOCUS].color);
1540 }
1541
1542 void
1543 unmap_all(void)
1544 {
1545 struct ws_win *win;
1546 int i, j;
1547
1548 for (i = 0; i < ScreenCount(display); i++)
1549 for (j = 0; j < SWM_WS_MAX; j++)
1550 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1551 unmap_window(win);
1552 }
1553
1554 void
1555 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1556 {
1557 XKeyEvent event;
1558
1559 if (win == NULL)
1560 return;
1561
1562 event.display = display; /* Ignored, but what the hell */
1563 event.window = win->id;
1564 event.root = win->s->root;
1565 event.subwindow = None;
1566 event.time = CurrentTime;
1567 event.x = win->g.x;
1568 event.y = win->g.y;
1569 event.x_root = 1;
1570 event.y_root = 1;
1571 event.same_screen = True;
1572 event.keycode = XKeysymToKeycode(display, keysym);
1573 event.state = modifiers;
1574
1575 event.type = KeyPress;
1576 XSendEvent(event.display, event.window, True,
1577 KeyPressMask, (XEvent *)&event);
1578
1579 event.type = KeyRelease;
1580 XSendEvent(event.display, event.window, True,
1581 KeyPressMask, (XEvent *)&event);
1582
1583 }
1584
1585 void
1586 restart(struct swm_region *r, union arg *args)
1587 {
1588 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1589
1590 /* disable alarm because the following code may not be interrupted */
1591 alarm(0);
1592 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1593 errx(1, "can't disable alarm");
1594
1595 bar_extra_stop();
1596 bar_extra = 1;
1597 unmap_all();
1598 XCloseDisplay(display);
1599 execvp(start_argv[0], start_argv);
1600 fprintf(stderr, "execvp failed\n");
1601 perror(" failed");
1602 quit(NULL, NULL);
1603 }
1604
1605 struct swm_region *
1606 root_to_region(Window root)
1607 {
1608 struct swm_region *r = NULL;
1609 Window rr, cr;
1610 int i, x, y, wx, wy;
1611 unsigned int mask;
1612
1613 for (i = 0; i < ScreenCount(display); i++)
1614 if (screens[i].root == root)
1615 break;
1616
1617 if (XQueryPointer(display, screens[i].root,
1618 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1619 /* choose a region based on pointer location */
1620 TAILQ_FOREACH(r, &screens[i].rl, entry)
1621 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1622 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1623 break;
1624 }
1625
1626 if (r == NULL)
1627 r = TAILQ_FIRST(&screens[i].rl);
1628
1629 return (r);
1630 }
1631
1632 struct ws_win *
1633 find_unmanaged_window(Window id)
1634 {
1635 struct ws_win *win;
1636 int i, j;
1637
1638 for (i = 0; i < ScreenCount(display); i++)
1639 for (j = 0; j < SWM_WS_MAX; j++)
1640 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1641 entry)
1642 if (id == win->id)
1643 return (win);
1644 return (NULL);
1645 }
1646
1647 struct ws_win *
1648 find_window(Window id)
1649 {
1650 struct ws_win *win;
1651 Window wrr, wpr, *wcr = NULL;
1652 int i, j;
1653 unsigned int nc;
1654
1655 for (i = 0; i < ScreenCount(display); i++)
1656 for (j = 0; j < SWM_WS_MAX; j++)
1657 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1658 if (id == win->id)
1659 return (win);
1660
1661 /* if we were looking for the parent return that window instead */
1662 if (XQueryTree(display, id, &wrr, &wpr, &wcr, &nc) == 0)
1663 return (NULL);
1664 if (wcr)
1665 XFree(wcr);
1666
1667 /* ignore not found and root */
1668 if (wpr == 0 || wrr == wpr)
1669 return (NULL);
1670
1671 /* look for parent */
1672 for (i = 0; i < ScreenCount(display); i++)
1673 for (j = 0; j < SWM_WS_MAX; j++)
1674 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1675 if (wpr == win->id)
1676 return (win);
1677
1678 return (NULL);
1679 }
1680
1681 void
1682 spawn(struct swm_region *r, union arg *args, int close_fd)
1683 {
1684 int fd;
1685 char *ret = NULL;
1686
1687 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1688
1689 if (display)
1690 close(ConnectionNumber(display));
1691
1692 setenv("LD_PRELOAD", SWM_LIB, 1);
1693
1694 if (asprintf(&ret, "%d", r->ws->idx) == -1) {
1695 perror("_SWM_WS");
1696 _exit(1);
1697 }
1698 setenv("_SWM_WS", ret, 1);
1699 free(ret);
1700 ret = NULL;
1701
1702 if (asprintf(&ret, "%d", getpid()) == -1) {
1703 perror("_SWM_PID");
1704 _exit(1);
1705 }
1706 setenv("_SWM_PID", ret, 1);
1707 free(ret);
1708 ret = NULL;
1709
1710 if (setsid() == -1) {
1711 perror("setsid");
1712 _exit(1);
1713 }
1714
1715 if (close_fd) {
1716 /*
1717 * close stdin and stdout to prevent interaction between apps
1718 * and the baraction script
1719 * leave stderr open to record errors
1720 */
1721 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) == -1) {
1722 perror("open");
1723 _exit(1);
1724 }
1725 dup2(fd, STDIN_FILENO);
1726 dup2(fd, STDOUT_FILENO);
1727 if (fd > 2)
1728 close(fd);
1729 }
1730
1731 execvp(args->argv[0], args->argv);
1732
1733 perror("execvp");
1734 _exit(1);
1735 }
1736
1737 void
1738 spawnterm(struct swm_region *r, union arg *args)
1739 {
1740 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1741
1742 if (term_width)
1743 setenv("_SWM_XTERM_FONTADJ", "", 1);
1744 if (fork() == 0)
1745 spawn(r, args, 1);
1746 }
1747
1748 void
1749 kill_refs(struct ws_win *win)
1750 {
1751 int i, x;
1752 struct swm_region *r;
1753 struct workspace *ws;
1754
1755 if (win == NULL)
1756 return;
1757
1758 for (i = 0; i < ScreenCount(display); i++)
1759 TAILQ_FOREACH(r, &screens[i].rl, entry)
1760 for (x = 0; x < SWM_WS_MAX; x++) {
1761 ws = &r->s->ws[x];
1762 if (win == ws->focus)
1763 ws->focus = NULL;
1764 if (win == ws->focus_prev)
1765 ws->focus_prev = NULL;
1766 }
1767 }
1768
1769 int
1770 validate_win(struct ws_win *testwin)
1771 {
1772 struct ws_win *win;
1773 struct workspace *ws;
1774 struct swm_region *r;
1775 int i, x, foundit = 0;
1776
1777 if (testwin == NULL)
1778 return(0);
1779
1780 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1781 TAILQ_FOREACH(r, &screens[i].rl, entry)
1782 for (x = 0; x < SWM_WS_MAX; x++) {
1783 ws = &r->s->ws[x];
1784 TAILQ_FOREACH(win, &ws->winlist, entry)
1785 if (win == testwin)
1786 return (0);
1787 }
1788 return (1);
1789 }
1790
1791 int
1792 validate_ws(struct workspace *testws)
1793 {
1794 struct swm_region *r;
1795 struct workspace *ws;
1796 int foundit, i, x;
1797
1798 /* validate all ws */
1799 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1800 TAILQ_FOREACH(r, &screens[i].rl, entry)
1801 for (x = 0; x < SWM_WS_MAX; x++) {
1802 ws = &r->s->ws[x];
1803 if (ws == testws)
1804 return (0);
1805 }
1806 return (1);
1807 }
1808
1809 void
1810 unfocus_win(struct ws_win *win)
1811 {
1812 XEvent cne;
1813 Window none = None;
1814
1815 DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", WINID(win));
1816
1817 if (win == NULL)
1818 return;
1819 if (win->ws == NULL)
1820 return;
1821
1822 if (validate_ws(win->ws))
1823 return; /* XXX this gets hit with thunderbird, needs fixing */
1824
1825 if (win->ws->r == NULL)
1826 return;
1827
1828 if (validate_win(win)) {
1829 kill_refs(win);
1830 return;
1831 }
1832
1833 if (win->ws->focus == win) {
1834 win->ws->focus = NULL;
1835 win->ws->focus_prev = win;
1836 }
1837
1838 if (validate_win(win->ws->focus)) {
1839 kill_refs(win->ws->focus);
1840 win->ws->focus = NULL;
1841 }
1842 if (validate_win(win->ws->focus_prev)) {
1843 kill_refs(win->ws->focus_prev);
1844 win->ws->focus_prev = NULL;
1845 }
1846
1847 /* drain all previous unfocus events */
1848 while (XCheckTypedEvent(display, FocusOut, &cne) == True)
1849 ;
1850
1851 grabbuttons(win, 0);
1852 XSetWindowBorder(display, win->id,
1853 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1854
1855 XChangeProperty(display, win->s->root,
1856 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1857 PropModeReplace, (unsigned char *)&none,1);
1858 }
1859
1860 void
1861 unfocus_all(void)
1862 {
1863 struct ws_win *win;
1864 int i, j;
1865
1866 DNPRINTF(SWM_D_FOCUS, "unfocus_all\n");
1867
1868 for (i = 0; i < ScreenCount(display); i++)
1869 for (j = 0; j < SWM_WS_MAX; j++)
1870 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1871 unfocus_win(win);
1872 }
1873
1874 void
1875 focus_win(struct ws_win *win)
1876 {
1877 XEvent cne;
1878 Window cur_focus;
1879 int rr;
1880 struct ws_win *cfw = NULL;
1881
1882
1883 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1884
1885 if (win == NULL)
1886 return;
1887 if (win->ws == NULL)
1888 return;
1889
1890 if (validate_ws(win->ws))
1891 return; /* XXX this gets hit with thunderbird, needs fixing */
1892
1893 if (validate_win(win)) {
1894 kill_refs(win);
1895 return;
1896 }
1897
1898 if (validate_win(win)) {
1899 kill_refs(win);
1900 return;
1901 }
1902
1903 XGetInputFocus(display, &cur_focus, &rr);
1904 if ((cfw = find_window(cur_focus)) != NULL)
1905 unfocus_win(cfw);
1906
1907 win->ws->focus = win;
1908
1909 if (win->ws->r != NULL) {
1910 /* drain all previous focus events */
1911 while (XCheckTypedEvent(display, FocusIn, &cne) == True)
1912 ;
1913
1914 if (win->java == 0)
1915 XSetInputFocus(display, win->id,
1916 RevertToParent, CurrentTime);
1917 XMapRaised(display, win->id);
1918 grabbuttons(win, 1);
1919 XSetWindowBorder(display, win->id,
1920 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1921 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1922 XMapRaised(display, win->id);
1923
1924 XChangeProperty(display, win->s->root,
1925 ewmh[_NET_ACTIVE_WINDOW].atom, XA_WINDOW, 32,
1926 PropModeReplace, (unsigned char *)&win->id,1);
1927 }
1928
1929 if (window_name_enabled)
1930 bar_update();
1931 }
1932
1933 void
1934 switchws(struct swm_region *r, union arg *args)
1935 {
1936 int wsid = args->id, unmap_old = 0;
1937 struct swm_region *this_r, *other_r;
1938 struct ws_win *win;
1939 struct workspace *new_ws, *old_ws;
1940 union arg a;
1941
1942 if (!(r && r->s))
1943 return;
1944
1945 this_r = r;
1946 old_ws = this_r->ws;
1947 new_ws = &this_r->s->ws[wsid];
1948
1949 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1950 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1951 old_ws->idx, wsid);
1952
1953 if (new_ws == NULL || old_ws == NULL)
1954 return;
1955 if (new_ws == old_ws)
1956 return;
1957
1958 other_r = new_ws->r;
1959 if (other_r == NULL) {
1960 /* the other workspace is hidden, hide this one */
1961 old_ws->r = NULL;
1962 unmap_old = 1;
1963 } else {
1964 /* the other ws is visible in another region, exchange them */
1965 other_r->ws_prior = new_ws;
1966 other_r->ws = old_ws;
1967 old_ws->r = other_r;
1968 }
1969 this_r->ws_prior = old_ws;
1970 this_r->ws = new_ws;
1971 new_ws->r = this_r;
1972
1973 stack();
1974 a.id = SWM_ARG_ID_FOCUSCUR;
1975 focus(new_ws->r, &a);
1976
1977 bar_update();
1978
1979 /* unmap old windows */
1980 if (unmap_old)
1981 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1982 unmap_window(win);
1983
1984 if (focus_mode == SWM_FOCUS_DEFAULT)
1985 drain_enter_notify();
1986 }
1987
1988 void
1989 cyclews(struct swm_region *r, union arg *args)
1990 {
1991 union arg a;
1992 struct swm_screen *s = r->s;
1993
1994 DNPRINTF(SWM_D_WS, "cyclews id %d "
1995 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1996 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1997
1998 a.id = r->ws->idx;
1999 do {
2000 switch (args->id) {
2001 case SWM_ARG_ID_CYCLEWS_UP:
2002 if (a.id < SWM_WS_MAX - 1)
2003 a.id++;
2004 else
2005 a.id = 0;
2006 break;
2007 case SWM_ARG_ID_CYCLEWS_DOWN:
2008 if (a.id > 0)
2009 a.id--;
2010 else
2011 a.id = SWM_WS_MAX - 1;
2012 break;
2013 default:
2014 return;
2015 };
2016
2017 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
2018 continue;
2019 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
2020 continue;
2021
2022 switchws(r, &a);
2023 } while (a.id != r->ws->idx);
2024 }
2025
2026 void
2027 priorws(struct swm_region *r, union arg *args)
2028 {
2029 union arg a;
2030
2031 DNPRINTF(SWM_D_WS, "priorws id %d "
2032 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
2033 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2034
2035 if (r->ws_prior == NULL)
2036 return;
2037
2038 a.id = r->ws_prior->idx;
2039 switchws(r, &a);
2040 }
2041
2042 void
2043 cyclescr(struct swm_region *r, union arg *args)
2044 {
2045 struct swm_region *rr = NULL;
2046 union arg a;
2047 int i, x, y;
2048
2049 /* do nothing if we don't have more than one screen */
2050 if (!(ScreenCount(display) > 1 || outputs > 1))
2051 return;
2052
2053 i = r->s->idx;
2054 switch (args->id) {
2055 case SWM_ARG_ID_CYCLESC_UP:
2056 rr = TAILQ_NEXT(r, entry);
2057 if (rr == NULL)
2058 rr = TAILQ_FIRST(&screens[i].rl);
2059 break;
2060 case SWM_ARG_ID_CYCLESC_DOWN:
2061 rr = TAILQ_PREV(r, swm_region_list, entry);
2062 if (rr == NULL)
2063 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
2064 break;
2065 default:
2066 return;
2067 };
2068 if (rr == NULL)
2069 return;
2070
2071 /* move mouse to region */
2072 x = rr->g.x + 1;
2073 y = rr->g.y + 1 + (bar_enabled ? bar_height : 0);
2074 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2075
2076 a.id = SWM_ARG_ID_FOCUSCUR;
2077 focus(rr, &a);
2078
2079 if (rr->ws->focus) {
2080 /* move to focus window */
2081 x = rr->ws->focus->g.x + 1;
2082 y = rr->ws->focus->g.y + 1;
2083 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
2084 }
2085 }
2086
2087 void
2088 sort_windows(struct ws_win_list *wl)
2089 {
2090 struct ws_win *win, *parent, *nxt;
2091
2092 if (wl == NULL)
2093 return;
2094
2095 for (win = TAILQ_FIRST(wl); win != TAILQ_END(wl); win = nxt) {
2096 nxt = TAILQ_NEXT(win, entry);
2097 if (win->transient) {
2098 parent = find_window(win->transient);
2099 if (parent == NULL) {
2100 fprintf(stderr, "not possible bug\n");
2101 continue;
2102 }
2103 TAILQ_REMOVE(wl, win, entry);
2104 TAILQ_INSERT_AFTER(wl, parent, win, entry);
2105 }
2106 }
2107
2108 }
2109
2110 void
2111 swapwin(struct swm_region *r, union arg *args)
2112 {
2113 struct ws_win *target, *source;
2114 struct ws_win *cur_focus;
2115 struct ws_win_list *wl;
2116
2117
2118 DNPRINTF(SWM_D_WS, "swapwin id %d "
2119 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
2120 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
2121
2122 cur_focus = r->ws->focus;
2123 if (cur_focus == NULL)
2124 return;
2125
2126 source = cur_focus;
2127 wl = &source->ws->winlist;
2128
2129 switch (args->id) {
2130 case SWM_ARG_ID_SWAPPREV:
2131 if (source->transient)
2132 source = find_window(source->transient);
2133 target = TAILQ_PREV(source, ws_win_list, entry);
2134 if (target && target->transient)
2135 target = find_window(target->transient);
2136 TAILQ_REMOVE(wl, source, entry);
2137 if (target == NULL)
2138 TAILQ_INSERT_TAIL(wl, source, entry);
2139 else
2140 TAILQ_INSERT_BEFORE(target, source, entry);
2141 break;
2142 case SWM_ARG_ID_SWAPNEXT:
2143 target = TAILQ_NEXT(source, entry);
2144 /* move the parent and let the sort handle the move */
2145 if (source->transient)
2146 source = find_window(source->transient);
2147 TAILQ_REMOVE(wl, source, entry);
2148 if (target == NULL)
2149 TAILQ_INSERT_HEAD(wl, source, entry);
2150 else
2151 TAILQ_INSERT_AFTER(wl, target, source, entry);
2152 break;
2153 case SWM_ARG_ID_SWAPMAIN:
2154 target = TAILQ_FIRST(wl);
2155 if (target == source) {
2156 if (source->ws->focus_prev != NULL &&
2157 source->ws->focus_prev != target)
2158
2159 source = source->ws->focus_prev;
2160 else
2161 return;
2162 }
2163 if (target == NULL || source == NULL)
2164 return;
2165 source->ws->focus_prev = target;
2166 TAILQ_REMOVE(wl, target, entry);
2167 TAILQ_INSERT_BEFORE(source, target, entry);
2168 TAILQ_REMOVE(wl, source, entry);
2169 TAILQ_INSERT_HEAD(wl, source, entry);
2170 break;
2171 case SWM_ARG_ID_MOVELAST:
2172 TAILQ_REMOVE(wl, source, entry);
2173 TAILQ_INSERT_TAIL(wl, source, entry);
2174 break;
2175 default:
2176 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
2177 return;
2178 }
2179
2180 sort_windows(wl);
2181
2182 stack();
2183 }
2184
2185 void
2186 focus_prev(struct ws_win *win)
2187 {
2188 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
2189 struct ws_win *cur_focus = NULL;
2190 struct ws_win_list *wl = NULL;
2191 struct workspace *ws = NULL;
2192
2193 DNPRINTF(SWM_D_FOCUS, "focus_prev: id %lu\n", WINID(win));
2194
2195 if (!(win && win->ws))
2196 return;
2197
2198 ws = win->ws;
2199 wl = &ws->winlist;
2200 cur_focus = ws->focus;
2201 winlostfocus = cur_focus;
2202
2203 /* pickle, just focus on whatever */
2204 if (cur_focus == NULL) {
2205 /* use prev_focus if valid */
2206 if (ws->focus_prev && ws->focus_prev != cur_focus &&
2207 find_window(WINID(ws->focus_prev)))
2208 winfocus = ws->focus_prev;
2209 if (winfocus == NULL)
2210 winfocus = TAILQ_FIRST(wl);
2211 goto done;
2212 }
2213
2214 /* if transient focus on parent */
2215 if (cur_focus->transient) {
2216 winfocus = find_window(cur_focus->transient);
2217 goto done;
2218 }
2219
2220 /* if in max_stack try harder */
2221 if ((win->quirks & SWM_Q_FOCUSPREV) ||
2222 (ws->cur_layout->flags & SWM_L_FOCUSPREV)) {
2223 if (cur_focus != ws->focus_prev)
2224 winfocus = ws->focus_prev;
2225 else if (cur_focus != ws->focus)
2226 winfocus = ws->focus;
2227 else
2228 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2229 if (winfocus)
2230 goto done;
2231 }
2232
2233 if (cur_focus == win)
2234 winfocus = TAILQ_PREV(win, ws_win_list, entry);
2235 if (winfocus == NULL)
2236 winfocus = TAILQ_LAST(wl, ws_win_list);
2237 if (winfocus == NULL || winfocus == win)
2238 winfocus = TAILQ_NEXT(cur_focus, entry);
2239 done:
2240 if (winfocus == winlostfocus || winfocus == NULL)
2241 return;
2242
2243 focus_magic(winfocus);
2244 }
2245
2246 void
2247 focus(struct swm_region *r, union arg *args)
2248 {
2249 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2250 struct ws_win *cur_focus = NULL;
2251 struct ws_win_list *wl = NULL;
2252 struct workspace *ws = NULL;
2253
2254 if (!(r && r->ws))
2255 return;
2256
2257 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2258
2259 /* treat FOCUS_CUR special */
2260 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2261 if (r->ws->focus && r->ws->focus->iconic == 0)
2262 winfocus = r->ws->focus;
2263 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2264 winfocus = r->ws->focus_prev;
2265 else
2266 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2267 if (winfocus->iconic == 0)
2268 break;
2269
2270 focus_magic(winfocus);
2271 return;
2272 }
2273
2274 if ((cur_focus = r->ws->focus) == NULL)
2275 return;
2276 ws = r->ws;
2277 wl = &ws->winlist;
2278
2279 winlostfocus = cur_focus;
2280
2281 switch (args->id) {
2282 case SWM_ARG_ID_FOCUSPREV:
2283 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2284 if (head == NULL)
2285 head = TAILQ_LAST(wl, ws_win_list);
2286 winfocus = head;
2287 if (WINID(winfocus) == cur_focus->transient) {
2288 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2289 if (head == NULL)
2290 head = TAILQ_LAST(wl, ws_win_list);
2291 winfocus = head;
2292 }
2293
2294 /* skip iconics */
2295 if (winfocus && winfocus->iconic) {
2296 TAILQ_FOREACH_REVERSE(winfocus, wl, ws_win_list, entry)
2297 if (winfocus->iconic == 0)
2298 break;
2299 }
2300 break;
2301
2302 case SWM_ARG_ID_FOCUSNEXT:
2303 head = TAILQ_NEXT(cur_focus, entry);
2304 if (head == NULL)
2305 head = TAILQ_FIRST(wl);
2306 winfocus = head;
2307
2308 /* skip iconics */
2309 if (winfocus && winfocus->iconic) {
2310 TAILQ_FOREACH(winfocus, wl, entry)
2311 if (winfocus->iconic == 0)
2312 break;
2313 }
2314 break;
2315
2316 case SWM_ARG_ID_FOCUSMAIN:
2317 winfocus = TAILQ_FIRST(wl);
2318 if (winfocus == cur_focus)
2319 winfocus = cur_focus->ws->focus_prev;
2320 break;
2321
2322 default:
2323 return;
2324 }
2325 if (winfocus == winlostfocus || winfocus == NULL)
2326 return;
2327
2328 focus_magic(winfocus);
2329 }
2330
2331 void
2332 cycle_layout(struct swm_region *r, union arg *args)
2333 {
2334 struct workspace *ws = r->ws;
2335 struct ws_win *winfocus;
2336 union arg a;
2337
2338 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2339
2340 winfocus = ws->focus;
2341
2342 ws->cur_layout++;
2343 if (ws->cur_layout->l_stack == NULL)
2344 ws->cur_layout = &layouts[0];
2345
2346 stack();
2347 if (focus_mode == SWM_FOCUS_DEFAULT)
2348 drain_enter_notify();
2349 a.id = SWM_ARG_ID_FOCUSCUR;
2350 focus(r, &a);
2351 bar_update();
2352 }
2353
2354 void
2355 stack_config(struct swm_region *r, union arg *args)
2356 {
2357 struct workspace *ws = r->ws;
2358
2359 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2360 args->id, ws->idx);
2361
2362 if (ws->cur_layout->l_config != NULL)
2363 ws->cur_layout->l_config(ws, args->id);
2364
2365 if (args->id != SWM_ARG_ID_STACKINIT);
2366 stack();
2367 }
2368
2369 void
2370 stack(void) {
2371 struct swm_geometry g;
2372 struct swm_region *r;
2373 int i, j;
2374
2375 DNPRINTF(SWM_D_STACK, "stack\n");
2376
2377 for (i = 0; i < ScreenCount(display); i++) {
2378 j = 0;
2379 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2380 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2381 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2382
2383 /* start with screen geometry, adjust for bar */
2384 g = r->g;
2385 g.w -= 2 * border_width;
2386 g.h -= 2 * border_width;
2387 if (bar_enabled) {
2388 if (!bar_at_bottom)
2389 g.y += bar_height;
2390 g.h -= bar_height;
2391 }
2392 r->ws->cur_layout->l_stack(r->ws, &g);
2393 /* save r so we can track region changes */
2394 r->ws->old_r = r;
2395 }
2396 }
2397 if (font_adjusted)
2398 font_adjusted--;
2399
2400 if (focus_mode == SWM_FOCUS_DEFAULT)
2401 drain_enter_notify();
2402 }
2403
2404 void
2405 store_float_geom(struct ws_win *win, struct swm_region *r)
2406 {
2407 /* retain window geom and region geom */
2408 win->g_float.x = win->g.x;
2409 win->g_float.y = win->g.y;
2410 win->g_float.w = win->g.w;
2411 win->g_float.h = win->g.h;
2412 win->rg_float.x = r->g.x;
2413 win->rg_float.y = r->g.y;
2414 win->rg_float.w = r->g.w;
2415 win->rg_float.h = r->g.h;
2416 win->g_floatvalid = 1;
2417 }
2418
2419 void
2420 stack_floater(struct ws_win *win, struct swm_region *r)
2421 {
2422 unsigned int mask;
2423 XWindowChanges wc;
2424
2425 if (win == NULL)
2426 return;
2427
2428 bzero(&wc, sizeof wc);
2429 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2430
2431 /*
2432 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2433 * geom on ws switches or return from max mode
2434 */
2435
2436 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2437 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2438 /*
2439 * use stored g and rg to set relative position and size
2440 * as in old region or before max stack mode
2441 */
2442 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2443 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2444 win->g.w = win->g_float.w;
2445 win->g.h = win->g_float.h;
2446 win->g_floatvalid = 0;
2447 }
2448
2449 win->floatmaxed = 0;
2450
2451 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2452 (win->g.h >= HEIGHT(r)))
2453 wc.border_width = 0;
2454 else
2455 wc.border_width = border_width;
2456 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2457 win->g.w = (double)WIDTH(r) * dialog_ratio;
2458 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2459 }
2460
2461 if (!win->manual) {
2462 /*
2463 * floaters and transients are auto-centred unless moved
2464 * or resized
2465 */
2466 win->g.x = r->g.x + (WIDTH(r) - win->g.w) / 2 - wc.border_width;
2467 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) / 2 - wc.border_width;
2468 }
2469
2470 /* win can be outside r if new r smaller than old r */
2471 /* Ensure top left corner inside r (move probs otherwise) */
2472 if (win->g.x < r->g.x - wc.border_width)
2473 win->g.x = r->g.x - wc.border_width;
2474 if (win->g.x > r->g.x + r->g.w - 1)
2475 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2476 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2477 if (win->g.y < r->g.y - wc.border_width)
2478 win->g.y = r->g.y - wc.border_width;
2479 if (win->g.y > r->g.y + r->g.h - 1)
2480 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2481 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2482
2483 wc.x = win->g.x;
2484 wc.y = win->g.y;
2485 wc.width = win->g.w;
2486 wc.height = win->g.h;
2487
2488 /*
2489 * Retain floater and transient geometry for correct positioning
2490 * when ws changes region
2491 */
2492 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2493 store_float_geom(win, r);
2494
2495 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2496 win->id, wc.x, wc.y, wc.width, wc.height);
2497
2498 XConfigureWindow(display, win->id, mask, &wc);
2499 }
2500
2501 /*
2502 * Send keystrokes to terminal to decrease/increase the font size as the
2503 * window size changes.
2504 */
2505 void
2506 adjust_font(struct ws_win *win)
2507 {
2508 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2509 win->floating || win->transient)
2510 return;
2511
2512 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2513 win->g.w / win->sh.width_inc < term_width &&
2514 win->font_steps < SWM_MAX_FONT_STEPS) {
2515 win->font_size_boundary[win->font_steps] =
2516 (win->sh.width_inc * term_width) + win->sh.base_width;
2517 win->font_steps++;
2518 font_adjusted++;
2519 win->last_inc = win->sh.width_inc;
2520 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2521 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2522 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2523 win->font_steps--;
2524 font_adjusted++;
2525 win->last_inc = win->sh.width_inc;
2526 fake_keypress(win, XK_KP_Add, ShiftMask);
2527 }
2528 }
2529
2530 #define SWAPXY(g) do { \
2531 int tmp; \
2532 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2533 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2534 } while (0)
2535 void
2536 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2537 {
2538 XWindowChanges wc;
2539 XWindowAttributes wa;
2540 struct swm_geometry win_g, r_g = *g;
2541 struct ws_win *win, *fs_win = 0;
2542 int i, j, s, stacks;
2543 int w_inc = 1, h_inc, w_base = 1, h_base;
2544 int hrh, extra = 0, h_slice, last_h = 0;
2545 int split, colno, winno, mwin, msize, mscale;
2546 int remain, missing, v_slice, reconfigure;
2547 unsigned int mask;
2548
2549 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2550 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2551
2552 winno = count_win(ws, 0);
2553 if (winno == 0 && count_win(ws, 1) == 0)
2554 return;
2555
2556 TAILQ_FOREACH(win, &ws->winlist, entry)
2557 if (win->transient == 0 && win->floating == 0 && win->iconic == 0)
2558 break;
2559
2560 if (win == NULL)
2561 goto notiles;
2562
2563 if (rot) {
2564 w_inc = win->sh.width_inc;
2565 w_base = win->sh.base_width;
2566 mwin = ws->l_state.horizontal_mwin;
2567 mscale = ws->l_state.horizontal_msize;
2568 stacks = ws->l_state.horizontal_stacks;
2569 SWAPXY(&r_g);
2570 } else {
2571 w_inc = win->sh.height_inc;
2572 w_base = win->sh.base_height;
2573 mwin = ws->l_state.vertical_mwin;
2574 mscale = ws->l_state.vertical_msize;
2575 stacks = ws->l_state.vertical_stacks;
2576 }
2577 win_g = r_g;
2578
2579 if (stacks > winno - mwin)
2580 stacks = winno - mwin;
2581 if (stacks < 1)
2582 stacks = 1;
2583
2584 h_slice = r_g.h / SWM_H_SLICE;
2585 if (mwin && winno > mwin) {
2586 v_slice = r_g.w / SWM_V_SLICE;
2587
2588 split = mwin;
2589 colno = split;
2590 win_g.w = v_slice * mscale;
2591
2592 if (w_inc > 1 && w_inc < v_slice) {
2593 /* adjust for window's requested size increment */
2594 remain = (win_g.w - w_base) % w_inc;
2595 missing = w_inc - remain;
2596 win_g.w -= remain;
2597 extra += remain;
2598 }
2599
2600 msize = win_g.w;
2601 if (flip)
2602 win_g.x += r_g.w - msize;
2603 } else {
2604 msize = -2;
2605 colno = split = winno / stacks;
2606 win_g.w = ((r_g.w - (stacks * 2 * border_width) + 2 * border_width) / stacks);
2607 }
2608 hrh = r_g.h / colno;
2609 extra = r_g.h - (colno * hrh);
2610 win_g.h = hrh - 2 * border_width;
2611
2612 /* stack all the tiled windows */
2613 i = j = 0, s = stacks;
2614 TAILQ_FOREACH(win, &ws->winlist, entry) {
2615 if (win->transient != 0 || win->floating != 0)
2616 continue;
2617 if (win->iconic != 0)
2618 continue;
2619
2620 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2621 fs_win = win;
2622 continue;
2623 }
2624
2625 if (split && i == split) {
2626 colno = (winno - mwin) / stacks;
2627 if (s <= (winno - mwin) % stacks)
2628 colno++;
2629 split = split + colno;
2630 hrh = (r_g.h / colno);
2631 extra = r_g.h - (colno * hrh);
2632 if (flip)
2633 win_g.x = r_g.x;
2634 else
2635 win_g.x += win_g.w + 2 * border_width;
2636 win_g.w = (r_g.w - msize - (stacks * 2 * border_width)) / stacks;
2637 if (s == 1)
2638 win_g.w += (r_g.w - msize - (stacks * 2 * border_width)) %
2639 stacks;
2640 s--;
2641 j = 0;
2642 }
2643 win_g.h = hrh - 2 * border_width;
2644 if (rot) {
2645 h_inc = win->sh.width_inc;
2646 h_base = win->sh.base_width;
2647 } else {
2648 h_inc = win->sh.height_inc;
2649 h_base = win->sh.base_height;
2650 }
2651 if (j == colno - 1) {
2652 win_g.h = hrh + extra;
2653 } else if (h_inc > 1 && h_inc < h_slice) {
2654 /* adjust for window's requested size increment */
2655 remain = (win_g.h - h_base) % h_inc;
2656 missing = h_inc - remain;
2657
2658 if (missing <= extra || j == 0) {
2659 extra -= missing;
2660 win_g.h += missing;
2661 } else {
2662 win_g.h -= remain;
2663 extra += remain;
2664 }
2665 }
2666
2667 if (j == 0)
2668 win_g.y = r_g.y;
2669 else
2670 win_g.y += last_h + 2 * border_width;
2671
2672 bzero(&wc, sizeof wc);
2673 if (disable_border && bar_enabled == 0 && winno == 1){
2674 wc.border_width = 0;
2675 win_g.w += 2 * border_width;
2676 win_g.h += 2 * border_width;
2677 } else
2678 wc.border_width = border_width;
2679 reconfigure = 0;
2680 if (rot) {
2681 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2682 win->g.w != win_g.h || win->g.h != win_g.w) {
2683 reconfigure = 1;
2684 win->g.x = wc.x = win_g.y;
2685 win->g.y = wc.y = win_g.x;
2686 win->g.w = wc.width = win_g.h;
2687 win->g.h = wc.height = win_g.w;
2688 }
2689 } else {
2690 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2691 win->g.w != win_g.w || win->g.h != win_g.h) {
2692 reconfigure = 1;
2693 win->g.x = wc.x = win_g.x;
2694 win->g.y = wc.y = win_g.y;
2695 win->g.w = wc.width = win_g.w;
2696 win->g.h = wc.height = win_g.h;
2697 }
2698 }
2699 if (reconfigure) {
2700 adjust_font(win);
2701 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2702 XConfigureWindow(display, win->id, mask, &wc);
2703 }
2704
2705 if (XGetWindowAttributes(display, win->id, &wa))
2706 if (wa.map_state == IsUnmapped)
2707 XMapRaised(display, win->id);
2708
2709 last_h = win_g.h;
2710 i++;
2711 j++;
2712 }
2713
2714 notiles:
2715 /* now, stack all the floaters and transients */
2716 TAILQ_FOREACH(win, &ws->winlist, entry) {
2717 if (win->transient == 0 && win->floating == 0)
2718 continue;
2719 if (win->iconic == 1)
2720 continue;
2721 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2722 fs_win = win;
2723 continue;
2724 }
2725
2726 stack_floater(win, ws->r);
2727 XMapRaised(display, win->id);
2728 }
2729
2730 if (fs_win) {
2731 stack_floater(fs_win, ws->r);
2732 XMapRaised(display, fs_win->id);
2733 }
2734 }
2735
2736 void
2737 vertical_config(struct workspace *ws, int id)
2738 {
2739 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2740
2741 switch (id) {
2742 case SWM_ARG_ID_STACKRESET:
2743 case SWM_ARG_ID_STACKINIT:
2744 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2745 ws->l_state.vertical_mwin = 1;
2746 ws->l_state.vertical_stacks = 1;
2747 break;
2748 case SWM_ARG_ID_MASTERSHRINK:
2749 if (ws->l_state.vertical_msize > 1)
2750 ws->l_state.vertical_msize--;
2751 break;
2752 case SWM_ARG_ID_MASTERGROW:
2753 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2754 ws->l_state.vertical_msize++;
2755 break;
2756 case SWM_ARG_ID_MASTERADD:
2757 ws->l_state.vertical_mwin++;
2758 break;
2759 case SWM_ARG_ID_MASTERDEL:
2760 if (ws->l_state.vertical_mwin > 0)
2761 ws->l_state.vertical_mwin--;
2762 break;
2763 case SWM_ARG_ID_STACKINC:
2764 ws->l_state.vertical_stacks++;
2765 break;
2766 case SWM_ARG_ID_STACKDEC:
2767 if (ws->l_state.vertical_stacks > 1)
2768 ws->l_state.vertical_stacks--;
2769 break;
2770 default:
2771 return;
2772 }
2773 }
2774
2775 void
2776 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2777 {
2778 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2779
2780 stack_master(ws, g, 0, 0);
2781 }
2782
2783 void
2784 horizontal_config(struct workspace *ws, int id)
2785 {
2786 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2787
2788 switch (id) {
2789 case SWM_ARG_ID_STACKRESET:
2790 case SWM_ARG_ID_STACKINIT:
2791 ws->l_state.horizontal_mwin = 1;
2792 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2793 ws->l_state.horizontal_stacks = 1;
2794 break;
2795 case SWM_ARG_ID_MASTERSHRINK:
2796 if (ws->l_state.horizontal_msize > 1)
2797 ws->l_state.horizontal_msize--;
2798 break;
2799 case SWM_ARG_ID_MASTERGROW:
2800 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2801 ws->l_state.horizontal_msize++;
2802 break;
2803 case SWM_ARG_ID_MASTERADD:
2804 ws->l_state.horizontal_mwin++;
2805 break;
2806 case SWM_ARG_ID_MASTERDEL:
2807 if (ws->l_state.horizontal_mwin > 0)
2808 ws->l_state.horizontal_mwin--;
2809 break;
2810 case SWM_ARG_ID_STACKINC:
2811 ws->l_state.horizontal_stacks++;
2812 break;
2813 case SWM_ARG_ID_STACKDEC:
2814 if (ws->l_state.horizontal_stacks > 1)
2815 ws->l_state.horizontal_stacks--;
2816 break;
2817 default:
2818 return;
2819 }
2820 }
2821
2822 void
2823 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2824 {
2825 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
2826
2827 stack_master(ws, g, 1, 0);
2828 }
2829
2830 /* fullscreen view */
2831 void
2832 max_stack(struct workspace *ws, struct swm_geometry *g)
2833 {
2834 XWindowChanges wc;
2835 struct swm_geometry gg = *g;
2836 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2837 unsigned int mask;
2838 int winno;
2839
2840 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2841
2842 if (ws == NULL)
2843 return;
2844
2845 winno = count_win(ws, 0);
2846 if (winno == 0 && count_win(ws, 1) == 0)
2847 return;
2848
2849 TAILQ_FOREACH(win, &ws->winlist, entry) {
2850 if (win->transient) {
2851 wintrans = win;
2852 parent = find_window(win->transient);
2853 continue;
2854 }
2855
2856 if (win->floating && win->floatmaxed == 0 ) {
2857 /*
2858 * retain geometry for retrieval on exit from
2859 * max_stack mode
2860 */
2861 store_float_geom(win, ws->r);
2862 win->floatmaxed = 1;
2863 }
2864
2865 /* only reconfigure if necessary */
2866 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2867 win->g.h != gg.h) {
2868 bzero(&wc, sizeof wc);
2869 win->g.x = wc.x = gg.x;
2870 win->g.y = wc.y = gg.y;
2871 if (bar_enabled){
2872 wc.border_width = border_width;
2873 win->g.w = wc.width = gg.w;
2874 win->g.h = wc.height = gg.h;
2875 } else {
2876 wc.border_width = 0;
2877 win->g.w = wc.width = gg.w + 2 * border_width;
2878 win->g.h = wc.height = gg.h + 2 * border_width;
2879 }
2880 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2881 XConfigureWindow(display, win->id, mask, &wc);
2882 }
2883 /* unmap only if we don't have multi screen */
2884 if (win != ws->focus)
2885 if (!(ScreenCount(display) > 1 || outputs > 1))
2886 unmap_window(win);
2887 }
2888
2889 /* put the last transient on top */
2890 if (wintrans) {
2891 if (parent)
2892 XMapRaised(display, parent->id);
2893 stack_floater(wintrans, ws->r);
2894 focus_magic(wintrans);
2895 }
2896 }
2897
2898 void
2899 send_to_ws(struct swm_region *r, union arg *args)
2900 {
2901 int wsid = args->id;
2902 struct ws_win *win = NULL, *parent;
2903 struct workspace *ws, *nws;
2904 Atom ws_idx_atom = 0;
2905 unsigned char ws_idx_str[SWM_PROPLEN];
2906 union arg a;
2907
2908 if (r && r->ws)
2909 win = r->ws->focus;
2910 else
2911 return;
2912 if (win == NULL)
2913 return;
2914 if (win->ws->idx == wsid)
2915 return;
2916
2917 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2918
2919 ws = win->ws;
2920 nws = &win->s->ws[wsid];
2921
2922 a.id = SWM_ARG_ID_FOCUSPREV;
2923 focus(r, &a);
2924 if (win->transient) {
2925 parent = find_window(win->transient);
2926 if (parent) {
2927 unmap_window(parent);
2928 TAILQ_REMOVE(&ws->winlist, parent, entry);
2929 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
2930 parent->ws = nws;
2931 }
2932 }
2933 unmap_window(win);
2934 TAILQ_REMOVE(&ws->winlist, win, entry);
2935 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2936 win->ws = nws;
2937
2938 /* Try to update the window's workspace property */
2939 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2940 if (ws_idx_atom &&
2941 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2942 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2943 ws_idx_str);
2944 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2945 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2946 }
2947
2948 stack();
2949 }
2950
2951 void
2952 iconify(struct swm_region *r, union arg *args)
2953 {
2954 union arg a;
2955
2956 if (r->ws->focus == NULL)
2957 return;
2958 unmap_window(r->ws->focus);
2959 update_iconic(r->ws->focus, 1);
2960 stack();
2961 if (focus_mode == SWM_FOCUS_DEFAULT)
2962 drain_enter_notify();
2963 r->ws->focus = NULL;
2964 a.id = SWM_ARG_ID_FOCUSCUR;
2965 focus(r, &a);
2966 }
2967
2968 unsigned char *
2969 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
2970 unsigned long *slen)
2971 {
2972 int status, retfmt;
2973 unsigned long nitems, nbytes, nextra;
2974 unsigned char *prop = NULL;
2975 Atom rettype;
2976
2977 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
2978 &rettype, &retfmt, &nitems, &nbytes, &prop);
2979 if (status != Success)
2980 return (NULL);
2981 XFree(prop);
2982
2983 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
2984 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
2985 if (status != Success) {
2986 XFree(prop);
2987 return (NULL);
2988 }
2989 if (rettype != stype) {
2990 XFree(prop);
2991 return (NULL);
2992 }
2993 *slen = nitems;
2994 return (prop);
2995 }
2996
2997 void
2998 uniconify(struct swm_region *r, union arg *args)
2999 {
3000 struct ws_win *win;
3001 FILE *lfile;
3002 char *name;
3003 int count = 0;
3004 unsigned long len;
3005
3006 DNPRINTF(SWM_D_MISC, "uniconify\n");
3007
3008 if (r && r->ws == NULL)
3009 return;
3010
3011 /* make sure we have anything to uniconify */
3012 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3013 if (win->ws == NULL)
3014 continue; /* should never happen */
3015 if (win->iconic == 0)
3016 continue;
3017 count++;
3018 }
3019 if (count == 0)
3020 return;
3021
3022 search_r = r;
3023
3024 spawn_select(r, args, "uniconify", &searchpid);
3025
3026 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3027 return;
3028
3029 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3030 if (win->ws == NULL)
3031 continue; /* should never happen */
3032 if (win->iconic == 0)
3033 continue;
3034
3035 name = get_win_name(display, win->id, a_wmname, a_string,
3036 &len);
3037 if (name == NULL)
3038 continue;
3039 fprintf(lfile, "%s.%lu\n", name, win->id);
3040 XFree(name);
3041 }
3042
3043 fclose(lfile);
3044 }
3045
3046 #define MAX_RESP_LEN 1024
3047
3048 void
3049 search_do_resp(void)
3050 {
3051 ssize_t rbytes;
3052 struct ws_win *win;
3053 char *name, *resp, *s;
3054 unsigned long len;
3055
3056 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3057
3058 search_resp = 0;
3059 searchpid = 0;
3060
3061 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3062 fprintf(stderr, "search: calloc\n");
3063 return;
3064 }
3065
3066 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3067 if (rbytes <= 0) {
3068 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3069 goto done;
3070 }
3071 resp[rbytes] = '\0';
3072 len = strlen(resp);
3073
3074 DNPRINTF(SWM_D_MISC, "search_do_resp: resp %s\n", resp);
3075 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3076 if (win->iconic == 0)
3077 continue;
3078 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3079 if (name == NULL)
3080 continue;
3081 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3082 XFree(name);
3083 continue;
3084 }
3085 XFree(name);
3086 if (strncmp(s, resp, len) == 0) {
3087 /* XXX this should be a callback to generalize */
3088 update_iconic(win, 0);
3089 free(s);
3090 break;
3091 }
3092 free(s);
3093 }
3094 done:
3095 free(resp);
3096 }
3097
3098 void
3099 wkill(struct swm_region *r, union arg *args)
3100 {
3101 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3102
3103 if (r->ws->focus == NULL)
3104 return;
3105
3106 if (args->id == SWM_ARG_ID_KILLWINDOW)
3107 XKillClient(display, r->ws->focus->id);
3108 else
3109 if (r->ws->focus->can_delete)
3110 client_msg(r->ws->focus, adelete);
3111 }
3112
3113
3114 int
3115 floating_toggle_win(struct ws_win *win)
3116 {
3117 struct swm_region *r;
3118
3119 if (win == NULL)
3120 return 0;
3121
3122 if (!win->ws->r)
3123 return 0;
3124
3125 r = win->ws->r;
3126
3127 /* reject floating toggles in max stack mode */
3128 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3129 return 0;
3130
3131 if (win->floating) {
3132 if (!win->floatmaxed) {
3133 /* retain position for refloat */
3134 store_float_geom(win, r);
3135 }
3136 win->floating = 0;
3137 } else {
3138 if (win->g_floatvalid) {
3139 /* refloat at last floating relative position */
3140 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3141 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3142 win->g.w = win->g_float.w;
3143 win->g.h = win->g_float.h;
3144 }
3145 win->floating = 1;
3146 }
3147
3148 ewmh_update_actions(win);
3149
3150 return 1;
3151 }
3152
3153 void
3154 floating_toggle(struct swm_region *r, union arg *args)
3155 {
3156 struct ws_win *win = r->ws->focus;
3157 union arg a;
3158
3159 if (win == NULL)
3160 return;
3161
3162 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3163 _NET_WM_STATE_TOGGLE);
3164
3165 stack();
3166 if (focus_mode == SWM_FOCUS_DEFAULT)
3167 drain_enter_notify();
3168
3169 if (win == win->ws->focus) {
3170 a.id = SWM_ARG_ID_FOCUSCUR;
3171 focus(win->ws->r, &a);
3172 }
3173 }
3174
3175 void
3176 resize_window(struct ws_win *win, int center)
3177 {
3178 unsigned int mask;
3179 XWindowChanges wc;
3180 struct swm_region *r;
3181
3182 r = root_to_region(win->wa.root);
3183 bzero(&wc, sizeof wc);
3184 mask = CWBorderWidth | CWWidth | CWHeight;
3185 wc.border_width = border_width;
3186 wc.width = win->g.w;
3187 wc.height = win->g.h;
3188 if (center == SWM_ARG_ID_CENTER) {
3189 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3190 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3191 mask |= CWX | CWY;
3192 }
3193
3194 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3195 win->id, wc.x, wc.y, wc.width, wc.height);
3196
3197 XConfigureWindow(display, win->id, mask, &wc);
3198 }
3199
3200 void
3201 resize(struct ws_win *win, union arg *args)
3202 {
3203 XEvent ev;
3204 Time time = 0;
3205 struct swm_region *r = win->ws->r;
3206 int relx, rely;
3207 union arg a;
3208
3209
3210 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3211 win->id, win->floating, win->transient);
3212
3213 if (!(win->transient != 0 || win->floating != 0))
3214 return;
3215
3216 /* reject resizes in max mode for floaters (transient ok) */
3217 if (win->floatmaxed)
3218 return;
3219
3220 win->manual = 1;
3221 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3222 _NET_WM_STATE_ADD);
3223 /* raise the window = move to last in window list */
3224 a.id = SWM_ARG_ID_MOVELAST;
3225 swapwin(r, &a);
3226 stack();
3227 if (focus_mode == SWM_FOCUS_DEFAULT)
3228 drain_enter_notify();
3229
3230 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3231 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3232 return;
3233
3234 /* place pointer at bottom left corner or nearest point inside r */
3235 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3236 relx = win->g.w - 1;
3237 else
3238 relx = r->g.x + r->g.w - win->g.x - 1;
3239
3240 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3241 rely = win->g.h - 1;
3242 else
3243 rely = r->g.y + r->g.h - win->g.y - 1;
3244
3245 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3246 do {
3247 XMaskEvent(display, MOUSEMASK | ExposureMask |
3248 SubstructureRedirectMask, &ev);
3249 switch(ev.type) {
3250 case ConfigureRequest:
3251 case Expose:
3252 case MapRequest:
3253 handler[ev.type](&ev);
3254 break;
3255 case MotionNotify:
3256 /* do not allow resize outside of region */
3257 if ( ev.xmotion.x_root < r->g.x ||
3258 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3259 ev.xmotion.y_root < r->g.y ||
3260 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3261 continue;
3262
3263 if (ev.xmotion.x <= 1)
3264 ev.xmotion.x = 1;
3265 if (ev.xmotion.y <= 1)
3266 ev.xmotion.y = 1;
3267 win->g.w = ev.xmotion.x + 1;
3268 win->g.h = ev.xmotion.y + 1;
3269
3270 /* not free, don't sync more than 120 times / second */
3271 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3272 time = ev.xmotion.time;
3273 XSync(display, False);
3274 resize_window(win, args->id);
3275 }
3276 break;
3277 }
3278 } while (ev.type != ButtonRelease);
3279 if (time) {
3280 XSync(display, False);
3281 resize_window(win, args->id);
3282 }
3283 store_float_geom(win,r);
3284
3285 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3286 win->g.h - 1);
3287 XUngrabPointer(display, CurrentTime);
3288
3289 /* drain events */
3290 drain_enter_notify();
3291 }
3292
3293 void
3294 move_window(struct ws_win *win)
3295 {
3296 unsigned int mask;
3297 XWindowChanges wc;
3298 struct swm_region *r;
3299
3300 r = root_to_region(win->wa.root);
3301 bzero(&wc, sizeof wc);
3302 mask = CWX | CWY;
3303 wc.x = win->g.x;
3304 wc.y = win->g.y;
3305 wc.border_width = border_width;
3306
3307 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3308 win->id, wc.x, wc.y, wc.width, wc.height);
3309
3310 XConfigureWindow(display, win->id, mask, &wc);
3311 }
3312
3313 void
3314 move(struct ws_win *win, union arg *args)
3315 {
3316 XEvent ev;
3317 Time time = 0;
3318 struct swm_region *r = win->ws->r;
3319 union arg a;
3320
3321 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3322 win->id, win->floating, win->transient);
3323
3324 /* in max_stack mode should only move transients */
3325 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3326 return;
3327
3328 win->manual = 1;
3329 if (win->floating == 0 && !win->transient) {
3330 win->floating = 1;
3331 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3332 _NET_WM_STATE_ADD);
3333 }
3334 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3335 _NET_WM_STATE_ADD);
3336
3337 /* raise the window = move to last in window list */
3338 a.id = SWM_ARG_ID_MOVELAST;
3339 swapwin(r, &a);
3340 stack();
3341
3342 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3343 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3344 return;
3345 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3346 do {
3347 XMaskEvent(display, MOUSEMASK | ExposureMask |
3348 SubstructureRedirectMask, &ev);
3349 switch(ev.type) {
3350 case ConfigureRequest:
3351 case Expose:
3352 case MapRequest:
3353 handler[ev.type](&ev);
3354 break;
3355 case MotionNotify:
3356 /* don't allow to move window origin out of region */
3357 if ( ev.xmotion.x_root < r->g.x ||
3358 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3359 ev.xmotion.y_root < r->g.y ||
3360 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3361 continue;
3362
3363 win->g.x = ev.xmotion.x_root - border_width;
3364 win->g.y = ev.xmotion.y_root - border_width;
3365
3366 /* not free, don't sync more than 120 times / second */
3367 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3368 time = ev.xmotion.time;
3369 XSync(display, False);
3370 move_window(win);
3371 }
3372 break;
3373 }
3374 } while (ev.type != ButtonRelease);
3375 if (time) {
3376 XSync(display, False);
3377 move_window(win);
3378 }
3379 store_float_geom(win,r);
3380 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3381 XUngrabPointer(display, CurrentTime);
3382
3383 /* drain events */
3384 drain_enter_notify();
3385 }
3386
3387 /* user/key callable function IDs */
3388 enum keyfuncid {
3389 kf_cycle_layout,
3390 kf_stack_reset,
3391 kf_master_shrink,
3392 kf_master_grow,
3393 kf_master_add,
3394 kf_master_del,
3395 kf_stack_inc,
3396 kf_stack_dec,
3397 kf_swap_main,
3398 kf_focus_next,
3399 kf_focus_prev,
3400 kf_swap_next,
3401 kf_swap_prev,
3402 kf_spawn_term,
3403 kf_spawn_menu,
3404 kf_quit,
3405 kf_restart,
3406 kf_focus_main,
3407 kf_ws_1,
3408 kf_ws_2,
3409 kf_ws_3,
3410 kf_ws_4,
3411 kf_ws_5,
3412 kf_ws_6,
3413 kf_ws_7,
3414 kf_ws_8,
3415 kf_ws_9,
3416 kf_ws_10,
3417 kf_ws_next,
3418 kf_ws_prev,
3419 kf_ws_prior,
3420 kf_screen_next,
3421 kf_screen_prev,
3422 kf_mvws_1,
3423 kf_mvws_2,
3424 kf_mvws_3,
3425 kf_mvws_4,
3426 kf_mvws_5,
3427 kf_mvws_6,
3428 kf_mvws_7,
3429 kf_mvws_8,
3430 kf_mvws_9,
3431 kf_mvws_10,
3432 kf_bar_toggle,
3433 kf_wind_kill,
3434 kf_wind_del,
3435 kf_screenshot_all,
3436 kf_screenshot_wind,
3437 kf_float_toggle,
3438 kf_version,
3439 kf_spawn_lock,
3440 kf_spawn_initscr,
3441 kf_spawn_custom,
3442 kf_iconify,
3443 kf_uniconify,
3444 kf_dumpwins, /* MUST BE LAST */
3445 kf_invalid
3446 };
3447
3448 /* key definitions */
3449 void
3450 dummykeyfunc(struct swm_region *r, union arg *args)
3451 {
3452 };
3453
3454 void
3455 legacyfunc(struct swm_region *r, union arg *args)
3456 {
3457 };
3458
3459 struct keyfunc {
3460 char name[SWM_FUNCNAME_LEN];
3461 void (*func)(struct swm_region *r, union arg *);
3462 union arg args;
3463 } keyfuncs[kf_invalid + 1] = {
3464 /* name function argument */
3465 { "cycle_layout", cycle_layout, {0} },
3466 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
3467 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
3468 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
3469 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
3470 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
3471 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
3472 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
3473 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
3474 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
3475 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
3476 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
3477 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
3478 { "spawn_term", spawnterm, {.argv = spawn_term} },
3479 { "spawn_menu", legacyfunc, {0} },
3480 { "quit", quit, {0} },
3481 { "restart", restart, {0} },
3482 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
3483 { "ws_1", switchws, {.id = 0} },
3484 { "ws_2", switchws, {.id = 1} },
3485 { "ws_3", switchws, {.id = 2} },
3486 { "ws_4", switchws, {.id = 3} },
3487 { "ws_5", switchws, {.id = 4} },
3488 { "ws_6", switchws, {.id = 5} },
3489 { "ws_7", switchws, {.id = 6} },
3490 { "ws_8", switchws, {.id = 7} },
3491 { "ws_9", switchws, {.id = 8} },
3492 { "ws_10", switchws, {.id = 9} },
3493 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
3494 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
3495 { "ws_prior", priorws, {0} },
3496 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
3497 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
3498 { "mvws_1", send_to_ws, {.id = 0} },
3499 { "mvws_2", send_to_ws, {.id = 1} },
3500 { "mvws_3", send_to_ws, {.id = 2} },
3501 { "mvws_4", send_to_ws, {.id = 3} },
3502 { "mvws_5", send_to_ws, {.id = 4} },
3503 { "mvws_6", send_to_ws, {.id = 5} },
3504 { "mvws_7", send_to_ws, {.id = 6} },
3505 { "mvws_8", send_to_ws, {.id = 7} },
3506 { "mvws_9", send_to_ws, {.id = 8} },
3507 { "mvws_10", send_to_ws, {.id = 9} },
3508 { "bar_toggle", bar_toggle, {0} },
3509 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
3510 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
3511 { "screenshot_all", legacyfunc, {0} },
3512 { "screenshot_wind", legacyfunc, {0} },
3513 { "float_toggle", floating_toggle,{0} },
3514 { "version", version, {0} },
3515 { "spawn_lock", legacyfunc, {0} },
3516 { "spawn_initscr", legacyfunc, {0} },
3517 { "spawn_custom", dummykeyfunc, {0} },
3518 { "iconify", iconify, {0} },
3519 { "uniconify", uniconify, {0} },
3520 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
3521 { "invalid key func", NULL, {0} },
3522 };
3523 struct key {
3524 unsigned int mod;
3525 KeySym keysym;
3526 enum keyfuncid funcid;
3527 char *spawn_name;
3528 };
3529 int keys_size = 0, keys_length = 0;
3530 struct key *keys = NULL;
3531
3532 /* mouse */
3533 enum { client_click, root_click };
3534 struct button {
3535 unsigned int action;
3536 unsigned int mask;
3537 unsigned int button;
3538 void (*func)(struct ws_win *, union arg *);
3539 union arg args;
3540 } buttons[] = {
3541 /* action key mouse button func args */
3542 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
3543 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
3544 { client_click, MODKEY, Button1, move, {0} },
3545 };
3546
3547 void
3548 update_modkey(unsigned int mod)
3549 {
3550 int i;
3551
3552 mod_key = mod;
3553 for (i = 0; i < keys_length; i++)
3554 if (keys[i].mod & ShiftMask)
3555 keys[i].mod = mod | ShiftMask;
3556 else
3557 keys[i].mod = mod;
3558
3559 for (i = 0; i < LENGTH(buttons); i++)
3560 if (buttons[i].mask & ShiftMask)
3561 buttons[i].mask = mod | ShiftMask;
3562 else
3563 buttons[i].mask = mod;
3564 }
3565
3566 /* spawn */
3567 struct spawn_prog {
3568 char *name;
3569 int argc;
3570 char **argv;
3571 };
3572
3573 int spawns_size = 0, spawns_length = 0;
3574 struct spawn_prog *spawns = NULL;
3575
3576 int
3577 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
3578 char ***ret_args)
3579 {
3580 struct spawn_prog *prog = NULL;
3581 int i;
3582 char *ap, **real_args;
3583
3584 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
3585
3586 /* find program */
3587 for (i = 0; i < spawns_length; i++) {
3588 if (!strcasecmp(spawn_name, spawns[i].name))
3589 prog = &spawns[i];
3590 }
3591 if (prog == NULL) {
3592 fprintf(stderr, "spawn_custom: program %s not found\n",
3593 spawn_name);
3594 return (-1);
3595 }
3596
3597 /* make room for expanded args */
3598 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
3599 err(1, "spawn_custom: calloc real_args");
3600
3601 /* expand spawn_args into real_args */
3602 for (i = 0; i < prog->argc; i++) {
3603 ap = prog->argv[i];
3604 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
3605 if (!strcasecmp(ap, "$bar_border")) {
3606 if ((real_args[i] =
3607 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
3608 == NULL)
3609 err(1, "spawn_custom border color");
3610 } else if (!strcasecmp(ap, "$bar_color")) {
3611 if ((real_args[i] =
3612 strdup(r->s->c[SWM_S_COLOR_BAR].name))
3613 == NULL)
3614 err(1, "spawn_custom bar color");
3615 } else if (!strcasecmp(ap, "$bar_font")) {
3616 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
3617 == NULL)
3618 err(1, "spawn_custom bar fonts");
3619 } else if (!strcasecmp(ap, "$bar_font_color")) {
3620 if ((real_args[i] =
3621 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
3622 == NULL)
3623 err(1, "spawn_custom color font");
3624 } else if (!strcasecmp(ap, "$color_focus")) {
3625 if ((real_args[i] =
3626 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
3627 == NULL)
3628 err(1, "spawn_custom color focus");
3629 } else if (!strcasecmp(ap, "$color_unfocus")) {
3630 if ((real_args[i] =
3631 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
3632 == NULL)
3633 err(1, "spawn_custom color unfocus");
3634 } else {
3635 /* no match --> copy as is */
3636 if ((real_args[i] = strdup(ap)) == NULL)
3637 err(1, "spawn_custom strdup(ap)");
3638 }
3639 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
3640 real_args[i]);
3641 }
3642
3643 #ifdef SWM_DEBUG
3644 if ((swm_debug & SWM_D_SPAWN) != 0) {
3645 fprintf(stderr, "spawn_custom: result = ");
3646 for (i = 0; i < prog->argc; i++)
3647 fprintf(stderr, "\"%s\" ", real_args[i]);
3648 fprintf(stderr, "\n");
3649 }
3650 #endif
3651 *ret_args = real_args;
3652 return (prog->argc);
3653 }
3654
3655 void
3656 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
3657 {
3658 union arg a;
3659 char **real_args;
3660 int spawn_argc, i;
3661
3662 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3663 return;
3664 a.argv = real_args;
3665 if (fork() == 0)
3666 spawn(r, &a, 1);
3667
3668 for (i = 0; i < spawn_argc; i++)
3669 free(real_args[i]);
3670 free(real_args);
3671 }
3672
3673 void
3674 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
3675 {
3676 union arg a;
3677 char **real_args;
3678 int i, spawn_argc;
3679
3680 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
3681 return;
3682 a.argv = real_args;
3683
3684 if (pipe(select_list_pipe) == -1)
3685 err(1, "pipe error");
3686 if (pipe(select_resp_pipe) == -1)
3687 err(1, "pipe error");
3688
3689 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
3690 err(1, "could not disable SIGPIPE");
3691 switch (*pid = fork()) {
3692 case -1:
3693 err(1, "cannot fork");
3694 break;
3695 case 0: /* child */
3696 if (dup2(select_list_pipe[0], 0) == -1)
3697 errx(1, "dup2");
3698 if (dup2(select_resp_pipe[1], 1) == -1)
3699 errx(1, "dup2");
3700 close(select_list_pipe[1]);
3701 close(select_resp_pipe[0]);
3702 spawn(r, &a, 0);
3703 break;
3704 default: /* parent */
3705 close(select_list_pipe[0]);
3706 close(select_resp_pipe[1]);
3707 break;
3708 }
3709
3710 for (i = 0; i < spawn_argc; i++)
3711 free(real_args[i]);
3712 free(real_args);
3713 }
3714
3715 void
3716 setspawn(struct spawn_prog *prog)
3717 {
3718 int i, j;
3719
3720 if (prog == NULL || prog->name == NULL)
3721 return;
3722
3723 /* find existing */
3724 for (i = 0; i < spawns_length; i++) {
3725 if (!strcmp(spawns[i].name, prog->name)) {
3726 /* found */
3727 if (prog->argv == NULL) {
3728 /* delete */
3729 DNPRINTF(SWM_D_SPAWN,
3730 "setspawn: delete #%d %s\n",
3731 i, spawns[i].name);
3732 free(spawns[i].name);
3733 for (j = 0; j < spawns[i].argc; j++)
3734 free(spawns[i].argv[j]);
3735 free(spawns[i].argv);
3736 j = spawns_length - 1;
3737 if (i < j)
3738 spawns[i] = spawns[j];
3739 spawns_length--;
3740 free(prog->name);
3741 } else {
3742 /* replace */
3743 DNPRINTF(SWM_D_SPAWN,
3744 "setspawn: replace #%d %s\n",
3745 i, spawns[i].name);
3746 free(spawns[i].name);
3747 for (j = 0; j < spawns[i].argc; j++)
3748 free(spawns[i].argv[j]);
3749 free(spawns[i].argv);
3750 spawns[i] = *prog;
3751 }
3752 /* found case handled */
3753 free(prog);
3754 return;
3755 }
3756 }
3757
3758 if (prog->argv == NULL) {
3759 fprintf(stderr,
3760 "error: setspawn: cannot find program %s", prog->name);
3761 free(prog);
3762 return;
3763 }
3764
3765 /* not found: add */
3766 if (spawns_size == 0 || spawns == NULL) {
3767 spawns_size = 4;
3768 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
3769 spawns = malloc((size_t)spawns_size *
3770 sizeof(struct spawn_prog));
3771 if (spawns == NULL) {
3772 fprintf(stderr, "setspawn: malloc failed\n");
3773 perror(" failed");
3774 quit(NULL, NULL);
3775 }
3776 } else if (spawns_length == spawns_size) {
3777 spawns_size *= 2;
3778 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
3779 spawns = realloc(spawns, (size_t)spawns_size *
3780 sizeof(struct spawn_prog));
3781 if (spawns == NULL) {
3782 fprintf(stderr, "setspawn: realloc failed\n");
3783 perror(" failed");
3784 quit(NULL, NULL);
3785 }
3786 }
3787
3788 if (spawns_length < spawns_size) {
3789 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
3790 spawns_length, prog->name);
3791 i = spawns_length++;
3792 spawns[i] = *prog;
3793 } else {
3794 fprintf(stderr, "spawns array problem?\n");
3795 if (spawns == NULL) {
3796 fprintf(stderr, "spawns array is NULL!\n");
3797 quit(NULL, NULL);
3798 }
3799 }
3800 free(prog);
3801 }
3802
3803 int
3804 setconfspawn(char *selector, char *value, int flags)
3805 {
3806 struct spawn_prog *prog;
3807 char *vp, *cp, *word;
3808
3809 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
3810 if ((prog = calloc(1, sizeof *prog)) == NULL)
3811 err(1, "setconfspawn: calloc prog");
3812 prog->name = strdup(selector);
3813 if (prog->name == NULL)
3814 err(1, "setconfspawn prog->name");
3815 if ((cp = vp = strdup(value)) == NULL)
3816 err(1, "setconfspawn: strdup(value) ");
3817 while ((word = strsep(&cp, " \t")) != NULL) {
3818 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
3819 if (cp)
3820 cp += (long)strspn(cp, " \t");
3821 if (strlen(word) > 0) {
3822 prog->argc++;
3823 prog->argv = realloc(prog->argv,
3824 prog->argc * sizeof(char *));
3825 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
3826 err(1, "setconfspawn: strdup");
3827 }
3828 }
3829 free(vp);
3830
3831 setspawn(prog);
3832
3833 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
3834 return (0);
3835 }
3836
3837 void
3838 setup_spawn(void)
3839 {
3840 setconfspawn("term", "xterm", 0);
3841 setconfspawn("screenshot_all", "screenshot.sh full", 0);
3842 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
3843 setconfspawn("lock", "xlock", 0);
3844 setconfspawn("initscr", "initscreen.sh", 0);
3845 setconfspawn("menu", "dmenu_run"
3846 " -fn $bar_font"
3847 " -nb $bar_color"
3848 " -nf $bar_font_color"
3849 " -sb $bar_border"
3850 " -sf $bar_color", 0);
3851 setconfspawn("uniconify", "dmenu"
3852 " -i"
3853 " -fn $bar_font"
3854 " -nb $bar_color"
3855 " -nf $bar_font_color"
3856 " -sb $bar_border"
3857 " -sf $bar_color", 0);
3858 }
3859
3860 /* key bindings */
3861 #define SWM_MODNAME_SIZE 32
3862 #define SWM_KEY_WS "\n+ \t"
3863 int
3864 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
3865 {
3866 char *cp, *name;
3867 KeySym uks;
3868 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
3869 if (mod == NULL || ks == NULL) {
3870 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
3871 return (1);
3872 }
3873 if (keystr == NULL || strlen(keystr) == 0) {
3874 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
3875 return (1);
3876 }
3877 cp = keystr;
3878 *ks = NoSymbol;
3879 *mod = 0;
3880 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
3881 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
3882 if (cp)
3883 cp += (long)strspn(cp, SWM_KEY_WS);
3884 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
3885 *mod |= currmod;
3886 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
3887 *mod |= Mod1Mask;
3888 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
3889 *mod += Mod2Mask;
3890 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
3891 *mod |= Mod3Mask;
3892 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
3893 *mod |= Mod4Mask;
3894 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
3895 *mod |= ShiftMask;
3896 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
3897 *mod |= ControlMask;
3898 else {
3899 *ks = XStringToKeysym(name);
3900 XConvertCase(*ks, ks, &uks);
3901 if (ks == NoSymbol) {
3902 DNPRINTF(SWM_D_KEY,
3903 "parsekeys: invalid key %s\n",
3904 name);
3905 return (1);
3906 }
3907 }
3908 }
3909 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
3910 return (0);
3911 }
3912
3913 char *
3914 strdupsafe(char *str)
3915 {
3916 if (str == NULL)
3917 return (NULL);
3918 else
3919 return (strdup(str));
3920 }
3921
3922 void
3923 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
3924 {
3925 int i, j;
3926 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
3927 keyfuncs[kfid].name, spawn_name);
3928 /* find existing */
3929 for (i = 0; i < keys_length; i++) {
3930 if (keys[i].mod == mod && keys[i].keysym == ks) {
3931 if (kfid == kf_invalid) {
3932 /* found: delete */
3933 DNPRINTF(SWM_D_KEY,
3934 "setkeybinding: delete #%d %s\n",
3935 i, keyfuncs[keys[i].funcid].name);
3936 free(keys[i].spawn_name);
3937 j = keys_length - 1;
3938 if (i < j)
3939 keys[i] = keys[j];
3940 keys_length--;
3941 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3942 return;
3943 } else {
3944 /* found: replace */
3945 DNPRINTF(SWM_D_KEY,
3946 "setkeybinding: replace #%d %s %s\n",
3947 i, keyfuncs[keys[i].funcid].name,
3948 spawn_name);
3949 free(keys[i].spawn_name);
3950 keys[i].mod = mod;
3951 keys[i].keysym = ks;
3952 keys[i].funcid = kfid;
3953 keys[i].spawn_name = strdupsafe(spawn_name);
3954 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3955 return;
3956 }
3957 }
3958 }
3959 if (kfid == kf_invalid) {
3960 fprintf(stderr,
3961 "error: setkeybinding: cannot find mod/key combination");
3962 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
3963 return;
3964 }
3965 /* not found: add */
3966 if (keys_size == 0 || keys == NULL) {
3967 keys_size = 4;
3968 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
3969 keys = malloc((size_t)keys_size * sizeof(struct key));
3970 if (!keys) {
3971 fprintf(stderr, "malloc failed\n");
3972 perror(" failed");
3973 quit(NULL, NULL);
3974 }
3975 } else if (keys_length == keys_size) {
3976 keys_size *= 2;
3977 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
3978 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
3979 if (!keys) {
3980 fprintf(stderr, "realloc failed\n");
3981 perror(" failed");
3982 quit(NULL, NULL);
3983 }
3984 }
3985 if (keys_length < keys_size) {
3986 j = keys_length++;
3987 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
3988 j, keyfuncs[kfid].name, spawn_name);
3989 keys[j].mod = mod;
3990 keys[j].keysym = ks;
3991 keys[j].funcid = kfid;
3992 keys[j].spawn_name = strdupsafe(spawn_name);
3993 } else {
3994 fprintf(stderr, "keys array problem?\n");
3995 if (!keys) {
3996 fprintf(stderr, "keys array problem\n");
3997 quit(NULL, NULL);
3998 }
3999 }
4000 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4001 }
4002
4003 int
4004 setconfbinding(char *selector, char *value, int flags)
4005 {
4006 enum keyfuncid kfid;
4007 unsigned int mod;
4008 KeySym ks;
4009 int i;
4010 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4011 if (selector == NULL) {
4012 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4013 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4014 kfid = kf_invalid;
4015 setkeybinding(mod, ks, kfid, NULL);
4016 return (0);
4017 } else
4018 return (1);
4019 }
4020 /* search by key function name */
4021 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4022 if (strncasecmp(selector, keyfuncs[kfid].name,
4023 SWM_FUNCNAME_LEN) == 0) {
4024 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4025 selector);
4026 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4027 setkeybinding(mod, ks, kfid, NULL);
4028 return (0);
4029 } else
4030 return (1);
4031 }
4032 }
4033 /* search by custom spawn name */
4034 for (i = 0; i < spawns_length; i++) {
4035 if (strcasecmp(selector, spawns[i].name) == 0) {
4036 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4037 selector);
4038 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4039 setkeybinding(mod, ks, kf_spawn_custom,
4040 spawns[i].name);
4041 return (0);
4042 } else
4043 return (1);
4044 }
4045 }
4046 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4047 return (1);
4048 }
4049
4050 void
4051 setup_keys(void)
4052 {
4053 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4054 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4055 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4056 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4057 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4058 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4059 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4060 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4061 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4062 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4063 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4064 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4065 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4066 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4067 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
4068 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4069 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4070 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4071 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4072 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4073 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4074 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4075 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4076 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4077 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4078 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4079 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4080 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4081 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4082 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4083 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4084 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4085 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4086 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4087 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4088 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4089 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4090 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4091 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4092 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4093 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4094 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4095 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4096 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4097 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4098 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4099 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4100 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4101 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
4102 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
4103 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4104 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4105 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
4106 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
4107 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4108 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4109 #ifdef SWM_DEBUG
4110 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4111 #endif
4112 }
4113
4114 void
4115 updatenumlockmask(void)
4116 {
4117 unsigned int i, j;
4118 XModifierKeymap *modmap;
4119
4120 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4121 numlockmask = 0;
4122 modmap = XGetModifierMapping(display);
4123 for (i = 0; i < 8; i++)
4124 for (j = 0; j < modmap->max_keypermod; j++)
4125 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4126 == XKeysymToKeycode(display, XK_Num_Lock))
4127 numlockmask = (1 << i);
4128
4129 XFreeModifiermap(modmap);
4130 }
4131
4132 void
4133 grabkeys(void)
4134 {
4135 unsigned int i, j, k;
4136 KeyCode code;
4137 unsigned int modifiers[] =
4138 { 0, LockMask, numlockmask, numlockmask | LockMask };
4139
4140 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4141 updatenumlockmask();
4142
4143 for (k = 0; k < ScreenCount(display); k++) {
4144 if (TAILQ_EMPTY(&screens[k].rl))
4145 continue;
4146 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4147 for (i = 0; i < keys_length; i++) {
4148 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4149 for (j = 0; j < LENGTH(modifiers); j++)
4150 XGrabKey(display, code,
4151 keys[i].mod | modifiers[j],
4152 screens[k].root, True,
4153 GrabModeAsync, GrabModeAsync);
4154 }
4155 }
4156 }
4157
4158 void
4159 grabbuttons(struct ws_win *win, int focused)
4160 {
4161 unsigned int i, j;
4162 unsigned int modifiers[] =
4163 { 0, LockMask, numlockmask, numlockmask|LockMask };
4164
4165 updatenumlockmask();
4166 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4167 if (focused) {
4168 for (i = 0; i < LENGTH(buttons); i++)
4169 if (buttons[i].action == client_click)
4170 for (j = 0; j < LENGTH(modifiers); j++)
4171 XGrabButton(display, buttons[i].button,
4172 buttons[i].mask | modifiers[j],
4173 win->id, False, BUTTONMASK,
4174 GrabModeAsync, GrabModeSync, None,
4175 None);
4176 } else
4177 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4178 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4179 }
4180
4181 const char *quirkname[] = {
4182 "NONE", /* config string for "no value" */
4183 "FLOAT",
4184 "TRANSSZ",
4185 "ANYWHERE",
4186 "XTERM_FONTADJ",
4187 "FULLSCREEN",
4188 };
4189
4190 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4191 #define SWM_Q_WS "\n|+ \t"
4192 int
4193 parsequirks(char *qstr, unsigned long *quirk)
4194 {
4195 char *cp, *name;
4196 int i;
4197
4198 if (quirk == NULL)
4199 return (1);
4200
4201 cp = qstr;
4202 *quirk = 0;
4203 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4204 if (cp)
4205 cp += (long)strspn(cp, SWM_Q_WS);
4206 for (i = 0; i < LENGTH(quirkname); i++) {
4207 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4208 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
4209 if (i == 0) {
4210 *quirk = 0;
4211 return (0);
4212 }
4213 *quirk |= 1 << (i-1);
4214 break;
4215 }
4216 }
4217 if (i >= LENGTH(quirkname)) {
4218 DNPRINTF(SWM_D_QUIRK,
4219 "parsequirks: invalid quirk [%s]\n", name);
4220 return (1);
4221 }
4222 }
4223 return (0);
4224 }
4225
4226 void
4227 setquirk(const char *class, const char *name, const int quirk)
4228 {
4229 int i, j;
4230
4231 /* find existing */
4232 for (i = 0; i < quirks_length; i++) {
4233 if (!strcmp(quirks[i].class, class) &&
4234 !strcmp(quirks[i].name, name)) {
4235 if (!quirk) {
4236 /* found: delete */
4237 DNPRINTF(SWM_D_QUIRK,
4238 "setquirk: delete #%d %s:%s\n",
4239 i, quirks[i].class, quirks[i].name);
4240 free(quirks[i].class);
4241 free(quirks[i].name);
4242 j = quirks_length - 1;
4243 if (i < j)
4244 quirks[i] = quirks[j];
4245 quirks_length--;
4246 return;
4247 } else {
4248 /* found: replace */
4249 DNPRINTF(SWM_D_QUIRK,
4250 "setquirk: replace #%d %s:%s\n",
4251 i, quirks[i].class, quirks[i].name);
4252 free(quirks[i].class);
4253 free(quirks[i].name);
4254 quirks[i].class = strdup(class);
4255 quirks[i].name = strdup(name);
4256 quirks[i].quirk = quirk;
4257 return;
4258 }
4259 }
4260 }
4261 if (!quirk) {
4262 fprintf(stderr,
4263 "error: setquirk: cannot find class/name combination");
4264 return;
4265 }
4266 /* not found: add */
4267 if (quirks_size == 0 || quirks == NULL) {
4268 quirks_size = 4;
4269 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4270 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4271 if (!quirks) {
4272 fprintf(stderr, "setquirk: malloc failed\n");
4273 perror(" failed");
4274 quit(NULL, NULL);
4275 }
4276 } else if (quirks_length == quirks_size) {
4277 quirks_size *= 2;
4278 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4279 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
4280 if (!quirks) {
4281 fprintf(stderr, "setquirk: realloc failed\n");
4282 perror(" failed");
4283 quit(NULL, NULL);
4284 }
4285 }
4286 if (quirks_length < quirks_size) {
4287 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4288 j = quirks_length++;
4289 quirks[j].class = strdup(class);
4290 quirks[j].name = strdup(name);
4291 quirks[j].quirk = quirk;
4292 } else {
4293 fprintf(stderr, "quirks array problem?\n");
4294 if (!quirks) {
4295 fprintf(stderr, "quirks array problem!\n");
4296 quit(NULL, NULL);
4297 }
4298 }
4299 }
4300
4301 int
4302 setconfquirk(char *selector, char *value, int flags)
4303 {
4304 char *cp, *class, *name;
4305 int retval;
4306 unsigned long quirks;
4307 if (selector == NULL)
4308 return (0);
4309 if ((cp = strchr(selector, ':')) == NULL)
4310 return (0);
4311 *cp = '\0';
4312 class = selector;
4313 name = cp + 1;
4314 if ((retval = parsequirks(value, &quirks)) == 0)
4315 setquirk(class, name, quirks);
4316 return (retval);
4317 }
4318
4319 void
4320 setup_quirks(void)
4321 {
4322 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4323 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4324 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4325 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4326 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4327 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4328 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4329 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4330 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4331 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4332 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
4333 setquirk("pcb", "pcb", SWM_Q_FLOAT);
4334 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
4335 }
4336
4337 /* conf file stuff */
4338 #define SWM_CONF_FILE "scrotwm.conf"
4339
4340 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH, SWM_S_STACK_ENABLED,
4341 SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT, SWM_S_CYCLE_EMPTY,
4342 SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH,
4343 SWM_S_TITLE_CLASS_ENABLED, SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED,
4344 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH, SWM_S_BAR_FONT,
4345 SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO,
4346 SWM_S_BAR_AT_BOTTOM
4347 };
4348
4349 int
4350 setconfvalue(char *selector, char *value, int flags)
4351 {
4352 switch (flags) {
4353 case SWM_S_BAR_DELAY:
4354 bar_delay = atoi(value);
4355 break;
4356 case SWM_S_BAR_ENABLED:
4357 bar_enabled = atoi(value);
4358 break;
4359 case SWM_S_BAR_BORDER_WIDTH:
4360 bar_border_width = atoi(value);
4361 break;
4362 case SWM_S_BAR_AT_BOTTOM:
4363 bar_at_bottom = atoi(value);
4364 break;
4365 case SWM_S_STACK_ENABLED:
4366 stack_enabled = atoi(value);
4367 break;
4368 case SWM_S_CLOCK_ENABLED:
4369 clock_enabled = atoi(value);
4370 break;
4371 case SWM_S_CLOCK_FORMAT:
4372 #ifndef SWM_DENY_CLOCK_FORMAT
4373 free(clock_format);
4374 if ((clock_format = strdup(value)) == NULL)
4375 err(1, "setconfvalue: clock_format");
4376 #endif
4377 break;
4378 case SWM_S_CYCLE_EMPTY:
4379 cycle_empty = atoi(value);
4380 break;
4381 case SWM_S_CYCLE_VISIBLE:
4382 cycle_visible = atoi(value);
4383 break;
4384 case SWM_S_SS_ENABLED:
4385 ss_enabled = atoi(value);
4386 break;
4387 case SWM_S_TERM_WIDTH:
4388 term_width = atoi(value);
4389 break;
4390 case SWM_S_TITLE_CLASS_ENABLED:
4391 title_class_enabled = atoi(value);
4392 break;
4393 case SWM_S_WINDOW_NAME_ENABLED:
4394 window_name_enabled = atoi(value);
4395 break;
4396 case SWM_S_TITLE_NAME_ENABLED:
4397 title_name_enabled = atoi(value);
4398 break;
4399 case SWM_S_FOCUS_MODE:
4400 if (!strcmp(value, "default"))
4401 focus_mode = SWM_FOCUS_DEFAULT;
4402 else if (!strcmp(value, "follow_cursor"))
4403 focus_mode = SWM_FOCUS_FOLLOW;
4404 else if (!strcmp(value, "synergy"))
4405 focus_mode = SWM_FOCUS_SYNERGY;
4406 else
4407 err(1, "focus_mode");
4408 break;
4409 case SWM_S_DISABLE_BORDER:
4410 disable_border = atoi(value);
4411 break;
4412 case SWM_S_BORDER_WIDTH:
4413 border_width = atoi(value);
4414 break;
4415 case SWM_S_BAR_FONT:
4416 free(bar_fonts[0]);
4417 if ((bar_fonts[0] = strdup(value)) == NULL)
4418 err(1, "setconfvalue: bar_font");
4419 break;
4420 case SWM_S_BAR_ACTION:
4421 free(bar_argv[0]);
4422 if ((bar_argv[0] = strdup(value)) == NULL)
4423 err(1, "setconfvalue: bar_action");
4424 break;
4425 case SWM_S_SPAWN_TERM:
4426 free(spawn_term[0]);
4427 if ((spawn_term[0] = strdup(value)) == NULL)
4428 err(1, "setconfvalue: spawn_term");
4429 break;
4430 case SWM_S_SS_APP:
4431 break;
4432 case SWM_S_DIALOG_RATIO:
4433 dialog_ratio = atof(value);
4434 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
4435 dialog_ratio = .6;
4436 break;
4437 default:
4438 return (1);
4439 }
4440 return (0);
4441 }
4442
4443 int
4444 setconfmodkey(char *selector, char *value, int flags)
4445 {
4446 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
4447 update_modkey(Mod1Mask);
4448 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
4449 update_modkey(Mod2Mask);
4450 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
4451 update_modkey(Mod3Mask);
4452 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
4453 update_modkey(Mod4Mask);
4454 else
4455 return (1);
4456 return (0);
4457 }
4458
4459 int
4460 setconfcolor(char *selector, char *value, int flags)
4461 {
4462 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
4463 return (0);
4464 }
4465
4466 int
4467 setconfregion(char *selector, char *value, int flags)
4468 {
4469 custom_region(value);
4470 return (0);
4471 }
4472
4473 /* config options */
4474 struct config_option {
4475 char *optname;
4476 int (*func)(char*, char*, int);
4477 int funcflags;
4478 };
4479 struct config_option configopt[] = {
4480 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
4481 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
4482 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
4483 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
4484 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
4485 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
4486 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
4487 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
4488 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
4489 { "bind", setconfbinding, 0 },
4490 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
4491 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
4492 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
4493 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
4494 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
4495 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
4496 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
4497 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
4498 { "modkey", setconfmodkey, 0 },
4499 { "program", setconfspawn, 0 },
4500 { "quirk", setconfquirk, 0 },
4501 { "region", setconfregion, 0 },
4502 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
4503 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
4504 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
4505 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
4506 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
4507 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
4508 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
4509 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
4510 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
4511 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
4512 };
4513
4514
4515 int
4516 conf_load(char *filename)
4517 {
4518 FILE *config;
4519 char *line, *cp, *optsub, *optval;
4520 size_t linelen, lineno = 0;
4521 int wordlen, i, optind;
4522 struct config_option *opt;
4523
4524 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
4525
4526 if (filename == NULL) {
4527 fprintf(stderr, "conf_load: no filename\n");
4528 return (1);
4529 }
4530 if ((config = fopen(filename, "r")) == NULL) {
4531 warn("conf_load: fopen");
4532 return (1);
4533 }
4534
4535 while (!feof(config)) {
4536 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
4537 == NULL) {
4538 if (ferror(config))
4539 err(1, "%s", filename);
4540 else
4541 continue;
4542 }
4543 cp = line;
4544 cp += strspn(cp, " \t\n"); /* eat whitespace */
4545 if (cp[0] == '\0') {
4546 /* empty line */
4547 free(line);
4548 continue;
4549 }
4550 /* get config option */
4551 wordlen = strcspn(cp, "=[ \t\n");
4552 if (wordlen == 0) {
4553 warnx("%s: line %zd: no option found",
4554 filename, lineno);
4555 return (1);
4556 }
4557 optind = -1;
4558 for (i = 0; i < LENGTH(configopt); i++) {
4559 opt = &configopt[i];
4560 if (!strncasecmp(cp, opt->optname, wordlen) &&
4561 strlen(opt->optname) == wordlen) {
4562 optind = i;
4563 break;
4564 }
4565 }
4566 if (optind == -1) {
4567 warnx("%s: line %zd: unknown option %.*s",
4568 filename, lineno, wordlen, cp);
4569 return (1);
4570 }
4571 cp += wordlen;
4572 cp += strspn(cp, " \t\n"); /* eat whitespace */
4573 /* get [selector] if any */
4574 optsub = NULL;
4575 if (*cp == '[') {
4576 cp++;
4577 wordlen = strcspn(cp, "]");
4578 if (*cp != ']') {
4579 if (wordlen == 0) {
4580 warnx("%s: line %zd: syntax error",
4581 filename, lineno);
4582 return (1);
4583 }
4584 asprintf(&optsub, "%.*s", wordlen, cp);
4585 }
4586 cp += wordlen;
4587 cp += strspn(cp, "] \t\n"); /* eat trailing */
4588 }
4589 cp += strspn(cp, "= \t\n"); /* eat trailing */
4590 /* get RHS value */
4591 optval = strdup(cp);
4592 /* call function to deal with it all */
4593 if (configopt[optind].func(optsub, optval,
4594 configopt[optind].funcflags) != 0) {
4595 fprintf(stderr, "%s line %zd: %s\n",
4596 filename, lineno, line);
4597 errx(1, "%s: line %zd: invalid data for %s",
4598 filename, lineno, configopt[optind].optname);
4599 }
4600 free(optval);
4601 free(optsub);
4602 free(line);
4603 }
4604
4605 fclose(config);
4606 DNPRINTF(SWM_D_CONF, "conf_load end\n");
4607
4608 return (0);
4609 }
4610
4611 void
4612 set_child_transient(struct ws_win *win, Window *trans)
4613 {
4614 struct ws_win *parent, *w;
4615 XWMHints *wmh = NULL;
4616 struct swm_region *r;
4617 struct workspace *ws;
4618
4619 parent = find_window(win->transient);
4620 if (parent)
4621 parent->child_trans = win;
4622 else {
4623 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
4624 " for %lu trans %lu\n", win->id, win->transient);
4625
4626 if (win->hints == NULL) {
4627 fprintf(stderr, "no hints for %lu\n", win->id);
4628 return;
4629 }
4630
4631 r = root_to_region(win->wa.root);
4632 ws = r->ws;
4633 /* parent doen't exist in our window list */
4634 TAILQ_FOREACH(w, &ws->winlist, entry) {
4635 if (wmh)
4636 XFree(wmh);
4637
4638 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
4639 fprintf(stderr, "can't get hints for %lu\n",
4640 w->id);
4641 continue;
4642 }
4643
4644 if (win->hints->window_group != wmh->window_group)
4645 continue;
4646
4647 w->child_trans = win;
4648 win->transient = w->id;
4649 *trans = w->id;
4650 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
4651 "transient to %lu\n", win->transient);
4652 break;
4653 }
4654 }
4655
4656 if (wmh)
4657 XFree(wmh);
4658 }
4659
4660 struct ws_win *
4661 manage_window(Window id)
4662 {
4663 Window trans = 0;
4664 struct workspace *ws;
4665 struct ws_win *win, *ww;
4666 int format, i, ws_idx, n, border_me = 0;
4667 unsigned long nitems, bytes;
4668 Atom ws_idx_atom = 0, type;
4669 Atom *prot = NULL, *pp;
4670 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
4671 struct swm_region *r;
4672 long mask;
4673 const char *errstr;
4674 XWindowChanges wc;
4675
4676 if ((win = find_window(id)) != NULL)
4677 return (win); /* already being managed */
4678
4679 /* see if we are on the unmanaged list */
4680 if ((win = find_unmanaged_window(id)) != NULL) {
4681 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
4682 "%lu\n", win->id);
4683 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4684 if (win->transient) {
4685 set_child_transient(win, &trans);
4686 } if (trans && (ww = find_window(trans)))
4687 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
4688 else
4689 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
4690 ewmh_update_actions(win);
4691 return (win);
4692 }
4693
4694 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
4695 errx(1, "calloc: failed to allocate memory for new window");
4696
4697 win->id = id;
4698
4699 /* Get all the window data in one shot */
4700 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4701 if (ws_idx_atom)
4702 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
4703 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
4704 XGetWindowAttributes(display, id, &win->wa);
4705 XGetWMNormalHints(display, id, &win->sh, &mask);
4706 win->hints = XGetWMHints(display, id);
4707 XGetTransientForHint(display, id, &trans);
4708 if (trans) {
4709 win->transient = trans;
4710 set_child_transient(win, &trans);
4711 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
4712 win->id, win->transient);
4713 }
4714
4715 /* get supported protocols */
4716 if (XGetWMProtocols(display, id, &prot, &n)) {
4717 for (i = 0, pp = prot; i < n; i++, pp++) {
4718 if (*pp == takefocus)
4719 win->take_focus = 1;
4720 if (*pp == adelete)
4721 win->can_delete = 1;
4722 }
4723 if (prot)
4724 XFree(prot);
4725 }
4726
4727 win->iconic = get_iconic(win);
4728
4729 /*
4730 * Figure out where to put the window. If it was previously assigned to
4731 * a workspace (either by spawn() or manually moving), and isn't
4732 * transient, * put it in the same workspace
4733 */
4734 r = root_to_region(win->wa.root);
4735 if (prop && win->transient == 0) {
4736 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
4737 ws_idx = strtonum(prop, 0, 9, &errstr);
4738 if (errstr) {
4739 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
4740 errstr, prop);
4741 }
4742 ws = &r->s->ws[ws_idx];
4743 } else {
4744 ws = r->ws;
4745 /* this should launch transients in the same ws as parent */
4746 if (id && trans)
4747 if ((ww = find_window(trans)) != NULL)
4748 if (ws->r) {
4749 ws = ww->ws;
4750 if (ww->ws->r)
4751 r = ww->ws->r;
4752 else
4753 fprintf(stderr,
4754 "fix this bug mcbride\n");
4755 border_me = 1;
4756 }
4757 }
4758
4759 /* set up the window layout */
4760 win->id = id;
4761 win->ws = ws;
4762 win->s = r->s; /* this never changes */
4763 if (trans && (ww = find_window(trans)))
4764 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
4765 else
4766 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
4767
4768 win->g.w = win->wa.width;
4769 win->g.h = win->wa.height;
4770 win->g.x = win->wa.x;
4771 win->g.y = win->wa.y;
4772 win->g_floatvalid = 0;
4773 win->floatmaxed = 0;
4774 win->ewmh_flags = 0;
4775
4776 /* Set window properties so we can remember this after reincarnation */
4777 if (ws_idx_atom && prop == NULL &&
4778 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
4779 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
4780 ws_idx_str);
4781 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
4782 PropModeReplace, ws_idx_str, SWM_PROPLEN);
4783 }
4784 if (prop)
4785 XFree(prop);
4786
4787 ewmh_autoquirk(win);
4788
4789 if (XGetClassHint(display, win->id, &win->ch)) {
4790 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
4791 win->ch.res_class, win->ch.res_name);
4792
4793 /* java is retarded so treat it special */
4794 if (strstr(win->ch.res_name, "sun-awt")) {
4795 win->java = 1;
4796 border_me = 1;
4797 }
4798
4799 for (i = 0; i < quirks_length; i++){
4800 if (!strcmp(win->ch.res_class, quirks[i].class) &&
4801 !strcmp(win->ch.res_name, quirks[i].name)) {
4802 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
4803 win->ch.res_class, win->ch.res_name);
4804 if (quirks[i].quirk & SWM_Q_FLOAT) {
4805 win->floating = 1;
4806 border_me = 1;
4807 }
4808 win->quirks = quirks[i].quirk;
4809 }
4810 }
4811 }
4812
4813 /* alter window position if quirky */
4814 if (win->quirks & SWM_Q_ANYWHERE) {
4815 win->manual = 1; /* don't center the quirky windows */
4816 bzero(&wc, sizeof wc);
4817 mask = 0;
4818 if (bar_enabled && win->g.y < bar_height) {
4819 win->g.y = wc.y = bar_height;
4820 mask |= CWY;
4821 }
4822 if (win->g.w + win->g.x > WIDTH(r)) {
4823 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
4824 mask |= CWX;
4825 }
4826 border_me = 1;
4827 }
4828
4829 /* Reset font sizes (the bruteforce way; no default keybinding). */
4830 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
4831 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4832 fake_keypress(win, XK_KP_Subtract, ShiftMask);
4833 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
4834 fake_keypress(win, XK_KP_Add, ShiftMask);
4835 }
4836
4837 ewmh_get_win_state(win);
4838 ewmh_update_actions(win);
4839 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
4840
4841 /* border me */
4842 if (border_me) {
4843 bzero(&wc, sizeof wc);
4844 wc.border_width = border_width;
4845 mask = CWBorderWidth;
4846 XConfigureWindow(display, win->id, mask, &wc);
4847 }
4848
4849 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
4850 PropertyChangeMask | StructureNotifyMask);
4851
4852 /* floaters need to be mapped if they are in the current workspace */
4853 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
4854 XMapRaised(display, win->id);
4855
4856 return (win);
4857 }
4858
4859 void
4860 free_window(struct ws_win *win)
4861 {
4862 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
4863
4864 if (win == NULL)
4865 return;
4866
4867 /* needed for restart wm */
4868 set_win_state(win, WithdrawnState);
4869
4870 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
4871
4872 if (win->ch.res_class)
4873 XFree(win->ch.res_class);
4874 if (win->ch.res_name)
4875 XFree(win->ch.res_name);
4876
4877 kill_refs(win);
4878
4879 /* paint memory */
4880 memset(win, 0xff, sizeof *win); /* XXX kill later */
4881
4882 free(win);
4883 }
4884
4885 void
4886 unmanage_window(struct ws_win *win)
4887 {
4888 struct ws_win *parent;
4889
4890 if (win == NULL)
4891 return;
4892
4893 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
4894
4895 if (win->transient) {
4896 parent = find_window(win->transient);
4897 if (parent)
4898 parent->child_trans = NULL;
4899 }
4900
4901 /* focus on root just in case */
4902 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
4903
4904 focus_prev(win);
4905
4906 TAILQ_REMOVE(&win->ws->winlist, win, entry);
4907 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
4908
4909 kill_refs(win);
4910 }
4911
4912 void
4913 focus_magic(struct ws_win *win)
4914 {
4915 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
4916
4917 if (win == NULL)
4918 return;
4919
4920 if (win->child_trans) {
4921 /* win = parent & has a transient so focus on that */
4922 if (win->java) {
4923 focus_win(win->child_trans);
4924 if (win->child_trans->take_focus)
4925 client_msg(win, takefocus);
4926 } else {
4927 /* make sure transient hasn't dissapeared */
4928 if (validate_win(win->child_trans) == 0) {
4929 focus_win(win->child_trans);
4930 if (win->child_trans->take_focus)
4931 client_msg(win->child_trans, takefocus);
4932 } else {
4933 win->child_trans = NULL;
4934 focus_win(win);
4935 if (win->take_focus)
4936 client_msg(win, takefocus);
4937 }
4938 }
4939 } else {
4940 /* regular focus */
4941 focus_win(win);
4942 if (win->take_focus)
4943 client_msg(win, takefocus);
4944 }
4945 }
4946
4947 void
4948 expose(XEvent *e)
4949 {
4950 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
4951 }
4952
4953 void
4954 keypress(XEvent *e)
4955 {
4956 unsigned int i;
4957 KeySym keysym;
4958 XKeyEvent *ev = &e->xkey;
4959
4960 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
4961
4962 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
4963 for (i = 0; i < keys_length; i++)
4964 if (keysym == keys[i].keysym
4965 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
4966 && keyfuncs[keys[i].funcid].func) {
4967 if (keys[i].funcid == kf_spawn_custom)
4968 spawn_custom(
4969 root_to_region(ev->root),
4970 &(keyfuncs[keys[i].funcid].args),
4971 keys[i].spawn_name
4972 );
4973 else
4974 keyfuncs[keys[i].funcid].func(
4975 root_to_region(ev->root),
4976 &(keyfuncs[keys[i].funcid].args)
4977 );
4978 }
4979 }
4980
4981 void
4982 buttonpress(XEvent *e)
4983 {
4984 struct ws_win *win;
4985 int i, action;
4986 XButtonPressedEvent *ev = &e->xbutton;
4987
4988 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
4989
4990 action = root_click;
4991 if ((win = find_window(ev->window)) == NULL)
4992 return;
4993
4994 focus_magic(win);
4995 action = client_click;
4996
4997 for (i = 0; i < LENGTH(buttons); i++)
4998 if (action == buttons[i].action && buttons[i].func &&
4999 buttons[i].button == ev->button &&
5000 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5001 buttons[i].func(win, &buttons[i].args);
5002 }
5003
5004 void
5005 configurerequest(XEvent *e)
5006 {
5007 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5008 struct ws_win *win;
5009 int new = 0;
5010 XWindowChanges wc;
5011
5012 if ((win = find_window(ev->window)) == NULL)
5013 if ((win = find_unmanaged_window(ev->window)) == NULL)
5014 new = 1;
5015
5016 if (new) {
5017 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5018 ev->window);
5019 bzero(&wc, sizeof wc);
5020 wc.x = ev->x;
5021 wc.y = ev->y;
5022 wc.width = ev->width;
5023 wc.height = ev->height;
5024 wc.border_width = ev->border_width;
5025 wc.sibling = ev->above;
5026 wc.stack_mode = ev->detail;
5027 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5028 } else {
5029 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5030 ev->window);
5031 config_win(win, ev);
5032 }
5033 }
5034
5035 void
5036 configurenotify(XEvent *e)
5037 {
5038 struct ws_win *win;
5039 long mask;
5040
5041 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5042 e->xconfigure.window);
5043
5044 win = find_window(e->xconfigure.window);
5045 if (win) {
5046 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5047 adjust_font(win);
5048 if (font_adjusted)
5049 stack();
5050 if (focus_mode == SWM_FOCUS_DEFAULT)
5051 drain_enter_notify();
5052 }
5053 }
5054
5055 void
5056 destroynotify(XEvent *e)
5057 {
5058 struct ws_win *win;
5059 XDestroyWindowEvent *ev = &e->xdestroywindow;
5060
5061 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5062
5063 if ((win = find_window(ev->window)) == NULL) {
5064 if ((win = find_unmanaged_window(ev->window)) == NULL)
5065 return;
5066 free_window(win);
5067 return;
5068 }
5069
5070 /* make sure we focus on something */
5071 win->floating = 0;
5072
5073 unmanage_window(win);
5074 stack();
5075 if (focus_mode == SWM_FOCUS_DEFAULT)
5076 drain_enter_notify();
5077 free_window(win);
5078 }
5079
5080 void
5081 enternotify(XEvent *e)
5082 {
5083 XCrossingEvent *ev = &e->xcrossing;
5084 XEvent cne;
5085 struct ws_win *win;
5086 #if 0
5087 struct ws_win *w;
5088 Window focus_return;
5089 int revert_to_return;
5090 #endif
5091 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5092 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5093 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5094 ev->same_screen, ev->focus, ev->state);
5095
5096 switch (focus_mode) {
5097 case SWM_FOCUS_DEFAULT:
5098 break;
5099 case SWM_FOCUS_FOLLOW:
5100 break;
5101 case SWM_FOCUS_SYNERGY:
5102 #if 0
5103 /*
5104 * all these checks need to be in this order because the
5105 * XCheckTypedWindowEvent relies on weeding out the previous events
5106 *
5107 * making this code an option would enable a follow mouse for focus
5108 * feature
5109 */
5110
5111 /*
5112 * state is set when we are switching workspaces and focus is set when
5113 * the window or a subwindow already has focus (occurs during restart).
5114 *
5115 * Only honor the focus flag if last_focus_event is not FocusOut,
5116 * this allows scrotwm to continue to control focus when another
5117 * program is also playing with it.
5118 */
5119 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
5120 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
5121 return;
5122 }
5123
5124 /*
5125 * happens when a window is created or destroyed and the border
5126 * crosses the mouse pointer and when switching ws
5127 *
5128 * we need the subwindow test to see if we came from root in order
5129 * to give focus to floaters
5130 */
5131 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
5132 ev->subwindow == 0) {
5133 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
5134 return;
5135 }
5136
5137 /* this window already has focus */
5138 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
5139 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
5140 return;
5141 }
5142
5143 /* this window is being deleted or moved to another ws */
5144 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
5145 &cne) == True) {
5146 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
5147 XPutBackEvent(display, &cne);
5148 return;
5149 }
5150
5151 if ((win = find_window(ev->window)) == NULL) {
5152 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5153 return;
5154 }
5155
5156 /*
5157 * In fullstack kill all enters unless they come from a different ws
5158 * (i.e. another region) or focus has been grabbed externally.
5159 */
5160 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
5161 last_focus_event != FocusOut) {
5162 XGetInputFocus(display, &focus_return, &revert_to_return);
5163 if ((w = find_window(focus_return)) == NULL ||
5164 w->ws == win->ws) {
5165 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
5166 return;
5167 }
5168 }
5169 #endif
5170 break;
5171 }
5172
5173 if ((win = find_window(ev->window)) == NULL) {
5174 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
5175 return;
5176 }
5177
5178 /*
5179 * if we have more enternotifies let them handle it in due time
5180 */
5181 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
5182 DNPRINTF(SWM_D_EVENT,
5183 "ignoring enternotify: got more enternotify\n");
5184 XPutBackEvent(display, &cne);
5185 return;
5186 }
5187
5188 focus_magic(win);
5189 }
5190
5191 /* lets us use one switch statement for arbitrary mode/detail combinations */
5192 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
5193
5194 void
5195 focusevent(XEvent *e)
5196 {
5197 #if 0
5198 struct ws_win *win;
5199 u_int32_t mode_detail;
5200 XFocusChangeEvent *ev = &e->xfocus;
5201
5202 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
5203 ev->type == FocusIn ? "entering" : "leaving",
5204 ev->window, ev->mode, ev->detail);
5205
5206 if (last_focus_event == ev->type) {
5207 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
5208 return;
5209 }
5210
5211 last_focus_event = ev->type;
5212 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
5213
5214 switch (mode_detail) {
5215 /* synergy client focus operations */
5216 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
5217 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
5218
5219 /* synergy server focus operations */
5220 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
5221
5222 /* Entering applications like rdesktop that mangle the pointer */
5223 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
5224
5225 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
5226 XSetWindowBorder(display, win->id,
5227 win->ws->r->s->c[ev->type == FocusIn ?
5228 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
5229 break;
5230 default:
5231 fprintf(stderr, "ignoring focusevent\n");
5232 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
5233 break;
5234 }
5235 #endif
5236 }
5237
5238 void
5239 mapnotify(XEvent *e)
5240 {
5241 struct ws_win *win;
5242 XMapEvent *ev = &e->xmap;
5243
5244 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
5245
5246 win = manage_window(ev->window);
5247 if (win)
5248 set_win_state(win, NormalState);
5249 }
5250
5251 void
5252 mappingnotify(XEvent *e)
5253 {
5254 XMappingEvent *ev = &e->xmapping;
5255
5256 XRefreshKeyboardMapping(ev);
5257 if (ev->request == MappingKeyboard)
5258 grabkeys();
5259 }
5260
5261 void
5262 maprequest(XEvent *e)
5263 {
5264 struct ws_win *win;
5265 struct swm_region *r;
5266 XWindowAttributes wa;
5267 XMapRequestEvent *ev = &e->xmaprequest;
5268
5269 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
5270 e->xmaprequest.window);
5271
5272 if (!XGetWindowAttributes(display, ev->window, &wa))
5273 return;
5274 if (wa.override_redirect)
5275 return;
5276
5277 win = manage_window(e->xmaprequest.window);
5278 if (win == NULL)
5279 return; /* can't happen */
5280
5281 stack();
5282
5283 /* make new win focused */
5284 r = root_to_region(win->wa.root);
5285 if (win->ws == r->ws)
5286 focus_magic(win);
5287 }
5288
5289 void
5290 propertynotify(XEvent *e)
5291 {
5292 struct ws_win *win;
5293 XPropertyEvent *ev = &e->xproperty;
5294
5295 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
5296 ev->window);
5297
5298 win = find_window(ev->window);
5299 if (win == NULL)
5300 return;
5301
5302 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
5303 update_iconic(win, 0);
5304 XMapRaised(display, win->id);
5305 stack();
5306 focus_win(win);
5307 return;
5308 }
5309
5310 switch (ev->atom) {
5311 case XA_WM_NORMAL_HINTS:
5312 #if 0
5313 long mask;
5314 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5315 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
5316 if (win->sh.flags & PMinSize) {
5317 win->g.w = win->sh.min_width;
5318 win->g.h = win->sh.min_height;
5319 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
5320 }
5321 XMoveResizeWindow(display, win->id,
5322 win->g.x, win->g.y, win->g.w, win->g.h);
5323 #endif
5324 if (window_name_enabled)
5325 bar_update();
5326 break;
5327 default:
5328 break;
5329 }
5330 }
5331
5332 void
5333 unmapnotify(XEvent *e)
5334 {
5335 struct ws_win *win;
5336
5337 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
5338
5339 /* determine if we need to help unmanage this window */
5340 win = find_window(e->xunmap.window);
5341 if (win == NULL)
5342 return;
5343
5344 if (getstate(e->xunmap.window) == NormalState) {
5345 unmanage_window(win);
5346 stack();
5347
5348 /* giant hack for apps that don't destroy transient windows */
5349 /* eat a bunch of events to prevent remanaging the window */
5350 XEvent cne;
5351 while (XCheckWindowEvent(display, e->xunmap.window,
5352 EnterWindowMask, &cne))
5353 ;
5354 while (XCheckWindowEvent(display, e->xunmap.window,
5355 StructureNotifyMask, &cne))
5356 ;
5357 while (XCheckWindowEvent(display, e->xunmap.window,
5358 SubstructureNotifyMask, &cne))
5359 ;
5360 /* resend unmap because we ated it */
5361 XUnmapWindow(display, e->xunmap.window);
5362 }
5363
5364 if (focus_mode == SWM_FOCUS_DEFAULT)
5365 drain_enter_notify();
5366 }
5367
5368 void
5369 visibilitynotify(XEvent *e)
5370 {
5371 int i;
5372 struct swm_region *r;
5373
5374 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
5375 e->xvisibility.window);
5376 if (e->xvisibility.state == VisibilityUnobscured)
5377 for (i = 0; i < ScreenCount(display); i++)
5378 TAILQ_FOREACH(r, &screens[i].rl, entry)
5379 if (e->xvisibility.window == r->bar_window)
5380 bar_update();
5381 }
5382
5383 void
5384 clientmessage(XEvent *e)
5385 {
5386 XClientMessageEvent *ev;
5387 struct ws_win *win;
5388
5389 ev = &e->xclient;
5390
5391 win = find_window(ev->window);
5392 if (win == NULL)
5393 return;
5394
5395 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
5396 ev->window, ev->message_type);
5397
5398 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
5399 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
5400 focus_win(win);
5401 }
5402 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
5403 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
5404 if (win->can_delete)
5405 client_msg(win, adelete);
5406 else
5407 XKillClient(display, win->id);
5408 }
5409 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
5410 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_MOVERESIZE_WINDOW \n");
5411 if (win->floating) {
5412 if (ev->data.l[0] & (1<<8)) /* x */
5413 win->g.x = ev->data.l[1];
5414 if (ev->data.l[0] & (1<<9)) /* y */
5415 win->g.y = ev->data.l[2];
5416 if (ev->data.l[0] & (1<<10)) /* width */
5417 win->g.w = ev->data.l[3];
5418 if (ev->data.l[0] & (1<<11)) /* height */
5419 win->g.h = ev->data.l[4];
5420 }
5421 else {
5422 /* TODO: Change stack sizes */
5423 }
5424 config_win(win, NULL);
5425 }
5426 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
5427 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
5428 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
5429 if (ev->data.l[2])
5430 ewmh_update_win_state(win, ev->data.l[2], ev->data.l[0]);
5431
5432 stack();
5433 }
5434 }
5435
5436 int
5437 xerror_start(Display *d, XErrorEvent *ee)
5438 {
5439 other_wm = 1;
5440 return (-1);
5441 }
5442
5443 int
5444 xerror(Display *d, XErrorEvent *ee)
5445 {
5446 /* fprintf(stderr, "error: %p %p\n", display, ee); */
5447 return (-1);
5448 }
5449
5450 int
5451 active_wm(void)
5452 {
5453 other_wm = 0;
5454 xerrorxlib = XSetErrorHandler(xerror_start);
5455
5456 /* this causes an error if some other window manager is running */
5457 XSelectInput(display, DefaultRootWindow(display),
5458 SubstructureRedirectMask);
5459 XSync(display, False);
5460 if (other_wm)
5461 return (1);
5462
5463 XSetErrorHandler(xerror);
5464 XSync(display, False);
5465 return (0);
5466 }
5467
5468 void
5469 new_region(struct swm_screen *s, int x, int y, int w, int h)
5470 {
5471 struct swm_region *r, *n;
5472 struct workspace *ws = NULL;
5473 int i;
5474
5475 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
5476 s->idx, w, h, x, y);
5477
5478 /* remove any conflicting regions */
5479 n = TAILQ_FIRST(&s->rl);
5480 while (n) {
5481 r = n;
5482 n = TAILQ_NEXT(r, entry);
5483 if (X(r) < (x + w) &&
5484 (X(r) + WIDTH(r)) > x &&
5485 Y(r) < (y + h) &&
5486 (Y(r) + HEIGHT(r)) > y) {
5487 if (r->ws->r != NULL)
5488 r->ws->old_r = r->ws->r;
5489 r->ws->r = NULL;
5490 XDestroyWindow(display, r->bar_window);
5491 TAILQ_REMOVE(&s->rl, r, entry);
5492 TAILQ_INSERT_TAIL(&s->orl, r, entry);
5493 }
5494 }
5495
5496 /* search old regions for one to reuse */
5497
5498 /* size + location match */
5499 TAILQ_FOREACH(r, &s->orl, entry)
5500 if (X(r) == x && Y(r) == y &&
5501 HEIGHT(r) == h && WIDTH(r) == w)
5502 break;
5503
5504 /* size match */
5505 TAILQ_FOREACH(r, &s->orl, entry)
5506 if (HEIGHT(r) == h && WIDTH(r) == w)
5507 break;
5508
5509 if (r != NULL) {
5510 TAILQ_REMOVE(&s->orl, r, entry);
5511 /* try to use old region's workspace */
5512 if (r->ws->r == NULL)
5513 ws = r->ws;
5514 } else
5515 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
5516 errx(1, "calloc: failed to allocate memory for screen");
5517
5518 /* if we don't have a workspace already, find one */
5519 if (ws == NULL) {
5520 for (i = 0; i < SWM_WS_MAX; i++)
5521 if (s->ws[i].r == NULL) {
5522 ws = &s->ws[i];
5523 break;
5524 }
5525 }
5526
5527 if (ws == NULL)
5528 errx(1, "no free workspaces\n");
5529
5530 X(r) = x;
5531 Y(r) = y;
5532 WIDTH(r) = w;
5533 HEIGHT(r) = h;
5534 r->s = s;
5535 r->ws = ws;
5536 r->ws_prior = NULL;
5537 ws->r = r;
5538 outputs++;
5539 TAILQ_INSERT_TAIL(&s->rl, r, entry);
5540 }
5541
5542 void
5543 scan_xrandr(int i)
5544 {
5545 #ifdef SWM_XRR_HAS_CRTC
5546 XRRCrtcInfo *ci;
5547 XRRScreenResources *sr;
5548 int c;
5549 int ncrtc = 0;
5550 #endif /* SWM_XRR_HAS_CRTC */
5551 struct swm_region *r;
5552
5553
5554 if (i >= ScreenCount(display))
5555 errx(1, "invalid screen");
5556
5557 /* remove any old regions */
5558 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
5559 r->ws->old_r = r->ws->r = NULL;
5560 XDestroyWindow(display, r->bar_window);
5561 TAILQ_REMOVE(&screens[i].rl, r, entry);
5562 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
5563 }
5564 outputs = 0;
5565
5566 /* map virtual screens onto physical screens */
5567 #ifdef SWM_XRR_HAS_CRTC
5568 if (xrandr_support) {
5569 sr = XRRGetScreenResources(display, screens[i].root);
5570 if (sr == NULL)
5571 new_region(&screens[i], 0, 0,
5572 DisplayWidth(display, i),
5573 DisplayHeight(display, i));
5574 else
5575 ncrtc = sr->ncrtc;
5576
5577 for (c = 0, ci = NULL; c < ncrtc; c++) {
5578 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
5579 if (ci->noutput == 0)
5580 continue;
5581
5582 if (ci != NULL && ci->mode == None)
5583 new_region(&screens[i], 0, 0,
5584 DisplayWidth(display, i),
5585 DisplayHeight(display, i));
5586 else
5587 new_region(&screens[i],
5588 ci->x, ci->y, ci->width, ci->height);
5589 }
5590 if (ci)
5591 XRRFreeCrtcInfo(ci);
5592 XRRFreeScreenResources(sr);
5593 } else
5594 #endif /* SWM_XRR_HAS_CRTC */
5595 {
5596 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
5597 DisplayHeight(display, i));
5598 }
5599 }
5600
5601 void
5602 screenchange(XEvent *e) {
5603 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
5604 struct swm_region *r;
5605 int i;
5606
5607 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
5608
5609 if (!XRRUpdateConfiguration(e))
5610 return;
5611
5612 /* silly event doesn't include the screen index */
5613 for (i = 0; i < ScreenCount(display); i++)
5614 if (screens[i].root == xe->root)
5615 break;
5616 if (i >= ScreenCount(display))
5617 errx(1, "screenchange: screen not found\n");
5618
5619 /* brute force for now, just re-enumerate the regions */
5620 scan_xrandr(i);
5621
5622 /* add bars to all regions */
5623 for (i = 0; i < ScreenCount(display); i++)
5624 TAILQ_FOREACH(r, &screens[i].rl, entry)
5625 bar_setup(r);
5626 stack();
5627 if (focus_mode == SWM_FOCUS_DEFAULT)
5628 drain_enter_notify();
5629 }
5630
5631 void
5632 grab_windows(void)
5633 {
5634 Window d1, d2, *wins = NULL;
5635 XWindowAttributes wa;
5636 unsigned int no;
5637 int i, j;
5638 long state, manage;
5639
5640 for (i = 0; i < ScreenCount(display); i++) {
5641 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
5642 continue;
5643
5644 /* attach windows to a region */
5645 /* normal windows */
5646 for (j = 0; j < no; j++) {
5647 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5648 wa.override_redirect ||
5649 XGetTransientForHint(display, wins[j], &d1))
5650 continue;
5651
5652 state = getstate(wins[j]);
5653 manage = state == IconicState;
5654 if (wa.map_state == IsViewable || manage)
5655 manage_window(wins[j]);
5656 }
5657 /* transient windows */
5658 for (j = 0; j < no; j++) {
5659 if (!XGetWindowAttributes(display, wins[j], &wa) ||
5660 wa.override_redirect)
5661 continue;
5662
5663 state = getstate(wins[j]);
5664 manage = state == IconicState;
5665 if (XGetTransientForHint(display, wins[j], &d1) &&
5666 manage)
5667 manage_window(wins[j]);
5668 }
5669 if (wins) {
5670 XFree(wins);
5671 wins = NULL;
5672 }
5673 }
5674 }
5675
5676 void
5677 setup_screens(void)
5678 {
5679 int i, j, k;
5680 int errorbase, major, minor;
5681 struct workspace *ws;
5682 int ws_idx_atom;
5683
5684 if ((screens = calloc(ScreenCount(display),
5685 sizeof(struct swm_screen))) == NULL)
5686 errx(1, "calloc: screens");
5687
5688 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5689
5690 /* initial Xrandr setup */
5691 xrandr_support = XRRQueryExtension(display,
5692 &xrandr_eventbase, &errorbase);
5693 if (xrandr_support)
5694 if (XRRQueryVersion(display, &major, &minor) && major < 1)
5695 xrandr_support = 0;
5696
5697 /* map physical screens */
5698 for (i = 0; i < ScreenCount(display); i++) {
5699 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
5700 screens[i].idx = i;
5701 TAILQ_INIT(&screens[i].rl);
5702 TAILQ_INIT(&screens[i].orl);
5703 screens[i].root = RootWindow(display, i);
5704
5705 /* set default colors */
5706 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
5707 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
5708 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
5709 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
5710 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
5711
5712 /* set default cursor */
5713 XDefineCursor(display, screens[i].root,
5714 XCreateFontCursor(display, XC_left_ptr));
5715
5716 /* init all workspaces */
5717 /* XXX these should be dynamically allocated too */
5718 for (j = 0; j < SWM_WS_MAX; j++) {
5719 ws = &screens[i].ws[j];
5720 ws->idx = j;
5721 ws->focus = NULL;
5722 ws->r = NULL;
5723 ws->old_r = NULL;
5724 TAILQ_INIT(&ws->winlist);
5725 TAILQ_INIT(&ws->unmanagedlist);
5726
5727 for (k = 0; layouts[k].l_stack != NULL; k++)
5728 if (layouts[k].l_config != NULL)
5729 layouts[k].l_config(ws,
5730 SWM_ARG_ID_STACKINIT);
5731 ws->cur_layout = &layouts[0];
5732 }
5733
5734 scan_xrandr(i);
5735
5736 if (xrandr_support)
5737 XRRSelectInput(display, screens[i].root,
5738 RRScreenChangeNotifyMask);
5739 }
5740 }
5741
5742 void
5743 setup_globals(void)
5744 {
5745 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
5746 == NULL)
5747 err(1, "setup_globals: strdup");
5748 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
5749 == NULL)
5750 err(1, "setup_globals: strdup");
5751 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
5752 == NULL)
5753 err(1, "setup_globals: strdup");
5754 if ((spawn_term[0] = strdup("xterm")) == NULL)
5755 err(1, "setup_globals: strdup");
5756 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
5757 errx(1, "strdup");
5758 }
5759
5760 void
5761 workaround(void)
5762 {
5763 int i;
5764 Atom netwmcheck, netwmname, utf8_string;
5765 Window root, win;
5766
5767 /* work around sun jdk bugs, code from wmname */
5768 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
5769 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
5770 utf8_string = XInternAtom(display, "UTF8_STRING", False);
5771 for (i = 0; i < ScreenCount(display); i++) {
5772 root = screens[i].root;
5773 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
5774 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
5775 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
5776
5777 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
5778 PropModeReplace, (unsigned char *)&win,1);
5779 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
5780 PropModeReplace, (unsigned char *)&win,1);
5781 XChangeProperty(display, win, netwmname, utf8_string, 8,
5782 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
5783 }
5784 }
5785
5786 int
5787 main(int argc, char *argv[])
5788 {
5789 struct passwd *pwd;
5790 struct swm_region *r, *rr;
5791 struct ws_win *winfocus = NULL;
5792 struct timeval tv;
5793 union arg a;
5794 char conf[PATH_MAX], *cfile = NULL;
5795 struct stat sb;
5796 XEvent e;
5797 int xfd, i;
5798 fd_set rd;
5799 struct sigaction sact;
5800
5801 start_argv = argv;
5802 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
5803 SWM_VERSION, cvstag);
5804 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
5805 warnx("no locale support");
5806
5807 if (!(display = XOpenDisplay(0)))
5808 errx(1, "can not open display");
5809
5810 if (active_wm())
5811 errx(1, "other wm running");
5812
5813 /* handle some signals */
5814 bzero(&sact, sizeof(sact));
5815 sigemptyset(&sact.sa_mask);
5816 sact.sa_flags = 0;
5817 sact.sa_handler = sighdlr;
5818 sigaction(SIGINT, &sact, NULL);
5819 sigaction(SIGQUIT, &sact, NULL);
5820 sigaction(SIGTERM, &sact, NULL);
5821 sigaction(SIGHUP, &sact, NULL);
5822
5823 sact.sa_handler = sighdlr;
5824 sact.sa_flags = SA_NOCLDSTOP;
5825 sigaction(SIGCHLD, &sact, NULL);
5826
5827 astate = XInternAtom(display, "WM_STATE", False);
5828 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
5829 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
5830 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
5831 a_wmname = XInternAtom(display, "WM_NAME", False);
5832 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
5833 a_string = XInternAtom(display, "STRING", False);
5834 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
5835
5836 /* look for local and global conf file */
5837 pwd = getpwuid(getuid());
5838 if (pwd == NULL)
5839 errx(1, "invalid user %d", getuid());
5840
5841 setup_screens();
5842 setup_globals();
5843 setup_keys();
5844 setup_quirks();
5845 setup_spawn();
5846
5847 /* load config */
5848 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
5849 if (stat(conf, &sb) != -1) {
5850 if (S_ISREG(sb.st_mode))
5851 cfile = conf;
5852 } else {
5853 /* try global conf file */
5854 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
5855 if (!stat(conf, &sb))
5856 if (S_ISREG(sb.st_mode))
5857 cfile = conf;
5858 }
5859 if (cfile)
5860 conf_load(cfile);
5861
5862 setup_ewmh();
5863 /* set some values to work around bad programs */
5864 workaround();
5865
5866 /* grab existing windows (before we build the bars) */
5867 grab_windows();
5868
5869 /* setup all bars */
5870 for (i = 0; i < ScreenCount(display); i++)
5871 TAILQ_FOREACH(r, &screens[i].rl, entry) {
5872 if (winfocus == NULL)
5873 winfocus = TAILQ_FIRST(&r->ws->winlist);
5874 bar_setup(r);
5875 }
5876
5877 unfocus_all();
5878
5879 grabkeys();
5880 stack();
5881 if (focus_mode == SWM_FOCUS_DEFAULT)
5882 drain_enter_notify();
5883
5884 xfd = ConnectionNumber(display);
5885 while (running) {
5886 while (XPending(display)) {
5887 XNextEvent(display, &e);
5888 if (running == 0)
5889 goto done;
5890 if (e.type < LASTEvent) {
5891 dumpevent(&e);
5892 if (handler[e.type])
5893 handler[e.type](&e);
5894 else
5895 DNPRINTF(SWM_D_EVENT,
5896 "win: %lu unknown event: %d\n",
5897 e.xany.window, e.type);
5898 } else {
5899 switch (e.type - xrandr_eventbase) {
5900 case RRScreenChangeNotify:
5901 screenchange(&e);
5902 break;
5903 default:
5904 DNPRINTF(SWM_D_EVENT,
5905 "win: %lu unknown xrandr event: "
5906 "%d\n", e.xany.window, e.type);
5907 break;
5908 }
5909 }
5910 }
5911
5912 /* if we are being restarted go focus on first window */
5913 if (winfocus) {
5914 rr = winfocus->ws->r;
5915 if (rr == NULL) {
5916 /* not a visible window */
5917 winfocus = NULL;
5918 continue;
5919 }
5920 /* move pointer to first screen if multi screen */
5921 if (ScreenCount(display) > 1 || outputs > 1)
5922 XWarpPointer(display, None, rr->s[0].root,
5923 0, 0, 0, 0, rr->g.x,
5924 rr->g.y + (bar_enabled ? bar_height : 0));
5925
5926 a.id = SWM_ARG_ID_FOCUSCUR;
5927 focus(rr, &a);
5928 winfocus = NULL;
5929 continue;
5930 }
5931
5932 FD_ZERO(&rd);
5933 FD_SET(xfd, &rd);
5934 tv.tv_sec = 1;
5935 tv.tv_usec = 0;
5936 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
5937 if (errno != EINTR)
5938 DNPRINTF(SWM_D_MISC, "select failed");
5939 if (restart_wm == 1)
5940 restart(NULL, NULL);
5941 if (search_resp == 1)
5942 search_do_resp();
5943 if (running == 0)
5944 goto done;
5945 if (bar_alarm) {
5946 bar_alarm = 0;
5947 bar_update();
5948 }
5949 }
5950 done:
5951 teardown_ewmh();
5952 bar_extra_stop();
5953 XFreeGC(display, bar_gc);
5954 XCloseDisplay(display);
5955
5956 return (0);
5957 }