1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of code that interfaces
9 * the ladishd Control 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 "control_proxy.h"
30 static bool g_clean_exit
;
32 static void on_studio_appeared(void * context
, DBusMessage
* message_ptr
)
34 log_info("StudioAppeared");
35 control_proxy_on_studio_appeared(false);
38 static void on_studio_disappeared(void * context
, DBusMessage
* message_ptr
)
40 log_info("StudioDisappeared");
41 control_proxy_on_studio_disappeared();
44 static void on_clean_exit(void * context
, DBusMessage
* message_ptr
)
49 /* this must be static because it is referenced by the
50 * dbus helper layer when hooks are active */
51 static struct dbus_signal_hook g_signal_hooks
[] =
53 {"StudioAppeared", on_studio_appeared
},
54 {"StudioDisappeared", on_studio_disappeared
},
55 {"CleanExit", on_clean_exit
},
59 static bool control_proxy_is_studio_loaded(bool * present_ptr
)
63 if (!dbus_call(SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, "IsStudioLoaded", "", "b", &present
))
68 *present_ptr
= present
;
73 void on_lifestatus_changed(bool appeared
)
77 control_proxy_on_daemon_appeared();
81 control_proxy_on_daemon_disappeared(g_clean_exit
);
87 bool control_proxy_init(void)
93 if (!control_proxy_is_studio_loaded(&studio_present
))
98 control_proxy_on_daemon_appeared();
100 if (!dbus_register_service_lifetime_hook(g_dbus_connection
, SERVICE_NAME
, on_lifestatus_changed
))
102 control_proxy_on_daemon_disappeared(true);
108 log_info("Initial studio appear");
109 control_proxy_on_studio_appeared(true);
112 if (!dbus_register_object_signal_hooks(g_dbus_connection
, SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, NULL
, g_signal_hooks
))
116 control_proxy_on_studio_disappeared();
119 control_proxy_on_daemon_disappeared(true);
127 void control_proxy_uninit(void)
129 dbus_unregister_object_signal_hooks(g_dbus_connection
, SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
);
130 dbus_unregister_service_lifetime_hook(g_dbus_connection
, SERVICE_NAME
);
133 void control_proxy_ping(void)
137 control_proxy_is_studio_loaded(&studio_present
);
140 bool control_proxy_get_studio_list(void (* callback
)(void * context
, const char * studio_name
), void * context
)
142 DBusMessage
* reply_ptr
;
143 const char * reply_signature
;
144 DBusMessageIter top_iter
;
145 DBusMessageIter struct_iter
;
146 DBusMessageIter array_iter
;
147 const char * studio_name
;
149 if (!dbus_call(SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, "GetStudioList", "", NULL
, &reply_ptr
))
151 log_error("GetStudioList() failed.");
155 reply_signature
= dbus_message_get_signature(reply_ptr
);
156 if (strcmp(reply_signature
, "a(sa{sv})") != 0)
158 log_error("GetStudioList() reply signature mismatch. '%s'", reply_signature
);
159 dbus_message_unref(reply_ptr
);
163 dbus_message_iter_init(reply_ptr
, &top_iter
);
164 for (dbus_message_iter_recurse(&top_iter
, &array_iter
);
165 dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_INVALID
;
166 dbus_message_iter_next(&array_iter
))
168 dbus_message_iter_recurse(&array_iter
, &struct_iter
);
169 dbus_message_iter_get_basic(&struct_iter
, &studio_name
);
170 callback(context
, studio_name
);
171 dbus_message_iter_next(&struct_iter
);
172 dbus_message_iter_next(&struct_iter
);
175 dbus_message_unref(reply_ptr
);
179 bool control_proxy_new_studio(const char * studio_name
)
181 if (studio_name
== NULL
)
186 if (!dbus_call(SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, "NewStudio", "s", &studio_name
, ""))
188 log_error("NewStudio() failed.");
195 bool control_proxy_load_studio(const char * studio_name
)
197 if (!dbus_call(SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, "LoadStudio", "s", &studio_name
, ""))
199 log_error("LoadStudio() failed.");
206 bool control_proxy_delete_studio(const char * studio_name
)
208 if (!dbus_call(SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, "DeleteStudio", "s", &studio_name
, ""))
210 log_error("DeleteStudio() failed.");
217 bool control_proxy_exit(void)
219 if (!dbus_call(SERVICE_NAME
, CONTROL_OBJECT_PATH
, IFACE_CONTROL
, "Exit", "", ""))
221 log_error("Exit() failed.");