Merge pull request #440 from bebehei/enums

Use more enums
This commit is contained in:
Benedikt Heine 2017-11-22 18:25:19 +01:00 committed by GitHub
commit e84922a484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 105 additions and 84 deletions

View File

@ -303,7 +303,7 @@ static void on_close_notification(GDBusConnection *connection,
{ {
guint32 id; guint32 id;
g_variant_get(parameters, "(u)", &id); g_variant_get(parameters, "(u)", &id);
queues_notification_close_id(id, 3); queues_notification_close_id(id, REASON_SIG);
wake_up(); wake_up();
g_dbus_method_invocation_return_value(invocation, NULL); g_dbus_method_invocation_return_value(invocation, NULL);
g_dbus_connection_flush(connection, NULL, NULL, NULL); g_dbus_connection_flush(connection, NULL, NULL, NULL);
@ -322,8 +322,14 @@ static void on_get_server_information(GDBusConnection *connection,
g_dbus_connection_flush(connection, NULL, NULL, NULL); g_dbus_connection_flush(connection, NULL, NULL, NULL);
} }
void notification_closed(notification *n, int reason) void notification_closed(notification *n, enum reason reason)
{ {
if (reason < REASON_MIN || REASON_MAX < reason) {
fprintf(stderr, "ERROR: Closing notification with reason '%d' not supported. "
"Closing it with reason '%d'.\n", reason, REASON_UNDEF);
reason = REASON_UNDEF;
}
if (!dbus_conn) { if (!dbus_conn) {
fprintf(stderr, "ERROR: Tried to close notification but dbus connection not set!\n"); fprintf(stderr, "ERROR: Tried to close notification but dbus connection not set!\n");
return; return;

View File

@ -5,10 +5,19 @@
#include "notification.h" #include "notification.h"
enum reason {
REASON_MIN = 1,
REASON_TIME = 1,
REASON_USER = 2,
REASON_SIG = 3,
REASON_UNDEF = 4,
REASON_MAX = 4,
};
int initdbus(void); int initdbus(void);
void dbus_tear_down(int id); void dbus_tear_down(int id);
/* void dbus_poll(int timeout); */ /* void dbus_poll(int timeout); */
void notification_closed(notification *n, int reason); void notification_closed(notification *n, enum reason reason);
void action_invoked(notification *n, const char *identifier); void action_invoked(notification *n, const char *identifier);
#endif #endif

View File

@ -157,7 +157,7 @@ int dunst_main(int argc, char *argv[])
n->progress = -1; n->progress = -1;
n->timeout = 10 * G_USEC_PER_SEC; n->timeout = 10 * G_USEC_PER_SEC;
n->markup = MARKUP_NO; n->markup = MARKUP_NO;
n->urgency = LOW; n->urgency = URG_LOW;
notification_init(n); notification_init(n);
queues_notification_insert(n, 0); queues_notification_insert(n, 0);
// we do not call wakeup now, wake_up does not work here yet // we do not call wakeup now, wake_up does not work here yet

View File

@ -38,7 +38,7 @@ void notification_print(notification *n)
printf("\traw_icon set: %s\n", (n->raw_icon ? "true" : "false")); printf("\traw_icon set: %s\n", (n->raw_icon ? "true" : "false"));
printf("\tcategory: %s\n", n->category); printf("\tcategory: %s\n", n->category);
printf("\ttimeout: %ld\n", n->timeout/1000); printf("\ttimeout: %ld\n", n->timeout/1000);
printf("\turgency: %d\n", n->urgency); printf("\turgency: %s\n", notification_urgency_to_string(n->urgency));
printf("\ttransient: %d\n", n->transient); printf("\ttransient: %d\n", n->transient);
printf("\tformatted: '%s'\n", n->msg); printf("\tformatted: '%s'\n", n->msg);
printf("\tfg: %s\n", n->color_strings[ColFG]); printf("\tfg: %s\n", n->color_strings[ColFG]);
@ -80,21 +80,7 @@ void notification_run_script(notification *n)
char *body = n->body ? n->body : ""; char *body = n->body ? n->body : "";
char *icon = n->icon ? n->icon : ""; char *icon = n->icon ? n->icon : "";
char *urgency; const char *urgency = notification_urgency_to_string(n->urgency);
switch (n->urgency) {
case LOW:
urgency = "LOW";
break;
case NORM:
urgency = "NORMAL";
break;
case CRIT:
urgency = "CRITICAL";
break;
default:
urgency = "NORMAL";
break;
}
int pid1 = fork(); int pid1 = fork();
@ -121,6 +107,25 @@ void notification_run_script(notification *n)
} }
} }
/*
* Helper function to convert an urgency to a string
*/
const char *notification_urgency_to_string(enum urgency urgency)
{
switch (urgency) {
case URG_NONE:
return "NONE";
case URG_LOW:
return "LOW";
case URG_NORM:
return "NORMAL";
case URG_CRIT:
return "CRITICAL";
default:
return "UNDEF";
}
}
/* /*
* Helper function to compare to given * Helper function to compare to given
* notifications. * notifications.
@ -432,8 +437,11 @@ void notification_init(notification *n)
n->dup_count = 0; n->dup_count = 0;
/* urgency > CRIT -> array out of range */ /* urgency > URG_CRIT -> array out of range */
n->urgency = n->urgency > CRIT ? CRIT : n->urgency; if (n->urgency < URG_MIN)
n->urgency = URG_LOW;
if (n->urgency > URG_MAX)
n->urgency = URG_CRIT;
if (!n->color_strings[ColFG]) { if (!n->color_strings[ColFG]) {
n->color_strings[ColFG] = xctx.color_strings[ColFG][n->urgency]; n->color_strings[ColFG] = xctx.color_strings[ColFG][n->urgency];

View File

@ -7,12 +7,17 @@
#include "settings.h" #include "settings.h"
#define LOW 0
#define NORM 1
#define CRIT 2
#define DUNST_NOTIF_MAX_CHARS 5000 #define DUNST_NOTIF_MAX_CHARS 5000
enum urgency {
URG_NONE = -1,
URG_MIN = 0,
URG_LOW = 0,
URG_NORM = 1,
URG_CRIT = 2,
URG_MAX = 2,
};
typedef struct _raw_image { typedef struct _raw_image {
int width; int width;
int height; int height;
@ -43,7 +48,7 @@ typedef struct _notification {
gint64 start; gint64 start;
gint64 timestamp; gint64 timestamp;
gint64 timeout; gint64 timeout;
int urgency; enum urgency urgency;
enum markup_mode markup; enum markup_mode markup;
bool redisplayed; /* has been displayed before? */ bool redisplayed; /* has been displayed before? */
int id; int id;
@ -74,5 +79,6 @@ void notification_replace_single_field(char **haystack, char **needle, const cha
void notification_update_text_to_render(notification *n); void notification_update_text_to_render(notification *n);
void notification_do_action(notification *n); void notification_do_action(notification *n);
const char *notification_urgency_to_string(enum urgency urgency);
#endif #endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -7,7 +7,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "dbus.h"
#include "notification.h" #include "notification.h"
#include "settings.h" #include "settings.h"
@ -179,7 +178,7 @@ bool queues_notification_replace_id(notification *new)
return false; return false;
} }
int queues_notification_close_id(int id, int reason) int queues_notification_close_id(int id, enum reason reason)
{ {
notification *target = NULL; notification *target = NULL;
@ -205,17 +204,14 @@ int queues_notification_close_id(int id, int reason)
} }
if (target) { if (target) {
notification_closed(target, reason);
if (reason > 0 && reason < 4)
notification_closed(target, reason);
queues_history_push(target); queues_history_push(target);
} }
return reason; return reason;
} }
int queues_notification_close(notification *n, int reason) int queues_notification_close(notification *n, enum reason reason)
{ {
assert(n != NULL); assert(n != NULL);
return queues_notification_close_id(n->id, reason); return queues_notification_close_id(n->id, reason);
@ -250,11 +246,11 @@ void queues_history_push(notification *n)
void queues_history_push_all(void) void queues_history_push_all(void)
{ {
while (displayed->length > 0) { while (displayed->length > 0) {
queues_notification_close(g_queue_peek_head_link(displayed)->data, 2); queues_notification_close(g_queue_peek_head_link(displayed)->data, REASON_USER);
} }
while (waiting->length > 0) { while (waiting->length > 0) {
queues_notification_close(g_queue_peek_head_link(waiting)->data, 2); queues_notification_close(g_queue_peek_head_link(waiting)->data, REASON_USER);
} }
} }
@ -288,7 +284,7 @@ void queues_check_timeouts(bool idle)
/* remove old message */ /* remove old message */
if (g_get_monotonic_time() - n->start > n->timeout) { if (g_get_monotonic_time() - n->start > n->timeout) {
queues_notification_close(n, 1); queues_notification_close(n, REASON_TIME);
} }
} }
} }

View File

@ -4,6 +4,7 @@
#define DUNST_QUEUE_H #define DUNST_QUEUE_H
#include "notification.h" #include "notification.h"
#include "dbus.h"
/* /*
* Initialise neccessary queues * Initialise neccessary queues
@ -58,21 +59,15 @@ bool queues_notification_replace_id(notification *new);
* *
* After closing, call wake_up to synchronize the queues with the UI * After closing, call wake_up to synchronize the queues with the UI
* (which closes the notification on screen) * (which closes the notification on screen)
*
* reasons:
* -1 -> notification is a replacement, no NotificationClosed signal emitted
* 1 -> the notification expired
* 2 -> the notification was dismissed by the user_data
* 3 -> The notification was closed by a call to CloseNotification
*/ */
int queues_notification_close_id(int id, int reason); int queues_notification_close_id(int id, enum reason reason);
/* Close the given notification. SEE queues_notification_close_id. /* Close the given notification. SEE queues_notification_close_id.
* *
* @n: (transfer full): The notification to close * @n: (transfer full): The notification to close
* @reason: The reason to close * @reason: The reason to close
* */ * */
int queues_notification_close(notification *n, int reason); int queues_notification_close(notification *n, enum reason reason);
/* /*
* Pushed the latest notification of history to the displayed queue * Pushed the latest notification of history to the displayed queue

View File

@ -14,7 +14,7 @@ void rule_apply(rule_t *r, notification *n)
{ {
if (r->timeout != -1) if (r->timeout != -1)
n->timeout = r->timeout; n->timeout = r->timeout;
if (r->urgency != -1) if (r->urgency != URG_NONE)
n->urgency = r->urgency; n->urgency = r->urgency;
if (r->history_ignore != -1) if (r->history_ignore != -1)
n->history_ignore = r->history_ignore; n->history_ignore = r->history_ignore;
@ -62,9 +62,9 @@ void rule_init(rule_t *r)
r->body = NULL; r->body = NULL;
r->icon = NULL; r->icon = NULL;
r->category = NULL; r->category = NULL;
r->msg_urgency = -1; r->msg_urgency = URG_NONE;
r->timeout = -1; r->timeout = -1;
r->urgency = -1; r->urgency = URG_NONE;
r->markup = MARKUP_NULL; r->markup = MARKUP_NULL;
r->new_icon = NULL; r->new_icon = NULL;
r->history_ignore = false; r->history_ignore = false;
@ -86,6 +86,6 @@ bool rule_matches_notification(rule_t *r, notification *n)
&& (!r->icon || !fnmatch(r->icon, n->icon, 0)) && (!r->icon || !fnmatch(r->icon, n->icon, 0))
&& (!r->category || !fnmatch(r->category, n->category, 0)) && (!r->category || !fnmatch(r->category, n->category, 0))
&& (r->match_transient == -1 || (r->match_transient == n->transient)) && (r->match_transient == -1 || (r->match_transient == n->transient))
&& (r->msg_urgency == -1 || r->msg_urgency == n->urgency)); && (r->msg_urgency == URG_NONE || r->msg_urgency == n->urgency));
} }
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -20,7 +20,7 @@ typedef struct _rule_t {
/* actions */ /* actions */
gint64 timeout; gint64 timeout;
int urgency; enum urgency urgency;
enum markup_mode markup; enum markup_mode markup;
int history_ignore; int history_ignore;
int match_transient; int match_transient;

View File

@ -47,18 +47,18 @@ static enum markup_mode parse_markup_mode(const char *mode)
} }
} }
static int ini_get_urgency(const char *section, const char *key, const int def) static enum urgency ini_get_urgency(const char *section, const char *key, const int def)
{ {
int ret = def; int ret = def;
char *urg = ini_get_string(section, key, ""); char *urg = ini_get_string(section, key, "");
if (strlen(urg) > 0) { if (strlen(urg) > 0) {
if (strcmp(urg, "low") == 0) if (strcmp(urg, "low") == 0)
ret = LOW; ret = URG_LOW;
else if (strcmp(urg, "normal") == 0) else if (strcmp(urg, "normal") == 0)
ret = NORM; ret = URG_NORM;
else if (strcmp(urg, "critical") == 0) else if (strcmp(urg, "critical") == 0)
ret = CRIT; ret = URG_CRIT;
else else
fprintf(stderr, fprintf(stderr,
"unknown urgency: %s, ignoring\n", "unknown urgency: %s, ignoring\n",
@ -492,15 +492,15 @@ void load_settings(char *cmdline_config_path)
"Frame color for notifications with low urgency" "Frame color for notifications with low urgency"
); );
settings.timeouts[LOW] = option_get_time( settings.timeouts[URG_LOW] = option_get_time(
"urgency_low", "urgency_low",
"timeout", "-lto", defaults.timeouts[LOW], "timeout", "-lto", defaults.timeouts[URG_LOW],
"Timeout for notifications with low urgency" "Timeout for notifications with low urgency"
); );
settings.icons[LOW] = option_get_string( settings.icons[URG_LOW] = option_get_string(
"urgency_low", "urgency_low",
"icon", "-li", defaults.icons[LOW], "icon", "-li", defaults.icons[URG_LOW],
"Icon for notifications with low urgency" "Icon for notifications with low urgency"
); );
@ -522,15 +522,15 @@ void load_settings(char *cmdline_config_path)
"Frame color for notifications with normal urgency" "Frame color for notifications with normal urgency"
); );
settings.timeouts[NORM] = option_get_time( settings.timeouts[URG_NORM] = option_get_time(
"urgency_normal", "urgency_normal",
"timeout", "-nto", defaults.timeouts[NORM], "timeout", "-nto", defaults.timeouts[URG_NORM],
"Timeout for notifications with normal urgency" "Timeout for notifications with normal urgency"
); );
settings.icons[NORM] = option_get_string( settings.icons[URG_NORM] = option_get_string(
"urgency_normal", "urgency_normal",
"icon", "-ni", defaults.icons[NORM], "icon", "-ni", defaults.icons[URG_NORM],
"Icon for notifications with normal urgency" "Icon for notifications with normal urgency"
); );
@ -552,15 +552,15 @@ void load_settings(char *cmdline_config_path)
"Frame color for notifications with critical urgency" "Frame color for notifications with critical urgency"
); );
settings.timeouts[CRIT] = option_get_time( settings.timeouts[URG_CRIT] = option_get_time(
"urgency_critical", "urgency_critical",
"timeout", "-cto", defaults.timeouts[CRIT], "timeout", "-cto", defaults.timeouts[URG_CRIT],
"Timeout for notifications with critical urgency" "Timeout for notifications with critical urgency"
); );
settings.icons[CRIT] = option_get_string( settings.icons[URG_CRIT] = option_get_string(
"urgency_critical", "urgency_critical",
"icon", "-ci", defaults.icons[CRIT], "icon", "-ci", defaults.icons[URG_CRIT],
"Icon for notifications with critical urgency" "Icon for notifications with critical urgency"
); );

View File

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "src/dbus.h"
#include "src/dunst.h" #include "src/dunst.h"
#include "src/markup.h" #include "src/markup.h"
#include "src/notification.h" #include "src/notification.h"
@ -856,7 +857,7 @@ gboolean x_mainloop_fd_dispatch(GSource *source, GSourceFunc callback,
&& settings.close_ks.mask == state) { && settings.close_ks.mask == state) {
const GList *displayed = queues_get_displayed(); const GList *displayed = queues_get_displayed();
if (displayed && displayed->data) { if (displayed && displayed->data) {
queues_notification_close(displayed->data, 2); queues_notification_close(displayed->data, REASON_USER);
wake_up(); wake_up();
} }
} }
@ -937,7 +938,7 @@ static void x_handle_click(XEvent ev)
if (n) { if (n) {
if (ev.xbutton.button == Button1) if (ev.xbutton.button == Button1)
queues_notification_close(n, 2); queues_notification_close(n, REASON_USER);
else else
notification_do_action(n); notification_do_action(n);
} }
@ -980,26 +981,26 @@ void x_setup(void)
x_shortcut_grab(&settings.context_ks); x_shortcut_grab(&settings.context_ks);
x_shortcut_ungrab(&settings.context_ks); x_shortcut_ungrab(&settings.context_ks);
xctx.color_strings[ColFG][LOW] = settings.lowfgcolor; xctx.color_strings[ColFG][URG_LOW] = settings.lowfgcolor;
xctx.color_strings[ColFG][NORM] = settings.normfgcolor; xctx.color_strings[ColFG][URG_NORM] = settings.normfgcolor;
xctx.color_strings[ColFG][CRIT] = settings.critfgcolor; xctx.color_strings[ColFG][URG_CRIT] = settings.critfgcolor;
xctx.color_strings[ColBG][LOW] = settings.lowbgcolor; xctx.color_strings[ColBG][URG_LOW] = settings.lowbgcolor;
xctx.color_strings[ColBG][NORM] = settings.normbgcolor; xctx.color_strings[ColBG][URG_NORM] = settings.normbgcolor;
xctx.color_strings[ColBG][CRIT] = settings.critbgcolor; xctx.color_strings[ColBG][URG_CRIT] = settings.critbgcolor;
if (settings.lowframecolor) if (settings.lowframecolor)
xctx.color_strings[ColFrame][LOW] = settings.lowframecolor; xctx.color_strings[ColFrame][URG_LOW] = settings.lowframecolor;
else else
xctx.color_strings[ColFrame][LOW] = settings.frame_color; xctx.color_strings[ColFrame][URG_LOW] = settings.frame_color;
if (settings.normframecolor) if (settings.normframecolor)
xctx.color_strings[ColFrame][NORM] = settings.normframecolor; xctx.color_strings[ColFrame][URG_NORM] = settings.normframecolor;
else else
xctx.color_strings[ColFrame][NORM] = settings.frame_color; xctx.color_strings[ColFrame][URG_NORM] = settings.frame_color;
if (settings.critframecolor) if (settings.critframecolor)
xctx.color_strings[ColFrame][CRIT] = settings.critframecolor; xctx.color_strings[ColFrame][URG_CRIT] = settings.critframecolor;
else else
xctx.color_strings[ColFrame][CRIT] = settings.frame_color; xctx.color_strings[ColFrame][URG_CRIT] = settings.frame_color;
/* parse and set xctx.geometry and monitor position */ /* parse and set xctx.geometry and monitor position */
if (settings.geom[0] == '-') { if (settings.geom[0] == '-') {

View File

@ -60,11 +60,11 @@ TEST test_notification_is_duplicate(void *notifications)
ASSERT(notification_is_duplicate(a, b)); ASSERT(notification_is_duplicate(a, b));
b->urgency = LOW; b->urgency = URG_LOW;
ASSERT_FALSE(notification_is_duplicate(a, b)); ASSERT_FALSE(notification_is_duplicate(a, b));
b->urgency = NORM; b->urgency = URG_NORM;
ASSERT(notification_is_duplicate(a, b)); ASSERT(notification_is_duplicate(a, b));
b->urgency = CRIT; b->urgency = URG_CRIT;
ASSERT_FALSE(notification_is_duplicate(a, b)); ASSERT_FALSE(notification_is_duplicate(a, b));
PASS(); PASS();
@ -107,7 +107,7 @@ SUITE(suite_notification)
a->summary = "Summary"; a->summary = "Summary";
a->body = "Body"; a->body = "Body";
a->icon = "Icon"; a->icon = "Icon";
a->urgency = NORM; a->urgency = URG_NORM;
notification *b = notification_create(); notification *b = notification_create();
memcpy(b, a, sizeof(*b)); memcpy(b, a, sizeof(*b));