2 * Copyright (c) 2017 Panasonic Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include "cpptoml/cpptoml.h"
37 #define RUNXDG_CONFIG "runxdg.toml"
38 #define AREA_NORMAL_FULL "normal.full"
40 void fatal(const char* format, ...)
43 va_start(va_args, format);
44 vfprintf(stderr, format, va_args);
50 void warn(const char* format, ...)
53 va_start(va_args, format);
54 vfprintf(stderr, format, va_args);
58 void debug(const char* format, ...)
61 va_start(va_args, format);
62 vfprintf(stderr, format, va_args);
66 void RunXDG::notify_ivi_control_cb (ilmObjectType object, t_ilm_uint id,
69 if (object == ILM_SURFACE) {
70 struct ilmSurfaceProperties surf_props;
72 ilm_getPropertiesOfSurface(id, &surf_props);
73 pid_t surf_pid = surf_props.creatorPid;
76 AGL_DEBUG("ivi surface (id=%d, pid=%d) destroyed.", id, surf_pid);
77 m_launcher->unregister_surfpid(surf_pid);
78 m_surfaces.erase(surf_pid);
82 AGL_DEBUG("ivi surface (id=%d, pid=%d) is created.", id, surf_pid);
84 m_launcher->register_surfpid(surf_pid);
85 if (m_launcher->m_rid &&
86 surf_pid == m_launcher->find_surfpid_by_rid(m_launcher->m_rid)) {
90 m_surfaces[surf_pid] = id;
91 } else if (object == ILM_LAYER) {
93 AGL_DEBUG("ivi layer: %d created.", id);
95 AGL_DEBUG("ivi layer: %d destroyed.", id);
99 void RunXDG::notify_ivi_control_cb_static (ilmObjectType object, t_ilm_uint id,
100 t_ilm_bool created, void *user_data)
102 RunXDG *runxdg = static_cast<RunXDG*>(user_data);
103 runxdg->notify_ivi_control_cb(object, id, created);
106 int POSIXLauncher::launch (std::string& name)
112 AGL_DEBUG("cannot fork()");
118 const char **argv = new const char * [m_args_v.size() + 1];
119 for (unsigned int i = 0; i < m_args_v.size(); ++i) {
120 argv[i] = m_args_v[i].c_str();
122 argv[m_args_v.size()] = NULL;
124 execv(argv[0], (char **)argv);
126 AGL_FATAL("fail to execve(%s)", argv[0]);
133 void POSIXLauncher::loop (volatile sig_atomic_t& e_flag)
139 ret = waitpid(m_rid, &status, 0);
141 if (errno == EINTR) {
142 AGL_DEBUG("catch EINTR while waitpid()");
150 if (WIFEXITED(status)) {
151 AGL_DEBUG("%s terminated, return %d", m_args_v[0].c_str(),
152 WEXITSTATUS(status));
154 if (WIFSIGNALED(status)) {
155 AGL_DEBUG("%s terminated by signal %d", m_args_v[0].c_str(),
161 /* parent killed by someone, so need to kill children */
162 AGL_DEBUG("killpg(0, SIGTERM)");
167 int AFMDBusLauncher::get_dbus_message_bus (GBusType bus_type,
168 GDBusConnection * &conn)
172 conn = g_bus_get_sync(bus_type, NULL, &err);
174 AGL_WARN("Failed to get session bus: %s", err->message);
175 g_clear_error (&err);
182 int AFMDBusLauncher::launch (std::string &name)
186 GDBusConnection* conn;
190 const char* xdg_app = name.c_str();
192 if (get_dbus_message_bus(G_BUS_TYPE_SESSION, conn)) {
196 msg = g_dbus_message_new_method_call (
203 AGL_WARN("Failed to allocate the dbus message");
204 g_object_unref(conn);
208 g_dbus_message_set_body(msg, g_variant_new("(s)", xdg_app));
210 re = g_dbus_connection_send_message_with_reply_sync (
211 conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
214 AGL_WARN("unable to send message: %s", err->message);
216 g_object_unref(conn);
221 g_dbus_connection_flush_sync(conn, NULL, &err);
223 AGL_WARN("unable to flush message queue: %s", err->message);
224 g_object_unref(conn);
230 body = g_dbus_message_get_body(re);
231 g_variant_get(body, "(&s)", &val);
233 AGL_DEBUG("dbus message get (%s)", val);
235 pid_t rid = std::stol(std::string(val));
236 AGL_DEBUG("RID = %d", rid);
238 g_object_unref(conn);
245 volatile sig_atomic_t e_flag = 0;
247 static void sigterm_handler (int signum)
252 static void init_signal (void)
254 struct sigaction act, info;
256 /* Setup signal for SIGTERM */
257 if (!sigaction(SIGTERM, NULL, &info)) {
258 if (info.sa_handler == SIG_IGN) {
259 AGL_DEBUG("SIGTERM being ignored.");
260 } else if (info.sa_handler == SIG_DFL) {
261 AGL_DEBUG("SIGTERM being defaulted.");
265 act.sa_handler = &sigterm_handler;
266 if (sigemptyset(&act.sa_mask) != 0) {
267 AGL_FATAL("Cannot initialize sigaction");
271 if (sigaction(SIGTERM, &act, &info) != 0) {
272 AGL_FATAL("Cannot register signal handler for SIGTERM");
276 int RunXDG::init_wm (void)
278 m_wm = new LibWindowmanager();
279 if (m_wm->init(m_port, m_token.c_str())) {
280 AGL_DEBUG("cannot initialize windowmanager");
284 std::function< void(json_object*) > h_active = [this](json_object* object) {
285 AGL_DEBUG("Got Event_Active");
288 std::function< void(json_object*) > h_inactive = [this](json_object* object) {
289 AGL_DEBUG("Got Event_Inactive");
292 std::function< void(json_object*) > h_visible = [this](json_object* object) {
293 AGL_DEBUG("Got Event_Visible");
294 t_ilm_surface s_ids[1] = { this->m_ivi_id };
295 ilm_setInputFocus(s_ids, 1, ILM_INPUT_DEVICE_KEYBOARD, ILM_TRUE);
298 std::function< void(json_object*) > h_invisible = [this](json_object* object) {
299 AGL_DEBUG("Got Event_Invisible");
300 t_ilm_surface s_ids[1] = { this->m_ivi_id };
301 ilm_setInputFocus(s_ids, 1, ILM_INPUT_DEVICE_KEYBOARD, ILM_FALSE);
304 std::function< void(json_object*) > h_syncdraw =
305 [this](json_object* object) {
306 AGL_DEBUG("Got Event_SyncDraw");
307 this->m_wm->endDraw(this->m_role.c_str());
310 std::function< void(json_object*) > h_flushdraw= [](json_object* object) {
311 AGL_DEBUG("Got Event_FlushDraw");
314 m_wm->set_event_handler(LibWindowmanager::Event_Active, h_active);
315 m_wm->set_event_handler(LibWindowmanager::Event_Inactive, h_inactive);
316 m_wm->set_event_handler(LibWindowmanager::Event_Visible, h_visible);
317 m_wm->set_event_handler(LibWindowmanager::Event_Invisible, h_invisible);
318 m_wm->set_event_handler(LibWindowmanager::Event_SyncDraw, h_syncdraw);
319 m_wm->set_event_handler(LibWindowmanager::Event_FlushDraw, h_flushdraw);
324 int RunXDG::init_hs (void)
326 m_hs = new LibHomeScreen();
327 if (m_hs->init(m_port, m_token.c_str())) {
328 AGL_DEBUG("cannot initialize homescreen");
332 std::function< void(json_object*) > handler = [this] (json_object* object) {
333 AGL_DEBUG("Activesurface %s ", this->m_role.c_str());
334 this->m_wm->activateWindow(this->m_role.c_str(), AREA_NORMAL_FULL);
336 m_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, handler);
338 std::function< void(json_object*) > h_default= [](json_object* object) {
339 const char *j_str = json_object_to_json_string(object);
340 AGL_DEBUG("Got event [%s]", j_str);
342 m_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, h_default);
347 int RunXDG::parse_config (const char *path_to_config)
349 auto config = cpptoml::parse_file(path_to_config);
351 if (config == nullptr) {
352 AGL_DEBUG("cannot parse %s", path_to_config);
356 AGL_DEBUG("[%s] parsed", path_to_config);
358 auto app = config->get_table("application");
359 if (app == nullptr) {
360 AGL_DEBUG("cannto find [application]");
364 m_role = *(app->get_as<std::string>("role"));
365 m_path = *(app->get_as<std::string>("path"));
366 if (m_role.empty() || m_path.empty()) {
367 AGL_FATAL("No name or path defined in config");
370 std::string method = *(app->get_as<std::string>("method"));
371 if (method.empty()) {
372 method = std::string("POSIX");
377 /* Setup API of launcher */
378 if (method == "POSIX") {
379 pl = new POSIXLauncher();
381 } else if (method == "AFM_DBUS") {
382 m_launcher = new AFMDBusLauncher();
384 } else if (method == "AFM_WEBSOCKET") {
385 m_launcher = new AFMWebSocketLauncher();
388 AGL_FATAL("Unknown type of launcher");
392 pl->m_args_v.push_back(m_path);
395 auto params = app->get_array_of<std::string>("params");
396 for (const auto& param : *params)
398 // replace special string "@port@" and "@token@"
399 size_t found = param.find("@port@");
400 if (found != std::string::npos) {
401 std::string sub1 = param.substr(0, found);
402 std::string sub2 = param.substr(found + 6, param.size() - found);
403 std::string str = sub1 + std::to_string(m_port) + sub2;
404 pl->m_args_v.push_back(str);
405 AGL_DEBUG("params[%s] (match @port@)", str.c_str());
409 found = param.find("@token@");
410 if (found != std::string::npos) {
411 std::string sub1 = param.substr(0, found);
412 std::string sub2 = param.substr(found + 7, param.size() - found);
413 std::string str = sub1 + m_token + sub2;
414 pl->m_args_v.push_back(str);
415 AGL_DEBUG("params[%s] (match @token@)", str.c_str());
419 pl->m_args_v.push_back(param);
421 AGL_DEBUG("params[%s]", param.c_str());
427 RunXDG::RunXDG (int port, const char* token, const char* id)
429 m_id = std::string(id);
431 m_token = std::string(token);
434 auto path = std::string(getenv("AFM_APP_INSTALL_DIR"));
435 path = path + "/" + RUNXDG_CONFIG;
437 // Parse config file of runxdg
438 if (parse_config(path.c_str())) {
439 AGL_FATAL("Error in config");
442 AGL_DEBUG("id=[%s], name=[%s], path=[%s], port=%lu, token=[%s]",
443 m_id.c_str(), m_role.c_str(), m_path.c_str(),
444 m_port, m_token.c_str());
446 // Setup HomeScreen/WindowManager API
448 AGL_FATAL("cannot setup wm API");
451 AGL_FATAL("cannot setup hs API");
453 // Setup ilmController API
454 m_ic = new ILMControl(notify_ivi_control_cb_static, this);
456 AGL_DEBUG("RunXDG created.");
459 void RunXDG::setup_surface (void)
461 std::string sid = std::to_string(m_ivi_id);
463 // This surface is mine, register pair app_name and ivi id.
464 AGL_DEBUG("requestSurfaceXDG(%s,%d)", m_role.c_str(), m_ivi_id);
465 m_wm->requestSurfaceXDG(this->m_role.c_str(), (unsigned int)m_ivi_id);
467 if (m_pending_create) {
468 // Recovering 1st time tap_shortcut is dropped because
469 // the application has not been run yet (1st time launch)
470 m_pending_create = false;
471 m_wm->activateWindow(this->m_role.c_str(), AREA_NORMAL_FULL);
475 void POSIXLauncher::register_surfpid (pid_t surf_pid)
477 if (surf_pid == m_rid) {
478 if (!std::count(m_pid_v.begin(), m_pid_v.end(), surf_pid)) {
479 AGL_DEBUG("surface creator(pid=%d) registered", surf_pid);
480 m_pid_v.push_back(surf_pid);
481 AGL_DEBUG("m_pid_v.count(%d) = %d", surf_pid,
482 std::count(m_pid_v.begin(), m_pid_v.end(), surf_pid));
487 void POSIXLauncher::unregister_surfpid (pid_t surf_pid)
489 auto itr = m_pid_v.begin();
490 while (itr != m_pid_v.end()) {
491 if (*itr == surf_pid) {
492 m_pid_v.erase(itr++);
499 pid_t POSIXLauncher::find_surfpid_by_rid (pid_t rid)
501 AGL_DEBUG("find surfpid by rid(%d)", rid);
502 if (std::count(m_pid_v.begin(), m_pid_v.end(), rid)) {
503 AGL_DEBUG("found return(%d)", rid);
510 void AFMLauncher::register_surfpid (pid_t surf_pid)
514 pgid = getpgid(surf_pid);
517 AGL_DEBUG("fail to get process group id");
521 AGL_DEBUG("Surface creator is pid=%d, pgid=%d", surf_pid, pgid);
523 if (!m_pgids.count(pgid)) {
524 m_pgids[pgid] = surf_pid;
528 void AFMLauncher::unregister_surfpid (pid_t surf_pid)
530 auto itr = m_pgids.begin();
531 while (itr != m_pgids.end()) {
532 if (itr->second == surf_pid) {
533 m_pgids.erase(itr++);
540 pid_t AFMLauncher::find_surfpid_by_rid (pid_t rid)
542 auto itr = m_pgids.find(rid);
543 if (itr != m_pgids.end())
549 void RunXDG::start (void)
551 // Initialize SIGTERM handler
554 /* Launch XDG application */
555 m_launcher->m_rid = m_launcher->launch(m_id);
556 if (m_launcher->m_rid < 0) {
557 AGL_FATAL("cannot launch XDG app (%s)", m_id);
560 // take care 1st time launch
561 AGL_DEBUG("waiting for notification: surafce created");
562 m_pending_create = true;
566 // in case, target app has already run
567 if (m_launcher->m_rid) {
568 pid_t surf_pid = m_launcher->find_surfpid_by_rid(m_launcher->m_rid);
570 AGL_DEBUG("match: surf:pid=%d, afm:rid=%d", surf_pid,
572 auto itr = m_surfaces.find(surf_pid);
573 if (itr != m_surfaces.end()) {
574 int id = itr->second;
575 AGL_DEBUG("surface %d for <%s> already exists", id,
584 m_launcher->loop(e_flag);
587 int main (int argc, const char* argv[])
590 // setenv("USE_HMI_DEBUG", "5", 1);
591 // setenv("WAYLAND_DEBUG", "1", 1);
598 AGL_FATAL("Missing port and token");
602 const char *afm_id = getenv("AFM_ID");
603 if (afm_id == NULL || !afm_id[0]) {
608 port = std::stol(argv[1]);
610 } catch (const std::invalid_argument& e) {
611 AGL_FATAL("Invalid argument");
612 } catch (const std::out_of_range& e) {
613 AGL_FATAL("Out of range");
616 RunXDG runxdg(port, token, afm_id);