daemon: set proper state after JACK server stop is detected in the hard way
[ladish.git] / gui / app_supervisor_proxy.c
blob3fafa73f1ad76bb81389e0143aa7feb2f5fd3e54
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 code that interfaces
9 * app supervisor object through D-Bus
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 "app_supervisor_proxy.h"
29 #include "../dbus/helpers.h"
30 #include "../dbus_constants.h"
32 struct ladish_app_supervisor_proxy
34 char * service;
35 char * object;
36 uint64_t version;
38 void * context;
39 void (* app_added)(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level);
40 void (* app_state_changed)(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level);
41 void (* app_removed)(void * context, uint64_t id);
44 static const char * g_signals[] =
46 "AppAdded",
47 "AppRemoved",
48 "AppStateChanged",
49 NULL
52 #define proxy_ptr ((struct ladish_app_supervisor_proxy *)proxy)
54 static
55 DBusHandlerResult
56 message_hook(
57 DBusConnection * connection,
58 DBusMessage * message,
59 void * proxy)
61 const char * object_path;
62 uint64_t new_list_version;
63 uint64_t id;
64 const char * name;
65 dbus_bool_t running;
66 dbus_bool_t terminal;
67 uint8_t level;
69 object_path = dbus_message_get_path(message);
70 if (object_path == NULL || strcmp(object_path, proxy_ptr->object) != 0)
72 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
75 if (dbus_message_is_signal(message, IFACE_APP_SUPERVISOR, "AppAdded"))
77 if (!dbus_message_get_args(
78 message,
79 &g_dbus_error,
80 DBUS_TYPE_UINT64, &new_list_version,
81 DBUS_TYPE_UINT64, &id,
82 DBUS_TYPE_STRING, &name,
83 DBUS_TYPE_BOOLEAN, &running,
84 DBUS_TYPE_BOOLEAN, &terminal,
85 DBUS_TYPE_BYTE, &level,
86 DBUS_TYPE_INVALID))
88 log_error("dbus_message_get_args() failed to extract AppAdded signal arguments (%s)", g_dbus_error.message);
89 dbus_error_free(&g_dbus_error);
90 return DBUS_HANDLER_RESULT_HANDLED;
93 //log_info("AppAdded signal received. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
94 proxy_ptr->app_added(proxy_ptr->context, id, name, running, terminal, level);
95 return DBUS_HANDLER_RESULT_HANDLED;
98 if (dbus_message_is_signal(message, IFACE_APP_SUPERVISOR, "AppRemoved"))
100 if (!dbus_message_get_args(
101 message,
102 &g_dbus_error,
103 DBUS_TYPE_UINT64, &new_list_version,
104 DBUS_TYPE_UINT64, &id,
105 DBUS_TYPE_INVALID))
107 log_error("dbus_message_get_args() failed to extract AppRemoved signal arguments (%s)", g_dbus_error.message);
108 dbus_error_free(&g_dbus_error);
109 return DBUS_HANDLER_RESULT_HANDLED;
112 //log_info("AppRemoved signal received, id=%"PRIu64, id);
113 proxy_ptr->app_removed(proxy_ptr->context, id);
114 return DBUS_HANDLER_RESULT_HANDLED;
117 if (dbus_message_is_signal(message, IFACE_APP_SUPERVISOR, "AppStateChanged"))
119 if (!dbus_message_get_args(
120 message,
121 &g_dbus_error,
122 DBUS_TYPE_UINT64, &id,
123 DBUS_TYPE_STRING, &name,
124 DBUS_TYPE_BOOLEAN, &running,
125 DBUS_TYPE_BOOLEAN, &terminal,
126 DBUS_TYPE_BYTE, &level,
127 DBUS_TYPE_INVALID))
129 log_error("dbus_message_get_args() failed to extract AppStateChanged signal arguments (%s)", g_dbus_error.message);
130 dbus_error_free(&g_dbus_error);
131 return DBUS_HANDLER_RESULT_HANDLED;
134 //log_info("AppStateChanged signal received");
135 proxy_ptr->app_state_changed(proxy_ptr->context, id, name, running, terminal, level);
136 return DBUS_HANDLER_RESULT_HANDLED;
139 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
142 #undef proxy_ptr
144 static void refresh_internal(struct ladish_app_supervisor_proxy * proxy_ptr, bool force)
146 DBusMessage* reply_ptr;
147 DBusMessageIter iter;
148 dbus_uint64_t version;
149 const char * reply_signature;
150 DBusMessageIter array_iter;
151 DBusMessageIter struct_iter;
152 uint64_t id;
153 const char * name;
154 dbus_bool_t running;
155 dbus_bool_t terminal;
156 uint8_t level;
158 log_info("refresh_internal() called");
160 version = proxy_ptr->version;
162 if (!dbus_call(proxy_ptr->service, proxy_ptr->object, IFACE_APP_SUPERVISOR, "GetAll", "t", &version, NULL, &reply_ptr))
164 log_error("GetAll() failed.");
165 return;
168 reply_signature = dbus_message_get_signature(reply_ptr);
170 if (strcmp(reply_signature, "ta(tsbby)") != 0)
172 log_error("GetAll() reply signature mismatch. '%s'", reply_signature);
173 goto unref;
176 dbus_message_iter_init(reply_ptr, &iter);
178 //log_info_msg("version " + (char)dbus_message_iter_get_arg_type(&iter));
179 dbus_message_iter_get_basic(&iter, &version);
180 dbus_message_iter_next(&iter);
182 if (!force && version <= proxy_ptr->version)
184 goto unref;
187 //log_info("got new list version %llu", (unsigned long long)version);
188 proxy_ptr->version = version;
190 for (dbus_message_iter_recurse(&iter, &array_iter);
191 dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID;
192 dbus_message_iter_next(&array_iter))
194 dbus_message_iter_recurse(&array_iter, &struct_iter);
196 dbus_message_iter_get_basic(&struct_iter, &id);
197 dbus_message_iter_next(&struct_iter);
199 dbus_message_iter_get_basic(&struct_iter, &name);
200 dbus_message_iter_next(&struct_iter);
202 dbus_message_iter_get_basic(&struct_iter, &running);
203 dbus_message_iter_next(&struct_iter);
205 dbus_message_iter_get_basic(&struct_iter, &terminal);
206 dbus_message_iter_next(&struct_iter);
208 dbus_message_iter_get_basic(&struct_iter, &level);
209 dbus_message_iter_next(&struct_iter);
211 //log_info("App id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
212 proxy_ptr->app_added(proxy_ptr->context, id, name, running, terminal, level);
214 dbus_message_iter_next(&struct_iter);
217 unref:
218 dbus_message_unref(reply_ptr);
221 bool
222 ladish_app_supervisor_proxy_create(
223 const char * service,
224 const char * object,
225 void * context,
226 void (* app_added)(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level),
227 void (* app_state_changed)(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level),
228 void (* app_removed)(void * context, uint64_t id),
229 ladish_app_supervisor_proxy_handle * handle_ptr)
231 struct ladish_app_supervisor_proxy * proxy_ptr;
233 proxy_ptr = malloc(sizeof(struct ladish_app_supervisor_proxy));
234 if (proxy_ptr == NULL)
236 log_error("malloc() failed to allocate struct proxy");
237 goto fail;
240 proxy_ptr->service = strdup(service);
241 if (proxy_ptr->service == NULL)
243 log_error("strdup() failed too duplicate service name '%s'", service);
244 goto free_proxy;
247 proxy_ptr->object = strdup(object);
248 if (proxy_ptr->object == NULL)
250 log_error("strdup() failed too duplicate object name '%s'", object);
251 goto free_service;
254 proxy_ptr->version = 0;
256 proxy_ptr->context = context;
257 proxy_ptr->app_added = app_added;
258 proxy_ptr->app_state_changed = app_state_changed;
259 proxy_ptr->app_removed = app_removed;
261 if (!dbus_register_object_signal_handler(
262 g_dbus_connection,
263 proxy_ptr->service,
264 proxy_ptr->object,
265 IFACE_APP_SUPERVISOR,
266 g_signals,
267 message_hook,
268 proxy_ptr))
270 return false;
273 refresh_internal(proxy_ptr, true);
275 *handle_ptr = (ladish_app_supervisor_proxy_handle)proxy_ptr;
277 return true;
279 free_service:
280 free(proxy_ptr->service);
282 free_proxy:
283 free(proxy_ptr);
285 fail:
286 return false;
289 #define proxy_ptr ((struct ladish_app_supervisor_proxy *)proxy)
291 void ladish_app_supervisor_proxy_destroy(ladish_app_supervisor_proxy_handle proxy)
293 dbus_unregister_object_signal_handler(
294 g_dbus_connection,
295 proxy_ptr->service,
296 proxy_ptr->object,
297 IFACE_APP_SUPERVISOR,
298 g_signals,
299 message_hook,
300 proxy_ptr);
302 free(proxy_ptr->object);
303 free(proxy_ptr->service);
304 free(proxy_ptr);
307 bool ladish_app_supervisor_proxy_run_custom(ladish_app_supervisor_proxy_handle proxy, const char * command, const char * name, bool run_in_terminal)
309 dbus_bool_t terminal;
311 terminal = run_in_terminal;
313 if (!dbus_call(proxy_ptr->service, proxy_ptr->object, IFACE_APP_SUPERVISOR, "RunCustom", "bss", &terminal, &command, &name, ""))
315 log_error("RunCustom() failed.");
316 return false;
319 return true;
322 bool ladish_app_supervisor_proxy_start_app(ladish_app_supervisor_proxy_handle proxy, uint64_t id)
324 if (!dbus_call(proxy_ptr->service, proxy_ptr->object, IFACE_APP_SUPERVISOR, "StartApp", "t", &id, ""))
326 log_error("StartApp() failed.");
327 return false;
330 return true;
333 bool ladish_app_supervisor_proxy_stop_app(ladish_app_supervisor_proxy_handle proxy, uint64_t id)
335 if (!dbus_call(proxy_ptr->service, proxy_ptr->object, IFACE_APP_SUPERVISOR, "StopApp", "t", &id, ""))
337 log_error("StopApp() failed.");
338 return false;
341 return true;
344 bool ladish_app_supervisor_proxy_kill_app(ladish_app_supervisor_proxy_handle proxy, uint64_t id)
346 if (!dbus_call(proxy_ptr->service, proxy_ptr->object, IFACE_APP_SUPERVISOR, "KillApp", "t", &id, ""))
348 log_error("KillApp() failed.");
349 return false;
352 return true;
355 bool ladish_app_supervisor_proxy_remove_app(ladish_app_supervisor_proxy_handle proxy, uint64_t id)
357 if (!dbus_call(proxy_ptr->service, proxy_ptr->object, IFACE_APP_SUPERVISOR, "RemoveApp", "t", &id, ""))
359 log_error("RemoveApp() failed.");
360 return false;
363 return true;
366 #undef proxy_ptr