3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
5 * Copyright (c) 2009 Darrin Chandler <dwchandler@stilyagin.com>
6 * Copyright (c) 2009 Pierre-Yves Ritschard <pyr@spootnik.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Much code and ideas taken from dwm under the following license:
22 * MIT/X Consortium License
24 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
25 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
26 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
27 * 2007 Premysl Hruby <dfenze at gmail dot com>
28 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
29 * 2007 Christof Musik <christof at sendfax dot de>
30 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
31 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
32 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
41 * The above copyright notice and this permission notice shall be included in
42 * all copies or substantial portions of the Software.
44 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
47 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
49 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50 * DEALINGS IN THE SOFTWARE.
53 static const char *cvstag
= "$scrotwm$";
55 #define SWM_VERSION "0.9.7"
71 #include <sys/types.h>
75 #include <sys/queue.h>
76 #include <sys/param.h>
77 #include <sys/select.h>
79 #include <X11/cursorfont.h>
80 #include <X11/keysym.h>
81 #include <X11/Xatom.h>
83 #include <X11/Xproto.h>
84 #include <X11/Xutil.h>
85 #include <X11/extensions/Xrandr.h>
88 # error XRandR versions less than 1.0 are not supported
93 #define SWM_XRR_HAS_CRTC
97 /* #define SWM_DEBUG */
99 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
100 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
101 #define SWM_D_MISC 0x0001
102 #define SWM_D_EVENT 0x0002
103 #define SWM_D_WS 0x0004
104 #define SWM_D_FOCUS 0x0008
105 #define SWM_D_MOVE 0x0010
106 #define SWM_D_STACK 0x0020
107 #define SWM_D_MOUSE 0x0040
108 #define SWM_D_PROP 0x0080
109 #define SWM_D_CLASS 0x0100
110 #define SWM_D_KEY 0x0200
111 #define SWM_D_QUIRK 0x0400
112 #define SWM_D_SPAWN 0x0800
114 u_int32_t swm_debug
= 0
129 #define DPRINTF(x...)
130 #define DNPRINTF(n,x...)
133 #define LENGTH(x) (sizeof x / sizeof x[0])
134 #define MODKEY Mod1Mask
135 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
136 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
137 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
138 #define SWM_PROPLEN (16)
139 #define SWM_FUNCNAME_LEN (32)
140 #define SWM_KEYS_LEN (255)
141 #define SWM_QUIRK_LEN (64)
142 #define X(r) (r)->g.x
143 #define Y(r) (r)->g.y
144 #define WIDTH(r) (r)->g.w
145 #define HEIGHT(r) (r)->g.h
146 #define SWM_MAX_FONT_STEPS (3)
149 #define SWM_LIB "/usr/local/lib/libswmhack.so.0.0"
156 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
161 int xrandr_eventbase
;
162 int ignore_enter
= 0;
163 unsigned int numlockmask
= 0;
167 int cycle_visible
= 0;
169 int font_adjusted
= 0;
170 unsigned int mod_key
= MODKEY
;
173 double dialog_ratio
= .6;
175 #define SWM_BAR_MAX (256)
176 char *bar_argv
[] = { NULL
, NULL
};
178 char bar_ext
[SWM_BAR_MAX
];
179 char bar_vertext
[SWM_BAR_MAX
];
181 sig_atomic_t bar_alarm
= 0;
185 int bar_extra_running
= 0;
188 int clock_enabled
= 1;
189 int title_name_enabled
= 0;
190 int title_class_enabled
= 0;
196 char *bar_fonts
[] = { NULL
, NULL
, NULL
}; /* XXX Make fully dynamic */
197 char *spawn_term
[] = { NULL
, NULL
}; /* XXX Make fully dynamic */
199 #define SWM_MENU_FN (2)
200 #define SWM_MENU_NB (4)
201 #define SWM_MENU_NF (6)
202 #define SWM_MENU_SB (8)
203 #define SWM_MENU_SF (10)
205 /* layout manager data */
206 struct swm_geometry
{
216 /* virtual "screens" */
218 TAILQ_ENTRY(swm_region
) entry
;
219 struct swm_geometry g
;
220 struct workspace
*ws
; /* current workspace on this region */
221 struct swm_screen
*s
; /* screen idx */
224 TAILQ_HEAD(swm_region_list
, swm_region
);
227 TAILQ_ENTRY(ws_win
) entry
;
229 struct swm_geometry g
;
234 int font_size_boundary
[SWM_MAX_FONT_STEPS
];
238 unsigned long quirks
;
239 struct workspace
*ws
; /* always valid */
240 struct swm_screen
*s
; /* always valid, never changes */
241 XWindowAttributes wa
;
245 TAILQ_HEAD(ws_win_list
, ws_win
);
247 /* user/key callable function IDs */
304 /* layout handlers */
306 void vertical_config(struct workspace
*, int);
307 void vertical_stack(struct workspace
*, struct swm_geometry
*);
308 void horizontal_config(struct workspace
*, int);
309 void horizontal_stack(struct workspace
*, struct swm_geometry
*);
310 void max_stack(struct workspace
*, struct swm_geometry
*);
312 void grabbuttons(struct ws_win
*, int);
313 void new_region(struct swm_screen
*, int, int, int, int);
316 void (*l_stack
)(struct workspace
*, struct swm_geometry
*);
317 void (*l_config
)(struct workspace
*, int);
319 /* stack, configure */
320 { vertical_stack
, vertical_config
},
321 { horizontal_stack
, horizontal_config
},
326 #define SWM_H_SLICE (32)
327 #define SWM_V_SLICE (32)
329 /* define work spaces */
331 int idx
; /* workspace index */
332 int restack
; /* restack on switch */
333 struct layout
*cur_layout
; /* current layout handlers */
334 struct ws_win
*focus
; /* may be NULL */
335 struct ws_win
*focus_prev
; /* may be NULL */
336 struct swm_region
*r
; /* may be NULL */
337 struct ws_win_list winlist
; /* list of windows in ws */
341 int horizontal_msize
;
343 int horizontal_stacks
;
350 enum { SWM_S_COLOR_BAR
, SWM_S_COLOR_BAR_BORDER
, SWM_S_COLOR_BAR_FONT
,
351 SWM_S_COLOR_FOCUS
, SWM_S_COLOR_UNFOCUS
, SWM_S_COLOR_MAX
};
353 /* physical screen mapping */
354 #define SWM_WS_MAX (10) /* XXX Too small? */
356 int idx
; /* screen index */
357 struct swm_region_list rl
; /* list of regions on this screen */
358 struct swm_region_list orl
; /* list of old regions */
360 struct workspace ws
[SWM_WS_MAX
];
366 } c
[SWM_S_COLOR_MAX
];
368 struct swm_screen
*screens
;
371 struct ws_win
*cur_focus
= NULL
;
373 /* args to functions */
376 #define SWM_ARG_ID_FOCUSNEXT (0)
377 #define SWM_ARG_ID_FOCUSPREV (1)
378 #define SWM_ARG_ID_FOCUSMAIN (2)
379 #define SWM_ARG_ID_SWAPNEXT (3)
380 #define SWM_ARG_ID_SWAPPREV (4)
381 #define SWM_ARG_ID_SWAPMAIN (5)
382 #define SWM_ARG_ID_MASTERSHRINK (6)
383 #define SWM_ARG_ID_MASTERGROW (7)
384 #define SWM_ARG_ID_MASTERADD (8)
385 #define SWM_ARG_ID_MASTERDEL (9)
386 #define SWM_ARG_ID_STACKRESET (10)
387 #define SWM_ARG_ID_STACKINIT (11)
388 #define SWM_ARG_ID_CYCLEWS_UP (12)
389 #define SWM_ARG_ID_CYCLEWS_DOWN (13)
390 #define SWM_ARG_ID_CYCLESC_UP (14)
391 #define SWM_ARG_ID_CYCLESC_DOWN (15)
392 #define SWM_ARG_ID_STACKINC (16)
393 #define SWM_ARG_ID_STACKDEC (17)
394 #define SWM_ARG_ID_SS_ALL (0)
395 #define SWM_ARG_ID_SS_WINDOW (1)
396 #define SWM_ARG_ID_DONTCENTER (0)
397 #define SWM_ARG_ID_CENTER (1)
398 #define SWM_ARG_ID_KILLWINDOW (0)
399 #define SWM_ARG_ID_DELETEWINDOW (1)
408 #define SWM_Q_FLOAT (1<<0) /* float this window */
409 #define SWM_Q_TRANSSZ (1<<1) /* transiend window size too small */
410 #define SWM_Q_ANYWHERE (1<<2) /* don't position this window */
411 #define SWM_Q_XTERM_FONTADJ (1<<3) /* adjust xterm fonts when resizing */
412 #define SWM_Q_FULLSCREEN (1<<4) /* remove border */
414 int quirks_size
= 0, quirks_length
= 0;
415 struct quirk
*quirks
= NULL
;
418 void expose(XEvent
*);
419 void keypress(XEvent
*);
420 void buttonpress(XEvent
*);
421 void configurerequest(XEvent
*);
422 void configurenotify(XEvent
*);
423 void destroynotify(XEvent
*);
424 void enternotify(XEvent
*);
425 void focusin(XEvent
*);
426 void focusout(XEvent
*);
427 void mappingnotify(XEvent
*);
428 void maprequest(XEvent
*);
429 void propertynotify(XEvent
*);
430 void unmapnotify(XEvent
*);
431 void visibilitynotify(XEvent
*);
433 void (*handler
[LASTEvent
])(XEvent
*) = {
435 [KeyPress
] = keypress
,
436 [ButtonPress
] = buttonpress
,
437 [ConfigureRequest
] = configurerequest
,
438 [ConfigureNotify
] = configurenotify
,
439 [DestroyNotify
] = destroynotify
,
440 [EnterNotify
] = enternotify
,
442 [FocusOut
] = focusout
,
443 [MappingNotify
] = mappingnotify
,
444 [MapRequest
] = maprequest
,
445 [PropertyNotify
] = propertynotify
,
446 [UnmapNotify
] = unmapnotify
,
447 [VisibilityNotify
] = visibilitynotify
,
451 name_to_color(char *colorname
)
455 XColor screen_def
, exact_def
;
456 unsigned long result
= 0;
457 char cname
[32] = "#";
459 cmap
= DefaultColormap(display
, screens
[0].idx
);
460 status
= XAllocNamedColor(display
, cmap
, colorname
,
461 &screen_def
, &exact_def
);
463 strlcat(cname
, colorname
+ 2, sizeof cname
- 1);
464 status
= XAllocNamedColor(display
, cmap
, cname
, &screen_def
,
468 result
= screen_def
.pixel
;
470 fprintf(stderr
, "color '%s' not found.\n", colorname
);
476 setscreencolor(char *val
, int i
, int c
)
478 if (i
> 0 && i
<= ScreenCount(display
)) {
479 screens
[i
- 1].c
[c
].color
= name_to_color(val
);
480 free(screens
[i
- 1].c
[c
].name
);
481 if ((screens
[i
- 1].c
[c
].name
= strdup(val
)) == NULL
)
483 } else if (i
== -1) {
484 for (i
= 0; i
< ScreenCount(display
); i
++) {
485 screens
[i
].c
[c
].color
= name_to_color(val
);
486 free(screens
[i
].c
[c
].name
);
487 if ((screens
[i
].c
[c
].name
= strdup(val
)) == NULL
)
491 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
492 i
, ScreenCount(display
));
496 custom_region(char *val
)
498 unsigned int sidx
, x
, y
, w
, h
;
500 if (sscanf(val
, "screen[%u]:%ux%u+%u+%u", &sidx
, &w
, &h
, &x
, &y
) != 5)
501 errx(1, "invalid custom region, "
502 "should be 'screen[<n>]:<n>x<n>+<n>+<n>\n");
503 if (sidx
< 1 || sidx
> ScreenCount(display
))
504 errx(1, "invalid screen index: %d out of bounds (maximum %d)\n",
505 sidx
, ScreenCount(display
));
509 errx(1, "region %ux%u+%u+%u too small\n", w
, h
, x
, y
);
511 if (x
< 0 || x
> DisplayWidth(display
, sidx
) ||
512 y
< 0 || y
> DisplayHeight(display
, sidx
) ||
513 w
+ x
> DisplayWidth(display
, sidx
) ||
514 h
+ y
> DisplayHeight(display
, sidx
))
515 errx(1, "region %ux%u+%u+%u not within screen boundaries "
516 "(%ux%u)\n", w
, h
, x
, y
,
517 DisplayWidth(display
, sidx
), DisplayHeight(display
, sidx
));
519 new_region(&screens
[sidx
], x
, y
, w
, h
);
523 socket_setnonblock(int fd
)
527 if ((flags
= fcntl(fd
, F_GETFL
, 0)) == -1)
528 err(1, "fcntl F_GETFL");
530 if ((flags
= fcntl(fd
, F_SETFL
, flags
)) == -1)
531 err(1, "fcntl F_SETFL");
535 bar_print(struct swm_region
*r
, char *s
)
537 XClearWindow(display
, r
->bar_window
);
538 XSetForeground(display
, bar_gc
, r
->s
->c
[SWM_S_COLOR_BAR_FONT
].color
);
539 XDrawString(display
, r
->bar_window
, bar_gc
, 4, bar_fs
->ascent
, s
,
548 bzero(bar_pipe
, sizeof bar_pipe
);
551 kill(bar_pid
, SIGTERM
);
554 strlcpy(bar_ext
, "", sizeof bar_ext
);
563 struct swm_region
*r
;
564 int i
, x
, do_class
, do_name
;
567 char loc
[SWM_BAR_MAX
];
572 if (bar_enabled
== 0)
574 if (bar_extra
&& bar_extra_running
) {
575 /* ignore short reads; it'll correct itself */
576 while ((b
= fgetln(stdin
, &len
)) != NULL
)
577 if (b
&& b
[len
- 1] == '\n') {
579 strlcpy(bar_ext
, b
, sizeof bar_ext
);
581 if (b
== NULL
&& errno
!= EAGAIN
) {
582 fprintf(stderr
, "bar_extra failed: errno: %d %s\n",
583 errno
, strerror(errno
));
587 strlcpy(bar_ext
, "", sizeof bar_ext
);
589 if (clock_enabled
== 0)
590 strlcpy(s
, "", sizeof s
);
593 localtime_r(&tmt
, &tm
);
594 strftime(s
, sizeof s
, "%a %b %d %R %Z %Y ", &tm
);
597 if ((title_name_enabled
== 1 || title_class_enabled
== 1) &&
599 if ((xch
= XAllocClassHint()) == NULL
)
601 status
= XGetClassHint(display
, cur_focus
->id
, xch
);
602 if (status
== BadWindow
|| status
== BadAlloc
)
604 do_class
= (title_class_enabled
&& xch
->res_class
!= NULL
);
605 do_name
= (title_name_enabled
&& xch
->res_name
!= NULL
);
607 strlcat(s
, xch
->res_class
, sizeof s
);
608 if (do_class
&& do_name
)
609 strlcat(s
, ":", sizeof s
);
611 strlcat(s
, xch
->res_name
, sizeof s
);
616 for (i
= 0; i
< ScreenCount(display
); i
++) {
618 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
619 snprintf(loc
, sizeof loc
, "%d:%d %s %s %s",
620 x
++, r
->ws
->idx
+ 1, s
, bar_ext
, bar_vertext
);
624 XSync(display
, False
);
635 bar_toggle(struct swm_region
*r
, union arg
*args
)
637 struct swm_region
*tmpr
;
638 int i
, j
, sc
= ScreenCount(display
);
640 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
643 for (i
= 0; i
< sc
; i
++)
644 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
645 XUnmapWindow(display
, tmpr
->bar_window
);
647 for (i
= 0; i
< sc
; i
++)
648 TAILQ_FOREACH(tmpr
, &screens
[i
].rl
, entry
)
649 XMapRaised(display
, tmpr
->bar_window
);
651 bar_enabled
= !bar_enabled
;
652 for (i
= 0; i
< sc
; i
++)
653 for (j
= 0; j
< SWM_WS_MAX
; j
++)
654 screens
[i
].ws
[j
].restack
= 1;
657 /* must be after stack */
664 XSetWindowAttributes wa
;
665 struct swm_region
*r
;
668 /* do this here because the conf file is in memory */
669 if (bar_extra
&& bar_extra_running
== 0 && bar_argv
[0]) {
670 /* launch external status app */
671 bar_extra_running
= 1;
672 if (pipe(bar_pipe
) == -1)
673 err(1, "pipe error");
674 socket_setnonblock(bar_pipe
[0]);
675 socket_setnonblock(bar_pipe
[1]); /* XXX hmmm, really? */
676 if (dup2(bar_pipe
[0], 0) == -1)
678 if (dup2(bar_pipe
[1], 1) == -1)
680 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
681 err(1, "could not disable SIGPIPE");
682 switch (bar_pid
= fork()) {
684 err(1, "cannot fork");
688 execvp(bar_argv
[0], bar_argv
);
689 err(1, "%s external app failed", bar_argv
[0]);
691 default: /* parent */
697 bzero(&wa
, sizeof wa
);
698 for (i
= 0; i
< ScreenCount(display
); i
++)
699 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
701 screens
[i
].c
[SWM_S_COLOR_BAR_BORDER
].color
;
702 wa
.background_pixel
=
703 screens
[i
].c
[SWM_S_COLOR_BAR
].color
;
704 XChangeWindowAttributes(display
, r
->bar_window
,
705 CWBackPixel
| CWBorderPixel
, &wa
);
711 bar_setup(struct swm_region
*r
)
715 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
716 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
722 if (bar_fonts
[i
] == NULL
)
723 errx(1, "couldn't load font");
724 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
726 r
->bar_window
= XCreateSimpleWindow(display
,
727 r
->s
->root
, X(r
), Y(r
), WIDTH(r
) - 2, bar_height
- 2,
728 1, r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].color
,
729 r
->s
->c
[SWM_S_COLOR_BAR
].color
);
730 bar_gc
= XCreateGC(display
, r
->bar_window
, 0, &bar_gcv
);
731 XSetFont(display
, bar_gc
, bar_fs
->fid
);
732 XSelectInput(display
, r
->bar_window
, VisibilityChangeMask
);
734 XMapRaised(display
, r
->bar_window
);
735 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %lu\n", r
->bar_window
);
737 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
738 err(1, "could not install bar_signal");
743 version(struct swm_region
*r
, union arg
*args
)
745 bar_version
= !bar_version
;
747 snprintf(bar_vertext
, sizeof bar_vertext
, "Version: %s CVS: %s",
748 SWM_VERSION
, cvstag
);
750 strlcpy(bar_vertext
, "", sizeof bar_vertext
);
755 client_msg(struct ws_win
*win
, Atom a
)
757 XClientMessageEvent cm
;
759 bzero(&cm
, sizeof cm
);
760 cm
.type
= ClientMessage
;
762 cm
.message_type
= aprot
;
765 cm
.data
.l
[1] = CurrentTime
;
766 XSendEvent(display
, win
->id
, False
, 0L, (XEvent
*)&cm
);
770 config_win(struct ws_win
*win
)
774 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
775 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
776 ce
.type
= ConfigureNotify
;
777 ce
.display
= display
;
783 ce
.height
= win
->g
.h
;
784 ce
.border_width
= 1; /* XXX store this! */
786 ce
.override_redirect
= False
;
787 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
791 count_win(struct workspace
*ws
, int count_transient
)
796 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
797 if (count_transient
== 0 && win
->floating
)
799 if (count_transient
== 0 && win
->transient
)
803 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
809 quit(struct swm_region
*r
, union arg
*args
)
811 DNPRINTF(SWM_D_MISC
, "quit\n");
821 for (i
= 0; i
< ScreenCount(display
); i
++)
822 for (j
= 0; j
< SWM_WS_MAX
; j
++)
823 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
824 XUnmapWindow(display
, win
->id
);
828 fake_keypress(struct ws_win
*win
, int keysym
, int modifiers
)
832 event
.display
= display
; /* Ignored, but what the hell */
833 event
.window
= win
->id
;
834 event
.root
= win
->s
->root
;
835 event
.subwindow
= None
;
836 event
.time
= CurrentTime
;
841 event
.same_screen
= True
;
842 event
.keycode
= XKeysymToKeycode(display
, keysym
);
843 event
.state
= modifiers
;
845 event
.type
= KeyPress
;
846 XSendEvent(event
.display
, event
.window
, True
,
847 KeyPressMask
, (XEvent
*)&event
);
849 event
.type
= KeyRelease
;
850 XSendEvent(event
.display
, event
.window
, True
,
851 KeyPressMask
, (XEvent
*)&event
);
856 restart(struct swm_region
*r
, union arg
*args
)
858 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
860 /* disable alarm because the following code may not be interrupted */
862 if (signal(SIGALRM
, SIG_IGN
) == SIG_ERR
)
863 errx(1, "can't disable alarm");
868 XCloseDisplay(display
);
869 execvp(start_argv
[0], start_argv
);
870 fprintf(stderr
, "execvp failed\n");
876 root_to_region(Window root
)
878 struct swm_region
*r
= NULL
;
883 for (i
= 0; i
< ScreenCount(display
); i
++)
884 if (screens
[i
].root
== root
)
887 if (XQueryPointer(display
, screens
[i
].root
,
888 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
) {
889 /* choose a region based on pointer location */
890 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
891 if (x
>= X(r
) && x
<= X(r
) + WIDTH(r
) &&
892 y
>= Y(r
) && y
<= Y(r
) + HEIGHT(r
))
897 r
= TAILQ_FIRST(&screens
[i
].rl
);
903 find_window(Window id
)
908 for (i
= 0; i
< ScreenCount(display
); i
++)
909 for (j
= 0; j
< SWM_WS_MAX
; j
++)
910 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
917 spawn(struct swm_region
*r
, union arg
*args
)
922 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
924 * The double-fork construct avoids zombie processes and keeps the code
925 * clean from stupid signal handlers.
930 close(ConnectionNumber(display
));
931 setenv("LD_PRELOAD", SWM_LIB
, 1);
932 if (asprintf(&ret
, "%d", r
->ws
->idx
)) {
933 setenv("_SWM_WS", ret
, 1);
936 if (asprintf(&ret
, "%d", getpid())) {
937 setenv("_SWM_PID", ret
, 1);
941 /* kill stdin, mplayer, ssh-add etc. need that */
942 si
= open("/dev/null", O_RDONLY
, 0);
944 err(1, "open /dev/null");
945 if (dup2(si
, 0) == -1)
946 err(1, "dup2 /dev/null");
947 execvp(args
->argv
[0], args
->argv
);
948 fprintf(stderr
, "execvp failed\n");
957 spawnterm(struct swm_region
*r
, union arg
*args
)
959 DNPRINTF(SWM_D_MISC
, "spawnterm\n");
962 setenv("_SWM_XTERM_FONTADJ", "", 1);
967 unfocus_win(struct ws_win
*win
)
972 if (win
->ws
->focus
!= win
&& win
->ws
->focus
!= NULL
)
973 win
->ws
->focus_prev
= win
->ws
->focus
;
975 if (win
->ws
->r
== NULL
)
979 XSetWindowBorder(display
, win
->id
,
980 win
->ws
->r
->s
->c
[SWM_S_COLOR_UNFOCUS
].color
);
982 if (win
->ws
->focus
== win
)
983 win
->ws
->focus
= NULL
;
984 if (cur_focus
== win
)
994 DNPRINTF(SWM_D_FOCUS
, "unfocus_all:\n");
996 for (i
= 0; i
< ScreenCount(display
); i
++)
997 for (j
= 0; j
< SWM_WS_MAX
; j
++)
998 TAILQ_FOREACH(win
, &screens
[i
].ws
[j
].winlist
, entry
)
1003 focus_win(struct ws_win
*win
)
1005 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
? win
->id
: 0);
1011 unfocus_win(cur_focus
);
1012 if (win
->ws
->focus
) {
1013 /* probably shouldn't happen due to the previous unfocus_win */
1014 DNPRINTF(SWM_D_FOCUS
, "unfocusing win->ws->focus: %lu\n",
1015 win
->ws
->focus
->id
);
1016 unfocus_win(win
->ws
->focus
);
1018 win
->ws
->focus
= win
;
1019 if (win
->ws
->r
!= NULL
) {
1021 if (!win
->got_focus
) {
1022 XSetWindowBorder(display
, win
->id
,
1023 win
->ws
->r
->s
->c
[SWM_S_COLOR_FOCUS
].color
);
1024 grabbuttons(win
, 1);
1027 XSetInputFocus(display
, win
->id
,
1028 RevertToPointerRoot
, CurrentTime
);
1033 switchws(struct swm_region
*r
, union arg
*args
)
1035 int wsid
= args
->id
;
1036 struct swm_region
*this_r
, *other_r
;
1038 struct workspace
*new_ws
, *old_ws
;
1041 old_ws
= this_r
->ws
;
1042 new_ws
= &this_r
->s
->ws
[wsid
];
1044 DNPRINTF(SWM_D_WS
, "switchws screen[%d]:%dx%d+%d+%d: "
1045 "%d -> %d\n", r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
),
1048 if (new_ws
== old_ws
)
1051 other_r
= new_ws
->r
;
1053 /* if the other workspace is hidden, switch windows */
1054 /* map new window first to prevent ugly blinking */
1056 old_ws
->restack
= 1;
1058 TAILQ_FOREACH(win
, &new_ws
->winlist
, entry
)
1059 XMapRaised(display
, win
->id
);
1061 TAILQ_FOREACH(win
, &old_ws
->winlist
, entry
)
1062 XUnmapWindow(display
, win
->id
);
1064 other_r
->ws
= old_ws
;
1065 old_ws
->r
= other_r
;
1067 this_r
->ws
= new_ws
;
1072 if (new_ws
->focus
== NULL
)
1073 new_ws
->focus
= TAILQ_FIRST(&new_ws
->winlist
);
1075 focus_win(new_ws
->focus
);
1081 cyclews(struct swm_region
*r
, union arg
*args
)
1084 struct swm_screen
*s
= r
->s
;
1086 DNPRINTF(SWM_D_WS
, "cyclews id %d "
1087 "in screen[%d]:%dx%d+%d+%d ws %d\n", args
->id
,
1088 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1093 case SWM_ARG_ID_CYCLEWS_UP
:
1094 if (a
.id
< SWM_WS_MAX
- 1)
1099 case SWM_ARG_ID_CYCLEWS_DOWN
:
1103 a
.id
= SWM_WS_MAX
- 1;
1109 if (cycle_empty
== 0 && TAILQ_EMPTY(&s
->ws
[a
.id
].winlist
))
1111 if (cycle_visible
== 0 && s
->ws
[a
.id
].r
!= NULL
)
1115 } while (a
.id
!= r
->ws
->idx
);
1119 cyclescr(struct swm_region
*r
, union arg
*args
)
1121 struct swm_region
*rr
;
1126 case SWM_ARG_ID_CYCLESC_UP
:
1127 rr
= TAILQ_NEXT(r
, entry
);
1129 rr
= TAILQ_FIRST(&screens
[i
].rl
);
1131 case SWM_ARG_ID_CYCLESC_DOWN
:
1132 rr
= TAILQ_PREV(r
, swm_region_list
, entry
);
1134 rr
= TAILQ_LAST(&screens
[i
].rl
, swm_region_list
);
1140 XSetInputFocus(display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
1141 XWarpPointer(display
, None
, rr
->s
[i
].root
, 0, 0, 0, 0, rr
->g
.x
,
1142 rr
->g
.y
+ bar_enabled
? bar_height
: 0);
1146 swapwin(struct swm_region
*r
, union arg
*args
)
1148 struct ws_win
*target
, *source
;
1149 struct ws_win_list
*wl
;
1152 DNPRINTF(SWM_D_WS
, "swapwin id %d "
1153 "in screen %d region %dx%d+%d+%d ws %d\n", args
->id
,
1154 r
->s
->idx
, WIDTH(r
), HEIGHT(r
), X(r
), Y(r
), r
->ws
->idx
);
1155 if (cur_focus
== NULL
)
1159 wl
= &source
->ws
->winlist
;
1162 case SWM_ARG_ID_SWAPPREV
:
1163 target
= TAILQ_PREV(source
, ws_win_list
, entry
);
1164 TAILQ_REMOVE(wl
, cur_focus
, entry
);
1166 TAILQ_INSERT_TAIL(wl
, source
, entry
);
1168 TAILQ_INSERT_BEFORE(target
, source
, entry
);
1170 case SWM_ARG_ID_SWAPNEXT
:
1171 target
= TAILQ_NEXT(source
, entry
);
1172 TAILQ_REMOVE(wl
, source
, entry
);
1174 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1176 TAILQ_INSERT_AFTER(wl
, target
, source
, entry
);
1178 case SWM_ARG_ID_SWAPMAIN
:
1179 target
= TAILQ_FIRST(wl
);
1180 if (target
== source
) {
1181 if (source
->ws
->focus_prev
!= NULL
&&
1182 source
->ws
->focus_prev
!= target
)
1184 source
= source
->ws
->focus_prev
;
1188 source
->ws
->focus_prev
= target
;
1189 TAILQ_REMOVE(wl
, target
, entry
);
1190 TAILQ_INSERT_BEFORE(source
, target
, entry
);
1191 TAILQ_REMOVE(wl
, source
, entry
);
1192 TAILQ_INSERT_HEAD(wl
, source
, entry
);
1195 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
1204 focus(struct swm_region
*r
, union arg
*args
)
1206 struct ws_win
*winfocus
, *winlostfocus
;
1207 struct ws_win_list
*wl
;
1209 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
1210 if (cur_focus
== NULL
)
1213 wl
= &cur_focus
->ws
->winlist
;
1215 winlostfocus
= cur_focus
;
1218 case SWM_ARG_ID_FOCUSPREV
:
1219 winfocus
= TAILQ_PREV(cur_focus
, ws_win_list
, entry
);
1220 if (winfocus
== NULL
)
1221 winfocus
= TAILQ_LAST(wl
, ws_win_list
);
1224 case SWM_ARG_ID_FOCUSNEXT
:
1225 winfocus
= TAILQ_NEXT(cur_focus
, entry
);
1226 if (winfocus
== NULL
)
1227 winfocus
= TAILQ_FIRST(wl
);
1230 case SWM_ARG_ID_FOCUSMAIN
:
1231 winfocus
= TAILQ_FIRST(wl
);
1232 if (winfocus
== cur_focus
)
1233 winfocus
= cur_focus
->ws
->focus_prev
;
1234 if (winfocus
== NULL
)
1242 if (winfocus
== winlostfocus
|| winfocus
== NULL
)
1245 XMapRaised(display
, winfocus
->id
);
1246 focus_win(winfocus
);
1247 XSync(display
, False
);
1251 cycle_layout(struct swm_region
*r
, union arg
*args
)
1253 struct workspace
*ws
= r
->ws
;
1255 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", ws
->idx
);
1258 if (ws
->cur_layout
->l_stack
== NULL
)
1259 ws
->cur_layout
= &layouts
[0];
1265 stack_config(struct swm_region
*r
, union arg
*args
)
1267 struct workspace
*ws
= r
->ws
;
1269 DNPRINTF(SWM_D_STACK
, "stack_config for workspace %d (id %d\n",
1272 if (ws
->cur_layout
->l_config
!= NULL
)
1273 ws
->cur_layout
->l_config(ws
, args
->id
);
1275 if (args
->id
!= SWM_ARG_ID_STACKINIT
);
1281 struct swm_geometry g
;
1282 struct swm_region
*r
;
1285 DNPRINTF(SWM_D_STACK
, "stack\n");
1287 for (i
= 0; i
< ScreenCount(display
); i
++) {
1289 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
) {
1290 DNPRINTF(SWM_D_STACK
, "stacking workspace %d "
1291 "(screen %d, region %d)\n", r
->ws
->idx
, i
, j
++);
1293 /* start with screen geometry, adjust for bar */
1303 r
->ws
->cur_layout
->l_stack(r
->ws
, &g
);
1308 XSync(display
, False
);
1312 stack_floater(struct ws_win
*win
, struct swm_region
*r
)
1317 bzero(&wc
, sizeof wc
);
1318 mask
= CWX
| CWY
| CWBorderWidth
| CWWidth
| CWHeight
;
1319 if ((win
->quirks
& SWM_Q_FULLSCREEN
) && (win
->g
.w
== WIDTH(r
)) &&
1320 (win
->g
.h
== HEIGHT(r
)))
1321 wc
.border_width
= 0;
1323 wc
.border_width
= 1;
1324 if (win
->transient
&& (win
->quirks
& SWM_Q_TRANSSZ
)) {
1325 win
->g
.w
= (double)WIDTH(r
) * dialog_ratio
;
1326 win
->g
.h
= (double)HEIGHT(r
) * dialog_ratio
;
1328 wc
.width
= win
->g
.w
;
1329 wc
.height
= win
->g
.h
;
1334 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1335 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1338 DNPRINTF(SWM_D_STACK
, "stack_floater: win %lu x %d y %d w %d h %d\n",
1339 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1341 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1345 * Send keystrokes to terminal to decrease/increase the font size as the
1346 * window size changes.
1349 adjust_font(struct ws_win
*win
)
1351 if (!(win
->quirks
& SWM_Q_XTERM_FONTADJ
) ||
1352 win
->floating
|| win
->transient
)
1355 if (win
->sh
.width_inc
&& win
->last_inc
!= win
->sh
.width_inc
&&
1356 win
->g
.w
/ win
->sh
.width_inc
< term_width
&&
1357 win
->font_steps
< SWM_MAX_FONT_STEPS
) {
1358 win
->font_size_boundary
[win
->font_steps
] =
1359 (win
->sh
.width_inc
* term_width
) + win
->sh
.base_width
;
1362 win
->last_inc
= win
->sh
.width_inc
;
1363 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
1364 } else if (win
->font_steps
&& win
->last_inc
!= win
->sh
.width_inc
&&
1365 win
->g
.w
> win
->font_size_boundary
[win
->font_steps
- 1]) {
1368 win
->last_inc
= win
->sh
.width_inc
;
1369 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
1373 #define SWAPXY(g) do { \
1375 tmp = (g)->y; (g)->y = (g)->x; (g)->x = tmp; \
1376 tmp = (g)->h; (g)->h = (g)->w; (g)->w = tmp; \
1379 stack_master(struct workspace
*ws
, struct swm_geometry
*g
, int rot
, int flip
)
1382 struct swm_geometry win_g
, r_g
= *g
;
1383 struct ws_win
*win
, *winfocus
;
1384 int i
, j
, s
, stacks
;
1385 int w_inc
= 1, h_inc
, w_base
= 1, h_base
;
1386 int hrh
, extra
= 0, h_slice
, last_h
= 0;
1387 int split
, colno
, winno
, mwin
, msize
, mscale
;
1388 int remain
, missing
, v_slice
;
1391 DNPRINTF(SWM_D_STACK
, "stack_master: workspace: %d\n rot=%s flip=%s",
1392 ws
->idx
, rot
? "yes" : "no", flip
? "yes" : "no");
1394 winno
= count_win(ws
, 0);
1395 if (winno
== 0 && count_win(ws
, 1) == 0)
1398 if (ws
->focus
== NULL
)
1399 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1400 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1402 TAILQ_FOREACH(win
, &ws
->winlist
, entry
)
1403 if (win
->transient
== 0 && win
->floating
== 0)
1410 w_inc
= win
->sh
.width_inc
;
1411 w_base
= win
->sh
.base_width
;
1412 mwin
= ws
->l_state
.horizontal_mwin
;
1413 mscale
= ws
->l_state
.horizontal_msize
;
1414 stacks
= ws
->l_state
.horizontal_stacks
;
1417 w_inc
= win
->sh
.height_inc
;
1418 w_base
= win
->sh
.base_height
;
1419 mwin
= ws
->l_state
.vertical_mwin
;
1420 mscale
= ws
->l_state
.vertical_msize
;
1421 stacks
= ws
->l_state
.vertical_stacks
;
1425 if (stacks
> winno
- mwin
)
1426 stacks
= winno
- mwin
;
1430 h_slice
= r_g
.h
/ SWM_H_SLICE
;
1431 if (mwin
&& winno
> mwin
) {
1432 v_slice
= r_g
.w
/ SWM_V_SLICE
;
1436 win_g
.w
= v_slice
* mscale
;
1438 if (w_inc
> 1 && w_inc
< v_slice
) {
1439 /* adjust for window's requested size increment */
1440 remain
= (win_g
.w
- w_base
) % w_inc
;
1441 missing
= w_inc
- remain
;
1448 win_g
.x
+= r_g
.w
- msize
;
1451 colno
= split
= winno
/ stacks
;
1452 win_g
.w
= ((r_g
.w
- (stacks
* 2) + 2) / stacks
);
1454 hrh
= r_g
.h
/ colno
;
1455 extra
= r_g
.h
- (colno
* hrh
);
1458 /* stack all the tiled windows */
1459 i
= j
= 0, s
= stacks
;
1460 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1461 if (win
->transient
!= 0 || win
->floating
!= 0)
1464 if (split
&& i
== split
) {
1465 colno
= (winno
- mwin
) / stacks
;
1466 if (s
<= (winno
- mwin
) % stacks
)
1468 split
= split
+ colno
;
1469 hrh
= (r_g
.h
/ colno
);
1470 extra
= r_g
.h
- (colno
* hrh
);
1474 win_g
.x
+= win_g
.w
+ 2;
1475 win_g
.w
= (r_g
.w
- msize
- (stacks
* 2)) / stacks
;
1477 win_g
.w
+= (r_g
.w
- msize
- (stacks
* 2)) %
1484 h_inc
= win
->sh
.width_inc
;
1485 h_base
= win
->sh
.base_width
;
1487 h_inc
= win
->sh
.height_inc
;
1488 h_base
= win
->sh
.base_height
;
1490 if (j
== colno
- 1) {
1491 win_g
.h
= hrh
+ extra
;
1492 } else if (h_inc
> 1 && h_inc
< h_slice
) {
1493 /* adjust for window's requested size increment */
1494 remain
= (win_g
.h
- h_base
) % h_inc
;
1495 missing
= h_inc
- remain
;
1497 if (missing
<= extra
|| j
== 0) {
1509 win_g
.y
+= last_h
+ 2;
1511 bzero(&wc
, sizeof wc
);
1512 wc
.border_width
= 1;
1514 win
->g
.x
= wc
.x
= win_g
.y
;
1515 win
->g
.y
= wc
.y
= win_g
.x
;
1516 win
->g
.w
= wc
.width
= win_g
.h
;
1517 win
->g
.h
= wc
.height
= win_g
.w
;
1519 win
->g
.x
= wc
.x
= win_g
.x
;
1520 win
->g
.y
= wc
.y
= win_g
.y
;
1521 win
->g
.w
= wc
.width
= win_g
.w
;
1522 win
->g
.h
= wc
.height
= win_g
.h
;
1525 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1526 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1527 XMapRaised(display
, win
->id
);
1535 /* now, stack all the floaters and transients */
1536 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1537 if (win
->transient
== 0 && win
->floating
== 0)
1540 stack_floater(win
, ws
->r
);
1541 XMapRaised(display
, win
->id
);
1545 focus_win(winfocus
); /* has to be done outside of the loop */
1549 vertical_config(struct workspace
*ws
, int id
)
1551 DNPRINTF(SWM_D_STACK
, "vertical_resize: workspace: %d\n", ws
->idx
);
1554 case SWM_ARG_ID_STACKRESET
:
1555 case SWM_ARG_ID_STACKINIT
:
1556 ws
->l_state
.vertical_msize
= SWM_V_SLICE
/ 2;
1557 ws
->l_state
.vertical_mwin
= 1;
1558 ws
->l_state
.vertical_stacks
= 1;
1560 case SWM_ARG_ID_MASTERSHRINK
:
1561 if (ws
->l_state
.vertical_msize
> 1)
1562 ws
->l_state
.vertical_msize
--;
1564 case SWM_ARG_ID_MASTERGROW
:
1565 if (ws
->l_state
.vertical_msize
< SWM_V_SLICE
- 1)
1566 ws
->l_state
.vertical_msize
++;
1568 case SWM_ARG_ID_MASTERADD
:
1569 ws
->l_state
.vertical_mwin
++;
1571 case SWM_ARG_ID_MASTERDEL
:
1572 if (ws
->l_state
.vertical_mwin
> 0)
1573 ws
->l_state
.vertical_mwin
--;
1575 case SWM_ARG_ID_STACKINC
:
1576 ws
->l_state
.vertical_stacks
++;
1578 case SWM_ARG_ID_STACKDEC
:
1579 if (ws
->l_state
.vertical_stacks
> 1)
1580 ws
->l_state
.vertical_stacks
--;
1588 vertical_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1590 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1592 stack_master(ws
, g
, 0, 0);
1596 horizontal_config(struct workspace
*ws
, int id
)
1598 DNPRINTF(SWM_D_STACK
, "horizontal_config: workspace: %d\n", ws
->idx
);
1601 case SWM_ARG_ID_STACKRESET
:
1602 case SWM_ARG_ID_STACKINIT
:
1603 ws
->l_state
.horizontal_mwin
= 1;
1604 ws
->l_state
.horizontal_msize
= SWM_H_SLICE
/ 2;
1605 ws
->l_state
.horizontal_stacks
= 1;
1607 case SWM_ARG_ID_MASTERSHRINK
:
1608 if (ws
->l_state
.horizontal_msize
> 1)
1609 ws
->l_state
.horizontal_msize
--;
1611 case SWM_ARG_ID_MASTERGROW
:
1612 if (ws
->l_state
.horizontal_msize
< SWM_H_SLICE
- 1)
1613 ws
->l_state
.horizontal_msize
++;
1615 case SWM_ARG_ID_MASTERADD
:
1616 ws
->l_state
.horizontal_mwin
++;
1618 case SWM_ARG_ID_MASTERDEL
:
1619 if (ws
->l_state
.horizontal_mwin
> 0)
1620 ws
->l_state
.horizontal_mwin
--;
1622 case SWM_ARG_ID_STACKINC
:
1623 ws
->l_state
.horizontal_stacks
++;
1625 case SWM_ARG_ID_STACKDEC
:
1626 if (ws
->l_state
.horizontal_stacks
> 1)
1627 ws
->l_state
.horizontal_stacks
--;
1635 horizontal_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1637 DNPRINTF(SWM_D_STACK
, "vertical_stack: workspace: %d\n", ws
->idx
);
1639 stack_master(ws
, g
, 1, 0);
1642 /* fullscreen view */
1644 max_stack(struct workspace
*ws
, struct swm_geometry
*g
)
1647 struct swm_geometry gg
= *g
;
1648 struct ws_win
*win
, *winfocus
;
1652 /* XXX this function needs to be rewritten it sucks crap */
1654 DNPRINTF(SWM_D_STACK
, "max_stack: workspace: %d\n", ws
->idx
);
1656 winno
= count_win(ws
, 0);
1657 if (winno
== 0 && count_win(ws
, 1) == 0)
1660 if (ws
->focus
== NULL
)
1661 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1662 winfocus
= cur_focus
? cur_focus
: ws
->focus
;
1664 TAILQ_FOREACH(win
, &ws
->winlist
, entry
) {
1665 if (win
->transient
!= 0 || win
->floating
!= 0) {
1666 if (win
== ws
->focus
) {
1668 stack_floater(win
, ws
->r
);
1669 XMapRaised(display
, win
->id
);
1671 /* XXX this sucks */
1672 XUnmapWindow(display
, win
->id
);
1675 bzero(&wc
, sizeof wc
);
1676 wc
.border_width
= 1;
1677 win
->g
.x
= wc
.x
= gg
.x
;
1678 win
->g
.y
= wc
.y
= gg
.y
;
1679 win
->g
.w
= wc
.width
= gg
.w
;
1680 win
->g
.h
= wc
.height
= gg
.h
;
1681 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
1682 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1684 if (win
== ws
->focus
) {
1685 XMapRaised(display
, win
->id
);
1687 XUnmapWindow(display
, win
->id
);
1692 focus_win(winfocus
); /* has to be done outside of the loop */
1696 send_to_ws(struct swm_region
*r
, union arg
*args
)
1698 int wsid
= args
->id
;
1699 struct ws_win
*win
= cur_focus
;
1700 struct workspace
*ws
, *nws
;
1701 Atom ws_idx_atom
= 0;
1702 unsigned char ws_idx_str
[SWM_PROPLEN
];
1707 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
1710 nws
= &win
->s
->ws
[wsid
];
1712 XUnmapWindow(display
, win
->id
);
1714 /* find a window to focus */
1715 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
1716 if (ws
->focus
== NULL
)
1717 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
1718 if (ws
->focus
== win
)
1721 TAILQ_REMOVE(&ws
->winlist
, win
, entry
);
1723 TAILQ_INSERT_TAIL(&nws
->winlist
, win
, entry
);
1726 /* Try to update the window's workspace property */
1727 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
1729 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", nws
->idx
) < SWM_PROPLEN
) {
1730 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
1732 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
1733 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
1736 if (count_win(nws
, 1) == 1)
1745 wkill(struct swm_region
*r
, union arg
*args
)
1747 DNPRINTF(SWM_D_MISC
, "wkill %d\n", args
->id
);
1749 if(r
->ws
->focus
== NULL
)
1752 if (args
->id
== SWM_ARG_ID_KILLWINDOW
)
1753 XKillClient(display
, r
->ws
->focus
->id
);
1755 if (r
->ws
->focus
->can_delete
)
1756 client_msg(r
->ws
->focus
, adelete
);
1760 floating_toggle(struct swm_region
*r
, union arg
*args
)
1762 struct ws_win
*win
= cur_focus
;
1767 win
->floating
= !win
->floating
;
1774 resize_window(struct ws_win
*win
, int center
)
1778 struct swm_region
*r
;
1780 r
= root_to_region(win
->wa
.root
);
1781 bzero(&wc
, sizeof wc
);
1782 mask
= CWBorderWidth
| CWWidth
| CWHeight
;
1783 wc
.border_width
= 1;
1784 wc
.width
= win
->g
.w
;
1785 wc
.height
= win
->g
.h
;
1786 if (center
== SWM_ARG_ID_CENTER
) {
1787 wc
.x
= (WIDTH(r
) - win
->g
.w
) / 2;
1788 wc
.y
= (HEIGHT(r
) - win
->g
.h
) / 2;
1792 DNPRINTF(SWM_D_STACK
, "resize_window: win %lu x %d y %d w %d h %d\n",
1793 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1795 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1800 resize(struct ws_win
*win
, union arg
*args
)
1805 DNPRINTF(SWM_D_MOUSE
, "resize: win %lu floating %d trans %d\n",
1806 win
->id
, win
->floating
, win
->transient
);
1808 if (!(win
->transient
!= 0 || win
->floating
!= 0))
1811 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1812 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1814 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
, win
->g
.h
);
1816 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1817 SubstructureRedirectMask
, &ev
);
1819 case ConfigureRequest
:
1822 handler
[ev
.type
](&ev
);
1825 if (ev
.xmotion
.x
<= 1)
1827 if (ev
.xmotion
.y
<= 1)
1829 win
->g
.w
= ev
.xmotion
.x
;
1830 win
->g
.h
= ev
.xmotion
.y
;
1832 /* not free, don't sync more than 60 times / second */
1833 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1834 time
= ev
.xmotion
.time
;
1835 XSync(display
, False
);
1836 resize_window(win
, args
->id
);
1840 } while (ev
.type
!= ButtonRelease
);
1842 XSync(display
, False
);
1843 resize_window(win
, args
->id
);
1845 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, win
->g
.w
- 1,
1847 XUngrabPointer(display
, CurrentTime
);
1850 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1854 move_window(struct ws_win
*win
)
1858 struct swm_region
*r
;
1860 r
= root_to_region(win
->wa
.root
);
1861 bzero(&wc
, sizeof wc
);
1866 DNPRINTF(SWM_D_STACK
, "move_window: win %lu x %d y %d w %d h %d\n",
1867 win
->id
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
1869 XConfigureWindow(display
, win
->id
, mask
, &wc
);
1874 move(struct ws_win
*win
, union arg
*args
)
1880 DNPRINTF(SWM_D_MOUSE
, "move: win %lu floating %d trans %d\n",
1881 win
->id
, win
->floating
, win
->transient
);
1883 if (win
->floating
== 0) {
1889 if (XGrabPointer(display
, win
->id
, False
, MOUSEMASK
, GrabModeAsync
,
1890 GrabModeAsync
, None
, None
/* cursor */, CurrentTime
) != GrabSuccess
)
1892 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1894 XMaskEvent(display
, MOUSEMASK
| ExposureMask
|
1895 SubstructureRedirectMask
, &ev
);
1897 case ConfigureRequest
:
1900 handler
[ev
.type
](&ev
);
1903 win
->g
.x
= ev
.xmotion
.x_root
;
1904 win
->g
.y
= ev
.xmotion
.y_root
;
1906 /* not free, don't sync more than 60 times / second */
1907 if ((ev
.xmotion
.time
- time
) > (1000 / 60) ) {
1908 time
= ev
.xmotion
.time
;
1909 XSync(display
, False
);
1914 } while (ev
.type
!= ButtonRelease
);
1916 XSync(display
, False
);
1919 XWarpPointer(display
, None
, win
->id
, 0, 0, 0, 0, 0, 0);
1920 XUngrabPointer(display
, CurrentTime
);
1925 while (XCheckMaskEvent(display
, EnterWindowMask
, &ev
));
1928 /* key definitions */
1929 void dummykeyfunc(struct swm_region
*r
, union arg
*args
) {};
1930 void legacyfunc(struct swm_region
*r
, union arg
*args
) {};
1933 char name
[SWM_FUNCNAME_LEN
];
1934 void (*func
)(struct swm_region
*r
, union arg
*);
1936 } keyfuncs
[kf_invalid
+ 1] = {
1937 /* name function argument */
1938 { "cycle_layout", cycle_layout
, {0} },
1939 { "stack_reset", stack_config
, {.id
= SWM_ARG_ID_STACKRESET
} },
1940 { "master_shrink", stack_config
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1941 { "master_grow", stack_config
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1942 { "master_add", stack_config
, {.id
= SWM_ARG_ID_MASTERADD
} },
1943 { "master_del", stack_config
, {.id
= SWM_ARG_ID_MASTERDEL
} },
1944 { "stack_inc", stack_config
, {.id
= SWM_ARG_ID_STACKINC
} },
1945 { "stack_dec", stack_config
, {.id
= SWM_ARG_ID_STACKDEC
} },
1946 { "swap_main", swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1947 { "focus_next", focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1948 { "focus_prev", focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1949 { "swap_next", swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1950 { "swap_prev", swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1951 { "spawn_term", spawnterm
, {.argv
= spawn_term
} },
1952 { "spawn_menu", legacyfunc
, {0} },
1953 { "quit", quit
, {0} },
1954 { "restart", restart
, {0} },
1955 { "focus_main", focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1956 { "ws_1", switchws
, {.id
= 0} },
1957 { "ws_2", switchws
, {.id
= 1} },
1958 { "ws_3", switchws
, {.id
= 2} },
1959 { "ws_4", switchws
, {.id
= 3} },
1960 { "ws_5", switchws
, {.id
= 4} },
1961 { "ws_6", switchws
, {.id
= 5} },
1962 { "ws_7", switchws
, {.id
= 6} },
1963 { "ws_8", switchws
, {.id
= 7} },
1964 { "ws_9", switchws
, {.id
= 8} },
1965 { "ws_10", switchws
, {.id
= 9} },
1966 { "ws_next", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_UP
} },
1967 { "ws_prev", cyclews
, {.id
= SWM_ARG_ID_CYCLEWS_DOWN
} },
1968 { "screen_next", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_UP
} },
1969 { "screen_prev", cyclescr
, {.id
= SWM_ARG_ID_CYCLESC_DOWN
} },
1970 { "mvws_1", send_to_ws
, {.id
= 0} },
1971 { "mvws_2", send_to_ws
, {.id
= 1} },
1972 { "mvws_3", send_to_ws
, {.id
= 2} },
1973 { "mvws_4", send_to_ws
, {.id
= 3} },
1974 { "mvws_5", send_to_ws
, {.id
= 4} },
1975 { "mvws_6", send_to_ws
, {.id
= 5} },
1976 { "mvws_7", send_to_ws
, {.id
= 6} },
1977 { "mvws_8", send_to_ws
, {.id
= 7} },
1978 { "mvws_9", send_to_ws
, {.id
= 8} },
1979 { "mvws_10", send_to_ws
, {.id
= 9} },
1980 { "bar_toggle", bar_toggle
, {0} },
1981 { "wind_kill", wkill
, {.id
= SWM_ARG_ID_KILLWINDOW
} },
1982 { "wind_del", wkill
, {.id
= SWM_ARG_ID_DELETEWINDOW
} },
1983 { "screenshot_all", legacyfunc
, {0} },
1984 { "screenshot_wind", legacyfunc
, {0} },
1985 { "float_toggle", floating_toggle
,{0} },
1986 { "version", version
, {0} },
1987 { "spawn_lock", legacyfunc
, {0} },
1988 { "spawn_initscr", legacyfunc
, {0} },
1989 { "spawn_custom", dummykeyfunc
, {0} },
1990 { "invalid key func", NULL
, {0} },
1995 enum keyfuncid funcid
;
1998 int keys_size
= 0, keys_length
= 0;
1999 struct key
*keys
= NULL
;
2002 enum { client_click
, root_click
};
2004 unsigned int action
;
2006 unsigned int button
;
2007 void (*func
)(struct ws_win
*, union arg
*);
2010 /* action key mouse button func args */
2011 { client_click
, MODKEY
, Button3
, resize
, {.id
= SWM_ARG_ID_DONTCENTER
} },
2012 { client_click
, MODKEY
| ShiftMask
, Button3
, resize
, {.id
= SWM_ARG_ID_CENTER
} },
2013 { client_click
, MODKEY
, Button1
, move
, {0} },
2017 update_modkey(unsigned int mod
)
2022 for (i
= 0; i
< keys_length
; i
++)
2023 if (keys
[i
].mod
& ShiftMask
)
2024 keys
[i
].mod
= mod
| ShiftMask
;
2028 for (i
= 0; i
< LENGTH(buttons
); i
++)
2029 if (buttons
[i
].mask
& ShiftMask
)
2030 buttons
[i
].mask
= mod
| ShiftMask
;
2032 buttons
[i
].mask
= mod
;
2042 int spawns_size
= 0, spawns_length
= 0;
2043 struct spawn_prog
*spawns
= NULL
;
2046 spawn_custom(struct swm_region
*r
, union arg
*args
, char *spawn_name
)
2049 struct spawn_prog
*prog
= NULL
;
2051 char *ap
, **real_args
;
2053 DNPRINTF(SWM_D_SPAWN
, "spawn_custom %s\n", spawn_name
);
2056 for (i
= 0; i
< spawns_length
; i
++) {
2057 if (!strcasecmp(spawn_name
, spawns
[i
].name
))
2061 fprintf(stderr
, "spawn_custom: program %s not found\n",
2066 /* make room for expanded args */
2067 if ((real_args
= calloc(prog
->argc
+ 1, sizeof(char *))) == NULL
)
2068 err(1, "spawn_custom: calloc real_args");
2070 /* expand spawn_args into real_args */
2071 for (i
= 0; i
< prog
->argc
; i
++) {
2073 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: raw arg = %s\n", ap
);
2074 if (!strcasecmp(ap
, "$bar_border")) {
2076 strdup(r
->s
->c
[SWM_S_COLOR_BAR_BORDER
].name
))
2078 err(1, "spawn_custom border color");
2079 } else if (!strcasecmp(ap
, "$bar_color")) {
2081 strdup(r
->s
->c
[SWM_S_COLOR_BAR
].name
))
2083 err(1, "spawn_custom bar color");
2084 } else if (!strcasecmp(ap
, "$bar_font")) {
2085 if ((real_args
[i
] = strdup(bar_fonts
[bar_fidx
]))
2087 err(1, "spawn_custom bar fonts");
2088 } else if (!strcasecmp(ap
, "$bar_font_color")) {
2090 strdup(r
->s
->c
[SWM_S_COLOR_BAR_FONT
].name
))
2092 err(1, "spawn_custom color font");
2093 } else if (!strcasecmp(ap
, "$color_focus")) {
2095 strdup(r
->s
->c
[SWM_S_COLOR_FOCUS
].name
))
2097 err(1, "spawn_custom color focus");
2098 } else if (!strcasecmp(ap
, "$color_unfocus")) {
2100 strdup(r
->s
->c
[SWM_S_COLOR_UNFOCUS
].name
))
2102 err(1, "spawn_custom color unfocus");
2104 /* no match --> copy as is */
2105 if ((real_args
[i
] = strdup(ap
)) == NULL
)
2106 err(1, "spawn_custom strdup(ap)");
2108 DNPRINTF(SWM_D_SPAWN
, "spawn_custom: cooked arg = %s\n",
2113 if ((swm_debug
& SWM_D_SPAWN
) != 0) {
2114 fprintf(stderr
, "spawn_custom: result = ");
2115 for (i
= 0; i
< prog
->argc
; i
++)
2116 fprintf(stderr
, "\"%s\" ", real_args
[i
]);
2117 fprintf(stderr
, "\n");
2123 for (i
= 0; i
< prog
->argc
; i
++)
2129 setspawn(struct spawn_prog
*prog
)
2133 if (prog
== NULL
|| prog
->name
== NULL
)
2137 for (i
= 0; i
< spawns_length
; i
++) {
2138 if (!strcmp(spawns
[i
].name
, prog
->name
)) {
2140 if (prog
->argv
== NULL
) {
2142 DNPRINTF(SWM_D_SPAWN
,
2143 "setspawn: delete #%d %s\n",
2145 free(spawns
[i
].name
);
2146 for (j
= 0; j
< spawns
[i
].argc
; j
++)
2147 free(spawns
[i
].argv
[j
]);
2148 free(spawns
[i
].argv
);
2149 j
= spawns_length
- 1;
2151 spawns
[i
] = spawns
[j
];
2156 DNPRINTF(SWM_D_SPAWN
,
2157 "setspawn: replace #%d %s\n",
2159 free(spawns
[i
].name
);
2160 for (j
= 0; j
< spawns
[i
].argc
; j
++)
2161 free(spawns
[i
].argv
[j
]);
2162 free(spawns
[i
].argv
);
2165 /* found case handled */
2171 if (prog
->argv
== NULL
) {
2173 "error: setspawn: cannot find program %s", prog
->name
);
2178 /* not found: add */
2179 if (spawns_size
== 0 || spawns
== NULL
) {
2181 DNPRINTF(SWM_D_SPAWN
, "setspawn: init list %d\n", spawns_size
);
2182 spawns
= malloc((size_t)spawns_size
*
2183 sizeof(struct spawn_prog
));
2184 if (spawns
== NULL
) {
2185 fprintf(stderr
, "setspawn: malloc failed\n");
2189 } else if (spawns_length
== spawns_size
) {
2191 DNPRINTF(SWM_D_SPAWN
, "setspawn: grow list %d\n", spawns_size
);
2192 spawns
= realloc(spawns
, (size_t)spawns_size
*
2193 sizeof(struct spawn_prog
));
2194 if (spawns
== NULL
) {
2195 fprintf(stderr
, "setspawn: realloc failed\n");
2201 if (spawns_length
< spawns_size
) {
2202 DNPRINTF(SWM_D_SPAWN
, "setspawn: add #%d %s\n",
2203 spawns_length
, prog
->name
);
2204 i
= spawns_length
++;
2207 fprintf(stderr
, "spawns array problem?\n");
2208 if (spawns
== NULL
) {
2209 fprintf(stderr
, "spawns array is NULL!\n");
2217 setconfspawn(char *selector
, char *value
, int flags
)
2219 struct spawn_prog
*prog
;
2220 char *vp
, *cp
, *word
;
2222 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: [%s] [%s]\n", selector
, value
);
2223 if ((prog
= calloc(1, sizeof *prog
)) == NULL
)
2224 err(1, "setconfspawn: calloc prog");
2225 prog
->name
= strdup(selector
);
2226 if (prog
->name
== NULL
)
2227 err(1, "setconfspawn prog->name");
2228 if ((cp
= vp
= strdup(value
)) == NULL
)
2229 err(1, "setconfspawn: strdup(value) ");
2230 while ((word
= strsep(&cp
, " \t")) != NULL
) {
2231 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: arg [%s]\n", word
);
2233 cp
+= (long)strspn(cp
, " \t");
2234 if (strlen(word
) > 0) {
2236 prog
->argv
= realloc(prog
->argv
,
2237 prog
->argc
* sizeof(char *));
2238 if ((prog
->argv
[prog
->argc
- 1] = strdup(word
)) == NULL
)
2239 err(1, "setconfspawn: strdup");
2246 DNPRINTF(SWM_D_SPAWN
, "setconfspawn: done\n");
2253 setconfspawn("term", "xterm", 0);
2254 setconfspawn("screenshot_all", "screenshot.sh full", 0);
2255 setconfspawn("screenshot_wind", "screenshot.sh window", 0);
2256 setconfspawn("lock", "xlock", 0);
2257 setconfspawn("initscr", "initscreen.sh", 0);
2258 setconfspawn("menu", "dmenu_run"
2261 " -nf $bar_font_color"
2263 " -sf $bar_color", 0);
2267 #define SWM_MODNAME_SIZE 32
2268 #define SWM_KEY_WS "\n+ \t"
2270 parsekeys(char *keystr
, unsigned int currmod
, unsigned int *mod
, KeySym
*ks
)
2274 DNPRINTF(SWM_D_KEY
, "parsekeys: enter [%s]\n", keystr
);
2275 if (mod
== NULL
|| ks
== NULL
) {
2276 DNPRINTF(SWM_D_KEY
, "parsekeys: no mod or key vars\n");
2279 if (keystr
== NULL
|| strlen(keystr
) == 0) {
2280 DNPRINTF(SWM_D_KEY
, "parsekeys: no keystr\n");
2286 while ((name
= strsep(&cp
, SWM_KEY_WS
)) != NULL
) {
2287 DNPRINTF(SWM_D_KEY
, "parsekeys: key [%s]\n", name
);
2289 cp
+= (long)strspn(cp
, SWM_KEY_WS
);
2290 if (strncasecmp(name
, "MOD", SWM_MODNAME_SIZE
) == 0)
2292 else if (!strncasecmp(name
, "Mod1", SWM_MODNAME_SIZE
))
2294 else if (!strncasecmp(name
, "Mod2", SWM_MODNAME_SIZE
))
2296 else if (!strncmp(name
, "Mod3", SWM_MODNAME_SIZE
))
2298 else if (!strncmp(name
, "Mod4", SWM_MODNAME_SIZE
))
2300 else if (strncasecmp(name
, "SHIFT", SWM_MODNAME_SIZE
) == 0)
2302 else if (strncasecmp(name
, "CONTROL", SWM_MODNAME_SIZE
) == 0)
2303 *mod
|= ControlMask
;
2305 *ks
= XStringToKeysym(name
);
2306 XConvertCase(*ks
, ks
, &uks
);
2307 if (ks
== NoSymbol
) {
2309 "parsekeys: invalid key %s\n",
2315 DNPRINTF(SWM_D_KEY
, "parsekeys: leave ok\n");
2319 strdupsafe(char *str
)
2324 return (strdup(str
));
2327 setkeybinding(unsigned int mod
, KeySym ks
, enum keyfuncid kfid
, char *spawn_name
)
2330 DNPRINTF(SWM_D_KEY
, "setkeybinding: enter %s [%s]\n",
2331 keyfuncs
[kfid
].name
, spawn_name
);
2333 for (i
= 0; i
< keys_length
; i
++) {
2334 if (keys
[i
].mod
== mod
&& keys
[i
].keysym
== ks
) {
2335 if (kfid
== kf_invalid
) {
2338 "setkeybinding: delete #%d %s\n",
2339 i
, keyfuncs
[keys
[i
].funcid
].name
);
2340 free(keys
[i
].spawn_name
);
2341 j
= keys_length
- 1;
2345 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
2348 /* found: replace */
2350 "setkeybinding: replace #%d %s %s\n",
2351 i
, keyfuncs
[keys
[i
].funcid
].name
,
2353 free(keys
[i
].spawn_name
);
2355 keys
[i
].keysym
= ks
;
2356 keys
[i
].funcid
= kfid
;
2357 keys
[i
].spawn_name
= strdupsafe(spawn_name
);
2358 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
2363 if (kfid
== kf_invalid
) {
2365 "error: setkeybinding: cannot find mod/key combination");
2366 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
2369 /* not found: add */
2370 if (keys_size
== 0 || keys
== NULL
) {
2372 DNPRINTF(SWM_D_KEY
, "setkeybinding: init list %d\n", keys_size
);
2373 keys
= malloc((size_t)keys_size
* sizeof(struct key
));
2375 fprintf(stderr
, "malloc failed\n");
2379 } else if (keys_length
== keys_size
) {
2381 DNPRINTF(SWM_D_KEY
, "setkeybinding: grow list %d\n", keys_size
);
2382 keys
= realloc(keys
, (size_t)keys_size
* sizeof(struct key
));
2384 fprintf(stderr
, "realloc failed\n");
2389 if (keys_length
< keys_size
) {
2391 DNPRINTF(SWM_D_KEY
, "setkeybinding: add #%d %s %s\n",
2392 j
, keyfuncs
[kfid
].name
, spawn_name
);
2394 keys
[j
].keysym
= ks
;
2395 keys
[j
].funcid
= kfid
;
2396 keys
[j
].spawn_name
= strdupsafe(spawn_name
);
2398 fprintf(stderr
, "keys array problem?\n");
2400 fprintf(stderr
, "keys array problem\n");
2404 DNPRINTF(SWM_D_KEY
, "setkeybinding: leave\n");
2407 setconfbinding(char *selector
, char *value
, int flags
)
2409 enum keyfuncid kfid
;
2413 DNPRINTF(SWM_D_KEY
, "setconfbinding: enter\n");
2414 if (selector
== NULL
) {
2415 DNPRINTF(SWM_D_KEY
, "setconfbinding: unbind %s\n", value
);
2416 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
2418 setkeybinding(mod
, ks
, kfid
, NULL
);
2423 /* search by key function name */
2424 for (kfid
= 0; kfid
< kf_invalid
; (kfid
)++) {
2425 if (strncasecmp(selector
, keyfuncs
[kfid
].name
,
2426 SWM_FUNCNAME_LEN
) == 0) {
2427 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
2429 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
2430 setkeybinding(mod
, ks
, kfid
, NULL
);
2436 /* search by custom spawn name */
2437 for (i
= 0; i
< spawns_length
; i
++) {
2438 if (strcasecmp(selector
, spawns
[i
].name
) == 0) {
2439 DNPRINTF(SWM_D_KEY
, "setconfbinding: %s: match\n",
2441 if (parsekeys(value
, mod_key
, &mod
, &ks
) == 0) {
2442 setkeybinding(mod
, ks
, kf_spawn_custom
,
2449 DNPRINTF(SWM_D_KEY
, "setconfbinding: no match\n");
2455 setkeybinding(MODKEY
, XK_space
, kf_cycle_layout
,NULL
);
2456 setkeybinding(MODKEY
|ShiftMask
, XK_space
, kf_stack_reset
, NULL
);
2457 setkeybinding(MODKEY
, XK_h
, kf_master_shrink
,NULL
);
2458 setkeybinding(MODKEY
, XK_l
, kf_master_grow
, NULL
);
2459 setkeybinding(MODKEY
, XK_comma
, kf_master_add
, NULL
);
2460 setkeybinding(MODKEY
, XK_period
, kf_master_del
, NULL
);
2461 setkeybinding(MODKEY
|ShiftMask
, XK_comma
, kf_stack_inc
, NULL
);
2462 setkeybinding(MODKEY
|ShiftMask
, XK_period
, kf_stack_dec
, NULL
);
2463 setkeybinding(MODKEY
, XK_Return
, kf_swap_main
, NULL
);
2464 setkeybinding(MODKEY
, XK_j
, kf_focus_next
, NULL
);
2465 setkeybinding(MODKEY
, XK_k
, kf_focus_prev
, NULL
);
2466 setkeybinding(MODKEY
|ShiftMask
, XK_j
, kf_swap_next
, NULL
);
2467 setkeybinding(MODKEY
|ShiftMask
, XK_k
, kf_swap_prev
, NULL
);
2468 setkeybinding(MODKEY
|ShiftMask
, XK_Return
, kf_spawn_term
, NULL
);
2469 setkeybinding(MODKEY
, XK_p
, kf_spawn_custom
, "menu");
2470 setkeybinding(MODKEY
|ShiftMask
, XK_q
, kf_quit
, NULL
);
2471 setkeybinding(MODKEY
, XK_q
, kf_restart
, NULL
);
2472 setkeybinding(MODKEY
, XK_m
, kf_focus_main
, NULL
);
2473 setkeybinding(MODKEY
, XK_1
, kf_ws_1
, NULL
);
2474 setkeybinding(MODKEY
, XK_2
, kf_ws_2
, NULL
);
2475 setkeybinding(MODKEY
, XK_3
, kf_ws_3
, NULL
);
2476 setkeybinding(MODKEY
, XK_4
, kf_ws_4
, NULL
);
2477 setkeybinding(MODKEY
, XK_5
, kf_ws_5
, NULL
);
2478 setkeybinding(MODKEY
, XK_6
, kf_ws_6
, NULL
);
2479 setkeybinding(MODKEY
, XK_7
, kf_ws_7
, NULL
);
2480 setkeybinding(MODKEY
, XK_8
, kf_ws_8
, NULL
);
2481 setkeybinding(MODKEY
, XK_9
, kf_ws_9
, NULL
);
2482 setkeybinding(MODKEY
, XK_0
, kf_ws_10
, NULL
);
2483 setkeybinding(MODKEY
, XK_Right
, kf_ws_next
, NULL
);
2484 setkeybinding(MODKEY
, XK_Left
, kf_ws_prev
, NULL
);
2485 setkeybinding(MODKEY
|ShiftMask
, XK_Right
, kf_screen_next
, NULL
);
2486 setkeybinding(MODKEY
|ShiftMask
, XK_Left
, kf_screen_prev
, NULL
);
2487 setkeybinding(MODKEY
|ShiftMask
, XK_1
, kf_mvws_1
, NULL
);
2488 setkeybinding(MODKEY
|ShiftMask
, XK_2
, kf_mvws_2
, NULL
);
2489 setkeybinding(MODKEY
|ShiftMask
, XK_3
, kf_mvws_3
, NULL
);
2490 setkeybinding(MODKEY
|ShiftMask
, XK_4
, kf_mvws_4
, NULL
);
2491 setkeybinding(MODKEY
|ShiftMask
, XK_5
, kf_mvws_5
, NULL
);
2492 setkeybinding(MODKEY
|ShiftMask
, XK_6
, kf_mvws_6
, NULL
);
2493 setkeybinding(MODKEY
|ShiftMask
, XK_7
, kf_mvws_7
, NULL
);
2494 setkeybinding(MODKEY
|ShiftMask
, XK_8
, kf_mvws_8
, NULL
);
2495 setkeybinding(MODKEY
|ShiftMask
, XK_9
, kf_mvws_9
, NULL
);
2496 setkeybinding(MODKEY
|ShiftMask
, XK_0
, kf_mvws_10
, NULL
);
2497 setkeybinding(MODKEY
, XK_b
, kf_bar_toggle
, NULL
);
2498 setkeybinding(MODKEY
, XK_Tab
, kf_focus_next
, NULL
);
2499 setkeybinding(MODKEY
|ShiftMask
, XK_Tab
, kf_focus_prev
, NULL
);
2500 setkeybinding(MODKEY
|ShiftMask
, XK_x
, kf_wind_kill
, NULL
);
2501 setkeybinding(MODKEY
, XK_x
, kf_wind_del
, NULL
);
2502 setkeybinding(MODKEY
, XK_s
, kf_spawn_custom
, "screenshot_all");
2503 setkeybinding(MODKEY
|ShiftMask
, XK_s
, kf_spawn_custom
, "screenshot_wind");
2504 setkeybinding(MODKEY
, XK_t
, kf_float_toggle
,NULL
);
2505 setkeybinding(MODKEY
|ShiftMask
, XK_v
, kf_version
, NULL
);
2506 setkeybinding(MODKEY
|ShiftMask
, XK_Delete
, kf_spawn_custom
, "lock");
2507 setkeybinding(MODKEY
|ShiftMask
, XK_i
, kf_spawn_custom
, "initscr");
2510 updatenumlockmask(void)
2513 XModifierKeymap
*modmap
;
2515 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
2517 modmap
= XGetModifierMapping(display
);
2518 for (i
= 0; i
< 8; i
++)
2519 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2520 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2521 == XKeysymToKeycode(display
, XK_Num_Lock
))
2522 numlockmask
= (1 << i
);
2524 XFreeModifiermap(modmap
);
2530 unsigned int i
, j
, k
;
2532 unsigned int modifiers
[] =
2533 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
2535 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
2536 updatenumlockmask();
2538 for (k
= 0; k
< ScreenCount(display
); k
++) {
2539 if (TAILQ_EMPTY(&screens
[k
].rl
))
2541 XUngrabKey(display
, AnyKey
, AnyModifier
, screens
[k
].root
);
2542 for (i
= 0; i
< keys_length
; i
++) {
2543 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
2544 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2545 XGrabKey(display
, code
,
2546 keys
[i
].mod
| modifiers
[j
],
2547 screens
[k
].root
, True
,
2548 GrabModeAsync
, GrabModeAsync
);
2554 grabbuttons(struct ws_win
*win
, int focused
)
2557 unsigned int modifiers
[] =
2558 { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
2560 updatenumlockmask();
2561 XUngrabButton(display
, AnyButton
, AnyModifier
, win
->id
);
2563 for (i
= 0; i
< LENGTH(buttons
); i
++)
2564 if (buttons
[i
].action
== client_click
)
2565 for (j
= 0; j
< LENGTH(modifiers
); j
++)
2566 XGrabButton(display
, buttons
[i
].button
,
2567 buttons
[i
].mask
| modifiers
[j
],
2568 win
->id
, False
, BUTTONMASK
,
2569 GrabModeAsync
, GrabModeSync
, None
,
2572 XGrabButton(display
, AnyButton
, AnyModifier
, win
->id
, False
,
2573 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
2579 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
2587 XKeyEvent
*ev
= &e
->xkey
;
2589 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
2591 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
2592 for (i
= 0; i
< keys_length
; i
++)
2593 if (keysym
== keys
[i
].keysym
2594 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
2595 && keyfuncs
[keys
[i
].funcid
].func
) {
2596 if (keys
[i
].funcid
== kf_spawn_custom
)
2598 root_to_region(ev
->root
),
2599 &(keyfuncs
[keys
[i
].funcid
].args
),
2603 keyfuncs
[keys
[i
].funcid
].func(
2604 root_to_region(ev
->root
),
2605 &(keyfuncs
[keys
[i
].funcid
].args
)
2611 buttonpress(XEvent
*e
)
2613 XButtonPressedEvent
*ev
= &e
->xbutton
;
2618 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
2620 action
= root_click
;
2621 if ((win
= find_window(ev
->window
)) == NULL
)
2625 action
= client_click
;
2628 for (i
= 0; i
< LENGTH(buttons
); i
++)
2629 if (action
== buttons
[i
].action
&& buttons
[i
].func
&&
2630 buttons
[i
].button
== ev
->button
&&
2631 CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
2632 buttons
[i
].func(win
, &buttons
[i
].args
);
2636 set_win_state(struct ws_win
*win
, long state
)
2638 long data
[] = {state
, None
};
2640 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
2642 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
2643 (unsigned char *)data
, 2);
2646 const char *quirkname
[] = {
2647 "NONE", /* config string for "no value" */
2655 /* SWM_Q_WS: retain '|' for back compat for now (2009-08-11) */
2656 #define SWM_Q_WS "\n|+ \t"
2658 parsequirks(char *qstr
, unsigned long *quirk
)
2666 while ((name
= strsep(&cp
, SWM_Q_WS
)) != NULL
) {
2668 cp
+= (long)strspn(cp
, SWM_Q_WS
);
2669 for (i
= 0; i
< LENGTH(quirkname
); i
++) {
2670 if (!strncasecmp(name
, quirkname
[i
], SWM_QUIRK_LEN
)) {
2671 DNPRINTF(SWM_D_QUIRK
, "parsequirks: %s\n", name
);
2676 *quirk
|= 1 << (i
-1);
2680 if (i
>= LENGTH(quirkname
)) {
2681 DNPRINTF(SWM_D_QUIRK
,
2682 "parsequirks: invalid quirk [%s]\n", name
);
2689 setquirk(const char *class, const char *name
, const int quirk
)
2693 for (i
= 0; i
< quirks_length
; i
++) {
2694 if (!strcmp(quirks
[i
].class, class) &&
2695 !strcmp(quirks
[i
].name
, name
)) {
2698 DNPRINTF(SWM_D_QUIRK
,
2699 "setquirk: delete #%d %s:%s\n",
2700 i
, quirks
[i
].class, quirks
[i
].name
);
2701 free(quirks
[i
].class);
2702 free(quirks
[i
].name
);
2703 j
= quirks_length
- 1;
2705 quirks
[i
] = quirks
[j
];
2709 /* found: replace */
2710 DNPRINTF(SWM_D_QUIRK
,
2711 "setquirk: replace #%d %s:%s\n",
2712 i
, quirks
[i
].class, quirks
[i
].name
);
2713 free(quirks
[i
].class);
2714 free(quirks
[i
].name
);
2715 quirks
[i
].class = strdup(class);
2716 quirks
[i
].name
= strdup(name
);
2717 quirks
[i
].quirk
= quirk
;
2724 "error: setquirk: cannot find class/name combination");
2727 /* not found: add */
2728 if (quirks_size
== 0 || quirks
== NULL
) {
2730 DNPRINTF(SWM_D_QUIRK
, "setquirk: init list %d\n", quirks_size
);
2731 quirks
= malloc((size_t)quirks_size
* sizeof(struct quirk
));
2733 fprintf(stderr
, "setquirk: malloc failed\n");
2737 } else if (quirks_length
== quirks_size
) {
2739 DNPRINTF(SWM_D_QUIRK
, "setquirk: grow list %d\n", quirks_size
);
2740 quirks
= realloc(quirks
, (size_t)quirks_size
* sizeof(struct quirk
));
2742 fprintf(stderr
, "setquirk: realloc failed\n");
2747 if (quirks_length
< quirks_size
) {
2748 DNPRINTF(SWM_D_QUIRK
, "setquirk: add %d\n", quirks_length
);
2749 j
= quirks_length
++;
2750 quirks
[j
].class = strdup(class);
2751 quirks
[j
].name
= strdup(name
);
2752 quirks
[j
].quirk
= quirk
;
2754 fprintf(stderr
, "quirks array problem?\n");
2756 fprintf(stderr
, "quirks array problem!\n");
2762 setconfquirk(char *selector
, char *value
, int flags
)
2764 char *cp
, *class, *name
;
2766 unsigned long quirks
;
2767 if (selector
== NULL
)
2769 if ((cp
= strchr(selector
, ':')) == NULL
)
2774 if ((retval
= parsequirks(value
, &quirks
)) == 0)
2775 setquirk(class, name
, quirks
);
2782 setquirk("MPlayer", "xv", SWM_Q_FLOAT
| SWM_Q_FULLSCREEN
);
2783 setquirk("OpenOffice.org 2.4", "VCLSalFrame", SWM_Q_FLOAT
);
2784 setquirk("OpenOffice.org 3.0", "VCLSalFrame", SWM_Q_FLOAT
);
2785 setquirk("Firefox-bin", "firefox-bin", SWM_Q_TRANSSZ
);
2786 setquirk("Firefox", "Dialog", SWM_Q_FLOAT
);
2787 setquirk("Gimp", "gimp", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
2788 setquirk("XTerm", "xterm", SWM_Q_XTERM_FONTADJ
);
2789 setquirk("xine", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
2790 setquirk("Xitk", "Xitk Combo", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
2791 setquirk("xine", "xine Panel", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
2792 setquirk("Xitk", "Xine Window", SWM_Q_FLOAT
| SWM_Q_ANYWHERE
);
2793 setquirk("xine", "xine Video Fullscreen Window", SWM_Q_FULLSCREEN
| SWM_Q_FLOAT
);
2794 setquirk("pcb", "pcb", SWM_Q_FLOAT
);
2797 /* conf file stuff */
2798 #define SWM_CONF_FILE "scrotwm.conf"
2800 enum { SWM_S_BAR_DELAY
, SWM_S_BAR_ENABLED
, SWM_S_CLOCK_ENABLED
,
2801 SWM_S_CYCLE_EMPTY
, SWM_S_CYCLE_VISIBLE
, SWM_S_SS_ENABLED
,
2802 SWM_S_TERM_WIDTH
, SWM_S_TITLE_CLASS_ENABLED
, SWM_S_TITLE_NAME_ENABLED
,
2803 SWM_S_BAR_FONT
, SWM_S_BAR_ACTION
, SWM_S_SPAWN_TERM
, SWM_S_SS_APP
,
2804 SWM_S_DIALOG_RATIO
};
2807 setconfvalue(char *selector
, char *value
, int flags
)
2810 case SWM_S_BAR_DELAY
:
2811 bar_delay
= atoi(value
);
2813 case SWM_S_BAR_ENABLED
:
2814 bar_enabled
= atoi(value
);
2816 case SWM_S_CLOCK_ENABLED
:
2817 clock_enabled
= atoi(value
);
2819 case SWM_S_CYCLE_EMPTY
:
2820 cycle_empty
= atoi(value
);
2822 case SWM_S_CYCLE_VISIBLE
:
2823 cycle_visible
= atoi(value
);
2825 case SWM_S_SS_ENABLED
:
2826 ss_enabled
= atoi(value
);
2828 case SWM_S_TERM_WIDTH
:
2829 term_width
= atoi(value
);
2831 case SWM_S_TITLE_CLASS_ENABLED
:
2832 title_class_enabled
= atoi(value
);
2834 case SWM_S_TITLE_NAME_ENABLED
:
2835 title_name_enabled
= atoi(value
);
2837 case SWM_S_BAR_FONT
:
2839 if ((bar_fonts
[0] = strdup(value
)) == NULL
)
2840 err(1, "setconfvalue: bar_font");
2842 case SWM_S_BAR_ACTION
:
2844 if ((bar_argv
[0] = strdup(value
)) == NULL
)
2845 err(1, "setconfvalue: bar_action");
2847 case SWM_S_SPAWN_TERM
:
2848 free(spawn_term
[0]);
2849 if ((spawn_term
[0] = strdup(value
)) == NULL
)
2850 err(1, "setconfvalue: spawn_term");
2854 case SWM_S_DIALOG_RATIO
:
2855 dialog_ratio
= atof(value
);
2856 if (dialog_ratio
> 1.0 || dialog_ratio
<= .3)
2866 setconfmodkey(char *selector
, char *value
, int flags
)
2868 if (!strncasecmp(value
, "Mod1", strlen("Mod1")))
2869 update_modkey(Mod1Mask
);
2870 else if (!strncasecmp(value
, "Mod2", strlen("Mod2")))
2871 update_modkey(Mod2Mask
);
2872 else if (!strncasecmp(value
, "Mod3", strlen("Mod3")))
2873 update_modkey(Mod3Mask
);
2874 else if (!strncasecmp(value
, "Mod4", strlen("Mod4")))
2875 update_modkey(Mod4Mask
);
2882 setconfcolor(char *selector
, char *value
, int flags
)
2884 setscreencolor(value
, ((selector
== NULL
)?-1:atoi(selector
)), flags
);
2889 setconfregion(char *selector
, char *value
, int flags
)
2891 custom_region(value
);
2895 /* config options */
2896 struct config_option
{
2898 int (*func
)(char*, char*, int);
2901 struct config_option configopt
[] = {
2902 { "bar_enabled", setconfvalue
, SWM_S_BAR_ENABLED
},
2903 { "bar_border", setconfcolor
, SWM_S_COLOR_BAR_BORDER
},
2904 { "bar_color", setconfcolor
, SWM_S_COLOR_BAR
},
2905 { "bar_font_color", setconfcolor
, SWM_S_COLOR_BAR_FONT
},
2906 { "bar_font", setconfvalue
, SWM_S_BAR_FONT
},
2907 { "bar_action", setconfvalue
, SWM_S_BAR_ACTION
},
2908 { "bar_delay", setconfvalue
, SWM_S_BAR_DELAY
},
2909 { "bind", setconfbinding
, 0 },
2910 { "clock_enabled", setconfvalue
, SWM_S_CLOCK_ENABLED
},
2911 { "color_focus", setconfcolor
, SWM_S_COLOR_FOCUS
},
2912 { "color_unfocus", setconfcolor
, SWM_S_COLOR_UNFOCUS
},
2913 { "cycle_empty", setconfvalue
, SWM_S_CYCLE_EMPTY
},
2914 { "cycle_visible", setconfvalue
, SWM_S_CYCLE_VISIBLE
},
2915 { "dialog_ratio", setconfvalue
, SWM_S_DIALOG_RATIO
},
2916 { "modkey", setconfmodkey
, 0 },
2917 { "program", setconfspawn
, 0 },
2918 { "quirk", setconfquirk
, 0 },
2919 { "region", setconfregion
, 0 },
2920 { "spawn_term", setconfvalue
, SWM_S_SPAWN_TERM
},
2921 { "screenshot_enabled", setconfvalue
, SWM_S_SS_ENABLED
},
2922 { "screenshot_app", setconfvalue
, SWM_S_SS_APP
},
2923 { "term_width", setconfvalue
, SWM_S_TERM_WIDTH
},
2924 { "title_class_enabled", setconfvalue
, SWM_S_TITLE_CLASS_ENABLED
},
2925 { "title_name_enabled", setconfvalue
, SWM_S_TITLE_NAME_ENABLED
}
2930 conf_load(char *filename
)
2933 char *line
, *cp
, *optsub
, *optval
;
2934 size_t linelen
, lineno
= 0;
2935 int wordlen
, i
, optind
;
2936 struct config_option
*opt
;
2937 DPRINTF("conf_load begin\n");
2938 if (filename
== NULL
) {
2939 fprintf(stderr
, "conf_load: no filename\n");
2942 if ((config
= fopen(filename
, "r")) == NULL
) {
2943 warn("conf_load: fopen");
2946 while (!feof(config
)) {
2947 if ((line
= fparseln(config
, &linelen
, &lineno
, NULL
, 0))
2950 err(1, "%s", filename
);
2955 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
2956 if (cp
[0] == '\0') {
2961 /* get config option */
2962 wordlen
= strcspn(cp
, "=[ \t\n");
2964 warnx("%s: line %zd: no option found",
2969 for (i
= 0; i
< LENGTH(configopt
); i
++) {
2970 opt
= &configopt
[i
];
2971 if (!strncasecmp(cp
, opt
->optname
, wordlen
) &&
2972 strlen(opt
->optname
) == wordlen
) {
2978 warnx("%s: line %zd: unknown option %.*s",
2979 filename
, lineno
, wordlen
, cp
);
2983 cp
+= strspn(cp
, " \t\n"); /* eat whitespace */
2984 /* get [selector] if any */
2988 wordlen
= strcspn(cp
, "]");
2991 warnx("%s: line %zd: syntax error",
2995 asprintf(&optsub
, "%.*s", wordlen
, cp
);
2998 cp
+= strspn(cp
, "] \t\n"); /* eat trailing */
3000 cp
+= strspn(cp
, "= \t\n"); /* eat trailing */
3002 optval
= strdup(cp
);
3003 /* call function to deal with it all */
3004 if (configopt
[optind
].func(optsub
, optval
,
3005 configopt
[optind
].funcflags
) != 0) {
3006 fprintf(stderr
, "%s line %zd: %s\n",
3007 filename
, lineno
, line
);
3008 errx(1, "%s: line %zd: invalid data for %s",
3009 filename
, lineno
, configopt
[optind
].optname
);
3016 DPRINTF("conf_load end\n");
3021 manage_window(Window id
)
3024 struct workspace
*ws
;
3026 int format
, i
, ws_idx
, n
;
3027 unsigned long nitems
, bytes
;
3028 Atom ws_idx_atom
= 0, type
;
3029 Atom
*prot
= NULL
, *pp
;
3030 unsigned char ws_idx_str
[SWM_PROPLEN
], *prop
= NULL
;
3031 struct swm_region
*r
;
3036 if ((win
= find_window(id
)) != NULL
)
3037 return (win
); /* already being managed */
3039 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
3040 errx(1, "calloc: failed to allocate memory for new window");
3042 /* Get all the window data in one shot */
3043 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3045 XGetWindowProperty(display
, id
, ws_idx_atom
, 0, SWM_PROPLEN
,
3046 False
, XA_STRING
, &type
, &format
, &nitems
, &bytes
, &prop
);
3047 XGetWindowAttributes(display
, id
, &win
->wa
);
3048 XGetTransientForHint(display
, id
, &trans
);
3049 XGetWMNormalHints(display
, id
, &win
->sh
, &mask
); /* XXX function? */
3051 win
->transient
= trans
;
3052 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
3053 (unsigned)win
->id
, win
->transient
);
3055 /* get supported protocols */
3056 if (XGetWMProtocols(display
, id
, &prot
, &n
)) {
3057 for (i
= 0, pp
= prot
; i
< n
; i
++, pp
++)
3059 win
->can_delete
= 1;
3065 * Figure out where to put the window. If it was previously assigned to
3066 * a workspace (either by spawn() or manually moving), and isn't
3067 * transient, * put it in the same workspace
3069 r
= root_to_region(win
->wa
.root
);
3070 if (prop
&& win
->transient
== 0) {
3071 DNPRINTF(SWM_D_PROP
, "got property _SWM_WS=%s\n", prop
);
3072 ws_idx
= strtonum(prop
, 0, 9, &errstr
);
3074 DNPRINTF(SWM_D_EVENT
, "window idx is %s: %s",
3077 ws
= &r
->s
->ws
[ws_idx
];
3081 /* set up the window layout */
3084 win
->s
= r
->s
; /* this never changes */
3085 TAILQ_INSERT_TAIL(&ws
->winlist
, win
, entry
);
3087 win
->g
.w
= win
->wa
.width
;
3088 win
->g
.h
= win
->wa
.height
;
3089 win
->g
.x
= win
->wa
.x
;
3090 win
->g
.y
= win
->wa
.y
;
3092 /* Set window properties so we can remember this after reincarnation */
3093 if (ws_idx_atom
&& prop
== NULL
&&
3094 snprintf(ws_idx_str
, SWM_PROPLEN
, "%d", ws
->idx
) < SWM_PROPLEN
) {
3095 DNPRINTF(SWM_D_PROP
, "setting property _SWM_WS to %s\n",
3097 XChangeProperty(display
, win
->id
, ws_idx_atom
, XA_STRING
, 8,
3098 PropModeReplace
, ws_idx_str
, SWM_PROPLEN
);
3102 if (XGetClassHint(display
, win
->id
, &win
->ch
)) {
3103 DNPRINTF(SWM_D_CLASS
, "class: %s name: %s\n",
3104 win
->ch
.res_class
, win
->ch
.res_name
);
3105 for (i
= 0; i
< quirks_length
; i
++){
3106 if (!strcmp(win
->ch
.res_class
, quirks
[i
].class) &&
3107 !strcmp(win
->ch
.res_name
, quirks
[i
].name
)) {
3108 DNPRINTF(SWM_D_CLASS
, "found: %s name: %s\n",
3109 win
->ch
.res_class
, win
->ch
.res_name
);
3110 if (quirks
[i
].quirk
& SWM_Q_FLOAT
)
3112 win
->quirks
= quirks
[i
].quirk
;
3117 /* alter window position if quirky */
3118 if (win
->quirks
& SWM_Q_ANYWHERE
) {
3119 win
->manual
= 1; /* don't center the quirky windows */
3120 bzero(&wc
, sizeof wc
);
3122 if (win
->g
.y
< bar_height
) {
3123 win
->g
.y
= wc
.y
= bar_height
;
3126 if (win
->g
.w
+ win
->g
.x
> WIDTH(r
)) {
3127 win
->g
.x
= wc
.x
= WIDTH(win
->ws
->r
) - win
->g
.w
- 2;
3130 wc
.border_width
= 1;
3131 mask
|= CWBorderWidth
;
3132 XConfigureWindow(display
, win
->id
, mask
, &wc
);
3135 /* Reset font sizes (the bruteforce way; no default keybinding). */
3136 if (win
->quirks
& SWM_Q_XTERM_FONTADJ
) {
3137 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
3138 fake_keypress(win
, XK_KP_Subtract
, ShiftMask
);
3139 for (i
= 0; i
< SWM_MAX_FONT_STEPS
; i
++)
3140 fake_keypress(win
, XK_KP_Add
, ShiftMask
);
3143 XSelectInput(display
, id
, EnterWindowMask
| FocusChangeMask
|
3144 PropertyChangeMask
| StructureNotifyMask
);
3146 set_win_state(win
, NormalState
);
3148 /* floaters need to be mapped if they are in the current workspace */
3149 if (win
->floating
&& (ws
->idx
== r
->ws
->idx
))
3150 XMapRaised(display
, win
->id
);
3152 /* make new win focused */
3159 unmanage_window(struct ws_win
*win
)
3161 struct workspace
*ws
;
3166 DNPRINTF(SWM_D_MISC
, "unmanage_window: %lu\n", win
->id
);
3170 /* find a window to focus */
3171 if (ws
->focus
== win
)
3172 ws
->focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
3173 if (ws
->focus
== NULL
)
3174 ws
->focus
= TAILQ_FIRST(&ws
->winlist
);
3175 if (ws
->focus
== NULL
|| ws
->focus
== win
) {
3179 focus_win(ws
->focus
);
3180 if (ws
->focus_prev
== win
)
3181 ws
->focus_prev
= NULL
;
3183 TAILQ_REMOVE(&win
->ws
->winlist
, win
, entry
);
3184 set_win_state(win
, WithdrawnState
);
3185 if (win
->ch
.res_class
)
3186 XFree(win
->ch
.res_class
);
3187 if (win
->ch
.res_name
)
3188 XFree(win
->ch
.res_name
);
3193 configurerequest(XEvent
*e
)
3195 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
3200 if ((win
= find_window(ev
->window
)) == NULL
)
3204 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
3206 bzero(&wc
, sizeof wc
);
3209 wc
.width
= ev
->width
;
3210 wc
.height
= ev
->height
;
3211 wc
.border_width
= ev
->border_width
;
3212 wc
.sibling
= ev
->above
;
3213 wc
.stack_mode
= ev
->detail
;
3214 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
3216 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
3218 if (win
->floating
) {
3219 if (ev
->value_mask
& CWX
)
3221 if (ev
->value_mask
& CWY
)
3223 if (ev
->value_mask
& CWWidth
)
3224 win
->g
.w
= ev
->width
;
3225 if (ev
->value_mask
& CWHeight
)
3226 win
->g
.h
= ev
->height
;
3227 if (win
->ws
->r
!= NULL
) {
3228 /* this seems to be full screen */
3229 if (win
->g
.w
>= WIDTH(win
->ws
->r
)) {
3231 win
->g
.w
= WIDTH(win
->ws
->r
);
3232 ev
->value_mask
|= CWX
| CWWidth
;
3234 if (win
->g
.h
>= HEIGHT(win
->ws
->r
)) {
3237 win
->g
.h
= HEIGHT(win
->ws
->r
);
3238 ev
->value_mask
|= CWY
| CWHeight
;
3241 if ((ev
->value_mask
& (CWX
| CWY
)) &&
3242 !(ev
->value_mask
& (CWWidth
| CWHeight
)))
3244 XMoveResizeWindow(display
, win
->id
,
3245 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
3252 configurenotify(XEvent
*e
)
3257 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
3258 e
->xconfigure
.window
);
3260 XMapWindow(display
, e
->xconfigure
.window
);
3261 win
= find_window(e
->xconfigure
.window
);
3263 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
3265 XMapWindow(display
, win
->id
);
3272 destroynotify(XEvent
*e
)
3275 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
3277 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
3279 if ((win
= find_window(ev
->window
)) != NULL
) {
3280 unmanage_window(win
);
3286 enternotify(XEvent
*e
)
3288 XCrossingEvent
*ev
= &e
->xcrossing
;
3291 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
3294 /* eat event(r) to prevent autofocus */
3299 if ((win
= find_window(ev
->window
)) != NULL
)
3306 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", e
->xfocus
.window
);
3312 struct swm_screen
*s
;
3317 DNPRINTF(SWM_D_EVENT
, "focusout: window: %lu\n", e
->xfocus
.window
);
3319 if (cur_focus
&& cur_focus
->ws
->r
&&
3320 cur_focus
->id
== e
->xfocus
.window
) {
3321 s
= cur_focus
->ws
->r
->s
;
3322 if (XQueryPointer(display
, cur_focus
->id
,
3323 &rr
, &cr
, &x
, &y
, &wx
, &wy
, &mask
) != False
&&
3325 x
== DisplayWidth(display
, s
->idx
) / 2 &&
3326 y
== DisplayHeight(display
, s
->idx
) / 2)
3327 unfocus_win(cur_focus
);
3332 mappingnotify(XEvent
*e
)
3334 XMappingEvent
*ev
= &e
->xmapping
;
3336 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
3338 XRefreshKeyboardMapping(ev
);
3339 if (ev
->request
== MappingKeyboard
)
3344 maprequest(XEvent
*e
)
3346 XMapRequestEvent
*ev
= &e
->xmaprequest
;
3347 XWindowAttributes wa
;
3349 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
3350 e
->xmaprequest
.window
);
3352 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
3354 if (wa
.override_redirect
)
3356 manage_window(e
->xmaprequest
.window
);
3362 propertynotify(XEvent
*e
)
3365 XPropertyEvent
*ev
= &e
->xproperty
;
3367 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
3370 if (ev
->state
== PropertyDelete
)
3371 return; /* ignore */
3372 win
= find_window(ev
->window
);
3377 case XA_WM_NORMAL_HINTS
:
3380 XGetWMNormalHints(display
, win
->id
, &win
->sh
, &mask
);
3381 fprintf(stderr
, "normal hints: flag 0x%x\n", win
->sh
.flags
);
3382 if (win
->sh
.flags
& PMinSize
) {
3383 win
->g
.w
= win
->sh
.min_width
;
3384 win
->g
.h
= win
->sh
.min_height
;
3385 fprintf(stderr
, "min %d %d\n", win
->g
.w
, win
->g
.h
);
3387 XMoveResizeWindow(display
, win
->id
,
3388 win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
3397 unmapnotify(XEvent
*e
)
3399 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
3403 visibilitynotify(XEvent
*e
)
3406 struct swm_region
*r
;
3408 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n",
3409 e
->xvisibility
.window
);
3410 if (e
->xvisibility
.state
== VisibilityUnobscured
)
3411 for (i
= 0; i
< ScreenCount(display
); i
++)
3412 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3413 if (e
->xvisibility
.window
== r
->bar_window
)
3418 xerror_start(Display
*d
, XErrorEvent
*ee
)
3425 xerror(Display
*d
, XErrorEvent
*ee
)
3427 /* fprintf(stderr, "error: %p %p\n", display, ee); */
3435 xerrorxlib
= XSetErrorHandler(xerror_start
);
3437 /* this causes an error if some other window manager is running */
3438 XSelectInput(display
, DefaultRootWindow(display
),
3439 SubstructureRedirectMask
);
3440 XSync(display
, False
);
3444 XSetErrorHandler(xerror
);
3445 XSync(display
, False
);
3454 unsigned char *p
= NULL
;
3455 unsigned long n
, extra
;
3458 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
3459 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
3460 if (status
!= Success
)
3463 result
= *((long *)p
);
3469 new_region(struct swm_screen
*s
, int x
, int y
, int w
, int h
)
3471 struct swm_region
*r
, *n
;
3472 struct workspace
*ws
= NULL
;
3475 DNPRINTF(SWM_D_MISC
, "new region: screen[%d]:%dx%d+%d+%d\n",
3476 s
->idx
, w
, h
, x
, y
);
3478 /* remove any conflicting regions */
3479 n
= TAILQ_FIRST(&s
->rl
);
3482 n
= TAILQ_NEXT(r
, entry
);
3483 if (X(r
) < (x
+ w
) &&
3484 (X(r
) + WIDTH(r
)) > x
&&
3486 (Y(r
) + HEIGHT(r
)) > y
) {
3487 XDestroyWindow(display
, r
->bar_window
);
3488 TAILQ_REMOVE(&s
->rl
, r
, entry
);
3489 TAILQ_INSERT_TAIL(&s
->orl
, r
, entry
);
3493 /* search old regions for one to reuse */
3495 /* size + location match */
3496 TAILQ_FOREACH(r
, &s
->orl
, entry
)
3497 if (X(r
) == x
&& Y(r
) == y
&&
3498 HEIGHT(r
) == h
&& WIDTH(r
) == w
)
3502 TAILQ_FOREACH(r
, &s
->orl
, entry
)
3503 if (HEIGHT(r
) == h
&& WIDTH(r
) == w
)
3507 TAILQ_REMOVE(&s
->orl
, r
, entry
);
3508 /* try to use old region's workspace */
3509 if (r
->ws
->r
== NULL
)
3512 if ((r
= calloc(1, sizeof(struct swm_region
))) == NULL
)
3513 errx(1, "calloc: failed to allocate memory for screen");
3515 /* if we don't have a workspace already, find one */
3517 for (i
= 0; i
< SWM_WS_MAX
; i
++)
3518 if (s
->ws
[i
].r
== NULL
) {
3525 errx(1, "no free workspaces\n");
3534 TAILQ_INSERT_TAIL(&s
->rl
, r
, entry
);
3540 #ifdef SWM_XRR_HAS_CRTC
3542 XRRScreenResources
*sr
;
3545 #endif /* SWM_XRR_HAS_CRTC */
3546 struct swm_region
*r
;
3549 if (i
>= ScreenCount(display
))
3550 errx(1, "invalid screen");
3552 /* remove any old regions */
3553 while ((r
= TAILQ_FIRST(&screens
[i
].rl
)) != NULL
) {
3555 XDestroyWindow(display
, r
->bar_window
);
3556 TAILQ_REMOVE(&screens
[i
].rl
, r
, entry
);
3557 TAILQ_INSERT_TAIL(&screens
[i
].orl
, r
, entry
);
3560 /* map virtual screens onto physical screens */
3561 #ifdef SWM_XRR_HAS_CRTC
3562 if (xrandr_support
) {
3563 sr
= XRRGetScreenResources(display
, screens
[i
].root
);
3565 new_region(&screens
[i
], 0, 0,
3566 DisplayWidth(display
, i
),
3567 DisplayHeight(display
, i
));
3571 for (c
= 0, ci
= NULL
; c
< ncrtc
; c
++) {
3572 ci
= XRRGetCrtcInfo(display
, sr
, sr
->crtcs
[c
]);
3573 if (ci
->noutput
== 0)
3576 if (ci
!= NULL
&& ci
->mode
== None
)
3577 new_region(&screens
[i
], 0, 0,
3578 DisplayWidth(display
, i
),
3579 DisplayHeight(display
, i
));
3581 new_region(&screens
[i
],
3582 ci
->x
, ci
->y
, ci
->width
, ci
->height
);
3585 XRRFreeCrtcInfo(ci
);
3586 XRRFreeScreenResources(sr
);
3588 #endif /* SWM_XRR_HAS_CRTC */
3590 new_region(&screens
[i
], 0, 0, DisplayWidth(display
, i
),
3591 DisplayHeight(display
, i
));
3596 screenchange(XEvent
*e
) {
3597 XRRScreenChangeNotifyEvent
*xe
= (XRRScreenChangeNotifyEvent
*)e
;
3598 struct swm_region
*r
;
3602 DNPRINTF(SWM_D_EVENT
, "screenchange: %lu\n", xe
->root
);
3604 if (!XRRUpdateConfiguration(e
))
3607 /* silly event doesn't include the screen index */
3608 for (i
= 0; i
< ScreenCount(display
); i
++)
3609 if (screens
[i
].root
== xe
->root
)
3611 if (i
>= ScreenCount(display
))
3612 errx(1, "screenchange: screen not found\n");
3614 /* brute force for now, just re-enumerate the regions */
3617 /* hide any windows that went away */
3618 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3619 TAILQ_FOREACH(win
, &r
->ws
->winlist
, entry
)
3620 XUnmapWindow(display
, win
->id
);
3622 /* add bars to all regions */
3623 for (i
= 0; i
< ScreenCount(display
); i
++)
3624 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3632 Window d1
, d2
, *wins
= NULL
;
3633 XWindowAttributes wa
;
3636 int errorbase
, major
, minor
;
3637 struct workspace
*ws
;
3641 if ((screens
= calloc(ScreenCount(display
),
3642 sizeof(struct swm_screen
))) == NULL
)
3643 errx(1, "calloc: screens");
3645 ws_idx_atom
= XInternAtom(display
, "_SWM_WS", False
);
3647 /* initial Xrandr setup */
3648 xrandr_support
= XRRQueryExtension(display
,
3649 &xrandr_eventbase
, &errorbase
);
3651 if (XRRQueryVersion(display
, &major
, &minor
) && major
< 1)
3654 /* map physical screens */
3655 for (i
= 0; i
< ScreenCount(display
); i
++) {
3656 DNPRINTF(SWM_D_WS
, "setup_screens: init screen %d\n", i
);
3658 TAILQ_INIT(&screens
[i
].rl
);
3659 TAILQ_INIT(&screens
[i
].orl
);
3660 screens
[i
].root
= RootWindow(display
, i
);
3662 /* set default colors */
3663 setscreencolor("red", i
+ 1, SWM_S_COLOR_FOCUS
);
3664 setscreencolor("rgb:88/88/88", i
+ 1, SWM_S_COLOR_UNFOCUS
);
3665 setscreencolor("rgb:00/80/80", i
+ 1, SWM_S_COLOR_BAR_BORDER
);
3666 setscreencolor("black", i
+ 1, SWM_S_COLOR_BAR
);
3667 setscreencolor("rgb:a0/a0/a0", i
+ 1, SWM_S_COLOR_BAR_FONT
);
3669 /* init all workspaces */
3670 /* XXX these should be dynamically allocated too */
3671 for (j
= 0; j
< SWM_WS_MAX
; j
++) {
3672 ws
= &screens
[i
].ws
[j
];
3677 TAILQ_INIT(&ws
->winlist
);
3679 for (k
= 0; layouts
[k
].l_stack
!= NULL
; k
++)
3680 if (layouts
[k
].l_config
!= NULL
)
3681 layouts
[k
].l_config(ws
,
3682 SWM_ARG_ID_STACKINIT
);
3683 ws
->cur_layout
= &layouts
[0];
3685 /* grab existing windows (before we build the bars)*/
3686 if (!XQueryTree(display
, screens
[i
].root
, &d1
, &d2
, &wins
, &no
))
3692 XRRSelectInput(display
, screens
[i
].root
,
3693 RRScreenChangeNotifyMask
);
3695 /* attach windows to a region */
3696 /* normal windows */
3697 for (j
= 0; j
< no
; j
++) {
3698 XGetWindowAttributes(display
, wins
[j
], &wa
);
3699 if (!XGetWindowAttributes(display
, wins
[j
], &wa
) ||
3700 wa
.override_redirect
||
3701 XGetTransientForHint(display
, wins
[j
], &d1
))
3704 if (wa
.map_state
== IsViewable
||
3705 getstate(wins
[j
]) == NormalState
)
3706 manage_window(wins
[j
]);
3708 /* transient windows */
3709 for (j
= 0; j
< no
; j
++) {
3710 if (!XGetWindowAttributes(display
, wins
[j
], &wa
))
3713 if (XGetTransientForHint(display
, wins
[j
], &d1
) &&
3714 (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) ==
3716 manage_window(wins
[j
]);
3727 if ((bar_fonts
[0] = strdup("-*-terminus-medium-*-*-*-*-*-*-*-*-*-*-*"))
3729 err(1, "setup_globals: strdup");
3730 if ((bar_fonts
[1] = strdup("-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"))
3732 err(1, "setup_globals: strdup");
3733 if ((spawn_term
[0] = strdup("xterm")) == NULL
)
3734 err(1, "setup_globals: strdup");
3741 Atom netwmcheck
, netwmname
, utf8_string
;
3744 /* work around sun jdk bugs, code from wmname */
3745 netwmcheck
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
3746 netwmname
= XInternAtom(display
, "_NET_WM_NAME", False
);
3747 utf8_string
= XInternAtom(display
, "UTF8_STRING", False
);
3748 for (i
= 0; i
< ScreenCount(display
); i
++) {
3749 root
= screens
[i
].root
;
3750 XChangeProperty(display
, root
, netwmcheck
, XA_WINDOW
, 32,
3751 PropModeReplace
, (unsigned char *)&root
, 1);
3752 XChangeProperty(display
, root
, netwmname
, utf8_string
, 8,
3753 PropModeReplace
, "LG3D", strlen("LG3D"));
3758 main(int argc
, char *argv
[])
3761 struct swm_region
*r
;
3762 char conf
[PATH_MAX
], *cfile
= NULL
;
3769 fprintf(stderr
, "Welcome to scrotwm V%s cvs tag: %s\n",
3770 SWM_VERSION
, cvstag
);
3771 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
3772 warnx("no locale support");
3774 if (!(display
= XOpenDisplay(0)))
3775 errx(1, "can not open display");
3778 errx(1, "other wm running");
3780 astate
= XInternAtom(display
, "WM_STATE", False
);
3781 aprot
= XInternAtom(display
, "WM_PROTOCOLS", False
);
3782 adelete
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
3784 /* look for local and global conf file */
3785 pwd
= getpwuid(getuid());
3787 errx(1, "invalid user %d", getuid());
3795 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
3796 if (stat(conf
, &sb
) != -1) {
3797 if (S_ISREG(sb
.st_mode
))
3800 /* try global conf file */
3801 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
3802 if (!stat(conf
, &sb
))
3803 if (S_ISREG(sb
.st_mode
))
3809 /* setup all bars */
3810 for (i
= 0; i
< ScreenCount(display
); i
++)
3811 TAILQ_FOREACH(r
, &screens
[i
].rl
, entry
)
3814 /* set some values to work around bad programs */
3820 xfd
= ConnectionNumber(display
);
3824 if (select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
3826 errx(1, "select failed");
3831 while (XPending(display
)) {
3832 XNextEvent(display
, &e
);
3833 if (e
.type
< LASTEvent
) {
3834 if (handler
[e
.type
])
3835 handler
[e
.type
](&e
);
3837 DNPRINTF(SWM_D_EVENT
,
3838 "win: %lu unknown event: %d\n",
3839 e
.xany
.window
, e
.type
);
3841 switch (e
.type
- xrandr_eventbase
) {
3842 case RRScreenChangeNotify
:
3846 DNPRINTF(SWM_D_EVENT
,
3847 "win: %lu unknown xrandr event: "
3848 "%d\n", e
.xany
.window
, e
.type
);
3855 XCloseDisplay(display
);