Debian Codegraph @

examples/cd-dbus-create-device.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <dbus/dbus.h>
23 #include <glib.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static void
29 helper_dict_add_property (DBusMessageIter *dict,
30 const char *key,
31 const char *value)
32 {
33
34 DBusMessageIter entry;
35 dbus_message_iter_open_container(dict,
36 DBUS_TYPE_DICT_ENTRY,
37 NULL,
38 &entry);
39 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
40 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
41 dbus_message_iter_close_container(dict, &entry);
42 }
43
44 int
45 main (int argc, char **argv)
46 {
47 const char *device_id;
48 const char *device_path_tmp;
49 const gchar *scope = "temp";
50 DBusConnection *con;
51 DBusError error;
52 DBusMessageIter args;
53 DBusMessageIter dict;
54 DBusMessage *message = NULL;
55 DBusMessage *reply = NULL;
56 GMainLoop *loop;
57
58 /* connect to system bus */
59 con = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
60 if (con == NULL) {
61 printf("failed to connect to system bus\n");
62 goto out;
63 }
64
65 /* this is unique to the device */
66 device_id = "hello-dave";
67 message = dbus_message_new_method_call("org.freedesktop.ColorManager",
68 "/org/freedesktop/ColorManager",
69 "org.freedesktop.ColorManager",
70 "CreateDevice");
71 dbus_message_iter_init_append(message, &args);
72 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &device_id);
73 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &scope);
74
75 /* set initial properties */
76 dbus_message_iter_open_container(&args,
77 DBUS_TYPE_ARRAY,
78 "{ss}",
79 &dict);
80 helper_dict_add_property(&dict, "Colorspace", "RGB");
81 helper_dict_add_property(&dict, "Kind", "scanner");
82 dbus_message_iter_close_container(&args, &dict);
83
84 /* send synchronous */
85 dbus_error_init(&error);
86 printf("Calling CreateDevice(%s,%s)\n",
87 device_id, scope);
88 reply = dbus_connection_send_with_reply_and_block(con,
89 message,
90 -1,
91 &error);
92 if (reply == NULL) {
93 printf("failed to send: %s:%s\n",
94 error.name, error.message);
95 dbus_error_free(&error);
96 goto out;
97 }
98
99 /* get reply data */
100 dbus_message_iter_init(reply, &args);
101 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
102 printf("incorrect reply type\n");
103 goto out;
104 }
105 dbus_message_iter_get_basic(&args, &device_path_tmp);
106 printf("created device %s\n", device_path_tmp);
107
108 /* just spin in a main loop */
109 loop = g_main_loop_new(NULL, TRUE);
110 g_main_loop_run(loop);
111 g_main_loop_unref(loop);
112 out:
113 if (con != NULL)
114 dbus_connection_unref(con);
115 if (message != NULL)
116 dbus_message_unref(message);
117 if (reply != NULL)
118 dbus_message_unref(reply);
119 return 0;
120 }