Add app list version number to the AppStateChanged signal and check it
[ladish.git] / daemon / studio.c
blobd41b17b90485b9920b23701975454f223b9523dc
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains part of the studio singleton object implementation
9 * Other parts are in the other studio*.c files in same directory.
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 <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <dirent.h>
35 #include "studio_internal.h"
36 #include "../dbus_constants.h"
37 #include "control.h"
38 #include "../catdup.h"
39 #include "dirhelpers.h"
40 #include "graph_dict.h"
41 #include "escape.h"
42 #include "studio.h"
43 #include "../proxies/notify_proxy.h"
45 #define STUDIOS_DIR "/studios/"
46 char * g_studios_dir;
48 struct studio g_studio;
50 bool ladish_studio_name_generate(char ** name_ptr)
52 time_t now;
53 char timestamp_str[26];
54 char * name;
56 time(&now);
57 //ctime_r(&now, timestamp_str);
58 //timestamp_str[24] = 0;
59 snprintf(timestamp_str, sizeof(timestamp_str), "%llu", (unsigned long long)now);
61 name = catdup("Studio ", timestamp_str);
62 if (name == NULL)
64 log_error("catdup failed to create studio name");
65 return false;
68 *name_ptr = name;
69 return true;
72 bool ladish_studio_show(void)
74 dbus_object_path object;
76 ASSERT(g_studio.name != NULL);
77 ASSERT(!g_studio.announced);
79 object = dbus_object_path_new(
80 STUDIO_OBJECT_PATH,
81 &g_interface_studio, &g_studio,
82 &g_interface_patchbay, ladish_graph_get_dbus_context(g_studio.studio_graph),
83 &g_iface_graph_dict, g_studio.studio_graph,
84 &g_iface_app_supervisor, g_studio.app_supervisor,
85 NULL);
86 if (object == NULL)
88 log_error("dbus_object_path_new() failed");
89 return false;
92 if (!dbus_object_path_register(g_dbus_connection, object))
94 log_error("object_path_register() failed");
95 dbus_object_path_destroy(g_dbus_connection, object);
96 return false;
99 log_info("Studio D-Bus object created. \"%s\"", g_studio.name);
101 g_studio.dbus_object = object;
103 emit_studio_appeared();
105 return true;
108 void ladish_studio_announce(void)
110 ASSERT(!g_studio.announced);
111 ladish_notify_simple(LADISH_NOTIFY_URGENCY_NORMAL, "Studio loaded", NULL);
112 g_studio.announced = true;
115 bool ladish_studio_publish(void)
117 if (!ladish_studio_show())
119 return false;
122 ladish_studio_announce();
123 return true;
126 void ladish_studio_clear(void)
128 ladish_graph_dump(g_studio.studio_graph);
129 ladish_graph_dump(g_studio.jack_graph);
131 /* remove rooms that own clients in studio graph before clearing it */
132 ladish_studio_remove_all_rooms();
134 ladish_graph_clear(g_studio.studio_graph, NULL);
135 ladish_graph_clear(g_studio.jack_graph, NULL);
137 ladish_studio_jack_conf_clear();
139 g_studio.modified = false;
140 g_studio.persisted = false;
142 ladish_app_supervisor_clear(g_studio.app_supervisor);
144 if (g_studio.dbus_object != NULL)
146 dbus_object_path_destroy(g_dbus_connection, g_studio.dbus_object);
147 g_studio.dbus_object = NULL;
148 emit_studio_disappeared();
151 if (g_studio.announced)
153 ladish_notify_simple(LADISH_NOTIFY_URGENCY_NORMAL, "Studio unloaded", NULL);
154 g_studio.announced = false;
157 if (g_studio.name != NULL)
159 free(g_studio.name);
160 g_studio.name = NULL;
163 if (g_studio.filename != NULL)
165 free(g_studio.filename);
166 g_studio.filename = NULL;
170 void ladish_studio_emit_started(void)
172 dbus_signal_emit(g_dbus_connection, STUDIO_OBJECT_PATH, IFACE_STUDIO, "StudioStarted", "");
175 void ladish_studio_emit_crashed(void)
177 dbus_signal_emit(g_dbus_connection, STUDIO_OBJECT_PATH, IFACE_STUDIO, "StudioCrashed", "");
180 void ladish_studio_emit_stopped(void)
182 dbus_signal_emit(g_dbus_connection, STUDIO_OBJECT_PATH, IFACE_STUDIO, "StudioStopped", "");
185 static bool ladish_studio_fill_room_info(DBusMessageIter * iter_ptr, ladish_room_handle room)
187 DBusMessageIter dict_iter;
188 const char * name;
189 uuid_t template_uuid;
190 ladish_room_handle template;
191 const char * template_name;
192 const char * opath;
194 name = ladish_room_get_name(room);
195 opath = ladish_room_get_opath(room);
197 if (!ladish_room_get_template_uuid(room, template_uuid))
199 template = NULL;
200 template_name = NULL;
202 else
204 template = find_room_template_by_uuid(template_uuid);
205 if (template != NULL)
207 template_name = ladish_room_get_name(template);
209 else
211 template_name = NULL;
215 if (!dbus_message_iter_append_basic(iter_ptr, DBUS_TYPE_STRING, &opath))
217 log_error("dbus_message_iter_append_basic() failed.");
218 return false;
221 if (!dbus_message_iter_open_container(iter_ptr, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
223 log_error("dbus_message_iter_open_container() failed.");
224 return false;
227 if (!dbus_maybe_add_dict_entry_string(&dict_iter, "template", template_name))
229 log_error("dbus_maybe_add_dict_entry_string() failed.");
230 return false;
233 if (!dbus_maybe_add_dict_entry_string(&dict_iter, "name", name))
235 log_error("dbus_maybe_add_dict_entry_string() failed.");
236 return false;
239 if (!dbus_message_iter_close_container(iter_ptr, &dict_iter))
241 log_error("dbus_message_iter_close_container() failed.");
242 return false;
245 return true;
248 static void ladish_studio_emit_room_appeared(ladish_room_handle room)
250 DBusMessage * message_ptr;
251 DBusMessageIter iter;
253 message_ptr = dbus_message_new_signal(STUDIO_OBJECT_PATH, IFACE_STUDIO, "RoomAppeared");
254 if (message_ptr == NULL)
256 log_error("dbus_message_new_signal() failed.");
257 return;
260 dbus_message_iter_init_append(message_ptr, &iter);
262 if (ladish_studio_fill_room_info(&iter, room))
264 dbus_signal_send(g_dbus_connection, message_ptr);
267 dbus_message_unref(message_ptr);
270 void ladish_studio_emit_room_disappeared(ladish_room_handle room)
272 DBusMessage * message_ptr;
273 DBusMessageIter iter;
275 message_ptr = dbus_message_new_signal(STUDIO_OBJECT_PATH, IFACE_STUDIO, "RoomDisappeared");
276 if (message_ptr == NULL)
278 log_error("dbus_message_new_signal() failed.");
279 return;
282 dbus_message_iter_init_append(message_ptr, &iter);
284 if (ladish_studio_fill_room_info(&iter, room))
286 dbus_signal_send(g_dbus_connection, message_ptr);
289 dbus_message_unref(message_ptr);
292 void ladish_studio_room_appeared(ladish_room_handle room)
294 log_info("Room \"%s\" appeared", ladish_room_get_name(room));
295 list_add_tail(ladish_room_get_list_node(room), &g_studio.rooms);
296 ladish_studio_emit_room_appeared(room);
299 void ladish_studio_room_disappeared(ladish_room_handle room)
301 log_info("Room \"%s\" disappeared", ladish_room_get_name(room));
302 list_del(ladish_room_get_list_node(room));
303 ladish_studio_emit_room_disappeared(room);
306 bool
307 ladish_studio_set_graph_connection_handlers(
308 void * context,
309 ladish_graph_handle graph,
310 ladish_app_supervisor_handle app_supervisor)
312 ladish_virtualizer_set_graph_connection_handlers(context, graph);
313 return true; /* iterate all graphs */
316 void ladish_studio_on_event_jack_started(void)
318 if (!ladish_studio_fetch_jack_settings())
320 log_error("studio_fetch_jack_settings() failed.");
322 return;
325 log_info("jack conf successfully retrieved");
326 g_studio.jack_conf_valid = true;
328 if (!graph_proxy_create(JACKDBUS_SERVICE_NAME, JACKDBUS_OBJECT_PATH, false, &g_studio.jack_graph_proxy))
330 log_error("graph_proxy_create() failed for jackdbus");
332 else
334 if (!ladish_virtualizer_create(g_studio.jack_graph_proxy, g_studio.jack_graph, &g_studio.virtualizer))
336 log_error("ladish_virtualizer_create() failed.");
338 else
340 ladish_studio_iterate_virtual_graphs(g_studio.virtualizer, ladish_studio_set_graph_connection_handlers);
343 if (!graph_proxy_activate(g_studio.jack_graph_proxy))
345 log_error("graph_proxy_activate() failed.");
349 ladish_app_supervisor_autorun(g_studio.app_supervisor);
351 ladish_studio_emit_started();
352 ladish_notify_simple(LADISH_NOTIFY_URGENCY_NORMAL, "Studio started", NULL);
355 static void ladish_studio_on_jack_stopped_internal(void)
357 if (g_studio.virtualizer)
359 ladish_virtualizer_destroy(g_studio.virtualizer);
360 g_studio.virtualizer = NULL;
363 if (g_studio.jack_graph_proxy)
365 graph_proxy_destroy(g_studio.jack_graph_proxy);
366 g_studio.jack_graph_proxy = NULL;
370 void ladish_studio_on_event_jack_stopped(void)
372 ladish_studio_emit_stopped();
373 ladish_studio_on_jack_stopped_internal();
374 ladish_notify_simple(LADISH_NOTIFY_URGENCY_NORMAL, "Studio stopped", NULL);
377 void ladish_studio_handle_unexpected_jack_server_stop(void)
379 ladish_studio_emit_crashed();
380 ladish_studio_on_jack_stopped_internal();
382 /* TODO: if user wants, restart jack server and reconnect all jack apps to it */
385 static bool ladish_studio_hide_vgraph_non_virtual(void * context, ladish_graph_handle graph, ladish_app_supervisor_handle app_supervisor)
387 ladish_graph_hide_non_virtual(graph);
388 return true; /* iterate all vgraphs */
391 void ladish_studio_run(void)
393 bool state;
395 ladish_cqueue_run(&g_studio.cmd_queue);
396 if (g_quit)
397 { /* if quit is requested, don't bother to process external events */
398 return;
401 if (ladish_environment_consume_change(&g_studio.env_store, ladish_environment_jack_server_started, &state))
403 ladish_cqueue_clear(&g_studio.cmd_queue);
405 if (state)
407 ladish_environment_ignore(&g_studio.env_store, ladish_environment_jack_server_present);
409 if (g_studio.jack_graph_proxy != NULL)
411 log_error("Ignoring \"JACK started\" notification because it is already known that JACK is currently started.");
412 return;
415 /* Automatic studio creation on JACK server start */
416 if (g_studio.dbus_object == NULL)
418 ASSERT(g_studio.name == NULL);
419 if (!ladish_studio_name_generate(&g_studio.name))
421 log_error("studio_name_generate() failed.");
422 return;
425 g_studio.automatic = true;
427 ladish_studio_publish();
430 ladish_studio_on_event_jack_started();
432 else
434 /* JACK stopped but this was not expected. When expected.
435 * the change will be consumed by the run method of the studio stop command */
437 if (g_studio.jack_graph_proxy == NULL)
439 log_error("Ignoring \"JACK stopped\" notification because it is already known that JACK is currently stopped.");
440 return;
443 if (g_studio.automatic)
445 log_info("Unloading automatic studio.");
446 ladish_command_unload_studio(NULL, &g_studio.cmd_queue);
448 ladish_studio_on_event_jack_stopped();
449 return;
452 log_error("JACK stopped unexpectedly.");
453 log_error("Save your work, then unload and reload the studio.");
454 ladish_notify_simple(
455 LADISH_NOTIFY_URGENCY_HIGH,
456 "Studio crashed",
457 "JACK stopped unexpectedly.\n\n"
458 "Save your work, then unload and reload the studio.");
459 ladish_studio_handle_unexpected_jack_server_stop();
463 if (ladish_environment_consume_change(&g_studio.env_store, ladish_environment_jack_server_present, &state))
465 if (g_studio.jack_graph_proxy != NULL)
467 ladish_cqueue_clear(&g_studio.cmd_queue);
469 /* jack was started, this probably means that jackdbus has crashed */
470 log_error("JACK disappeared unexpectedly. Maybe it crashed.");
471 log_error("Save your work, then unload and reload the studio.");
472 ladish_notify_simple(
473 LADISH_NOTIFY_URGENCY_HIGH,
474 "Studio crashed",
475 "JACK disappeared unexpectedly. Maybe it crashed.\n\n"
476 "Save your work, then unload and reload the studio.");
477 ladish_environment_reset_stealth(&g_studio.env_store, ladish_environment_jack_server_started);
479 ladish_studio_iterate_virtual_graphs(NULL, ladish_studio_hide_vgraph_non_virtual);
481 ladish_studio_handle_unexpected_jack_server_stop();
485 ladish_environment_assert_consumed(&g_studio.env_store);
488 static void ladish_studio_on_jack_server_started(void)
490 log_info("JACK server start detected.");
491 ladish_environment_set(&g_studio.env_store, ladish_environment_jack_server_started);
494 static void ladish_studio_on_jack_server_stopped(void)
496 log_info("JACK server stop detected.");
497 ladish_environment_reset(&g_studio.env_store, ladish_environment_jack_server_started);
500 static void ladish_studio_on_jack_server_appeared(void)
502 log_info("JACK controller appeared.");
503 ladish_environment_set(&g_studio.env_store, ladish_environment_jack_server_present);
506 static void ladish_studio_on_jack_server_disappeared(void)
508 log_info("JACK controller disappeared.");
509 ladish_environment_reset(&g_studio.env_store, ladish_environment_jack_server_present);
512 bool ladish_studio_init(void)
514 log_info("studio object construct");
516 g_studios_dir = catdup(g_base_dir, STUDIOS_DIR);
517 if (g_studios_dir == NULL)
519 log_error("catdup failed for '%s' and '%s'", g_base_dir, STUDIOS_DIR);
520 goto fail;
523 if (!ensure_dir_exist(g_studios_dir, 0700))
525 goto free_studios_dir;
528 INIT_LIST_HEAD(&g_studio.all_connections);
529 INIT_LIST_HEAD(&g_studio.all_ports);
530 INIT_LIST_HEAD(&g_studio.all_clients);
531 INIT_LIST_HEAD(&g_studio.jack_connections);
532 INIT_LIST_HEAD(&g_studio.jack_ports);
533 INIT_LIST_HEAD(&g_studio.jack_clients);
534 INIT_LIST_HEAD(&g_studio.rooms);
535 INIT_LIST_HEAD(&g_studio.clients);
536 INIT_LIST_HEAD(&g_studio.ports);
538 INIT_LIST_HEAD(&g_studio.jack_conf);
539 INIT_LIST_HEAD(&g_studio.jack_params);
541 g_studio.dbus_object = NULL;
542 g_studio.announced = false;
543 g_studio.name = NULL;
544 g_studio.filename = NULL;
546 g_studio.room_count = 0;
548 if (!ladish_graph_create(&g_studio.jack_graph, NULL))
550 log_error("ladish_graph_create() failed to create jack graph object.");
551 goto free_studios_dir;
554 if (!ladish_graph_create(&g_studio.studio_graph, STUDIO_OBJECT_PATH))
556 log_error("ladish_graph_create() failed to create studio graph object.");
557 goto jack_graph_destroy;
560 if (!ladish_app_supervisor_create(&g_studio.app_supervisor, STUDIO_OBJECT_PATH, "studio", g_studio.studio_graph, ladish_virtualizer_rename_app))
562 log_error("ladish_app_supervisor_create() failed.");
563 goto studio_graph_destroy;
566 ladish_cqueue_init(&g_studio.cmd_queue);
567 ladish_environment_init(&g_studio.env_store);
569 if (!jack_proxy_init(
570 ladish_studio_on_jack_server_started,
571 ladish_studio_on_jack_server_stopped,
572 ladish_studio_on_jack_server_appeared,
573 ladish_studio_on_jack_server_disappeared))
575 log_error("jack_proxy_init() failed.");
576 goto app_supervisor_destroy;
579 return true;
581 app_supervisor_destroy:
582 ladish_app_supervisor_destroy(g_studio.app_supervisor);
583 studio_graph_destroy:
584 ladish_graph_destroy(g_studio.studio_graph);
585 jack_graph_destroy:
586 ladish_graph_destroy(g_studio.jack_graph);
587 free_studios_dir:
588 free(g_studios_dir);
589 fail:
590 return false;
593 void ladish_studio_uninit(void)
595 log_info("studio_uninit()");
597 jack_proxy_uninit();
599 ladish_cqueue_clear(&g_studio.cmd_queue);
601 ladish_graph_destroy(g_studio.studio_graph);
602 ladish_graph_destroy(g_studio.jack_graph);
604 free(g_studios_dir);
606 log_info("studio object destroy");
609 struct on_child_exit_context
611 pid_t pid;
612 bool found;
615 #define child_exit_context_ptr ((struct on_child_exit_context *)context)
617 static
618 bool
619 ladish_studio_on_child_exit_callback(
620 void * context,
621 ladish_graph_handle graph,
622 ladish_app_supervisor_handle app_supervisor)
624 child_exit_context_ptr->found = ladish_app_supervisor_child_exit(app_supervisor, child_exit_context_ptr->pid);
625 /* if child is found, return false - it will cause iteration to stop */
626 /* if child is not found, return true - it will cause next supervisor to be checked */
627 return !child_exit_context_ptr->found;
630 #undef child_exit_context_ptr
632 void ladish_studio_on_child_exit(pid_t pid)
634 struct on_child_exit_context context;
636 context.pid = pid;
637 context.found = false;
639 ladish_studio_iterate_virtual_graphs(&context, ladish_studio_on_child_exit_callback);
641 if (!context.found)
643 log_error("unknown child exit detected. pid is %llu", (unsigned long long)pid);
647 bool ladish_studio_is_loaded(void)
649 return g_studio.dbus_object != NULL && g_studio.announced;
652 bool ladish_studio_is_started(void)
654 return ladish_environment_get(&g_studio.env_store, ladish_environment_jack_server_started);
657 bool ladish_studio_compose_filename(const char * name, char ** filename_ptr_ptr, char ** backup_filename_ptr_ptr)
659 size_t len_dir;
660 char * p;
661 const char * src;
662 char * filename_ptr;
663 char * backup_filename_ptr = NULL;
665 len_dir = strlen(g_studios_dir);
667 filename_ptr = malloc(len_dir + 1 + strlen(name) * 3 + 4 + 1);
668 if (filename_ptr == NULL)
670 log_error("malloc failed to allocate memory for studio file path");
671 return false;
674 if (backup_filename_ptr_ptr != NULL)
676 backup_filename_ptr = malloc(len_dir + 1 + strlen(name) * 3 + 4 + 4 + 1);
677 if (backup_filename_ptr == NULL)
679 log_error("malloc failed to allocate memory for studio backup file path");
680 free(filename_ptr);
681 return false;
685 p = filename_ptr;
686 memcpy(p, g_studios_dir, len_dir);
687 p += len_dir;
689 *p++ = '/';
691 src = name;
692 escape(&src, &p);
693 strcpy(p, ".xml");
695 *filename_ptr_ptr = filename_ptr;
697 if (backup_filename_ptr_ptr != NULL)
699 strcpy(backup_filename_ptr, filename_ptr);
700 strcat(backup_filename_ptr, ".bak");
701 *backup_filename_ptr_ptr = backup_filename_ptr;
704 return true;
707 struct ladish_cqueue * ladish_studio_get_cmd_queue(void)
709 return &g_studio.cmd_queue;
712 ladish_virtualizer_handle ladish_studio_get_virtualizer(void)
714 return g_studio.virtualizer;
717 ladish_graph_handle ladish_studio_get_jack_graph(void)
719 return g_studio.jack_graph;
722 ladish_graph_handle ladish_studio_get_studio_graph(void)
724 return g_studio.studio_graph;
727 ladish_app_supervisor_handle ladish_studio_get_studio_app_supervisor(void)
729 return g_studio.app_supervisor;
732 struct ladish_studio_app_supervisor_match_context
734 const char * opath;
735 ladish_app_supervisor_handle supervisor;
738 #define iterate_context_ptr ((struct ladish_studio_app_supervisor_match_context *)context)
740 static bool ladish_studio_app_supervisor_match(void * context, ladish_graph_handle graph, ladish_app_supervisor_handle app_supervisor)
742 ASSERT(strcmp(ladish_app_supervisor_get_opath(app_supervisor), ladish_graph_get_opath(graph)) == 0);
743 if (strcmp(ladish_app_supervisor_get_opath(app_supervisor), iterate_context_ptr->opath) == 0)
745 ASSERT(iterate_context_ptr->supervisor == NULL);
746 iterate_context_ptr->supervisor = app_supervisor;
747 return false; /* stop iteration */
750 return true; /* continue iteration */
753 #undef iterate_context_ptr
755 ladish_app_supervisor_handle ladish_studio_find_app_supervisor(const char * opath)
757 struct ladish_studio_app_supervisor_match_context ctx;
759 ctx.opath = opath;
760 ctx.supervisor = NULL;
761 ladish_studio_iterate_virtual_graphs(&ctx, ladish_studio_app_supervisor_match);
762 return ctx.supervisor;
765 bool ladish_studios_iterate(void * call_ptr, void * context, bool (* callback)(void * call_ptr, void * context, const char * studio, uint32_t modtime))
767 DIR * dir;
768 struct dirent * dentry;
769 size_t len;
770 struct stat st;
771 char * path;
772 char * name;
774 dir = opendir(g_studios_dir);
775 if (dir == NULL)
777 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "Cannot open directory '%s': %d (%s)", g_studios_dir, errno, strerror(errno));
778 return false;
781 while ((dentry = readdir(dir)) != NULL)
783 len = strlen(dentry->d_name);
784 if (len <= 4 || strcmp(dentry->d_name + (len - 4), ".xml") != 0)
785 continue;
787 path = catdup(g_studios_dir, dentry->d_name);
788 if (path == NULL)
790 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "catdup() failed");
791 return false;
794 if (stat(path, &st) != 0)
796 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "failed to stat '%s': %d (%s)", path, errno, strerror(errno));
797 free(path);
798 return false;
801 free(path);
803 if (!S_ISREG(st.st_mode))
805 //log_info("Ignoring direntry that is not regular file. Mode is %07o", st.st_mode);
806 continue;
809 name = malloc(len - 4 + 1);
810 if (name == NULL)
812 log_error("malloc() failed.");
813 closedir(dir);
814 return false;
817 name[unescape(dentry->d_name, len - 4, name)] = 0;
818 //log_info("name = '%s'", name);
820 if (!callback(call_ptr, context, name, st.st_mtime))
822 free(name);
823 closedir(dir);
824 return false;
827 free(name);
830 closedir(dir);
831 return true;
834 bool ladish_studio_delete(void * call_ptr, const char * studio_name)
836 char * filename;
837 char * bak_filename;
838 struct stat st;
839 bool ret;
841 ret = false;
843 if (!ladish_studio_compose_filename(studio_name, &filename, &bak_filename))
845 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "failed to compose studio filename");
846 goto exit;
849 log_info("Deleting studio ('%s')", filename);
851 if (unlink(filename) != 0)
853 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "unlink(%s) failed: %d (%s)", filename, errno, strerror(errno));
854 goto free;
857 /* try to delete the backup file */
858 if (stat(bak_filename, &st) == 0)
860 if (unlink(bak_filename) != 0)
862 /* failing to delete backup file will not case delete command failure */
863 log_error("unlink(%s) failed: %d (%s)", bak_filename, errno, strerror(errno));
867 ret = true;
869 free:
870 free(filename);
871 free(bak_filename);
872 exit:
873 return ret;
876 bool
877 ladish_studio_iterate_virtual_graphs(
878 void * context,
879 bool (* callback)(
880 void * context,
881 ladish_graph_handle graph,
882 ladish_app_supervisor_handle app_supervisor))
884 struct list_head * node_ptr;
885 ladish_room_handle room;
886 ladish_app_supervisor_handle room_app_supervisor;
887 ladish_graph_handle room_graph;
889 if (!callback(context, g_studio.studio_graph, g_studio.app_supervisor))
891 return false;
894 list_for_each(node_ptr, &g_studio.rooms)
896 room = ladish_room_from_list_node(node_ptr);
897 room_app_supervisor = ladish_room_get_app_supervisor(room);
898 ASSERT(room_app_supervisor != NULL);
899 room_graph = ladish_room_get_graph(room);
901 if (!callback(context, room_graph, room_app_supervisor))
903 return false;
907 return true;
910 bool
911 ladish_studio_iterate_rooms(
912 void * context,
913 bool (* callback)(
914 void * context,
915 ladish_room_handle room))
917 struct list_head * node_ptr;
918 ladish_room_handle room;
920 list_for_each(node_ptr, &g_studio.rooms)
922 room = ladish_room_from_list_node(node_ptr);
923 if (!callback(context, room))
925 return false;
929 return true;
932 bool ladish_studio_has_rooms(void)
934 return !list_empty(&g_studio.rooms);
937 static bool ladish_studio_stop_app_supervisor(void * context, ladish_graph_handle graph, ladish_app_supervisor_handle app_supervisor)
939 ladish_app_supervisor_stop(app_supervisor);
940 return true; /* iterate all supervisors */
943 void ladish_studio_stop_app_supervisors(void)
945 ladish_studio_iterate_virtual_graphs(NULL, ladish_studio_stop_app_supervisor);
948 void ladish_studio_emit_renamed(void)
950 dbus_signal_emit(g_dbus_connection, STUDIO_OBJECT_PATH, IFACE_STUDIO, "StudioRenamed", "s", &g_studio.name);
953 unsigned int ladish_studio_get_room_index(void)
955 return ++g_studio.room_count;
958 void ladish_studio_release_room_index(unsigned int index)
960 if (index == g_studio.room_count)
962 g_studio.room_count--;
966 void ladish_studio_remove_all_rooms(void)
968 while (!list_empty(&g_studio.rooms))
970 ladish_room_destroy(ladish_room_from_list_node(g_studio.rooms.next));
974 ladish_room_handle ladish_studio_find_room_by_uuid(const uuid_t room_uuid_ptr)
976 struct list_head * node_ptr;
977 ladish_room_handle room;
978 uuid_t room_uuid;
980 list_for_each(node_ptr, &g_studio.rooms)
982 room = ladish_room_from_list_node(node_ptr);
983 ladish_room_get_uuid(room, room_uuid);
984 if (uuid_compare(room_uuid, room_uuid_ptr) == 0)
986 return room;
990 return NULL;
993 /**********************************************************************************/
994 /* D-Bus methods */
995 /**********************************************************************************/
997 static void ladish_studio_dbus_get_name(struct dbus_method_call * call_ptr)
999 method_return_new_single(call_ptr, DBUS_TYPE_STRING, &g_studio.name);
1002 static void ladish_studio_dbus_rename(struct dbus_method_call * call_ptr)
1004 const char * new_name;
1005 char * new_name_dup;
1007 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &new_name, DBUS_TYPE_INVALID))
1009 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
1010 dbus_error_free(&g_dbus_error);
1011 return;
1014 log_info("Rename studio request (%s)", new_name);
1016 new_name_dup = strdup(new_name);
1017 if (new_name_dup == NULL)
1019 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup() failed to allocate new name.");
1020 return;
1023 free(g_studio.name);
1024 g_studio.name = new_name_dup;
1026 method_return_new_void(call_ptr);
1027 ladish_studio_emit_renamed();
1030 static void ladish_studio_dbus_save(struct dbus_method_call * call_ptr)
1032 log_info("Save studio request");
1034 if (ladish_command_save_studio(call_ptr, &g_studio.cmd_queue, g_studio.name))
1036 method_return_new_void(call_ptr);
1040 static void ladish_studio_dbus_save_as(struct dbus_method_call * call_ptr)
1042 const char * new_name;
1044 log_info("SaveAs studio request");
1046 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &new_name, DBUS_TYPE_INVALID))
1048 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
1049 dbus_error_free(&g_dbus_error);
1050 return;
1053 if (ladish_command_save_studio(call_ptr, &g_studio.cmd_queue, new_name))
1055 method_return_new_void(call_ptr);
1059 static void ladish_studio_dbus_unload(struct dbus_method_call * call_ptr)
1061 log_info("Unload studio request");
1063 if (ladish_command_unload_studio(call_ptr, &g_studio.cmd_queue))
1065 method_return_new_void(call_ptr);
1069 static void ladish_studio_dbus_stop(struct dbus_method_call * call_ptr)
1071 log_info("Stop studio request");
1073 g_studio.automatic = false; /* even if it was automatic, it is not anymore because user knows about it */
1075 if (ladish_command_stop_studio(call_ptr, &g_studio.cmd_queue))
1077 method_return_new_void(call_ptr);
1081 static void ladish_studio_dbus_start(struct dbus_method_call * call_ptr)
1083 log_info("Start studio request");
1085 g_studio.automatic = false; /* even if it was automatic, it is not anymore because user knows about it */
1087 if (ladish_command_start_studio(call_ptr, &g_studio.cmd_queue))
1089 method_return_new_void(call_ptr);
1093 static void ladish_studio_dbus_is_started(struct dbus_method_call * call_ptr)
1095 dbus_bool_t started;
1097 started = g_studio.jack_graph_proxy != NULL;
1099 method_return_new_single(call_ptr, DBUS_TYPE_BOOLEAN, &started);
1102 static void ladish_studio_dbus_create_room(struct dbus_method_call * call_ptr)
1104 const char * room_name;
1105 const char * template_name;
1107 dbus_error_init(&g_dbus_error);
1109 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &room_name, DBUS_TYPE_STRING, &template_name, DBUS_TYPE_INVALID))
1111 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
1112 dbus_error_free(&g_dbus_error);
1113 return;
1116 if (ladish_command_create_room(call_ptr, ladish_studio_get_cmd_queue(), room_name, template_name))
1118 method_return_new_void(call_ptr);
1122 static void ladish_studio_dbus_get_room_list(struct dbus_method_call * call_ptr)
1124 DBusMessageIter iter, array_iter;
1125 DBusMessageIter struct_iter;
1126 struct list_head * node_ptr;
1127 ladish_room_handle room;
1129 call_ptr->reply = dbus_message_new_method_return(call_ptr->message);
1130 if (call_ptr->reply == NULL)
1132 goto fail;
1135 dbus_message_iter_init_append(call_ptr->reply, &iter);
1137 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sa{sv})", &array_iter))
1139 goto fail_unref;
1142 list_for_each(node_ptr, &g_studio.rooms)
1144 room = ladish_room_from_list_node(node_ptr);
1146 if (!dbus_message_iter_open_container(&array_iter, DBUS_TYPE_STRUCT, NULL, &struct_iter))
1147 goto fail_unref;
1149 if (!ladish_studio_fill_room_info(&struct_iter, room))
1150 goto fail_unref;
1152 if (!dbus_message_iter_close_container(&array_iter, &struct_iter))
1153 goto fail_unref;
1156 if (!dbus_message_iter_close_container(&iter, &array_iter))
1158 goto fail_unref;
1161 return;
1163 fail_unref:
1164 dbus_message_unref(call_ptr->reply);
1165 call_ptr->reply = NULL;
1167 fail:
1168 log_error("Ran out of memory trying to construct method return");
1171 static void ladish_studio_dbus_delete_room(struct dbus_method_call * call_ptr)
1173 const char * name;
1175 dbus_error_init(&g_dbus_error);
1177 if (!dbus_message_get_args(call_ptr->message, &g_dbus_error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
1179 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_INVALID_ARGS, "Invalid arguments to method \"%s\": %s", call_ptr->method_name, g_dbus_error.message);
1180 dbus_error_free(&g_dbus_error);
1181 return;
1184 if (ladish_command_delete_room(call_ptr, ladish_studio_get_cmd_queue(), name))
1186 method_return_new_void(call_ptr);
1190 METHOD_ARGS_BEGIN(GetName, "Get studio name")
1191 METHOD_ARG_DESCRIBE_OUT("studio_name", "s", "Name of studio")
1192 METHOD_ARGS_END
1194 METHOD_ARGS_BEGIN(Rename, "Rename studio")
1195 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "New name")
1196 METHOD_ARGS_END
1198 METHOD_ARGS_BEGIN(Save, "Save studio")
1199 METHOD_ARGS_END
1201 METHOD_ARGS_BEGIN(SaveAs, "SaveAs studio")
1202 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "New name")
1203 METHOD_ARGS_END
1205 METHOD_ARGS_BEGIN(Unload, "Unload studio")
1206 METHOD_ARGS_END
1208 METHOD_ARGS_BEGIN(Start, "Start studio")
1209 METHOD_ARGS_END
1211 METHOD_ARGS_BEGIN(Stop, "Stop studio")
1212 METHOD_ARGS_END
1214 METHOD_ARGS_BEGIN(IsStarted, "Check whether studio is started")
1215 METHOD_ARG_DESCRIBE_OUT("started", "b", "Whether studio is started")
1216 METHOD_ARGS_END
1218 METHOD_ARGS_BEGIN(CreateRoom, "Create new studio room")
1219 METHOD_ARG_DESCRIBE_IN("room_name", "s", "Studio room name")
1220 METHOD_ARG_DESCRIBE_IN("room_template_name", "s", "Room template name")
1221 METHOD_ARGS_END
1223 METHOD_ARGS_BEGIN(GetRoomList, "Get list of rooms in this studio")
1224 METHOD_ARG_DESCRIBE_OUT("room_list", "a(sa{sv})", "List of studio rooms: opaths and properties")
1225 METHOD_ARGS_END
1227 METHOD_ARGS_BEGIN(DeleteRoom, "Delete studio room")
1228 METHOD_ARG_DESCRIBE_IN("room_name", "s", "Name of studio room to delete")
1229 METHOD_ARGS_END
1231 METHODS_BEGIN
1232 METHOD_DESCRIBE(GetName, ladish_studio_dbus_get_name) /* sync */
1233 METHOD_DESCRIBE(Rename, ladish_studio_dbus_rename) /* sync */
1234 METHOD_DESCRIBE(Save, ladish_studio_dbus_save) /* async */
1235 METHOD_DESCRIBE(SaveAs, ladish_studio_dbus_save_as) /* async */
1236 METHOD_DESCRIBE(Unload, ladish_studio_dbus_unload) /* async */
1237 METHOD_DESCRIBE(Start, ladish_studio_dbus_start) /* async */
1238 METHOD_DESCRIBE(Stop, ladish_studio_dbus_stop) /* async */
1239 METHOD_DESCRIBE(IsStarted, ladish_studio_dbus_is_started) /* sync */
1240 METHOD_DESCRIBE(CreateRoom, ladish_studio_dbus_create_room) /* async */
1241 METHOD_DESCRIBE(GetRoomList, ladish_studio_dbus_get_room_list) /* sync */
1242 METHOD_DESCRIBE(DeleteRoom, ladish_studio_dbus_delete_room) /* async */
1243 METHODS_END
1245 SIGNAL_ARGS_BEGIN(StudioRenamed, "Studio name changed")
1246 SIGNAL_ARG_DESCRIBE("studio_name", "s", "New studio name")
1247 SIGNAL_ARGS_END
1249 SIGNAL_ARGS_BEGIN(StudioStarted, "Studio started")
1250 SIGNAL_ARGS_END
1252 SIGNAL_ARGS_BEGIN(StudioCrashed, "Studio crashed")
1253 SIGNAL_ARGS_END
1255 SIGNAL_ARGS_BEGIN(StudioStopped, "Studio stopped")
1256 SIGNAL_ARGS_END
1258 SIGNAL_ARGS_BEGIN(RoomAppeared, "Room D-Bus object appeared")
1259 SIGNAL_ARG_DESCRIBE("opath", "s", "room object path")
1260 SIGNAL_ARG_DESCRIBE("properties", "a{sv}", "room properties")
1261 SIGNAL_ARGS_END
1263 SIGNAL_ARGS_BEGIN(RoomChanged, "Room D-Bus object changed")
1264 SIGNAL_ARG_DESCRIBE("opath", "s", "room object path")
1265 SIGNAL_ARG_DESCRIBE("properties", "a{sv}", "room properties")
1266 SIGNAL_ARGS_END
1268 SIGNAL_ARGS_BEGIN(RoomDisappeared, "Room D-Bus object disappeared")
1269 SIGNAL_ARG_DESCRIBE("opath", "s", "room object path")
1270 SIGNAL_ARG_DESCRIBE("properties", "a{sv}", "room properties")
1271 SIGNAL_ARGS_END
1273 SIGNALS_BEGIN
1274 SIGNAL_DESCRIBE(StudioRenamed)
1275 SIGNAL_DESCRIBE(StudioStarted)
1276 SIGNAL_DESCRIBE(StudioCrashed)
1277 SIGNAL_DESCRIBE(StudioStopped)
1278 SIGNAL_DESCRIBE(RoomAppeared)
1279 SIGNAL_DESCRIBE(RoomDisappeared)
1280 SIGNAL_DESCRIBE(RoomChanged)
1281 SIGNALS_END
1283 INTERFACE_BEGIN(g_interface_studio, IFACE_STUDIO)
1284 INTERFACE_DEFAULT_HANDLER
1285 INTERFACE_EXPOSE_METHODS
1286 INTERFACE_EXPOSE_SIGNALS
1287 INTERFACE_END