]>
code.delx.au - spectrwm/blob - scrotwm.c
572be960929a8e78c1b9150886e872c9c9d13b87
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 static const char *cvstag
= "$scrotwm$";
53 #define SWM_VERSION "0.7"
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
86 # error XRandR versions less than 1.0 are not supported
91 #define SWM_XRR_HAS_CRTC
95 /* #define SWM_DEBUG */
97 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
98 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
99 #define SWM_D_MISC 0x0001
100 #define SWM_D_EVENT 0x0002
101 #define SWM_D_WS 0x0004
102 #define SWM_D_FOCUS 0x0008
103 #define SWM_D_MOVE 0x0010
104 #define SWM_D_STACK 0x0020
105 #define SWM_D_MOUSE 0x0040
106 #define SWM_D_PROP 0x0080
107 #define SWM_D_CLASS 0x0100
109 u_int32_t swm_debug
= 0
121 #define DPRINTF(x...)
122 #define DNPRINTF(n,x...)
125 #define LENGTH(x) (sizeof x / sizeof x[0])
126 #define MODKEY Mod1Mask
127 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
128 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
129 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
130 #define SWM_PROPLEN (16)
131 #define X(r) (r)->g.x
132 #define Y(r) (r)->g.y
133 #define WIDTH(r) (r)->g.w
134 #define HEIGHT(r) (r)->g.h
137 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
142 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
146 int xrandr_eventbase
;
147 int ignore_enter
= 0;
148 unsigned int numlockmask
= 0;
152 int cycle_visible
= 0;
155 double dialog_ratio
= .6;
157 #define SWM_BAR_MAX (128)
158 char *bar_argv
[] = { NULL
, NULL
};
160 char bar_ext
[SWM_BAR_MAX
];
161 char bar_vertext
[SWM_BAR_MAX
];
163 sig_atomic_t bar_alarm
= 0;
167 int bar_extra_running
= 0;
175 char *bar_fonts
[] = {
176 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
177 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
181 /* terminal + args */
182 char *spawn_term
[] = { "xterm", NULL
};
183 char *spawn_screenshot
[] = { "screenshot.sh", NULL
, NULL
};
184 char *spawn_lock
[] = { "xlock", NULL
};
185 char *spawn_menu
[] = { "dmenu_run", "-fn", NULL
, "-nb", NULL
,
186 "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
188 #define SWM_MENU_FN (2)
189 #define SWM_MENU_NB (4)
190 #define SWM_MENU_NF (6)
191 #define SWM_MENU_SB (8)
192 #define SWM_MENU_SF (10)
194 /* layout manager data */
195 struct swm_geometry
{
205 /* virtual "screens" */
207 TAILQ_ENTRY(swm_region
) entry
;
208 struct swm_geometry g
;
209 struct workspace
*ws
; /* current workspace on this region */
210 struct swm_screen
*s
; /* screen idx */
213 TAILQ_HEAD(swm_region_list
, swm_region
);
216 TAILQ_ENTRY(ws_win
) entry
;
218 struct swm_geometry g
;
223 struct workspace
*ws
; /* always valid */
224 struct swm_screen
*s
; /* always valid, never changes */
225 XWindowAttributes wa
;
229 TAILQ_HEAD(ws_win_list
, ws_win
);
231 /* layout handlers */
233 void vertical_config(struct workspace
*, int);
234 void vertical_stack(struct workspace
*, struct swm_geometry
*);
235 void horizontal_config(struct workspace
*, int);
236 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
237 void max_stack(struct workspace
*, struct swm_geometry
*);
239 void grabbuttons(struct ws_win
*, int);
242 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
243 void (*l_config
)(struct workspace
*, int);
245 /* stack, configure */
246 { vertical_stack
, vertical_config
},
247 { horizontal_stack
, horizontal_config
},
252 #define SWM_H_SLICE (32)
253 #define SWM_V_SLICE (32)
255 /* define work spaces */
257 int idx
; /* workspace index */
258 int restack
; /* restack on switch */
259 struct layout
*cur_layout
; /* current layout handlers */
260 struct ws_win
*focus
; /* may be NULL */
261 struct swm_region
*r
; /* may be NULL */
262 struct ws_win_list winlist
; /* list of windows in ws */
266 int horizontal_msize
;
273 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
274 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
276 /* physical screen mapping */
277 #define SWM_WS_MAX (10) /* XXX Too small? */
279 int idx
; /* screen index */
280 struct swm_region_list rl
; /* list of regions on this screen */
283 struct workspace ws
[SWM_WS_MAX
];
289 } c
[SWM_S_COLOR_MAX
];
291 struct swm_screen
*screens
;
294 struct ws_win
*cur_focus
= NULL
;
296 /* args to functions */
299 #define SWM_ARG_ID_FOCUSNEXT (0)
300 #define SWM_ARG_ID_FOCUSPREV (1)
301 #define SWM_ARG_ID_FOCUSMAIN (2)
302 #define SWM_ARG_ID_SWAPNEXT (3)
303 #define SWM_ARG_ID_SWAPPREV (4)
304 #define SWM_ARG_ID_SWAPMAIN (5)
305 #define SWM_ARG_ID_MASTERSHRINK (6)
306 #define SWM_ARG_ID_MASTERGROW (7)
307 #define SWM_ARG_ID_MASTERADD (8)
308 #define SWM_ARG_ID_MASTERDEL (9)
309 #define SWM_ARG_ID_STACKRESET (10)
310 #define SWM_ARG_ID_STACKINIT (11)
311 #define SWM_ARG_ID_CYCLEWS_UP (12)
312 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
313 #define SWM_ARG_ID_CYCLESC_UP (14)
314 #define SWM_ARG_ID_CYCLESC_DOWN (15)
315 #define SWM_ARG_ID_SS_ALL (0)
316 #define SWM_ARG_ID_SS_WINDOW (1)
317 #define SWM_ARG_ID_DONTCENTER (0)
318 #define SWM_ARG_ID_CENTER (1)
327 #define SWM_Q_FLOAT (1<<0)
329 { "MPlayer", "xv", SWM_Q_FLOAT
},
330 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
},
335 void expose(XEvent
*);
336 void keypress(XEvent
*);
337 void buttonpress(XEvent
*);
338 void configurerequest(XEvent
*);
339 void configurenotify(XEvent
*);
340 void destroynotify(XEvent
*);
341 void enternotify(XEvent
*);
342 void focusin(XEvent
*);
343 void mappingnotify(XEvent
*);
344 void maprequest(XEvent
*);
345 void propertynotify(XEvent
*);
346 void unmapnotify(XEvent
*);
347 void visibilitynotify(XEvent
*);
349 void (*handler
[LASTEvent
])(XEvent
*) = {
351 [KeyPress
] = keypress
,
352 [ButtonPress
] = buttonpress
,
353 [ConfigureRequest
] = configurerequest
,
354 [ConfigureNotify
] = configurenotify
,
355 [DestroyNotify
] = destroynotify
,
356 [EnterNotify
] = enternotify
,
358 [MappingNotify
] = mappingnotify
,
359 [MapRequest
] = maprequest
,
360 [PropertyNotify
] = propertynotify
,
361 [UnmapNotify
] = unmapnotify
,
362 [VisibilityNotify
] = visibilitynotify
,
366 name_to_color(char *colorname
)
370 XColor screen_def
, exact_def
;
371 unsigned long result
= 0;
372 char cname
[32] = "#";
374 cmap
= DefaultColormap(display
, screens
[0].idx
);
375 status
= XAllocNamedColor(display
, cmap
, colorname
,
376 &screen_def
, &exact_def
);
378 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
379 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
383 result
= screen_def
.pixel
;
385 fprintf(stderr
, "color '%s' not found.\n", colorname
);
391 setscreencolor(char *val
, int i
, int c
)
393 if (i
> 0 && i
<= ScreenCount(display
)) {
394 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
395 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
397 } else if (i
== -1) {
398 for (i
= 0; i
< ScreenCount(display
); i
++)
399 screens
[i
].c
[c
].color
= name_to_color(val
);
400 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
403 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
404 i
, ScreenCount(display
));
407 void new_region(struct swm_screen
*, struct workspace
*,
411 custom_region(char *val
)
413 unsigned int sidx
, x
, y
, w
, h
;
415 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
416 errx(1, "invalid custom region, "
417 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
418 if (sidx
< 1 || sidx
> ScreenCount(display
))
419 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
420 sidx
, ScreenCount(display
));
424 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
426 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
427 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
428 w
+ x
> DisplayWidth(display
, sidx
) ||
429 h
+ y
> DisplayHeight(display
, sidx
))
430 errx(1, "region %ux%u+%u+%u not within screen boundaries "
431 "(%ux%u)\n", w
, h
, x
, y
,
432 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
434 new_region(&screens
[sidx
], NULL
, x
, y
, w
, h
);
438 varmatch(char *var
, char *name
, int *index
)
443 i
= strncmp(var
, name
, 255);
450 p
= var
+ strlen(name
);
453 bzero(buf
, sizeof buf
);
455 while (isdigit(*p
) && i
< sizeof buf
)
457 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
459 *index
= strtonum(buf
, 0, 99, NULL
);
463 /* conf file stuff */
464 #define SWM_CONF_WS "\n= \t"
465 #define SWM_CONF_FILE "scrotwm.conf"
467 conf_load(char *filename
)
470 char *line
, *cp
, *var
, *val
;
471 size_t len
, lineno
= 0;
474 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
476 if (filename
== NULL
)
479 if ((config
= fopen(filename
, "r")) == NULL
)
482 for (sc
= ScreenCount(display
);;) {
483 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
487 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
493 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
495 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
496 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
499 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
502 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
503 bar_enabled
= atoi(val
);
504 else if (!varmatch(var
, "bar_border", &i
))
505 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
506 else if (!varmatch(var
, "bar_color", &i
))
507 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
508 else if (!varmatch(var
, "bar_font_color", &i
))
509 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
510 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
511 asprintf(&bar_fonts
[0], "%s", val
);
512 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
513 asprintf(&bar_argv
[0], "%s", val
);
514 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
515 bar_delay
= atoi(val
);
521 if (!varmatch(var
, "color_focus", &i
))
522 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
523 else if (!varmatch(var
, "color_unfocus", &i
))
524 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
525 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
526 cycle_visible
= atoi(val
);
527 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
528 cycle_visible
= atoi(val
);
534 if (!strncmp(var
, "dialog_ratio",
535 strlen("dialog_ratio"))) {
536 dialog_ratio
= atof(val
);
537 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
544 if (!strncmp(var
, "region", strlen("region")))
551 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
552 asprintf(&spawn_term
[0], "%s", val
);
553 if (!strncmp(var
, "screenshot_enabled",
554 strlen("screenshot_enabled")))
555 ss_enabled
= atoi(val
);
556 if (!strncmp(var
, "screenshot_app",
557 strlen("screenshot_app")))
558 asprintf(&spawn_screenshot
[0], "%s", val
);
570 errx(1, "invalid conf file entry: %s=%s", var
, val
);
574 socket_setnonblock(int fd
)
578 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
579 err(1, "fcntl F_GETFL");
581 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
582 err(1, "fcntl F_SETFL");
586 bar_print(struct swm_region
*r
, char *s
)
588 XClearWindow(display
, r
->bar_window
);
589 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
590 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
599 bzero(bar_pipe
, sizeof bar_pipe
);
602 kill(bar_pid
, SIGTERM
);
605 strlcpy(bar_ext
, "", sizeof bar_ext
);
614 struct swm_region
*r
;
618 char loc
[SWM_BAR_MAX
];
621 if (bar_enabled
== 0)
624 if (bar_extra
&& bar_extra_running
) {
625 /* ignore short reads; it'll correct itself */
626 while ((b
= fgetln(stdin
, &len
)) != NULL
)
627 if (b
&& b
[len
- 1] == '\n') {
629 strlcpy(bar_ext
, b
, sizeof bar_ext
);
631 if (b
== NULL
&& errno
!= EAGAIN
) {
632 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
633 errno
, strerror(errno
));
637 strlcpy(bar_ext
, "", sizeof bar_ext
);
640 localtime_r(&tmt
, &tm
);
641 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
642 for (i
= 0; i
< ScreenCount(display
); i
++) {
644 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
645 snprintf(loc
, sizeof loc
, "%s %d:%d %s %s",
646 s
, x
++, r
->ws
->idx
+ 1, bar_ext
, bar_vertext
);
650 XSync(display
, False
);
661 bar_toggle(struct swm_region
*r
, union arg
*args
)
663 struct swm_region
*tmpr
;
664 int i
, j
, sc
= ScreenCount(display
);
666 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
669 for (i
= 0; i
< sc
; i
++)
670 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
671 XUnmapWindow(display
, tmpr
->bar_window
);
673 for (i
= 0; i
< sc
; i
++)
674 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
675 XMapRaised(display
, tmpr
->bar_window
);
677 bar_enabled
= !bar_enabled
;
678 XSync(display
, False
);
679 for (i
= 0; i
< sc
; i
++)
680 for (j
= 0; j
< SWM_WS_MAX
; j
++)
681 screens
[i
].ws
[j
].restack
= 1;
684 /* must be after stack */
685 for (i
= 0; i
< sc
; i
++)
686 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
693 XSetWindowAttributes wa
;
694 struct swm_region
*r
;
697 /* do this here because the conf file is in memory */
698 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
699 /* launch external status app */
700 bar_extra_running
= 1;
701 if (pipe(bar_pipe
) == -1)
702 err(1, "pipe error");
703 socket_setnonblock(bar_pipe
[0]);
704 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
705 if (dup2(bar_pipe
[0], 0) == -1)
707 if (dup2(bar_pipe
[1], 1) == -1)
709 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
710 err(1, "could not disable SIGPIPE");
711 switch (bar_pid
= fork()) {
713 err(1, "cannot fork");
717 execvp(bar_argv
[0], bar_argv
);
718 err(1, "%s external app failed", bar_argv
[0]);
720 default: /* parent */
726 bzero(&wa
, sizeof wa
);
727 for (i
= 0; i
< ScreenCount(display
); i
++)
728 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
730 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
731 wa
.background_pixel
=
732 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
733 XChangeWindowAttributes(display
, r
->bar_window
,
734 CWBackPixel
| CWBorderPixel
, &wa
);
740 bar_setup(struct swm_region
*r
)
744 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
745 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
751 if (bar_fonts
[i
] == NULL
)
752 errx(1, "couldn't load font");
753 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
755 r
->bar_window
= XCreateSimpleWindow(display
,
756 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
757 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
758 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
759 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
760 XSetFont(display
, bar_gc
, bar_fs
->fid
);
761 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
763 XMapRaised(display
, r
->bar_window
);
764 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
766 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
767 err(1, "could not install bar_signal");
772 version(struct swm_region
*r
, union arg
*args
)
774 bar_version
= !bar_version
;
776 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
778 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
783 config_win(struct ws_win
*win
)
787 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
788 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
789 ce
.type
= ConfigureNotify
;
790 ce
.display
= display
;
796 ce
.height
= win
->g
.h
;
797 ce
.border_width
= 1; /* XXX store this! */
799 ce
.override_redirect
= False
;
800 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
804 count_win(struct workspace
*ws
, int count_transient
)
809 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
810 if (count_transient
== 0 && win
->floating
)
812 if (count_transient
== 0 && win
->transient
)
816 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
822 quit(struct swm_region
*r
, union arg
*args
)
824 DNPRINTF(SWM_D_MISC
, "quit\n");
834 for (i
= 0; i
< ScreenCount(display
); i
++)
835 for (j
= 0; j
< SWM_WS_MAX
; j
++)
836 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
837 XUnmapWindow(display
, win
->id
);
841 restart(struct swm_region
*r
, union arg
*args
)
843 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
845 /* disable alarm because the following code may not be interrupted */
847 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
848 errx(1, "can't disable alarm");
853 XCloseDisplay(display
);
854 execvp(start_argv
[0], start_argv
);
855 fprintf(stderr
, "execvp failed\n");
861 root_to_region(Window root
)
863 struct swm_region
*r
;
868 for (i
= 0; i
< ScreenCount(display
); i
++)
869 if (screens
[i
].root
== root
)
872 if (XQueryPointer(display
, screens
[i
].root
,
873 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
874 /* if we can't query the pointer, grab the first region */
875 r
= TAILQ_FIRST(&screens
[i
].rl
);
877 /* otherwise, choose a region based on pointer location */
878 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
879 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
880 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
885 r
= TAILQ_FIRST(&screens
[i
].rl
);
891 find_window(Window id
)
896 for (i
= 0; i
< ScreenCount(display
); i
++)
897 for (j
= 0; j
< SWM_WS_MAX
; j
++)
898 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
905 spawn(struct swm_region
*r
, union arg
*args
)
910 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
912 * The double-fork construct avoids zombie processes and keeps the code
913 * clean from stupid signal handlers.
918 close(ConnectionNumber(display
));
919 setenv("LD_PRELOAD", SWM_LIB
, 1);
920 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
921 setenv("_SWM_WS", ret
, 1);
924 if (asprintf(&ret
, "%d", getpid())) {
925 setenv("_SWM_PID", ret
, 1);
929 /* kill stdin, mplayer, ssh-add etc. need that */
930 si
= open("/dev/null", O_RDONLY
, 0);
932 err(1, "open /dev/null");
933 if (dup2(si
, 0) == -1)
934 err(1, "dup2 /dev/null");
935 execvp(args
->argv
[0], args
->argv
);
936 fprintf(stderr
, "execvp failed\n");
945 spawnmenu(struct swm_region
*r
, union arg
*args
)
947 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
949 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
950 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
951 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
952 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
953 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
964 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
966 for (i
= 0; i
< ScreenCount(display
); i
++)
967 for (j
= 0; j
< SWM_WS_MAX
; j
++)
968 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
969 if (win
->ws
->r
== NULL
)
972 XSetWindowBorder(display
, win
->id
,
973 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
975 win
->ws
->focus
= NULL
;
981 focus_win(struct ws_win
*win
)
983 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
989 win
->ws
->focus
= win
;
990 if (win
->ws
->r
!= NULL
) {
992 if (!win
->got_focus
) {
993 XSetWindowBorder(display
, win
->id
,
994 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
998 XSetInputFocus(display
, win
->id
,
999 RevertToPointerRoot
, CurrentTime
);
1004 switchws(struct swm_region
*r
, union arg
*args
)
1006 int wsid
= args
->id
;
1007 struct swm_region
*this_r
, *other_r
;
1009 struct workspace
*new_ws
, *old_ws
;
1012 old_ws
= this_r
->ws
;
1013 new_ws
= &this_r
->s
->ws
[wsid
];
1015 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1016 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1019 if (new_ws
== old_ws
)
1022 other_r
= new_ws
->r
;
1024 /* if the other workspace is hidden, switch windows */
1025 /* map new window first to prevent ugly blinking */
1026 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1027 XMapRaised(display
, win
->id
);
1029 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1030 XUnmapWindow(display
, win
->id
);
1032 old_ws
->restack
= 1;
1034 other_r
->ws
= old_ws
;
1035 old_ws
->r
= other_r
;
1037 this_r
->ws
= new_ws
;
1043 focus_win(new_ws
->focus
);
1049 cyclews(struct swm_region
*r
, union arg
*args
)
1052 struct swm_screen
*s
= r
->s
;
1054 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1055 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1056 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1061 case SWM_ARG_ID_CYCLEWS_UP
:
1062 if (a
.id
< SWM_WS_MAX
- 1)
1067 case SWM_ARG_ID_CYCLEWS_DOWN
:
1071 a
.id
= SWM_WS_MAX
- 1;
1077 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1079 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1083 } while (a
.id
!= r
->ws
->idx
);
1087 cyclescr(struct swm_region
*r
, union arg
*args
)
1089 struct swm_region
*rr
;
1094 case SWM_ARG_ID_CYCLESC_UP
:
1095 rr
= TAILQ_NEXT(r
, entry
);
1097 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1099 case SWM_ARG_ID_CYCLESC_DOWN
:
1100 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1102 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1108 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1109 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1110 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1114 swapwin(struct swm_region
*r
, union arg
*args
)
1116 struct ws_win
*target
;
1117 struct ws_win_list
*wl
;
1120 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1121 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1122 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1123 if (cur_focus
== NULL
)
1126 wl
= &cur_focus
->ws
->winlist
;
1129 case SWM_ARG_ID_SWAPPREV
:
1130 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1131 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1133 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
1135 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
1137 case SWM_ARG_ID_SWAPNEXT
:
1138 target
= TAILQ_NEXT(cur_focus
, entry
);
1139 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1141 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1143 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
1145 case SWM_ARG_ID_SWAPMAIN
:
1146 target
= TAILQ_FIRST(wl
);
1147 if (target
== cur_focus
)
1149 TAILQ_REMOVE(wl
, target
, entry
);
1150 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
1151 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1152 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1155 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1164 focus(struct swm_region
*r
, union arg
*args
)
1166 struct ws_win
*winfocus
, *winlostfocus
;
1167 struct ws_win_list
*wl
;
1169 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1170 if (cur_focus
== NULL
)
1173 wl
= &cur_focus
->ws
->winlist
;
1175 winlostfocus
= cur_focus
;
1178 case SWM_ARG_ID_FOCUSPREV
:
1179 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1180 if (winfocus
== NULL
)
1181 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1184 case SWM_ARG_ID_FOCUSNEXT
:
1185 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1186 if (winfocus
== NULL
)
1187 winfocus
= TAILQ_FIRST(wl
);
1190 case SWM_ARG_ID_FOCUSMAIN
:
1191 winfocus
= TAILQ_FIRST(wl
);
1198 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1201 XMapRaised(display
, winfocus
->id
);
1202 focus_win(winfocus
);
1203 XSync(display
, False
);
1207 cycle_layout(struct swm_region
*r
, union arg
*args
)
1209 struct workspace
*ws
= r
->ws
;
1211 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1214 if (ws
->cur_layout
->l_stack
== NULL
)
1215 ws
->cur_layout
= &layouts
[0];
1221 stack_config(struct swm_region
*r
, union arg
*args
)
1223 struct workspace
*ws
= r
->ws
;
1225 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1228 if (ws
->cur_layout
->l_config
!= NULL
)
1229 ws
->cur_layout
->l_config(ws
, args
->id
);
1231 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1237 struct swm_geometry g
;
1238 struct swm_region
*r
;
1241 DNPRINTF(SWM_D_STACK
, "stack\n");
1243 for (i
= 0; i
< ScreenCount(display
); i
++) {
1245 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1246 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1247 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1249 /* start with screen geometry, adjust for bar */
1259 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1262 XSync(display
, False
);
1266 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1271 bzero(&wc
, sizeof wc
);
1272 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1273 wc
.border_width
= 1;
1274 if (win
->transient
) {
1275 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1276 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1278 wc
.width
= win
->g
.w
;
1279 wc
.height
= win
->g
.h
;
1284 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1285 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1288 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1289 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1291 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1294 #define SWAPXY(g) do { \
1296 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1297 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1300 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1303 struct swm_geometry win_g
, r_g
= *g
;
1304 struct ws_win
*win
, *winfocus
;
1305 int i
, j
, w_inc
, h_inc
, w_base
, h_base
;
1306 int hrh
, extra
, h_slice
, last_h
= 0;
1307 int split
, colno
, winno
, mwin
, msize
, mscale
;
1308 int remain
, missing
, v_slice
;;
1311 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1312 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1314 if ((winno
= count_win(ws
, 0)) == 0)
1317 if (ws
->focus
== NULL
)
1318 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1319 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1321 win
= TAILQ_FIRST(&ws
->winlist
);
1323 w_inc
= win
->sh
.width_inc
;
1324 w_base
= win
->sh
.base_width
;
1325 mwin
= ws
->l_state
.horizontal_mwin
;
1326 mscale
= ws
->l_state
.horizontal_msize
;
1329 w_inc
= win
->sh
.height_inc
;
1330 w_base
= win
->sh
.base_height
;
1331 mwin
= ws
->l_state
.vertical_mwin
;
1332 mscale
= ws
->l_state
.vertical_msize
;
1336 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1337 if (mwin
&& winno
> mwin
) {
1338 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1342 msize
= v_slice
* mscale
;
1344 if (w_inc
> 1 && w_inc
< v_slice
) {
1345 /* adjust for window's requested size increment */
1346 remain
= (win_g
.w
- w_base
) % w_inc
;
1347 missing
= w_inc
- remain
;
1349 if (missing
<= extra
|| j
== 0) {
1360 win_g
.x
+= r_g
.w
- msize
;
1365 hrh
= r_g
.h
/ colno
;
1366 extra
= r_g
.h
- (colno
* hrh
);
1369 /* stack all the tiled windows */
1371 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1372 if (win
->transient
!= 0 || win
->floating
!= 0)
1375 if (split
&& i
== split
) {
1376 colno
= winno
- split
;
1377 hrh
= (r_g
.h
/ colno
);
1378 extra
= r_g
.h
- (colno
* hrh
);
1382 win_g
.x
+= msize
+ 2;
1383 win_g
.w
= r_g
.w
- (msize
+ 2);
1388 h_inc
= win
->sh
.width_inc
;
1389 h_base
= win
->sh
.base_width
;
1391 h_inc
= win
->sh
.height_inc
;
1392 h_base
= win
->sh
.base_height
;
1394 if (j
== colno
- 1) {
1395 win_g
.h
= hrh
+ extra
;
1396 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1397 /* adjust for window's requested size increment */
1398 remain
= (win_g
.h
- h_base
) % h_inc
;
1399 missing
= h_inc
- remain
;
1401 if (missing
<= extra
|| j
== 0) {
1413 win_g
.y
+= last_h
+ 2;
1415 bzero(&wc
, sizeof wc
);
1416 wc
.border_width
= 1;
1418 win
->g
.x
= wc
.x
= win_g
.y
;
1419 win
->g
.y
= wc
.y
= win_g
.x
;
1420 win
->g
.w
= wc
.width
= win_g
.h
;
1421 win
->g
.h
= wc
.height
= win_g
.w
;
1423 win
->g
.x
= wc
.x
= win_g
.x
;
1424 win
->g
.y
= wc
.y
= win_g
.y
;
1425 win
->g
.w
= wc
.width
= win_g
.w
;
1426 win
->g
.h
= wc
.height
= win_g
.h
;
1428 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1429 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1430 XMapRaised(display
, win
->id
);
1432 fprintf(stderr, "vertical_stack: win %d x %d y %d w %d h %d bw %d\n", win->id, win->g.x, win->g.y, win->g.w , win->g.h, wc.border_width);
1440 /* now, stack all the floaters and transients */
1441 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1442 if (win
->transient
== 0 && win
->floating
== 0)
1445 stack_floater(win
, ws
->r
);
1446 XMapRaised(display
, win
->id
);
1450 focus_win(winfocus
); /* has to be done outside of the loop */
1454 vertical_config(struct workspace
*ws
, int id
)
1456 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1459 case SWM_ARG_ID_STACKRESET
:
1460 case SWM_ARG_ID_STACKINIT
:
1461 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1462 ws
->l_state
.vertical_mwin
= 1;
1464 case SWM_ARG_ID_MASTERSHRINK
:
1465 if (ws
->l_state
.vertical_msize
> 1)
1466 ws
->l_state
.vertical_msize
--;
1468 case SWM_ARG_ID_MASTERGROW
:
1469 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1470 ws
->l_state
.vertical_msize
++;
1472 case SWM_ARG_ID_MASTERADD
:
1473 ws
->l_state
.vertical_mwin
++;
1475 case SWM_ARG_ID_MASTERDEL
:
1476 if (ws
->l_state
.vertical_mwin
> 0)
1477 ws
->l_state
.vertical_mwin
--;
1485 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1487 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1489 stack_master(ws
, g
, 0, 0);
1493 horizontal_config(struct workspace
*ws
, int id
)
1495 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1498 case SWM_ARG_ID_STACKRESET
:
1499 case SWM_ARG_ID_STACKINIT
:
1500 ws
->l_state
.horizontal_mwin
= 1;
1501 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1503 case SWM_ARG_ID_MASTERSHRINK
:
1504 if (ws
->l_state
.horizontal_msize
> 1)
1505 ws
->l_state
.horizontal_msize
--;
1507 case SWM_ARG_ID_MASTERGROW
:
1508 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1509 ws
->l_state
.horizontal_msize
++;
1511 case SWM_ARG_ID_MASTERADD
:
1512 ws
->l_state
.horizontal_mwin
++;
1514 case SWM_ARG_ID_MASTERDEL
:
1515 if (ws
->l_state
.horizontal_mwin
> 0)
1516 ws
->l_state
.horizontal_mwin
--;
1524 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1526 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1528 stack_master(ws
, g
, 1, 0);
1531 /* fullscreen view */
1533 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1535 struct swm_geometry gg
= *g
;
1536 struct ws_win
*win
, *winfocus
;
1539 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1541 if (count_win(ws
, 0) == 0)
1544 if (ws
->focus
== NULL
)
1545 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1546 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1548 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1549 if (win
->transient
!= 0 || win
->floating
!= 0) {
1550 if (win
== ws
->focus
) {
1552 stack_floater(win
, ws
->r
);
1553 XMapRaised(display
, win
->id
);
1555 XUnmapWindow(display
, win
->id
);
1557 bzero(&wc
, sizeof wc
);
1558 wc
.border_width
= 1;
1559 win
->g
.x
= wc
.x
= gg
.x
;
1560 win
->g
.y
= wc
.y
= gg
.y
;
1561 win
->g
.w
= wc
.width
= gg
.w
;
1562 win
->g
.h
= wc
.height
= gg
.h
;
1563 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1564 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1566 if (win
== ws
->focus
) {
1567 XMapRaised(display
, win
->id
);
1569 XUnmapWindow(display
, win
->id
);
1574 focus_win(winfocus
); /* has to be done outside of the loop */
1578 send_to_ws(struct swm_region
*r
, union arg
*args
)
1580 int wsid
= args
->id
;
1581 struct ws_win
*win
= cur_focus
;
1582 struct workspace
*ws
, *nws
;
1583 Atom ws_idx_atom
= 0;
1584 unsigned char ws_idx_str
[SWM_PROPLEN
];
1589 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1592 nws
= &win
->s
->ws
[wsid
];
1594 XUnmapWindow(display
, win
->id
);
1596 /* find a window to focus */
1597 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1598 if (ws
->focus
== NULL
)
1599 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1600 if (ws
->focus
== win
)
1603 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1605 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1608 /* Try to update the window's workspace property */
1609 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1611 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1612 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1614 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1615 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1618 if (count_win(nws
, 1) == 1)
1627 wkill(struct swm_region
*r
, union arg
*args
)
1629 DNPRINTF(SWM_D_MISC
, "wkill\n");
1630 if(r
->ws
->focus
!= NULL
)
1631 XKillClient(display
, r
->ws
->focus
->id
);
1635 screenshot(struct swm_region
*r
, union arg
*args
)
1639 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1641 if (ss_enabled
== 0)
1645 case SWM_ARG_ID_SS_ALL
:
1646 spawn_screenshot
[1] = "full";
1648 case SWM_ARG_ID_SS_WINDOW
:
1649 spawn_screenshot
[1] = "window";
1654 a
.argv
= spawn_screenshot
;
1659 floating_toggle(struct swm_region
*r
, union arg
*args
)
1661 struct ws_win
*win
= cur_focus
;
1666 win
->floating
= !win
->floating
;
1672 /* key definitions */
1676 void (*func
)(struct swm_region
*r
, union arg
*);
1679 /* modifier key function argument */
1680 { MODKEY
, XK_space
, cycle_layout
, {0} },
1681 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1682 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1683 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1684 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1685 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1686 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1687 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1688 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1689 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1690 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1691 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1692 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1693 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1694 { MODKEY
, XK_q
, restart
, {0} },
1695 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1696 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1697 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1698 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1699 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1700 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1701 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1702 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1703 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1704 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1705 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1706 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1707 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1708 { MODKEY
| ShiftMask
, XK_Right
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1709 { MODKEY
| ShiftMask
, XK_Left
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1710 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1711 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1712 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1713 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1714 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1715 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1716 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1717 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1718 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1719 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1720 { MODKEY
, XK_b
, bar_toggle
, {0} },
1721 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1722 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1723 { MODKEY
| ShiftMask
, XK_x
, wkill
, {0} },
1724 { MODKEY
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
1725 { MODKEY
| ShiftMask
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
1726 { MODKEY
, XK_t
, floating_toggle
,{0} },
1727 { MODKEY
| ShiftMask
, XK_v
, version
, {0} },
1728 { MODKEY
| ShiftMask
, XK_Delete
, spawn
, {.argv
= spawn_lock
} },
1732 resize_window(struct ws_win
*win
, int center
)
1736 struct swm_region
*r
;
1738 r
= root_to_region(win
->wa
.root
);
1739 bzero(&wc
, sizeof wc
);
1740 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1741 wc
.border_width
= 1;
1742 wc
.width
= win
->g
.w
;
1743 wc
.height
= win
->g
.h
;
1744 if (center
== SWM_ARG_ID_CENTER
) {
1745 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1746 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1750 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1751 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1753 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1758 resize(struct ws_win
*win
, union arg
*args
)
1763 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1764 win
->id
, win
->floating
, win
->transient
);
1766 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1769 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1770 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1772 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1774 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1775 SubstructureRedirectMask
, &ev
);
1777 case ConfigureRequest
:
1780 handler
[ev
.type
](&ev
);
1783 if (ev
.xmotion
.x
< 0)
1785 if (ev
.xmotion
.y
< 0)
1787 win
->g
.w
= ev
.xmotion
.x
;
1788 win
->g
.h
= ev
.xmotion
.y
;
1790 /* not free, don't sync more than 60 times / second */
1791 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1792 time
= ev
.xmotion
.time
;
1793 XSync(display
, False
);
1794 resize_window(win
, args
->id
);
1798 } while (ev
.type
!= ButtonRelease
);
1800 XSync(display
, False
);
1801 resize_window(win
, args
->id
);
1803 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1805 XUngrabPointer(display
, CurrentTime
);
1808 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1812 move_window(struct ws_win
*win
)
1816 struct swm_region
*r
;
1818 r
= root_to_region(win
->wa
.root
);
1819 bzero(&wc
, sizeof wc
);
1824 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
1825 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1827 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1832 move(struct ws_win
*win
, union arg
*args
)
1838 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
1839 win
->id
, win
->floating
, win
->transient
);
1841 if (win
->floating
== 0) {
1847 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1848 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1850 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1852 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1853 SubstructureRedirectMask
, &ev
);
1855 case ConfigureRequest
:
1858 handler
[ev
.type
](&ev
);
1861 win
->g
.x
= ev
.xmotion
.x_root
;
1862 win
->g
.y
= ev
.xmotion
.y_root
;
1864 /* not free, don't sync more than 60 times / second */
1865 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1866 time
= ev
.xmotion
.time
;
1867 XSync(display
, False
);
1872 } while (ev
.type
!= ButtonRelease
);
1874 XSync(display
, False
);
1877 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1878 XUngrabPointer(display
, CurrentTime
);
1883 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1887 enum { client_click
, root_click
};
1889 unsigned int action
;
1891 unsigned int button
;
1892 void (*func
)(struct ws_win
*, union arg
*);
1895 /* action key mouse button func args */
1896 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
1897 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
1898 { client_click
, MODKEY
, Button1
, move
, {0} },
1902 updatenumlockmask(void)
1905 XModifierKeymap
*modmap
;
1907 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1909 modmap
= XGetModifierMapping(display
);
1910 for (i
= 0; i
< 8; i
++)
1911 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1912 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1913 == XKeysymToKeycode(display
, XK_Num_Lock
))
1914 numlockmask
= (1 << i
);
1916 XFreeModifiermap(modmap
);
1922 unsigned int i
, j
, k
;
1924 unsigned int modifiers
[] =
1925 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1927 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1928 updatenumlockmask();
1930 for (k
= 0; k
< ScreenCount(display
); k
++) {
1931 if (TAILQ_EMPTY(&screens
[k
].rl
))
1933 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1934 for (i
= 0; i
< LENGTH(keys
); i
++) {
1935 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1936 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1937 XGrabKey(display
, code
,
1938 keys
[i
].mod
| modifiers
[j
],
1939 screens
[k
].root
, True
,
1940 GrabModeAsync
, GrabModeAsync
);
1946 grabbuttons(struct ws_win
*win
, int focused
)
1949 unsigned int modifiers
[] =
1950 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1952 updatenumlockmask();
1953 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
1955 for (i
= 0; i
< LENGTH(buttons
); i
++)
1956 if (buttons
[i
].action
== client_click
)
1957 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1958 XGrabButton(display
, buttons
[i
].button
,
1959 buttons
[i
].mask
| modifiers
[j
],
1960 win
->id
, False
, BUTTONMASK
,
1961 GrabModeAsync
, GrabModeSync
, None
,
1964 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
1965 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1971 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1979 XKeyEvent
*ev
= &e
->xkey
;
1981 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1983 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1984 for (i
= 0; i
< LENGTH(keys
); i
++)
1985 if (keysym
== keys
[i
].keysym
1986 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1988 keys
[i
].func(root_to_region(ev
->root
),
1993 buttonpress(XEvent
*e
)
1995 XButtonPressedEvent
*ev
= &e
->xbutton
;
2000 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2002 action
= root_click
;
2003 if ((win
= find_window(ev
->window
)) == NULL
)
2007 action
= client_click
;
2010 for (i
= 0; i
< LENGTH(buttons
); i
++)
2011 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2012 buttons
[i
].button
== ev
->button
&&
2013 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2014 buttons
[i
].func(win
, &buttons
[i
].args
);
2018 set_win_state(struct ws_win
*win
, long state
)
2020 long data
[] = {state
, None
};
2022 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2024 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2025 (unsigned char *)data
, 2);
2029 manage_window(Window id
)
2032 struct workspace
*ws
;
2034 int format
, i
, ws_idx
;
2035 unsigned long nitems
, bytes
;
2036 Atom ws_idx_atom
= 0, type
;
2037 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2038 struct swm_region
*r
;
2042 if ((win
= find_window(id
)) != NULL
)
2043 return (win
); /* already being managed */
2045 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2046 errx(1, "calloc: failed to allocate memory for new window");
2048 /* Get all the window data in one shot */
2049 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2051 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2052 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2053 XGetWindowAttributes(display
, id
, &win
->wa
);
2054 XGetTransientForHint(display
, id
, &trans
);
2055 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2057 win
->transient
= trans
;
2058 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2059 (unsigned)win
->id
, win
->transient
);
2063 * Figure out where to put the window. If it was previously assigned to
2064 * a workspace (either by spawn() or manually moving), and isn't
2065 * transient, * put it in the same workspace
2067 r
= root_to_region(win
->wa
.root
);
2068 if (prop
&& win
->transient
== 0) {
2069 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2070 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2072 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2075 ws
= &r
->s
->ws
[ws_idx
];
2079 /* set up the window layout */
2082 win
->s
= r
->s
; /* this never changes */
2083 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2085 win
->g
.w
= win
->wa
.width
;
2086 win
->g
.h
= win
->wa
.height
;
2087 win
->g
.x
= win
->wa
.x
;
2088 win
->g
.y
= win
->wa
.y
;
2090 /* Set window properties so we can remember this after reincarnation */
2091 if (ws_idx_atom
&& prop
== NULL
&&
2092 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2093 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2095 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2096 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2101 fprintf(stderr, "manage window: %d x %d y %d w %d h %d\n", win->id, win->g.x, win->g.y, win->g.w, win->g.h);
2104 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2105 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2106 win
->ch
.res_class
, win
->ch
.res_name
);
2107 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2108 quirks
[i
].quirk
!= 0; i
++){
2109 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2110 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2111 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2112 win
->ch
.res_class
, win
->ch
.res_name
);
2113 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2119 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2120 PropertyChangeMask
| StructureNotifyMask
);
2122 set_win_state(win
, NormalState
);
2124 /* make new win focused */
2131 configurerequest(XEvent
*e
)
2133 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2138 if ((win
= find_window(ev
->window
)) == NULL
)
2142 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2145 fprintf(stderr, "configurerequest: new window: %lu x %d y %d w %d h %d bw %d s %d sm %d\n",
2146 ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width, ev->above, ev->detail);
2148 bzero(&wc
, sizeof wc
);
2151 wc
.width
= ev
->width
;
2152 wc
.height
= ev
->height
;
2153 wc
.border_width
= ev
->border_width
;
2154 wc
.sibling
= ev
->above
;
2155 wc
.stack_mode
= ev
->detail
;
2156 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2159 fprintf(stderr, "configurerequest: change window: %lu\n",
2162 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2164 if (win
->floating
) {
2165 if (ev
->value_mask
& CWX
)
2167 if (ev
->value_mask
& CWY
)
2169 if (ev
->value_mask
& CWWidth
)
2170 win
->g
.w
= ev
->width
;
2171 if (ev
->value_mask
& CWHeight
)
2172 win
->g
.h
= ev
->height
;
2173 if (win
->ws
->r
!= NULL
) {
2174 /* this seems to be full screen */
2175 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2177 win
->g
.w
= WIDTH(win
->ws
->r
);
2178 ev
->value_mask
|= CWX
| CWWidth
;
2180 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2183 win
->g
.h
= HEIGHT(win
->ws
->r
);
2184 ev
->value_mask
|= CWY
| CWHeight
;
2187 if ((ev
->value_mask
& (CWX
|CWY
)) &&
2188 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
2190 XMoveResizeWindow(display
, win
->id
,
2191 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2198 configurenotify(XEvent
*e
)
2200 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2201 e
->xconfigure
.window
);
2205 destroynotify(XEvent
*e
)
2208 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2209 struct workspace
*ws
;
2211 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2213 if ((win
= find_window(ev
->window
)) != NULL
) {
2215 /* find a window to focus */
2216 if (ws
->focus
== win
)
2217 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2218 if (ws
->focus
== NULL
)
2219 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2220 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2224 focus_win(ws
->focus
);
2225 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
2226 set_win_state(win
, WithdrawnState
);
2227 if (win
->ch
.res_class
)
2228 XFree(win
->ch
.res_class
);
2229 if (win
->ch
.res_name
)
2230 XFree(win
->ch
.res_name
);
2237 enternotify(XEvent
*e
)
2239 XCrossingEvent
*ev
= &e
->xcrossing
;
2242 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2245 /* eat event(r) to prevent autofocus */
2250 if ((win
= find_window(ev
->window
)) != NULL
)
2257 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2261 mappingnotify(XEvent
*e
)
2263 XMappingEvent
*ev
= &e
->xmapping
;
2265 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2267 XRefreshKeyboardMapping(ev
);
2268 if (ev
->request
== MappingKeyboard
)
2273 maprequest(XEvent
*e
)
2275 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2276 XWindowAttributes wa
;
2278 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2279 e
->xmaprequest
.window
);
2281 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2283 if (wa
.override_redirect
)
2285 manage_window(e
->xmaprequest
.window
);
2290 propertynotify(XEvent
*e
)
2293 XPropertyEvent
*ev
= &e
->xproperty
;
2295 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2298 if (ev
->state
== PropertyDelete
)
2299 return; /* ignore */
2300 win
= find_window(ev
->window
);
2305 case XA_WM_NORMAL_HINTS
:
2308 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2309 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2310 if (win
->sh
.flags
& PMinSize
) {
2311 win
->g
.w
= win
->sh
.min_width
;
2312 win
->g
.h
= win
->sh
.min_height
;
2313 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2315 XMoveResizeWindow(display
, win
->id
,
2316 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2325 unmapnotify(XEvent
*e
)
2327 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2331 visibilitynotify(XEvent
*e
)
2334 struct swm_region
*r
;
2336 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2337 e
->xvisibility
.window
);
2338 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2339 for (i
= 0; i
< ScreenCount(display
); i
++)
2340 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2341 if (e
->xvisibility
.window
== r
->bar_window
)
2346 xerror_start(Display
*d
, XErrorEvent
*ee
)
2353 xerror(Display
*d
, XErrorEvent
*ee
)
2355 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2363 xerrorxlib
= XSetErrorHandler(xerror_start
);
2365 /* this causes an error if some other window manager is running */
2366 XSelectInput(display
, DefaultRootWindow(display
),
2367 SubstructureRedirectMask
);
2368 XSync(display
, False
);
2372 XSetErrorHandler(xerror
);
2373 XSync(display
, False
);
2382 unsigned char *p
= NULL
;
2383 unsigned long n
, extra
;
2386 astate
= XInternAtom(display
, "WM_STATE", False
);
2387 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
2388 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
2389 if (status
!= Success
)
2398 remove_region(struct swm_region
*r
)
2400 struct swm_screen
*s
= r
->s
;
2403 DNPRINTF(SWM_D_MISC
, "removing region: screen[%d]:%dx%d+%d+%d\n",
2404 s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
));
2406 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
2407 XUnmapWindow(display
, win
->id
);
2410 XDestroyWindow(display
, r
->bar_window
);
2411 TAILQ_REMOVE(&s
->rl
, r
, entry
);
2416 new_region(struct swm_screen
*s
, struct workspace
*ws
,
2417 int x
, int y
, int w
, int h
)
2419 struct swm_region
*r
, *n
;
2422 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
2423 s
->idx
, w
, h
, x
, y
);
2425 /* remove any conflicting regions */
2426 n
= TAILQ_FIRST(&s
->rl
);
2429 n
= TAILQ_NEXT(r
, entry
);
2430 if (X(r
) < (x
+ w
) &&
2431 (X(r
) + WIDTH(r
)) > x
&&
2433 (Y(r
) + HEIGHT(r
)) > y
) {
2440 /* pick an appropriate workspace */
2442 for (i
= 0; i
< SWM_WS_MAX
; i
++)
2443 if (s
->ws
[i
].r
== NULL
) {
2449 errx(1, "no free regions\n");
2452 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
2453 errx(1, "calloc: failed to allocate memory for screen");
2462 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
2469 #ifdef SWM_XRR_HAS_CRTC
2471 XRRScreenResources
*sr
;
2473 #endif /* SWM_XRR_HAS_CRTC */
2474 Window d1
, d2
, *wins
= NULL
;
2475 XWindowAttributes wa
;
2476 struct swm_region
*r
;
2478 int errorbase
, major
, minor
;
2479 int ncrtc
= 0, w
= 0;
2481 struct workspace
*ws
;
2485 if ((screens
= calloc(ScreenCount(display
),
2486 sizeof(struct swm_screen
))) == NULL
)
2487 errx(1, "calloc: screens");
2489 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2492 /* map physical screens */
2493 for (i
= 0; i
< ScreenCount(display
); i
++) {
2494 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
2496 TAILQ_INIT(&screens
[i
].rl
);
2497 screens
[i
].root
= RootWindow(display
, i
);
2499 /* set default colors */
2500 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
2501 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
2502 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
2503 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
2504 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
2506 /* init all workspaces */
2507 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
2508 ws
= &screens
[i
].ws
[j
];
2513 TAILQ_INIT(&ws
->winlist
);
2515 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
2516 if (layouts
[k
].l_config
!= NULL
)
2517 layouts
[k
].l_config(ws
,
2518 SWM_ARG_ID_STACKINIT
);
2519 ws
->cur_layout
= &layouts
[0];
2522 /* map virtual screens onto physical screens */
2523 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
2524 &xrandr_eventbase
, &errorbase
);
2525 if (screens
[i
].xrandr_support
)
2526 if (XRRQueryVersion(display
, &major
, &minor
) &&
2528 screens
[i
].xrandr_support
= 0;
2530 #if 0 /* not ready for dynamic screen changes */
2531 if (screens
[i
].xrandr_support
)
2532 XRRSelectInput(display
,
2534 RRScreenChangeNotifyMask
);
2537 /* grab existing windows (before we build the bars)*/
2538 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
2541 #ifdef SWM_XRR_HAS_CRTC
2542 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
2544 new_region(&screens
[i
], &screens
[i
].ws
[w
],
2545 0, 0, DisplayWidth(display
, i
),
2546 DisplayHeight(display
, i
));
2550 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
2551 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
2552 if (ci
->noutput
== 0)
2555 if (ci
!= NULL
&& ci
->mode
== None
)
2556 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
2557 DisplayWidth(display
, i
),
2558 DisplayHeight(display
, i
));
2560 new_region(&screens
[i
], &screens
[i
].ws
[w
],
2561 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
2565 XRRFreeCrtcInfo(ci
);
2566 XRRFreeScreenResources(sr
);
2568 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
2569 DisplayWidth(display
, i
),
2570 DisplayHeight(display
, i
));
2571 #endif /* SWM_XRR_HAS_CRTC */
2573 /* attach windows to a region */
2574 /* normal windows */
2575 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
2576 errx(1, "no regions on screen %d", i
);
2578 for (i
= 0; i
< no
; i
++) {
2579 XGetWindowAttributes(display
, wins
[i
], &wa
);
2580 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
2581 wa
.override_redirect
||
2582 XGetTransientForHint(display
, wins
[i
], &d1
))
2585 if (wa
.map_state
== IsViewable
||
2586 getstate(wins
[i
]) == NormalState
)
2587 manage_window(wins
[i
]);
2589 /* transient windows */
2590 for (i
= 0; i
< no
; i
++) {
2591 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
2594 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
2595 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
2597 manage_window(wins
[i
]);
2607 main(int argc
, char *argv
[])
2610 char conf
[PATH_MAX
], *cfile
= NULL
;
2617 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
2618 SWM_VERSION
, cvstag
);
2619 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2620 warnx("no locale support");
2622 if (!(display
= XOpenDisplay(0)))
2623 errx(1, "can not open display");
2626 errx(1, "other wm running");
2628 astate
= XInternAtom(display
, "WM_STATE", False
);
2630 /* look for local and global conf file */
2631 pwd
= getpwuid(getuid());
2633 errx(1, "invalid user %d", getuid());
2637 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2638 if (stat(conf
, &sb
) != -1) {
2639 if (S_ISREG(sb
.st_mode
))
2642 /* try global conf file */
2643 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2644 if (!stat(conf
, &sb
))
2645 if (S_ISREG(sb
.st_mode
))
2652 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2657 xfd
= ConnectionNumber(display
);
2661 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2663 errx(1, "select failed");
2668 while(XPending(display
)) {
2669 XNextEvent(display
, &e
);
2670 if (handler
[e
.type
])
2671 handler
[e
.type
](&e
);
2675 XCloseDisplay(display
);