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 ws_win
*focus_prev
; /* may be NULL */
265 struct swm_region
*r
; /* may be NULL */
266 struct ws_win_list winlist
; /* list of windows in ws */
270 int horizontal_msize
;
272 int horizontal_stacks
;
279 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
280 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
282 /* physical screen mapping */
283 #define SWM_WS_MAX (10) /* XXX Too small? */
285 int idx
; /* screen index */
286 struct swm_region_list rl
; /* list of regions on this screen */
287 struct swm_region_list orl
; /* list of old regions */
289 struct workspace ws
[SWM_WS_MAX
];
295 } c
[SWM_S_COLOR_MAX
];
297 struct swm_screen
*screens
;
300 struct ws_win
*cur_focus
= NULL
;
302 /* args to functions */
305 #define SWM_ARG_ID_FOCUSNEXT (0)
306 #define SWM_ARG_ID_FOCUSPREV (1)
307 #define SWM_ARG_ID_FOCUSMAIN (2)
308 #define SWM_ARG_ID_SWAPNEXT (3)
309 #define SWM_ARG_ID_SWAPPREV (4)
310 #define SWM_ARG_ID_SWAPMAIN (5)
311 #define SWM_ARG_ID_MASTERSHRINK (6)
312 #define SWM_ARG_ID_MASTERGROW (7)
313 #define SWM_ARG_ID_MASTERADD (8)
314 #define SWM_ARG_ID_MASTERDEL (9)
315 #define SWM_ARG_ID_STACKRESET (10)
316 #define SWM_ARG_ID_STACKINIT (11)
317 #define SWM_ARG_ID_CYCLEWS_UP (12)
318 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
319 #define SWM_ARG_ID_CYCLESC_UP (14)
320 #define SWM_ARG_ID_CYCLESC_DOWN (15)
321 #define SWM_ARG_ID_STACKINC (16)
322 #define SWM_ARG_ID_STACKDEC (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) /* float this window */
336 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
337 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
339 { "MPlayer", "xv", SWM_Q_FLOAT
},
340 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
},
341 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT
},
342 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
},
343 { "Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
348 void expose(XEvent
*);
349 void keypress(XEvent
*);
350 void buttonpress(XEvent
*);
351 void configurerequest(XEvent
*);
352 void configurenotify(XEvent
*);
353 void destroynotify(XEvent
*);
354 void enternotify(XEvent
*);
355 void focusin(XEvent
*);
356 void mappingnotify(XEvent
*);
357 void maprequest(XEvent
*);
358 void propertynotify(XEvent
*);
359 void unmapnotify(XEvent
*);
360 void visibilitynotify(XEvent
*);
362 void (*handler
[LASTEvent
])(XEvent
*) = {
364 [KeyPress
] = keypress
,
365 [ButtonPress
] = buttonpress
,
366 [ConfigureRequest
] = configurerequest
,
367 [ConfigureNotify
] = configurenotify
,
368 [DestroyNotify
] = destroynotify
,
369 [EnterNotify
] = enternotify
,
371 [MappingNotify
] = mappingnotify
,
372 [MapRequest
] = maprequest
,
373 [PropertyNotify
] = propertynotify
,
374 [UnmapNotify
] = unmapnotify
,
375 [VisibilityNotify
] = visibilitynotify
,
379 name_to_color(char *colorname
)
383 XColor screen_def
, exact_def
;
384 unsigned long result
= 0;
385 char cname
[32] = "#";
387 cmap
= DefaultColormap(display
, screens
[0].idx
);
388 status
= XAllocNamedColor(display
, cmap
, colorname
,
389 &screen_def
, &exact_def
);
391 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
392 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
396 result
= screen_def
.pixel
;
398 fprintf(stderr
, "color '%s' not found.\n", colorname
);
404 setscreencolor(char *val
, int i
, int c
)
406 if (i
> 0 && i
<= ScreenCount(display
)) {
407 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
408 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
410 } else if (i
== -1) {
411 for (i
= 0; i
< ScreenCount(display
); i
++)
412 screens
[i
].c
[c
].color
= name_to_color(val
);
413 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
416 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
417 i
, ScreenCount(display
));
420 void new_region(struct swm_screen
*, int, int, int, int);
423 custom_region(char *val
)
425 unsigned int sidx
, x
, y
, w
, h
;
427 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
428 errx(1, "invalid custom region, "
429 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
430 if (sidx
< 1 || sidx
> ScreenCount(display
))
431 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
432 sidx
, ScreenCount(display
));
436 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
438 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
439 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
440 w
+ x
> DisplayWidth(display
, sidx
) ||
441 h
+ y
> DisplayHeight(display
, sidx
))
442 errx(1, "region %ux%u+%u+%u not within screen boundaries "
443 "(%ux%u)\n", w
, h
, x
, y
,
444 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
446 new_region(&screens
[sidx
], x
, y
, w
, h
);
450 varmatch(char *var
, char *name
, int *index
)
455 i
= strncmp(var
, name
, 255);
462 p
= var
+ strlen(name
);
465 bzero(buf
, sizeof buf
);
467 while (isdigit(*p
) && i
< sizeof buf
)
469 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
471 *index
= strtonum(buf
, 0, 99, NULL
);
475 /* conf file stuff */
476 #define SWM_CONF_WS "\n= \t"
477 #define SWM_CONF_FILE "scrotwm.conf"
479 conf_load(char *filename
)
482 char *line
, *cp
, *var
, *val
;
483 size_t len
, lineno
= 0;
486 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
488 if (filename
== NULL
)
491 if ((config
= fopen(filename
, "r")) == NULL
)
494 for (sc
= ScreenCount(display
);;) {
495 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
499 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
505 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
507 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
508 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
511 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
514 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
515 bar_enabled
= atoi(val
);
516 else if (!varmatch(var
, "bar_border", &i
))
517 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
518 else if (!varmatch(var
, "bar_color", &i
))
519 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
520 else if (!varmatch(var
, "bar_font_color", &i
))
521 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
522 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
523 asprintf(&bar_fonts
[0], "%s", val
);
524 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
525 asprintf(&bar_argv
[0], "%s", val
);
526 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
527 bar_delay
= atoi(val
);
533 if (!varmatch(var
, "color_focus", &i
))
534 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
535 else if (!varmatch(var
, "color_unfocus", &i
))
536 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
537 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
538 cycle_visible
= atoi(val
);
539 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
540 cycle_visible
= atoi(val
);
546 if (!strncmp(var
, "dialog_ratio",
547 strlen("dialog_ratio"))) {
548 dialog_ratio
= atof(val
);
549 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
556 if (!strncmp(var
, "region", strlen("region")))
563 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
564 asprintf(&spawn_term
[0], "%s", val
);
565 if (!strncmp(var
, "screenshot_enabled",
566 strlen("screenshot_enabled")))
567 ss_enabled
= atoi(val
);
568 if (!strncmp(var
, "screenshot_app",
569 strlen("screenshot_app")))
570 asprintf(&spawn_screenshot
[0], "%s", val
);
582 errx(1, "invalid conf file entry: %s=%s", var
, val
);
586 socket_setnonblock(int fd
)
590 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
591 err(1, "fcntl F_GETFL");
593 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
594 err(1, "fcntl F_SETFL");
598 bar_print(struct swm_region
*r
, char *s
)
600 XClearWindow(display
, r
->bar_window
);
601 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
602 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
611 bzero(bar_pipe
, sizeof bar_pipe
);
614 kill(bar_pid
, SIGTERM
);
617 strlcpy(bar_ext
, "", sizeof bar_ext
);
626 struct swm_region
*r
;
630 char loc
[SWM_BAR_MAX
];
633 if (bar_enabled
== 0)
636 if (bar_extra
&& bar_extra_running
) {
637 /* ignore short reads; it'll correct itself */
638 while ((b
= fgetln(stdin
, &len
)) != NULL
)
639 if (b
&& b
[len
- 1] == '\n') {
641 strlcpy(bar_ext
, b
, sizeof bar_ext
);
643 if (b
== NULL
&& errno
!= EAGAIN
) {
644 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
645 errno
, strerror(errno
));
649 strlcpy(bar_ext
, "", sizeof bar_ext
);
652 localtime_r(&tmt
, &tm
);
653 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
654 for (i
= 0; i
< ScreenCount(display
); i
++) {
656 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
657 snprintf(loc
, sizeof loc
, "%s %d:%d %s %s",
658 s
, x
++, r
->ws
->idx
+ 1, bar_ext
, bar_vertext
);
662 XSync(display
, False
);
673 bar_toggle(struct swm_region
*r
, union arg
*args
)
675 struct swm_region
*tmpr
;
676 int i
, j
, sc
= ScreenCount(display
);
678 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
681 for (i
= 0; i
< sc
; i
++)
682 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
683 XUnmapWindow(display
, tmpr
->bar_window
);
685 for (i
= 0; i
< sc
; i
++)
686 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
687 XMapRaised(display
, tmpr
->bar_window
);
689 bar_enabled
= !bar_enabled
;
690 XSync(display
, False
);
691 for (i
= 0; i
< sc
; i
++)
692 for (j
= 0; j
< SWM_WS_MAX
; j
++)
693 screens
[i
].ws
[j
].restack
= 1;
696 /* must be after stack */
697 for (i
= 0; i
< sc
; i
++)
698 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
705 XSetWindowAttributes wa
;
706 struct swm_region
*r
;
709 /* do this here because the conf file is in memory */
710 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
711 /* launch external status app */
712 bar_extra_running
= 1;
713 if (pipe(bar_pipe
) == -1)
714 err(1, "pipe error");
715 socket_setnonblock(bar_pipe
[0]);
716 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
717 if (dup2(bar_pipe
[0], 0) == -1)
719 if (dup2(bar_pipe
[1], 1) == -1)
721 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
722 err(1, "could not disable SIGPIPE");
723 switch (bar_pid
= fork()) {
725 err(1, "cannot fork");
729 execvp(bar_argv
[0], bar_argv
);
730 err(1, "%s external app failed", bar_argv
[0]);
732 default: /* parent */
738 bzero(&wa
, sizeof wa
);
739 for (i
= 0; i
< ScreenCount(display
); i
++)
740 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
742 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
743 wa
.background_pixel
=
744 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
745 XChangeWindowAttributes(display
, r
->bar_window
,
746 CWBackPixel
| CWBorderPixel
, &wa
);
752 bar_setup(struct swm_region
*r
)
756 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
757 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
763 if (bar_fonts
[i
] == NULL
)
764 errx(1, "couldn't load font");
765 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
767 r
->bar_window
= XCreateSimpleWindow(display
,
768 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
769 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
770 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
771 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
772 XSetFont(display
, bar_gc
, bar_fs
->fid
);
773 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
775 XMapRaised(display
, r
->bar_window
);
776 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
778 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
779 err(1, "could not install bar_signal");
784 version(struct swm_region
*r
, union arg
*args
)
786 bar_version
= !bar_version
;
788 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
790 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
795 config_win(struct ws_win
*win
)
799 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
800 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
801 ce
.type
= ConfigureNotify
;
802 ce
.display
= display
;
808 ce
.height
= win
->g
.h
;
809 ce
.border_width
= 1; /* XXX store this! */
811 ce
.override_redirect
= False
;
812 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
816 count_win(struct workspace
*ws
, int count_transient
)
821 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
822 if (count_transient
== 0 && win
->floating
)
824 if (count_transient
== 0 && win
->transient
)
828 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
834 quit(struct swm_region
*r
, union arg
*args
)
836 DNPRINTF(SWM_D_MISC
, "quit\n");
846 for (i
= 0; i
< ScreenCount(display
); i
++)
847 for (j
= 0; j
< SWM_WS_MAX
; j
++)
848 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
849 XUnmapWindow(display
, win
->id
);
853 restart(struct swm_region
*r
, union arg
*args
)
855 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
857 /* disable alarm because the following code may not be interrupted */
859 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
860 errx(1, "can't disable alarm");
865 XCloseDisplay(display
);
866 execvp(start_argv
[0], start_argv
);
867 fprintf(stderr
, "execvp failed\n");
873 root_to_region(Window root
)
875 struct swm_region
*r
= NULL
;
880 for (i
= 0; i
< ScreenCount(display
); i
++)
881 if (screens
[i
].root
== root
)
884 if (XQueryPointer(display
, screens
[i
].root
,
885 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
886 /* choose a region based on pointer location */
887 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
888 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
889 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
894 r
= TAILQ_FIRST(&screens
[i
].rl
);
900 find_window(Window id
)
905 for (i
= 0; i
< ScreenCount(display
); i
++)
906 for (j
= 0; j
< SWM_WS_MAX
; j
++)
907 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
914 spawn(struct swm_region
*r
, union arg
*args
)
919 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
921 * The double-fork construct avoids zombie processes and keeps the code
922 * clean from stupid signal handlers.
927 close(ConnectionNumber(display
));
928 setenv("LD_PRELOAD", SWM_LIB
, 1);
929 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
930 setenv("_SWM_WS", ret
, 1);
933 if (asprintf(&ret
, "%d", getpid())) {
934 setenv("_SWM_PID", ret
, 1);
938 /* kill stdin, mplayer, ssh-add etc. need that */
939 si
= open("/dev/null", O_RDONLY
, 0);
941 err(1, "open /dev/null");
942 if (dup2(si
, 0) == -1)
943 err(1, "dup2 /dev/null");
944 execvp(args
->argv
[0], args
->argv
);
945 fprintf(stderr
, "execvp failed\n");
954 spawnmenu(struct swm_region
*r
, union arg
*args
)
956 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
958 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
959 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
960 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
961 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
962 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
973 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
975 for (i
= 0; i
< ScreenCount(display
); i
++)
976 for (j
= 0; j
< SWM_WS_MAX
; j
++)
977 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
978 if (win
->ws
->r
== NULL
)
981 XSetWindowBorder(display
, win
->id
,
982 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
984 win
->ws
->focus
= NULL
;
990 focus_win(struct ws_win
*win
)
992 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
997 if (win
->ws
->focus
!= win
&& win
->ws
->focus
!= NULL
)
998 win
->ws
->focus_prev
= win
->ws
->focus
;
1001 win
->ws
->focus
= win
;
1002 if (win
->ws
->r
!= NULL
) {
1004 if (!win
->got_focus
) {
1005 XSetWindowBorder(display
, win
->id
,
1006 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1007 grabbuttons(win
, 1);
1010 XSetInputFocus(display
, win
->id
,
1011 RevertToPointerRoot
, CurrentTime
);
1016 switchws(struct swm_region
*r
, union arg
*args
)
1018 int wsid
= args
->id
;
1019 struct swm_region
*this_r
, *other_r
;
1021 struct workspace
*new_ws
, *old_ws
;
1024 old_ws
= this_r
->ws
;
1025 new_ws
= &this_r
->s
->ws
[wsid
];
1027 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1028 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1031 if (new_ws
== old_ws
)
1034 other_r
= new_ws
->r
;
1036 /* if the other workspace is hidden, switch windows */
1037 /* map new window first to prevent ugly blinking */
1039 old_ws
->restack
= 1;
1041 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1042 XMapRaised(display
, win
->id
);
1044 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1045 XUnmapWindow(display
, win
->id
);
1047 other_r
->ws
= old_ws
;
1048 old_ws
->r
= other_r
;
1050 this_r
->ws
= new_ws
;
1056 focus_win(new_ws
->focus
);
1062 cyclews(struct swm_region
*r
, union arg
*args
)
1065 struct swm_screen
*s
= r
->s
;
1067 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1068 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1069 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1074 case SWM_ARG_ID_CYCLEWS_UP
:
1075 if (a
.id
< SWM_WS_MAX
- 1)
1080 case SWM_ARG_ID_CYCLEWS_DOWN
:
1084 a
.id
= SWM_WS_MAX
- 1;
1090 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1092 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1096 } while (a
.id
!= r
->ws
->idx
);
1100 cyclescr(struct swm_region
*r
, union arg
*args
)
1102 struct swm_region
*rr
;
1107 case SWM_ARG_ID_CYCLESC_UP
:
1108 rr
= TAILQ_NEXT(r
, entry
);
1110 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1112 case SWM_ARG_ID_CYCLESC_DOWN
:
1113 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1115 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1121 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1122 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1123 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1127 swapwin(struct swm_region
*r
, union arg
*args
)
1129 struct ws_win
*target
, *source
;
1130 struct ws_win_list
*wl
;
1133 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1134 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1135 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1136 if (cur_focus
== NULL
)
1140 wl
= &source
->ws
->winlist
;
1143 case SWM_ARG_ID_SWAPPREV
:
1144 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
1145 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1147 TAILQ_INSERT_TAIL(wl
, source
, entry
);
1149 TAILQ_INSERT_BEFORE(target
, source
, entry
);
1151 case SWM_ARG_ID_SWAPNEXT
:
1152 target
= TAILQ_NEXT(source
, entry
);
1153 TAILQ_REMOVE(wl
, source
, entry
);
1155 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1157 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
1159 case SWM_ARG_ID_SWAPMAIN
:
1160 target
= TAILQ_FIRST(wl
);
1161 if (target
== source
) {
1162 if (source
->ws
->focus_prev
!= NULL
&&
1163 source
->ws
->focus_prev
!= target
)
1165 source
= source
->ws
->focus_prev
;
1169 source
->ws
->focus_prev
= target
;
1170 TAILQ_REMOVE(wl
, target
, entry
);
1171 TAILQ_INSERT_BEFORE(source
, target
, entry
);
1172 TAILQ_REMOVE(wl
, source
, entry
);
1173 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1176 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1185 focus(struct swm_region
*r
, union arg
*args
)
1187 struct ws_win
*winfocus
, *winlostfocus
;
1188 struct ws_win_list
*wl
;
1190 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1191 if (cur_focus
== NULL
)
1194 wl
= &cur_focus
->ws
->winlist
;
1196 winlostfocus
= cur_focus
;
1199 case SWM_ARG_ID_FOCUSPREV
:
1200 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1201 if (winfocus
== NULL
)
1202 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1205 case SWM_ARG_ID_FOCUSNEXT
:
1206 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1207 if (winfocus
== NULL
)
1208 winfocus
= TAILQ_FIRST(wl
);
1211 case SWM_ARG_ID_FOCUSMAIN
:
1212 winfocus
= TAILQ_FIRST(wl
);
1219 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1222 XMapRaised(display
, winfocus
->id
);
1223 focus_win(winfocus
);
1224 XSync(display
, False
);
1228 cycle_layout(struct swm_region
*r
, union arg
*args
)
1230 struct workspace
*ws
= r
->ws
;
1232 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1235 if (ws
->cur_layout
->l_stack
== NULL
)
1236 ws
->cur_layout
= &layouts
[0];
1242 stack_config(struct swm_region
*r
, union arg
*args
)
1244 struct workspace
*ws
= r
->ws
;
1246 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1249 if (ws
->cur_layout
->l_config
!= NULL
)
1250 ws
->cur_layout
->l_config(ws
, args
->id
);
1252 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1258 struct swm_geometry g
;
1259 struct swm_region
*r
;
1262 DNPRINTF(SWM_D_STACK
, "stack\n");
1264 for (i
= 0; i
< ScreenCount(display
); i
++) {
1266 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1267 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1268 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1270 /* start with screen geometry, adjust for bar */
1280 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1283 XSync(display
, False
);
1287 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1292 bzero(&wc
, sizeof wc
);
1293 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1294 wc
.border_width
= 1;
1295 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1296 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1297 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1299 wc
.width
= win
->g
.w
;
1300 wc
.height
= win
->g
.h
;
1305 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1306 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1309 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1310 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1312 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1315 #define SWAPXY(g) do { \
1317 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1318 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1321 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1324 struct swm_geometry win_g
, r_g
= *g
;
1325 struct ws_win
*win
, *winfocus
;
1326 int i
, j
, s
, w_inc
, h_inc
, w_base
, h_base
, stacks
;
1327 int hrh
, extra
, h_slice
, last_h
= 0;
1328 int split
, colno
, winno
, mwin
, msize
, mscale
;
1329 int remain
, missing
, v_slice
;;
1332 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1333 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1335 if ((winno
= count_win(ws
, 0)) == 0)
1338 if (ws
->focus
== NULL
)
1339 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1340 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1342 win
= TAILQ_FIRST(&ws
->winlist
);
1344 w_inc
= win
->sh
.width_inc
;
1345 w_base
= win
->sh
.base_width
;
1346 mwin
= ws
->l_state
.horizontal_mwin
;
1347 mscale
= ws
->l_state
.horizontal_msize
;
1348 stacks
= ws
->l_state
.horizontal_stacks
;
1351 w_inc
= win
->sh
.height_inc
;
1352 w_base
= win
->sh
.base_height
;
1353 mwin
= ws
->l_state
.vertical_mwin
;
1354 mscale
= ws
->l_state
.vertical_msize
;
1355 stacks
= ws
->l_state
.vertical_stacks
;
1359 if (stacks
> winno
- mwin
)
1360 stacks
= winno
- mwin
;
1364 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1365 if (mwin
&& winno
> mwin
) {
1366 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1370 win_g
.w
= v_slice
* mscale
;
1372 if (w_inc
> 1 && w_inc
< v_slice
) {
1373 /* adjust for window's requested size increment */
1374 remain
= (win_g
.w
- w_base
) % w_inc
;
1375 missing
= w_inc
- remain
;
1377 if (missing
<= extra
|| j
== 0) {
1388 win_g
.x
+= r_g
.w
- msize
;
1391 colno
= split
= winno
/ stacks
;
1392 win_g
.w
= ((r_g
.w
- (stacks
* 2) + 2) / stacks
);
1394 hrh
= r_g
.h
/ colno
;
1395 extra
= r_g
.h
- (colno
* hrh
);
1398 /* stack all the tiled windows */
1399 i
= j
= 0, s
= stacks
;
1400 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1401 if (win
->transient
!= 0 || win
->floating
!= 0)
1404 if (split
&& i
== split
) {
1405 colno
= (winno
- mwin
) / stacks
;
1406 if (s
<= (winno
- mwin
) % stacks
)
1408 split
= split
+ colno
;
1409 hrh
= (r_g
.h
/ colno
);
1410 extra
= r_g
.h
- (colno
* hrh
);
1414 win_g
.x
+= win_g
.w
+ 2;
1415 win_g
.w
= (r_g
.w
- msize
- (stacks
* 2)) / stacks
;
1417 win_g
.w
+= (r_g
.w
- msize
- (stacks
* 2)) %
1424 h_inc
= win
->sh
.width_inc
;
1425 h_base
= win
->sh
.base_width
;
1427 h_inc
= win
->sh
.height_inc
;
1428 h_base
= win
->sh
.base_height
;
1430 if (j
== colno
- 1) {
1431 win_g
.h
= hrh
+ extra
;
1432 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1433 /* adjust for window's requested size increment */
1434 remain
= (win_g
.h
- h_base
) % h_inc
;
1435 missing
= h_inc
- remain
;
1437 if (missing
<= extra
|| j
== 0) {
1449 win_g
.y
+= last_h
+ 2;
1451 bzero(&wc
, sizeof wc
);
1452 wc
.border_width
= 1;
1454 win
->g
.x
= wc
.x
= win_g
.y
;
1455 win
->g
.y
= wc
.y
= win_g
.x
;
1456 win
->g
.w
= wc
.width
= win_g
.h
;
1457 win
->g
.h
= wc
.height
= win_g
.w
;
1459 win
->g
.x
= wc
.x
= win_g
.x
;
1460 win
->g
.y
= wc
.y
= win_g
.y
;
1461 win
->g
.w
= wc
.width
= win_g
.w
;
1462 win
->g
.h
= wc
.height
= win_g
.h
;
1464 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1465 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1466 XMapRaised(display
, win
->id
);
1473 /* now, stack all the floaters and transients */
1474 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1475 if (win
->transient
== 0 && win
->floating
== 0)
1478 stack_floater(win
, ws
->r
);
1479 XMapRaised(display
, win
->id
);
1483 focus_win(winfocus
); /* has to be done outside of the loop */
1487 vertical_config(struct workspace
*ws
, int id
)
1489 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1492 case SWM_ARG_ID_STACKRESET
:
1493 case SWM_ARG_ID_STACKINIT
:
1494 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1495 ws
->l_state
.vertical_mwin
= 1;
1496 ws
->l_state
.vertical_stacks
= 1;
1498 case SWM_ARG_ID_MASTERSHRINK
:
1499 if (ws
->l_state
.vertical_msize
> 1)
1500 ws
->l_state
.vertical_msize
--;
1502 case SWM_ARG_ID_MASTERGROW
:
1503 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1504 ws
->l_state
.vertical_msize
++;
1506 case SWM_ARG_ID_MASTERADD
:
1507 ws
->l_state
.vertical_mwin
++;
1509 case SWM_ARG_ID_MASTERDEL
:
1510 if (ws
->l_state
.vertical_mwin
> 0)
1511 ws
->l_state
.vertical_mwin
--;
1513 case SWM_ARG_ID_STACKINC
:
1514 ws
->l_state
.vertical_stacks
++;
1516 case SWM_ARG_ID_STACKDEC
:
1517 if (ws
->l_state
.vertical_stacks
> 1)
1518 ws
->l_state
.vertical_stacks
--;
1526 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1528 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1530 stack_master(ws
, g
, 0, 0);
1534 horizontal_config(struct workspace
*ws
, int id
)
1536 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1539 case SWM_ARG_ID_STACKRESET
:
1540 case SWM_ARG_ID_STACKINIT
:
1541 ws
->l_state
.horizontal_mwin
= 1;
1542 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1543 ws
->l_state
.horizontal_stacks
= 1;
1545 case SWM_ARG_ID_MASTERSHRINK
:
1546 if (ws
->l_state
.horizontal_msize
> 1)
1547 ws
->l_state
.horizontal_msize
--;
1549 case SWM_ARG_ID_MASTERGROW
:
1550 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1551 ws
->l_state
.horizontal_msize
++;
1553 case SWM_ARG_ID_MASTERADD
:
1554 ws
->l_state
.horizontal_mwin
++;
1556 case SWM_ARG_ID_MASTERDEL
:
1557 if (ws
->l_state
.horizontal_mwin
> 0)
1558 ws
->l_state
.horizontal_mwin
--;
1560 case SWM_ARG_ID_STACKINC
:
1561 ws
->l_state
.horizontal_stacks
++;
1563 case SWM_ARG_ID_STACKDEC
:
1564 if (ws
->l_state
.horizontal_stacks
> 1)
1565 ws
->l_state
.horizontal_stacks
--;
1573 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1575 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1577 stack_master(ws
, g
, 1, 0);
1580 /* fullscreen view */
1582 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1584 struct swm_geometry gg
= *g
;
1585 struct ws_win
*win
, *winfocus
;
1588 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1590 if (count_win(ws
, 0) == 0)
1593 if (ws
->focus
== NULL
)
1594 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1595 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1597 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1598 if (win
->transient
!= 0 || win
->floating
!= 0) {
1599 if (win
== ws
->focus
) {
1601 stack_floater(win
, ws
->r
);
1602 XMapRaised(display
, win
->id
);
1604 XUnmapWindow(display
, win
->id
);
1606 bzero(&wc
, sizeof wc
);
1607 wc
.border_width
= 1;
1608 win
->g
.x
= wc
.x
= gg
.x
;
1609 win
->g
.y
= wc
.y
= gg
.y
;
1610 win
->g
.w
= wc
.width
= gg
.w
;
1611 win
->g
.h
= wc
.height
= gg
.h
;
1612 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1613 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1615 if (win
== ws
->focus
) {
1616 XMapRaised(display
, win
->id
);
1618 XUnmapWindow(display
, win
->id
);
1623 focus_win(winfocus
); /* has to be done outside of the loop */
1627 send_to_ws(struct swm_region
*r
, union arg
*args
)
1629 int wsid
= args
->id
;
1630 struct ws_win
*win
= cur_focus
;
1631 struct workspace
*ws
, *nws
;
1632 Atom ws_idx_atom
= 0;
1633 unsigned char ws_idx_str
[SWM_PROPLEN
];
1638 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1641 nws
= &win
->s
->ws
[wsid
];
1643 XUnmapWindow(display
, win
->id
);
1645 /* find a window to focus */
1646 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1647 if (ws
->focus
== NULL
)
1648 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1649 if (ws
->focus
== win
)
1652 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1654 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1657 /* Try to update the window's workspace property */
1658 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1660 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1661 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1663 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1664 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1667 if (count_win(nws
, 1) == 1)
1676 wkill(struct swm_region
*r
, union arg
*args
)
1678 DNPRINTF(SWM_D_MISC
, "wkill\n");
1679 if(r
->ws
->focus
!= NULL
)
1680 XKillClient(display
, r
->ws
->focus
->id
);
1684 screenshot(struct swm_region
*r
, union arg
*args
)
1688 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1690 if (ss_enabled
== 0)
1694 case SWM_ARG_ID_SS_ALL
:
1695 spawn_screenshot
[1] = "full";
1697 case SWM_ARG_ID_SS_WINDOW
:
1698 spawn_screenshot
[1] = "window";
1703 a
.argv
= spawn_screenshot
;
1708 floating_toggle(struct swm_region
*r
, union arg
*args
)
1710 struct ws_win
*win
= cur_focus
;
1715 win
->floating
= !win
->floating
;
1721 /* key definitions */
1725 void (*func
)(struct swm_region
*r
, union arg
*);
1728 /* modifier key function argument */
1729 { MODKEY
, XK_space
, cycle_layout
, {0} },
1730 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1731 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1732 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1733 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1734 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1735 { MODKEY
| ShiftMask
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
1736 { MODKEY
| ShiftMask
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
1737 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1738 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1739 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1740 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1741 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1742 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1743 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1744 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1745 { MODKEY
, XK_q
, restart
, {0} },
1746 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1747 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1748 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1749 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1750 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1751 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1752 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1753 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1754 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1755 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1756 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1757 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1758 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1759 { MODKEY
| ShiftMask
, XK_Right
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1760 { MODKEY
| ShiftMask
, XK_Left
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1761 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1762 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1763 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1764 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1765 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1766 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1767 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1768 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1769 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1770 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1771 { MODKEY
, XK_b
, bar_toggle
, {0} },
1772 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1773 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1774 { MODKEY
| ShiftMask
, XK_x
, wkill
, {0} },
1775 { MODKEY
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
1776 { MODKEY
| ShiftMask
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
1777 { MODKEY
, XK_t
, floating_toggle
,{0} },
1778 { MODKEY
| ShiftMask
, XK_v
, version
, {0} },
1779 { MODKEY
| ShiftMask
, XK_Delete
, spawn
, {.argv
= spawn_lock
} },
1780 { MODKEY
| ShiftMask
, XK_i
, spawn
, {.argv
= spawn_initscr
} },
1784 resize_window(struct ws_win
*win
, int center
)
1788 struct swm_region
*r
;
1790 r
= root_to_region(win
->wa
.root
);
1791 bzero(&wc
, sizeof wc
);
1792 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1793 wc
.border_width
= 1;
1794 wc
.width
= win
->g
.w
;
1795 wc
.height
= win
->g
.h
;
1796 if (center
== SWM_ARG_ID_CENTER
) {
1797 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1798 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1802 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1803 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1805 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1810 resize(struct ws_win
*win
, union arg
*args
)
1815 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1816 win
->id
, win
->floating
, win
->transient
);
1818 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1821 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1822 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1824 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1826 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1827 SubstructureRedirectMask
, &ev
);
1829 case ConfigureRequest
:
1832 handler
[ev
.type
](&ev
);
1835 if (ev
.xmotion
.x
< 0)
1837 if (ev
.xmotion
.y
< 0)
1839 win
->g
.w
= ev
.xmotion
.x
;
1840 win
->g
.h
= ev
.xmotion
.y
;
1842 /* not free, don't sync more than 60 times / second */
1843 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1844 time
= ev
.xmotion
.time
;
1845 XSync(display
, False
);
1846 resize_window(win
, args
->id
);
1850 } while (ev
.type
!= ButtonRelease
);
1852 XSync(display
, False
);
1853 resize_window(win
, args
->id
);
1855 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1857 XUngrabPointer(display
, CurrentTime
);
1860 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1864 move_window(struct ws_win
*win
)
1868 struct swm_region
*r
;
1870 r
= root_to_region(win
->wa
.root
);
1871 bzero(&wc
, sizeof wc
);
1876 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
1877 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1879 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1884 move(struct ws_win
*win
, union arg
*args
)
1890 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
1891 win
->id
, win
->floating
, win
->transient
);
1893 if (win
->floating
== 0) {
1899 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1900 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1902 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1904 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1905 SubstructureRedirectMask
, &ev
);
1907 case ConfigureRequest
:
1910 handler
[ev
.type
](&ev
);
1913 win
->g
.x
= ev
.xmotion
.x_root
;
1914 win
->g
.y
= ev
.xmotion
.y_root
;
1916 /* not free, don't sync more than 60 times / second */
1917 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1918 time
= ev
.xmotion
.time
;
1919 XSync(display
, False
);
1924 } while (ev
.type
!= ButtonRelease
);
1926 XSync(display
, False
);
1929 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1930 XUngrabPointer(display
, CurrentTime
);
1935 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1939 enum { client_click
, root_click
};
1941 unsigned int action
;
1943 unsigned int button
;
1944 void (*func
)(struct ws_win
*, union arg
*);
1947 /* action key mouse button func args */
1948 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
1949 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
1950 { client_click
, MODKEY
, Button1
, move
, {0} },
1954 updatenumlockmask(void)
1957 XModifierKeymap
*modmap
;
1959 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1961 modmap
= XGetModifierMapping(display
);
1962 for (i
= 0; i
< 8; i
++)
1963 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1964 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1965 == XKeysymToKeycode(display
, XK_Num_Lock
))
1966 numlockmask
= (1 << i
);
1968 XFreeModifiermap(modmap
);
1974 unsigned int i
, j
, k
;
1976 unsigned int modifiers
[] =
1977 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1979 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1980 updatenumlockmask();
1982 for (k
= 0; k
< ScreenCount(display
); k
++) {
1983 if (TAILQ_EMPTY(&screens
[k
].rl
))
1985 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1986 for (i
= 0; i
< LENGTH(keys
); i
++) {
1987 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1988 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1989 XGrabKey(display
, code
,
1990 keys
[i
].mod
| modifiers
[j
],
1991 screens
[k
].root
, True
,
1992 GrabModeAsync
, GrabModeAsync
);
1998 grabbuttons(struct ws_win
*win
, int focused
)
2001 unsigned int modifiers
[] =
2002 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
2004 updatenumlockmask();
2005 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
2007 for (i
= 0; i
< LENGTH(buttons
); i
++)
2008 if (buttons
[i
].action
== client_click
)
2009 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2010 XGrabButton(display
, buttons
[i
].button
,
2011 buttons
[i
].mask
| modifiers
[j
],
2012 win
->id
, False
, BUTTONMASK
,
2013 GrabModeAsync
, GrabModeSync
, None
,
2016 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
2017 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
2023 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
2031 XKeyEvent
*ev
= &e
->xkey
;
2033 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
2035 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
2036 for (i
= 0; i
< LENGTH(keys
); i
++)
2037 if (keysym
== keys
[i
].keysym
2038 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
2040 keys
[i
].func(root_to_region(ev
->root
),
2045 buttonpress(XEvent
*e
)
2047 XButtonPressedEvent
*ev
= &e
->xbutton
;
2052 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2054 action
= root_click
;
2055 if ((win
= find_window(ev
->window
)) == NULL
)
2059 action
= client_click
;
2062 for (i
= 0; i
< LENGTH(buttons
); i
++)
2063 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2064 buttons
[i
].button
== ev
->button
&&
2065 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2066 buttons
[i
].func(win
, &buttons
[i
].args
);
2070 set_win_state(struct ws_win
*win
, long state
)
2072 long data
[] = {state
, None
};
2074 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2076 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2077 (unsigned char *)data
, 2);
2081 manage_window(Window id
)
2084 struct workspace
*ws
;
2086 int format
, i
, ws_idx
;
2087 unsigned long nitems
, bytes
;
2088 Atom ws_idx_atom
= 0, type
;
2089 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2090 struct swm_region
*r
;
2095 if ((win
= find_window(id
)) != NULL
)
2096 return (win
); /* already being managed */
2098 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2099 errx(1, "calloc: failed to allocate memory for new window");
2101 /* Get all the window data in one shot */
2102 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2104 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2105 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2106 XGetWindowAttributes(display
, id
, &win
->wa
);
2107 XGetTransientForHint(display
, id
, &trans
);
2108 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2110 win
->transient
= trans
;
2111 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2112 (unsigned)win
->id
, win
->transient
);
2116 * Figure out where to put the window. If it was previously assigned to
2117 * a workspace (either by spawn() or manually moving), and isn't
2118 * transient, * put it in the same workspace
2120 r
= root_to_region(win
->wa
.root
);
2121 if (prop
&& win
->transient
== 0) {
2122 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2123 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2125 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2128 ws
= &r
->s
->ws
[ws_idx
];
2132 /* set up the window layout */
2135 win
->s
= r
->s
; /* this never changes */
2136 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2138 win
->g
.w
= win
->wa
.width
;
2139 win
->g
.h
= win
->wa
.height
;
2140 win
->g
.x
= win
->wa
.x
;
2141 win
->g
.y
= win
->wa
.y
;
2143 /* Set window properties so we can remember this after reincarnation */
2144 if (ws_idx_atom
&& prop
== NULL
&&
2145 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2146 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2148 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2149 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2153 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2154 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2155 win
->ch
.res_class
, win
->ch
.res_name
);
2156 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2157 quirks
[i
].quirk
!= 0; i
++){
2158 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2159 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2160 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2161 win
->ch
.res_class
, win
->ch
.res_name
);
2162 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2164 win
->quirks
= quirks
[i
].quirk
;
2169 /* alter window position if quirky */
2170 if (win
->quirks
& SWM_Q_ANYWHERE
) {
2171 win
->manual
= 1; /* don't center the quirky windows */
2172 bzero(&wc
, sizeof wc
);
2174 if (win
->g
.y
< bar_height
) {
2175 win
->g
.y
= wc
.y
= bar_height
;
2178 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
2179 win
->g
.x
= wc
.x
= WIDTH(win
->ws
->r
) - win
->g
.w
- 2;
2182 wc
.border_width
= 1;
2183 mask
|= CWBorderWidth
;
2184 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2187 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2188 PropertyChangeMask
| StructureNotifyMask
);
2190 set_win_state(win
, NormalState
);
2192 /* floaters need to be mapped if they are in the current workspace */
2193 if (win
->floating
&& (ws
->idx
== r
->ws
->idx
))
2194 XMapRaised(display
, win
->id
);
2196 /* make new win focused */
2203 unmanage_window(struct ws_win
*win
)
2205 struct workspace
*ws
;
2210 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
2212 /* don't unmanage if we are switching workspaces */
2217 /* find a window to focus */
2218 if (ws
->focus
== win
)
2219 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2220 if (ws
->focus
== NULL
)
2221 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2222 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2226 focus_win(ws
->focus
);
2227 if (ws
->focus_prev
== win
)
2228 ws
->focus_prev
= NULL
;
2230 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
2231 set_win_state(win
, WithdrawnState
);
2232 if (win
->ch
.res_class
)
2233 XFree(win
->ch
.res_class
);
2234 if (win
->ch
.res_name
)
2235 XFree(win
->ch
.res_name
);
2240 configurerequest(XEvent
*e
)
2242 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2247 if ((win
= find_window(ev
->window
)) == NULL
)
2251 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2253 bzero(&wc
, sizeof wc
);
2256 wc
.width
= ev
->width
;
2257 wc
.height
= ev
->height
;
2258 wc
.border_width
= ev
->border_width
;
2259 wc
.sibling
= ev
->above
;
2260 wc
.stack_mode
= ev
->detail
;
2261 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2263 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2265 if (win
->floating
) {
2266 if (ev
->value_mask
& CWX
)
2268 if (ev
->value_mask
& CWY
)
2270 if (ev
->value_mask
& CWWidth
)
2271 win
->g
.w
= ev
->width
;
2272 if (ev
->value_mask
& CWHeight
)
2273 win
->g
.h
= ev
->height
;
2274 if (win
->ws
->r
!= NULL
) {
2275 /* this seems to be full screen */
2276 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2278 win
->g
.w
= WIDTH(win
->ws
->r
);
2279 ev
->value_mask
|= CWX
| CWWidth
;
2281 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2284 win
->g
.h
= HEIGHT(win
->ws
->r
);
2285 ev
->value_mask
|= CWY
| CWHeight
;
2288 if ((ev
->value_mask
& (CWX
| CWY
)) &&
2289 !(ev
->value_mask
& (CWWidth
| CWHeight
)))
2291 XMoveResizeWindow(display
, win
->id
,
2292 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2299 configurenotify(XEvent
*e
)
2301 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2302 e
->xconfigure
.window
);
2306 destroynotify(XEvent
*e
)
2309 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2311 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2313 if ((win
= find_window(ev
->window
)) != NULL
) {
2314 unmanage_window(win
);
2320 enternotify(XEvent
*e
)
2322 XCrossingEvent
*ev
= &e
->xcrossing
;
2325 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2328 /* eat event(r) to prevent autofocus */
2333 if ((win
= find_window(ev
->window
)) != NULL
)
2340 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2344 mappingnotify(XEvent
*e
)
2346 XMappingEvent
*ev
= &e
->xmapping
;
2348 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2350 XRefreshKeyboardMapping(ev
);
2351 if (ev
->request
== MappingKeyboard
)
2356 maprequest(XEvent
*e
)
2358 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2359 XWindowAttributes wa
;
2361 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2362 e
->xmaprequest
.window
);
2364 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2366 if (wa
.override_redirect
)
2368 manage_window(e
->xmaprequest
.window
);
2374 propertynotify(XEvent
*e
)
2377 XPropertyEvent
*ev
= &e
->xproperty
;
2379 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2382 if (ev
->state
== PropertyDelete
)
2383 return; /* ignore */
2384 win
= find_window(ev
->window
);
2389 case XA_WM_NORMAL_HINTS
:
2392 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2393 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2394 if (win
->sh
.flags
& PMinSize
) {
2395 win
->g
.w
= win
->sh
.min_width
;
2396 win
->g
.h
= win
->sh
.min_height
;
2397 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2399 XMoveResizeWindow(display
, win
->id
,
2400 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2409 unmapnotify(XEvent
*e
)
2411 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2414 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2416 if ((win
= find_window(ev
->window
)) != NULL
)
2418 unmanage_window(win
);
2422 visibilitynotify(XEvent
*e
)
2425 struct swm_region
*r
;
2427 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2428 e
->xvisibility
.window
);
2429 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2430 for (i
= 0; i
< ScreenCount(display
); i
++)
2431 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2432 if (e
->xvisibility
.window
== r
->bar_window
)
2437 xerror_start(Display
*d
, XErrorEvent
*ee
)
2444 xerror(Display
*d
, XErrorEvent
*ee
)
2446 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2454 xerrorxlib
= XSetErrorHandler(xerror_start
);
2456 /* this causes an error if some other window manager is running */
2457 XSelectInput(display
, DefaultRootWindow(display
),
2458 SubstructureRedirectMask
);
2459 XSync(display
, False
);
2463 XSetErrorHandler(xerror
);
2464 XSync(display
, False
);
2473 unsigned char *p
= NULL
;
2474 unsigned long n
, extra
;
2477 astate
= XInternAtom(display
, "WM_STATE", False
);
2478 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
2479 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
2480 if (status
!= Success
)
2489 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
2491 struct swm_region
*r
, *n
;
2492 struct workspace
*ws
= NULL
;
2495 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
2496 s
->idx
, w
, h
, x
, y
);
2498 /* remove any conflicting regions */
2499 n
= TAILQ_FIRST(&s
->rl
);
2502 n
= TAILQ_NEXT(r
, entry
);
2503 if (X(r
) < (x
+ w
) &&
2504 (X(r
) + WIDTH(r
)) > x
&&
2506 (Y(r
) + HEIGHT(r
)) > y
) {
2507 XDestroyWindow(display
, r
->bar_window
);
2508 TAILQ_REMOVE(&s
->rl
, r
, entry
);
2509 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
2513 /* search old regions for one to reuse */
2515 /* size + location match */
2516 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2517 if (X(r
) == x
&& Y(r
) == y
&&
2518 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2522 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2523 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2527 TAILQ_REMOVE(&s
->orl
, r
, entry
);
2528 /* try to use old region's workspace */
2529 if (r
->ws
->r
== NULL
)
2532 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
2533 errx(1, "calloc: failed to allocate memory for screen");
2535 /* if we don't have a workspace already, find one */
2537 for (i
= 0; i
< SWM_WS_MAX
; i
++)
2538 if (s
->ws
[i
].r
== NULL
) {
2545 errx(1, "no free workspaces\n");
2554 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
2561 #ifdef SWM_XRR_HAS_CRTC
2563 XRRScreenResources
*sr
;
2566 #endif /* SWM_XRR_HAS_CRTC */
2567 struct swm_region
*r
;
2570 if (i
>= ScreenCount(display
))
2571 errx(1, "invalid screen");
2573 /* remove any old regions */
2574 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
2576 XDestroyWindow(display
, r
->bar_window
);
2577 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
2578 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
2581 /* map virtual screens onto physical screens */
2582 #ifdef SWM_XRR_HAS_CRTC
2583 if (xrandr_support
) {
2584 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
2586 new_region(&screens
[i
], 0, 0,
2587 DisplayWidth(display
, i
),
2588 DisplayHeight(display
, i
));
2592 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
2593 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
2594 if (ci
->noutput
== 0)
2597 if (ci
!= NULL
&& ci
->mode
== None
)
2598 new_region(&screens
[i
], 0, 0,
2599 DisplayWidth(display
, i
),
2600 DisplayHeight(display
, i
));
2602 new_region(&screens
[i
],
2603 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
2606 XRRFreeCrtcInfo(ci
);
2607 XRRFreeScreenResources(sr
);
2609 #endif /* SWM_XRR_HAS_CRTC */
2611 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
2612 DisplayHeight(display
, i
));
2617 screenchange(XEvent
*e
) {
2618 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
2619 struct swm_region
*r
;
2623 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
2625 if (!XRRUpdateConfiguration(e
))
2628 /* silly event doesn't include the screen index */
2629 for (i
= 0; i
< ScreenCount(display
); i
++)
2630 if (screens
[i
].root
== xe
->root
)
2632 if (i
>= ScreenCount(display
))
2633 errx(1, "screenchange: screen not found\n");
2635 /* brute force for now, just re-enumerate the regions */
2638 /* hide any windows that went away */
2639 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2640 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
2641 XUnmapWindow(display
, win
->id
);
2648 Window d1
, d2
, *wins
= NULL
;
2649 XWindowAttributes wa
;
2652 int errorbase
, major
, minor
;
2653 struct workspace
*ws
;
2657 if ((screens
= calloc(ScreenCount(display
),
2658 sizeof(struct swm_screen
))) == NULL
)
2659 errx(1, "calloc: screens");
2661 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2663 /* initial Xrandr setup */
2664 xrandr_support
= XRRQueryExtension(display
,
2665 &xrandr_eventbase
, &errorbase
);
2667 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
2670 /* map physical screens */
2671 for (i
= 0; i
< ScreenCount(display
); i
++) {
2672 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
2674 TAILQ_INIT(&screens
[i
].rl
);
2675 TAILQ_INIT(&screens
[i
].orl
);
2676 screens
[i
].root
= RootWindow(display
, i
);
2678 /* set default colors */
2679 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
2680 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
2681 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
2682 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
2683 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
2685 /* init all workspaces */
2686 /* XXX these should be dynamically allocated too */
2687 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
2688 ws
= &screens
[i
].ws
[j
];
2693 TAILQ_INIT(&ws
->winlist
);
2695 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
2696 if (layouts
[k
].l_config
!= NULL
)
2697 layouts
[k
].l_config(ws
,
2698 SWM_ARG_ID_STACKINIT
);
2699 ws
->cur_layout
= &layouts
[0];
2701 /* grab existing windows (before we build the bars)*/
2702 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
2708 XRRSelectInput(display
, screens
[i
].root
,
2709 RRScreenChangeNotifyMask
);
2711 /* attach windows to a region */
2712 /* normal windows */
2713 for (j
= 0; j
< no
; j
++) {
2714 XGetWindowAttributes(display
, wins
[j
], &wa
);
2715 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
2716 wa
.override_redirect
||
2717 XGetTransientForHint(display
, wins
[j
], &d1
))
2720 if (wa
.map_state
== IsViewable
||
2721 getstate(wins
[j
]) == NormalState
)
2722 manage_window(wins
[j
]);
2724 /* transient windows */
2725 for (j
= 0; j
< no
; j
++) {
2726 if (!XGetWindowAttributes(display
, wins
[j
], &wa
))
2729 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
2730 (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) ==
2732 manage_window(wins
[j
]);
2742 main(int argc
, char *argv
[])
2745 char conf
[PATH_MAX
], *cfile
= NULL
;
2752 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
2753 SWM_VERSION
, cvstag
);
2754 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2755 warnx("no locale support");
2757 if (!(display
= XOpenDisplay(0)))
2758 errx(1, "can not open display");
2761 errx(1, "other wm running");
2763 astate
= XInternAtom(display
, "WM_STATE", False
);
2765 /* look for local and global conf file */
2766 pwd
= getpwuid(getuid());
2768 errx(1, "invalid user %d", getuid());
2772 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2773 if (stat(conf
, &sb
) != -1) {
2774 if (S_ISREG(sb
.st_mode
))
2777 /* try global conf file */
2778 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2779 if (!stat(conf
, &sb
))
2780 if (S_ISREG(sb
.st_mode
))
2787 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2792 xfd
= ConnectionNumber(display
);
2796 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2798 errx(1, "select failed");
2803 while (XPending(display
)) {
2804 XNextEvent(display
, &e
);
2805 if (e
.type
< LASTEvent
) {
2806 if (handler
[e
.type
])
2807 handler
[e
.type
](&e
);
2809 DNPRINTF(SWM_D_EVENT
,
2810 "win: %lu unknown event: %d\n",
2811 e
.xany
.window
, e
.type
);
2813 switch (e
.type
- xrandr_eventbase
) {
2814 case RRScreenChangeNotify
:
2818 DNPRINTF(SWM_D_EVENT
,
2819 "win: %lu unknown xrandr event: "
2820 "%d\n", e
.xany
.window
, e
.type
);
2827 XCloseDisplay(display
);