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