1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2009,2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
*
**************************************************************************
* This file contains implementation of the helper functionality
* for accessing Studio object through D-Bus
**************************************************************************
*
* LADI Session Handler is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* LADI Session Handler is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "common.h"
static void (* g_renamed_callback)(const char * new_studio_name) = NULL;
static void (* g_started_callback)(void) = NULL;
static void (* g_stopped_callback)(void) = NULL;
static void (* g_crashed_callback)(void) = NULL;
static void (* g_room_appeared_calback)(const char * opath, const char * name, const char * template) = NULL;
static void (* g_room_disappeared_calback)(const char * opath, const char * name, const char * template) = NULL;
static void (* g_room_changed_calback)(const char * opath, const char * name, const char * template) = NULL;
static void on_studio_renamed(void * context, DBusMessage * message_ptr)
{
char * name;
if (!dbus_message_get_args(message_ptr, &cdbus_g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
{
log_error("Invalid parameters of StudioRenamed signal: %s", cdbus_g_dbus_error.message);
dbus_error_free(&cdbus_g_dbus_error);
}
else
{
log_info("StudioRenamed");
if (g_renamed_callback != NULL)
{
g_renamed_callback(name);
}
}
}
static bool extract_room_info(DBusMessageIter * iter_ptr, const char ** opath, const char ** name, const char ** template)
{
dbus_message_iter_get_basic(iter_ptr, opath);
//log_info("opath is \"%s\"", *opath);
dbus_message_iter_next(iter_ptr);
if (!cdbus_iter_get_dict_entry_string(iter_ptr, "name", name))
{
log_error("dbus_iter_get_dict_entry() failed");
return false;
}
//log_info("name is \"%s\"", *name);
if (!cdbus_iter_get_dict_entry_string(iter_ptr, "template", template))
{
*template = NULL;
}
//log_info("template is \"%s\"", *template);
return true;
}
static bool extract_room_info_from_signal(DBusMessage * message_ptr, const char ** opath, const char ** name, const char ** template)
{
const char * signature;
DBusMessageIter iter;
signature = dbus_message_get_signature(message_ptr);
if (strcmp(signature, "sa{sv}") != 0)
{
log_error("Invalid signature of room signal");
return false;
}
dbus_message_iter_init(message_ptr, &iter);
return extract_room_info(&iter, opath, name, template);
}
static void on_studio_started(void * context, DBusMessage * message_ptr)
{
log_info("StudioStarted");
if (g_started_callback != NULL)
{
g_started_callback();
}
}
static void on_studio_stopped(void * context, DBusMessage * message_ptr)
{
log_info("StudioStopped");
if (g_stopped_callback != NULL)
{
g_stopped_callback();
}
}
static void on_studio_crashed(void * context, DBusMessage * message_ptr)
{
log_info("StudioCrashed");
if (g_crashed_callback != NULL)
{
g_crashed_callback();
}
}
static void on_room_appeared(void * context, DBusMessage * message_ptr)
{
const char * opath;
const char * name;
const char * template;
log_info("RoomAppeared");
if (g_room_appeared_calback != NULL && extract_room_info_from_signal(message_ptr, &opath, &name, &template))
{
g_room_appeared_calback(opath, name, template);
}
}
static void on_room_disappeared(void * context, DBusMessage * message_ptr)
{
const char * opath;
const char * name;
const char * template;
log_info("RoomDisappeared");
if (g_room_disappeared_calback != NULL && extract_room_info_from_signal(message_ptr, &opath, &name, &template))
{
g_room_disappeared_calback(opath, name, template);
}
}
static void on_room_changed(void * context, DBusMessage * message_ptr)
{
const char * opath;
const char * name;
const char * template;
log_info("RoomChanged");
if (g_room_changed_calback != NULL && extract_room_info_from_signal(message_ptr, &opath, &name, &template))
{
g_room_changed_calback(opath, name, template);
}
}
/* this must be static because it is referenced by the
* dbus helper layer when hooks are active */
static struct cdbus_signal_hook g_signal_hooks[] =
{
{"StudioRenamed", on_studio_renamed},
{"StudioStarted", on_studio_started},
{"StudioStopped", on_studio_stopped},
{"StudioCrashed", on_studio_crashed},
{"RoomAppeared", on_room_appeared},
{"RoomDisappeared", on_room_disappeared},
{"RoomChanged", on_room_changed},
{NULL, NULL}
};
bool studio_proxy_init(void)
{
if (!cdbus_register_object_signal_hooks(
cdbus_g_dbus_connection,
SERVICE_NAME,
STUDIO_OBJECT_PATH,
IFACE_STUDIO,
NULL,
g_signal_hooks))
{
log_error("dbus_register_object_signal_hooks() failed");
return false;
}
return true;
}
void studio_proxy_uninit(void)
{
cdbus_unregister_object_signal_hooks(cdbus_g_dbus_connection, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO);
}
bool studio_proxy_get_name(char ** name_ptr)
{
const char * name;
if (!cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "GetName", "", "s", &name))
{
return false;
}
*name_ptr = strdup(name);
if (*name_ptr == NULL)
{
log_error("strdup() failed to duplicate studio name");
return false;
}
return true;
}
bool studio_proxy_rename(const char * name)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Rename", "s", &name, "");
}
bool studio_proxy_save(void)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Save", "", "");
}
bool studio_proxy_save_as(const char * name)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "SaveAs", "s", &name, "");
}
bool studio_proxy_unload(void)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Unload", "", "");
}
void studio_proxy_set_renamed_callback(void (* callback)(const char * new_studio_name))
{
g_renamed_callback = callback;
}
void studio_proxy_set_startstop_callbacks(void (* started_callback)(void), void (* stopped_callback)(void), void (* crashed_callback)(void))
{
g_started_callback = started_callback;
g_stopped_callback = stopped_callback;
g_crashed_callback = crashed_callback;
}
bool studio_proxy_start(void)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Start", "", "");
}
bool studio_proxy_stop(void)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "Stop", "", "");
}
bool studio_proxy_is_started(bool * is_started_ptr)
{
dbus_bool_t is_started;
if (!cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "IsStarted", "", "b", &is_started))
{
return false;
}
*is_started_ptr = is_started;
return true;
}
void
studio_proxy_set_room_callbacks(
void (* appeared)(const char * opath, const char * name, const char * template),
void (* disappeared)(const char * opath, const char * name, const char * template),
void (* changed)(const char * opath, const char * name, const char * template))
{
DBusMessage * reply_ptr;
const char * signature;
DBusMessageIter top_iter;
DBusMessageIter array_iter;
DBusMessageIter struct_iter;
const char * opath;
const char * name;
const char * template;
g_room_appeared_calback = appeared;
g_room_disappeared_calback = disappeared;
g_room_changed_calback = changed;
if (!cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "GetRoomList", "", NULL, &reply_ptr))
{
/* Don't log error if there is no studio loaded */
if (!cdbus_call_last_error_is_name(DBUS_ERROR_UNKNOWN_METHOD))
{
log_error("Cannot fetch studio room list: %s", cdbus_call_last_error_get_message());
}
return;
}
signature = dbus_message_get_signature(reply_ptr);
if (strcmp(signature, "a(sa{sv})") != 0)
{
log_error("Invalid signature of GetRoomList reply");
goto unref;
}
dbus_message_iter_init(reply_ptr, &top_iter);
for (dbus_message_iter_recurse(&top_iter, &array_iter);
dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID;
dbus_message_iter_next(&array_iter))
{
dbus_message_iter_recurse(&array_iter, &struct_iter);
if (!extract_room_info(&struct_iter, &opath, &name, &template))
{
log_error("extract_room_info() failed.");
goto unref;
}
g_room_appeared_calback(opath, name, template);
}
unref:
dbus_message_unref(reply_ptr);
}
bool studio_proxy_create_room(const char * name, const char * template)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "CreateRoom", "ss", &name, &template, "");
}
bool studio_proxy_delete_room(const char * name)
{
return cdbus_call(0, SERVICE_NAME, STUDIO_OBJECT_PATH, IFACE_STUDIO, "DeleteRoom", "s", &name, "");
}
|