daemon: add some logs
[ladish.git] / daemon / control.c
blobf82997c162865ae2494b3189b2f6af59a48aafd0
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
8 **************************************************************************
9 * This file contains code of the D-Bus control interface helpers
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"
30 #include "../dbus/error.h"
31 #include "control.h"
32 #include "../dbus_constants.h"
33 #include "studio_internal.h"
35 #define INTERFACE_NAME IFACE_CONTROL
37 static void ladish_is_studio_loaded(struct dbus_method_call * call_ptr)
39 DBusMessageIter iter;
40 dbus_bool_t is_loaded;
42 is_loaded = studio_is_loaded();
44 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
45 if (call_ptr->reply == NULL)
47 goto fail;
50 dbus_message_iter_init_append(call_ptr->reply, &iter);
52 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &is_loaded))
54 goto fail_unref;
57 return;
59 fail_unref:
60 dbus_message_unref(call_ptr->reply);
61 call_ptr->reply = NULL;
63 fail:
64 log_error("Ran out of memory trying to construct method return");
67 #define array_iter_ptr ((DBusMessageIter *)context)
69 static bool get_studio_list_callback(void * call_ptr, void * context, const char * studio, uint32_t modtime)
71 DBusMessageIter struct_iter;
72 DBusMessageIter dict_iter;
73 bool ret;
75 ret = false;
77 if (!dbus_message_iter_open_container(array_iter_ptr, DBUS_TYPE_STRUCT, NULL, &struct_iter))
78 goto exit;
80 if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, &studio))
81 goto close_struct;
83 if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
84 goto close_struct;
86 /* if (!maybe_add_dict_entry_string(&dict_iter, "Description", xxx)) */
87 /* goto close_dict; */
89 if (!dbus_add_dict_entry_uint32(&dict_iter, "Modification Time", modtime))
90 goto close_dict;
92 ret = true;
94 close_dict:
95 if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
96 ret = false;
98 close_struct:
99 if (!dbus_message_iter_close_container(array_iter_ptr, &struct_iter))
100 ret = false;
102 exit:
103 return ret;
106 static void ladish_get_studio_list(struct dbus_method_call * call_ptr)
108 DBusMessageIter iter, array_iter;
110 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
111 if (call_ptr->reply == NULL)
113 goto fail;
116 dbus_message_iter_init_append(call_ptr->reply, &iter);
118 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &array_iter))
120 goto fail_unref;
123 if (!studios_iterate(call_ptr, &array_iter, get_studio_list_callback))
125 dbus_message_iter_close_container(&iter, &array_iter);
126 if (call_ptr->reply == NULL)
127 goto fail_unref;
129 /* studios_iterate or get_studio_list_callback() composed error reply */
130 return;
133 if (!dbus_message_iter_close_container(&iter, &array_iter))
135 goto fail_unref;
138 return;
140 fail_unref:
141 dbus_message_unref(call_ptr->reply);
142 call_ptr->reply = NULL;
144 fail:
145 log_error("Ran out of memory trying to construct method return");
148 static void ladish_load_studio(struct dbus_method_call * call_ptr)
150 const char * name;
152 dbus_error_init(&g_dbus_error);
154 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
156 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
157 dbus_error_free(&g_dbus_error);
158 return;
161 log_info("Load studio request (%s)", name);
163 if (ladish_command_load_studio(call_ptr, &g_studio.cmd_queue, name))
165 method_return_new_void(call_ptr);
169 static void ladish_delete_studio(struct dbus_method_call * call_ptr)
171 const char * name;
173 dbus_error_init(&g_dbus_error);
175 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
177 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
178 dbus_error_free(&g_dbus_error);
179 return;
182 if (studio_delete(call_ptr, name))
184 method_return_new_void(call_ptr);
188 static void ladish_new_studio(struct dbus_method_call * call_ptr)
190 const char * name;
192 dbus_error_init(&g_dbus_error);
194 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
196 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
197 dbus_error_free(&g_dbus_error);
198 return;
201 log_info("New studio request (%s)", name);
203 if (ladish_command_new_studio(call_ptr, &g_studio.cmd_queue, name))
205 method_return_new_void(call_ptr);
209 static void ladish_get_application_list(struct dbus_method_call * call_ptr)
211 DBusMessageIter iter;
212 DBusMessageIter array_iter;
213 #if 0
214 DBusMessageIter struct_iter;
215 DBusMessageIter dict_iter;
216 struct list_head * node_ptr;
217 struct lash_appdb_entry * entry_ptr;
218 #endif
220 log_info("Getting applications list");
222 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
223 if (call_ptr->reply == NULL)
225 goto fail;
228 dbus_message_iter_init_append(call_ptr->reply, &iter);
230 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &array_iter))
232 goto fail_unref;
235 #if 0
236 list_for_each(node_ptr, &g_server->appdb)
238 entry_ptr = list_entry(node_ptr, struct lash_appdb_entry, siblings);
240 if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
241 goto fail_unref;
243 if (!dbus_message_iter_append_basic(&struct_iter, DBUS_TYPE_STRING, (const void *) &entry_ptr->name))
245 dbus_message_iter_close_container(&iter, &array_iter);
246 goto fail_unref;
249 if (!dbus_message_iter_open_container(&struct_iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
250 goto fail_unref;
252 if (!maybe_add_dict_entry_string(&dict_iter, "GenericName", entry_ptr->generic_name))
253 goto fail_unref;
255 if (!maybe_add_dict_entry_string(&dict_iter, "Comment", entry_ptr->comment))
256 goto fail_unref;
258 if (!maybe_add_dict_entry_string(&dict_iter, "Icon", entry_ptr->icon))
259 goto fail_unref;
261 if (!dbus_message_iter_close_container(&struct_iter, &dict_iter))
262 goto fail_unref;
264 if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
265 goto fail_unref;
267 #endif
269 if (!dbus_message_iter_close_container(&iter, &array_iter))
271 goto fail_unref;
274 return;
276 fail_unref:
277 dbus_message_unref(call_ptr->reply);
278 call_ptr->reply = NULL;
279 fail:
280 return;
283 static void ladish_exit(struct dbus_method_call * call_ptr)
285 log_info("Exit command received through D-Bus");
287 if (!ladish_command_exit(NULL, &g_studio.cmd_queue))
288 { /* if queuing of command failed, force exit anyway,
289 JACK server will be left started,
290 but refusing exit command is worse */
291 g_quit = true;
294 method_return_new_void(call_ptr);
297 void emit_studio_appeared()
299 dbus_signal_emit(g_dbus_connection, CONTROL_OBJECT_PATH, INTERFACE_NAME, "StudioAppeared", "");
302 void emit_studio_disappeared()
304 dbus_signal_emit(g_dbus_connection, CONTROL_OBJECT_PATH, INTERFACE_NAME, "StudioDisappeared", "");
307 METHOD_ARGS_BEGIN(IsStudioLoaded, "Check whether studio D-Bus object is present")
308 METHOD_ARG_DESCRIBE_OUT("present", "b", "Whether studio D-Bus object is present")
309 METHOD_ARGS_END
311 METHOD_ARGS_BEGIN(GetStudioList, "Get list of studios")
312 METHOD_ARG_DESCRIBE_OUT("studio_list", "a(sa{sv})", "List of studios, name and properties")
313 METHOD_ARGS_END
315 METHOD_ARGS_BEGIN(NewStudio, "New studio")
316 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "Name of studio, if empty name will be generated")
317 METHOD_ARGS_END
319 METHOD_ARGS_BEGIN(LoadStudio, "Load studio")
320 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "Name of studio to load")
321 METHOD_ARG_DESCRIBE_IN("options", "a{sv}", "Load options")
322 METHOD_ARGS_END
324 METHOD_ARGS_BEGIN(DeleteStudio, "Delete studio")
325 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "Name of studio to delete")
326 METHOD_ARGS_END
328 METHOD_ARGS_BEGIN(GetApplicationList, "Get list of applications that can be launched")
329 METHOD_ARG_DESCRIBE_OUT("applications", "a(sa{sv})", "List of applications, name and properties")
330 METHOD_ARGS_END
332 METHOD_ARGS_BEGIN(Exit, "Tell ladish D-Bus service to exit")
333 METHOD_ARGS_END
335 METHODS_BEGIN
336 METHOD_DESCRIBE(IsStudioLoaded, ladish_is_studio_loaded)
337 METHOD_DESCRIBE(GetStudioList, ladish_get_studio_list)
338 METHOD_DESCRIBE(NewStudio, ladish_new_studio)
339 METHOD_DESCRIBE(LoadStudio, ladish_load_studio)
340 METHOD_DESCRIBE(DeleteStudio, ladish_delete_studio)
341 METHOD_DESCRIBE(GetApplicationList, ladish_get_application_list)
342 METHOD_DESCRIBE(Exit, ladish_exit)
343 METHODS_END
345 SIGNAL_ARGS_BEGIN(StudioAppeared, "Studio D-Bus object appeared")
346 SIGNAL_ARGS_END
348 SIGNAL_ARGS_BEGIN(StudioDisappeared, "Studio D-Bus object disappeared")
349 SIGNAL_ARGS_END
351 SIGNALS_BEGIN
352 SIGNAL_DESCRIBE(StudioAppeared)
353 SIGNAL_DESCRIBE(StudioDisappeared)
354 SIGNALS_END
357 * Interface description.
360 INTERFACE_BEGIN(g_lashd_interface_control, INTERFACE_NAME)
361 INTERFACE_DEFAULT_HANDLER
362 INTERFACE_EXPOSE_METHODS
363 INTERFACE_EXPOSE_SIGNALS
364 INTERFACE_END