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.8"
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
*);
147 int xrandr_eventbase
;
148 int ignore_enter
= 0;
149 unsigned int numlockmask
= 0;
153 int cycle_visible
= 0;
156 double dialog_ratio
= .6;
158 #define SWM_BAR_MAX (128)
159 char *bar_argv
[] = { NULL
, NULL
};
161 char bar_ext
[SWM_BAR_MAX
];
162 char bar_vertext
[SWM_BAR_MAX
];
164 sig_atomic_t bar_alarm
= 0;
168 int bar_extra_running
= 0;
176 char *bar_fonts
[] = {
177 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
178 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
182 /* terminal + args */
183 char *spawn_term
[] = { "xterm", NULL
};
184 char *spawn_screenshot
[] = { "screenshot.sh", NULL
, NULL
};
185 char *spawn_lock
[] = { "xlock", NULL
};
186 char *spawn_initscr
[] = { "initscreen.sh", NULL
};
187 char *spawn_menu
[] = { "dmenu_run", "-fn", NULL
, "-nb", NULL
,
188 "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
190 #define SWM_MENU_FN (2)
191 #define SWM_MENU_NB (4)
192 #define SWM_MENU_NF (6)
193 #define SWM_MENU_SB (8)
194 #define SWM_MENU_SF (10)
196 /* layout manager data */
197 struct swm_geometry
{
207 /* virtual "screens" */
209 TAILQ_ENTRY(swm_region
) entry
;
210 struct swm_geometry g
;
211 struct workspace
*ws
; /* current workspace on this region */
212 struct swm_screen
*s
; /* screen idx */
215 TAILQ_HEAD(swm_region_list
, swm_region
);
218 TAILQ_ENTRY(ws_win
) entry
;
220 struct swm_geometry g
;
225 unsigned long quirks
;
226 struct workspace
*ws
; /* always valid */
227 struct swm_screen
*s
; /* always valid, never changes */
228 XWindowAttributes wa
;
232 TAILQ_HEAD(ws_win_list
, ws_win
);
234 /* layout handlers */
236 void vertical_config(struct workspace
*, int);
237 void vertical_stack(struct workspace
*, struct swm_geometry
*);
238 void horizontal_config(struct workspace
*, int);
239 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
240 void max_stack(struct workspace
*, struct swm_geometry
*);
242 void grabbuttons(struct ws_win
*, int);
245 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
246 void (*l_config
)(struct workspace
*, int);
248 /* stack, configure */
249 { vertical_stack
, vertical_config
},
250 { horizontal_stack
, horizontal_config
},
255 #define SWM_H_SLICE (32)
256 #define SWM_V_SLICE (32)
258 /* define work spaces */
260 int idx
; /* workspace index */
261 int restack
; /* restack on switch */
262 struct layout
*cur_layout
; /* current layout handlers */
263 struct ws_win
*focus
; /* may be NULL */
264 struct swm_region
*r
; /* may be NULL */
265 struct ws_win_list winlist
; /* list of windows in ws */
269 int horizontal_msize
;
271 int horizontal_stacks
;
278 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
279 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
281 /* physical screen mapping */
282 #define SWM_WS_MAX (10) /* XXX Too small? */
284 int idx
; /* screen index */
285 struct swm_region_list rl
; /* list of regions on this screen */
286 struct swm_region_list orl
; /* list of old regions */
288 struct workspace ws
[SWM_WS_MAX
];
294 } c
[SWM_S_COLOR_MAX
];
296 struct swm_screen
*screens
;
299 struct ws_win
*cur_focus
= NULL
;
301 /* args to functions */
304 #define SWM_ARG_ID_FOCUSNEXT (0)
305 #define SWM_ARG_ID_FOCUSPREV (1)
306 #define SWM_ARG_ID_FOCUSMAIN (2)
307 #define SWM_ARG_ID_SWAPNEXT (3)
308 #define SWM_ARG_ID_SWAPPREV (4)
309 #define SWM_ARG_ID_SWAPMAIN (5)
310 #define SWM_ARG_ID_MASTERSHRINK (6)
311 #define SWM_ARG_ID_MASTERGROW (7)
312 #define SWM_ARG_ID_MASTERADD (8)
313 #define SWM_ARG_ID_MASTERDEL (9)
314 #define SWM_ARG_ID_STACKRESET (10)
315 #define SWM_ARG_ID_STACKINIT (11)
316 #define SWM_ARG_ID_CYCLEWS_UP (12)
317 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
318 #define SWM_ARG_ID_CYCLESC_UP (14)
319 #define SWM_ARG_ID_CYCLESC_DOWN (15)
320 #define SWM_ARG_ID_STACKINC (16)
321 #define SWM_ARG_ID_STACKDEC (17)
322 #define SWM_ARG_ID_SS_ALL (0)
323 #define SWM_ARG_ID_SS_WINDOW (1)
324 #define SWM_ARG_ID_DONTCENTER (0)
325 #define SWM_ARG_ID_CENTER (1)
334 #define SWM_Q_FLOAT (1<<0) /* float this window */
335 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
336 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
338 { "MPlayer", "xv", SWM_Q_FLOAT
},
339 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
},
340 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT
},
341 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
},
342 { "Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
347 void expose(XEvent
*);
348 void keypress(XEvent
*);
349 void buttonpress(XEvent
*);
350 void configurerequest(XEvent
*);
351 void configurenotify(XEvent
*);
352 void destroynotify(XEvent
*);
353 void enternotify(XEvent
*);
354 void focusin(XEvent
*);
355 void mappingnotify(XEvent
*);
356 void maprequest(XEvent
*);
357 void propertynotify(XEvent
*);
358 void unmapnotify(XEvent
*);
359 void visibilitynotify(XEvent
*);
361 void (*handler
[LASTEvent
])(XEvent
*) = {
363 [KeyPress
] = keypress
,
364 [ButtonPress
] = buttonpress
,
365 [ConfigureRequest
] = configurerequest
,
366 [ConfigureNotify
] = configurenotify
,
367 [DestroyNotify
] = destroynotify
,
368 [EnterNotify
] = enternotify
,
370 [MappingNotify
] = mappingnotify
,
371 [MapRequest
] = maprequest
,
372 [PropertyNotify
] = propertynotify
,
373 [UnmapNotify
] = unmapnotify
,
374 [VisibilityNotify
] = visibilitynotify
,
378 name_to_color(char *colorname
)
382 XColor screen_def
, exact_def
;
383 unsigned long result
= 0;
384 char cname
[32] = "#";
386 cmap
= DefaultColormap(display
, screens
[0].idx
);
387 status
= XAllocNamedColor(display
, cmap
, colorname
,
388 &screen_def
, &exact_def
);
390 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
391 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
395 result
= screen_def
.pixel
;
397 fprintf(stderr
, "color '%s' not found.\n", colorname
);
403 setscreencolor(char *val
, int i
, int c
)
405 if (i
> 0 && i
<= ScreenCount(display
)) {
406 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
407 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
409 } else if (i
== -1) {
410 for (i
= 0; i
< ScreenCount(display
); i
++)
411 screens
[i
].c
[c
].color
= name_to_color(val
);
412 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
415 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
416 i
, ScreenCount(display
));
419 void new_region(struct swm_screen
*, int, int, int, int);
422 custom_region(char *val
)
424 unsigned int sidx
, x
, y
, w
, h
;
426 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
427 errx(1, "invalid custom region, "
428 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
429 if (sidx
< 1 || sidx
> ScreenCount(display
))
430 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
431 sidx
, ScreenCount(display
));
435 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
437 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
438 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
439 w
+ x
> DisplayWidth(display
, sidx
) ||
440 h
+ y
> DisplayHeight(display
, sidx
))
441 errx(1, "region %ux%u+%u+%u not within screen boundaries "
442 "(%ux%u)\n", w
, h
, x
, y
,
443 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
445 new_region(&screens
[sidx
], x
, y
, w
, h
);
449 varmatch(char *var
, char *name
, int *index
)
454 i
= strncmp(var
, name
, 255);
461 p
= var
+ strlen(name
);
464 bzero(buf
, sizeof buf
);
466 while (isdigit(*p
) && i
< sizeof buf
)
468 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
470 *index
= strtonum(buf
, 0, 99, NULL
);
474 /* conf file stuff */
475 #define SWM_CONF_WS "\n= \t"
476 #define SWM_CONF_FILE "scrotwm.conf"
478 conf_load(char *filename
)
481 char *line
, *cp
, *var
, *val
;
482 size_t len
, lineno
= 0;
485 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
487 if (filename
== NULL
)
490 if ((config
= fopen(filename
, "r")) == NULL
)
493 for (sc
= ScreenCount(display
);;) {
494 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
498 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
504 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
506 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
507 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
510 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
513 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
514 bar_enabled
= atoi(val
);
515 else if (!varmatch(var
, "bar_border", &i
))
516 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
517 else if (!varmatch(var
, "bar_color", &i
))
518 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
519 else if (!varmatch(var
, "bar_font_color", &i
))
520 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
521 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
522 asprintf(&bar_fonts
[0], "%s", val
);
523 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
524 asprintf(&bar_argv
[0], "%s", val
);
525 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
526 bar_delay
= atoi(val
);
532 if (!varmatch(var
, "color_focus", &i
))
533 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
534 else if (!varmatch(var
, "color_unfocus", &i
))
535 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
536 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
537 cycle_visible
= atoi(val
);
538 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
539 cycle_visible
= atoi(val
);
545 if (!strncmp(var
, "dialog_ratio",
546 strlen("dialog_ratio"))) {
547 dialog_ratio
= atof(val
);
548 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
555 if (!strncmp(var
, "region", strlen("region")))
562 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
563 asprintf(&spawn_term
[0], "%s", val
);
564 if (!strncmp(var
, "screenshot_enabled",
565 strlen("screenshot_enabled")))
566 ss_enabled
= atoi(val
);
567 if (!strncmp(var
, "screenshot_app",
568 strlen("screenshot_app")))
569 asprintf(&spawn_screenshot
[0], "%s", val
);
581 errx(1, "invalid conf file entry: %s=%s", var
, val
);
585 socket_setnonblock(int fd
)
589 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
590 err(1, "fcntl F_GETFL");
592 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
593 err(1, "fcntl F_SETFL");
597 bar_print(struct swm_region
*r
, char *s
)
599 XClearWindow(display
, r
->bar_window
);
600 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
601 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
610 bzero(bar_pipe
, sizeof bar_pipe
);
613 kill(bar_pid
, SIGTERM
);
616 strlcpy(bar_ext
, "", sizeof bar_ext
);
625 struct swm_region
*r
;
629 char loc
[SWM_BAR_MAX
];
632 if (bar_enabled
== 0)
635 if (bar_extra
&& bar_extra_running
) {
636 /* ignore short reads; it'll correct itself */
637 while ((b
= fgetln(stdin
, &len
)) != NULL
)
638 if (b
&& b
[len
- 1] == '\n') {
640 strlcpy(bar_ext
, b
, sizeof bar_ext
);
642 if (b
== NULL
&& errno
!= EAGAIN
) {
643 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
644 errno
, strerror(errno
));
648 strlcpy(bar_ext
, "", sizeof bar_ext
);
651 localtime_r(&tmt
, &tm
);
652 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
653 for (i
= 0; i
< ScreenCount(display
); i
++) {
655 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
656 snprintf(loc
, sizeof loc
, "%s %d:%d %s %s",
657 s
, x
++, r
->ws
->idx
+ 1, bar_ext
, bar_vertext
);
661 XSync(display
, False
);
672 bar_toggle(struct swm_region
*r
, union arg
*args
)
674 struct swm_region
*tmpr
;
675 int i
, j
, sc
= ScreenCount(display
);
677 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
680 for (i
= 0; i
< sc
; i
++)
681 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
682 XUnmapWindow(display
, tmpr
->bar_window
);
684 for (i
= 0; i
< sc
; i
++)
685 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
686 XMapRaised(display
, tmpr
->bar_window
);
688 bar_enabled
= !bar_enabled
;
689 XSync(display
, False
);
690 for (i
= 0; i
< sc
; i
++)
691 for (j
= 0; j
< SWM_WS_MAX
; j
++)
692 screens
[i
].ws
[j
].restack
= 1;
695 /* must be after stack */
696 for (i
= 0; i
< sc
; i
++)
697 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
704 XSetWindowAttributes wa
;
705 struct swm_region
*r
;
708 /* do this here because the conf file is in memory */
709 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
710 /* launch external status app */
711 bar_extra_running
= 1;
712 if (pipe(bar_pipe
) == -1)
713 err(1, "pipe error");
714 socket_setnonblock(bar_pipe
[0]);
715 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
716 if (dup2(bar_pipe
[0], 0) == -1)
718 if (dup2(bar_pipe
[1], 1) == -1)
720 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
721 err(1, "could not disable SIGPIPE");
722 switch (bar_pid
= fork()) {
724 err(1, "cannot fork");
728 execvp(bar_argv
[0], bar_argv
);
729 err(1, "%s external app failed", bar_argv
[0]);
731 default: /* parent */
737 bzero(&wa
, sizeof wa
);
738 for (i
= 0; i
< ScreenCount(display
); i
++)
739 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
741 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
742 wa
.background_pixel
=
743 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
744 XChangeWindowAttributes(display
, r
->bar_window
,
745 CWBackPixel
| CWBorderPixel
, &wa
);
751 bar_setup(struct swm_region
*r
)
755 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
756 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
762 if (bar_fonts
[i
] == NULL
)
763 errx(1, "couldn't load font");
764 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
766 r
->bar_window
= XCreateSimpleWindow(display
,
767 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
768 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
769 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
770 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
771 XSetFont(display
, bar_gc
, bar_fs
->fid
);
772 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
774 XMapRaised(display
, r
->bar_window
);
775 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
777 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
778 err(1, "could not install bar_signal");
783 version(struct swm_region
*r
, union arg
*args
)
785 bar_version
= !bar_version
;
787 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
789 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
794 config_win(struct ws_win
*win
)
798 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
799 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
800 ce
.type
= ConfigureNotify
;
801 ce
.display
= display
;
807 ce
.height
= win
->g
.h
;
808 ce
.border_width
= 1; /* XXX store this! */
810 ce
.override_redirect
= False
;
811 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
815 count_win(struct workspace
*ws
, int count_transient
)
820 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
821 if (count_transient
== 0 && win
->floating
)
823 if (count_transient
== 0 && win
->transient
)
827 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
833 quit(struct swm_region
*r
, union arg
*args
)
835 DNPRINTF(SWM_D_MISC
, "quit\n");
845 for (i
= 0; i
< ScreenCount(display
); i
++)
846 for (j
= 0; j
< SWM_WS_MAX
; j
++)
847 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
848 XUnmapWindow(display
, win
->id
);
852 restart(struct swm_region
*r
, union arg
*args
)
854 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
856 /* disable alarm because the following code may not be interrupted */
858 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
859 errx(1, "can't disable alarm");
864 XCloseDisplay(display
);
865 execvp(start_argv
[0], start_argv
);
866 fprintf(stderr
, "execvp failed\n");
872 root_to_region(Window root
)
874 struct swm_region
*r
= NULL
;
879 for (i
= 0; i
< ScreenCount(display
); i
++)
880 if (screens
[i
].root
== root
)
883 if (XQueryPointer(display
, screens
[i
].root
,
884 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
885 /* choose a region based on pointer location */
886 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
887 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
888 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
893 r
= TAILQ_FIRST(&screens
[i
].rl
);
899 find_window(Window id
)
904 for (i
= 0; i
< ScreenCount(display
); i
++)
905 for (j
= 0; j
< SWM_WS_MAX
; j
++)
906 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
913 spawn(struct swm_region
*r
, union arg
*args
)
918 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
920 * The double-fork construct avoids zombie processes and keeps the code
921 * clean from stupid signal handlers.
926 close(ConnectionNumber(display
));
927 setenv("LD_PRELOAD", SWM_LIB
, 1);
928 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
929 setenv("_SWM_WS", ret
, 1);
932 if (asprintf(&ret
, "%d", getpid())) {
933 setenv("_SWM_PID", ret
, 1);
937 /* kill stdin, mplayer, ssh-add etc. need that */
938 si
= open("/dev/null", O_RDONLY
, 0);
940 err(1, "open /dev/null");
941 if (dup2(si
, 0) == -1)
942 err(1, "dup2 /dev/null");
943 execvp(args
->argv
[0], args
->argv
);
944 fprintf(stderr
, "execvp failed\n");
953 spawnmenu(struct swm_region
*r
, union arg
*args
)
955 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
957 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
958 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
959 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
960 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
961 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
972 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
974 for (i
= 0; i
< ScreenCount(display
); i
++)
975 for (j
= 0; j
< SWM_WS_MAX
; j
++)
976 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
977 if (win
->ws
->r
== NULL
)
980 XSetWindowBorder(display
, win
->id
,
981 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
983 win
->ws
->focus
= NULL
;
989 focus_win(struct ws_win
*win
)
991 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
997 win
->ws
->focus
= win
;
998 if (win
->ws
->r
!= NULL
) {
1000 if (!win
->got_focus
) {
1001 XSetWindowBorder(display
, win
->id
,
1002 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1003 grabbuttons(win
, 1);
1006 XSetInputFocus(display
, win
->id
,
1007 RevertToPointerRoot
, CurrentTime
);
1012 switchws(struct swm_region
*r
, union arg
*args
)
1014 int wsid
= args
->id
;
1015 struct swm_region
*this_r
, *other_r
;
1017 struct workspace
*new_ws
, *old_ws
;
1020 old_ws
= this_r
->ws
;
1021 new_ws
= &this_r
->s
->ws
[wsid
];
1023 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1024 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1027 if (new_ws
== old_ws
)
1030 other_r
= new_ws
->r
;
1032 /* if the other workspace is hidden, switch windows */
1033 /* map new window first to prevent ugly blinking */
1035 old_ws
->restack
= 1;
1037 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1038 XMapRaised(display
, win
->id
);
1040 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1041 XUnmapWindow(display
, win
->id
);
1043 other_r
->ws
= old_ws
;
1044 old_ws
->r
= other_r
;
1046 this_r
->ws
= new_ws
;
1052 focus_win(new_ws
->focus
);
1058 cyclews(struct swm_region
*r
, union arg
*args
)
1061 struct swm_screen
*s
= r
->s
;
1063 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1064 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1065 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1070 case SWM_ARG_ID_CYCLEWS_UP
:
1071 if (a
.id
< SWM_WS_MAX
- 1)
1076 case SWM_ARG_ID_CYCLEWS_DOWN
:
1080 a
.id
= SWM_WS_MAX
- 1;
1086 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1088 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1092 } while (a
.id
!= r
->ws
->idx
);
1096 cyclescr(struct swm_region
*r
, union arg
*args
)
1098 struct swm_region
*rr
;
1103 case SWM_ARG_ID_CYCLESC_UP
:
1104 rr
= TAILQ_NEXT(r
, entry
);
1106 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1108 case SWM_ARG_ID_CYCLESC_DOWN
:
1109 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1111 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1117 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1118 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1119 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1123 swapwin(struct swm_region
*r
, union arg
*args
)
1125 struct ws_win
*target
;
1126 struct ws_win_list
*wl
;
1129 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1130 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1131 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1132 if (cur_focus
== NULL
)
1135 wl
= &cur_focus
->ws
->winlist
;
1138 case SWM_ARG_ID_SWAPPREV
:
1139 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1140 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1142 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
1144 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
1146 case SWM_ARG_ID_SWAPNEXT
:
1147 target
= TAILQ_NEXT(cur_focus
, entry
);
1148 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1150 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1152 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
1154 case SWM_ARG_ID_SWAPMAIN
:
1155 target
= TAILQ_FIRST(wl
);
1156 if (target
== cur_focus
)
1158 TAILQ_REMOVE(wl
, target
, entry
);
1159 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
1160 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1161 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1164 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1173 focus(struct swm_region
*r
, union arg
*args
)
1175 struct ws_win
*winfocus
, *winlostfocus
;
1176 struct ws_win_list
*wl
;
1178 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1179 if (cur_focus
== NULL
)
1182 wl
= &cur_focus
->ws
->winlist
;
1184 winlostfocus
= cur_focus
;
1187 case SWM_ARG_ID_FOCUSPREV
:
1188 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1189 if (winfocus
== NULL
)
1190 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1193 case SWM_ARG_ID_FOCUSNEXT
:
1194 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1195 if (winfocus
== NULL
)
1196 winfocus
= TAILQ_FIRST(wl
);
1199 case SWM_ARG_ID_FOCUSMAIN
:
1200 winfocus
= TAILQ_FIRST(wl
);
1207 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1210 XMapRaised(display
, winfocus
->id
);
1211 focus_win(winfocus
);
1212 XSync(display
, False
);
1216 cycle_layout(struct swm_region
*r
, union arg
*args
)
1218 struct workspace
*ws
= r
->ws
;
1220 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1223 if (ws
->cur_layout
->l_stack
== NULL
)
1224 ws
->cur_layout
= &layouts
[0];
1230 stack_config(struct swm_region
*r
, union arg
*args
)
1232 struct workspace
*ws
= r
->ws
;
1234 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1237 if (ws
->cur_layout
->l_config
!= NULL
)
1238 ws
->cur_layout
->l_config(ws
, args
->id
);
1240 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1246 struct swm_geometry g
;
1247 struct swm_region
*r
;
1250 DNPRINTF(SWM_D_STACK
, "stack\n");
1252 for (i
= 0; i
< ScreenCount(display
); i
++) {
1254 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1255 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1256 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1258 /* start with screen geometry, adjust for bar */
1268 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1271 XSync(display
, False
);
1275 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1280 bzero(&wc
, sizeof wc
);
1281 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1282 wc
.border_width
= 1;
1283 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1284 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1285 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1287 wc
.width
= win
->g
.w
;
1288 wc
.height
= win
->g
.h
;
1293 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1294 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1297 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1298 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1300 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1303 #define SWAPXY(g) do { \
1305 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1306 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1309 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1312 struct swm_geometry win_g
, r_g
= *g
;
1313 struct ws_win
*win
, *winfocus
;
1314 int i
, j
, s
, w_inc
, h_inc
, w_base
, h_base
, stacks
;
1315 int hrh
, extra
, h_slice
, last_h
= 0;
1316 int split
, colno
, winno
, mwin
, msize
, mscale
;
1317 int remain
, missing
, v_slice
;;
1320 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1321 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1323 if ((winno
= count_win(ws
, 0)) == 0)
1326 if (ws
->focus
== NULL
)
1327 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1328 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1330 win
= TAILQ_FIRST(&ws
->winlist
);
1332 w_inc
= win
->sh
.width_inc
;
1333 w_base
= win
->sh
.base_width
;
1334 mwin
= ws
->l_state
.horizontal_mwin
;
1335 mscale
= ws
->l_state
.horizontal_msize
;
1336 stacks
= ws
->l_state
.horizontal_stacks
;
1339 w_inc
= win
->sh
.height_inc
;
1340 w_base
= win
->sh
.base_height
;
1341 mwin
= ws
->l_state
.vertical_mwin
;
1342 mscale
= ws
->l_state
.vertical_msize
;
1343 stacks
= ws
->l_state
.vertical_stacks
;
1347 if (stacks
> winno
- mwin
)
1348 stacks
= winno
- mwin
;
1352 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1353 if (mwin
&& winno
> mwin
) {
1354 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1358 win_g
.w
= v_slice
* mscale
;
1360 if (w_inc
> 1 && w_inc
< v_slice
) {
1361 /* adjust for window's requested size increment */
1362 remain
= (win_g
.w
- w_base
) % w_inc
;
1363 missing
= w_inc
- remain
;
1365 if (missing
<= extra
|| j
== 0) {
1376 win_g
.x
+= r_g
.w
- msize
;
1379 colno
= split
= winno
/ stacks
;
1380 win_g
.w
= ((r_g
.w
- (stacks
* 2) + 2) / stacks
);
1382 hrh
= r_g
.h
/ colno
;
1383 extra
= r_g
.h
- (colno
* hrh
);
1386 /* stack all the tiled windows */
1387 i
= j
= 0, s
= stacks
;
1388 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1389 if (win
->transient
!= 0 || win
->floating
!= 0)
1392 if (split
&& i
== split
) {
1393 colno
= (winno
- mwin
) / stacks
;
1394 if (s
<= (winno
- mwin
) % stacks
)
1396 split
= split
+ colno
;
1397 hrh
= (r_g
.h
/ colno
);
1398 extra
= r_g
.h
- (colno
* hrh
);
1402 win_g
.x
+= win_g
.w
+ 2;
1403 win_g
.w
= (r_g
.w
- msize
- (stacks
* 2)) / stacks
;
1405 win_g
.w
+= (r_g
.w
- msize
- (stacks
* 2)) %
1412 h_inc
= win
->sh
.width_inc
;
1413 h_base
= win
->sh
.base_width
;
1415 h_inc
= win
->sh
.height_inc
;
1416 h_base
= win
->sh
.base_height
;
1418 if (j
== colno
- 1) {
1419 win_g
.h
= hrh
+ extra
;
1420 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1421 /* adjust for window's requested size increment */
1422 remain
= (win_g
.h
- h_base
) % h_inc
;
1423 missing
= h_inc
- remain
;
1425 if (missing
<= extra
|| j
== 0) {
1437 win_g
.y
+= last_h
+ 2;
1439 bzero(&wc
, sizeof wc
);
1440 wc
.border_width
= 1;
1442 win
->g
.x
= wc
.x
= win_g
.y
;
1443 win
->g
.y
= wc
.y
= win_g
.x
;
1444 win
->g
.w
= wc
.width
= win_g
.h
;
1445 win
->g
.h
= wc
.height
= win_g
.w
;
1447 win
->g
.x
= wc
.x
= win_g
.x
;
1448 win
->g
.y
= wc
.y
= win_g
.y
;
1449 win
->g
.w
= wc
.width
= win_g
.w
;
1450 win
->g
.h
= wc
.height
= win_g
.h
;
1452 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1453 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1454 XMapRaised(display
, win
->id
);
1461 /* now, stack all the floaters and transients */
1462 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1463 if (win
->transient
== 0 && win
->floating
== 0)
1466 stack_floater(win
, ws
->r
);
1467 XMapRaised(display
, win
->id
);
1471 focus_win(winfocus
); /* has to be done outside of the loop */
1475 vertical_config(struct workspace
*ws
, int id
)
1477 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1480 case SWM_ARG_ID_STACKRESET
:
1481 case SWM_ARG_ID_STACKINIT
:
1482 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1483 ws
->l_state
.vertical_mwin
= 1;
1484 ws
->l_state
.vertical_stacks
= 1;
1486 case SWM_ARG_ID_MASTERSHRINK
:
1487 if (ws
->l_state
.vertical_msize
> 1)
1488 ws
->l_state
.vertical_msize
--;
1490 case SWM_ARG_ID_MASTERGROW
:
1491 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1492 ws
->l_state
.vertical_msize
++;
1494 case SWM_ARG_ID_MASTERADD
:
1495 ws
->l_state
.vertical_mwin
++;
1497 case SWM_ARG_ID_MASTERDEL
:
1498 if (ws
->l_state
.vertical_mwin
> 0)
1499 ws
->l_state
.vertical_mwin
--;
1501 case SWM_ARG_ID_STACKINC
:
1502 ws
->l_state
.vertical_stacks
++;
1504 case SWM_ARG_ID_STACKDEC
:
1505 if (ws
->l_state
.vertical_stacks
> 1)
1506 ws
->l_state
.vertical_stacks
--;
1514 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1516 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1518 stack_master(ws
, g
, 0, 0);
1522 horizontal_config(struct workspace
*ws
, int id
)
1524 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1527 case SWM_ARG_ID_STACKRESET
:
1528 case SWM_ARG_ID_STACKINIT
:
1529 ws
->l_state
.horizontal_mwin
= 1;
1530 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1531 ws
->l_state
.horizontal_stacks
= 1;
1533 case SWM_ARG_ID_MASTERSHRINK
:
1534 if (ws
->l_state
.horizontal_msize
> 1)
1535 ws
->l_state
.horizontal_msize
--;
1537 case SWM_ARG_ID_MASTERGROW
:
1538 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1539 ws
->l_state
.horizontal_msize
++;
1541 case SWM_ARG_ID_MASTERADD
:
1542 ws
->l_state
.horizontal_mwin
++;
1544 case SWM_ARG_ID_MASTERDEL
:
1545 if (ws
->l_state
.horizontal_mwin
> 0)
1546 ws
->l_state
.horizontal_mwin
--;
1548 case SWM_ARG_ID_STACKINC
:
1549 ws
->l_state
.horizontal_stacks
++;
1551 case SWM_ARG_ID_STACKDEC
:
1552 if (ws
->l_state
.horizontal_stacks
> 1)
1553 ws
->l_state
.horizontal_stacks
--;
1561 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1563 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1565 stack_master(ws
, g
, 1, 0);
1568 /* fullscreen view */
1570 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1572 struct swm_geometry gg
= *g
;
1573 struct ws_win
*win
, *winfocus
;
1576 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1578 if (count_win(ws
, 0) == 0)
1581 if (ws
->focus
== NULL
)
1582 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1583 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1585 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1586 if (win
->transient
!= 0 || win
->floating
!= 0) {
1587 if (win
== ws
->focus
) {
1589 stack_floater(win
, ws
->r
);
1590 XMapRaised(display
, win
->id
);
1592 XUnmapWindow(display
, win
->id
);
1594 bzero(&wc
, sizeof wc
);
1595 wc
.border_width
= 1;
1596 win
->g
.x
= wc
.x
= gg
.x
;
1597 win
->g
.y
= wc
.y
= gg
.y
;
1598 win
->g
.w
= wc
.width
= gg
.w
;
1599 win
->g
.h
= wc
.height
= gg
.h
;
1600 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1601 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1603 if (win
== ws
->focus
) {
1604 XMapRaised(display
, win
->id
);
1606 XUnmapWindow(display
, win
->id
);
1611 focus_win(winfocus
); /* has to be done outside of the loop */
1615 send_to_ws(struct swm_region
*r
, union arg
*args
)
1617 int wsid
= args
->id
;
1618 struct ws_win
*win
= cur_focus
;
1619 struct workspace
*ws
, *nws
;
1620 Atom ws_idx_atom
= 0;
1621 unsigned char ws_idx_str
[SWM_PROPLEN
];
1626 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1629 nws
= &win
->s
->ws
[wsid
];
1631 XUnmapWindow(display
, win
->id
);
1633 /* find a window to focus */
1634 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1635 if (ws
->focus
== NULL
)
1636 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1637 if (ws
->focus
== win
)
1640 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1642 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1645 /* Try to update the window's workspace property */
1646 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1648 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1649 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1651 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1652 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1655 if (count_win(nws
, 1) == 1)
1664 wkill(struct swm_region
*r
, union arg
*args
)
1666 DNPRINTF(SWM_D_MISC
, "wkill\n");
1667 if(r
->ws
->focus
!= NULL
)
1668 XKillClient(display
, r
->ws
->focus
->id
);
1672 screenshot(struct swm_region
*r
, union arg
*args
)
1676 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1678 if (ss_enabled
== 0)
1682 case SWM_ARG_ID_SS_ALL
:
1683 spawn_screenshot
[1] = "full";
1685 case SWM_ARG_ID_SS_WINDOW
:
1686 spawn_screenshot
[1] = "window";
1691 a
.argv
= spawn_screenshot
;
1696 floating_toggle(struct swm_region
*r
, union arg
*args
)
1698 struct ws_win
*win
= cur_focus
;
1703 win
->floating
= !win
->floating
;
1709 /* key definitions */
1713 void (*func
)(struct swm_region
*r
, union arg
*);
1716 /* modifier key function argument */
1717 { MODKEY
, XK_space
, cycle_layout
, {0} },
1718 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1719 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1720 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1721 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1722 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1723 { MODKEY
| ShiftMask
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
1724 { MODKEY
| ShiftMask
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
1725 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1726 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1727 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1728 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1729 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1730 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1731 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1732 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1733 { MODKEY
, XK_q
, restart
, {0} },
1734 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1735 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1736 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1737 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1738 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1739 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1740 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1741 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1742 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1743 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1744 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1745 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1746 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1747 { MODKEY
| ShiftMask
, XK_Right
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1748 { MODKEY
| ShiftMask
, XK_Left
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1749 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1750 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1751 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1752 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1753 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1754 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1755 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1756 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1757 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1758 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1759 { MODKEY
, XK_b
, bar_toggle
, {0} },
1760 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1761 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1762 { MODKEY
| ShiftMask
, XK_x
, wkill
, {0} },
1763 { MODKEY
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
1764 { MODKEY
| ShiftMask
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
1765 { MODKEY
, XK_t
, floating_toggle
,{0} },
1766 { MODKEY
| ShiftMask
, XK_v
, version
, {0} },
1767 { MODKEY
| ShiftMask
, XK_Delete
, spawn
, {.argv
= spawn_lock
} },
1768 { MODKEY
| ShiftMask
, XK_i
, spawn
, {.argv
= spawn_initscr
} },
1772 resize_window(struct ws_win
*win
, int center
)
1776 struct swm_region
*r
;
1778 r
= root_to_region(win
->wa
.root
);
1779 bzero(&wc
, sizeof wc
);
1780 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1781 wc
.border_width
= 1;
1782 wc
.width
= win
->g
.w
;
1783 wc
.height
= win
->g
.h
;
1784 if (center
== SWM_ARG_ID_CENTER
) {
1785 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1786 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1790 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1791 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1793 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1798 resize(struct ws_win
*win
, union arg
*args
)
1803 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1804 win
->id
, win
->floating
, win
->transient
);
1806 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1809 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1810 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1812 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1814 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1815 SubstructureRedirectMask
, &ev
);
1817 case ConfigureRequest
:
1820 handler
[ev
.type
](&ev
);
1823 if (ev
.xmotion
.x
< 0)
1825 if (ev
.xmotion
.y
< 0)
1827 win
->g
.w
= ev
.xmotion
.x
;
1828 win
->g
.h
= ev
.xmotion
.y
;
1830 /* not free, don't sync more than 60 times / second */
1831 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1832 time
= ev
.xmotion
.time
;
1833 XSync(display
, False
);
1834 resize_window(win
, args
->id
);
1838 } while (ev
.type
!= ButtonRelease
);
1840 XSync(display
, False
);
1841 resize_window(win
, args
->id
);
1843 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1845 XUngrabPointer(display
, CurrentTime
);
1848 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1852 move_window(struct ws_win
*win
)
1856 struct swm_region
*r
;
1858 r
= root_to_region(win
->wa
.root
);
1859 bzero(&wc
, sizeof wc
);
1864 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
1865 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1867 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1872 move(struct ws_win
*win
, union arg
*args
)
1878 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
1879 win
->id
, win
->floating
, win
->transient
);
1881 if (win
->floating
== 0) {
1887 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1888 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1890 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1892 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1893 SubstructureRedirectMask
, &ev
);
1895 case ConfigureRequest
:
1898 handler
[ev
.type
](&ev
);
1901 win
->g
.x
= ev
.xmotion
.x_root
;
1902 win
->g
.y
= ev
.xmotion
.y_root
;
1904 /* not free, don't sync more than 60 times / second */
1905 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1906 time
= ev
.xmotion
.time
;
1907 XSync(display
, False
);
1912 } while (ev
.type
!= ButtonRelease
);
1914 XSync(display
, False
);
1917 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1918 XUngrabPointer(display
, CurrentTime
);
1923 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1927 enum { client_click
, root_click
};
1929 unsigned int action
;
1931 unsigned int button
;
1932 void (*func
)(struct ws_win
*, union arg
*);
1935 /* action key mouse button func args */
1936 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
1937 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
1938 { client_click
, MODKEY
, Button1
, move
, {0} },
1942 updatenumlockmask(void)
1945 XModifierKeymap
*modmap
;
1947 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1949 modmap
= XGetModifierMapping(display
);
1950 for (i
= 0; i
< 8; i
++)
1951 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1952 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1953 == XKeysymToKeycode(display
, XK_Num_Lock
))
1954 numlockmask
= (1 << i
);
1956 XFreeModifiermap(modmap
);
1962 unsigned int i
, j
, k
;
1964 unsigned int modifiers
[] =
1965 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1967 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1968 updatenumlockmask();
1970 for (k
= 0; k
< ScreenCount(display
); k
++) {
1971 if (TAILQ_EMPTY(&screens
[k
].rl
))
1973 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1974 for (i
= 0; i
< LENGTH(keys
); i
++) {
1975 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1976 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1977 XGrabKey(display
, code
,
1978 keys
[i
].mod
| modifiers
[j
],
1979 screens
[k
].root
, True
,
1980 GrabModeAsync
, GrabModeAsync
);
1986 grabbuttons(struct ws_win
*win
, int focused
)
1989 unsigned int modifiers
[] =
1990 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1992 updatenumlockmask();
1993 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
1995 for (i
= 0; i
< LENGTH(buttons
); i
++)
1996 if (buttons
[i
].action
== client_click
)
1997 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1998 XGrabButton(display
, buttons
[i
].button
,
1999 buttons
[i
].mask
| modifiers
[j
],
2000 win
->id
, False
, BUTTONMASK
,
2001 GrabModeAsync
, GrabModeSync
, None
,
2004 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
2005 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
2011 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
2019 XKeyEvent
*ev
= &e
->xkey
;
2021 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
2023 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
2024 for (i
= 0; i
< LENGTH(keys
); i
++)
2025 if (keysym
== keys
[i
].keysym
2026 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
2028 keys
[i
].func(root_to_region(ev
->root
),
2033 buttonpress(XEvent
*e
)
2035 XButtonPressedEvent
*ev
= &e
->xbutton
;
2040 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2042 action
= root_click
;
2043 if ((win
= find_window(ev
->window
)) == NULL
)
2047 action
= client_click
;
2050 for (i
= 0; i
< LENGTH(buttons
); i
++)
2051 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2052 buttons
[i
].button
== ev
->button
&&
2053 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2054 buttons
[i
].func(win
, &buttons
[i
].args
);
2058 set_win_state(struct ws_win
*win
, long state
)
2060 long data
[] = {state
, None
};
2062 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2064 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2065 (unsigned char *)data
, 2);
2069 manage_window(Window id
)
2072 struct workspace
*ws
;
2074 int format
, i
, ws_idx
;
2075 unsigned long nitems
, bytes
;
2076 Atom ws_idx_atom
= 0, type
;
2077 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2078 struct swm_region
*r
;
2083 if ((win
= find_window(id
)) != NULL
)
2084 return (win
); /* already being managed */
2086 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2087 errx(1, "calloc: failed to allocate memory for new window");
2089 /* Get all the window data in one shot */
2090 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2092 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2093 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2094 XGetWindowAttributes(display
, id
, &win
->wa
);
2095 XGetTransientForHint(display
, id
, &trans
);
2096 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2098 win
->transient
= trans
;
2099 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2100 (unsigned)win
->id
, win
->transient
);
2104 * Figure out where to put the window. If it was previously assigned to
2105 * a workspace (either by spawn() or manually moving), and isn't
2106 * transient, * put it in the same workspace
2108 r
= root_to_region(win
->wa
.root
);
2109 if (prop
&& win
->transient
== 0) {
2110 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2111 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2113 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2116 ws
= &r
->s
->ws
[ws_idx
];
2120 /* set up the window layout */
2123 win
->s
= r
->s
; /* this never changes */
2124 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2126 win
->g
.w
= win
->wa
.width
;
2127 win
->g
.h
= win
->wa
.height
;
2128 win
->g
.x
= win
->wa
.x
;
2129 win
->g
.y
= win
->wa
.y
;
2131 /* Set window properties so we can remember this after reincarnation */
2132 if (ws_idx_atom
&& prop
== NULL
&&
2133 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2134 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2136 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2137 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2141 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2142 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2143 win
->ch
.res_class
, win
->ch
.res_name
);
2144 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2145 quirks
[i
].quirk
!= 0; i
++){
2146 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2147 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2148 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2149 win
->ch
.res_class
, win
->ch
.res_name
);
2150 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2152 win
->quirks
= quirks
[i
].quirk
;
2157 /* alter window position if quirky */
2158 if (win
->quirks
& SWM_Q_ANYWHERE
) {
2159 win
->manual
= 1; /* don't center the quirky windows */
2160 bzero(&wc
, sizeof wc
);
2162 if (win
->g
.y
< bar_height
) {
2163 win
->g
.y
= wc
.y
= bar_height
;
2166 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
2167 win
->g
.x
= wc
.x
= WIDTH(win
->ws
->r
) - win
->g
.w
- 2;
2170 wc
.border_width
= 1;
2171 mask
|= CWBorderWidth
;
2172 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2175 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2176 PropertyChangeMask
| StructureNotifyMask
);
2178 set_win_state(win
, NormalState
);
2180 /* floaters need to be mapped if they are in the current workspace */
2181 if (win
->floating
&& (ws
->idx
== r
->ws
->idx
))
2182 XMapRaised(display
, win
->id
);
2184 /* make new win focused */
2191 unmanage_window(struct ws_win
*win
)
2193 struct workspace
*ws
;
2198 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
2200 /* don't unmanage if we are switching workspaces */
2205 /* find a window to focus */
2206 if (ws
->focus
== win
)
2207 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2208 if (ws
->focus
== NULL
)
2209 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2210 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2214 focus_win(ws
->focus
);
2216 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
2217 set_win_state(win
, WithdrawnState
);
2218 if (win
->ch
.res_class
)
2219 XFree(win
->ch
.res_class
);
2220 if (win
->ch
.res_name
)
2221 XFree(win
->ch
.res_name
);
2226 configurerequest(XEvent
*e
)
2228 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2233 if ((win
= find_window(ev
->window
)) == NULL
)
2237 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2239 bzero(&wc
, sizeof wc
);
2242 wc
.width
= ev
->width
;
2243 wc
.height
= ev
->height
;
2244 wc
.border_width
= ev
->border_width
;
2245 wc
.sibling
= ev
->above
;
2246 wc
.stack_mode
= ev
->detail
;
2247 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2249 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2251 if (win
->floating
) {
2252 if (ev
->value_mask
& CWX
)
2254 if (ev
->value_mask
& CWY
)
2256 if (ev
->value_mask
& CWWidth
)
2257 win
->g
.w
= ev
->width
;
2258 if (ev
->value_mask
& CWHeight
)
2259 win
->g
.h
= ev
->height
;
2260 if (win
->ws
->r
!= NULL
) {
2261 /* this seems to be full screen */
2262 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2264 win
->g
.w
= WIDTH(win
->ws
->r
);
2265 ev
->value_mask
|= CWX
| CWWidth
;
2267 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2270 win
->g
.h
= HEIGHT(win
->ws
->r
);
2271 ev
->value_mask
|= CWY
| CWHeight
;
2274 if ((ev
->value_mask
& (CWX
| CWY
)) &&
2275 !(ev
->value_mask
& (CWWidth
| CWHeight
)))
2277 XMoveResizeWindow(display
, win
->id
,
2278 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2285 configurenotify(XEvent
*e
)
2287 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2288 e
->xconfigure
.window
);
2292 destroynotify(XEvent
*e
)
2295 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2297 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2299 if ((win
= find_window(ev
->window
)) != NULL
) {
2300 unmanage_window(win
);
2306 enternotify(XEvent
*e
)
2308 XCrossingEvent
*ev
= &e
->xcrossing
;
2311 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2314 /* eat event(r) to prevent autofocus */
2319 if ((win
= find_window(ev
->window
)) != NULL
)
2326 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2330 mappingnotify(XEvent
*e
)
2332 XMappingEvent
*ev
= &e
->xmapping
;
2334 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2336 XRefreshKeyboardMapping(ev
);
2337 if (ev
->request
== MappingKeyboard
)
2342 maprequest(XEvent
*e
)
2344 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2345 XWindowAttributes wa
;
2347 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2348 e
->xmaprequest
.window
);
2350 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2352 if (wa
.override_redirect
)
2354 manage_window(e
->xmaprequest
.window
);
2360 propertynotify(XEvent
*e
)
2363 XPropertyEvent
*ev
= &e
->xproperty
;
2365 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2368 if (ev
->state
== PropertyDelete
)
2369 return; /* ignore */
2370 win
= find_window(ev
->window
);
2375 case XA_WM_NORMAL_HINTS
:
2378 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2379 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2380 if (win
->sh
.flags
& PMinSize
) {
2381 win
->g
.w
= win
->sh
.min_width
;
2382 win
->g
.h
= win
->sh
.min_height
;
2383 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2385 XMoveResizeWindow(display
, win
->id
,
2386 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2395 unmapnotify(XEvent
*e
)
2397 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2400 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2402 if ((win
= find_window(ev
->window
)) != NULL
)
2404 unmanage_window(win
);
2408 visibilitynotify(XEvent
*e
)
2411 struct swm_region
*r
;
2413 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2414 e
->xvisibility
.window
);
2415 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2416 for (i
= 0; i
< ScreenCount(display
); i
++)
2417 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2418 if (e
->xvisibility
.window
== r
->bar_window
)
2423 xerror_start(Display
*d
, XErrorEvent
*ee
)
2430 xerror(Display
*d
, XErrorEvent
*ee
)
2432 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2440 xerrorxlib
= XSetErrorHandler(xerror_start
);
2442 /* this causes an error if some other window manager is running */
2443 XSelectInput(display
, DefaultRootWindow(display
),
2444 SubstructureRedirectMask
);
2445 XSync(display
, False
);
2449 XSetErrorHandler(xerror
);
2450 XSync(display
, False
);
2459 unsigned char *p
= NULL
;
2460 unsigned long n
, extra
;
2463 astate
= XInternAtom(display
, "WM_STATE", False
);
2464 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
2465 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
2466 if (status
!= Success
)
2475 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
2477 struct swm_region
*r
, *n
;
2478 struct workspace
*ws
= NULL
;
2481 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
2482 s
->idx
, w
, h
, x
, y
);
2484 /* remove any conflicting regions */
2485 n
= TAILQ_FIRST(&s
->rl
);
2488 n
= TAILQ_NEXT(r
, entry
);
2489 if (X(r
) < (x
+ w
) &&
2490 (X(r
) + WIDTH(r
)) > x
&&
2492 (Y(r
) + HEIGHT(r
)) > y
) {
2493 XDestroyWindow(display
, r
->bar_window
);
2494 TAILQ_REMOVE(&s
->rl
, r
, entry
);
2495 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
2499 /* search old regions for one to reuse */
2501 /* size + location match */
2502 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2503 if (X(r
) == x
&& Y(r
) == y
&&
2504 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2508 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2509 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2513 TAILQ_REMOVE(&s
->orl
, r
, entry
);
2514 /* try to use old region's workspace */
2515 if (r
->ws
->r
== NULL
)
2518 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
2519 errx(1, "calloc: failed to allocate memory for screen");
2521 /* if we don't have a workspace already, find one */
2523 for (i
= 0; i
< SWM_WS_MAX
; i
++)
2524 if (s
->ws
[i
].r
== NULL
) {
2531 errx(1, "no free workspaces\n");
2540 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
2547 #ifdef SWM_XRR_HAS_CRTC
2549 XRRScreenResources
*sr
;
2552 #endif /* SWM_XRR_HAS_CRTC */
2553 struct swm_region
*r
;
2556 if (i
>= ScreenCount(display
))
2557 errx(1, "invalid screen");
2559 /* remove any old regions */
2560 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
2562 XDestroyWindow(display
, r
->bar_window
);
2563 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
2564 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
2567 /* map virtual screens onto physical screens */
2568 #ifdef SWM_XRR_HAS_CRTC
2569 if (xrandr_support
) {
2570 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
2572 new_region(&screens
[i
], 0, 0,
2573 DisplayWidth(display
, i
),
2574 DisplayHeight(display
, i
));
2578 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
2579 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
2580 if (ci
->noutput
== 0)
2583 if (ci
!= NULL
&& ci
->mode
== None
)
2584 new_region(&screens
[i
], 0, 0,
2585 DisplayWidth(display
, i
),
2586 DisplayHeight(display
, i
));
2588 new_region(&screens
[i
],
2589 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
2592 XRRFreeCrtcInfo(ci
);
2593 XRRFreeScreenResources(sr
);
2595 #endif /* SWM_XRR_HAS_CRTC */
2597 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
2598 DisplayHeight(display
, i
));
2603 screenchange(XEvent
*e
) {
2604 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
2605 struct swm_region
*r
;
2609 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
2611 if (!XRRUpdateConfiguration(e
))
2614 /* silly event doesn't include the screen index */
2615 for (i
= 0; i
< ScreenCount(display
); i
++)
2616 if (screens
[i
].root
== xe
->root
)
2618 if (i
>= ScreenCount(display
))
2619 errx(1, "screenchange: screen not found\n");
2621 /* brute force for now, just re-enumerate the regions */
2624 /* hide any windows that went away */
2625 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2626 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
2627 XUnmapWindow(display
, win
->id
);
2634 Window d1
, d2
, *wins
= NULL
;
2635 XWindowAttributes wa
;
2638 int errorbase
, major
, minor
;
2639 struct workspace
*ws
;
2643 if ((screens
= calloc(ScreenCount(display
),
2644 sizeof(struct swm_screen
))) == NULL
)
2645 errx(1, "calloc: screens");
2647 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2649 /* initial Xrandr setup */
2650 xrandr_support
= XRRQueryExtension(display
,
2651 &xrandr_eventbase
, &errorbase
);
2653 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
2656 /* map physical screens */
2657 for (i
= 0; i
< ScreenCount(display
); i
++) {
2658 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
2660 TAILQ_INIT(&screens
[i
].rl
);
2661 TAILQ_INIT(&screens
[i
].orl
);
2662 screens
[i
].root
= RootWindow(display
, i
);
2664 /* set default colors */
2665 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
2666 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
2667 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
2668 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
2669 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
2671 /* init all workspaces */
2672 /* XXX these should be dynamically allocated too */
2673 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
2674 ws
= &screens
[i
].ws
[j
];
2679 TAILQ_INIT(&ws
->winlist
);
2681 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
2682 if (layouts
[k
].l_config
!= NULL
)
2683 layouts
[k
].l_config(ws
,
2684 SWM_ARG_ID_STACKINIT
);
2685 ws
->cur_layout
= &layouts
[0];
2687 /* grab existing windows (before we build the bars)*/
2688 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
2694 XRRSelectInput(display
, screens
[i
].root
,
2695 RRScreenChangeNotifyMask
);
2697 /* attach windows to a region */
2698 /* normal windows */
2699 for (j
= 0; j
< no
; j
++) {
2700 XGetWindowAttributes(display
, wins
[j
], &wa
);
2701 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
2702 wa
.override_redirect
||
2703 XGetTransientForHint(display
, wins
[j
], &d1
))
2706 if (wa
.map_state
== IsViewable
||
2707 getstate(wins
[j
]) == NormalState
)
2708 manage_window(wins
[j
]);
2710 /* transient windows */
2711 for (j
= 0; j
< no
; j
++) {
2712 if (!XGetWindowAttributes(display
, wins
[j
], &wa
))
2715 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
2716 (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) ==
2718 manage_window(wins
[j
]);
2728 main(int argc
, char *argv
[])
2731 char conf
[PATH_MAX
], *cfile
= NULL
;
2738 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
2739 SWM_VERSION
, cvstag
);
2740 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2741 warnx("no locale support");
2743 if (!(display
= XOpenDisplay(0)))
2744 errx(1, "can not open display");
2747 errx(1, "other wm running");
2749 astate
= XInternAtom(display
, "WM_STATE", False
);
2751 /* look for local and global conf file */
2752 pwd
= getpwuid(getuid());
2754 errx(1, "invalid user %d", getuid());
2758 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2759 if (stat(conf
, &sb
) != -1) {
2760 if (S_ISREG(sb
.st_mode
))
2763 /* try global conf file */
2764 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2765 if (!stat(conf
, &sb
))
2766 if (S_ISREG(sb
.st_mode
))
2773 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2778 xfd
= ConnectionNumber(display
);
2782 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2784 errx(1, "select failed");
2789 while (XPending(display
)) {
2790 XNextEvent(display
, &e
);
2791 if (e
.type
< LASTEvent
) {
2792 if (handler
[e
.type
])
2793 handler
[e
.type
](&e
);
2795 DNPRINTF(SWM_D_EVENT
,
2796 "win: %lu unknown event: %d\n",
2797 e
.xany
.window
, e
.type
);
2799 switch (e
.type
- xrandr_eventbase
) {
2800 case RRScreenChangeNotify
:
2804 DNPRINTF(SWM_D_EVENT
,
2805 "win: %lu unknown xrandr event: "
2806 "%d\n", e
.xany
.window
, e
.type
);
2813 XCloseDisplay(display
);