ladish/proxies/control_proxy.c

266 lines
7.0 KiB
C
Raw Permalink Normal View History

2009-08-20 00:51:27 +03:00
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2009,2010,2011,2012 Nedko Arnaudov <nedko@arnaudov.name>
2009-08-20 00:51:27 +03:00
*
**************************************************************************
* This file contains implementation of code that interfaces
* the ladishd Control 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 "control_proxy.h"
2009-12-17 07:41:57 +02:00
static bool g_clean_exit;
static void on_studio_appeared(void * UNUSED(context), DBusMessage * UNUSED(message_ptr))
2009-08-20 00:51:27 +03:00
{
2009-12-13 17:48:29 +02:00
log_info("StudioAppeared");
control_proxy_on_studio_appeared(false);
2009-12-13 17:48:29 +02:00
}
2009-08-20 00:51:27 +03:00
static void on_studio_disappeared(void * UNUSED(context), DBusMessage * UNUSED(message_ptr))
2009-08-20 00:51:27 +03:00
{
2009-12-13 17:48:29 +02:00
log_info("StudioDisappeared");
control_proxy_on_studio_disappeared();
2009-08-20 00:51:27 +03:00
}
static void on_clean_exit(void * UNUSED(context), DBusMessage * UNUSED(message_ptr))
2009-12-17 07:41:57 +02:00
{
g_clean_exit = true;
}
2009-12-13 17:48:29 +02:00
/* this must be static because it is referenced by the
* dbus helper layer when hooks are active */
2011-09-10 03:35:48 +03:00
static struct cdbus_signal_hook g_signal_hooks[] =
2009-12-13 17:48:29 +02:00
{
{"StudioAppeared", on_studio_appeared},
{"StudioDisappeared", on_studio_disappeared},
2009-12-17 07:41:57 +02:00
{"CleanExit", on_clean_exit},
2009-12-13 17:48:29 +02:00
{NULL, NULL}
};
static bool control_proxy_is_studio_loaded(bool * present_ptr)
{
dbus_bool_t present;
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "IsStudioLoaded", "", "b", &present))
{
return false;
}
*present_ptr = present;
return true;
}
2009-12-17 07:41:57 +02:00
void on_lifestatus_changed(bool appeared)
{
if (appeared)
{
control_proxy_on_daemon_appeared();
}
else
{
control_proxy_on_daemon_disappeared(g_clean_exit);
}
g_clean_exit = false;
}
2009-08-20 00:51:27 +03:00
bool control_proxy_init(void)
{
bool studio_present;
2009-12-17 07:41:57 +02:00
g_clean_exit = false;
if (!control_proxy_is_studio_loaded(&studio_present))
{
2009-12-17 07:41:57 +02:00
return true;
}
control_proxy_on_daemon_appeared();
2011-09-10 03:35:48 +03:00
if (!cdbus_register_service_lifetime_hook(cdbus_g_dbus_connection, SERVICE_NAME, on_lifestatus_changed))
2009-12-17 07:41:57 +02:00
{
control_proxy_on_daemon_disappeared(true);
return false;
}
if (studio_present)
{
2009-09-20 18:23:42 +03:00
log_info("Initial studio appear");
control_proxy_on_studio_appeared(true);
}
2011-09-10 03:35:48 +03:00
if (!cdbus_register_object_signal_hooks(cdbus_g_dbus_connection, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, NULL, g_signal_hooks))
{
if (studio_present)
{
control_proxy_on_studio_disappeared();
}
2009-12-17 07:41:57 +02:00
control_proxy_on_daemon_disappeared(true);
return false;
}
2009-08-20 00:51:27 +03:00
return true;
}
void control_proxy_uninit(void)
{
2011-09-10 03:35:48 +03:00
cdbus_unregister_object_signal_hooks(cdbus_g_dbus_connection, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL);
cdbus_unregister_service_lifetime_hook(cdbus_g_dbus_connection, SERVICE_NAME);
2009-12-17 07:41:57 +02:00
}
void control_proxy_ping(void)
{
bool studio_present;
control_proxy_is_studio_loaded(&studio_present);
2009-08-20 00:51:27 +03:00
}
2009-08-28 21:59:45 +03:00
bool control_proxy_get_studio_list(void (* callback)(void * context, const char * studio_name), void * context)
2009-08-20 00:51:27 +03:00
{
2009-08-28 21:59:45 +03:00
DBusMessage * reply_ptr;
const char * reply_signature;
DBusMessageIter top_iter;
DBusMessageIter struct_iter;
DBusMessageIter array_iter;
const char * studio_name;
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "GetStudioList", "", NULL, &reply_ptr))
2009-08-28 21:59:45 +03:00
{
2009-09-20 18:23:42 +03:00
log_error("GetStudioList() failed.");
2009-08-28 21:59:45 +03:00
return false;
}
reply_signature = dbus_message_get_signature(reply_ptr);
if (strcmp(reply_signature, "a(sa{sv})") != 0)
{
2009-09-20 18:23:42 +03:00
log_error("GetStudioList() reply signature mismatch. '%s'", reply_signature);
2009-08-28 21:59:45 +03:00
dbus_message_unref(reply_ptr);
return false;
}
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);
dbus_message_iter_get_basic(&struct_iter, &studio_name);
callback(context, studio_name);
dbus_message_iter_next(&struct_iter);
dbus_message_iter_next(&struct_iter);
}
dbus_message_unref(reply_ptr);
return true;
2009-08-20 00:51:27 +03:00
}
2009-08-30 16:13:38 +03:00
bool control_proxy_new_studio(const char * studio_name)
{
if (studio_name == NULL)
{
studio_name = "";
}
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "NewStudio", "s", &studio_name, ""))
2009-08-30 16:13:38 +03:00
{
2009-09-20 18:23:42 +03:00
log_error("NewStudio() failed.");
2009-08-30 16:13:38 +03:00
return false;
}
return true;
}
2009-08-20 00:51:27 +03:00
bool control_proxy_load_studio(const char * studio_name)
{
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "LoadStudio", "s", &studio_name, ""))
2009-08-28 21:59:45 +03:00
{
2009-09-20 18:23:42 +03:00
log_error("LoadStudio() failed.");
2009-08-28 21:59:45 +03:00
return false;
}
return true;
2009-08-20 00:51:27 +03:00
}
2009-08-30 14:36:27 +03:00
bool control_proxy_delete_studio(const char * studio_name)
{
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "DeleteStudio", "s", &studio_name, ""))
2009-08-30 14:36:27 +03:00
{
2009-09-20 18:23:42 +03:00
log_error("DeleteStudio() failed.");
2009-08-30 14:36:27 +03:00
return false;
}
return true;
}
2009-08-20 00:51:27 +03:00
bool control_proxy_exit(void)
{
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "Exit", "", ""))
2009-08-28 21:59:45 +03:00
{
2009-09-20 18:23:42 +03:00
log_error("Exit() failed.");
2009-08-28 21:59:45 +03:00
return false;
}
return true;
2009-08-20 00:51:27 +03:00
}
2010-03-21 16:33:39 +02:00
bool control_proxy_get_room_template_list(void (* callback)(void * context, const char * template_name), void * context)
{
DBusMessage * reply_ptr;
const char * reply_signature;
DBusMessageIter top_iter;
DBusMessageIter struct_iter;
DBusMessageIter array_iter;
const char * name;
2011-09-10 03:35:48 +03:00
if (!cdbus_call(0, SERVICE_NAME, CONTROL_OBJECT_PATH, IFACE_CONTROL, "GetRoomTemplateList", "", NULL, &reply_ptr))
2010-03-21 16:33:39 +02:00
{
log_error("GetRoomTemplateList() failed.");
2010-03-21 16:33:39 +02:00
return false;
}
reply_signature = dbus_message_get_signature(reply_ptr);
if (strcmp(reply_signature, "a(sa{sv})") != 0)
{
log_error("GetRoomTemplateList() reply signature mismatch. '%s'", reply_signature);
2010-03-21 16:33:39 +02:00
dbus_message_unref(reply_ptr);
return false;
}
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);
dbus_message_iter_get_basic(&struct_iter, &name);
callback(context, name);
dbus_message_iter_next(&struct_iter);
dbus_message_iter_next(&struct_iter);
}
dbus_message_unref(reply_ptr);
return true;
}