diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/idevice.c | 59 |
1 files changed, 49 insertions, 10 deletions
diff --git a/src/idevice.c b/src/idevice.c index 22d57e3..c8574fc 100644 --- a/src/idevice.c +++ b/src/idevice.c | |||
@@ -215,10 +215,17 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved) | |||
215 | #warning No compiler support for constructor/destructor attributes, some features might not be available. | 215 | #warning No compiler support for constructor/destructor attributes, some features might not be available. |
216 | #endif | 216 | #endif |
217 | 217 | ||
218 | static idevice_event_cb_t event_cb = NULL; | 218 | struct idevice_subscription_context { |
219 | idevice_event_cb_t callback; | ||
220 | void *user_data; | ||
221 | usbmuxd_subscription_context_t ctx; | ||
222 | }; | ||
223 | |||
224 | static idevice_subscription_context_t event_ctx = NULL; | ||
219 | 225 | ||
220 | static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) | 226 | static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) |
221 | { | 227 | { |
228 | idevice_subscription_context_t context = (idevice_subscription_context_t)user_data; | ||
222 | idevice_event_t ev; | 229 | idevice_event_t ev; |
223 | 230 | ||
224 | ev.event = event->event; | 231 | ev.event = event->event; |
@@ -232,34 +239,66 @@ static void usbmux_event_cb(const usbmuxd_event_t *event, void *user_data) | |||
232 | debug_info("Unknown connection type %d", event->device.conn_type); | 239 | debug_info("Unknown connection type %d", event->device.conn_type); |
233 | } | 240 | } |
234 | 241 | ||
235 | if (event_cb) { | 242 | if (context->callback) { |
236 | event_cb(&ev, user_data); | 243 | context->callback(&ev, context->user_data); |
237 | } | 244 | } |
238 | } | 245 | } |
239 | 246 | ||
240 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data) | 247 | LIBIMOBILEDEVICE_API idevice_error_t idevice_events_subscribe(idevice_subscription_context_t *context, idevice_event_cb_t callback, void *user_data) |
241 | { | 248 | { |
242 | event_cb = callback; | 249 | if (!context || !callback) { |
243 | int res = usbmuxd_subscribe(usbmux_event_cb, user_data); | 250 | return IDEVICE_E_INVALID_ARG; |
251 | } | ||
252 | *context = malloc(sizeof(struct idevice_subscription_context)); | ||
253 | if (!*context) { | ||
254 | debug_info("ERROR: %s: Failed to allocate subscription context\n", __func__); | ||
255 | return IDEVICE_E_UNKNOWN_ERROR; | ||
256 | } | ||
257 | (*context)->callback = callback; | ||
258 | (*context)->user_data = user_data; | ||
259 | int res = usbmuxd_events_subscribe(&(*context)->ctx, usbmux_event_cb, *context); | ||
244 | if (res != 0) { | 260 | if (res != 0) { |
245 | event_cb = NULL; | 261 | free(*context); |
262 | *context = NULL; | ||
246 | debug_info("ERROR: usbmuxd_subscribe() returned %d!", res); | 263 | debug_info("ERROR: usbmuxd_subscribe() returned %d!", res); |
247 | return IDEVICE_E_UNKNOWN_ERROR; | 264 | return IDEVICE_E_UNKNOWN_ERROR; |
248 | } | 265 | } |
249 | return IDEVICE_E_SUCCESS; | 266 | return IDEVICE_E_SUCCESS; |
250 | } | 267 | } |
251 | 268 | ||
252 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_unsubscribe(void) | 269 | LIBIMOBILEDEVICE_API idevice_error_t idevice_events_unsubscribe(idevice_subscription_context_t context) |
253 | { | 270 | { |
254 | event_cb = NULL; | 271 | if (!context) { |
255 | int res = usbmuxd_unsubscribe(); | 272 | return IDEVICE_E_INVALID_ARG; |
273 | } | ||
274 | int res = usbmuxd_events_unsubscribe(context->ctx); | ||
256 | if (res != 0) { | 275 | if (res != 0) { |
257 | debug_info("ERROR: usbmuxd_unsubscribe() returned %d!", res); | 276 | debug_info("ERROR: usbmuxd_unsubscribe() returned %d!", res); |
258 | return IDEVICE_E_UNKNOWN_ERROR; | 277 | return IDEVICE_E_UNKNOWN_ERROR; |
259 | } | 278 | } |
279 | if (context == event_ctx) { | ||
280 | event_ctx = NULL; | ||
281 | } | ||
282 | free(context); | ||
260 | return IDEVICE_E_SUCCESS; | 283 | return IDEVICE_E_SUCCESS; |
261 | } | 284 | } |
262 | 285 | ||
286 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_subscribe(idevice_event_cb_t callback, void *user_data) | ||
287 | { | ||
288 | if (event_ctx) { | ||
289 | idevice_events_unsubscribe(event_ctx); | ||
290 | } | ||
291 | return idevice_events_subscribe(&event_ctx, callback, user_data); | ||
292 | } | ||
293 | |||
294 | LIBIMOBILEDEVICE_API idevice_error_t idevice_event_unsubscribe(void) | ||
295 | { | ||
296 | if (!event_ctx) { | ||
297 | return IDEVICE_E_SUCCESS; | ||
298 | } | ||
299 | return idevice_events_unsubscribe(event_ctx); | ||
300 | } | ||
301 | |||
263 | LIBIMOBILEDEVICE_API idevice_error_t idevice_get_device_list_extended(idevice_info_t **devices, int *count) | 302 | LIBIMOBILEDEVICE_API idevice_error_t idevice_get_device_list_extended(idevice_info_t **devices, int *count) |
264 | { | 303 | { |
265 | usbmuxd_device_info_t *dev_list; | 304 | usbmuxd_device_info_t *dev_list; |