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