[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 */
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);
}
@ -1715,7 +1715,7 @@ cache(void *arg)
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_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;

View File

@ -971,7 +971,7 @@ speaker_update_handler_cb(void *arg)
// Thread: player (must not block)
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);
}
@ -1636,7 +1636,7 @@ httpd_init(const char *webroot)
// We need to know about speaker format changes so we can ask the cache to
// 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;

View File

@ -787,7 +787,7 @@ update_fail_cb(void *arg)
/* Thread: player */
static void
dacp_playstatus_update_handler(short event_mask)
dacp_playstatus_update_handler(short event_mask, void *ctx)
{
struct dacp_update_request *ur;
@ -2818,7 +2818,7 @@ dacp_init(void)
CHECK_ERR(L_DACP, mutex_init(&update_request_lck));
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;
}

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.
static void
pipe_listener_cb(short event_mask)
pipe_listener_cb(short event_mask, void *ctx)
{
union pipe_arg *cmdarg;
@ -1146,8 +1146,8 @@ init(void)
pipe_autostart = cfg_getbool(cfg_getsec(cfg, "library"), "pipe_autostart");
if (pipe_autostart)
{
pipe_listener_cb(0);
CHECK_ERR(L_PLAYER, listener_add(pipe_listener_cb, LISTENER_DATABASE));
pipe_listener_cb(0, NULL);
CHECK_ERR(L_PLAYER, listener_add(pipe_listener_cb, LISTENER_DATABASE, NULL));
}
pipe_sample_rate = cfg_getint(cfg_getsec(cfg, "library"), "pipe_sample_rate");

View File

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

View File

@ -30,7 +30,7 @@ enum listener_event_type
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.
@ -39,10 +39,11 @@ typedef void (*notify)(short event_mask);
* @param notify_cb Callback function (should be a non-blocking function,
* especially when the event is from the player)
* @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
*/
int
listener_add(notify notify_cb, short event_mask);
listener_add(notify notify_cb, short event_mask, void *ctx);
/*
* Removes the given callback function

View File

@ -4117,7 +4117,7 @@ mpd_notify_idle(void *arg, int *retval)
}
static void
mpd_listener_cb(short event_mask)
mpd_listener_cb(short event_mask, void *ctx)
{
short *ptr;
@ -4381,7 +4381,7 @@ mpd_init(void)
thread_setname(tid_mpd, "mpd");
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;

View File

@ -50,7 +50,7 @@ static short websocket_write_events;
/* Thread: library (the thread the event occurred) */
static void
listener_cb(short event_mask)
listener_cb(short event_mask, void *ctx)
{
pthread_mutex_lock(&websocket_write_event_lock);
websocket_write_events |= event_mask;
@ -416,7 +416,7 @@ static void *
websocket(void *arg)
{
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)
{