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