From 07c8cc46d312de8a7a65086707b693ccee25e45d Mon Sep 17 00:00:00 2001 From: Sravan Balaji Date: Sun, 6 Oct 2024 15:12:55 -0400 Subject: [PATCH] Add menu patch --- README.org | 32 +++++ config.def.h | 22 +++- config.h | 12 ++ dwl.c | 164 +++++++++++++++++++++++++ patches.def.h | 2 + patches/menu-20240713.patch | 230 ++++++++++++++++++++++++++++++++++++ 6 files changed, 457 insertions(+), 5 deletions(-) create mode 100644 patches/menu-20240713.patch diff --git a/README.org b/README.org index a1c507d..fea3011 100644 --- a/README.org +++ b/README.org @@ -15,6 +15,7 @@ - [[#foreign-toplevel-management][Foreign Toplevel Management]] - [[#gapless-grid][Gapless Grid]] - [[#ipc][IPC]] + - [[#menu][Menu]] - [[#move-stack][Move Stack]] - [[#natural-scroll-trackpad][Natural Scroll Trackpad]] - [[#numlock-capslock][Numlock Capslock]] @@ -28,6 +29,7 @@ - [[#appearance][Appearance]] - [[#tagging][Tagging]] - [[#logging][Logging]] + - [[#menu-1][Menu]] - [[#environment-variables][Environment Variables]] - [[#autostart][Autostart]] - [[#window-rules][Window Rules]] @@ -162,6 +164,20 @@ Note to [[https://codeberg.org/dwl/dwl-patches/src/branch/main/patches/pertag][p #define IPC_PATCH 1 #+END_SRC +*** [[https://codeberg.org/dwl/dwl-patches/src/branch/main/patches/menu][Menu]] + +This patch adds ~menu~ command, which allows dwl to interface with dmenu-like programs. + +By default, two menus are available: +- focusing a window by its title by pressing ~Alt+o~ +- selecting a layout from a list by pressing ~Alt+Shift+o~ + +Edit ~menus~ array in ~config.h~ to add/change menus and use a different dmenu program. + +#+BEGIN_SRC c :tangle patches.def.h +#define MENU_PATCH 1 +#+END_SRC + *** [[https://codeberg.org/dwl/dwl-patches/src/branch/main/patches/movestack][Move Stack]] Allows you to move a window up and down the stack. @@ -289,6 +305,18 @@ static const float fullscreen_bg[] = {0.1f, 0.1f, 0.1f, 1.0f}; /* You ca static int log_level = WLR_ERROR; #+END_SRC +*** Menu + +#+BEGIN_SRC c :tangle config.h +#if MENU_PATCH +static const Menu menus[] = { + /* command feed function action function */ + { "rofi -dmenu -i", menuwinfeed, menuwinaction }, + { "rofi -dmenu -i", menulayoutfeed, menulayoutaction }, +}; +#endif // MENU_PATCH +#+END_SRC + *** Environment Variables #+BEGIN_SRC c :tangle config.h #if SETUPENV_PATCH @@ -493,6 +521,10 @@ static const Key keys[] = { { MODKEY, XKB_KEY_g, setlayout, {.v = &layouts[3]} }, #endif // GAPLESSGRID_PATCH { MODKEY, XKB_KEY_space, setlayout, {0} }, +#if MENU_PATCH + { MODKEY|WLR_MODIFIER_SHIFT|WLR_MODIFIER_CTRL, XKB_KEY_p, menu, {.v = &menus[0]} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_P, menu, {.v = &menus[1]} }, +#endif // MENU_PATCH { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_space, togglefloating, {0} }, { MODKEY, XKB_KEY_e, togglefullscreen, {0} }, #if FAKE_FULLSCREEN_CLIENT_PATCH diff --git a/config.def.h b/config.def.h index 364489a..5168f2b 100644 --- a/config.def.h +++ b/config.def.h @@ -1,8 +1,8 @@ /* Taken from https://github.com/djpohly/dwl/issues/466 */ #define COLOR(hex) { ((hex >> 24) & 0xFF) / 255.0f, \ - ((hex >> 16) & 0xFF) / 255.0f, \ - ((hex >> 8) & 0xFF) / 255.0f, \ - (hex & 0xFF) / 255.0f } + ((hex >> 16) & 0xFF) / 255.0f, \ + ((hex >> 8) & 0xFF) / 255.0f, \ + (hex & 0xFF) / 255.0f } /* appearance */ static const int sloppyfocus = 1; /* focus follows mouse */ static const int bypass_surface_visibility = 0; /* 1 means idle inhibitors will disable idle tracking even if it's surface isn't visible */ @@ -33,6 +33,14 @@ static const float fullscreen_bg[] = {0.1f, 0.1f, 0.1f, 1.0f}; /* You ca /* logging */ static int log_level = WLR_ERROR; +#if MENU_PATCH +static const Menu menus[] = { + /* command feed function action function */ + { "rofi -dmenu -i", menuwinfeed, menuwinaction }, + { "rofi -dmenu -i", menulayoutfeed, menulayoutaction }, +}; +#endif // MENU_PATCH + #if SETUPENV_PATCH static const Env envs[] = { /* variable value */ @@ -43,8 +51,8 @@ static const Env envs[] = { /* Autostart */ #if AUTOSTART_PATCH static const char *const autostart[] = { - "wbg", "/path/to/your/image", NULL, - NULL /* terminate */ + "wbg", "/path/to/your/image", NULL, + NULL /* terminate */ }; #endif // AUTOSTART_PATCH @@ -208,6 +216,10 @@ static const Key keys[] = { { MODKEY, XKB_KEY_g, setlayout, {.v = &layouts[3]} }, #endif // GAPLESSGRID_PATCH { MODKEY, XKB_KEY_space, setlayout, {0} }, +#if MENU_PATCH + { MODKEY|WLR_MODIFIER_SHIFT|WLR_MODIFIER_CTRL, XKB_KEY_p, menu, {.v = &menus[0]} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_P, menu, {.v = &menus[1]} }, +#endif // MENU_PATCH { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_space, togglefloating, {0} }, { MODKEY, XKB_KEY_e, togglefullscreen, {0} }, #if FAKE_FULLSCREEN_CLIENT_PATCH diff --git a/config.h b/config.h index 964eb65..fbb479a 100644 --- a/config.h +++ b/config.h @@ -31,6 +31,14 @@ static const float fullscreen_bg[] = {0.1f, 0.1f, 0.1f, 1.0f}; /* You ca static int log_level = WLR_ERROR; +#if MENU_PATCH +static const Menu menus[] = { + /* command feed function action function */ + { "rofi -dmenu -i", menuwinfeed, menuwinaction }, + { "rofi -dmenu -i", menulayoutfeed, menulayoutaction }, +}; +#endif // MENU_PATCH + #if SETUPENV_PATCH static const Env envs[] = { /* variable value */ @@ -201,6 +209,10 @@ static const Key keys[] = { { MODKEY, XKB_KEY_g, setlayout, {.v = &layouts[3]} }, #endif // GAPLESSGRID_PATCH { MODKEY, XKB_KEY_space, setlayout, {0} }, +#if MENU_PATCH + { MODKEY|WLR_MODIFIER_SHIFT|WLR_MODIFIER_CTRL, XKB_KEY_p, menu, {.v = &menus[0]} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_P, menu, {.v = &menus[1]} }, +#endif // MENU_PATCH { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_space, togglefloating, {0} }, { MODKEY, XKB_KEY_e, togglefullscreen, {0} }, #if FAKE_FULLSCREEN_CLIENT_PATCH diff --git a/dwl.c b/dwl.c index ddcd94c..a70bf80 100644 --- a/dwl.c +++ b/dwl.c @@ -296,6 +296,14 @@ typedef struct { struct wl_listener destroy; } SessionLock; +#if MENU_PATCH +typedef struct { + const char *cmd; /* command to run a menu */ + void (*feed)(FILE *f); /* feed input to menu */ + void (*action)(char *line); /* do action based on menu output */ +} Menu; +#endif // MENU_PATCH + /* function declarations */ static void applybounds(Client *c, struct wlr_box *bbox); static void applyrules(Client *c); @@ -391,6 +399,14 @@ static void killclient(const Arg *arg); static void locksession(struct wl_listener *listener, void *data); static void mapnotify(struct wl_listener *listener, void *data); static void maximizenotify(struct wl_listener *listener, void *data); +#if MENU_PATCH +static void menu(const Arg *arg); +static int menuloop(void *data); +static void menuwinfeed(FILE *f); +static void menuwinaction(char *line); +static void menulayoutfeed(FILE *f); +static void menulayoutaction(char *line); +#endif // MENU_PATCH static void monocle(Monitor *m); #if MOVESTACK_PATCH static void movestack(const Arg *arg); @@ -547,6 +563,13 @@ static struct wlr_box sgeom; static struct wl_list mons; static Monitor *selmon; +#if MENU_PATCH +static struct wl_event_source *menu_source; +static pid_t menu_pid; +static int menu_fd; +static const Menu *menu_current; +#endif // MENU_PATCH + #if IPC_PATCH static struct zdwl_ipc_manager_v2_interface dwl_manager_implementation = {.release = dwl_ipc_manager_release, .get_output = dwl_ipc_manager_get_output}; static struct zdwl_ipc_output_v2_interface dwl_output_implementation = {.release = dwl_ipc_output_release, .set_tags = dwl_ipc_output_set_tags, .set_layout = dwl_ipc_output_set_layout, .set_client_tags = dwl_ipc_output_set_client_tags}; @@ -891,6 +914,9 @@ cleanup(void) wlr_xwayland_destroy(xwayland); xwayland = NULL; #endif // XWAYLAND +#if MENU_PATCH + wl_event_source_remove(menu_source); +#endif // MENU_PATCH wl_display_destroy_clients(dpy); #if AUTOSTART_PATCH @@ -2558,6 +2584,138 @@ maximizenotify(struct wl_listener *listener, void *data) wlr_xdg_surface_schedule_configure(c->surface.xdg); } +#if MENU_PATCH +void +menu(const Arg *arg) +{ + FILE *f; + pid_t pid; + int fd_right[2], fd_left[2]; + + if (!selmon || menu_pid != 0) + return; + + menu_current = arg->v; + + if (pipe(fd_right) == -1 || pipe(fd_left) == -1) + return; + if ((pid = fork()) == -1) + return; + if (pid == 0) { + close(fd_right[1]); + close(fd_left[0]); + dup2(fd_right[0], STDIN_FILENO); + close(fd_right[0]); + dup2(fd_left[1], STDOUT_FILENO); + close(fd_left[1]); + + execl("/bin/sh", "/bin/sh", "-c", menu_current->cmd, NULL); + die("dwl: execl %s failed:", "/bin/sh"); + } + + close(fd_right[0]); + close(fd_left[1]); + + if (!(f = fdopen(fd_right[1], "w"))) + return; + menu_current->feed(f); + fclose(f); + + menu_pid = pid; + menu_fd = fd_left[0]; + wl_event_source_timer_update(menu_source, 10); +} + +int +menuloop(void *data) +{ + FILE *f; + pid_t pid; + char line[256], *s; + + /* If process is still running, wait for another 50 ms */ + if ((pid = waitpid(menu_pid, NULL, WNOHANG)) == 0) { + wl_event_source_timer_update(menu_source, 10); + return 0; + } + + menu_pid = 0; + + if (!(f = fdopen(menu_fd, "r"))) + return 0; + if (fgets(line, sizeof(line), f)) { + if ((s = strchr(line, '\n'))) + *s = '\0'; + menu_current->action(line); + } + fclose(f); + return 0; +} + +void +menuwinfeed(FILE *f) +{ + Client *c; + const char *title; + + wl_list_for_each(c, &fstack, flink) { + if (!(title = client_get_title(c))) + continue; + fprintf(f, "%s\n", title); + } +} + +void +menuwinaction(char *line) +{ + Client *c; + Monitor *prevm = selmon; + const char *title; + + if (!selmon) + return; + + wl_list_for_each(c, &fstack, flink) { + if (!(title = client_get_title(c))) + continue; + if (strcmp(line, title) == 0) + goto found; + } + return; + +found: + focusclient(c, 1); + wlr_cursor_move(cursor, NULL, selmon->m.x - prevm->m.x , 0); + selmon->seltags ^= 1; /* toggle sel tagset */ + selmon->tagset[selmon->seltags] = c->tags; + arrange(selmon); + printstatus(); +} + +void +menulayoutfeed(FILE *f) +{ + unsigned int i; + for (i = 0; i < LENGTH(layouts); i++) + fprintf(f, "%s\n", layouts[i].symbol); +} + +void +menulayoutaction(char *line) +{ + unsigned int i; + Arg a; + for (i = 0; i < LENGTH(layouts); i++) + if (strcmp(line, layouts[i].symbol) == 0) + goto found; + return; + +found: + a.v = &layouts[i]; + setlayout(&a); +} +#endif // MENU_PATCH + void monocle(Monitor *m) { @@ -3582,6 +3740,12 @@ setup(void) * e.g when running in the x11 backend or the wayland backend and the * compositor has Xwayland support */ unsetenv("DISPLAY"); + +#if MENU_PATCH + menu_source = wl_event_loop_add_timer( + wl_display_get_event_loop(dpy), menuloop, NULL); +#endif // MENU_PATCH + #ifdef XWAYLAND /* * Initialise the XWayland X server. diff --git a/patches.def.h b/patches.def.h index 245be78..1d84975 100644 --- a/patches.def.h +++ b/patches.def.h @@ -14,6 +14,8 @@ #define IPC_PATCH 1 +#define MENU_PATCH 1 + #define MOVESTACK_PATCH 1 #define NATURALSCROLLTRACKPAD_PATCH 1 diff --git a/patches/menu-20240713.patch b/patches/menu-20240713.patch new file mode 100644 index 0000000..ffdc584 --- /dev/null +++ b/patches/menu-20240713.patch @@ -0,0 +1,230 @@ +commit 2d7e19dc948aec61746fd858394a9c80d34a3216 +Author: Nikita Ivanov +Date: Sat Jul 13 01:05:20 2024 +0200 + + Add menu command + +diff --git a/config.def.h b/config.def.h +index 22d2171..ecd2c67 100644 +--- a/config.def.h ++++ b/config.def.h +@@ -20,6 +20,12 @@ static const float fullscreen_bg[] = {0.1f, 0.1f, 0.1f, 1.0f}; /* You ca + /* logging */ + static int log_level = WLR_ERROR; + ++static const Menu menus[] = { ++ /* command feed function action function */ ++ { "wmenu -i -l 5 -p Windows", menuwinfeed, menuwinaction }, ++ { "wmenu -i -p Layouts", menulayoutfeed, menulayoutaction }, ++}; ++ + /* NOTE: ALWAYS keep a rule declared even if you don't use rules (e.g leave at least one example) */ + static const Rule rules[] = { + /* app_id title tags mask isfloating monitor */ +@@ -140,6 +146,8 @@ static const Key keys[] = { + { MODKEY, XKB_KEY_f, setlayout, {.v = &layouts[1]} }, + { MODKEY, XKB_KEY_m, setlayout, {.v = &layouts[2]} }, + { MODKEY, XKB_KEY_space, setlayout, {0} }, ++ { MODKEY, XKB_KEY_o, menu, {.v = &menus[0]} }, ++ { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_O, menu, {.v = &menus[1]} }, + { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_space, togglefloating, {0} }, + { MODKEY, XKB_KEY_e, togglefullscreen, {0} }, + { MODKEY, XKB_KEY_0, view, {.ui = ~0} }, +diff --git a/dwl.c b/dwl.c +index dc0437e..90bb09a 100644 +--- a/dwl.c ++++ b/dwl.c +@@ -241,6 +241,12 @@ typedef struct { + struct wl_listener destroy; + } SessionLock; + ++typedef struct { ++ const char *cmd; /* command to run a menu */ ++ void (*feed)(FILE *f); /* feed input to menu */ ++ void (*action)(char *line); /* do action based on menu output */ ++} Menu; ++ + /* function declarations */ + static void applybounds(Client *c, struct wlr_box *bbox); + static void applyrules(Client *c); +@@ -298,6 +304,12 @@ static void killclient(const Arg *arg); + static void locksession(struct wl_listener *listener, void *data); + static void mapnotify(struct wl_listener *listener, void *data); + static void maximizenotify(struct wl_listener *listener, void *data); ++static void menu(const Arg *arg); ++static int menuloop(void *data); ++static void menuwinfeed(FILE *f); ++static void menuwinaction(char *line); ++static void menulayoutfeed(FILE *f); ++static void menulayoutaction(char *line); + static void monocle(Monitor *m); + static void motionabsolute(struct wl_listener *listener, void *data); + static void motionnotify(uint32_t time, struct wlr_input_device *device, double sx, +@@ -408,6 +420,11 @@ static struct wlr_box sgeom; + static struct wl_list mons; + static Monitor *selmon; + ++static struct wl_event_source *menu_source; ++static pid_t menu_pid; ++static int menu_fd; ++static const Menu *menu_current; ++ + #ifdef XWAYLAND + static void activatex11(struct wl_listener *listener, void *data); + static void associatex11(struct wl_listener *listener, void *data); +@@ -675,6 +692,7 @@ cleanup(void) + wlr_xwayland_destroy(xwayland); + xwayland = NULL; + #endif ++ wl_event_source_remove(menu_source); + wl_display_destroy_clients(dpy); + if (child_pid > 0) { + kill(-child_pid, SIGTERM); +@@ -1717,6 +1735,136 @@ maximizenotify(struct wl_listener *listener, void *data) + wlr_xdg_surface_schedule_configure(c->surface.xdg); + } + ++void ++menu(const Arg *arg) ++{ ++ FILE *f; ++ pid_t pid; ++ int fd_right[2], fd_left[2]; ++ ++ if (!selmon || menu_pid != 0) ++ return; ++ ++ menu_current = arg->v; ++ ++ if (pipe(fd_right) == -1 || pipe(fd_left) == -1) ++ return; ++ if ((pid = fork()) == -1) ++ return; ++ if (pid == 0) { ++ close(fd_right[1]); ++ close(fd_left[0]); ++ dup2(fd_right[0], STDIN_FILENO); ++ close(fd_right[0]); ++ dup2(fd_left[1], STDOUT_FILENO); ++ close(fd_left[1]); ++ ++ execl("/bin/sh", "/bin/sh", "-c", menu_current->cmd, NULL); ++ die("dwl: execl %s failed:", "/bin/sh"); ++ } ++ ++ close(fd_right[0]); ++ close(fd_left[1]); ++ ++ if (!(f = fdopen(fd_right[1], "w"))) ++ return; ++ menu_current->feed(f); ++ fclose(f); ++ ++ menu_pid = pid; ++ menu_fd = fd_left[0]; ++ wl_event_source_timer_update(menu_source, 10); ++} ++ ++int ++menuloop(void *data) ++{ ++ FILE *f; ++ pid_t pid; ++ char line[256], *s; ++ ++ /* If process is still running, wait for another 50 ms */ ++ if ((pid = waitpid(menu_pid, NULL, WNOHANG)) == 0) { ++ wl_event_source_timer_update(menu_source, 10); ++ return 0; ++ } ++ ++ menu_pid = 0; ++ ++ if (!(f = fdopen(menu_fd, "r"))) ++ return 0; ++ if (fgets(line, sizeof(line), f)) { ++ if ((s = strchr(line, '\n'))) ++ *s = '\0'; ++ menu_current->action(line); ++ } ++ fclose(f); ++ return 0; ++} ++ ++void ++menuwinfeed(FILE *f) ++{ ++ Client *c; ++ const char *title; ++ ++ wl_list_for_each(c, &fstack, flink) { ++ if (!(title = client_get_title(c))) ++ continue; ++ fprintf(f, "%s\n", title); ++ } ++} ++ ++void ++menuwinaction(char *line) ++{ ++ Client *c; ++ Monitor *prevm = selmon; ++ const char *title; ++ ++ if (!selmon) ++ return; ++ ++ wl_list_for_each(c, &fstack, flink) { ++ if (!(title = client_get_title(c))) ++ continue; ++ if (strcmp(line, title) == 0) ++ goto found; ++ } ++ return; ++ ++found: ++ focusclient(c, 1); ++ wlr_cursor_move(cursor, NULL, selmon->m.x - prevm->m.x , 0); ++ selmon->seltags ^= 1; /* toggle sel tagset */ ++ selmon->tagset[selmon->seltags] = c->tags; ++ arrange(selmon); ++ printstatus(); ++} ++ ++void ++menulayoutfeed(FILE *f) ++{ ++ unsigned int i; ++ for (i = 0; i < LENGTH(layouts); i++) ++ fprintf(f, "%s\n", layouts[i].symbol); ++} ++ ++void ++menulayoutaction(char *line) ++{ ++ unsigned int i; ++ Arg a; ++ for (i = 0; i < LENGTH(layouts); i++) ++ if (strcmp(line, layouts[i].symbol) == 0) ++ goto found; ++ return; ++ ++found: ++ a.v = &layouts[i]; ++ setlayout(&a); ++} ++ + void + monocle(Monitor *m) + { +@@ -2576,6 +2724,10 @@ setup(void) + * e.g when running in the x11 backend or the wayland backend and the + * compositor has Xwayland support */ + unsetenv("DISPLAY"); ++ ++ menu_source = wl_event_loop_add_timer( ++ wl_display_get_event_loop(dpy), menuloop, NULL); ++ + #ifdef XWAYLAND + /* + * Initialise the XWayland X server.