Refactor: Move queue specific methods to queues.c

This commit is contained in:
Benedikt Heine 2017-10-06 14:31:55 +02:00
parent 177fc30484
commit a7003e3616
9 changed files with 197 additions and 149 deletions

View File

@ -8,6 +8,7 @@
#include "dunst.h" #include "dunst.h"
#include "notification.h" #include "notification.h"
#include "queues.h"
#include "settings.h" #include "settings.h"
#include "utils.h" #include "utils.h"

View File

@ -16,6 +16,7 @@
#include "menu.h" #include "menu.h"
#include "notification.h" #include "notification.h"
#include "option_parser.h" #include "option_parser.h"
#include "queues.h"
#include "settings.h" #include "settings.h"
#include "x11/x.h" #include "x11/x.h"
#include "x11/screen.h" #include "x11/screen.h"
@ -39,14 +40,11 @@ bool pause_display = false;
GMainLoop *mainloop = NULL; GMainLoop *mainloop = NULL;
/* notification lists */
GQueue *queue = NULL; /* all new notifications get into here */
GQueue *displayed = NULL; /* currently displayed notifications */
GQueue *history = NULL; /* history of displayed notifications */
GSList *rules = NULL; GSList *rules = NULL;
/* misc funtions */ /* misc funtions */
/*TODO: move to queues.c */
void check_timeouts(void) void check_timeouts(void)
{ {
/* nothing to do */ /* nothing to do */
@ -82,6 +80,7 @@ void check_timeouts(void)
} }
} }
/*TODO: move to queues.c */
void update_lists() void update_lists()
{ {
int limit; int limit;
@ -128,47 +127,12 @@ void update_lists()
} }
} }
void move_all_to_history()
{
while (displayed->length > 0) {
notification_close(g_queue_peek_head_link(displayed)->data, 2);
}
while (queue->length > 0) {
notification_close(g_queue_peek_head_link(queue)->data, 2);
}
}
void history_pop(void)
{
if (g_queue_is_empty(history))
return;
notification *n = g_queue_pop_tail(history);
n->redisplayed = true;
n->start = 0;
n->timeout = settings.sticky_history ? 0 : n->timeout;
g_queue_push_head(queue, n);
wake_up();
}
void history_push(notification *n)
{
if (settings.history_length > 0 && history->length >= settings.history_length) {
notification *to_free = g_queue_pop_head(history);
notification_free(to_free);
}
if (!n->history_ignore)
g_queue_push_tail(history, n);
}
void wake_up(void) void wake_up(void)
{ {
run(NULL); run(NULL);
} }
/* TODO: move parts to queue.c */
static gint64 get_sleep_time(void) static gint64 get_sleep_time(void)
{ {
gint64 time = g_get_monotonic_time(); gint64 time = g_get_monotonic_time();
@ -264,19 +228,11 @@ gboolean quit_signal(gpointer data)
return G_SOURCE_CONTINUE; return G_SOURCE_CONTINUE;
} }
static void teardown_notification(gpointer data)
{
notification *n = data;
notification_free(n);
}
static void teardown(void) static void teardown(void)
{ {
regex_teardown(); regex_teardown();
g_queue_free_full(history, teardown_notification); teardown_queues();
g_queue_free_full(displayed, teardown_notification);
g_queue_free_full(queue, teardown_notification);
x_free(); x_free();
} }
@ -284,6 +240,7 @@ static void teardown(void)
int dunst_main(int argc, char *argv[]) int dunst_main(int argc, char *argv[])
{ {
/*TODO: move to queues.c */
history = g_queue_new(); history = g_queue_new();
displayed = g_queue_new(); displayed = g_queue_new();
queue = g_queue_new(); queue = g_queue_new();

View File

@ -16,9 +16,6 @@
#define ColFG 1 #define ColFG 1
#define ColBG 0 #define ColBG 0
extern GQueue *queue;
extern GQueue *displayed;
extern GQueue *history;
extern GSList *rules; extern GSList *rules;
extern bool pause_display; extern bool pause_display;
extern const char *color_strings[3][3]; extern const char *color_strings[3][3];
@ -30,10 +27,7 @@ void wake_up(void);
int dunst_main(int argc, char *argv[]); int dunst_main(int argc, char *argv[]);
void check_timeouts(void); void check_timeouts(void);
void history_pop(void);
void history_push(notification *n);
void usage(int exit_status); void usage(int exit_status);
void move_all_to_history(void);
void print_version(void); void print_version(void);
char *extract_urls(const char *str); char *extract_urls(const char *str);
void context_menu(void); void context_menu(void);

View File

@ -16,6 +16,7 @@
#include "dunst.h" #include "dunst.h"
#include "settings.h" #include "settings.h"
#include "notification.h" #include "notification.h"
#include "queues.h"
#include "utils.h" #include "utils.h"
static bool is_initialized = false; static bool is_initialized = false;

View File

@ -19,6 +19,7 @@
#include "menu.h" #include "menu.h"
#include "rules.h" #include "rules.h"
#include "settings.h" #include "settings.h"
#include "queues.h"
#include "utils.h" #include "utils.h"
#include "x11/x.h" #include "x11/x.h"
@ -430,6 +431,7 @@ int notification_init(notification *n, int id)
n->dup_count = 0; n->dup_count = 0;
/* TODO: move this to queue.c */
/* check if n is a duplicate */ /* check if n is a duplicate */
if (settings.stack_duplicates) { if (settings.stack_duplicates) {
for (GList *iter = g_queue_peek_head_link(queue); iter; for (GList *iter = g_queue_peek_head_link(queue); iter;
@ -545,97 +547,6 @@ int notification_init(notification *n, int id)
return n->id; return n->id;
} }
/*
* Close the notification that has id.
*
* 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 notification_close_by_id(int id, int reason)
{
notification *target = NULL;
for (GList *iter = g_queue_peek_head_link(displayed); iter;
iter = iter->next) {
notification *n = iter->data;
if (n->id == id) {
g_queue_remove(displayed, n);
history_push(n);
target = n;
break;
}
}
for (GList *iter = g_queue_peek_head_link(queue); iter;
iter = iter->next) {
notification *n = iter->data;
if (n->id == id) {
g_queue_remove(queue, n);
history_push(n);
target = n;
break;
}
}
if (reason > 0 && reason < 4 && target != NULL) {
notification_closed(target, reason);
}
wake_up();
return reason;
}
/*
* Close the given notification. SEE notification_close_by_id.
*/
int notification_close(notification *n, int reason)
{
assert(n != NULL);
return notification_close_by_id(n->id, reason);
}
/*
* Replace the notification which matches the id field of
* the new notification. The given notification is inserted
* right in the same position as the old notification.
*
* Returns true, if a matching notification has been found
* and is replaced. Else false.
*/
bool notification_replace_by_id(notification *new)
{
for (GList *iter = g_queue_peek_head_link(displayed);
iter;
iter = iter->next) {
notification *old = iter->data;
if (old->id == new->id) {
iter->data = new;
new->start = time(NULL);
new->dup_count = old->dup_count;
notification_run_script(new);
history_push(old);
return true;
}
}
for (GList *iter = g_queue_peek_head_link(queue);
iter;
iter = iter->next) {
notification *old = iter->data;
if (old->id == new->id) {
iter->data = new;
new->dup_count = old->dup_count;
history_push(old);
return true;
}
}
return false;
}
void notification_update_text_to_render(notification *n) void notification_update_text_to_render(notification *n)
{ {
g_free(n->text_to_render); g_free(n->text_to_render);

View File

@ -64,13 +64,10 @@ typedef struct _notification {
notification *notification_create(void); notification *notification_create(void);
int notification_init(notification *n, int id); int notification_init(notification *n, int id);
void notification_free(notification *n); void notification_free(notification *n);
int notification_close_by_id(int id, int reason);
bool notification_replace_by_id(notification *n);
int notification_cmp(const void *a, const void *b); int notification_cmp(const void *a, const void *b);
int notification_cmp_data(const void *a, const void *b, void *data); int notification_cmp_data(const void *a, const void *b, void *data);
int notification_is_duplicate(const notification *a, const notification *b); int notification_is_duplicate(const notification *a, const notification *b);
void notification_run_script(notification *n); void notification_run_script(notification *n);
int notification_close(notification *n, int reason);
void notification_print(notification *n); void notification_print(notification *n);
void notification_replace_single_field(char **haystack, char **needle, const char *replacement, enum markup_mode markup_mode); void notification_replace_single_field(char **haystack, char **needle, const char *replacement, enum markup_mode markup_mode);
void notification_update_text_to_render(notification *n); void notification_update_text_to_render(notification *n);

137
src/queues.c Normal file
View File

@ -0,0 +1,137 @@
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#include "queues.h"
#include <assert.h>
#include <glib.h>
#include "dbus.h"
#include "dunst.h"
#include "notification.h"
#include "settings.h"
/* notification lists */
GQueue *queue = NULL; /* all new notifications get into here */
GQueue *displayed = NULL; /* currently displayed notifications */
GQueue *history = NULL; /* history of displayed notifications */
bool notification_replace_by_id(notification *new)
{
for (GList *iter = g_queue_peek_head_link(displayed);
iter;
iter = iter->next) {
notification *old = iter->data;
if (old->id == new->id) {
iter->data = new;
new->start = time(NULL);
new->dup_count = old->dup_count;
notification_run_script(new);
history_push(old);
return true;
}
}
for (GList *iter = g_queue_peek_head_link(queue);
iter;
iter = iter->next) {
notification *old = iter->data;
if (old->id == new->id) {
iter->data = new;
new->dup_count = old->dup_count;
history_push(old);
return true;
}
}
return false;
}
int notification_close_by_id(int id, int reason)
{
notification *target = NULL;
for (GList *iter = g_queue_peek_head_link(displayed); iter;
iter = iter->next) {
notification *n = iter->data;
if (n->id == id) {
g_queue_remove(displayed, n);
history_push(n);
target = n;
break;
}
}
for (GList *iter = g_queue_peek_head_link(queue); iter;
iter = iter->next) {
notification *n = iter->data;
if (n->id == id) {
g_queue_remove(queue, n);
history_push(n);
target = n;
break;
}
}
if (reason > 0 && reason < 4 && target != NULL) {
notification_closed(target, reason);
}
wake_up();
return reason;
}
int notification_close(notification *n, int reason)
{
assert(n != NULL);
return notification_close_by_id(n->id, reason);
}
void move_all_to_history()
{
while (displayed->length > 0) {
notification_close(g_queue_peek_head_link(displayed)->data, 2);
}
while (queue->length > 0) {
notification_close(g_queue_peek_head_link(queue)->data, 2);
}
}
void history_pop(void)
{
if (g_queue_is_empty(history))
return;
notification *n = g_queue_pop_tail(history);
n->redisplayed = true;
n->start = 0;
n->timeout = settings.sticky_history ? 0 : n->timeout;
g_queue_push_head(queue, n);
wake_up();
}
void history_push(notification *n)
{
if (settings.history_length > 0 && history->length >= settings.history_length) {
notification *to_free = g_queue_pop_head(history);
notification_free(to_free);
}
if (!n->history_ignore)
g_queue_push_tail(history, n);
}
static void teardown_notification(gpointer data)
{
notification *n = data;
notification_free(n);
}
void teardown_queues(void)
{
g_queue_free_full(history, teardown_notification);
g_queue_free_full(displayed, teardown_notification);
g_queue_free_full(queue, teardown_notification);
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

49
src/queues.h Normal file
View File

@ -0,0 +1,49 @@
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#ifndef DUNST_QUEUE_H
#define DUNST_QUEUE_H
#include "notification.h"
extern GQueue *queue;
extern GQueue *displayed;
extern GQueue *history;
/*
* Replace the notification which matches the id field of
* the new notification. The given notification is inserted
* right in the same position as the old notification.
*
* Returns true, if a matching notification has been found
* and is replaced. Else false.
*/
bool notification_replace_by_id(notification *new);
/*
* Close the notification that has id.
*
* After closing, call wake_up to remove the notification from UI
*
* 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 notification_close_by_id(int id, int reason);
/* Close the given notification. SEE notification_close_by_id. */
int notification_close(notification *n, int reason);
void history_pop(void);
void history_push(notification *n);
void move_all_to_history(void);
/*
* Remove all notifications from all lists
* and free the notifications
*/
void teardown_queues(void);
#endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -29,6 +29,7 @@
#include "src/markup.h" #include "src/markup.h"
#include "src/notification.h" #include "src/notification.h"
#include "src/settings.h" #include "src/settings.h"
#include "src/queues.h"
#include "src/utils.h" #include "src/utils.h"
#include "screen.h" #include "screen.h"