Improve Windows README.
[jack2.git] / dbus / reserve.c
blob88e91b2bec14dd20108aaecd96eff7ec9033915c
1 /***
2 Copyright 2009 Lennart Poettering
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 ***/
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <assert.h>
31 #include <stdint.h>
33 #include "reserve.h"
35 struct rd_device {
36 int ref;
38 char *device_name;
39 char *application_name;
40 char *application_device_name;
41 char *service_name;
42 char *object_path;
43 int32_t priority;
45 DBusConnection *connection;
47 int owning:1;
48 int registered:1;
49 int filtering:1;
50 int gave_up:1;
52 rd_request_cb_t request_cb;
53 void *userdata;
57 #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
58 #define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
60 static const char introspection[] =
61 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
62 "<node>"
63 " <!-- If you are looking for documentation make sure to check out\n"
64 " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
65 " <interface name=\"org.freedesktop.ReserveDevice1\">"
66 " <method name=\"RequestRelease\">"
67 " <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
68 " <arg name=\"result\" type=\"b\" direction=\"out\"/>"
69 " </method>"
70 " <property name=\"Priority\" type=\"i\" access=\"read\"/>"
71 " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
72 " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
73 " </interface>"
74 " <interface name=\"org.freedesktop.DBus.Properties\">"
75 " <method name=\"Get\">"
76 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
77 " <arg name=\"property\" direction=\"in\" type=\"s\"/>"
78 " <arg name=\"value\" direction=\"out\" type=\"v\"/>"
79 " </method>"
80 " </interface>"
81 " <interface name=\"org.freedesktop.DBus.Introspectable\">"
82 " <method name=\"Introspect\">"
83 " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
84 " </method>"
85 " </interface>"
86 "</node>";
88 static dbus_bool_t add_variant(
89 DBusMessage *m,
90 int type,
91 const void *data) {
93 DBusMessageIter iter, sub;
94 char t[2];
96 t[0] = (char) type;
97 t[1] = 0;
99 dbus_message_iter_init_append(m, &iter);
101 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub))
102 return FALSE;
104 if (!dbus_message_iter_append_basic(&sub, type, data))
105 return FALSE;
107 if (!dbus_message_iter_close_container(&iter, &sub))
108 return FALSE;
110 return TRUE;
113 static DBusHandlerResult object_handler(
114 DBusConnection *c,
115 DBusMessage *m,
116 void *userdata) {
118 rd_device *d;
119 DBusError error;
120 DBusMessage *reply = NULL;
122 dbus_error_init(&error);
124 d = (rd_device*)userdata;
125 assert(d->ref >= 1);
127 if (dbus_message_is_method_call(
129 "org.freedesktop.ReserveDevice1",
130 "RequestRelease")) {
132 int32_t priority;
133 dbus_bool_t ret;
135 if (!dbus_message_get_args(
137 &error,
138 DBUS_TYPE_INT32, &priority,
139 DBUS_TYPE_INVALID))
140 goto invalid;
142 ret = FALSE;
144 if (priority > d->priority && d->request_cb) {
145 d->ref++;
147 if (d->request_cb(d, 0) > 0) {
148 ret = TRUE;
149 d->gave_up = 1;
152 rd_release(d);
155 if (!(reply = dbus_message_new_method_return(m)))
156 goto oom;
158 if (!dbus_message_append_args(
159 reply,
160 DBUS_TYPE_BOOLEAN, &ret,
161 DBUS_TYPE_INVALID))
162 goto oom;
164 if (!dbus_connection_send(c, reply, NULL))
165 goto oom;
167 dbus_message_unref(reply);
169 return DBUS_HANDLER_RESULT_HANDLED;
171 } else if (dbus_message_is_method_call(
173 "org.freedesktop.DBus.Properties",
174 "Get")) {
176 const char *interface, *property;
178 if (!dbus_message_get_args(
180 &error,
181 DBUS_TYPE_STRING, &interface,
182 DBUS_TYPE_STRING, &property,
183 DBUS_TYPE_INVALID))
184 goto invalid;
186 if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) {
187 const char *empty = "";
189 if (strcmp(property, "ApplicationName") == 0 && d->application_name) {
190 if (!(reply = dbus_message_new_method_return(m)))
191 goto oom;
193 if (!add_variant(
194 reply,
195 DBUS_TYPE_STRING,
196 d->application_name ? (const char**) &d->application_name : &empty))
197 goto oom;
199 } else if (strcmp(property, "ApplicationDeviceName") == 0) {
200 if (!(reply = dbus_message_new_method_return(m)))
201 goto oom;
203 if (!add_variant(
204 reply,
205 DBUS_TYPE_STRING,
206 d->application_device_name ? (const char**) &d->application_device_name : &empty))
207 goto oom;
209 } else if (strcmp(property, "Priority") == 0) {
210 if (!(reply = dbus_message_new_method_return(m)))
211 goto oom;
213 if (!add_variant(
214 reply,
215 DBUS_TYPE_INT32,
216 &d->priority))
217 goto oom;
218 } else {
219 if (!(reply = dbus_message_new_error_printf(
221 DBUS_ERROR_UNKNOWN_METHOD,
222 "Unknown property %s",
223 property)))
224 goto oom;
227 if (!dbus_connection_send(c, reply, NULL))
228 goto oom;
230 dbus_message_unref(reply);
232 return DBUS_HANDLER_RESULT_HANDLED;
235 } else if (dbus_message_is_method_call(
237 "org.freedesktop.DBus.Introspectable",
238 "Introspect")) {
239 const char *i = introspection;
241 if (!(reply = dbus_message_new_method_return(m)))
242 goto oom;
244 if (!dbus_message_append_args(
245 reply,
246 DBUS_TYPE_STRING,
248 DBUS_TYPE_INVALID))
249 goto oom;
251 if (!dbus_connection_send(c, reply, NULL))
252 goto oom;
254 dbus_message_unref(reply);
256 return DBUS_HANDLER_RESULT_HANDLED;
259 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
261 invalid:
262 if (reply)
263 dbus_message_unref(reply);
265 if (!(reply = dbus_message_new_error(
267 DBUS_ERROR_INVALID_ARGS,
268 "Invalid arguments")))
269 goto oom;
271 if (!dbus_connection_send(c, reply, NULL))
272 goto oom;
274 dbus_message_unref(reply);
276 dbus_error_free(&error);
278 return DBUS_HANDLER_RESULT_HANDLED;
280 oom:
281 if (reply)
282 dbus_message_unref(reply);
284 dbus_error_free(&error);
286 return DBUS_HANDLER_RESULT_NEED_MEMORY;
289 static DBusHandlerResult filter_handler(
290 DBusConnection *c,
291 DBusMessage *m,
292 void *userdata) {
294 DBusMessage *reply;
295 rd_device *d;
296 DBusError error;
298 dbus_error_init(&error);
300 d = (rd_device*)userdata;
302 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
303 const char *name;
305 if (!dbus_message_get_args(
307 &error,
308 DBUS_TYPE_STRING, &name,
309 DBUS_TYPE_INVALID))
310 goto invalid;
312 if (strcmp(name, d->service_name) == 0 && d->owning) {
313 d->owning = 0;
315 if (!d->gave_up) {
316 d->ref++;
318 if (d->request_cb)
319 d->request_cb(d, 1);
320 d->gave_up = 1;
322 rd_release(d);
325 return DBUS_HANDLER_RESULT_HANDLED;
329 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
331 invalid:
332 if (!(reply = dbus_message_new_error(
334 DBUS_ERROR_INVALID_ARGS,
335 "Invalid arguments")))
336 goto oom;
338 if (!dbus_connection_send(c, reply, NULL))
339 goto oom;
341 dbus_message_unref(reply);
343 dbus_error_free(&error);
345 return DBUS_HANDLER_RESULT_HANDLED;
347 oom:
348 if (reply)
349 dbus_message_unref(reply);
351 dbus_error_free(&error);
353 return DBUS_HANDLER_RESULT_NEED_MEMORY;
356 static DBusObjectPathVTable vtable;
358 int rd_acquire(
359 rd_device **_d,
360 DBusConnection *connection,
361 const char *device_name,
362 const char *application_name,
363 int32_t priority,
364 rd_request_cb_t request_cb,
365 DBusError *error) {
367 rd_device *d = NULL;
368 int r, k;
369 DBusError _error;
370 DBusMessage *m = NULL, *reply = NULL;
371 dbus_bool_t good;
372 vtable.message_function = object_handler;
374 if (!error)
375 error = &_error;
377 dbus_error_init(error);
379 if (!_d)
380 return -EINVAL;
382 if (!connection)
383 return -EINVAL;
385 if (!device_name)
386 return -EINVAL;
388 if (!request_cb && priority != INT32_MAX)
389 return -EINVAL;
391 if (!(d = (rd_device *)calloc(sizeof(rd_device), 1)))
392 return -ENOMEM;
394 d->ref = 1;
396 if (!(d->device_name = strdup(device_name))) {
397 r = -ENOMEM;
398 goto fail;
401 if (!(d->application_name = strdup(application_name))) {
402 r = -ENOMEM;
403 goto fail;
406 d->priority = priority;
407 d->connection = dbus_connection_ref(connection);
408 d->request_cb = request_cb;
410 if (!(d->service_name = (char*)malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
411 r = -ENOMEM;
412 goto fail;
414 sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
416 if (!(d->object_path = (char*)malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
417 r = -ENOMEM;
418 goto fail;
420 sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
422 if ((k = dbus_bus_request_name(
423 d->connection,
424 d->service_name,
425 DBUS_NAME_FLAG_DO_NOT_QUEUE|
426 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
427 error)) < 0) {
428 r = -EIO;
429 goto fail;
432 if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER || k == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER)
433 goto success;
435 if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) {
436 r = -EIO;
437 goto fail;
440 if (priority <= INT32_MIN) {
441 r = -EBUSY;
442 goto fail;
445 if (!(m = dbus_message_new_method_call(
446 d->service_name,
447 d->object_path,
448 "org.freedesktop.ReserveDevice1",
449 "RequestRelease"))) {
450 r = -ENOMEM;
451 goto fail;
454 if (!dbus_message_append_args(
456 DBUS_TYPE_INT32, &d->priority,
457 DBUS_TYPE_INVALID)) {
458 r = -ENOMEM;
459 goto fail;
462 if (!(reply = dbus_connection_send_with_reply_and_block(
463 d->connection,
465 5000, /* 5s */
466 error))) {
468 if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) ||
469 dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) ||
470 dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) {
471 /* This must be treated as denied. */
472 r = -EBUSY;
473 goto fail;
476 r = -EIO;
477 goto fail;
480 if (!dbus_message_get_args(
481 reply,
482 error,
483 DBUS_TYPE_BOOLEAN, &good,
484 DBUS_TYPE_INVALID)) {
485 r = -EIO;
486 goto fail;
489 if (!good) {
490 r = -EBUSY;
491 goto fail;
494 if ((k = dbus_bus_request_name(
495 d->connection,
496 d->service_name,
497 DBUS_NAME_FLAG_DO_NOT_QUEUE|
498 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)|
499 DBUS_NAME_FLAG_REPLACE_EXISTING,
500 error)) < 0) {
501 r = -EIO;
502 goto fail;
505 if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
506 r = -EIO;
507 goto fail;
510 success:
511 d->owning = 1;
513 if (!(dbus_connection_register_object_path(
514 d->connection,
515 d->object_path,
516 &vtable,
517 d))) {
518 r = -ENOMEM;
519 goto fail;
522 d->registered = 1;
524 if (!dbus_connection_add_filter(
525 d->connection,
526 filter_handler,
528 NULL)) {
529 r = -ENOMEM;
530 goto fail;
533 d->filtering = 1;
535 *_d = d;
536 return 0;
538 fail:
539 if (m)
540 dbus_message_unref(m);
542 if (reply)
543 dbus_message_unref(reply);
545 if (&_error == error)
546 dbus_error_free(&_error);
548 if (d)
549 rd_release(d);
551 return r;
554 void rd_release(
555 rd_device *d) {
557 if (!d)
558 return;
560 assert(d->ref > 0);
562 if (--d->ref)
563 return;
566 if (d->filtering)
567 dbus_connection_remove_filter(
568 d->connection,
569 filter_handler,
572 if (d->registered)
573 dbus_connection_unregister_object_path(
574 d->connection,
575 d->object_path);
577 if (d->owning) {
578 DBusError error;
579 dbus_error_init(&error);
581 dbus_bus_release_name(
582 d->connection,
583 d->service_name,
584 &error);
586 dbus_error_free(&error);
589 free(d->device_name);
590 free(d->application_name);
591 free(d->application_device_name);
592 free(d->service_name);
593 free(d->object_path);
595 if (d->connection)
596 dbus_connection_unref(d->connection);
598 free(d);
601 int rd_set_application_device_name(rd_device *d, const char *n) {
602 char *t;
604 if (!d)
605 return -EINVAL;
607 assert(d->ref > 0);
609 if (!(t = strdup(n)))
610 return -ENOMEM;
612 free(d->application_device_name);
613 d->application_device_name = t;
614 return 0;
617 void rd_set_userdata(rd_device *d, void *userdata) {
619 if (!d)
620 return;
622 assert(d->ref > 0);
623 d->userdata = userdata;
626 void* rd_get_userdata(rd_device *d) {
628 if (!d)
629 return NULL;
631 assert(d->ref > 0);
633 return d->userdata;