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