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