X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=scrotwm.c;h=9ced019c4a19f4acb1f30ac3461e56fc87b96dcb;hb=0782d0971bc1cfc3e26d141a2282d29b96c7d6b5;hp=5bdca5ab2b503b78d804f78d40eb6442b5d41946;hpb=58414f14f4c2751c1f3e644194c555e90ebd809b;p=spectrwm.git diff --git a/scrotwm.c b/scrotwm.c index 5bdca5a..9ced019 100644 --- a/scrotwm.c +++ b/scrotwm.c @@ -78,16 +78,18 @@ #ifdef SWM_DEBUG #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while(0) #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 +#define SWM_D_MISC 0x0001 +#define SWM_D_EVENT 0x0002 +#define SWM_D_WS 0x0004 +#define SWM_D_FOCUS 0x0008 +#define SWM_D_MOVE 0x0010 -uint32_t swm_debug = 0 +u_int32_t swm_debug = 0 + | SWM_D_MISC | SWM_D_EVENT | SWM_D_WS | SWM_D_FOCUS - | SWM_D_MISC + | SWM_D_MOVE ; #else #define DPRINTF(x...) @@ -130,6 +132,29 @@ char *bar_fonts[] = { /* terminal + args */ char *spawn_term[] = { "xterm", NULL }; char *spawn_menu[] = { "dmenu_run", NULL }; +char *spawn_scrotwm[] = { "scrotwm", NULL }; + +/* layout manager data */ +void stack(void); +void vertical_init(void); +void vertical_stack(void); +void horizontal_init(void); +void horizontal_stack(void); +void max_init(void); +void max_stack(void); + +struct layout { + void (*l_init)(void); /* init/reset */ + void (*l_stack)(void); /* restack windows */ +} layouts[] = { + /* init stack */ + { vertical_init, vertical_stack}, + { horizontal_init, horizontal_stack}, + /* XXX not working yet + * { max_init, max_stack}, + */ + { NULL, NULL}, +}; struct ws_win { TAILQ_ENTRY(ws_win) entry; @@ -138,6 +163,9 @@ struct ws_win { int y; int width; int height; + int floating; + int transient; + XWindowAttributes wa; }; TAILQ_HEAD(ws_win_list, ws_win); @@ -147,8 +175,8 @@ TAILQ_HEAD(ws_win_list, ws_win); struct workspace { int visible; /* workspace visible */ int restack; /* restack on switch */ + struct layout *cur_layout; /* current layout handlers */ struct ws_win *focus; /* which win has focus */ - int winno; /* total nr of windows */ struct ws_win_list winlist; /* list of windows in ws */ } ws[SWM_WS_MAX]; int current_ws = 0; @@ -159,11 +187,13 @@ union arg { #define SWM_ARG_ID_FOCUSNEXT (0) #define SWM_ARG_ID_FOCUSPREV (1) #define SWM_ARG_ID_FOCUSMAIN (2) +#define SWM_ARG_ID_SWAPNEXT (3) +#define SWM_ARG_ID_SWAPPREV (4) +#define SWM_ARG_ID_SWAPMAIN (5) char **argv; }; -void stack(void); #define SWM_CONF_WS "\n= \t" #define SWM_CONF_FILE "scrotwm.conf" @@ -320,16 +350,33 @@ bar_setup(void) bar_height - 2, 1, bar_border, bar_color); bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv); XSetFont(display, bar_gc, bar_fs->fid); + XSelectInput(display, bar_window, VisibilityChangeMask); if (bar_enabled) { height -= bar_height; /* correct screen height */ XMapWindow(display, bar_window); } + DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window); if (signal(SIGALRM, bar_signal) == SIG_ERR) err(1, "could not install bar_signal"); bar_print(); } +int +count_win(int wsid, int count_transient) +{ + struct ws_win *win; + int count = 0; + + TAILQ_FOREACH (win, &ws[wsid].winlist, entry) { + if (count_transient == 0 && win->transient) + continue; + count++; + } + DNPRINTF(SWM_D_MISC, "count_win: %d\n", count); + + return (count); +} void quit(union arg *args) { @@ -337,6 +384,17 @@ quit(union arg *args) running = 0; } + +void +restart(union arg *args) +{ + XCloseDisplay(display); + execvp(args->argv[0], args->argv); + fprintf(stderr, "execvp failed\n"); + perror(" failed"); + quit(NULL); +} + void spawn(union arg *args) { @@ -411,70 +469,150 @@ switchws(union arg *args) } void +swapwin(union arg *args) +{ + struct ws_win *target; + + DNPRINTF(SWM_D_MOVE, "swapwin: id %d\n", args->id); + if (ws[current_ws].focus == NULL) + return; + + switch (args->id) { + case SWM_ARG_ID_SWAPPREV: + target = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry); + TAILQ_REMOVE(&ws[current_ws].winlist, + ws[current_ws].focus, entry); + if (target == NULL) + TAILQ_INSERT_TAIL(&ws[current_ws].winlist, + ws[current_ws].focus, entry); + else + TAILQ_INSERT_BEFORE(target, ws[current_ws].focus, + entry); + break; + case SWM_ARG_ID_SWAPNEXT: + target = TAILQ_NEXT(ws[current_ws].focus, entry); + TAILQ_REMOVE(&ws[current_ws].winlist, + ws[current_ws].focus, entry); + if (target == NULL) + TAILQ_INSERT_HEAD(&ws[current_ws].winlist, + ws[current_ws].focus, entry); + else + TAILQ_INSERT_AFTER(&ws[current_ws].winlist, target, + ws[current_ws].focus, entry); + break; + case SWM_ARG_ID_SWAPMAIN: + target = TAILQ_FIRST(&ws[current_ws].winlist); + if (target == ws[current_ws].focus) + return; + TAILQ_REMOVE(&ws[current_ws].winlist, target, entry); + TAILQ_INSERT_BEFORE(ws[current_ws].focus, target, entry); + TAILQ_REMOVE(&ws[current_ws].winlist, + ws[current_ws].focus, entry); + TAILQ_INSERT_HEAD(&ws[current_ws].winlist, + ws[current_ws].focus, entry); + break; + default: + DNPRINTF(SWM_D_MOVE, "invalid id: %d\n", args->id); + return; + } + + ignore_enter = 2; + stack(); +} + +void focus(union arg *args) { 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) + if (ws[current_ws].focus == NULL) return; winlostfocus = ws[current_ws].focus; switch (args->id) { case SWM_ARG_ID_FOCUSPREV: - ws[current_ws].focus = - TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry); - if (ws[current_ws].focus == NULL) - ws[current_ws].focus = + winfocus = TAILQ_PREV(ws[current_ws].focus, ws_win_list, entry); + if (winfocus == NULL) + winfocus = TAILQ_LAST(&ws[current_ws].winlist, ws_win_list); break; case SWM_ARG_ID_FOCUSNEXT: - ws[current_ws].focus = TAILQ_NEXT(ws[current_ws].focus, entry); - if (ws[current_ws].focus == NULL) - ws[current_ws].focus = - TAILQ_FIRST(&ws[current_ws].winlist); + winfocus = TAILQ_NEXT(ws[current_ws].focus, entry); + if (winfocus == NULL) + winfocus = TAILQ_FIRST(&ws[current_ws].winlist); break; case SWM_ARG_ID_FOCUSMAIN: - ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist); + winfocus = TAILQ_FIRST(&ws[current_ws].winlist); break; default: return; } - winfocus = ws[current_ws].focus; + if (winfocus == winlostfocus) + return; + unfocus_win(winlostfocus); focus_win(winfocus); XSync(display, False); } -/* I know this sucks but it works well enough */ void -stack(void) +cycle_layout(union arg *args) { + DNPRINTF(SWM_D_EVENT, "cycle_layout: workspace: %d\n", current_ws); + + ws[current_ws].cur_layout++; + if (ws[current_ws].cur_layout->l_stack == NULL) + ws[current_ws].cur_layout = &layouts[0]; + stack(); +} + +void +stack(void) { + DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws); + + ws[current_ws].restack = 0; + + ws[current_ws].cur_layout->l_stack(); + + XSync(display, False); +} + +void +vertical_init(void) +{ + DNPRINTF(SWM_D_EVENT, "vertical_init: workspace: %d\n", current_ws); +} + +/* I know this sucks but it works well enough */ +void +vertical_stack(void) { XWindowChanges wc; struct ws_win wf, *win, *winfocus = &wf; - int i, h, w, x, y, hrh; + int i, h, w, x, y, hrh, winno; + int floater = 0; + unsigned int mask; - DNPRINTF(SWM_D_EVENT, "stack: workspace: %d\n", current_ws); + DNPRINTF(SWM_D_EVENT, "vertical_stack: workspace: %d\n", current_ws); winfocus->id = root; - ws[current_ws].restack = 0; - - if (ws[current_ws].winno == 0) + winno = count_win(current_ws, 0); + if (winno == 0) return; - if (ws[current_ws].winno > 1) + if (winno > 1) w = width / 2; else w = width; - if (ws[current_ws].winno > 2) - hrh = height / (ws[current_ws].winno - 1); + if (winno > 2) + hrh = height / (winno - 1); else hrh = 0; @@ -505,14 +643,31 @@ stack(void) } } + if (win->transient != 0 || win->floating != 0) + floater = 1; + else + floater = 0; + bzero(&wc, sizeof wc); - 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 (floater == 0) { + win->x = wc.x = x; + win->y = wc.y = y; + win->width = wc.width = w; + win->height = wc.height = h; + mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth; + } else { + /* make sure we don't clobber the screen */ + if (win->wa.width > width) + win->wa.width = width; + if (win->wa.height > height) + win->wa.width = height; + win->x = wc.x = (width - win->wa.width) / 2; + win->y = wc.y = (height - win->wa.height) / 2; + mask = CWX | CWY | CWBorderWidth; + } + XConfigureWindow(display, win->id, mask, &wc); + if (win == ws[current_ws].focus) winfocus = win; else @@ -522,28 +677,152 @@ stack(void) } focus_win(winfocus); /* this has to be done outside of the loop */ - XSync(display, False); } void -swap_to_main(union arg *args) +horizontal_init(void) { - struct ws_win *tmpwin = TAILQ_FIRST(&ws[current_ws].winlist); + DNPRINTF(SWM_D_EVENT, "horizontal_init: workspace: %d\n", current_ws); +} + +void +horizontal_stack(void) { + XWindowChanges wc; + struct ws_win wf, *win, *winfocus = &wf; + int i, h, w, x, y, hrw, winno; + int floater = 0; + unsigned int mask; + + DNPRINTF(SWM_D_EVENT, "horizontal_stack: workspace: %d\n", current_ws); - DNPRINTF(SWM_D_MISC, "swap_to_main: win: %lu\n", - ws[current_ws].focus ? ws[current_ws].focus->id : 0); + winfocus->id = root; - if (ws[current_ws].focus == NULL || ws[current_ws].focus == tmpwin) + winno = count_win(current_ws, 0); + if (winno == 0) return; - TAILQ_REMOVE(&ws[current_ws].winlist, tmpwin, entry); - TAILQ_INSERT_AFTER(&ws[current_ws].winlist, ws[current_ws].focus, - tmpwin, entry); - TAILQ_REMOVE(&ws[current_ws].winlist, ws[current_ws].focus, entry); - TAILQ_INSERT_HEAD(&ws[current_ws].winlist, ws[current_ws].focus, entry); - ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist); - ignore_enter = 2; - stack(); + if (winno > 1) + h = height / 2; + else + h = height; + + if (winno > 2) + hrw = width / (winno - 1); + else + hrw = 0; + + x = 0; + y = bar_enabled ? bar_height : 0; + w = width; + i = 0; + TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { + if (i == 1) { + y += h + 2; + h -= 2; + } + if (i != 0 && hrw != 0) { + /* correct the last window for lost pixels */ + if (win == TAILQ_LAST(&ws[current_ws].winlist, + ws_win_list)) { + w = width - (i * hrw); + if (w == 0) + w = hrw; + else + w += hrw; + x += hrw; + } else { + w = hrw - 2; + /* leave first bottom window at x = 0 */ + if (i > 1) + x += w + 2; + } + } + + if (win->transient != 0 || win->floating != 0) + floater = 1; + else + floater = 0; + + bzero(&wc, sizeof wc); + wc.border_width = 1; + if (floater == 0) { + win->x = wc.x = x; + win->y = wc.y = y; + win->width = wc.width = w; + win->height = wc.height = h; + mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth; + } else { + /* make sure we don't clobber the screen */ + if (win->wa.width > width) + win->wa.width = width; + if (win->wa.height > height) + win->wa.width = height; + win->x = wc.x = (width - win->wa.width) / 2; + win->y = wc.y = (height - win->wa.height) / 2; + mask = CWX | CWY | CWBorderWidth; + } + XConfigureWindow(display, win->id, mask, &wc); + + if (win == ws[current_ws].focus) + winfocus = win; + else + unfocus_win(win); + XMapRaised(display, win->id); + i++; + } + + focus_win(winfocus); /* this has to be done outside of the loop */ +} + +/* fullscreen view */ +void +max_init(void) +{ + DNPRINTF(SWM_D_EVENT, "max_init: workspace: %d\n", current_ws); +} + +void +max_stack(void) { + XWindowChanges wc; + struct ws_win wf, *win, *winfocus = &wf; + int i, h, w, x, y, winno; + unsigned int mask; + + DNPRINTF(SWM_D_EVENT, "max_stack: workspace: %d\n", current_ws); + + winfocus->id = root; + + winno = count_win(current_ws, 0); + if (winno == 0) + return; + + x = 0; + y = bar_enabled ? bar_height : 0; + TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { + if (i == 1 && win->transient == 0 && win->floating != 0) { + h = height - 2; + w = width - 2; + + winfocus = win; + + bzero(&wc, sizeof wc); + wc.border_width = 1; + win->x = wc.x = x; + win->y = wc.y = y; + win->width = wc.width = w; + win->height = wc.height = h; + mask = CWX | CWY | CWWidth | CWHeight | CWBorderWidth; + XConfigureWindow(display, win->id, mask, &wc); + + XMapRaised(display, win->id); + } else { + /* hide all but the master window */ + XUnmapWindow(display, win->id); + } + i++; + } + + focus_win(winfocus); /* this has to be done outside of the loop */ } void @@ -552,22 +831,22 @@ send_to_ws(union arg *args) int wsid = args->id; struct ws_win *win = ws[current_ws].focus; - DNPRINTF(SWM_D_WS, "send_to_ws: win: %lu\n", win->id); + DNPRINTF(SWM_D_MOVE, "send_to_ws: win: %lu\n", win->id); XUnmapWindow(display, win->id); /* find a window to focus */ - ws[current_ws].focus = TAILQ_PREV(win, ws_win_list,entry); + ws[current_ws].focus = TAILQ_PREV(win, ws_win_list, entry); if (ws[current_ws].focus == NULL) ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist); + if (ws[current_ws].focus == win) + ws[current_ws].focus = NULL; TAILQ_REMOVE(&ws[current_ws].winlist, win, entry); - ws[current_ws].winno--; TAILQ_INSERT_TAIL(&ws[wsid].winlist, win, entry); - if (ws[wsid].winno == 0) + if (count_win(wsid, 1) == 0) ws[wsid].focus = win; - ws[wsid].winno++; ws[wsid].restack = 1; stack(); @@ -581,10 +860,17 @@ struct key { union arg args; } keys[] = { /* modifier key function argument */ - { MODKEY, XK_Return, swap_to_main, {0} }, + /* XXX alt-c is temporary, until I figure out how to grab spacebar */ + { MODKEY, XK_c, cycle_layout, {0} }, + { MODKEY, XK_Return, swapwin, {.id = SWM_ARG_ID_SWAPMAIN} }, + { MODKEY, XK_j, focus, {.id = SWM_ARG_ID_FOCUSNEXT} }, + { MODKEY, XK_k, focus, {.id = SWM_ARG_ID_FOCUSPREV} }, + { MODKEY | ShiftMask, XK_j, swapwin, {.id = SWM_ARG_ID_SWAPNEXT} }, + { MODKEY | ShiftMask, XK_k, swapwin, {.id = SWM_ARG_ID_SWAPPREV} }, { MODKEY | ShiftMask, XK_Return, spawn, {.argv = spawn_term} }, { MODKEY, XK_p, spawn, {.argv = spawn_menu} }, { MODKEY | ShiftMask, XK_q, quit, {0} }, + { MODKEY, XK_q, restart, {.argv = spawn_scrotwm } }, { MODKEY, XK_m, focus, {.id = SWM_ARG_ID_FOCUSMAIN} }, { MODKEY, XK_1, switchws, {.id = 0} }, { MODKEY, XK_2, switchws, {.id = 1} }, @@ -703,29 +989,62 @@ buttonpress(XEvent *e) #endif } -void -configurerequest(XEvent *e) +struct ws_win * +manage_window(Window id) { - XConfigureRequestEvent *ev = &e->xconfigurerequest; struct ws_win *win; - DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window); - TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { - if (ev->window == win->id) - return; + if (win->id == id) + return (win); /* already being managed */ } - XSelectInput(display, ev->window, ButtonPressMask | EnterWindowMask | - FocusChangeMask); - if ((win = calloc(1, sizeof(struct ws_win))) == NULL) errx(1, "calloc: failed to allocate memory for new window"); - win->id = ev->window; + win->id = id; TAILQ_INSERT_TAIL(&ws[current_ws].winlist, win, entry); + + XSelectInput(display, id, ButtonPressMask | EnterWindowMask | + FocusChangeMask | ExposureMask); + + return win; +} + + +void +configurerequest(XEvent *e) +{ + XConfigureRequestEvent *ev = &e->xconfigurerequest; + Window trans; + struct ws_win *win; + + DNPRINTF(SWM_D_EVENT, "configurerequest: window: %lu\n", ev->window); + + + win = manage_window(ev->window); ws[current_ws].focus = win; /* make new win focused */ - ws[current_ws].winno++; + + XGetTransientForHint(display, win->id, &trans); + if (trans) { + win->transient = trans; + DNPRINTF(SWM_D_MISC, "configurerequest: win %u transient %u\n", + (unsigned)win->id, win->transient); + } + XGetWindowAttributes(display, win->id, &win->wa); +#if 0 + XClassHint ch = { 0 }; + if(XGetClassHint(display, win->id, &ch)) { + fprintf(stderr, "class: %s name: %s\n", ch.res_class, ch.res_name); + if (!strcmp(ch.res_class, "Gvim") && !strcmp(ch.res_name, "gvim")) { + win->floating = 0; + } + if(ch.res_class) + XFree(ch.res_class); + if(ch.res_name) + XFree(ch.res_name); + } +#endif stack(); } @@ -747,15 +1066,16 @@ destroynotify(XEvent *e) TAILQ_FOREACH (win, &ws[current_ws].winlist, entry) { if (ev->window == win->id) { /* find a window to focus */ - ws[current_ws].focus = - TAILQ_PREV(win, ws_win_list,entry); + ws[current_ws].focus = TAILQ_PREV(win, + ws_win_list, entry); if (ws[current_ws].focus == NULL) ws[current_ws].focus = TAILQ_FIRST(&ws[current_ws].winlist); + if (win == ws[current_ws].focus) + ws[current_ws].focus = NULL; TAILQ_REMOVE(&ws[current_ws].winlist, win, entry); free(win); - ws[current_ws].winno--; break; } } @@ -826,6 +1146,9 @@ maprequest(XEvent *e) { DNPRINTF(SWM_D_EVENT, "maprequest: window: %lu\n", e->xmaprequest.window); + + manage_window(e->xmaprequest.window); + stack(); } void @@ -841,6 +1164,16 @@ unmapnotify(XEvent *e) DNPRINTF(SWM_D_EVENT, "unmapnotify: window: %lu\n", e->xunmap.window); } +void +visibilitynotify(XEvent *e) +{ + DNPRINTF(SWM_D_EVENT, "visibilitynotify: window: %lu\n", e->xvisibility.window); + + if (e->xvisibility.window == bar_window && + e->xvisibility.state == VisibilityUnobscured) + bar_print(); +} + void (*handler[LASTEvent])(XEvent *) = { [Expose] = expose, [KeyPress] = keypress, @@ -854,6 +1187,7 @@ void (*handler[LASTEvent])(XEvent *) = { [MapRequest] = maprequest, [PropertyNotify] = propertynotify, [UnmapNotify] = unmapnotify, + [VisibilityNotify] = visibilitynotify, }; int @@ -896,7 +1230,9 @@ main(int argc, char *argv[]) char conf[PATH_MAX], *cfile = NULL; struct stat sb; XEvent e; - int i; + unsigned int i, num; + Window d1, d2, *wins = NULL; + XWindowAttributes wa; fprintf(stderr, "Welcome to scrotwm V%s\n", SWM_VERSION); if(!setlocale(LC_CTYPE, "") || !XSupportsLocale()) @@ -932,19 +1268,30 @@ main(int argc, char *argv[]) if (cfile) conf_load(cfile); - /* make work space 1 active */ - ws[0].visible = 1; - ws[0].restack = 0; - ws[0].focus = NULL; - ws[0].winno = 0; - TAILQ_INIT(&ws[0].winlist); - for (i = 1; i < SWM_WS_MAX; i++) { + /* init all workspaces */ + for (i = 0; i < SWM_WS_MAX; i++) { ws[i].visible = 0; - ws[i].restack = 0; + ws[i].restack = 1; ws[i].focus = NULL; - ws[i].winno = 0; TAILQ_INIT(&ws[i].winlist); + ws[i].cur_layout = &layouts[0]; } + /* make work space 1 active */ + ws[0].visible = 1; + + /* grab existing windows */ + if (XQueryTree(display, root, &d1, &d2, &wins, &num)) { + for (i = 0; i < num; i++) { + if (!XGetWindowAttributes(display, wins[i], &wa) + || wa.override_redirect || + XGetTransientForHint(display, wins[i], &d1)) + continue; + manage_window(wins[i]); + } + if(wins) + XFree(wins); + } + ws[0].focus = TAILQ_FIRST(&ws[0].winlist); /* setup status bar */ bar_setup(); @@ -952,9 +1299,10 @@ main(int argc, char *argv[]) XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask | ButtonPressMask | KeyPressMask | EnterWindowMask | LeaveWindowMask | StructureNotifyMask | - FocusChangeMask | PropertyChangeMask); + FocusChangeMask | PropertyChangeMask | ExposureMask); grabkeys(); + stack(); while (running) { XNextEvent(display, &e);