ladishd: stop apps on studio stop
[ladish.git] / gui / dbus_helpers.c
blob0fe30661c868e946f4ac8ab55b16e9361d77f29a
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains D-Bus helpers
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <stdbool.h>
28 #include <dbus/dbus.h>
29 #include <dbus/dbus-glib.h>
30 #include <dbus/dbus-glib-lowlevel.h>
32 #include "dbus_helpers.h"
33 #include "../dbus/helpers.h"
35 void
36 patchage_dbus_init()
38 dbus_error_init(&g_dbus_error);
40 // Connect to the bus
41 g_dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &g_dbus_error);
42 if (dbus_error_is_set(&g_dbus_error))
44 //error_msg("dbus_bus_get() failed");
45 //error_msg(g_dbus_error.message);
46 dbus_error_free(&g_dbus_error);
49 dbus_connection_setup_with_g_main(g_dbus_connection, NULL);
52 bool
53 patchage_dbus_call_valist(
54 bool response_expected,
55 const char * service,
56 const char * object,
57 const char * iface,
58 const char * method,
59 DBusMessage ** reply_ptr_ptr,
60 int in_type,
61 va_list ap)
63 DBusMessage * request_ptr;
64 DBusMessage * reply_ptr;
66 request_ptr = dbus_message_new_method_call(
67 service,
68 object,
69 iface,
70 method);
71 if (!request_ptr)
73 //throw std::runtime_error("dbus_message_new_method_call() returned 0");
76 dbus_message_append_args_valist(request_ptr, in_type, ap);
78 // send message and get a handle for a reply
79 reply_ptr = dbus_connection_send_with_reply_and_block(
80 g_dbus_connection,
81 request_ptr,
82 DBUS_CALL_DEFAULT_TIMEOUT,
83 &g_dbus_error);
85 dbus_message_unref(request_ptr);
87 if (!reply_ptr)
89 if (response_expected)
91 //error_msg("no reply from server when calling method '%s', error is '%s'", method, _error.message);
93 dbus_error_free(&g_dbus_error);
95 else
97 *reply_ptr_ptr = reply_ptr;
100 return reply_ptr;
103 bool
104 patchage_dbus_call(
105 bool response_expected,
106 const char * service,
107 const char * object,
108 const char * iface,
109 const char * method,
110 DBusMessage ** reply_ptr_ptr,
111 int in_type,
112 ...)
114 bool ret;
115 va_list ap;
117 va_start(ap, in_type);
119 ret = patchage_dbus_call_valist(
120 response_expected,
121 service,
122 object,
123 iface,
124 method,
125 reply_ptr_ptr,
126 in_type,
127 ap);
129 va_end(ap);
131 return (ap != NULL);
134 void
135 patchage_dbus_add_match(
136 const char * rule)
138 dbus_bus_add_match(g_dbus_connection, rule, NULL);
141 void
142 patchage_dbus_add_filter(
143 DBusHandleMessageFunction function,
144 void * user_data)
146 dbus_connection_add_filter(g_dbus_connection, function, user_data, NULL);
149 void
150 patchage_dbus_uninit()
152 if (g_dbus_connection)
154 dbus_connection_flush(g_dbus_connection);
157 if (dbus_error_is_set(&g_dbus_error))
159 dbus_error_free(&g_dbus_error);