1 /* -*- Mode: C ; c-basic-offset: 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.
30 #include <sys/types.h>
35 #include "studio_internal.h"
36 #include "../dbus_constants.h"
38 #include "../catdup.h"
39 #include "dirhelpers.h"
40 #include "graph_dict.h"
43 #include "../proxies/notify_proxy.h"
45 #define STUDIOS_DIR "/studios/"
48 struct studio g_studio
;
50 bool ladish_studio_name_generate(char ** name_ptr
)
53 char timestamp_str
[26];
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
);
64 log_error("catdup failed to create studio name");
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(
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
,
88 log_error("dbus_object_path_new() failed");
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
);
99 log_info("Studio D-Bus object created. \"%s\"", g_studio
.name
);
101 g_studio
.dbus_object
= object
;
103 emit_studio_appeared();
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())
122 ladish_studio_announce();
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
)
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
;
189 uuid_t template_uuid
;
190 ladish_room_handle
template;
191 const char * template_name
;
194 name
= ladish_room_get_name(room
);
195 opath
= ladish_room_get_opath(room
);
197 if (!ladish_room_get_template_uuid(room
, template_uuid
))
200 template_name
= NULL
;
204 template = find_room_template_by_uuid(template_uuid
);
205 if (template != NULL
)
207 template_name
= ladish_room_get_name(template);
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.");
221 if (!dbus_message_iter_open_container(iter_ptr
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
))
223 log_error("dbus_message_iter_open_container() failed.");
227 if (!dbus_maybe_add_dict_entry_string(&dict_iter
, "template", template_name
))
229 log_error("dbus_maybe_add_dict_entry_string() failed.");
233 if (!dbus_maybe_add_dict_entry_string(&dict_iter
, "name", name
))
235 log_error("dbus_maybe_add_dict_entry_string() failed.");
239 if (!dbus_message_iter_close_container(iter_ptr
, &dict_iter
))
241 log_error("dbus_message_iter_close_container() failed.");
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.");
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.");
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
);
307 ladish_studio_set_graph_connection_handlers(
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.");
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");
334 if (!ladish_virtualizer_create(g_studio
.jack_graph_proxy
, g_studio
.jack_graph
, &g_studio
.virtualizer
))
336 log_error("ladish_virtualizer_create() failed.");
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)
395 ladish_cqueue_run(&g_studio
.cmd_queue
);
397 { /* if quit is requested, don't bother to process external events */
401 if (ladish_environment_consume_change(&g_studio
.env_store
, ladish_environment_jack_server_started
, &state
))
403 ladish_cqueue_clear(&g_studio
.cmd_queue
);
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.");
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.");
425 g_studio
.automatic
= true;
427 ladish_studio_publish();
430 ladish_studio_on_event_jack_started();
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.");
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();
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
,
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
,
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
);
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
;
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
);
586 ladish_graph_destroy(g_studio
.jack_graph
);
593 void ladish_studio_uninit(void)
595 log_info("studio_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
);
606 log_info("studio object destroy");
609 struct on_child_exit_context
615 #define child_exit_context_ptr ((struct on_child_exit_context *)context)
619 ladish_studio_on_child_exit_callback(
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
;
637 context
.found
= false;
639 ladish_studio_iterate_virtual_graphs(&context
, ladish_studio_on_child_exit_callback
);
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
)
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");
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");
686 memcpy(p
, g_studios_dir
, len_dir
);
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
;
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
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
;
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
))
768 struct dirent
* dentry
;
774 dir
= opendir(g_studios_dir
);
777 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "Cannot open directory '%s': %d (%s)", g_studios_dir
, errno
, strerror(errno
));
781 while ((dentry
= readdir(dir
)) != NULL
)
783 len
= strlen(dentry
->d_name
);
784 if (len
<= 4 || strcmp(dentry
->d_name
+ (len
- 4), ".xml") != 0)
787 path
= catdup(g_studios_dir
, dentry
->d_name
);
790 lash_dbus_error(call_ptr
, LASH_DBUS_ERROR_GENERIC
, "catdup() failed");
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
));
803 if (!S_ISREG(st
.st_mode
))
805 //log_info("Ignoring direntry that is not regular file. Mode is %07o", st.st_mode);
809 name
= malloc(len
- 4 + 1);
812 log_error("malloc() failed.");
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
))
834 bool ladish_studio_delete(void * call_ptr
, const char * studio_name
)
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");
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
));
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
));
877 ladish_studio_iterate_virtual_graphs(
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
))
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
))
911 ladish_studio_iterate_rooms(
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
))
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
;
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)
993 /**********************************************************************************/
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
);
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.");
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
);
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
);
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
)
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
))
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
))
1149 if (!ladish_studio_fill_room_info(&struct_iter
, room
))
1152 if (!dbus_message_iter_close_container(&array_iter
, &struct_iter
))
1156 if (!dbus_message_iter_close_container(&iter
, &array_iter
))
1164 dbus_message_unref(call_ptr
->reply
);
1165 call_ptr
->reply
= NULL
;
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
)
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
);
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")
1194 METHOD_ARGS_BEGIN(Rename
, "Rename studio")
1195 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "New name")
1198 METHOD_ARGS_BEGIN(Save
, "Save studio")
1201 METHOD_ARGS_BEGIN(SaveAs
, "SaveAs studio")
1202 METHOD_ARG_DESCRIBE_IN("studio_name", "s", "New name")
1205 METHOD_ARGS_BEGIN(Unload
, "Unload studio")
1208 METHOD_ARGS_BEGIN(Start
, "Start studio")
1211 METHOD_ARGS_BEGIN(Stop
, "Stop studio")
1214 METHOD_ARGS_BEGIN(IsStarted
, "Check whether studio is started")
1215 METHOD_ARG_DESCRIBE_OUT("started", "b", "Whether studio is started")
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")
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")
1227 METHOD_ARGS_BEGIN(DeleteRoom
, "Delete studio room")
1228 METHOD_ARG_DESCRIBE_IN("room_name", "s", "Name of studio room to delete")
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 */
1245 SIGNAL_ARGS_BEGIN(StudioRenamed
, "Studio name changed")
1246 SIGNAL_ARG_DESCRIBE("studio_name", "s", "New studio name")
1249 SIGNAL_ARGS_BEGIN(StudioStarted
, "Studio started")
1252 SIGNAL_ARGS_BEGIN(StudioCrashed
, "Studio crashed")
1255 SIGNAL_ARGS_BEGIN(StudioStopped
, "Studio stopped")
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")
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")
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")
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
)
1283 INTERFACE_BEGIN(g_interface_studio
, IFACE_STUDIO
)
1284 INTERFACE_DEFAULT_HANDLER
1285 INTERFACE_EXPOSE_METHODS
1286 INTERFACE_EXPOSE_SIGNALS