core: split of FAIL_ON_SUSPEND into KILL_ON_SUSPEND and NO_CREATE_ON_SUSPEND
[pulseaudio-mirror.git] / src / modules / reserve.c
blob5597f177df359e662dd530b1c70ebaa715141a99
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/
3 /***
4 Copyright 2009 Lennart Poettering
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 ***/
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <assert.h>
34 #include "reserve.h"
36 struct rd_device {
37 int ref;
39 char *device_name;
40 char *application_name;
41 char *application_device_name;
42 char *service_name;
43 char *object_path;
44 int32_t priority;
46 DBusConnection *connection;
48 unsigned owning:1;
49 unsigned registered:1;
50 unsigned filtering:1;
51 unsigned gave_up:1;
53 rd_request_cb_t request_cb;
54 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 = 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 = userdata;
301 assert(d->ref >= 1);
303 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
304 const char *name;
306 if (!dbus_message_get_args(
308 &error,
309 DBUS_TYPE_STRING, &name,
310 DBUS_TYPE_INVALID))
311 goto invalid;
313 if (strcmp(name, d->service_name) == 0 && d->owning) {
314 d->owning = 0;
316 if (!d->gave_up) {
317 d->ref++;
319 if (d->request_cb)
320 d->request_cb(d, 1);
321 d->gave_up = 1;
323 rd_release(d);
326 return DBUS_HANDLER_RESULT_HANDLED;
330 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
332 invalid:
333 if (!(reply = dbus_message_new_error(
335 DBUS_ERROR_INVALID_ARGS,
336 "Invalid arguments")))
337 goto oom;
339 if (!dbus_connection_send(c, reply, NULL))
340 goto oom;
342 dbus_message_unref(reply);
344 dbus_error_free(&error);
346 return DBUS_HANDLER_RESULT_HANDLED;
348 oom:
349 if (reply)
350 dbus_message_unref(reply);
352 dbus_error_free(&error);
354 return DBUS_HANDLER_RESULT_NEED_MEMORY;
358 static const struct DBusObjectPathVTable vtable ={
359 .message_function = object_handler
362 int rd_acquire(
363 rd_device **_d,
364 DBusConnection *connection,
365 const char *device_name,
366 const char *application_name,
367 int32_t priority,
368 rd_request_cb_t request_cb,
369 DBusError *error) {
371 rd_device *d = NULL;
372 int r, k;
373 DBusError _error;
374 DBusMessage *m = NULL, *reply = NULL;
375 dbus_bool_t good;
377 if (!error)
378 error = &_error;
380 dbus_error_init(error);
382 if (!_d)
383 return -EINVAL;
385 if (!connection)
386 return -EINVAL;
388 if (!device_name)
389 return -EINVAL;
391 if (!request_cb && priority != INT32_MAX)
392 return -EINVAL;
394 if (!(d = calloc(sizeof(rd_device), 1)))
395 return -ENOMEM;
397 d->ref = 1;
399 if (!(d->device_name = strdup(device_name))) {
400 r = -ENOMEM;
401 goto fail;
404 if (!(d->application_name = strdup(application_name))) {
405 r = -ENOMEM;
406 goto fail;
409 d->priority = priority;
410 d->connection = dbus_connection_ref(connection);
411 d->request_cb = request_cb;
413 if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
414 r = -ENOMEM;
415 goto fail;
417 sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
419 if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
420 r = -ENOMEM;
421 goto fail;
423 sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
425 if ((k = dbus_bus_request_name(
426 d->connection,
427 d->service_name,
428 DBUS_NAME_FLAG_DO_NOT_QUEUE|
429 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
430 error)) < 0) {
431 r = -EIO;
432 goto fail;
435 if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
436 goto success;
438 if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) {
439 r = -EIO;
440 goto fail;
443 if (priority <= INT32_MIN) {
444 r = -EBUSY;
445 goto fail;
448 if (!(m = dbus_message_new_method_call(
449 d->service_name,
450 d->object_path,
451 "org.freedesktop.ReserveDevice1",
452 "RequestRelease"))) {
453 r = -ENOMEM;
454 goto fail;
457 if (!dbus_message_append_args(
459 DBUS_TYPE_INT32, &d->priority,
460 DBUS_TYPE_INVALID)) {
461 r = -ENOMEM;
462 goto fail;
465 if (!(reply = dbus_connection_send_with_reply_and_block(
466 d->connection,
468 5000, /* 5s */
469 error))) {
471 if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) ||
472 dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) ||
473 dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) {
474 /* This must be treated as denied. */
475 r = -EBUSY;
476 goto fail;
479 r = -EIO;
480 goto fail;
483 if (!dbus_message_get_args(
484 reply,
485 error,
486 DBUS_TYPE_BOOLEAN, &good,
487 DBUS_TYPE_INVALID)) {
488 r = -EIO;
489 goto fail;
492 if (!good) {
493 r = -EBUSY;
494 goto fail;
497 if ((k = dbus_bus_request_name(
498 d->connection,
499 d->service_name,
500 DBUS_NAME_FLAG_DO_NOT_QUEUE|
501 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)|
502 DBUS_NAME_FLAG_REPLACE_EXISTING,
503 error)) < 0) {
504 r = -EIO;
505 goto fail;
508 if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
509 r = -EIO;
510 goto fail;
513 success:
514 d->owning = 1;
516 if (!(dbus_connection_register_object_path(
517 d->connection,
518 d->object_path,
519 &vtable,
520 d))) {
521 r = -ENOMEM;
522 goto fail;
525 d->registered = 1;
527 if (!dbus_connection_add_filter(
528 d->connection,
529 filter_handler,
531 NULL)) {
532 r = -ENOMEM;
533 goto fail;
536 d->filtering = 1;
538 *_d = d;
539 return 0;
541 fail:
542 if (m)
543 dbus_message_unref(m);
545 if (reply)
546 dbus_message_unref(reply);
548 if (&_error == error)
549 dbus_error_free(&_error);
551 if (d)
552 rd_release(d);
554 return r;
557 void rd_release(
558 rd_device *d) {
560 if (!d)
561 return;
563 assert(d->ref > 0);
565 if (--d->ref > 0)
566 return;
569 if (d->filtering)
570 dbus_connection_remove_filter(
571 d->connection,
572 filter_handler,
575 if (d->registered)
576 dbus_connection_unregister_object_path(
577 d->connection,
578 d->object_path);
580 if (d->owning)
581 dbus_bus_release_name(
582 d->connection,
583 d->service_name,
584 NULL);
586 free(d->device_name);
587 free(d->application_name);
588 free(d->application_device_name);
589 free(d->service_name);
590 free(d->object_path);
592 if (d->connection)
593 dbus_connection_unref(d->connection);
595 free(d);
598 int rd_set_application_device_name(rd_device *d, const char *n) {
599 char *t;
601 if (!d)
602 return -EINVAL;
604 assert(d->ref > 0);
606 if (!(t = strdup(n)))
607 return -ENOMEM;
609 free(d->application_device_name);
610 d->application_device_name = t;
611 return 0;
614 void rd_set_userdata(rd_device *d, void *userdata) {
616 if (!d)
617 return;
619 assert(d->ref > 0);
620 d->userdata = userdata;
623 void* rd_get_userdata(rd_device *d) {
625 if (!d)
626 return NULL;
628 assert(d->ref > 0);
630 return d->userdata;