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