]>
code.delx.au - spectrwm/blob - scrotwm.c
3 * Copyright (c) 2009 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2009 Ryan McBride <mcbride@countersiege.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Much code and ideas taken from dwm under the following license:
20 * MIT/X Consortium License
22 * 2006-2008 Anselm R Garbe <garbeam at gmail dot com>
23 * 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
24 * 2006-2007 Jukka Salmi <jukka at salmi dot ch>
25 * 2007 Premysl Hruby <dfenze at gmail dot com>
26 * 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
27 * 2007 Christof Musik <christof at sendfax dot de>
28 * 2007-2008 Enno Gottox Boland <gottox at s01 dot de>
29 * 2007-2008 Peter Hartlich <sgkkr at hartlich dot com>
30 * 2008 Martin Hurton <martin dot hurton at gmail dot com>
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
39 * The above copyright notice and this permission notice shall be included in
40 * all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48 * DEALINGS IN THE SOFTWARE.
51 #define SWM_VERSION "0.5"
64 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/param.h>
70 #include <X11/cursorfont.h>
71 #include <X11/keysym.h>
72 #include <X11/Xatom.h>
74 #include <X11/Xproto.h>
75 #include <X11/Xutil.h>
77 /* #define SWM_DEBUG */
79 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
80 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
81 #define SWM_D_MISC 0x0001
82 #define SWM_D_EVENT 0x0002
83 #define SWM_D_WS 0x0004
84 #define SWM_D_FOCUS 0x0008
85 #define SWM_D_MOVE 0x0010
87 u_int32_t swm_debug
= 0
96 #define DNPRINTF(n,x...)
99 #define LENGTH(x) (sizeof x / sizeof x[0])
100 #define MODKEY Mod1Mask
101 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
104 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
108 int ignore_enter
= 0;
109 unsigned int numlockmask
= 0;
110 unsigned long color_focus
= 0xff0000; /* XXX should this be per ws? */
111 unsigned long color_unfocus
= 0x888888;
118 unsigned long bar_border
= 0x008080;
119 unsigned long bar_color
= 0x000000;
120 unsigned long bar_font_color
= 0xa0a0a0;
126 char *bar_fonts
[] = {
127 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
128 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
132 /* terminal + args */
133 char *spawn_term
[] = { "xterm", NULL
};
134 char *spawn_menu
[] = { "dmenu_run", NULL
};
136 /* layout manager data */
137 struct swm_geometry
{
146 TAILQ_ENTRY(ws_win
) entry
;
148 struct swm_geometry g
;
151 XWindowAttributes wa
;
154 TAILQ_HEAD(ws_win_list
, ws_win
);
156 /* layout handlers */
158 void vertical_init(int);
159 void vertical_resize(int);
160 void vertical_stack(struct swm_geometry
*);
161 void horizontal_init(int);
162 void horizontal_resize(int);
163 void horizontal_stack(struct swm_geometry
*);
165 void max_focus(struct ws_win
*);
166 void max_stack(struct swm_geometry
*);
169 void (*l_init
)(int); /* init/reset */
170 void (*l_stack
)(struct swm_geometry
*);
171 void (*l_resize
)(int);
172 void (*l_focus
)(struct ws_win
*);
174 /* init stack, resize */
175 { vertical_init
, vertical_stack
, vertical_resize
, NULL
},
176 { horizontal_init
, horizontal_stack
, horizontal_resize
, NULL
},
177 { NULL
, max_stack
, NULL
, max_focus
},
178 { NULL
, NULL
, NULL
, NULL
},
182 /* define work spaces */
183 #define SWM_WS_MAX (10)
185 int visible
; /* workspace visible */
186 int restack
; /* restack on switch */
187 struct swm_geometry g
;
188 struct layout
*cur_layout
; /* current layout handlers */
189 struct ws_win
*focus
; /* which win has focus */
190 struct ws_win_list winlist
; /* list of windows in ws */
194 /* args to functions */
197 #define SWM_ARG_ID_FOCUSNEXT (0)
198 #define SWM_ARG_ID_FOCUSPREV (1)
199 #define SWM_ARG_ID_FOCUSMAIN (2)
200 #define SWM_ARG_ID_SWAPNEXT (3)
201 #define SWM_ARG_ID_SWAPPREV (4)
202 #define SWM_ARG_ID_SWAPMAIN (5)
203 #define SWM_ARG_ID_MASTERSHRINK (6)
204 #define SWM_ARG_ID_MASTERGROW (7)
208 /* conf file stuff */
209 #define SWM_CONF_WS "\n= \t"
210 #define SWM_CONF_FILE "scrotwm.conf"
212 conf_load(char *filename
)
215 char *line
, *cp
, *var
, *val
;
216 size_t len
, lineno
= 0;
218 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
220 if (filename
== NULL
)
223 if ((config
= fopen(filename
, "r")) == NULL
)
227 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
231 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
237 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
239 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
240 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
243 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
246 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
247 bar_enabled
= atoi(val
);
248 else if (!strncmp(var
, "bar_border",
249 strlen("bar_border")))
250 bar_border
= strtol(val
, NULL
, 16);
251 else if (!strncmp(var
, "bar_color",
252 strlen("bar_color")))
253 bar_color
= strtol(val
, NULL
, 16);
254 else if (!strncmp(var
, "bar_font_color",
255 strlen("bar_font_color")))
256 bar_font_color
= strtol(val
, NULL
, 16);
257 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
258 asprintf(&bar_fonts
[0], "%s", val
);
264 if (!strncmp(var
, "color_focus", strlen("color_focus")))
265 color_focus
= strtol(val
, NULL
, 16);
266 else if (!strncmp(var
, "color_unfocus",
267 strlen("color_unfocus")))
268 color_unfocus
= strtol(val
, NULL
, 16);
274 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
275 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
286 errx(1, "invalid conf file entry: %s=%s", var
, val
);
295 if (bar_enabled
== 0)
299 XSetForeground(display
, bar_gc
, bar_color
);
300 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
305 localtime_r(&tmt
, &tm
);
306 strftime(bar_text
, sizeof bar_text
, "%a %b %d %R %Z %Y", &tm
);
307 XSetForeground(display
, bar_gc
, bar_font_color
);
308 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
310 XSync(display
, False
);
318 /* XXX yeah yeah byte me */
323 bar_toggle(union arg
*args
)
327 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
331 XUnmapWindow(display
, bar_window
);
334 XMapRaised(display
, bar_window
);
336 XSync(display
, False
);
337 for (i
= 0; i
< SWM_WS_MAX
; i
++)
341 bar_print(); /* must be after stack */
349 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
350 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
354 if (bar_fonts
[i
] == NULL
)
355 errx(1, "couldn't load font");
356 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
358 bar_window
= XCreateSimpleWindow(display
, root
, 0, 0,
359 ws
[current_ws
].g
.w
, bar_height
- 2, 1, bar_border
, bar_color
);
360 bar_gc
= XCreateGC(display
, bar_window
, 0, &bar_gcv
);
361 XSetFont(display
, bar_gc
, bar_fs
->fid
);
362 XSelectInput(display
, bar_window
, VisibilityChangeMask
);
364 XMapRaised(display
, bar_window
);
366 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %d\n", (int)bar_window
);
368 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
369 err(1, "could not install bar_signal");
374 config_win(struct ws_win
*win
)
378 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
379 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
380 ce
.type
= ConfigureNotify
;
381 ce
.display
= display
;
387 ce
.height
= win
->g
.h
;
388 ce
.border_width
= 1; /* XXX store this! */
390 ce
.override_redirect
= False
;
391 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
395 count_win(int wsid
, int count_transient
)
400 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
) {
401 if (count_transient
== 0 && win
->floating
)
403 if (count_transient
== 0 && win
->transient
)
407 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
412 quit(union arg
*args
)
414 DNPRINTF(SWM_D_MISC
, "quit\n");
419 restart(union arg
*args
)
421 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
423 XCloseDisplay(display
);
424 execvp(start_argv
[0], start_argv
);
425 fprintf(stderr
, "execvp failed\n");
431 spawn(union arg
*args
)
433 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
435 * The double-fork construct avoids zombie processes and keeps the code
436 * clean from stupid signal handlers.
441 close(ConnectionNumber(display
));
443 execvp(args
->argv
[0], args
->argv
);
444 fprintf(stderr
, "execvp failed\n");
453 focus_win(struct ws_win
*win
)
455 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
456 XSetWindowBorder(display
, win
->id
, color_focus
);
457 XSetInputFocus(display
, win
->id
, RevertToPointerRoot
, CurrentTime
);
458 ws
[current_ws
].focus
= win
;
462 unfocus_win(struct ws_win
*win
)
464 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
465 XSetWindowBorder(display
, win
->id
, color_unfocus
);
466 if (ws
[current_ws
].focus
== win
)
467 ws
[current_ws
].focus
= NULL
;
471 switchws(union arg
*args
)
476 DNPRINTF(SWM_D_WS
, "switchws: %d\n", wsid
+ 1);
478 if (wsid
== current_ws
)
481 /* map new window first to prevent ugly blinking */
482 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
)
483 XMapRaised(display
, win
->id
);
484 ws
[wsid
].visible
= 1;
486 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
)
487 XUnmapWindow(display
, win
->id
);
488 ws
[current_ws
].visible
= 0;
493 if (ws
[wsid
].restack
) {
497 if (ws
[wsid
].focus
!= NULL
)
498 focus_win(ws
[wsid
].focus
);
499 XSync(display
, False
);
504 swapwin(union arg
*args
)
506 struct ws_win
*target
;
508 DNPRINTF(SWM_D_MOVE
, "swapwin: id %d\n", args
->id
);
509 if (ws
[current_ws
].focus
== NULL
)
513 case SWM_ARG_ID_SWAPPREV
:
514 target
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
515 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
516 ws
[current_ws
].focus
, entry
);
518 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
,
519 ws
[current_ws
].focus
, entry
);
521 TAILQ_INSERT_BEFORE(target
, ws
[current_ws
].focus
,
524 case SWM_ARG_ID_SWAPNEXT
:
525 target
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
526 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
527 ws
[current_ws
].focus
, entry
);
529 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
530 ws
[current_ws
].focus
, entry
);
532 TAILQ_INSERT_AFTER(&ws
[current_ws
].winlist
, target
,
533 ws
[current_ws
].focus
, entry
);
535 case SWM_ARG_ID_SWAPMAIN
:
536 target
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
537 if (target
== ws
[current_ws
].focus
)
539 TAILQ_REMOVE(&ws
[current_ws
].winlist
, target
, entry
);
540 TAILQ_INSERT_BEFORE(ws
[current_ws
].focus
, target
, entry
);
541 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
542 ws
[current_ws
].focus
, entry
);
543 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
544 ws
[current_ws
].focus
, entry
);
547 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
556 focus(union arg
*args
)
558 struct ws_win
*winfocus
, *winlostfocus
;
560 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
561 if (ws
[current_ws
].focus
== NULL
)
564 winlostfocus
= ws
[current_ws
].focus
;
567 case SWM_ARG_ID_FOCUSPREV
:
568 winfocus
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
569 if (winfocus
== NULL
)
571 TAILQ_LAST(&ws
[current_ws
].winlist
, ws_win_list
);
574 case SWM_ARG_ID_FOCUSNEXT
:
575 winfocus
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
576 if (winfocus
== NULL
)
577 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
580 case SWM_ARG_ID_FOCUSMAIN
:
581 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
588 if (winfocus
== winlostfocus
)
591 unfocus_win(winlostfocus
);
593 /* XXX if we hook in focus_win(), we get a nasty cycle */
594 if (ws
[current_ws
].cur_layout
->l_focus
!= NULL
)
595 ws
[current_ws
].cur_layout
->l_focus(winfocus
);
596 XSync(display
, False
);
600 cycle_layout(union arg
*args
)
602 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", current_ws
);
604 ws
[current_ws
].cur_layout
++;
605 if (ws
[current_ws
].cur_layout
->l_stack
== NULL
)
606 ws
[current_ws
].cur_layout
= &layouts
[0];
613 resize_master(union arg
*args
)
615 DNPRINTF(SWM_D_EVENT
, "resize_master: id %d\n", args
->id
);
617 if (ws
[current_ws
].cur_layout
->l_resize
!= NULL
)
618 ws
[current_ws
].cur_layout
->l_resize(args
->id
);
622 stack_reset(union arg
*args
)
624 DNPRINTF(SWM_D_EVENT
, "stack_reset: ws %d\n", current_ws
);
626 if (ws
[current_ws
].cur_layout
->l_init
!= NULL
) {
627 ws
[current_ws
].cur_layout
->l_init(current_ws
);
634 struct swm_geometry g
;
635 DNPRINTF(SWM_D_EVENT
, "stack: workspace: %d\n", current_ws
);
637 /* start with workspace geometry, adjust for bar */
638 g
= ws
[current_ws
].g
;
644 ws
[current_ws
].restack
= 0;
645 ws
[current_ws
].cur_layout
->l_stack(&g
);
646 XSync(display
, False
);
650 stack_floater(struct ws_win
*win
)
656 bzero(&wc
, sizeof wc
);
658 mask
= CWX
| CWY
| CWBorderWidth
;
660 win
->g
.w
= wc
.width
= win
->wa
.width
;
661 win
->g
.h
= wc
.height
= win
->wa
.height
;
662 win
->g
.x
= wc
.x
= (ws
[current_ws
].g
.w
- win
->wa
.width
) / 2;
663 win
->g
.y
= wc
.y
= (ws
[current_ws
].g
.h
- win
->wa
.height
) / 2;
665 DNPRINTF(SWM_D_EVENT
, "stack_floater: win %d x %d y %d w %d h %d\n",
666 win
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
668 XConfigureWindow(display
, win
->id
, mask
, &wc
);
671 bzero(&wc
, sizeof wc
);
673 mask
= CWX
| CWY
| CWBorderWidth
;
675 /* use obsolete width height */
676 if (win
->sh
.flags
& USPosition
) {
677 win
->g
.w
= wc
.width
= win
->sh
.width
;
678 win
->g
.h
= wc
.height
= win
->sh
.height
;
679 mask
|= CWWidth
| CWHeight
;
683 if (win
->sh
.flags
& PMinSize
) {
684 /* some hints are retarded */
685 if (win
->sh
.min_width
< ws
[current_ws
].g
.w
/ 10)
686 win
->sh
.min_width
= ws
[current_ws
].g
.w
/ 3;
687 if (win
->sh
.min_height
< ws
[current_ws
].g
.h
/ 10)
688 win
->sh
.height
= ws
[current_ws
].g
.h
/ 3;
690 win
->g
.w
= wc
.width
= win
->sh
.min_width
* 2;
691 win
->g
.h
= wc
.height
= win
->sh
.min_height
* 2;
692 mask
|= CWWidth
| CWHeight
;
694 if (win
->sh
.flags
& PMaxSize
) {
695 /* potentially override min values */
696 if (win
->sh
.max_width
< ws
[current_ws
].g
.w
) {
697 win
->g
.w
= wc
.width
= win
->sh
.max_width
;
700 if (win
->sh
.max_height
< ws
[current_ws
].g
.h
) {
701 win
->g
.h
= wc
.height
= win
->sh
.max_height
;
706 /* make sure we don't clobber the screen */
707 if ((mask
& CWWidth
) && win
->wa
.width
> ws
[current_ws
].g
.w
)
708 win
->wa
.width
= ws
[current_ws
].g
.w
- 4;
709 if ((mask
& CWHeight
) && win
->wa
.height
> ws
[current_ws
].g
.h
)
710 win
->wa
.height
= ws
[current_ws
].g
.h
- 4;
712 /* supposed to be obsolete */
713 if (win
->sh
.flags
& USPosition
) {
714 win
->g
.x
= wc
.x
= win
->sh
.x
;
715 win
->g
.y
= wc
.y
= win
->sh
.y
;
717 win
->g
.x
= wc
.x
= (ws
[current_ws
].g
.w
- win
->wa
.width
) / 2;
718 win
->g
.y
= wc
.y
= (ws
[current_ws
].g
.h
- win
->wa
.height
) / 2;
720 DNPRINTF(SWM_D_EVENT
, "stack_floater: win %d x %d y %d w %d h %d\n",
721 win
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
723 XConfigureWindow(display
, win
->id
, mask
, &wc
);
727 int vertical_msize
[SWM_WS_MAX
];
730 vertical_init(int ws_idx
)
732 DNPRINTF(SWM_D_MISC
, "vertical_init: workspace: %d\n", current_ws
);
734 vertical_msize
[ws_idx
] = ws
[ws_idx
].g
.w
/ 2;
738 vertical_resize(int id
)
740 DNPRINTF(SWM_D_MISC
, "vertical_resize: workspace: %d\n", current_ws
);
743 case SWM_ARG_ID_MASTERSHRINK
:
744 vertical_msize
[current_ws
] -= ws
[current_ws
].g
.w
/ 32;
745 if ( vertical_msize
[current_ws
] < ws
[current_ws
].g
.w
/ 16)
746 vertical_msize
[current_ws
] = ws
[current_ws
].g
.w
/ 16;
748 case SWM_ARG_ID_MASTERGROW
:
749 vertical_msize
[current_ws
] += ws
[current_ws
].g
.w
/ 32;
750 if ( vertical_msize
[current_ws
] >
751 (ws
[current_ws
].g
.w
- (ws
[current_ws
].g
.w
/ 16)))
752 vertical_msize
[current_ws
] =
753 ws
[current_ws
].g
.w
- ws
[current_ws
].g
.w
/ 16;
762 vertical_stack(struct swm_geometry
*g
) {
764 struct swm_geometry gg
= *g
;
765 struct ws_win wf
, *win
, *winfocus
= &wf
;
769 DNPRINTF(SWM_D_EVENT
, "vertical_stack: workspace: %d\n", current_ws
);
771 winno
= count_win(current_ws
, 0);
777 gg
.w
= vertical_msize
[current_ws
];
780 hrh
= g
->h
/ (winno
- 1);
785 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
787 gg
.x
+= vertical_msize
[current_ws
] + 2;
788 gg
.w
= g
->w
- (vertical_msize
[current_ws
] + 2);
790 if (i
!= 0 && hrh
!= 0) {
791 /* correct the last window for lost pixels */
792 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
794 gg
.h
= hrh
+ (g
->h
- (i
* hrh
));
798 /* leave first right hand window at y = 0 */
804 if (win
->transient
!= 0 || win
->floating
!= 0)
807 bzero(&wc
, sizeof wc
);
809 win
->g
.x
= wc
.x
= gg
.x
;
810 win
->g
.y
= wc
.y
= gg
.y
;
811 win
->g
.w
= wc
.width
= gg
.w
;
812 win
->g
.h
= wc
.height
= gg
.h
;
813 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
814 XConfigureWindow(display
, win
->id
, mask
, &wc
);
817 if (win
== ws
[current_ws
].focus
)
821 XMapRaised(display
, win
->id
);
825 focus_win(winfocus
); /* this has to be done outside of the loop */
828 int horizontal_msize
[SWM_WS_MAX
];
831 horizontal_init(int ws_idx
)
833 DNPRINTF(SWM_D_MISC
, "horizontal_init: workspace: %d\n", current_ws
);
835 horizontal_msize
[ws_idx
] = ws
[ws_idx
].g
.h
/ 2;
839 horizontal_resize(int id
)
841 DNPRINTF(SWM_D_MISC
, "horizontal_resize: workspace: %d\n", current_ws
);
844 case SWM_ARG_ID_MASTERSHRINK
:
845 horizontal_msize
[current_ws
] -= ws
[current_ws
].g
.h
/ 32;
846 if ( horizontal_msize
[current_ws
] < ws
[current_ws
].g
.h
/ 16)
847 horizontal_msize
[current_ws
] = ws
[current_ws
].g
.h
/ 16;
849 case SWM_ARG_ID_MASTERGROW
:
850 horizontal_msize
[current_ws
] += ws
[current_ws
].g
.h
/ 32;
851 if ( horizontal_msize
[current_ws
] >
852 (ws
[current_ws
].g
.h
- (ws
[current_ws
].g
.h
/ 16)))
853 horizontal_msize
[current_ws
] =
854 ws
[current_ws
].g
.h
- ws
[current_ws
].g
.h
/ 16;
863 horizontal_stack(struct swm_geometry
*g
) {
865 struct swm_geometry gg
= *g
;
866 struct ws_win wf
, *win
, *winfocus
= &wf
;
870 DNPRINTF(SWM_D_EVENT
, "horizontal_stack: workspace: %d\n", current_ws
);
872 winno
= count_win(current_ws
, 0);
878 gg
.h
= horizontal_msize
[current_ws
];
881 hrw
= g
->w
/ (winno
- 1);
886 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
888 gg
.y
+= horizontal_msize
[current_ws
] + 2;
889 gg
.h
= g
->h
- (horizontal_msize
[current_ws
] + 2);
891 if (i
!= 0 && hrw
!= 0) {
892 /* correct the last window for lost pixels */
893 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
895 gg
.w
= hrw
+ (g
->w
- (i
* hrw
));
899 /* leave first bottom window at x = 0 */
905 if (win
->transient
!= 0 || win
->floating
!= 0)
908 bzero(&wc
, sizeof wc
);
910 win
->g
.x
= wc
.x
= gg
.x
;
911 win
->g
.y
= wc
.y
= gg
.y
;
912 win
->g
.w
= wc
.width
= gg
.w
;
913 win
->g
.h
= wc
.height
= gg
.h
;
914 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
915 XConfigureWindow(display
, win
->id
, mask
, &wc
);
918 if (win
== ws
[current_ws
].focus
)
922 XMapRaised(display
, win
->id
);
926 focus_win(winfocus
); /* this has to be done outside of the loop */
930 max_focus(struct ws_win
*fwin
)
934 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
935 if (win
->transient
== 0 && win
->floating
== 0 && win
== fwin
)
936 XMapRaised(display
, win
->id
);
938 XUnmapWindow(display
, win
->id
);
942 /* fullscreen view */
944 max_stack(struct swm_geometry
*g
) {
946 struct swm_geometry gg
= *g
;
947 struct ws_win
*win
, *winfocus
;
950 DNPRINTF(SWM_D_EVENT
, "max_stack: workspace: %d\n", current_ws
);
952 if (count_win(current_ws
, 0) == 0)
955 winfocus
= ws
[current_ws
].focus
;
957 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
959 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
960 if (win
->transient
!= 0 || win
->floating
!= 0) {
962 stack_floater(win
); /* XXX maximize? */
964 XUnmapWindow(display
, win
->id
);
966 bzero(&wc
, sizeof wc
);
968 win
->g
.x
= wc
.x
= gg
.x
;
969 win
->g
.y
= wc
.y
= gg
.y
;
970 win
->g
.w
= wc
.width
= gg
.w
;
971 win
->g
.h
= wc
.height
= gg
.h
;
972 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
973 XConfigureWindow(display
, win
->id
, mask
, &wc
);
976 XMapRaised(display
, win
->id
);
978 XUnmapWindow(display
, win
->id
);
982 focus_win(winfocus
); /* this has to be done outside of the loop */
986 send_to_ws(union arg
*args
)
989 struct ws_win
*win
= ws
[current_ws
].focus
;
991 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
993 XUnmapWindow(display
, win
->id
);
995 /* find a window to focus */
996 ws
[current_ws
].focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
997 if (ws
[current_ws
].focus
== NULL
)
998 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
999 if (ws
[current_ws
].focus
== win
)
1000 ws
[current_ws
].focus
= NULL
;
1002 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
1004 TAILQ_INSERT_TAIL(&ws
[wsid
].winlist
, win
, entry
);
1005 if (count_win(wsid
, 1) == 1)
1006 ws
[wsid
].focus
= win
;
1007 ws
[wsid
].restack
= 1;
1012 /* key definitions */
1016 void (*func
)(union arg
*);
1019 /* modifier key function argument */
1020 { MODKEY
, XK_space
, cycle_layout
, {0} },
1021 { MODKEY
| ShiftMask
, XK_space
, stack_reset
, {0} },
1022 { MODKEY
, XK_h
, resize_master
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1023 { MODKEY
, XK_l
, resize_master
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1024 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1025 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1026 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1027 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1028 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1029 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1030 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1031 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1032 { MODKEY
, XK_q
, restart
, {0} },
1033 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1034 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1035 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1036 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1037 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1038 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1039 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1040 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1041 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1042 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1043 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1044 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1045 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1046 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1047 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1048 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1049 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1050 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1051 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1052 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1053 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1054 { MODKEY
, XK_b
, bar_toggle
, {0} },
1055 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1056 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1060 updatenumlockmask(void)
1063 XModifierKeymap
*modmap
;
1065 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1067 modmap
= XGetModifierMapping(display
);
1068 for (i
= 0; i
< 8; i
++)
1069 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1070 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1071 == XKeysymToKeycode(display
, XK_Num_Lock
))
1072 numlockmask
= (1 << i
);
1074 XFreeModifiermap(modmap
);
1082 unsigned int modifiers
[] =
1083 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1085 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1086 updatenumlockmask();
1088 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
1089 for(i
= 0; i
< LENGTH(keys
); i
++) {
1090 if((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1091 for(j
= 0; j
< LENGTH(modifiers
); j
++)
1092 XGrabKey(display
, code
,
1093 keys
[i
].mod
| modifiers
[j
], root
,
1094 True
, GrabModeAsync
, GrabModeAsync
);
1100 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1108 XKeyEvent
*ev
= &e
->xkey
;
1110 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1112 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1113 for(i
= 0; i
< LENGTH(keys
); i
++)
1114 if(keysym
== keys
[i
].keysym
1115 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1117 keys
[i
].func(&(keys
[i
].args
));
1121 buttonpress(XEvent
*e
)
1123 XButtonPressedEvent
*ev
= &e
->xbutton
;
1124 #ifdef SWM_CLICKTOFOCUS
1129 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1131 if (ev
->window
== root
)
1133 if (ev
->window
== ws
[current_ws
].focus
->id
)
1135 #ifdef SWM_CLICKTOFOCUS
1136 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
1137 if (win
->id
== ev
->window
) {
1138 /* focus in the clicked window */
1139 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1140 XSetWindowBorder(display
,
1141 ws
[current_ws
].focus
->id
, 0x888888);
1142 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1144 ws
[current_ws
].focus
= win
;
1145 XSync(display
, False
);
1152 manage_window(Window id
)
1158 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1160 return (win
); /* already being managed */
1163 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
1164 errx(1, "calloc: failed to allocate memory for new window");
1167 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
, win
, entry
);
1168 ws
[current_ws
].focus
= win
; /* make new win focused */
1170 XGetTransientForHint(display
, win
->id
, &trans
);
1172 win
->transient
= trans
;
1173 DNPRINTF(SWM_D_MISC
, "manage_window: win %u transient %u\n",
1174 (unsigned)win
->id
, win
->transient
);
1176 XGetWindowAttributes(display
, win
->id
, &win
->wa
);
1177 XGetNormalHints(display
, win
->id
, &win
->sh
);
1180 bzero(&ch
, sizeof ch
);
1181 if(XGetClassHint(display
, win
->id
, &ch
)) {
1182 /*fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); */
1183 if (!strcmp(ch
.res_class
, "MPlayer") && !strcmp(ch
.res_name
, "xv")) {
1187 XFree(ch
.res_class
);
1192 XSelectInput(display
, id
, ButtonPressMask
| EnterWindowMask
|
1193 FocusChangeMask
| ExposureMask
);
1199 configurerequest(XEvent
*e
)
1201 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1206 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1207 if (win
->id
== ev
->window
) {
1214 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1216 bzero(&wc
, sizeof wc
);
1219 wc
.width
= ev
->width
;
1220 wc
.height
= ev
->height
;
1221 wc
.border_width
= ev
->border_width
;
1222 wc
.sibling
= ev
->above
;
1223 wc
.stack_mode
= ev
->detail
;
1224 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1226 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1233 configurenotify(XEvent
*e
)
1235 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1236 e
->xconfigure
.window
);
1240 destroynotify(XEvent
*e
)
1243 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1245 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1247 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1248 if (ev
->window
== win
->id
) {
1249 /* find a window to focus */
1250 ws
[current_ws
].focus
= TAILQ_PREV(win
,
1251 ws_win_list
, entry
);
1252 if (ws
[current_ws
].focus
== NULL
)
1253 ws
[current_ws
].focus
=
1254 TAILQ_FIRST(&ws
[current_ws
].winlist
);
1255 if (win
== ws
[current_ws
].focus
)
1256 ws
[current_ws
].focus
= NULL
;
1258 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
1268 enternotify(XEvent
*e
)
1270 XCrossingEvent
*ev
= &e
->xcrossing
;
1273 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1275 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1279 /* eat event(s) to prevent autofocus */
1283 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1284 if (win
->id
== ev
->window
)
1294 XFocusChangeEvent
*ev
= &e
->xfocus
;
1296 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", ev
->window
);
1298 if (ev
->window
== root
)
1301 * kill grab for now so that we can cut and paste , this screws up
1305 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1306 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1307 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1312 mappingnotify(XEvent
*e
)
1314 XMappingEvent
*ev
= &e
->xmapping
;
1316 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1318 XRefreshKeyboardMapping(ev
);
1319 if(ev
->request
== MappingKeyboard
)
1324 maprequest(XEvent
*e
)
1326 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1327 XWindowAttributes wa
;
1329 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1330 e
->xmaprequest
.window
);
1332 if(!XGetWindowAttributes(display
, ev
->window
, &wa
))
1334 if(wa
.override_redirect
)
1336 manage_window(e
->xmaprequest
.window
);
1341 propertynotify(XEvent
*e
)
1343 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1344 e
->xproperty
.window
);
1348 unmapnotify(XEvent
*e
)
1350 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1354 visibilitynotify(XEvent
*e
)
1356 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n", e
->xvisibility
.window
);
1358 if (e
->xvisibility
.window
== bar_window
&&
1359 e
->xvisibility
.state
== VisibilityUnobscured
)
1363 void (*handler
[LASTEvent
])(XEvent
*) = {
1365 [KeyPress
] = keypress
,
1366 [ButtonPress
] = buttonpress
,
1367 [ConfigureRequest
] = configurerequest
,
1368 [ConfigureNotify
] = configurenotify
,
1369 [DestroyNotify
] = destroynotify
,
1370 [EnterNotify
] = enternotify
,
1371 [FocusIn
] = focusin
,
1372 [MappingNotify
] = mappingnotify
,
1373 [MapRequest
] = maprequest
,
1374 [PropertyNotify
] = propertynotify
,
1375 [UnmapNotify
] = unmapnotify
,
1376 [VisibilityNotify
] = visibilitynotify
,
1380 xerror_start(Display
*d
, XErrorEvent
*ee
)
1387 xerror(Display
*d
, XErrorEvent
*ee
)
1389 fprintf(stderr
, "error: %p %p\n", display
, ee
);
1398 xerrorxlib
= XSetErrorHandler(xerror_start
);
1400 /* this causes an error if some other window manager is running */
1401 XSelectInput(display
, DefaultRootWindow(display
),
1402 SubstructureRedirectMask
);
1403 XSync(display
, False
);
1407 XSetErrorHandler(xerror
);
1408 XSync(display
, False
);
1413 main(int argc
, char *argv
[])
1416 char conf
[PATH_MAX
], *cfile
= NULL
;
1419 unsigned int i
, j
, num
;
1420 Window d1
, d2
, *wins
= NULL
;
1421 XWindowAttributes wa
;
1424 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1425 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1426 warnx("no locale support");
1428 if(!(display
= XOpenDisplay(0)))
1429 errx(1, "can not open display");
1432 errx(1, "other wm running");
1434 screen
= DefaultScreen(display
);
1435 root
= RootWindow(display
, screen
);
1437 /* look for local and global conf file */
1438 pwd
= getpwuid(getuid());
1440 errx(1, "invalid user %d", getuid());
1442 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1443 if (stat(conf
, &sb
) != -1) {
1444 if (S_ISREG(sb
.st_mode
))
1447 /* try global conf file */
1448 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1449 if (!stat(conf
, &sb
))
1450 if (S_ISREG(sb
.st_mode
))
1456 /* init all workspaces */
1457 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1463 ws
[i
].g
.w
= DisplayWidth(display
, screen
) - 2;
1464 ws
[i
].g
.h
= DisplayHeight(display
, screen
) - 2;
1465 TAILQ_INIT(&ws
[i
].winlist
);
1467 for (j
= 0; layouts
[j
].l_stack
!= NULL
; j
++) {
1468 if (layouts
[j
].l_init
!= NULL
)
1469 layouts
[j
].l_init(i
);
1471 ws
[i
].cur_layout
= &layouts
[0];
1473 /* make work space 1 active */
1476 /* grab existing windows */
1477 if (XQueryTree(display
, root
, &d1
, &d2
, &wins
, &num
)) {
1478 for (i
= 0; i
< num
; i
++) {
1479 if (!XGetWindowAttributes(display
, wins
[i
], &wa
)
1480 || wa
.override_redirect
1481 || wa
.map_state
!= IsViewable
)
1483 manage_window(wins
[i
]);
1488 ws
[0].focus
= TAILQ_FIRST(&ws
[0].winlist
);
1490 /* setup status bar */
1493 XSelectInput(display
, root
, SubstructureRedirectMask
|
1494 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
1495 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
1496 FocusChangeMask
| PropertyChangeMask
| ExposureMask
);
1502 XNextEvent(display
, &e
);
1503 if (handler
[e
.type
])
1504 handler
[e
.type
](&e
);
1507 XCloseDisplay(display
);