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