1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009, 2010 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.
30 static void (* g_renamed_callback
)(const char * new_studio_name
) = NULL
;
31 static void (* g_started_callback
)(void) = NULL
;
32 static void (* g_stopped_callback
)(void) = NULL
;
33 static void (* g_crashed_callback
)(void) = NULL
;
35 static void (* g_room_appeared_calback
)(const char * opath
, const char * name
, const char * template) = NULL
;
36 static void (* g_room_disappeared_calback
)(const char * opath
, const char * name
, const char * template) = NULL
;
37 static void (* g_room_changed_calback
)(const char * opath
, const char * name
, const char * template) = NULL
;
39 static void on_studio_renamed(void * context
, DBusMessage
* message_ptr
)
43 if (!dbus_message_get_args(message_ptr
, &g_dbus_error
, DBUS_TYPE_STRING
, &name
, DBUS_TYPE_INVALID
))
45 log_error("Invalid parameters of StudioRenamed signal: %s", g_dbus_error
.message
);
46 dbus_error_free(&g_dbus_error
);
50 log_info("StudioRenamed");
52 if (g_renamed_callback
!= NULL
)
54 g_renamed_callback(name
);
59 static bool extract_room_info(DBusMessageIter
* iter_ptr
, const char ** opath
, const char ** name
, const char ** template)
61 dbus_message_iter_get_basic(iter_ptr
, opath
);
62 //log_info("opath is \"%s\"", *opath);
63 dbus_message_iter_next(iter_ptr
);
65 if (!dbus_iter_get_dict_entry_string(iter_ptr
, "name", name
))
67 log_error("dbus_iter_get_dict_entry() failed");
70 //log_info("name is \"%s\"", *name);
72 if (!dbus_iter_get_dict_entry_string(iter_ptr
, "template", template))
76 //log_info("template is \"%s\"", *template);
81 static bool extract_room_info_from_signal(DBusMessage
* message_ptr
, const char ** opath
, const char ** name
, const char ** template)
83 const char * signature
;
86 signature
= dbus_message_get_signature(message_ptr
);
87 if (strcmp(signature
, "sa{sv}") != 0)
89 log_error("Invalid signature of room signal");
93 dbus_message_iter_init(message_ptr
, &iter
);
95 return extract_room_info(&iter
, opath
, name
, template);
98 static void on_studio_started(void * context
, DBusMessage
* message_ptr
)
100 log_info("StudioStarted");
102 if (g_started_callback
!= NULL
)
104 g_started_callback();
108 static void on_studio_stopped(void * context
, DBusMessage
* message_ptr
)
110 log_info("StudioStopped");
112 if (g_stopped_callback
!= NULL
)
114 g_stopped_callback();
118 static void on_studio_crashed(void * context
, DBusMessage
* message_ptr
)
120 log_info("StudioCrashed");
122 if (g_crashed_callback
!= NULL
)
124 g_crashed_callback();
128 static void on_room_appeared(void * context
, DBusMessage
* message_ptr
)
132 const char * template;
134 log_info("RoomAppeared");
136 if (g_room_appeared_calback
!= NULL
&& extract_room_info_from_signal(message_ptr
, &opath
, &name
, &template))
138 g_room_appeared_calback(opath
, name
, template);
142 static void on_room_disappeared(void * context
, DBusMessage
* message_ptr
)
146 const char * template;
148 log_info("RoomDisappeared");
150 if (g_room_disappeared_calback
!= NULL
&& extract_room_info_from_signal(message_ptr
, &opath
, &name
, &template))
152 g_room_disappeared_calback(opath
, name
, template);
156 static void on_room_changed(void * context
, DBusMessage
* message_ptr
)
160 const char * template;
162 log_info("RoomChanged");
164 if (g_room_changed_calback
!= NULL
&& extract_room_info_from_signal(message_ptr
, &opath
, &name
, &template))
166 g_room_changed_calback(opath
, name
, template);
170 /* this must be static because it is referenced by the
171 * dbus helper layer when hooks are active */
172 static struct dbus_signal_hook g_signal_hooks
[] =
174 {"StudioRenamed", on_studio_renamed
},
175 {"StudioStarted", on_studio_started
},
176 {"StudioStopped", on_studio_stopped
},
177 {"StudioCrashed", on_studio_crashed
},
178 {"RoomAppeared", on_room_appeared
},
179 {"RoomDisappeared", on_room_disappeared
},
180 {"RoomChanged", on_room_changed
},
184 bool studio_proxy_init(void)
186 if (!dbus_register_object_signal_hooks(
194 log_error("dbus_register_object_signal_hooks() failed");
201 void studio_proxy_uninit(void)
203 dbus_unregister_object_signal_hooks(g_dbus_connection
, SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
);
206 bool studio_proxy_get_name(char ** name_ptr
)
209 if (!dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "GetName", "", "s", &name
))
214 *name_ptr
= strdup(name
);
215 if (*name_ptr
== NULL
)
217 log_error("strdup() failed to duplicate studio name");
224 bool studio_proxy_rename(const char * name
)
226 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "Rename", "s", &name
, "");
229 bool studio_proxy_save(void)
231 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "Save", "", "");
234 bool studio_proxy_save_as(const char * name
)
236 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "SaveAs", "s", &name
, "");
239 bool studio_proxy_unload(void)
241 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "Unload", "", "");
244 void studio_proxy_set_renamed_callback(void (* callback
)(const char * new_studio_name
))
246 g_renamed_callback
= callback
;
249 void studio_proxy_set_startstop_callbacks(void (* started_callback
)(void), void (* stopped_callback
)(void), void (* crashed_callback
)(void))
251 g_started_callback
= started_callback
;
252 g_stopped_callback
= stopped_callback
;
253 g_crashed_callback
= crashed_callback
;
256 bool studio_proxy_start(void)
258 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "Start", "", "");
261 bool studio_proxy_stop(void)
263 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "Stop", "", "");
266 bool studio_proxy_is_started(bool * is_started_ptr
)
268 dbus_bool_t is_started
;
270 if (!dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "IsStarted", "", "b", &is_started
))
275 *is_started_ptr
= is_started
;
281 studio_proxy_set_room_callbacks(
282 void (* appeared
)(const char * opath
, const char * name
, const char * template),
283 void (* disappeared
)(const char * opath
, const char * name
, const char * template),
284 void (* changed
)(const char * opath
, const char * name
, const char * template))
286 DBusMessage
* reply_ptr
;
287 const char * signature
;
288 DBusMessageIter top_iter
;
289 DBusMessageIter array_iter
;
290 DBusMessageIter struct_iter
;
293 const char * template;
295 g_room_appeared_calback
= appeared
;
296 g_room_disappeared_calback
= disappeared
;
297 g_room_changed_calback
= changed
;
299 if (!dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "GetRoomList", "", NULL
, &reply_ptr
))
301 log_error("Cannot fetch studio room list");
305 signature
= dbus_message_get_signature(reply_ptr
);
306 if (strcmp(signature
, "a(sa{sv})") != 0)
308 log_error("Invalid signature of GetRoomList reply");
312 dbus_message_iter_init(reply_ptr
, &top_iter
);
314 for (dbus_message_iter_recurse(&top_iter
, &array_iter
);
315 dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_INVALID
;
316 dbus_message_iter_next(&array_iter
))
318 dbus_message_iter_recurse(&array_iter
, &struct_iter
);
320 if (!extract_room_info(&struct_iter
, &opath
, &name
, &template))
322 log_error("extract_room_info() failed.");
326 g_room_appeared_calback(opath
, name
, template);
330 dbus_message_unref(reply_ptr
);
333 bool studio_proxy_create_room(const char * name
, const char * template)
335 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "CreateRoom", "ss", &name
, &template, "");
338 bool studio_proxy_delete_room(const char * name
)
340 return dbus_call(SERVICE_NAME
, STUDIO_OBJECT_PATH
, IFACE_STUDIO
, "DeleteRoom", "s", &name
, "");