From 5ecaa97f10602f44d1897dd8383d4e2f07d195f9 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Wed, 28 Jan 2026 11:43:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20raise=5For=5Frun=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/action.c | 18 ++++++++++++++++++ src/action.h | 1 + src/client.c | 13 +++++++++++++ src/client.h | 1 + src/default_config.h | 31 ++++++++++++++++++++++++++++--- 5 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/action.c b/src/action.c index fb1d9b0..bacedfc 100644 --- a/src/action.c +++ b/src/action.c @@ -57,3 +57,21 @@ void quit(const user_action_arg_t *arg) { wm_quit(); } } + +void raise_or_run(const user_action_arg_t *arg) { + const char *class = ((const char **)arg->ptr)[0]; + client_t *client = client_get_next_by_class(wm.client_focused, class); + if (client && client == wm.client_focused) return; + + if (client) { + wm.current_monitor = client->monitor; + monitor_select_tag(client->monitor, client->tags); + client_stack_raise(client); + client_focus(client); + return; + } + + const char *command = ((const char **)arg->ptr)[1]; + user_action_arg_t arguments = {.ptr = command}; + spawn(&arguments); +} diff --git a/src/action.h b/src/action.h index e1b46b1..82f5e50 100644 --- a/src/action.h +++ b/src/action.h @@ -54,3 +54,4 @@ void send_client_to_tag(const user_action_arg_t *arg); void send_client_to_next_monitor(const user_action_arg_t *arg); void spawn(const user_action_arg_t *arg); void quit(const user_action_arg_t *arg); +void raise_or_run(const user_action_arg_t *arg); diff --git a/src/client.c b/src/client.c index 6325587..c275213 100644 --- a/src/client.c +++ b/src/client.c @@ -23,6 +23,19 @@ client_t *client_get_by_window(xcb_window_t window) { return nullptr; } +client_t *client_get_next_by_class(client_t *current, const char *class) { + client_t *start = current ? current->next : wm.client_list; + for (client_t *c = start; c; c = c->next) { + if (strcmp(class, c->class) == 0) return c; + } + + for (client_t *c = wm.client_list; c && c != start; c = c->next) { + if (strcmp(class, c->class) == 0) return c; + } + + return nullptr; +} + static void client_attach_list(client_t *client) { client->next = wm.client_list; wm.client_list = client; diff --git a/src/client.h b/src/client.h index 680a7b8..c5731d1 100644 --- a/src/client.h +++ b/src/client.h @@ -6,6 +6,7 @@ #include "types.h" client_t *client_get_by_window(xcb_window_t window); +client_t *client_get_next_by_class(client_t *current,const char *class); task_in_tag_t *client_get_task_in_tag(client_t *client, tag_t *tag); task_in_tag_t *client_get_next_task_in_tag(client_t *client, tag_t *tag); task_in_tag_t *client_get_previous_task_in_tag(client_t *client, tag_t *tag); diff --git a/src/default_config.h b/src/default_config.h index 777eeed..b7ba177 100644 --- a/src/default_config.h +++ b/src/default_config.h @@ -37,10 +37,35 @@ static const button_t button_list[] = { static const char launcher[] = "rofi -show combi -modes combi -combi-modes window,drun,run,ssh,windowcd"; +static const char terminal[] = "xterm"; +static const char terminal_class[] = "XTerm"; +static const char editor[] = "emacsclient -a '' -r"; +static const char editor_class[] = "Emacs"; +static const char browser[] = "firefox-bin"; +static const char browser_class[] = "firefox"; static const keyboard_t key_list[] = { - {modifier_super, XK_p, spawn, {.ptr = launcher}}, - {modifier_super, XK_q, quit, {.b = false}}, - {modifier_super, XK_r, quit, {.b = true}}, + {modifier_super, XK_r, spawn, {.ptr = launcher}}, + {modifier_super, XK_Return, spawn, {.ptr = terminal}}, + { + modifier_control | modifier_alt, + XK_r, + raise_or_run, + {.ptr = (const char *[]){terminal_class, terminal}}, + }, + { + modifier_super, + XK_e, + raise_or_run, + {.ptr = (const char *[]){editor_class, editor}}, + }, + { + modifier_super, + XK_q, + raise_or_run, + {.ptr = (const char *[]){browser_class, browser}}, + }, + {modifier_super | modifier_shift, XK_q, quit, {.b = false}}, + {modifier_super | modifier_control, XK_r, quit, {.b = true}}, {modifier_super | modifier_shift, XK_j, focus_client_in_same_tag,