add spawnorfocus patch

This commit is contained in:
Gravity 2025-12-09 08:32:54 +08:00
parent 955529b809
commit e35096c909
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,16 @@
### Description
Allows to assign a keybind to focus on a client with a specified matching app_id/title or
spawn command if there is no match.
Originally taken the idea from [namedscratchpads](https://codeberg.org/dwl/dwl-patches/src/branch/main/patches/namedscratchpads)
and [clicseo](https://github.com/djpohly/dwl/issues/519) to make run or raise.
### Download
- [git branch](https://github.com/GravityShark/dwl-grav) (not really properly maintained)
- [main 2025-12-09](/dwl/dwl-patches/raw/branch/main/patches/spawnorfocus/spawnorfocus.patch)
### Authors - latest at top
- [GravityShark](https://codeberg.org/GravityShark)

View File

@ -0,0 +1,87 @@
From c7ce8e9511f6d8d651f671c4113b50fbd05fb77e Mon Sep 17 00:00:00 2001
From: Gravity <alternativegravitational@protonmail.com>
Date: Tue, 9 Dec 2025 07:46:19 +0800
Subject: [PATCH] spawnorfocus
---
config.def.h | 8 ++++++--
dwl.c | 25 +++++++++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/config.def.h b/config.def.h
index 95c2afa..9559f02 100644
--- a/config.def.h
+++ b/config.def.h
@@ -119,14 +119,18 @@ static const enum libinput_config_tap_button_map button_map = LIBINPUT_CONFIG_TA
#define SHCMD(cmd) { .v = (const char*[]){ "/bin/sh", "-c", cmd, NULL } }
/* commands */
-static const char *termcmd[] = { "foot", NULL };
+/* Set the first item as title or app_id, then the command */
+/* or as NULL to use the command as title or app_id */
+static const char *termcmd[] = { NULL, "foot", NULL };
+static const char *browsercmd[] = { "Mozilla Firefox", "firefox", NULL };
static const char *menucmd[] = { "wmenu-run", NULL };
static const Key keys[] = {
/* Note that Shift changes certain key codes: c -> C, 2 -> at, etc. */
/* modifier key function argument */
{ MODKEY, XKB_KEY_p, spawn, {.v = menucmd} },
- { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Return, spawn, {.v = termcmd} },
+ { MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_Return, spawnorfocus, {.v = termcmd} },
+ { MODKEY, XKB_KEY_b, spawnorfocus, {.v = browsercmd} },
{ MODKEY, XKB_KEY_j, focusstack, {.i = +1} },
{ MODKEY, XKB_KEY_k, focusstack, {.i = -1} },
{ MODKEY, XKB_KEY_i, incnmaster, {.i = +1} },
diff --git a/dwl.c b/dwl.c
index 12f441e..ee9cd66 100644
--- a/dwl.c
+++ b/dwl.c
@@ -8,6 +8,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
@@ -330,6 +331,7 @@ static void setpsel(struct wl_listener *listener, void *data);
static void setsel(struct wl_listener *listener, void *data);
static void setup(void);
static void spawn(const Arg *arg);
+static void spawnorfocus(const Arg *arg);
static void startdrag(struct wl_listener *listener, void *data);
static void tag(const Arg *arg);
static void tagmon(const Arg *arg);
@@ -2676,6 +2678,29 @@ spawn(const Arg *arg)
}
}
+void
+spawnorfocus(const Arg *arg)
+{
+ char **argv = (char **)arg->v;
+ Client *c;
+ const Arg cmd = {.v = argv + 1};
+ const char *needle = argv[0] ? argv[0] : argv[1];
+
+ wl_list_for_each(c, &clients, link)
+ if (strstr(client_get_title(c), needle)
+ || strstr(client_get_appid(c), needle)) {
+ selmon = c->mon;
+ if (!(c->tags & selmon->tagset[selmon->seltags]))
+ selmon->tagset[selmon->seltags] = c->tags;
+
+ focusclient(c, 1);
+ arrange(selmon);
+ return;
+ }
+
+ spawn(&cmd);
+}
+
void
startdrag(struct wl_listener *listener, void *data)
{
--
2.51.2