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