From fbcc26b5e11ee03f983e2a521a9379ed0da090e6 Mon Sep 17 00:00:00 2001 From: Zijung Chueh Date: Fri, 13 Jul 2018 01:37:05 +0800 Subject: [PATCH 1/3] Add options to specify mouse event --- dunstrc | 11 +++++++++ src/settings.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 4 ++++ src/x11/x.c | 20 +++++++++++++--- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/dunstrc b/dunstrc index 406c7a7..5744b1b 100644 --- a/dunstrc +++ b/dunstrc @@ -258,6 +258,17 @@ # Context menu. context = ctrl+shift+period +# Defines action of mouse event +# Possible values are: +# * do_action: If the notification has exactly one action, or one is marked as default, +# invoke it. If there are multiple and no default, open the context menu. +# * close_current: Close current notification. +# * push_all: Push all waiting and displayed notifications to history. +[mouse] + left_click = close_current + middle_click = do_action + right_click = push_all + [urgency_low] # IMPORTANT: colors have to be defined in quotation marks. # Otherwise the "#" and following would be interpreted as a comment. diff --git a/src/settings.c b/src/settings.c index f278ac6..28f3c13 100644 --- a/src/settings.c +++ b/src/settings.c @@ -516,6 +516,70 @@ void load_settings(char *cmdline_config_path) ); } + + { + char *c = option_get_string( + "mouse", + "left_click", "-left_click", "close_current", + "Action of Left click event" + ); + + if (strlen(c) > 0) { + if (strcmp(c, "do_action") == 0) + settings.left_click = do_action; + else if (strcmp(c, "close_current") == 0) + settings.left_click = close_current; + else if (strcmp(c, "push_all") == 0) + settings.left_click = push_all; + else { + LOG_W("Unknown left_click position: '%s'", c); + } + } + g_free(c); + } + + { + char *c = option_get_string( + "mouse", + "middle_click", "-middel_click", "do_action", + "Action of middle click event" + ); + + if (strlen(c) > 0) { + if (strcmp(c, "do_action") == 0) + settings.middle_click = do_action; + else if (strcmp(c, "close_current") == 0) + settings.middle_click = close_current; + else if (strcmp(c, "push_all") == 0) + settings.middle_click = push_all; + else { + LOG_W("Unknown middle_click position: '%s'", c); + } + } + g_free(c); + } + + { + char *c = option_get_string( + "mouse", + "right_click", "-right_click", "push_all", + "Action of right click event" + ); + + if (strlen(c) > 0) { + if (strcmp(c, "do_action") == 0) + settings.right_click = do_action; + else if (strcmp(c, "close_current") == 0) + settings.right_click = close_current; + else if (strcmp(c, "push_all") == 0) + settings.right_click = push_all; + else { + LOG_W("Unknown right_click position: '%s'", c); + } + } + g_free(c); + } + settings.lowbgcolor = option_get_string( "urgency_low", "background", "-lb", defaults.lowbgcolor, diff --git a/src/settings.h b/src/settings.h index ece154e..f2b7e96 100644 --- a/src/settings.h +++ b/src/settings.h @@ -12,6 +12,7 @@ enum icon_position_t { icons_left, icons_right, icons_off }; enum separator_color { SEP_FOREGROUND, SEP_AUTO, SEP_FRAME, SEP_CUSTOM }; enum follow_mode { FOLLOW_NONE, FOLLOW_MOUSE, FOLLOW_KEYBOARD }; enum markup_mode { MARKUP_NULL, MARKUP_NO, MARKUP_STRIP, MARKUP_FULL }; +enum mouse_action { do_action, close_current, push_all }; struct geometry { int x; @@ -85,6 +86,9 @@ typedef struct _settings { keyboard_shortcut context_ks; bool force_xinerama; int corner_radius; + enum mouse_action left_click; + enum mouse_action middle_click; + enum mouse_action right_click; } settings_t; extern settings_t settings; diff --git a/src/x11/x.c b/src/x11/x.c index 8e94598..368c6f5 100644 --- a/src/x11/x.c +++ b/src/x11/x.c @@ -386,13 +386,27 @@ bool x_is_idle(void) */ static void x_handle_click(XEvent ev) { - if (ev.xbutton.button == Button3) { + enum mouse_action act; + + switch (ev.xbutton.button) { + case Button1: + act = settings.left_click; + break; + case Button2: + act = settings.middle_click; + break; + case Button3: + act = settings.right_click; + break; + } + + if (act == push_all) { queues_history_push_all(); return; } - if (ev.xbutton.button == Button1 || ev.xbutton.button == Button2) { + if (act == do_action || act == close_current) { int y = settings.separator_height; notification *n = NULL; int first = true; @@ -408,7 +422,7 @@ static void x_handle_click(XEvent ev) } if (n) { - if (ev.xbutton.button == Button1) + if (act == close_current) queues_notification_close(n, REASON_USER); else notification_do_action(n); From 7a22fa0e5f1d759c7671a9f05ed508fd8a067472 Mon Sep 17 00:00:00 2001 From: Zijung Chueh Date: Fri, 13 Jul 2018 14:45:38 +0800 Subject: [PATCH 2/3] Fix code style * Move mouse_left/middle/right_click to global section * Match the enum value style * Ignore unknow mouse event * Split copy-paste code into a function * Fix typo --- docs/dunst.pod | 24 +++++++++++++++++++ dunstrc | 24 ++++++++++--------- src/settings.c | 65 ++++++++++++++++++++------------------------------ src/settings.h | 8 +++---- src/x11/x.c | 15 +++++++----- 5 files changed, 76 insertions(+), 60 deletions(-) diff --git a/docs/dunst.pod b/docs/dunst.pod index 41ccfb6..743ffd2 100644 --- a/docs/dunst.pod +++ b/docs/dunst.pod @@ -472,6 +472,30 @@ single notification. To avoid the corners clipping the icon or text the corner radius will be automatically lowered to half of the notification height if it exceeds it. +=item B (values: [none/do_action/close_current/close_all]) + +Defines action of mouse click. + +=over 4 + +=item B + +Don't do anything. + +=item B (default for mouse_middle_click) + +If the notification has exactly one action, or one is marked as default, invoke it. If there are multiple and no default, open the context menu. + +=item B (default for mouse_left_click) + +Close current notification. + +=item B (default for mouse_right_click) + +Close all notifications. + +=back + =back =head2 Shortcut section diff --git a/dunstrc b/dunstrc index 5744b1b..b3f99fb 100644 --- a/dunstrc +++ b/dunstrc @@ -226,6 +226,19 @@ # layout changes. force_xinerama = false + ### mouse + + # Defines action of mouse event + # Possible values are: + # * none: Don't do anything. + # * do_action: If the notification has exactly one action, or one is marked as default, + # invoke it. If there are multiple and no default, open the context menu. + # * close_current: Close current notification. + # * close_all: Close all notifications. + mouse_left_click = close_current + mouse_middle_click = do_action + mouse_right_click = close_all + # Experimental features that may or may not work correctly. Do not expect them # to have a consistent behaviour across releases. [experimental] @@ -258,17 +271,6 @@ # Context menu. context = ctrl+shift+period -# Defines action of mouse event -# Possible values are: -# * do_action: If the notification has exactly one action, or one is marked as default, -# invoke it. If there are multiple and no default, open the context menu. -# * close_current: Close current notification. -# * push_all: Push all waiting and displayed notifications to history. -[mouse] - left_click = close_current - middle_click = do_action - right_click = push_all - [urgency_low] # IMPORTANT: colors have to be defined in quotation marks. # Otherwise the "#" and following would be interpreted as a comment. diff --git a/src/settings.c b/src/settings.c index 28f3c13..3f02e21 100644 --- a/src/settings.c +++ b/src/settings.c @@ -49,6 +49,23 @@ static enum markup_mode parse_markup_mode(const char *mode) } } +static enum mouse_action parse_mouse_action(const char *action) +{ + if (strcmp(action, "none") == 0) + return MOUSE_NONE; + else if (strcmp(action, "do_action") == 0) + return MOUSE_DO_ACTION; + else if (strcmp(action, "close_current") == 0) + return MOUSE_CLOSE_CURRENT; + else if (strcmp(action, "close_all") == 0) + return MOUSE_CLOSE_ALL; + else { + LOG_W("Unknown mouse action: '%s'", action); + return MOUSE_NONE; + } +} + + static enum urgency ini_get_urgency(const char *section, const char *key, const int def) { int ret = def; @@ -519,64 +536,34 @@ void load_settings(char *cmdline_config_path) { char *c = option_get_string( - "mouse", - "left_click", "-left_click", "close_current", + "global", + "mouse_left_click", "-left_click", "close_current", "Action of Left click event" ); - if (strlen(c) > 0) { - if (strcmp(c, "do_action") == 0) - settings.left_click = do_action; - else if (strcmp(c, "close_current") == 0) - settings.left_click = close_current; - else if (strcmp(c, "push_all") == 0) - settings.left_click = push_all; - else { - LOG_W("Unknown left_click position: '%s'", c); - } - } + settings.mouse_left_click = parse_mouse_action(c); g_free(c); } { char *c = option_get_string( - "mouse", - "middle_click", "-middel_click", "do_action", + "global", + "mouse_middle_click", "-mouse_middle_click", "do_action", "Action of middle click event" ); - if (strlen(c) > 0) { - if (strcmp(c, "do_action") == 0) - settings.middle_click = do_action; - else if (strcmp(c, "close_current") == 0) - settings.middle_click = close_current; - else if (strcmp(c, "push_all") == 0) - settings.middle_click = push_all; - else { - LOG_W("Unknown middle_click position: '%s'", c); - } - } + settings.mouse_middle_click = parse_mouse_action(c); g_free(c); } { char *c = option_get_string( - "mouse", - "right_click", "-right_click", "push_all", + "global", + "mouse_right_click", "-mouse_right_click", "close_all", "Action of right click event" ); - if (strlen(c) > 0) { - if (strcmp(c, "do_action") == 0) - settings.right_click = do_action; - else if (strcmp(c, "close_current") == 0) - settings.right_click = close_current; - else if (strcmp(c, "push_all") == 0) - settings.right_click = push_all; - else { - LOG_W("Unknown right_click position: '%s'", c); - } - } + settings.mouse_right_click = parse_mouse_action(c); g_free(c); } diff --git a/src/settings.h b/src/settings.h index f2b7e96..272e079 100644 --- a/src/settings.h +++ b/src/settings.h @@ -12,7 +12,7 @@ enum icon_position_t { icons_left, icons_right, icons_off }; enum separator_color { SEP_FOREGROUND, SEP_AUTO, SEP_FRAME, SEP_CUSTOM }; enum follow_mode { FOLLOW_NONE, FOLLOW_MOUSE, FOLLOW_KEYBOARD }; enum markup_mode { MARKUP_NULL, MARKUP_NO, MARKUP_STRIP, MARKUP_FULL }; -enum mouse_action { do_action, close_current, push_all }; +enum mouse_action { MOUSE_NONE, MOUSE_DO_ACTION, MOUSE_CLOSE_CURRENT, MOUSE_CLOSE_ALL }; struct geometry { int x; @@ -86,9 +86,9 @@ typedef struct _settings { keyboard_shortcut context_ks; bool force_xinerama; int corner_radius; - enum mouse_action left_click; - enum mouse_action middle_click; - enum mouse_action right_click; + enum mouse_action mouse_left_click; + enum mouse_action mouse_middle_click; + enum mouse_action mouse_right_click; } settings_t; extern settings_t settings; diff --git a/src/x11/x.c b/src/x11/x.c index 368c6f5..21eed84 100644 --- a/src/x11/x.c +++ b/src/x11/x.c @@ -390,23 +390,26 @@ static void x_handle_click(XEvent ev) switch (ev.xbutton.button) { case Button1: - act = settings.left_click; + act = settings.mouse_left_click; break; case Button2: - act = settings.middle_click; + act = settings.mouse_middle_click; break; case Button3: - act = settings.right_click; + act = settings.mouse_right_click; break; + default: + LOG_W("Unsupported mouse button: '%d'", ev.xbutton.button); + return; } - if (act == push_all) { + if (act == MOUSE_CLOSE_ALL) { queues_history_push_all(); return; } - if (act == do_action || act == close_current) { + if (act == MOUSE_DO_ACTION || act == MOUSE_CLOSE_CURRENT) { int y = settings.separator_height; notification *n = NULL; int first = true; @@ -422,7 +425,7 @@ static void x_handle_click(XEvent ev) } if (n) { - if (act == close_current) + if (act == MOUSE_CLOSE_CURRENT) queues_notification_close(n, REASON_USER); else notification_do_action(n); From 88175b259a641a8967d2813e45d7fb42b81ca36e Mon Sep 17 00:00:00 2001 From: Zijung Chueh Date: Fri, 13 Jul 2018 21:31:51 +0800 Subject: [PATCH 3/3] Put the default value in the defaults --- config.h | 6 ++++++ src/settings.c | 27 +++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/config.h b/config.h index 3dc7b0a..f0432f6 100644 --- a/config.h +++ b/config.h @@ -102,6 +102,12 @@ settings_t defaults = { .code = 0,.sym = NoSymbol,.is_valid = false }, /* ignore this */ +.mouse_left_click = MOUSE_CLOSE_CURRENT, + +.mouse_middle_click = MOUSE_DO_ACTION, + +.mouse_right_click = MOUSE_CLOSE_ALL, + }; rule_t default_rules[] = { diff --git a/src/settings.c b/src/settings.c index 3f02e21..5d3fbff 100644 --- a/src/settings.c +++ b/src/settings.c @@ -537,33 +537,48 @@ void load_settings(char *cmdline_config_path) { char *c = option_get_string( "global", - "mouse_left_click", "-left_click", "close_current", + "mouse_left_click", "-left_click", NULL, "Action of Left click event" ); - settings.mouse_left_click = parse_mouse_action(c); + if (c) { + settings.mouse_left_click = parse_mouse_action(c); + } else { + settings.mouse_left_click = defaults.mouse_left_click; + } + g_free(c); } { char *c = option_get_string( "global", - "mouse_middle_click", "-mouse_middle_click", "do_action", + "mouse_middle_click", "-mouse_middle_click", NULL, "Action of middle click event" ); - settings.mouse_middle_click = parse_mouse_action(c); + if (c) { + settings.mouse_middle_click = parse_mouse_action(c); + } else { + settings.mouse_middle_click = defaults.mouse_middle_click; + } + g_free(c); } { char *c = option_get_string( "global", - "mouse_right_click", "-mouse_right_click", "close_all", + "mouse_right_click", "-mouse_right_click", NULL, "Action of right click event" ); - settings.mouse_right_click = parse_mouse_action(c); + if (c) { + settings.mouse_right_click = parse_mouse_action(c); + } else { + settings.mouse_right_click = defaults.mouse_right_click; + } + g_free(c); }