Skip to content

Commit ee283c7

Browse files
committed
lib: Implement obs-websocket event callback access
This allows plugins to listen directly for obs-websocket events.
1 parent 179e197 commit ee283c7

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

lib/obs-websocket-api.h

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ with this program. If not, see <https://www.gnu.org/licenses/>
2222

2323
#include <obs.h>
2424

25-
#define OBS_WEBSOCKET_API_VERSION 2
25+
#define OBS_WEBSOCKET_API_VERSION 3
2626

2727
#ifdef __cplusplus
2828
extern "C" {
2929
#endif
3030

3131
typedef void *obs_websocket_vendor;
3232
typedef void (*obs_websocket_request_callback_function)(obs_data_t *, obs_data_t *, void *);
33+
typedef void (*obs_websocket_event_callback_function)(uint64_t, const char *, const char *, void *);
3334

3435
struct obs_websocket_request_response {
3536
unsigned int status_code;
@@ -44,6 +45,11 @@ struct obs_websocket_request_callback {
4445
void *priv_data;
4546
};
4647

48+
struct obs_websocket_event_callback {
49+
obs_websocket_event_callback_function callback;
50+
void *priv_data;
51+
};
52+
4753
static proc_handler_t *_ph;
4854

4955
/* ==================== INTERNAL API FUNCTIONS ==================== */
@@ -118,7 +124,6 @@ static inline struct obs_websocket_request_response *obs_websocket_call_request(
118124
request_data_string = obs_data_get_json(request_data);
119125

120126
calldata_t cd = {0, 0, 0, 0};
121-
122127
calldata_set_string(&cd, "request_type", request_type);
123128
calldata_set_string(&cd, "request_data", request_data_string);
124129

@@ -144,6 +149,46 @@ static inline void obs_websocket_request_response_free(struct obs_websocket_requ
144149
bfree(response);
145150
}
146151

152+
// Register an event handler to receive obs-websocket events
153+
static inline bool obs_websocket_register_event_callback(obs_websocket_event_callback_function event_callback, void *priv_data)
154+
{
155+
if (!obs_websocket_ensure_ph())
156+
return false;
157+
158+
struct obs_websocket_event_callback cb = {event_callback, priv_data};
159+
160+
calldata_t cd = {0, 0, 0, 0};
161+
calldata_set_ptr(&cd, "callback", &cb);
162+
163+
proc_handler_call(_ph, "register_event_callback", &cd);
164+
165+
bool ret = calldata_bool(&cd, "success");
166+
167+
calldata_free(&cd);
168+
169+
return ret;
170+
}
171+
172+
// Unregister an existing event handler
173+
static inline bool obs_websocket_unregister_event_callback(obs_websocket_event_callback_function event_callback, void *priv_data)
174+
{
175+
if (!obs_websocket_ensure_ph())
176+
return false;
177+
178+
struct obs_websocket_event_callback cb = {event_callback, priv_data};
179+
180+
calldata_t cd = {0, 0, 0, 0};
181+
calldata_set_ptr(&cd, "callback", &cb);
182+
183+
proc_handler_call(_ph, "unregister_event_callback", &cd);
184+
185+
bool ret = calldata_bool(&cd, "success");
186+
187+
calldata_free(&cd);
188+
189+
return ret;
190+
}
191+
147192
/* ==================== VENDOR API FUNCTIONS ==================== */
148193

149194
// 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
154199
return NULL;
155200

156201
calldata_t cd = {0, 0, 0, 0};
157-
158202
calldata_set_string(&cd, "name", vendor_name);
159203

160204
proc_handler_call(_ph, "vendor_register", &cd);
@@ -168,10 +212,9 @@ static inline obs_websocket_vendor obs_websocket_register_vendor(const char *ven
168212
static inline bool obs_websocket_vendor_register_request(obs_websocket_vendor vendor, const char *request_type,
169213
obs_websocket_request_callback_function request_callback, void *priv_data)
170214
{
171-
calldata_t cd = {0, 0, 0, 0};
172-
173215
struct obs_websocket_request_callback cb = {request_callback, priv_data};
174216

217+
calldata_t cd = {0, 0, 0, 0};
175218
calldata_set_string(&cd, "type", request_type);
176219
calldata_set_ptr(&cd, "callback", &cb);
177220

@@ -185,7 +228,6 @@ static inline bool obs_websocket_vendor_register_request(obs_websocket_vendor ve
185228
static inline bool obs_websocket_vendor_unregister_request(obs_websocket_vendor vendor, const char *request_type)
186229
{
187230
calldata_t cd = {0, 0, 0, 0};
188-
189231
calldata_set_string(&cd, "type", request_type);
190232

191233
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
199241
static inline bool obs_websocket_vendor_emit_event(obs_websocket_vendor vendor, const char *event_name, obs_data_t *event_data)
200242
{
201243
calldata_t cd = {0, 0, 0, 0};
202-
203244
calldata_set_string(&cd, "type", event_name);
204245
calldata_set_ptr(&cd, "data", (void *)event_data);
205246

0 commit comments

Comments
 (0)