]>
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"
59 #include <sys/types.h>
61 #include <sys/queue.h>
63 #include <X11/cursorfont.h>
64 #include <X11/keysym.h>
65 #include <X11/Xatom.h>
67 #include <X11/Xproto.h>
68 #include <X11/Xutil.h>
72 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0)
73 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0)
74 #define SWM_D_EVENT 0x0001
75 #define SWM_D_WS 0x0002
76 #define SWM_D_FOCUS 0x0004
77 #define SWM_D_MISC 0x0008
79 uint32_t swm_debug
= 0
87 #define DNPRINTF(n,x...)
90 #define LENGTH(x) (sizeof x / sizeof x[0])
91 #define MODKEY Mod1Mask
92 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
94 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
100 unsigned int numlockmask
= 0;
101 unsigned long col_focus
= 0xff0000; /* XXX should this be per ws? */
102 unsigned long col_unfocus
= 0x888888;
107 TAILQ_ENTRY(ws_win
) entry
;
115 TAILQ_HEAD(ws_win_list
, ws_win
);
117 /* define work spaces */
118 #define SWM_WS_MAX (10)
120 int visible
; /* workspace visible */
121 struct ws_win
*focus
; /* which win has focus */
122 int winno
; /* total nr of windows */
123 struct ws_win_list winlist
;
127 /* args to functions */
130 #define SWM_ARG_ID_FOCUSNEXT (0)
131 #define SWM_ARG_ID_FOCUSPREV (1)
132 #define SWM_ARG_ID_FOCUSMAIN (2)
137 quit(union arg
*args
)
139 DNPRINTF(SWM_D_MISC
, "quit\n");
144 spawn(union arg
*args
)
146 DNPRINTF(SWM_D_MISC
, "spawn: %s\n", args
->argv
[0]);
148 * The double-fork construct avoids zombie processes and keeps the code
149 * clean from stupid signal handlers.
154 close(ConnectionNumber(display
));
156 execvp(args
->argv
[0], args
->argv
);
157 fprintf(stderr
, "execvp failed\n");
166 focus_win(struct ws_win
*win
)
168 DNPRINTF(SWM_D_FOCUS
, "focus_win: id: %d\n", win
->id
);
169 XSetWindowBorder(display
, win
->id
, col_focus
);
170 XSetInputFocus(display
, win
->id
, RevertToPointerRoot
, CurrentTime
);
171 ws
[current_ws
].focus
= win
;
175 unfocus_win(struct ws_win
*win
)
177 DNPRINTF(SWM_D_FOCUS
, "unfocus_win: id: %d\n", win
->id
);
178 XSetWindowBorder(display
, win
->id
, col_unfocus
);
179 if (ws
[current_ws
].focus
== win
)
180 ws
[current_ws
].focus
= NULL
;
184 switchws(union arg
*args
)
190 DNPRINTF(SWM_D_WS
, "switchws: %d\n", wsid
+ 1);
192 if (wsid
== current_ws
)
195 /* map new window first to prevent ugly blinking */
196 TAILQ_FOREACH (win
, &ws
[wsid
].winlist
, entry
)
197 XMapWindow(display
, win
->id
);
198 ws
[wsid
].visible
= 1;
200 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
)
201 XUnmapWindow(display
, win
->id
);
202 ws
[current_ws
].visible
= 0;
207 if (ws
[wsid
].focus
!= NULL
)
208 focus_win(ws
[wsid
].focus
);
209 XSync(display
, False
);
213 focus(union arg
*args
)
215 struct ws_win
*winfocus
, *winlostfocus
;
217 DNPRINTF(SWM_D_FOCUS
, "focus: id %d\n", args
->id
);
218 if (ws
[current_ws
].focus
== NULL
|| ws
[current_ws
].winno
== 0)
221 winlostfocus
= ws
[current_ws
].focus
;
224 case SWM_ARG_ID_FOCUSPREV
:
225 if (ws
[current_ws
].focus
==
226 TAILQ_FIRST(&ws
[current_ws
].winlist
))
227 ws
[current_ws
].focus
=
228 TAILQ_LAST(&ws
[current_ws
].winlist
, ws_win_list
);
230 ws
[current_ws
].focus
=TAILQ_PREV(ws
[current_ws
].focus
,
234 case SWM_ARG_ID_FOCUSNEXT
:
235 if (ws
[current_ws
].focus
== TAILQ_LAST(&ws
[current_ws
].winlist
,
237 ws
[current_ws
].focus
=
238 TAILQ_FIRST(&ws
[current_ws
].winlist
);
240 ws
[current_ws
].focus
=
241 TAILQ_NEXT(ws
[current_ws
].focus
, entry
);
244 case SWM_ARG_ID_FOCUSMAIN
:
245 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);;
252 winfocus
= ws
[current_ws
].focus
;
253 unfocus_win(winlostfocus
);
255 XSync(display
, False
);
258 /* I know this sucks but it works well enough */
263 struct ws_win wf
, *win
, *winfocus
= &wf
;
264 int i
, h
, w
, x
, y
, winno
, hrh
;
266 DNPRINTF(SWM_D_EVENT
, "stack: workspace: %d\n", current_ws
);
270 if (ws
[current_ws
].winno
== 0)
273 if (ws
[current_ws
].winno
> 1)
278 if (ws
[current_ws
].winno
> 2)
279 hrh
= height
/ (ws
[current_ws
].winno
- 1);
287 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
292 if (i
!= 0 && hrh
!= 0) {
293 /* correct the last window for lost pixels */
294 if (win
== TAILQ_LAST(&ws
[current_ws
].winlist
,
296 h
= height
- (i
* hrh
);
304 /* leave first right hand window at y = 0 */
310 bzero(&wc
, sizeof wc
);
313 win
->width
= wc
.width
= w
;
314 win
->height
= wc
.height
= h
;
316 XConfigureWindow(display
, win
->id
, CWX
| CWY
| CWWidth
|
317 CWHeight
| CWBorderWidth
, &wc
);
318 if (win
== ws
[current_ws
].focus
)
322 XMapWindow(display
, win
->id
);
326 focus_win(winfocus
); /* this has to be done outside of the loop */
327 XSync(display
, False
);
331 swap_to_main(union arg
*args
)
333 struct ws_win
*tmpwin
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
335 DNPRINTF(SWM_D_MISC
, "swap_to_main: win: %d\n", ws
[current_ws
].focus
->id
);
336 TAILQ_REMOVE(&ws
[current_ws
].winlist
, tmpwin
, entry
);
337 TAILQ_INSERT_AFTER(&ws
[current_ws
].winlist
, ws
[current_ws
].focus
,
339 TAILQ_REMOVE(&ws
[current_ws
].winlist
, ws
[current_ws
].focus
, entry
);
340 TAILQ_INSERT_HEAD(&ws
[current_ws
].winlist
, ws
[current_ws
].focus
, entry
);
341 ws
[current_ws
].focus
= TAILQ_FIRST(&ws
[current_ws
].winlist
);
346 /* terminal + args */
347 char *term
[] = { "xterm", NULL
};
349 /* key definitions */
353 void (*func
)(union arg
*);
356 /* modifier key function argument */
357 { MODKEY
, XK_Return
, swap_to_main
, {0} },
358 { MODKEY
| ShiftMask
, XK_Return
, spawn
, {.argv
= term
} },
359 { MODKEY
| ShiftMask
, XK_q
, quit
, {0} },
360 { MODKEY
, XK_m
, focus
, {.id
= SWM_ARG_ID_FOCUSMAIN
} },
361 { MODKEY
, XK_1
, switchws
, {.id
= 0} },
362 { MODKEY
, XK_2
, switchws
, {.id
= 1} },
363 { MODKEY
, XK_3
, switchws
, {.id
= 2} },
364 { MODKEY
, XK_4
, switchws
, {.id
= 3} },
365 { MODKEY
, XK_5
, switchws
, {.id
= 4} },
366 { MODKEY
, XK_6
, switchws
, {.id
= 5} },
367 { MODKEY
, XK_7
, switchws
, {.id
= 6} },
368 { MODKEY
, XK_8
, switchws
, {.id
= 7} },
369 { MODKEY
, XK_9
, switchws
, {.id
= 8} },
370 { MODKEY
, XK_0
, switchws
, {.id
= 9} },
371 { MODKEY
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSNEXT
} },
372 { MODKEY
| ShiftMask
, XK_Tab
, focus
, {.id
= SWM_ARG_ID_FOCUSPREV
} },
376 updatenumlockmask(void)
379 XModifierKeymap
*modmap
;
381 DNPRINTF(SWM_D_MISC
, "updatenumlockmask\n");
383 modmap
= XGetModifierMapping(display
);
384 for (i
= 0; i
< 8; i
++)
385 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
386 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
387 == XKeysymToKeycode(display
, XK_Num_Lock
))
388 numlockmask
= (1 << i
);
390 XFreeModifiermap(modmap
);
398 unsigned int modifiers
[] =
399 { 0, LockMask
, numlockmask
, numlockmask
| LockMask
};
401 DNPRINTF(SWM_D_MISC
, "grabkeys\n");
404 XUngrabKey(display
, AnyKey
, AnyModifier
, root
);
405 for(i
= 0; i
< LENGTH(keys
); i
++) {
406 if((code
= XKeysymToKeycode(display
, keys
[i
].keysym
)))
407 for(j
= 0; j
< LENGTH(modifiers
); j
++)
408 XGrabKey(display
, code
,
409 keys
[i
].mod
| modifiers
[j
], root
,
410 True
, GrabModeAsync
, GrabModeAsync
);
416 XExposeEvent
*ev
= &e
->xexpose
;
418 DNPRINTF(SWM_D_EVENT
, "expose: window: %d\n", ev
->window
);
426 XKeyEvent
*ev
= &e
->xkey
;
428 DNPRINTF(SWM_D_EVENT
, "keypress: window: %d\n", ev
->window
);
430 keysym
= XKeycodeToKeysym(display
, (KeyCode
)ev
->keycode
, 0);
431 for(i
= 0; i
< LENGTH(keys
); i
++)
432 if(keysym
== keys
[i
].keysym
433 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
435 keys
[i
].func(&(keys
[i
].args
));
439 buttonpress(XEvent
*e
)
441 XButtonPressedEvent
*ev
= &e
->xbutton
;
444 DNPRINTF(SWM_D_EVENT
, "buttonpress: window: %d\n", ev
->window
);
446 if (ev
->window
== root
)
448 if (ev
->window
== ws
[current_ws
].focus
->id
)
451 TAILQ_FOREACH(win
, &ws
[current_ws
].winlist
, entry
)
452 if (win
->id
== ev
->window
) {
453 /* focus in the clicked window */
454 XSetWindowBorder(display
, ev
->window
, 0xff0000);
455 XSetWindowBorder(display
,
456 ws
[current_ws
].focus
->id
, 0x888888);
457 XSetInputFocus(display
, ev
->window
, RevertToPointerRoot
,
459 ws
[current_ws
].focus
= win
;
460 XSync(display
, False
);
467 configurerequest(XEvent
*e
)
469 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
472 DNPRINTF(SWM_D_EVENT
, "configurerequest: window: %d\n", ev
->window
);
474 XSelectInput(display
, ev
->window
, ButtonPressMask
| EnterWindowMask
|
477 if ((win
= calloc(1, sizeof(struct ws_win
))) == NULL
)
478 errx(1, "calloc: failed to allocate memory for new window");
480 win
->id
= ev
->window
;
481 TAILQ_INSERT_TAIL(&ws
[current_ws
].winlist
, win
, entry
);
482 ws
[current_ws
].focus
= win
; /* make new win focused */
483 ws
[current_ws
].winno
++;
488 configurenotify(XEvent
*e
)
490 XConfigureEvent
*ev
= &e
->xconfigure
;
492 DNPRINTF(SWM_D_EVENT
, "configurenotify: window: %d\n", ev
->window
);
496 destroynotify(XEvent
*e
)
500 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
502 DNPRINTF(SWM_D_EVENT
, "destroynotify: window %d\n", ev
->window
);
504 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
505 if (ev
->window
== win
->id
) {
506 /* find a window to focus */
507 ws
[current_ws
].focus
=
508 TAILQ_PREV(win
, ws_win_list
,entry
);
509 if (ws
[current_ws
].focus
== NULL
)
510 ws
[current_ws
].focus
=
511 TAILQ_FIRST(&ws
[current_ws
].winlist
);
513 TAILQ_REMOVE(&ws
[current_ws
].winlist
, win
, entry
);
515 ws
[current_ws
].winno
--;
524 enternotify(XEvent
*e
)
526 XCrossingEvent
*ev
= &e
->xcrossing
;
529 DNPRINTF(SWM_D_EVENT
, "enternotify: window: %d\n", ev
->window
);
531 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) &&
535 /* eat event(s) to prevent autofocus */
539 TAILQ_FOREACH (win
, &ws
[current_ws
].winlist
, entry
) {
540 if (win
->id
== ev
->window
)
550 XFocusChangeEvent
*ev
= &e
->xfocus
;
552 DNPRINTF(SWM_D_EVENT
, "focusin: window: %d\n", ev
->window
);
554 if (ev
->window
== root
)
558 * kill grab for now so that we can cut and paste , this screws up
562 DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window);
563 XGrabButton(display, Button1, AnyModifier, ev->window, False,
564 ButtonPress, GrabModeAsync, GrabModeSync, None, None);
569 mappingnotify(XEvent
*e
)
571 XMappingEvent
*ev
= &e
->xmapping
;
573 DNPRINTF(SWM_D_EVENT
, "mappingnotify: window: %d\n", ev
->window
);
575 XRefreshKeyboardMapping(ev
);
576 if(ev
->request
== MappingKeyboard
)
581 maprequest(XEvent
*e
)
583 XMapRequestEvent
*ev
= &e
->xmaprequest
;
585 DNPRINTF(SWM_D_EVENT
, "maprequest: window: %d\n", ev
->window
);
589 propertynotify(XEvent
*e
)
591 XPropertyEvent
*ev
= &e
->xproperty
;
593 DNPRINTF(SWM_D_EVENT
, "propertynotify: window: %d\n", ev
->window
);
597 unmapnotify(XEvent
*e
)
599 XUnmapEvent
*ev
= &e
->xunmap
;
601 DNPRINTF(SWM_D_EVENT
, "unmapnotify: window: %d\n", ev
->window
);
604 void (*handler
[LASTEvent
])(XEvent
*) = {
606 [KeyPress
] = keypress
,
607 [ButtonPress
] = buttonpress
,
608 [ConfigureRequest
] = configurerequest
,
609 [ConfigureNotify
] = configurenotify
,
610 [DestroyNotify
] = destroynotify
,
611 [EnterNotify
] = enternotify
,
613 [MappingNotify
] = mappingnotify
,
614 [MapRequest
] = maprequest
,
615 [PropertyNotify
] = propertynotify
,
616 [UnmapNotify
] = unmapnotify
,
620 xerror_start(Display
*d
, XErrorEvent
*ee
)
627 xerror(Display
*d
, XErrorEvent
*ee
)
629 fprintf(stderr
, "error: %p %p\n", display
, ee
);
638 xerrorxlib
= XSetErrorHandler(xerror_start
);
640 /* this causes an error if some other window manager is running */
641 XSelectInput(display
, DefaultRootWindow(display
),
642 SubstructureRedirectMask
);
643 XSync(display
, False
);
647 XSetErrorHandler(xerror
);
648 XSync(display
, False
);
653 main(int argc
, char *argv
[])
658 fprintf(stderr
, "Welcome to scrotwm V%s\n", SWM_VERSION
);
659 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
660 warnx("no locale support");
662 if(!(display
= XOpenDisplay(0)))
663 errx(1, "can not open display");
666 errx(1, "other wm running");
668 screen
= DefaultScreen(display
);
669 root
= RootWindow(display
, screen
);
670 width
= DisplayWidth(display
, screen
) - 2;
671 height
= DisplayHeight(display
, screen
) - 2;
673 /* make work space 1 active */
677 TAILQ_INIT(&ws
[0].winlist
);
678 for (i
= 1; i
< SWM_WS_MAX
; i
++) {
682 TAILQ_INIT(&ws
[i
].winlist
);
685 XSelectInput(display
, root
, SubstructureRedirectMask
|
686 SubstructureNotifyMask
| ButtonPressMask
| KeyPressMask
|
687 EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
|
688 FocusChangeMask
| PropertyChangeMask
);
693 XNextEvent(display
, &e
);
698 XCloseDisplay(display
);