From ee283c7141158e6a3c1a6996a11caea7821bd580 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Tue, 23 Apr 2024 01:50:11 -0700 Subject: [PATCH] lib: Implement obs-websocket event callback access This allows plugins to listen directly for obs-websocket events. --- lib/obs-websocket-api.h | 55 +++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/obs-websocket-api.h b/lib/obs-websocket-api.h index adc95019..b7a6d5c0 100644 --- a/lib/obs-websocket-api.h +++ b/lib/obs-websocket-api.h @@ -22,7 +22,7 @@ with this program. If not, see #include -#define OBS_WEBSOCKET_API_VERSION 2 +#define OBS_WEBSOCKET_API_VERSION 3 #ifdef __cplusplus extern "C" { @@ -30,6 +30,7 @@ extern "C" { typedef void *obs_websocket_vendor; typedef void (*obs_websocket_request_callback_function)(obs_data_t *, obs_data_t *, void *); +typedef void (*obs_websocket_event_callback_function)(uint64_t, const char *, const char *, void *); struct obs_websocket_request_response { unsigned int status_code; @@ -44,6 +45,11 @@ struct obs_websocket_request_callback { void *priv_data; }; +struct obs_websocket_event_callback { + obs_websocket_event_callback_function callback; + void *priv_data; +}; + static proc_handler_t *_ph; /* ==================== INTERNAL API FUNCTIONS ==================== */ @@ -118,7 +124,6 @@ static inline struct obs_websocket_request_response *obs_websocket_call_request( request_data_string = obs_data_get_json(request_data); calldata_t cd = {0, 0, 0, 0}; - calldata_set_string(&cd, "request_type", request_type); calldata_set_string(&cd, "request_data", request_data_string); @@ -144,6 +149,46 @@ static inline void obs_websocket_request_response_free(struct obs_websocket_requ bfree(response); } +// Register an event handler to receive obs-websocket events +static inline bool obs_websocket_register_event_callback(obs_websocket_event_callback_function event_callback, void *priv_data) +{ + if (!obs_websocket_ensure_ph()) + return false; + + struct obs_websocket_event_callback cb = {event_callback, priv_data}; + + calldata_t cd = {0, 0, 0, 0}; + calldata_set_ptr(&cd, "callback", &cb); + + proc_handler_call(_ph, "register_event_callback", &cd); + + bool ret = calldata_bool(&cd, "success"); + + calldata_free(&cd); + + return ret; +} + +// Unregister an existing event handler +static inline bool obs_websocket_unregister_event_callback(obs_websocket_event_callback_function event_callback, void *priv_data) +{ + if (!obs_websocket_ensure_ph()) + return false; + + struct obs_websocket_event_callback cb = {event_callback, priv_data}; + + calldata_t cd = {0, 0, 0, 0}; + calldata_set_ptr(&cd, "callback", &cb); + + proc_handler_call(_ph, "unregister_event_callback", &cd); + + bool ret = calldata_bool(&cd, "success"); + + calldata_free(&cd); + + return ret; +} + /* ==================== VENDOR API FUNCTIONS ==================== */ // ALWAYS CALL ONLY VIA `obs_module_post_load()` CALLBACK! @@ -154,7 +199,6 @@ static inline obs_websocket_vendor obs_websocket_register_vendor(const char *ven return NULL; calldata_t cd = {0, 0, 0, 0}; - calldata_set_string(&cd, "name", vendor_name); proc_handler_call(_ph, "vendor_register", &cd); @@ -168,10 +212,9 @@ static inline obs_websocket_vendor obs_websocket_register_vendor(const char *ven static inline bool obs_websocket_vendor_register_request(obs_websocket_vendor vendor, const char *request_type, obs_websocket_request_callback_function request_callback, void *priv_data) { - calldata_t cd = {0, 0, 0, 0}; - struct obs_websocket_request_callback cb = {request_callback, priv_data}; + calldata_t cd = {0, 0, 0, 0}; calldata_set_string(&cd, "type", request_type); calldata_set_ptr(&cd, "callback", &cb); @@ -185,7 +228,6 @@ static inline bool obs_websocket_vendor_register_request(obs_websocket_vendor ve static inline bool obs_websocket_vendor_unregister_request(obs_websocket_vendor vendor, const char *request_type) { calldata_t cd = {0, 0, 0, 0}; - calldata_set_string(&cd, "type", request_type); bool success = obs_websocket_vendor_run_simple_proc(vendor, "vendor_request_unregister", &cd); @@ -199,7 +241,6 @@ static inline bool obs_websocket_vendor_unregister_request(obs_websocket_vendor static inline bool obs_websocket_vendor_emit_event(obs_websocket_vendor vendor, const char *event_name, obs_data_t *event_data) { calldata_t cd = {0, 0, 0, 0}; - calldata_set_string(&cd, "type", event_name); calldata_set_ptr(&cd, "data", (void *)event_data);