From ab4c8d5679bd92970bdc5ce8ae8577c47336a084 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Thu, 20 Mar 2025 17:03:54 +0800 Subject: [PATCH] feat: add dispatch spawn_on_empty --- config.conf | 4 +++- dispatch.h | 1 + maomao.c | 19 +++++++++++++++++++ parse_config.h | 9 +++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/config.conf b/config.conf index b6d9aa9..4910f69 100644 --- a/config.conf +++ b/config.conf @@ -265,7 +265,9 @@ bind=ALT+SHIFT,Z,incgaps,-1 bind=ALT+SHIFT,R,togglegaps #custom app bind example -# bind=SUPER,Return,spawn,google-chrome +# spawn_on_empty (if tag 4 is empty , open app in this,otherwise view to tag 4) +# bind=SUPER,Return,spawn_on_empty,google-chrome,4 +# spawn # bind=CTRL+ALT,Return,spawn,st -e ~/tool/ter-multiplexer.sh # Mouse Button Bindings diff --git a/dispatch.h b/dispatch.h index 87102d6..0594e1b 100644 --- a/dispatch.h +++ b/dispatch.h @@ -19,6 +19,7 @@ void togglemaxmizescreen(const Arg *arg); void togglegaps(const Arg *arg); void tagmon(const Arg *arg); void spawn(const Arg *arg); +void spawn_on_empty(const Arg *arg); void setlayout(const Arg *arg); void switch_layout(const Arg *arg); void setmfact(const Arg *arg); diff --git a/maomao.c b/maomao.c index e22c8b8..ad78516 100644 --- a/maomao.c +++ b/maomao.c @@ -5287,6 +5287,25 @@ void spawn(const Arg *arg) { } } +void spawn_on_empty(const Arg *arg) { + bool is_empty = true; + Client *c; + + wl_list_for_each(c, &clients, link) { + if (arg->ui & c->tags) { + is_empty = false; + break; + } + } + if(!is_empty) { + view(arg,true); + return; + } else { + view(arg,true); + spawn(arg); + } +} + void startdrag(struct wl_listener *listener, void *data) { struct wlr_drag *drag = data; if (!drag->icon) diff --git a/parse_config.h b/parse_config.h index 0f49741..5706053 100644 --- a/parse_config.h +++ b/parse_config.h @@ -449,6 +449,15 @@ FuncType parse_func_name(char *func_name, Arg *arg, char *arg_value) { } else if (strcmp(func_name, "spawn") == 0) { func = spawn; (*arg).v = strdup(arg_value); + } else if (strcmp(func_name, "spawn_on_empty") == 0) { + char cmd[256],tag_num[256]; + if (sscanf(arg_value, "%255[^,],%255s", cmd, tag_num) == 2) { + func = spawn_on_empty; + (*arg).v = strdup(cmd); // 注意:之后需要释放这个内存 + (*arg).ui = 1 << (atoi(tag_num) - 1); + } else { + fprintf(stderr, "Error: Invalid value format: %s\n", arg_value); + } } else if (strcmp(func_name, "quit") == 0) { func = quit; } else if (strcmp(func_name, "moveresize") == 0) {