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_FOCUSPREV (1<<0)
333 #define SWM_L_MAPONFOCUS (1<<1)
336 /* stack, configure */
337 { vertical_stack, vertical_config, 0, "[|]" },
338 { horizontal_stack, horizontal_config, 0, "[-]" },
340 SWM_L_MAPONFOCUS | SWM_L_FOCUSPREV, "[ ]"},
341 { NULL, NULL, 0, NULL },
344 #define SWM_H_SLICE (32)
345 #define SWM_V_SLICE (32)
347 /* define work spaces */
349 int idx; /* workspace index */
350 struct layout *cur_layout; /* current layout handlers */
351 struct ws_win *focus; /* may be NULL */
352 struct ws_win *focus_prev; /* may be NULL */
353 struct swm_region *r; /* may be NULL */
354 struct swm_region *old_r; /* may be NULL */
355 struct ws_win_list winlist; /* list of windows in ws */
356 struct ws_win_list unmanagedlist; /* list of dead windows in ws */
360 int horizontal_msize;
362 int horizontal_stacks;
369 enum { SWM_S_COLOR_BAR, SWM_S_COLOR_BAR_BORDER, SWM_S_COLOR_BAR_FONT,
370 SWM_S_COLOR_FOCUS, SWM_S_COLOR_UNFOCUS, SWM_S_COLOR_MAX };
372 /* physical screen mapping */
373 #define SWM_WS_MAX (10)
375 int idx; /* screen index */
376 struct swm_region_list rl; /* list of regions on this screen */
377 struct swm_region_list orl; /* list of old regions */
379 struct workspace ws[SWM_WS_MAX];
385 } c[SWM_S_COLOR_MAX];
387 struct swm_screen *screens;
390 /* args to functions */
393 #define SWM_ARG_ID_FOCUSNEXT (0)
394 #define SWM_ARG_ID_FOCUSPREV (1)
395 #define SWM_ARG_ID_FOCUSMAIN (2)
396 #define SWM_ARG_ID_FOCUSCUR (4)
397 #define SWM_ARG_ID_SWAPNEXT (10)
398 #define SWM_ARG_ID_SWAPPREV (11)
399 #define SWM_ARG_ID_SWAPMAIN (12)
400 #define SWM_ARG_ID_MASTERSHRINK (20)
401 #define SWM_ARG_ID_MASTERGROW (21)
402 #define SWM_ARG_ID_MASTERADD (22)
403 #define SWM_ARG_ID_MASTERDEL (23)
404 #define SWM_ARG_ID_STACKRESET (30)
405 #define SWM_ARG_ID_STACKINIT (31)
406 #define SWM_ARG_ID_CYCLEWS_UP (40)
407 #define SWM_ARG_ID_CYCLEWS_DOWN (41)
408 #define SWM_ARG_ID_CYCLESC_UP (42)
409 #define SWM_ARG_ID_CYCLESC_DOWN (43)
410 #define SWM_ARG_ID_STACKINC (50)
411 #define SWM_ARG_ID_STACKDEC (51)
412 #define SWM_ARG_ID_SS_ALL (60)
413 #define SWM_ARG_ID_SS_WINDOW (61)
414 #define SWM_ARG_ID_DONTCENTER (70)
415 #define SWM_ARG_ID_CENTER (71)
416 #define SWM_ARG_ID_KILLWINDOW (80)
417 #define SWM_ARG_ID_DELETEWINDOW (81)
421 void focus(struct swm_region *, union arg *);
422 void focus_magic(struct ws_win *, int);
429 #define SWM_Q_FLOAT (1<<0) /* float this window */
430 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
431 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
432 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
433 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
435 int quirks_size = 0, quirks_length = 0;
436 struct quirk *quirks = NULL;
453 name = "ButtonPress";
456 name = "ButtonRelease";
459 name = "MotionNotify";
462 name = "EnterNotify";
465 name = "LeaveNotify";
474 name = "KeymapNotify";
480 name = "GraphicsExpose";
485 case VisibilityNotify:
486 name = "VisibilityNotify";
489 name = "CreateNotify";
492 name = "DestroyNotify";
495 name = "UnmapNotify";
504 name = "ReparentNotify";
506 case ConfigureNotify:
507 name = "ConfigureNotify";
509 case ConfigureRequest:
510 name = "ConfigureRequest";
513 name = "GravityNotify";
516 name = "ResizeRequest";
518 case CirculateNotify:
519 name = "CirculateNotify";
521 case CirculateRequest:
522 name = "CirculateRequest";
525 name = "PropertyNotify";
528 name = "SelectionClear";
530 case SelectionRequest:
531 name = "SelectionRequest";
533 case SelectionNotify:
534 name = "SelectionNotify";
537 name = "ColormapNotify";
540 name = "ClientMessage";
543 name = "MappingNotify";
548 DNPRINTF(SWM_D_EVENTQ ,"window: %lu event: %s (%d), %d "
550 e->xany.window, name, e->type, QLength(display));
552 DNPRINTF(SWM_D_EVENTQ, "window: %lu unknown event %d, %d "
554 e->xany.window, e->type, QLength(display));
558 dumpwins(struct swm_region *r, union arg *args)
562 XWindowAttributes wa;
565 fprintf(stderr, "invalid workspace\n");
569 fprintf(stderr, "=== managed window list ws %02d ===\n", r->ws->idx);
571 TAILQ_FOREACH(win, &r->ws->winlist, entry) {
572 state = getstate(win->id);
573 if (!XGetWindowAttributes(display, win->id, &wa))
574 fprintf(stderr, "window: %lu failed "
575 "XGetWindowAttributes\n", win->id);
576 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
577 win->id, wa.map_state, state);
580 fprintf(stderr, "===== unmanaged window list =====\n");
581 TAILQ_FOREACH(win, &r->ws->unmanagedlist, entry) {
582 state = getstate(win->id);
583 if (!XGetWindowAttributes(display, win->id, &wa))
584 fprintf(stderr, "window: %lu failed "
585 "XGetWindowAttributes\n", win->id);
586 fprintf(stderr, "window: %lu map_state: %d state: %d\n",
587 win->id, wa.map_state, state);
590 fprintf(stderr, "=================================\n");
595 dumpwins(struct swm_region *r, union arg *args)
598 #endif /* SWM_DEBUG */
600 void expose(XEvent *);
601 void keypress(XEvent *);
602 void buttonpress(XEvent *);
603 void configurerequest(XEvent *);
604 void configurenotify(XEvent *);
605 void destroynotify(XEvent *);
606 void enternotify(XEvent *);
607 void focusin(XEvent *);
608 void focusout(XEvent *);
609 void mapnotify(XEvent *);
610 void mappingnotify(XEvent *);
611 void maprequest(XEvent *);
612 void propertynotify(XEvent *);
613 void unmapnotify(XEvent *);
614 void visibilitynotify(XEvent *);
616 void (*handler[LASTEvent])(XEvent *) = {
618 [KeyPress] = keypress,
619 [ButtonPress] = buttonpress,
620 [ConfigureRequest] = configurerequest,
621 [ConfigureNotify] = configurenotify,
622 [DestroyNotify] = destroynotify,
623 [EnterNotify] = enternotify,
625 [FocusOut] = focusout,
626 [MapNotify] = mapnotify,
627 [MappingNotify] = mappingnotify,
628 [MapRequest] = maprequest,
629 [PropertyNotify] = propertynotify,
630 [UnmapNotify] = unmapnotify,
631 [VisibilityNotify] = visibilitynotify,
641 while ((pid = waitpid(WAIT_ANY, NULL, WNOHANG)) != -1) {
642 DNPRINTF(SWM_D_MISC, "reaping: %d\n", pid);
657 installsignal(int sig, char *name)
661 sa.sa_handler = sighdlr;
662 sigemptyset(&sa.sa_mask);
664 if (sigaction(sig, &sa, NULL) == -1)
665 err(1, "could not install %s handler", name);
669 name_to_color(char *colorname)
673 XColor screen_def, exact_def;
674 unsigned long result = 0;
675 char cname[32] = "#";
677 cmap = DefaultColormap(display, screens[0].idx);
678 status = XAllocNamedColor(display, cmap, colorname,
679 &screen_def, &exact_def);
681 strlcat(cname, colorname + 2, sizeof cname - 1);
682 status = XAllocNamedColor(display, cmap, cname, &screen_def,
686 result = screen_def.pixel;
688 fprintf(stderr, "color '%s' not found.\n", colorname);
694 setscreencolor(char *val, int i, int c)
696 if (i > 0 && i <= ScreenCount(display)) {
697 screens[i - 1].c[c].color = name_to_color(val);
698 free(screens[i - 1].c[c].name);
699 if ((screens[i - 1].c[c].name = strdup(val)) == NULL)
701 } else if (i == -1) {
702 for (i = 0; i < ScreenCount(display); i++) {
703 screens[i].c[c].color = name_to_color(val);
704 free(screens[i].c[c].name);
705 if ((screens[i].c[c].name = strdup(val)) == NULL)
709 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
710 i, ScreenCount(display));
714 custom_region(char *val)
716 unsigned int sidx, x, y, w, h;
718 if (sscanf(val, "screen[%u]:%ux%u+%u+%u", &sidx, &w, &h, &x, &y) != 5)
719 errx(1, "invalid custom region, "
720 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
721 if (sidx < 1 || sidx > ScreenCount(display))
722 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
723 sidx, ScreenCount(display));
727 errx(1, "region %ux%u+%u+%u too small\n", w, h, x, y);
729 if (x < 0 || x > DisplayWidth(display, sidx) ||
730 y < 0 || y > DisplayHeight(display, sidx) ||
731 w + x > DisplayWidth(display, sidx) ||
732 h + y > DisplayHeight(display, sidx))
733 errx(1, "region %ux%u+%u+%u not within screen boundaries "
734 "(%ux%u)\n", w, h, x, y,
735 DisplayWidth(display, sidx), DisplayHeight(display, sidx));
737 new_region(&screens[sidx], x, y, w, h);
741 socket_setnonblock(int fd)
745 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
746 err(1, "fcntl F_GETFL");
748 if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
749 err(1, "fcntl F_SETFL");
753 bar_print(struct swm_region *r, char *s)
755 XClearWindow(display, r->bar_window);
756 XSetForeground(display, bar_gc, r->s->c[SWM_S_COLOR_BAR_FONT].color);
757 XDrawString(display, r->bar_window, bar_gc, 4, bar_fs->ascent, s,
766 bzero(bar_pipe, sizeof bar_pipe);
769 kill(bar_pid, SIGTERM);
772 strlcpy(bar_ext, "", sizeof bar_ext);
777 bar_class_name(char *s, ssize_t sz, struct ws_win *cur_focus)
779 int do_class, do_name;
781 XClassHint *xch = NULL;
783 if ((title_name_enabled == 1 || title_class_enabled == 1) &&
785 if ((xch = XAllocClassHint()) == NULL)
787 status = XGetClassHint(display, cur_focus->id, xch);
788 if (status == BadWindow || status == BadAlloc)
790 do_class = (title_class_enabled && xch->res_class != NULL);
791 do_name = (title_name_enabled && xch->res_name != NULL);
793 strlcat(s, xch->res_class, sz);
794 if (do_class && do_name)
797 strlcat(s, xch->res_name, sz);
810 struct swm_region *r;
814 char loc[SWM_BAR_MAX];
818 if (bar_enabled == 0)
820 if (bar_extra && bar_extra_running) {
821 /* ignore short reads; it'll correct itself */
822 while ((b = fgetln(stdin, &len)) != NULL)
823 if (b && b[len - 1] == '\n') {
825 strlcpy(bar_ext, b, sizeof bar_ext);
827 if (b == NULL && errno != EAGAIN) {
828 fprintf(stderr, "bar_extra failed: errno: %d %s\n",
829 errno, strerror(errno));
833 strlcpy(bar_ext, "", sizeof bar_ext);
835 if (clock_enabled == 0)
836 strlcpy(s, "", sizeof s);
839 localtime_r(&tmt, &tm);
840 strftime(s, sizeof s, "%a %b %d %R %Z %Y ", &tm);
843 for (i = 0; i < ScreenCount(display); i++) {
845 TAILQ_FOREACH(r, &screens[i].rl, entry) {
847 bar_class_name(s, sizeof s, r->ws->focus);
850 stack = r->ws->cur_layout->name;
852 snprintf(loc, sizeof loc, "%d:%d %s %s %s %s",
853 x++, r->ws->idx + 1, stack, s, bar_ext,
868 bar_toggle(struct swm_region *r, union arg *args)
870 struct swm_region *tmpr;
871 int i, sc = ScreenCount(display);
873 DNPRINTF(SWM_D_MISC, "bar_toggle\n");
876 for (i = 0; i < sc; i++)
877 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
878 XUnmapWindow(display, tmpr->bar_window);
880 for (i = 0; i < sc; i++)
881 TAILQ_FOREACH(tmpr, &screens[i].rl, entry)
882 XMapRaised(display, tmpr->bar_window);
884 bar_enabled = !bar_enabled;
887 /* must be after stack */
894 XSetWindowAttributes wa;
895 struct swm_region *r;
898 /* do this here because the conf file is in memory */
899 if (bar_extra && bar_extra_running == 0 && bar_argv[0]) {
900 /* launch external status app */
901 bar_extra_running = 1;
902 if (pipe(bar_pipe) == -1)
903 err(1, "pipe error");
904 socket_setnonblock(bar_pipe[0]);
905 socket_setnonblock(bar_pipe[1]); /* XXX hmmm, really? */
906 if (dup2(bar_pipe[0], 0) == -1)
908 if (dup2(bar_pipe[1], 1) == -1)
910 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
911 err(1, "could not disable SIGPIPE");
912 switch (bar_pid = fork()) {
914 err(1, "cannot fork");
918 execvp(bar_argv[0], bar_argv);
919 err(1, "%s external app failed", bar_argv[0]);
921 default: /* parent */
927 bzero(&wa, sizeof wa);
928 for (i = 0; i < ScreenCount(display); i++)
929 TAILQ_FOREACH(r, &screens[i].rl, entry) {
931 screens[i].c[SWM_S_COLOR_BAR_BORDER].color;
932 wa.background_pixel =
933 screens[i].c[SWM_S_COLOR_BAR].color;
934 XChangeWindowAttributes(display, r->bar_window,
935 CWBackPixel | CWBorderPixel, &wa);
941 bar_setup(struct swm_region *r)
945 for (i = 0; bar_fonts[i] != NULL; i++) {
946 bar_fs = XLoadQueryFont(display, bar_fonts[i]);
952 if (bar_fonts[i] == NULL)
953 errx(1, "couldn't load font");
954 bar_height = bar_fs->ascent + bar_fs->descent + 3;
956 r->bar_window = XCreateSimpleWindow(display,
957 r->s->root, X(r), Y(r), WIDTH(r) - 2, bar_height - 2,
958 1, r->s->c[SWM_S_COLOR_BAR_BORDER].color,
959 r->s->c[SWM_S_COLOR_BAR].color);
960 bar_gc = XCreateGC(display, r->bar_window, 0, &bar_gcv);
961 XSetFont(display, bar_gc, bar_fs->fid);
962 XSelectInput(display, r->bar_window, VisibilityChangeMask);
964 XMapRaised(display, r->bar_window);
965 DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %lu\n", r->bar_window);
967 if (signal(SIGALRM, bar_signal) == SIG_ERR)
968 err(1, "could not install bar_signal");
973 set_win_state(struct ws_win *win, long state)
975 long data[] = {state, None};
977 DNPRINTF(SWM_D_EVENT, "set_win_state: window: %lu\n", win->id);
982 XChangeProperty(display, win->id, astate, astate, 32, PropModeReplace,
983 (unsigned char *)data, 2);
991 unsigned char *p = NULL;
992 unsigned long n, extra;
995 status = XGetWindowProperty(display, w, astate, 0L, 2L, False, astate,
996 &real, &format, &n, &extra, (unsigned char **)&p);
997 if (status != Success)
1000 result = *((long *)p);
1006 version(struct swm_region *r, union arg *args)
1008 bar_version = !bar_version;
1010 snprintf(bar_vertext, sizeof bar_vertext, "Version: %s CVS: %s",
1011 SWM_VERSION, cvstag);
1013 strlcpy(bar_vertext, "", sizeof bar_vertext);
1018 client_msg(struct ws_win *win, Atom a)
1020 XClientMessageEvent cm;
1025 bzero(&cm, sizeof cm);
1026 cm.type = ClientMessage;
1027 cm.window = win->id;
1028 cm.message_type = aprot;
1031 cm.data.l[1] = CurrentTime;
1032 XSendEvent(display, win->id, False, 0L, (XEvent *)&cm);
1036 configreq_win(struct ws_win *win)
1038 XConfigureRequestEvent cr;
1043 bzero(&cr, sizeof cr);
1044 cr.type = ConfigureRequest;
1045 cr.display = display;
1046 cr.parent = win->id;
1047 cr.window = win->id;
1050 cr.width = win->g.w;
1051 cr.height = win->g.h;
1052 cr.border_width = 1;
1054 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&cr);
1058 config_win(struct ws_win *win)
1062 DNPRINTF(SWM_D_MISC, "config_win: win %lu x %d y %d w %d h %d\n",
1063 win->id, win->g.x, win->g.y, win->g.w, win->g.h);
1068 ce.type = ConfigureNotify;
1069 ce.display = display;
1071 ce.window = win->id;
1074 ce.width = win->g.w;
1075 ce.height = win->g.h;
1076 ce.border_width = 1; /* XXX store this! */
1078 ce.override_redirect = False;
1079 XSendEvent(display, win->id, False, StructureNotifyMask, (XEvent *)&ce);
1083 count_win(struct workspace *ws, int count_transient)
1088 TAILQ_FOREACH(win, &ws->winlist, entry) {
1089 if (count_transient == 0 && win->floating)
1091 if (count_transient == 0 && win->transient)
1095 DNPRINTF(SWM_D_MISC, "count_win: %d\n", count);
1101 quit(struct swm_region *r, union arg *args)
1103 DNPRINTF(SWM_D_MISC, "quit\n");
1108 unmap_window(struct ws_win *win)
1113 /* don't unmap again */
1114 if (getstate(win->id) == IconicState)
1117 set_win_state(win, IconicState);
1119 XUnmapWindow(display, win->id);
1128 for (i = 0; i < ScreenCount(display); i++)
1129 for (j = 0; j < SWM_WS_MAX; j++)
1130 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1135 fake_keypress(struct ws_win *win, int keysym, int modifiers)
1142 event.display = display; /* Ignored, but what the hell */
1143 event.window = win->id;
1144 event.root = win->s->root;
1145 event.subwindow = None;
1146 event.time = CurrentTime;
1151 event.same_screen = True;
1152 event.keycode = XKeysymToKeycode(display, keysym);
1153 event.state = modifiers;
1155 event.type = KeyPress;
1156 XSendEvent(event.display, event.window, True,
1157 KeyPressMask, (XEvent *)&event);
1159 event.type = KeyRelease;
1160 XSendEvent(event.display, event.window, True,
1161 KeyPressMask, (XEvent *)&event);
1166 restart(struct swm_region *r, union arg *args)
1168 DNPRINTF(SWM_D_MISC, "restart: %s\n", start_argv[0]);
1170 /* disable alarm because the following code may not be interrupted */
1172 if (signal(SIGALRM, SIG_IGN) == SIG_ERR)
1173 errx(1, "can't disable alarm");
1178 XCloseDisplay(display);
1179 execvp(start_argv[0], start_argv);
1180 fprintf(stderr, "execvp failed\n");
1186 root_to_region(Window root)
1188 struct swm_region *r = NULL;
1190 int i, x, y, wx, wy;
1193 for (i = 0; i < ScreenCount(display); i++)
1194 if (screens[i].root == root)
1197 if (XQueryPointer(display, screens[i].root,
1198 &rr, &cr, &x, &y, &wx, &wy, &mask) != False) {
1199 /* choose a region based on pointer location */
1200 TAILQ_FOREACH(r, &screens[i].rl, entry)
1201 if (x >= X(r) && x <= X(r) + WIDTH(r) &&
1202 y >= Y(r) && y <= Y(r) + HEIGHT(r))
1207 r = TAILQ_FIRST(&screens[i].rl);
1213 find_unmanaged_window(Window id)
1218 for (i = 0; i < ScreenCount(display); i++)
1219 for (j = 0; j < SWM_WS_MAX; j++)
1220 TAILQ_FOREACH(win, &screens[i].ws[j].unmanagedlist,
1228 find_window(Window id)
1233 for (i = 0; i < ScreenCount(display); i++)
1234 for (j = 0; j < SWM_WS_MAX; j++)
1235 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1242 spawn(struct swm_region *r, union arg *args)
1247 DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]);
1249 * The double-fork construct avoids zombie processes and keeps the code
1250 * clean from stupid signal handlers.
1255 close(ConnectionNumber(display));
1256 setenv("LD_PRELOAD", SWM_LIB, 1);
1257 if (asprintf(&ret, "%d", r->ws->idx)) {
1258 setenv("_SWM_WS", ret, 1);
1261 if (asprintf(&ret, "%d", getpid())) {
1262 setenv("_SWM_PID", ret, 1);
1266 /* kill stdin, mplayer, ssh-add etc. need that */
1267 si = open("/dev/null", O_RDONLY, 0);
1269 err(1, "open /dev/null");
1270 if (dup2(si, 0) == -1)
1271 err(1, "dup2 /dev/null");
1272 execvp(args->argv[0], args->argv);
1273 fprintf(stderr, "execvp failed\n");
1281 spawnterm(struct swm_region *r, union arg *args)
1283 DNPRINTF(SWM_D_MISC, "spawnterm\n");
1286 setenv("_SWM_XTERM_FONTADJ", "", 1);
1291 kill_refs(struct ws_win *win)
1294 struct swm_region *r;
1295 struct workspace *ws;
1300 for (i = 0; i < ScreenCount(display); i++)
1301 TAILQ_FOREACH(r, &screens[i].rl, entry)
1302 for (x = 0; x < SWM_WS_MAX; x++) {
1304 if (win == ws->focus)
1306 if (win == ws->focus_prev)
1307 ws->focus_prev = NULL;
1312 validate_win(struct ws_win *testwin)
1315 struct workspace *ws;
1316 struct swm_region *r;
1317 int i, x, foundit = 0;
1319 if (testwin == NULL)
1322 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1323 TAILQ_FOREACH(r, &screens[i].rl, entry)
1324 for (x = 0; x < SWM_WS_MAX; x++) {
1326 TAILQ_FOREACH(win, &ws->winlist, entry)
1334 validate_ws(struct workspace *testws)
1336 struct swm_region *r;
1337 struct workspace *ws;
1340 /* validate all ws */
1341 for (i = 0, foundit = 0; i < ScreenCount(display); i++)
1342 TAILQ_FOREACH(r, &screens[i].rl, entry)
1343 for (x = 0; x < SWM_WS_MAX; x++) {
1352 unfocus_win(struct ws_win *win)
1356 if (win->ws == NULL)
1359 if (validate_ws(win->ws))
1362 if (win->ws->r == NULL)
1365 if (validate_win(win)) {
1370 if (win->ws->focus == win) {
1371 win->ws->focus = NULL;
1372 win->ws->focus_prev = win;
1375 if (validate_win(win->ws->focus)) {
1376 kill_refs(win->ws->focus);
1377 win->ws->focus = NULL;
1379 if (validate_win(win->ws->focus_prev)) {
1380 kill_refs(win->ws->focus_prev);
1381 win->ws->focus_prev = NULL;
1384 grabbuttons(win, 0);
1385 XSetWindowBorder(display, win->id,
1386 win->ws->r->s->c[SWM_S_COLOR_UNFOCUS].color);
1395 DNPRINTF(SWM_D_FOCUS, "unfocus_all:\n");
1397 for (i = 0; i < ScreenCount(display); i++)
1398 for (j = 0; j < SWM_WS_MAX; j++)
1399 TAILQ_FOREACH(win, &screens[i].ws[j].winlist, entry)
1404 focus_win(struct ws_win *win)
1406 DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win ? win->id : 0);
1410 if (win->ws == NULL)
1413 if (validate_ws(win->ws))
1415 if (validate_win(win)) {
1420 /* use big hammer to make sure it works under all use cases */
1423 if (validate_win(win)) {
1428 win->ws->focus = win;
1430 if (win->ws->r != NULL) {
1431 grabbuttons(win, 1);
1432 if (win->ws->cur_layout->flags & SWM_L_MAPONFOCUS)
1433 XMapRaised(display, win->id);
1434 XSetWindowBorder(display, win->id,
1435 win->ws->r->s->c[SWM_S_COLOR_FOCUS].color);
1437 XSetInputFocus(display, win->id,
1438 RevertToPointerRoot, CurrentTime);
1443 switchws(struct swm_region *r, union arg *args)
1445 int wsid = args->id;
1446 struct swm_region *this_r, *other_r;
1448 struct workspace *new_ws, *old_ws;
1455 old_ws = this_r->ws;
1456 new_ws = &this_r->s->ws[wsid];
1458 DNPRINTF(SWM_D_WS, "switchws screen[%d]:%dx%d+%d+%d: "
1459 "%d -> %d\n", r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r),
1462 if (new_ws == NULL || old_ws == NULL)
1464 if (new_ws == old_ws)
1467 other_r = new_ws->r;
1468 if (other_r == NULL) {
1469 /* if the other workspace is hidden, switch windows */
1470 if (old_ws->r != NULL)
1471 old_ws->old_r = old_ws->r;
1474 TAILQ_FOREACH(win, &old_ws->winlist, entry)
1477 other_r->ws = old_ws;
1478 old_ws->r = other_r;
1480 this_r->ws = new_ws;
1484 a.id = SWM_ARG_ID_FOCUSCUR;
1485 focus(new_ws->r, &a);
1490 cyclews(struct swm_region *r, union arg *args)
1493 struct swm_screen *s = r->s;
1495 DNPRINTF(SWM_D_WS, "cyclews id %d "
1496 "in screen[%d]:%dx%d+%d+%d ws %d\n", args->id,
1497 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1502 case SWM_ARG_ID_CYCLEWS_UP:
1503 if (a.id < SWM_WS_MAX - 1)
1508 case SWM_ARG_ID_CYCLEWS_DOWN:
1512 a.id = SWM_WS_MAX - 1;
1518 if (cycle_empty == 0 && TAILQ_EMPTY(&s->ws[a.id].winlist))
1520 if (cycle_visible == 0 && s->ws[a.id].r != NULL)
1524 } while (a.id != r->ws->idx);
1528 cyclescr(struct swm_region *r, union arg *args)
1530 struct swm_region *rr = NULL;
1534 /* do nothing if we don't have more than one screen */
1535 if (!(ScreenCount(display) > 1 || outputs > 1))
1540 case SWM_ARG_ID_CYCLESC_UP:
1541 rr = TAILQ_NEXT(r, entry);
1543 rr = TAILQ_FIRST(&screens[i].rl);
1545 case SWM_ARG_ID_CYCLESC_DOWN:
1546 rr = TAILQ_PREV(r, swm_region_list, entry);
1548 rr = TAILQ_LAST(&screens[i].rl, swm_region_list);
1556 /* move mouse to region */
1558 y = rr->g.y + 1 + bar_enabled ? bar_height : 0;
1559 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1561 a.id = SWM_ARG_ID_FOCUSCUR;
1564 if (rr->ws->focus) {
1565 /* move to focus window */
1566 x = rr->ws->focus->g.x + 1;
1567 y = rr->ws->focus->g.y + 1;
1568 XWarpPointer(display, None, rr->s[i].root, 0, 0, 0, 0, x, y);
1573 swapwin(struct swm_region *r, union arg *args)
1575 struct ws_win *target, *source;
1576 struct ws_win *cur_focus;
1577 struct ws_win_list *wl;
1580 DNPRINTF(SWM_D_WS, "swapwin id %d "
1581 "in screen %d region %dx%d+%d+%d ws %d\n", args->id,
1582 r->s->idx, WIDTH(r), HEIGHT(r), X(r), Y(r), r->ws->idx);
1584 cur_focus = r->ws->focus;
1585 if (cur_focus == NULL)
1589 wl = &source->ws->winlist;
1592 case SWM_ARG_ID_SWAPPREV:
1593 target = TAILQ_PREV(source, ws_win_list, entry);
1594 TAILQ_REMOVE(wl, cur_focus, entry);
1596 TAILQ_INSERT_TAIL(wl, source, entry);
1598 TAILQ_INSERT_BEFORE(target, source, entry);
1600 case SWM_ARG_ID_SWAPNEXT:
1601 target = TAILQ_NEXT(source, entry);
1602 TAILQ_REMOVE(wl, source, entry);
1604 TAILQ_INSERT_HEAD(wl, source, entry);
1606 TAILQ_INSERT_AFTER(wl, target, source, entry);
1608 case SWM_ARG_ID_SWAPMAIN:
1609 target = TAILQ_FIRST(wl);
1610 if (target == source) {
1611 if (source->ws->focus_prev != NULL &&
1612 source->ws->focus_prev != target)
1614 source = source->ws->focus_prev;
1618 if (target == NULL || source == NULL)
1620 source->ws->focus_prev = target;
1621 TAILQ_REMOVE(wl, target, entry);
1622 TAILQ_INSERT_BEFORE(source, target, entry);
1623 TAILQ_REMOVE(wl, source, entry);
1624 TAILQ_INSERT_HEAD(wl, source, entry);
1627 DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id);
1635 focus(struct swm_region *r, union arg *args)
1637 struct ws_win *winfocus = NULL, *winlostfocus = NULL;
1638 struct ws_win *cur_focus = NULL;
1639 struct ws_win_list *wl = NULL;
1640 struct workspace *ws = NULL;
1645 DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id);
1647 /* treat FOCUS_CUR special */
1648 if (args->id == SWM_ARG_ID_FOCUSCUR) {
1650 winfocus = r->ws->focus;
1651 else if (r->ws->focus_prev)
1652 winfocus = r->ws->focus_prev;
1654 winfocus = TAILQ_FIRST(&r->ws->winlist);
1656 focus_magic(winfocus, 0);
1660 if (r->ws->focus == NULL)
1663 cur_focus = r->ws->focus;
1667 winlostfocus = cur_focus;
1670 case SWM_ARG_ID_FOCUSPREV:
1671 if (cur_focus->transient)
1672 winfocus = find_window(cur_focus->transient);
1673 else if (ws->focus == cur_focus) {
1674 /* if in max_stack try harder */
1675 if (ws->cur_layout->flags & SWM_L_FOCUSPREV) {
1676 if (cur_focus != ws->focus_prev)
1677 winfocus = ws->focus_prev;
1678 else if (cur_focus != ws->focus)
1679 winfocus = ws->focus;
1682 /* fallback and normal handling */
1683 if (winfocus == NULL) {
1684 if (TAILQ_FIRST(wl) == cur_focus)
1685 winfocus = TAILQ_NEXT(cur_focus, entry);
1687 winfocus = TAILQ_PREV(ws->focus,
1688 ws_win_list, entry);
1689 if (winfocus == NULL)
1690 winfocus = TAILQ_LAST(wl,
1696 case SWM_ARG_ID_FOCUSNEXT:
1697 winfocus = TAILQ_NEXT(cur_focus, entry);
1698 if (winfocus == NULL)
1699 winfocus = TAILQ_FIRST(wl);
1702 case SWM_ARG_ID_FOCUSMAIN:
1703 winfocus = TAILQ_FIRST(wl);
1704 if (winfocus == cur_focus)
1705 winfocus = cur_focus->ws->focus_prev;
1706 if (winfocus == NULL)
1714 if (winfocus == winlostfocus || winfocus == NULL)
1717 focus_magic(winfocus, 0);
1721 cycle_layout(struct swm_region *r, union arg *args)
1723 struct workspace *ws = r->ws;
1724 struct ws_win *winfocus;
1727 DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", ws->idx);
1729 winfocus = ws->focus;
1732 if (ws->cur_layout->l_stack == NULL)
1733 ws->cur_layout = &layouts[0];
1736 a.id = SWM_ARG_ID_FOCUSCUR;
1741 stack_config(struct swm_region *r, union arg *args)
1743 struct workspace *ws = r->ws;
1745 DNPRINTF(SWM_D_STACK, "stack_config for workspace %d (id %d\n",
1748 if (ws->cur_layout->l_config != NULL)
1749 ws->cur_layout->l_config(ws, args->id);
1751 if (args->id != SWM_ARG_ID_STACKINIT);
1757 struct swm_geometry g;
1758 struct swm_region *r;
1761 DNPRINTF(SWM_D_STACK, "stack\n");
1763 for (i = 0; i < ScreenCount(display); i++) {
1765 TAILQ_FOREACH(r, &screens[i].rl, entry) {
1766 DNPRINTF(SWM_D_STACK, "stacking workspace %d "
1767 "(screen %d, region %d)\n", r->ws->idx, i, j++);
1769 /* start with screen geometry, adjust for bar */
1777 r->ws->cur_layout->l_stack(r->ws, &g);
1785 stack_floater(struct ws_win *win, struct swm_region *r)
1793 bzero(&wc, sizeof wc);
1794 mask = CWX | CWY | CWBorderWidth | CWWidth | CWHeight;
1795 if ((win->quirks & SWM_Q_FULLSCREEN) && (win->g.w >= WIDTH(r)) &&
1796 (win->g.h >= HEIGHT(r)))
1797 wc.border_width = 0;
1799 wc.border_width = 1;
1800 if (win->transient && (win->quirks & SWM_Q_TRANSSZ)) {
1801 win->g.w = (double)WIDTH(r) * dialog_ratio;
1802 win->g.h = (double)HEIGHT(r) * dialog_ratio;
1804 wc.width = win->g.w;
1805 wc.height = win->g.h;
1810 wc.x = (WIDTH(r) - win->g.w) / 2;
1811 wc.y = (HEIGHT(r) - win->g.h) / 2;
1814 /* adjust for region */
1822 win->g.w = wc.width;
1823 win->g.h = wc.height;
1825 DNPRINTF(SWM_D_MISC, "stack_floater: win %lu x %d y %d w %d h %d\n",
1826 win->id, wc.x, wc.y, wc.width, wc.height);
1828 XConfigureWindow(display, win->id, mask, &wc);
1833 * Send keystrokes to terminal to decrease/increase the font size as the
1834 * window size changes.
1837 adjust_font(struct ws_win *win)
1839 if (!(win->quirks & SWM_Q_XTERM_FONTADJ) ||
1840 win->floating || win->transient)
1843 if (win->sh.width_inc && win->last_inc != win->sh.width_inc &&
1844 win->g.w / win->sh.width_inc < term_width &&
1845 win->font_steps < SWM_MAX_FONT_STEPS) {
1846 win->font_size_boundary[win->font_steps] =
1847 (win->sh.width_inc * term_width) + win->sh.base_width;
1850 win->last_inc = win->sh.width_inc;
1851 fake_keypress(win, XK_KP_Subtract, ShiftMask);
1852 } else if (win->font_steps && win->last_inc != win->sh.width_inc &&
1853 win->g.w > win->font_size_boundary[win->font_steps - 1]) {
1856 win->last_inc = win->sh.width_inc;
1857 fake_keypress(win, XK_KP_Add, ShiftMask);
1861 #define SWAPXY(g) do { \
1863 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1864 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1867 stack_master(struct workspace *ws, struct swm_geometry *g, int rot, int flip)
1870 struct swm_geometry win_g, r_g = *g;
1872 int i, j, s, stacks;
1873 int w_inc = 1, h_inc, w_base = 1, h_base;
1874 int hrh, extra = 0, h_slice, last_h = 0;
1875 int split, colno, winno, mwin, msize, mscale;
1876 int remain, missing, v_slice, reconfigure;
1879 DNPRINTF(SWM_D_STACK, "stack_master: workspace: %d\n rot=%s flip=%s",
1880 ws->idx, rot ? "yes" : "no", flip ? "yes" : "no");
1882 winno = count_win(ws, 0);
1883 if (winno == 0 && count_win(ws, 1) == 0)
1886 TAILQ_FOREACH(win, &ws->winlist, entry)
1887 if (win->transient == 0 && win->floating == 0)
1894 w_inc = win->sh.width_inc;
1895 w_base = win->sh.base_width;
1896 mwin = ws->l_state.horizontal_mwin;
1897 mscale = ws->l_state.horizontal_msize;
1898 stacks = ws->l_state.horizontal_stacks;
1901 w_inc = win->sh.height_inc;
1902 w_base = win->sh.base_height;
1903 mwin = ws->l_state.vertical_mwin;
1904 mscale = ws->l_state.vertical_msize;
1905 stacks = ws->l_state.vertical_stacks;
1909 if (stacks > winno - mwin)
1910 stacks = winno - mwin;
1914 h_slice = r_g.h / SWM_H_SLICE;
1915 if (mwin && winno > mwin) {
1916 v_slice = r_g.w / SWM_V_SLICE;
1920 win_g.w = v_slice * mscale;
1922 if (w_inc > 1 && w_inc < v_slice) {
1923 /* adjust for window's requested size increment */
1924 remain = (win_g.w - w_base) % w_inc;
1925 missing = w_inc - remain;
1932 win_g.x += r_g.w - msize;
1935 colno = split = winno / stacks;
1936 win_g.w = ((r_g.w - (stacks * 2) + 2) / stacks);
1938 hrh = r_g.h / colno;
1939 extra = r_g.h - (colno * hrh);
1942 /* stack all the tiled windows */
1943 i = j = 0, s = stacks;
1944 TAILQ_FOREACH(win, &ws->winlist, entry) {
1945 if (win->transient != 0 || win->floating != 0)
1948 if (split && i == split) {
1949 colno = (winno - mwin) / stacks;
1950 if (s <= (winno - mwin) % stacks)
1952 split = split + colno;
1953 hrh = (r_g.h / colno);
1954 extra = r_g.h - (colno * hrh);
1958 win_g.x += win_g.w + 2;
1959 win_g.w = (r_g.w - msize - (stacks * 2)) / stacks;
1961 win_g.w += (r_g.w - msize - (stacks * 2)) %
1968 h_inc = win->sh.width_inc;
1969 h_base = win->sh.base_width;
1971 h_inc = win->sh.height_inc;
1972 h_base = win->sh.base_height;
1974 if (j == colno - 1) {
1975 win_g.h = hrh + extra;
1976 } else if (h_inc > 1 && h_inc < h_slice) {
1977 /* adjust for window's requested size increment */
1978 remain = (win_g.h - h_base) % h_inc;
1979 missing = h_inc - remain;
1981 if (missing <= extra || j == 0) {
1993 win_g.y += last_h + 2;
1995 bzero(&wc, sizeof wc);
1996 wc.border_width = 1;
1999 if (win->g.x != win_g.y || win->g.y != win_g.x ||
2000 win->g.w != win_g.h || win->g.h != win_g.w) {
2002 win->g.x = wc.x = win_g.y;
2003 win->g.y = wc.y = win_g.x;
2004 win->g.w = wc.width = win_g.h;
2005 win->g.h = wc.height = win_g.w;
2008 if (win->g.x != win_g.x || win->g.y != win_g.y ||
2009 win->g.w != win_g.w || win->g.h != win_g.h) {
2011 win->g.x = wc.x = win_g.x;
2012 win->g.y = wc.y = win_g.y;
2013 win->g.w = wc.width = win_g.w;
2014 win->g.h = wc.height = win_g.h;
2019 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2020 XConfigureWindow(display, win->id, mask, &wc);
2023 XMapRaised(display, win->id);
2031 /* now, stack all the floaters and transients */
2032 TAILQ_FOREACH(win, &ws->winlist, entry) {
2033 if (win->transient == 0 && win->floating == 0)
2036 stack_floater(win, ws->r);
2037 XMapRaised(display, win->id);
2042 vertical_config(struct workspace *ws, int id)
2044 DNPRINTF(SWM_D_STACK, "vertical_resize: workspace: %d\n", ws->idx);
2047 case SWM_ARG_ID_STACKRESET:
2048 case SWM_ARG_ID_STACKINIT:
2049 ws->l_state.vertical_msize = SWM_V_SLICE / 2;
2050 ws->l_state.vertical_mwin = 1;
2051 ws->l_state.vertical_stacks = 1;
2053 case SWM_ARG_ID_MASTERSHRINK:
2054 if (ws->l_state.vertical_msize > 1)
2055 ws->l_state.vertical_msize--;
2057 case SWM_ARG_ID_MASTERGROW:
2058 if (ws->l_state.vertical_msize < SWM_V_SLICE - 1)
2059 ws->l_state.vertical_msize++;
2061 case SWM_ARG_ID_MASTERADD:
2062 ws->l_state.vertical_mwin++;
2064 case SWM_ARG_ID_MASTERDEL:
2065 if (ws->l_state.vertical_mwin > 0)
2066 ws->l_state.vertical_mwin--;
2068 case SWM_ARG_ID_STACKINC:
2069 ws->l_state.vertical_stacks++;
2071 case SWM_ARG_ID_STACKDEC:
2072 if (ws->l_state.vertical_stacks > 1)
2073 ws->l_state.vertical_stacks--;
2081 vertical_stack(struct workspace *ws, struct swm_geometry *g)
2083 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2085 stack_master(ws, g, 0, 0);
2089 horizontal_config(struct workspace *ws, int id)
2091 DNPRINTF(SWM_D_STACK, "horizontal_config: workspace: %d\n", ws->idx);
2094 case SWM_ARG_ID_STACKRESET:
2095 case SWM_ARG_ID_STACKINIT:
2096 ws->l_state.horizontal_mwin = 1;
2097 ws->l_state.horizontal_msize = SWM_H_SLICE / 2;
2098 ws->l_state.horizontal_stacks = 1;
2100 case SWM_ARG_ID_MASTERSHRINK:
2101 if (ws->l_state.horizontal_msize > 1)
2102 ws->l_state.horizontal_msize--;
2104 case SWM_ARG_ID_MASTERGROW:
2105 if (ws->l_state.horizontal_msize < SWM_H_SLICE - 1)
2106 ws->l_state.horizontal_msize++;
2108 case SWM_ARG_ID_MASTERADD:
2109 ws->l_state.horizontal_mwin++;
2111 case SWM_ARG_ID_MASTERDEL:
2112 if (ws->l_state.horizontal_mwin > 0)
2113 ws->l_state.horizontal_mwin--;
2115 case SWM_ARG_ID_STACKINC:
2116 ws->l_state.horizontal_stacks++;
2118 case SWM_ARG_ID_STACKDEC:
2119 if (ws->l_state.horizontal_stacks > 1)
2120 ws->l_state.horizontal_stacks--;
2128 horizontal_stack(struct workspace *ws, struct swm_geometry *g)
2130 DNPRINTF(SWM_D_STACK, "vertical_stack: workspace: %d\n", ws->idx);
2132 stack_master(ws, g, 1, 0);
2135 /* fullscreen view */
2137 max_stack(struct workspace *ws, struct swm_geometry *g)
2140 struct swm_geometry gg = *g;
2141 struct ws_win *win, *wintrans = NULL, *parent = NULL;
2145 DNPRINTF(SWM_D_STACK, "max_stack: workspace: %d\n", ws->idx);
2150 winno = count_win(ws, 0);
2151 if (winno == 0 && count_win(ws, 1) == 0)
2154 TAILQ_FOREACH(win, &ws->winlist, entry) {
2155 if (win->transient) {
2157 parent = find_window(win->transient);
2161 /* only reconfigure if necessary */
2162 if (win->g.x != gg.x || win->g.y != gg.y || win->g.w != gg.w ||
2164 bzero(&wc, sizeof wc);
2165 wc.border_width = 1;
2166 win->g.x = wc.x = gg.x;
2167 win->g.y = wc.y = gg.y;
2168 win->g.w = wc.width = gg.w;
2169 win->g.h = wc.height = gg.h;
2170 mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
2171 XConfigureWindow(display, win->id, mask, &wc);
2174 /* unmap only if we don't have multi screen */
2175 if (win != ws->focus)
2176 if (!(ScreenCount(display) > 1 || outputs > 1))
2180 /* put the last transient on top */
2183 XMapRaised(display, parent->id);
2184 stack_floater(wintrans, ws->r);
2185 focus_magic(wintrans, 1);
2190 send_to_ws(struct swm_region *r, union arg *args)
2192 int wsid = args->id;
2193 struct ws_win *win = win;
2194 struct workspace *ws, *nws;
2195 Atom ws_idx_atom = 0;
2196 unsigned char ws_idx_str[SWM_PROPLEN];
2206 DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id);
2209 nws = &win->s->ws[wsid];
2211 a.id = SWM_ARG_ID_FOCUSPREV;
2214 TAILQ_REMOVE(&ws->winlist, win, entry);
2215 TAILQ_INSERT_TAIL(&nws->winlist, win, entry);
2218 /* Try to update the window's workspace property */
2219 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
2221 snprintf(ws_idx_str, SWM_PROPLEN, "%d", nws->idx) < SWM_PROPLEN) {
2222 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
2224 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
2225 PropModeReplace, ws_idx_str, SWM_PROPLEN);
2231 wkill(struct swm_region *r, union arg *args)
2233 DNPRINTF(SWM_D_MISC, "wkill %d\n", args->id);
2235 if(r->ws->focus == NULL)
2238 if (args->id == SWM_ARG_ID_KILLWINDOW)
2239 XKillClient(display, r->ws->focus->id);
2241 if (r->ws->focus->can_delete)
2242 client_msg(r->ws->focus, adelete);
2246 floating_toggle(struct swm_region *r, union arg *args)
2248 struct ws_win *win = r->ws->focus;
2254 win->floating = !win->floating;
2257 a.id = SWM_ARG_ID_FOCUSCUR;
2258 focus(win->ws->r, &a);
2262 resize_window(struct ws_win *win, int center)
2266 struct swm_region *r;
2268 r = root_to_region(win->wa.root);
2269 bzero(&wc, sizeof wc);
2270 mask = CWBorderWidth | CWWidth | CWHeight;
2271 wc.border_width = 1;
2272 wc.width = win->g.w;
2273 wc.height = win->g.h;
2274 if (center == SWM_ARG_ID_CENTER) {
2275 wc.x = (WIDTH(r) - win->g.w) / 2;
2276 wc.y = (HEIGHT(r) - win->g.h) / 2;
2280 DNPRINTF(SWM_D_STACK, "resize_window: win %lu x %d y %d w %d h %d\n",
2281 win->id, wc.x, wc.y, wc.width, wc.height);
2283 XConfigureWindow(display, win->id, mask, &wc);
2288 resize(struct ws_win *win, union arg *args)
2292 struct swm_region *r = win->ws->r;
2294 DNPRINTF(SWM_D_MOUSE, "resize: win %lu floating %d trans %lu\n",
2295 win->id, win->floating, win->transient);
2297 if (!(win->transient != 0 || win->floating != 0))
2300 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2301 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2303 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w, win->g.h);
2305 XMaskEvent(display, MOUSEMASK | ExposureMask |
2306 SubstructureRedirectMask, &ev);
2308 case ConfigureRequest:
2311 handler[ev.type](&ev);
2314 /* do not allow resize outside of region */
2315 if (ev.xmotion.y_root < r->g.y ||
2316 ev.xmotion.y_root >= r->g.y + r->g.h - 1)
2318 if (ev.xmotion.x_root < r->g.x ||
2319 ev.xmotion.x_root >= r->g.x + r->g.w - 1)
2322 if (ev.xmotion.x <= 1)
2324 if (ev.xmotion.y <= 1)
2326 win->g.w = ev.xmotion.x;
2327 win->g.h = ev.xmotion.y;
2329 /* not free, don't sync more than 60 times / second */
2330 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2331 time = ev.xmotion.time;
2332 XSync(display, False);
2333 resize_window(win, args->id);
2337 } while (ev.type != ButtonRelease);
2339 XSync(display, False);
2340 resize_window(win, args->id);
2342 XWarpPointer(display, None, win->id, 0, 0, 0, 0, win->g.w - 1,
2344 XUngrabPointer(display, CurrentTime);
2347 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2351 move_window(struct ws_win *win)
2355 struct swm_region *r;
2357 r = root_to_region(win->wa.root);
2358 bzero(&wc, sizeof wc);
2363 DNPRINTF(SWM_D_STACK, "move_window: win %lu x %d y %d w %d h %d\n",
2364 win->id, wc.x, wc.y, wc.width, wc.height);
2366 XConfigureWindow(display, win->id, mask, &wc);
2371 move(struct ws_win *win, union arg *args)
2376 struct swm_region *r = win->ws->r;
2378 DNPRINTF(SWM_D_MOUSE, "move: win %lu floating %d trans %lu\n",
2379 win->id, win->floating, win->transient);
2381 if (win->floating == 0) {
2387 if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
2388 GrabModeAsync, None, None /* cursor */, CurrentTime) != GrabSuccess)
2390 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2392 XMaskEvent(display, MOUSEMASK | ExposureMask |
2393 SubstructureRedirectMask, &ev);
2395 case ConfigureRequest:
2398 handler[ev.type](&ev);
2401 /* don't allow to move window out of region */
2402 if (ev.xmotion.y_root < r->g.y ||
2403 ev.xmotion.y_root + win->g.h >= r->g.y + r->g.h - 1)
2405 if (ev.xmotion.x_root < r->g.x ||
2406 ev.xmotion.x_root + win->g.w >= r->g.x + r->g.w - 1)
2409 win->g.x = ev.xmotion.x_root;
2410 win->g.y = ev.xmotion.y_root;
2412 /* not free, don't sync more than 60 times / second */
2413 if ((ev.xmotion.time - time) > (1000 / 60) ) {
2414 time = ev.xmotion.time;
2415 XSync(display, False);
2420 } while (ev.type != ButtonRelease);
2422 XSync(display, False);
2425 XWarpPointer(display, None, win->id, 0, 0, 0, 0, 0, 0);
2426 XUngrabPointer(display, CurrentTime);
2431 while (XCheckMaskEvent(display, EnterWindowMask, &ev));
2434 /* key definitions */
2436 dummykeyfunc(struct swm_region *r, union arg *args)
2441 legacyfunc(struct swm_region *r, union arg *args)
2446 char name[SWM_FUNCNAME_LEN];
2447 void (*func)(struct swm_region *r, union arg *);
2449 } keyfuncs[kf_invalid + 1] = {
2450 /* name function argument */
2451 { "cycle_layout", cycle_layout, {0} },
2452 { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
2453 { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
2454 { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
2455 { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
2456 { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
2457 { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
2458 { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
2459 { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
2460 { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
2461 { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
2462 { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
2463 { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
2464 { "spawn_term", spawnterm, {.argv = spawn_term} },
2465 { "spawn_menu", legacyfunc, {0} },
2466 { "quit", quit, {0} },
2467 { "restart", restart, {0} },
2468 { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
2469 { "ws_1", switchws, {.id = 0} },
2470 { "ws_2", switchws, {.id = 1} },
2471 { "ws_3", switchws, {.id = 2} },
2472 { "ws_4", switchws, {.id = 3} },
2473 { "ws_5", switchws, {.id = 4} },
2474 { "ws_6", switchws, {.id = 5} },
2475 { "ws_7", switchws, {.id = 6} },
2476 { "ws_8", switchws, {.id = 7} },
2477 { "ws_9", switchws, {.id = 8} },
2478 { "ws_10", switchws, {.id = 9} },
2479 { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
2480 { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
2481 { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
2482 { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
2483 { "mvws_1", send_to_ws, {.id = 0} },
2484 { "mvws_2", send_to_ws, {.id = 1} },
2485 { "mvws_3", send_to_ws, {.id = 2} },
2486 { "mvws_4", send_to_ws, {.id = 3} },
2487 { "mvws_5", send_to_ws, {.id = 4} },
2488 { "mvws_6", send_to_ws, {.id = 5} },
2489 { "mvws_7", send_to_ws, {.id = 6} },
2490 { "mvws_8", send_to_ws, {.id = 7} },
2491 { "mvws_9", send_to_ws, {.id = 8} },
2492 { "mvws_10", send_to_ws, {.id = 9} },
2493 { "bar_toggle", bar_toggle, {0} },
2494 { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
2495 { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
2496 { "screenshot_all", legacyfunc, {0} },
2497 { "screenshot_wind", legacyfunc, {0} },
2498 { "float_toggle", floating_toggle,{0} },
2499 { "version", version, {0} },
2500 { "spawn_lock", legacyfunc, {0} },
2501 { "spawn_initscr", legacyfunc, {0} },
2502 { "spawn_custom", dummykeyfunc, {0} },
2503 { "dumpwins", dumpwins, {0} },
2504 { "invalid key func", NULL, {0} },
2509 enum keyfuncid funcid;
2512 int keys_size = 0, keys_length = 0;
2513 struct key *keys = NULL;
2516 enum { client_click, root_click };
2518 unsigned int action;
2520 unsigned int button;
2521 void (*func)(struct ws_win *, union arg *);
2524 /* action key mouse button func args */
2525 { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
2526 { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
2527 { client_click, MODKEY, Button1, move, {0} },
2531 update_modkey(unsigned int mod)
2536 for (i = 0; i < keys_length; i++)
2537 if (keys[i].mod & ShiftMask)
2538 keys[i].mod = mod | ShiftMask;
2542 for (i = 0; i < LENGTH(buttons); i++)
2543 if (buttons[i].mask & ShiftMask)
2544 buttons[i].mask = mod | ShiftMask;
2546 buttons[i].mask = mod;
2556 int spawns_size = 0, spawns_length = 0;
2557 struct spawn_prog *spawns = NULL;
2560 spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
2563 struct spawn_prog *prog = NULL;
2565 char *ap, **real_args;
2567 DNPRINTF(SWM_D_SPAWN, "spawn_custom %s\n", spawn_name);
2570 for (i = 0; i < spawns_length; i++) {
2571 if (!strcasecmp(spawn_name, spawns[i].name))
2575 fprintf(stderr, "spawn_custom: program %s not found\n",
2580 /* make room for expanded args */
2581 if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
2582 err(1, "spawn_custom: calloc real_args");
2584 /* expand spawn_args into real_args */
2585 for (i = 0; i < prog->argc; i++) {
2587 DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg = %s\n", ap);
2588 if (!strcasecmp(ap, "$bar_border")) {
2590 strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
2592 err(1, "spawn_custom border color");
2593 } else if (!strcasecmp(ap, "$bar_color")) {
2595 strdup(r->s->c[SWM_S_COLOR_BAR].name))
2597 err(1, "spawn_custom bar color");
2598 } else if (!strcasecmp(ap, "$bar_font")) {
2599 if ((real_args[i] = strdup(bar_fonts[bar_fidx]))
2601 err(1, "spawn_custom bar fonts");
2602 } else if (!strcasecmp(ap, "$bar_font_color")) {
2604 strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
2606 err(1, "spawn_custom color font");
2607 } else if (!strcasecmp(ap, "$color_focus")) {
2609 strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
2611 err(1, "spawn_custom color focus");
2612 } else if (!strcasecmp(ap, "$color_unfocus")) {
2614 strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
2616 err(1, "spawn_custom color unfocus");
2618 /* no match --> copy as is */
2619 if ((real_args[i] = strdup(ap)) == NULL)
2620 err(1, "spawn_custom strdup(ap)");
2622 DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg = %s\n",
2627 if ((swm_debug & SWM_D_SPAWN) != 0) {
2628 fprintf(stderr, "spawn_custom: result = ");
2629 for (i = 0; i < prog->argc; i++)
2630 fprintf(stderr, "\"%s\" ", real_args[i]);
2631 fprintf(stderr, "\n");
2637 for (i = 0; i < prog->argc; i++)
2643 setspawn(struct spawn_prog *prog)
2647 if (prog == NULL || prog->name == NULL)
2651 for (i = 0; i < spawns_length; i++) {
2652 if (!strcmp(spawns[i].name, prog->name)) {
2654 if (prog->argv == NULL) {
2656 DNPRINTF(SWM_D_SPAWN,
2657 "setspawn: delete #%d %s\n",
2659 free(spawns[i].name);
2660 for (j = 0; j < spawns[i].argc; j++)
2661 free(spawns[i].argv[j]);
2662 free(spawns[i].argv);
2663 j = spawns_length - 1;
2665 spawns[i] = spawns[j];
2670 DNPRINTF(SWM_D_SPAWN,
2671 "setspawn: replace #%d %s\n",
2673 free(spawns[i].name);
2674 for (j = 0; j < spawns[i].argc; j++)
2675 free(spawns[i].argv[j]);
2676 free(spawns[i].argv);
2679 /* found case handled */
2685 if (prog->argv == NULL) {
2687 "error: setspawn: cannot find program %s", prog->name);
2692 /* not found: add */
2693 if (spawns_size == 0 || spawns == NULL) {
2695 DNPRINTF(SWM_D_SPAWN, "setspawn: init list %d\n", spawns_size);
2696 spawns = malloc((size_t)spawns_size *
2697 sizeof(struct spawn_prog));
2698 if (spawns == NULL) {
2699 fprintf(stderr, "setspawn: malloc failed\n");
2703 } else if (spawns_length == spawns_size) {
2705 DNPRINTF(SWM_D_SPAWN, "setspawn: grow list %d\n", spawns_size);
2706 spawns = realloc(spawns, (size_t)spawns_size *
2707 sizeof(struct spawn_prog));
2708 if (spawns == NULL) {
2709 fprintf(stderr, "setspawn: realloc failed\n");
2715 if (spawns_length < spawns_size) {
2716 DNPRINTF(SWM_D_SPAWN, "setspawn: add #%d %s\n",
2717 spawns_length, prog->name);
2718 i = spawns_length++;
2721 fprintf(stderr, "spawns array problem?\n");
2722 if (spawns == NULL) {
2723 fprintf(stderr, "spawns array is NULL!\n");
2731 setconfspawn(char *selector, char *value, int flags)
2733 struct spawn_prog *prog;
2734 char *vp, *cp, *word;
2736 DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
2737 if ((prog = calloc(1, sizeof *prog)) == NULL)
2738 err(1, "setconfspawn: calloc prog");
2739 prog->name = strdup(selector);
2740 if (prog->name == NULL)
2741 err(1, "setconfspawn prog->name");
2742 if ((cp = vp = strdup(value)) == NULL)
2743 err(1, "setconfspawn: strdup(value) ");
2744 while ((word = strsep(&cp, " \t")) != NULL) {
2745 DNPRINTF(SWM_D_SPAWN, "setconfspawn: arg [%s]\n", word);
2747 cp += (long)strspn(cp, " \t");
2748 if (strlen(word) > 0) {
2750 prog->argv = realloc(prog->argv,
2751 prog->argc * sizeof(char *));
2752 if ((prog->argv[prog->argc - 1] = strdup(word)) == NULL)
2753 err(1, "setconfspawn: strdup");
2760 DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
2767 setconfspawn("term", "xterm", 0);
2768 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2769 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2770 setconfspawn("lock", "xlock", 0);
2771 setconfspawn("initscr", "initscreen.sh", 0);
2772 setconfspawn("menu", "dmenu_run"
2775 " -nf $bar_font_color"
2777 " -sf $bar_color", 0);
2781 #define SWM_MODNAME_SIZE 32
2782 #define SWM_KEY_WS "\n+ \t"
2784 parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
2788 DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
2789 if (mod == NULL || ks == NULL) {
2790 DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
2793 if (keystr == NULL || strlen(keystr) == 0) {
2794 DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
2800 while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
2801 DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
2803 cp += (long)strspn(cp, SWM_KEY_WS);
2804 if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
2806 else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
2808 else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
2810 else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
2812 else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
2814 else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
2816 else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
2817 *mod |= ControlMask;
2819 *ks = XStringToKeysym(name);
2820 XConvertCase(*ks, ks, &uks);
2821 if (ks == NoSymbol) {
2823 "parsekeys: invalid key %s\n",
2829 DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
2834 strdupsafe(char *str)
2839 return (strdup(str));
2843 setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
2846 DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
2847 keyfuncs[kfid].name, spawn_name);
2849 for (i = 0; i < keys_length; i++) {
2850 if (keys[i].mod == mod && keys[i].keysym == ks) {
2851 if (kfid == kf_invalid) {
2854 "setkeybinding: delete #%d %s\n",
2855 i, keyfuncs[keys[i].funcid].name);
2856 free(keys[i].spawn_name);
2857 j = keys_length - 1;
2861 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2864 /* found: replace */
2866 "setkeybinding: replace #%d %s %s\n",
2867 i, keyfuncs[keys[i].funcid].name,
2869 free(keys[i].spawn_name);
2871 keys[i].keysym = ks;
2872 keys[i].funcid = kfid;
2873 keys[i].spawn_name = strdupsafe(spawn_name);
2874 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2879 if (kfid == kf_invalid) {
2881 "error: setkeybinding: cannot find mod/key combination");
2882 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2885 /* not found: add */
2886 if (keys_size == 0 || keys == NULL) {
2888 DNPRINTF(SWM_D_KEY, "setkeybinding: init list %d\n", keys_size);
2889 keys = malloc((size_t)keys_size * sizeof(struct key));
2891 fprintf(stderr, "malloc failed\n");
2895 } else if (keys_length == keys_size) {
2897 DNPRINTF(SWM_D_KEY, "setkeybinding: grow list %d\n", keys_size);
2898 keys = realloc(keys, (size_t)keys_size * sizeof(struct key));
2900 fprintf(stderr, "realloc failed\n");
2905 if (keys_length < keys_size) {
2907 DNPRINTF(SWM_D_KEY, "setkeybinding: add #%d %s %s\n",
2908 j, keyfuncs[kfid].name, spawn_name);
2910 keys[j].keysym = ks;
2911 keys[j].funcid = kfid;
2912 keys[j].spawn_name = strdupsafe(spawn_name);
2914 fprintf(stderr, "keys array problem?\n");
2916 fprintf(stderr, "keys array problem\n");
2920 DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
2924 setconfbinding(char *selector, char *value, int flags)
2926 enum keyfuncid kfid;
2930 DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
2931 if (selector == NULL) {
2932 DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
2933 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2935 setkeybinding(mod, ks, kfid, NULL);
2940 /* search by key function name */
2941 for (kfid = 0; kfid < kf_invalid; (kfid)++) {
2942 if (strncasecmp(selector, keyfuncs[kfid].name,
2943 SWM_FUNCNAME_LEN) == 0) {
2944 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2946 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2947 setkeybinding(mod, ks, kfid, NULL);
2953 /* search by custom spawn name */
2954 for (i = 0; i < spawns_length; i++) {
2955 if (strcasecmp(selector, spawns[i].name) == 0) {
2956 DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
2958 if (parsekeys(value, mod_key, &mod, &ks) == 0) {
2959 setkeybinding(mod, ks, kf_spawn_custom,
2966 DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
2973 setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
2974 setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
2975 setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
2976 setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
2977 setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
2978 setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
2979 setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
2980 setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
2981 setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
2982 setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
2983 setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
2984 setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
2985 setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
2986 setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
2987 setkeybinding(MODKEY, XK_p, kf_spawn_custom, "menu");
2988 setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
2989 setkeybinding(MODKEY, XK_q, kf_restart, NULL);
2990 setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
2991 setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
2992 setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
2993 setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
2994 setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
2995 setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
2996 setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
2997 setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
2998 setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
2999 setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
3000 setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
3001 setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
3002 setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
3003 setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
3004 setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
3005 setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
3006 setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
3007 setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
3008 setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
3009 setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
3010 setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
3011 setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
3012 setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
3013 setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
3014 setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
3015 setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
3016 setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
3017 setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
3018 setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
3019 setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
3020 setkeybinding(MODKEY, XK_s, kf_spawn_custom, "screenshot_all");
3021 setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom, "screenshot_wind");
3022 setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
3023 setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
3024 setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom, "lock");
3025 setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom, "initscr");
3027 setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
3032 updatenumlockmask(void)
3035 XModifierKeymap *modmap;
3037 DNPRINTF(SWM_D_MISC, "updatenumlockmask\n");
3039 modmap = XGetModifierMapping(display);
3040 for (i = 0; i < 8; i++)
3041 for (j = 0; j < modmap->max_keypermod; j++)
3042 if (modmap->modifiermap[i * modmap->max_keypermod + j]
3043 == XKeysymToKeycode(display, XK_Num_Lock))
3044 numlockmask = (1 << i);
3046 XFreeModifiermap(modmap);
3052 unsigned int i, j, k;
3054 unsigned int modifiers[] =
3055 { 0, LockMask, numlockmask, numlockmask | LockMask };
3057 DNPRINTF(SWM_D_MISC, "grabkeys\n");
3058 updatenumlockmask();
3060 for (k = 0; k < ScreenCount(display); k++) {
3061 if (TAILQ_EMPTY(&screens[k].rl))
3063 XUngrabKey(display, AnyKey, AnyModifier, screens[k].root);
3064 for (i = 0; i < keys_length; i++) {
3065 if ((code = XKeysymToKeycode(display, keys[i].keysym)))
3066 for (j = 0; j < LENGTH(modifiers); j++)
3067 XGrabKey(display, code,
3068 keys[i].mod | modifiers[j],
3069 screens[k].root, True,
3070 GrabModeAsync, GrabModeAsync);
3076 grabbuttons(struct ws_win *win, int focused)
3079 unsigned int modifiers[] =
3080 { 0, LockMask, numlockmask, numlockmask|LockMask };
3082 updatenumlockmask();
3083 XUngrabButton(display, AnyButton, AnyModifier, win->id);
3085 for (i = 0; i < LENGTH(buttons); i++)
3086 if (buttons[i].action == client_click)
3087 for (j = 0; j < LENGTH(modifiers); j++)
3088 XGrabButton(display, buttons[i].button,
3089 buttons[i].mask | modifiers[j],
3090 win->id, False, BUTTONMASK,
3091 GrabModeAsync, GrabModeSync, None,
3094 XGrabButton(display, AnyButton, AnyModifier, win->id, False,
3095 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
3098 const char *quirkname[] = {
3099 "NONE", /* config string for "no value" */
3107 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
3108 #define SWM_Q_WS "\n|+ \t"
3110 parsequirks(char *qstr, unsigned long *quirk)
3120 while ((name = strsep(&cp, SWM_Q_WS)) != NULL) {
3122 cp += (long)strspn(cp, SWM_Q_WS);
3123 for (i = 0; i < LENGTH(quirkname); i++) {
3124 if (!strncasecmp(name, quirkname[i], SWM_QUIRK_LEN)) {
3125 DNPRINTF(SWM_D_QUIRK, "parsequirks: %s\n", name);
3130 *quirk |= 1 << (i-1);
3134 if (i >= LENGTH(quirkname)) {
3135 DNPRINTF(SWM_D_QUIRK,
3136 "parsequirks: invalid quirk [%s]\n", name);
3144 setquirk(const char *class, const char *name, const int quirk)
3149 for (i = 0; i < quirks_length; i++) {
3150 if (!strcmp(quirks[i].class, class) &&
3151 !strcmp(quirks[i].name, name)) {
3154 DNPRINTF(SWM_D_QUIRK,
3155 "setquirk: delete #%d %s:%s\n",
3156 i, quirks[i].class, quirks[i].name);
3157 free(quirks[i].class);
3158 free(quirks[i].name);
3159 j = quirks_length - 1;
3161 quirks[i] = quirks[j];
3165 /* found: replace */
3166 DNPRINTF(SWM_D_QUIRK,
3167 "setquirk: replace #%d %s:%s\n",
3168 i, quirks[i].class, quirks[i].name);
3169 free(quirks[i].class);
3170 free(quirks[i].name);
3171 quirks[i].class = strdup(class);
3172 quirks[i].name = strdup(name);
3173 quirks[i].quirk = quirk;
3180 "error: setquirk: cannot find class/name combination");
3183 /* not found: add */
3184 if (quirks_size == 0 || quirks == NULL) {
3186 DNPRINTF(SWM_D_QUIRK, "setquirk: init list %d\n", quirks_size);
3187 quirks = malloc((size_t)quirks_size * sizeof(struct quirk));
3189 fprintf(stderr, "setquirk: malloc failed\n");
3193 } else if (quirks_length == quirks_size) {
3195 DNPRINTF(SWM_D_QUIRK, "setquirk: grow list %d\n", quirks_size);
3196 quirks = realloc(quirks, (size_t)quirks_size * sizeof(struct quirk));
3198 fprintf(stderr, "setquirk: realloc failed\n");
3203 if (quirks_length < quirks_size) {
3204 DNPRINTF(SWM_D_QUIRK, "setquirk: add %d\n", quirks_length);
3205 j = quirks_length++;
3206 quirks[j].class = strdup(class);
3207 quirks[j].name = strdup(name);
3208 quirks[j].quirk = quirk;
3210 fprintf(stderr, "quirks array problem?\n");
3212 fprintf(stderr, "quirks array problem!\n");
3219 setconfquirk(char *selector, char *value, int flags)
3221 char *cp, *class, *name;
3223 unsigned long quirks;
3224 if (selector == NULL)
3226 if ((cp = strchr(selector, ':')) == NULL)
3231 if ((retval = parsequirks(value, &quirks)) == 0)
3232 setquirk(class, name, quirks);
3239 setquirk("MPlayer", "xv", SWM_Q_FLOAT | SWM_Q_FULLSCREEN);
3240 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT);
3241 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT);
3242 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ);
3243 setquirk("Firefox", "Dialog", SWM_Q_FLOAT);
3244 setquirk("Gimp", "gimp", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3245 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ);
3246 setquirk("xine", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3247 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3248 setquirk("xine", "xine Panel", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3249 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT | SWM_Q_ANYWHERE);
3250 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN | SWM_Q_FLOAT);
3251 setquirk("pcb", "pcb", SWM_Q_FLOAT);
3254 /* conf file stuff */
3255 #define SWM_CONF_FILE "scrotwm.conf"
3257 enum { SWM_S_BAR_DELAY, SWM_S_BAR_ENABLED, SWM_S_STACK_ENABLED,
3258 SWM_S_CLOCK_ENABLED, SWM_S_CYCLE_EMPTY, SWM_S_CYCLE_VISIBLE,
3259 SWM_S_SS_ENABLED, SWM_S_TERM_WIDTH, SWM_S_TITLE_CLASS_ENABLED,
3260 SWM_S_TITLE_NAME_ENABLED, SWM_S_BAR_FONT, SWM_S_BAR_ACTION,
3261 SWM_S_SPAWN_TERM, SWM_S_SS_APP, SWM_S_DIALOG_RATIO };
3264 setconfvalue(char *selector, char *value, int flags)
3267 case SWM_S_BAR_DELAY:
3268 bar_delay = atoi(value);
3270 case SWM_S_BAR_ENABLED:
3271 bar_enabled = atoi(value);
3273 case SWM_S_STACK_ENABLED:
3274 stack_enabled = atoi(value);
3276 case SWM_S_CLOCK_ENABLED:
3277 clock_enabled = atoi(value);
3279 case SWM_S_CYCLE_EMPTY:
3280 cycle_empty = atoi(value);
3282 case SWM_S_CYCLE_VISIBLE:
3283 cycle_visible = atoi(value);
3285 case SWM_S_SS_ENABLED:
3286 ss_enabled = atoi(value);
3288 case SWM_S_TERM_WIDTH:
3289 term_width = atoi(value);
3291 case SWM_S_TITLE_CLASS_ENABLED:
3292 title_class_enabled = atoi(value);
3294 case SWM_S_TITLE_NAME_ENABLED:
3295 title_name_enabled = atoi(value);
3297 case SWM_S_BAR_FONT:
3299 if ((bar_fonts[0] = strdup(value)) == NULL)
3300 err(1, "setconfvalue: bar_font");
3302 case SWM_S_BAR_ACTION:
3304 if ((bar_argv[0] = strdup(value)) == NULL)
3305 err(1, "setconfvalue: bar_action");
3307 case SWM_S_SPAWN_TERM:
3308 free(spawn_term[0]);
3309 if ((spawn_term[0] = strdup(value)) == NULL)
3310 err(1, "setconfvalue: spawn_term");
3314 case SWM_S_DIALOG_RATIO:
3315 dialog_ratio = atof(value);
3316 if (dialog_ratio > 1.0 || dialog_ratio <= .3)
3326 setconfmodkey(char *selector, char *value, int flags)
3328 if (!strncasecmp(value, "Mod1", strlen("Mod1")))
3329 update_modkey(Mod1Mask);
3330 else if (!strncasecmp(value, "Mod2", strlen("Mod2")))
3331 update_modkey(Mod2Mask);
3332 else if (!strncasecmp(value, "Mod3", strlen("Mod3")))
3333 update_modkey(Mod3Mask);
3334 else if (!strncasecmp(value, "Mod4", strlen("Mod4")))
3335 update_modkey(Mod4Mask);
3342 setconfcolor(char *selector, char *value, int flags)
3344 setscreencolor(value, ((selector == NULL)?-1:atoi(selector)), flags);
3349 setconfregion(char *selector, char *value, int flags)
3351 custom_region(value);
3355 /* config options */
3356 struct config_option {
3358 int (*func)(char*, char*, int);
3361 struct config_option configopt[] = {
3362 { "bar_enabled", setconfvalue, SWM_S_BAR_ENABLED },
3363 { "bar_border", setconfcolor, SWM_S_COLOR_BAR_BORDER },
3364 { "bar_color", setconfcolor, SWM_S_COLOR_BAR },
3365 { "bar_font_color", setconfcolor, SWM_S_COLOR_BAR_FONT },
3366 { "bar_font", setconfvalue, SWM_S_BAR_FONT },
3367 { "bar_action", setconfvalue, SWM_S_BAR_ACTION },
3368 { "bar_delay", setconfvalue, SWM_S_BAR_DELAY },
3369 { "bind", setconfbinding, 0 },
3370 { "stack_enabled", setconfvalue, SWM_S_STACK_ENABLED },
3371 { "clock_enabled", setconfvalue, SWM_S_CLOCK_ENABLED },
3372 { "color_focus", setconfcolor, SWM_S_COLOR_FOCUS },
3373 { "color_unfocus", setconfcolor, SWM_S_COLOR_UNFOCUS },
3374 { "cycle_empty", setconfvalue, SWM_S_CYCLE_EMPTY },
3375 { "cycle_visible", setconfvalue, SWM_S_CYCLE_VISIBLE },
3376 { "dialog_ratio", setconfvalue, SWM_S_DIALOG_RATIO },
3377 { "modkey", setconfmodkey, 0 },
3378 { "program", setconfspawn, 0 },
3379 { "quirk", setconfquirk, 0 },
3380 { "region", setconfregion, 0 },
3381 { "spawn_term", setconfvalue, SWM_S_SPAWN_TERM },
3382 { "screenshot_enabled", setconfvalue, SWM_S_SS_ENABLED },
3383 { "screenshot_app", setconfvalue, SWM_S_SS_APP },
3384 { "term_width", setconfvalue, SWM_S_TERM_WIDTH },
3385 { "title_class_enabled", setconfvalue, SWM_S_TITLE_CLASS_ENABLED },
3386 { "title_name_enabled", setconfvalue, SWM_S_TITLE_NAME_ENABLED }
3391 conf_load(char *filename)
3394 char *line, *cp, *optsub, *optval;
3395 size_t linelen, lineno = 0;
3396 int wordlen, i, optind;
3397 struct config_option *opt;
3399 DNPRINTF(SWM_D_CONF, "conf_load begin\n");
3401 if (filename == NULL) {
3402 fprintf(stderr, "conf_load: no filename\n");
3405 if ((config = fopen(filename, "r")) == NULL) {
3406 warn("conf_load: fopen");
3410 while (!feof(config)) {
3411 if ((line = fparseln(config, &linelen, &lineno, NULL, 0))
3414 err(1, "%s", filename);
3419 cp += strspn(cp, " \t\n"); /* eat whitespace */
3420 if (cp[0] == '\0') {
3425 /* get config option */
3426 wordlen = strcspn(cp, "=[ \t\n");
3428 warnx("%s: line %zd: no option found",
3433 for (i = 0; i < LENGTH(configopt); i++) {
3434 opt = &configopt[i];
3435 if (!strncasecmp(cp, opt->optname, wordlen) &&
3436 strlen(opt->optname) == wordlen) {
3442 warnx("%s: line %zd: unknown option %.*s",
3443 filename, lineno, wordlen, cp);
3447 cp += strspn(cp, " \t\n"); /* eat whitespace */
3448 /* get [selector] if any */
3452 wordlen = strcspn(cp, "]");
3455 warnx("%s: line %zd: syntax error",
3459 asprintf(&optsub, "%.*s", wordlen, cp);
3462 cp += strspn(cp, "] \t\n"); /* eat trailing */
3464 cp += strspn(cp, "= \t\n"); /* eat trailing */
3466 optval = strdup(cp);
3467 /* call function to deal with it all */
3468 if (configopt[optind].func(optsub, optval,
3469 configopt[optind].funcflags) != 0) {
3470 fprintf(stderr, "%s line %zd: %s\n",
3471 filename, lineno, line);
3472 errx(1, "%s: line %zd: invalid data for %s",
3473 filename, lineno, configopt[optind].optname);
3481 DNPRINTF(SWM_D_CONF, "conf_load end\n");
3487 set_child_transient(struct ws_win *win)
3489 struct ws_win *parent;
3491 parent = find_window(win->transient);
3493 parent->child_trans = win;
3497 manage_window(Window id)
3500 struct workspace *ws;
3501 struct ws_win *win, *ww;
3502 int format, i, ws_idx, n, border_me = 0;
3503 unsigned long nitems, bytes;
3504 Atom ws_idx_atom = 0, type;
3505 Atom *prot = NULL, *pp;
3506 unsigned char ws_idx_str[SWM_PROPLEN], *prop = NULL;
3507 struct swm_region *r;
3512 if ((win = find_window(id)) != NULL)
3513 return (win); /* already being managed */
3515 /* see if we are on the unmanaged list */
3516 if ((win = find_unmanaged_window(id)) != NULL) {
3517 DNPRINTF(SWM_D_MISC, "manage previously unmanaged window "
3519 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3520 TAILQ_INSERT_TAIL(&win->ws->winlist, win, entry);
3522 set_child_transient(win);
3526 if ((win = calloc(1, sizeof(struct ws_win))) == NULL)
3527 errx(1, "calloc: failed to allocate memory for new window");
3529 /* Get all the window data in one shot */
3530 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
3532 XGetWindowProperty(display, id, ws_idx_atom, 0, SWM_PROPLEN,
3533 False, XA_STRING, &type, &format, &nitems, &bytes, &prop);
3534 XGetWindowAttributes(display, id, &win->wa);
3535 XGetWMNormalHints(display, id, &win->sh, &mask);
3536 XGetTransientForHint(display, id, &trans);
3538 win->transient = trans;
3539 set_child_transient(win);
3540 DNPRINTF(SWM_D_MISC, "manage_window: win %lu transient %lu\n",
3541 win->id, win->transient);
3543 /* get supported protocols */
3544 if (XGetWMProtocols(display, id, &prot, &n)) {
3545 for (i = 0, pp = prot; i < n; i++, pp++) {
3546 if (*pp == takefocus)
3547 win->take_focus = 1;
3549 win->can_delete = 1;
3556 * Figure out where to put the window. If it was previously assigned to
3557 * a workspace (either by spawn() or manually moving), and isn't
3558 * transient, * put it in the same workspace
3560 r = root_to_region(win->wa.root);
3561 if (prop && win->transient == 0) {
3562 DNPRINTF(SWM_D_PROP, "got property _SWM_WS=%s\n", prop);
3563 ws_idx = strtonum(prop, 0, 9, &errstr);
3565 DNPRINTF(SWM_D_EVENT, "window idx is %s: %s",
3568 ws = &r->s->ws[ws_idx];
3571 /* this should launch transients in the same ws as parent */
3573 if ((ww = find_window(trans)) != NULL)
3580 "fix this bug mcbride\n");
3585 /* set up the window layout */
3588 win->s = r->s; /* this never changes */
3589 TAILQ_INSERT_TAIL(&ws->winlist, win, entry);
3591 win->g.w = win->wa.width;
3592 win->g.h = win->wa.height;
3593 win->g.x = win->wa.x;
3594 win->g.y = win->wa.y;
3596 /* Set window properties so we can remember this after reincarnation */
3597 if (ws_idx_atom && prop == NULL &&
3598 snprintf(ws_idx_str, SWM_PROPLEN, "%d", ws->idx) < SWM_PROPLEN) {
3599 DNPRINTF(SWM_D_PROP, "setting property _SWM_WS to %s\n",
3601 XChangeProperty(display, win->id, ws_idx_atom, XA_STRING, 8,
3602 PropModeReplace, ws_idx_str, SWM_PROPLEN);
3606 if (XGetClassHint(display, win->id, &win->ch)) {
3607 DNPRINTF(SWM_D_CLASS, "class: %s name: %s\n",
3608 win->ch.res_class, win->ch.res_name);
3610 /* java is retarded so treat it special */
3611 if (strstr(win->ch.res_name, "sun-awt")) {
3616 for (i = 0; i < quirks_length; i++){
3617 if (!strcmp(win->ch.res_class, quirks[i].class) &&
3618 !strcmp(win->ch.res_name, quirks[i].name)) {
3619 DNPRINTF(SWM_D_CLASS, "found: %s name: %s\n",
3620 win->ch.res_class, win->ch.res_name);
3621 if (quirks[i].quirk & SWM_Q_FLOAT) {
3625 win->quirks = quirks[i].quirk;
3630 /* alter window position if quirky */
3631 if (win->quirks & SWM_Q_ANYWHERE) {
3632 win->manual = 1; /* don't center the quirky windows */
3633 bzero(&wc, sizeof wc);
3635 if (win->g.y < bar_height) {
3636 win->g.y = wc.y = bar_height;
3639 if (win->g.w + win->g.x > WIDTH(r)) {
3640 win->g.x = wc.x = WIDTH(r) - win->g.w - 2;
3646 /* Reset font sizes (the bruteforce way; no default keybinding). */
3647 if (win->quirks & SWM_Q_XTERM_FONTADJ) {
3648 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3649 fake_keypress(win, XK_KP_Subtract, ShiftMask);
3650 for (i = 0; i < SWM_MAX_FONT_STEPS; i++)
3651 fake_keypress(win, XK_KP_Add, ShiftMask);
3656 bzero(&wc, sizeof wc);
3657 wc.border_width = 1;
3658 mask = CWBorderWidth;
3659 XConfigureWindow(display, win->id, mask, &wc);
3663 XSelectInput(display, id, EnterWindowMask | FocusChangeMask |
3664 PropertyChangeMask | StructureNotifyMask);
3666 set_win_state(win, NormalState);
3668 /* floaters need to be mapped if they are in the current workspace */
3669 if ((win->floating || win->transient) && (ws->idx == r->ws->idx))
3670 XMapRaised(display, win->id);
3676 free_window(struct ws_win *win)
3678 DNPRINTF(SWM_D_MISC, "free_window: %lu\n", win->id);
3683 /* needed for restart wm */
3684 set_win_state(win, WithdrawnState);
3686 TAILQ_REMOVE(&win->ws->unmanagedlist, win, entry);
3688 if (win->ch.res_class)
3689 XFree(win->ch.res_class);
3690 if (win->ch.res_name)
3691 XFree(win->ch.res_name);
3696 memset(win, 0xff, sizeof *win); /* XXX kill later */
3702 unmanage_window(struct ws_win *win)
3704 struct ws_win *parent;
3709 DNPRINTF(SWM_D_MISC, "unmanage_window: %lu\n", win->id);
3711 if (win->transient) {
3712 parent = find_window(win->transient);
3714 parent->child_trans = NULL;
3717 TAILQ_REMOVE(&win->ws->winlist, win, entry);
3718 TAILQ_INSERT_TAIL(&win->ws->unmanagedlist, win, entry);
3724 focus_magic(struct ws_win *win, int do_trans)
3729 if (do_trans && win->child_trans) {
3730 /* win = parent & has a transient so focus on that */
3732 focus_win(win->child_trans);
3733 if (win->child_trans->take_focus)
3734 client_msg(win, takefocus);
3736 focus_win(win->child_trans);
3737 if (win->child_trans->take_focus)
3738 client_msg(win->child_trans, takefocus);
3743 if (win->take_focus)
3744 client_msg(win, takefocus);
3751 DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window);
3759 XKeyEvent *ev = &e->xkey;
3761 DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window);
3763 keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0);
3764 for (i = 0; i < keys_length; i++)
3765 if (keysym == keys[i].keysym
3766 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
3767 && keyfuncs[keys[i].funcid].func) {
3768 if (keys[i].funcid == kf_spawn_custom)
3770 root_to_region(ev->root),
3771 &(keyfuncs[keys[i].funcid].args),
3775 keyfuncs[keys[i].funcid].func(
3776 root_to_region(ev->root),
3777 &(keyfuncs[keys[i].funcid].args)
3783 buttonpress(XEvent *e)
3787 XButtonPressedEvent *ev = &e->xbutton;
3789 DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window);
3791 action = root_click;
3792 if ((win = find_window(ev->window)) == NULL)
3795 focus_magic(win, 1);
3796 action = client_click;
3798 for (i = 0; i < LENGTH(buttons); i++)
3799 if (action == buttons[i].action && buttons[i].func &&
3800 buttons[i].button == ev->button &&
3801 CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
3802 buttons[i].func(win, &buttons[i].args);
3806 configurerequest(XEvent *e)
3808 XConfigureRequestEvent *ev = &e->xconfigurerequest;
3813 if ((win = find_window(ev->window)) == NULL)
3814 if ((win = find_unmanaged_window(ev->window)) == NULL)
3818 DNPRINTF(SWM_D_EVENT, "configurerequest: new window: %lu\n",
3820 bzero(&wc, sizeof wc);
3823 wc.width = ev->width;
3824 wc.height = ev->height;
3825 wc.border_width = ev->border_width;
3826 wc.sibling = ev->above;
3827 wc.stack_mode = ev->detail;
3828 XConfigureWindow(display, ev->window, ev->value_mask, &wc);
3830 DNPRINTF(SWM_D_EVENT, "configurerequest: change window: %lu\n",
3832 if (win->floating) {
3833 if (ev->value_mask & CWX)
3835 if (ev->value_mask & CWY)
3837 if (ev->value_mask & CWWidth)
3838 win->g.w = ev->width;
3839 if (ev->value_mask & CWHeight)
3840 win->g.h = ev->height;
3847 configurenotify(XEvent *e)
3852 DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n",
3853 e->xconfigure.window);
3855 win = find_window(e->xconfigure.window);
3857 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3865 destroynotify(XEvent *e)
3868 XDestroyWindowEvent *ev = &e->xdestroywindow;
3871 DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window);
3873 if ((win = find_window(ev->window)) == NULL) {
3874 if ((win = find_unmanaged_window(ev->window)) == NULL)
3880 a.id = SWM_ARG_ID_FOCUSPREV;
3881 focus(win->ws->r, &a);
3882 unmanage_window(win);
3888 enternotify(XEvent *e)
3890 XCrossingEvent *ev = &e->xcrossing;
3893 DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window);
3896 * happens when a window is created or destroyed and the border
3897 * crosses the mouse pointer
3899 if (QLength(display))
3902 if ((win = find_window(ev->window)) == NULL)
3905 focus_magic(win, 1);
3911 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", e->xfocus.window);
3917 DNPRINTF(SWM_D_EVENT, "focusout: window: %lu\n", e->xfocus.window);
3921 mapnotify(XEvent *e)
3924 XMapEvent *ev = &e->xmap;
3926 DNPRINTF(SWM_D_EVENT, "mapnotify: window: %lu\n", ev->window);
3928 win = manage_window(ev->window);
3930 set_win_state(win, NormalState);
3934 mappingnotify(XEvent *e)
3936 XMappingEvent *ev = &e->xmapping;
3938 XRefreshKeyboardMapping(ev);
3939 if (ev->request == MappingKeyboard)
3944 maprequest(XEvent *e)
3947 struct swm_region *r;
3948 XWindowAttributes wa;
3949 XMapRequestEvent *ev = &e->xmaprequest;
3951 DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n",
3952 e->xmaprequest.window);
3954 if (!XGetWindowAttributes(display, ev->window, &wa))
3956 if (wa.override_redirect)
3959 win = manage_window(e->xmaprequest.window);
3961 return; /* can't happen */
3965 /* make new win focused */
3966 r = root_to_region(win->wa.root);
3967 if (win->ws == r->ws)
3968 focus_magic(win, 0);
3972 propertynotify(XEvent *e)
3975 XPropertyEvent *ev = &e->xproperty;
3977 DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n",
3980 if (ev->state == PropertyDelete)
3981 return; /* ignore */
3982 win = find_window(ev->window);
3987 case XA_WM_NORMAL_HINTS:
3990 XGetWMNormalHints(display, win->id, &win->sh, &mask);
3991 fprintf(stderr, "normal hints: flag 0x%x\n", win->sh.flags);
3992 if (win->sh.flags & PMinSize) {
3993 win->g.w = win->sh.min_width;
3994 win->g.h = win->sh.min_height;
3995 fprintf(stderr, "min %d %d\n", win->g.w, win->g.h);
3997 XMoveResizeWindow(display, win->id,
3998 win->g.x, win->g.y, win->g.w, win->g.h);
4007 unmapnotify(XEvent *e)
4012 DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window);
4014 /* determine if we need to help unmanage this window */
4015 win = find_window(e->xunmap.window);
4019 if (getstate(e->xunmap.window) == NormalState) {
4020 /* trash window and refocus */
4021 a.id = SWM_ARG_ID_FOCUSPREV;
4022 focus(win->ws->r, &a);
4023 unmanage_window(win);
4029 visibilitynotify(XEvent *e)
4032 struct swm_region *r;
4034 DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n",
4035 e->xvisibility.window);
4036 if (e->xvisibility.state == VisibilityUnobscured)
4037 for (i = 0; i < ScreenCount(display); i++)
4038 TAILQ_FOREACH(r, &screens[i].rl, entry)
4039 if (e->xvisibility.window == r->bar_window)
4044 xerror_start(Display *d, XErrorEvent *ee)
4051 xerror(Display *d, XErrorEvent *ee)
4053 /* fprintf(stderr, "error: %p %p\n", display, ee); */
4061 xerrorxlib = XSetErrorHandler(xerror_start);
4063 /* this causes an error if some other window manager is running */
4064 XSelectInput(display, DefaultRootWindow(display),
4065 SubstructureRedirectMask);
4066 XSync(display, False);
4070 XSetErrorHandler(xerror);
4071 XSync(display, False);
4076 new_region(struct swm_screen *s, int x, int y, int w, int h)
4078 struct swm_region *r, *n;
4079 struct workspace *ws = NULL;
4082 DNPRINTF(SWM_D_MISC, "new region: screen[%d]:%dx%d+%d+%d\n",
4083 s->idx, w, h, x, y);
4085 /* remove any conflicting regions */
4086 n = TAILQ_FIRST(&s->rl);
4089 n = TAILQ_NEXT(r, entry);
4090 if (X(r) < (x + w) &&
4091 (X(r) + WIDTH(r)) > x &&
4093 (Y(r) + HEIGHT(r)) > y) {
4094 XDestroyWindow(display, r->bar_window);
4095 TAILQ_REMOVE(&s->rl, r, entry);
4096 TAILQ_INSERT_TAIL(&s->orl, r, entry);
4100 /* search old regions for one to reuse */
4102 /* size + location match */
4103 TAILQ_FOREACH(r, &s->orl, entry)
4104 if (X(r) == x && Y(r) == y &&
4105 HEIGHT(r) == h && WIDTH(r) == w)
4109 TAILQ_FOREACH(r, &s->orl, entry)
4110 if (HEIGHT(r) == h && WIDTH(r) == w)
4114 TAILQ_REMOVE(&s->orl, r, entry);
4115 /* try to use old region's workspace */
4116 if (r->ws->r == NULL)
4119 if ((r = calloc(1, sizeof(struct swm_region))) == NULL)
4120 errx(1, "calloc: failed to allocate memory for screen");
4122 /* if we don't have a workspace already, find one */
4124 for (i = 0; i < SWM_WS_MAX; i++)
4125 if (s->ws[i].r == NULL) {
4132 errx(1, "no free workspaces\n");
4141 TAILQ_INSERT_TAIL(&s->rl, r, entry);
4147 #ifdef SWM_XRR_HAS_CRTC
4149 XRRScreenResources *sr;
4152 #endif /* SWM_XRR_HAS_CRTC */
4153 struct swm_region *r;
4156 if (i >= ScreenCount(display))
4157 errx(1, "invalid screen");
4159 /* remove any old regions */
4160 while ((r = TAILQ_FIRST(&screens[i].rl)) != NULL) {
4161 r->ws->old_r = r->ws->r = NULL;
4162 XDestroyWindow(display, r->bar_window);
4163 TAILQ_REMOVE(&screens[i].rl, r, entry);
4164 TAILQ_INSERT_TAIL(&screens[i].orl, r, entry);
4167 /* map virtual screens onto physical screens */
4168 #ifdef SWM_XRR_HAS_CRTC
4170 if (xrandr_support) {
4171 sr = XRRGetScreenResources(display, screens[i].root);
4173 new_region(&screens[i], 0, 0,
4174 DisplayWidth(display, i),
4175 DisplayHeight(display, i));
4179 for (c = 0, ci = NULL; c < ncrtc; c++) {
4180 ci = XRRGetCrtcInfo(display, sr, sr->crtcs[c]);
4181 if (ci->noutput == 0)
4185 if (ci != NULL && ci->mode == None)
4186 new_region(&screens[i], 0, 0,
4187 DisplayWidth(display, i),
4188 DisplayHeight(display, i));
4190 new_region(&screens[i],
4191 ci->x, ci->y, ci->width, ci->height);
4194 XRRFreeCrtcInfo(ci);
4195 XRRFreeScreenResources(sr);
4197 #endif /* SWM_XRR_HAS_CRTC */
4199 new_region(&screens[i], 0, 0, DisplayWidth(display, i),
4200 DisplayHeight(display, i));
4205 screenchange(XEvent *e) {
4206 XRRScreenChangeNotifyEvent *xe = (XRRScreenChangeNotifyEvent *)e;
4207 struct swm_region *r;
4210 DNPRINTF(SWM_D_EVENT, "screenchange: %lu\n", xe->root);
4212 if (!XRRUpdateConfiguration(e))
4215 /* silly event doesn't include the screen index */
4216 for (i = 0; i < ScreenCount(display); i++)
4217 if (screens[i].root == xe->root)
4219 if (i >= ScreenCount(display))
4220 errx(1, "screenchange: screen not found\n");
4222 /* brute force for now, just re-enumerate the regions */
4225 /* add bars to all regions */
4226 for (i = 0; i < ScreenCount(display); i++)
4227 TAILQ_FOREACH(r, &screens[i].rl, entry)
4235 Window d1, d2, *wins = NULL;
4236 XWindowAttributes wa;
4239 int errorbase, major, minor;
4240 struct workspace *ws;
4244 if ((screens = calloc(ScreenCount(display),
4245 sizeof(struct swm_screen))) == NULL)
4246 errx(1, "calloc: screens");
4248 ws_idx_atom = XInternAtom(display, "_SWM_WS", False);
4250 /* initial Xrandr setup */
4251 xrandr_support = XRRQueryExtension(display,
4252 &xrandr_eventbase, &errorbase);
4254 if (XRRQueryVersion(display, &major, &minor) && major < 1)
4257 /* map physical screens */
4258 for (i = 0; i < ScreenCount(display); i++) {
4259 DNPRINTF(SWM_D_WS, "setup_screens: init screen %d\n", i);
4261 TAILQ_INIT(&screens[i].rl);
4262 TAILQ_INIT(&screens[i].orl);
4263 screens[i].root = RootWindow(display, i);
4265 /* set default colors */
4266 setscreencolor("red", i + 1, SWM_S_COLOR_FOCUS);
4267 setscreencolor("rgb:88/88/88", i + 1, SWM_S_COLOR_UNFOCUS);
4268 setscreencolor("rgb:00/80/80", i + 1, SWM_S_COLOR_BAR_BORDER);
4269 setscreencolor("black", i + 1, SWM_S_COLOR_BAR);
4270 setscreencolor("rgb:a0/a0/a0", i + 1, SWM_S_COLOR_BAR_FONT);
4272 /* init all workspaces */
4273 /* XXX these should be dynamically allocated too */
4274 for (j = 0; j < SWM_WS_MAX; j++) {
4275 ws = &screens[i].ws[j];
4280 TAILQ_INIT(&ws->winlist);
4281 TAILQ_INIT(&ws->unmanagedlist);
4283 for (k = 0; layouts[k].l_stack != NULL; k++)
4284 if (layouts[k].l_config != NULL)
4285 layouts[k].l_config(ws,
4286 SWM_ARG_ID_STACKINIT);
4287 ws->cur_layout = &layouts[0];
4289 /* grab existing windows (before we build the bars)*/
4290 if (!XQueryTree(display, screens[i].root, &d1, &d2, &wins, &no))
4296 XRRSelectInput(display, screens[i].root,
4297 RRScreenChangeNotifyMask);
4299 /* attach windows to a region */
4300 /* normal windows */
4301 for (j = 0; j < no; j++) {
4302 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4303 wa.override_redirect ||
4304 XGetTransientForHint(display, wins[j], &d1))
4307 state = getstate(wins[j]);
4308 manage = state == IconicState;
4309 if (wa.map_state == IsViewable || manage)
4310 manage_window(wins[j]);
4312 /* transient windows */
4313 for (j = 0; j < no; j++) {
4314 if (!XGetWindowAttributes(display, wins[j], &wa) ||
4315 wa.override_redirect)
4318 state = getstate(wins[j]);
4319 manage = state == IconicState;
4320 if (XGetTransientForHint(display, wins[j], &d1) &&
4322 manage_window(wins[j]);
4334 if ((bar_fonts[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
4336 err(1, "setup_globals: strdup");
4337 if ((bar_fonts[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
4339 err(1, "setup_globals: strdup");
4340 if ((bar_fonts[2] = strdup("-misc-fixed-medium-r-*-*-*-*-*-*-*-*-*-*"))
4342 err(1, "setup_globals: strdup");
4343 if ((spawn_term[0] = strdup("xterm")) == NULL)
4344 err(1, "setup_globals: strdup");
4351 Atom netwmcheck, netwmname, utf8_string;
4354 /* work around sun jdk bugs, code from wmname */
4355 netwmcheck = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
4356 netwmname = XInternAtom(display, "_NET_WM_NAME", False);
4357 utf8_string = XInternAtom(display, "UTF8_STRING", False);
4358 for (i = 0; i < ScreenCount(display); i++) {
4359 root = screens[i].root;
4360 XChangeProperty(display, root, netwmcheck, XA_WINDOW, 32,
4361 PropModeReplace, (unsigned char *)&root, 1);
4362 XChangeProperty(display, root, netwmname, utf8_string, 8,
4363 PropModeReplace, "LG3D", strlen("LG3D"));
4368 main(int argc, char *argv[])
4371 struct swm_region *r, *rr;
4372 struct ws_win *winfocus = NULL;
4375 char conf[PATH_MAX], *cfile = NULL;
4382 fprintf(stderr, "Welcome to scrotwm V%s cvs tag: %s\n",
4383 SWM_VERSION, cvstag);
4384 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
4385 warnx("no locale support");
4387 if (!(display = XOpenDisplay(0)))
4388 errx(1, "can not open display");
4391 errx(1, "other wm running");
4393 /* handle some signale */
4394 installsignal(SIGINT, "INT");
4395 installsignal(SIGHUP, "HUP");
4396 installsignal(SIGQUIT, "QUIT");
4397 installsignal(SIGTERM, "TERM");
4398 installsignal(SIGCHLD, "CHLD");
4400 astate = XInternAtom(display, "WM_STATE", False);
4401 aprot = XInternAtom(display, "WM_PROTOCOLS", False);
4402 adelete = XInternAtom(display, "WM_DELETE_WINDOW", False);
4403 takefocus = XInternAtom(display, "WM_TAKE_FOCUS", False);
4405 /* look for local and global conf file */
4406 pwd = getpwuid(getuid());
4408 errx(1, "invalid user %d", getuid());
4416 snprintf(conf, sizeof conf, "%s/.%s", pwd->pw_dir, SWM_CONF_FILE);
4417 if (stat(conf, &sb) != -1) {
4418 if (S_ISREG(sb.st_mode))
4421 /* try global conf file */
4422 snprintf(conf, sizeof conf, "/etc/%s", SWM_CONF_FILE);
4423 if (!stat(conf, &sb))
4424 if (S_ISREG(sb.st_mode))
4430 /* setup all bars */
4431 for (i = 0; i < ScreenCount(display); i++)
4432 TAILQ_FOREACH(r, &screens[i].rl, entry) {
4433 if (winfocus == NULL)
4434 winfocus = TAILQ_FIRST(&r->ws->winlist);
4438 /* set some values to work around bad programs */
4444 xfd = ConnectionNumber(display);
4446 while (XPending(display)) {
4447 XNextEvent(display, &e);
4450 if (e.type < LASTEvent) {
4452 if (handler[e.type])
4453 handler[e.type](&e);
4455 DNPRINTF(SWM_D_EVENT,
4456 "win: %lu unknown event: %d\n",
4457 e.xany.window, e.type);
4459 switch (e.type - xrandr_eventbase) {
4460 case RRScreenChangeNotify:
4464 DNPRINTF(SWM_D_EVENT,
4465 "win: %lu unknown xrandr event: "
4466 "%d\n", e.xany.window, e.type);
4472 /* if we are being restarted go focus on first window */
4474 rr = winfocus->ws->r;
4476 /* not a visible window */
4480 /* move pointer to first screen if multi screen */
4481 if (ScreenCount(display) > 1 || outputs > 1)
4482 XWarpPointer(display, None, rr->s[0].root,
4483 0, 0, 0, 0, rr->g.x,
4484 rr->g.y + bar_enabled ? bar_height : 0);
4486 a.id = SWM_ARG_ID_FOCUSCUR;
4496 if (select(xfd + 1, &rd, NULL, NULL, &tv) == -1)
4498 DNPRINTF(SWM_D_MISC, "select failed");
4509 XCloseDisplay(display);