3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag = "$scrotwm$";
55 #define SWM_VERSION "0.9.17"
71 #include <sys/types.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
88 # error XRandR versions less than 1.0 are not supported
93 #define SWM_XRR_HAS_CRTC
97 /* #define SWM_DEBUG */
99 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
100 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
101 #define SWM_D_MISC 0x0001
102 #define SWM_D_EVENT 0x0002
103 #define SWM_D_WS 0x0004
104 #define SWM_D_FOCUS 0x0008
105 #define SWM_D_MOVE 0x0010
106 #define SWM_D_STACK 0x0020
107 #define SWM_D_MOUSE 0x0040
108 #define SWM_D_PROP 0x0080
109 #define SWM_D_CLASS 0x0100
110 #define SWM_D_KEY 0x0200
111 #define SWM_D_QUIRK 0x0400
112 #define SWM_D_SPAWN 0x0800
113 #define SWM_D_EVENTQ 0x1000
114 #define SWM_D_CONF 0x2000
116 u_int32_t swm_debug = 0
133 #define DPRINTF(x...)
134 #define DNPRINTF(n,x...)
137 #define LENGTH(x) (sizeof x / sizeof x[0])
138 #define MODKEY Mod1Mask
139 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
140 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
141 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
142 #define SWM_PROPLEN (16)
143 #define SWM_FUNCNAME_LEN (32)
144 #define SWM_KEYS_LEN (255)
145 #define SWM_QUIRK_LEN (64)
146 #define X(r) (r)->g.x
147 #define Y(r) (r)->g.y
148 #define WIDTH(r) (r)->g.w
149 #define HEIGHT(r) (r)->g.h
150 #define SWM_MAX_FONT_STEPS (3)
153 #define SWM_LIB "/usr/local/lib/libswmhack.so"
161 volatile sig_atomic_t running = 1;
163 int (*xerrorxlib)(Display *, XErrorEvent *);
167 int xrandr_eventbase;
168 unsigned int numlockmask = 0;
172 int cycle_visible = 0;
174 int font_adjusted = 0;
175 unsigned int mod_key = MODKEY;
178 double dialog_ratio = .6;
180 #define SWM_BAR_MAX (256)
181 char *bar_argv[] = { NULL, NULL };
183 char bar_ext[SWM_BAR_MAX];
184 char bar_vertext[SWM_BAR_MAX];
186 sig_atomic_t bar_alarm = 0;
190 int bar_extra_running = 0;
193 int stack_enabled = 1;
194 int clock_enabled = 1;
195 int title_name_enabled = 0;
196 int title_class_enabled = 0;
202 char *bar_fonts[] = { NULL, NULL, NULL, NULL };/* XXX Make fully dynamic */
203 char *spawn_term[] = { NULL, NULL }; /* XXX Make fully dynamic */
205 #define SWM_MENU_FN (2)
206 #define SWM_MENU_NB (4)
207 #define SWM_MENU_NF (6)
208 #define SWM_MENU_SB (8)
209 #define SWM_MENU_SF (10)
211 /* layout manager data */
212 struct swm_geometry {
222 /* virtual "screens" */
224 TAILQ_ENTRY(swm_region) entry;
225 struct swm_geometry g;
226 struct workspace *ws; /* current workspace on this region */
227 struct swm_screen *s; /* screen idx */
230 TAILQ_HEAD(swm_region_list, swm_region);
233 TAILQ_ENTRY(ws_win) entry;
236 struct ws_win *child_trans; /* transient child window */
237 struct swm_geometry g;
240 int font_size_boundary[SWM_MAX_FONT_STEPS];
246 unsigned long quirks;
247 struct workspace *ws; /* always valid */
248 struct swm_screen *s; /* always valid, never changes */
249 XWindowAttributes wa;
253 TAILQ_HEAD(ws_win_list, ws_win);
255 /* user/key callable function IDs */
313 /* layout handlers */
315 void vertical_config(struct workspace *, int);
316 void vertical_stack(struct workspace *, struct swm_geometry *);
317 void horizontal_config(struct workspace *, int);
318 void horizontal_stack(struct workspace *, struct swm_geometry *);
319 void max_stack(struct workspace *, struct swm_geometry *);
321 struct ws_win *find_window(Window);
323 void grabbuttons(struct ws_win *, int);
324 void new_region(struct swm_screen *, int, int, int, int);
325 void unmanage_window(struct ws_win *);
326 long getstate(Window);
329 void (*l_stack)(struct workspace *, struct swm_geometry *);
330 void (*l_config)(struct workspace *, int);
332 #define SWM_L_MAPONFOCUS (1<<0)
335 /* stack, configure */
336 { vertical_stack, vertical_config, 0, "[|]" },
337 { horizontal_stack, horizontal_config, 0, "[-]" },
339 SWM_L_MAPONFOCUS, "[ ]"},
340 { NULL, NULL, 0, NULL },
343 #define SWM_H_SLICE (32)
344 #define SWM_V_SLICE (32)
346 /* define work spaces */
348 int idx; /* workspace index */
349 struct layout *cur_layout; /* current layout handlers */
350 struct ws_win *focus; /* may be NULL */
351 struct ws_win *focus_prev; /* may be NULL */
352 struct swm_region *r; /* may be NULL */
353 struct swm_region *old_r; /* may be NULL */
354 struct ws_win_list winlist; /* list of windows in ws */
355 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
359 int horizontal_msize;
361 int horizontal_stacks;
368 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
369 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
371 /* physical screen mapping */
372 #define SWM_WS_MAX (10)
374 int idx; /* screen index */
375 struct swm_region_list rl; /* list of regions on this screen */
376 struct swm_region_list orl; /* list of old regions */
378 struct workspace ws[SWM_WS_MAX];
384 } c[SWM_S_COLOR_MAX];
386 struct swm_screen *screens;
389 /* args to functions */
392 #define SWM_ARG_ID_FOCUSNEXT (0)
393 #define SWM_ARG_ID_FOCUSPREV (1)
394 #define SWM_ARG_ID_FOCUSMAIN (2)
395 #define SWM_ARG_ID_FOCUSCUR (4)
396 #define SWM_ARG_ID_SWAPNEXT (10)
397 #define SWM_ARG_ID_SWAPPREV (11)
398 #define SWM_ARG_ID_SWAPMAIN (12)
399 #define SWM_ARG_ID_MASTERSHRINK (20)
400 #define SWM_ARG_ID_MASTERGROW (21)
401 #define SWM_ARG_ID_MASTERADD (22)
402 #define SWM_ARG_ID_MASTERDEL (23)
403 #define SWM_ARG_ID_STACKRESET (30)
404 #define SWM_ARG_ID_STACKINIT (31)
405 #define SWM_ARG_ID_CYCLEWS_UP (40)
406 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
407 #define SWM_ARG_ID_CYCLESC_UP (42)
408 #define SWM_ARG_ID_CYCLESC_DOWN (43)
409 #define SWM_ARG_ID_STACKINC (50)
410 #define SWM_ARG_ID_STACKDEC (51)
411 #define SWM_ARG_ID_SS_ALL (60)
412 #define SWM_ARG_ID_SS_WINDOW (61)
413 #define SWM_ARG_ID_DONTCENTER (70)
414 #define SWM_ARG_ID_CENTER (71)
415 #define SWM_ARG_ID_KILLWINDOW (80)
416 #define SWM_ARG_ID_DELETEWINDOW (81)
420 void focus(struct swm_region *, union arg *);
421 void focus_magic(struct ws_win *);
428 #define SWM_Q_FLOAT (1<<0) /* float this window */
429 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
430 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
431 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
432 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
434 int quirks_size = 0, quirks_length = 0;
435 struct quirk *quirks = NULL;
452 name = "ButtonPress";
455 name = "ButtonRelease";
458 name = "MotionNotify";
461 name = "EnterNotify";
464 name = "LeaveNotify";
473 name = "KeymapNotify";
479 name = "GraphicsExpose";
484 case VisibilityNotify:
485 name = "VisibilityNotify";
488 name = "CreateNotify";
491 name = "DestroyNotify";
494 name = "UnmapNotify";
503 name = "ReparentNotify";
505 case ConfigureNotify:
506 name = "ConfigureNotify";
508 case ConfigureRequest:
509 name = "ConfigureRequest";
512 name = "GravityNotify";
515 name = "ResizeRequest";
517 case CirculateNotify:
518 name = "CirculateNotify";
520 case CirculateRequest:
521 name = "CirculateRequest";
524 name = "PropertyNotify";
527 name = "SelectionClear";
529 case SelectionRequest:
530 name = "SelectionRequest";
532 case SelectionNotify:
533 name = "SelectionNotify";
536 name = "ColormapNotify";
539 name = "ClientMessage";
542 name = "MappingNotify";
547 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
549 e->xany.window, name, e->type, QLength(display));
551 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
553 e->xany.window, e->type, QLength(display));
557 dumpwins(struct swm_region *r, union arg *args)
561 XWindowAttributes wa;
564 fprintf(stderr, "invalid workspace\n");
568 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
570 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
571 state = getstate(win->id);
572 if (!XGetWindowAttributes(display, win->id, &wa))
573 fprintf(stderr, "window: %lu failed "
574 "XGetWindowAttributes\n", win->id);
575 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
576 win->id, wa.map_state, state);
579 fprintf(stderr, "===== unmanaged window list =====\n");
580 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
581 state = getstate(win->id);
582 if (!XGetWindowAttributes(display, win->id, &wa))
583 fprintf(stderr, "window: %lu failed "
584 "XGetWindowAttributes\n", win->id);
585 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
586 win->id, wa.map_state, state);
589 fprintf(stderr, "=================================\n");
594 dumpwins(struct swm_region *r, union arg *args)
597 #endif /* SWM_DEBUG */
599 void expose(XEvent *);
600 void keypress(XEvent *);
601 void buttonpress(XEvent *);
602 void configurerequest(XEvent *);
603 void configurenotify(XEvent *);
604 void destroynotify(XEvent *);
605 void enternotify(XEvent *);
606 void focusin(XEvent *);
607 void focusout(XEvent *);
608 void mapnotify(XEvent *);
609 void mappingnotify(XEvent *);
610 void maprequest(XEvent *);
611 void propertynotify(XEvent *);
612 void unmapnotify(XEvent *);
613 void visibilitynotify(XEvent *);
615 void (*handler[LASTEvent])(XEvent *) = {
617 [KeyPress] = keypress,
618 [ButtonPress] = buttonpress,
619 [ConfigureRequest] = configurerequest,
620 [ConfigureNotify] = configurenotify,
621 [DestroyNotify] = destroynotify,
622 [EnterNotify] = enternotify,
624 [FocusOut] = focusout,
625 [MapNotify] = mapnotify,
626 [MappingNotify] = mappingnotify,
627 [MapRequest] = maprequest,
628 [PropertyNotify] = propertynotify,
629 [UnmapNotify] = unmapnotify,
630 [VisibilityNotify] = visibilitynotify,
640 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
641 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
656 installsignal(int sig, char *name)
660 sa.sa_handler = sighdlr;
661 sigemptyset(&sa.sa_mask);
663 if (sigaction(sig, &sa, NULL) == -1)
664 err(1, "could not install %s handler", name);
668 name_to_color(char *colorname)
672 XColor screen_def, exact_def;
673 unsigned long result = 0;
674 char cname[32] = "#";
676 cmap = DefaultColormap(display, screens[0].idx);
677 status = XAllocNamedColor(display, cmap, colorname,
678 &screen_def, &exact_def);
680 strlcat(cname, colorname + 2, sizeof cname - 1);
681 status = XAllocNamedColor(display, cmap, cname, &screen_def,
685 result = screen_def.pixel;
687 fprintf(stderr, "color '%s' not found.\n", colorname);
693 setscreencolor(char *val, int i, int c)
695 if (i > 0 && i <= ScreenCount(display)) {
696 screens[i - 1].c[c].color = name_to_color(val);
697 free(screens[i - 1].c[c].name);
698 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
700 } else if (i == -1) {
701 for (i = 0; i < ScreenCount(display); i++) {
702 screens[i].c[c].color = name_to_color(val);
703 free(screens[i].c[c].name);
704 if ((screens[i].c[c].name = strdup(val)) == NULL)
708 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
709 i, ScreenCount(display));
713 custom_region(char *val)
715 unsigned int sidx, x, y, w, h;
717 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
718 errx(1, "invalid custom region, "
719 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
720 if (sidx < 1 || sidx > ScreenCount(display))
721 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
722 sidx, ScreenCount(display));
726 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
728 if (x < 0 || x > DisplayWidth(display, sidx) ||
729 y < 0 || y > DisplayHeight(display, sidx) ||
730 w + x > DisplayWidth(display, sidx) ||
731 h + y > DisplayHeight(display, sidx))
732 errx(1, "region %ux%u+%u+%u not within screen boundaries "
733 "(%ux%u)\n", w, h, x, y,
734 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
736 new_region(&screens[sidx], x, y, w, h);
740 socket_setnonblock(int fd)
744 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
745 err(1, "fcntl F_GETFL");
747 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
748 err(1, "fcntl F_SETFL");
752 bar_print(struct swm_region *r, char *s)
754 XClearWindow(display, r->bar_window);
755 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
756 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
765 bzero(bar_pipe, sizeof bar_pipe);
768 kill(bar_pid, SIGTERM);
771 strlcpy(bar_ext, "", sizeof bar_ext);
776 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
778 int do_class, do_name;
780 XClassHint *xch = NULL;
782 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
784 if ((xch = XAllocClassHint()) == NULL)
786 status = XGetClassHint(display, cur_focus->id, xch);
787 if (status == BadWindow || status == BadAlloc)
789 do_class = (title_class_enabled && xch->res_class != NULL);
790 do_name = (title_name_enabled && xch->res_name != NULL);
792 strlcat(s, xch->res_class, sz);
793 if (do_class && do_name)
796 strlcat(s, xch->res_name, sz);
809 struct swm_region *r;
813 char loc[SWM_BAR_MAX];
817 if (bar_enabled == 0)
819 if (bar_extra && bar_extra_running) {
820 /* ignore short reads; it'll correct itself */
821 while ((b = fgetln(stdin, &len)) != NULL)
822 if (b && b[len - 1] == '\n') {
824 strlcpy(bar_ext, b, sizeof bar_ext);
826 if (b == NULL && errno != EAGAIN) {
827 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
828 errno, strerror(errno));
832 strlcpy(bar_ext, "", sizeof bar_ext);
834 if (clock_enabled == 0)
835 strlcpy(s, "", sizeof s);
838 localtime_r(&tmt, &tm);
839 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
842 for (i = 0; i < ScreenCount(display); i++) {
844 TAILQ_FOREACH(r, &screens[i].rl, entry) {
846 bar_class_name(s, sizeof s, r->ws->focus);
849 stack = r->ws->cur_layout->name;
851 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
852 x++, r->ws->idx + 1, stack, s, bar_ext,
867 bar_toggle(struct swm_region *r, union arg *args)
869 struct swm_region *tmpr;
870 int i, sc = ScreenCount(display);
872 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
875 for (i = 0; i < sc; i++)
876 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
877 XUnmapWindow(display, tmpr->bar_window);
879 for (i = 0; i < sc; i++)
880 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
881 XMapRaised(display, tmpr->bar_window);
883 bar_enabled = !bar_enabled;
886 /* must be after stack */
893 XSetWindowAttributes wa;
894 struct swm_region *r;
897 /* do this here because the conf file is in memory */
898 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
899 /* launch external status app */
900 bar_extra_running = 1;
901 if (pipe(bar_pipe) == -1)
902 err(1, "pipe error");
903 socket_setnonblock(bar_pipe[0]);
904 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
905 if (dup2(bar_pipe[0], 0) == -1)
907 if (dup2(bar_pipe[1], 1) == -1)
909 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
910 err(1, "could not disable SIGPIPE");
911 switch (bar_pid = fork()) {
913 err(1, "cannot fork");
917 execvp(bar_argv[0], bar_argv);
918 err(1, "%s external app failed", bar_argv[0]);
920 default: /* parent */
926 bzero(&wa, sizeof wa);
927 for (i = 0; i < ScreenCount(display); i++)
928 TAILQ_FOREACH(r, &screens[i].rl, entry) {
930 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
931 wa.background_pixel =
932 screens[i].c[SWM_S_COLOR_BAR].color;
933 XChangeWindowAttributes(display, r->bar_window,
934 CWBackPixel | CWBorderPixel, &wa);
940 bar_setup(struct swm_region *r)
944 for (i = 0; bar_fonts[i] != NULL; i++) {
945 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
951 if (bar_fonts[i] == NULL)
952 errx(1, "couldn't load font");
953 bar_height = bar_fs->ascent + bar_fs->descent + 3;
955 r->bar_window = XCreateSimpleWindow(display,
956 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
957 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
958 r->s->c[SWM_S_COLOR_BAR].color);
959 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
960 XSetFont(display, bar_gc, bar_fs->fid);
961 XSelectInput(display, r->bar_window, VisibilityChangeMask);
963 XMapRaised(display, r->bar_window);
964 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
966 if (signal(SIGALRM, bar_signal) == SIG_ERR)
967 err(1, "could not install bar_signal");
972 set_win_state(struct ws_win *win, long state)
974 long data[] = {state, None};
976 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
981 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
982 (unsigned char *)data, 2);
990 unsigned char *p = NULL;
991 unsigned long n, extra;
994 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
995 &real, &format, &n, &extra, (unsigned char **)&p);
996 if (status != Success)
999 result = *((long *)p);
1005 version(struct swm_region *r, union arg *args)
1007 bar_version = !bar_version;
1009 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1010 SWM_VERSION, cvstag);
1012 strlcpy(bar_vertext, "", sizeof bar_vertext);
1017 client_msg(struct ws_win *win, Atom a)
1019 XClientMessageEvent cm;
1024 bzero(&cm, sizeof cm);
1025 cm.type = ClientMessage;
1026 cm.window = win->id;
1027 cm.message_type = aprot;
1030 cm.data.l[1] = CurrentTime;
1031 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1035 configreq_win(struct ws_win *win)
1037 XConfigureRequestEvent cr;
1042 bzero(&cr, sizeof cr);
1043 cr.type = ConfigureRequest;
1044 cr.display = display;
1045 cr.parent = win->id;
1046 cr.window = win->id;
1049 cr.width = win->g.w;
1050 cr.height = win->g.h;
1051 cr.border_width = 1;
1053 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1057 config_win(struct ws_win *win)
1061 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1062 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1067 ce.type = ConfigureNotify;
1068 ce.display = display;
1070 ce.window = win->id;
1073 ce.width = win->g.w;
1074 ce.height = win->g.h;
1075 ce.border_width = 1; /* XXX store this! */
1077 ce.override_redirect = False;
1078 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1082 count_win(struct workspace *ws, int count_transient)
1087 TAILQ_FOREACH(win, &ws->winlist, entry) {
1088 if (count_transient == 0 && win->floating)
1090 if (count_transient == 0 && win->transient)
1094 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1100 quit(struct swm_region *r, union arg *args)
1102 DNPRINTF(SWM_D_MISC, "quit\n");
1107 unmap_window(struct ws_win *win)
1112 /* don't unmap again */
1113 if (getstate(win->id) == IconicState)
1116 set_win_state(win, IconicState);
1118 XUnmapWindow(display, win->id);
1127 for (i = 0; i < ScreenCount(display); i++)
1128 for (j = 0; j < SWM_WS_MAX; j++)
1129 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1134 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1141 event.display = display; /* Ignored, but what the hell */
1142 event.window = win->id;
1143 event.root = win->s->root;
1144 event.subwindow = None;
1145 event.time = CurrentTime;
1150 event.same_screen = True;
1151 event.keycode = XKeysymToKeycode(display, keysym);
1152 event.state = modifiers;
1154 event.type = KeyPress;
1155 XSendEvent(event.display, event.window, True,
1156 KeyPressMask, (XEvent *)&event);
1158 event.type = KeyRelease;
1159 XSendEvent(event.display, event.window, True,
1160 KeyPressMask, (XEvent *)&event);
1165 restart(struct swm_region *r, union arg *args)
1167 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1169 /* disable alarm because the following code may not be interrupted */
1171 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1172 errx(1, "can't disable alarm");
1177 XCloseDisplay(display);
1178 execvp(start_argv[0], start_argv);
1179 fprintf(stderr, "execvp failed\n");
1185 root_to_region(Window root)
1187 struct swm_region *r = NULL;
1189 int i, x, y, wx, wy;
1192 for (i = 0; i < ScreenCount(display); i++)
1193 if (screens[i].root == root)
1196 if (XQueryPointer(display, screens[i].root,
1197 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1198 /* choose a region based on pointer location */
1199 TAILQ_FOREACH(r, &screens[i].rl, entry)
1200 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1201 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1206 r = TAILQ_FIRST(&screens[i].rl);
1212 find_unmanaged_window(Window id)
1217 for (i = 0; i < ScreenCount(display); i++)
1218 for (j = 0; j < SWM_WS_MAX; j++)
1219 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1227 find_window(Window id)
1232 for (i = 0; i < ScreenCount(display); i++)
1233 for (j = 0; j < SWM_WS_MAX; j++)
1234 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1241 spawn(struct swm_region *r, union arg *args)
1246 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1248 * The double-fork construct avoids zombie processes and keeps the code
1249 * clean from stupid signal handlers.
1254 close(ConnectionNumber(display));
1255 setenv("LD_PRELOAD", SWM_LIB, 1);
1256 if (asprintf(&ret, "%d", r->ws->idx)) {
1257 setenv("_SWM_WS", ret, 1);
1260 if (asprintf(&ret, "%d", getpid())) {
1261 setenv("_SWM_PID", ret, 1);
1265 /* kill stdin, mplayer, ssh-add etc. need that */
1266 si = open("/dev/null", O_RDONLY, 0);
1268 err(1, "open /dev/null");
1269 if (dup2(si, 0) == -1)
1270 err(1, "dup2 /dev/null");
1271 execvp(args->argv[0], args->argv);
1272 fprintf(stderr, "execvp failed\n");
1280 spawnterm(struct swm_region *r, union arg *args)
1282 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1285 setenv("_SWM_XTERM_FONTADJ", "", 1);
1290 kill_refs(struct ws_win *win)
1293 struct swm_region *r;
1294 struct workspace *ws;
1299 for (i = 0; i < ScreenCount(display); i++)
1300 TAILQ_FOREACH(r, &screens[i].rl, entry)
1301 for (x = 0; x < SWM_WS_MAX; x++) {
1303 if (win == ws->focus)
1305 if (win == ws->focus_prev)
1306 ws->focus_prev = NULL;
1311 validate_win(struct ws_win *testwin)
1314 struct workspace *ws;
1315 struct swm_region *r;
1316 int i, x, foundit = 0;
1318 if (testwin == NULL)
1321 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1322 TAILQ_FOREACH(r, &screens[i].rl, entry)
1323 for (x = 0; x < SWM_WS_MAX; x++) {
1325 TAILQ_FOREACH(win, &ws->winlist, entry)
1333 validate_ws(struct workspace *testws)
1335 struct swm_region *r;
1336 struct workspace *ws;
1339 /* validate all ws */
1340 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1341 TAILQ_FOREACH(r, &screens[i].rl, entry)
1342 for (x = 0; x < SWM_WS_MAX; x++) {
1351 unfocus_win(struct ws_win *win)
1355 if (win->ws == NULL)
1358 if (validate_ws(win->ws))
1361 if (win->ws->r == NULL)
1364 if (validate_win(win)) {
1369 if (win->ws->focus == win) {
1370 win->ws->focus = NULL;
1371 win->ws->focus_prev = win;
1374 if (validate_win(win->ws->focus)) {
1375 kill_refs(win->ws->focus);
1376 win->ws->focus = NULL;
1378 if (validate_win(win->ws->focus_prev)) {
1379 kill_refs(win->ws->focus_prev);
1380 win->ws->focus_prev = NULL;
1383 grabbuttons(win, 0);
1384 XSetWindowBorder(display, win->id,
1385 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1394 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1396 for (i = 0; i < ScreenCount(display); i++)
1397 for (j = 0; j < SWM_WS_MAX; j++)
1398 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1403 focus_win(struct ws_win *win)
1405 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1409 if (win->ws == NULL)
1412 if (validate_ws(win->ws))
1414 if (validate_win(win)) {
1419 /* use big hammer to make sure it works under all use cases */
1422 if (validate_win(win)) {
1427 win->ws->focus = win;
1429 if (win->ws->r != NULL) {
1430 grabbuttons(win, 1);
1431 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1432 XMapRaised(display, win->id);
1433 XSetWindowBorder(display, win->id,
1434 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1436 XSetInputFocus(display, win->id,
1437 RevertToPointerRoot, CurrentTime);
1442 switchws(struct swm_region *r, union arg *args)
1444 int wsid = args->id;
1445 struct swm_region *this_r, *other_r;
1447 struct workspace *new_ws, *old_ws;
1454 old_ws = this_r->ws;
1455 new_ws = &this_r->s->ws[wsid];
1457 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1458 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1461 if (new_ws == NULL || old_ws == NULL)
1463 if (new_ws == old_ws)
1466 other_r = new_ws->r;
1467 if (other_r == NULL) {
1468 /* if the other workspace is hidden, switch windows */
1469 if (old_ws->r != NULL)
1470 old_ws->old_r = old_ws->r;
1473 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1476 other_r->ws = old_ws;
1477 old_ws->r = other_r;
1479 this_r->ws = new_ws;
1483 a.id = SWM_ARG_ID_FOCUSCUR;
1484 focus(new_ws->r, &a);
1489 cyclews(struct swm_region *r, union arg *args)
1492 struct swm_screen *s = r->s;
1494 DNPRINTF(SWM_D_WS, "cyclews id %d "
1495 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1496 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1501 case SWM_ARG_ID_CYCLEWS_UP:
1502 if (a.id < SWM_WS_MAX - 1)
1507 case SWM_ARG_ID_CYCLEWS_DOWN:
1511 a.id = SWM_WS_MAX - 1;
1517 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1519 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1523 } while (a.id != r->ws->idx);
1527 cyclescr(struct swm_region *r, union arg *args)
1529 struct swm_region *rr = NULL;
1533 /* do nothing if we don't have more than one screen */
1534 if (!(ScreenCount(display) > 1 || outputs > 1))
1539 case SWM_ARG_ID_CYCLESC_UP:
1540 rr = TAILQ_NEXT(r, entry);
1542 rr = TAILQ_FIRST(&screens[i].rl);
1544 case SWM_ARG_ID_CYCLESC_DOWN:
1545 rr = TAILQ_PREV(r, swm_region_list, entry);
1547 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1555 /* move mouse to region */
1557 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1558 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1560 a.id = SWM_ARG_ID_FOCUSCUR;
1563 if (rr->ws->focus) {
1564 /* move to focus window */
1565 x = rr->ws->focus->g.x + 1;
1566 y = rr->ws->focus->g.y + 1;
1567 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1572 swapwin(struct swm_region *r, union arg *args)
1574 struct ws_win *target, *source;
1575 struct ws_win *cur_focus;
1576 struct ws_win_list *wl;
1579 DNPRINTF(SWM_D_WS, "swapwin id %d "
1580 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1581 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1583 cur_focus = r->ws->focus;
1584 if (cur_focus == NULL)
1588 wl = &source->ws->winlist;
1591 case SWM_ARG_ID_SWAPPREV:
1592 target = TAILQ_PREV(source, ws_win_list, entry);
1593 TAILQ_REMOVE(wl, cur_focus, entry);
1595 TAILQ_INSERT_TAIL(wl, source, entry);
1597 TAILQ_INSERT_BEFORE(target, source, entry);
1599 case SWM_ARG_ID_SWAPNEXT:
1600 target = TAILQ_NEXT(source, entry);
1601 TAILQ_REMOVE(wl, source, entry);
1603 TAILQ_INSERT_HEAD(wl, source, entry);
1605 TAILQ_INSERT_AFTER(wl, target, source, entry);
1607 case SWM_ARG_ID_SWAPMAIN:
1608 target = TAILQ_FIRST(wl);
1609 if (target == source) {
1610 if (source->ws->focus_prev != NULL &&
1611 source->ws->focus_prev != target)
1613 source = source->ws->focus_prev;
1617 if (target == NULL || source == NULL)
1619 source->ws->focus_prev = target;
1620 TAILQ_REMOVE(wl, target, entry);
1621 TAILQ_INSERT_BEFORE(source, target, entry);
1622 TAILQ_REMOVE(wl, source, entry);
1623 TAILQ_INSERT_HEAD(wl, source, entry);
1626 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1634 focus(struct swm_region *r, union arg *args)
1636 struct ws_win *winfocus, *winlostfocus;
1637 struct ws_win_list *wl;
1638 struct ws_win *cur_focus;
1643 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1645 /* treat FOCUS_CUR special */
1646 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1648 winfocus = r->ws->focus;
1649 else if (r->ws->focus_prev)
1650 winfocus = r->ws->focus_prev;
1652 winfocus = TAILQ_FIRST(&r->ws->winlist);
1654 focus_magic(winfocus);
1658 cur_focus = r->ws->focus;
1659 if (cur_focus == NULL)
1662 wl = &cur_focus->ws->winlist;
1664 winlostfocus = cur_focus;
1667 case SWM_ARG_ID_FOCUSPREV:
1668 winfocus = TAILQ_PREV(cur_focus, ws_win_list, entry);
1669 if (winfocus == NULL)
1670 winfocus = TAILQ_LAST(wl, ws_win_list);
1673 case SWM_ARG_ID_FOCUSNEXT:
1674 winfocus = TAILQ_NEXT(cur_focus, entry);
1675 if (winfocus == NULL)
1676 winfocus = TAILQ_FIRST(wl);
1679 case SWM_ARG_ID_FOCUSMAIN:
1680 winfocus = TAILQ_FIRST(wl);
1681 if (winfocus == cur_focus)
1682 winfocus = cur_focus->ws->focus_prev;
1683 if (winfocus == NULL)
1691 if (winfocus == winlostfocus || winfocus == NULL)
1694 focus_magic(winfocus);
1698 cycle_layout(struct swm_region *r, union arg *args)
1700 struct workspace *ws = r->ws;
1701 struct ws_win *winfocus;
1704 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1706 winfocus = ws->focus;
1709 if (ws->cur_layout->l_stack == NULL)
1710 ws->cur_layout = &layouts[0];
1713 a.id = SWM_ARG_ID_FOCUSCUR;
1718 stack_config(struct swm_region *r, union arg *args)
1720 struct workspace *ws = r->ws;
1722 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1725 if (ws->cur_layout->l_config != NULL)
1726 ws->cur_layout->l_config(ws, args->id);
1728 if (args->id != SWM_ARG_ID_STACKINIT);
1734 struct swm_geometry g;
1735 struct swm_region *r;
1738 DNPRINTF(SWM_D_STACK, "stack\n");
1740 for (i = 0; i < ScreenCount(display); i++) {
1742 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1743 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1744 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1746 /* start with screen geometry, adjust for bar */
1754 r->ws->cur_layout->l_stack(r->ws, &g);
1762 stack_floater(struct ws_win *win, struct swm_region *r)
1770 bzero(&wc, sizeof wc);
1771 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1772 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1773 (win->g.h >= HEIGHT(r)))
1774 wc.border_width = 0;
1776 wc.border_width = 1;
1777 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1778 win->g.w = (double)WIDTH(r) * dialog_ratio;
1779 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1781 wc.width = win->g.w;
1782 wc.height = win->g.h;
1787 wc.x = (WIDTH(r) - win->g.w) / 2;
1788 wc.y = (HEIGHT(r) - win->g.h) / 2;
1791 /* adjust for region */
1799 win->g.w = wc.width;
1800 win->g.h = wc.height;
1802 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1803 win->id, wc.x, wc.y, wc.width, wc.height);
1805 XConfigureWindow(display, win->id, mask, &wc);
1810 * Send keystrokes to terminal to decrease/increase the font size as the
1811 * window size changes.
1814 adjust_font(struct ws_win *win)
1816 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1817 win->floating || win->transient)
1820 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1821 win->g.w / win->sh.width_inc < term_width &&
1822 win->font_steps < SWM_MAX_FONT_STEPS) {
1823 win->font_size_boundary[win->font_steps] =
1824 (win->sh.width_inc * term_width) + win->sh.base_width;
1827 win->last_inc = win->sh.width_inc;
1828 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1829 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1830 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1833 win->last_inc = win->sh.width_inc;
1834 fake_keypress(win, XK_KP_Add, ShiftMask);
1838 #define SWAPXY(g) do { \
1840 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1841 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1844 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1847 struct swm_geometry win_g, r_g = *g;
1849 int i, j, s, stacks;
1850 int w_inc = 1, h_inc, w_base = 1, h_base;
1851 int hrh, extra = 0, h_slice, last_h = 0;
1852 int split, colno, winno, mwin, msize, mscale;
1853 int remain, missing, v_slice, reconfigure;
1856 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1857 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1859 winno = count_win(ws, 0);
1860 if (winno == 0 && count_win(ws, 1) == 0)
1863 TAILQ_FOREACH(win, &ws->winlist, entry)
1864 if (win->transient == 0 && win->floating == 0)
1871 w_inc = win->sh.width_inc;
1872 w_base = win->sh.base_width;
1873 mwin = ws->l_state.horizontal_mwin;
1874 mscale = ws->l_state.horizontal_msize;
1875 stacks = ws->l_state.horizontal_stacks;
1878 w_inc = win->sh.height_inc;
1879 w_base = win->sh.base_height;
1880 mwin = ws->l_state.vertical_mwin;
1881 mscale = ws->l_state.vertical_msize;
1882 stacks = ws->l_state.vertical_stacks;
1886 if (stacks > winno - mwin)
1887 stacks = winno - mwin;
1891 h_slice = r_g.h / SWM_H_SLICE;
1892 if (mwin && winno > mwin) {
1893 v_slice = r_g.w / SWM_V_SLICE;
1897 win_g.w = v_slice * mscale;
1899 if (w_inc > 1 && w_inc < v_slice) {
1900 /* adjust for window's requested size increment */
1901 remain = (win_g.w - w_base) % w_inc;
1902 missing = w_inc - remain;
1909 win_g.x += r_g.w - msize;
1912 colno = split = winno / stacks;
1913 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1915 hrh = r_g.h / colno;
1916 extra = r_g.h - (colno * hrh);
1919 /* stack all the tiled windows */
1920 i = j = 0, s = stacks;
1921 TAILQ_FOREACH(win, &ws->winlist, entry) {
1922 if (win->transient != 0 || win->floating != 0)
1925 if (split && i == split) {
1926 colno = (winno - mwin) / stacks;
1927 if (s <= (winno - mwin) % stacks)
1929 split = split + colno;
1930 hrh = (r_g.h / colno);
1931 extra = r_g.h - (colno * hrh);
1935 win_g.x += win_g.w + 2;
1936 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1938 win_g.w += (r_g.w - msize - (stacks * 2)) %
1945 h_inc = win->sh.width_inc;
1946 h_base = win->sh.base_width;
1948 h_inc = win->sh.height_inc;
1949 h_base = win->sh.base_height;
1951 if (j == colno - 1) {
1952 win_g.h = hrh + extra;
1953 } else if (h_inc > 1 && h_inc < h_slice) {
1954 /* adjust for window's requested size increment */
1955 remain = (win_g.h - h_base) % h_inc;
1956 missing = h_inc - remain;
1958 if (missing <= extra || j == 0) {
1970 win_g.y += last_h + 2;
1972 bzero(&wc, sizeof wc);
1973 wc.border_width = 1;
1976 if (win->g.x != win_g.y || win->g.y != win_g.x ||
1977 win->g.w != win_g.h || win->g.h != win_g.w) {
1979 win->g.x = wc.x = win_g.y;
1980 win->g.y = wc.y = win_g.x;
1981 win->g.w = wc.width = win_g.h;
1982 win->g.h = wc.height = win_g.w;
1985 if (win->g.x != win_g.x || win->g.y != win_g.y ||
1986 win->g.w != win_g.w || win->g.h != win_g.h) {
1988 win->g.x = wc.x = win_g.x;
1989 win->g.y = wc.y = win_g.y;
1990 win->g.w = wc.width = win_g.w;
1991 win->g.h = wc.height = win_g.h;
1996 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
1997 XConfigureWindow(display, win->id, mask, &wc);
2000 XMapRaised(display, win->id);
2008 /* now, stack all the floaters and transients */
2009 TAILQ_FOREACH(win, &ws->winlist, entry) {
2010 if (win->transient == 0 && win->floating == 0)
2013 stack_floater(win, ws->r);
2014 XMapRaised(display, win->id);
2019 vertical_config(struct workspace *ws, int id)
2021 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2024 case SWM_ARG_ID_STACKRESET:
2025 case SWM_ARG_ID_STACKINIT:
2026 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2027 ws->l_state.vertical_mwin = 1;
2028 ws->l_state.vertical_stacks = 1;
2030 case SWM_ARG_ID_MASTERSHRINK:
2031 if (ws->l_state.vertical_msize > 1)
2032 ws->l_state.vertical_msize--;
2034 case SWM_ARG_ID_MASTERGROW:
2035 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2036 ws->l_state.vertical_msize++;
2038 case SWM_ARG_ID_MASTERADD:
2039 ws->l_state.vertical_mwin++;
2041 case SWM_ARG_ID_MASTERDEL:
2042 if (ws->l_state.vertical_mwin > 0)
2043 ws->l_state.vertical_mwin--;
2045 case SWM_ARG_ID_STACKINC:
2046 ws->l_state.vertical_stacks++;
2048 case SWM_ARG_ID_STACKDEC:
2049 if (ws->l_state.vertical_stacks > 1)
2050 ws->l_state.vertical_stacks--;
2058 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2060 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2062 stack_master(ws, g, 0, 0);
2066 horizontal_config(struct workspace *ws, int id)
2068 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2071 case SWM_ARG_ID_STACKRESET:
2072 case SWM_ARG_ID_STACKINIT:
2073 ws->l_state.horizontal_mwin = 1;
2074 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2075 ws->l_state.horizontal_stacks = 1;
2077 case SWM_ARG_ID_MASTERSHRINK:
2078 if (ws->l_state.horizontal_msize > 1)
2079 ws->l_state.horizontal_msize--;
2081 case SWM_ARG_ID_MASTERGROW:
2082 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2083 ws->l_state.horizontal_msize++;
2085 case SWM_ARG_ID_MASTERADD:
2086 ws->l_state.horizontal_mwin++;
2088 case SWM_ARG_ID_MASTERDEL:
2089 if (ws->l_state.horizontal_mwin > 0)
2090 ws->l_state.horizontal_mwin--;
2092 case SWM_ARG_ID_STACKINC:
2093 ws->l_state.horizontal_stacks++;
2095 case SWM_ARG_ID_STACKDEC:
2096 if (ws->l_state.horizontal_stacks > 1)
2097 ws->l_state.horizontal_stacks--;
2105 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2107 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2109 stack_master(ws, g, 1, 0);
2112 /* fullscreen view */
2114 max_stack(struct workspace *ws, struct swm_geometry *g)
2117 struct swm_geometry gg = *g;
2118 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2122 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2127 winno = count_win(ws, 0);
2128 if (winno == 0 && count_win(ws, 1) == 0)
2131 TAILQ_FOREACH(win, &ws->winlist, entry) {
2132 if (win->transient) {
2134 parent = find_window(win->transient);
2138 /* only reconfigure if necessary */
2139 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2141 bzero(&wc, sizeof wc);
2142 wc.border_width = 1;
2143 win->g.x = wc.x = gg.x;
2144 win->g.y = wc.y = gg.y;
2145 win->g.w = wc.width = gg.w;
2146 win->g.h = wc.height = gg.h;
2147 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2148 XConfigureWindow(display, win->id, mask, &wc);
2151 /* unmap only if we don't have multi screen */
2152 if (win != ws->focus)
2153 if (!(ScreenCount(display) > 1 || outputs > 1))
2157 /* put the last transient on top */
2160 XMapRaised(display, parent->id);
2161 stack_floater(wintrans, ws->r);
2162 focus_magic(wintrans);
2167 send_to_ws(struct swm_region *r, union arg *args)
2169 int wsid = args->id;
2170 struct ws_win *win = win;
2171 struct workspace *ws, *nws;
2172 Atom ws_idx_atom = 0;
2173 unsigned char ws_idx_str[SWM_PROPLEN];
2183 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2186 nws = &win->s->ws[wsid];
2188 a.id = SWM_ARG_ID_FOCUSPREV;
2191 TAILQ_REMOVE(&ws->winlist, win, entry);
2192 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2195 /* Try to update the window's workspace property */
2196 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2198 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2199 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2201 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2202 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2208 wkill(struct swm_region *r, union arg *args)
2210 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2212 if(r->ws->focus == NULL)
2215 if (args->id == SWM_ARG_ID_KILLWINDOW)
2216 XKillClient(display, r->ws->focus->id);
2218 if (r->ws->focus->can_delete)
2219 client_msg(r->ws->focus, adelete);
2223 floating_toggle(struct swm_region *r, union arg *args)
2225 struct ws_win *win = r->ws->focus;
2231 win->floating = !win->floating;
2234 a.id = SWM_ARG_ID_FOCUSCUR;
2235 focus(win->ws->r, &a);
2239 resize_window(struct ws_win *win, int center)
2243 struct swm_region *r;
2245 r = root_to_region(win->wa.root);
2246 bzero(&wc, sizeof wc);
2247 mask = CWBorderWidth | CWWidth | CWHeight;
2248 wc.border_width = 1;
2249 wc.width = win->g.w;
2250 wc.height = win->g.h;
2251 if (center == SWM_ARG_ID_CENTER) {
2252 wc.x = (WIDTH(r) - win->g.w) / 2;
2253 wc.y = (HEIGHT(r) - win->g.h) / 2;
2257 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2258 win->id, wc.x, wc.y, wc.width, wc.height);
2260 XConfigureWindow(display, win->id, mask, &wc);
2265 resize(struct ws_win *win, union arg *args)
2269 struct swm_region *r = win->ws->r;
2271 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2272 win->id, win->floating, win->transient);
2274 if (!(win->transient != 0 || win->floating != 0))
2277 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2278 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2280 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2282 XMaskEvent(display, MOUSEMASK | ExposureMask |
2283 SubstructureRedirectMask, &ev);
2285 case ConfigureRequest:
2288 handler[ev.type](&ev);
2291 /* do not allow resize outside of region */
2292 if (ev.xmotion.y_root < r->g.y ||
2293 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2295 if (ev.xmotion.x_root < r->g.x ||
2296 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2299 if (ev.xmotion.x <= 1)
2301 if (ev.xmotion.y <= 1)
2303 win->g.w = ev.xmotion.x;
2304 win->g.h = ev.xmotion.y;
2306 /* not free, don't sync more than 60 times / second */
2307 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2308 time = ev.xmotion.time;
2309 XSync(display, False);
2310 resize_window(win, args->id);
2314 } while (ev.type != ButtonRelease);
2316 XSync(display, False);
2317 resize_window(win, args->id);
2319 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2321 XUngrabPointer(display, CurrentTime);
2324 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2328 move_window(struct ws_win *win)
2332 struct swm_region *r;
2334 r = root_to_region(win->wa.root);
2335 bzero(&wc, sizeof wc);
2340 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2341 win->id, wc.x, wc.y, wc.width, wc.height);
2343 XConfigureWindow(display, win->id, mask, &wc);
2348 move(struct ws_win *win, union arg *args)
2353 struct swm_region *r = win->ws->r;
2355 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2356 win->id, win->floating, win->transient);
2358 if (win->floating == 0) {
2364 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2365 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2367 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2369 XMaskEvent(display, MOUSEMASK | ExposureMask |
2370 SubstructureRedirectMask, &ev);
2372 case ConfigureRequest:
2375 handler[ev.type](&ev);
2378 /* don't allow to move window out of region */
2379 if (ev.xmotion.y_root < r->g.y ||
2380 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2382 if (ev.xmotion.x_root < r->g.x ||
2383 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2386 win->g.x = ev.xmotion.x_root;
2387 win->g.y = ev.xmotion.y_root;
2389 /* not free, don't sync more than 60 times / second */
2390 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2391 time = ev.xmotion.time;
2392 XSync(display, False);
2397 } while (ev.type != ButtonRelease);
2399 XSync(display, False);
2402 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2403 XUngrabPointer(display, CurrentTime);
2408 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2411 /* key definitions */
2413 dummykeyfunc(struct swm_region *r, union arg *args)
2418 legacyfunc(struct swm_region *r, union arg *args)
2423 char name[SWM_FUNCNAME_LEN];
2424 void (*func)(struct swm_region *r, union arg *);
2426 } keyfuncs[kf_invalid + 1] = {
2427 /* name function argument */
2428 { "cycle_layout", cycle_layout, {0} },
2429 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2430 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2431 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2432 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2433 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2434 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2435 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2436 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2437 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2438 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2439 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2440 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2441 { "spawn_term", spawnterm, {.argv = spawn_term} },
2442 { "spawn_menu", legacyfunc, {0} },
2443 { "quit", quit, {0} },
2444 { "restart", restart, {0} },
2445 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2446 { "ws_1", switchws, {.id = 0} },
2447 { "ws_2", switchws, {.id = 1} },
2448 { "ws_3", switchws, {.id = 2} },
2449 { "ws_4", switchws, {.id = 3} },
2450 { "ws_5", switchws, {.id = 4} },
2451 { "ws_6", switchws, {.id = 5} },
2452 { "ws_7", switchws, {.id = 6} },
2453 { "ws_8", switchws, {.id = 7} },
2454 { "ws_9", switchws, {.id = 8} },
2455 { "ws_10", switchws, {.id = 9} },
2456 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2457 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2458 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2459 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2460 { "mvws_1", send_to_ws, {.id = 0} },
2461 { "mvws_2", send_to_ws, {.id = 1} },
2462 { "mvws_3", send_to_ws, {.id = 2} },
2463 { "mvws_4", send_to_ws, {.id = 3} },
2464 { "mvws_5", send_to_ws, {.id = 4} },
2465 { "mvws_6", send_to_ws, {.id = 5} },
2466 { "mvws_7", send_to_ws, {.id = 6} },
2467 { "mvws_8", send_to_ws, {.id = 7} },
2468 { "mvws_9", send_to_ws, {.id = 8} },
2469 { "mvws_10", send_to_ws, {.id = 9} },
2470 { "bar_toggle", bar_toggle, {0} },
2471 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2472 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2473 { "screenshot_all", legacyfunc, {0} },
2474 { "screenshot_wind", legacyfunc, {0} },
2475 { "float_toggle", floating_toggle,{0} },
2476 { "version", version, {0} },
2477 { "spawn_lock", legacyfunc, {0} },
2478 { "spawn_initscr", legacyfunc, {0} },
2479 { "spawn_custom", dummykeyfunc, {0} },
2480 { "dumpwins", dumpwins, {0} },
2481 { "invalid key func", NULL, {0} },
2486 enum keyfuncid funcid;
2489 int keys_size = 0, keys_length = 0;
2490 struct key *keys = NULL;
2493 enum { client_click, root_click };
2495 unsigned int action;
2497 unsigned int button;
2498 void (*func)(struct ws_win *, union arg *);
2501 /* action key mouse button func args */
2502 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2503 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2504 { client_click, MODKEY, Button1, move, {0} },
2508 update_modkey(unsigned int mod)
2513 for (i = 0; i < keys_length; i++)
2514 if (keys[i].mod & ShiftMask)
2515 keys[i].mod = mod | ShiftMask;
2519 for (i = 0; i < LENGTH(buttons); i++)
2520 if (buttons[i].mask & ShiftMask)
2521 buttons[i].mask = mod | ShiftMask;
2523 buttons[i].mask = mod;
2533 int spawns_size = 0, spawns_length = 0;
2534 struct spawn_prog *spawns = NULL;
2537 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2540 struct spawn_prog *prog = NULL;
2542 char *ap, **real_args;
2544 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2547 for (i = 0; i < spawns_length; i++) {
2548 if (!strcasecmp(spawn_name, spawns[i].name))
2552 fprintf(stderr, "spawn_custom: program %s not found\n",
2557 /* make room for expanded args */
2558 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2559 err(1, "spawn_custom: calloc real_args");
2561 /* expand spawn_args into real_args */
2562 for (i = 0; i < prog->argc; i++) {
2564 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2565 if (!strcasecmp(ap, "$bar_border")) {
2567 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2569 err(1, "spawn_custom border color");
2570 } else if (!strcasecmp(ap, "$bar_color")) {
2572 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2574 err(1, "spawn_custom bar color");
2575 } else if (!strcasecmp(ap, "$bar_font")) {
2576 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2578 err(1, "spawn_custom bar fonts");
2579 } else if (!strcasecmp(ap, "$bar_font_color")) {
2581 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2583 err(1, "spawn_custom color font");
2584 } else if (!strcasecmp(ap, "$color_focus")) {
2586 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2588 err(1, "spawn_custom color focus");
2589 } else if (!strcasecmp(ap, "$color_unfocus")) {
2591 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2593 err(1, "spawn_custom color unfocus");
2595 /* no match --> copy as is */
2596 if ((real_args[i] = strdup(ap)) == NULL)
2597 err(1, "spawn_custom strdup(ap)");
2599 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2604 if ((swm_debug & SWM_D_SPAWN) != 0) {
2605 fprintf(stderr, "spawn_custom: result = ");
2606 for (i = 0; i < prog->argc; i++)
2607 fprintf(stderr, "\"%s\" ", real_args[i]);
2608 fprintf(stderr, "\n");
2614 for (i = 0; i < prog->argc; i++)
2620 setspawn(struct spawn_prog *prog)
2624 if (prog == NULL || prog->name == NULL)
2628 for (i = 0; i < spawns_length; i++) {
2629 if (!strcmp(spawns[i].name, prog->name)) {
2631 if (prog->argv == NULL) {
2633 DNPRINTF(SWM_D_SPAWN,
2634 "setspawn: delete #%d %s\n",
2636 free(spawns[i].name);
2637 for (j = 0; j < spawns[i].argc; j++)
2638 free(spawns[i].argv[j]);
2639 free(spawns[i].argv);
2640 j = spawns_length - 1;
2642 spawns[i] = spawns[j];
2647 DNPRINTF(SWM_D_SPAWN,
2648 "setspawn: replace #%d %s\n",
2650 free(spawns[i].name);
2651 for (j = 0; j < spawns[i].argc; j++)
2652 free(spawns[i].argv[j]);
2653 free(spawns[i].argv);
2656 /* found case handled */
2662 if (prog->argv == NULL) {
2664 "error: setspawn: cannot find program %s", prog->name);
2669 /* not found: add */
2670 if (spawns_size == 0 || spawns == NULL) {
2672 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2673 spawns = malloc((size_t)spawns_size *
2674 sizeof(struct spawn_prog));
2675 if (spawns == NULL) {
2676 fprintf(stderr, "setspawn: malloc failed\n");
2680 } else if (spawns_length == spawns_size) {
2682 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2683 spawns = realloc(spawns, (size_t)spawns_size *
2684 sizeof(struct spawn_prog));
2685 if (spawns == NULL) {
2686 fprintf(stderr, "setspawn: realloc failed\n");
2692 if (spawns_length < spawns_size) {
2693 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2694 spawns_length, prog->name);
2695 i = spawns_length++;
2698 fprintf(stderr, "spawns array problem?\n");
2699 if (spawns == NULL) {
2700 fprintf(stderr, "spawns array is NULL!\n");
2708 setconfspawn(char *selector, char *value, int flags)
2710 struct spawn_prog *prog;
2711 char *vp, *cp, *word;
2713 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2714 if ((prog = calloc(1, sizeof *prog)) == NULL)
2715 err(1, "setconfspawn: calloc prog");
2716 prog->name = strdup(selector);
2717 if (prog->name == NULL)
2718 err(1, "setconfspawn prog->name");
2719 if ((cp = vp = strdup(value)) == NULL)
2720 err(1, "setconfspawn: strdup(value) ");
2721 while ((word = strsep(&cp, " \t")) != NULL) {
2722 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2724 cp += (long)strspn(cp, " \t");
2725 if (strlen(word) > 0) {
2727 prog->argv = realloc(prog->argv,
2728 prog->argc * sizeof(char *));
2729 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2730 err(1, "setconfspawn: strdup");
2737 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2744 setconfspawn("term", "xterm", 0);
2745 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2746 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2747 setconfspawn("lock", "xlock", 0);
2748 setconfspawn("initscr", "initscreen.sh", 0);
2749 setconfspawn("menu", "dmenu_run"
2752 " -nf $bar_font_color"
2754 " -sf $bar_color", 0);
2758 #define SWM_MODNAME_SIZE 32
2759 #define SWM_KEY_WS "\n+ \t"
2761 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2765 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2766 if (mod == NULL || ks == NULL) {
2767 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2770 if (keystr == NULL || strlen(keystr) == 0) {
2771 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2777 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2778 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2780 cp += (long)strspn(cp, SWM_KEY_WS);
2781 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2783 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2785 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2787 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2789 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2791 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2793 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2794 *mod |= ControlMask;
2796 *ks = XStringToKeysym(name);
2797 XConvertCase(*ks, ks, &uks);
2798 if (ks == NoSymbol) {
2800 "parsekeys: invalid key %s\n",
2806 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2811 strdupsafe(char *str)
2816 return (strdup(str));
2820 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2823 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2824 keyfuncs[kfid].name, spawn_name);
2826 for (i = 0; i < keys_length; i++) {
2827 if (keys[i].mod == mod && keys[i].keysym == ks) {
2828 if (kfid == kf_invalid) {
2831 "setkeybinding: delete #%d %s\n",
2832 i, keyfuncs[keys[i].funcid].name);
2833 free(keys[i].spawn_name);
2834 j = keys_length - 1;
2838 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2841 /* found: replace */
2843 "setkeybinding: replace #%d %s %s\n",
2844 i, keyfuncs[keys[i].funcid].name,
2846 free(keys[i].spawn_name);
2848 keys[i].keysym = ks;
2849 keys[i].funcid = kfid;
2850 keys[i].spawn_name = strdupsafe(spawn_name);
2851 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2856 if (kfid == kf_invalid) {
2858 "error: setkeybinding: cannot find mod/key combination");
2859 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2862 /* not found: add */
2863 if (keys_size == 0 || keys == NULL) {
2865 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2866 keys = malloc((size_t)keys_size * sizeof(struct key));
2868 fprintf(stderr, "malloc failed\n");
2872 } else if (keys_length == keys_size) {
2874 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2875 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2877 fprintf(stderr, "realloc failed\n");
2882 if (keys_length < keys_size) {
2884 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2885 j, keyfuncs[kfid].name, spawn_name);
2887 keys[j].keysym = ks;
2888 keys[j].funcid = kfid;
2889 keys[j].spawn_name = strdupsafe(spawn_name);
2891 fprintf(stderr, "keys array problem?\n");
2893 fprintf(stderr, "keys array problem\n");
2897 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2901 setconfbinding(char *selector, char *value, int flags)
2903 enum keyfuncid kfid;
2907 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2908 if (selector == NULL) {
2909 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2910 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2912 setkeybinding(mod, ks, kfid, NULL);
2917 /* search by key function name */
2918 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2919 if (strncasecmp(selector, keyfuncs[kfid].name,
2920 SWM_FUNCNAME_LEN) == 0) {
2921 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2923 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2924 setkeybinding(mod, ks, kfid, NULL);
2930 /* search by custom spawn name */
2931 for (i = 0; i < spawns_length; i++) {
2932 if (strcasecmp(selector, spawns[i].name) == 0) {
2933 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2935 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2936 setkeybinding(mod, ks, kf_spawn_custom,
2943 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
2950 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
2951 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
2952 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
2953 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
2954 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
2955 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
2956 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
2957 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
2958 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
2959 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
2960 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
2961 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
2962 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
2963 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
2964 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
2965 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
2966 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
2967 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
2968 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
2969 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
2970 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
2971 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
2972 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
2973 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
2974 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
2975 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
2976 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
2977 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
2978 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
2979 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
2980 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
2981 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
2982 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
2983 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
2984 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
2985 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
2986 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
2987 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
2988 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
2989 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
2990 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
2991 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
2992 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
2993 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
2994 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
2995 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
2996 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
2997 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
2998 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
2999 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3000 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3001 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3002 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3004 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3009 updatenumlockmask(void)
3012 XModifierKeymap *modmap;
3014 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3016 modmap = XGetModifierMapping(display);
3017 for (i = 0; i < 8; i++)
3018 for (j = 0; j < modmap->max_keypermod; j++)
3019 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3020 == XKeysymToKeycode(display, XK_Num_Lock))
3021 numlockmask = (1 << i);
3023 XFreeModifiermap(modmap);
3029 unsigned int i, j, k;
3031 unsigned int modifiers[] =
3032 { 0, LockMask, numlockmask, numlockmask | LockMask };
3034 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3035 updatenumlockmask();
3037 for (k = 0; k < ScreenCount(display); k++) {
3038 if (TAILQ_EMPTY(&screens[k].rl))
3040 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3041 for (i = 0; i < keys_length; i++) {
3042 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3043 for (j = 0; j < LENGTH(modifiers); j++)
3044 XGrabKey(display, code,
3045 keys[i].mod | modifiers[j],
3046 screens[k].root, True,
3047 GrabModeAsync, GrabModeAsync);
3053 grabbuttons(struct ws_win *win, int focused)
3056 unsigned int modifiers[] =
3057 { 0, LockMask, numlockmask, numlockmask|LockMask };
3059 updatenumlockmask();
3060 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3062 for (i = 0; i < LENGTH(buttons); i++)
3063 if (buttons[i].action == client_click)
3064 for (j = 0; j < LENGTH(modifiers); j++)
3065 XGrabButton(display, buttons[i].button,
3066 buttons[i].mask | modifiers[j],
3067 win->id, False, BUTTONMASK,
3068 GrabModeAsync, GrabModeSync, None,
3071 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3072 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3075 const char *quirkname[] = {
3076 "NONE", /* config string for "no value" */
3084 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3085 #define SWM_Q_WS "\n|+ \t"
3087 parsequirks(char *qstr, unsigned long *quirk)
3097 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3099 cp += (long)strspn(cp, SWM_Q_WS);
3100 for (i = 0; i < LENGTH(quirkname); i++) {
3101 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3102 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3107 *quirk |= 1 << (i-1);
3111 if (i >= LENGTH(quirkname)) {
3112 DNPRINTF(SWM_D_QUIRK,
3113 "parsequirks: invalid quirk [%s]\n", name);
3121 setquirk(const char *class, const char *name, const int quirk)
3126 for (i = 0; i < quirks_length; i++) {
3127 if (!strcmp(quirks[i].class, class) &&
3128 !strcmp(quirks[i].name, name)) {
3131 DNPRINTF(SWM_D_QUIRK,
3132 "setquirk: delete #%d %s:%s\n",
3133 i, quirks[i].class, quirks[i].name);
3134 free(quirks[i].class);
3135 free(quirks[i].name);
3136 j = quirks_length - 1;
3138 quirks[i] = quirks[j];
3142 /* found: replace */
3143 DNPRINTF(SWM_D_QUIRK,
3144 "setquirk: replace #%d %s:%s\n",
3145 i, quirks[i].class, quirks[i].name);
3146 free(quirks[i].class);
3147 free(quirks[i].name);
3148 quirks[i].class = strdup(class);
3149 quirks[i].name = strdup(name);
3150 quirks[i].quirk = quirk;
3157 "error: setquirk: cannot find class/name combination");
3160 /* not found: add */
3161 if (quirks_size == 0 || quirks == NULL) {
3163 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3164 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3166 fprintf(stderr, "setquirk: malloc failed\n");
3170 } else if (quirks_length == quirks_size) {
3172 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3173 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3175 fprintf(stderr, "setquirk: realloc failed\n");
3180 if (quirks_length < quirks_size) {
3181 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3182 j = quirks_length++;
3183 quirks[j].class = strdup(class);
3184 quirks[j].name = strdup(name);
3185 quirks[j].quirk = quirk;
3187 fprintf(stderr, "quirks array problem?\n");
3189 fprintf(stderr, "quirks array problem!\n");
3196 setconfquirk(char *selector, char *value, int flags)
3198 char *cp, *class, *name;
3200 unsigned long quirks;
3201 if (selector == NULL)
3203 if ((cp = strchr(selector, ':')) == NULL)
3208 if ((retval = parsequirks(value, &quirks)) == 0)
3209 setquirk(class, name, quirks);
3216 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3217 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3218 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3219 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3220 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3221 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3222 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3223 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3224 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3225 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3226 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3227 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3228 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3231 /* conf file stuff */
3232 #define SWM_CONF_FILE "scrotwm.conf"
3234 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3235 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3236 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3237 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3238 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3241 setconfvalue(char *selector, char *value, int flags)
3244 case SWM_S_BAR_DELAY:
3245 bar_delay = atoi(value);
3247 case SWM_S_BAR_ENABLED:
3248 bar_enabled = atoi(value);
3250 case SWM_S_STACK_ENABLED:
3251 stack_enabled = atoi(value);
3253 case SWM_S_CLOCK_ENABLED:
3254 clock_enabled = atoi(value);
3256 case SWM_S_CYCLE_EMPTY:
3257 cycle_empty = atoi(value);
3259 case SWM_S_CYCLE_VISIBLE:
3260 cycle_visible = atoi(value);
3262 case SWM_S_SS_ENABLED:
3263 ss_enabled = atoi(value);
3265 case SWM_S_TERM_WIDTH:
3266 term_width = atoi(value);
3268 case SWM_S_TITLE_CLASS_ENABLED:
3269 title_class_enabled = atoi(value);
3271 case SWM_S_TITLE_NAME_ENABLED:
3272 title_name_enabled = atoi(value);
3274 case SWM_S_BAR_FONT:
3276 if ((bar_fonts[0] = strdup(value)) == NULL)
3277 err(1, "setconfvalue: bar_font");
3279 case SWM_S_BAR_ACTION:
3281 if ((bar_argv[0] = strdup(value)) == NULL)
3282 err(1, "setconfvalue: bar_action");
3284 case SWM_S_SPAWN_TERM:
3285 free(spawn_term[0]);
3286 if ((spawn_term[0] = strdup(value)) == NULL)
3287 err(1, "setconfvalue: spawn_term");
3291 case SWM_S_DIALOG_RATIO:
3292 dialog_ratio = atof(value);
3293 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3303 setconfmodkey(char *selector, char *value, int flags)
3305 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3306 update_modkey(Mod1Mask);
3307 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3308 update_modkey(Mod2Mask);
3309 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3310 update_modkey(Mod3Mask);
3311 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3312 update_modkey(Mod4Mask);
3319 setconfcolor(char *selector, char *value, int flags)
3321 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3326 setconfregion(char *selector, char *value, int flags)
3328 custom_region(value);
3332 /* config options */
3333 struct config_option {
3335 int (*func)(char*, char*, int);
3338 struct config_option configopt[] = {
3339 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3340 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3341 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3342 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3343 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3344 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3345 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3346 { "bind", setconfbinding, 0 },
3347 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3348 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3349 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3350 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3351 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3352 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3353 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3354 { "modkey", setconfmodkey, 0 },
3355 { "program", setconfspawn, 0 },
3356 { "quirk", setconfquirk, 0 },
3357 { "region", setconfregion, 0 },
3358 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3359 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3360 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3361 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3362 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3363 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3368 conf_load(char *filename)
3371 char *line, *cp, *optsub, *optval;
3372 size_t linelen, lineno = 0;
3373 int wordlen, i, optind;
3374 struct config_option *opt;
3376 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3378 if (filename == NULL) {
3379 fprintf(stderr, "conf_load: no filename\n");
3382 if ((config = fopen(filename, "r")) == NULL) {
3383 warn("conf_load: fopen");
3387 while (!feof(config)) {
3388 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3391 err(1, "%s", filename);
3396 cp += strspn(cp, " \t\n"); /* eat whitespace */
3397 if (cp[0] == '\0') {
3402 /* get config option */
3403 wordlen = strcspn(cp, "=[ \t\n");
3405 warnx("%s: line %zd: no option found",
3410 for (i = 0; i < LENGTH(configopt); i++) {
3411 opt = &configopt[i];
3412 if (!strncasecmp(cp, opt->optname, wordlen) &&
3413 strlen(opt->optname) == wordlen) {
3419 warnx("%s: line %zd: unknown option %.*s",
3420 filename, lineno, wordlen, cp);
3424 cp += strspn(cp, " \t\n"); /* eat whitespace */
3425 /* get [selector] if any */
3429 wordlen = strcspn(cp, "]");
3432 warnx("%s: line %zd: syntax error",
3436 asprintf(&optsub, "%.*s", wordlen, cp);
3439 cp += strspn(cp, "] \t\n"); /* eat trailing */
3441 cp += strspn(cp, "= \t\n"); /* eat trailing */
3443 optval = strdup(cp);
3444 /* call function to deal with it all */
3445 if (configopt[optind].func(optsub, optval,
3446 configopt[optind].funcflags) != 0) {
3447 fprintf(stderr, "%s line %zd: %s\n",
3448 filename, lineno, line);
3449 errx(1, "%s: line %zd: invalid data for %s",
3450 filename, lineno, configopt[optind].optname);
3458 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3464 set_child_transient(struct ws_win *win)
3466 struct ws_win *parent;
3468 parent = find_window(win->transient);
3470 parent->child_trans = win;
3474 manage_window(Window id)
3477 struct workspace *ws;
3478 struct ws_win *win, *ww;
3479 int format, i, ws_idx, n, border_me = 0;
3480 unsigned long nitems, bytes;
3481 Atom ws_idx_atom = 0, type;
3482 Atom *prot = NULL, *pp;
3483 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3484 struct swm_region *r;
3489 if ((win = find_window(id)) != NULL)
3490 return (win); /* already being managed */
3492 /* see if we are on the unmanaged list */
3493 if ((win = find_unmanaged_window(id)) != NULL) {
3494 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3496 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3497 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3499 set_child_transient(win);
3503 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3504 errx(1, "calloc: failed to allocate memory for new window");
3506 /* Get all the window data in one shot */
3507 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3509 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3510 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3511 XGetWindowAttributes(display, id, &win->wa);
3512 XGetWMNormalHints(display, id, &win->sh, &mask);
3513 XGetTransientForHint(display, id, &trans);
3515 win->transient = trans;
3516 set_child_transient(win);
3517 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3518 win->id, win->transient);
3520 /* get supported protocols */
3521 if (XGetWMProtocols(display, id, &prot, &n)) {
3522 for (i = 0, pp = prot; i < n; i++, pp++) {
3523 if (*pp == takefocus)
3524 win->take_focus = 1;
3526 win->can_delete = 1;
3533 * Figure out where to put the window. If it was previously assigned to
3534 * a workspace (either by spawn() or manually moving), and isn't
3535 * transient, * put it in the same workspace
3537 r = root_to_region(win->wa.root);
3538 if (prop && win->transient == 0) {
3539 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3540 ws_idx = strtonum(prop, 0, 9, &errstr);
3542 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3545 ws = &r->s->ws[ws_idx];
3548 /* this should launch transients in the same ws as parent */
3550 if ((ww = find_window(trans)) != NULL)
3557 "fix this bug mcbride\n");
3562 /* set up the window layout */
3565 win->s = r->s; /* this never changes */
3566 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3568 win->g.w = win->wa.width;
3569 win->g.h = win->wa.height;
3570 win->g.x = win->wa.x;
3571 win->g.y = win->wa.y;
3573 /* Set window properties so we can remember this after reincarnation */
3574 if (ws_idx_atom && prop == NULL &&
3575 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3576 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3578 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3579 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3583 if (XGetClassHint(display, win->id, &win->ch)) {
3584 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3585 win->ch.res_class, win->ch.res_name);
3587 /* java is retarded so treat it special */
3588 if (strstr(win->ch.res_name, "sun-awt")) {
3593 for (i = 0; i < quirks_length; i++){
3594 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3595 !strcmp(win->ch.res_name, quirks[i].name)) {
3596 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3597 win->ch.res_class, win->ch.res_name);
3598 if (quirks[i].quirk & SWM_Q_FLOAT) {
3602 win->quirks = quirks[i].quirk;
3607 /* alter window position if quirky */
3608 if (win->quirks & SWM_Q_ANYWHERE) {
3609 win->manual = 1; /* don't center the quirky windows */
3610 bzero(&wc, sizeof wc);
3612 if (win->g.y < bar_height) {
3613 win->g.y = wc.y = bar_height;
3616 if (win->g.w + win->g.x > WIDTH(r)) {
3617 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3623 /* Reset font sizes (the bruteforce way; no default keybinding). */
3624 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3625 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3626 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3627 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3628 fake_keypress(win, XK_KP_Add, ShiftMask);
3633 bzero(&wc, sizeof wc);
3634 wc.border_width = 1;
3635 mask = CWBorderWidth;
3636 XConfigureWindow(display, win->id, mask, &wc);
3640 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3641 PropertyChangeMask | StructureNotifyMask);
3643 set_win_state(win, NormalState);
3645 /* floaters need to be mapped if they are in the current workspace */
3646 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3647 XMapRaised(display, win->id);
3653 free_window(struct ws_win *win)
3655 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3660 /* needed for restart wm */
3661 set_win_state(win, WithdrawnState);
3663 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3665 if (win->ch.res_class)
3666 XFree(win->ch.res_class);
3667 if (win->ch.res_name)
3668 XFree(win->ch.res_name);
3673 memset(win, 0xff, sizeof *win); /* XXX kill later */
3679 unmanage_window(struct ws_win *win)
3681 struct ws_win *parent;
3686 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3688 if (win->transient) {
3689 parent = find_window(win->transient);
3691 parent->child_trans = NULL;
3694 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3695 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3701 focus_magic(struct ws_win *win)
3706 if (win->child_trans) {
3707 /* win = parent & has a transient so focus on that */
3709 focus_win(win->child_trans);
3710 if (win->child_trans->take_focus)
3711 client_msg(win, takefocus);
3713 focus_win(win->child_trans);
3714 if (win->child_trans->take_focus)
3715 client_msg(win->child_trans, takefocus);
3720 if (win->take_focus)
3721 client_msg(win, takefocus);
3728 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3736 XKeyEvent *ev = &e->xkey;
3738 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3740 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3741 for (i = 0; i < keys_length; i++)
3742 if (keysym == keys[i].keysym
3743 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3744 && keyfuncs[keys[i].funcid].func) {
3745 if (keys[i].funcid == kf_spawn_custom)
3747 root_to_region(ev->root),
3748 &(keyfuncs[keys[i].funcid].args),
3752 keyfuncs[keys[i].funcid].func(
3753 root_to_region(ev->root),
3754 &(keyfuncs[keys[i].funcid].args)
3760 buttonpress(XEvent *e)
3764 XButtonPressedEvent *ev = &e->xbutton;
3766 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3768 action = root_click;
3769 if ((win = find_window(ev->window)) == NULL)
3773 action = client_click;
3775 for (i = 0; i < LENGTH(buttons); i++)
3776 if (action == buttons[i].action && buttons[i].func &&
3777 buttons[i].button == ev->button &&
3778 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3779 buttons[i].func(win, &buttons[i].args);
3783 configurerequest(XEvent *e)
3785 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3790 if ((win = find_window(ev->window)) == NULL)
3791 if ((win = find_unmanaged_window(ev->window)) == NULL)
3795 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3797 bzero(&wc, sizeof wc);
3800 wc.width = ev->width;
3801 wc.height = ev->height;
3802 wc.border_width = ev->border_width;
3803 wc.sibling = ev->above;
3804 wc.stack_mode = ev->detail;
3805 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3807 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3809 if (win->floating) {
3810 if (ev->value_mask & CWX)
3812 if (ev->value_mask & CWY)
3814 if (ev->value_mask & CWWidth)
3815 win->g.w = ev->width;
3816 if (ev->value_mask & CWHeight)
3817 win->g.h = ev->height;
3824 configurenotify(XEvent *e)
3829 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3830 e->xconfigure.window);
3832 win = find_window(e->xconfigure.window);
3834 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3842 destroynotify(XEvent *e)
3845 XDestroyWindowEvent *ev = &e->xdestroywindow;
3848 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3850 if ((win = find_window(ev->window)) == NULL) {
3851 if ((win = find_unmanaged_window(ev->window)) == NULL)
3857 a.id = SWM_ARG_ID_FOCUSPREV;
3858 focus(win->ws->r, &a);
3859 unmanage_window(win);
3865 enternotify(XEvent *e)
3867 XCrossingEvent *ev = &e->xcrossing;
3870 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3873 * happens when a window is created or destroyed and the border
3874 * crosses the mouse pointer
3876 if (QLength(display))
3879 if ((win = find_window(ev->window)) == NULL)
3888 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3894 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3898 mapnotify(XEvent *e)
3901 XMapEvent *ev = &e->xmap;
3903 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
3905 win = manage_window(ev->window);
3907 set_win_state(win, NormalState);
3911 mappingnotify(XEvent *e)
3913 XMappingEvent *ev = &e->xmapping;
3915 XRefreshKeyboardMapping(ev);
3916 if (ev->request == MappingKeyboard)
3921 maprequest(XEvent *e)
3924 struct swm_region *r;
3925 XWindowAttributes wa;
3926 XMapRequestEvent *ev = &e->xmaprequest;
3928 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3929 e->xmaprequest.window);
3931 if (!XGetWindowAttributes(display, ev->window, &wa))
3933 if (wa.override_redirect)
3936 win = manage_window(e->xmaprequest.window);
3938 return; /* can't happen */
3942 /* make new win focused */
3943 r = root_to_region(win->wa.root);
3944 if (win->ws == r->ws)
3949 propertynotify(XEvent *e)
3952 XPropertyEvent *ev = &e->xproperty;
3954 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3957 if (ev->state == PropertyDelete)
3958 return; /* ignore */
3959 win = find_window(ev->window);
3964 case XA_WM_NORMAL_HINTS:
3967 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3968 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3969 if (win->sh.flags & PMinSize) {
3970 win->g.w = win->sh.min_width;
3971 win->g.h = win->sh.min_height;
3972 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3974 XMoveResizeWindow(display, win->id,
3975 win->g.x, win->g.y, win->g.w, win->g.h);
3984 unmapnotify(XEvent *e)
3989 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
3991 /* determine if we need to help unmanage this window */
3992 win = find_window(e->xunmap.window);
3996 if (getstate(e->xunmap.window) == NormalState) {
3997 /* trash window and refocus */
3998 a.id = SWM_ARG_ID_FOCUSPREV;
3999 focus(win->ws->r, &a);
4000 unmanage_window(win);
4006 visibilitynotify(XEvent *e)
4009 struct swm_region *r;
4011 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4012 e->xvisibility.window);
4013 if (e->xvisibility.state == VisibilityUnobscured)
4014 for (i = 0; i < ScreenCount(display); i++)
4015 TAILQ_FOREACH(r, &screens[i].rl, entry)
4016 if (e->xvisibility.window == r->bar_window)
4021 xerror_start(Display *d, XErrorEvent *ee)
4028 xerror(Display *d, XErrorEvent *ee)
4030 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4038 xerrorxlib = XSetErrorHandler(xerror_start);
4040 /* this causes an error if some other window manager is running */
4041 XSelectInput(display, DefaultRootWindow(display),
4042 SubstructureRedirectMask);
4043 XSync(display, False);
4047 XSetErrorHandler(xerror);
4048 XSync(display, False);
4053 new_region(struct swm_screen *s, int x, int y, int w, int h)
4055 struct swm_region *r, *n;
4056 struct workspace *ws = NULL;
4059 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4060 s->idx, w, h, x, y);
4062 /* remove any conflicting regions */
4063 n = TAILQ_FIRST(&s->rl);
4066 n = TAILQ_NEXT(r, entry);
4067 if (X(r) < (x + w) &&
4068 (X(r) + WIDTH(r)) > x &&
4070 (Y(r) + HEIGHT(r)) > y) {
4071 XDestroyWindow(display, r->bar_window);
4072 TAILQ_REMOVE(&s->rl, r, entry);
4073 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4077 /* search old regions for one to reuse */
4079 /* size + location match */
4080 TAILQ_FOREACH(r, &s->orl, entry)
4081 if (X(r) == x && Y(r) == y &&
4082 HEIGHT(r) == h && WIDTH(r) == w)
4086 TAILQ_FOREACH(r, &s->orl, entry)
4087 if (HEIGHT(r) == h && WIDTH(r) == w)
4091 TAILQ_REMOVE(&s->orl, r, entry);
4092 /* try to use old region's workspace */
4093 if (r->ws->r == NULL)
4096 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4097 errx(1, "calloc: failed to allocate memory for screen");
4099 /* if we don't have a workspace already, find one */
4101 for (i = 0; i < SWM_WS_MAX; i++)
4102 if (s->ws[i].r == NULL) {
4109 errx(1, "no free workspaces\n");
4118 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4124 #ifdef SWM_XRR_HAS_CRTC
4126 XRRScreenResources *sr;
4129 #endif /* SWM_XRR_HAS_CRTC */
4130 struct swm_region *r;
4133 if (i >= ScreenCount(display))
4134 errx(1, "invalid screen");
4136 /* remove any old regions */
4137 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4138 r->ws->old_r = r->ws->r = NULL;
4139 XDestroyWindow(display, r->bar_window);
4140 TAILQ_REMOVE(&screens[i].rl, r, entry);
4141 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4144 /* map virtual screens onto physical screens */
4145 #ifdef SWM_XRR_HAS_CRTC
4147 if (xrandr_support) {
4148 sr = XRRGetScreenResources(display, screens[i].root);
4150 new_region(&screens[i], 0, 0,
4151 DisplayWidth(display, i),
4152 DisplayHeight(display, i));
4156 for (c = 0, ci = NULL; c < ncrtc; c++) {
4157 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4158 if (ci->noutput == 0)
4162 if (ci != NULL && ci->mode == None)
4163 new_region(&screens[i], 0, 0,
4164 DisplayWidth(display, i),
4165 DisplayHeight(display, i));
4167 new_region(&screens[i],
4168 ci->x, ci->y, ci->width, ci->height);
4171 XRRFreeCrtcInfo(ci);
4172 XRRFreeScreenResources(sr);
4174 #endif /* SWM_XRR_HAS_CRTC */
4176 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4177 DisplayHeight(display, i));
4182 screenchange(XEvent *e) {
4183 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4184 struct swm_region *r;
4187 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4189 if (!XRRUpdateConfiguration(e))
4192 /* silly event doesn't include the screen index */
4193 for (i = 0; i < ScreenCount(display); i++)
4194 if (screens[i].root == xe->root)
4196 if (i >= ScreenCount(display))
4197 errx(1, "screenchange: screen not found\n");
4199 /* brute force for now, just re-enumerate the regions */
4202 /* add bars to all regions */
4203 for (i = 0; i < ScreenCount(display); i++)
4204 TAILQ_FOREACH(r, &screens[i].rl, entry)
4212 Window d1, d2, *wins = NULL;
4213 XWindowAttributes wa;
4216 int errorbase, major, minor;
4217 struct workspace *ws;
4221 if ((screens = calloc(ScreenCount(display),
4222 sizeof(struct swm_screen))) == NULL)
4223 errx(1, "calloc: screens");
4225 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4227 /* initial Xrandr setup */
4228 xrandr_support = XRRQueryExtension(display,
4229 &xrandr_eventbase, &errorbase);
4231 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4234 /* map physical screens */
4235 for (i = 0; i < ScreenCount(display); i++) {
4236 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4238 TAILQ_INIT(&screens[i].rl);
4239 TAILQ_INIT(&screens[i].orl);
4240 screens[i].root = RootWindow(display, i);
4242 /* set default colors */
4243 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4244 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4245 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4246 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4247 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4249 /* init all workspaces */
4250 /* XXX these should be dynamically allocated too */
4251 for (j = 0; j < SWM_WS_MAX; j++) {
4252 ws = &screens[i].ws[j];
4257 TAILQ_INIT(&ws->winlist);
4258 TAILQ_INIT(&ws->unmanagedlist);
4260 for (k = 0; layouts[k].l_stack != NULL; k++)
4261 if (layouts[k].l_config != NULL)
4262 layouts[k].l_config(ws,
4263 SWM_ARG_ID_STACKINIT);
4264 ws->cur_layout = &layouts[0];
4266 /* grab existing windows (before we build the bars)*/
4267 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4273 XRRSelectInput(display, screens[i].root,
4274 RRScreenChangeNotifyMask);
4276 /* attach windows to a region */
4277 /* normal windows */
4278 for (j = 0; j < no; j++) {
4279 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4280 wa.override_redirect ||
4281 XGetTransientForHint(display, wins[j], &d1))
4284 state = getstate(wins[j]);
4285 manage = state == IconicState;
4286 if (wa.map_state == IsViewable || manage)
4287 manage_window(wins[j]);
4289 /* transient windows */
4290 for (j = 0; j < no; j++) {
4291 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4292 wa.override_redirect)
4295 state = getstate(wins[j]);
4296 manage = state == IconicState;
4297 if (XGetTransientForHint(display, wins[j], &d1) &&
4299 manage_window(wins[j]);
4311 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4313 err(1, "setup_globals: strdup");
4314 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4316 err(1, "setup_globals: strdup");
4317 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4319 err(1, "setup_globals: strdup");
4320 if ((spawn_term[0] = strdup("xterm")) == NULL)
4321 err(1, "setup_globals: strdup");
4328 Atom netwmcheck, netwmname, utf8_string;
4331 /* work around sun jdk bugs, code from wmname */
4332 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4333 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4334 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4335 for (i = 0; i < ScreenCount(display); i++) {
4336 root = screens[i].root;
4337 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4338 PropModeReplace, (unsigned char *)&root, 1);
4339 XChangeProperty(display, root, netwmname, utf8_string, 8,
4340 PropModeReplace, "LG3D", strlen("LG3D"));
4345 main(int argc, char *argv[])
4348 struct swm_region *r, *rr;
4349 struct ws_win *winfocus = NULL;
4352 char conf[PATH_MAX], *cfile = NULL;
4359 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4360 SWM_VERSION, cvstag);
4361 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4362 warnx("no locale support");
4364 if (!(display = XOpenDisplay(0)))
4365 errx(1, "can not open display");
4368 errx(1, "other wm running");
4370 /* handle some signale */
4371 installsignal(SIGINT, "INT");
4372 installsignal(SIGHUP, "HUP");
4373 installsignal(SIGQUIT, "QUIT");
4374 installsignal(SIGTERM, "TERM");
4375 installsignal(SIGCHLD, "CHLD");
4377 astate = XInternAtom(display, "WM_STATE", False);
4378 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4379 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4380 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4382 /* look for local and global conf file */
4383 pwd = getpwuid(getuid());
4385 errx(1, "invalid user %d", getuid());
4393 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4394 if (stat(conf, &sb) != -1) {
4395 if (S_ISREG(sb.st_mode))
4398 /* try global conf file */
4399 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4400 if (!stat(conf, &sb))
4401 if (S_ISREG(sb.st_mode))
4407 /* setup all bars */
4408 for (i = 0; i < ScreenCount(display); i++)
4409 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4410 if (winfocus == NULL)
4411 winfocus = TAILQ_FIRST(&r->ws->winlist);
4415 /* set some values to work around bad programs */
4421 xfd = ConnectionNumber(display);
4423 while (XPending(display)) {
4424 XNextEvent(display, &e);
4427 if (e.type < LASTEvent) {
4429 if (handler[e.type])
4430 handler[e.type](&e);
4432 DNPRINTF(SWM_D_EVENT,
4433 "win: %lu unknown event: %d\n",
4434 e.xany.window, e.type);
4436 switch (e.type - xrandr_eventbase) {
4437 case RRScreenChangeNotify:
4441 DNPRINTF(SWM_D_EVENT,
4442 "win: %lu unknown xrandr event: "
4443 "%d\n", e.xany.window, e.type);
4449 /* if we are being restarted go focus on first window */
4451 rr = winfocus->ws->r;
4453 /* not a visible window */
4457 /* move pointer to first screen if multi screen */
4458 if (ScreenCount(display) > 1 || outputs > 1)
4459 XWarpPointer(display, None, rr->s[0].root,
4460 0, 0, 0, 0, rr->g.x,
4461 rr->g.y + bar_enabled ? bar_height : 0);
4463 a.id = SWM_ARG_ID_FOCUSCUR;
4473 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4475 DNPRINTF(SWM_D_MISC, "select failed");
4486 XCloseDisplay(display);