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