+ if (win == NULL)
+ return;
+
+ ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
+ _NET_WM_STATE_TOGGLE);
+
+ stack();
+ if (focus_mode == SWM_FOCUS_DEFAULT)
+ drain_enter_notify();
+
+ if (win == win->ws->focus) {
+ a.id = SWM_ARG_ID_FOCUSCUR;
+ focus(win->ws->r, &a);
+ }
+}
+
+void
+constrain_window(struct ws_win *win, struct swm_region *r, int resizable)
+{
+ if (X(win) + WIDTH(win) > X(r) + WIDTH(r) - border_width) {
+ if (resizable)
+ WIDTH(win) = X(r) + WIDTH(r) - X(win) - border_width;
+ else
+ X(win) = X(r) + WIDTH(r) - WIDTH(win) - border_width;
+ }
+
+ if (X(win) < X(r) - border_width) {
+ if (resizable)
+ WIDTH(win) -= X(r) - X(win) - border_width;
+
+ X(win) = X(r) - border_width;
+ }
+
+ if (Y(win) + HEIGHT(win) > Y(r) + HEIGHT(r) - border_width) {
+ if (resizable)
+ HEIGHT(win) = Y(r) + HEIGHT(r) - Y(win) - border_width;
+ else
+ Y(win) = Y(r) + HEIGHT(r) - HEIGHT(win) - border_width;
+ }
+
+ if (Y(win) < Y(r) - border_width) {
+ if (resizable)
+ HEIGHT(win) -= Y(r) - Y(win) - border_width;
+
+ Y(win) = Y(r) - border_width;
+ }
+
+ if (WIDTH(win) < 1)
+ WIDTH(win) = 1;
+ if (HEIGHT(win) < 1)
+ HEIGHT(win) = 1;
+}
+
+void
+update_window(struct ws_win *win)
+{
+ unsigned int mask;
+ XWindowChanges wc;
+
+ bzero(&wc, sizeof wc);
+ mask = CWBorderWidth | CWWidth | CWHeight | CWX | CWY;
+ wc.border_width = border_width;
+ wc.x = X(win);
+ wc.y = Y(win);
+ wc.width = WIDTH(win);
+ wc.height = HEIGHT(win);
+
+ DNPRINTF(SWM_D_MISC, "update_window: window: 0x%lx, (x,y) w x h: "
+ "(%d,%d) %d x %d\n", win->id, wc.x, wc.y, wc.width, wc.height);
+
+ XConfigureWindow(display, win->id, mask, &wc);
+}
+
+#define SWM_RESIZE_STEPS (50)
+
+void
+resize(struct ws_win *win, union arg *args)
+{
+ XEvent ev;
+ Time time = 0;
+ struct swm_region *r = NULL;
+ int resize_step = 0;
+ Window rr, cr;
+ int x, y, wx, wy;
+ unsigned int mask;
+ struct swm_geometry g;
+ int top = 0, left = 0;
+ int dx, dy;
+ Cursor cursor;
+ unsigned int shape; /* cursor style */
+
+ if (win == NULL)
+ return;
+ r = win->ws->r;
+
+ DNPRINTF(SWM_D_MOUSE, "resize: window: 0x%lx, floating: %s, "
+ "transient: 0x%lx\n", win->id, YESNO(win->floating),
+ win->transient);
+
+ if (!(win->transient != 0 || win->floating != 0))
+ return;
+
+ /* reject resizes in max mode for floaters (transient ok) */
+ if (win->floatmaxed)
+ return;
+
+ win->manual = 1;
+ ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
+ _NET_WM_STATE_ADD);
+
+ stack();
+
+ switch (args->id) {
+ case SWM_ARG_ID_WIDTHSHRINK:
+ WIDTH(win) -= SWM_RESIZE_STEPS;
+ resize_step = 1;
+ break;
+ case SWM_ARG_ID_WIDTHGROW:
+ WIDTH(win) += SWM_RESIZE_STEPS;
+ resize_step = 1;
+ break;
+ case SWM_ARG_ID_HEIGHTSHRINK:
+ HEIGHT(win) -= SWM_RESIZE_STEPS;
+ resize_step = 1;
+ break;
+ case SWM_ARG_ID_HEIGHTGROW:
+ HEIGHT(win) += SWM_RESIZE_STEPS;
+ resize_step = 1;
+ break;
+ default:
+ break;
+ }
+ if (resize_step) {
+ constrain_window(win, r, 1);
+ update_window(win);
+ store_float_geom(win,r);
+ return;
+ }
+
+ if (focus_mode == SWM_FOCUS_DEFAULT)
+ drain_enter_notify();
+
+ /* get cursor offset from window root */
+ if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
+ return;
+
+ g = win->g;
+
+ if (wx < WIDTH(win) / 2)
+ left = 1;
+
+ if (wy < HEIGHT(win) / 2)
+ top = 1;
+
+ if (args->id == SWM_ARG_ID_CENTER)
+ shape = XC_sizing;
+ else if (top)
+ shape = (left) ? XC_top_left_corner : XC_top_right_corner;
+ else
+ shape = (left) ? XC_bottom_left_corner : XC_bottom_right_corner;
+
+ cursor = XCreateFontCursor(display, shape);
+
+ if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
+ GrabModeAsync, None, cursor, CurrentTime) != GrabSuccess) {
+ XFreeCursor(display, cursor);
+ return;
+ }
+
+ do {
+ XMaskEvent(display, MOUSEMASK | ExposureMask |
+ SubstructureRedirectMask, &ev);
+ switch (ev.type) {
+ case ConfigureRequest:
+ case Expose:
+ case MapRequest:
+ handler[ev.type](&ev);
+ break;
+ case MotionNotify:
+ /* cursor offset/delta from start of the operation */
+ dx = ev.xmotion.x_root - x;
+ dy = ev.xmotion.y_root - y;
+
+ /* vertical */
+ if (top)
+ dy = -dy;
+
+ if (args->id == SWM_ARG_ID_CENTER) {
+ if (g.h / 2 + dy < 1)
+ dy = 1 - g.h / 2;
+
+ Y(win) = g.y - dy;
+ HEIGHT(win) = g.h + 2 * dy;
+ } else {
+ if (g.h + dy < 1)
+ dy = 1 - g.h;
+
+ if (top)
+ Y(win) = g.y - dy;
+
+ HEIGHT(win) = g.h + dy;
+ }
+
+ /* horizontal */
+ if (left)
+ dx = -dx;
+
+ if (args->id == SWM_ARG_ID_CENTER) {
+ if (g.w / 2 + dx < 1)
+ dx = 1 - g.w / 2;
+
+ X(win) = g.x - dx;
+ WIDTH(win) = g.w + 2 * dx;
+ } else {
+ if (g.w + dx < 1)
+ dx = 1 - g.w;
+
+ if (left)
+ X(win) = g.x - dx;
+
+ WIDTH(win) = g.w + dx;
+ }
+
+ constrain_window(win, r, 1);
+
+ /* not free, don't sync more than 120 times / second */
+ if ((ev.xmotion.time - time) > (1000 / 120) ) {
+ time = ev.xmotion.time;
+ XSync(display, False);
+ update_window(win);
+ }
+ break;
+ }
+ } while (ev.type != ButtonRelease);
+ if (time) {
+ XSync(display, False);
+ update_window(win);
+ }
+ store_float_geom(win,r);
+
+ XUngrabPointer(display, CurrentTime);
+ XFreeCursor(display, cursor);
+
+ /* drain events */
+ drain_enter_notify();
+}
+
+void
+resize_step(struct swm_region *r, union arg *args)
+{
+ struct ws_win *win = NULL;
+
+ if (r && r->ws && r->ws->focus)
+ win = r->ws->focus;
+ else
+ return;
+
+ resize(win, args);
+}
+
+#define SWM_MOVE_STEPS (50)
+
+void
+move(struct ws_win *win, union arg *args)
+{
+ XEvent ev;
+ Time time = 0;
+ int move_step = 0;
+ struct swm_region *r = NULL;
+
+ Window rr, cr;
+ int x, y, wx, wy;
+ unsigned int mask;
+
+ if (win == NULL)
+ return;
+ r = win->ws->r;
+
+ DNPRINTF(SWM_D_MOUSE, "move: window: 0x%lx, floating: %s, transient: "
+ "0x%lx\n", win->id, YESNO(win->floating), win->transient);
+
+ /* in max_stack mode should only move transients */
+ if (win->ws->cur_layout == &layouts[SWM_MAX_STACK] && !win->transient)
+ return;
+
+ win->manual = 1;
+ if (win->floating == 0 && !win->transient) {
+ store_float_geom(win,r);
+ ewmh_update_win_state(win, ewmh[_NET_WM_STATE_ABOVE].atom,
+ _NET_WM_STATE_ADD);
+ }
+ ewmh_update_win_state(win, ewmh[_SWM_WM_STATE_MANUAL].atom,
+ _NET_WM_STATE_ADD);
+
+ stack();
+
+ move_step = 0;
+ switch (args->id) {
+ case SWM_ARG_ID_MOVELEFT:
+ X(win) -= (SWM_MOVE_STEPS - border_width);
+ move_step = 1;
+ break;
+ case SWM_ARG_ID_MOVERIGHT:
+ X(win) += (SWM_MOVE_STEPS - border_width);
+ move_step = 1;
+ break;
+ case SWM_ARG_ID_MOVEUP:
+ Y(win) -= (SWM_MOVE_STEPS - border_width);
+ move_step = 1;
+ break;
+ case SWM_ARG_ID_MOVEDOWN:
+ Y(win) += (SWM_MOVE_STEPS - border_width);
+ move_step = 1;
+ break;
+ default:
+ break;
+ }
+ if (move_step) {
+ constrain_window(win, r, 0);
+ update_window(win);
+ store_float_geom(win, r);
+ return;
+ }
+
+ if (XGrabPointer(display, win->id, False, MOUSEMASK, GrabModeAsync,
+ GrabModeAsync, None, XCreateFontCursor(display, XC_fleur),
+ CurrentTime) != GrabSuccess)
+ return;
+
+ /* get cursor offset from window root */
+ if (!XQueryPointer(display, win->id, &rr, &cr, &x, &y, &wx, &wy, &mask))
+ return;
+
+ do {
+ XMaskEvent(display, MOUSEMASK | ExposureMask |
+ SubstructureRedirectMask, &ev);
+ switch (ev.type) {
+ case ConfigureRequest:
+ case Expose:
+ case MapRequest:
+ handler[ev.type](&ev);
+ break;
+ case MotionNotify:
+ X(win) = ev.xmotion.x_root - wx - border_width;
+ Y(win) = ev.xmotion.y_root - wy - border_width;
+
+ constrain_window(win, r, 0);
+
+ /* not free, don't sync more than 120 times / second */
+ if ((ev.xmotion.time - time) > (1000 / 120) ) {
+ time = ev.xmotion.time;
+ XSync(display, False);
+ update_window(win);
+ }
+ break;
+ }
+ } while (ev.type != ButtonRelease);
+ if (time) {
+ XSync(display, False);
+ update_window(win);
+ }
+ store_float_geom(win,r);
+ XUngrabPointer(display, CurrentTime);
+
+ /* drain events */
+ drain_enter_notify();
+}
+
+void
+move_step(struct swm_region *r, union arg *args)
+{
+ struct ws_win *win = NULL;
+
+ if (r && r->ws && r->ws->focus)
+ win = r->ws->focus;
+ else
+ return;
+
+ if (!(win->transient != 0 || win->floating != 0))
+ return;
+
+ move(win, args);
+}
+
+
+/* user/key callable function IDs */
+enum keyfuncid {
+ kf_cycle_layout,
+ kf_flip_layout,
+ kf_stack_reset,
+ kf_master_shrink,
+ kf_master_grow,
+ kf_master_add,
+ kf_master_del,
+ kf_stack_inc,
+ kf_stack_dec,
+ kf_swap_main,
+ kf_focus_next,
+ kf_focus_prev,
+ kf_swap_next,
+ kf_swap_prev,
+ kf_spawn_term,
+ kf_spawn_menu,
+ kf_quit,
+ kf_restart,
+ kf_focus_main,
+ kf_ws_1,
+ kf_ws_2,
+ kf_ws_3,
+ kf_ws_4,
+ kf_ws_5,
+ kf_ws_6,
+ kf_ws_7,
+ kf_ws_8,
+ kf_ws_9,
+ kf_ws_10,
+ kf_ws_next,
+ kf_ws_prev,
+ kf_ws_next_all,
+ kf_ws_prev_all,
+ kf_ws_prior,
+ kf_screen_next,
+ kf_screen_prev,
+ kf_mvws_1,
+ kf_mvws_2,
+ kf_mvws_3,
+ kf_mvws_4,
+ kf_mvws_5,
+ kf_mvws_6,
+ kf_mvws_7,
+ kf_mvws_8,
+ kf_mvws_9,
+ kf_mvws_10,
+ kf_bar_toggle,
+ kf_wind_kill,
+ kf_wind_del,
+ kf_screenshot_all,
+ kf_screenshot_wind,
+ kf_float_toggle,
+ kf_version,
+ kf_spawn_lock,
+ kf_spawn_initscr,
+ kf_spawn_custom,
+ kf_iconify,
+ kf_uniconify,
+ kf_raise_toggle,
+ kf_button2,
+ kf_width_shrink,
+ kf_width_grow,
+ kf_height_shrink,
+ kf_height_grow,
+ kf_move_left,
+ kf_move_right,
+ kf_move_up,
+ kf_move_down,
+ kf_name_workspace,
+ kf_search_workspace,
+ kf_search_win,
+ kf_dumpwins, /* MUST BE LAST */
+ kf_invalid
+};
+
+/* key definitions */
+void
+dummykeyfunc(struct swm_region *r, union arg *args)
+{
+};
+
+void
+legacyfunc(struct swm_region *r, union arg *args)
+{
+};
+
+struct keyfunc {
+ char name[SWM_FUNCNAME_LEN];
+ void (*func)(struct swm_region *r, union arg *);
+ union arg args;
+} keyfuncs[kf_invalid + 1] = {
+ /* name function argument */
+ { "cycle_layout", cycle_layout, {0} },
+ { "flip_layout", stack_config, {.id = SWM_ARG_ID_FLIPLAYOUT} },
+ { "stack_reset", stack_config, {.id = SWM_ARG_ID_STACKRESET} },
+ { "master_shrink", stack_config, {.id = SWM_ARG_ID_MASTERSHRINK} },
+ { "master_grow", stack_config, {.id = SWM_ARG_ID_MASTERGROW} },
+ { "master_add", stack_config, {.id = SWM_ARG_ID_MASTERADD} },
+ { "master_del", stack_config, {.id = SWM_ARG_ID_MASTERDEL} },
+ { "stack_inc", stack_config, {.id = SWM_ARG_ID_STACKINC} },
+ { "stack_dec", stack_config, {.id = SWM_ARG_ID_STACKDEC} },
+ { "swap_main", swapwin, {.id = SWM_ARG_ID_SWAPMAIN} },
+ { "focus_next", focus, {.id = SWM_ARG_ID_FOCUSNEXT} },
+ { "focus_prev", focus, {.id = SWM_ARG_ID_FOCUSPREV} },
+ { "swap_next", swapwin, {.id = SWM_ARG_ID_SWAPNEXT} },
+ { "swap_prev", swapwin, {.id = SWM_ARG_ID_SWAPPREV} },
+ { "spawn_term", spawnterm, {.argv = spawn_term} },
+ { "spawn_menu", legacyfunc, {0} },
+ { "quit", quit, {0} },
+ { "restart", restart, {0} },
+ { "focus_main", focus, {.id = SWM_ARG_ID_FOCUSMAIN} },
+ { "ws_1", switchws, {.id = 0} },
+ { "ws_2", switchws, {.id = 1} },
+ { "ws_3", switchws, {.id = 2} },
+ { "ws_4", switchws, {.id = 3} },
+ { "ws_5", switchws, {.id = 4} },
+ { "ws_6", switchws, {.id = 5} },
+ { "ws_7", switchws, {.id = 6} },
+ { "ws_8", switchws, {.id = 7} },
+ { "ws_9", switchws, {.id = 8} },
+ { "ws_10", switchws, {.id = 9} },
+ { "ws_next", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP} },
+ { "ws_prev", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN} },
+ { "ws_next_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_UP_ALL} },
+ { "ws_prev_all", cyclews, {.id = SWM_ARG_ID_CYCLEWS_DOWN_ALL} },
+ { "ws_prior", priorws, {0} },
+ { "screen_next", cyclescr, {.id = SWM_ARG_ID_CYCLESC_UP} },
+ { "screen_prev", cyclescr, {.id = SWM_ARG_ID_CYCLESC_DOWN} },
+ { "mvws_1", send_to_ws, {.id = 0} },
+ { "mvws_2", send_to_ws, {.id = 1} },
+ { "mvws_3", send_to_ws, {.id = 2} },
+ { "mvws_4", send_to_ws, {.id = 3} },
+ { "mvws_5", send_to_ws, {.id = 4} },
+ { "mvws_6", send_to_ws, {.id = 5} },
+ { "mvws_7", send_to_ws, {.id = 6} },
+ { "mvws_8", send_to_ws, {.id = 7} },
+ { "mvws_9", send_to_ws, {.id = 8} },
+ { "mvws_10", send_to_ws, {.id = 9} },
+ { "bar_toggle", bar_toggle, {0} },
+ { "wind_kill", wkill, {.id = SWM_ARG_ID_KILLWINDOW} },
+ { "wind_del", wkill, {.id = SWM_ARG_ID_DELETEWINDOW} },
+ { "screenshot_all", legacyfunc, {0} },
+ { "screenshot_wind", legacyfunc, {0} },
+ { "float_toggle", floating_toggle,{0} },
+ { "version", version, {0} },
+ { "spawn_lock", legacyfunc, {0} },
+ { "spawn_initscr", legacyfunc, {0} },
+ { "spawn_custom", dummykeyfunc, {0} },
+ { "iconify", iconify, {0} },
+ { "uniconify", uniconify, {0} },
+ { "raise_toggle", raise_toggle, {0} },
+ { "button2", pressbutton, {2} },
+ { "width_shrink", resize_step, {.id = SWM_ARG_ID_WIDTHSHRINK} },
+ { "width_grow", resize_step, {.id = SWM_ARG_ID_WIDTHGROW} },
+ { "height_shrink", resize_step, {.id = SWM_ARG_ID_HEIGHTSHRINK} },
+ { "height_grow", resize_step, {.id = SWM_ARG_ID_HEIGHTGROW} },
+ { "move_left", move_step, {.id = SWM_ARG_ID_MOVELEFT} },
+ { "move_right", move_step, {.id = SWM_ARG_ID_MOVERIGHT} },
+ { "move_up", move_step, {.id = SWM_ARG_ID_MOVEUP} },
+ { "move_down", move_step, {.id = SWM_ARG_ID_MOVEDOWN} },
+ { "name_workspace", name_workspace, {0} },
+ { "search_workspace", search_workspace, {0} },
+ { "search_win", search_win, {0} },
+ { "dumpwins", dumpwins, {0} }, /* MUST BE LAST */
+ { "invalid key func", NULL, {0} },
+};
+struct key {
+ RB_ENTRY(key) entry;
+ unsigned int mod;
+ KeySym keysym;
+ enum keyfuncid funcid;
+ char *spawn_name;
+};
+RB_HEAD(key_list, key);
+
+int
+key_cmp(struct key *kp1, struct key *kp2)
+{
+ if (kp1->keysym < kp2->keysym)
+ return (-1);
+ if (kp1->keysym > kp2->keysym)
+ return (1);
+
+ if (kp1->mod < kp2->mod)
+ return (-1);
+ if (kp1->mod > kp2->mod)
+ return (1);
+
+ return (0);
+}
+
+RB_GENERATE_STATIC(key_list, key, entry, key_cmp);
+struct key_list keys;
+
+/* mouse */
+enum { client_click, root_click };
+struct button {
+ unsigned int action;
+ unsigned int mask;
+ unsigned int button;
+ void (*func)(struct ws_win *, union arg *);
+ union arg args;
+} buttons[] = {
+ /* action key mouse button func args */
+ { client_click, MODKEY, Button3, resize, {.id = SWM_ARG_ID_DONTCENTER} },
+ { client_click, MODKEY | ShiftMask, Button3, resize, {.id = SWM_ARG_ID_CENTER} },
+ { client_click, MODKEY, Button1, move, {0} },
+};
+
+void
+update_modkey(unsigned int mod)
+{
+ int i;
+ struct key *kp;
+
+ mod_key = mod;
+ RB_FOREACH(kp, key_list, &keys)
+ if (kp->mod & ShiftMask)
+ kp->mod = mod | ShiftMask;
+ else
+ kp->mod = mod;
+
+ for (i = 0; i < LENGTH(buttons); i++)
+ if (buttons[i].mask & ShiftMask)
+ buttons[i].mask = mod | ShiftMask;
+ else
+ buttons[i].mask = mod;
+}
+
+/* spawn */
+struct spawn_prog {
+ TAILQ_ENTRY(spawn_prog) entry;
+ char *name;
+ int argc;
+ char **argv;
+};
+TAILQ_HEAD(spawn_list, spawn_prog);
+struct spawn_list spawns = TAILQ_HEAD_INITIALIZER(spawns);
+
+int
+spawn_expand(struct swm_region *r, union arg *args, char *spawn_name,
+ char ***ret_args)
+{
+ struct spawn_prog *prog = NULL;
+ int i;
+ char *ap, **real_args;
+
+ DNPRINTF(SWM_D_SPAWN, "spawn_expand: %s\n", spawn_name);
+
+ /* find program */
+ TAILQ_FOREACH(prog, &spawns, entry) {
+ if (!strcasecmp(spawn_name, prog->name))
+ break;
+ }
+ if (prog == NULL) {
+ warnx("spawn_custom: program %s not found", spawn_name);
+ return (-1);
+ }
+
+ /* make room for expanded args */
+ if ((real_args = calloc(prog->argc + 1, sizeof(char *))) == NULL)
+ err(1, "spawn_custom: calloc real_args");
+
+ /* expand spawn_args into real_args */
+ for (i = 0; i < prog->argc; i++) {
+ ap = prog->argv[i];
+ DNPRINTF(SWM_D_SPAWN, "spawn_custom: raw arg: %s\n", ap);
+ if (!strcasecmp(ap, "$bar_border")) {
+ if ((real_args[i] =
+ strdup(r->s->c[SWM_S_COLOR_BAR_BORDER].name))
+ == NULL)
+ err(1, "spawn_custom border color");
+ } else if (!strcasecmp(ap, "$bar_color")) {
+ if ((real_args[i] =
+ strdup(r->s->c[SWM_S_COLOR_BAR].name))
+ == NULL)
+ err(1, "spawn_custom bar color");
+ } else if (!strcasecmp(ap, "$bar_font")) {
+ if ((real_args[i] = strdup(bar_fonts))
+ == NULL)
+ err(1, "spawn_custom bar fonts");
+ } else if (!strcasecmp(ap, "$bar_font_color")) {
+ if ((real_args[i] =
+ strdup(r->s->c[SWM_S_COLOR_BAR_FONT].name))
+ == NULL)
+ err(1, "spawn_custom color font");
+ } else if (!strcasecmp(ap, "$color_focus")) {
+ if ((real_args[i] =
+ strdup(r->s->c[SWM_S_COLOR_FOCUS].name))
+ == NULL)
+ err(1, "spawn_custom color focus");
+ } else if (!strcasecmp(ap, "$color_unfocus")) {
+ if ((real_args[i] =
+ strdup(r->s->c[SWM_S_COLOR_UNFOCUS].name))
+ == NULL)
+ err(1, "spawn_custom color unfocus");
+ } else {
+ /* no match --> copy as is */
+ if ((real_args[i] = strdup(ap)) == NULL)
+ err(1, "spawn_custom strdup(ap)");
+ }
+ DNPRINTF(SWM_D_SPAWN, "spawn_custom: cooked arg: %s\n",
+ real_args[i]);
+ }
+
+#ifdef SWM_DEBUG
+ DNPRINTF(SWM_D_SPAWN, "spawn_custom: result: ");
+ for (i = 0; i < prog->argc; i++)
+ DNPRINTF(SWM_D_SPAWN, "\"%s\" ", real_args[i]);
+ DNPRINTF(SWM_D_SPAWN, "\n");
+#endif
+ *ret_args = real_args;
+ return (prog->argc);
+}
+
+void
+spawn_custom(struct swm_region *r, union arg *args, char *spawn_name)
+{
+ union arg a;
+ char **real_args;
+ int spawn_argc, i;
+
+ if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
+ return;
+ a.argv = real_args;
+ if (fork() == 0)
+ spawn(r->ws->idx, &a, 1);
+
+ for (i = 0; i < spawn_argc; i++)
+ free(real_args[i]);
+ free(real_args);
+}
+
+void
+spawn_select(struct swm_region *r, union arg *args, char *spawn_name, int *pid)
+{
+ union arg a;
+ char **real_args;
+ int i, spawn_argc;
+
+ if ((spawn_argc = spawn_expand(r, args, spawn_name, &real_args)) < 0)
+ return;
+ a.argv = real_args;
+
+ if (pipe(select_list_pipe) == -1)
+ err(1, "pipe error");
+ if (pipe(select_resp_pipe) == -1)
+ err(1, "pipe error");
+
+ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
+ err(1, "could not disable SIGPIPE");
+ switch (*pid = fork()) {
+ case -1:
+ err(1, "cannot fork");
+ break;
+ case 0: /* child */
+ if (dup2(select_list_pipe[0], 0) == -1)
+ err(1, "dup2");
+ if (dup2(select_resp_pipe[1], 1) == -1)
+ err(1, "dup2");
+ close(select_list_pipe[1]);
+ close(select_resp_pipe[0]);
+ spawn(r->ws->idx, &a, 0);
+ break;
+ default: /* parent */
+ close(select_list_pipe[0]);
+ close(select_resp_pipe[1]);
+ break;
+ }
+
+ for (i = 0; i < spawn_argc; i++)
+ free(real_args[i]);
+ free(real_args);
+}
+
+void
+spawn_insert(char *name, char *args)
+{
+ char *arg, *cp, *ptr;
+ struct spawn_prog *sp;
+
+ DNPRINTF(SWM_D_SPAWN, "spawn_insert: %s\n", name);
+
+ if ((sp = calloc(1, sizeof *sp)) == NULL)
+ err(1, "spawn_insert: malloc");
+ if ((sp->name = strdup(name)) == NULL)
+ err(1, "spawn_insert: strdup");
+
+ /* convert the arguments to an argument list */
+ if ((ptr = cp = strdup(args)) == NULL)
+ err(1, "spawn_insert: strdup");
+ while ((arg = strsep(&ptr, " \t")) != NULL) {
+ /* empty field; skip it */
+ if (*arg == '\0')
+ continue;
+
+ sp->argc++;
+ if ((sp->argv = realloc(sp->argv, sp->argc *
+ sizeof *sp->argv)) == NULL)
+ err(1, "spawn_insert: realloc");
+ if ((sp->argv[sp->argc - 1] = strdup(arg)) == NULL)
+ err(1, "spawn_insert: strdup");
+ }
+ free(cp);
+
+ TAILQ_INSERT_TAIL(&spawns, sp, entry);
+ DNPRINTF(SWM_D_SPAWN, "spawn_insert: leave\n");
+}
+
+void
+spawn_remove(struct spawn_prog *sp)
+{
+ int i;
+
+ DNPRINTF(SWM_D_SPAWN, "spawn_remove: %s\n", sp->name);
+
+ TAILQ_REMOVE(&spawns, sp, entry);
+ for (i = 0; i < sp->argc; i++)
+ free(sp->argv[i]);
+ free(sp->argv);
+ free(sp->name);
+ free(sp);
+
+ DNPRINTF(SWM_D_SPAWN, "spawn_remove: leave\n");
+}
+
+void
+spawn_replace(struct spawn_prog *sp, char *name, char *args)
+{
+ DNPRINTF(SWM_D_SPAWN, "spawn_replace: %s [%s]\n", sp->name, name);
+
+ spawn_remove(sp);
+ spawn_insert(name, args);
+
+ DNPRINTF(SWM_D_SPAWN, "spawn_replace: leave\n");
+}
+
+void
+setspawn(char *name, char *args)
+{
+ struct spawn_prog *sp;
+
+ DNPRINTF(SWM_D_SPAWN, "setspawn: %s\n", name);
+
+ if (name == NULL)
+ return;
+
+ TAILQ_FOREACH(sp, &spawns, entry) {
+ if (!strcmp(sp->name, name)) {
+ if (*args == '\0')
+ spawn_remove(sp);
+ else
+ spawn_replace(sp, name, args);
+ DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
+ return;
+ }
+ }
+ if (*args == '\0') {
+ warnx("error: setspawn: cannot find program: %s", name);
+ return;
+ }
+
+ spawn_insert(name, args);
+ DNPRINTF(SWM_D_SPAWN, "setspawn: leave\n");
+}
+
+int
+setconfspawn(char *selector, char *value, int flags)
+{
+ DNPRINTF(SWM_D_SPAWN, "setconfspawn: [%s] [%s]\n", selector, value);
+
+ setspawn(selector, value);
+
+ DNPRINTF(SWM_D_SPAWN, "setconfspawn: done\n");
+ return (0);
+}
+
+void
+setup_spawn(void)
+{
+ setconfspawn("term", "xterm", 0);
+ setconfspawn("screenshot_all", "screenshot.sh full", 0);
+ setconfspawn("screenshot_wind", "screenshot.sh window", 0);
+ setconfspawn("lock", "xlock", 0);
+ setconfspawn("initscr", "initscreen.sh", 0);
+ setconfspawn("menu", "dmenu_run"
+ " -fn $bar_font"
+ " -nb $bar_color"
+ " -nf $bar_font_color"
+ " -sb $bar_border"
+ " -sf $bar_color", 0);
+ setconfspawn("search", "dmenu"
+ " -i"
+ " -fn $bar_font"
+ " -nb $bar_color"
+ " -nf $bar_font_color"
+ " -sb $bar_border"
+ " -sf $bar_color", 0);
+ setconfspawn("name_workspace", "dmenu"
+ " -p Workspace"
+ " -fn $bar_font"
+ " -nb $bar_color"
+ " -nf $bar_font_color"
+ " -sb $bar_border"
+ " -sf $bar_color", 0);
+}
+
+/* key bindings */
+#define SWM_MODNAME_SIZE 32
+#define SWM_KEY_WS "\n+ \t"
+int
+parsekeys(char *keystr, unsigned int currmod, unsigned int *mod, KeySym *ks)
+{
+ char *cp, *name;
+ KeySym uks;
+ DNPRINTF(SWM_D_KEY, "parsekeys: enter [%s]\n", keystr);
+ if (mod == NULL || ks == NULL) {
+ DNPRINTF(SWM_D_KEY, "parsekeys: no mod or key vars\n");
+ return (1);
+ }
+ if (keystr == NULL || strlen(keystr) == 0) {
+ DNPRINTF(SWM_D_KEY, "parsekeys: no keystr\n");
+ return (1);
+ }
+ cp = keystr;
+ *ks = NoSymbol;
+ *mod = 0;
+ while ((name = strsep(&cp, SWM_KEY_WS)) != NULL) {
+ DNPRINTF(SWM_D_KEY, "parsekeys: key [%s]\n", name);
+ if (cp)
+ cp += (long)strspn(cp, SWM_KEY_WS);
+ if (strncasecmp(name, "MOD", SWM_MODNAME_SIZE) == 0)
+ *mod |= currmod;
+ else if (!strncasecmp(name, "Mod1", SWM_MODNAME_SIZE))
+ *mod |= Mod1Mask;
+ else if (!strncasecmp(name, "Mod2", SWM_MODNAME_SIZE))
+ *mod += Mod2Mask;
+ else if (!strncmp(name, "Mod3", SWM_MODNAME_SIZE))
+ *mod |= Mod3Mask;
+ else if (!strncmp(name, "Mod4", SWM_MODNAME_SIZE))
+ *mod |= Mod4Mask;
+ else if (strncasecmp(name, "SHIFT", SWM_MODNAME_SIZE) == 0)
+ *mod |= ShiftMask;
+ else if (strncasecmp(name, "CONTROL", SWM_MODNAME_SIZE) == 0)
+ *mod |= ControlMask;
+ else {
+ *ks = XStringToKeysym(name);
+ XConvertCase(*ks, ks, &uks);
+ if (ks == NoSymbol) {
+ DNPRINTF(SWM_D_KEY,
+ "parsekeys: invalid key %s\n",
+ name);
+ return (1);
+ }
+ }
+ }
+ DNPRINTF(SWM_D_KEY, "parsekeys: leave ok\n");
+ return (0);
+}
+
+char *
+strdupsafe(char *str)
+{
+ if (str == NULL)
+ return (NULL);
+ else
+ return (strdup(str));
+}
+
+void
+key_insert(unsigned int mod, KeySym ks, enum keyfuncid kfid, char *spawn_name)
+{
+ struct key *kp;
+
+ DNPRINTF(SWM_D_KEY, "key_insert: enter %s [%s]\n",
+ keyfuncs[kfid].name, spawn_name);
+
+ if ((kp = malloc(sizeof *kp)) == NULL)
+ err(1, "key_insert: malloc");
+
+ kp->mod = mod;
+ kp->keysym = ks;
+ kp->funcid = kfid;
+ kp->spawn_name = strdupsafe(spawn_name);
+ RB_INSERT(key_list, &keys, kp);
+
+ DNPRINTF(SWM_D_KEY, "key_insert: leave\n");
+}
+
+struct key *
+key_lookup(unsigned int mod, KeySym ks)
+{
+ struct key kp;
+
+ kp.keysym = ks;
+ kp.mod = mod;
+
+ return (RB_FIND(key_list, &keys, &kp));
+}
+
+void
+key_remove(struct key *kp)
+{
+ DNPRINTF(SWM_D_KEY, "key_remove: %s\n", keyfuncs[kp->funcid].name);
+
+ RB_REMOVE(key_list, &keys, kp);
+ free(kp->spawn_name);
+ free(kp);
+
+ DNPRINTF(SWM_D_KEY, "key_remove: leave\n");
+}
+
+void
+key_replace(struct key *kp, unsigned int mod, KeySym ks, enum keyfuncid kfid,
+ char *spawn_name)
+{
+ DNPRINTF(SWM_D_KEY, "key_replace: %s [%s]\n", keyfuncs[kp->funcid].name,
+ spawn_name);
+
+ key_remove(kp);
+ key_insert(mod, ks, kfid, spawn_name);
+
+ DNPRINTF(SWM_D_KEY, "key_replace: leave\n");
+}
+
+void
+setkeybinding(unsigned int mod, KeySym ks, enum keyfuncid kfid,
+ char *spawn_name)
+{
+ struct key *kp;
+
+ DNPRINTF(SWM_D_KEY, "setkeybinding: enter %s [%s]\n",
+ keyfuncs[kfid].name, spawn_name);
+
+ if ((kp = key_lookup(mod, ks)) != NULL) {
+ if (kfid == kf_invalid)
+ key_remove(kp);
+ else
+ key_replace(kp, mod, ks, kfid, spawn_name);
+ DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
+ return;
+ }
+ if (kfid == kf_invalid) {
+ warnx("error: setkeybinding: cannot find mod/key combination");
+ DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
+ return;
+ }
+
+ key_insert(mod, ks, kfid, spawn_name);
+ DNPRINTF(SWM_D_KEY, "setkeybinding: leave\n");
+}
+
+int
+setconfbinding(char *selector, char *value, int flags)
+{
+ enum keyfuncid kfid;
+ unsigned int mod;
+ KeySym ks;
+ struct spawn_prog *sp;
+ DNPRINTF(SWM_D_KEY, "setconfbinding: enter\n");
+ if (selector == NULL) {
+ DNPRINTF(SWM_D_KEY, "setconfbinding: unbind %s\n", value);
+ if (parsekeys(value, mod_key, &mod, &ks) == 0) {
+ kfid = kf_invalid;
+ setkeybinding(mod, ks, kfid, NULL);
+ return (0);
+ } else
+ return (1);
+ }
+ /* search by key function name */
+ for (kfid = 0; kfid < kf_invalid; (kfid)++) {
+ if (strncasecmp(selector, keyfuncs[kfid].name,
+ SWM_FUNCNAME_LEN) == 0) {
+ DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
+ selector);
+ if (parsekeys(value, mod_key, &mod, &ks) == 0) {
+ setkeybinding(mod, ks, kfid, NULL);
+ return (0);
+ } else
+ return (1);
+ }
+ }
+ /* search by custom spawn name */
+ TAILQ_FOREACH(sp, &spawns, entry) {
+ if (strcasecmp(selector, sp->name) == 0) {
+ DNPRINTF(SWM_D_KEY, "setconfbinding: %s: match\n",
+ selector);
+ if (parsekeys(value, mod_key, &mod, &ks) == 0) {
+ setkeybinding(mod, ks, kf_spawn_custom,
+ sp->name);
+ return (0);
+ } else
+ return (1);
+ }
+ }
+ DNPRINTF(SWM_D_KEY, "setconfbinding: no match\n");
+ return (1);
+}
+
+void
+setup_keys(void)
+{
+ setkeybinding(MODKEY, XK_space, kf_cycle_layout,NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_backslash, kf_flip_layout, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_space, kf_stack_reset, NULL);
+ setkeybinding(MODKEY, XK_h, kf_master_shrink,NULL);
+ setkeybinding(MODKEY, XK_l, kf_master_grow, NULL);
+ setkeybinding(MODKEY, XK_comma, kf_master_add, NULL);
+ setkeybinding(MODKEY, XK_period, kf_master_del, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_comma, kf_stack_inc, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_period, kf_stack_dec, NULL);
+ setkeybinding(MODKEY, XK_Return, kf_swap_main, NULL);
+ setkeybinding(MODKEY, XK_j, kf_focus_next, NULL);
+ setkeybinding(MODKEY, XK_k, kf_focus_prev, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_j, kf_swap_next, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_k, kf_swap_prev, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_Return, kf_spawn_term, NULL);
+ setkeybinding(MODKEY, XK_p, kf_spawn_custom,"menu");
+ setkeybinding(MODKEY|ShiftMask, XK_q, kf_quit, NULL);
+ setkeybinding(MODKEY, XK_q, kf_restart, NULL);
+ setkeybinding(MODKEY, XK_m, kf_focus_main, NULL);
+ setkeybinding(MODKEY, XK_1, kf_ws_1, NULL);
+ setkeybinding(MODKEY, XK_2, kf_ws_2, NULL);
+ setkeybinding(MODKEY, XK_3, kf_ws_3, NULL);
+ setkeybinding(MODKEY, XK_4, kf_ws_4, NULL);
+ setkeybinding(MODKEY, XK_5, kf_ws_5, NULL);
+ setkeybinding(MODKEY, XK_6, kf_ws_6, NULL);
+ setkeybinding(MODKEY, XK_7, kf_ws_7, NULL);
+ setkeybinding(MODKEY, XK_8, kf_ws_8, NULL);
+ setkeybinding(MODKEY, XK_9, kf_ws_9, NULL);
+ setkeybinding(MODKEY, XK_0, kf_ws_10, NULL);
+ setkeybinding(MODKEY, XK_Right, kf_ws_next, NULL);
+ setkeybinding(MODKEY, XK_Left, kf_ws_prev, NULL);
+ setkeybinding(MODKEY, XK_Up, kf_ws_next_all, NULL);
+ setkeybinding(MODKEY, XK_Down, kf_ws_prev_all, NULL);
+ setkeybinding(MODKEY, XK_a, kf_ws_prior, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_Right, kf_screen_next, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_Left, kf_screen_prev, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_1, kf_mvws_1, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_2, kf_mvws_2, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_3, kf_mvws_3, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_4, kf_mvws_4, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_5, kf_mvws_5, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_6, kf_mvws_6, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_7, kf_mvws_7, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_8, kf_mvws_8, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_9, kf_mvws_9, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_0, kf_mvws_10, NULL);
+ setkeybinding(MODKEY, XK_b, kf_bar_toggle, NULL);
+ setkeybinding(MODKEY, XK_Tab, kf_focus_next, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_Tab, kf_focus_prev, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_x, kf_wind_kill, NULL);
+ setkeybinding(MODKEY, XK_x, kf_wind_del, NULL);
+ setkeybinding(MODKEY, XK_s, kf_spawn_custom,"screenshot_all");
+ setkeybinding(MODKEY|ShiftMask, XK_s, kf_spawn_custom,"screenshot_wind");
+ setkeybinding(MODKEY, XK_t, kf_float_toggle,NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_v, kf_version, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_Delete, kf_spawn_custom,"lock");
+ setkeybinding(MODKEY|ShiftMask, XK_i, kf_spawn_custom,"initscr");
+ setkeybinding(MODKEY, XK_w, kf_iconify, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_w, kf_uniconify, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_r, kf_raise_toggle,NULL);
+ setkeybinding(MODKEY, XK_v, kf_button2, NULL);
+ setkeybinding(MODKEY, XK_equal, kf_width_grow, NULL);
+ setkeybinding(MODKEY, XK_minus, kf_width_shrink,NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_equal, kf_height_grow, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_minus, kf_height_shrink,NULL);
+ setkeybinding(MODKEY, XK_bracketleft, kf_move_left, NULL);
+ setkeybinding(MODKEY, XK_bracketright,kf_move_right, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_bracketleft, kf_move_up, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_bracketright,kf_move_down, NULL);
+ setkeybinding(MODKEY|ShiftMask, XK_slash, kf_name_workspace,NULL);
+ setkeybinding(MODKEY, XK_slash, kf_search_workspace,NULL);
+ setkeybinding(MODKEY, XK_f, kf_search_win, NULL);
+#ifdef SWM_DEBUG
+ setkeybinding(MODKEY|ShiftMask, XK_d, kf_dumpwins, NULL);
+#endif
+}
+
+void
+clear_keys(void)
+{
+ struct key *kp;
+
+ while (RB_EMPTY(&keys) == 0) {
+ kp = RB_ROOT(&keys);
+ key_remove(kp);
+ }
+}
+
+int
+setkeymapping(char *selector, char *value, int flags)
+{
+ char keymapping_file[PATH_MAX];
+ DNPRINTF(SWM_D_KEY, "setkeymapping: enter\n");
+ if (value[0] == '~')
+ snprintf(keymapping_file, sizeof keymapping_file, "%s/%s",
+ pwd->pw_dir, &value[1]);
+ else
+ strlcpy(keymapping_file, value, sizeof keymapping_file);
+ clear_keys();
+ /* load new key bindings; if it fails, revert to default bindings */
+ if (conf_load(keymapping_file, SWM_CONF_KEYMAPPING)) {
+ clear_keys();
+ setup_keys();
+ }
+ DNPRINTF(SWM_D_KEY, "setkeymapping: leave\n");
+ return (0);
+}
+
+void
+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++)
+ for (j = 0; j < modmap->max_keypermod; j++)