]>
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))
103 #define WIDTH ws[current_ws].g.w
104 #define HEIGHT ws[current_ws].g.h
108 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
112 int ignore_enter
= 0;
113 unsigned int numlockmask
= 0;
114 unsigned long color_focus
= 0xff0000; /* XXX should this be per ws? */
115 unsigned long color_unfocus
= 0x888888;
122 unsigned long bar_border
= 0x008080;
123 unsigned long bar_color
= 0x000000;
124 unsigned long bar_font_color
= 0xa0a0a0;
130 char *bar_fonts
[] = {
131 "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*",
132 "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*",
136 /* terminal + args */
137 char *spawn_term
[] = { "xterm", NULL
};
138 char *spawn_menu
[] = { "dmenu_run", NULL
};
140 /* layout manager data */
141 struct swm_geometry
{
150 TAILQ_ENTRY(ws_win
) entry
;
152 struct swm_geometry g
;
155 XWindowAttributes wa
;
158 TAILQ_HEAD(ws_win_list
, ws_win
);
160 /* layout handlers */
162 void vertical_init(int);
163 void vertical_resize(int);
164 void vertical_stack(struct swm_geometry
*);
165 void horizontal_init(int);
166 void horizontal_resize(int);
167 void horizontal_stack(struct swm_geometry
*);
169 void max_focus(struct ws_win
*);
170 void max_stack(struct swm_geometry
*);
173 void (*l_init
)(int); /* init/reset */
174 void (*l_stack
)(struct swm_geometry
*);
175 void (*l_resize
)(int);
176 void (*l_focus
)(struct ws_win
*);
178 /* init stack, resize */
179 { vertical_init
, vertical_stack
, vertical_resize
, NULL
},
180 { horizontal_init
, horizontal_stack
, horizontal_resize
, NULL
},
181 { NULL
, max_stack
, NULL
, max_focus
},
182 { NULL
, NULL
, NULL
, NULL
},
186 /* define work spaces */
187 #define SWM_WS_MAX (10)
189 int visible
; /* workspace visible */
190 int restack
; /* restack on switch */
191 struct swm_geometry g
;
192 struct layout
*cur_layout
; /* current layout handlers */
193 struct ws_win
*focus
; /* which win has focus */
194 struct ws_win_list winlist
; /* list of windows in ws */
198 /* args to functions */
201 #define SWM_ARG_ID_FOCUSNEXT (0)
202 #define SWM_ARG_ID_FOCUSPREV (1)
203 #define SWM_ARG_ID_FOCUSMAIN (2)
204 #define SWM_ARG_ID_SWAPNEXT (3)
205 #define SWM_ARG_ID_SWAPPREV (4)
206 #define SWM_ARG_ID_SWAPMAIN (5)
207 #define SWM_ARG_ID_MASTERSHRINK (6)
208 #define SWM_ARG_ID_MASTERGROW (7)
212 /* conf file stuff */
213 #define SWM_CONF_WS "\n= \t"
214 #define SWM_CONF_FILE "scrotwm.conf"
216 conf_load(char *filename
)
219 char *line
, *cp
, *var
, *val
;
220 size_t len
, lineno
= 0;
222 DNPRINTF(SWM_D_MISC
, "conf_load: filename %s\n", filename
);
224 if (filename
== NULL
)
227 if ((config
= fopen(filename
, "r")) == NULL
)
231 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
235 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
241 if ((var
= strsep(&cp
, SWM_CONF_WS
)) == NULL
|| cp
== NULL
)
243 cp
+= (long)strspn(cp
, SWM_CONF_WS
);
244 if ((val
= strsep(&cp
, SWM_CONF_WS
)) == NULL
)
247 DNPRINTF(SWM_D_MISC
, "conf_load: %s=%s\n",var
,val
);
250 if (!strncmp(var
, "bar_enabled", strlen("bar_enabled")))
251 bar_enabled
= atoi(val
);
252 else if (!strncmp(var
, "bar_border",
253 strlen("bar_border")))
254 bar_border
= strtol(val
, NULL
, 16);
255 else if (!strncmp(var
, "bar_color",
256 strlen("bar_color")))
257 bar_color
= strtol(val
, NULL
, 16);
258 else if (!strncmp(var
, "bar_font_color",
259 strlen("bar_font_color")))
260 bar_font_color
= strtol(val
, NULL
, 16);
261 else if (!strncmp(var
, "bar_font", strlen("bar_font")))
262 asprintf(&bar_fonts
[0], "%s", val
);
268 if (!strncmp(var
, "color_focus", strlen("color_focus")))
269 color_focus
= strtol(val
, NULL
, 16);
270 else if (!strncmp(var
, "color_unfocus",
271 strlen("color_unfocus")))
272 color_unfocus
= strtol(val
, NULL
, 16);
278 if (!strncmp(var
, "spawn_term", strlen("spawn_term")))
279 asprintf(&spawn_term
[0], "%s", val
); /* XXX args? */
290 errx(1, "invalid conf file entry: %s=%s", var
, val
);
299 if (bar_enabled
== 0)
303 XSetForeground(display
, bar_gc
, bar_color
);
304 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
309 localtime_r(&tmt
, &tm
);
310 strftime(bar_text
, sizeof bar_text
, "%a %b %d %R %Z %Y", &tm
);
311 XSetForeground(display
, bar_gc
, bar_font_color
);
312 XDrawString(display
, bar_window
, bar_gc
, 4, bar_fs
->ascent
, bar_text
,
314 XSync(display
, False
);
322 /* XXX yeah yeah byte me */
327 bar_toggle(union arg
*args
)
331 DNPRINTF(SWM_D_MISC
, "bar_toggle\n");
335 XUnmapWindow(display
, bar_window
);
338 XMapRaised(display
, bar_window
);
340 XSync(display
, False
);
341 for (i
= 0; i
< SWM_WS_MAX
; i
++)
345 bar_print(); /* must be after stack */
353 for (i
= 0; bar_fonts
[i
] != NULL
; i
++) {
354 bar_fs
= XLoadQueryFont(display
, bar_fonts
[i
]);
358 if (bar_fonts
[i
] == NULL
)
359 errx(1, "couldn't load font");
360 bar_height
= bar_fs
->ascent
+ bar_fs
->descent
+ 3;
362 bar_window
= XCreateSimpleWindow(display
, root
, 0, 0,
363 WIDTH
, bar_height
- 2, 1, bar_border
, bar_color
);
364 bar_gc
= XCreateGC(display
, bar_window
, 0, &bar_gcv
);
365 XSetFont(display
, bar_gc
, bar_fs
->fid
);
366 XSelectInput(display
, bar_window
, VisibilityChangeMask
);
368 XMapRaised(display
, bar_window
);
370 DNPRINTF(SWM_D_MISC
, "bar_setup: bar_window %d\n", (int)bar_window
);
372 if (signal(SIGALRM
, bar_signal
) == SIG_ERR
)
373 err(1, "could not install bar_signal");
378 config_win(struct ws_win
*win
)
382 DNPRINTF(SWM_D_MISC
, "config_win: win %lu x %d y %d w %d h %d\n",
383 win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
384 ce
.type
= ConfigureNotify
;
385 ce
.display
= display
;
391 ce
.height
= win
->g
.h
;
392 ce
.border_width
= 1; /* XXX store this! */
394 ce
.override_redirect
= False
;
395 XSendEvent(display
, win
->id
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
399 count_win(int wsid
, int count_transient
)
404 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
) {
405 if (count_transient
== 0 && win
->floating
)
407 if (count_transient
== 0 && win
->transient
)
411 DNPRINTF(SWM_D_MISC
, "count_win: %d\n", count
);
416 quit(union arg
*args
)
418 DNPRINTF(SWM_D_MISC
, "quit\n");
423 restart(union arg
*args
)
425 DNPRINTF(SWM_D_MISC
, "restart: %s\n", start_argv
[0]);
427 XCloseDisplay(display
);
428 execvp(start_argv
[0], start_argv
);
429 fprintf(stderr
, "execvp failed\n");
435 spawn(union arg
*args
)
437 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
439 * The double-fork construct avoids zombie processes and keeps the code
440 * clean from stupid signal handlers.
445 close(ConnectionNumber(display
));
447 execvp(args
->argv
[0], args
->argv
);
448 fprintf(stderr
, "execvp failed\n");
457 focus_win(struct ws_win
*win
)
459 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %lu\n", win
->id
);
460 XSetWindowBorder(display
, win
->id
, color_focus
);
461 XSetInputFocus(display
, win
->id
, RevertToPointerRoot
, CurrentTime
);
462 ws
[current_ws
].focus
= win
;
466 unfocus_win(struct ws_win
*win
)
468 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %lu\n", win
->id
);
469 XSetWindowBorder(display
, win
->id
, color_unfocus
);
470 if (ws
[current_ws
].focus
== win
)
471 ws
[current_ws
].focus
= NULL
;
475 switchws(union arg
*args
)
480 DNPRINTF(SWM_D_WS
, "switchws: %d\n", wsid
+ 1);
482 if (wsid
== current_ws
)
485 /* map new window first to prevent ugly blinking */
486 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
)
487 XMapRaised(display
, win
->id
);
488 ws
[wsid
].visible
= 1;
490 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
)
491 XUnmapWindow(display
, win
->id
);
492 ws
[current_ws
].visible
= 0;
497 if (ws
[wsid
].restack
) {
501 if (ws
[wsid
].focus
!= NULL
)
502 focus_win(ws
[wsid
].focus
);
503 XSync(display
, False
);
508 swapwin(union arg
*args
)
510 struct ws_win
*target
;
512 DNPRINTF(SWM_D_MOVE
, "swapwin: id %d\n", args
->id
);
513 if (ws
[current_ws
].focus
== NULL
)
517 case SWM_ARG_ID_SWAPPREV
:
518 target
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
519 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
520 ws
[current_ws
].focus
, entry
);
522 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
,
523 ws
[current_ws
].focus
, entry
);
525 TAILQ_INSERT_BEFORE(target
, ws
[current_ws
].focus
,
528 case SWM_ARG_ID_SWAPNEXT
:
529 target
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
530 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
531 ws
[current_ws
].focus
, entry
);
533 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
534 ws
[current_ws
].focus
, entry
);
536 TAILQ_INSERT_AFTER(&ws
[current_ws
].winlist
, target
,
537 ws
[current_ws
].focus
, entry
);
539 case SWM_ARG_ID_SWAPMAIN
:
540 target
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
541 if (target
== ws
[current_ws
].focus
)
543 TAILQ_REMOVE(&ws
[current_ws
].winlist
, target
, entry
);
544 TAILQ_INSERT_BEFORE(ws
[current_ws
].focus
, target
, entry
);
545 TAILQ_REMOVE(&ws
[current_ws
].winlist
,
546 ws
[current_ws
].focus
, entry
);
547 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
,
548 ws
[current_ws
].focus
, entry
);
551 DNPRINTF(SWM_D_MOVE
, "invalid id: %d\n", args
->id
);
560 focus(union arg
*args
)
562 struct ws_win
*winfocus
, *winlostfocus
;
564 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
565 if (ws
[current_ws
].focus
== NULL
)
568 winlostfocus
= ws
[current_ws
].focus
;
571 case SWM_ARG_ID_FOCUSPREV
:
572 winfocus
= TAILQ_PREV(ws
[current_ws
].focus
, ws_win_list
, entry
);
573 if (winfocus
== NULL
)
575 TAILQ_LAST(&ws
[current_ws
].winlist
, ws_win_list
);
578 case SWM_ARG_ID_FOCUSNEXT
:
579 winfocus
= TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
580 if (winfocus
== NULL
)
581 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
584 case SWM_ARG_ID_FOCUSMAIN
:
585 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
592 if (winfocus
== winlostfocus
)
595 unfocus_win(winlostfocus
);
597 /* XXX if we hook in focus_win(), we get a nasty cycle */
598 if (ws
[current_ws
].cur_layout
->l_focus
!= NULL
)
599 ws
[current_ws
].cur_layout
->l_focus(winfocus
);
600 XSync(display
, False
);
604 cycle_layout(union arg
*args
)
606 DNPRINTF(SWM_D_EVENT
, "cycle_layout: workspace: %d\n", current_ws
);
608 ws
[current_ws
].cur_layout
++;
609 if (ws
[current_ws
].cur_layout
->l_stack
== NULL
)
610 ws
[current_ws
].cur_layout
= &layouts
[0];
617 resize_master(union arg
*args
)
619 DNPRINTF(SWM_D_EVENT
, "resize_master: id %d\n", args
->id
);
621 if (ws
[current_ws
].cur_layout
->l_resize
!= NULL
)
622 ws
[current_ws
].cur_layout
->l_resize(args
->id
);
626 stack_reset(union arg
*args
)
628 DNPRINTF(SWM_D_EVENT
, "stack_reset: ws %d\n", current_ws
);
630 if (ws
[current_ws
].cur_layout
->l_init
!= NULL
) {
631 ws
[current_ws
].cur_layout
->l_init(current_ws
);
638 struct swm_geometry g
;
639 DNPRINTF(SWM_D_EVENT
, "stack: workspace: %d\n", current_ws
);
641 /* start with workspace geometry, adjust for bar */
642 g
= ws
[current_ws
].g
;
648 ws
[current_ws
].restack
= 0;
649 ws
[current_ws
].cur_layout
->l_stack(&g
);
650 XSync(display
, False
);
654 stack_floater(struct ws_win
*win
)
659 bzero(&wc
, sizeof wc
);
661 mask
= CWX
| CWY
| CWBorderWidth
;
663 /* use obsolete width height */
664 if (win
->sh
.flags
& USPosition
) {
665 win
->g
.w
= wc
.width
= win
->sh
.width
;
666 win
->g
.h
= wc
.height
= win
->sh
.height
;
667 mask
|= CWWidth
| CWHeight
;
671 if (win
->sh
.flags
& PMinSize
) {
672 /* some hints are retarded */
673 if (win
->sh
.min_width
< WIDTH
/ 10)
674 win
->sh
.min_width
= WIDTH
/ 3;
675 if (win
->sh
.min_height
< HEIGHT
/ 10)
676 win
->sh
.height
= HEIGHT
/ 3;
678 win
->g
.w
= wc
.width
= win
->sh
.min_width
* 2;
679 win
->g
.h
= wc
.height
= win
->sh
.min_height
* 2;
680 mask
|= CWWidth
| CWHeight
;
682 if (win
->sh
.flags
& PMaxSize
) {
683 /* potentially override min values */
684 if (win
->sh
.max_width
< WIDTH
) {
685 win
->g
.w
= wc
.width
= win
->sh
.max_width
;
688 if (win
->sh
.max_height
< HEIGHT
) {
689 win
->g
.h
= wc
.height
= win
->sh
.max_height
;
694 /* make sure we don't clobber the screen */
695 if ((mask
& CWWidth
) && win
->wa
.width
> WIDTH
)
696 win
->wa
.width
= WIDTH
- 4;
697 if ((mask
& CWHeight
) && win
->wa
.height
> HEIGHT
)
698 win
->wa
.height
= HEIGHT
- 4;
700 /* supposed to be obsolete */
701 if (win
->sh
.flags
& USPosition
) {
702 win
->g
.x
= wc
.x
= win
->sh
.x
;
703 win
->g
.y
= wc
.y
= win
->sh
.y
;
705 win
->g
.x
= wc
.x
= (WIDTH
- win
->wa
.width
) / 2;
706 win
->g
.y
= wc
.y
= (HEIGHT
- win
->wa
.height
) / 2;
708 DNPRINTF(SWM_D_EVENT
, "stack_floater: win %d x %d y %d w %d h %d\n",
709 win
, wc
.x
, wc
.y
, wc
.width
, wc
.height
);
711 XConfigureWindow(display
, win
->id
, mask
, &wc
);
715 int vertical_msize
[SWM_WS_MAX
];
718 vertical_init(int ws_idx
)
720 DNPRINTF(SWM_D_MISC
, "vertical_init: workspace: %d\n", current_ws
);
722 vertical_msize
[ws_idx
] = ws
[ws_idx
].g
.w
/ 2;
726 vertical_resize(int id
)
728 DNPRINTF(SWM_D_MISC
, "vertical_resize: workspace: %d\n", current_ws
);
731 case SWM_ARG_ID_MASTERSHRINK
:
732 vertical_msize
[current_ws
] -= WIDTH
/ 32;
733 if ( vertical_msize
[current_ws
] < WIDTH
/ 16)
734 vertical_msize
[current_ws
] = WIDTH
/ 16;
736 case SWM_ARG_ID_MASTERGROW
:
737 vertical_msize
[current_ws
] += WIDTH
/ 32;
738 if ( vertical_msize
[current_ws
] >
739 (WIDTH
- (WIDTH
/ 16)))
740 vertical_msize
[current_ws
] =
750 vertical_stack(struct swm_geometry
*g
) {
752 struct swm_geometry gg
= *g
;
753 struct ws_win wf
, *win
, *winfocus
= &wf
;
757 DNPRINTF(SWM_D_EVENT
, "vertical_stack: workspace: %d\n", current_ws
);
759 winno
= count_win(current_ws
, 0);
765 gg
.w
= vertical_msize
[current_ws
];
768 hrh
= g
->h
/ (winno
- 1);
773 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
775 gg
.x
+= vertical_msize
[current_ws
] + 2;
776 gg
.w
= g
->w
- (vertical_msize
[current_ws
] + 2);
778 if (i
!= 0 && hrh
!= 0) {
779 /* correct the last window for lost pixels */
780 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
782 gg
.h
= hrh
+ (g
->h
- (i
* hrh
));
786 /* leave first right hand window at y = 0 */
792 if (win
->transient
!= 0 || win
->floating
!= 0)
795 bzero(&wc
, sizeof wc
);
797 win
->g
.x
= wc
.x
= gg
.x
;
798 win
->g
.y
= wc
.y
= gg
.y
;
799 win
->g
.w
= wc
.width
= gg
.w
;
800 win
->g
.h
= wc
.height
= gg
.h
;
801 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
802 XConfigureWindow(display
, win
->id
, mask
, &wc
);
805 if (win
== ws
[current_ws
].focus
)
809 XMapRaised(display
, win
->id
);
813 focus_win(winfocus
); /* this has to be done outside of the loop */
816 int horizontal_msize
[SWM_WS_MAX
];
819 horizontal_init(int ws_idx
)
821 DNPRINTF(SWM_D_MISC
, "horizontal_init: workspace: %d\n", current_ws
);
823 horizontal_msize
[ws_idx
] = ws
[ws_idx
].g
.h
/ 2;
827 horizontal_resize(int id
)
829 DNPRINTF(SWM_D_MISC
, "horizontal_resize: workspace: %d\n", current_ws
);
832 case SWM_ARG_ID_MASTERSHRINK
:
833 horizontal_msize
[current_ws
] -= HEIGHT
/ 32;
834 if ( horizontal_msize
[current_ws
] < HEIGHT
/ 16)
835 horizontal_msize
[current_ws
] = HEIGHT
/ 16;
837 case SWM_ARG_ID_MASTERGROW
:
838 horizontal_msize
[current_ws
] += HEIGHT
/ 32;
839 if ( horizontal_msize
[current_ws
] >
840 (HEIGHT
- (HEIGHT
/ 16)))
841 horizontal_msize
[current_ws
] =
842 HEIGHT
- HEIGHT
/ 16;
851 horizontal_stack(struct swm_geometry
*g
) {
853 struct swm_geometry gg
= *g
;
854 struct ws_win wf
, *win
, *winfocus
= &wf
;
858 DNPRINTF(SWM_D_EVENT
, "horizontal_stack: workspace: %d\n", current_ws
);
860 winno
= count_win(current_ws
, 0);
866 gg
.h
= horizontal_msize
[current_ws
];
869 hrw
= g
->w
/ (winno
- 1);
874 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
876 gg
.y
+= horizontal_msize
[current_ws
] + 2;
877 gg
.h
= g
->h
- (horizontal_msize
[current_ws
] + 2);
879 if (i
!= 0 && hrw
!= 0) {
880 /* correct the last window for lost pixels */
881 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
883 gg
.w
= hrw
+ (g
->w
- (i
* hrw
));
887 /* leave first bottom window at x = 0 */
893 if (win
->transient
!= 0 || win
->floating
!= 0)
896 bzero(&wc
, sizeof wc
);
898 win
->g
.x
= wc
.x
= gg
.x
;
899 win
->g
.y
= wc
.y
= gg
.y
;
900 win
->g
.w
= wc
.width
= gg
.w
;
901 win
->g
.h
= wc
.height
= gg
.h
;
902 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
903 XConfigureWindow(display
, win
->id
, mask
, &wc
);
906 if (win
== ws
[current_ws
].focus
)
910 XMapRaised(display
, win
->id
);
914 focus_win(winfocus
); /* this has to be done outside of the loop */
918 max_focus(struct ws_win
*fwin
)
922 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
923 if (win
->transient
== 0 && win
->floating
== 0 && win
== fwin
)
924 XMapRaised(display
, win
->id
);
926 XUnmapWindow(display
, win
->id
);
930 /* fullscreen view */
932 max_stack(struct swm_geometry
*g
) {
934 struct swm_geometry gg
= *g
;
935 struct ws_win
*win
, *winfocus
;
938 DNPRINTF(SWM_D_EVENT
, "max_stack: workspace: %d\n", current_ws
);
940 if (count_win(current_ws
, 0) == 0)
943 winfocus
= ws
[current_ws
].focus
;
945 winfocus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
947 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
948 if (win
->transient
!= 0 || win
->floating
!= 0) {
949 if (win
== winfocus
) {
950 stack_floater(win
); /* XXX maximize? */
951 XMapRaised(display
, win
->id
);
953 XUnmapWindow(display
, win
->id
);
955 bzero(&wc
, sizeof wc
);
957 win
->g
.x
= wc
.x
= gg
.x
;
958 win
->g
.y
= wc
.y
= gg
.y
;
959 win
->g
.w
= wc
.width
= gg
.w
;
960 win
->g
.h
= wc
.height
= gg
.h
;
961 mask
= CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
;
962 XConfigureWindow(display
, win
->id
, mask
, &wc
);
965 XMapRaised(display
, win
->id
);
967 XUnmapWindow(display
, win
->id
);
971 focus_win(winfocus
); /* this has to be done outside of the loop */
975 send_to_ws(union arg
*args
)
978 struct ws_win
*win
= ws
[current_ws
].focus
;
980 DNPRINTF(SWM_D_MOVE
, "send_to_ws: win: %lu\n", win
->id
);
982 XUnmapWindow(display
, win
->id
);
984 /* find a window to focus */
985 ws
[current_ws
].focus
= TAILQ_PREV(win
, ws_win_list
, entry
);
986 if (ws
[current_ws
].focus
== NULL
)
987 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
988 if (ws
[current_ws
].focus
== win
)
989 ws
[current_ws
].focus
= NULL
;
991 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
993 TAILQ_INSERT_TAIL(&ws
[wsid
].winlist
, win
, entry
);
994 if (count_win(wsid
, 1) == 1)
995 ws
[wsid
].focus
= win
;
996 ws
[wsid
].restack
= 1;
1001 /* key definitions */
1005 void (*func
)(union arg
*);
1008 /* modifier key function argument */
1009 { MODKEY
, XK_space
, cycle_layout
, {0} },
1010 { MODKEY
| ShiftMask
, XK_space
, stack_reset
, {0} },
1011 { MODKEY
, XK_h
, resize_master
, {.id
= SWM_ARG_ID_MASTERSHRINK
} },
1012 { MODKEY
, XK_l
, resize_master
, {.id
= SWM_ARG_ID_MASTERGROW
} },
1013 { MODKEY
, XK_Return
, swapwin
, {.id
= SWM_ARG_ID_SWAPMAIN
} },
1014 { MODKEY
, XK_j
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1015 { MODKEY
, XK_k
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1016 { MODKEY
| ShiftMask
, XK_j
, swapwin
, {.id
= SWM_ARG_ID_SWAPNEXT
} },
1017 { MODKEY
| ShiftMask
, XK_k
, swapwin
, {.id
= SWM_ARG_ID_SWAPPREV
} },
1018 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= spawn_term
} },
1019 { MODKEY
, XK_p
, spawn
, {.argv
= spawn_menu
} },
1020 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
1021 { MODKEY
, XK_q
, restart
, {0} },
1022 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
1023 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
1024 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
1025 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
1026 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
1027 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
1028 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
1029 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
1030 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
1031 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
1032 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
1033 { MODKEY
| ShiftMask
, XK_1
, send_to_ws
, {.id
= 0} },
1034 { MODKEY
| ShiftMask
, XK_2
, send_to_ws
, {.id
= 1} },
1035 { MODKEY
| ShiftMask
, XK_3
, send_to_ws
, {.id
= 2} },
1036 { MODKEY
| ShiftMask
, XK_4
, send_to_ws
, {.id
= 3} },
1037 { MODKEY
| ShiftMask
, XK_5
, send_to_ws
, {.id
= 4} },
1038 { MODKEY
| ShiftMask
, XK_6
, send_to_ws
, {.id
= 5} },
1039 { MODKEY
| ShiftMask
, XK_7
, send_to_ws
, {.id
= 6} },
1040 { MODKEY
| ShiftMask
, XK_8
, send_to_ws
, {.id
= 7} },
1041 { MODKEY
| ShiftMask
, XK_9
, send_to_ws
, {.id
= 8} },
1042 { MODKEY
| ShiftMask
, XK_0
, send_to_ws
, {.id
= 9} },
1043 { MODKEY
, XK_b
, bar_toggle
, {0} },
1044 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
1045 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
1049 updatenumlockmask(void)
1052 XModifierKeymap
*modmap
;
1054 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
1056 modmap
= XGetModifierMapping(display
);
1057 for (i
= 0; i
< 8; i
++)
1058 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1059 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1060 == XKeysymToKeycode(display
, XK_Num_Lock
))
1061 numlockmask
= (1 << i
);
1063 XFreeModifiermap(modmap
);
1071 unsigned int modifiers
[] =
1072 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
1074 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
1075 updatenumlockmask();
1077 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
1078 for (i
= 0; i
< LENGTH(keys
); i
++) {
1079 if ((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
1080 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1081 XGrabKey(display
, code
,
1082 keys
[i
].mod
| modifiers
[j
], root
,
1083 True
, GrabModeAsync
, GrabModeAsync
);
1089 DNPRINTF(SWM_D_EVENT
, "expose: window: %lu\n", e
->xexpose
.window
);
1097 XKeyEvent
*ev
= &e
->xkey
;
1099 DNPRINTF(SWM_D_EVENT
, "keypress: window: %lu\n", ev
->window
);
1101 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
1102 for (i
= 0; i
< LENGTH(keys
); i
++)
1103 if (keysym
== keys
[i
].keysym
1104 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1106 keys
[i
].func(&(keys
[i
].args
));
1110 buttonpress(XEvent
*e
)
1112 XButtonPressedEvent
*ev
= &e
->xbutton
;
1113 #ifdef SWM_CLICKTOFOCUS
1118 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %lu\n", ev
->window
);
1120 if (ev
->window
== root
)
1122 if (ev
->window
== ws
[current_ws
].focus
->id
)
1124 #ifdef SWM_CLICKTOFOCUS
1125 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
1126 if (win
->id
== ev
->window
) {
1127 /* focus in the clicked window */
1128 XSetWindowBorder(display
, ev
->window
, 0xff0000);
1129 XSetWindowBorder(display
,
1130 ws
[current_ws
].focus
->id
, 0x888888);
1131 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
1133 ws
[current_ws
].focus
= win
;
1134 XSync(display
, False
);
1141 set_win_state(struct ws_win
*win
, long state
)
1143 long data
[] = {state
, None
};
1145 DNPRINTF(SWM_D_EVENT
, "set_win_state: window: %lu\n", win
->id
);
1147 XChangeProperty(display
, win
->id
, astate
, astate
, 32, PropModeReplace
,
1148 (unsigned char *)data
, 2);
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
);
1179 /* XXX make this a table */
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
);
1195 set_win_state(win
, NormalState
);
1201 configurerequest(XEvent
*e
)
1203 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
1208 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1209 if (win
->id
== ev
->window
) {
1216 DNPRINTF(SWM_D_EVENT
, "configurerequest: new window: %lu\n",
1218 bzero(&wc
, sizeof wc
);
1221 wc
.width
= ev
->width
;
1222 wc
.height
= ev
->height
;
1223 wc
.border_width
= ev
->border_width
;
1224 wc
.sibling
= ev
->above
;
1225 wc
.stack_mode
= ev
->detail
;
1226 XConfigureWindow(display
, ev
->window
, ev
->value_mask
, &wc
);
1228 DNPRINTF(SWM_D_EVENT
, "configurerequest: change window: %lu\n",
1231 if(ev
->value_mask
& CWX
)
1233 if(ev
->value_mask
& CWY
)
1235 if(ev
->value_mask
& CWWidth
)
1236 win
->g
.w
= ev
->width
;
1237 if(ev
->value_mask
& CWHeight
)
1238 win
->g
.h
= ev
->height
;
1239 /* this seems to be full screen */
1240 if (win
->g
.w
> WIDTH
) {
1245 if (win
->g
.h
> HEIGHT
) {
1250 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
1252 XMoveResizeWindow(display
, win
->id
, win
->g
.x
, win
->g
.y
, win
->g
.w
, win
->g
.h
);
1259 configurenotify(XEvent
*e
)
1261 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %lu\n",
1262 e
->xconfigure
.window
);
1266 destroynotify(XEvent
*e
)
1269 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
1271 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %lu\n", ev
->window
);
1273 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1274 if (ev
->window
== win
->id
) {
1275 /* find a window to focus */
1276 ws
[current_ws
].focus
= TAILQ_PREV(win
,
1277 ws_win_list
, entry
);
1278 if (ws
[current_ws
].focus
== NULL
)
1279 ws
[current_ws
].focus
=
1280 TAILQ_FIRST(&ws
[current_ws
].winlist
);
1281 if (win
== ws
[current_ws
].focus
)
1282 ws
[current_ws
].focus
= NULL
;
1284 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
1285 set_win_state(win
, WithdrawnState
);
1295 enternotify(XEvent
*e
)
1297 XCrossingEvent
*ev
= &e
->xcrossing
;
1300 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %lu\n", ev
->window
);
1302 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
1306 /* eat event(s) to prevent autofocus */
1310 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
1311 if (win
->id
== ev
->window
)
1321 XFocusChangeEvent
*ev
= &e
->xfocus
;
1323 DNPRINTF(SWM_D_EVENT
, "focusin: window: %lu\n", ev
->window
);
1325 if (ev
->window
== root
)
1328 * kill grab for now so that we can cut and paste , this screws up
1332 DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window);
1333 XGrabButton(display, Button1, AnyModifier, ev->window, False,
1334 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
1339 mappingnotify(XEvent
*e
)
1341 XMappingEvent
*ev
= &e
->xmapping
;
1343 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %lu\n", ev
->window
);
1345 XRefreshKeyboardMapping(ev
);
1346 if (ev
->request
== MappingKeyboard
)
1351 maprequest(XEvent
*e
)
1353 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1354 XWindowAttributes wa
;
1356 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %lu\n",
1357 e
->xmaprequest
.window
);
1359 if (!XGetWindowAttributes(display
, ev
->window
, &wa
))
1361 if (wa
.override_redirect
)
1363 manage_window(e
->xmaprequest
.window
);
1368 propertynotify(XEvent
*e
)
1370 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %lu\n",
1371 e
->xproperty
.window
);
1375 unmapnotify(XEvent
*e
)
1377 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %lu\n", e
->xunmap
.window
);
1381 visibilitynotify(XEvent
*e
)
1383 DNPRINTF(SWM_D_EVENT
, "visibilitynotify: window: %lu\n", e
->xvisibility
.window
);
1385 if (e
->xvisibility
.window
== bar_window
&&
1386 e
->xvisibility
.state
== VisibilityUnobscured
)
1390 void (*handler
[LASTEvent
])(XEvent
*) = {
1392 [KeyPress
] = keypress
,
1393 [ButtonPress
] = buttonpress
,
1394 [ConfigureRequest
] = configurerequest
,
1395 [ConfigureNotify
] = configurenotify
,
1396 [DestroyNotify
] = destroynotify
,
1397 [EnterNotify
] = enternotify
,
1398 [FocusIn
] = focusin
,
1399 [MappingNotify
] = mappingnotify
,
1400 [MapRequest
] = maprequest
,
1401 [PropertyNotify
] = propertynotify
,
1402 [UnmapNotify
] = unmapnotify
,
1403 [VisibilityNotify
] = visibilitynotify
,
1407 xerror_start(Display
*d
, XErrorEvent
*ee
)
1414 xerror(Display
*d
, XErrorEvent
*ee
)
1416 /* fprintf(stderr, "error: %p %p\n", display, ee); */
1424 xerrorxlib
= XSetErrorHandler(xerror_start
);
1426 /* this causes an error if some other window manager is running */
1427 XSelectInput(display
, DefaultRootWindow(display
),
1428 SubstructureRedirectMask
);
1429 XSync(display
, False
);
1433 XSetErrorHandler(xerror
);
1434 XSync(display
, False
);
1443 unsigned char *p
= NULL
;
1444 unsigned long n
, extra
;
1447 astate
= XInternAtom(display
, "WM_STATE", False
);
1448 status
= XGetWindowProperty(display
, w
, astate
, 0L, 2L, False
, astate
,
1449 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
1450 if (status
!= Success
)
1459 main(int argc
, char *argv
[])
1462 char conf
[PATH_MAX
], *cfile
= NULL
;
1465 unsigned int i
, j
, num
;
1466 Window d1
, d2
, *wins
= NULL
;
1467 XWindowAttributes wa
;
1470 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
1471 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1472 warnx("no locale support");
1474 if (!(display
= XOpenDisplay(0)))
1475 errx(1, "can not open display");
1478 errx(1, "other wm running");
1480 screen
= DefaultScreen(display
);
1481 root
= RootWindow(display
, screen
);
1482 astate
= XInternAtom(display
, "WM_STATE", False
);
1484 /* look for local and global conf file */
1485 pwd
= getpwuid(getuid());
1487 errx(1, "invalid user %d", getuid());
1489 snprintf(conf
, sizeof conf
, "%s/.%s", pwd
->pw_dir
, SWM_CONF_FILE
);
1490 if (stat(conf
, &sb
) != -1) {
1491 if (S_ISREG(sb
.st_mode
))
1494 /* try global conf file */
1495 snprintf(conf
, sizeof conf
, "/etc/%s", SWM_CONF_FILE
);
1496 if (!stat(conf
, &sb
))
1497 if (S_ISREG(sb
.st_mode
))
1503 /* init all workspaces */
1504 for (i
= 0; i
< SWM_WS_MAX
; i
++) {
1510 ws
[i
].g
.w
= DisplayWidth(display
, screen
) - 2;
1511 ws
[i
].g
.h
= DisplayHeight(display
, screen
) - 2;
1512 TAILQ_INIT(&ws
[i
].winlist
);
1514 for (j
= 0; layouts
[j
].l_stack
!= NULL
; j
++) {
1515 if (layouts
[j
].l_init
!= NULL
)
1516 layouts
[j
].l_init(i
);
1518 ws
[i
].cur_layout
= &layouts
[0];
1520 /* make work space 1 active */
1523 /* grab existing windows */
1524 if (XQueryTree(display
, root
, &d1
, &d2
, &wins
, &num
)) {
1525 /* normal windows */
1526 for (i
= 0; i
< num
; i
++) {
1527 XGetWindowAttributes(display
, wins
[i
], &wa
);
1528 if (!XGetWindowAttributes(display
, wins
[i
], &wa
) ||
1529 wa
.override_redirect
|| XGetTransientForHint(display
, wins
[i
], &d1
))
1531 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == NormalState
)
1532 manage_window(wins
[i
]);
1534 /* transient windows */
1535 for (i
= 0; i
< num
; i
++) {
1536 if (!XGetWindowAttributes(display
, wins
[i
], &wa
))
1538 if (XGetTransientForHint(display
, wins
[i
], &d1
) &&
1539 (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) ==
1541 manage_window(wins
[i
]);
1546 ws
[0].focus
= TAILQ_FIRST(&ws
[0].winlist
);
1548 /* setup status bar */
1551 XSelectInput(display
, root
, SubstructureRedirectMask
|
1552 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
1553 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
1554 FocusChangeMask
| PropertyChangeMask
| ExposureMask
);
1560 XNextEvent(display
, &e
);
1561 if (handler
[e
.type
])
1562 handler
[e
.type
](&e
);
1565 XCloseDisplay(display
);