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