daemon: handle apps with same name in different vgraphs
[ladish.git] / daemon / cmd_start_studio.c
blob585164e81ec6a04b567af2a6c2119f97eb864328
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 implementation of the "start studio" command
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <unistd.h>
29 #include "cmd.h"
30 #include "studio_internal.h"
31 #include "loader.h"
32 #include "../common/time.h"
33 #include "../proxies/notify_proxy.h"
35 struct ladish_command_start_studio
37 struct ladish_command command;
38 uint64_t deadline;
41 static bool start_room(void * context, ladish_room_handle room)
43 return ladish_room_start(room, context);
46 #define cmd_ptr ((struct ladish_command_start_studio *)context)
48 static bool run(void * context)
50 bool jack_server_started;
51 unsigned int app_count;
53 switch (cmd_ptr->command.state)
55 case LADISH_COMMAND_STATE_PENDING:
56 if (ladish_studio_is_started())
58 log_info("Ignoring start request because studio is already started.");
59 /* nothing to do, studio is already running */
60 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
61 return true;
64 app_count = loader_get_app_count();
65 if (app_count != 0)
67 log_error("Ignoring start request because there are apps running.");
68 log_error("This could happen when JACK has crashed or when JACK stopped unexpectedly.");
69 log_error("Save your work, then unload and reload the studio.");
70 return false;
73 log_info("Starting JACK server.");
75 ladish_graph_dump(g_studio.studio_graph);
77 if (!jack_proxy_start_server())
79 log_error("Starting JACK server failed.");
80 ladish_notify_simple(
81 LADISH_NOTIFY_URGENCY_HIGH,
82 "JACK start failed",
83 "You may want to inspect the <a href=\"http://ladish.org/wiki/diagnose_files\">JACK log file</a>\n\n"
84 "Check if the sound device is connected.\n"
85 "Check the JACK configuration.\n"
86 "If your sound device does not support hardware mixing, check that it is not in use.\n"
87 "If you have more than one sound device, check that their order is correct.");
88 return false;
91 cmd_ptr->deadline = ladish_get_current_microseconds();
92 if (cmd_ptr->deadline != 0)
94 cmd_ptr->deadline += 5000000;
97 cmd_ptr->command.state = LADISH_COMMAND_STATE_WAITING;
98 /* fall through */
99 case LADISH_COMMAND_STATE_WAITING:
100 if (!ladish_environment_consume_change(&g_studio.env_store, ladish_environment_jack_server_started, &jack_server_started))
102 /* we are still waiting for the JACK server start */
103 ASSERT(!ladish_environment_get(&g_studio.env_store, ladish_environment_jack_server_started)); /* someone else consumed the state change? */
105 if (cmd_ptr->deadline != 0 && ladish_get_current_microseconds() >= cmd_ptr->deadline)
107 log_error("Starting JACK server succeded, but 'started' signal was not received within 5 seconds.");
108 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
109 return false;
112 return true;
115 log_info("Wait for JACK server start complete.");
117 ASSERT(jack_server_started);
119 ladish_studio_on_event_jack_started(); /* fetch configuration and announce start */
121 ladish_studio_iterate_rooms(ladish_studio_get_virtualizer(), start_room);
123 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
124 return true;
127 ASSERT_NO_PASS;
128 return false;
131 #undef cmd_ptr
133 bool ladish_command_start_studio(void * call_ptr, struct ladish_cqueue * queue_ptr)
135 struct ladish_command_start_studio * cmd_ptr;
137 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_start_studio));
138 if (cmd_ptr == NULL)
140 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_command_new() failed.");
141 goto fail;
144 cmd_ptr->command.run = run;
145 cmd_ptr->deadline = 0;
147 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
149 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
150 goto fail_destroy_command;
153 return true;
155 fail_destroy_command:
156 free(cmd_ptr);
158 fail:
159 return false;