daemon: fix memory leaks on error conditions
[ladish.git] / daemon / cmd_start_studio.c
blob44b28f91c8c704c44d561ffce0cb2871c592ea42
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 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 "cmd.h"
28 #include "studio_internal.h"
29 #include "loader.h"
31 #define cmd_ptr ((struct ladish_command *)context)
33 static bool run(void * context)
35 bool jack_server_started;
36 unsigned int app_count;
38 switch (cmd_ptr->state)
40 case LADISH_COMMAND_STATE_PENDING:
41 if (studio_is_started())
43 log_info("Ignoring start request because studio is already started.");
44 /* nothing to do, studio is already running */
45 cmd_ptr->state = LADISH_COMMAND_STATE_DONE;
46 return true;
49 app_count = loader_get_app_count();
50 if (app_count != 0)
52 log_error("Ignoring start request because there are apps running.");
53 log_error("This could happen when JACK has crashed or when JACK stopped unexpectedly.");
54 log_error("Save your work, then unload and reload the studio.");
55 return false;
58 log_info("Starting JACK server.");
60 ladish_graph_dump(g_studio.studio_graph);
62 if (!jack_proxy_start_server())
64 log_error("Starting JACK server failed.");
65 return false;
68 cmd_ptr->state = LADISH_COMMAND_STATE_WAITING;
69 /* fall through */
70 case LADISH_COMMAND_STATE_WAITING:
71 if (!ladish_environment_consume_change(&g_studio.env_store, ladish_environment_jack_server_started, &jack_server_started))
73 /* we are still waiting for the JACK server start */
74 ASSERT(!ladish_environment_get(&g_studio.env_store, ladish_environment_jack_server_started)); /* someone else consumed the state change? */
75 return true;
78 log_info("Wait for JACK server start complete.");
80 ASSERT(jack_server_started);
82 on_event_jack_started(); /* fetch configuration and announce start */
84 cmd_ptr->state = LADISH_COMMAND_STATE_DONE;
85 return true;
88 ASSERT_NO_PASS;
89 return false;
92 #undef cmd_ptr
94 bool ladish_command_start_studio(void * call_ptr, struct ladish_cqueue * queue_ptr)
96 struct ladish_command * cmd_ptr;
98 cmd_ptr = ladish_command_new(sizeof(struct ladish_command));
99 if (cmd_ptr == NULL)
101 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_command_new() failed.");
102 goto fail;
105 cmd_ptr->run = run;
107 if (!ladish_cqueue_add_command(queue_ptr, cmd_ptr))
109 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
110 goto fail_destroy_command;
113 return true;
115 fail_destroy_command:
116 free(cmd_ptr);
118 fail:
119 return false;