]>
code.delx.au - spectrwm/blob - scrotwm.c
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 static const char *cvstag
= "$scrotwm$";
53 #define SWM_VERSION "0.7"
69 #include <sys/types.h>
73 #include <sys/queue.h>
74 #include <sys/param.h>
75 #include <sys/select.h>
77 #include <X11/cursorfont.h>
78 #include <X11/keysym.h>
79 #include <X11/Xatom.h>
81 #include <X11/Xproto.h>
82 #include <X11/Xutil.h>
83 #include <X11/extensions/Xrandr.h>
86 # error XRandR versions less than 1.0 are not supported
91 #define SWM_XRR_HAS_CRTC
95 /* #define SWM_DEBUG */
97 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
98 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
99 #define SWM_D_MISC 0x0001
100 #define SWM_D_EVENT 0x0002
101 #define SWM_D_WS 0x0004
102 #define SWM_D_FOCUS 0x0008
103 #define SWM_D_MOVE 0x0010
104 #define SWM_D_STACK 0x0020
105 #define SWM_D_MOUSE 0x0040
106 #define SWM_D_PROP 0x0080
107 #define SWM_D_CLASS 0x0100
109 u_int32_t swm_debug
= 0
121 #define DPRINTF(x...)
122 #define DNPRINTF(n,x...)
125 #define LENGTH(x) (sizeof x / sizeof x[0])
126 #define MODKEY Mod1Mask
127 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
128 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
129 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
130 #define SWM_PROPLEN (16)
131 #define X(r) (r)->g.x
132 #define Y(r) (r)->g.y
133 #define WIDTH(r) (r)->g.w
134 #define HEIGHT(r) (r)->g.h
137 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
142 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
146 int xrandr_eventbase
;
147 int ignore_enter
= 0;
148 unsigned int numlockmask
= 0;
152 int cycle_visible
= 0;
155 double dialog_ratio
= .6;
157 #define SWM_BAR_MAX (128)
158 char *bar_argv
[] = { NULL
, NULL
};
160 char bar_ext
[SWM_BAR_MAX
];
161 char bar_vertext
[SWM_BAR_MAX
];
163 sig_atomic_t bar_alarm
= 0;
167 int bar_extra_running
= 0;
175 char *bar_fonts
[] = {
176 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
177 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
181 /* terminal + args */
182 char *spawn_term
[] = { "xterm", NULL
};
183 char *spawn_screenshot
[] = { "screenshot.sh", NULL
, NULL
};
184 char *spawn_lock
[] = { "xlock", NULL
};
185 char *spawn_menu
[] = { "dmenu_run", "-fn", NULL
, "-nb", NULL
,
186 "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
188 #define SWM_MENU_FN (2)
189 #define SWM_MENU_NB (4)
190 #define SWM_MENU_NF (6)
191 #define SWM_MENU_SB (8)
192 #define SWM_MENU_SF (10)
194 /* layout manager data */
195 struct swm_geometry
{
205 /* virtual "screens" */
207 TAILQ_ENTRY(swm_region
) entry
;
208 struct swm_geometry g
;
209 struct workspace
*ws
; /* current workspace on this region */
210 struct swm_screen
*s
; /* screen idx */
213 TAILQ_HEAD(swm_region_list
, swm_region
);
216 TAILQ_ENTRY(ws_win
) entry
;
218 struct swm_geometry g
;
223 unsigned long quirks
;
224 struct workspace
*ws
; /* always valid */
225 struct swm_screen
*s
; /* always valid, never changes */
226 XWindowAttributes wa
;
230 TAILQ_HEAD(ws_win_list
, ws_win
);
232 /* layout handlers */
234 void vertical_config(struct workspace
*, int);
235 void vertical_stack(struct workspace
*, struct swm_geometry
*);
236 void horizontal_config(struct workspace
*, int);
237 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
238 void max_stack(struct workspace
*, struct swm_geometry
*);
240 void grabbuttons(struct ws_win
*, int);
243 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
244 void (*l_config
)(struct workspace
*, int);
246 /* stack, configure */
247 { vertical_stack
, vertical_config
},
248 { horizontal_stack
, horizontal_config
},
253 #define SWM_H_SLICE (32)
254 #define SWM_V_SLICE (32)
256 /* define work spaces */
258 int idx
; /* workspace index */
259 int restack
; /* restack on switch */
260 struct layout
*cur_layout
; /* current layout handlers */
261 struct ws_win
*focus
; /* may be NULL */
262 struct swm_region
*r
; /* may be NULL */
263 struct ws_win_list winlist
; /* list of windows in ws */
267 int horizontal_msize
;
274 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
275 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
277 /* physical screen mapping */
278 #define SWM_WS_MAX (10) /* XXX Too small? */
280 int idx
; /* screen index */
281 struct swm_region_list rl
; /* list of regions on this screen */
284 struct workspace ws
[SWM_WS_MAX
];
290 } c
[SWM_S_COLOR_MAX
];
292 struct swm_screen
*screens
;
295 struct ws_win
*cur_focus
= NULL
;
297 /* args to functions */
300 #define SWM_ARG_ID_FOCUSNEXT (0)
301 #define SWM_ARG_ID_FOCUSPREV (1)
302 #define SWM_ARG_ID_FOCUSMAIN (2)
303 #define SWM_ARG_ID_SWAPNEXT (3)
304 #define SWM_ARG_ID_SWAPPREV (4)
305 #define SWM_ARG_ID_SWAPMAIN (5)
306 #define SWM_ARG_ID_MASTERSHRINK (6)
307 #define SWM_ARG_ID_MASTERGROW (7)
308 #define SWM_ARG_ID_MASTERADD (8)
309 #define SWM_ARG_ID_MASTERDEL (9)
310 #define SWM_ARG_ID_STACKRESET (10)
311 #define SWM_ARG_ID_STACKINIT (11)
312 #define SWM_ARG_ID_CYCLEWS_UP (12)
313 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
314 #define SWM_ARG_ID_CYCLESC_UP (14)
315 #define SWM_ARG_ID_CYCLESC_DOWN (15)
316 #define SWM_ARG_ID_SS_ALL (0)
317 #define SWM_ARG_ID_SS_WINDOW (1)
318 #define SWM_ARG_ID_DONTCENTER (0)
319 #define SWM_ARG_ID_CENTER (1)
328 #define SWM_Q_FLOAT (1<<0)
329 #define SWM_Q_TRANSSZ (1<<1)
331 { "MPlayer", "xv", SWM_Q_FLOAT
},
332 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
},
333 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT
},
334 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
},
339 void expose(XEvent
*);
340 void keypress(XEvent
*);
341 void buttonpress(XEvent
*);
342 void configurerequest(XEvent
*);
343 void configurenotify(XEvent
*);
344 void destroynotify(XEvent
*);
345 void enternotify(XEvent
*);
346 void focusin(XEvent
*);
347 void mappingnotify(XEvent
*);
348 void maprequest(XEvent
*);
349 void propertynotify(XEvent
*);
350 void unmapnotify(XEvent
*);
351 void visibilitynotify(XEvent
*);
353 void (*handler
[LASTEvent
])(XEvent
*) = {
355 [KeyPress
] = keypress
,
356 [ButtonPress
] = buttonpress
,
357 [ConfigureRequest
] = configurerequest
,
358 [ConfigureNotify
] = configurenotify
,
359 [DestroyNotify
] = destroynotify
,
360 [EnterNotify
] = enternotify
,
362 [MappingNotify
] = mappingnotify
,
363 [MapRequest
] = maprequest
,
364 [PropertyNotify
] = propertynotify
,
365 [UnmapNotify
] = unmapnotify
,
366 [VisibilityNotify
] = visibilitynotify
,
370 name_to_color(char *colorname
)
374 XColor screen_def
, exact_def
;
375 unsigned long result
= 0;
376 char cname
[32] = "#";
378 cmap
= DefaultColormap(display
, screens
[0].idx
);
379 status
= XAllocNamedColor(display
, cmap
, colorname
,
380 &screen_def
, &exact_def
);
382 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
383 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
387 result
= screen_def
.pixel
;
389 fprintf(stderr
, "color '%s' not found.\n", colorname
);
395 setscreencolor(char *val
, int i
, int c
)
397 if (i
> 0 && i
<= ScreenCount(display
)) {
398 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
399 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
401 } else if (i
== -1) {
402 for (i
= 0; i
< ScreenCount(display
); i
++)
403 screens
[i
].c
[c
].color
= name_to_color(val
);
404 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
407 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
408 i
, ScreenCount(display
));
411 void new_region(struct swm_screen
*, struct workspace
*,
415 custom_region(char *val
)
417 unsigned int sidx
, x
, y
, w
, h
;
419 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
420 errx(1, "invalid custom region, "
421 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
422 if (sidx
< 1 || sidx
> ScreenCount(display
))
423 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
424 sidx
, ScreenCount(display
));
428 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
430 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
431 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
432 w
+ x
> DisplayWidth(display
, sidx
) ||
433 h
+ y
> DisplayHeight(display
, sidx
))
434 errx(1, "region %ux%u+%u+%u not within screen boundaries "
435 "(%ux%u)\n", w
, h
, x
, y
,
436 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
438 new_region(&screens
[sidx
], NULL
, x
, y
, w
, h
);
442 varmatch(char *var
, char *name
, int *index
)
447 i
= strncmp(var
, name
, 255);
454 p
= var
+ strlen(name
);
457 bzero(buf
, sizeof buf
);
459 while (isdigit(*p
) && i
< sizeof buf
)
461 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
463 *index
= strtonum(buf
, 0, 99, NULL
);
467 /* conf file stuff */
468 #define SWM_CONF_WS "\n= \t"
469 #define SWM_CONF_FILE "scrotwm.conf"
471 conf_load(char *filename
)
474 char *line
, *cp
, *var
, *val
;
475 size_t len
, lineno
= 0;
478 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
480 if (filename
== NULL
)
483 if ((config
= fopen(filename
, "r")) == NULL
)
486 for (sc
= ScreenCount(display
);;) {
487 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
491 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
497 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
499 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
500 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
503 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
506 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
507 bar_enabled
= atoi(val
);
508 else if (!varmatch(var
, "bar_border", &i
))
509 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
510 else if (!varmatch(var
, "bar_color", &i
))
511 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
512 else if (!varmatch(var
, "bar_font_color", &i
))
513 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
514 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
515 asprintf(&bar_fonts
[0], "%s", val
);
516 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
517 asprintf(&bar_argv
[0], "%s", val
);
518 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
519 bar_delay
= atoi(val
);
525 if (!varmatch(var
, "color_focus", &i
))
526 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
527 else if (!varmatch(var
, "color_unfocus", &i
))
528 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
529 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
530 cycle_visible
= atoi(val
);
531 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
532 cycle_visible
= atoi(val
);
538 if (!strncmp(var
, "dialog_ratio",
539 strlen("dialog_ratio"))) {
540 dialog_ratio
= atof(val
);
541 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
548 if (!strncmp(var
, "region", strlen("region")))
555 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
556 asprintf(&spawn_term
[0], "%s", val
);
557 if (!strncmp(var
, "screenshot_enabled",
558 strlen("screenshot_enabled")))
559 ss_enabled
= atoi(val
);
560 if (!strncmp(var
, "screenshot_app",
561 strlen("screenshot_app")))
562 asprintf(&spawn_screenshot
[0], "%s", val
);
574 errx(1, "invalid conf file entry: %s=%s", var
, val
);
578 socket_setnonblock(int fd
)
582 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
583 err(1, "fcntl F_GETFL");
585 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
586 err(1, "fcntl F_SETFL");
590 bar_print(struct swm_region
*r
, char *s
)
592 XClearWindow(display
, r
->bar_window
);
593 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
594 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
603 bzero(bar_pipe
, sizeof bar_pipe
);
606 kill(bar_pid
, SIGTERM
);
609 strlcpy(bar_ext
, "", sizeof bar_ext
);
618 struct swm_region
*r
;
622 char loc
[SWM_BAR_MAX
];
625 if (bar_enabled
== 0)
628 if (bar_extra
&& bar_extra_running
) {
629 /* ignore short reads; it'll correct itself */
630 while ((b
= fgetln(stdin
, &len
)) != NULL
)
631 if (b
&& b
[len
- 1] == '\n') {
633 strlcpy(bar_ext
, b
, sizeof bar_ext
);
635 if (b
== NULL
&& errno
!= EAGAIN
) {
636 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
637 errno
, strerror(errno
));
641 strlcpy(bar_ext
, "", sizeof bar_ext
);
644 localtime_r(&tmt
, &tm
);
645 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
646 for (i
= 0; i
< ScreenCount(display
); i
++) {
648 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
649 snprintf(loc
, sizeof loc
, "%s %d:%d %s %s",
650 s
, x
++, r
->ws
->idx
+ 1, bar_ext
, bar_vertext
);
654 XSync(display
, False
);
665 bar_toggle(struct swm_region
*r
, union arg
*args
)
667 struct swm_region
*tmpr
;
668 int i
, j
, sc
= ScreenCount(display
);
670 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
673 for (i
= 0; i
< sc
; i
++)
674 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
675 XUnmapWindow(display
, tmpr
->bar_window
);
677 for (i
= 0; i
< sc
; i
++)
678 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
679 XMapRaised(display
, tmpr
->bar_window
);
681 bar_enabled
= !bar_enabled
;
682 XSync(display
, False
);
683 for (i
= 0; i
< sc
; i
++)
684 for (j
= 0; j
< SWM_WS_MAX
; j
++)
685 screens
[i
].ws
[j
].restack
= 1;
688 /* must be after stack */
689 for (i
= 0; i
< sc
; i
++)
690 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
697 XSetWindowAttributes wa
;
698 struct swm_region
*r
;
701 /* do this here because the conf file is in memory */
702 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
703 /* launch external status app */
704 bar_extra_running
= 1;
705 if (pipe(bar_pipe
) == -1)
706 err(1, "pipe error");
707 socket_setnonblock(bar_pipe
[0]);
708 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
709 if (dup2(bar_pipe
[0], 0) == -1)
711 if (dup2(bar_pipe
[1], 1) == -1)
713 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
714 err(1, "could not disable SIGPIPE");
715 switch (bar_pid
= fork()) {
717 err(1, "cannot fork");
721 execvp(bar_argv
[0], bar_argv
);
722 err(1, "%s external app failed", bar_argv
[0]);
724 default: /* parent */
730 bzero(&wa
, sizeof wa
);
731 for (i
= 0; i
< ScreenCount(display
); i
++)
732 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
734 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
735 wa
.background_pixel
=
736 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
737 XChangeWindowAttributes(display
, r
->bar_window
,
738 CWBackPixel
| CWBorderPixel
, &wa
);
744 bar_setup(struct swm_region
*r
)
748 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
749 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
755 if (bar_fonts
[i
] == NULL
)
756 errx(1, "couldn't load font");
757 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
759 r
->bar_window
= XCreateSimpleWindow(display
,
760 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
761 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
762 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
763 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
764 XSetFont(display
, bar_gc
, bar_fs
->fid
);
765 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
767 XMapRaised(display
, r
->bar_window
);
768 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
770 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
771 err(1, "could not install bar_signal");
776 version(struct swm_region
*r
, union arg
*args
)
778 bar_version
= !bar_version
;
780 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
782 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
787 config_win(struct ws_win
*win
)
791 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
792 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
793 ce
.type
= ConfigureNotify
;
794 ce
.display
= display
;
800 ce
.height
= win
->g
.h
;
801 ce
.border_width
= 1; /* XXX store this! */
803 ce
.override_redirect
= False
;
804 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
808 count_win(struct workspace
*ws
, int count_transient
)
813 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
814 if (count_transient
== 0 && win
->floating
)
816 if (count_transient
== 0 && win
->transient
)
820 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
826 quit(struct swm_region
*r
, union arg
*args
)
828 DNPRINTF(SWM_D_MISC
, "quit\n");
838 for (i
= 0; i
< ScreenCount(display
); i
++)
839 for (j
= 0; j
< SWM_WS_MAX
; j
++)
840 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
841 XUnmapWindow(display
, win
->id
);
845 restart(struct swm_region
*r
, union arg
*args
)
847 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
849 /* disable alarm because the following code may not be interrupted */
851 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
852 errx(1, "can't disable alarm");
857 XCloseDisplay(display
);
858 execvp(start_argv
[0], start_argv
);
859 fprintf(stderr
, "execvp failed\n");
865 root_to_region(Window root
)
867 struct swm_region
*r
;
872 for (i
= 0; i
< ScreenCount(display
); i
++)
873 if (screens
[i
].root
== root
)
876 if (XQueryPointer(display
, screens
[i
].root
,
877 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) == False
) {
878 /* if we can't query the pointer, grab the first region */
879 r
= TAILQ_FIRST(&screens
[i
].rl
);
881 /* otherwise, choose a region based on pointer location */
882 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
883 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
884 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
889 r
= TAILQ_FIRST(&screens
[i
].rl
);
895 find_window(Window id
)
900 for (i
= 0; i
< ScreenCount(display
); i
++)
901 for (j
= 0; j
< SWM_WS_MAX
; j
++)
902 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
909 spawn(struct swm_region
*r
, union arg
*args
)
914 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
916 * The double-fork construct avoids zombie processes and keeps the code
917 * clean from stupid signal handlers.
922 close(ConnectionNumber(display
));
923 setenv("LD_PRELOAD", SWM_LIB
, 1);
924 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
925 setenv("_SWM_WS", ret
, 1);
928 if (asprintf(&ret
, "%d", getpid())) {
929 setenv("_SWM_PID", ret
, 1);
933 /* kill stdin, mplayer, ssh-add etc. need that */
934 si
= open("/dev/null", O_RDONLY
, 0);
936 err(1, "open /dev/null");
937 if (dup2(si
, 0) == -1)
938 err(1, "dup2 /dev/null");
939 execvp(args
->argv
[0], args
->argv
);
940 fprintf(stderr
, "execvp failed\n");
949 spawnmenu(struct swm_region
*r
, union arg
*args
)
951 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
953 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
954 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
955 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
956 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
957 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
968 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
970 for (i
= 0; i
< ScreenCount(display
); i
++)
971 for (j
= 0; j
< SWM_WS_MAX
; j
++)
972 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
973 if (win
->ws
->r
== NULL
)
976 XSetWindowBorder(display
, win
->id
,
977 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
979 win
->ws
->focus
= NULL
;
985 focus_win(struct ws_win
*win
)
987 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
993 win
->ws
->focus
= win
;
994 if (win
->ws
->r
!= NULL
) {
996 if (!win
->got_focus
) {
997 XSetWindowBorder(display
, win
->id
,
998 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1002 XSetInputFocus(display
, win
->id
,
1003 RevertToPointerRoot
, CurrentTime
);
1008 switchws(struct swm_region
*r
, union arg
*args
)
1010 int wsid
= args
->id
;
1011 struct swm_region
*this_r
, *other_r
;
1013 struct workspace
*new_ws
, *old_ws
;
1016 old_ws
= this_r
->ws
;
1017 new_ws
= &this_r
->s
->ws
[wsid
];
1019 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1020 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1023 if (new_ws
== old_ws
)
1026 other_r
= new_ws
->r
;
1028 /* if the other workspace is hidden, switch windows */
1029 /* map new window first to prevent ugly blinking */
1031 old_ws
->restack
= 1;
1033 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1034 XMapRaised(display
, win
->id
);
1036 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1037 XUnmapWindow(display
, win
->id
);
1039 other_r
->ws
= old_ws
;
1040 old_ws
->r
= other_r
;
1042 this_r
->ws
= new_ws
;
1048 focus_win(new_ws
->focus
);
1054 cyclews(struct swm_region
*r
, union arg
*args
)
1057 struct swm_screen
*s
= r
->s
;
1059 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1060 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1061 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1066 case SWM_ARG_ID_CYCLEWS_UP
:
1067 if (a
.id
< SWM_WS_MAX
- 1)
1072 case SWM_ARG_ID_CYCLEWS_DOWN
:
1076 a
.id
= SWM_WS_MAX
- 1;
1082 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1084 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1088 } while (a
.id
!= r
->ws
->idx
);
1092 cyclescr(struct swm_region
*r
, union arg
*args
)
1094 struct swm_region
*rr
;
1099 case SWM_ARG_ID_CYCLESC_UP
:
1100 rr
= TAILQ_NEXT(r
, entry
);
1102 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1104 case SWM_ARG_ID_CYCLESC_DOWN
:
1105 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1107 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1113 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1114 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1115 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1119 swapwin(struct swm_region
*r
, union arg
*args
)
1121 struct ws_win
*target
;
1122 struct ws_win_list
*wl
;
1125 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1126 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1127 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1128 if (cur_focus
== NULL
)
1131 wl
= &cur_focus
->ws
->winlist
;
1134 case SWM_ARG_ID_SWAPPREV
:
1135 target
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1136 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1138 TAILQ_INSERT_TAIL(wl
, cur_focus
, entry
);
1140 TAILQ_INSERT_BEFORE(target
, cur_focus
, entry
);
1142 case SWM_ARG_ID_SWAPNEXT
:
1143 target
= TAILQ_NEXT(cur_focus
, entry
);
1144 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1146 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1148 TAILQ_INSERT_AFTER(wl
, target
, cur_focus
, entry
);
1150 case SWM_ARG_ID_SWAPMAIN
:
1151 target
= TAILQ_FIRST(wl
);
1152 if (target
== cur_focus
)
1154 TAILQ_REMOVE(wl
, target
, entry
);
1155 TAILQ_INSERT_BEFORE(cur_focus
, target
, entry
);
1156 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1157 TAILQ_INSERT_HEAD(wl
, cur_focus
, entry
);
1160 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1169 focus(struct swm_region
*r
, union arg
*args
)
1171 struct ws_win
*winfocus
, *winlostfocus
;
1172 struct ws_win_list
*wl
;
1174 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1175 if (cur_focus
== NULL
)
1178 wl
= &cur_focus
->ws
->winlist
;
1180 winlostfocus
= cur_focus
;
1183 case SWM_ARG_ID_FOCUSPREV
:
1184 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1185 if (winfocus
== NULL
)
1186 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1189 case SWM_ARG_ID_FOCUSNEXT
:
1190 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1191 if (winfocus
== NULL
)
1192 winfocus
= TAILQ_FIRST(wl
);
1195 case SWM_ARG_ID_FOCUSMAIN
:
1196 winfocus
= TAILQ_FIRST(wl
);
1203 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1206 XMapRaised(display
, winfocus
->id
);
1207 focus_win(winfocus
);
1208 XSync(display
, False
);
1212 cycle_layout(struct swm_region
*r
, union arg
*args
)
1214 struct workspace
*ws
= r
->ws
;
1216 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1219 if (ws
->cur_layout
->l_stack
== NULL
)
1220 ws
->cur_layout
= &layouts
[0];
1226 stack_config(struct swm_region
*r
, union arg
*args
)
1228 struct workspace
*ws
= r
->ws
;
1230 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1233 if (ws
->cur_layout
->l_config
!= NULL
)
1234 ws
->cur_layout
->l_config(ws
, args
->id
);
1236 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1242 struct swm_geometry g
;
1243 struct swm_region
*r
;
1246 DNPRINTF(SWM_D_STACK
, "stack\n");
1248 for (i
= 0; i
< ScreenCount(display
); i
++) {
1250 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1251 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1252 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1254 /* start with screen geometry, adjust for bar */
1264 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1267 XSync(display
, False
);
1271 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1276 bzero(&wc
, sizeof wc
);
1277 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1278 wc
.border_width
= 1;
1279 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1280 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1281 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1283 wc
.width
= win
->g
.w
;
1284 wc
.height
= win
->g
.h
;
1289 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1290 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1293 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1294 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1296 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1299 #define SWAPXY(g) do { \
1301 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1302 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1305 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1308 struct swm_geometry win_g
, r_g
= *g
;
1309 struct ws_win
*win
, *winfocus
;
1310 int i
, j
, w_inc
, h_inc
, w_base
, h_base
;
1311 int hrh
, extra
, h_slice
, last_h
= 0;
1312 int split
, colno
, winno
, mwin
, msize
, mscale
;
1313 int remain
, missing
, v_slice
;;
1316 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1317 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1319 if ((winno
= count_win(ws
, 0)) == 0)
1322 if (ws
->focus
== NULL
)
1323 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1324 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1326 win
= TAILQ_FIRST(&ws
->winlist
);
1328 w_inc
= win
->sh
.width_inc
;
1329 w_base
= win
->sh
.base_width
;
1330 mwin
= ws
->l_state
.horizontal_mwin
;
1331 mscale
= ws
->l_state
.horizontal_msize
;
1334 w_inc
= win
->sh
.height_inc
;
1335 w_base
= win
->sh
.base_height
;
1336 mwin
= ws
->l_state
.vertical_mwin
;
1337 mscale
= ws
->l_state
.vertical_msize
;
1341 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1342 if (mwin
&& winno
> mwin
) {
1343 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1347 msize
= v_slice
* mscale
;
1349 if (w_inc
> 1 && w_inc
< v_slice
) {
1350 /* adjust for window's requested size increment */
1351 remain
= (win_g
.w
- w_base
) % w_inc
;
1352 missing
= w_inc
- remain
;
1354 if (missing
<= extra
|| j
== 0) {
1365 win_g
.x
+= r_g
.w
- msize
;
1370 hrh
= r_g
.h
/ colno
;
1371 extra
= r_g
.h
- (colno
* hrh
);
1374 /* stack all the tiled windows */
1376 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1377 if (win
->transient
!= 0 || win
->floating
!= 0)
1380 if (split
&& i
== split
) {
1381 colno
= winno
- split
;
1382 hrh
= (r_g
.h
/ colno
);
1383 extra
= r_g
.h
- (colno
* hrh
);
1387 win_g
.x
+= msize
+ 2;
1388 win_g
.w
= r_g
.w
- (msize
+ 2);
1393 h_inc
= win
->sh
.width_inc
;
1394 h_base
= win
->sh
.base_width
;
1396 h_inc
= win
->sh
.height_inc
;
1397 h_base
= win
->sh
.base_height
;
1399 if (j
== colno
- 1) {
1400 win_g
.h
= hrh
+ extra
;
1401 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1402 /* adjust for window's requested size increment */
1403 remain
= (win_g
.h
- h_base
) % h_inc
;
1404 missing
= h_inc
- remain
;
1406 if (missing
<= extra
|| j
== 0) {
1418 win_g
.y
+= last_h
+ 2;
1420 bzero(&wc
, sizeof wc
);
1421 wc
.border_width
= 1;
1423 win
->g
.x
= wc
.x
= win_g
.y
;
1424 win
->g
.y
= wc
.y
= win_g
.x
;
1425 win
->g
.w
= wc
.width
= win_g
.h
;
1426 win
->g
.h
= wc
.height
= win_g
.w
;
1428 win
->g
.x
= wc
.x
= win_g
.x
;
1429 win
->g
.y
= wc
.y
= win_g
.y
;
1430 win
->g
.w
= wc
.width
= win_g
.w
;
1431 win
->g
.h
= wc
.height
= win_g
.h
;
1433 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1434 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1435 XMapRaised(display
, win
->id
);
1442 /* now, stack all the floaters and transients */
1443 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1444 if (win
->transient
== 0 && win
->floating
== 0)
1447 stack_floater(win
, ws
->r
);
1448 XMapRaised(display
, win
->id
);
1452 focus_win(winfocus
); /* has to be done outside of the loop */
1456 vertical_config(struct workspace
*ws
, int id
)
1458 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1461 case SWM_ARG_ID_STACKRESET
:
1462 case SWM_ARG_ID_STACKINIT
:
1463 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1464 ws
->l_state
.vertical_mwin
= 1;
1466 case SWM_ARG_ID_MASTERSHRINK
:
1467 if (ws
->l_state
.vertical_msize
> 1)
1468 ws
->l_state
.vertical_msize
--;
1470 case SWM_ARG_ID_MASTERGROW
:
1471 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1472 ws
->l_state
.vertical_msize
++;
1474 case SWM_ARG_ID_MASTERADD
:
1475 ws
->l_state
.vertical_mwin
++;
1477 case SWM_ARG_ID_MASTERDEL
:
1478 if (ws
->l_state
.vertical_mwin
> 0)
1479 ws
->l_state
.vertical_mwin
--;
1487 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1489 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1491 stack_master(ws
, g
, 0, 0);
1495 horizontal_config(struct workspace
*ws
, int id
)
1497 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1500 case SWM_ARG_ID_STACKRESET
:
1501 case SWM_ARG_ID_STACKINIT
:
1502 ws
->l_state
.horizontal_mwin
= 1;
1503 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1505 case SWM_ARG_ID_MASTERSHRINK
:
1506 if (ws
->l_state
.horizontal_msize
> 1)
1507 ws
->l_state
.horizontal_msize
--;
1509 case SWM_ARG_ID_MASTERGROW
:
1510 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1511 ws
->l_state
.horizontal_msize
++;
1513 case SWM_ARG_ID_MASTERADD
:
1514 ws
->l_state
.horizontal_mwin
++;
1516 case SWM_ARG_ID_MASTERDEL
:
1517 if (ws
->l_state
.horizontal_mwin
> 0)
1518 ws
->l_state
.horizontal_mwin
--;
1526 horizontal_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
, 1, 0);
1533 /* fullscreen view */
1535 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1537 struct swm_geometry gg
= *g
;
1538 struct ws_win
*win
, *winfocus
;
1541 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1543 if (count_win(ws
, 0) == 0)
1546 if (ws
->focus
== NULL
)
1547 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1548 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1550 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1551 if (win
->transient
!= 0 || win
->floating
!= 0) {
1552 if (win
== ws
->focus
) {
1554 stack_floater(win
, ws
->r
);
1555 XMapRaised(display
, win
->id
);
1557 XUnmapWindow(display
, win
->id
);
1559 bzero(&wc
, sizeof wc
);
1560 wc
.border_width
= 1;
1561 win
->g
.x
= wc
.x
= gg
.x
;
1562 win
->g
.y
= wc
.y
= gg
.y
;
1563 win
->g
.w
= wc
.width
= gg
.w
;
1564 win
->g
.h
= wc
.height
= gg
.h
;
1565 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1566 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1568 if (win
== ws
->focus
) {
1569 XMapRaised(display
, win
->id
);
1571 XUnmapWindow(display
, win
->id
);
1576 focus_win(winfocus
); /* has to be done outside of the loop */
1580 send_to_ws(struct swm_region
*r
, union arg
*args
)
1582 int wsid
= args
->id
;
1583 struct ws_win
*win
= cur_focus
;
1584 struct workspace
*ws
, *nws
;
1585 Atom ws_idx_atom
= 0;
1586 unsigned char ws_idx_str
[SWM_PROPLEN
];
1591 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1594 nws
= &win
->s
->ws
[wsid
];
1596 XUnmapWindow(display
, win
->id
);
1598 /* find a window to focus */
1599 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1600 if (ws
->focus
== NULL
)
1601 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1602 if (ws
->focus
== win
)
1605 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1607 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1610 /* Try to update the window's workspace property */
1611 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1613 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1614 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1616 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1617 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1620 if (count_win(nws
, 1) == 1)
1629 wkill(struct swm_region
*r
, union arg
*args
)
1631 DNPRINTF(SWM_D_MISC
, "wkill\n");
1632 if(r
->ws
->focus
!= NULL
)
1633 XKillClient(display
, r
->ws
->focus
->id
);
1637 screenshot(struct swm_region
*r
, union arg
*args
)
1641 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1643 if (ss_enabled
== 0)
1647 case SWM_ARG_ID_SS_ALL
:
1648 spawn_screenshot
[1] = "full";
1650 case SWM_ARG_ID_SS_WINDOW
:
1651 spawn_screenshot
[1] = "window";
1656 a
.argv
= spawn_screenshot
;
1661 floating_toggle(struct swm_region
*r
, union arg
*args
)
1663 struct ws_win
*win
= cur_focus
;
1668 win
->floating
= !win
->floating
;
1674 /* key definitions */
1678 void (*func
)(struct swm_region
*r
, union arg
*);
1681 /* modifier key function argument */
1682 { MODKEY
, XK_space
, cycle_layout
, {0} },
1683 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1684 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1685 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1686 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1687 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1688 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1689 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1690 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1691 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1692 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1693 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1694 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1695 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1696 { MODKEY
, XK_q
, restart
, {0} },
1697 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1698 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1699 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1700 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1701 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1702 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1703 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1704 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1705 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1706 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1707 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1708 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1709 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1710 { MODKEY
| ShiftMask
, XK_Right
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1711 { MODKEY
| ShiftMask
, XK_Left
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1712 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1713 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1714 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1715 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1716 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1717 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1718 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1719 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1720 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1721 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1722 { MODKEY
, XK_b
, bar_toggle
, {0} },
1723 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1724 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1725 { MODKEY
| ShiftMask
, XK_x
, wkill
, {0} },
1726 { MODKEY
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
1727 { MODKEY
| ShiftMask
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
1728 { MODKEY
, XK_t
, floating_toggle
,{0} },
1729 { MODKEY
| ShiftMask
, XK_v
, version
, {0} },
1730 { MODKEY
| ShiftMask
, XK_Delete
, spawn
, {.argv
= spawn_lock
} },
1734 resize_window(struct ws_win
*win
, int center
)
1738 struct swm_region
*r
;
1740 r
= root_to_region(win
->wa
.root
);
1741 bzero(&wc
, sizeof wc
);
1742 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1743 wc
.border_width
= 1;
1744 wc
.width
= win
->g
.w
;
1745 wc
.height
= win
->g
.h
;
1746 if (center
== SWM_ARG_ID_CENTER
) {
1747 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1748 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1752 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1753 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1755 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1760 resize(struct ws_win
*win
, union arg
*args
)
1765 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1766 win
->id
, win
->floating
, win
->transient
);
1768 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1771 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1772 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1774 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1776 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1777 SubstructureRedirectMask
, &ev
);
1779 case ConfigureRequest
:
1782 handler
[ev
.type
](&ev
);
1785 if (ev
.xmotion
.x
< 0)
1787 if (ev
.xmotion
.y
< 0)
1789 win
->g
.w
= ev
.xmotion
.x
;
1790 win
->g
.h
= ev
.xmotion
.y
;
1792 /* not free, don't sync more than 60 times / second */
1793 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1794 time
= ev
.xmotion
.time
;
1795 XSync(display
, False
);
1796 resize_window(win
, args
->id
);
1800 } while (ev
.type
!= ButtonRelease
);
1802 XSync(display
, False
);
1803 resize_window(win
, args
->id
);
1805 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1807 XUngrabPointer(display
, CurrentTime
);
1810 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1814 move_window(struct ws_win
*win
)
1818 struct swm_region
*r
;
1820 r
= root_to_region(win
->wa
.root
);
1821 bzero(&wc
, sizeof wc
);
1826 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
1827 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1829 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1834 move(struct ws_win
*win
, union arg
*args
)
1840 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
1841 win
->id
, win
->floating
, win
->transient
);
1843 if (win
->floating
== 0) {
1849 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1850 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1852 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1854 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1855 SubstructureRedirectMask
, &ev
);
1857 case ConfigureRequest
:
1860 handler
[ev
.type
](&ev
);
1863 win
->g
.x
= ev
.xmotion
.x_root
;
1864 win
->g
.y
= ev
.xmotion
.y_root
;
1866 /* not free, don't sync more than 60 times / second */
1867 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1868 time
= ev
.xmotion
.time
;
1869 XSync(display
, False
);
1874 } while (ev
.type
!= ButtonRelease
);
1876 XSync(display
, False
);
1879 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1880 XUngrabPointer(display
, CurrentTime
);
1885 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1889 enum { client_click
, root_click
};
1891 unsigned int action
;
1893 unsigned int button
;
1894 void (*func
)(struct ws_win
*, union arg
*);
1897 /* action key mouse button func args */
1898 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
1899 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
1900 { client_click
, MODKEY
, Button1
, move
, {0} },
1904 updatenumlockmask(void)
1907 XModifierKeymap
*modmap
;
1909 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1911 modmap
= XGetModifierMapping(display
);
1912 for (i
= 0; i
< 8; i
++)
1913 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1914 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1915 == XKeysymToKeycode(display
, XK_Num_Lock
))
1916 numlockmask
= (1 << i
);
1918 XFreeModifiermap(modmap
);
1924 unsigned int i
, j
, k
;
1926 unsigned int modifiers
[] =
1927 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1929 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1930 updatenumlockmask();
1932 for (k
= 0; k
< ScreenCount(display
); k
++) {
1933 if (TAILQ_EMPTY(&screens
[k
].rl
))
1935 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
1936 for (i
= 0; i
< LENGTH(keys
); i
++) {
1937 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1938 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1939 XGrabKey(display
, code
,
1940 keys
[i
].mod
| modifiers
[j
],
1941 screens
[k
].root
, True
,
1942 GrabModeAsync
, GrabModeAsync
);
1948 grabbuttons(struct ws_win
*win
, int focused
)
1951 unsigned int modifiers
[] =
1952 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1954 updatenumlockmask();
1955 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
1957 for (i
= 0; i
< LENGTH(buttons
); i
++)
1958 if (buttons
[i
].action
== client_click
)
1959 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1960 XGrabButton(display
, buttons
[i
].button
,
1961 buttons
[i
].mask
| modifiers
[j
],
1962 win
->id
, False
, BUTTONMASK
,
1963 GrabModeAsync
, GrabModeSync
, None
,
1966 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
1967 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1973 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1981 XKeyEvent
*ev
= &e
->xkey
;
1983 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1985 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1986 for (i
= 0; i
< LENGTH(keys
); i
++)
1987 if (keysym
== keys
[i
].keysym
1988 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1990 keys
[i
].func(root_to_region(ev
->root
),
1995 buttonpress(XEvent
*e
)
1997 XButtonPressedEvent
*ev
= &e
->xbutton
;
2002 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2004 action
= root_click
;
2005 if ((win
= find_window(ev
->window
)) == NULL
)
2009 action
= client_click
;
2012 for (i
= 0; i
< LENGTH(buttons
); i
++)
2013 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2014 buttons
[i
].button
== ev
->button
&&
2015 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2016 buttons
[i
].func(win
, &buttons
[i
].args
);
2020 set_win_state(struct ws_win
*win
, long state
)
2022 long data
[] = {state
, None
};
2024 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2026 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2027 (unsigned char *)data
, 2);
2031 manage_window(Window id
)
2034 struct workspace
*ws
;
2036 int format
, i
, ws_idx
;
2037 unsigned long nitems
, bytes
;
2038 Atom ws_idx_atom
= 0, type
;
2039 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2040 struct swm_region
*r
;
2044 if ((win
= find_window(id
)) != NULL
)
2045 return (win
); /* already being managed */
2047 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2048 errx(1, "calloc: failed to allocate memory for new window");
2050 /* Get all the window data in one shot */
2051 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2053 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2054 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2055 XGetWindowAttributes(display
, id
, &win
->wa
);
2056 XGetTransientForHint(display
, id
, &trans
);
2057 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2059 win
->transient
= trans
;
2060 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2061 (unsigned)win
->id
, win
->transient
);
2065 * Figure out where to put the window. If it was previously assigned to
2066 * a workspace (either by spawn() or manually moving), and isn't
2067 * transient, * put it in the same workspace
2069 r
= root_to_region(win
->wa
.root
);
2070 if (prop
&& win
->transient
== 0) {
2071 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2072 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2074 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2077 ws
= &r
->s
->ws
[ws_idx
];
2081 /* set up the window layout */
2084 win
->s
= r
->s
; /* this never changes */
2085 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2087 win
->g
.w
= win
->wa
.width
;
2088 win
->g
.h
= win
->wa
.height
;
2089 win
->g
.x
= win
->wa
.x
;
2090 win
->g
.y
= win
->wa
.y
;
2092 /* Set window properties so we can remember this after reincarnation */
2093 if (ws_idx_atom
&& prop
== NULL
&&
2094 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2095 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2097 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2098 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2102 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2103 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2104 win
->ch
.res_class
, win
->ch
.res_name
);
2105 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2106 quirks
[i
].quirk
!= 0; i
++){
2107 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2108 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2109 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2110 win
->ch
.res_class
, win
->ch
.res_name
);
2111 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2113 win
->quirks
= quirks
[i
].quirk
;
2118 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2119 PropertyChangeMask
| StructureNotifyMask
);
2121 set_win_state(win
, NormalState
);
2123 /* make new win focused */
2130 unmanage_window(struct ws_win
*win
)
2132 struct workspace
*ws
;
2137 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
2143 /* find a window to focus */
2144 if (ws
->focus
== win
)
2145 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2146 if (ws
->focus
== NULL
)
2147 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2148 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2152 focus_win(ws
->focus
);
2154 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
2155 set_win_state(win
, WithdrawnState
);
2156 if (win
->ch
.res_class
)
2157 XFree(win
->ch
.res_class
);
2158 if (win
->ch
.res_name
)
2159 XFree(win
->ch
.res_name
);
2164 configurerequest(XEvent
*e
)
2166 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2171 if ((win
= find_window(ev
->window
)) == NULL
)
2175 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2177 bzero(&wc
, sizeof wc
);
2180 wc
.width
= ev
->width
;
2181 wc
.height
= ev
->height
;
2182 wc
.border_width
= ev
->border_width
;
2183 wc
.sibling
= ev
->above
;
2184 wc
.stack_mode
= ev
->detail
;
2185 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2187 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2189 if (win
->floating
) {
2190 if (ev
->value_mask
& CWX
)
2192 if (ev
->value_mask
& CWY
)
2194 if (ev
->value_mask
& CWWidth
)
2195 win
->g
.w
= ev
->width
;
2196 if (ev
->value_mask
& CWHeight
)
2197 win
->g
.h
= ev
->height
;
2198 if (win
->ws
->r
!= NULL
) {
2199 /* this seems to be full screen */
2200 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2202 win
->g
.w
= WIDTH(win
->ws
->r
);
2203 ev
->value_mask
|= CWX
| CWWidth
;
2205 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2208 win
->g
.h
= HEIGHT(win
->ws
->r
);
2209 ev
->value_mask
|= CWY
| CWHeight
;
2212 if ((ev
->value_mask
& (CWX
|CWY
)) &&
2213 !(ev
->value_mask
& (CWWidth
|CWHeight
)))
2215 XMoveResizeWindow(display
, win
->id
,
2216 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2223 configurenotify(XEvent
*e
)
2225 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2226 e
->xconfigure
.window
);
2230 destroynotify(XEvent
*e
)
2233 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2235 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2237 if ((win
= find_window(ev
->window
)) != NULL
) {
2238 unmanage_window(win
);
2244 enternotify(XEvent
*e
)
2246 XCrossingEvent
*ev
= &e
->xcrossing
;
2249 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2252 /* eat event(r) to prevent autofocus */
2257 if ((win
= find_window(ev
->window
)) != NULL
)
2264 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2268 mappingnotify(XEvent
*e
)
2270 XMappingEvent
*ev
= &e
->xmapping
;
2272 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2274 XRefreshKeyboardMapping(ev
);
2275 if (ev
->request
== MappingKeyboard
)
2280 maprequest(XEvent
*e
)
2282 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2283 XWindowAttributes wa
;
2285 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2286 e
->xmaprequest
.window
);
2288 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2290 if (wa
.override_redirect
)
2292 manage_window(e
->xmaprequest
.window
);
2297 propertynotify(XEvent
*e
)
2300 XPropertyEvent
*ev
= &e
->xproperty
;
2302 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2305 if (ev
->state
== PropertyDelete
)
2306 return; /* ignore */
2307 win
= find_window(ev
->window
);
2312 case XA_WM_NORMAL_HINTS
:
2315 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2316 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2317 if (win
->sh
.flags
& PMinSize
) {
2318 win
->g
.w
= win
->sh
.min_width
;
2319 win
->g
.h
= win
->sh
.min_height
;
2320 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2322 XMoveResizeWindow(display
, win
->id
,
2323 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2332 unmapnotify(XEvent
*e
)
2334 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2337 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2339 if ((win
= find_window(ev
->window
)) != NULL
)
2341 unmanage_window(win
);
2345 visibilitynotify(XEvent
*e
)
2348 struct swm_region
*r
;
2350 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2351 e
->xvisibility
.window
);
2352 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2353 for (i
= 0; i
< ScreenCount(display
); i
++)
2354 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2355 if (e
->xvisibility
.window
== r
->bar_window
)
2360 xerror_start(Display
*d
, XErrorEvent
*ee
)
2367 xerror(Display
*d
, XErrorEvent
*ee
)
2369 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2377 xerrorxlib
= XSetErrorHandler(xerror_start
);
2379 /* this causes an error if some other window manager is running */
2380 XSelectInput(display
, DefaultRootWindow(display
),
2381 SubstructureRedirectMask
);
2382 XSync(display
, False
);
2386 XSetErrorHandler(xerror
);
2387 XSync(display
, False
);
2396 unsigned char *p
= NULL
;
2397 unsigned long n
, extra
;
2400 astate
= XInternAtom(display
, "WM_STATE", False
);
2401 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
2402 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
2403 if (status
!= Success
)
2412 remove_region(struct swm_region
*r
)
2414 struct swm_screen
*s
= r
->s
;
2417 DNPRINTF(SWM_D_MISC
, "removing region: screen[%d]:%dx%d+%d+%d\n",
2418 s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
));
2420 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
2421 XUnmapWindow(display
, win
->id
);
2424 XDestroyWindow(display
, r
->bar_window
);
2425 TAILQ_REMOVE(&s
->rl
, r
, entry
);
2430 new_region(struct swm_screen
*s
, struct workspace
*ws
,
2431 int x
, int y
, int w
, int h
)
2433 struct swm_region
*r
, *n
;
2436 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
2437 s
->idx
, w
, h
, x
, y
);
2439 /* remove any conflicting regions */
2440 n
= TAILQ_FIRST(&s
->rl
);
2443 n
= TAILQ_NEXT(r
, entry
);
2444 if (X(r
) < (x
+ w
) &&
2445 (X(r
) + WIDTH(r
)) > x
&&
2447 (Y(r
) + HEIGHT(r
)) > y
) {
2454 /* pick an appropriate workspace */
2456 for (i
= 0; i
< SWM_WS_MAX
; i
++)
2457 if (s
->ws
[i
].r
== NULL
) {
2463 errx(1, "no free regions\n");
2466 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
2467 errx(1, "calloc: failed to allocate memory for screen");
2476 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
2483 #ifdef SWM_XRR_HAS_CRTC
2485 XRRScreenResources
*sr
;
2487 #endif /* SWM_XRR_HAS_CRTC */
2488 Window d1
, d2
, *wins
= NULL
;
2489 XWindowAttributes wa
;
2490 struct swm_region
*r
;
2492 int errorbase
, major
, minor
;
2493 int ncrtc
= 0, w
= 0;
2495 struct workspace
*ws
;
2499 if ((screens
= calloc(ScreenCount(display
),
2500 sizeof(struct swm_screen
))) == NULL
)
2501 errx(1, "calloc: screens");
2503 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2506 /* map physical screens */
2507 for (i
= 0; i
< ScreenCount(display
); i
++) {
2508 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
2510 TAILQ_INIT(&screens
[i
].rl
);
2511 screens
[i
].root
= RootWindow(display
, i
);
2513 /* set default colors */
2514 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
2515 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
2516 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
2517 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
2518 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
2520 /* init all workspaces */
2521 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
2522 ws
= &screens
[i
].ws
[j
];
2527 TAILQ_INIT(&ws
->winlist
);
2529 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
2530 if (layouts
[k
].l_config
!= NULL
)
2531 layouts
[k
].l_config(ws
,
2532 SWM_ARG_ID_STACKINIT
);
2533 ws
->cur_layout
= &layouts
[0];
2536 /* map virtual screens onto physical screens */
2537 screens
[i
].xrandr_support
= XRRQueryExtension(display
,
2538 &xrandr_eventbase
, &errorbase
);
2539 if (screens
[i
].xrandr_support
)
2540 if (XRRQueryVersion(display
, &major
, &minor
) &&
2542 screens
[i
].xrandr_support
= 0;
2544 #if 0 /* not ready for dynamic screen changes */
2545 if (screens
[i
].xrandr_support
)
2546 XRRSelectInput(display
,
2548 RRScreenChangeNotifyMask
);
2551 /* grab existing windows (before we build the bars)*/
2552 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
2555 #ifdef SWM_XRR_HAS_CRTC
2556 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
2558 new_region(&screens
[i
], &screens
[i
].ws
[w
],
2559 0, 0, DisplayWidth(display
, i
),
2560 DisplayHeight(display
, i
));
2564 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
2565 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
2566 if (ci
->noutput
== 0)
2569 if (ci
!= NULL
&& ci
->mode
== None
)
2570 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
2571 DisplayWidth(display
, i
),
2572 DisplayHeight(display
, i
));
2574 new_region(&screens
[i
], &screens
[i
].ws
[w
],
2575 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
2579 XRRFreeCrtcInfo(ci
);
2580 XRRFreeScreenResources(sr
);
2582 new_region(&screens
[i
], &screens
[i
].ws
[w
], 0, 0,
2583 DisplayWidth(display
, i
),
2584 DisplayHeight(display
, i
));
2585 #endif /* SWM_XRR_HAS_CRTC */
2587 /* attach windows to a region */
2588 /* normal windows */
2589 if ((r
= TAILQ_FIRST(&screens
[i
].rl
)) == NULL
)
2590 errx(1, "no regions on screen %d", i
);
2592 for (i
= 0; i
< no
; i
++) {
2593 XGetWindowAttributes(display
, wins
[i
], &wa
);
2594 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
2595 wa
.override_redirect
||
2596 XGetTransientForHint(display
, wins
[i
], &d1
))
2599 if (wa
.map_state
== IsViewable
||
2600 getstate(wins
[i
]) == NormalState
)
2601 manage_window(wins
[i
]);
2603 /* transient windows */
2604 for (i
= 0; i
< no
; i
++) {
2605 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
2608 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
2609 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
2611 manage_window(wins
[i
]);
2621 main(int argc
, char *argv
[])
2624 char conf
[PATH_MAX
], *cfile
= NULL
;
2631 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
2632 SWM_VERSION
, cvstag
);
2633 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2634 warnx("no locale support");
2636 if (!(display
= XOpenDisplay(0)))
2637 errx(1, "can not open display");
2640 errx(1, "other wm running");
2642 astate
= XInternAtom(display
, "WM_STATE", False
);
2644 /* look for local and global conf file */
2645 pwd
= getpwuid(getuid());
2647 errx(1, "invalid user %d", getuid());
2651 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2652 if (stat(conf
, &sb
) != -1) {
2653 if (S_ISREG(sb
.st_mode
))
2656 /* try global conf file */
2657 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2658 if (!stat(conf
, &sb
))
2659 if (S_ISREG(sb
.st_mode
))
2666 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2671 xfd
= ConnectionNumber(display
);
2675 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2677 errx(1, "select failed");
2682 while(XPending(display
)) {
2683 XNextEvent(display
, &e
);
2684 if (handler
[e
.type
])
2685 handler
[e
.type
](&e
);
2689 XCloseDisplay(display
);