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
*);
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_menu
[] = { "dmenu_run", "-fn", NULL
, "-nb", NULL
,
187 "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
189 #define SWM_MENU_FN (2)
190 #define SWM_MENU_NB (4)
191 #define SWM_MENU_NF (6)
192 #define SWM_MENU_SB (8)
193 #define SWM_MENU_SF (10)
195 /* layout manager data */
196 struct swm_geometry
{
206 /* virtual "screens" */
208 TAILQ_ENTRY(swm_region
) entry
;
209 struct swm_geometry g
;
210 struct workspace
*ws
; /* current workspace on this region */
211 struct swm_screen
*s
; /* screen idx */
214 TAILQ_HEAD(swm_region_list
, swm_region
);
217 TAILQ_ENTRY(ws_win
) entry
;
219 struct swm_geometry g
;
224 unsigned long quirks
;
225 struct workspace
*ws
; /* always valid */
226 struct swm_screen
*s
; /* always valid, never changes */
227 XWindowAttributes wa
;
231 TAILQ_HEAD(ws_win_list
, ws_win
);
233 /* layout handlers */
235 void vertical_config(struct workspace
*, int);
236 void vertical_stack(struct workspace
*, struct swm_geometry
*);
237 void horizontal_config(struct workspace
*, int);
238 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
239 void max_stack(struct workspace
*, struct swm_geometry
*);
241 void grabbuttons(struct ws_win
*, int);
244 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
245 void (*l_config
)(struct workspace
*, int);
247 /* stack, configure */
248 { vertical_stack
, vertical_config
},
249 { horizontal_stack
, horizontal_config
},
254 #define SWM_H_SLICE (32)
255 #define SWM_V_SLICE (32)
257 /* define work spaces */
259 int idx
; /* workspace index */
260 int restack
; /* restack on switch */
261 struct layout
*cur_layout
; /* current layout handlers */
262 struct ws_win
*focus
; /* may be NULL */
263 struct swm_region
*r
; /* may be NULL */
264 struct ws_win_list winlist
; /* list of windows in ws */
268 int horizontal_msize
;
270 int horizontal_stacks
;
277 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
278 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
280 /* physical screen mapping */
281 #define SWM_WS_MAX (10) /* XXX Too small? */
283 int idx
; /* screen index */
284 struct swm_region_list rl
; /* list of regions on this screen */
285 struct swm_region_list orl
; /* list of old regions */
287 struct workspace ws
[SWM_WS_MAX
];
293 } c
[SWM_S_COLOR_MAX
];
295 struct swm_screen
*screens
;
298 struct ws_win
*cur_focus
= NULL
;
300 /* args to functions */
303 #define SWM_ARG_ID_FOCUSNEXT (0)
304 #define SWM_ARG_ID_FOCUSPREV (1)
305 #define SWM_ARG_ID_FOCUSMAIN (2)
306 #define SWM_ARG_ID_SWAPNEXT (3)
307 #define SWM_ARG_ID_SWAPPREV (4)
308 #define SWM_ARG_ID_SWAPMAIN (5)
309 #define SWM_ARG_ID_MASTERSHRINK (6)
310 #define SWM_ARG_ID_MASTERGROW (7)
311 #define SWM_ARG_ID_MASTERADD (8)
312 #define SWM_ARG_ID_MASTERDEL (9)
313 #define SWM_ARG_ID_STACKRESET (10)
314 #define SWM_ARG_ID_STACKINIT (11)
315 #define SWM_ARG_ID_CYCLEWS_UP (12)
316 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
317 #define SWM_ARG_ID_CYCLESC_UP (14)
318 #define SWM_ARG_ID_CYCLESC_DOWN (15)
319 #define SWM_ARG_ID_COLINC (16)
320 #define SWM_ARG_ID_COLDEC (17)
321 #define SWM_ARG_ID_ROWINC (16)
322 #define SWM_ARG_ID_ROWDEL (17)
323 #define SWM_ARG_ID_SS_ALL (0)
324 #define SWM_ARG_ID_SS_WINDOW (1)
325 #define SWM_ARG_ID_DONTCENTER (0)
326 #define SWM_ARG_ID_CENTER (1)
335 #define SWM_Q_FLOAT (1<<0)
336 #define SWM_Q_TRANSSZ (1<<1)
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
},
346 void expose(XEvent
*);
347 void keypress(XEvent
*);
348 void buttonpress(XEvent
*);
349 void configurerequest(XEvent
*);
350 void configurenotify(XEvent
*);
351 void destroynotify(XEvent
*);
352 void enternotify(XEvent
*);
353 void focusin(XEvent
*);
354 void mappingnotify(XEvent
*);
355 void maprequest(XEvent
*);
356 void propertynotify(XEvent
*);
357 void unmapnotify(XEvent
*);
358 void visibilitynotify(XEvent
*);
360 void (*handler
[LASTEvent
])(XEvent
*) = {
362 [KeyPress
] = keypress
,
363 [ButtonPress
] = buttonpress
,
364 [ConfigureRequest
] = configurerequest
,
365 [ConfigureNotify
] = configurenotify
,
366 [DestroyNotify
] = destroynotify
,
367 [EnterNotify
] = enternotify
,
369 [MappingNotify
] = mappingnotify
,
370 [MapRequest
] = maprequest
,
371 [PropertyNotify
] = propertynotify
,
372 [UnmapNotify
] = unmapnotify
,
373 [VisibilityNotify
] = visibilitynotify
,
377 name_to_color(char *colorname
)
381 XColor screen_def
, exact_def
;
382 unsigned long result
= 0;
383 char cname
[32] = "#";
385 cmap
= DefaultColormap(display
, screens
[0].idx
);
386 status
= XAllocNamedColor(display
, cmap
, colorname
,
387 &screen_def
, &exact_def
);
389 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
390 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
394 result
= screen_def
.pixel
;
396 fprintf(stderr
, "color '%s' not found.\n", colorname
);
402 setscreencolor(char *val
, int i
, int c
)
404 if (i
> 0 && i
<= ScreenCount(display
)) {
405 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
406 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
408 } else if (i
== -1) {
409 for (i
= 0; i
< ScreenCount(display
); i
++)
410 screens
[i
].c
[c
].color
= name_to_color(val
);
411 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
414 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
415 i
, ScreenCount(display
));
418 void new_region(struct swm_screen
*, int, int, int, int);
421 custom_region(char *val
)
423 unsigned int sidx
, x
, y
, w
, h
;
425 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
426 errx(1, "invalid custom region, "
427 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
428 if (sidx
< 1 || sidx
> ScreenCount(display
))
429 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
430 sidx
, ScreenCount(display
));
434 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
436 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
437 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
438 w
+ x
> DisplayWidth(display
, sidx
) ||
439 h
+ y
> DisplayHeight(display
, sidx
))
440 errx(1, "region %ux%u+%u+%u not within screen boundaries "
441 "(%ux%u)\n", w
, h
, x
, y
,
442 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
444 new_region(&screens
[sidx
], x
, y
, w
, h
);
448 varmatch(char *var
, char *name
, int *index
)
453 i
= strncmp(var
, name
, 255);
460 p
= var
+ strlen(name
);
463 bzero(buf
, sizeof buf
);
465 while (isdigit(*p
) && i
< sizeof buf
)
467 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
469 *index
= strtonum(buf
, 0, 99, NULL
);
473 /* conf file stuff */
474 #define SWM_CONF_WS "\n= \t"
475 #define SWM_CONF_FILE "scrotwm.conf"
477 conf_load(char *filename
)
480 char *line
, *cp
, *var
, *val
;
481 size_t len
, lineno
= 0;
484 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
486 if (filename
== NULL
)
489 if ((config
= fopen(filename
, "r")) == NULL
)
492 for (sc
= ScreenCount(display
);;) {
493 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
497 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
503 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
505 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
506 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
509 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
512 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
513 bar_enabled
= atoi(val
);
514 else if (!varmatch(var
, "bar_border", &i
))
515 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
516 else if (!varmatch(var
, "bar_color", &i
))
517 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
518 else if (!varmatch(var
, "bar_font_color", &i
))
519 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
520 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
521 asprintf(&bar_fonts
[0], "%s", val
);
522 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
523 asprintf(&bar_argv
[0], "%s", val
);
524 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
525 bar_delay
= atoi(val
);
531 if (!varmatch(var
, "color_focus", &i
))
532 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
533 else if (!varmatch(var
, "color_unfocus", &i
))
534 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
535 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
536 cycle_visible
= atoi(val
);
537 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
538 cycle_visible
= atoi(val
);
544 if (!strncmp(var
, "dialog_ratio",
545 strlen("dialog_ratio"))) {
546 dialog_ratio
= atof(val
);
547 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
554 if (!strncmp(var
, "region", strlen("region")))
561 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
562 asprintf(&spawn_term
[0], "%s", val
);
563 if (!strncmp(var
, "screenshot_enabled",
564 strlen("screenshot_enabled")))
565 ss_enabled
= atoi(val
);
566 if (!strncmp(var
, "screenshot_app",
567 strlen("screenshot_app")))
568 asprintf(&spawn_screenshot
[0], "%s", val
);
580 errx(1, "invalid conf file entry: %s=%s", var
, val
);
584 socket_setnonblock(int fd
)
588 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
589 err(1, "fcntl F_GETFL");
591 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
592 err(1, "fcntl F_SETFL");
596 bar_print(struct swm_region
*r
, char *s
)
598 XClearWindow(display
, r
->bar_window
);
599 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
600 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
609 bzero(bar_pipe
, sizeof bar_pipe
);
612 kill(bar_pid
, SIGTERM
);
615 strlcpy(bar_ext
, "", sizeof bar_ext
);
624 struct swm_region
*r
;
628 char loc
[SWM_BAR_MAX
];
631 if (bar_enabled
== 0)
634 if (bar_extra
&& bar_extra_running
) {
635 /* ignore short reads; it'll correct itself */
636 while ((b
= fgetln(stdin
, &len
)) != NULL
)
637 if (b
&& b
[len
- 1] == '\n') {
639 strlcpy(bar_ext
, b
, sizeof bar_ext
);
641 if (b
== NULL
&& errno
!= EAGAIN
) {
642 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
643 errno
, strerror(errno
));
647 strlcpy(bar_ext
, "", sizeof bar_ext
);
650 localtime_r(&tmt
, &tm
);
651 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
652 for (i
= 0; i
< ScreenCount(display
); i
++) {
654 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
655 snprintf(loc
, sizeof loc
, "%s %d:%d %s %s",
656 s
, x
++, r
->ws
->idx
+ 1, bar_ext
, bar_vertext
);
660 XSync(display
, False
);
671 bar_toggle(struct swm_region
*r
, union arg
*args
)
673 struct swm_region
*tmpr
;
674 int i
, j
, sc
= ScreenCount(display
);
676 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
679 for (i
= 0; i
< sc
; i
++)
680 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
681 XUnmapWindow(display
, tmpr
->bar_window
);
683 for (i
= 0; i
< sc
; i
++)
684 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
685 XMapRaised(display
, tmpr
->bar_window
);
687 bar_enabled
= !bar_enabled
;
688 XSync(display
, False
);
689 for (i
= 0; i
< sc
; i
++)
690 for (j
= 0; j
< SWM_WS_MAX
; j
++)
691 screens
[i
].ws
[j
].restack
= 1;
694 /* must be after stack */
695 for (i
= 0; i
< sc
; i
++)
696 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
703 XSetWindowAttributes wa
;
704 struct swm_region
*r
;
707 /* do this here because the conf file is in memory */
708 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
709 /* launch external status app */
710 bar_extra_running
= 1;
711 if (pipe(bar_pipe
) == -1)
712 err(1, "pipe error");
713 socket_setnonblock(bar_pipe
[0]);
714 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
715 if (dup2(bar_pipe
[0], 0) == -1)
717 if (dup2(bar_pipe
[1], 1) == -1)
719 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
720 err(1, "could not disable SIGPIPE");
721 switch (bar_pid
= fork()) {
723 err(1, "cannot fork");
727 execvp(bar_argv
[0], bar_argv
);
728 err(1, "%s external app failed", bar_argv
[0]);
730 default: /* parent */
736 bzero(&wa
, sizeof wa
);
737 for (i
= 0; i
< ScreenCount(display
); i
++)
738 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
740 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
741 wa
.background_pixel
=
742 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
743 XChangeWindowAttributes(display
, r
->bar_window
,
744 CWBackPixel
| CWBorderPixel
, &wa
);
750 bar_setup(struct swm_region
*r
)
754 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
755 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
761 if (bar_fonts
[i
] == NULL
)
762 errx(1, "couldn't load font");
763 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
765 r
->bar_window
= XCreateSimpleWindow(display
,
766 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
767 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
768 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
769 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
770 XSetFont(display
, bar_gc
, bar_fs
->fid
);
771 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
773 XMapRaised(display
, r
->bar_window
);
774 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
776 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
777 err(1, "could not install bar_signal");
782 version(struct swm_region
*r
, union arg
*args
)
784 bar_version
= !bar_version
;
786 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
788 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
793 config_win(struct ws_win
*win
)
797 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
798 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
799 ce
.type
= ConfigureNotify
;
800 ce
.display
= display
;
806 ce
.height
= win
->g
.h
;
807 ce
.border_width
= 1; /* XXX store this! */
809 ce
.override_redirect
= False
;
810 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
814 count_win(struct workspace
*ws
, int count_transient
)
819 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
820 if (count_transient
== 0 && win
->floating
)
822 if (count_transient
== 0 && win
->transient
)
826 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
832 quit(struct swm_region
*r
, union arg
*args
)
834 DNPRINTF(SWM_D_MISC
, "quit\n");
844 for (i
= 0; i
< ScreenCount(display
); i
++)
845 for (j
= 0; j
< SWM_WS_MAX
; j
++)
846 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
847 XUnmapWindow(display
, win
->id
);
851 restart(struct swm_region
*r
, union arg
*args
)
853 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
855 /* disable alarm because the following code may not be interrupted */
857 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
858 errx(1, "can't disable alarm");
863 XCloseDisplay(display
);
864 execvp(start_argv
[0], start_argv
);
865 fprintf(stderr
, "execvp failed\n");
871 root_to_region(Window root
)
873 struct swm_region
*r
= NULL
;
878 for (i
= 0; i
< ScreenCount(display
); i
++)
879 if (screens
[i
].root
== root
)
882 if (XQueryPointer(display
, screens
[i
].root
,
883 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
884 /* choose a region based on pointer location */
885 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
886 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
887 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
892 r
= TAILQ_FIRST(&screens
[i
].rl
);
898 find_window(Window id
)
903 for (i
= 0; i
< ScreenCount(display
); i
++)
904 for (j
= 0; j
< SWM_WS_MAX
; j
++)
905 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
912 spawn(struct swm_region
*r
, union arg
*args
)
917 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
919 * The double-fork construct avoids zombie processes and keeps the code
920 * clean from stupid signal handlers.
925 close(ConnectionNumber(display
));
926 setenv("LD_PRELOAD", SWM_LIB
, 1);
927 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
928 setenv("_SWM_WS", ret
, 1);
931 if (asprintf(&ret
, "%d", getpid())) {
932 setenv("_SWM_PID", ret
, 1);
936 /* kill stdin, mplayer, ssh-add etc. need that */
937 si
= open("/dev/null", O_RDONLY
, 0);
939 err(1, "open /dev/null");
940 if (dup2(si
, 0) == -1)
941 err(1, "dup2 /dev/null");
942 execvp(args
->argv
[0], args
->argv
);
943 fprintf(stderr
, "execvp failed\n");
952 spawnmenu(struct swm_region
*r
, union arg
*args
)
954 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
956 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
957 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
958 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
959 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
960 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
971 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
973 for (i
= 0; i
< ScreenCount(display
); i
++)
974 for (j
= 0; j
< SWM_WS_MAX
; j
++)
975 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
976 if (win
->ws
->r
== NULL
)
979 XSetWindowBorder(display
, win
->id
,
980 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
982 win
->ws
->focus
= NULL
;
988 focus_win(struct ws_win
*win
)
990 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
996 win
->ws
->focus
= win
;
997 if (win
->ws
->r
!= NULL
) {
999 if (!win
->got_focus
) {
1000 XSetWindowBorder(display
, win
->id
,
1001 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1002 grabbuttons(win
, 1);
1005 XSetInputFocus(display
, win
->id
,
1006 RevertToPointerRoot
, CurrentTime
);
1011 switchws(struct swm_region
*r
, union arg
*args
)
1013 int wsid
= args
->id
;
1014 struct swm_region
*this_r
, *other_r
;
1016 struct workspace
*new_ws
, *old_ws
;
1019 old_ws
= this_r
->ws
;
1020 new_ws
= &this_r
->s
->ws
[wsid
];
1022 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1023 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1026 if (new_ws
== old_ws
)
1029 other_r
= new_ws
->r
;
1031 /* if the other workspace is hidden, switch windows */
1032 /* map new window first to prevent ugly blinking */
1034 old_ws
->restack
= 1;
1036 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1037 XMapRaised(display
, win
->id
);
1039 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1040 XUnmapWindow(display
, win
->id
);
1042 other_r
->ws
= old_ws
;
1043 old_ws
->r
= other_r
;
1045 this_r
->ws
= new_ws
;
1051 focus_win(new_ws
->focus
);
1057 cyclews(struct swm_region
*r
, union arg
*args
)
1060 struct swm_screen
*s
= r
->s
;
1062 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1063 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1064 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1069 case SWM_ARG_ID_CYCLEWS_UP
:
1070 if (a
.id
< SWM_WS_MAX
- 1)
1075 case SWM_ARG_ID_CYCLEWS_DOWN
:
1079 a
.id
= SWM_WS_MAX
- 1;
1085 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1087 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1091 } while (a
.id
!= r
->ws
->idx
);
1095 cyclescr(struct swm_region
*r
, union arg
*args
)
1097 struct swm_region
*rr
;
1102 case SWM_ARG_ID_CYCLESC_UP
:
1103 rr
= TAILQ_NEXT(r
, entry
);
1105 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1107 case SWM_ARG_ID_CYCLESC_DOWN
:
1108 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1110 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1116 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1117 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1118 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1122 swapwin(struct swm_region
*r
, union arg
*args
)
1124 struct ws_win
*target
;
1125 struct ws_win_list
*wl
;
1128 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1129 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1130 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1131 if (cur_focus
== NULL
)
1134 wl
= &cur_focus
->ws
->winlist
;
1137 case SWM_ARG_ID_SWAPPREV
:
1138 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1139 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1141 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
1143 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
1145 case SWM_ARG_ID_SWAPNEXT
:
1146 target
= TAILQ_NEXT(cur_focus
, entry
);
1147 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1149 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1151 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
1153 case SWM_ARG_ID_SWAPMAIN
:
1154 target
= TAILQ_FIRST(wl
);
1155 if (target
== cur_focus
)
1157 TAILQ_REMOVE(wl
, target
, entry
);
1158 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
1159 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1160 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1163 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1172 focus(struct swm_region
*r
, union arg
*args
)
1174 struct ws_win
*winfocus
, *winlostfocus
;
1175 struct ws_win_list
*wl
;
1177 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1178 if (cur_focus
== NULL
)
1181 wl
= &cur_focus
->ws
->winlist
;
1183 winlostfocus
= cur_focus
;
1186 case SWM_ARG_ID_FOCUSPREV
:
1187 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1188 if (winfocus
== NULL
)
1189 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1192 case SWM_ARG_ID_FOCUSNEXT
:
1193 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1194 if (winfocus
== NULL
)
1195 winfocus
= TAILQ_FIRST(wl
);
1198 case SWM_ARG_ID_FOCUSMAIN
:
1199 winfocus
= TAILQ_FIRST(wl
);
1206 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1209 XMapRaised(display
, winfocus
->id
);
1210 focus_win(winfocus
);
1211 XSync(display
, False
);
1215 cycle_layout(struct swm_region
*r
, union arg
*args
)
1217 struct workspace
*ws
= r
->ws
;
1219 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1222 if (ws
->cur_layout
->l_stack
== NULL
)
1223 ws
->cur_layout
= &layouts
[0];
1229 stack_config(struct swm_region
*r
, union arg
*args
)
1231 struct workspace
*ws
= r
->ws
;
1233 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1236 if (ws
->cur_layout
->l_config
!= NULL
)
1237 ws
->cur_layout
->l_config(ws
, args
->id
);
1239 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1245 struct swm_geometry g
;
1246 struct swm_region
*r
;
1249 DNPRINTF(SWM_D_STACK
, "stack\n");
1251 for (i
= 0; i
< ScreenCount(display
); i
++) {
1253 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1254 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1255 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1257 /* start with screen geometry, adjust for bar */
1267 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1270 XSync(display
, False
);
1274 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1279 bzero(&wc
, sizeof wc
);
1280 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1281 wc
.border_width
= 1;
1282 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1283 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1284 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1286 wc
.width
= win
->g
.w
;
1287 wc
.height
= win
->g
.h
;
1292 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1293 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1296 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1297 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1299 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1302 #define SWAPXY(g) do { \
1304 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1305 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1308 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1311 struct swm_geometry win_g
, r_g
= *g
;
1312 struct ws_win
*win
, *winfocus
;
1313 int i
, j
, s
, w_inc
, h_inc
, w_base
, h_base
, stacks
;
1314 int hrh
, extra
, h_slice
, last_h
= 0;
1315 int split
, colno
, winno
, mwin
, msize
, mscale
;
1316 int remain
, missing
, v_slice
;;
1319 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1320 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1322 if ((winno
= count_win(ws
, 0)) == 0)
1325 if (ws
->focus
== NULL
)
1326 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1327 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1329 win
= TAILQ_FIRST(&ws
->winlist
);
1331 w_inc
= win
->sh
.width_inc
;
1332 w_base
= win
->sh
.base_width
;
1333 mwin
= ws
->l_state
.horizontal_mwin
;
1334 mscale
= ws
->l_state
.horizontal_msize
;
1335 stacks
= ws
->l_state
.horizontal_stacks
;
1338 w_inc
= win
->sh
.height_inc
;
1339 w_base
= win
->sh
.base_height
;
1340 mwin
= ws
->l_state
.vertical_mwin
;
1341 mscale
= ws
->l_state
.vertical_msize
;
1342 stacks
= ws
->l_state
.vertical_stacks
;
1346 if (stacks
> winno
- mwin
)
1347 stacks
= winno
- mwin
;
1349 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1350 if (mwin
&& winno
> mwin
) {
1351 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1355 win_g
.w
= v_slice
* mscale
;
1357 if (w_inc
> 1 && w_inc
< v_slice
) {
1358 /* adjust for window's requested size increment */
1359 remain
= (win_g
.w
- w_base
) % w_inc
;
1360 missing
= w_inc
- remain
;
1362 if (missing
<= extra
|| j
== 0) {
1373 win_g
.x
+= r_g
.w
- msize
;
1376 colno
= split
= (winno
- mwin
) / 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
- split
) / s
;
1395 colno
+= (winno
- split
) % s
;
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
+ 2) - (stacks
* 2)) / stacks
;
1409 h_inc
= win
->sh
.width_inc
;
1410 h_base
= win
->sh
.base_width
;
1412 h_inc
= win
->sh
.height_inc
;
1413 h_base
= win
->sh
.base_height
;
1415 if (j
== colno
- 1) {
1416 win_g
.h
= hrh
+ extra
;
1417 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1418 /* adjust for window's requested size increment */
1419 remain
= (win_g
.h
- h_base
) % h_inc
;
1420 missing
= h_inc
- remain
;
1422 if (missing
<= extra
|| j
== 0) {
1434 win_g
.y
+= last_h
+ 2;
1436 bzero(&wc
, sizeof wc
);
1437 wc
.border_width
= 1;
1439 win
->g
.x
= wc
.x
= win_g
.y
;
1440 win
->g
.y
= wc
.y
= win_g
.x
;
1441 win
->g
.w
= wc
.width
= win_g
.h
;
1442 win
->g
.h
= wc
.height
= win_g
.w
;
1444 win
->g
.x
= wc
.x
= win_g
.x
;
1445 win
->g
.y
= wc
.y
= win_g
.y
;
1446 win
->g
.w
= wc
.width
= win_g
.w
;
1447 win
->g
.h
= wc
.height
= win_g
.h
;
1449 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1450 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1451 XMapRaised(display
, win
->id
);
1458 /* now, stack all the floaters and transients */
1459 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1460 if (win
->transient
== 0 && win
->floating
== 0)
1463 stack_floater(win
, ws
->r
);
1464 XMapRaised(display
, win
->id
);
1468 focus_win(winfocus
); /* has to be done outside of the loop */
1472 vertical_config(struct workspace
*ws
, int id
)
1474 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1477 case SWM_ARG_ID_STACKRESET
:
1478 case SWM_ARG_ID_STACKINIT
:
1479 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1480 ws
->l_state
.vertical_mwin
= 1;
1481 ws
->l_state
.vertical_stacks
= 1;
1483 case SWM_ARG_ID_MASTERSHRINK
:
1484 if (ws
->l_state
.vertical_msize
> 1)
1485 ws
->l_state
.vertical_msize
--;
1487 case SWM_ARG_ID_MASTERGROW
:
1488 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1489 ws
->l_state
.vertical_msize
++;
1491 case SWM_ARG_ID_MASTERADD
:
1492 ws
->l_state
.vertical_mwin
++;
1494 case SWM_ARG_ID_MASTERDEL
:
1495 if (ws
->l_state
.vertical_mwin
> 0)
1496 ws
->l_state
.vertical_mwin
--;
1497 case SWM_ARG_ID_COLINC
:
1498 ws
->l_state
.vertical_stacks
++;
1500 case SWM_ARG_ID_COLDEC
:
1501 if (ws
->l_state
.vertical_stacks
> 1)
1502 ws
->l_state
.vertical_stacks
--;
1510 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1512 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1514 stack_master(ws
, g
, 0, 0);
1518 horizontal_config(struct workspace
*ws
, int id
)
1520 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1523 case SWM_ARG_ID_STACKRESET
:
1524 case SWM_ARG_ID_STACKINIT
:
1525 ws
->l_state
.horizontal_mwin
= 1;
1526 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1527 ws
->l_state
.horizontal_stacks
= 1;
1529 case SWM_ARG_ID_MASTERSHRINK
:
1530 if (ws
->l_state
.horizontal_msize
> 1)
1531 ws
->l_state
.horizontal_msize
--;
1533 case SWM_ARG_ID_MASTERGROW
:
1534 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1535 ws
->l_state
.horizontal_msize
++;
1537 case SWM_ARG_ID_MASTERADD
:
1538 ws
->l_state
.horizontal_mwin
++;
1540 case SWM_ARG_ID_MASTERDEL
:
1541 if (ws
->l_state
.horizontal_mwin
> 0)
1542 ws
->l_state
.horizontal_mwin
--;
1544 case SWM_ARG_ID_COLINC
:
1545 ws
->l_state
.horizontal_stacks
++;
1547 case SWM_ARG_ID_COLDEC
:
1548 if (ws
->l_state
.horizontal_stacks
> 1)
1549 ws
->l_state
.horizontal_stacks
--;
1556 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1558 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1560 stack_master(ws
, g
, 1, 0);
1563 /* fullscreen view */
1565 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1567 struct swm_geometry gg
= *g
;
1568 struct ws_win
*win
, *winfocus
;
1571 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1573 if (count_win(ws
, 0) == 0)
1576 if (ws
->focus
== NULL
)
1577 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1578 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1580 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1581 if (win
->transient
!= 0 || win
->floating
!= 0) {
1582 if (win
== ws
->focus
) {
1584 stack_floater(win
, ws
->r
);
1585 XMapRaised(display
, win
->id
);
1587 XUnmapWindow(display
, win
->id
);
1589 bzero(&wc
, sizeof wc
);
1590 wc
.border_width
= 1;
1591 win
->g
.x
= wc
.x
= gg
.x
;
1592 win
->g
.y
= wc
.y
= gg
.y
;
1593 win
->g
.w
= wc
.width
= gg
.w
;
1594 win
->g
.h
= wc
.height
= gg
.h
;
1595 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1596 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1598 if (win
== ws
->focus
) {
1599 XMapRaised(display
, win
->id
);
1601 XUnmapWindow(display
, win
->id
);
1606 focus_win(winfocus
); /* has to be done outside of the loop */
1610 send_to_ws(struct swm_region
*r
, union arg
*args
)
1612 int wsid
= args
->id
;
1613 struct ws_win
*win
= cur_focus
;
1614 struct workspace
*ws
, *nws
;
1615 Atom ws_idx_atom
= 0;
1616 unsigned char ws_idx_str
[SWM_PROPLEN
];
1621 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1624 nws
= &win
->s
->ws
[wsid
];
1626 XUnmapWindow(display
, win
->id
);
1628 /* find a window to focus */
1629 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1630 if (ws
->focus
== NULL
)
1631 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1632 if (ws
->focus
== win
)
1635 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1637 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1640 /* Try to update the window's workspace property */
1641 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1643 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1644 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1646 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1647 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1650 if (count_win(nws
, 1) == 1)
1659 wkill(struct swm_region
*r
, union arg
*args
)
1661 DNPRINTF(SWM_D_MISC
, "wkill\n");
1662 if(r
->ws
->focus
!= NULL
)
1663 XKillClient(display
, r
->ws
->focus
->id
);
1667 screenshot(struct swm_region
*r
, union arg
*args
)
1671 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1673 if (ss_enabled
== 0)
1677 case SWM_ARG_ID_SS_ALL
:
1678 spawn_screenshot
[1] = "full";
1680 case SWM_ARG_ID_SS_WINDOW
:
1681 spawn_screenshot
[1] = "window";
1686 a
.argv
= spawn_screenshot
;
1691 floating_toggle(struct swm_region
*r
, union arg
*args
)
1693 struct ws_win
*win
= cur_focus
;
1698 win
->floating
= !win
->floating
;
1704 /* key definitions */
1708 void (*func
)(struct swm_region
*r
, union arg
*);
1711 /* modifier key function argument */
1712 { MODKEY
, XK_space
, cycle_layout
, {0} },
1713 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1714 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1715 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1716 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1717 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1718 { MODKEY
| ShiftMask
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_COLINC
} },
1719 { MODKEY
| ShiftMask
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_COLDEC
} },
1720 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1721 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1722 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1723 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1724 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1725 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1726 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1727 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1728 { MODKEY
, XK_q
, restart
, {0} },
1729 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1730 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1731 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1732 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1733 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1734 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1735 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1736 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1737 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1738 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1739 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1740 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1741 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1742 { MODKEY
| ShiftMask
, XK_Right
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1743 { MODKEY
| ShiftMask
, XK_Left
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1744 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1745 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1746 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1747 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1748 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1749 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1750 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1751 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1752 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1753 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1754 { MODKEY
, XK_b
, bar_toggle
, {0} },
1755 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1756 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1757 { MODKEY
| ShiftMask
, XK_x
, wkill
, {0} },
1758 { MODKEY
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
1759 { MODKEY
| ShiftMask
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
1760 { MODKEY
, XK_t
, floating_toggle
,{0} },
1761 { MODKEY
| ShiftMask
, XK_v
, version
, {0} },
1762 { MODKEY
| ShiftMask
, XK_Delete
, spawn
, {.argv
= spawn_lock
} },
1766 resize_window(struct ws_win
*win
, int center
)
1770 struct swm_region
*r
;
1772 r
= root_to_region(win
->wa
.root
);
1773 bzero(&wc
, sizeof wc
);
1774 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1775 wc
.border_width
= 1;
1776 wc
.width
= win
->g
.w
;
1777 wc
.height
= win
->g
.h
;
1778 if (center
== SWM_ARG_ID_CENTER
) {
1779 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1780 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1784 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1785 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1787 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1792 resize(struct ws_win
*win
, union arg
*args
)
1797 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1798 win
->id
, win
->floating
, win
->transient
);
1800 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1803 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1804 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1806 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1808 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1809 SubstructureRedirectMask
, &ev
);
1811 case ConfigureRequest
:
1814 handler
[ev
.type
](&ev
);
1817 if (ev
.xmotion
.x
< 0)
1819 if (ev
.xmotion
.y
< 0)
1821 win
->g
.w
= ev
.xmotion
.x
;
1822 win
->g
.h
= ev
.xmotion
.y
;
1824 /* not free, don't sync more than 60 times / second */
1825 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1826 time
= ev
.xmotion
.time
;
1827 XSync(display
, False
);
1828 resize_window(win
, args
->id
);
1832 } while (ev
.type
!= ButtonRelease
);
1834 XSync(display
, False
);
1835 resize_window(win
, args
->id
);
1837 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1839 XUngrabPointer(display
, CurrentTime
);
1842 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1846 move_window(struct ws_win
*win
)
1850 struct swm_region
*r
;
1852 r
= root_to_region(win
->wa
.root
);
1853 bzero(&wc
, sizeof wc
);
1858 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
1859 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1861 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1866 move(struct ws_win
*win
, union arg
*args
)
1872 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
1873 win
->id
, win
->floating
, win
->transient
);
1875 if (win
->floating
== 0) {
1881 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1882 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1884 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1886 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1887 SubstructureRedirectMask
, &ev
);
1889 case ConfigureRequest
:
1892 handler
[ev
.type
](&ev
);
1895 win
->g
.x
= ev
.xmotion
.x_root
;
1896 win
->g
.y
= ev
.xmotion
.y_root
;
1898 /* not free, don't sync more than 60 times / second */
1899 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1900 time
= ev
.xmotion
.time
;
1901 XSync(display
, False
);
1906 } while (ev
.type
!= ButtonRelease
);
1908 XSync(display
, False
);
1911 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1912 XUngrabPointer(display
, CurrentTime
);
1917 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1921 enum { client_click
, root_click
};
1923 unsigned int action
;
1925 unsigned int button
;
1926 void (*func
)(struct ws_win
*, union arg
*);
1929 /* action key mouse button func args */
1930 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
1931 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
1932 { client_click
, MODKEY
, Button1
, move
, {0} },
1936 updatenumlockmask(void)
1939 XModifierKeymap
*modmap
;
1941 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1943 modmap
= XGetModifierMapping(display
);
1944 for (i
= 0; i
< 8; i
++)
1945 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1946 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1947 == XKeysymToKeycode(display
, XK_Num_Lock
))
1948 numlockmask
= (1 << i
);
1950 XFreeModifiermap(modmap
);
1956 unsigned int i
, j
, k
;
1958 unsigned int modifiers
[] =
1959 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1961 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1962 updatenumlockmask();
1964 for (k
= 0; k
< ScreenCount(display
); k
++) {
1965 if (TAILQ_EMPTY(&screens
[k
].rl
))
1967 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1968 for (i
= 0; i
< LENGTH(keys
); i
++) {
1969 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1970 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1971 XGrabKey(display
, code
,
1972 keys
[i
].mod
| modifiers
[j
],
1973 screens
[k
].root
, True
,
1974 GrabModeAsync
, GrabModeAsync
);
1980 grabbuttons(struct ws_win
*win
, int focused
)
1983 unsigned int modifiers
[] =
1984 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1986 updatenumlockmask();
1987 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
1989 for (i
= 0; i
< LENGTH(buttons
); i
++)
1990 if (buttons
[i
].action
== client_click
)
1991 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1992 XGrabButton(display
, buttons
[i
].button
,
1993 buttons
[i
].mask
| modifiers
[j
],
1994 win
->id
, False
, BUTTONMASK
,
1995 GrabModeAsync
, GrabModeSync
, None
,
1998 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
1999 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
2005 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
2013 XKeyEvent
*ev
= &e
->xkey
;
2015 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
2017 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
2018 for (i
= 0; i
< LENGTH(keys
); i
++)
2019 if (keysym
== keys
[i
].keysym
2020 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
2022 keys
[i
].func(root_to_region(ev
->root
),
2027 buttonpress(XEvent
*e
)
2029 XButtonPressedEvent
*ev
= &e
->xbutton
;
2034 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2036 action
= root_click
;
2037 if ((win
= find_window(ev
->window
)) == NULL
)
2041 action
= client_click
;
2044 for (i
= 0; i
< LENGTH(buttons
); i
++)
2045 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2046 buttons
[i
].button
== ev
->button
&&
2047 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2048 buttons
[i
].func(win
, &buttons
[i
].args
);
2052 set_win_state(struct ws_win
*win
, long state
)
2054 long data
[] = {state
, None
};
2056 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2058 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2059 (unsigned char *)data
, 2);
2063 manage_window(Window id
)
2066 struct workspace
*ws
;
2068 int format
, i
, ws_idx
;
2069 unsigned long nitems
, bytes
;
2070 Atom ws_idx_atom
= 0, type
;
2071 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2072 struct swm_region
*r
;
2076 if ((win
= find_window(id
)) != NULL
)
2077 return (win
); /* already being managed */
2079 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2080 errx(1, "calloc: failed to allocate memory for new window");
2082 /* Get all the window data in one shot */
2083 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2085 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2086 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2087 XGetWindowAttributes(display
, id
, &win
->wa
);
2088 XGetTransientForHint(display
, id
, &trans
);
2089 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2091 win
->transient
= trans
;
2092 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2093 (unsigned)win
->id
, win
->transient
);
2097 * Figure out where to put the window. If it was previously assigned to
2098 * a workspace (either by spawn() or manually moving), and isn't
2099 * transient, * put it in the same workspace
2101 r
= root_to_region(win
->wa
.root
);
2102 if (prop
&& win
->transient
== 0) {
2103 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2104 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2106 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2109 ws
= &r
->s
->ws
[ws_idx
];
2113 /* set up the window layout */
2116 win
->s
= r
->s
; /* this never changes */
2117 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2119 win
->g
.w
= win
->wa
.width
;
2120 win
->g
.h
= win
->wa
.height
;
2121 win
->g
.x
= win
->wa
.x
;
2122 win
->g
.y
= win
->wa
.y
;
2124 /* Set window properties so we can remember this after reincarnation */
2125 if (ws_idx_atom
&& prop
== NULL
&&
2126 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2127 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2129 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2130 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2134 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2135 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2136 win
->ch
.res_class
, win
->ch
.res_name
);
2137 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2138 quirks
[i
].quirk
!= 0; i
++){
2139 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2140 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2141 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2142 win
->ch
.res_class
, win
->ch
.res_name
);
2143 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2145 win
->quirks
= quirks
[i
].quirk
;
2150 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2151 PropertyChangeMask
| StructureNotifyMask
);
2153 set_win_state(win
, NormalState
);
2155 /* make new win focused */
2162 unmanage_window(struct ws_win
*win
)
2164 struct workspace
*ws
;
2169 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
2171 /* don't unmanage if we are switching workspaces */
2176 /* find a window to focus */
2177 if (ws
->focus
== win
)
2178 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2179 if (ws
->focus
== NULL
)
2180 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2181 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2185 focus_win(ws
->focus
);
2187 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
2188 set_win_state(win
, WithdrawnState
);
2189 if (win
->ch
.res_class
)
2190 XFree(win
->ch
.res_class
);
2191 if (win
->ch
.res_name
)
2192 XFree(win
->ch
.res_name
);
2197 configurerequest(XEvent
*e
)
2199 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2204 if ((win
= find_window(ev
->window
)) == NULL
)
2208 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2210 bzero(&wc
, sizeof wc
);
2213 wc
.width
= ev
->width
;
2214 wc
.height
= ev
->height
;
2215 wc
.border_width
= ev
->border_width
;
2216 wc
.sibling
= ev
->above
;
2217 wc
.stack_mode
= ev
->detail
;
2218 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2220 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2222 if (win
->floating
) {
2223 if (ev
->value_mask
& CWX
)
2225 if (ev
->value_mask
& CWY
)
2227 if (ev
->value_mask
& CWWidth
)
2228 win
->g
.w
= ev
->width
;
2229 if (ev
->value_mask
& CWHeight
)
2230 win
->g
.h
= ev
->height
;
2231 if (win
->ws
->r
!= NULL
) {
2232 /* this seems to be full screen */
2233 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2235 win
->g
.w
= WIDTH(win
->ws
->r
);
2236 ev
->value_mask
|= CWX
| CWWidth
;
2238 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2241 win
->g
.h
= HEIGHT(win
->ws
->r
);
2242 ev
->value_mask
|= CWY
| CWHeight
;
2245 if ((ev
->value_mask
& (CWX
|CWY
)) &&
2246 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
2248 XMoveResizeWindow(display
, win
->id
,
2249 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2256 configurenotify(XEvent
*e
)
2258 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2259 e
->xconfigure
.window
);
2263 destroynotify(XEvent
*e
)
2266 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2268 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2270 if ((win
= find_window(ev
->window
)) != NULL
) {
2271 unmanage_window(win
);
2277 enternotify(XEvent
*e
)
2279 XCrossingEvent
*ev
= &e
->xcrossing
;
2282 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2285 /* eat event(r) to prevent autofocus */
2290 if ((win
= find_window(ev
->window
)) != NULL
)
2297 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2301 mappingnotify(XEvent
*e
)
2303 XMappingEvent
*ev
= &e
->xmapping
;
2305 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2307 XRefreshKeyboardMapping(ev
);
2308 if (ev
->request
== MappingKeyboard
)
2313 maprequest(XEvent
*e
)
2315 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2316 XWindowAttributes wa
;
2318 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2319 e
->xmaprequest
.window
);
2321 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2323 if (wa
.override_redirect
)
2325 manage_window(e
->xmaprequest
.window
);
2330 propertynotify(XEvent
*e
)
2333 XPropertyEvent
*ev
= &e
->xproperty
;
2335 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2338 if (ev
->state
== PropertyDelete
)
2339 return; /* ignore */
2340 win
= find_window(ev
->window
);
2345 case XA_WM_NORMAL_HINTS
:
2348 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2349 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2350 if (win
->sh
.flags
& PMinSize
) {
2351 win
->g
.w
= win
->sh
.min_width
;
2352 win
->g
.h
= win
->sh
.min_height
;
2353 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2355 XMoveResizeWindow(display
, win
->id
,
2356 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2365 unmapnotify(XEvent
*e
)
2367 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2370 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2372 if ((win
= find_window(ev
->window
)) != NULL
)
2374 unmanage_window(win
);
2378 visibilitynotify(XEvent
*e
)
2381 struct swm_region
*r
;
2383 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2384 e
->xvisibility
.window
);
2385 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2386 for (i
= 0; i
< ScreenCount(display
); i
++)
2387 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2388 if (e
->xvisibility
.window
== r
->bar_window
)
2393 xerror_start(Display
*d
, XErrorEvent
*ee
)
2400 xerror(Display
*d
, XErrorEvent
*ee
)
2402 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2410 xerrorxlib
= XSetErrorHandler(xerror_start
);
2412 /* this causes an error if some other window manager is running */
2413 XSelectInput(display
, DefaultRootWindow(display
),
2414 SubstructureRedirectMask
);
2415 XSync(display
, False
);
2419 XSetErrorHandler(xerror
);
2420 XSync(display
, False
);
2429 unsigned char *p
= NULL
;
2430 unsigned long n
, extra
;
2433 astate
= XInternAtom(display
, "WM_STATE", False
);
2434 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
2435 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
2436 if (status
!= Success
)
2445 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
2447 struct swm_region
*r
, *n
;
2448 struct workspace
*ws
= NULL
;
2451 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
2452 s
->idx
, w
, h
, x
, y
);
2454 /* remove any conflicting regions */
2455 n
= TAILQ_FIRST(&s
->rl
);
2458 n
= TAILQ_NEXT(r
, entry
);
2459 if (X(r
) < (x
+ w
) &&
2460 (X(r
) + WIDTH(r
)) > x
&&
2462 (Y(r
) + HEIGHT(r
)) > y
) {
2463 XDestroyWindow(display
, r
->bar_window
);
2464 TAILQ_REMOVE(&s
->rl
, r
, entry
);
2465 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
2469 /* search old regions for one to reuse */
2471 /* size + location match */
2472 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2473 if (X(r
) == x
&& Y(r
) == y
&&
2474 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2478 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2479 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2483 TAILQ_REMOVE(&s
->orl
, r
, entry
);
2484 /* try to use old region's workspace */
2485 if (r
->ws
->r
== NULL
)
2488 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
2489 errx(1, "calloc: failed to allocate memory for screen");
2491 /* if we don't have a workspace already, find one */
2493 for (i
= 0; i
< SWM_WS_MAX
; i
++)
2494 if (s
->ws
[i
].r
== NULL
) {
2501 errx(1, "no free workspaces\n");
2510 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
2517 #ifdef SWM_XRR_HAS_CRTC
2519 XRRScreenResources
*sr
;
2522 #endif /* SWM_XRR_HAS_CRTC */
2523 struct swm_region
*r
;
2526 if (i
>= ScreenCount(display
))
2527 errx(1, "invalid screen");
2529 /* remove any old regions */
2530 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
2532 XDestroyWindow(display
, r
->bar_window
);
2533 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
2534 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
2537 /* map virtual screens onto physical screens */
2538 #ifdef SWM_XRR_HAS_CRTC
2539 if (xrandr_support
) {
2540 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
2542 new_region(&screens
[i
], 0, 0,
2543 DisplayWidth(display
, i
),
2544 DisplayHeight(display
, i
));
2548 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
2549 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
2550 if (ci
->noutput
== 0)
2553 if (ci
!= NULL
&& ci
->mode
== None
)
2554 new_region(&screens
[i
], 0, 0,
2555 DisplayWidth(display
, i
),
2556 DisplayHeight(display
, i
));
2558 new_region(&screens
[i
],
2559 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
2562 XRRFreeCrtcInfo(ci
);
2563 XRRFreeScreenResources(sr
);
2565 #endif /* SWM_XRR_HAS_CRTC */
2567 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
2568 DisplayHeight(display
, i
));
2573 screenchange(XEvent
*e
) {
2574 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
2575 struct swm_region
*r
;
2579 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
2581 if (!XRRUpdateConfiguration(e
))
2584 /* silly event doesn't include the screen index */
2585 for (i
= 0; i
< ScreenCount(display
); i
++)
2586 if (screens
[i
].root
== xe
->root
)
2588 if (i
>= ScreenCount(display
))
2589 errx(1, "screenchange: screen not found\n");
2591 /* brute force for now, just re-enumerate the regions */
2594 /* hide any windows that went away */
2595 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2596 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
2597 XUnmapWindow(display
, win
->id
);
2604 Window d1
, d2
, *wins
= NULL
;
2605 XWindowAttributes wa
;
2608 int errorbase
, major
, minor
;
2609 struct workspace
*ws
;
2613 if ((screens
= calloc(ScreenCount(display
),
2614 sizeof(struct swm_screen
))) == NULL
)
2615 errx(1, "calloc: screens");
2617 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2619 /* initial Xrandr setup */
2620 xrandr_support
= XRRQueryExtension(display
,
2621 &xrandr_eventbase
, &errorbase
);
2623 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
2626 /* map physical screens */
2627 for (i
= 0; i
< ScreenCount(display
); i
++) {
2628 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
2630 TAILQ_INIT(&screens
[i
].rl
);
2631 TAILQ_INIT(&screens
[i
].orl
);
2632 screens
[i
].root
= RootWindow(display
, i
);
2634 /* set default colors */
2635 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
2636 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
2637 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
2638 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
2639 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
2641 /* init all workspaces */
2642 /* XXX these should be dynamically allocated too */
2643 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
2644 ws
= &screens
[i
].ws
[j
];
2649 TAILQ_INIT(&ws
->winlist
);
2651 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
2652 if (layouts
[k
].l_config
!= NULL
)
2653 layouts
[k
].l_config(ws
,
2654 SWM_ARG_ID_STACKINIT
);
2655 ws
->cur_layout
= &layouts
[0];
2657 /* grab existing windows (before we build the bars)*/
2658 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
2664 XRRSelectInput(display
, screens
[i
].root
,
2665 RRScreenChangeNotifyMask
);
2667 /* attach windows to a region */
2668 /* normal windows */
2669 for (j
= 0; j
< no
; j
++) {
2670 XGetWindowAttributes(display
, wins
[j
], &wa
);
2671 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
2672 wa
.override_redirect
||
2673 XGetTransientForHint(display
, wins
[j
], &d1
))
2676 if (wa
.map_state
== IsViewable
||
2677 getstate(wins
[j
]) == NormalState
)
2678 manage_window(wins
[j
]);
2680 /* transient windows */
2681 for (j
= 0; j
< no
; j
++) {
2682 if (!XGetWindowAttributes(display
, wins
[j
], &wa
))
2685 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
2686 (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) ==
2688 manage_window(wins
[j
]);
2698 main(int argc
, char *argv
[])
2701 char conf
[PATH_MAX
], *cfile
= NULL
;
2708 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
2709 SWM_VERSION
, cvstag
);
2710 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2711 warnx("no locale support");
2713 if (!(display
= XOpenDisplay(0)))
2714 errx(1, "can not open display");
2717 errx(1, "other wm running");
2719 astate
= XInternAtom(display
, "WM_STATE", False
);
2721 /* look for local and global conf file */
2722 pwd
= getpwuid(getuid());
2724 errx(1, "invalid user %d", getuid());
2728 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2729 if (stat(conf
, &sb
) != -1) {
2730 if (S_ISREG(sb
.st_mode
))
2733 /* try global conf file */
2734 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2735 if (!stat(conf
, &sb
))
2736 if (S_ISREG(sb
.st_mode
))
2743 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2748 xfd
= ConnectionNumber(display
);
2752 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2754 errx(1, "select failed");
2759 while (XPending(display
)) {
2760 XNextEvent(display
, &e
);
2761 if (e
.type
< LASTEvent
) {
2762 if (handler
[e
.type
])
2763 handler
[e
.type
](&e
);
2765 DNPRINTF(SWM_D_EVENT
,
2766 "unkown event: %d\n", e
.type
);
2768 switch (e
.type
- xrandr_eventbase
) {
2769 case RRScreenChangeNotify
:
2773 DNPRINTF(SWM_D_EVENT
,
2774 "unkown event: %d\n", e
.type
);
2781 XCloseDisplay(display
);