[listener] Support passing context arg to listener callbacks

This commit is contained in:
Christian Meffert 2024-12-27 10:58:54 +00:00
parent 07a5e6858d
commit 27c9224f69
8 changed files with 20 additions and 17 deletions

View File

@ -1307,7 +1307,7 @@ cache_database_update(void *arg, int *retval)
/* Callback from filescanner thread */ /* Callback from filescanner thread */
static void static void
cache_daap_listener_cb(short event_mask) cache_daap_listener_cb(short event_mask, void *ctx)
{ {
commands_exec_async(cmdbase, cache_database_update, NULL); commands_exec_async(cmdbase, cache_database_update, NULL);
} }
@ -1715,7 +1715,7 @@ cache(void *arg)
for (i = 0; i < ARRAY_SIZE(cache_xcode_jobs); i++) for (i = 0; i < ARRAY_SIZE(cache_xcode_jobs); i++)
CHECK_NULL(L_CACHE, cache_xcode_jobs[i].ev = evtimer_new(evbase_cache, cache_xcode_job_complete_cb, &cache_xcode_jobs[i])); CHECK_NULL(L_CACHE, cache_xcode_jobs[i].ev = evtimer_new(evbase_cache, cache_xcode_job_complete_cb, &cache_xcode_jobs[i]));
CHECK_ERR(L_CACHE, listener_add(cache_daap_listener_cb, LISTENER_DATABASE)); CHECK_ERR(L_CACHE, listener_add(cache_daap_listener_cb, LISTENER_DATABASE, NULL));
cache_is_initialized = 1; cache_is_initialized = 1;

View File

@ -971,7 +971,7 @@ speaker_update_handler_cb(void *arg)
// Thread: player (must not block) // Thread: player (must not block)
static void static void
httpd_speaker_update_handler(short event_mask) httpd_speaker_update_handler(short event_mask, void *ctx)
{ {
worker_execute(speaker_update_handler_cb, NULL, 0, 0); worker_execute(speaker_update_handler_cb, NULL, 0, 0);
} }
@ -1636,7 +1636,7 @@ httpd_init(const char *webroot)
// We need to know about speaker format changes so we can ask the cache to // We need to know about speaker format changes so we can ask the cache to
// start preparing headers for mp4/alac if selected // start preparing headers for mp4/alac if selected
listener_add(httpd_speaker_update_handler, LISTENER_SPEAKER); listener_add(httpd_speaker_update_handler, LISTENER_SPEAKER, NULL);
return 0; return 0;

View File

@ -787,7 +787,7 @@ update_fail_cb(void *arg)
/* Thread: player */ /* Thread: player */
static void static void
dacp_playstatus_update_handler(short event_mask) dacp_playstatus_update_handler(short event_mask, void *ctx)
{ {
struct dacp_update_request *ur; struct dacp_update_request *ur;
@ -2818,7 +2818,7 @@ dacp_init(void)
CHECK_ERR(L_DACP, mutex_init(&update_request_lck)); CHECK_ERR(L_DACP, mutex_init(&update_request_lck));
update_current_rev = 2; update_current_rev = 2;
listener_add(dacp_playstatus_update_handler, LISTENER_PLAYER | LISTENER_VOLUME | LISTENER_QUEUE); listener_add(dacp_playstatus_update_handler, LISTENER_PLAYER | LISTENER_VOLUME | LISTENER_QUEUE, NULL);
return 0; return 0;
} }

View File

@ -999,7 +999,7 @@ pipelist_create(void)
// the pipe thread to watch the pipes. If no pipes in library, it will shut down // the pipe thread to watch the pipes. If no pipes in library, it will shut down
// the pipe thread. // the pipe thread.
static void static void
pipe_listener_cb(short event_mask) pipe_listener_cb(short event_mask, void *ctx)
{ {
union pipe_arg *cmdarg; union pipe_arg *cmdarg;
@ -1146,8 +1146,8 @@ init(void)
pipe_autostart = cfg_getbool(cfg_getsec(cfg, "library"), "pipe_autostart"); pipe_autostart = cfg_getbool(cfg_getsec(cfg, "library"), "pipe_autostart");
if (pipe_autostart) if (pipe_autostart)
{ {
pipe_listener_cb(0); pipe_listener_cb(0, NULL);
CHECK_ERR(L_PLAYER, listener_add(pipe_listener_cb, LISTENER_DATABASE)); CHECK_ERR(L_PLAYER, listener_add(pipe_listener_cb, LISTENER_DATABASE, NULL));
} }
pipe_sample_rate = cfg_getint(cfg_getsec(cfg, "library"), "pipe_sample_rate"); pipe_sample_rate = cfg_getint(cfg_getsec(cfg, "library"), "pipe_sample_rate");

View File

@ -27,13 +27,14 @@ struct listener
{ {
notify notify_cb; notify notify_cb;
short events; short events;
void *ctx;
struct listener *next; struct listener *next;
}; };
struct listener *listener_list = NULL; struct listener *listener_list = NULL;
int int
listener_add(notify notify_cb, short events) listener_add(notify notify_cb, short events, void *ctx)
{ {
struct listener *listener; struct listener *listener;
@ -44,6 +45,7 @@ listener_add(notify notify_cb, short events)
} }
listener->notify_cb = notify_cb; listener->notify_cb = notify_cb;
listener->events = events; listener->events = events;
listener->ctx = ctx;
listener->next = listener_list; listener->next = listener_list;
listener_list = listener; listener_list = listener;
@ -88,7 +90,7 @@ listener_notify(short event_mask)
while (listener) while (listener)
{ {
if (event_mask & listener->events) if (event_mask & listener->events)
listener->notify_cb(event_mask & listener->events); listener->notify_cb(event_mask & listener->events, listener->ctx);
listener = listener->next; listener = listener->next;
} }
} }

View File

@ -30,7 +30,7 @@ enum listener_event_type
LISTENER_RATING = (1 << 11), LISTENER_RATING = (1 << 11),
}; };
typedef void (*notify)(short event_mask); typedef void (*notify)(short event_mask, void *ctx);
/* /*
* Registers the given callback function to the given event types. * Registers the given callback function to the given event types.
@ -39,10 +39,11 @@ typedef void (*notify)(short event_mask);
* @param notify_cb Callback function (should be a non-blocking function, * @param notify_cb Callback function (should be a non-blocking function,
* especially when the event is from the player) * especially when the event is from the player)
* @param event_mask Event mask, one or more of LISTENER_* * @param event_mask Event mask, one or more of LISTENER_*
* @param ctx Context will be passed to the notify callback
* @return 0 on success, -1 on failure * @return 0 on success, -1 on failure
*/ */
int int
listener_add(notify notify_cb, short event_mask); listener_add(notify notify_cb, short event_mask, void *ctx);
/* /*
* Removes the given callback function * Removes the given callback function

View File

@ -4117,7 +4117,7 @@ mpd_notify_idle(void *arg, int *retval)
} }
static void static void
mpd_listener_cb(short event_mask) mpd_listener_cb(short event_mask, void *ctx)
{ {
short *ptr; short *ptr;
@ -4381,7 +4381,7 @@ mpd_init(void)
thread_setname(tid_mpd, "mpd"); thread_setname(tid_mpd, "mpd");
mpd_clients = NULL; mpd_clients = NULL;
listener_add(mpd_listener_cb, MPD_ALL_IDLE_LISTENER_EVENTS); listener_add(mpd_listener_cb, MPD_ALL_IDLE_LISTENER_EVENTS, NULL);
return 0; return 0;

View File

@ -50,7 +50,7 @@ static short websocket_write_events;
/* Thread: library (the thread the event occurred) */ /* Thread: library (the thread the event occurred) */
static void static void
listener_cb(short event_mask) listener_cb(short event_mask, void *ctx)
{ {
pthread_mutex_lock(&websocket_write_event_lock); pthread_mutex_lock(&websocket_write_event_lock);
websocket_write_events |= event_mask; websocket_write_events |= event_mask;
@ -416,7 +416,7 @@ static void *
websocket(void *arg) websocket(void *arg)
{ {
listener_add(listener_cb, LISTENER_UPDATE | LISTENER_DATABASE | LISTENER_PAIRING | LISTENER_SPOTIFY | LISTENER_LASTFM | LISTENER_SPEAKER listener_add(listener_cb, LISTENER_UPDATE | LISTENER_DATABASE | LISTENER_PAIRING | LISTENER_SPOTIFY | LISTENER_LASTFM | LISTENER_SPEAKER
| LISTENER_PLAYER | LISTENER_OPTIONS | LISTENER_VOLUME | LISTENER_QUEUE); | LISTENER_PLAYER | LISTENER_OPTIONS | LISTENER_VOLUME | LISTENER_QUEUE, NULL);
while(!websocket_exit) while(!websocket_exit)
{ {