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.9"
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
135 #define SWM_MAX_FONT_STEPS (3)
138 #define SWM_LIB "/usr/X11R6/lib/swmhack.so"
145 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
150 int xrandr_eventbase
;
151 int ignore_enter
= 0;
152 unsigned int numlockmask
= 0;
156 int cycle_visible
= 0;
158 int font_adjusted
= 0;
161 double dialog_ratio
= .6;
163 #define SWM_BAR_MAX (128)
164 char *bar_argv
[] = { NULL
, NULL
};
166 char bar_ext
[SWM_BAR_MAX
];
167 char bar_vertext
[SWM_BAR_MAX
];
169 sig_atomic_t bar_alarm
= 0;
173 int bar_extra_running
= 0;
181 char *bar_fonts
[] = {
182 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
183 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
187 /* terminal + args */
188 char *spawn_term
[] = { "xterm", NULL
};
189 char *spawn_screenshot
[] = { "screenshot.sh", NULL
, NULL
};
190 char *spawn_lock
[] = { "xlock", NULL
};
191 char *spawn_initscr
[] = { "initscreen.sh", NULL
};
192 char *spawn_menu
[] = { "dmenu_run", "-fn", NULL
, "-nb", NULL
,
193 "-nf", NULL
, "-sb", NULL
, "-sf", NULL
, NULL
};
195 #define SWM_MENU_FN (2)
196 #define SWM_MENU_NB (4)
197 #define SWM_MENU_NF (6)
198 #define SWM_MENU_SB (8)
199 #define SWM_MENU_SF (10)
201 /* layout manager data */
202 struct swm_geometry
{
212 /* virtual "screens" */
214 TAILQ_ENTRY(swm_region
) entry
;
215 struct swm_geometry g
;
216 struct workspace
*ws
; /* current workspace on this region */
217 struct swm_screen
*s
; /* screen idx */
220 TAILQ_HEAD(swm_region_list
, swm_region
);
223 TAILQ_ENTRY(ws_win
) entry
;
225 struct swm_geometry g
;
230 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
234 unsigned long quirks
;
235 struct workspace
*ws
; /* always valid */
236 struct swm_screen
*s
; /* always valid, never changes */
237 XWindowAttributes wa
;
241 TAILQ_HEAD(ws_win_list
, ws_win
);
243 /* layout handlers */
245 void vertical_config(struct workspace
*, int);
246 void vertical_stack(struct workspace
*, struct swm_geometry
*);
247 void horizontal_config(struct workspace
*, int);
248 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
249 void max_stack(struct workspace
*, struct swm_geometry
*);
251 void grabbuttons(struct ws_win
*, int);
254 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
255 void (*l_config
)(struct workspace
*, int);
257 /* stack, configure */
258 { vertical_stack
, vertical_config
},
259 { horizontal_stack
, horizontal_config
},
264 #define SWM_H_SLICE (32)
265 #define SWM_V_SLICE (32)
267 /* define work spaces */
269 int idx
; /* workspace index */
270 int restack
; /* restack on switch */
271 struct layout
*cur_layout
; /* current layout handlers */
272 struct ws_win
*focus
; /* may be NULL */
273 struct ws_win
*focus_prev
; /* may be NULL */
274 struct swm_region
*r
; /* may be NULL */
275 struct ws_win_list winlist
; /* list of windows in ws */
279 int horizontal_msize
;
281 int horizontal_stacks
;
288 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
289 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
291 /* physical screen mapping */
292 #define SWM_WS_MAX (10) /* XXX Too small? */
294 int idx
; /* screen index */
295 struct swm_region_list rl
; /* list of regions on this screen */
296 struct swm_region_list orl
; /* list of old regions */
298 struct workspace ws
[SWM_WS_MAX
];
304 } c
[SWM_S_COLOR_MAX
];
306 struct swm_screen
*screens
;
309 struct ws_win
*cur_focus
= NULL
;
311 /* args to functions */
314 #define SWM_ARG_ID_FOCUSNEXT (0)
315 #define SWM_ARG_ID_FOCUSPREV (1)
316 #define SWM_ARG_ID_FOCUSMAIN (2)
317 #define SWM_ARG_ID_SWAPNEXT (3)
318 #define SWM_ARG_ID_SWAPPREV (4)
319 #define SWM_ARG_ID_SWAPMAIN (5)
320 #define SWM_ARG_ID_MASTERSHRINK (6)
321 #define SWM_ARG_ID_MASTERGROW (7)
322 #define SWM_ARG_ID_MASTERADD (8)
323 #define SWM_ARG_ID_MASTERDEL (9)
324 #define SWM_ARG_ID_STACKRESET (10)
325 #define SWM_ARG_ID_STACKINIT (11)
326 #define SWM_ARG_ID_CYCLEWS_UP (12)
327 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
328 #define SWM_ARG_ID_CYCLESC_UP (14)
329 #define SWM_ARG_ID_CYCLESC_DOWN (15)
330 #define SWM_ARG_ID_STACKINC (16)
331 #define SWM_ARG_ID_STACKDEC (17)
332 #define SWM_ARG_ID_SS_ALL (0)
333 #define SWM_ARG_ID_SS_WINDOW (1)
334 #define SWM_ARG_ID_DONTCENTER (0)
335 #define SWM_ARG_ID_CENTER (1)
336 #define SWM_ARG_ID_KILLWINDOW (0)
337 #define SWM_ARG_ID_DELETEWINDOW (1)
346 #define SWM_Q_FLOAT (1<<0) /* float this window */
347 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
348 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
349 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
350 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
352 { "MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
},
353 { "OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
},
354 { "OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT
},
355 { "Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
},
356 { "Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
357 { "XTerm", "xterm", SWM_Q_XTERM_FONTADJ
},
358 { "xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
359 { "Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
360 { "xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
361 { "Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
},
362 { "xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
},
363 { "pcb", "pcb", SWM_Q_FLOAT
},
368 void expose(XEvent
*);
369 void keypress(XEvent
*);
370 void buttonpress(XEvent
*);
371 void configurerequest(XEvent
*);
372 void configurenotify(XEvent
*);
373 void destroynotify(XEvent
*);
374 void enternotify(XEvent
*);
375 void focusin(XEvent
*);
376 void mappingnotify(XEvent
*);
377 void maprequest(XEvent
*);
378 void propertynotify(XEvent
*);
379 void unmapnotify(XEvent
*);
380 void visibilitynotify(XEvent
*);
382 void (*handler
[LASTEvent
])(XEvent
*) = {
384 [KeyPress
] = keypress
,
385 [ButtonPress
] = buttonpress
,
386 [ConfigureRequest
] = configurerequest
,
387 [ConfigureNotify
] = configurenotify
,
388 [DestroyNotify
] = destroynotify
,
389 [EnterNotify
] = enternotify
,
391 [MappingNotify
] = mappingnotify
,
392 [MapRequest
] = maprequest
,
393 [PropertyNotify
] = propertynotify
,
394 [UnmapNotify
] = unmapnotify
,
395 [VisibilityNotify
] = visibilitynotify
,
399 name_to_color(char *colorname
)
403 XColor screen_def
, exact_def
;
404 unsigned long result
= 0;
405 char cname
[32] = "#";
407 cmap
= DefaultColormap(display
, screens
[0].idx
);
408 status
= XAllocNamedColor(display
, cmap
, colorname
,
409 &screen_def
, &exact_def
);
411 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
412 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
416 result
= screen_def
.pixel
;
418 fprintf(stderr
, "color '%s' not found.\n", colorname
);
424 setscreencolor(char *val
, int i
, int c
)
426 if (i
> 0 && i
<= ScreenCount(display
)) {
427 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
428 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
430 } else if (i
== -1) {
431 for (i
= 0; i
< ScreenCount(display
); i
++)
432 screens
[i
].c
[c
].color
= name_to_color(val
);
433 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
436 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
437 i
, ScreenCount(display
));
440 void new_region(struct swm_screen
*, int, int, int, int);
443 custom_region(char *val
)
445 unsigned int sidx
, x
, y
, w
, h
;
447 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
448 errx(1, "invalid custom region, "
449 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
450 if (sidx
< 1 || sidx
> ScreenCount(display
))
451 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
452 sidx
, ScreenCount(display
));
456 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
458 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
459 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
460 w
+ x
> DisplayWidth(display
, sidx
) ||
461 h
+ y
> DisplayHeight(display
, sidx
))
462 errx(1, "region %ux%u+%u+%u not within screen boundaries "
463 "(%ux%u)\n", w
, h
, x
, y
,
464 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
466 new_region(&screens
[sidx
], x
, y
, w
, h
);
470 varmatch(char *var
, char *name
, int *index
)
475 i
= strncmp(var
, name
, 255);
482 p
= var
+ strlen(name
);
485 bzero(buf
, sizeof buf
);
487 while (isdigit(*p
) && i
< sizeof buf
)
489 if (i
== 0 || i
>= sizeof buf
|| *p
!= ']')
491 *index
= strtonum(buf
, 0, 99, NULL
);
495 /* conf file stuff */
496 #define SWM_CONF_WS "\n= \t"
497 #define SWM_CONF_FILE "scrotwm.conf"
499 conf_load(char *filename
)
502 char *line
, *cp
, *var
, *val
;
503 size_t len
, lineno
= 0;
506 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
508 if (filename
== NULL
)
511 if ((config
= fopen(filename
, "r")) == NULL
)
514 for (sc
= ScreenCount(display
);;) {
515 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
519 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
525 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
527 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
528 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
531 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
534 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
535 bar_enabled
= atoi(val
);
536 else if (!varmatch(var
, "bar_border", &i
))
537 setscreencolor(val
, i
, SWM_S_COLOR_BAR_BORDER
);
538 else if (!varmatch(var
, "bar_color", &i
))
539 setscreencolor(val
, i
, SWM_S_COLOR_BAR
);
540 else if (!varmatch(var
, "bar_font_color", &i
))
541 setscreencolor(val
, i
, SWM_S_COLOR_BAR_FONT
);
542 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
543 asprintf(&bar_fonts
[0], "%s", val
);
544 else if (!strncmp(var
, "bar_action", strlen("bar_action")))
545 asprintf(&bar_argv
[0], "%s", val
);
546 else if (!strncmp(var
, "bar_delay", strlen("bar_delay")))
547 bar_delay
= atoi(val
);
553 if (!varmatch(var
, "color_focus", &i
))
554 setscreencolor(val
, i
, SWM_S_COLOR_FOCUS
);
555 else if (!varmatch(var
, "color_unfocus", &i
))
556 setscreencolor(val
, i
, SWM_S_COLOR_UNFOCUS
);
557 else if (!strncmp(var
, "cycle_empty", strlen("cycle_empty")))
558 cycle_visible
= atoi(val
);
559 else if (!strncmp(var
, "cycle_visible", strlen("cycle_visible")))
560 cycle_visible
= atoi(val
);
566 if (!strncmp(var
, "dialog_ratio",
567 strlen("dialog_ratio"))) {
568 dialog_ratio
= atof(val
);
569 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
576 if (!strncmp(var
, "region", strlen("region")))
583 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
584 asprintf(&spawn_term
[0], "%s", val
);
585 else if (!strncmp(var
, "screenshot_enabled",
586 strlen("screenshot_enabled")))
587 ss_enabled
= atoi(val
);
588 else if (!strncmp(var
, "screenshot_app",
589 strlen("screenshot_app")))
590 asprintf(&spawn_screenshot
[0], "%s", val
);
596 if (!strncmp(var
, "term_width", strlen("term_width")))
597 term_width
= atoi(val
);
612 errx(1, "invalid conf file entry: %s=%s", var
, val
);
616 socket_setnonblock(int fd
)
620 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
621 err(1, "fcntl F_GETFL");
623 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
624 err(1, "fcntl F_SETFL");
628 bar_print(struct swm_region
*r
, char *s
)
630 XClearWindow(display
, r
->bar_window
);
631 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
632 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
641 bzero(bar_pipe
, sizeof bar_pipe
);
644 kill(bar_pid
, SIGTERM
);
647 strlcpy(bar_ext
, "", sizeof bar_ext
);
656 struct swm_region
*r
;
660 char loc
[SWM_BAR_MAX
];
663 if (bar_enabled
== 0)
665 if (bar_extra
&& bar_extra_running
) {
666 /* ignore short reads; it'll correct itself */
667 while ((b
= fgetln(stdin
, &len
)) != NULL
)
668 if (b
&& b
[len
- 1] == '\n') {
670 strlcpy(bar_ext
, b
, sizeof bar_ext
);
672 if (b
== NULL
&& errno
!= EAGAIN
) {
673 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
674 errno
, strerror(errno
));
678 strlcpy(bar_ext
, "", sizeof bar_ext
);
681 localtime_r(&tmt
, &tm
);
682 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y", &tm
);
683 for (i
= 0; i
< ScreenCount(display
); i
++) {
685 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
686 snprintf(loc
, sizeof loc
, "%s %d:%d %s %s",
687 s
, x
++, r
->ws
->idx
+ 1, bar_ext
, bar_vertext
);
691 XSync(display
, False
);
702 bar_toggle(struct swm_region
*r
, union arg
*args
)
704 struct swm_region
*tmpr
;
705 int i
, j
, sc
= ScreenCount(display
);
707 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
710 for (i
= 0; i
< sc
; i
++)
711 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
712 XUnmapWindow(display
, tmpr
->bar_window
);
714 for (i
= 0; i
< sc
; i
++)
715 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
716 XMapRaised(display
, tmpr
->bar_window
);
718 bar_enabled
= !bar_enabled
;
719 for (i
= 0; i
< sc
; i
++)
720 for (j
= 0; j
< SWM_WS_MAX
; j
++)
721 screens
[i
].ws
[j
].restack
= 1;
724 /* must be after stack */
731 XSetWindowAttributes wa
;
732 struct swm_region
*r
;
735 /* do this here because the conf file is in memory */
736 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
737 /* launch external status app */
738 bar_extra_running
= 1;
739 if (pipe(bar_pipe
) == -1)
740 err(1, "pipe error");
741 socket_setnonblock(bar_pipe
[0]);
742 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
743 if (dup2(bar_pipe
[0], 0) == -1)
745 if (dup2(bar_pipe
[1], 1) == -1)
747 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
748 err(1, "could not disable SIGPIPE");
749 switch (bar_pid
= fork()) {
751 err(1, "cannot fork");
755 execvp(bar_argv
[0], bar_argv
);
756 err(1, "%s external app failed", bar_argv
[0]);
758 default: /* parent */
764 bzero(&wa
, sizeof wa
);
765 for (i
= 0; i
< ScreenCount(display
); i
++)
766 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
768 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
769 wa
.background_pixel
=
770 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
771 XChangeWindowAttributes(display
, r
->bar_window
,
772 CWBackPixel
| CWBorderPixel
, &wa
);
778 bar_setup(struct swm_region
*r
)
782 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
783 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
789 if (bar_fonts
[i
] == NULL
)
790 errx(1, "couldn't load font");
791 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
793 r
->bar_window
= XCreateSimpleWindow(display
,
794 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
795 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
796 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
797 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
798 XSetFont(display
, bar_gc
, bar_fs
->fid
);
799 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
801 XMapRaised(display
, r
->bar_window
);
802 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
804 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
805 err(1, "could not install bar_signal");
810 version(struct swm_region
*r
, union arg
*args
)
812 bar_version
= !bar_version
;
814 strlcpy(bar_vertext
, cvstag
, sizeof bar_vertext
);
816 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
821 client_msg(struct ws_win
*win
, Atom a
)
823 XClientMessageEvent cm
;
825 bzero(&cm
, sizeof cm
);
826 cm
.type
= ClientMessage
;
828 cm
.message_type
= aprot
;
831 cm
.data
.l
[1] = CurrentTime
;
832 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
836 config_win(struct ws_win
*win
)
840 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
841 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
842 ce
.type
= ConfigureNotify
;
843 ce
.display
= display
;
849 ce
.height
= win
->g
.h
;
850 ce
.border_width
= 1; /* XXX store this! */
852 ce
.override_redirect
= False
;
853 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
857 count_win(struct workspace
*ws
, int count_transient
)
862 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
863 if (count_transient
== 0 && win
->floating
)
865 if (count_transient
== 0 && win
->transient
)
869 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
875 quit(struct swm_region
*r
, union arg
*args
)
877 DNPRINTF(SWM_D_MISC
, "quit\n");
887 for (i
= 0; i
< ScreenCount(display
); i
++)
888 for (j
= 0; j
< SWM_WS_MAX
; j
++)
889 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
890 XUnmapWindow(display
, win
->id
);
894 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
898 event
.display
= display
; /* Ignored, but what the hell */
899 event
.window
= win
->id
;
900 event
.root
= win
->s
->root
;
901 event
.subwindow
= None
;
902 event
.time
= CurrentTime
;
907 event
.same_screen
= True
;
908 event
.keycode
= XKeysymToKeycode(display
, keysym
);
909 event
.state
= modifiers
;
911 event
.type
= KeyPress
;
912 XSendEvent(event
.display
, event
.window
, True
,
913 KeyPressMask
, (XEvent
*)&event
);
915 event
.type
= KeyRelease
;
916 XSendEvent(event
.display
, event
.window
, True
,
917 KeyPressMask
, (XEvent
*)&event
);
922 restart(struct swm_region
*r
, union arg
*args
)
924 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
926 /* disable alarm because the following code may not be interrupted */
928 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
929 errx(1, "can't disable alarm");
934 XCloseDisplay(display
);
935 execvp(start_argv
[0], start_argv
);
936 fprintf(stderr
, "execvp failed\n");
942 root_to_region(Window root
)
944 struct swm_region
*r
= NULL
;
949 for (i
= 0; i
< ScreenCount(display
); i
++)
950 if (screens
[i
].root
== root
)
953 if (XQueryPointer(display
, screens
[i
].root
,
954 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
955 /* choose a region based on pointer location */
956 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
957 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
958 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
963 r
= TAILQ_FIRST(&screens
[i
].rl
);
969 find_window(Window id
)
974 for (i
= 0; i
< ScreenCount(display
); i
++)
975 for (j
= 0; j
< SWM_WS_MAX
; j
++)
976 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
983 spawn(struct swm_region
*r
, union arg
*args
)
988 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
990 * The double-fork construct avoids zombie processes and keeps the code
991 * clean from stupid signal handlers.
996 close(ConnectionNumber(display
));
997 setenv("LD_PRELOAD", SWM_LIB
, 1);
998 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
999 setenv("_SWM_WS", ret
, 1);
1002 if (asprintf(&ret
, "%d", getpid())) {
1003 setenv("_SWM_PID", ret
, 1);
1007 /* kill stdin, mplayer, ssh-add etc. need that */
1008 si
= open("/dev/null", O_RDONLY
, 0);
1010 err(1, "open /dev/null");
1011 if (dup2(si
, 0) == -1)
1012 err(1, "dup2 /dev/null");
1013 execvp(args
->argv
[0], args
->argv
);
1014 fprintf(stderr
, "execvp failed\n");
1023 spawnterm(struct swm_region
*r
, union arg
*args
)
1025 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
1028 setenv("_SWM_XTERM_FONTADJ", "", 1);
1033 spawnmenu(struct swm_region
*r
, union arg
*args
)
1035 DNPRINTF(SWM_D_MISC
, "spawnmenu\n");
1037 spawn_menu
[SWM_MENU_FN
] = bar_fonts
[bar_fidx
];
1038 spawn_menu
[SWM_MENU_NB
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
1039 spawn_menu
[SWM_MENU_NF
] = r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
;
1040 spawn_menu
[SWM_MENU_SB
] = r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
;
1041 spawn_menu
[SWM_MENU_SF
] = r
->s
->c
[SWM_S_COLOR_BAR
].name
;
1052 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
1054 for (i
= 0; i
< ScreenCount(display
); i
++)
1055 for (j
= 0; j
< SWM_WS_MAX
; j
++)
1056 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
) {
1057 if (win
->ws
->r
== NULL
)
1059 grabbuttons(win
, 0);
1060 XSetWindowBorder(display
, win
->id
,
1061 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
1063 win
->ws
->focus
= NULL
;
1069 focus_win(struct ws_win
*win
)
1071 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
1076 if (win
->ws
->focus
!= win
&& win
->ws
->focus
!= NULL
)
1077 win
->ws
->focus_prev
= win
->ws
->focus
;
1080 win
->ws
->focus
= win
;
1081 if (win
->ws
->r
!= NULL
) {
1083 if (!win
->got_focus
) {
1084 XSetWindowBorder(display
, win
->id
,
1085 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1086 grabbuttons(win
, 1);
1089 XSetInputFocus(display
, win
->id
,
1090 RevertToPointerRoot
, CurrentTime
);
1095 switchws(struct swm_region
*r
, union arg
*args
)
1097 int wsid
= args
->id
;
1098 struct swm_region
*this_r
, *other_r
;
1100 struct workspace
*new_ws
, *old_ws
;
1103 old_ws
= this_r
->ws
;
1104 new_ws
= &this_r
->s
->ws
[wsid
];
1106 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1107 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1110 if (new_ws
== old_ws
)
1113 other_r
= new_ws
->r
;
1115 /* if the other workspace is hidden, switch windows */
1116 /* map new window first to prevent ugly blinking */
1118 old_ws
->restack
= 1;
1120 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1121 XMapRaised(display
, win
->id
);
1123 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1124 XUnmapWindow(display
, win
->id
);
1126 other_r
->ws
= old_ws
;
1127 old_ws
->r
= other_r
;
1129 this_r
->ws
= new_ws
;
1135 focus_win(new_ws
->focus
);
1141 cyclews(struct swm_region
*r
, union arg
*args
)
1144 struct swm_screen
*s
= r
->s
;
1146 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1147 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1148 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1153 case SWM_ARG_ID_CYCLEWS_UP
:
1154 if (a
.id
< SWM_WS_MAX
- 1)
1159 case SWM_ARG_ID_CYCLEWS_DOWN
:
1163 a
.id
= SWM_WS_MAX
- 1;
1169 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1171 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1175 } while (a
.id
!= r
->ws
->idx
);
1179 cyclescr(struct swm_region
*r
, union arg
*args
)
1181 struct swm_region
*rr
;
1186 case SWM_ARG_ID_CYCLESC_UP
:
1187 rr
= TAILQ_NEXT(r
, entry
);
1189 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1191 case SWM_ARG_ID_CYCLESC_DOWN
:
1192 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1194 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1200 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1201 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1202 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1206 swapwin(struct swm_region
*r
, union arg
*args
)
1208 struct ws_win
*target
, *source
;
1209 struct ws_win_list
*wl
;
1212 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1213 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1214 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1215 if (cur_focus
== NULL
)
1219 wl
= &source
->ws
->winlist
;
1222 case SWM_ARG_ID_SWAPPREV
:
1223 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
1224 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1226 TAILQ_INSERT_TAIL(wl
, source
, entry
);
1228 TAILQ_INSERT_BEFORE(target
, source
, entry
);
1230 case SWM_ARG_ID_SWAPNEXT
:
1231 target
= TAILQ_NEXT(source
, entry
);
1232 TAILQ_REMOVE(wl
, source
, entry
);
1234 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1236 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
1238 case SWM_ARG_ID_SWAPMAIN
:
1239 target
= TAILQ_FIRST(wl
);
1240 if (target
== source
) {
1241 if (source
->ws
->focus_prev
!= NULL
&&
1242 source
->ws
->focus_prev
!= target
)
1244 source
= source
->ws
->focus_prev
;
1248 source
->ws
->focus_prev
= target
;
1249 TAILQ_REMOVE(wl
, target
, entry
);
1250 TAILQ_INSERT_BEFORE(source
, target
, entry
);
1251 TAILQ_REMOVE(wl
, source
, entry
);
1252 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1255 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1264 focus(struct swm_region
*r
, union arg
*args
)
1266 struct ws_win
*winfocus
, *winlostfocus
;
1267 struct ws_win_list
*wl
;
1269 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1270 if (cur_focus
== NULL
)
1273 wl
= &cur_focus
->ws
->winlist
;
1275 winlostfocus
= cur_focus
;
1278 case SWM_ARG_ID_FOCUSPREV
:
1279 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1280 if (winfocus
== NULL
)
1281 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1284 case SWM_ARG_ID_FOCUSNEXT
:
1285 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1286 if (winfocus
== NULL
)
1287 winfocus
= TAILQ_FIRST(wl
);
1290 case SWM_ARG_ID_FOCUSMAIN
:
1291 winfocus
= TAILQ_FIRST(wl
);
1298 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1301 XMapRaised(display
, winfocus
->id
);
1302 focus_win(winfocus
);
1303 XSync(display
, False
);
1307 cycle_layout(struct swm_region
*r
, union arg
*args
)
1309 struct workspace
*ws
= r
->ws
;
1311 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1314 if (ws
->cur_layout
->l_stack
== NULL
)
1315 ws
->cur_layout
= &layouts
[0];
1321 stack_config(struct swm_region
*r
, union arg
*args
)
1323 struct workspace
*ws
= r
->ws
;
1325 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1328 if (ws
->cur_layout
->l_config
!= NULL
)
1329 ws
->cur_layout
->l_config(ws
, args
->id
);
1331 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1337 struct swm_geometry g
;
1338 struct swm_region
*r
;
1341 DNPRINTF(SWM_D_STACK
, "stack\n");
1343 for (i
= 0; i
< ScreenCount(display
); i
++) {
1345 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1346 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1347 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1349 /* start with screen geometry, adjust for bar */
1359 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1364 XSync(display
, False
);
1368 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1373 bzero(&wc
, sizeof wc
);
1374 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1375 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
== WIDTH(r
)) &&
1376 (win
->g
.h
== HEIGHT(r
)))
1377 wc
.border_width
= 0;
1379 wc
.border_width
= 1;
1380 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1381 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1382 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1384 wc
.width
= win
->g
.w
;
1385 wc
.height
= win
->g
.h
;
1390 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1391 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1394 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1395 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1397 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1401 * Send keystrokes to terminal to decrease/increase the font size as the
1402 * window size changes.
1405 adjust_font(struct ws_win
*win
)
1407 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
1408 win
->floating
|| win
->transient
)
1411 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
1412 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
1413 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
1414 win
->font_size_boundary
[win
->font_steps
] =
1415 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
1418 win
->last_inc
= win
->sh
.width_inc
;
1419 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
1420 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
1421 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
1424 win
->last_inc
= win
->sh
.width_inc
;
1425 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
1429 #define SWAPXY(g) do { \
1431 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1432 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1435 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1438 struct swm_geometry win_g
, r_g
= *g
;
1439 struct ws_win
*win
, *winfocus
;
1440 int i
, j
, s
, stacks
;
1441 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
1442 int hrh
, extra
= 0, h_slice
, last_h
= 0;
1443 int split
, colno
, winno
, mwin
, msize
, mscale
;
1444 int remain
, missing
, v_slice
;;
1447 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1448 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1450 if ((winno
= count_win(ws
, 0)) == 0)
1453 if (ws
->focus
== NULL
)
1454 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1455 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1457 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1458 if (win
->transient
== 0 && win
->floating
== 0)
1465 w_inc
= win
->sh
.width_inc
;
1466 w_base
= win
->sh
.base_width
;
1467 mwin
= ws
->l_state
.horizontal_mwin
;
1468 mscale
= ws
->l_state
.horizontal_msize
;
1469 stacks
= ws
->l_state
.horizontal_stacks
;
1472 w_inc
= win
->sh
.height_inc
;
1473 w_base
= win
->sh
.base_height
;
1474 mwin
= ws
->l_state
.vertical_mwin
;
1475 mscale
= ws
->l_state
.vertical_msize
;
1476 stacks
= ws
->l_state
.vertical_stacks
;
1480 if (stacks
> winno
- mwin
)
1481 stacks
= winno
- mwin
;
1485 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1486 if (mwin
&& winno
> mwin
) {
1487 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1491 win_g
.w
= v_slice
* mscale
;
1493 if (w_inc
> 1 && w_inc
< v_slice
) {
1494 /* adjust for window's requested size increment */
1495 remain
= (win_g
.w
- w_base
) % w_inc
;
1496 missing
= w_inc
- remain
;
1503 win_g
.x
+= r_g
.w
- msize
;
1506 colno
= split
= winno
/ stacks
;
1507 win_g
.w
= ((r_g
.w
- (stacks
* 2) + 2) / stacks
);
1509 hrh
= r_g
.h
/ colno
;
1510 extra
= r_g
.h
- (colno
* hrh
);
1513 /* stack all the tiled windows */
1514 i
= j
= 0, s
= stacks
;
1515 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1516 if (win
->transient
!= 0 || win
->floating
!= 0)
1519 if (split
&& i
== split
) {
1520 colno
= (winno
- mwin
) / stacks
;
1521 if (s
<= (winno
- mwin
) % stacks
)
1523 split
= split
+ colno
;
1524 hrh
= (r_g
.h
/ colno
);
1525 extra
= r_g
.h
- (colno
* hrh
);
1529 win_g
.x
+= win_g
.w
+ 2;
1530 win_g
.w
= (r_g
.w
- msize
- (stacks
* 2)) / stacks
;
1532 win_g
.w
+= (r_g
.w
- msize
- (stacks
* 2)) %
1539 h_inc
= win
->sh
.width_inc
;
1540 h_base
= win
->sh
.base_width
;
1542 h_inc
= win
->sh
.height_inc
;
1543 h_base
= win
->sh
.base_height
;
1545 if (j
== colno
- 1) {
1546 win_g
.h
= hrh
+ extra
;
1547 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1548 /* adjust for window's requested size increment */
1549 remain
= (win_g
.h
- h_base
) % h_inc
;
1550 missing
= h_inc
- remain
;
1552 if (missing
<= extra
|| j
== 0) {
1564 win_g
.y
+= last_h
+ 2;
1566 bzero(&wc
, sizeof wc
);
1567 wc
.border_width
= 1;
1569 win
->g
.x
= wc
.x
= win_g
.y
;
1570 win
->g
.y
= wc
.y
= win_g
.x
;
1571 win
->g
.w
= wc
.width
= win_g
.h
;
1572 win
->g
.h
= wc
.height
= win_g
.w
;
1574 win
->g
.x
= wc
.x
= win_g
.x
;
1575 win
->g
.y
= wc
.y
= win_g
.y
;
1576 win
->g
.w
= wc
.width
= win_g
.w
;
1577 win
->g
.h
= wc
.height
= win_g
.h
;
1580 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1581 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1582 XMapRaised(display
, win
->id
);
1590 /* now, stack all the floaters and transients */
1591 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1592 if (win
->transient
== 0 && win
->floating
== 0)
1595 stack_floater(win
, ws
->r
);
1596 XMapRaised(display
, win
->id
);
1600 focus_win(winfocus
); /* has to be done outside of the loop */
1604 vertical_config(struct workspace
*ws
, int id
)
1606 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1609 case SWM_ARG_ID_STACKRESET
:
1610 case SWM_ARG_ID_STACKINIT
:
1611 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1612 ws
->l_state
.vertical_mwin
= 1;
1613 ws
->l_state
.vertical_stacks
= 1;
1615 case SWM_ARG_ID_MASTERSHRINK
:
1616 if (ws
->l_state
.vertical_msize
> 1)
1617 ws
->l_state
.vertical_msize
--;
1619 case SWM_ARG_ID_MASTERGROW
:
1620 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1621 ws
->l_state
.vertical_msize
++;
1623 case SWM_ARG_ID_MASTERADD
:
1624 ws
->l_state
.vertical_mwin
++;
1626 case SWM_ARG_ID_MASTERDEL
:
1627 if (ws
->l_state
.vertical_mwin
> 0)
1628 ws
->l_state
.vertical_mwin
--;
1630 case SWM_ARG_ID_STACKINC
:
1631 ws
->l_state
.vertical_stacks
++;
1633 case SWM_ARG_ID_STACKDEC
:
1634 if (ws
->l_state
.vertical_stacks
> 1)
1635 ws
->l_state
.vertical_stacks
--;
1643 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1645 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1647 stack_master(ws
, g
, 0, 0);
1651 horizontal_config(struct workspace
*ws
, int id
)
1653 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1656 case SWM_ARG_ID_STACKRESET
:
1657 case SWM_ARG_ID_STACKINIT
:
1658 ws
->l_state
.horizontal_mwin
= 1;
1659 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1660 ws
->l_state
.horizontal_stacks
= 1;
1662 case SWM_ARG_ID_MASTERSHRINK
:
1663 if (ws
->l_state
.horizontal_msize
> 1)
1664 ws
->l_state
.horizontal_msize
--;
1666 case SWM_ARG_ID_MASTERGROW
:
1667 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1668 ws
->l_state
.horizontal_msize
++;
1670 case SWM_ARG_ID_MASTERADD
:
1671 ws
->l_state
.horizontal_mwin
++;
1673 case SWM_ARG_ID_MASTERDEL
:
1674 if (ws
->l_state
.horizontal_mwin
> 0)
1675 ws
->l_state
.horizontal_mwin
--;
1677 case SWM_ARG_ID_STACKINC
:
1678 ws
->l_state
.horizontal_stacks
++;
1680 case SWM_ARG_ID_STACKDEC
:
1681 if (ws
->l_state
.horizontal_stacks
> 1)
1682 ws
->l_state
.horizontal_stacks
--;
1690 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1692 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1694 stack_master(ws
, g
, 1, 0);
1697 /* fullscreen view */
1699 max_stack(struct workspace
*ws
, struct swm_geometry
*g
) {
1701 struct swm_geometry gg
= *g
;
1702 struct ws_win
*win
, *winfocus
;
1705 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1707 if (count_win(ws
, 0) == 0)
1710 if (ws
->focus
== NULL
)
1711 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1712 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1714 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1715 if (win
->transient
!= 0 || win
->floating
!= 0) {
1716 if (win
== ws
->focus
) {
1718 stack_floater(win
, ws
->r
);
1719 XMapRaised(display
, win
->id
);
1721 XUnmapWindow(display
, win
->id
);
1723 bzero(&wc
, sizeof wc
);
1724 wc
.border_width
= 1;
1725 win
->g
.x
= wc
.x
= gg
.x
;
1726 win
->g
.y
= wc
.y
= gg
.y
;
1727 win
->g
.w
= wc
.width
= gg
.w
;
1728 win
->g
.h
= wc
.height
= gg
.h
;
1729 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1730 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1732 if (win
== ws
->focus
) {
1733 XMapRaised(display
, win
->id
);
1735 XUnmapWindow(display
, win
->id
);
1740 focus_win(winfocus
); /* has to be done outside of the loop */
1744 send_to_ws(struct swm_region
*r
, union arg
*args
)
1746 int wsid
= args
->id
;
1747 struct ws_win
*win
= cur_focus
;
1748 struct workspace
*ws
, *nws
;
1749 Atom ws_idx_atom
= 0;
1750 unsigned char ws_idx_str
[SWM_PROPLEN
];
1755 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1758 nws
= &win
->s
->ws
[wsid
];
1760 XUnmapWindow(display
, win
->id
);
1762 /* find a window to focus */
1763 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1764 if (ws
->focus
== NULL
)
1765 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1766 if (ws
->focus
== win
)
1769 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1771 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1774 /* Try to update the window's workspace property */
1775 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1777 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1778 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1780 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1781 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1784 if (count_win(nws
, 1) == 1)
1793 wkill(struct swm_region
*r
, union arg
*args
)
1795 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
1797 if(r
->ws
->focus
== NULL
)
1800 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
1801 XKillClient(display
, r
->ws
->focus
->id
);
1803 if (r
->ws
->focus
->can_delete
)
1804 client_msg(r
->ws
->focus
, adelete
);
1808 screenshot(struct swm_region
*r
, union arg
*args
)
1812 DNPRINTF(SWM_D_MISC
, "screenshot\n");
1814 if (ss_enabled
== 0)
1818 case SWM_ARG_ID_SS_ALL
:
1819 spawn_screenshot
[1] = "full";
1821 case SWM_ARG_ID_SS_WINDOW
:
1822 spawn_screenshot
[1] = "window";
1827 a
.argv
= spawn_screenshot
;
1832 floating_toggle(struct swm_region
*r
, union arg
*args
)
1834 struct ws_win
*win
= cur_focus
;
1839 win
->floating
= !win
->floating
;
1845 /* key definitions */
1849 void (*func
)(struct swm_region
*r
, union arg
*);
1852 /* modifier key function argument */
1853 { MODKEY
, XK_space
, cycle_layout
, {0} },
1854 { MODKEY
| ShiftMask
, XK_space
, stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1855 { MODKEY
, XK_h
, stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1856 { MODKEY
, XK_l
, stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1857 { MODKEY
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1858 { MODKEY
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1859 { MODKEY
| ShiftMask
, XK_comma
, stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
1860 { MODKEY
| ShiftMask
, XK_period
, stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
1861 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1862 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1863 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1864 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1865 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1866 { MODKEY
| ShiftMask
, XK_Return
, spawnterm
, {.argv
= spawn_term
} },
1867 { MODKEY
, XK_p
, spawnmenu
, {.argv
= spawn_menu
} },
1868 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1869 { MODKEY
, XK_q
, restart
, {0} },
1870 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1871 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1872 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1873 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1874 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1875 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1876 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1877 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1878 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1879 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1880 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1881 { MODKEY
, XK_Right
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1882 { MODKEY
, XK_Left
, cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1883 { MODKEY
| ShiftMask
, XK_Right
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1884 { MODKEY
| ShiftMask
, XK_Left
, cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1885 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1886 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1887 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1888 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1889 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1890 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1891 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1892 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1893 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1894 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1895 { MODKEY
, XK_b
, bar_toggle
, {0} },
1896 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1897 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1898 { MODKEY
| ShiftMask
, XK_x
, wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
1899 { MODKEY
, XK_x
, wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
1900 { MODKEY
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_ALL
} },
1901 { MODKEY
| ShiftMask
, XK_s
, screenshot
, {.id
= SWM_ARG_ID_SS_WINDOW
} },
1902 { MODKEY
, XK_t
, floating_toggle
,{0} },
1903 { MODKEY
| ShiftMask
, XK_v
, version
, {0} },
1904 { MODKEY
| ShiftMask
, XK_Delete
, spawn
, {.argv
= spawn_lock
} },
1905 { MODKEY
| ShiftMask
, XK_i
, spawn
, {.argv
= spawn_initscr
} },
1909 resize_window(struct ws_win
*win
, int center
)
1913 struct swm_region
*r
;
1915 r
= root_to_region(win
->wa
.root
);
1916 bzero(&wc
, sizeof wc
);
1917 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1918 wc
.border_width
= 1;
1919 wc
.width
= win
->g
.w
;
1920 wc
.height
= win
->g
.h
;
1921 if (center
== SWM_ARG_ID_CENTER
) {
1922 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1923 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1927 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1928 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1930 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1935 resize(struct ws_win
*win
, union arg
*args
)
1940 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1941 win
->id
, win
->floating
, win
->transient
);
1943 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1946 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1947 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1949 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1951 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1952 SubstructureRedirectMask
, &ev
);
1954 case ConfigureRequest
:
1957 handler
[ev
.type
](&ev
);
1960 if (ev
.xmotion
.x
<= 1)
1962 if (ev
.xmotion
.y
<= 1)
1964 win
->g
.w
= ev
.xmotion
.x
;
1965 win
->g
.h
= ev
.xmotion
.y
;
1967 /* not free, don't sync more than 60 times / second */
1968 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1969 time
= ev
.xmotion
.time
;
1970 XSync(display
, False
);
1971 resize_window(win
, args
->id
);
1975 } while (ev
.type
!= ButtonRelease
);
1977 XSync(display
, False
);
1978 resize_window(win
, args
->id
);
1980 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1982 XUngrabPointer(display
, CurrentTime
);
1985 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1989 move_window(struct ws_win
*win
)
1993 struct swm_region
*r
;
1995 r
= root_to_region(win
->wa
.root
);
1996 bzero(&wc
, sizeof wc
);
2001 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
2002 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
2004 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2009 move(struct ws_win
*win
, union arg
*args
)
2015 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
2016 win
->id
, win
->floating
, win
->transient
);
2018 if (win
->floating
== 0) {
2024 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
2025 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
2027 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
2029 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
2030 SubstructureRedirectMask
, &ev
);
2032 case ConfigureRequest
:
2035 handler
[ev
.type
](&ev
);
2038 win
->g
.x
= ev
.xmotion
.x_root
;
2039 win
->g
.y
= ev
.xmotion
.y_root
;
2041 /* not free, don't sync more than 60 times / second */
2042 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
2043 time
= ev
.xmotion
.time
;
2044 XSync(display
, False
);
2049 } while (ev
.type
!= ButtonRelease
);
2051 XSync(display
, False
);
2054 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
2055 XUngrabPointer(display
, CurrentTime
);
2060 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
2064 enum { client_click
, root_click
};
2066 unsigned int action
;
2068 unsigned int button
;
2069 void (*func
)(struct ws_win
*, union arg
*);
2072 /* action key mouse button func args */
2073 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
2074 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
2075 { client_click
, MODKEY
, Button1
, move
, {0} },
2079 updatenumlockmask(void)
2082 XModifierKeymap
*modmap
;
2084 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
2086 modmap
= XGetModifierMapping(display
);
2087 for (i
= 0; i
< 8; i
++)
2088 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2089 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2090 == XKeysymToKeycode(display
, XK_Num_Lock
))
2091 numlockmask
= (1 << i
);
2093 XFreeModifiermap(modmap
);
2099 unsigned int i
, j
, k
;
2101 unsigned int modifiers
[] =
2102 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
2104 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
2105 updatenumlockmask();
2107 for (k
= 0; k
< ScreenCount(display
); k
++) {
2108 if (TAILQ_EMPTY(&screens
[k
].rl
))
2110 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
2111 for (i
= 0; i
< LENGTH(keys
); i
++) {
2112 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
2113 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2114 XGrabKey(display
, code
,
2115 keys
[i
].mod
| modifiers
[j
],
2116 screens
[k
].root
, True
,
2117 GrabModeAsync
, GrabModeAsync
);
2123 grabbuttons(struct ws_win
*win
, int focused
)
2126 unsigned int modifiers
[] =
2127 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
2129 updatenumlockmask();
2130 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
2132 for (i
= 0; i
< LENGTH(buttons
); i
++)
2133 if (buttons
[i
].action
== client_click
)
2134 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2135 XGrabButton(display
, buttons
[i
].button
,
2136 buttons
[i
].mask
| modifiers
[j
],
2137 win
->id
, False
, BUTTONMASK
,
2138 GrabModeAsync
, GrabModeSync
, None
,
2141 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
2142 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
2148 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
2156 XKeyEvent
*ev
= &e
->xkey
;
2158 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
2160 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
2161 for (i
= 0; i
< LENGTH(keys
); i
++)
2162 if (keysym
== keys
[i
].keysym
2163 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
2165 keys
[i
].func(root_to_region(ev
->root
),
2170 buttonpress(XEvent
*e
)
2172 XButtonPressedEvent
*ev
= &e
->xbutton
;
2177 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2179 action
= root_click
;
2180 if ((win
= find_window(ev
->window
)) == NULL
)
2184 action
= client_click
;
2187 for (i
= 0; i
< LENGTH(buttons
); i
++)
2188 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2189 buttons
[i
].button
== ev
->button
&&
2190 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2191 buttons
[i
].func(win
, &buttons
[i
].args
);
2195 set_win_state(struct ws_win
*win
, long state
)
2197 long data
[] = {state
, None
};
2199 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2201 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2202 (unsigned char *)data
, 2);
2206 manage_window(Window id
)
2209 struct workspace
*ws
;
2211 int format
, i
, ws_idx
, n
;
2212 unsigned long nitems
, bytes
;
2213 Atom ws_idx_atom
= 0, type
;
2214 Atom
*prot
= NULL
, *pp
;
2215 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
2216 struct swm_region
*r
;
2221 if ((win
= find_window(id
)) != NULL
)
2222 return (win
); /* already being managed */
2224 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
2225 errx(1, "calloc: failed to allocate memory for new window");
2227 /* Get all the window data in one shot */
2228 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2230 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
2231 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
2232 XGetWindowAttributes(display
, id
, &win
->wa
);
2233 XGetTransientForHint(display
, id
, &trans
);
2234 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
2236 win
->transient
= trans
;
2237 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
2238 (unsigned)win
->id
, win
->transient
);
2240 /* get supported protocols */
2241 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
2242 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++)
2244 win
->can_delete
= 1;
2250 * Figure out where to put the window. If it was previously assigned to
2251 * a workspace (either by spawn() or manually moving), and isn't
2252 * transient, * put it in the same workspace
2254 r
= root_to_region(win
->wa
.root
);
2255 if (prop
&& win
->transient
== 0) {
2256 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
2257 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
2259 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
2262 ws
= &r
->s
->ws
[ws_idx
];
2266 /* set up the window layout */
2269 win
->s
= r
->s
; /* this never changes */
2270 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
2272 win
->g
.w
= win
->wa
.width
;
2273 win
->g
.h
= win
->wa
.height
;
2274 win
->g
.x
= win
->wa
.x
;
2275 win
->g
.y
= win
->wa
.y
;
2277 /* Set window properties so we can remember this after reincarnation */
2278 if (ws_idx_atom
&& prop
== NULL
&&
2279 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
2280 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
2282 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
2283 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
2287 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
2288 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
2289 win
->ch
.res_class
, win
->ch
.res_name
);
2290 for (i
= 0; quirks
[i
].class != NULL
&& quirks
[i
].name
!= NULL
&&
2291 quirks
[i
].quirk
!= 0; i
++){
2292 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
2293 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
2294 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
2295 win
->ch
.res_class
, win
->ch
.res_name
);
2296 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
2298 win
->quirks
= quirks
[i
].quirk
;
2303 /* alter window position if quirky */
2304 if (win
->quirks
& SWM_Q_ANYWHERE
) {
2305 win
->manual
= 1; /* don't center the quirky windows */
2306 bzero(&wc
, sizeof wc
);
2308 if (win
->g
.y
< bar_height
) {
2309 win
->g
.y
= wc
.y
= bar_height
;
2312 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
2313 win
->g
.x
= wc
.x
= WIDTH(win
->ws
->r
) - win
->g
.w
- 2;
2316 wc
.border_width
= 1;
2317 mask
|= CWBorderWidth
;
2318 XConfigureWindow(display
, win
->id
, mask
, &wc
);
2321 /* Reset font sizes (the bruteforce way; no default keybinding). */
2322 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
2323 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
2324 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
2325 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
2326 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
2329 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
2330 PropertyChangeMask
| StructureNotifyMask
);
2332 set_win_state(win
, NormalState
);
2334 /* floaters need to be mapped if they are in the current workspace */
2335 if (win
->floating
&& (ws
->idx
== r
->ws
->idx
))
2336 XMapRaised(display
, win
->id
);
2338 /* make new win focused */
2345 unmanage_window(struct ws_win
*win
)
2347 struct workspace
*ws
;
2352 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
2354 /* don't unmanage if we are switching workspaces */
2359 /* find a window to focus */
2360 if (ws
->focus
== win
)
2361 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
2362 if (ws
->focus
== NULL
)
2363 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
2364 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
2368 focus_win(ws
->focus
);
2369 if (ws
->focus_prev
== win
)
2370 ws
->focus_prev
= NULL
;
2372 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
2373 set_win_state(win
, WithdrawnState
);
2374 if (win
->ch
.res_class
)
2375 XFree(win
->ch
.res_class
);
2376 if (win
->ch
.res_name
)
2377 XFree(win
->ch
.res_name
);
2382 configurerequest(XEvent
*e
)
2384 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
2389 if ((win
= find_window(ev
->window
)) == NULL
)
2393 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
2395 bzero(&wc
, sizeof wc
);
2398 wc
.width
= ev
->width
;
2399 wc
.height
= ev
->height
;
2400 wc
.border_width
= ev
->border_width
;
2401 wc
.sibling
= ev
->above
;
2402 wc
.stack_mode
= ev
->detail
;
2403 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
2405 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
2407 if (win
->floating
) {
2408 if (ev
->value_mask
& CWX
)
2410 if (ev
->value_mask
& CWY
)
2412 if (ev
->value_mask
& CWWidth
)
2413 win
->g
.w
= ev
->width
;
2414 if (ev
->value_mask
& CWHeight
)
2415 win
->g
.h
= ev
->height
;
2416 if (win
->ws
->r
!= NULL
) {
2417 /* this seems to be full screen */
2418 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
2420 win
->g
.w
= WIDTH(win
->ws
->r
);
2421 ev
->value_mask
|= CWX
| CWWidth
;
2423 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
2426 win
->g
.h
= HEIGHT(win
->ws
->r
);
2427 ev
->value_mask
|= CWY
| CWHeight
;
2430 if ((ev
->value_mask
& (CWX
| CWY
)) &&
2431 !(ev
->value_mask
& (CWWidth
| CWHeight
)))
2433 XMoveResizeWindow(display
, win
->id
,
2434 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2441 configurenotify(XEvent
*e
)
2446 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
2447 e
->xconfigure
.window
);
2449 win
= find_window(e
->xconfigure
.window
);
2450 XMapWindow(display
, win
->id
);
2451 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2453 XMapWindow(display
, win
->id
);
2459 destroynotify(XEvent
*e
)
2462 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2464 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
2466 if ((win
= find_window(ev
->window
)) != NULL
) {
2467 unmanage_window(win
);
2473 enternotify(XEvent
*e
)
2475 XCrossingEvent
*ev
= &e
->xcrossing
;
2478 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
2481 /* eat event(r) to prevent autofocus */
2486 if ((win
= find_window(ev
->window
)) != NULL
)
2493 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
2497 mappingnotify(XEvent
*e
)
2499 XMappingEvent
*ev
= &e
->xmapping
;
2501 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
2503 XRefreshKeyboardMapping(ev
);
2504 if (ev
->request
== MappingKeyboard
)
2509 maprequest(XEvent
*e
)
2511 XMapRequestEvent
*ev
= &e
->xmaprequest
;
2512 XWindowAttributes wa
;
2514 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
2515 e
->xmaprequest
.window
);
2517 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
2519 if (wa
.override_redirect
)
2521 manage_window(e
->xmaprequest
.window
);
2527 propertynotify(XEvent
*e
)
2530 XPropertyEvent
*ev
= &e
->xproperty
;
2532 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
2535 if (ev
->state
== PropertyDelete
)
2536 return; /* ignore */
2537 win
= find_window(ev
->window
);
2542 case XA_WM_NORMAL_HINTS
:
2545 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
2546 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
2547 if (win
->sh
.flags
& PMinSize
) {
2548 win
->g
.w
= win
->sh
.min_width
;
2549 win
->g
.h
= win
->sh
.min_height
;
2550 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
2552 XMoveResizeWindow(display
, win
->id
,
2553 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
2562 unmapnotify(XEvent
*e
)
2564 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
2567 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
2569 if ((win
= find_window(ev
->window
)) != NULL
)
2571 unmanage_window(win
);
2575 visibilitynotify(XEvent
*e
)
2578 struct swm_region
*r
;
2580 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
2581 e
->xvisibility
.window
);
2582 if (e
->xvisibility
.state
== VisibilityUnobscured
)
2583 for (i
= 0; i
< ScreenCount(display
); i
++)
2584 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2585 if (e
->xvisibility
.window
== r
->bar_window
)
2590 xerror_start(Display
*d
, XErrorEvent
*ee
)
2597 xerror(Display
*d
, XErrorEvent
*ee
)
2599 /* fprintf(stderr, "error: %p %p\n", display, ee); */
2607 xerrorxlib
= XSetErrorHandler(xerror_start
);
2609 /* this causes an error if some other window manager is running */
2610 XSelectInput(display
, DefaultRootWindow(display
),
2611 SubstructureRedirectMask
);
2612 XSync(display
, False
);
2616 XSetErrorHandler(xerror
);
2617 XSync(display
, False
);
2626 unsigned char *p
= NULL
;
2627 unsigned long n
, extra
;
2630 astate
= XInternAtom(display
, "WM_STATE", False
);
2631 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
2632 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
2633 if (status
!= Success
)
2642 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
2644 struct swm_region
*r
, *n
;
2645 struct workspace
*ws
= NULL
;
2648 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
2649 s
->idx
, w
, h
, x
, y
);
2651 /* remove any conflicting regions */
2652 n
= TAILQ_FIRST(&s
->rl
);
2655 n
= TAILQ_NEXT(r
, entry
);
2656 if (X(r
) < (x
+ w
) &&
2657 (X(r
) + WIDTH(r
)) > x
&&
2659 (Y(r
) + HEIGHT(r
)) > y
) {
2660 XDestroyWindow(display
, r
->bar_window
);
2661 TAILQ_REMOVE(&s
->rl
, r
, entry
);
2662 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
2666 /* search old regions for one to reuse */
2668 /* size + location match */
2669 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2670 if (X(r
) == x
&& Y(r
) == y
&&
2671 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2675 TAILQ_FOREACH(r
, &s
->orl
, entry
)
2676 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
2680 TAILQ_REMOVE(&s
->orl
, r
, entry
);
2681 /* try to use old region's workspace */
2682 if (r
->ws
->r
== NULL
)
2685 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
2686 errx(1, "calloc: failed to allocate memory for screen");
2688 /* if we don't have a workspace already, find one */
2690 for (i
= 0; i
< SWM_WS_MAX
; i
++)
2691 if (s
->ws
[i
].r
== NULL
) {
2698 errx(1, "no free workspaces\n");
2707 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
2713 #ifdef SWM_XRR_HAS_CRTC
2715 XRRScreenResources
*sr
;
2718 #endif /* SWM_XRR_HAS_CRTC */
2719 struct swm_region
*r
;
2722 if (i
>= ScreenCount(display
))
2723 errx(1, "invalid screen");
2725 /* remove any old regions */
2726 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
2728 XDestroyWindow(display
, r
->bar_window
);
2729 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
2730 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
2733 /* map virtual screens onto physical screens */
2734 #ifdef SWM_XRR_HAS_CRTC
2735 if (xrandr_support
) {
2736 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
2738 new_region(&screens
[i
], 0, 0,
2739 DisplayWidth(display
, i
),
2740 DisplayHeight(display
, i
));
2744 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
2745 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
2746 if (ci
->noutput
== 0)
2749 if (ci
!= NULL
&& ci
->mode
== None
)
2750 new_region(&screens
[i
], 0, 0,
2751 DisplayWidth(display
, i
),
2752 DisplayHeight(display
, i
));
2754 new_region(&screens
[i
],
2755 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
2758 XRRFreeCrtcInfo(ci
);
2759 XRRFreeScreenResources(sr
);
2761 #endif /* SWM_XRR_HAS_CRTC */
2763 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
2764 DisplayHeight(display
, i
));
2769 screenchange(XEvent
*e
) {
2770 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
2771 struct swm_region
*r
;
2775 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
2777 if (!XRRUpdateConfiguration(e
))
2780 /* silly event doesn't include the screen index */
2781 for (i
= 0; i
< ScreenCount(display
); i
++)
2782 if (screens
[i
].root
== xe
->root
)
2784 if (i
>= ScreenCount(display
))
2785 errx(1, "screenchange: screen not found\n");
2787 /* brute force for now, just re-enumerate the regions */
2790 /* hide any windows that went away */
2791 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2792 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
2793 XUnmapWindow(display
, win
->id
);
2800 Window d1
, d2
, *wins
= NULL
;
2801 XWindowAttributes wa
;
2804 int errorbase
, major
, minor
;
2805 struct workspace
*ws
;
2809 if ((screens
= calloc(ScreenCount(display
),
2810 sizeof(struct swm_screen
))) == NULL
)
2811 errx(1, "calloc: screens");
2813 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
2815 /* initial Xrandr setup */
2816 xrandr_support
= XRRQueryExtension(display
,
2817 &xrandr_eventbase
, &errorbase
);
2819 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
2822 /* map physical screens */
2823 for (i
= 0; i
< ScreenCount(display
); i
++) {
2824 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
2826 TAILQ_INIT(&screens
[i
].rl
);
2827 TAILQ_INIT(&screens
[i
].orl
);
2828 screens
[i
].root
= RootWindow(display
, i
);
2830 /* set default colors */
2831 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
2832 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
2833 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
2834 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
2835 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
2837 /* init all workspaces */
2838 /* XXX these should be dynamically allocated too */
2839 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
2840 ws
= &screens
[i
].ws
[j
];
2845 TAILQ_INIT(&ws
->winlist
);
2847 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
2848 if (layouts
[k
].l_config
!= NULL
)
2849 layouts
[k
].l_config(ws
,
2850 SWM_ARG_ID_STACKINIT
);
2851 ws
->cur_layout
= &layouts
[0];
2853 /* grab existing windows (before we build the bars)*/
2854 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
2860 XRRSelectInput(display
, screens
[i
].root
,
2861 RRScreenChangeNotifyMask
);
2863 /* attach windows to a region */
2864 /* normal windows */
2865 for (j
= 0; j
< no
; j
++) {
2866 XGetWindowAttributes(display
, wins
[j
], &wa
);
2867 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
2868 wa
.override_redirect
||
2869 XGetTransientForHint(display
, wins
[j
], &d1
))
2872 if (wa
.map_state
== IsViewable
||
2873 getstate(wins
[j
]) == NormalState
)
2874 manage_window(wins
[j
]);
2876 /* transient windows */
2877 for (j
= 0; j
< no
; j
++) {
2878 if (!XGetWindowAttributes(display
, wins
[j
], &wa
))
2881 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
2882 (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) ==
2884 manage_window(wins
[j
]);
2894 main(int argc
, char *argv
[])
2897 struct swm_region
*r
;
2898 char conf
[PATH_MAX
], *cfile
= NULL
;
2905 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
2906 SWM_VERSION
, cvstag
);
2907 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2908 warnx("no locale support");
2910 if (!(display
= XOpenDisplay(0)))
2911 errx(1, "can not open display");
2914 errx(1, "other wm running");
2916 astate
= XInternAtom(display
, "WM_STATE", False
);
2917 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
2918 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
2920 /* look for local and global conf file */
2921 pwd
= getpwuid(getuid());
2923 errx(1, "invalid user %d", getuid());
2927 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
2928 if (stat(conf
, &sb
) != -1) {
2929 if (S_ISREG(sb
.st_mode
))
2932 /* try global conf file */
2933 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
2934 if (!stat(conf
, &sb
))
2935 if (S_ISREG(sb
.st_mode
))
2941 /* setup all bars */
2942 for (i
= 0; i
< ScreenCount(display
); i
++)
2943 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
2946 /* ws[0].focus = TAILQ_FIRST(&ws[0].winlist); */
2951 xfd
= ConnectionNumber(display
);
2955 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
2957 errx(1, "select failed");
2962 while (XPending(display
)) {
2963 XNextEvent(display
, &e
);
2964 if (e
.type
< LASTEvent
) {
2965 if (handler
[e
.type
])
2966 handler
[e
.type
](&e
);
2968 DNPRINTF(SWM_D_EVENT
,
2969 "win: %lu unknown event: %d\n",
2970 e
.xany
.window
, e
.type
);
2972 switch (e
.type
- xrandr_eventbase
) {
2973 case RRScreenChangeNotify
:
2977 DNPRINTF(SWM_D_EVENT
,
2978 "win: %lu unknown xrandr event: "
2979 "%d\n", e
.xany
.window
, e
.type
);
2986 XCloseDisplay(display
);