From 7a22fa0e5f1d759c7671a9f05ed508fd8a067472 Mon Sep 17 00:00:00 2001 From: Zijung Chueh Date: Fri, 13 Jul 2018 14:45:38 +0800 Subject: [PATCH] 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);