Update menu patch

This commit is contained in:
Nikita Ivanov 2025-03-21 22:12:45 +01:00
parent 5cf233eb6f
commit b0de2fb715
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
2 changed files with 96 additions and 92 deletions

View File

@ -1,16 +1,18 @@
### Description ### Description
This patch adds `menu` command, which allows dwl to interface with dmenu-like programs. This patch adds `menu` command, which allows dwl to interface with dmenu-like
programs.
By default, two menus are available: By default, two menus are available:
- focusing a window by its title by pressing `Alt+o` - focusing a window by its title/appid by pressing `Alt+o`
- selecting a layout from a list by pressing `Alt+Shift+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. Edit `menus` array in `config.h` to add/change menus and use a different dmenu
program.
### Download ### Download
- [git branch](https://codeberg.org/nikitaivanov/dwl/src/branch/menu) - [2025-03-21 v0.7](https://codeberg.org/dwl/dwl-patches/raw/branch/main/patches/menu/menu.patch)
- [2024-07-13](https://codeberg.org/dwl/dwl-patches/raw/branch/main/patches/menu/menu.patch) - [2024-07-13 v0.7](https://codeberg.org/dwl/dwl-patches/raw/commit/65ea99519bbf7a52f48932aea7385f81f8b30867/patches/menu/menu.patch)
### Authors ### Authors
- [Nikita Ivanov](https://codeberg.org/nikitaivanov) ([GitHub](https://github.com/NikitaIvanovV)) - [Nikita Ivanov](https://codeberg.org/nikitaivanov) ([GitHub](https://github.com/NikitaIvanovV))

View File

@ -1,11 +1,15 @@
commit 2d7e19dc948aec61746fd858394a9c80d34a3216 From 1ce61fea52891ed719898c05d616ec20d34f2c73 Mon Sep 17 00:00:00 2001
Author: Nikita Ivanov <nikita.vyach.ivanov@gmail.com> From: Nikita Ivanov <nikita.vyach.ivanov@gmail.com>
Date: Sat Jul 13 01:05:20 2024 +0200 Date: Fri, 21 Mar 2025 21:48:42 +0100
Subject: [PATCH] Add menu command
Add menu command ---
config.def.h | 8 +++
dwl.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 162 insertions(+)
diff --git a/config.def.h b/config.def.h diff --git a/config.def.h b/config.def.h
index 22d2171..ecd2c67 100644 index 22d2171..a5914ca 100644
--- a/config.def.h --- a/config.def.h
+++ b/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 @@ -20,6 +20,12 @@ static const float fullscreen_bg[] = {0.1f, 0.1f, 0.1f, 1.0f}; /* You ca
@ -14,7 +18,7 @@ index 22d2171..ecd2c67 100644
+static const Menu menus[] = { +static const Menu menus[] = {
+ /* command feed function action function */ + /* command feed function action function */
+ { "wmenu -i -l 5 -p Windows", menuwinfeed, menuwinaction }, + { "wmenu -i -l 10 -p Windows", menuwinfeed, menuwinaction },
+ { "wmenu -i -p Layouts", menulayoutfeed, menulayoutaction }, + { "wmenu -i -p Layouts", menulayoutfeed, menulayoutaction },
+}; +};
+ +
@ -31,10 +35,18 @@ index 22d2171..ecd2c67 100644
{ MODKEY, XKB_KEY_e, togglefullscreen, {0} }, { MODKEY, XKB_KEY_e, togglefullscreen, {0} },
{ MODKEY, XKB_KEY_0, view, {.ui = ~0} }, { MODKEY, XKB_KEY_0, view, {.ui = ~0} },
diff --git a/dwl.c b/dwl.c diff --git a/dwl.c b/dwl.c
index dc0437e..90bb09a 100644 index def2562..04543a6 100644
--- a/dwl.c --- a/dwl.c
+++ b/dwl.c +++ b/dwl.c
@@ -241,6 +241,12 @@ typedef struct { @@ -1,6 +1,7 @@
/*
* See LICENSE file for copyright and license details.
*/
+#include <fcntl.h>
#include <getopt.h>
#include <libinput.h>
#include <linux/input-event-codes.h>
@@ -242,6 +243,12 @@ typedef struct {
struct wl_listener destroy; struct wl_listener destroy;
} SessionLock; } SessionLock;
@ -47,12 +59,12 @@ index dc0437e..90bb09a 100644
/* function declarations */ /* function declarations */
static void applybounds(Client *c, struct wlr_box *bbox); static void applybounds(Client *c, struct wlr_box *bbox);
static void applyrules(Client *c); static void applyrules(Client *c);
@@ -298,6 +304,12 @@ static void killclient(const Arg *arg); @@ -302,6 +309,12 @@ static void killclient(const Arg *arg);
static void locksession(struct wl_listener *listener, void *data); static void locksession(struct wl_listener *listener, void *data);
static void mapnotify(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 maximizenotify(struct wl_listener *listener, void *data);
+static void menu(const Arg *arg); +static void menu(const Arg *arg);
+static int menuloop(void *data); +static int menuread(int fd, uint32_t mask, void *data);
+static void menuwinfeed(FILE *f); +static void menuwinfeed(FILE *f);
+static void menuwinaction(char *line); +static void menuwinaction(char *line);
+static void menulayoutfeed(FILE *f); +static void menulayoutfeed(FILE *f);
@ -60,27 +72,19 @@ index dc0437e..90bb09a 100644
static void monocle(Monitor *m); static void monocle(Monitor *m);
static void motionabsolute(struct wl_listener *listener, void *data); static void motionabsolute(struct wl_listener *listener, void *data);
static void motionnotify(uint32_t time, struct wlr_input_device *device, double sx, static void motionnotify(uint32_t time, struct wlr_input_device *device, double sx,
@@ -408,6 +420,11 @@ static struct wlr_box sgeom; @@ -413,6 +426,11 @@ static struct wlr_box sgeom;
static struct wl_list mons; static struct wl_list mons;
static Monitor *selmon; static Monitor *selmon;
+static struct wl_event_source *menu_source;
+static pid_t menu_pid;
+static int menu_fd;
+static const Menu *menu_current; +static const Menu *menu_current;
+static int menu_fd;
+static pid_t menu_pid;
+static struct wl_event_source *menu_source;
+ +
#ifdef XWAYLAND #ifdef XWAYLAND
static void activatex11(struct wl_listener *listener, void *data); static void activatex11(struct wl_listener *listener, void *data);
static void associatex11(struct wl_listener *listener, void *data); static void associatex11(struct wl_listener *listener, void *data);
@@ -675,6 +692,7 @@ cleanup(void) @@ -1768,6 +1786,142 @@ maximizenotify(struct wl_listener *listener, void *data)
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); wlr_xdg_surface_schedule_configure(c->surface.xdg);
} }
@ -88,66 +92,71 @@ index dc0437e..90bb09a 100644
+menu(const Arg *arg) +menu(const Arg *arg)
+{ +{
+ FILE *f; + FILE *f;
+ pid_t pid;
+ int fd_right[2], fd_left[2]; + int fd_right[2], fd_left[2];
+ +
+ if (!selmon || menu_pid != 0) + if (menu_current != NULL) {
+ wl_event_source_remove(menu_source);
+ close(menu_fd);
+ kill(menu_pid, SIGTERM);
+ menu_current = NULL;
+ if (!arg->v)
+ return; + return;
+ + }
+ menu_current = arg->v;
+ +
+ if (pipe(fd_right) == -1 || pipe(fd_left) == -1) + if (pipe(fd_right) == -1 || pipe(fd_left) == -1)
+ return; + return;
+ if ((pid = fork()) == -1) + if ((menu_pid = fork()) == -1)
+ return; + return;
+ if (pid == 0) { + if (menu_pid == 0) {
+ close(fd_right[1]); + close(fd_right[1]);
+ close(fd_left[0]); + close(fd_left[0]);
+ dup2(fd_right[0], STDIN_FILENO); + dup2(fd_right[0], STDIN_FILENO);
+ close(fd_right[0]); + close(fd_right[0]);
+ dup2(fd_left[1], STDOUT_FILENO); + dup2(fd_left[1], STDOUT_FILENO);
+ close(fd_left[1]); + close(fd_left[1]);
+ + execl("/bin/sh", "/bin/sh", "-c", ((Menu *)(arg->v))->cmd, NULL);
+ execl("/bin/sh", "/bin/sh", "-c", menu_current->cmd, NULL);
+ die("dwl: execl %s failed:", "/bin/sh"); + die("dwl: execl %s failed:", "/bin/sh");
+ } + }
+ +
+ close(fd_right[0]); + close(fd_right[0]);
+ close(fd_left[1]); + close(fd_left[1]);
+ + menu_fd = fd_left[0];
+ if (fcntl(menu_fd, F_SETFL, fcntl(menu_fd, F_GETFL) | O_NONBLOCK) == -1)
+ return;
+ if (!(f = fdopen(fd_right[1], "w"))) + if (!(f = fdopen(fd_right[1], "w")))
+ return; + return;
+ menu_current = arg->v;
+ menu_current->feed(f); + menu_current->feed(f);
+ fclose(f); + fclose(f);
+ + menu_source = wl_event_loop_add_fd(event_loop,
+ menu_pid = pid; + menu_fd, WL_EVENT_READABLE, menuread, NULL);
+ menu_fd = fd_left[0];
+ wl_event_source_timer_update(menu_source, 10);
+} +}
+ +
+int +int
+menuloop(void *data) +menuread(int fd, uint32_t mask, void *data)
+{ +{
+ FILE *f; + char *s;
+ pid_t pid; + int n;
+ char line[256], *s; + static char line[512];
+ static int i = 0;
+ +
+ /* If process is still running, wait for another 50 ms */ + if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
+ if ((pid = waitpid(menu_pid, NULL, WNOHANG)) == 0) { + menu(&(const Arg){ .v = NULL });
+ wl_event_source_timer_update(menu_source, 10);
+ return 0; + return 0;
+ } + }
+ + if ((n = read(menu_fd, line + i, LENGTH(line) - 1 - i)) == -1) {
+ menu_pid = 0; + if (errno != EAGAIN)
+ + menu(&(const Arg){ .v = NULL });
+ if (!(f = fdopen(menu_fd, "r")))
+ return 0; + return 0;
+ if (fgets(line, sizeof(line), f)) { + }
+ if ((s = strchr(line, '\n'))) + line[i + n] = '\0';
+ if (!(s = strchr(line + i, '\n'))) {
+ i += n;
+ return 0;
+ }
+ i = 0;
+ *s = '\0'; + *s = '\0';
+ menu_current->action(line); + menu_current->action(line);
+ }
+ fclose(f);
+ return 0; + return 0;
+} +}
+ +
@ -155,12 +164,15 @@ index dc0437e..90bb09a 100644
+menuwinfeed(FILE *f) +menuwinfeed(FILE *f)
+{ +{
+ Client *c; + Client *c;
+ const char *title; + const char *title, *appid;
+ +
+ wl_list_for_each(c, &fstack, flink) { + wl_list_for_each(c, &fstack, flink) {
+ if (!(title = client_get_title(c))) + if (!(title = client_get_title(c)))
+ continue; + continue;
+ fprintf(f, "%s\n", title); + fprintf(f, "%s", title);
+ if ((appid = client_get_appid(c)))
+ fprintf(f, " | %s", appid);
+ fputc('\n', f);
+ } + }
+} +}
+ +
@ -168,63 +180,53 @@ index dc0437e..90bb09a 100644
+menuwinaction(char *line) +menuwinaction(char *line)
+{ +{
+ Client *c; + Client *c;
+ Monitor *prevm = selmon; + const char *appid, *title;
+ const char *title; + static char buf[512];
+
+ if (!selmon)
+ return;
+ +
+ wl_list_for_each(c, &fstack, flink) { + wl_list_for_each(c, &fstack, flink) {
+ if (!(title = client_get_title(c))) + if (!(title = client_get_title(c)))
+ continue; + continue;
+ if (strcmp(line, title) == 0) + appid = client_get_appid(c);
+ snprintf(buf, LENGTH(buf) - 1, "%s%s%s",
+ title, appid ? " | " : "", appid ? appid : "");
+ if (strcmp(line, buf) == 0)
+ goto found; + goto found;
+ } + }
+ return; + return;
+ +
+found: +found:
+ focusclient(c, 1); + if (!c->mon)
+ wlr_cursor_move(cursor, NULL, selmon->m.x - prevm->m.x , 0); + return;
+ selmon->seltags ^= 1; /* toggle sel tagset */ + wl_list_remove(&c->flink);
+ selmon->tagset[selmon->seltags] = c->tags; + wl_list_insert(&fstack, &c->flink);
+ arrange(selmon); + selmon = c->mon;
+ printstatus(); + view(&(const Arg){ .ui = c->tags });
+} +}
+ +
+void +void
+menulayoutfeed(FILE *f) +menulayoutfeed(FILE *f)
+{ +{
+ unsigned int i; + const Layout *l;
+ for (i = 0; i < LENGTH(layouts); i++) + for (l = layouts; l < END(layouts); l++)
+ fprintf(f, "%s\n", layouts[i].symbol); + fprintf(f, "%s\n", l->symbol);
+} +}
+ +
+void +void
+menulayoutaction(char *line) +menulayoutaction(char *line)
+{ +{
+ unsigned int i; + const Layout *l;
+ Arg a; + for (l = layouts; l < END(layouts); l++)
+ for (i = 0; i < LENGTH(layouts); i++) + if (strcmp(line, l->symbol) == 0)
+ if (strcmp(line, layouts[i].symbol) == 0)
+ goto found; + goto found;
+ return; + return;
+ +
+found: +found:
+ a.v = &layouts[i]; + setlayout(&(const Arg){ .v = l });
+ setlayout(&a);
+} +}
+ +
void void
monocle(Monitor *m) monocle(Monitor *m)
{ {
@@ -2576,6 +2724,10 @@ setup(void) --
* e.g when running in the x11 backend or the wayland backend and the 2.49.0
* 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.