1 /* -*- Mode: C ; c-basic-offset: 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
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
[] =
52 #define proxy_ptr ((struct ladish_app_supervisor_proxy *)proxy)
57 DBusConnection
* connection
,
58 DBusMessage
* message
,
61 const char * object_path
;
62 uint64_t new_list_version
;
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(
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
,
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(
103 DBUS_TYPE_UINT64
, &new_list_version
,
104 DBUS_TYPE_UINT64
, &id
,
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(
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
,
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
;
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
;
155 dbus_bool_t terminal
;
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.");
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
);
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
)
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
);
218 dbus_message_unref(reply_ptr
);
222 ladish_app_supervisor_proxy_create(
223 const char * service
,
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");
240 proxy_ptr
->service
= strdup(service
);
241 if (proxy_ptr
->service
== NULL
)
243 log_error("strdup() failed too duplicate service name '%s'", service
);
247 proxy_ptr
->object
= strdup(object
);
248 if (proxy_ptr
->object
== NULL
)
250 log_error("strdup() failed too duplicate object name '%s'", object
);
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(
265 IFACE_APP_SUPERVISOR
,
273 refresh_internal(proxy_ptr
, true);
275 *handle_ptr
= (ladish_app_supervisor_proxy_handle
)proxy_ptr
;
280 free(proxy_ptr
->service
);
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(
297 IFACE_APP_SUPERVISOR
,
302 free(proxy_ptr
->object
);
303 free(proxy_ptr
->service
);
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.");
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.");
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.");
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.");
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.");