From 41a08d1931373b89e66172dab8597784990818eb Mon Sep 17 00:00:00 2001 From: chme Date: Mon, 18 May 2015 20:12:18 +0200 Subject: [PATCH] [mpd] rework listener events to use a bit map --- src/httpd_dacp.c | 2 +- src/listener.c | 7 +++++-- src/listener.h | 15 +++++++-------- src/mpd.c | 47 ++++++++++++----------------------------------- 4 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/httpd_dacp.c b/src/httpd_dacp.c index 8fa79076..446436de 100644 --- a/src/httpd_dacp.c +++ b/src/httpd_dacp.c @@ -2428,7 +2428,7 @@ dacp_init(void) event_base_set(evbase_httpd, &updateev); event_add(&updateev, NULL); - listener_add(dacp_playstatus_update_handler); + listener_add(dacp_playstatus_update_handler, LISTENER_PLAYER); return 0; diff --git a/src/listener.c b/src/listener.c index 64ffed2f..0c4a7cec 100644 --- a/src/listener.c +++ b/src/listener.c @@ -27,18 +27,20 @@ struct listener { notify notify_cb; + short events; struct listener *next; }; struct listener *listener_list = NULL; int -listener_add(notify notify_cb) +listener_add(notify notify_cb, short events) { struct listener *listener; listener = (struct listener*)malloc(sizeof(struct listener)); listener->notify_cb = notify_cb; + listener->events = events; listener->next = listener_list; listener_list = listener; @@ -82,7 +84,8 @@ listener_notify(enum listener_event_type type) listener = listener_list; while (listener) { - listener->notify_cb(type); + if (type & listener->events) + listener->notify_cb(type); listener = listener->next; } diff --git a/src/listener.h b/src/listener.h index fb16c81a..7fffe37a 100644 --- a/src/listener.h +++ b/src/listener.h @@ -4,19 +4,18 @@ enum listener_event_type { - LISTENER_NONE = 0, - LISTENER_DATABASE, - LISTENER_PLAYER, - LISTENER_PLAYLIST, - LISTENER_VOLUME, - LISTENER_SPEAKER, - LISTENER_OPTIONS, + LISTENER_PLAYER = (1 << 0), + LISTENER_PLAYLIST = (1 << 1), + LISTENER_VOLUME = (1 << 2), + LISTENER_SPEAKER = (1 << 3), + LISTENER_OPTIONS = (1 << 4), + LISTENER_DATABASE = (1 << 5), }; typedef void (*notify)(enum listener_event_type type); int -listener_add(notify notify_cb); +listener_add(notify notify_cb, short events); int listener_remove(notify notify_cb); diff --git a/src/mpd.c b/src/mpd.c index ac62bc94..30e7d2d4 100644 --- a/src/mpd.c +++ b/src/mpd.c @@ -153,8 +153,7 @@ free_outputs(struct output *outputs) struct idle_client { struct evbuffer *evbuffer; - enum listener_event_type *types; - unsigned int types_len; + short events; struct idle_client *next; }; @@ -640,54 +639,45 @@ mpd_command_idle(struct evbuffer *evbuf, int argc, char **argv, char **errmsg) } client->evbuffer = evbuf; - client->types = NULL; - client->types_len = 0; + client->events = 0; client->next = idle_clients; if (argc > 1) { - client->types_len = argc - 1; - client->types = (enum listener_event_type*)malloc(client->types_len * sizeof(enum listener_event_type)); - - if (!client->types) - { - DPRINTF(E_LOG, L_MPD, "Out of memory for types\n"); - return ACK_ERROR_UNKNOWN; - } - for (i = 1; i < argc; i++) { if (0 == strcmp(argv[i], "database")) { - client->types[i - 1] = LISTENER_DATABASE; + client->events |= LISTENER_DATABASE; } else if (0 == strcmp(argv[i], "player")) { - client->types[i - 1] = LISTENER_PLAYER; + client->events |= LISTENER_PLAYER; } else if (0 == strcmp(argv[i], "playlist")) { - client->types[i - 1] = LISTENER_PLAYLIST; + client->events |= LISTENER_PLAYLIST; } else if (0 == strcmp(argv[i], "mixer")) { - client->types[i - 1] = LISTENER_VOLUME; + client->events |= LISTENER_VOLUME; } else if (0 == strcmp(argv[i], "output")) { - client->types[i - 1] = LISTENER_SPEAKER; + client->events |= LISTENER_SPEAKER; } else if (0 == strcmp(argv[i], "options")) { - client->types[i - 1] = LISTENER_OPTIONS; + client->events |= LISTENER_OPTIONS; } else { DPRINTF(E_DBG, L_MPD, "Idle command for '%s' not supported\n", argv[i]); - client->types[i - 1] = LISTENER_NONE; } } } + else + client->events = LISTENER_PLAYER | LISTENER_PLAYLIST | LISTENER_VOLUME | LISTENER_SPEAKER | LISTENER_OPTIONS; idle_clients = client; @@ -3827,20 +3817,7 @@ mpd_accept_error_cb(struct evconnlistener *listener, void *ctx) static int mpd_notify_idle_client(struct idle_client *client, enum listener_event_type type) { - int i; - int has_type; - - if (client->types_len == 0) - has_type = 1; - else - has_type = 0; - - for (i = 0; !has_type && (i < client->types_len); i++) - { - has_type = (client->types[i] == type); - } - - if (!has_type) + if (!(client->events & type)) { DPRINTF(E_DBG, L_MPD, "Client not listening for event: %d\n", type); return 1; @@ -4095,7 +4072,7 @@ int mpd_init(void) } idle_clients = NULL; - listener_add(mpd_listener_cb); + listener_add(mpd_listener_cb, LISTENER_PLAYER | LISTENER_PLAYLIST | LISTENER_VOLUME | LISTENER_SPEAKER | LISTENER_OPTIONS); return 0;