daemon: handle malloc() failure
[ladish.git] / studio_proxy.c
blob83f1762989f64b77352c3d581b16a7b3b7f2eb1c
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the helper functionality
9 * for accessing Studio object through D-Bus
10 **************************************************************************
12 * LADI Session Handler is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * LADI Session Handler is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
24 * or write to the Free Software Foundation, Inc.,
25 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "common.h"
29 #include "dbus_constants.h"
30 #include "dbus/helpers.h"
32 static void (* g_renamed_callback)(const char * new_studio_name) = NULL;
33 static void (* g_started_callback)(void) = NULL;
34 static void (* g_stopped_callback)(void) = NULL;
36 static const char * g_signals[] =
38 "StudioRenamed",
39 "StudioStarted",
40 "StudioStopped",
41 "RoomAppeared",
42 "RoomDisappeared",
43 NULL
46 static DBusHandlerResult message_hook(DBusConnection * connection, DBusMessage * message, void * data)
48 const char * object_path;
49 char * name;
51 object_path = dbus_message_get_path(message);
52 if (object_path == NULL || strcmp(object_path, STUDIO_OBJECT_PATH) != 0)
54 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
57 if (dbus_message_is_signal(message, IFACE_STUDIO, "StudioRenamed"))
59 if (!dbus_message_get_args(message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
61 log_error("Invalid parameters of StudioRenamed signal: %s", g_dbus_error.message);
62 dbus_error_free(&g_dbus_error);
64 else
66 log_info("StudioRenamed");
68 if (g_renamed_callback != NULL)
70 g_renamed_callback(name);
74 return DBUS_HANDLER_RESULT_HANDLED;
77 if (dbus_message_is_signal(message, IFACE_STUDIO, "StudioStarted"))
79 log_info("StudioStarted");
81 if (g_started_callback != NULL)
83 g_started_callback();
86 return DBUS_HANDLER_RESULT_HANDLED;
89 if (dbus_message_is_signal(message, IFACE_STUDIO, "StudioStopped"))
91 log_info("StudioStopped");
93 if (g_stopped_callback != NULL)
95 g_stopped_callback();
98 return DBUS_HANDLER_RESULT_HANDLED;
101 if (dbus_message_is_signal(message, IFACE_STUDIO, "RoomAppeared"))
103 log_info("RoomAppeared");
104 return DBUS_HANDLER_RESULT_HANDLED;
107 if (dbus_message_is_signal(message, IFACE_STUDIO, "RoomDisappeared"))
109 log_info("RoomDisappeared");
110 return DBUS_HANDLER_RESULT_HANDLED;
113 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
116 bool studio_proxy_init(void)
118 if (!dbus_register_object_signal_handler(
119 g_dbus_connection,
120 SERVICE_NAME,
121 STUDIO_OBJECT_PATH,
122 IFACE_STUDIO,
123 g_signals,
124 message_hook,
125 NULL))
127 log_error("studio_object_path() failed");
128 return false;
131 return true;
134 void studio_proxy_uninit(void)
136 if (!dbus_unregister_object_signal_handler(
137 g_dbus_connection,
138 SERVICE_NAME,
139 STUDIO_OBJECT_PATH,
140 IFACE_STUDIO,
141 g_signals,
142 message_hook,
143 NULL))
145 log_error("studio_object_path() failed");
149 bool studio_proxy_get_name(char ** name_ptr)
151 const char * name;
152 if (!dbus_call(SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "GetName", "", "s", &name))
154 return false;
157 *name_ptr = strdup(name);
158 if (*name_ptr == NULL)
160 log_error("strdup() failed to duplicate studio name");
161 return false;
164 return true;
167 bool studio_proxy_rename(const char * name)
169 return dbus_call(SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Rename", "s", &name, "");
172 bool studio_proxy_save(void)
174 return dbus_call(SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Save", "", "");
177 bool studio_proxy_unload(void)
179 return dbus_call(SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Unload", "", "");
182 void studio_proxy_set_renamed_callback(void (* callback)(const char * new_studio_name))
184 g_renamed_callback = callback;
187 void studio_proxy_set_startstop_callbacks(void (* started_callback)(void), void (* stopped_callback)(void))
189 g_started_callback = started_callback;
190 g_stopped_callback = stopped_callback;
193 bool studio_proxy_start(void)
195 return dbus_call(SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Start", "", "");
198 bool studio_proxy_stop(void)
200 return dbus_call(SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Stop", "", "");