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