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