2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include "window_manager.hpp"
23 #include "json_helper.hpp"
27 #include <afb/afb-binding.h>
28 #include <systemd/sd-event.h>
31 typedef struct WMClientCtxt
35 WMClientCtxt(const char *appName, const char* appRole)
44 wm::WindowManager wmgr;
46 afb_instance() : wmgr() {}
47 ~afb_instance() = default;
52 struct afb_instance *g_afb_instance;
55 int afb_instance::init()
57 return this->wmgr.init();
62 HMI_NOTICE("WinMan ver. %s", WINMAN_VERSION_STRING);
64 g_afb_instance = new afb_instance;
66 if (g_afb_instance->init() == -1)
68 HMI_ERROR("Could not connect to compositor");
72 atexit([] { delete g_afb_instance; });
77 delete g_afb_instance;
78 g_afb_instance = nullptr;
82 int binding_init() noexcept
86 return _binding_init();
88 catch (std::exception &e)
90 HMI_ERROR("Uncaught exception in binding_init(): %s", e.what());
95 static void cbRemoveClientCtxt(void *data)
97 WMClientCtxt *ctxt = (WMClientCtxt *)data;
102 HMI_DEBUG("remove app %s", ctxt->name.c_str());
104 // Policy Manager does not know this app was killed,
105 // so notify it by deactivate request.
106 g_afb_instance->wmgr.api_deactivate_surface(
107 ctxt->name.c_str(), ctxt->role.c_str(),
108 [](const char *) {});
110 g_afb_instance->wmgr.removeClient(ctxt->name);
114 static void createSecurityContext(afb_req req, const char* appid, const char* role)
116 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
119 // Create Security Context at first time
120 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
121 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
122 HMI_DEBUG("create session for %s", ctxt->name.c_str());
123 afb_req_session_set_LOA(req, 1);
124 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
128 void windowmanager_requestsurface(afb_req req) noexcept
130 std::lock_guard<std::mutex> guard(binding_m);
132 if (g_afb_instance == nullptr)
134 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
140 const char *a_drawing_name = afb_req_value(req, "drawing_name");
143 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
147 char *appid = afb_req_get_application_id(req);
150 auto ret = g_afb_instance->wmgr.api_request_surface(
151 appid, a_drawing_name);
154 afb_req_fail(req, "failed", ret.unwrap_err());
158 createSecurityContext(req, appid, a_drawing_name);
159 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
165 afb_req_fail(req, "failed", nullptr);
168 catch (std::exception &e)
170 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
175 void windowmanager_requestsurfacexdg(afb_req req) noexcept
177 std::lock_guard<std::mutex> guard(binding_m);
179 if (g_afb_instance == nullptr)
181 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
187 json_object *jreq = afb_req_json(req);
189 json_object *j_drawing_name = nullptr;
190 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
192 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
195 char const *a_drawing_name = json_object_get_string(j_drawing_name);
197 json_object *j_ivi_id = nullptr;
198 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
200 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
203 char const *a_ivi_id = json_object_get_string(j_ivi_id);
204 char *appid = afb_req_get_application_id(req);
207 auto ret = g_afb_instance->wmgr.api_request_surface(
208 appid, a_drawing_name, a_ivi_id);
212 afb_req_fail(req, "failed", ret);
216 createSecurityContext(req, appid, a_drawing_name);
217 afb_req_success(req, NULL, "success");
222 catch (std::exception &e)
224 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
229 void windowmanager_setrole(afb_req req) noexcept
231 std::lock_guard<std::mutex> guard(binding_m);
232 if (g_afb_instance == nullptr)
234 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
239 json_object *jreq = afb_req_json(req);
241 json_object *j_role = nullptr;
242 if (!json_object_object_get_ex(jreq, "role", &j_role))
244 afb_req_fail(req, "failed", "Need char const* argument role");
247 char const *a_role = json_object_get_string(j_role);
248 char *appid = afb_req_get_application_id(req);
252 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role);
255 afb_req_fail(req, "failed", "Couldn't register");
259 createSecurityContext(req, appid, a_role);
260 afb_req_success(req, NULL, "success");
265 catch (std::exception &e)
267 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
272 void windowmanager_activatewindow(afb_req req) noexcept
274 std::lock_guard<std::mutex> guard(binding_m);
276 if (g_afb_instance == nullptr)
278 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
284 const char *a_drawing_name = afb_req_value(req, "drawing_name");
287 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
291 const char *a_drawing_area = afb_req_value(req, "drawing_area");
294 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
298 char* appid = afb_req_get_application_id(req);
301 g_afb_instance->wmgr.api_activate_surface(
302 appid, a_drawing_name, a_drawing_area,
303 [&req](const char *errmsg) {
304 if (errmsg != nullptr)
307 afb_req_fail(req, "failed", errmsg);
310 afb_req_success(req, NULL, "success");
315 catch (std::exception &e)
317 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
318 g_afb_instance->wmgr.exceptionProcessForTransition();
323 void windowmanager_deactivatewindow(afb_req req) noexcept
325 std::lock_guard<std::mutex> guard(binding_m);
327 if (g_afb_instance == nullptr)
329 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
335 const char *a_drawing_name = afb_req_value(req, "drawing_name");
338 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
342 char* appid = afb_req_get_application_id(req);
345 g_afb_instance->wmgr.api_deactivate_surface(
346 appid, a_drawing_name,
347 [&req](const char *errmsg) {
348 if (errmsg != nullptr)
351 afb_req_fail(req, "failed", errmsg);
354 afb_req_success(req, NULL, "success");
359 catch (std::exception &e)
361 HMI_WARNING("failed: Uncaught exception while calling deactivatesurface: %s", e.what());
362 g_afb_instance->wmgr.exceptionProcessForTransition();
367 void windowmanager_enddraw(afb_req req) noexcept
369 std::lock_guard<std::mutex> guard(binding_m);
371 if (g_afb_instance == nullptr)
373 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
379 const char *a_drawing_name = afb_req_value(req, "drawing_name");
382 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
385 afb_req_success(req, NULL, "success");
387 char* appid = afb_req_get_application_id(req);
390 g_afb_instance->wmgr.api_enddraw(appid, a_drawing_name);
394 catch (std::exception &e)
396 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
397 g_afb_instance->wmgr.exceptionProcessForTransition();
402 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
404 std::lock_guard<std::mutex> guard(binding_m);
406 if (g_afb_instance == nullptr)
408 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
414 auto ret = g_afb_instance->wmgr.api_get_display_info();
417 afb_req_fail(req, "failed", ret.unwrap_err());
421 afb_req_success(req, ret.unwrap(), "success");
423 catch (std::exception &e)
425 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
430 void windowmanager_getareainfo_thunk(afb_req req) noexcept
432 std::lock_guard<std::mutex> guard(binding_m);
434 if (g_afb_instance == nullptr)
436 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
442 json_object *jreq = afb_req_json(req);
444 json_object *j_drawing_name = nullptr;
445 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
447 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
450 char const *a_drawing_name = json_object_get_string(j_drawing_name);
452 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
455 afb_req_fail(req, "failed", ret.unwrap_err());
459 afb_req_success(req, ret.unwrap(), "success");
461 catch (std::exception &e)
463 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
468 void windowmanager_wm_subscribe(afb_req req) noexcept
470 std::lock_guard<std::mutex> guard(binding_m);
472 if (g_afb_instance == nullptr)
474 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
480 json_object *jreq = afb_req_json(req);
481 json_object *j = nullptr;
482 if (!json_object_object_get_ex(jreq, "event", &j))
484 afb_req_fail(req, "failed", "Need char const* argument event");
487 int event_type = json_object_get_int(j);
488 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
489 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
490 int ret = afb_req_subscribe(req, event);
493 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
496 afb_req_success(req, NULL, "success");
498 catch (std::exception &e)
500 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
505 void windowmanager_list_drawing_names(afb_req req) noexcept
507 std::lock_guard<std::mutex> guard(binding_m);
509 /* if (g_afb_instance == nullptr)
511 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
518 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
519 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
522 afb_req_fail(req, "failed", ret.unwrap_err());
526 afb_req_success(req, ret.unwrap(), "success");
528 catch (std::exception &e)
530 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
535 void windowmanager_ping(afb_req req) noexcept
537 std::lock_guard<std::mutex> guard(binding_m);
539 if (g_afb_instance == nullptr)
541 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
546 afb_req_success(req, NULL, "success");
550 void windowmanager_debug_status(afb_req req) noexcept
552 std::lock_guard<std::mutex> guard(binding_m);
554 /* if (g_afb_instance == nullptr)
556 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
563 json_object *jr = json_object_new_object();
564 json_object_object_add(jr, "surfaces",
565 to_json(g_afb_instance->wmgr.controller->sprops));
566 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
568 afb_req_success(req, jr, "success");
570 catch (std::exception &e)
572 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
577 void windowmanager_debug_layers(afb_req req) noexcept
579 std::lock_guard<std::mutex> guard(binding_m);
581 /* if (g_afb_instance == nullptr)
583 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
589 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
591 afb_req_success(req, ret, "success");
593 catch (std::exception &e)
595 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
600 void windowmanager_debug_surfaces(afb_req req) noexcept
602 std::lock_guard<std::mutex> guard(binding_m);
604 /* if (g_afb_instance == nullptr)
606 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
613 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
616 afb_req_fail(req, "failed", ret.unwrap_err());
620 afb_req_success(req, ret.unwrap(), "success");
622 catch (std::exception &e)
624 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
629 void windowmanager_debug_terminate(afb_req req) noexcept
631 std::lock_guard<std::mutex> guard(binding_m);
633 if (g_afb_instance == nullptr)
635 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
642 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
644 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
645 // doesn't play well with perf
648 afb_req_success(req, NULL, "success");
650 catch (std::exception &e)
652 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
657 const struct afb_verb_v2 windowmanager_verbs[] = {
658 {"requestSurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
659 {"requestSurfaceXdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
660 {"setRole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
661 {"activateWindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
662 {"deactivateWindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
663 {"endDraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
664 {"getDisplayInfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
665 {"getAreaInfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
666 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
667 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
668 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
669 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
670 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
671 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
672 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
675 extern "C" const struct afb_binding_v2 afbBindingV2 = {
676 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};