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>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Much code and ideas taken from dwm under the following license:
21 * MIT/X Consortium License
23 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
24 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
25 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
26 * 2007 Premysl Hruby <dfenze at gmail dot com>
27 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
28 * 2007 Christof Musik <christof at sendfax dot de>
29 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
30 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
31 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
33 * Permission is hereby granted, free of charge, to any person obtaining a
34 * copy of this software and associated documentation files (the "Software"),
35 * to deal in the Software without restriction, including without limitation
36 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
37 * and/or sell copies of the Software, and to permit persons to whom the
38 * Software is furnished to do so, subject to the following conditions:
40 * The above copyright notice and this permission notice shall be included in
41 * all copies or substantial portions of the Software.
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
46 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
48 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
49 * DEALINGS IN THE SOFTWARE.
52 static const char *cvstag
= "$scrotwm$";
54 #define SWM_VERSION "0.9.2"
70 #include <sys/types.h>
74 #include <sys/queue.h>
75 #include <sys/param.h>
76 #include <sys/select.h>
78 #include <X11/cursorfont.h>
79 #include <X11/keysym.h>
80 #include <X11/Xatom.h>
82 #include <X11/Xproto.h>
83 #include <X11/Xutil.h>
84 #include <X11/extensions/Xrandr.h>
87 # error XRandR versions less than 1.0 are not supported
92 #define SWM_XRR_HAS_CRTC
96 /* #define SWM_DEBUG */
98 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
99 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
100 #define SWM_D_MISC 0x0001
101 #define SWM_D_EVENT 0x0002
102 #define SWM_D_WS 0x0004
103 #define SWM_D_FOCUS 0x0008
104 #define SWM_D_MOVE 0x0010
105 #define SWM_D_STACK 0x0020
106 #define SWM_D_MOUSE 0x0040
107 #define SWM_D_PROP 0x0080
108 #define SWM_D_CLASS 0x0100
109 #define SWM_D_KEY 0x0200
111 u_int32_t swm_debug
= 0
124 #define DPRINTF(x...)
125 #define DNPRINTF(n,x...)
128 #define LENGTH(x) (sizeof x / sizeof x[0])
129 #define MODKEY Mod1Mask
130 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
131 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
132 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
133 #define SWM_PROPLEN (16)
134 #define SWM_FUNCNAME_LEN (32)
135 #define SWM_KEYS_LEN (255)
136 #define X(r) (r)->g.x
137 #define Y(r) (r)->g.y
138 #define WIDTH(r) (r)->g.w
139 #define HEIGHT(r) (r)->g.h
140 #define SWM_MAX_FONT_STEPS (3)
143 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
150 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
155 int xrandr_eventbase
;
156 int ignore_enter
= 0;
157 unsigned int numlockmask
= 0;
161 int cycle_visible
= 0;
163 int font_adjusted
= 0;
166 double dialog_ratio
= .6;
168 #define SWM_BAR_MAX (256)
169 char *bar_argv
[] = { NULL
, NULL
};
171 char bar_ext
[SWM_BAR_MAX
];
172 char bar_vertext
[SWM_BAR_MAX
];
174 sig_atomic_t bar_alarm
= 0;
178 int bar_extra_running
= 0;
181 int clock_enabled
= 1;
187 char *bar_fonts
[] = {
188 "-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*",
189 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
193 /* terminal + args */
194 char *spawn_term
[] = { "xterm", NULL
};
195 char *spawn_screenshot
[] = { "screenshot.sh", NULL
, NULL
};
196 char *spawn_lock
[] = { "xlock", NULL
};
197 char *spawn_initscr
[] = { "initscreen.sh", NULL
};
198 char *spawn_menu
[] = { "dmenu_run", "-fn", NULL
, "-nb", NULL
,
199 "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
201 #define SWM_MENU_FN (2)
202 #define SWM_MENU_NB (4)
203 #define SWM_MENU_NF (6)
204 #define SWM_MENU_SB (8)
205 #define SWM_MENU_SF (10)
207 /* layout manager data */
208 struct swm_geometry
{
218 /* virtual "screens" */
220 TAILQ_ENTRY(swm_region
) entry
;
221 struct swm_geometry g
;
222 struct workspace
*ws
; /* current workspace on this region */
223 struct swm_screen
*s
; /* screen idx */
226 TAILQ_HEAD(swm_region_list
, swm_region
);
229 TAILQ_ENTRY(ws_win
) entry
;
231 struct swm_geometry g
;
236 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
240 unsigned long quirks
;
241 struct workspace
*ws
; /* always valid */
242 struct swm_screen
*s
; /* always valid, never changes */
243 XWindowAttributes wa
;
247 TAILQ_HEAD(ws_win_list
, ws_win
);
249 /* user/key callable function IDs */
305 /* layout handlers */
307 void vertical_config(struct workspace
*, int);
308 void vertical_stack(struct workspace
*, struct swm_geometry
*);
309 void horizontal_config(struct workspace
*, int);
310 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
311 void max_stack(struct workspace
*, struct swm_geometry
*);
313 void grabbuttons(struct ws_win
*, int);
314 void new_region(struct swm_screen
*, int, int, int, int);
315 void update_modkey(unsigned int);
316 /* user functions / key binding */
317 int bindmatch(const char *var
, const char *name
, unsigned int currmod
, char *keystr
,
318 enum keyfuncid
*kfid
, unsigned int *mod
, KeySym
*ks
);
319 void setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
);
322 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
323 void (*l_config
)(struct workspace
*, int);
325 /* stack, configure */
326 { vertical_stack
, vertical_config
},
327 { horizontal_stack
, horizontal_config
},
332 #define SWM_H_SLICE (32)
333 #define SWM_V_SLICE (32)
335 /* define work spaces */
337 int idx
; /* workspace index */
338 int restack
; /* restack on switch */
339 struct layout
*cur_layout
; /* current layout handlers */
340 struct ws_win
*focus
; /* may be NULL */
341 struct ws_win
*focus_prev
; /* may be NULL */
342 struct swm_region
*r
; /* may be NULL */
343 struct ws_win_list winlist
; /* list of windows in ws */
347 int horizontal_msize
;
349 int horizontal_stacks
;
356 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
357 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
359 /* physical screen mapping */
360 #define SWM_WS_MAX (10) /* XXX Too small? */
362 int idx
; /* screen index */
363 struct swm_region_list rl
; /* list of regions on this screen */
364 struct swm_region_list orl
; /* list of old regions */
366 struct workspace ws
[SWM_WS_MAX
];
372 } c
[SWM_S_COLOR_MAX
];
374 struct swm_screen
*screens
;
377 struct ws_win
*cur_focus
= NULL
;
379 /* args to functions */
382 #define SWM_ARG_ID_FOCUSNEXT (0)
383 #define SWM_ARG_ID_FOCUSPREV (1)
384 #define SWM_ARG_ID_FOCUSMAIN (2)
385 #define SWM_ARG_ID_SWAPNEXT (3)
386 #define SWM_ARG_ID_SWAPPREV (4)
387 #define SWM_ARG_ID_SWAPMAIN (5)
388 #define SWM_ARG_ID_MASTERSHRINK (6)
389 #define SWM_ARG_ID_MASTERGROW (7)
390 #define SWM_ARG_ID_MASTERADD (8)
391 #define SWM_ARG_ID_MASTERDEL (9)
392 #define SWM_ARG_ID_STACKRESET (10)
393 #define SWM_ARG_ID_STACKINIT (11)
394 #define SWM_ARG_ID_CYCLEWS_UP (12)
395 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
396 #define SWM_ARG_ID_CYCLESC_UP (14)
397 #define SWM_ARG_ID_CYCLESC_DOWN (15)
398 #define SWM_ARG_ID_STACKINC (16)
399 #define SWM_ARG_ID_STACKDEC (17)
400 #define SWM_ARG_ID_SS_ALL (0)
401 #define SWM_ARG_ID_SS_WINDOW (1)
402 #define SWM_ARG_ID_DONTCENTER (0)
403 #define SWM_ARG_ID_CENTER (1)
404 #define SWM_ARG_ID_KILLWINDOW (0)
405 #define SWM_ARG_ID_DELETEWINDOW (1)
414 #define SWM_Q_FLOAT (1<<0) /* float this window */
415 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
416 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
417 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
418 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
420 { "MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
},
421 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
},
422 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT
},
423 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
},
424 { "Firefox", "Dialog", SWM_Q_FLOAT
},
425 { "Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
426 { "XTerm", "xterm", SWM_Q_XTERM_FONTADJ
},
427 { "xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
428 { "Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
429 { "xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
430 { "Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
431 { "xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
},
432 { "pcb", "pcb", SWM_Q_FLOAT
},
437 void expose(XEvent
*);
438 void keypress(XEvent
*);
439 void buttonpress(XEvent
*);
440 void configurerequest(XEvent
*);
441 void configurenotify(XEvent
*);
442 void destroynotify(XEvent
*);
443 void enternotify(XEvent
*);
444 void focusin(XEvent
*);
445 void focusout(XEvent
*);
446 void mappingnotify(XEvent
*);
447 void maprequest(XEvent
*);
448 void propertynotify(XEvent
*);
449 void unmapnotify(XEvent
*);
450 void visibilitynotify(XEvent
*);
452 void (*handler
[LASTEvent
])(XEvent
*) = {
454 [KeyPress
] = keypress
,
455 [ButtonPress
] = buttonpress
,
456 [ConfigureRequest
] = configurerequest
,
457 [ConfigureNotify
] = configurenotify
,
458 [DestroyNotify
] = destroynotify
,
459 [EnterNotify
] = enternotify
,
461 [FocusOut
] = focusout
,
462 [MappingNotify
] = mappingnotify
,
463 [MapRequest
] = maprequest
,
464 [PropertyNotify
] = propertynotify
,
465 [UnmapNotify
] = unmapnotify
,
466 [VisibilityNotify
] = visibilitynotify
,
470 name_to_color(char *colorname
)
474 XColor screen_def
, exact_def
;
475 unsigned long result
= 0;
476 char cname
[32] = "#";
478 cmap
= DefaultColormap(display
, screens
[0].idx
);
479 status
= XAllocNamedColor(display
, cmap
, colorname
,
480 &screen_def
, &exact_def
);
482 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
483 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
487 result
= screen_def
.pixel
;
489 fprintf(stderr
, "color '%s' not found.\n", colorname
);
495 setscreencolor(char *val
, int i
, int c
)
497 if (i
> 0 && i
<= ScreenCount(display
)) {
498 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
499 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
501 } else if (i
== -1) {
502 for (i
= 0; i
< ScreenCount(display
); i
++)
503 screens
[i
].c
[c
].color
= name_to_color(val
);
504 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
507 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
508 i
, ScreenCount(display
));
512 custom_region(char *val
)
514 unsigned int sidx
, x
, y
, w
, h
;
516 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
517 errx(1, "invalid custom region, "
518 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
519 if (sidx
< 1 || sidx
> ScreenCount(display
))
520 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
521 sidx
, ScreenCount(display
));
525 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
527 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
528 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
529 w
+ x
> DisplayWidth(display
, sidx
) ||
530 h
+ y
> DisplayHeight(display
, sidx
))
531 errx(1, "region %ux%u+%u+%u not within screen boundaries "
532 "(%ux%u)\n", w
, h
, x
, y
,
533 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
535 new_region(&screens
[sidx
], x
, y
, w
, h
);
539 varmatch(char *var
, char *name
, int *index
)
544 i
= strncmp(var
, name
, 255);
551 p
= var
+ strlen(name
);
554 bzero(buf
, sizeof buf
);
556 while (isdigit(*p
) && i
< sizeof buf
)
558 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
560 *index
= strtonum(buf
, 0, 99, NULL
);
564 /* conf file stuff */
565 #define SWM_CONF_WS "\n= \t"
566 #define SWM_CONF_FILE "scrotwm.conf"
568 conf_load(char *filename
)
571 char *line
, *cp
, *var
, *val
;
572 size_t len
, lineno
= 0;
574 unsigned int modkey
= MODKEY
, modmask
;
578 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
580 if (filename
== NULL
)
583 if ((config
= fopen(filename
, "r")) == NULL
)
586 for (sc
= ScreenCount(display
);;) {
587 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
591 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
597 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
599 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
600 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
603 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
606 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
607 bar_enabled
= atoi(val
);
608 else if (!varmatch(var
, "bar_border", &i
))
609 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
610 else if (!varmatch(var
, "bar_color", &i
))
611 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
612 else if (!varmatch(var
, "bar_font_color", &i
))
613 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
614 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
615 asprintf(&bar_fonts
[0], "%s", val
);
616 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
617 asprintf(&bar_argv
[0], "%s", val
);
618 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
619 bar_delay
= atoi(val
);
620 else if (!bindmatch(var
, "bind", modkey
, val
,
621 &kfid
, &modmask
, &ks
))
622 setkeybinding(modmask
, ks
, kfid
);
628 if (!strncmp(var
, "clock_enabled", strlen("clock_enabled")))
629 clock_enabled
= atoi(val
);
630 else if (!varmatch(var
, "color_focus", &i
))
631 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
632 else if (!varmatch(var
, "color_unfocus", &i
))
633 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
634 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
635 cycle_visible
= atoi(val
);
636 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
637 cycle_visible
= atoi(val
);
643 if (!strncmp(var
, "dialog_ratio",
644 strlen("dialog_ratio"))) {
645 dialog_ratio
= atof(val
);
646 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
653 if (!strncmp(var
, "modkey", strlen("modkey"))) {
655 if (!strncmp(val
, "Mod2", strlen("Mod2")))
657 else if (!strncmp(val
, "Mod3", strlen("Mod3")))
659 else if (!strncmp(val
, "Mod4", strlen("Mod4")))
663 update_modkey(modkey
);
669 if (!strncmp(var
, "region", strlen("region")))
676 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
677 asprintf(&spawn_term
[0], "%s", val
);
678 else if (!strncmp(var
, "screenshot_enabled",
679 strlen("screenshot_enabled")))
680 ss_enabled
= atoi(val
);
681 else if (!strncmp(var
, "screenshot_app",
682 strlen("screenshot_app")))
683 asprintf(&spawn_screenshot
[0], "%s", val
);
689 if (!strncmp(var
, "term_width", strlen("term_width")))
690 term_width
= atoi(val
);
705 errx(1, "invalid conf file entry: %s=%s", var
, val
);
709 socket_setnonblock(int fd
)
713 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
714 err(1, "fcntl F_GETFL");
716 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
717 err(1, "fcntl F_SETFL");
721 bar_print(struct swm_region
*r
, char *s
)
723 XClearWindow(display
, r
->bar_window
);
724 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
725 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
734 bzero(bar_pipe
, sizeof bar_pipe
);
737 kill(bar_pid
, SIGTERM
);
740 strlcpy(bar_ext
, "", sizeof bar_ext
);
749 struct swm_region
*r
;
753 char loc
[SWM_BAR_MAX
];
756 if (bar_enabled
== 0)
758 if (bar_extra
&& bar_extra_running
) {
759 /* ignore short reads; it'll correct itself */
760 while ((b
= fgetln(stdin
, &len
)) != NULL
)
761 if (b
&& b
[len
- 1] == '\n') {
763 strlcpy(bar_ext
, b
, sizeof bar_ext
);
765 if (b
== NULL
&& errno
!= EAGAIN
) {
766 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
767 errno
, strerror(errno
));
771 strlcpy(bar_ext
, "", sizeof bar_ext
);
773 if (clock_enabled
== 0)
774 strlcpy(s
, "", sizeof s
);
777 localtime_r(&tmt
, &tm
);
778 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y ", &tm
);
780 for (i
= 0; i
< ScreenCount(display
); i
++) {
782 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
783 snprintf(loc
, sizeof loc
, "%d:%d %s%s %s",
784 x
++, r
->ws
->idx
+ 1, s
, bar_ext
, bar_vertext
);
788 XSync(display
, False
);
799 bar_toggle(struct swm_region
*r
, union arg
*args
)
801 struct swm_region
*tmpr
;
802 int i
, j
, sc
= ScreenCount(display
);
804 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
807 for (i
= 0; i
< sc
; i
++)
808 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
809 XUnmapWindow(display
, tmpr
->bar_window
);
811 for (i
= 0; i
< sc
; i
++)
812 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
813 XMapRaised(display
, tmpr
->bar_window
);
815 bar_enabled
= !bar_enabled
;
816 for (i
= 0; i
< sc
; i
++)
817 for (j
= 0; j
< SWM_WS_MAX
; j
++)
818 screens
[i
].ws
[j
].restack
= 1;
821 /* must be after stack */
828 XSetWindowAttributes wa
;
829 struct swm_region
*r
;
832 /* do this here because the conf file is in memory */
833 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
834 /* launch external status app */
835 bar_extra_running
= 1;
836 if (pipe(bar_pipe
) == -1)
837 err(1, "pipe error");
838 socket_setnonblock(bar_pipe
[0]);
839 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
840 if (dup2(bar_pipe
[0], 0) == -1)
842 if (dup2(bar_pipe
[1], 1) == -1)
844 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
845 err(1, "could not disable SIGPIPE");
846 switch (bar_pid
= fork()) {
848 err(1, "cannot fork");
852 execvp(bar_argv
[0], bar_argv
);
853 err(1, "%s external app failed", bar_argv
[0]);
855 default: /* parent */
861 bzero(&wa
, sizeof wa
);
862 for (i
= 0; i
< ScreenCount(display
); i
++)
863 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
865 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
866 wa
.background_pixel
=
867 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
868 XChangeWindowAttributes(display
, r
->bar_window
,
869 CWBackPixel
| CWBorderPixel
, &wa
);
875 bar_setup(struct swm_region
*r
)
879 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
880 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
886 if (bar_fonts
[i
] == NULL
)
887 errx(1, "couldn't load font");
888 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
890 r
->bar_window
= XCreateSimpleWindow(display
,
891 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
892 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
893 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
894 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
895 XSetFont(display
, bar_gc
, bar_fs
->fid
);
896 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
898 XMapRaised(display
, r
->bar_window
);
899 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
901 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
902 err(1, "could not install bar_signal");
907 version(struct swm_region
*r
, union arg
*args
)
909 bar_version
= !bar_version
;
911 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
913 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
918 client_msg(struct ws_win
*win
, Atom a
)
920 XClientMessageEvent cm
;
922 bzero(&cm
, sizeof cm
);
923 cm
.type
= ClientMessage
;
925 cm
.message_type
= aprot
;
928 cm
.data
.l
[1] = CurrentTime
;
929 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
933 config_win(struct ws_win
*win
)
937 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
938 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
939 ce
.type
= ConfigureNotify
;
940 ce
.display
= display
;
946 ce
.height
= win
->g
.h
;
947 ce
.border_width
= 1; /* XXX store this! */
949 ce
.override_redirect
= False
;
950 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
954 count_win(struct workspace
*ws
, int count_transient
)
959 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
960 if (count_transient
== 0 && win
->floating
)
962 if (count_transient
== 0 && win
->transient
)
966 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
972 quit(struct swm_region
*r
, union arg
*args
)
974 DNPRINTF(SWM_D_MISC
, "quit\n");
984 for (i
= 0; i
< ScreenCount(display
); i
++)
985 for (j
= 0; j
< SWM_WS_MAX
; j
++)
986 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
987 XUnmapWindow(display
, win
->id
);
991 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
995 event
.display
= display
; /* Ignored, but what the hell */
996 event
.window
= win
->id
;
997 event
.root
= win
->s
->root
;
998 event
.subwindow
= None
;
999 event
.time
= CurrentTime
;
1004 event
.same_screen
= True
;
1005 event
.keycode
= XKeysymToKeycode(display
, keysym
);
1006 event
.state
= modifiers
;
1008 event
.type
= KeyPress
;
1009 XSendEvent(event
.display
, event
.window
, True
,
1010 KeyPressMask
, (XEvent
*)&event
);
1012 event
.type
= KeyRelease
;
1013 XSendEvent(event
.display
, event
.window
, True
,
1014 KeyPressMask
, (XEvent
*)&event
);
1019 restart(struct swm_region
*r
, union arg
*args
)
1021 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
1023 /* disable alarm because the following code may not be interrupted */
1025 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
1026 errx(1, "can't disable alarm");
1031 XCloseDisplay(display
);
1032 execvp(start_argv
[0], start_argv
);
1033 fprintf(stderr
, "execvp failed\n");
1039 root_to_region(Window root
)
1041 struct swm_region
*r
= NULL
;
1043 int i
, x
, y
, wx
, wy
;
1046 for (i
= 0; i
< ScreenCount(display
); i
++)
1047 if (screens
[i
].root
== root
)
1050 if (XQueryPointer(display
, screens
[i
].root
,
1051 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
1052 /* choose a region based on pointer location */
1053 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
1054 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
1055 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
1060 r
= TAILQ_FIRST(&screens
[i
].rl
);
1066 find_window(Window id
)
1071 for (i
= 0; i
< ScreenCount(display
); i
++)
1072 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1073 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1080 spawn(struct swm_region
*r
, union arg
*args
)
1085 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
1087 * The double-fork construct avoids zombie processes and keeps the code
1088 * clean from stupid signal handlers.
1093 close(ConnectionNumber(display
));
1094 setenv("LD_PRELOAD", SWM_LIB
, 1);
1095 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
1096 setenv("_SWM_WS", ret
, 1);
1099 if (asprintf(&ret
, "%d", getpid())) {
1100 setenv("_SWM_PID", ret
, 1);
1104 /* kill stdin, mplayer, ssh-add etc. need that */
1105 si
= open("/dev/null", O_RDONLY
, 0);
1107 err(1, "open /dev/null");
1108 if (dup2(si
, 0) == -1)
1109 err(1, "dup2 /dev/null");
1110 execvp(args
->argv
[0], args
->argv
);
1111 fprintf(stderr
, "execvp failed\n");
1120 spawnterm(struct swm_region
*r
, union arg
*args
)
1122 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1125 setenv("_SWM_XTERM_FONTADJ", "", 1);
1130 spawnmenu(struct swm_region
*r
, union arg
*args
)
1132 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
1134 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
1135 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
1136 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
1137 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
1138 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
1144 unfocus_win(struct ws_win
*win
)
1149 if (win
->ws
->focus
!= win
&& win
->ws
->focus
!= NULL
)
1150 win
->ws
->focus_prev
= win
->ws
->focus
;
1152 if (win
->ws
->r
== NULL
)
1155 grabbuttons(win
, 0);
1156 XSetWindowBorder(display
, win
->id
,
1157 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1159 if (win
->ws
->focus
== win
)
1160 win
->ws
->focus
= NULL
;
1161 if (cur_focus
== win
)
1171 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
1173 for (i
= 0; i
< ScreenCount(display
); i
++)
1174 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1175 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1180 focus_win(struct ws_win
*win
)
1182 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
1188 unfocus_win(cur_focus
);
1189 if (win
->ws
->focus
) {
1190 /* probably shouldn't happen due to the previous unfocus_win */
1191 DNPRINTF(SWM_D_FOCUS
, "unfocusing win->ws->focus: %lu\n",
1192 win
->ws
->focus
->id
);
1193 unfocus_win(win
->ws
->focus
);
1195 win
->ws
->focus
= win
;
1196 if (win
->ws
->r
!= NULL
) {
1198 if (!win
->got_focus
) {
1199 XSetWindowBorder(display
, win
->id
,
1200 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1201 grabbuttons(win
, 1);
1204 XSetInputFocus(display
, win
->id
,
1205 RevertToPointerRoot
, CurrentTime
);
1210 switchws(struct swm_region
*r
, union arg
*args
)
1212 int wsid
= args
->id
;
1213 struct swm_region
*this_r
, *other_r
;
1215 struct workspace
*new_ws
, *old_ws
;
1218 old_ws
= this_r
->ws
;
1219 new_ws
= &this_r
->s
->ws
[wsid
];
1221 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1222 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1225 if (new_ws
== old_ws
)
1228 other_r
= new_ws
->r
;
1230 /* if the other workspace is hidden, switch windows */
1231 /* map new window first to prevent ugly blinking */
1233 old_ws
->restack
= 1;
1235 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1236 XMapRaised(display
, win
->id
);
1238 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1239 XUnmapWindow(display
, win
->id
);
1241 other_r
->ws
= old_ws
;
1242 old_ws
->r
= other_r
;
1244 this_r
->ws
= new_ws
;
1249 if (new_ws
->focus
== NULL
)
1250 new_ws
->focus
= TAILQ_FIRST(&new_ws
->winlist
);
1252 focus_win(new_ws
->focus
);
1258 cyclews(struct swm_region
*r
, union arg
*args
)
1261 struct swm_screen
*s
= r
->s
;
1263 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1264 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1265 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1270 case SWM_ARG_ID_CYCLEWS_UP
:
1271 if (a
.id
< SWM_WS_MAX
- 1)
1276 case SWM_ARG_ID_CYCLEWS_DOWN
:
1280 a
.id
= SWM_WS_MAX
- 1;
1286 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1288 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1292 } while (a
.id
!= r
->ws
->idx
);
1296 cyclescr(struct swm_region
*r
, union arg
*args
)
1298 struct swm_region
*rr
;
1303 case SWM_ARG_ID_CYCLESC_UP
:
1304 rr
= TAILQ_NEXT(r
, entry
);
1306 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1308 case SWM_ARG_ID_CYCLESC_DOWN
:
1309 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1311 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1317 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1318 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1319 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1323 swapwin(struct swm_region
*r
, union arg
*args
)
1325 struct ws_win
*target
, *source
;
1326 struct ws_win_list
*wl
;
1329 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1330 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1331 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1332 if (cur_focus
== NULL
)
1336 wl
= &source
->ws
->winlist
;
1339 case SWM_ARG_ID_SWAPPREV
:
1340 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
1341 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1343 TAILQ_INSERT_TAIL(wl
, source
, entry
);
1345 TAILQ_INSERT_BEFORE(target
, source
, entry
);
1347 case SWM_ARG_ID_SWAPNEXT
:
1348 target
= TAILQ_NEXT(source
, entry
);
1349 TAILQ_REMOVE(wl
, source
, entry
);
1351 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1353 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
1355 case SWM_ARG_ID_SWAPMAIN
:
1356 target
= TAILQ_FIRST(wl
);
1357 if (target
== source
) {
1358 if (source
->ws
->focus_prev
!= NULL
&&
1359 source
->ws
->focus_prev
!= target
)
1361 source
= source
->ws
->focus_prev
;
1365 source
->ws
->focus_prev
= target
;
1366 TAILQ_REMOVE(wl
, target
, entry
);
1367 TAILQ_INSERT_BEFORE(source
, target
, entry
);
1368 TAILQ_REMOVE(wl
, source
, entry
);
1369 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1372 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1381 focus(struct swm_region
*r
, union arg
*args
)
1383 struct ws_win
*winfocus
, *winlostfocus
;
1384 struct ws_win_list
*wl
;
1386 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1387 if (cur_focus
== NULL
)
1390 wl
= &cur_focus
->ws
->winlist
;
1392 winlostfocus
= cur_focus
;
1395 case SWM_ARG_ID_FOCUSPREV
:
1396 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1397 if (winfocus
== NULL
)
1398 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1401 case SWM_ARG_ID_FOCUSNEXT
:
1402 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1403 if (winfocus
== NULL
)
1404 winfocus
= TAILQ_FIRST(wl
);
1407 case SWM_ARG_ID_FOCUSMAIN
:
1408 winfocus
= TAILQ_FIRST(wl
);
1409 if (winfocus
== cur_focus
)
1410 winfocus
= cur_focus
->ws
->focus_prev
;
1411 if (winfocus
== NULL
)
1419 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1422 XMapRaised(display
, winfocus
->id
);
1423 focus_win(winfocus
);
1424 XSync(display
, False
);
1428 cycle_layout(struct swm_region
*r
, union arg
*args
)
1430 struct workspace
*ws
= r
->ws
;
1432 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1435 if (ws
->cur_layout
->l_stack
== NULL
)
1436 ws
->cur_layout
= &layouts
[0];
1442 stack_config(struct swm_region
*r
, union arg
*args
)
1444 struct workspace
*ws
= r
->ws
;
1446 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1449 if (ws
->cur_layout
->l_config
!= NULL
)
1450 ws
->cur_layout
->l_config(ws
, args
->id
);
1452 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1458 struct swm_geometry g
;
1459 struct swm_region
*r
;
1462 DNPRINTF(SWM_D_STACK
, "stack\n");
1464 for (i
= 0; i
< ScreenCount(display
); i
++) {
1466 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1467 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1468 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1470 /* start with screen geometry, adjust for bar */
1480 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1485 XSync(display
, False
);
1489 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1494 bzero(&wc
, sizeof wc
);
1495 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1496 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
== WIDTH(r
)) &&
1497 (win
->g
.h
== HEIGHT(r
)))
1498 wc
.border_width
= 0;
1500 wc
.border_width
= 1;
1501 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1502 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1503 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1505 wc
.width
= win
->g
.w
;
1506 wc
.height
= win
->g
.h
;
1511 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1512 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1515 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1516 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1518 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1522 * Send keystrokes to terminal to decrease/increase the font size as the
1523 * window size changes.
1526 adjust_font(struct ws_win
*win
)
1528 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
1529 win
->floating
|| win
->transient
)
1532 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
1533 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
1534 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
1535 win
->font_size_boundary
[win
->font_steps
] =
1536 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
1539 win
->last_inc
= win
->sh
.width_inc
;
1540 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
1541 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
1542 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
1545 win
->last_inc
= win
->sh
.width_inc
;
1546 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
1550 #define SWAPXY(g) do { \
1552 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1553 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1556 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1559 struct swm_geometry win_g
, r_g
= *g
;
1560 struct ws_win
*win
, *winfocus
;
1561 int i
, j
, s
, stacks
;
1562 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
1563 int hrh
, extra
= 0, h_slice
, last_h
= 0;
1564 int split
, colno
, winno
, mwin
, msize
, mscale
;
1565 int remain
, missing
, v_slice
;;
1568 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1569 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1571 if ((winno
= count_win(ws
, 0)) == 0)
1574 if (ws
->focus
== NULL
)
1575 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1576 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1578 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1579 if (win
->transient
== 0 && win
->floating
== 0)
1586 w_inc
= win
->sh
.width_inc
;
1587 w_base
= win
->sh
.base_width
;
1588 mwin
= ws
->l_state
.horizontal_mwin
;
1589 mscale
= ws
->l_state
.horizontal_msize
;
1590 stacks
= ws
->l_state
.horizontal_stacks
;
1593 w_inc
= win
->sh
.height_inc
;
1594 w_base
= win
->sh
.base_height
;
1595 mwin
= ws
->l_state
.vertical_mwin
;
1596 mscale
= ws
->l_state
.vertical_msize
;
1597 stacks
= ws
->l_state
.vertical_stacks
;
1601 if (stacks
> winno
- mwin
)
1602 stacks
= winno
- mwin
;
1606 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1607 if (mwin
&& winno
> mwin
) {
1608 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1612 win_g
.w
= v_slice
* mscale
;
1614 if (w_inc
> 1 && w_inc
< v_slice
) {
1615 /* adjust for window's requested size increment */
1616 remain
= (win_g
.w
- w_base
) % w_inc
;
1617 missing
= w_inc
- remain
;
1624 win_g
.x
+= r_g
.w
- msize
;
1627 colno
= split
= winno
/ stacks
;
1628 win_g
.w
= ((r_g
.w
- (stacks
* 2) + 2) / stacks
);
1630 hrh
= r_g
.h
/ colno
;
1631 extra
= r_g
.h
- (colno
* hrh
);
1634 /* stack all the tiled windows */
1635 i
= j
= 0, s
= stacks
;
1636 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1637 if (win
->transient
!= 0 || win
->floating
!= 0)
1640 if (split
&& i
== split
) {
1641 colno
= (winno
- mwin
) / stacks
;
1642 if (s
<= (winno
- mwin
) % stacks
)
1644 split
= split
+ colno
;
1645 hrh
= (r_g
.h
/ colno
);
1646 extra
= r_g
.h
- (colno
* hrh
);
1650 win_g
.x
+= win_g
.w
+ 2;
1651 win_g
.w
= (r_g
.w
- msize
- (stacks
* 2)) / stacks
;
1653 win_g
.w
+= (r_g
.w
- msize
- (stacks
* 2)) %
1660 h_inc
= win
->sh
.width_inc
;
1661 h_base
= win
->sh
.base_width
;
1663 h_inc
= win
->sh
.height_inc
;
1664 h_base
= win
->sh
.base_height
;
1666 if (j
== colno
- 1) {
1667 win_g
.h
= hrh
+ extra
;
1668 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1669 /* adjust for window's requested size increment */
1670 remain
= (win_g
.h
- h_base
) % h_inc
;
1671 missing
= h_inc
- remain
;
1673 if (missing
<= extra
|| j
== 0) {
1685 win_g
.y
+= last_h
+ 2;
1687 bzero(&wc
, sizeof wc
);
1688 wc
.border_width
= 1;
1690 win
->g
.x
= wc
.x
= win_g
.y
;
1691 win
->g
.y
= wc
.y
= win_g
.x
;
1692 win
->g
.w
= wc
.width
= win_g
.h
;
1693 win
->g
.h
= wc
.height
= win_g
.w
;
1695 win
->g
.x
= wc
.x
= win_g
.x
;
1696 win
->g
.y
= wc
.y
= win_g
.y
;
1697 win
->g
.w
= wc
.width
= win_g
.w
;
1698 win
->g
.h
= wc
.height
= win_g
.h
;
1701 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1702 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1703 XMapRaised(display
, win
->id
);
1711 /* now, stack all the floaters and transients */
1712 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1713 if (win
->transient
== 0 && win
->floating
== 0)
1716 stack_floater(win
, ws
->r
);
1717 XMapRaised(display
, win
->id
);
1721 focus_win(winfocus
); /* has to be done outside of the loop */
1725 vertical_config(struct workspace
*ws
, int id
)
1727 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1730 case SWM_ARG_ID_STACKRESET
:
1731 case SWM_ARG_ID_STACKINIT
:
1732 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1733 ws
->l_state
.vertical_mwin
= 1;
1734 ws
->l_state
.vertical_stacks
= 1;
1736 case SWM_ARG_ID_MASTERSHRINK
:
1737 if (ws
->l_state
.vertical_msize
> 1)
1738 ws
->l_state
.vertical_msize
--;
1740 case SWM_ARG_ID_MASTERGROW
:
1741 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1742 ws
->l_state
.vertical_msize
++;
1744 case SWM_ARG_ID_MASTERADD
:
1745 ws
->l_state
.vertical_mwin
++;
1747 case SWM_ARG_ID_MASTERDEL
:
1748 if (ws
->l_state
.vertical_mwin
> 0)
1749 ws
->l_state
.vertical_mwin
--;
1751 case SWM_ARG_ID_STACKINC
:
1752 ws
->l_state
.vertical_stacks
++;
1754 case SWM_ARG_ID_STACKDEC
:
1755 if (ws
->l_state
.vertical_stacks
> 1)
1756 ws
->l_state
.vertical_stacks
--;
1764 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1766 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1768 stack_master(ws
, g
, 0, 0);
1772 horizontal_config(struct workspace
*ws
, int id
)
1774 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1777 case SWM_ARG_ID_STACKRESET
:
1778 case SWM_ARG_ID_STACKINIT
:
1779 ws
->l_state
.horizontal_mwin
= 1;
1780 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1781 ws
->l_state
.horizontal_stacks
= 1;
1783 case SWM_ARG_ID_MASTERSHRINK
:
1784 if (ws
->l_state
.horizontal_msize
> 1)
1785 ws
->l_state
.horizontal_msize
--;
1787 case SWM_ARG_ID_MASTERGROW
:
1788 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1789 ws
->l_state
.horizontal_msize
++;
1791 case SWM_ARG_ID_MASTERADD
:
1792 ws
->l_state
.horizontal_mwin
++;
1794 case SWM_ARG_ID_MASTERDEL
:
1795 if (ws
->l_state
.horizontal_mwin
> 0)
1796 ws
->l_state
.horizontal_mwin
--;
1798 case SWM_ARG_ID_STACKINC
:
1799 ws
->l_state
.horizontal_stacks
++;
1801 case SWM_ARG_ID_STACKDEC
:
1802 if (ws
->l_state
.horizontal_stacks
> 1)
1803 ws
->l_state
.horizontal_stacks
--;
1811 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1813 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1815 stack_master(ws
, g
, 1, 0);
1818 /* fullscreen view */
1820 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1822 struct swm_geometry gg
= *g
;
1823 struct ws_win
*win
, *winfocus
;
1826 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1828 if (count_win(ws
, 0) == 0)
1831 if (ws
->focus
== NULL
)
1832 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1833 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1835 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1836 if (win
->transient
!= 0 || win
->floating
!= 0) {
1837 if (win
== ws
->focus
) {
1839 stack_floater(win
, ws
->r
);
1840 XMapRaised(display
, win
->id
);
1842 XUnmapWindow(display
, win
->id
);
1844 bzero(&wc
, sizeof wc
);
1845 wc
.border_width
= 1;
1846 win
->g
.x
= wc
.x
= gg
.x
;
1847 win
->g
.y
= wc
.y
= gg
.y
;
1848 win
->g
.w
= wc
.width
= gg
.w
;
1849 win
->g
.h
= wc
.height
= gg
.h
;
1850 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1851 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1853 if (win
== ws
->focus
) {
1854 XMapRaised(display
, win
->id
);
1856 XUnmapWindow(display
, win
->id
);
1861 focus_win(winfocus
); /* has to be done outside of the loop */
1865 send_to_ws(struct swm_region
*r
, union arg
*args
)
1867 int wsid
= args
->id
;
1868 struct ws_win
*win
= cur_focus
;
1869 struct workspace
*ws
, *nws
;
1870 Atom ws_idx_atom
= 0;
1871 unsigned char ws_idx_str
[SWM_PROPLEN
];
1876 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1879 nws
= &win
->s
->ws
[wsid
];
1881 XUnmapWindow(display
, win
->id
);
1883 /* find a window to focus */
1884 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1885 if (ws
->focus
== NULL
)
1886 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1887 if (ws
->focus
== win
)
1890 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1892 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1895 /* Try to update the window's workspace property */
1896 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1898 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1899 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1901 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1902 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1905 if (count_win(nws
, 1) == 1)
1914 wkill(struct swm_region
*r
, union arg
*args
)
1916 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
1918 if(r
->ws
->focus
== NULL
)
1921 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
1922 XKillClient(display
, r
->ws
->focus
->id
);
1924 if (r
->ws
->focus
->can_delete
)
1925 client_msg(r
->ws
->focus
, adelete
);
1929 screenshot(struct swm_region
*r
, union arg
*args
)
1933 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1935 if (ss_enabled
== 0)
1939 case SWM_ARG_ID_SS_ALL
:
1940 spawn_screenshot
[1] = "full";
1942 case SWM_ARG_ID_SS_WINDOW
:
1943 spawn_screenshot
[1] = "window";
1948 a
.argv
= spawn_screenshot
;
1953 floating_toggle(struct swm_region
*r
, union arg
*args
)
1955 struct ws_win
*win
= cur_focus
;
1960 win
->floating
= !win
->floating
;
1967 resize_window(struct ws_win
*win
, int center
)
1971 struct swm_region
*r
;
1973 r
= root_to_region(win
->wa
.root
);
1974 bzero(&wc
, sizeof wc
);
1975 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1976 wc
.border_width
= 1;
1977 wc
.width
= win
->g
.w
;
1978 wc
.height
= win
->g
.h
;
1979 if (center
== SWM_ARG_ID_CENTER
) {
1980 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1981 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1985 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1986 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1988 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1993 resize(struct ws_win
*win
, union arg
*args
)
1998 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1999 win
->id
, win
->floating
, win
->transient
);
2001 if (!(win
->transient
!= 0 || win
->floating
!= 0))
2004 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
2005 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
2007 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
2009 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
2010 SubstructureRedirectMask
, &ev
);
2012 case ConfigureRequest
:
2015 handler
[ev
.type
](&ev
);
2018 if (ev
.xmotion
.x
<= 1)
2020 if (ev
.xmotion
.y
<= 1)
2022 win
->g
.w
= ev
.xmotion
.x
;
2023 win
->g
.h
= ev
.xmotion
.y
;
2025 /* not free, don't sync more than 60 times / second */
2026 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
2027 time
= ev
.xmotion
.time
;
2028 XSync(display
, False
);
2029 resize_window(win
, args
->id
);
2033 } while (ev
.type
!= ButtonRelease
);
2035 XSync(display
, False
);
2036 resize_window(win
, args
->id
);
2038 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
2040 XUngrabPointer(display
, CurrentTime
);
2043 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
2047 move_window(struct ws_win
*win
)
2051 struct swm_region
*r
;
2053 r
= root_to_region(win
->wa
.root
);
2054 bzero(&wc
, sizeof wc
);
2059 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
2060 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2062 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2067 move(struct ws_win
*win
, union arg
*args
)
2073 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
2074 win
->id
, win
->floating
, win
->transient
);
2076 if (win
->floating
== 0) {
2082 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
2083 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
2085 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
2087 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
2088 SubstructureRedirectMask
, &ev
);
2090 case ConfigureRequest
:
2093 handler
[ev
.type
](&ev
);
2096 win
->g
.x
= ev
.xmotion
.x_root
;
2097 win
->g
.y
= ev
.xmotion
.y_root
;
2099 /* not free, don't sync more than 60 times / second */
2100 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
2101 time
= ev
.xmotion
.time
;
2102 XSync(display
, False
);
2107 } while (ev
.type
!= ButtonRelease
);
2109 XSync(display
, False
);
2112 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
2113 XUngrabPointer(display
, CurrentTime
);
2118 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
2121 /* key definitions */
2123 char name
[SWM_FUNCNAME_LEN
];
2124 void (*func
)(struct swm_region
*r
, union arg
*);
2126 } keyfuncs
[kf_invalid
] = {
2127 /* name function argument */
2128 { "cycle_layout", cycle_layout
, {0} },
2129 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
2130 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
2131 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
2132 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
2133 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
2134 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
2135 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
2136 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
2137 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
2138 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
2139 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
2140 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
2141 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
2142 { "spawn_menu", spawnmenu
, {.argv
= spawn_menu
} },
2143 { "quit", quit
, {0} },
2144 { "restart", restart
, {0} },
2145 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
2146 { "ws_1", switchws
, {.id
= 0} },
2147 { "ws_2", switchws
, {.id
= 1} },
2148 { "ws_3", switchws
, {.id
= 2} },
2149 { "ws_4", switchws
, {.id
= 3} },
2150 { "ws_5", switchws
, {.id
= 4} },
2151 { "ws_6", switchws
, {.id
= 5} },
2152 { "ws_7", switchws
, {.id
= 6} },
2153 { "ws_8", switchws
, {.id
= 7} },
2154 { "ws_9", switchws
, {.id
= 8} },
2155 { "ws_10", switchws
, {.id
= 9} },
2156 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
2157 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
2158 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
2159 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
2160 { "mvws_1", send_to_ws
, {.id
= 0} },
2161 { "mvws_2", send_to_ws
, {.id
= 1} },
2162 { "mvws_3", send_to_ws
, {.id
= 2} },
2163 { "mvws_4", send_to_ws
, {.id
= 3} },
2164 { "mvws_5", send_to_ws
, {.id
= 4} },
2165 { "mvws_6", send_to_ws
, {.id
= 5} },
2166 { "mvws_7", send_to_ws
, {.id
= 6} },
2167 { "mvws_8", send_to_ws
, {.id
= 7} },
2168 { "mvws_9", send_to_ws
, {.id
= 8} },
2169 { "mvws_10", send_to_ws
, {.id
= 9} },
2170 { "bar_toggle", bar_toggle
, {0} },
2171 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
2172 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
2173 { "screenshot_all", screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
2174 { "screenshot_wind", screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
2175 { "float_toggle", floating_toggle
,{0} },
2176 { "version", version
, {0} },
2177 { "spawn_lock", spawn
, {.argv
= spawn_lock
} },
2178 { "spawn_initscr", spawn
, {.argv
= spawn_initscr
} },
2183 enum keyfuncid funcid
;
2185 int keys_size
= 0, keys_length
= 0;
2186 struct key
*keys
= NULL
;
2189 enum { client_click
, root_click
};
2191 unsigned int action
;
2193 unsigned int button
;
2194 void (*func
)(struct ws_win
*, union arg
*);
2197 /* action key mouse button func args */
2198 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
2199 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
2200 { client_click
, MODKEY
, Button1
, move
, {0} },
2204 update_modkey(unsigned int mod
)
2208 for (i
= 0; i
< keys_length
; i
++)
2209 if (keys
[i
].mod
& ShiftMask
)
2210 keys
[i
].mod
= mod
| ShiftMask
;
2214 for (i
= 0; i
< LENGTH(buttons
); i
++)
2215 if (buttons
[i
].mask
& ShiftMask
)
2216 buttons
[i
].mask
= mod
| ShiftMask
;
2218 buttons
[i
].mask
= mod
;
2221 #define SWM_MODNAME_SIZE 32
2222 #define SWM_KEY_WS "\n+ \t"
2224 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
2228 if (mod
== NULL
|| ks
== NULL
)
2232 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
2234 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
2235 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
2237 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
2239 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
2241 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
2243 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
2245 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
2248 *ks
= XStringToKeysym(name
);
2249 XConvertCase(*ks
, ks
, &uks
);
2250 if (ks
== NoSymbol
) {
2252 "parsekeys: invalid key %s\n",
2261 bindmatch(const char *var
, const char *name
, unsigned int currmod
, char *keystr
,
2262 enum keyfuncid
*kfid
, unsigned int *mod
, KeySym
*ks
)
2266 char funcname
[SWM_FUNCNAME_LEN
];
2267 i
= strncmp(var
, name
, 255);
2268 if (kfid
== NULL
|| mod
== NULL
|| ks
== NULL
)
2273 bzero(funcname
, LENGTH(funcname
));
2276 p
= (char *)var
+ strlen(name
);
2280 while (isgraph(*p
) && *p
!= ']' && i
< LENGTH(funcname
))
2281 funcname
[i
++] = *p
++;
2282 if (i
>= LENGTH(funcname
) || *p
!= ']')
2285 return (!parsekeys(keystr
, currmod
, mod
, ks
));
2286 for (*kfid
= 0; *kfid
< kf_invalid
; (*kfid
)++) {
2287 if (strncasecmp(funcname
, keyfuncs
[*kfid
].name
,
2288 SWM_FUNCNAME_LEN
) == 0) {
2289 return (!parsekeys(keystr
, currmod
, mod
, ks
));
2296 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
)
2300 for (i
= 0; i
< keys_length
; i
++) {
2301 if (keys
[i
].mod
== mod
&& keys
[i
].keysym
== ks
) {
2302 if (kfid
== kf_invalid
) {
2305 "setkeybinding: delete #%d %s\n",
2306 i
, keyfuncs
[keys
[i
].funcid
].name
);
2307 j
= keys_length
- 1;
2313 /* found: replace */
2315 "setkeybinding: replace #%d %s\n",
2316 i
, keyfuncs
[keys
[i
].funcid
].name
);
2318 keys
[i
].keysym
= ks
;
2319 keys
[i
].funcid
= kfid
;
2324 if (kfid
== kf_invalid
) {
2326 "error: setkeybinding: cannot find mod/key combination");
2329 /* not found: add */
2330 if (keys_size
== 0 || keys
== NULL
) {
2332 DNPRINTF(SWM_D_KEY
, "setkeybinding: init list %d\n", keys_size
);
2333 keys
= malloc((size_t)keys_size
* sizeof(struct key
));
2335 fprintf(stderr
, "malloc failed\n");
2339 } else if (keys_length
== keys_size
) {
2341 DNPRINTF(SWM_D_KEY
, "setkeybinding: grow list %d\n", keys_size
);
2342 keys
= realloc(keys
, (size_t)keys_size
* sizeof(struct key
));
2344 fprintf(stderr
, "realloc failed\n");
2349 if (keys_length
< keys_size
) {
2350 DNPRINTF(SWM_D_KEY
, "setkeybinding: add %d\n", keys_length
);
2353 keys
[j
].keysym
= ks
;
2354 keys
[j
].funcid
= kfid
;
2356 fprintf(stderr
, "keys array problem?\n");
2358 fprintf(stderr
, "keys array problem\n");
2366 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
);
2367 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
);
2368 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
);
2369 setkeybinding(MODKEY
, XK_l
, kf_master_grow
);
2370 setkeybinding(MODKEY
, XK_comma
, kf_master_add
);
2371 setkeybinding(MODKEY
, XK_period
, kf_master_del
);
2372 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
);
2373 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
);
2374 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
);
2375 setkeybinding(MODKEY
, XK_j
, kf_focus_next
);
2376 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
);
2377 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
);
2378 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_next
);
2379 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
);
2380 setkeybinding(MODKEY
, XK_p
, kf_spawn_menu
);
2381 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
);
2382 setkeybinding(MODKEY
, XK_q
, kf_restart
);
2383 setkeybinding(MODKEY
, XK_m
, kf_focus_main
);
2384 setkeybinding(MODKEY
, XK_1
, kf_ws_1
);
2385 setkeybinding(MODKEY
, XK_2
, kf_ws_2
);
2386 setkeybinding(MODKEY
, XK_3
, kf_ws_3
);
2387 setkeybinding(MODKEY
, XK_4
, kf_ws_4
);
2388 setkeybinding(MODKEY
, XK_5
, kf_ws_5
);
2389 setkeybinding(MODKEY
, XK_6
, kf_ws_6
);
2390 setkeybinding(MODKEY
, XK_7
, kf_ws_7
);
2391 setkeybinding(MODKEY
, XK_8
, kf_ws_8
);
2392 setkeybinding(MODKEY
, XK_9
, kf_ws_9
);
2393 setkeybinding(MODKEY
, XK_0
, kf_ws_10
);
2394 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
);
2395 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
);
2396 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
);
2397 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
);
2398 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
);
2399 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
);
2400 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
);
2401 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
);
2402 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
);
2403 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
);
2404 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
);
2405 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
);
2406 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
);
2407 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
);
2408 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
);
2409 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
);
2410 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
);
2411 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
);
2412 setkeybinding(MODKEY
, XK_x
, kf_wind_del
);
2413 setkeybinding(MODKEY
, XK_s
, kf_screenshot_all
);
2414 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_screenshot_wind
);
2415 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
);
2416 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
);
2417 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_lock
);
2418 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_initscr
);
2421 updatenumlockmask(void)
2424 XModifierKeymap
*modmap
;
2426 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
2428 modmap
= XGetModifierMapping(display
);
2429 for (i
= 0; i
< 8; i
++)
2430 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2431 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2432 == XKeysymToKeycode(display
, XK_Num_Lock
))
2433 numlockmask
= (1 << i
);
2435 XFreeModifiermap(modmap
);
2441 unsigned int i
, j
, k
;
2443 unsigned int modifiers
[] =
2444 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
2446 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
2447 updatenumlockmask();
2449 for (k
= 0; k
< ScreenCount(display
); k
++) {
2450 if (TAILQ_EMPTY(&screens
[k
].rl
))
2452 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
2453 for (i
= 0; i
< keys_length
; i
++) {
2454 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
2455 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2456 XGrabKey(display
, code
,
2457 keys
[i
].mod
| modifiers
[j
],
2458 screens
[k
].root
, True
,
2459 GrabModeAsync
, GrabModeAsync
);
2465 grabbuttons(struct ws_win
*win
, int focused
)
2468 unsigned int modifiers
[] =
2469 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
2471 updatenumlockmask();
2472 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
2474 for (i
= 0; i
< LENGTH(buttons
); i
++)
2475 if (buttons
[i
].action
== client_click
)
2476 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2477 XGrabButton(display
, buttons
[i
].button
,
2478 buttons
[i
].mask
| modifiers
[j
],
2479 win
->id
, False
, BUTTONMASK
,
2480 GrabModeAsync
, GrabModeSync
, None
,
2483 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
2484 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
2490 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
2498 XKeyEvent
*ev
= &e
->xkey
;
2500 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
2502 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
2503 for (i
= 0; i
< keys_length
; i
++)
2504 if (keysym
== keys
[i
].keysym
2505 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
2506 && keyfuncs
[keys
[i
].funcid
].func
)
2507 keyfuncs
[keys
[i
].funcid
].func(
2508 root_to_region(ev
->root
),
2509 &(keyfuncs
[keys
[i
].funcid
].args
)
2514 buttonpress(XEvent
*e
)
2516 XButtonPressedEvent
*ev
= &e
->xbutton
;
2521 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2523 action
= root_click
;
2524 if ((win
= find_window(ev
->window
)) == NULL
)
2528 action
= client_click
;
2531 for (i
= 0; i
< LENGTH(buttons
); i
++)
2532 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2533 buttons
[i
].button
== ev
->button
&&
2534 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2535 buttons
[i
].func(win
, &buttons
[i
].args
);
2539 set_win_state(struct ws_win
*win
, long state
)
2541 long data
[] = {state
, None
};
2543 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2545 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2546 (unsigned char *)data
, 2);
2550 manage_window(Window id
)
2553 struct workspace
*ws
;
2555 int format
, i
, ws_idx
, n
;
2556 unsigned long nitems
, bytes
;
2557 Atom ws_idx_atom
= 0, type
;
2558 Atom
*prot
= NULL
, *pp
;
2559 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2560 struct swm_region
*r
;
2565 if ((win
= find_window(id
)) != NULL
)
2566 return (win
); /* already being managed */
2568 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2569 errx(1, "calloc: failed to allocate memory for new window");
2571 /* Get all the window data in one shot */
2572 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2574 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2575 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2576 XGetWindowAttributes(display
, id
, &win
->wa
);
2577 XGetTransientForHint(display
, id
, &trans
);
2578 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2580 win
->transient
= trans
;
2581 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2582 (unsigned)win
->id
, win
->transient
);
2584 /* get supported protocols */
2585 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
2586 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++)
2588 win
->can_delete
= 1;
2594 * Figure out where to put the window. If it was previously assigned to
2595 * a workspace (either by spawn() or manually moving), and isn't
2596 * transient, * put it in the same workspace
2598 r
= root_to_region(win
->wa
.root
);
2599 if (prop
&& win
->transient
== 0) {
2600 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2601 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2603 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2606 ws
= &r
->s
->ws
[ws_idx
];
2610 /* set up the window layout */
2613 win
->s
= r
->s
; /* this never changes */
2614 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2616 win
->g
.w
= win
->wa
.width
;
2617 win
->g
.h
= win
->wa
.height
;
2618 win
->g
.x
= win
->wa
.x
;
2619 win
->g
.y
= win
->wa
.y
;
2621 /* Set window properties so we can remember this after reincarnation */
2622 if (ws_idx_atom
&& prop
== NULL
&&
2623 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2624 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2626 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2627 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2631 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2632 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2633 win
->ch
.res_class
, win
->ch
.res_name
);
2634 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2635 quirks
[i
].quirk
!= 0; i
++){
2636 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2637 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2638 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2639 win
->ch
.res_class
, win
->ch
.res_name
);
2640 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2642 win
->quirks
= quirks
[i
].quirk
;
2647 /* alter window position if quirky */
2648 if (win
->quirks
& SWM_Q_ANYWHERE
) {
2649 win
->manual
= 1; /* don't center the quirky windows */
2650 bzero(&wc
, sizeof wc
);
2652 if (win
->g
.y
< bar_height
) {
2653 win
->g
.y
= wc
.y
= bar_height
;
2656 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
2657 win
->g
.x
= wc
.x
= WIDTH(win
->ws
->r
) - win
->g
.w
- 2;
2660 wc
.border_width
= 1;
2661 mask
|= CWBorderWidth
;
2662 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2665 /* Reset font sizes (the bruteforce way; no default keybinding). */
2666 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
2667 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
2668 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2669 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
2670 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2673 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2674 PropertyChangeMask
| StructureNotifyMask
);
2676 set_win_state(win
, NormalState
);
2678 /* floaters need to be mapped if they are in the current workspace */
2679 if (win
->floating
&& (ws
->idx
== r
->ws
->idx
))
2680 XMapRaised(display
, win
->id
);
2682 /* make new win focused */
2689 unmanage_window(struct ws_win
*win
)
2691 struct workspace
*ws
;
2696 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
2698 /* don't unmanage if we are switching workspaces */
2703 /* find a window to focus */
2704 if (ws
->focus
== win
)
2705 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2706 if (ws
->focus
== NULL
)
2707 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2708 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2712 focus_win(ws
->focus
);
2713 if (ws
->focus_prev
== win
)
2714 ws
->focus_prev
= NULL
;
2716 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
2717 set_win_state(win
, WithdrawnState
);
2718 if (win
->ch
.res_class
)
2719 XFree(win
->ch
.res_class
);
2720 if (win
->ch
.res_name
)
2721 XFree(win
->ch
.res_name
);
2726 configurerequest(XEvent
*e
)
2728 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2733 if ((win
= find_window(ev
->window
)) == NULL
)
2737 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2739 bzero(&wc
, sizeof wc
);
2742 wc
.width
= ev
->width
;
2743 wc
.height
= ev
->height
;
2744 wc
.border_width
= ev
->border_width
;
2745 wc
.sibling
= ev
->above
;
2746 wc
.stack_mode
= ev
->detail
;
2747 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2749 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2751 if (win
->floating
) {
2752 if (ev
->value_mask
& CWX
)
2754 if (ev
->value_mask
& CWY
)
2756 if (ev
->value_mask
& CWWidth
)
2757 win
->g
.w
= ev
->width
;
2758 if (ev
->value_mask
& CWHeight
)
2759 win
->g
.h
= ev
->height
;
2760 if (win
->ws
->r
!= NULL
) {
2761 /* this seems to be full screen */
2762 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2764 win
->g
.w
= WIDTH(win
->ws
->r
);
2765 ev
->value_mask
|= CWX
| CWWidth
;
2767 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2770 win
->g
.h
= HEIGHT(win
->ws
->r
);
2771 ev
->value_mask
|= CWY
| CWHeight
;
2774 if ((ev
->value_mask
& (CWX
| CWY
)) &&
2775 !(ev
->value_mask
& (CWWidth
| CWHeight
)))
2777 XMoveResizeWindow(display
, win
->id
,
2778 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2785 configurenotify(XEvent
*e
)
2790 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2791 e
->xconfigure
.window
);
2793 XMapWindow(display
, e
->xconfigure
.window
);
2794 win
= find_window(e
->xconfigure
.window
);
2796 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2798 XMapWindow(display
, win
->id
);
2805 destroynotify(XEvent
*e
)
2808 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2810 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2812 if ((win
= find_window(ev
->window
)) != NULL
) {
2813 unmanage_window(win
);
2819 enternotify(XEvent
*e
)
2821 XCrossingEvent
*ev
= &e
->xcrossing
;
2824 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2827 /* eat event(r) to prevent autofocus */
2832 if ((win
= find_window(ev
->window
)) != NULL
)
2839 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2845 DNPRINTF(SWM_D_EVENT
, "focusout: window: %lu\n", e
->xfocus
.window
);
2847 if (cur_focus
&& cur_focus
->ws
->r
&&
2848 cur_focus
->id
== e
->xfocus
.window
) {
2849 struct swm_screen
*s
= cur_focus
->ws
->r
->s
;
2854 /* Try to detect synergy hiding the cursor. */
2855 if (XQueryPointer(display
, cur_focus
->id
,
2856 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
&&
2858 x
== DisplayWidth(display
, s
->idx
)/2 &&
2859 y
== DisplayHeight(display
, s
->idx
)/2) {
2860 unfocus_win(cur_focus
);
2866 mappingnotify(XEvent
*e
)
2868 XMappingEvent
*ev
= &e
->xmapping
;
2870 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2872 XRefreshKeyboardMapping(ev
);
2873 if (ev
->request
== MappingKeyboard
)
2878 maprequest(XEvent
*e
)
2880 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2881 XWindowAttributes wa
;
2883 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2884 e
->xmaprequest
.window
);
2886 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2888 if (wa
.override_redirect
)
2890 manage_window(e
->xmaprequest
.window
);
2896 propertynotify(XEvent
*e
)
2899 XPropertyEvent
*ev
= &e
->xproperty
;
2901 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2904 if (ev
->state
== PropertyDelete
)
2905 return; /* ignore */
2906 win
= find_window(ev
->window
);
2911 case XA_WM_NORMAL_HINTS
:
2914 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2915 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2916 if (win
->sh
.flags
& PMinSize
) {
2917 win
->g
.w
= win
->sh
.min_width
;
2918 win
->g
.h
= win
->sh
.min_height
;
2919 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2921 XMoveResizeWindow(display
, win
->id
,
2922 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2931 unmapnotify(XEvent
*e
)
2933 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2936 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2938 if ((win
= find_window(ev
->window
)) != NULL
)
2940 unmanage_window(win
);
2944 visibilitynotify(XEvent
*e
)
2947 struct swm_region
*r
;
2949 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2950 e
->xvisibility
.window
);
2951 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2952 for (i
= 0; i
< ScreenCount(display
); i
++)
2953 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2954 if (e
->xvisibility
.window
== r
->bar_window
)
2959 xerror_start(Display
*d
, XErrorEvent
*ee
)
2966 xerror(Display
*d
, XErrorEvent
*ee
)
2968 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2976 xerrorxlib
= XSetErrorHandler(xerror_start
);
2978 /* this causes an error if some other window manager is running */
2979 XSelectInput(display
, DefaultRootWindow(display
),
2980 SubstructureRedirectMask
);
2981 XSync(display
, False
);
2985 XSetErrorHandler(xerror
);
2986 XSync(display
, False
);
2995 unsigned char *p
= NULL
;
2996 unsigned long n
, extra
;
2999 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
3000 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
3001 if (status
!= Success
)
3004 result
= *((long *)p
);
3010 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
3012 struct swm_region
*r
, *n
;
3013 struct workspace
*ws
= NULL
;
3016 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
3017 s
->idx
, w
, h
, x
, y
);
3019 /* remove any conflicting regions */
3020 n
= TAILQ_FIRST(&s
->rl
);
3023 n
= TAILQ_NEXT(r
, entry
);
3024 if (X(r
) < (x
+ w
) &&
3025 (X(r
) + WIDTH(r
)) > x
&&
3027 (Y(r
) + HEIGHT(r
)) > y
) {
3028 XDestroyWindow(display
, r
->bar_window
);
3029 TAILQ_REMOVE(&s
->rl
, r
, entry
);
3030 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
3034 /* search old regions for one to reuse */
3036 /* size + location match */
3037 TAILQ_FOREACH(r
, &s
->orl
, entry
)
3038 if (X(r
) == x
&& Y(r
) == y
&&
3039 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
3043 TAILQ_FOREACH(r
, &s
->orl
, entry
)
3044 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
3048 TAILQ_REMOVE(&s
->orl
, r
, entry
);
3049 /* try to use old region's workspace */
3050 if (r
->ws
->r
== NULL
)
3053 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
3054 errx(1, "calloc: failed to allocate memory for screen");
3056 /* if we don't have a workspace already, find one */
3058 for (i
= 0; i
< SWM_WS_MAX
; i
++)
3059 if (s
->ws
[i
].r
== NULL
) {
3066 errx(1, "no free workspaces\n");
3075 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
3081 #ifdef SWM_XRR_HAS_CRTC
3083 XRRScreenResources
*sr
;
3086 #endif /* SWM_XRR_HAS_CRTC */
3087 struct swm_region
*r
;
3090 if (i
>= ScreenCount(display
))
3091 errx(1, "invalid screen");
3093 /* remove any old regions */
3094 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
3096 XDestroyWindow(display
, r
->bar_window
);
3097 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
3098 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
3101 /* map virtual screens onto physical screens */
3102 #ifdef SWM_XRR_HAS_CRTC
3103 if (xrandr_support
) {
3104 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
3106 new_region(&screens
[i
], 0, 0,
3107 DisplayWidth(display
, i
),
3108 DisplayHeight(display
, i
));
3112 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
3113 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
3114 if (ci
->noutput
== 0)
3117 if (ci
!= NULL
&& ci
->mode
== None
)
3118 new_region(&screens
[i
], 0, 0,
3119 DisplayWidth(display
, i
),
3120 DisplayHeight(display
, i
));
3122 new_region(&screens
[i
],
3123 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
3126 XRRFreeCrtcInfo(ci
);
3127 XRRFreeScreenResources(sr
);
3129 #endif /* SWM_XRR_HAS_CRTC */
3131 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
3132 DisplayHeight(display
, i
));
3137 screenchange(XEvent
*e
) {
3138 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
3139 struct swm_region
*r
;
3143 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
3145 if (!XRRUpdateConfiguration(e
))
3148 /* silly event doesn't include the screen index */
3149 for (i
= 0; i
< ScreenCount(display
); i
++)
3150 if (screens
[i
].root
== xe
->root
)
3152 if (i
>= ScreenCount(display
))
3153 errx(1, "screenchange: screen not found\n");
3155 /* brute force for now, just re-enumerate the regions */
3158 /* hide any windows that went away */
3159 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3160 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
3161 XUnmapWindow(display
, win
->id
);
3163 /* add bars to all regions */
3164 for (i
= 0; i
< ScreenCount(display
); i
++)
3165 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3173 Window d1
, d2
, *wins
= NULL
;
3174 XWindowAttributes wa
;
3177 int errorbase
, major
, minor
;
3178 struct workspace
*ws
;
3182 if ((screens
= calloc(ScreenCount(display
),
3183 sizeof(struct swm_screen
))) == NULL
)
3184 errx(1, "calloc: screens");
3186 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3188 /* initial Xrandr setup */
3189 xrandr_support
= XRRQueryExtension(display
,
3190 &xrandr_eventbase
, &errorbase
);
3192 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
3195 /* map physical screens */
3196 for (i
= 0; i
< ScreenCount(display
); i
++) {
3197 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
3199 TAILQ_INIT(&screens
[i
].rl
);
3200 TAILQ_INIT(&screens
[i
].orl
);
3201 screens
[i
].root
= RootWindow(display
, i
);
3203 /* set default colors */
3204 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
3205 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
3206 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
3207 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
3208 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
3210 /* init all workspaces */
3211 /* XXX these should be dynamically allocated too */
3212 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
3213 ws
= &screens
[i
].ws
[j
];
3218 TAILQ_INIT(&ws
->winlist
);
3220 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
3221 if (layouts
[k
].l_config
!= NULL
)
3222 layouts
[k
].l_config(ws
,
3223 SWM_ARG_ID_STACKINIT
);
3224 ws
->cur_layout
= &layouts
[0];
3226 /* grab existing windows (before we build the bars)*/
3227 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
3233 XRRSelectInput(display
, screens
[i
].root
,
3234 RRScreenChangeNotifyMask
);
3236 /* attach windows to a region */
3237 /* normal windows */
3238 for (j
= 0; j
< no
; j
++) {
3239 XGetWindowAttributes(display
, wins
[j
], &wa
);
3240 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
3241 wa
.override_redirect
||
3242 XGetTransientForHint(display
, wins
[j
], &d1
))
3245 if (wa
.map_state
== IsViewable
||
3246 getstate(wins
[j
]) == NormalState
)
3247 manage_window(wins
[j
]);
3249 /* transient windows */
3250 for (j
= 0; j
< no
; j
++) {
3251 if (!XGetWindowAttributes(display
, wins
[j
], &wa
))
3254 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
3255 (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) ==
3257 manage_window(wins
[j
]);
3270 Atom netwmcheck
, netwmname
, utf8_string
;
3273 /* work around sun jdk bugs, code from wmname */
3274 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
3275 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
3276 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
3277 for (i
= 0; i
< ScreenCount(display
); i
++) {
3278 root
= screens
[i
].root
;
3279 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
3280 PropModeReplace
, (unsigned char *)&root
, 1);
3281 XChangeProperty(display
, root
, netwmname
, utf8_string
, 8,
3282 PropModeReplace
, "LG3D", strlen("LG3D"));
3287 main(int argc
, char *argv
[])
3290 struct swm_region
*r
;
3291 char conf
[PATH_MAX
], *cfile
= NULL
;
3298 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
3299 SWM_VERSION
, cvstag
);
3300 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
3301 warnx("no locale support");
3303 if (!(display
= XOpenDisplay(0)))
3304 errx(1, "can not open display");
3307 errx(1, "other wm running");
3309 astate
= XInternAtom(display
, "WM_STATE", False
);
3310 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
3311 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
3313 /* look for local and global conf file */
3314 pwd
= getpwuid(getuid());
3316 errx(1, "invalid user %d", getuid());
3321 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
3322 if (stat(conf
, &sb
) != -1) {
3323 if (S_ISREG(sb
.st_mode
))
3326 /* try global conf file */
3327 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
3328 if (!stat(conf
, &sb
))
3329 if (S_ISREG(sb
.st_mode
))
3335 /* setup all bars */
3336 for (i
= 0; i
< ScreenCount(display
); i
++)
3337 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3340 /* set some values to work around bad programs */
3346 xfd
= ConnectionNumber(display
);
3350 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
3352 errx(1, "select failed");
3357 while (XPending(display
)) {
3358 XNextEvent(display
, &e
);
3359 if (e
.type
< LASTEvent
) {
3360 if (handler
[e
.type
])
3361 handler
[e
.type
](&e
);
3363 DNPRINTF(SWM_D_EVENT
,
3364 "win: %lu unknown event: %d\n",
3365 e
.xany
.window
, e
.type
);
3367 switch (e
.type
- xrandr_eventbase
) {
3368 case RRScreenChangeNotify
:
3372 DNPRINTF(SWM_D_EVENT
,
3373 "win: %lu unknown xrandr event: "
3374 "%d\n", e
.xany
.window
, e
.type
);
3381 XCloseDisplay(display
);