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