daemon: refuse to save studios with rooms
[ladish.git] / daemon / cmd_change_app_state.c
blob14852f2654748c446c9749350451efde41fceb0a
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the "change app state" 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 <ctype.h>
28 #include "cmd.h"
29 #include "studio.h"
30 #include "../dbus/error.h"
31 #include "../proxies/notify_proxy.h"
32 #include "virtualizer.h"
34 struct ladish_command_change_app_state
36 struct ladish_command command; /* must be the first member */
37 char * opath;
38 uint64_t id;
39 unsigned int target_state;
40 const char * target_state_description;
41 bool (* run_target)(struct ladish_command_change_app_state *, ladish_app_supervisor_handle, ladish_app_handle);
42 void (* initiate_stop)(ladish_app_handle app);
45 static bool run_target_start(struct ladish_command_change_app_state * cmd_ptr, ladish_app_supervisor_handle supervisor, ladish_app_handle app)
47 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
48 ASSERT(cmd_ptr->initiate_stop == NULL);
50 if (!ladish_studio_is_started())
52 log_error("cannot start app because studio is not started", cmd_ptr->opath);
53 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot start app because studio is not started", NULL);
54 return false;
57 if (ladish_app_is_running(app))
59 log_error("App %s is already running", ladish_app_get_name(app));
60 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot start app because it is already running", NULL);
61 return false;
64 if (!ladish_app_supervisor_start_app(supervisor, app))
66 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot start app (execution failed)", NULL);
67 return false;
70 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
71 return true;
74 static bool run_target_stop(struct ladish_command_change_app_state * cmd_ptr, ladish_app_supervisor_handle supervisor, ladish_app_handle app)
76 const char * app_name;
78 ASSERT(cmd_ptr->initiate_stop != NULL);
80 app_name = ladish_app_get_name(app);
82 if (ladish_app_is_running(app))
84 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
86 cmd_ptr->initiate_stop(app);
87 cmd_ptr->command.state = LADISH_COMMAND_STATE_WAITING;
88 return true;
91 ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_WAITING);
92 log_info("Waiting '%s' process termination (%s)...", app_name, cmd_ptr->target_state_description);
93 return true;
96 if (!ladish_virtualizer_is_hidden_app(ladish_studio_get_jack_graph(), app_name))
98 log_info("Waiting '%s' client disappear (%s)...", app_name, cmd_ptr->target_state_description);
99 return true;
102 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
104 log_info("App %s is already stopped (%s)", app_name, cmd_ptr->target_state_description);
107 cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
108 return true;
111 #define cmd_ptr ((struct ladish_command_change_app_state *)context)
113 static bool run(void * context)
115 ladish_app_supervisor_handle supervisor;
116 ladish_app_handle app;
118 if (cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING)
120 log_info("%s app command. opath='%s'", cmd_ptr->target_state_description, cmd_ptr->opath);
123 supervisor = ladish_studio_find_app_supervisor(cmd_ptr->opath);
124 if (supervisor == NULL)
126 log_error("cannot find supervisor '%s' to %s app", cmd_ptr->opath, cmd_ptr->target_state_description);
127 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot change app state because of internal error (unknown supervisor)", NULL);
128 return false;
131 app = ladish_app_supervisor_find_app_by_id(supervisor, cmd_ptr->id);
132 if (app == NULL)
134 log_error("App with ID %"PRIu64" not found (%s)", cmd_ptr->id, cmd_ptr->target_state_description);
135 ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot change app state because it is not found", NULL);
136 return false;
139 return cmd_ptr->run_target(cmd_ptr, supervisor, app);
142 static void destructor(void * context)
144 log_info("change_app_state command destructor");
145 free(cmd_ptr->opath);
148 #undef cmd_ptr
150 bool ladish_command_change_app_state(void * call_ptr, struct ladish_cqueue * queue_ptr, const char * opath, uint64_t id, unsigned int target_state)
152 struct ladish_command_change_app_state * cmd_ptr;
153 char * opath_dup;
155 opath_dup = strdup(opath);
156 if (opath_dup == NULL)
158 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", opath);
159 goto fail;
162 cmd_ptr = ladish_command_new(sizeof(struct ladish_command_change_app_state));
163 if (cmd_ptr == NULL)
165 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_command_new() failed.");
166 goto fail_free_opath;
169 cmd_ptr->command.run = run;
170 cmd_ptr->command.destructor = destructor;
171 cmd_ptr->opath = opath_dup;
172 cmd_ptr->id = id;
173 cmd_ptr->target_state = target_state;
175 switch (target_state)
177 case LADISH_APP_STATE_STARTED:
178 cmd_ptr->target_state_description = "start";
179 cmd_ptr->run_target = run_target_start;
180 cmd_ptr->initiate_stop = NULL;
181 break;
182 case LADISH_APP_STATE_STOPPED:
183 cmd_ptr->target_state_description = "stop";
184 cmd_ptr->run_target = run_target_stop;
185 cmd_ptr->initiate_stop = ladish_app_stop;
186 break;
187 case LADISH_APP_STATE_KILL:
188 cmd_ptr->target_state_description = "kill";
189 cmd_ptr->run_target = run_target_stop;
190 cmd_ptr->initiate_stop = ladish_app_kill;
191 break;
192 default:
193 ASSERT_NO_PASS;
194 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "Invalid target state (internal error).", opath);
195 goto fail_destroy_command;
198 if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
200 lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
201 goto fail_destroy_command;
204 return true;
206 fail_destroy_command:
207 free(cmd_ptr);
208 fail_free_opath:
209 free(opath_dup);
210 fail:
211 return false;