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
38 char *application_name;
39 char *application_device_name;
44 DBusConnection *connection;
47 unsigned registered:1;
51 rd_request_cb_t request_cb;
55 #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
56 #define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/"
58 static const char introspection[] =
59 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
61 " <!-- If you are looking for documentation make sure to check out\n"
62 " http://git.0pointer.de/?p=reserve.git;a=blob;f=reserve.txt -->\n"
63 " <interface name=\"org.freedesktop.ReserveDevice1\">"
64 " <method name=\"RequestRelease\">"
65 " <arg name=\"priority\" type=\"i\" direction=\"in\"/>"
66 " <arg name=\"result\" type=\"b\" direction=\"out\"/>"
68 " <property name=\"Priority\" type=\"i\" access=\"read\"/>"
69 " <property name=\"ApplicationName\" type=\"s\" access=\"read\"/>"
70 " <property name=\"ApplicationDeviceName\" type=\"s\" access=\"read\"/>"
72 " <interface name=\"org.freedesktop.DBus.Properties\">"
73 " <method name=\"Get\">"
74 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>"
75 " <arg name=\"property\" direction=\"in\" type=\"s\"/>"
76 " <arg name=\"value\" direction=\"out\" type=\"v\"/>"
79 " <interface name=\"org.freedesktop.DBus.Introspectable\">"
80 " <method name=\"Introspect\">"
81 " <arg name=\"data\" type=\"s\" direction=\"out\"/>"
86 static dbus_bool_t add_variant(
91 DBusMessageIter iter, sub;
97 dbus_message_iter_init_append(m, &iter);
99 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, t, &sub))
102 if (!dbus_message_iter_append_basic(&sub, type, data))
105 if (!dbus_message_iter_close_container(&iter, &sub))
111 static DBusHandlerResult object_handler(
118 DBusMessage *reply = NULL;
120 dbus_error_init(&error);
125 if (dbus_message_is_method_call(
127 "org.freedesktop.ReserveDevice1",
133 if (!dbus_message_get_args(
136 DBUS_TYPE_INT32, &priority,
142 if (priority > d->priority && d->request_cb) {
145 if (d->request_cb(d, 0) > 0) {
153 if (!(reply = dbus_message_new_method_return(m)))
156 if (!dbus_message_append_args(
158 DBUS_TYPE_BOOLEAN, &ret,
162 if (!dbus_connection_send(c, reply, NULL))
165 dbus_message_unref(reply);
167 return DBUS_HANDLER_RESULT_HANDLED;
169 } else if (dbus_message_is_method_call(
171 "org.freedesktop.DBus.Properties",
174 const char *interface, *property;
176 if (!dbus_message_get_args(
179 DBUS_TYPE_STRING, &interface,
180 DBUS_TYPE_STRING, &property,
184 if (strcmp(interface, "org.freedesktop.ReserveDevice1") == 0) {
185 const char *empty = "";
187 if (strcmp(property, "ApplicationName") == 0 && d->application_name) {
188 if (!(reply = dbus_message_new_method_return(m)))
194 d->application_name ? (const char**) &d->application_name : &empty))
197 } else if (strcmp(property, "ApplicationDeviceName") == 0) {
198 if (!(reply = dbus_message_new_method_return(m)))
204 d->application_device_name ? (const char**) &d->application_device_name : &empty))
207 } else if (strcmp(property, "Priority") == 0) {
208 if (!(reply = dbus_message_new_method_return(m)))
217 if (!(reply = dbus_message_new_error_printf(
219 DBUS_ERROR_UNKNOWN_METHOD,
220 "Unknown property %s",
225 if (!dbus_connection_send(c, reply, NULL))
228 dbus_message_unref(reply);
230 return DBUS_HANDLER_RESULT_HANDLED;
233 } else if (dbus_message_is_method_call(
235 "org.freedesktop.DBus.Introspectable",
237 const char *i = introspection;
239 if (!(reply = dbus_message_new_method_return(m)))
242 if (!dbus_message_append_args(
249 if (!dbus_connection_send(c, reply, NULL))
252 dbus_message_unref(reply);
254 return DBUS_HANDLER_RESULT_HANDLED;
257 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
261 dbus_message_unref(reply);
263 if (!(reply = dbus_message_new_error(
265 DBUS_ERROR_INVALID_ARGS,
266 "Invalid arguments")))
269 if (!dbus_connection_send(c, reply, NULL))
272 dbus_message_unref(reply);
274 dbus_error_free(&error);
276 return DBUS_HANDLER_RESULT_HANDLED;
280 dbus_message_unref(reply);
282 dbus_error_free(&error);
284 return DBUS_HANDLER_RESULT_NEED_MEMORY;
287 static DBusHandlerResult filter_handler(
296 dbus_error_init(&error);
301 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
304 if (!dbus_message_get_args(
307 DBUS_TYPE_STRING, &name,
311 if (strcmp(name, d->service_name) == 0 && d->owning) {
324 return DBUS_HANDLER_RESULT_HANDLED;
328 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
331 if (!(reply = dbus_message_new_error(
333 DBUS_ERROR_INVALID_ARGS,
334 "Invalid arguments")))
337 if (!dbus_connection_send(c, reply, NULL))
340 dbus_message_unref(reply);
342 dbus_error_free(&error);
344 return DBUS_HANDLER_RESULT_HANDLED;
348 dbus_message_unref(reply);
350 dbus_error_free(&error);
352 return DBUS_HANDLER_RESULT_NEED_MEMORY;
356 static const struct DBusObjectPathVTable vtable ={
357 .message_function = object_handler
362 DBusConnection *connection,
363 const char *device_name,
364 const char *application_name,
366 rd_request_cb_t request_cb,
372 DBusMessage *m = NULL, *reply = NULL;
378 dbus_error_init(error);
389 if (!request_cb && priority != INT32_MAX)
392 if (!(d = calloc(sizeof(rd_device), 1)))
397 if (!(d->device_name = strdup(device_name))) {
402 if (!(d->application_name = strdup(application_name))) {
407 d->priority = priority;
408 d->connection = dbus_connection_ref(connection);
409 d->request_cb = request_cb;
411 if (!(d->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) {
415 sprintf(d->service_name, SERVICE_PREFIX "%s", d->device_name);
417 if (!(d->object_path = malloc(sizeof(OBJECT_PREFIX) + strlen(device_name)))) {
421 sprintf(d->object_path, OBJECT_PREFIX "%s", d->device_name);
423 if ((k = dbus_bus_request_name(
426 DBUS_NAME_FLAG_DO_NOT_QUEUE|
427 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0),
433 if (k == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
436 if (k != DBUS_REQUEST_NAME_REPLY_EXISTS) {
441 if (priority <= INT32_MIN) {
446 if (!(m = dbus_message_new_method_call(
449 "org.freedesktop.ReserveDevice1",
450 "RequestRelease"))) {
455 if (!dbus_message_append_args(
457 DBUS_TYPE_INT32, &d->priority,
458 DBUS_TYPE_INVALID)) {
463 if (!(reply = dbus_connection_send_with_reply_and_block(
469 if (dbus_error_has_name(error, DBUS_ERROR_TIMED_OUT) ||
470 dbus_error_has_name(error, DBUS_ERROR_UNKNOWN_METHOD) ||
471 dbus_error_has_name(error, DBUS_ERROR_NO_REPLY)) {
472 /* This must be treated as denied. */
481 if (!dbus_message_get_args(
484 DBUS_TYPE_BOOLEAN, &good,
485 DBUS_TYPE_INVALID)) {
495 if ((k = dbus_bus_request_name(
498 DBUS_NAME_FLAG_DO_NOT_QUEUE|
499 (priority < INT32_MAX ? DBUS_NAME_FLAG_ALLOW_REPLACEMENT : 0)|
500 DBUS_NAME_FLAG_REPLACE_EXISTING,
506 if (k != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
514 if (!(dbus_connection_register_object_path(
525 if (!dbus_connection_add_filter(
541 dbus_message_unref(m);
544 dbus_message_unref(reply);
546 if (&_error == error)
547 dbus_error_free(&_error);
568 dbus_connection_remove_filter(
574 dbus_connection_unregister_object_path(
579 dbus_bus_release_name(
584 free(d->device_name);
585 free(d->application_name);
586 free(d->application_device_name);
587 free(d->service_name);
588 free(d->object_path);
591 dbus_connection_unref(d->connection);
596 int rd_set_application_device_name(rd_device *d, const char *n) {
604 if (!(t = strdup(n)))
607 free(d->application_device_name);
608 d->application_device_name = t;
612 void rd_set_userdata(rd_device *d, void *userdata) {
618 d->userdata = userdata;
621 void* rd_get_userdata(rd_device *d) {