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