Skip to content

Commit

Permalink
[listener] Support passing context arg to listener callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
chme committed Dec 27, 2024
1 parent 07a5e68 commit 27c9224
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/httpd_dacp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/inputs/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
6 changes: 4 additions & 2 deletions src/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/mpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 27c9224

Please sign in to comment.