]> code.delx.au - spectrwm/blob - scrotwm.c
Skip iconified windows correctly.
[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 /* update the bar so that title/class/name will be cleared. */
2433 if (window_name_enabled || title_name_enabled || title_class_enabled)
2434 bar_update();
2435
2436 return;
2437 }
2438
2439 focus_magic(winfocus);
2440 }
2441
2442 void
2443 focus(struct swm_region *r, union arg *args)
2444 {
2445 struct ws_win *winfocus = NULL, *winlostfocus = NULL, *head;
2446 struct ws_win *cur_focus = NULL;
2447 struct ws_win_list *wl = NULL;
2448 struct workspace *ws = NULL;
2449
2450 if (!(r && r->ws))
2451 return;
2452
2453 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
2454
2455 /* treat FOCUS_CUR special */
2456 if (args->id == SWM_ARG_ID_FOCUSCUR) {
2457 if (r->ws->focus && r->ws->focus->iconic == 0)
2458 winfocus = r->ws->focus;
2459 else if (r->ws->focus_prev && r->ws->focus_prev->iconic == 0)
2460 winfocus = r->ws->focus_prev;
2461 else
2462 TAILQ_FOREACH(winfocus, &r->ws->winlist, entry)
2463 if (winfocus->iconic == 0)
2464 break;
2465
2466 focus_magic(winfocus);
2467 return;
2468 }
2469
2470 if ((cur_focus = r->ws->focus) == NULL)
2471 return;
2472 ws = r->ws;
2473 wl = &ws->winlist;
2474
2475 winlostfocus = cur_focus;
2476
2477 switch (args->id) {
2478 case SWM_ARG_ID_FOCUSPREV:
2479 head = TAILQ_PREV(cur_focus, ws_win_list, entry);
2480 if (head == NULL)
2481 head = TAILQ_LAST(wl, ws_win_list);
2482 winfocus = head;
2483 if (WINID(winfocus) == cur_focus->transient) {
2484 head = TAILQ_PREV(winfocus, ws_win_list, entry);
2485 if (head == NULL)
2486 head = TAILQ_LAST(wl, ws_win_list);
2487 winfocus = head;
2488 }
2489
2490 /* skip iconics */
2491 if (winfocus && winfocus->iconic) {
2492 while (winfocus != cur_focus) {
2493 if (winfocus == NULL)
2494 winfocus = TAILQ_LAST(wl, ws_win_list);
2495 if (winfocus->iconic == 0)
2496 break;
2497 winfocus = TAILQ_PREV(winfocus, ws_win_list, entry);
2498 }
2499 }
2500 break;
2501
2502 case SWM_ARG_ID_FOCUSNEXT:
2503 head = TAILQ_NEXT(cur_focus, entry);
2504 if (head == NULL)
2505 head = TAILQ_FIRST(wl);
2506 winfocus = head;
2507
2508 /* skip iconics */
2509 if (winfocus && winfocus->iconic) {
2510 while (winfocus != cur_focus) {
2511 if (winfocus == NULL)
2512 winfocus = TAILQ_FIRST(wl);
2513 if (winfocus->iconic == 0)
2514 break;
2515 winfocus = TAILQ_NEXT(winfocus, entry);
2516 }
2517 }
2518 break;
2519
2520 case SWM_ARG_ID_FOCUSMAIN:
2521 winfocus = TAILQ_FIRST(wl);
2522 if (winfocus == cur_focus)
2523 winfocus = cur_focus->ws->focus_prev;
2524 break;
2525
2526 default:
2527 return;
2528 }
2529 if (winfocus == winlostfocus || winfocus == NULL) {
2530 /* update the bar so that title/class/name will be cleared. */
2531 if (window_name_enabled || title_name_enabled || title_class_enabled)
2532 bar_update();
2533
2534 return;
2535 }
2536
2537 focus_magic(winfocus);
2538 }
2539
2540 void
2541 cycle_layout(struct swm_region *r, union arg *args)
2542 {
2543 struct workspace *ws = r->ws;
2544 union arg a;
2545
2546 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
2547
2548 ws->cur_layout++;
2549 if (ws->cur_layout->l_stack == NULL)
2550 ws->cur_layout = &layouts[0];
2551
2552 stack();
2553 if (focus_mode == SWM_FOCUS_DEFAULT)
2554 drain_enter_notify();
2555 a.id = SWM_ARG_ID_FOCUSCUR;
2556 focus(r, &a);
2557 bar_update();
2558 }
2559
2560 void
2561 stack_config(struct swm_region *r, union arg *args)
2562 {
2563 struct workspace *ws = r->ws;
2564
2565 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
2566 args->id, ws->idx);
2567
2568 if (ws->cur_layout->l_config != NULL)
2569 ws->cur_layout->l_config(ws, args->id);
2570
2571 if (args->id != SWM_ARG_ID_STACKINIT)
2572 stack();
2573 bar_update();
2574 }
2575
2576 void
2577 stack(void) {
2578 struct swm_geometry g;
2579 struct swm_region *r;
2580 int i, j;
2581
2582 DNPRINTF(SWM_D_STACK, "stack\n");
2583
2584 for (i = 0; i < ScreenCount(display); i++) {
2585 j = 0;
2586 TAILQ_FOREACH(r, &screens[i].rl, entry) {
2587 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
2588 "(screen %d, region %d)\n", r->ws->idx, i, j++);
2589
2590 /* start with screen geometry, adjust for bar */
2591 g = r->g;
2592 g.w -= 2 * border_width;
2593 g.h -= 2 * border_width;
2594 if (bar_enabled) {
2595 if (!bar_at_bottom)
2596 g.y += bar_height;
2597 g.h -= bar_height;
2598 }
2599 r->ws->cur_layout->l_stack(r->ws, &g);
2600 r->ws->cur_layout->l_string(r->ws);
2601 /* save r so we can track region changes */
2602 r->ws->old_r = r;
2603 }
2604 }
2605 if (font_adjusted)
2606 font_adjusted--;
2607
2608 if (focus_mode == SWM_FOCUS_DEFAULT)
2609 drain_enter_notify();
2610 }
2611
2612 void
2613 store_float_geom(struct ws_win *win, struct swm_region *r)
2614 {
2615 /* retain window geom and region geom */
2616 win->g_float.x = win->g.x;
2617 win->g_float.y = win->g.y;
2618 win->g_float.w = win->g.w;
2619 win->g_float.h = win->g.h;
2620 win->rg_float.x = r->g.x;
2621 win->rg_float.y = r->g.y;
2622 win->rg_float.w = r->g.w;
2623 win->rg_float.h = r->g.h;
2624 win->g_floatvalid = 1;
2625 }
2626
2627 void
2628 stack_floater(struct ws_win *win, struct swm_region *r)
2629 {
2630 unsigned int mask;
2631 XWindowChanges wc;
2632
2633 if (win == NULL)
2634 return;
2635
2636 bzero(&wc, sizeof wc);
2637 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
2638
2639 /*
2640 * to allow windows to change their size (e.g. mplayer fs) only retrieve
2641 * geom on ws switches or return from max mode
2642 */
2643
2644 if (win->floatmaxed || (r != r->ws->old_r && win->g_floatvalid
2645 && !(win->ewmh_flags & EWMH_F_FULLSCREEN))) {
2646 /*
2647 * use stored g and rg to set relative position and size
2648 * as in old region or before max stack mode
2649 */
2650 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
2651 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
2652 win->g.w = win->g_float.w;
2653 win->g.h = win->g_float.h;
2654 win->g_floatvalid = 0;
2655 }
2656
2657 win->floatmaxed = 0;
2658
2659 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
2660 (win->g.h >= HEIGHT(r)))
2661 wc.border_width = 0;
2662 else
2663 wc.border_width = border_width;
2664 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
2665 win->g.w = (double)WIDTH(r) * dialog_ratio;
2666 win->g.h = (double)HEIGHT(r) * dialog_ratio;
2667 }
2668
2669 if (!win->manual) {
2670 /*
2671 * floaters and transients are auto-centred unless moved
2672 * or resized
2673 */
2674 win->g.x = r->g.x + (WIDTH(r) - win->g.w) /
2675 2 - wc.border_width;
2676 win->g.y = r->g.y + (HEIGHT(r) - win->g.h) /
2677 2 - wc.border_width;
2678 }
2679
2680 /* win can be outside r if new r smaller than old r */
2681 /* Ensure top left corner inside r (move probs otherwise) */
2682 if (win->g.x < r->g.x - wc.border_width)
2683 win->g.x = r->g.x - wc.border_width;
2684 if (win->g.x > r->g.x + r->g.w - 1)
2685 win->g.x = (win->g.w > r->g.w) ? r->g.x :
2686 (r->g.x + r->g.w - win->g.w - 2 * wc.border_width);
2687 if (win->g.y < r->g.y - wc.border_width)
2688 win->g.y = r->g.y - wc.border_width;
2689 if (win->g.y > r->g.y + r->g.h - 1)
2690 win->g.y = (win->g.h > r->g.h) ? r->g.y :
2691 (r->g.y + r->g.h - win->g.h - 2 * wc.border_width);
2692
2693 wc.x = win->g.x;
2694 wc.y = win->g.y;
2695 wc.width = win->g.w;
2696 wc.height = win->g.h;
2697
2698 /*
2699 * Retain floater and transient geometry for correct positioning
2700 * when ws changes region
2701 */
2702 if (!(win->ewmh_flags & EWMH_F_FULLSCREEN))
2703 store_float_geom(win, r);
2704
2705 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
2706 win->id, wc.x, wc.y, wc.width, wc.height);
2707
2708 XConfigureWindow(display, win->id, mask, &wc);
2709 }
2710
2711 /*
2712 * Send keystrokes to terminal to decrease/increase the font size as the
2713 * window size changes.
2714 */
2715 void
2716 adjust_font(struct ws_win *win)
2717 {
2718 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
2719 win->floating || win->transient)
2720 return;
2721
2722 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
2723 win->g.w / win->sh.width_inc < term_width &&
2724 win->font_steps < SWM_MAX_FONT_STEPS) {
2725 win->font_size_boundary[win->font_steps] =
2726 (win->sh.width_inc * term_width) + win->sh.base_width;
2727 win->font_steps++;
2728 font_adjusted++;
2729 win->last_inc = win->sh.width_inc;
2730 fake_keypress(win, XK_KP_Subtract, ShiftMask);
2731 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
2732 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
2733 win->font_steps--;
2734 font_adjusted++;
2735 win->last_inc = win->sh.width_inc;
2736 fake_keypress(win, XK_KP_Add, ShiftMask);
2737 }
2738 }
2739
2740 #define SWAPXY(g) do { \
2741 int tmp; \
2742 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
2743 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
2744 } while (0)
2745 void
2746 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
2747 {
2748 XWindowChanges wc;
2749 XWindowAttributes wa;
2750 struct swm_geometry win_g, r_g = *g;
2751 struct ws_win *win, *fs_win = 0;
2752 int i, j, s, stacks;
2753 int w_inc = 1, h_inc, w_base = 1, h_base;
2754 int hrh, extra = 0, h_slice, last_h = 0;
2755 int split, colno, winno, mwin, msize, mscale;
2756 int remain, missing, v_slice, reconfigure;
2757 unsigned int mask;
2758
2759 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
2760 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
2761
2762 winno = count_win(ws, 0);
2763 if (winno == 0 && count_win(ws, 1) == 0)
2764 return;
2765
2766 TAILQ_FOREACH(win, &ws->winlist, entry)
2767 if (win->transient == 0 && win->floating == 0
2768 && win->iconic == 0)
2769 break;
2770
2771 if (win == NULL)
2772 goto notiles;
2773
2774 if (rot) {
2775 w_inc = win->sh.width_inc;
2776 w_base = win->sh.base_width;
2777 mwin = ws->l_state.horizontal_mwin;
2778 mscale = ws->l_state.horizontal_msize;
2779 stacks = ws->l_state.horizontal_stacks;
2780 SWAPXY(&r_g);
2781 } else {
2782 w_inc = win->sh.height_inc;
2783 w_base = win->sh.base_height;
2784 mwin = ws->l_state.vertical_mwin;
2785 mscale = ws->l_state.vertical_msize;
2786 stacks = ws->l_state.vertical_stacks;
2787 }
2788 win_g = r_g;
2789
2790 if (stacks > winno - mwin)
2791 stacks = winno - mwin;
2792 if (stacks < 1)
2793 stacks = 1;
2794
2795 h_slice = r_g.h / SWM_H_SLICE;
2796 if (mwin && winno > mwin) {
2797 v_slice = r_g.w / SWM_V_SLICE;
2798
2799 split = mwin;
2800 colno = split;
2801 win_g.w = v_slice * mscale;
2802
2803 if (w_inc > 1 && w_inc < v_slice) {
2804 /* adjust for window's requested size increment */
2805 remain = (win_g.w - w_base) % w_inc;
2806 missing = w_inc - remain;
2807 win_g.w -= remain;
2808 extra += remain;
2809 }
2810
2811 msize = win_g.w;
2812 if (flip)
2813 win_g.x += r_g.w - msize;
2814 } else {
2815 msize = -2;
2816 colno = split = winno / stacks;
2817 win_g.w = ((r_g.w - (stacks * 2 * border_width) +
2818 2 * border_width) / stacks);
2819 }
2820 hrh = r_g.h / colno;
2821 extra = r_g.h - (colno * hrh);
2822 win_g.h = hrh - 2 * border_width;
2823
2824 /* stack all the tiled windows */
2825 i = j = 0, s = stacks;
2826 TAILQ_FOREACH(win, &ws->winlist, entry) {
2827 if (win->transient != 0 || win->floating != 0)
2828 continue;
2829 if (win->iconic != 0)
2830 continue;
2831
2832 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2833 fs_win = win;
2834 continue;
2835 }
2836
2837 if (split && i == split) {
2838 colno = (winno - mwin) / stacks;
2839 if (s <= (winno - mwin) % stacks)
2840 colno++;
2841 split = split + colno;
2842 hrh = (r_g.h / colno);
2843 extra = r_g.h - (colno * hrh);
2844 if (flip)
2845 win_g.x = r_g.x;
2846 else
2847 win_g.x += win_g.w + 2 * border_width;
2848 win_g.w = (r_g.w - msize -
2849 (stacks * 2 * border_width)) / stacks;
2850 if (s == 1)
2851 win_g.w += (r_g.w - msize -
2852 (stacks * 2 * border_width)) % stacks;
2853 s--;
2854 j = 0;
2855 }
2856 win_g.h = hrh - 2 * border_width;
2857 if (rot) {
2858 h_inc = win->sh.width_inc;
2859 h_base = win->sh.base_width;
2860 } else {
2861 h_inc = win->sh.height_inc;
2862 h_base = win->sh.base_height;
2863 }
2864 if (j == colno - 1) {
2865 win_g.h = hrh + extra;
2866 } else if (h_inc > 1 && h_inc < h_slice) {
2867 /* adjust for window's requested size increment */
2868 remain = (win_g.h - h_base) % h_inc;
2869 missing = h_inc - remain;
2870
2871 if (missing <= extra || j == 0) {
2872 extra -= missing;
2873 win_g.h += missing;
2874 } else {
2875 win_g.h -= remain;
2876 extra += remain;
2877 }
2878 }
2879
2880 if (j == 0)
2881 win_g.y = r_g.y;
2882 else
2883 win_g.y += last_h + 2 * border_width;
2884
2885 bzero(&wc, sizeof wc);
2886 if (disable_border && bar_enabled == 0 && winno == 1){
2887 wc.border_width = 0;
2888 win_g.w += 2 * border_width;
2889 win_g.h += 2 * border_width;
2890 } else
2891 wc.border_width = border_width;
2892 reconfigure = 0;
2893 if (rot) {
2894 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2895 win->g.w != win_g.h || win->g.h != win_g.w) {
2896 reconfigure = 1;
2897 win->g.x = wc.x = win_g.y;
2898 win->g.y = wc.y = win_g.x;
2899 win->g.w = wc.width = win_g.h;
2900 win->g.h = wc.height = win_g.w;
2901 }
2902 } else {
2903 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2904 win->g.w != win_g.w || win->g.h != win_g.h) {
2905 reconfigure = 1;
2906 win->g.x = wc.x = win_g.x;
2907 win->g.y = wc.y = win_g.y;
2908 win->g.w = wc.width = win_g.w;
2909 win->g.h = wc.height = win_g.h;
2910 }
2911 }
2912 if (reconfigure) {
2913 adjust_font(win);
2914 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2915 XConfigureWindow(display, win->id, mask, &wc);
2916 }
2917
2918 if (XGetWindowAttributes(display, win->id, &wa))
2919 if (wa.map_state == IsUnmapped)
2920 XMapRaised(display, win->id);
2921
2922 last_h = win_g.h;
2923 i++;
2924 j++;
2925 }
2926
2927 notiles:
2928 /* now, stack all the floaters and transients */
2929 TAILQ_FOREACH(win, &ws->winlist, entry) {
2930 if (win->transient == 0 && win->floating == 0)
2931 continue;
2932 if (win->iconic == 1)
2933 continue;
2934 if (win->ewmh_flags & EWMH_F_FULLSCREEN) {
2935 fs_win = win;
2936 continue;
2937 }
2938
2939 stack_floater(win, ws->r);
2940 XMapRaised(display, win->id);
2941 }
2942
2943 if (fs_win) {
2944 stack_floater(fs_win, ws->r);
2945 XMapRaised(display, fs_win->id);
2946 }
2947 }
2948
2949 void
2950 vertical_config(struct workspace *ws, int id)
2951 {
2952 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2953
2954 switch (id) {
2955 case SWM_ARG_ID_STACKRESET:
2956 case SWM_ARG_ID_STACKINIT:
2957 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2958 ws->l_state.vertical_mwin = 1;
2959 ws->l_state.vertical_stacks = 1;
2960 break;
2961 case SWM_ARG_ID_MASTERSHRINK:
2962 if (ws->l_state.vertical_msize > 1)
2963 ws->l_state.vertical_msize--;
2964 break;
2965 case SWM_ARG_ID_MASTERGROW:
2966 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2967 ws->l_state.vertical_msize++;
2968 break;
2969 case SWM_ARG_ID_MASTERADD:
2970 ws->l_state.vertical_mwin++;
2971 break;
2972 case SWM_ARG_ID_MASTERDEL:
2973 if (ws->l_state.vertical_mwin > 0)
2974 ws->l_state.vertical_mwin--;
2975 break;
2976 case SWM_ARG_ID_STACKINC:
2977 ws->l_state.vertical_stacks++;
2978 break;
2979 case SWM_ARG_ID_STACKDEC:
2980 if (ws->l_state.vertical_stacks > 1)
2981 ws->l_state.vertical_stacks--;
2982 break;
2983 default:
2984 return;
2985 }
2986 }
2987
2988 void
2989 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2990 {
2991 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2992
2993 stack_master(ws, g, 0, 0);
2994 }
2995
2996 void
2997 horizontal_config(struct workspace *ws, int id)
2998 {
2999 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
3000
3001 switch (id) {
3002 case SWM_ARG_ID_STACKRESET:
3003 case SWM_ARG_ID_STACKINIT:
3004 ws->l_state.horizontal_mwin = 1;
3005 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
3006 ws->l_state.horizontal_stacks = 1;
3007 break;
3008 case SWM_ARG_ID_MASTERSHRINK:
3009 if (ws->l_state.horizontal_msize > 1)
3010 ws->l_state.horizontal_msize--;
3011 break;
3012 case SWM_ARG_ID_MASTERGROW:
3013 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
3014 ws->l_state.horizontal_msize++;
3015 break;
3016 case SWM_ARG_ID_MASTERADD:
3017 ws->l_state.horizontal_mwin++;
3018 break;
3019 case SWM_ARG_ID_MASTERDEL:
3020 if (ws->l_state.horizontal_mwin > 0)
3021 ws->l_state.horizontal_mwin--;
3022 break;
3023 case SWM_ARG_ID_STACKINC:
3024 ws->l_state.horizontal_stacks++;
3025 break;
3026 case SWM_ARG_ID_STACKDEC:
3027 if (ws->l_state.horizontal_stacks > 1)
3028 ws->l_state.horizontal_stacks--;
3029 break;
3030 default:
3031 return;
3032 }
3033 }
3034
3035 void
3036 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
3037 {
3038 DNPRINTF(SWM_D_STACK, "horizontal_stack: workspace: %d\n", ws->idx);
3039
3040 stack_master(ws, g, 1, 0);
3041 }
3042
3043 /* fullscreen view */
3044 void
3045 max_stack(struct workspace *ws, struct swm_geometry *g)
3046 {
3047 XWindowChanges wc;
3048 struct swm_geometry gg = *g;
3049 struct ws_win *win, *wintrans = NULL, *parent = NULL;
3050 unsigned int mask;
3051 int winno;
3052
3053 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
3054
3055 if (ws == NULL)
3056 return;
3057
3058 winno = count_win(ws, 0);
3059 if (winno == 0 && count_win(ws, 1) == 0)
3060 return;
3061
3062 TAILQ_FOREACH(win, &ws->winlist, entry) {
3063 if (win->transient) {
3064 wintrans = win;
3065 parent = find_window(win->transient);
3066 continue;
3067 }
3068
3069 if (win->floating && win->floatmaxed == 0 ) {
3070 /*
3071 * retain geometry for retrieval on exit from
3072 * max_stack mode
3073 */
3074 store_float_geom(win, ws->r);
3075 win->floatmaxed = 1;
3076 }
3077
3078 /* only reconfigure if necessary */
3079 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
3080 win->g.h != gg.h) {
3081 bzero(&wc, sizeof wc);
3082 win->g.x = wc.x = gg.x;
3083 win->g.y = wc.y = gg.y;
3084 if (bar_enabled){
3085 wc.border_width = border_width;
3086 win->g.w = wc.width = gg.w;
3087 win->g.h = wc.height = gg.h;
3088 } else {
3089 wc.border_width = 0;
3090 win->g.w = wc.width = gg.w + 2 * border_width;
3091 win->g.h = wc.height = gg.h + 2 * border_width;
3092 }
3093 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
3094 XConfigureWindow(display, win->id, mask, &wc);
3095 }
3096 /* unmap only if we don't have multi screen */
3097 if (win != ws->focus)
3098 if (!(ScreenCount(display) > 1 || outputs > 1))
3099 unmap_window(win);
3100 }
3101
3102 /* put the last transient on top */
3103 if (wintrans) {
3104 if (parent)
3105 XMapRaised(display, parent->id);
3106 stack_floater(wintrans, ws->r);
3107 focus_magic(wintrans);
3108 }
3109 }
3110
3111 void
3112 send_to_ws(struct swm_region *r, union arg *args)
3113 {
3114 int wsid = args->id;
3115 struct ws_win *win = NULL, *parent;
3116 struct workspace *ws, *nws;
3117 Atom ws_idx_atom = 0;
3118 unsigned char ws_idx_str[SWM_PROPLEN];
3119 union arg a;
3120
3121 if (r && r->ws)
3122 win = r->ws->focus;
3123 else
3124 return;
3125 if (win == NULL)
3126 return;
3127 if (win->ws->idx == wsid)
3128 return;
3129
3130 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
3131
3132 ws = win->ws;
3133 nws = &win->s->ws[wsid];
3134
3135 a.id = SWM_ARG_ID_FOCUSPREV;
3136 focus(r, &a);
3137 if (win->transient) {
3138 parent = find_window(win->transient);
3139 if (parent) {
3140 unmap_window(parent);
3141 TAILQ_REMOVE(&ws->winlist, parent, entry);
3142 TAILQ_INSERT_TAIL(&nws->winlist, parent, entry);
3143 parent->ws = nws;
3144 }
3145 }
3146 unmap_window(win);
3147 TAILQ_REMOVE(&ws->winlist, win, entry);
3148 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
3149 win->ws = nws;
3150
3151 /* Try to update the window's workspace property */
3152 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3153 if (ws_idx_atom &&
3154 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", nws->idx) <
3155 SWM_PROPLEN) {
3156 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3157 ws_idx_str);
3158 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3159 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3160 }
3161
3162 stack();
3163 }
3164
3165 void
3166 pressbutton(struct swm_region *r, union arg *args)
3167 {
3168 XTestFakeButtonEvent(display, args->id, True, CurrentTime);
3169 XTestFakeButtonEvent(display, args->id, False, CurrentTime);
3170 }
3171
3172 void
3173 raise_toggle(struct swm_region *r, union arg *args)
3174 {
3175 if (r && r->ws == NULL)
3176 return;
3177
3178 r->ws->always_raise = !r->ws->always_raise;
3179
3180 /* bring floaters back to top */
3181 if (r->ws->always_raise == 0)
3182 stack();
3183 }
3184
3185 void
3186 iconify(struct swm_region *r, union arg *args)
3187 {
3188 union arg a;
3189
3190 if (r->ws->focus == NULL)
3191 return;
3192 unmap_window(r->ws->focus);
3193 update_iconic(r->ws->focus, 1);
3194 stack();
3195 if (focus_mode == SWM_FOCUS_DEFAULT)
3196 drain_enter_notify();
3197 r->ws->focus = NULL;
3198 a.id = SWM_ARG_ID_FOCUSCUR;
3199 focus(r, &a);
3200 }
3201
3202 unsigned char *
3203 get_win_name(Display *dpy, Window win, Atom wname, Atom stype,
3204 unsigned long *slen)
3205 {
3206 int status, retfmt;
3207 unsigned long nitems, nbytes, nextra;
3208 unsigned char *prop = NULL;
3209 Atom rettype;
3210
3211 status = XGetWindowProperty(dpy, win, wname, 0L, 0L, False, stype,
3212 &rettype, &retfmt, &nitems, &nbytes, &prop);
3213 if (status != Success)
3214 return (NULL);
3215 XFree(prop);
3216
3217 status = XGetWindowProperty(dpy, win, wname, 0L, nbytes, False,
3218 stype, &rettype, &retfmt, &nitems, &nextra, &prop);
3219 if (status != Success) {
3220 XFree(prop);
3221 return (NULL);
3222 }
3223 if (rettype != stype) {
3224 XFree(prop);
3225 return (NULL);
3226 }
3227 *slen = nitems;
3228 return (prop);
3229 }
3230
3231 void
3232 uniconify(struct swm_region *r, union arg *args)
3233 {
3234 struct ws_win *win;
3235 FILE *lfile;
3236 unsigned char *name;
3237 int count = 0;
3238 unsigned long len;
3239
3240 DNPRINTF(SWM_D_MISC, "uniconify\n");
3241
3242 if (r && r->ws == NULL)
3243 return;
3244
3245 /* make sure we have anything to uniconify */
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 count++;
3252 }
3253 if (count == 0)
3254 return;
3255
3256 search_r = r;
3257 search_resp_action = SWM_SEARCH_UNICONIFY;
3258
3259 spawn_select(r, args, "search", &searchpid);
3260
3261 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3262 return;
3263
3264 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3265 if (win->ws == NULL)
3266 continue; /* should never happen */
3267 if (win->iconic == 0)
3268 continue;
3269
3270 name = get_win_name(display, win->id, a_wmname, a_string,
3271 &len);
3272 if (name == NULL)
3273 continue;
3274 fprintf(lfile, "%s.%lu\n", name, win->id);
3275 XFree(name);
3276 }
3277
3278 fclose(lfile);
3279 }
3280
3281 void
3282 name_workspace(struct swm_region *r, union arg *args)
3283 {
3284 FILE *lfile;
3285
3286 DNPRINTF(SWM_D_MISC, "name_workspace\n");
3287
3288 if (r == NULL)
3289 return;
3290
3291 search_r = r;
3292 search_resp_action = SWM_SEARCH_NAME_WORKSPACE;
3293
3294 spawn_select(r, args, "name_workspace", &searchpid);
3295
3296 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3297 return;
3298
3299 fprintf(lfile, "%s", "");
3300 fclose(lfile);
3301 }
3302
3303 void
3304 search_workspace(struct swm_region *r, union arg *args)
3305 {
3306 int i;
3307 struct workspace *ws;
3308 FILE *lfile;
3309
3310 DNPRINTF(SWM_D_MISC, "search_workspace\n");
3311
3312 if (r == NULL)
3313 return;
3314
3315 search_r = r;
3316 search_resp_action = SWM_SEARCH_SEARCH_WORKSPACE;
3317
3318 spawn_select(r, args, "search", &searchpid);
3319
3320 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3321 return;
3322
3323 for (i = 0; i < SWM_WS_MAX; i++) {
3324 ws = &r->s->ws[i];
3325 if (ws == NULL)
3326 continue;
3327 fprintf(lfile, "%d%s%s\n", ws->idx + 1,
3328 (ws->name ? ":" : ""), (ws->name ? ws->name : ""));
3329 }
3330
3331 fclose(lfile);
3332 }
3333
3334 void
3335 search_win_cleanup(void)
3336 {
3337 struct search_window *sw = NULL;
3338
3339 while ((sw = TAILQ_FIRST(&search_wl)) != NULL) {
3340 XDestroyWindow(display, sw->indicator);
3341 TAILQ_REMOVE(&search_wl, sw, entry);
3342 free(sw);
3343 }
3344 }
3345
3346 void
3347 search_win(struct swm_region *r, union arg *args)
3348 {
3349 struct ws_win *win = NULL;
3350 struct search_window *sw = NULL;
3351 Window w;
3352 GC gc;
3353 XGCValues gcv;
3354 int i;
3355 char s[8];
3356 FILE *lfile;
3357 size_t len;
3358 int textwidth;
3359
3360 DNPRINTF(SWM_D_MISC, "search_win\n");
3361
3362 search_r = r;
3363 search_resp_action = SWM_SEARCH_SEARCH_WINDOW;
3364
3365 spawn_select(r, args, "search", &searchpid);
3366
3367 if ((lfile = fdopen(select_list_pipe[1], "w")) == NULL)
3368 return;
3369
3370 TAILQ_INIT(&search_wl);
3371
3372 i = 1;
3373 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
3374 if (win->iconic == 1)
3375 continue;
3376
3377 sw = calloc(1, sizeof(struct search_window));
3378 if (sw == NULL) {
3379 fprintf(stderr, "search_win: calloc: %s", strerror(errno));
3380 fclose(lfile);
3381 search_win_cleanup();
3382 return;
3383 }
3384 sw->idx = i;
3385 sw->win = win;
3386
3387 snprintf(s, sizeof s, "%d", i);
3388 len = strlen(s);
3389 textwidth = XTextWidth(bar_fs, s, len);
3390
3391 w = XCreateSimpleWindow(display,
3392 win->id, 0, 0, textwidth + 12,
3393 bar_fs->ascent + bar_fs->descent + 4, 1,
3394 r->s->c[SWM_S_COLOR_UNFOCUS].color,
3395 r->s->c[SWM_S_COLOR_FOCUS].color);
3396
3397 sw->indicator = w;
3398 TAILQ_INSERT_TAIL(&search_wl, sw, entry);
3399
3400 gc = XCreateGC(display, w, 0, &gcv);
3401 XSetFont(display, gc, bar_fs->fid);
3402 XMapRaised(display, w);
3403 XSetForeground(display, gc, r->s->c[SWM_S_COLOR_BAR].color);
3404
3405 XDrawString(display, w, gc, 6, bar_fs->ascent + 2, s, len);
3406
3407 fprintf(lfile, "%d\n", i);
3408 i++;
3409 }
3410
3411 fclose(lfile);
3412 }
3413
3414 void
3415 search_resp_uniconify(char *resp, unsigned long len)
3416 {
3417 unsigned char *name;
3418 struct ws_win *win;
3419 char *s;
3420
3421 DNPRINTF(SWM_D_MISC, "search_resp_uniconify: resp %s\n", resp);
3422
3423 TAILQ_FOREACH(win, &search_r->ws->winlist, entry) {
3424 if (win->iconic == 0)
3425 continue;
3426 name = get_win_name(display, win->id, a_wmname, a_string, &len);
3427 if (name == NULL)
3428 continue;
3429 if (asprintf(&s, "%s.%lu", name, win->id) == -1) {
3430 XFree(name);
3431 continue;
3432 }
3433 XFree(name);
3434 if (strncmp(s, resp, len) == 0) {
3435 /* XXX this should be a callback to generalize */
3436 update_iconic(win, 0);
3437 free(s);
3438 break;
3439 }
3440 free(s);
3441 }
3442 }
3443
3444 void
3445 search_resp_name_workspace(char *resp, unsigned long len)
3446 {
3447 struct workspace *ws;
3448
3449 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: resp %s\n", resp);
3450
3451 if (search_r->ws == NULL)
3452 return;
3453 ws = search_r->ws;
3454
3455 if (ws->name) {
3456 free(search_r->ws->name);
3457 search_r->ws->name = NULL;
3458 }
3459
3460 if (len > 1) {
3461 ws->name = strdup(resp);
3462 if (ws->name == NULL) {
3463 DNPRINTF(SWM_D_MISC, "search_resp_name_workspace: strdup: %s",
3464 strerror(errno));
3465 return;
3466 }
3467 }
3468 }
3469
3470 void
3471 search_resp_search_workspace(char *resp, unsigned long len)
3472 {
3473 char *p, *q;
3474 int ws_idx;
3475 const char *errstr;
3476 union arg a;
3477
3478 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: resp %s\n", resp);
3479
3480 q = strdup(resp);
3481 if (!q) {
3482 DNPRINTF(SWM_D_MISC, "search_resp_search_workspace: strdup: %s",
3483 strerror(errno));
3484 return;
3485 }
3486 p = strchr(q, ':');
3487 if (p != NULL)
3488 *p = '\0';
3489 ws_idx = strtonum(q, 1, SWM_WS_MAX, &errstr);
3490 if (errstr) {
3491 DNPRINTF(SWM_D_MISC, "workspace idx is %s: %s",
3492 errstr, q);
3493 free(q);
3494 return;
3495 }
3496 free(q);
3497 a.id = ws_idx - 1;
3498 switchws(search_r, &a);
3499 }
3500
3501 void
3502 search_resp_search_window(char *resp, unsigned long len)
3503 {
3504 char *s;
3505 int idx;
3506 const char *errstr;
3507 struct search_window *sw;
3508
3509 DNPRINTF(SWM_D_MISC, "search_resp_search_window: resp %s\n", resp);
3510
3511 s = strdup(resp);
3512 if (!s) {
3513 DNPRINTF(SWM_D_MISC, "search_resp_search_window: strdup: %s",
3514 strerror(errno));
3515 return;
3516 }
3517
3518 idx = strtonum(s, 1, INT_MAX, &errstr);
3519 if (errstr) {
3520 DNPRINTF(SWM_D_MISC, "window idx is %s: %s",
3521 errstr, s);
3522 free(s);
3523 return;
3524 }
3525 free(s);
3526
3527 TAILQ_FOREACH(sw, &search_wl, entry)
3528 if (idx == sw->idx) {
3529 focus_win(sw->win);
3530 break;
3531 }
3532 }
3533
3534 #define MAX_RESP_LEN 1024
3535
3536 void
3537 search_do_resp(void)
3538 {
3539 ssize_t rbytes;
3540 char *resp;
3541 unsigned long len;
3542
3543 DNPRINTF(SWM_D_MISC, "search_do_resp:\n");
3544
3545 search_resp = 0;
3546 searchpid = 0;
3547
3548 if ((resp = calloc(1, MAX_RESP_LEN + 1)) == NULL) {
3549 fprintf(stderr, "search: calloc\n");
3550 goto done;
3551 }
3552
3553 rbytes = read(select_resp_pipe[0], resp, MAX_RESP_LEN);
3554 if (rbytes <= 0) {
3555 fprintf(stderr, "search: read error: %s\n", strerror(errno));
3556 goto done;
3557 }
3558 resp[rbytes] = '\0';
3559
3560 /* XXX:
3561 * Older versions of dmenu (Atleast pre 4.4.1) do not send a
3562 * newline, so work around that by sanitizing the resp now.
3563 */
3564 resp[strcspn(resp, "\n")] = '\0';
3565 len = strlen(resp);
3566
3567 switch (search_resp_action) {
3568 case SWM_SEARCH_UNICONIFY:
3569 search_resp_uniconify(resp, len);
3570 break;
3571 case SWM_SEARCH_NAME_WORKSPACE:
3572 search_resp_name_workspace(resp, len);
3573 break;
3574 case SWM_SEARCH_SEARCH_WORKSPACE:
3575 search_resp_search_workspace(resp, len);
3576 break;
3577 case SWM_SEARCH_SEARCH_WINDOW:
3578 search_resp_search_window(resp, len);
3579 break;
3580 }
3581
3582 done:
3583 if (search_resp_action == SWM_SEARCH_SEARCH_WINDOW)
3584 search_win_cleanup();
3585
3586 search_resp_action = SWM_SEARCH_NONE;
3587 close(select_resp_pipe[0]);
3588 free(resp);
3589 }
3590
3591 void
3592 wkill(struct swm_region *r, union arg *args)
3593 {
3594 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
3595
3596 if (r->ws->focus == NULL)
3597 return;
3598
3599 if (args->id == SWM_ARG_ID_KILLWINDOW)
3600 XKillClient(display, r->ws->focus->id);
3601 else
3602 if (r->ws->focus->can_delete)
3603 client_msg(r->ws->focus, adelete);
3604 }
3605
3606
3607 int
3608 floating_toggle_win(struct ws_win *win)
3609 {
3610 struct swm_region *r;
3611
3612 if (win == NULL)
3613 return 0;
3614
3615 if (!win->ws->r)
3616 return 0;
3617
3618 r = win->ws->r;
3619
3620 /* reject floating toggles in max stack mode */
3621 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK])
3622 return 0;
3623
3624 if (win->floating) {
3625 if (!win->floatmaxed) {
3626 /* retain position for refloat */
3627 store_float_geom(win, r);
3628 }
3629 win->floating = 0;
3630 } else {
3631 if (win->g_floatvalid) {
3632 /* refloat at last floating relative position */
3633 win->g.x = win->g_float.x - win->rg_float.x + r->g.x;
3634 win->g.y = win->g_float.y - win->rg_float.y + r->g.y;
3635 win->g.w = win->g_float.w;
3636 win->g.h = win->g_float.h;
3637 }
3638 win->floating = 1;
3639 }
3640
3641 ewmh_update_actions(win);
3642
3643 return 1;
3644 }
3645
3646 void
3647 floating_toggle(struct swm_region *r, union arg *args)
3648 {
3649 struct ws_win *win = r->ws->focus;
3650 union arg a;
3651
3652 if (win == NULL)
3653 return;
3654
3655 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3656 _NET_WM_STATE_TOGGLE);
3657
3658 stack();
3659 if (focus_mode == SWM_FOCUS_DEFAULT)
3660 drain_enter_notify();
3661
3662 if (win == win->ws->focus) {
3663 a.id = SWM_ARG_ID_FOCUSCUR;
3664 focus(win->ws->r, &a);
3665 }
3666 }
3667
3668 void
3669 resize_window(struct ws_win *win, int center)
3670 {
3671 unsigned int mask;
3672 XWindowChanges wc;
3673 struct swm_region *r;
3674
3675 r = root_to_region(win->wa.root);
3676 bzero(&wc, sizeof wc);
3677 mask = CWBorderWidth | CWWidth | CWHeight;
3678 wc.border_width = border_width;
3679 wc.width = win->g.w;
3680 wc.height = win->g.h;
3681 if (center == SWM_ARG_ID_CENTER) {
3682 wc.x = (WIDTH(r) - win->g.w) / 2 - border_width;
3683 wc.y = (HEIGHT(r) - win->g.h) / 2 - border_width;
3684 mask |= CWX | CWY;
3685 }
3686
3687 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
3688 win->id, wc.x, wc.y, wc.width, wc.height);
3689
3690 XConfigureWindow(display, win->id, mask, &wc);
3691 }
3692
3693 #define SWM_RESIZE_STEPS (50)
3694
3695 void
3696 resize(struct ws_win *win, union arg *args)
3697 {
3698 XEvent ev;
3699 Time time = 0;
3700 struct swm_region *r = NULL;
3701 int relx, rely;
3702 int resize_step = 0;
3703
3704 if (win == NULL)
3705 return;
3706 r = win->ws->r;
3707
3708 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
3709 win->id, win->floating, win->transient);
3710
3711 if (!(win->transient != 0 || win->floating != 0))
3712 return;
3713
3714 /* reject resizes in max mode for floaters (transient ok) */
3715 if (win->floatmaxed)
3716 return;
3717
3718 win->manual = 1;
3719 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3720 _NET_WM_STATE_ADD);
3721
3722 stack();
3723
3724 switch (args->id) {
3725 case SWM_ARG_ID_WIDTHSHRINK:
3726 win->g.w -= SWM_RESIZE_STEPS;
3727 resize_step = 1;
3728 break;
3729 case SWM_ARG_ID_WIDTHGROW:
3730 win->g.w += SWM_RESIZE_STEPS;
3731 resize_step = 1;
3732 break;
3733 case SWM_ARG_ID_HEIGHTSHRINK:
3734 win->g.h -= SWM_RESIZE_STEPS;
3735 resize_step = 1;
3736 break;
3737 case SWM_ARG_ID_HEIGHTGROW:
3738 win->g.h += SWM_RESIZE_STEPS;
3739 resize_step = 1;
3740 break;
3741 default:
3742 break;
3743 }
3744 if (resize_step) {
3745 resize_window(win, 0);
3746 store_float_geom(win,r);
3747 return;
3748 }
3749
3750 if (focus_mode == SWM_FOCUS_DEFAULT)
3751 drain_enter_notify();
3752
3753 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3754 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3755 return;
3756
3757 /* place pointer at bottom left corner or nearest point inside r */
3758 if ( win->g.x + win->g.w < r->g.x + r->g.w - 1)
3759 relx = win->g.w - 1;
3760 else
3761 relx = r->g.x + r->g.w - win->g.x - 1;
3762
3763 if ( win->g.y + win->g.h < r->g.y + r->g.h - 1)
3764 rely = win->g.h - 1;
3765 else
3766 rely = r->g.y + r->g.h - win->g.y - 1;
3767
3768 XWarpPointer(display, None, win->id, 0, 0, 0, 0, relx, rely);
3769 do {
3770 XMaskEvent(display, MOUSEMASK | ExposureMask |
3771 SubstructureRedirectMask, &ev);
3772 switch (ev.type) {
3773 case ConfigureRequest:
3774 case Expose:
3775 case MapRequest:
3776 handler[ev.type](&ev);
3777 break;
3778 case MotionNotify:
3779 /* do not allow resize outside of region */
3780 if ( ev.xmotion.x_root < r->g.x ||
3781 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3782 ev.xmotion.y_root < r->g.y ||
3783 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3784 continue;
3785
3786 if (ev.xmotion.x <= 1)
3787 ev.xmotion.x = 1;
3788 if (ev.xmotion.y <= 1)
3789 ev.xmotion.y = 1;
3790 win->g.w = ev.xmotion.x + 1;
3791 win->g.h = ev.xmotion.y + 1;
3792
3793 /* not free, don't sync more than 120 times / second */
3794 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3795 time = ev.xmotion.time;
3796 XSync(display, False);
3797 resize_window(win, args->id);
3798 }
3799 break;
3800 }
3801 } while (ev.type != ButtonRelease);
3802 if (time) {
3803 XSync(display, False);
3804 resize_window(win, args->id);
3805 }
3806 store_float_geom(win,r);
3807
3808 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
3809 win->g.h - 1);
3810 XUngrabPointer(display, CurrentTime);
3811
3812 /* drain events */
3813 drain_enter_notify();
3814 }
3815
3816 void
3817 resize_step(struct swm_region *r, union arg *args)
3818 {
3819 struct ws_win *win = NULL;
3820
3821 if (r && r->ws && r->ws->focus)
3822 win = r->ws->focus;
3823 else
3824 return;
3825
3826 resize(win, args);
3827 }
3828
3829
3830 void
3831 move_window(struct ws_win *win)
3832 {
3833 unsigned int mask;
3834 XWindowChanges wc;
3835
3836 bzero(&wc, sizeof wc);
3837 mask = CWX | CWY;
3838 wc.x = win->g.x;
3839 wc.y = win->g.y;
3840 wc.border_width = border_width;
3841
3842 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
3843 win->id, wc.x, wc.y, wc.width, wc.height);
3844
3845 XConfigureWindow(display, win->id, mask, &wc);
3846 }
3847
3848 #define SWM_MOVE_STEPS (50)
3849
3850 void
3851 move(struct ws_win *win, union arg *args)
3852 {
3853 XEvent ev;
3854 Time time = 0;
3855 int move_step = 0;
3856 struct swm_region *r = NULL;
3857
3858 if (win == NULL)
3859 return;
3860 r = win->ws->r;
3861
3862 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
3863 win->id, win->floating, win->transient);
3864
3865 /* in max_stack mode should only move transients */
3866 if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
3867 return;
3868
3869 win->manual = 1;
3870 if (win->floating == 0 && !win->transient) {
3871 ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
3872 _NET_WM_STATE_ADD);
3873 }
3874 ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
3875 _NET_WM_STATE_ADD);
3876
3877 stack();
3878
3879 move_step = 0;
3880 switch (args->id) {
3881 case SWM_ARG_ID_MOVELEFT:
3882 win->g.x -= (SWM_MOVE_STEPS - border_width);
3883 move_step = 1;
3884 break;
3885 case SWM_ARG_ID_MOVERIGHT:
3886 win->g.x += (SWM_MOVE_STEPS - border_width);
3887 move_step = 1;
3888 break;
3889 case SWM_ARG_ID_MOVEUP:
3890 win->g.y -= (SWM_MOVE_STEPS - border_width);
3891 move_step = 1;
3892 break;
3893 case SWM_ARG_ID_MOVEDOWN:
3894 win->g.y += (SWM_MOVE_STEPS - border_width);
3895 move_step = 1;
3896 break;
3897 default:
3898 break;
3899 }
3900 if (move_step) {
3901 move_window(win);
3902 store_float_geom(win,r);
3903 return;
3904 }
3905
3906
3907 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
3908 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
3909 return;
3910 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3911 do {
3912 XMaskEvent(display, MOUSEMASK | ExposureMask |
3913 SubstructureRedirectMask, &ev);
3914 switch (ev.type) {
3915 case ConfigureRequest:
3916 case Expose:
3917 case MapRequest:
3918 handler[ev.type](&ev);
3919 break;
3920 case MotionNotify:
3921 /* don't allow to move window origin out of region */
3922 if ( ev.xmotion.x_root < r->g.x ||
3923 ev.xmotion.x_root > r->g.x + r->g.w - 1 ||
3924 ev.xmotion.y_root < r->g.y ||
3925 ev.xmotion.y_root > r->g.y + r->g.h - 1)
3926 continue;
3927
3928 win->g.x = ev.xmotion.x_root - border_width;
3929 win->g.y = ev.xmotion.y_root - border_width;
3930
3931 /* not free, don't sync more than 120 times / second */
3932 if ((ev.xmotion.time - time) > (1000 / 120) ) {
3933 time = ev.xmotion.time;
3934 XSync(display, False);
3935 move_window(win);
3936 }
3937 break;
3938 }
3939 } while (ev.type != ButtonRelease);
3940 if (time) {
3941 XSync(display, False);
3942 move_window(win);
3943 }
3944 store_float_geom(win,r);
3945 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
3946 XUngrabPointer(display, CurrentTime);
3947
3948 /* drain events */
3949 drain_enter_notify();
3950 }
3951
3952 void
3953 move_step(struct swm_region *r, union arg *args)
3954 {
3955 struct ws_win *win = NULL;
3956
3957 if (r && r->ws && r->ws->focus)
3958 win = r->ws->focus;
3959 else
3960 return;
3961
3962 if (!(win->transient != 0 || win->floating != 0))
3963 return;
3964
3965 move(win, args);
3966 }
3967
3968
3969 /* user/key callable function IDs */
3970 enum keyfuncid {
3971 kf_cycle_layout,
3972 kf_stack_reset,
3973 kf_master_shrink,
3974 kf_master_grow,
3975 kf_master_add,
3976 kf_master_del,
3977 kf_stack_inc,
3978 kf_stack_dec,
3979 kf_swap_main,
3980 kf_focus_next,
3981 kf_focus_prev,
3982 kf_swap_next,
3983 kf_swap_prev,
3984 kf_spawn_term,
3985 kf_spawn_menu,
3986 kf_quit,
3987 kf_restart,
3988 kf_focus_main,
3989 kf_ws_1,
3990 kf_ws_2,
3991 kf_ws_3,
3992 kf_ws_4,
3993 kf_ws_5,
3994 kf_ws_6,
3995 kf_ws_7,
3996 kf_ws_8,
3997 kf_ws_9,
3998 kf_ws_10,
3999 kf_ws_next,
4000 kf_ws_prev,
4001 kf_ws_next_all,
4002 kf_ws_prev_all,
4003 kf_ws_prior,
4004 kf_screen_next,
4005 kf_screen_prev,
4006 kf_mvws_1,
4007 kf_mvws_2,
4008 kf_mvws_3,
4009 kf_mvws_4,
4010 kf_mvws_5,
4011 kf_mvws_6,
4012 kf_mvws_7,
4013 kf_mvws_8,
4014 kf_mvws_9,
4015 kf_mvws_10,
4016 kf_bar_toggle,
4017 kf_wind_kill,
4018 kf_wind_del,
4019 kf_screenshot_all,
4020 kf_screenshot_wind,
4021 kf_float_toggle,
4022 kf_version,
4023 kf_spawn_lock,
4024 kf_spawn_initscr,
4025 kf_spawn_custom,
4026 kf_iconify,
4027 kf_uniconify,
4028 kf_raise_toggle,
4029 kf_button2,
4030 kf_width_shrink,
4031 kf_width_grow,
4032 kf_height_shrink,
4033 kf_height_grow,
4034 kf_move_left,
4035 kf_move_right,
4036 kf_move_up,
4037 kf_move_down,
4038 kf_name_workspace,
4039 kf_search_workspace,
4040 kf_search_win,
4041 kf_dumpwins, /* MUST BE LAST */
4042 kf_invalid
4043 };
4044
4045 /* key definitions */
4046 void
4047 dummykeyfunc(struct swm_region *r, union arg *args)
4048 {
4049 };
4050
4051 void
4052 legacyfunc(struct swm_region *r, union arg *args)
4053 {
4054 };
4055
4056 struct keyfunc {
4057 char name[SWM_FUNCNAME_LEN];
4058 void (*func)(struct swm_region *r, union arg *);
4059 union arg args;
4060 } keyfuncs[kf_invalid + 1] = {
4061 /* name function argument */
4062 { "cycle_layout", cycle_layout, {0} },
4063 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
4064 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
4065 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
4066 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
4067 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
4068 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
4069 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
4070 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
4071 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
4072 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
4073 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
4074 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
4075 { "spawn_term", spawnterm, {.argv = spawn_term} },
4076 { "spawn_menu", legacyfunc, {0} },
4077 { "quit", quit, {0} },
4078 { "restart", restart, {0} },
4079 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
4080 { "ws_1", switchws, {.id = 0} },
4081 { "ws_2", switchws, {.id = 1} },
4082 { "ws_3", switchws, {.id = 2} },
4083 { "ws_4", switchws, {.id = 3} },
4084 { "ws_5", switchws, {.id = 4} },
4085 { "ws_6", switchws, {.id = 5} },
4086 { "ws_7", switchws, {.id = 6} },
4087 { "ws_8", switchws, {.id = 7} },
4088 { "ws_9", switchws, {.id = 8} },
4089 { "ws_10", switchws, {.id = 9} },
4090 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
4091 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
4092 { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
4093 { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
4094 { "ws_prior", priorws, {0} },
4095 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
4096 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
4097 { "mvws_1", send_to_ws, {.id = 0} },
4098 { "mvws_2", send_to_ws, {.id = 1} },
4099 { "mvws_3", send_to_ws, {.id = 2} },
4100 { "mvws_4", send_to_ws, {.id = 3} },
4101 { "mvws_5", send_to_ws, {.id = 4} },
4102 { "mvws_6", send_to_ws, {.id = 5} },
4103 { "mvws_7", send_to_ws, {.id = 6} },
4104 { "mvws_8", send_to_ws, {.id = 7} },
4105 { "mvws_9", send_to_ws, {.id = 8} },
4106 { "mvws_10", send_to_ws, {.id = 9} },
4107 { "bar_toggle", bar_toggle, {0} },
4108 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
4109 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
4110 { "screenshot_all", legacyfunc, {0} },
4111 { "screenshot_wind", legacyfunc, {0} },
4112 { "float_toggle", floating_toggle,{0} },
4113 { "version", version, {0} },
4114 { "spawn_lock", legacyfunc, {0} },
4115 { "spawn_initscr", legacyfunc, {0} },
4116 { "spawn_custom", dummykeyfunc, {0} },
4117 { "iconify", iconify, {0} },
4118 { "uniconify", uniconify, {0} },
4119 { "raise_toggle", raise_toggle, {0} },
4120 { "button2", pressbutton, {2} },
4121 { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
4122 { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
4123 { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
4124 { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
4125 { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
4126 { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
4127 { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
4128 { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
4129 { "name_workspace", name_workspace, {0} },
4130 { "search_workspace", search_workspace, {0} },
4131 { "search_win", search_win, {0} },
4132 { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
4133 { "invalid key func", NULL, {0} },
4134 };
4135 struct key {
4136 unsigned int mod;
4137 KeySym keysym;
4138 enum keyfuncid funcid;
4139 char *spawn_name;
4140 };
4141 int keys_size = 0, keys_length = 0;
4142 struct key *keys = NULL;
4143
4144 /* mouse */
4145 enum { client_click, root_click };
4146 struct button {
4147 unsigned int action;
4148 unsigned int mask;
4149 unsigned int button;
4150 void (*func)(struct ws_win *, union arg *);
4151 union arg args;
4152 } buttons[] = {
4153 /* action key mouse button func args */
4154 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
4155 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
4156 { client_click, MODKEY, Button1, move, {0} },
4157 };
4158
4159 void
4160 update_modkey(unsigned int mod)
4161 {
4162 int i;
4163
4164 mod_key = mod;
4165 for (i = 0; i < keys_length; i++)
4166 if (keys[i].mod & ShiftMask)
4167 keys[i].mod = mod | ShiftMask;
4168 else
4169 keys[i].mod = mod;
4170
4171 for (i = 0; i < LENGTH(buttons); i++)
4172 if (buttons[i].mask & ShiftMask)
4173 buttons[i].mask = mod | ShiftMask;
4174 else
4175 buttons[i].mask = mod;
4176 }
4177
4178 /* spawn */
4179 struct spawn_prog {
4180 char *name;
4181 int argc;
4182 char **argv;
4183 };
4184
4185 int spawns_size = 0, spawns_length = 0;
4186 struct spawn_prog *spawns = NULL;
4187
4188 int
4189 spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
4190 char ***ret_args)
4191 {
4192 struct spawn_prog *prog = NULL;
4193 int i;
4194 char *ap, **real_args;
4195
4196 DNPRINTF(SWM_D_SPAWN, "spawn_expand %s\n", spawn_name);
4197
4198 /* find program */
4199 for (i = 0; i < spawns_length; i++) {
4200 if (!strcasecmp(spawn_name, spawns[i].name))
4201 prog = &spawns[i];
4202 }
4203 if (prog == NULL) {
4204 fprintf(stderr, "spawn_custom: program %s not found\n",
4205 spawn_name);
4206 return (-1);
4207 }
4208
4209 /* make room for expanded args */
4210 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
4211 err(1, "spawn_custom: calloc real_args");
4212
4213 /* expand spawn_args into real_args */
4214 for (i = 0; i < prog->argc; i++) {
4215 ap = prog->argv[i];
4216 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
4217 if (!strcasecmp(ap, "$bar_border")) {
4218 if ((real_args[i] =
4219 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
4220 == NULL)
4221 err(1, "spawn_custom border color");
4222 } else if (!strcasecmp(ap, "$bar_color")) {
4223 if ((real_args[i] =
4224 strdup(r->s->c[SWM_S_COLOR_BAR].name))
4225 == NULL)
4226 err(1, "spawn_custom bar color");
4227 } else if (!strcasecmp(ap, "$bar_font")) {
4228 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
4229 == NULL)
4230 err(1, "spawn_custom bar fonts");
4231 } else if (!strcasecmp(ap, "$bar_font_color")) {
4232 if ((real_args[i] =
4233 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
4234 == NULL)
4235 err(1, "spawn_custom color font");
4236 } else if (!strcasecmp(ap, "$color_focus")) {
4237 if ((real_args[i] =
4238 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
4239 == NULL)
4240 err(1, "spawn_custom color focus");
4241 } else if (!strcasecmp(ap, "$color_unfocus")) {
4242 if ((real_args[i] =
4243 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
4244 == NULL)
4245 err(1, "spawn_custom color unfocus");
4246 } else {
4247 /* no match --> copy as is */
4248 if ((real_args[i] = strdup(ap)) == NULL)
4249 err(1, "spawn_custom strdup(ap)");
4250 }
4251 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
4252 real_args[i]);
4253 }
4254
4255 #ifdef SWM_DEBUG
4256 if ((swm_debug & SWM_D_SPAWN) != 0) {
4257 fprintf(stderr, "spawn_custom: result = ");
4258 for (i = 0; i < prog->argc; i++)
4259 fprintf(stderr, "\"%s\" ", real_args[i]);
4260 fprintf(stderr, "\n");
4261 }
4262 #endif
4263 *ret_args = real_args;
4264 return (prog->argc);
4265 }
4266
4267 void
4268 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
4269 {
4270 union arg a;
4271 char **real_args;
4272 int spawn_argc, i;
4273
4274 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4275 return;
4276 a.argv = real_args;
4277 if (fork() == 0)
4278 spawn(r->ws->idx, &a, 1);
4279
4280 for (i = 0; i < spawn_argc; i++)
4281 free(real_args[i]);
4282 free(real_args);
4283 }
4284
4285 void
4286 spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
4287 {
4288 union arg a;
4289 char **real_args;
4290 int i, spawn_argc;
4291
4292 if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
4293 return;
4294 a.argv = real_args;
4295
4296 if (pipe(select_list_pipe) == -1)
4297 err(1, "pipe error");
4298 if (pipe(select_resp_pipe) == -1)
4299 err(1, "pipe error");
4300
4301 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4302 err(1, "could not disable SIGPIPE");
4303 switch (*pid = fork()) {
4304 case -1:
4305 err(1, "cannot fork");
4306 break;
4307 case 0: /* child */
4308 if (dup2(select_list_pipe[0], 0) == -1)
4309 err(1, "dup2");
4310 if (dup2(select_resp_pipe[1], 1) == -1)
4311 err(1, "dup2");
4312 close(select_list_pipe[1]);
4313 close(select_resp_pipe[0]);
4314 spawn(r->ws->idx, &a, 0);
4315 break;
4316 default: /* parent */
4317 close(select_list_pipe[0]);
4318 close(select_resp_pipe[1]);
4319 break;
4320 }
4321
4322 for (i = 0; i < spawn_argc; i++)
4323 free(real_args[i]);
4324 free(real_args);
4325 }
4326
4327 void
4328 setspawn(struct spawn_prog *prog)
4329 {
4330 int i, j;
4331
4332 if (prog == NULL || prog->name == NULL)
4333 return;
4334
4335 /* find existing */
4336 for (i = 0; i < spawns_length; i++) {
4337 if (!strcmp(spawns[i].name, prog->name)) {
4338 /* found */
4339 if (prog->argv == NULL) {
4340 /* delete */
4341 DNPRINTF(SWM_D_SPAWN,
4342 "setspawn: delete #%d %s\n",
4343 i, spawns[i].name);
4344 free(spawns[i].name);
4345 for (j = 0; j < spawns[i].argc; j++)
4346 free(spawns[i].argv[j]);
4347 free(spawns[i].argv);
4348 j = spawns_length - 1;
4349 if (i < j)
4350 spawns[i] = spawns[j];
4351 spawns_length--;
4352 free(prog->name);
4353 } else {
4354 /* replace */
4355 DNPRINTF(SWM_D_SPAWN,
4356 "setspawn: replace #%d %s\n",
4357 i, spawns[i].name);
4358 free(spawns[i].name);
4359 for (j = 0; j < spawns[i].argc; j++)
4360 free(spawns[i].argv[j]);
4361 free(spawns[i].argv);
4362 spawns[i] = *prog;
4363 }
4364 /* found case handled */
4365 free(prog);
4366 return;
4367 }
4368 }
4369
4370 if (prog->argv == NULL) {
4371 fprintf(stderr,
4372 "error: setspawn: cannot find program %s", prog->name);
4373 free(prog);
4374 return;
4375 }
4376
4377 /* not found: add */
4378 if (spawns_size == 0 || spawns == NULL) {
4379 spawns_size = 4;
4380 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
4381 spawns = malloc((size_t)spawns_size *
4382 sizeof(struct spawn_prog));
4383 if (spawns == NULL) {
4384 fprintf(stderr, "setspawn: malloc failed\n");
4385 perror(" failed");
4386 quit(NULL, NULL);
4387 }
4388 } else if (spawns_length == spawns_size) {
4389 spawns_size *= 2;
4390 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
4391 spawns = realloc(spawns, (size_t)spawns_size *
4392 sizeof(struct spawn_prog));
4393 if (spawns == NULL) {
4394 fprintf(stderr, "setspawn: realloc failed\n");
4395 perror(" failed");
4396 quit(NULL, NULL);
4397 }
4398 }
4399
4400 if (spawns_length < spawns_size) {
4401 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
4402 spawns_length, prog->name);
4403 i = spawns_length++;
4404 spawns[i] = *prog;
4405 } else {
4406 fprintf(stderr, "spawns array problem?\n");
4407 if (spawns == NULL) {
4408 fprintf(stderr, "spawns array is NULL!\n");
4409 quit(NULL, NULL);
4410 }
4411 }
4412 free(prog);
4413 }
4414
4415 int
4416 setconfspawn(char *selector, char *value, int flags)
4417 {
4418 struct spawn_prog *prog;
4419 char *vp, *cp, *word;
4420
4421 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
4422 if ((prog = calloc(1, sizeof *prog)) == NULL)
4423 err(1, "setconfspawn: calloc prog");
4424 prog->name = strdup(selector);
4425 if (prog->name == NULL)
4426 err(1, "setconfspawn prog->name");
4427 if ((cp = vp = strdup(value)) == NULL)
4428 err(1, "setconfspawn: strdup(value) ");
4429 while ((word = strsep(&cp, " \t")) != NULL) {
4430 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
4431 if (cp)
4432 cp += (long)strspn(cp, " \t");
4433 if (strlen(word) > 0) {
4434 prog->argc++;
4435 if ((prog->argv = realloc(prog->argv,
4436 prog->argc * sizeof(char *))) == NULL)
4437 err(1, "setconfspawn: realloc");
4438 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
4439 err(1, "setconfspawn: strdup");
4440 }
4441 }
4442 free(vp);
4443
4444 setspawn(prog);
4445
4446 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
4447 return (0);
4448 }
4449
4450 void
4451 setup_spawn(void)
4452 {
4453 setconfspawn("term", "xterm", 0);
4454 setconfspawn("screenshot_all", "screenshot.sh full", 0);
4455 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
4456 setconfspawn("lock", "xlock", 0);
4457 setconfspawn("initscr", "initscreen.sh", 0);
4458 setconfspawn("menu", "dmenu_run"
4459 " -fn $bar_font"
4460 " -nb $bar_color"
4461 " -nf $bar_font_color"
4462 " -sb $bar_border"
4463 " -sf $bar_color", 0);
4464 setconfspawn("search", "dmenu"
4465 " -i"
4466 " -fn $bar_font"
4467 " -nb $bar_color"
4468 " -nf $bar_font_color"
4469 " -sb $bar_border"
4470 " -sf $bar_color", 0);
4471 setconfspawn("name_workspace", "dmenu"
4472 " -p Workspace"
4473 " -fn $bar_font"
4474 " -nb $bar_color"
4475 " -nf $bar_font_color"
4476 " -sb $bar_border"
4477 " -sf $bar_color", 0);
4478 }
4479
4480 /* key bindings */
4481 #define SWM_MODNAME_SIZE 32
4482 #define SWM_KEY_WS "\n+ \t"
4483 int
4484 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
4485 {
4486 char *cp, *name;
4487 KeySym uks;
4488 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
4489 if (mod == NULL || ks == NULL) {
4490 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
4491 return (1);
4492 }
4493 if (keystr == NULL || strlen(keystr) == 0) {
4494 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
4495 return (1);
4496 }
4497 cp = keystr;
4498 *ks = NoSymbol;
4499 *mod = 0;
4500 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
4501 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
4502 if (cp)
4503 cp += (long)strspn(cp, SWM_KEY_WS);
4504 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
4505 *mod |= currmod;
4506 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
4507 *mod |= Mod1Mask;
4508 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
4509 *mod += Mod2Mask;
4510 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
4511 *mod |= Mod3Mask;
4512 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
4513 *mod |= Mod4Mask;
4514 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
4515 *mod |= ShiftMask;
4516 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
4517 *mod |= ControlMask;
4518 else {
4519 *ks = XStringToKeysym(name);
4520 XConvertCase(*ks, ks, &uks);
4521 if (ks == NoSymbol) {
4522 DNPRINTF(SWM_D_KEY,
4523 "parsekeys: invalid key %s\n",
4524 name);
4525 return (1);
4526 }
4527 }
4528 }
4529 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
4530 return (0);
4531 }
4532
4533 char *
4534 strdupsafe(char *str)
4535 {
4536 if (str == NULL)
4537 return (NULL);
4538 else
4539 return (strdup(str));
4540 }
4541
4542 void
4543 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
4544 char *spawn_name)
4545 {
4546 int i, j;
4547 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
4548 keyfuncs[kfid].name, spawn_name);
4549 /* find existing */
4550 for (i = 0; i < keys_length; i++) {
4551 if (keys[i].mod == mod && keys[i].keysym == ks) {
4552 if (kfid == kf_invalid) {
4553 /* found: delete */
4554 DNPRINTF(SWM_D_KEY,
4555 "setkeybinding: delete #%d %s\n",
4556 i, keyfuncs[keys[i].funcid].name);
4557 free(keys[i].spawn_name);
4558 j = keys_length - 1;
4559 if (i < j)
4560 keys[i] = keys[j];
4561 keys_length--;
4562 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4563 return;
4564 } else {
4565 /* found: replace */
4566 DNPRINTF(SWM_D_KEY,
4567 "setkeybinding: replace #%d %s %s\n",
4568 i, keyfuncs[keys[i].funcid].name,
4569 spawn_name);
4570 free(keys[i].spawn_name);
4571 keys[i].mod = mod;
4572 keys[i].keysym = ks;
4573 keys[i].funcid = kfid;
4574 keys[i].spawn_name = strdupsafe(spawn_name);
4575 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4576 return;
4577 }
4578 }
4579 }
4580 if (kfid == kf_invalid) {
4581 fprintf(stderr,
4582 "error: setkeybinding: cannot find mod/key combination");
4583 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4584 return;
4585 }
4586 /* not found: add */
4587 if (keys_size == 0 || keys == NULL) {
4588 keys_size = 4;
4589 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
4590 keys = malloc((size_t)keys_size * sizeof(struct key));
4591 if (keys == NULL) {
4592 fprintf(stderr, "malloc failed\n");
4593 perror(" failed");
4594 quit(NULL, NULL);
4595 }
4596 } else if (keys_length == keys_size) {
4597 keys_size *= 2;
4598 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
4599 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
4600 if (keys == NULL) {
4601 fprintf(stderr, "realloc failed\n");
4602 perror(" failed");
4603 quit(NULL, NULL);
4604 }
4605 }
4606 if (keys_length < keys_size) {
4607 j = keys_length++;
4608 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
4609 j, keyfuncs[kfid].name, spawn_name);
4610 keys[j].mod = mod;
4611 keys[j].keysym = ks;
4612 keys[j].funcid = kfid;
4613 keys[j].spawn_name = strdupsafe(spawn_name);
4614 } else {
4615 fprintf(stderr, "keys array problem?\n");
4616 if (keys == NULL) {
4617 fprintf(stderr, "keys array problem\n");
4618 quit(NULL, NULL);
4619 }
4620 }
4621 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
4622 }
4623
4624 int
4625 setconfbinding(char *selector, char *value, int flags)
4626 {
4627 enum keyfuncid kfid;
4628 unsigned int mod;
4629 KeySym ks;
4630 int i;
4631 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
4632 if (selector == NULL) {
4633 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
4634 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4635 kfid = kf_invalid;
4636 setkeybinding(mod, ks, kfid, NULL);
4637 return (0);
4638 } else
4639 return (1);
4640 }
4641 /* search by key function name */
4642 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
4643 if (strncasecmp(selector, keyfuncs[kfid].name,
4644 SWM_FUNCNAME_LEN) == 0) {
4645 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4646 selector);
4647 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4648 setkeybinding(mod, ks, kfid, NULL);
4649 return (0);
4650 } else
4651 return (1);
4652 }
4653 }
4654 /* search by custom spawn name */
4655 for (i = 0; i < spawns_length; i++) {
4656 if (strcasecmp(selector, spawns[i].name) == 0) {
4657 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
4658 selector);
4659 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
4660 setkeybinding(mod, ks, kf_spawn_custom,
4661 spawns[i].name);
4662 return (0);
4663 } else
4664 return (1);
4665 }
4666 }
4667 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
4668 return (1);
4669 }
4670
4671 void
4672 setup_keys(void)
4673 {
4674 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
4675 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
4676 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
4677 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
4678 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
4679 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
4680 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
4681 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
4682 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
4683 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
4684 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
4685 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
4686 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
4687 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
4688 setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
4689 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
4690 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
4691 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
4692 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
4693 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
4694 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
4695 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
4696 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
4697 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
4698 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
4699 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
4700 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
4701 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
4702 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
4703 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
4704 setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
4705 setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
4706 setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
4707 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
4708 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
4709 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
4710 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
4711 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
4712 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
4713 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
4714 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
4715 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
4716 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
4717 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
4718 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
4719 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
4720 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
4721 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
4722 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
4723 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
4724 setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
4725 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
4726 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
4727 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
4728 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
4729 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
4730 setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
4731 setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
4732 setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
4733 setkeybinding(MODKEY, XK_v, kf_button2, NULL);
4734 setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
4735 setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
4736 setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
4737 setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
4738 setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
4739 setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
4740 setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
4741 setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
4742 setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
4743 setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
4744 setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
4745 #ifdef SWM_DEBUG
4746 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
4747 #endif
4748 }
4749
4750 void
4751 clear_keys(void)
4752 {
4753 int i;
4754
4755 /* clear all key bindings, if any */
4756 for (i = 0; i < keys_length; i++)
4757 free(keys[i].spawn_name);
4758 keys_length = 0;
4759 }
4760
4761 int
4762 setkeymapping(char *selector, char *value, int flags)
4763 {
4764 char keymapping_file[PATH_MAX];
4765 DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
4766 if (value[0] == '~')
4767 snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
4768 pwd->pw_dir, &value[1]);
4769 else
4770 strlcpy(keymapping_file, value, sizeof keymapping_file);
4771 clear_keys();
4772 /* load new key bindings; if it fails, revert to default bindings */
4773 if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
4774 clear_keys();
4775 setup_keys();
4776 }
4777 DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
4778 return (0);
4779 }
4780
4781 void
4782 updatenumlockmask(void)
4783 {
4784 unsigned int i, j;
4785 XModifierKeymap *modmap;
4786
4787 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
4788 numlockmask = 0;
4789 modmap = XGetModifierMapping(display);
4790 for (i = 0; i < 8; i++)
4791 for (j = 0; j < modmap->max_keypermod; j++)
4792 if (modmap->modifiermap[i * modmap->max_keypermod + j]
4793 == XKeysymToKeycode(display, XK_Num_Lock))
4794 numlockmask = (1 << i);
4795
4796 XFreeModifiermap(modmap);
4797 }
4798
4799 void
4800 grabkeys(void)
4801 {
4802 unsigned int i, j, k;
4803 KeyCode code;
4804 unsigned int modifiers[] =
4805 { 0, LockMask, numlockmask, numlockmask | LockMask };
4806
4807 DNPRINTF(SWM_D_MISC, "grabkeys\n");
4808 updatenumlockmask();
4809
4810 for (k = 0; k < ScreenCount(display); k++) {
4811 if (TAILQ_EMPTY(&screens[k].rl))
4812 continue;
4813 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
4814 for (i = 0; i < keys_length; i++) {
4815 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
4816 for (j = 0; j < LENGTH(modifiers); j++)
4817 XGrabKey(display, code,
4818 keys[i].mod | modifiers[j],
4819 screens[k].root, True,
4820 GrabModeAsync, GrabModeAsync);
4821 }
4822 }
4823 }
4824
4825 void
4826 grabbuttons(struct ws_win *win, int focused)
4827 {
4828 unsigned int i, j;
4829 unsigned int modifiers[] =
4830 { 0, LockMask, numlockmask, numlockmask|LockMask };
4831
4832 updatenumlockmask();
4833 XUngrabButton(display, AnyButton, AnyModifier, win->id);
4834 if (focused) {
4835 for (i = 0; i < LENGTH(buttons); i++)
4836 if (buttons[i].action == client_click)
4837 for (j = 0; j < LENGTH(modifiers); j++)
4838 XGrabButton(display, buttons[i].button,
4839 buttons[i].mask | modifiers[j],
4840 win->id, False, BUTTONMASK,
4841 GrabModeAsync, GrabModeSync, None,
4842 None);
4843 } else
4844 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
4845 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
4846 }
4847
4848 const char *quirkname[] = {
4849 "NONE", /* config string for "no value" */
4850 "FLOAT",
4851 "TRANSSZ",
4852 "ANYWHERE",
4853 "XTERM_FONTADJ",
4854 "FULLSCREEN",
4855 "FOCUSPREV",
4856 };
4857
4858 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
4859 #define SWM_Q_WS "\n|+ \t"
4860 int
4861 parsequirks(char *qstr, unsigned long *quirk)
4862 {
4863 char *cp, *name;
4864 int i;
4865
4866 if (quirk == NULL)
4867 return (1);
4868
4869 cp = qstr;
4870 *quirk = 0;
4871 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
4872 if (cp)
4873 cp += (long)strspn(cp, SWM_Q_WS);
4874 for (i = 0; i < LENGTH(quirkname); i++) {
4875 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
4876 DNPRINTF(SWM_D_QUIRK,
4877 "parsequirks: %s\n", name);
4878 if (i == 0) {
4879 *quirk = 0;
4880 return (0);
4881 }
4882 *quirk |= 1 << (i-1);
4883 break;
4884 }
4885 }
4886 if (i >= LENGTH(quirkname)) {
4887 DNPRINTF(SWM_D_QUIRK,
4888 "parsequirks: invalid quirk [%s]\n", name);
4889 return (1);
4890 }
4891 }
4892 return (0);
4893 }
4894
4895 void
4896 setquirk(const char *class, const char *name, const int quirk)
4897 {
4898 int i, j;
4899
4900 /* find existing */
4901 for (i = 0; i < quirks_length; i++) {
4902 if (!strcmp(quirks[i].class, class) &&
4903 !strcmp(quirks[i].name, name)) {
4904 if (!quirk) {
4905 /* found: delete */
4906 DNPRINTF(SWM_D_QUIRK,
4907 "setquirk: delete #%d %s:%s\n",
4908 i, quirks[i].class, quirks[i].name);
4909 free(quirks[i].class);
4910 free(quirks[i].name);
4911 j = quirks_length - 1;
4912 if (i < j)
4913 quirks[i] = quirks[j];
4914 quirks_length--;
4915 return;
4916 } else {
4917 /* found: replace */
4918 DNPRINTF(SWM_D_QUIRK,
4919 "setquirk: replace #%d %s:%s\n",
4920 i, quirks[i].class, quirks[i].name);
4921 free(quirks[i].class);
4922 free(quirks[i].name);
4923 quirks[i].class = strdup(class);
4924 quirks[i].name = strdup(name);
4925 quirks[i].quirk = quirk;
4926 return;
4927 }
4928 }
4929 }
4930 if (!quirk) {
4931 fprintf(stderr,
4932 "error: setquirk: cannot find class/name combination");
4933 return;
4934 }
4935 /* not found: add */
4936 if (quirks_size == 0 || quirks == NULL) {
4937 quirks_size = 4;
4938 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
4939 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
4940 if (quirks == NULL) {
4941 fprintf(stderr, "setquirk: malloc failed\n");
4942 perror(" failed");
4943 quit(NULL, NULL);
4944 }
4945 } else if (quirks_length == quirks_size) {
4946 quirks_size *= 2;
4947 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
4948 quirks = realloc(quirks,
4949 (size_t)quirks_size * sizeof(struct quirk));
4950 if (quirks == NULL) {
4951 fprintf(stderr, "setquirk: realloc failed\n");
4952 perror(" failed");
4953 quit(NULL, NULL);
4954 }
4955 }
4956 if (quirks_length < quirks_size) {
4957 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
4958 j = quirks_length++;
4959 quirks[j].class = strdup(class);
4960 quirks[j].name = strdup(name);
4961 quirks[j].quirk = quirk;
4962 } else {
4963 fprintf(stderr, "quirks array problem?\n");
4964 if (quirks == NULL) {
4965 fprintf(stderr, "quirks array problem!\n");
4966 quit(NULL, NULL);
4967 }
4968 }
4969 }
4970
4971 int
4972 setconfquirk(char *selector, char *value, int flags)
4973 {
4974 char *cp, *class, *name;
4975 int retval;
4976 unsigned long quirks;
4977 if (selector == NULL)
4978 return (0);
4979 if ((cp = strchr(selector, ':')) == NULL)
4980 return (0);
4981 *cp = '\0';
4982 class = selector;
4983 name = cp + 1;
4984 if ((retval = parsequirks(value, &quirks)) == 0)
4985 setquirk(class, name, quirks);
4986 return (retval);
4987 }
4988
4989 void
4990 setup_quirks(void)
4991 {
4992 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN | SWM_Q_FOCUSPREV);
4993 setquirk("OpenOffice.org 3.2", "VCLSalFrame", SWM_Q_FLOAT);
4994 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
4995 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
4996 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4997 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
4998 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
4999 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5000 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5001 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
5002 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
5003 setquirk("pcb", "pcb", SWM_Q_FLOAT);
5004 setquirk("SDL_App", "SDL_App", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
5005 }
5006
5007 /* conf file stuff */
5008 #define SWM_CONF_FILE "scrotwm.conf"
5009
5010 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_BAR_BORDER_WIDTH,
5011 SWM_S_STACK_ENABLED, SWM_S_CLOCK_ENABLED, SWM_S_CLOCK_FORMAT,
5012 SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE, SWM_S_SS_ENABLED,
5013 SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
5014 SWM_S_TITLE_NAME_ENABLED, SWM_S_WINDOW_NAME_ENABLED, SWM_S_URGENT_ENABLED,
5015 SWM_S_FOCUS_MODE, SWM_S_DISABLE_BORDER, SWM_S_BORDER_WIDTH,
5016 SWM_S_BAR_FONT, SWM_S_BAR_ACTION, SWM_S_SPAWN_TERM,
5017 SWM_S_SS_APP, SWM_S_DIALOG_RATIO, SWM_S_BAR_AT_BOTTOM,
5018 SWM_S_VERBOSE_LAYOUT, SWM_S_BAR_JUSTIFY
5019 };
5020
5021 int
5022 setconfvalue(char *selector, char *value, int flags)
5023 {
5024 int i;
5025
5026 switch (flags) {
5027 case SWM_S_BAR_DELAY:
5028 bar_delay = atoi(value);
5029 break;
5030 case SWM_S_BAR_ENABLED:
5031 bar_enabled = atoi(value);
5032 break;
5033 case SWM_S_BAR_BORDER_WIDTH:
5034 bar_border_width = atoi(value);
5035 break;
5036 case SWM_S_BAR_AT_BOTTOM:
5037 bar_at_bottom = atoi(value);
5038 break;
5039 case SWM_S_BAR_JUSTIFY:
5040 if (!strcmp(value, "left"))
5041 bar_justify = SWM_BAR_JUSTIFY_LEFT;
5042 else if (!strcmp(value, "center"))
5043 bar_justify = SWM_BAR_JUSTIFY_CENTER;
5044 else if (!strcmp(value, "right"))
5045 bar_justify = SWM_BAR_JUSTIFY_RIGHT;
5046 else
5047 errx(1, "invalid bar_justify");
5048 break;
5049 case SWM_S_STACK_ENABLED:
5050 stack_enabled = atoi(value);
5051 break;
5052 case SWM_S_CLOCK_ENABLED:
5053 clock_enabled = atoi(value);
5054 break;
5055 case SWM_S_CLOCK_FORMAT:
5056 #ifndef SWM_DENY_CLOCK_FORMAT
5057 free(clock_format);
5058 if ((clock_format = strdup(value)) == NULL)
5059 err(1, "setconfvalue: clock_format");
5060 #endif
5061 break;
5062 case SWM_S_CYCLE_EMPTY:
5063 cycle_empty = atoi(value);
5064 break;
5065 case SWM_S_CYCLE_VISIBLE:
5066 cycle_visible = atoi(value);
5067 break;
5068 case SWM_S_SS_ENABLED:
5069 ss_enabled = atoi(value);
5070 break;
5071 case SWM_S_TERM_WIDTH:
5072 term_width = atoi(value);
5073 break;
5074 case SWM_S_TITLE_CLASS_ENABLED:
5075 title_class_enabled = atoi(value);
5076 break;
5077 case SWM_S_WINDOW_NAME_ENABLED:
5078 window_name_enabled = atoi(value);
5079 break;
5080 case SWM_S_TITLE_NAME_ENABLED:
5081 title_name_enabled = atoi(value);
5082 break;
5083 case SWM_S_URGENT_ENABLED:
5084 urgent_enabled = atoi(value);
5085 break;
5086 case SWM_S_FOCUS_MODE:
5087 if (!strcmp(value, "default"))
5088 focus_mode = SWM_FOCUS_DEFAULT;
5089 else if (!strcmp(value, "follow_cursor"))
5090 focus_mode = SWM_FOCUS_FOLLOW;
5091 else if (!strcmp(value, "synergy"))
5092 focus_mode = SWM_FOCUS_SYNERGY;
5093 else
5094 errx(1, "focus_mode");
5095 break;
5096 case SWM_S_DISABLE_BORDER:
5097 disable_border = atoi(value);
5098 break;
5099 case SWM_S_BORDER_WIDTH:
5100 border_width = atoi(value);
5101 break;
5102 case SWM_S_BAR_FONT:
5103 free(bar_fonts[0]);
5104 if ((bar_fonts[0] = strdup(value)) == NULL)
5105 err(1, "setconfvalue: bar_font");
5106 break;
5107 case SWM_S_BAR_ACTION:
5108 free(bar_argv[0]);
5109 if ((bar_argv[0] = strdup(value)) == NULL)
5110 err(1, "setconfvalue: bar_action");
5111 break;
5112 case SWM_S_SPAWN_TERM:
5113 free(spawn_term[0]);
5114 if ((spawn_term[0] = strdup(value)) == NULL)
5115 err(1, "setconfvalue: spawn_term");
5116 break;
5117 case SWM_S_SS_APP:
5118 break;
5119 case SWM_S_DIALOG_RATIO:
5120 dialog_ratio = atof(value);
5121 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
5122 dialog_ratio = .6;
5123 break;
5124 case SWM_S_VERBOSE_LAYOUT:
5125 verbose_layout = atoi(value);
5126 for (i = 0; layouts[i].l_stack != NULL; i++) {
5127 if (verbose_layout)
5128 layouts[i].l_string = fancy_stacker;
5129 else
5130 layouts[i].l_string = plain_stacker;
5131 }
5132 break;
5133 default:
5134 return (1);
5135 }
5136 return (0);
5137 }
5138
5139 int
5140 setconfmodkey(char *selector, char *value, int flags)
5141 {
5142 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
5143 update_modkey(Mod1Mask);
5144 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
5145 update_modkey(Mod2Mask);
5146 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
5147 update_modkey(Mod3Mask);
5148 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
5149 update_modkey(Mod4Mask);
5150 else
5151 return (1);
5152 return (0);
5153 }
5154
5155 int
5156 setconfcolor(char *selector, char *value, int flags)
5157 {
5158 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
5159 return (0);
5160 }
5161
5162 int
5163 setconfregion(char *selector, char *value, int flags)
5164 {
5165 custom_region(value);
5166 return (0);
5167 }
5168
5169 int
5170 setautorun(char *selector, char *value, int flags)
5171 {
5172 int ws_id;
5173 char s[1024];
5174 char *ap, *sp = s;
5175 union arg a;
5176 int argc = 0;
5177 long pid;
5178 struct pid_e *p;
5179
5180 if (getenv("SWM_STARTED"))
5181 return (0);
5182
5183 bzero(s, sizeof s);
5184 if (sscanf(value, "ws[%d]:%1023c", &ws_id, s) != 2)
5185 errx(1, "invalid autorun entry, should be 'ws[<idx>]:command'");
5186 ws_id--;
5187 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5188 errx(1, "autorun: invalid workspace %d", ws_id + 1);
5189
5190 /*
5191 * This is a little intricate
5192 *
5193 * If the pid already exists we simply reuse it because it means it was
5194 * used before AND not claimed by manage_window. We get away with
5195 * altering it in the parent after INSERT because this can not be a race
5196 */
5197 a.argv = NULL;
5198 while ((ap = strsep(&sp, " \t")) != NULL) {
5199 if (*ap == '\0')
5200 continue;
5201 DNPRINTF(SWM_D_SPAWN, "setautorun: arg [%s]\n", ap);
5202 argc++;
5203 if ((a.argv = realloc(a.argv, argc * sizeof(char *))) == NULL)
5204 err(1, "setautorun: realloc");
5205 a.argv[argc - 1] = ap;
5206 }
5207
5208 if ((a.argv = realloc(a.argv, (argc + 1) * sizeof(char *))) == NULL)
5209 err(1, "setautorun: realloc");
5210 a.argv[argc] = NULL;
5211
5212 if ((pid = fork()) == 0) {
5213 spawn(ws_id, &a, 1);
5214 /* NOTREACHED */
5215 _exit(1);
5216 }
5217 free(a.argv);
5218
5219 /* parent */
5220 p = find_pid(pid);
5221 if (p == NULL) {
5222 p = calloc(1, sizeof *p);
5223 if (p == NULL)
5224 return (1);
5225 TAILQ_INSERT_TAIL(&pidlist, p, entry);
5226 }
5227
5228 p->pid = pid;
5229 p->ws = ws_id;
5230
5231 return (0);
5232 }
5233
5234 int
5235 setlayout(char *selector, char *value, int flags)
5236 {
5237 int ws_id, i, x, mg, ma, si, raise;
5238 int st = SWM_V_STACK;
5239 char s[1024];
5240 struct workspace *ws;
5241
5242 if (getenv("SWM_STARTED"))
5243 return (0);
5244
5245 bzero(s, sizeof s);
5246 if (sscanf(value, "ws[%d]:%d:%d:%d:%d:%1023c",
5247 &ws_id, &mg, &ma, &si, &raise, s) != 6)
5248 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5249 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5250 "<type>'");
5251 ws_id--;
5252 if (ws_id < 0 || ws_id >= SWM_WS_MAX)
5253 errx(1, "layout: invalid workspace %d", ws_id + 1);
5254
5255 if (!strcasecmp(s, "vertical"))
5256 st = SWM_V_STACK;
5257 else if (!strcasecmp(s, "horizontal"))
5258 st = SWM_H_STACK;
5259 else if (!strcasecmp(s, "fullscreen"))
5260 st = SWM_MAX_STACK;
5261 else
5262 errx(1, "invalid layout entry, should be 'ws[<idx>]:"
5263 "<master_grow>:<master_add>:<stack_inc>:<always_raise>:"
5264 "<type>'");
5265
5266 for (i = 0; i < ScreenCount(display); i++) {
5267 ws = (struct workspace *)&screens[i].ws;
5268 ws[ws_id].cur_layout = &layouts[st];
5269
5270 ws[ws_id].always_raise = raise;
5271 if (st == SWM_MAX_STACK)
5272 continue;
5273
5274 /* master grow */
5275 for (x = 0; x < abs(mg); x++) {
5276 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5277 mg >= 0 ? SWM_ARG_ID_MASTERGROW :
5278 SWM_ARG_ID_MASTERSHRINK);
5279 stack();
5280 }
5281 /* master add */
5282 for (x = 0; x < abs(ma); x++) {
5283 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5284 ma >= 0 ? SWM_ARG_ID_MASTERADD :
5285 SWM_ARG_ID_MASTERDEL);
5286 stack();
5287 }
5288 /* stack inc */
5289 for (x = 0; x < abs(si); x++) {
5290 ws[ws_id].cur_layout->l_config(&ws[ws_id],
5291 si >= 0 ? SWM_ARG_ID_STACKINC :
5292 SWM_ARG_ID_STACKDEC);
5293 stack();
5294 }
5295 }
5296
5297 return (0);
5298 }
5299
5300 /* config options */
5301 struct config_option {
5302 char *optname;
5303 int (*func)(char*, char*, int);
5304 int funcflags;
5305 };
5306 struct config_option configopt[] = {
5307 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
5308 { "bar_at_bottom", setconfvalue, SWM_S_BAR_AT_BOTTOM },
5309 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
5310 { "bar_border_width", setconfvalue, SWM_S_BAR_BORDER_WIDTH },
5311 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
5312 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
5313 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
5314 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
5315 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
5316 { "bar_justify", setconfvalue, SWM_S_BAR_JUSTIFY },
5317 { "keyboard_mapping", setkeymapping, 0 },
5318 { "bind", setconfbinding, 0 },
5319 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
5320 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
5321 { "clock_format", setconfvalue, SWM_S_CLOCK_FORMAT },
5322 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
5323 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
5324 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
5325 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
5326 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
5327 { "verbose_layout", setconfvalue, SWM_S_VERBOSE_LAYOUT },
5328 { "modkey", setconfmodkey, 0 },
5329 { "program", setconfspawn, 0 },
5330 { "quirk", setconfquirk, 0 },
5331 { "region", setconfregion, 0 },
5332 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
5333 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
5334 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
5335 { "window_name_enabled", setconfvalue, SWM_S_WINDOW_NAME_ENABLED },
5336 { "urgent_enabled", setconfvalue, SWM_S_URGENT_ENABLED },
5337 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
5338 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
5339 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED },
5340 { "focus_mode", setconfvalue, SWM_S_FOCUS_MODE },
5341 { "disable_border", setconfvalue, SWM_S_DISABLE_BORDER },
5342 { "border_width", setconfvalue, SWM_S_BORDER_WIDTH },
5343 { "autorun", setautorun, 0 },
5344 { "layout", setlayout, 0 },
5345 };
5346
5347
5348 int
5349 conf_load(char *filename, int keymapping)
5350 {
5351 FILE *config;
5352 char *line, *cp, *optsub, *optval;
5353 size_t linelen, lineno = 0;
5354 int wordlen, i, optind;
5355 struct config_option *opt;
5356
5357 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
5358
5359 if (filename == NULL) {
5360 fprintf(stderr, "conf_load: no filename\n");
5361 return (1);
5362 }
5363 if ((config = fopen(filename, "r")) == NULL) {
5364 warn("conf_load: fopen: %s", filename);
5365 return (1);
5366 }
5367
5368 while (!feof(config)) {
5369 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
5370 == NULL) {
5371 if (ferror(config))
5372 err(1, "%s", filename);
5373 else
5374 continue;
5375 }
5376 cp = line;
5377 cp += strspn(cp, " \t\n"); /* eat whitespace */
5378 if (cp[0] == '\0') {
5379 /* empty line */
5380 free(line);
5381 continue;
5382 }
5383 /* get config option */
5384 wordlen = strcspn(cp, "=[ \t\n");
5385 if (wordlen == 0) {
5386 warnx("%s: line %zd: no option found",
5387 filename, lineno);
5388 return (1);
5389 }
5390 optind = -1;
5391 for (i = 0; i < LENGTH(configopt); i++) {
5392 opt = &configopt[i];
5393 if (!strncasecmp(cp, opt->optname, wordlen) &&
5394 strlen(opt->optname) == wordlen) {
5395 optind = i;
5396 break;
5397 }
5398 }
5399 if (optind == -1) {
5400 warnx("%s: line %zd: unknown option %.*s",
5401 filename, lineno, wordlen, cp);
5402 return (1);
5403 }
5404 if (keymapping && strcmp(opt->optname, "bind")) {
5405 warnx("%s: line %zd: invalid option %.*s",
5406 filename, lineno, wordlen, cp);
5407 return (1);
5408 }
5409 cp += wordlen;
5410 cp += strspn(cp, " \t\n"); /* eat whitespace */
5411 /* get [selector] if any */
5412 optsub = NULL;
5413 if (*cp == '[') {
5414 cp++;
5415 wordlen = strcspn(cp, "]");
5416 if (*cp != ']') {
5417 if (wordlen == 0) {
5418 warnx("%s: line %zd: syntax error",
5419 filename, lineno);
5420 return (1);
5421 }
5422 asprintf(&optsub, "%.*s", wordlen, cp);
5423 }
5424 cp += wordlen;
5425 cp += strspn(cp, "] \t\n"); /* eat trailing */
5426 }
5427 cp += strspn(cp, "= \t\n"); /* eat trailing */
5428 /* get RHS value */
5429 optval = strdup(cp);
5430 /* call function to deal with it all */
5431 if (configopt[optind].func(optsub, optval,
5432 configopt[optind].funcflags) != 0) {
5433 fprintf(stderr, "%s line %zd: %s\n",
5434 filename, lineno, line);
5435 errx(1, "%s: line %zd: invalid data for %s",
5436 filename, lineno, configopt[optind].optname);
5437 }
5438 free(optval);
5439 free(optsub);
5440 free(line);
5441 }
5442
5443 fclose(config);
5444 DNPRINTF(SWM_D_CONF, "conf_load end\n");
5445
5446 return (0);
5447 }
5448
5449 void
5450 set_child_transient(struct ws_win *win, Window *trans)
5451 {
5452 struct ws_win *parent, *w;
5453 XWMHints *wmh = NULL;
5454 struct swm_region *r;
5455 struct workspace *ws;
5456
5457 parent = find_window(win->transient);
5458 if (parent)
5459 parent->child_trans = win;
5460 else {
5461 DNPRINTF(SWM_D_MISC, "set_child_transient: parent doesn't exist"
5462 " for %lu trans %lu\n", win->id, win->transient);
5463
5464 if (win->hints == NULL) {
5465 fprintf(stderr, "no hints for %lu\n", win->id);
5466 return;
5467 }
5468
5469 r = root_to_region(win->wa.root);
5470 ws = r->ws;
5471 /* parent doen't exist in our window list */
5472 TAILQ_FOREACH(w, &ws->winlist, entry) {
5473 if (wmh)
5474 XFree(wmh);
5475
5476 if ((wmh = XGetWMHints(display, w->id)) == NULL) {
5477 fprintf(stderr, "can't get hints for %lu\n",
5478 w->id);
5479 continue;
5480 }
5481
5482 if (win->hints->window_group != wmh->window_group)
5483 continue;
5484
5485 w->child_trans = win;
5486 win->transient = w->id;
5487 *trans = w->id;
5488 DNPRINTF(SWM_D_MISC, "set_child_transient: asjusting "
5489 "transient to %lu\n", win->transient);
5490 break;
5491 }
5492 }
5493
5494 if (wmh)
5495 XFree(wmh);
5496 }
5497
5498 long
5499 window_get_pid(Window win)
5500 {
5501 Atom actual_type_return;
5502 int actual_format_return = 0;
5503 unsigned long nitems_return = 0;
5504 unsigned long bytes_after_return = 0;
5505 long *pid = NULL;
5506 long ret = 0;
5507 const char *errstr;
5508 unsigned char *prop = NULL;
5509
5510 if (XGetWindowProperty(display, win,
5511 XInternAtom(display, "_NET_WM_PID", False), 0, 1, False,
5512 XA_CARDINAL, &actual_type_return, &actual_format_return,
5513 &nitems_return, &bytes_after_return,
5514 (unsigned char**)(void*)&pid) != Success)
5515 goto tryharder;
5516 if (actual_type_return != XA_CARDINAL)
5517 goto tryharder;
5518 if (pid == NULL)
5519 goto tryharder;
5520
5521 ret = *pid;
5522 XFree(pid);
5523
5524 return (ret);
5525
5526 tryharder:
5527 if (XGetWindowProperty(display, win,
5528 XInternAtom(display, "_SWM_PID", False), 0, SWM_PROPLEN, False,
5529 XA_STRING, &actual_type_return, &actual_format_return,
5530 &nitems_return, &bytes_after_return, &prop) != Success)
5531 return (0);
5532 if (actual_type_return != XA_STRING)
5533 return (0);
5534 if (prop == NULL)
5535 return (0);
5536
5537 ret = strtonum((const char *)prop, 0, UINT_MAX, &errstr);
5538 /* ignore error because strtonum returns 0 anyway */
5539 XFree(prop);
5540
5541 return (ret);
5542 }
5543
5544 struct ws_win *
5545 manage_window(Window id)
5546 {
5547 Window trans = 0;
5548 struct workspace *ws;
5549 struct ws_win *win, *ww;
5550 int format, i, ws_idx, n, border_me = 0;
5551 unsigned long nitems, bytes;
5552 Atom ws_idx_atom = 0, type;
5553 Atom *prot = NULL, *pp;
5554 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
5555 struct swm_region *r;
5556 long mask;
5557 const char *errstr;
5558 XWindowChanges wc;
5559 struct pid_e *p;
5560
5561 if ((win = find_window(id)) != NULL)
5562 return (win); /* already being managed */
5563
5564 /* see if we are on the unmanaged list */
5565 if ((win = find_unmanaged_window(id)) != NULL) {
5566 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
5567 "%lu\n", win->id);
5568 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5569 if (win->transient) {
5570 set_child_transient(win, &trans);
5571 } if (trans && (ww = find_window(trans)))
5572 TAILQ_INSERT_AFTER(&win->ws->winlist, ww, win, entry);
5573 else
5574 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
5575 ewmh_update_actions(win);
5576 return (win);
5577 }
5578
5579 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
5580 err(1, "calloc: failed to allocate memory for new window");
5581
5582 win->id = id;
5583
5584 /* see if we need to override the workspace */
5585 p = find_pid(window_get_pid(id));
5586
5587 /* Get all the window data in one shot */
5588 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
5589 if (ws_idx_atom) {
5590 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
5591 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
5592 }
5593 XGetWindowAttributes(display, id, &win->wa);
5594 XGetWMNormalHints(display, id, &win->sh, &mask);
5595 win->hints = XGetWMHints(display, id);
5596 XGetTransientForHint(display, id, &trans);
5597 if (trans) {
5598 win->transient = trans;
5599 set_child_transient(win, &trans);
5600 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
5601 win->id, win->transient);
5602 }
5603
5604 /* get supported protocols */
5605 if (XGetWMProtocols(display, id, &prot, &n)) {
5606 for (i = 0, pp = prot; i < n; i++, pp++) {
5607 if (*pp == takefocus)
5608 win->take_focus = 1;
5609 if (*pp == adelete)
5610 win->can_delete = 1;
5611 }
5612 if (prot)
5613 XFree(prot);
5614 }
5615
5616 win->iconic = get_iconic(win);
5617
5618 /*
5619 * Figure out where to put the window. If it was previously assigned to
5620 * a workspace (either by spawn() or manually moving), and isn't
5621 * transient, * put it in the same workspace
5622 */
5623 r = root_to_region(win->wa.root);
5624 if (p) {
5625 ws = &r->s->ws[p->ws];
5626 TAILQ_REMOVE(&pidlist, p, entry);
5627 free(p);
5628 p = NULL;
5629 } else if (prop && win->transient == 0) {
5630 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
5631 ws_idx = strtonum((const char *)prop, 0, 9, &errstr);
5632 if (errstr) {
5633 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
5634 errstr, prop);
5635 }
5636 ws = &r->s->ws[ws_idx];
5637 } else {
5638 ws = r->ws;
5639 /* this should launch transients in the same ws as parent */
5640 if (id && trans)
5641 if ((ww = find_window(trans)) != NULL)
5642 if (ws->r) {
5643 ws = ww->ws;
5644 if (ww->ws->r)
5645 r = ww->ws->r;
5646 else
5647 fprintf(stderr,
5648 "fix this bug mcbride\n");
5649 border_me = 1;
5650 }
5651 }
5652
5653 /* set up the window layout */
5654 win->id = id;
5655 win->ws = ws;
5656 win->s = r->s; /* this never changes */
5657 if (trans && (ww = find_window(trans)))
5658 TAILQ_INSERT_AFTER(&ws->winlist, ww, win, entry);
5659 else
5660 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
5661
5662 win->g.w = win->wa.width;
5663 win->g.h = win->wa.height;
5664 win->g.x = win->wa.x;
5665 win->g.y = win->wa.y;
5666 win->g_floatvalid = 0;
5667 win->floatmaxed = 0;
5668 win->ewmh_flags = 0;
5669
5670 /* Set window properties so we can remember this after reincarnation */
5671 if (ws_idx_atom && prop == NULL &&
5672 snprintf((char *)ws_idx_str, SWM_PROPLEN, "%d", ws->idx) <
5673 SWM_PROPLEN) {
5674 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
5675 ws_idx_str);
5676 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
5677 PropModeReplace, ws_idx_str, SWM_PROPLEN);
5678 }
5679 if (prop)
5680 XFree(prop);
5681
5682 ewmh_autoquirk(win);
5683
5684 if (XGetClassHint(display, win->id, &win->ch)) {
5685 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
5686 win->ch.res_class, win->ch.res_name);
5687
5688 /* java is retarded so treat it special */
5689 if (strstr(win->ch.res_name, "sun-awt")) {
5690 win->java = 1;
5691 border_me = 1;
5692 }
5693
5694 for (i = 0; i < quirks_length; i++){
5695 if (!strcmp(win->ch.res_class, quirks[i].class) &&
5696 !strcmp(win->ch.res_name, quirks[i].name)) {
5697 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
5698 win->ch.res_class, win->ch.res_name);
5699 if (quirks[i].quirk & SWM_Q_FLOAT) {
5700 win->floating = 1;
5701 border_me = 1;
5702 }
5703 win->quirks = quirks[i].quirk;
5704 }
5705 }
5706 }
5707
5708 /* alter window position if quirky */
5709 if (win->quirks & SWM_Q_ANYWHERE) {
5710 win->manual = 1; /* don't center the quirky windows */
5711 bzero(&wc, sizeof wc);
5712 mask = 0;
5713 if (bar_enabled && win->g.y < bar_height) {
5714 win->g.y = wc.y = bar_height;
5715 mask |= CWY;
5716 }
5717 if (win->g.w + win->g.x > WIDTH(r)) {
5718 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
5719 mask |= CWX;
5720 }
5721 border_me = 1;
5722 }
5723
5724 /* Reset font sizes (the bruteforce way; no default keybinding). */
5725 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
5726 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5727 fake_keypress(win, XK_KP_Subtract, ShiftMask);
5728 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
5729 fake_keypress(win, XK_KP_Add, ShiftMask);
5730 }
5731
5732 ewmh_get_win_state(win);
5733 ewmh_update_actions(win);
5734 ewmh_update_win_state(win, None, _NET_WM_STATE_REMOVE);
5735
5736 /* border me */
5737 if (border_me) {
5738 bzero(&wc, sizeof wc);
5739 wc.border_width = border_width;
5740 mask = CWBorderWidth;
5741 XConfigureWindow(display, win->id, mask, &wc);
5742 }
5743
5744 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
5745 PropertyChangeMask | StructureNotifyMask);
5746
5747 /* floaters need to be mapped if they are in the current workspace */
5748 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
5749 XMapRaised(display, win->id);
5750
5751 return (win);
5752 }
5753
5754 void
5755 free_window(struct ws_win *win)
5756 {
5757 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
5758
5759 if (win == NULL)
5760 return;
5761
5762 /* needed for restart wm */
5763 set_win_state(win, WithdrawnState);
5764
5765 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
5766
5767 if (win->ch.res_class)
5768 XFree(win->ch.res_class);
5769 if (win->ch.res_name)
5770 XFree(win->ch.res_name);
5771
5772 kill_refs(win);
5773
5774 /* paint memory */
5775 memset(win, 0xff, sizeof *win); /* XXX kill later */
5776
5777 free(win);
5778 }
5779
5780 void
5781 unmanage_window(struct ws_win *win)
5782 {
5783 struct ws_win *parent;
5784
5785 if (win == NULL)
5786 return;
5787
5788 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
5789
5790 if (win->transient) {
5791 parent = find_window(win->transient);
5792 if (parent)
5793 parent->child_trans = NULL;
5794 }
5795
5796 /* focus on root just in case */
5797 XSetInputFocus(display, PointerRoot, PointerRoot, CurrentTime);
5798
5799 focus_prev(win);
5800
5801 TAILQ_REMOVE(&win->ws->winlist, win, entry);
5802 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
5803
5804 kill_refs(win);
5805 }
5806
5807 void
5808 focus_magic(struct ws_win *win)
5809 {
5810 DNPRINTF(SWM_D_FOCUS, "focus_magic: %lu\n", WINID(win));
5811
5812 if (win == NULL)
5813 return;
5814
5815 if (win->child_trans) {
5816 /* win = parent & has a transient so focus on that */
5817 if (win->java) {
5818 focus_win(win->child_trans);
5819 if (win->child_trans->take_focus)
5820 client_msg(win, takefocus);
5821 } else {
5822 /* make sure transient hasn't dissapeared */
5823 if (validate_win(win->child_trans) == 0) {
5824 focus_win(win->child_trans);
5825 if (win->child_trans->take_focus)
5826 client_msg(win->child_trans, takefocus);
5827 } else {
5828 win->child_trans = NULL;
5829 focus_win(win);
5830 if (win->take_focus)
5831 client_msg(win, takefocus);
5832 }
5833 }
5834 } else {
5835 /* regular focus */
5836 focus_win(win);
5837 if (win->take_focus)
5838 client_msg(win, takefocus);
5839 }
5840 }
5841
5842 void
5843 expose(XEvent *e)
5844 {
5845 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
5846 }
5847
5848 void
5849 keypress(XEvent *e)
5850 {
5851 unsigned int i;
5852 KeySym keysym;
5853 XKeyEvent *ev = &e->xkey;
5854
5855 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
5856
5857 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
5858 for (i = 0; i < keys_length; i++)
5859 if (keysym == keys[i].keysym
5860 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
5861 && keyfuncs[keys[i].funcid].func) {
5862 if (keys[i].funcid == kf_spawn_custom)
5863 spawn_custom(
5864 root_to_region(ev->root),
5865 &(keyfuncs[keys[i].funcid].args),
5866 keys[i].spawn_name
5867 );
5868 else
5869 keyfuncs[keys[i].funcid].func(
5870 root_to_region(ev->root),
5871 &(keyfuncs[keys[i].funcid].args)
5872 );
5873 }
5874 }
5875
5876 void
5877 buttonpress(XEvent *e)
5878 {
5879 struct ws_win *win;
5880 int i, action;
5881 XButtonPressedEvent *ev = &e->xbutton;
5882
5883 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
5884
5885 if ((win = find_window(ev->window)) == NULL)
5886 return;
5887
5888 focus_magic(win);
5889 action = client_click;
5890
5891 for (i = 0; i < LENGTH(buttons); i++)
5892 if (action == buttons[i].action && buttons[i].func &&
5893 buttons[i].button == ev->button &&
5894 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
5895 buttons[i].func(win, &buttons[i].args);
5896 }
5897
5898 void
5899 configurerequest(XEvent *e)
5900 {
5901 XConfigureRequestEvent *ev = &e->xconfigurerequest;
5902 struct ws_win *win;
5903 int new = 0;
5904 XWindowChanges wc;
5905
5906 if ((win = find_window(ev->window)) == NULL)
5907 if ((win = find_unmanaged_window(ev->window)) == NULL)
5908 new = 1;
5909
5910 if (new) {
5911 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
5912 ev->window);
5913 bzero(&wc, sizeof wc);
5914 wc.x = ev->x;
5915 wc.y = ev->y;
5916 wc.width = ev->width;
5917 wc.height = ev->height;
5918 wc.border_width = ev->border_width;
5919 wc.sibling = ev->above;
5920 wc.stack_mode = ev->detail;
5921 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
5922 } else {
5923 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
5924 ev->window);
5925 config_win(win, ev);
5926 }
5927 }
5928
5929 void
5930 configurenotify(XEvent *e)
5931 {
5932 struct ws_win *win;
5933 long mask;
5934
5935 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
5936 e->xconfigure.window);
5937
5938 win = find_window(e->xconfigure.window);
5939 if (win) {
5940 XGetWMNormalHints(display, win->id, &win->sh, &mask);
5941 adjust_font(win);
5942 if (font_adjusted)
5943 stack();
5944 if (focus_mode == SWM_FOCUS_DEFAULT)
5945 drain_enter_notify();
5946 }
5947 }
5948
5949 void
5950 destroynotify(XEvent *e)
5951 {
5952 struct ws_win *win;
5953 XDestroyWindowEvent *ev = &e->xdestroywindow;
5954
5955 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
5956
5957 if ((win = find_window(ev->window)) == NULL) {
5958 if ((win = find_unmanaged_window(ev->window)) == NULL)
5959 return;
5960 free_window(win);
5961 return;
5962 }
5963
5964 /* make sure we focus on something */
5965 win->floating = 0;
5966
5967 unmanage_window(win);
5968 stack();
5969 if (focus_mode == SWM_FOCUS_DEFAULT)
5970 drain_enter_notify();
5971 free_window(win);
5972 }
5973
5974 void
5975 enternotify(XEvent *e)
5976 {
5977 XCrossingEvent *ev = &e->xcrossing;
5978 XEvent cne;
5979 struct ws_win *win;
5980 #if 0
5981 struct ws_win *w;
5982 Window focus_return;
5983 int revert_to_return;
5984 #endif
5985 DNPRINTF(SWM_D_FOCUS, "enternotify: window: %lu mode %d detail %d root "
5986 "%lu subwindow %lu same_screen %d focus %d state %d\n",
5987 ev->window, ev->mode, ev->detail, ev->root, ev->subwindow,
5988 ev->same_screen, ev->focus, ev->state);
5989
5990 switch (focus_mode) {
5991 case SWM_FOCUS_DEFAULT:
5992 break;
5993 case SWM_FOCUS_FOLLOW:
5994 break;
5995 case SWM_FOCUS_SYNERGY:
5996 #if 0
5997 /*
5998 * all these checks need to be in this order because the
5999 * XCheckTypedWindowEvent relies on weeding out the previous events
6000 *
6001 * making this code an option would enable a follow mouse for focus
6002 * feature
6003 */
6004
6005 /*
6006 * state is set when we are switching workspaces and focus is set when
6007 * the window or a subwindow already has focus (occurs during restart).
6008 *
6009 * Only honor the focus flag if last_focus_event is not FocusOut,
6010 * this allows scrotwm to continue to control focus when another
6011 * program is also playing with it.
6012 */
6013 if (ev->state || (ev->focus && last_focus_event != FocusOut)) {
6014 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: focus\n");
6015 return;
6016 }
6017
6018 /*
6019 * happens when a window is created or destroyed and the border
6020 * crosses the mouse pointer and when switching ws
6021 *
6022 * we need the subwindow test to see if we came from root in order
6023 * to give focus to floaters
6024 */
6025 if (ev->mode == NotifyNormal && ev->detail == NotifyVirtual &&
6026 ev->subwindow == 0) {
6027 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: NotifyVirtual\n");
6028 return;
6029 }
6030
6031 /* this window already has focus */
6032 if (ev->mode == NotifyNormal && ev->detail == NotifyInferior) {
6033 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win has focus\n");
6034 return;
6035 }
6036
6037 /* this window is being deleted or moved to another ws */
6038 if (XCheckTypedWindowEvent(display, ev->window, ConfigureNotify,
6039 &cne) == True) {
6040 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: configurenotify\n");
6041 XPutBackEvent(display, &cne);
6042 return;
6043 }
6044
6045 if ((win = find_window(ev->window)) == NULL) {
6046 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6047 return;
6048 }
6049
6050 /*
6051 * In fullstack kill all enters unless they come from a different ws
6052 * (i.e. another region) or focus has been grabbed externally.
6053 */
6054 if (win->ws->cur_layout->flags & SWM_L_FOCUSPREV &&
6055 last_focus_event != FocusOut) {
6056 XGetInputFocus(display, &focus_return, &revert_to_return);
6057 if ((w = find_window(focus_return)) == NULL ||
6058 w->ws == win->ws) {
6059 DNPRINTF(SWM_D_EVENT, "ignoring event: fullstack\n");
6060 return;
6061 }
6062 }
6063 #endif
6064 break;
6065 }
6066
6067 if ((win = find_window(ev->window)) == NULL) {
6068 DNPRINTF(SWM_D_EVENT, "ignoring enternotify: win == NULL\n");
6069 return;
6070 }
6071
6072 /*
6073 * if we have more enternotifies let them handle it in due time
6074 */
6075 if (XCheckTypedEvent(display, EnterNotify, &cne) == True) {
6076 DNPRINTF(SWM_D_EVENT,
6077 "ignoring enternotify: got more enternotify\n");
6078 XPutBackEvent(display, &cne);
6079 return;
6080 }
6081
6082 focus_magic(win);
6083 }
6084
6085 /* lets us use one switch statement for arbitrary mode/detail combinations */
6086 #define MERGE_MEMBERS(a,b) (((a & 0xffff) << 16) | (b & 0xffff))
6087
6088 void
6089 focusevent(XEvent *e)
6090 {
6091 #if 0
6092 struct ws_win *win;
6093 u_int32_t mode_detail;
6094 XFocusChangeEvent *ev = &e->xfocus;
6095
6096 DNPRINTF(SWM_D_EVENT, "focusevent: %s window: %lu mode %d detail %d\n",
6097 ev->type == FocusIn ? "entering" : "leaving",
6098 ev->window, ev->mode, ev->detail);
6099
6100 if (last_focus_event == ev->type) {
6101 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent: bad ordering\n");
6102 return;
6103 }
6104
6105 last_focus_event = ev->type;
6106 mode_detail = MERGE_MEMBERS(ev->mode, ev->detail);
6107
6108 switch (mode_detail) {
6109 /* synergy client focus operations */
6110 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinear):
6111 case MERGE_MEMBERS(NotifyNormal, NotifyNonlinearVirtual):
6112
6113 /* synergy server focus operations */
6114 case MERGE_MEMBERS(NotifyWhileGrabbed, NotifyNonlinear):
6115
6116 /* Entering applications like rdesktop that mangle the pointer */
6117 case MERGE_MEMBERS(NotifyNormal, NotifyPointer):
6118
6119 if ((win = find_window(e->xfocus.window)) != NULL && win->ws->r)
6120 XSetWindowBorder(display, win->id,
6121 win->ws->r->s->c[ev->type == FocusIn ?
6122 SWM_S_COLOR_FOCUS : SWM_S_COLOR_UNFOCUS].color);
6123 break;
6124 default:
6125 fprintf(stderr, "ignoring focusevent\n");
6126 DNPRINTF(SWM_D_FOCUS, "ignoring focusevent\n");
6127 break;
6128 }
6129 #endif
6130 }
6131
6132 void
6133 mapnotify(XEvent *e)
6134 {
6135 struct ws_win *win;
6136 XMapEvent *ev = &e->xmap;
6137
6138 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
6139
6140 win = manage_window(ev->window);
6141 if (win)
6142 set_win_state(win, NormalState);
6143 }
6144
6145 void
6146 mappingnotify(XEvent *e)
6147 {
6148 XMappingEvent *ev = &e->xmapping;
6149
6150 XRefreshKeyboardMapping(ev);
6151 if (ev->request == MappingKeyboard)
6152 grabkeys();
6153 }
6154
6155 void
6156 maprequest(XEvent *e)
6157 {
6158 struct ws_win *win;
6159 struct swm_region *r;
6160 XWindowAttributes wa;
6161 XMapRequestEvent *ev = &e->xmaprequest;
6162
6163 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
6164 e->xmaprequest.window);
6165
6166 if (!XGetWindowAttributes(display, ev->window, &wa))
6167 return;
6168 if (wa.override_redirect)
6169 return;
6170
6171 win = manage_window(e->xmaprequest.window);
6172 if (win == NULL)
6173 return; /* can't happen */
6174
6175 stack();
6176
6177 /* make new win focused */
6178 r = root_to_region(win->wa.root);
6179 if (win->ws == r->ws)
6180 focus_magic(win);
6181 }
6182
6183 void
6184 propertynotify(XEvent *e)
6185 {
6186 struct ws_win *win;
6187 XPropertyEvent *ev = &e->xproperty;
6188
6189 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
6190 ev->window);
6191
6192 win = find_window(ev->window);
6193 if (win == NULL)
6194 return;
6195
6196 if (ev->state == PropertyDelete && ev->atom == a_swm_iconic) {
6197 update_iconic(win, 0);
6198 XMapRaised(display, win->id);
6199 stack();
6200 focus_win(win);
6201 return;
6202 }
6203
6204 switch (ev->atom) {
6205 #if 0
6206 case XA_WM_NORMAL_HINTS:
6207 long mask;
6208 XGetWMNormalHints(display, win->id, &win->sh, &mask);
6209 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
6210 if (win->sh.flags & PMinSize) {
6211 win->g.w = win->sh.min_width;
6212 win->g.h = win->sh.min_height;
6213 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
6214 }
6215 XMoveResizeWindow(display, win->id,
6216 win->g.x, win->g.y, win->g.w, win->g.h);
6217 #endif
6218 case XA_WM_CLASS:
6219 if (title_name_enabled || title_class_enabled)
6220 bar_update();
6221 break;
6222 case XA_WM_NAME:
6223 if (window_name_enabled)
6224 bar_update();
6225 break;
6226 default:
6227 break;
6228 }
6229 }
6230
6231 void
6232 unmapnotify(XEvent *e)
6233 {
6234 struct ws_win *win;
6235
6236 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
6237
6238 /* determine if we need to help unmanage this window */
6239 win = find_window(e->xunmap.window);
6240 if (win == NULL)
6241 return;
6242
6243 if (getstate(e->xunmap.window) == NormalState) {
6244 unmanage_window(win);
6245 stack();
6246
6247 /* giant hack for apps that don't destroy transient windows */
6248 /* eat a bunch of events to prevent remanaging the window */
6249 XEvent cne;
6250 while (XCheckWindowEvent(display, e->xunmap.window,
6251 EnterWindowMask, &cne))
6252 ;
6253 while (XCheckWindowEvent(display, e->xunmap.window,
6254 StructureNotifyMask, &cne))
6255 ;
6256 while (XCheckWindowEvent(display, e->xunmap.window,
6257 SubstructureNotifyMask, &cne))
6258 ;
6259 /* resend unmap because we ated it */
6260 XUnmapWindow(display, e->xunmap.window);
6261 }
6262
6263 if (focus_mode == SWM_FOCUS_DEFAULT)
6264 drain_enter_notify();
6265 }
6266
6267 void
6268 visibilitynotify(XEvent *e)
6269 {
6270 int i;
6271 struct swm_region *r;
6272
6273 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
6274 e->xvisibility.window);
6275 if (e->xvisibility.state == VisibilityUnobscured)
6276 for (i = 0; i < ScreenCount(display); i++)
6277 TAILQ_FOREACH(r, &screens[i].rl, entry)
6278 if (e->xvisibility.window == r->bar_window)
6279 bar_update();
6280 }
6281
6282 void
6283 clientmessage(XEvent *e)
6284 {
6285 XClientMessageEvent *ev;
6286 struct ws_win *win;
6287
6288 ev = &e->xclient;
6289
6290 win = find_window(ev->window);
6291 if (win == NULL)
6292 return;
6293
6294 DNPRINTF(SWM_D_EVENT, "clientmessage: window: 0x%lx type: %ld \n",
6295 ev->window, ev->message_type);
6296
6297 if (ev->message_type == ewmh[_NET_ACTIVE_WINDOW].atom) {
6298 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_ACTIVE_WINDOW \n");
6299 focus_win(win);
6300 }
6301 if (ev->message_type == ewmh[_NET_CLOSE_WINDOW].atom) {
6302 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_CLOSE_WINDOW \n");
6303 if (win->can_delete)
6304 client_msg(win, adelete);
6305 else
6306 XKillClient(display, win->id);
6307 }
6308 if (ev->message_type == ewmh[_NET_MOVERESIZE_WINDOW].atom) {
6309 DNPRINTF(SWM_D_EVENT,
6310 "clientmessage: _NET_MOVERESIZE_WINDOW \n");
6311 if (win->floating) {
6312 if (ev->data.l[0] & (1<<8)) /* x */
6313 win->g.x = ev->data.l[1];
6314 if (ev->data.l[0] & (1<<9)) /* y */
6315 win->g.y = ev->data.l[2];
6316 if (ev->data.l[0] & (1<<10)) /* width */
6317 win->g.w = ev->data.l[3];
6318 if (ev->data.l[0] & (1<<11)) /* height */
6319 win->g.h = ev->data.l[4];
6320 }
6321 else {
6322 /* TODO: Change stack sizes */
6323 }
6324 config_win(win, NULL);
6325 }
6326 if (ev->message_type == ewmh[_NET_WM_STATE].atom) {
6327 DNPRINTF(SWM_D_EVENT, "clientmessage: _NET_WM_STATE \n");
6328 ewmh_update_win_state(win, ev->data.l[1], ev->data.l[0]);
6329 if (ev->data.l[2])
6330 ewmh_update_win_state(win, ev->data.l[2],
6331 ev->data.l[0]);
6332
6333 stack();
6334 }
6335 }
6336
6337 int
6338 xerror_start(Display *d, XErrorEvent *ee)
6339 {
6340 other_wm = 1;
6341 return (-1);
6342 }
6343
6344 int
6345 xerror(Display *d, XErrorEvent *ee)
6346 {
6347 /* fprintf(stderr, "error: %p %p\n", display, ee); */
6348 return (-1);
6349 }
6350
6351 int
6352 active_wm(void)
6353 {
6354 other_wm = 0;
6355 xerrorxlib = XSetErrorHandler(xerror_start);
6356
6357 /* this causes an error if some other window manager is running */
6358 XSelectInput(display, DefaultRootWindow(display),
6359 SubstructureRedirectMask);
6360 XSync(display, False);
6361 if (other_wm)
6362 return (1);
6363
6364 XSetErrorHandler(xerror);
6365 XSync(display, False);
6366 return (0);
6367 }
6368
6369 void
6370 new_region(struct swm_screen *s, int x, int y, int w, int h)
6371 {
6372 struct swm_region *r, *n;
6373 struct workspace *ws = NULL;
6374 int i;
6375
6376 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
6377 s->idx, w, h, x, y);
6378
6379 /* remove any conflicting regions */
6380 n = TAILQ_FIRST(&s->rl);
6381 while (n) {
6382 r = n;
6383 n = TAILQ_NEXT(r, entry);
6384 if (X(r) < (x + w) &&
6385 (X(r) + WIDTH(r)) > x &&
6386 Y(r) < (y + h) &&
6387 (Y(r) + HEIGHT(r)) > y) {
6388 if (r->ws->r != NULL)
6389 r->ws->old_r = r->ws->r;
6390 r->ws->r = NULL;
6391 XDestroyWindow(display, r->bar_window);
6392 TAILQ_REMOVE(&s->rl, r, entry);
6393 TAILQ_INSERT_TAIL(&s->orl, r, entry);
6394 }
6395 }
6396
6397 /* search old regions for one to reuse */
6398
6399 /* size + location match */
6400 TAILQ_FOREACH(r, &s->orl, entry)
6401 if (X(r) == x && Y(r) == y &&
6402 HEIGHT(r) == h && WIDTH(r) == w)
6403 break;
6404
6405 /* size match */
6406 TAILQ_FOREACH(r, &s->orl, entry)
6407 if (HEIGHT(r) == h && WIDTH(r) == w)
6408 break;
6409
6410 if (r != NULL) {
6411 TAILQ_REMOVE(&s->orl, r, entry);
6412 /* try to use old region's workspace */
6413 if (r->ws->r == NULL)
6414 ws = r->ws;
6415 } else
6416 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
6417 err(1, "calloc: failed to allocate memory for screen");
6418
6419 /* if we don't have a workspace already, find one */
6420 if (ws == NULL) {
6421 for (i = 0; i < SWM_WS_MAX; i++)
6422 if (s->ws[i].r == NULL) {
6423 ws = &s->ws[i];
6424 break;
6425 }
6426 }
6427
6428 if (ws == NULL)
6429 errx(1, "no free workspaces");
6430
6431 X(r) = x;
6432 Y(r) = y;
6433 WIDTH(r) = w;
6434 HEIGHT(r) = h;
6435 r->s = s;
6436 r->ws = ws;
6437 r->ws_prior = NULL;
6438 ws->r = r;
6439 outputs++;
6440 TAILQ_INSERT_TAIL(&s->rl, r, entry);
6441 }
6442
6443 void
6444 scan_xrandr(int i)
6445 {
6446 #ifdef SWM_XRR_HAS_CRTC
6447 XRRCrtcInfo *ci;
6448 XRRScreenResources *sr;
6449 int c;
6450 int ncrtc = 0;
6451 #endif /* SWM_XRR_HAS_CRTC */
6452 struct swm_region *r;
6453
6454
6455 if (i >= ScreenCount(display))
6456 errx(1, "invalid screen");
6457
6458 /* remove any old regions */
6459 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
6460 r->ws->old_r = r->ws->r = NULL;
6461 XDestroyWindow(display, r->bar_window);
6462 TAILQ_REMOVE(&screens[i].rl, r, entry);
6463 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
6464 }
6465 outputs = 0;
6466
6467 /* map virtual screens onto physical screens */
6468 #ifdef SWM_XRR_HAS_CRTC
6469 if (xrandr_support) {
6470 sr = XRRGetScreenResources(display, screens[i].root);
6471 if (sr == NULL)
6472 new_region(&screens[i], 0, 0,
6473 DisplayWidth(display, i),
6474 DisplayHeight(display, i));
6475 else
6476 ncrtc = sr->ncrtc;
6477
6478 for (c = 0, ci = NULL; c < ncrtc; c++) {
6479 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
6480 if (ci->noutput == 0)
6481 continue;
6482
6483 if (ci != NULL && ci->mode == None)
6484 new_region(&screens[i], 0, 0,
6485 DisplayWidth(display, i),
6486 DisplayHeight(display, i));
6487 else
6488 new_region(&screens[i],
6489 ci->x, ci->y, ci->width, ci->height);
6490 }
6491 if (ci)
6492 XRRFreeCrtcInfo(ci);
6493 XRRFreeScreenResources(sr);
6494 } else
6495 #endif /* SWM_XRR_HAS_CRTC */
6496 {
6497 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
6498 DisplayHeight(display, i));
6499 }
6500 }
6501
6502 void
6503 screenchange(XEvent *e) {
6504 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
6505 struct swm_region *r;
6506 int i;
6507
6508 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
6509
6510 if (!XRRUpdateConfiguration(e))
6511 return;
6512
6513 /* silly event doesn't include the screen index */
6514 for (i = 0; i < ScreenCount(display); i++)
6515 if (screens[i].root == xe->root)
6516 break;
6517 if (i >= ScreenCount(display))
6518 errx(1, "screenchange: screen not found");
6519
6520 /* brute force for now, just re-enumerate the regions */
6521 scan_xrandr(i);
6522
6523 /* add bars to all regions */
6524 for (i = 0; i < ScreenCount(display); i++)
6525 TAILQ_FOREACH(r, &screens[i].rl, entry)
6526 bar_setup(r);
6527 stack();
6528 if (focus_mode == SWM_FOCUS_DEFAULT)
6529 drain_enter_notify();
6530 }
6531
6532 void
6533 grab_windows(void)
6534 {
6535 Window d1, d2, *wins = NULL;
6536 XWindowAttributes wa;
6537 unsigned int no;
6538 int i, j;
6539 long state, manage;
6540
6541 for (i = 0; i < ScreenCount(display); i++) {
6542 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
6543 continue;
6544
6545 /* attach windows to a region */
6546 /* normal windows */
6547 for (j = 0; j < no; j++) {
6548 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6549 wa.override_redirect ||
6550 XGetTransientForHint(display, wins[j], &d1))
6551 continue;
6552
6553 state = getstate(wins[j]);
6554 manage = state == IconicState;
6555 if (wa.map_state == IsViewable || manage)
6556 manage_window(wins[j]);
6557 }
6558 /* transient windows */
6559 for (j = 0; j < no; j++) {
6560 if (!XGetWindowAttributes(display, wins[j], &wa) ||
6561 wa.override_redirect)
6562 continue;
6563
6564 state = getstate(wins[j]);
6565 manage = state == IconicState;
6566 if (XGetTransientForHint(display, wins[j], &d1) &&
6567 manage)
6568 manage_window(wins[j]);
6569 }
6570 if (wins) {
6571 XFree(wins);
6572 wins = NULL;
6573 }
6574 }
6575 }
6576
6577 void
6578 setup_screens(void)
6579 {
6580 int i, j, k;
6581 int errorbase, major, minor;
6582 struct workspace *ws;
6583
6584 if ((screens = calloc(ScreenCount(display),
6585 sizeof(struct swm_screen))) == NULL)
6586 err(1, "calloc: screens");
6587
6588 /* initial Xrandr setup */
6589 xrandr_support = XRRQueryExtension(display,
6590 &xrandr_eventbase, &errorbase);
6591 if (xrandr_support)
6592 if (XRRQueryVersion(display, &major, &minor) && major < 1)
6593 xrandr_support = 0;
6594
6595 /* map physical screens */
6596 for (i = 0; i < ScreenCount(display); i++) {
6597 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
6598 screens[i].idx = i;
6599 TAILQ_INIT(&screens[i].rl);
6600 TAILQ_INIT(&screens[i].orl);
6601 screens[i].root = RootWindow(display, i);
6602
6603 /* set default colors */
6604 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
6605 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
6606 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
6607 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
6608 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
6609
6610 /* set default cursor */
6611 XDefineCursor(display, screens[i].root,
6612 XCreateFontCursor(display, XC_left_ptr));
6613
6614 /* init all workspaces */
6615 /* XXX these should be dynamically allocated too */
6616 for (j = 0; j < SWM_WS_MAX; j++) {
6617 ws = &screens[i].ws[j];
6618 ws->idx = j;
6619 ws->name = NULL;
6620 ws->focus = NULL;
6621 ws->r = NULL;
6622 ws->old_r = NULL;
6623 TAILQ_INIT(&ws->winlist);
6624 TAILQ_INIT(&ws->unmanagedlist);
6625
6626 for (k = 0; layouts[k].l_stack != NULL; k++)
6627 if (layouts[k].l_config != NULL)
6628 layouts[k].l_config(ws,
6629 SWM_ARG_ID_STACKINIT);
6630 ws->cur_layout = &layouts[0];
6631 ws->cur_layout->l_string(ws);
6632 }
6633
6634 scan_xrandr(i);
6635
6636 if (xrandr_support)
6637 XRRSelectInput(display, screens[i].root,
6638 RRScreenChangeNotifyMask);
6639 }
6640 }
6641
6642 void
6643 setup_globals(void)
6644 {
6645 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
6646 == NULL)
6647 err(1, "setup_globals: strdup");
6648 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
6649 == NULL)
6650 err(1, "setup_globals: strdup");
6651 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
6652 == NULL)
6653 err(1, "setup_globals: strdup");
6654 if ((spawn_term[0] = strdup("xterm")) == NULL)
6655 err(1, "setup_globals: strdup");
6656 if ((clock_format = strdup("%a %b %d %R %Z %Y")) == NULL)
6657 err(1, "strdup");
6658 }
6659
6660 void
6661 workaround(void)
6662 {
6663 int i;
6664 Atom netwmcheck, netwmname, utf8_string;
6665 Window root, win;
6666
6667 /* work around sun jdk bugs, code from wmname */
6668 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
6669 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
6670 utf8_string = XInternAtom(display, "UTF8_STRING", False);
6671 for (i = 0; i < ScreenCount(display); i++) {
6672 root = screens[i].root;
6673 win = XCreateSimpleWindow(display,root, 0, 0, 1, 1, 0,
6674 screens[i].c[SWM_S_COLOR_UNFOCUS].color,
6675 screens[i].c[SWM_S_COLOR_UNFOCUS].color);
6676
6677 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
6678 PropModeReplace, (unsigned char *)&win,1);
6679 XChangeProperty(display, win, netwmcheck, XA_WINDOW, 32,
6680 PropModeReplace, (unsigned char *)&win,1);
6681 XChangeProperty(display, win, netwmname, utf8_string, 8,
6682 PropModeReplace, (unsigned char*)"LG3D", strlen("LG3D"));
6683 }
6684 }
6685
6686 int
6687 main(int argc, char *argv[])
6688 {
6689 struct swm_region *r, *rr;
6690 struct ws_win *winfocus = NULL;
6691 struct timeval tv;
6692 union arg a;
6693 char conf[PATH_MAX], *cfile = NULL;
6694 struct stat sb;
6695 XEvent e;
6696 int xfd, i;
6697 fd_set rd;
6698 struct sigaction sact;
6699
6700 start_argv = argv;
6701 fprintf(stderr, "Welcome to scrotwm V%s Build: %s\n",
6702 SCROTWM_VERSION, buildstr);
6703 if (!setlocale(LC_CTYPE, "") || !setlocale(LC_TIME, "") ||
6704 !XSupportsLocale())
6705 warnx("no locale support");
6706
6707 if (!(display = XOpenDisplay(0)))
6708 errx(1, "can not open display");
6709
6710 if (active_wm())
6711 errx(1, "other wm running");
6712
6713 /* handle some signals */
6714 bzero(&sact, sizeof(sact));
6715 sigemptyset(&sact.sa_mask);
6716 sact.sa_flags = 0;
6717 sact.sa_handler = sighdlr;
6718 sigaction(SIGINT, &sact, NULL);
6719 sigaction(SIGQUIT, &sact, NULL);
6720 sigaction(SIGTERM, &sact, NULL);
6721 sigaction(SIGHUP, &sact, NULL);
6722
6723 sact.sa_handler = sighdlr;
6724 sact.sa_flags = SA_NOCLDSTOP;
6725 sigaction(SIGCHLD, &sact, NULL);
6726
6727 astate = XInternAtom(display, "WM_STATE", False);
6728 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
6729 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
6730 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
6731 a_wmname = XInternAtom(display, "WM_NAME", False);
6732 a_utf8_string = XInternAtom(display, "UTF8_STRING", False);
6733 a_string = XInternAtom(display, "STRING", False);
6734 a_swm_iconic = XInternAtom(display, "_SWM_ICONIC", False);
6735
6736 /* look for local and global conf file */
6737 pwd = getpwuid(getuid());
6738 if (pwd == NULL)
6739 errx(1, "invalid user %d", getuid());
6740
6741 setup_screens();
6742 setup_globals();
6743 setup_keys();
6744 setup_quirks();
6745 setup_spawn();
6746
6747 /* load config */
6748 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
6749 if (stat(conf, &sb) != -1) {
6750 if (S_ISREG(sb.st_mode))
6751 cfile = conf;
6752 } else {
6753 /* try global conf file */
6754 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
6755 if (!stat(conf, &sb))
6756 if (S_ISREG(sb.st_mode))
6757 cfile = conf;
6758 }
6759 if (cfile)
6760 conf_load(cfile, SWM_CONF_DEFAULT);
6761
6762 setup_ewmh();
6763 /* set some values to work around bad programs */
6764 workaround();
6765 /* grab existing windows (before we build the bars) */
6766 grab_windows();
6767
6768 if (getenv("SWM_STARTED") == NULL)
6769 setenv("SWM_STARTED", "YES", 1);
6770
6771 /* setup all bars */
6772 for (i = 0; i < ScreenCount(display); i++)
6773 TAILQ_FOREACH(r, &screens[i].rl, entry) {
6774 if (winfocus == NULL)
6775 winfocus = TAILQ_FIRST(&r->ws->winlist);
6776 bar_setup(r);
6777 }
6778
6779 unfocus_all();
6780
6781 grabkeys();
6782 stack();
6783 if (focus_mode == SWM_FOCUS_DEFAULT)
6784 drain_enter_notify();
6785
6786 xfd = ConnectionNumber(display);
6787 while (running) {
6788 while (XPending(display)) {
6789 XNextEvent(display, &e);
6790 if (running == 0)
6791 goto done;
6792 if (e.type < LASTEvent) {
6793 dumpevent(&e);
6794 if (handler[e.type])
6795 handler[e.type](&e);
6796 else
6797 DNPRINTF(SWM_D_EVENT,
6798 "win: %lu unknown event: %d\n",
6799 e.xany.window, e.type);
6800 } else {
6801 switch (e.type - xrandr_eventbase) {
6802 case RRScreenChangeNotify:
6803 screenchange(&e);
6804 break;
6805 default:
6806 DNPRINTF(SWM_D_EVENT,
6807 "win: %lu unknown xrandr event: "
6808 "%d\n", e.xany.window, e.type);
6809 break;
6810 }
6811 }
6812 }
6813
6814 /* if we are being restarted go focus on first window */
6815 if (winfocus) {
6816 rr = winfocus->ws->r;
6817 if (rr == NULL) {
6818 /* not a visible window */
6819 winfocus = NULL;
6820 continue;
6821 }
6822 /* move pointer to first screen if multi screen */
6823 if (ScreenCount(display) > 1 || outputs > 1)
6824 XWarpPointer(display, None, rr->s[0].root,
6825 0, 0, 0, 0, rr->g.x,
6826 rr->g.y + (bar_enabled ? bar_height : 0));
6827
6828 a.id = SWM_ARG_ID_FOCUSCUR;
6829 focus(rr, &a);
6830 winfocus = NULL;
6831 continue;
6832 }
6833
6834 FD_ZERO(&rd);
6835 FD_SET(xfd, &rd);
6836 tv.tv_sec = 1;
6837 tv.tv_usec = 0;
6838 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
6839 if (errno != EINTR)
6840 DNPRINTF(SWM_D_MISC, "select failed");
6841 if (restart_wm == 1)
6842 restart(NULL, NULL);
6843 if (search_resp == 1)
6844 search_do_resp();
6845 if (running == 0)
6846 goto done;
6847 if (bar_alarm) {
6848 bar_alarm = 0;
6849 bar_update();
6850 }
6851 }
6852 done:
6853 teardown_ewmh();
6854 bar_extra_stop();
6855 XFreeGC(display, bar_gc);
6856 XCloseDisplay(display);
6857
6858 return (0);
6859 }