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
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:
- 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`
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
- [git branch](https://codeberg.org/nikitaivanov/dwl/src/branch/menu)
- [2024-07-13](https://codeberg.org/dwl/dwl-patches/raw/branch/main/patches/menu/menu.patch)
- [2025-03-21 v0.7](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
- [Nikita Ivanov](https://codeberg.org/nikitaivanov) ([GitHub](https://github.com/NikitaIvanovV))

View File

@ -1,11 +1,15 @@
commit 2d7e19dc948aec61746fd858394a9c80d34a3216
Author: Nikita Ivanov <nikita.vyach.ivanov@gmail.com>
Date: Sat Jul 13 01:05:20 2024 +0200
From 1ce61fea52891ed719898c05d616ec20d34f2c73 Mon Sep 17 00:00:00 2001
From: Nikita Ivanov <nikita.vyach.ivanov@gmail.com>
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
index 22d2171..ecd2c67 100644
index 22d2171..a5914ca 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
@ -14,7 +18,7 @@ index 22d2171..ecd2c67 100644
+static const Menu menus[] = {
+ /* 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 },
+};
+
@ -31,10 +35,18 @@ index 22d2171..ecd2c67 100644
{ 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
index def2562..04543a6 100644
--- a/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;
} SessionLock;
@ -47,12 +59,12 @@ index dc0437e..90bb09a 100644
/* 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);
@@ -302,6 +309,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 int menuread(int fd, uint32_t mask, void *data);
+static void menuwinfeed(FILE *f);
+static void menuwinaction(char *line);
+static void menulayoutfeed(FILE *f);
@ -60,27 +72,19 @@ index dc0437e..90bb09a 100644
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;
@@ -413,6 +426,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;
+static int menu_fd;
+static pid_t menu_pid;
+static struct wl_event_source *menu_source;
+
#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)
@@ -1768,6 +1786,142 @@ maximizenotify(struct wl_listener *listener, void *data)
wlr_xdg_surface_schedule_configure(c->surface.xdg);
}
@ -88,66 +92,71 @@ index dc0437e..90bb09a 100644
+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 (menu_current != NULL) {
+ wl_event_source_remove(menu_source);
+ close(menu_fd);
+ kill(menu_pid, SIGTERM);
+ menu_current = NULL;
+ if (!arg->v)
+ return;
+ }
+
+ if (pipe(fd_right) == -1 || pipe(fd_left) == -1)
+ return;
+ if ((pid = fork()) == -1)
+ if ((menu_pid = fork()) == -1)
+ return;
+ if (pid == 0) {
+ if (menu_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);
+ execl("/bin/sh", "/bin/sh", "-c", ((Menu *)(arg->v))->cmd, NULL);
+ die("dwl: execl %s failed:", "/bin/sh");
+ }
+
+ close(fd_right[0]);
+ 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")))
+ return;
+ menu_current = arg->v;
+ menu_current->feed(f);
+ fclose(f);
+
+ menu_pid = pid;
+ menu_fd = fd_left[0];
+ wl_event_source_timer_update(menu_source, 10);
+ menu_source = wl_event_loop_add_fd(event_loop,
+ menu_fd, WL_EVENT_READABLE, menuread, NULL);
+}
+
+int
+menuloop(void *data)
+menuread(int fd, uint32_t mask, void *data)
+{
+ FILE *f;
+ pid_t pid;
+ char line[256], *s;
+ char *s;
+ int n;
+ static char line[512];
+ static int i = 0;
+
+ /* 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);
+ if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
+ menu(&(const Arg){ .v = NULL });
+ return 0;
+ }
+
+ menu_pid = 0;
+
+ if (!(f = fdopen(menu_fd, "r")))
+ if ((n = read(menu_fd, line + i, LENGTH(line) - 1 - i)) == -1) {
+ if (errno != EAGAIN)
+ menu(&(const Arg){ .v = NULL });
+ return 0;
+ if (fgets(line, sizeof(line), f)) {
+ if ((s = strchr(line, '\n')))
+ *s = '\0';
+ menu_current->action(line);
+ }
+ fclose(f);
+ line[i + n] = '\0';
+ if (!(s = strchr(line + i, '\n'))) {
+ i += n;
+ return 0;
+ }
+ i = 0;
+ *s = '\0';
+ menu_current->action(line);
+ return 0;
+}
+
@ -155,12 +164,15 @@ index dc0437e..90bb09a 100644
+menuwinfeed(FILE *f)
+{
+ Client *c;
+ const char *title;
+ const char *title, *appid;
+
+ wl_list_for_each(c, &fstack, flink) {
+ if (!(title = client_get_title(c)))
+ 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)
+{
+ Client *c;
+ Monitor *prevm = selmon;
+ const char *title;
+
+ if (!selmon)
+ return;
+ const char *appid, *title;
+ static char buf[512];
+
+ wl_list_for_each(c, &fstack, flink) {
+ if (!(title = client_get_title(c)))
+ 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;
+ }
+ 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();
+ if (!c->mon)
+ return;
+ wl_list_remove(&c->flink);
+ wl_list_insert(&fstack, &c->flink);
+ selmon = c->mon;
+ view(&(const Arg){ .ui = c->tags });
+}
+
+void
+menulayoutfeed(FILE *f)
+{
+ unsigned int i;
+ for (i = 0; i < LENGTH(layouts); i++)
+ fprintf(f, "%s\n", layouts[i].symbol);
+ const Layout *l;
+ for (l = layouts; l < END(layouts); l++)
+ fprintf(f, "%s\n", l->symbol);
+}
+
+void
+menulayoutaction(char *line)
+{
+ unsigned int i;
+ Arg a;
+ for (i = 0; i < LENGTH(layouts); i++)
+ if (strcmp(line, layouts[i].symbol) == 0)
+ const Layout *l;
+ for (l = layouts; l < END(layouts); l++)
+ if (strcmp(line, l->symbol) == 0)
+ goto found;
+ return;
+
+found:
+ a.v = &layouts[i];
+ setlayout(&a);
+ setlayout(&(const Arg){ .v = l });
+}
+
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.
--
2.49.0