X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=scrotwm.c;h=3cb675ec6e084d454cacc79cc4b746f217f0d338;hb=58096354b15dacd1951cbfab5c061e69c0a859a0;hp=ed80300900863ed429c41bb676ff435d98742910;hpb=1daee531ae5359ccf8d86f15bf63574fd30d6bcf;p=spectrwm.git diff --git a/scrotwm.c b/scrotwm.c index ed80300..3cb675e 100644 --- a/scrotwm.c +++ b/scrotwm.c @@ -1,5 +1,4 @@ -/* $scrotwm$ -/* add to .xinitrc */ +/* $scrotwm$ */ /* * Copyright (c) 2009 Marco Peereboom * Copyright (c) 2009 Ryan McBride @@ -56,6 +55,8 @@ #include #include #include +#include +#include #include #include @@ -74,10 +75,14 @@ #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while(0) #define SWM_D_EVENT 0x0001 #define SWM_D_WS 0x0002 +#define SWM_D_FOCUS 0x0004 +#define SWM_D_MISC 0x0008 uint32_t swm_debug = 0 | SWM_D_EVENT | SWM_D_WS + | SWM_D_FOCUS + | SWM_D_MISC ; #else #define DPRINTF(x...) @@ -95,12 +100,27 @@ int width, height; int running = 1; int ignore_enter = 0; unsigned int numlockmask = 0; +unsigned long col_focus = 0xff0000; /* XXX should this be per ws? */ +unsigned long col_unfocus = 0x888888; Display *display; Window root; +/* status bar */ +int bar_enabled = 1; +int bar_height = 12; +Window bar_window; +GC bar_gc; +XGCValues bar_gcv; +XFontStruct *bar_fs; +char bar_text[128]; + struct ws_win { TAILQ_ENTRY(ws_win) entry; Window id; + int x; + int y; + int width; + int height; }; TAILQ_HEAD(ws_win_list, ws_win); @@ -108,32 +128,53 @@ TAILQ_HEAD(ws_win_list, ws_win); /* define work spaces */ #define SWM_WS_MAX (10) struct workspace { - int visible; /* workspace visible */ + int visible; /* workspace visible */ struct ws_win *focus; /* which win has focus */ - int winno; /* total nr of windows */ - struct ws_win_list winlist; + int winno; /* total nr of windows */ + struct ws_win_list winlist; } ws[SWM_WS_MAX]; int current_ws = 0; /* args to functions */ union arg { - int id; + int id; #define SWM_ARG_ID_FOCUSNEXT (0) #define SWM_ARG_ID_FOCUSPREV (1) #define SWM_ARG_ID_FOCUSMAIN (2) + char **argv; }; void +bar_print(void) +{ + time_t tmt; + struct tm tm; + + /* clear old text */ + XSetForeground(display, bar_gc, 0x000000); + XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text, + strlen(bar_text)); + + /* draw new text */ + time(&tmt); + localtime_r(&tmt, &tm); + strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm); + XSetForeground(display, bar_gc, 0xa0a0a0); + XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text, + strlen(bar_text)); + XSync(display, False); +} +void quit(union arg *args) { + DNPRINTF(SWM_D_MISC, "quit\n"); running = 0; } void spawn(union arg *args) { - char *argv[] = { "xterm", NULL }; /* XXX make this in args */ - + DNPRINTF(SWM_D_MISC, "spawn: %s\n", args->argv[0]); /* * The double-fork construct avoids zombie processes and keeps the code * clean from stupid signal handlers. @@ -143,7 +184,7 @@ spawn(union arg *args) if(display) close(ConnectionNumber(display)); setsid(); - execvp(argv[0], argv); + execvp(args->argv[0], args->argv); fprintf(stderr, "execvp failed\n"); perror(" failed"); } @@ -153,11 +194,28 @@ spawn(union arg *args) } void +focus_win(struct ws_win *win) +{ + DNPRINTF(SWM_D_FOCUS, "focus_win: id: %lu\n", win->id); + XSetWindowBorder(display, win->id, col_focus); + XSetInputFocus(display, win->id, RevertToPointerRoot, CurrentTime); + ws[current_ws].focus = win; +} + +void +unfocus_win(struct ws_win *win) +{ + DNPRINTF(SWM_D_FOCUS, "unfocus_win: id: %lu\n", win->id); + XSetWindowBorder(display, win->id, col_unfocus); + if (ws[current_ws].focus == win) + ws[current_ws].focus = NULL; +} + +void switchws(union arg *args) { int wsid = args->id; struct ws_win *win; - Window winfocus; DNPRINTF(SWM_D_WS, "switchws: %d\n", wsid + 1); @@ -176,23 +234,21 @@ switchws(union arg *args) current_ws = wsid; ignore_enter = 1; - if (ws[wsid].focus != NULL) { - DNPRINTF(SWM_D_WS, "switchws: focus %d\n", ws[wsid].focus); - XSetInputFocus(display, ws[wsid].focus->id, RevertToPointerRoot, - CurrentTime); - } + if (ws[wsid].focus != NULL) + focus_win(ws[wsid].focus); XSync(display, False); } void focus(union arg *args) { - Window winfocus, winlostfocus; + struct ws_win *winfocus, *winlostfocus; + DNPRINTF(SWM_D_FOCUS, "focus: id %d\n", args->id); if (ws[current_ws].focus == NULL || ws[current_ws].winno == 0) return; - winlostfocus = ws[current_ws].focus->id; + winlostfocus = ws[current_ws].focus; switch (args->id) { case SWM_ARG_ID_FOCUSPREV: @@ -223,10 +279,9 @@ focus(union arg *args) return; } - winfocus = ws[current_ws].focus->id; - XSetWindowBorder(display, winlostfocus, 0x888888); - XSetWindowBorder(display, winfocus, 0xff0000); - XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime); + winfocus = ws[current_ws].focus; + unfocus_win(winlostfocus); + focus_win(winfocus); XSync(display, False); } @@ -235,12 +290,13 @@ void stack(void) { XWindowChanges wc; - struct ws_win *win; - Window winfocus = root; - int i, h, w, x, y, winno, hrh; + struct ws_win wf, *win, *winfocus = &wf; + int i, h, w, x, y, hrh; DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws); + winfocus->id = root; + if (ws[current_ws].winno == 0) return; @@ -255,7 +311,7 @@ stack(void) hrh = 0; x = 0; - y = 0; + y = bar_height; h = height; i = 0; TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { @@ -282,23 +338,22 @@ stack(void) } bzero(&wc, sizeof wc); - wc.x = x; - wc.y = y; - wc.width = w; - wc.height = h; + win->x = wc.x = x; + win->y = wc.y = y; + win->width = wc.width = w; + win->height = wc.height = h; wc.border_width = 1; XConfigureWindow(display, win->id, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc); - if (win == ws[current_ws].focus) { - winfocus = win->id; - XSetWindowBorder(display, win->id, 0xff0000); - } else - XSetWindowBorder(display, win->id, 0x888888); + if (win == ws[current_ws].focus) + winfocus = win; + else + unfocus_win(win); XMapWindow(display, win->id); i++; } - XSetInputFocus(display, winfocus, RevertToPointerRoot, CurrentTime); + focus_win(winfocus); /* this has to be done outside of the loop */ XSync(display, False); } @@ -307,6 +362,9 @@ swap_to_main(union arg *args) { struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist); + DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n", + ws[current_ws].focus->id); + TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry); TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus, tmpwin, entry); @@ -317,6 +375,9 @@ swap_to_main(union arg *args) stack(); } +/* terminal + args */ +char *term[] = { "xterm", NULL }; + /* key definitions */ struct key { unsigned int mod; @@ -326,7 +387,7 @@ struct key { } keys[] = { /* modifier key function argument */ { MODKEY, XK_Return, swap_to_main, {0} }, - { MODKEY | ShiftMask, XK_Return, spawn, {0} }, + { MODKEY | ShiftMask, XK_Return, spawn, {.argv = term } }, { MODKEY | ShiftMask, XK_q, quit, {0} }, { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} }, { MODKEY, XK_1, switchws, {.id = 0} }, @@ -349,6 +410,7 @@ updatenumlockmask(void) unsigned int i, j; XModifierKeymap *modmap; + DNPRINTF(SWM_D_MISC, "updatenumlockmask\n"); numlockmask = 0; modmap = XGetModifierMapping(display); for (i = 0; i < 8; i++) @@ -368,6 +430,7 @@ grabkeys(void) unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask | LockMask }; + DNPRINTF(SWM_D_MISC, "grabkeys\n"); updatenumlockmask(); XUngrabKey(display, AnyKey, AnyModifier, root); @@ -382,9 +445,7 @@ grabkeys(void) void expose(XEvent *e) { - XExposeEvent *ev = &e->xexpose; - - DNPRINTF(SWM_D_EVENT, "expose: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "expose: window: %lu\n", e->xexpose.window); } void @@ -394,7 +455,7 @@ keypress(XEvent *e) KeySym keysym; XKeyEvent *ev = &e->xkey; - DNPRINTF(SWM_D_EVENT, "keypress: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "keypress: window: %lu\n", ev->window); keysym = XKeycodeToKeysym(display, (KeyCode)ev->keycode, 0); for(i = 0; i < LENGTH(keys); i++) @@ -408,15 +469,18 @@ void buttonpress(XEvent *e) { XButtonPressedEvent *ev = &e->xbutton; +#ifdef SWM_CLICKTOFOCUS struct ws_win *win; +#endif + - DNPRINTF(SWM_D_EVENT, "buttonpress: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "buttonpress: window: %lu\n", ev->window); if (ev->window == root) return; if (ev->window == ws[current_ws].focus->id) return; - +#ifdef SWM_CLICKTOFOCUS TAILQ_FOREACH(win, &ws[current_ws].winlist, entry) if (win->id == ev->window) { /* focus in the clicked window */ @@ -429,6 +493,7 @@ buttonpress(XEvent *e) XSync(display, False); break; } +#endif } void @@ -437,7 +502,7 @@ configurerequest(XEvent *e) XConfigureRequestEvent *ev = &e->xconfigurerequest; struct ws_win *win; - DNPRINTF(SWM_D_EVENT, "configurerequest: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window); XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask | FocusChangeMask); @@ -455,22 +520,19 @@ configurerequest(XEvent *e) void configurenotify(XEvent *e) { - XConfigureEvent *ev = &e->xconfigure; - - DNPRINTF(SWM_D_EVENT, "configurenotify: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "configurenotify: window: %lu\n", + e->xconfigure.window); } void destroynotify(XEvent *e) { - size_t sz; struct ws_win *win; XDestroyWindowEvent *ev = &e->xdestroywindow; - DNPRINTF(SWM_D_EVENT, "destroynotify: window %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "destroynotify: window %lu\n", ev->window); TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { - DNPRINTF(SWM_D_EVENT, "trying: %x\n", win->id); if (ev->window == win->id) { /* find a window to focus */ ws[current_ws].focus = @@ -495,7 +557,7 @@ enternotify(XEvent *e) XCrossingEvent *ev = &e->xcrossing; struct ws_win *win; - DNPRINTF(SWM_D_EVENT, "enternotify: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "enternotify: window: %lu\n", ev->window); if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root) @@ -506,13 +568,10 @@ enternotify(XEvent *e) return; } TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { - if (win->id == ev->window) { - XSetInputFocus(display, ev->window, RevertToPointerRoot, - CurrentTime); - XSetWindowBorder(display, ev->window, 0xff0000); - ws[current_ws].focus = win; - } else - XSetWindowBorder(display, win->id, 0x888888); + if (win->id == ev->window) + focus_win(win); + else + unfocus_win(win); } } @@ -521,7 +580,7 @@ focusin(XEvent *e) { XFocusChangeEvent *ev = &e->xfocus; - DNPRINTF(SWM_D_EVENT, "focusin: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "focusin: window: %lu\n", ev->window); if (ev->window == root) return; @@ -531,7 +590,7 @@ focusin(XEvent *e) * click to focus */ /* - DNPRINTF(SWM_D_EVENT, "focusin: window: %d grabbing\n", ev->window); + DNPRINTF(SWM_D_EVENT, "focusin: window: %lu grabbing\n", ev->window); XGrabButton(display, Button1, AnyModifier, ev->window, False, ButtonPress, GrabModeAsync, GrabModeSync, None, None); */ @@ -542,7 +601,7 @@ mappingnotify(XEvent *e) { XMappingEvent *ev = &e->xmapping; - DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "mappingnotify: window: %lu\n", ev->window); XRefreshKeyboardMapping(ev); if(ev->request == MappingKeyboard) @@ -552,25 +611,21 @@ mappingnotify(XEvent *e) void maprequest(XEvent *e) { - XMapRequestEvent *ev = &e->xmaprequest; - - DNPRINTF(SWM_D_EVENT, "maprequest: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n", + e->xmaprequest.window); } void propertynotify(XEvent *e) { - XPropertyEvent *ev = &e->xproperty; - - DNPRINTF(SWM_D_EVENT, "propertynotify: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "propertynotify: window: %lu\n", + e->xproperty.window); } void unmapnotify(XEvent *e) { - XUnmapEvent *ev = &e->xunmap; - - DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %d\n", ev->window); + DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window); } void (*handler[LASTEvent])(XEvent *) = { @@ -654,6 +709,18 @@ main(int argc, char *argv[]) TAILQ_INIT(&ws[i].winlist); } + /* setup status bar */ + bar_fs = XLoadQueryFont(display, + "-*-terminus-*-*-*-*-*-*-*-*-*-*-*-*"); + if (bar_fs == NULL) { + /* load a font that is default */ + bar_fs = XLoadQueryFont(display, + "-*-times-medium-r-*-*-*-*-*-*-*-*-*-*"); + if (bar_fs == NULL) + errx(1, "couldn't load font"); + } + bar_height = bar_fs->ascent + bar_fs->descent + 3; + XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask | ButtonPressMask | KeyPressMask | EnterWindowMask | LeaveWindowMask | StructureNotifyMask | @@ -661,6 +728,16 @@ main(int argc, char *argv[]) grabkeys(); + bar_window = XCreateSimpleWindow(display, root, 0, 0, width, + bar_height - 2, 1, 0x008080, 0x000000); + bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv); + XSetFont(display, bar_gc, bar_fs->fid); + if (bar_enabled) { + height -= bar_height; /* correct screen height */ + XMapWindow(display, bar_window); + } + bar_print(); + while (running) { XNextEvent(display, &e); if (handler[e.type])