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.
23 #include "window_manager.hpp"
24 #include "json_helper.hpp"
28 #include <afb/afb-binding.h>
29 #include <systemd/sd-event.h>
32 typedef struct WMClientCtxt
36 WMClientCtxt(const char *appName, const char* appRole)
45 wm::WindowManager wmgr;
47 afb_instance() : wmgr() {}
48 ~afb_instance() = default;
53 struct afb_instance *g_afb_instance;
56 int afb_instance::init()
58 return this->wmgr.init();
63 HMI_NOTICE("WinMan ver. %s", WINMAN_VERSION_STRING);
65 g_afb_instance = new afb_instance;
67 if (g_afb_instance->init() == -1)
69 HMI_ERROR("Could not connect to compositor");
73 atexit([] { delete g_afb_instance; });
78 delete g_afb_instance;
79 g_afb_instance = nullptr;
83 int binding_init() noexcept
87 return _binding_init();
89 catch (std::exception &e)
91 HMI_ERROR("Uncaught exception in binding_init(): %s", e.what());
96 static void cbRemoveClientCtxt(void *data)
98 WMClientCtxt *ctxt = (WMClientCtxt *)data;
103 HMI_DEBUG("remove app %s", ctxt->name.c_str());
105 // Policy Manager does not know this app was killed,
106 // so notify it by deactivate request.
107 g_afb_instance->wmgr.api_deactivate_window(
108 ctxt->name.c_str(), ctxt->role.c_str(),
109 [](const char *) {});
111 g_afb_instance->wmgr.removeClient(ctxt->name);
115 static void createSecurityContext(afb_req req, const char* appid, const char* role)
117 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
120 // Create Security Context at first time
121 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
122 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
123 HMI_DEBUG("create session for %s", ctxt->name.c_str());
124 afb_req_session_set_LOA(req, 1);
125 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
129 void windowmanager_requestsurface(afb_req req) noexcept
131 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);
178 if (g_afb_instance == nullptr)
180 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
186 json_object *jreq = afb_req_json(req);
188 json_object *j_drawing_name = nullptr;
189 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
191 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
194 char const *a_drawing_name = json_object_get_string(j_drawing_name);
196 json_object *j_ivi_id = nullptr;
197 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
199 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
202 char const *a_ivi_id = json_object_get_string(j_ivi_id);
203 char *appid = afb_req_get_application_id(req);
206 auto ret = g_afb_instance->wmgr.api_request_surface(
207 appid, a_drawing_name, a_ivi_id);
211 afb_req_fail(req, "failed", ret);
215 createSecurityContext(req, appid, a_drawing_name);
216 afb_req_success(req, NULL, "success");
221 catch (std::exception &e)
223 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
228 void windowmanager_setrole(afb_req req) noexcept
230 std::lock_guard<std::mutex> guard(binding_m);
231 if (g_afb_instance == nullptr)
233 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
238 json_object *jreq = afb_req_json(req);
240 json_object *j_role = nullptr;
241 if (!json_object_object_get_ex(jreq, "role", &j_role))
243 afb_req_fail(req, "failed", "Need char const* argument role");
246 char const *a_role = json_object_get_string(j_role);
247 char *appid = afb_req_get_application_id(req);
251 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role);
254 afb_req_fail(req, "failed", "Couldn't register");
258 createSecurityContext(req, appid, a_role);
259 afb_req_success(req, NULL, "success");
264 catch (std::exception &e)
266 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
271 void windowmanager_activatewindow(afb_req req) noexcept
273 std::lock_guard<std::mutex> guard(binding_m);
274 if (g_afb_instance == nullptr)
276 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
282 const char *a_drawing_name = afb_req_value(req, "drawing_name");
285 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
289 const char *a_drawing_area = afb_req_value(req, "drawing_area");
292 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
296 char* appid = afb_req_get_application_id(req);
299 auto reply = [&req](const char *errmsg) {
300 if (errmsg != nullptr)
303 afb_req_fail(req, "failed", errmsg);
306 afb_req_success(req, NULL, "success");
309 if (g_afb_instance->wmgr.wmcon.isMasterMode() ||
310 !g_afb_instance->wmgr.wmcon.isMasterArea(a_drawing_area))
312 g_afb_instance->wmgr.api_activate_window(
313 appid, a_drawing_name, a_drawing_area, reply);
318 if (!g_afb_instance->wmgr.wmcon.isConnecting())
320 g_afb_instance->wmgr.wmcon.connectToMaster();
323 // If Window Manager is slave and this request is for master,
324 // request activateWindow to master
325 g_afb_instance->wmgr.api_activate_surface_to_master(
326 appid, a_drawing_name, a_drawing_area, reply);
331 catch (std::exception &e)
333 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
334 g_afb_instance->wmgr.exceptionProcessForTransition();
339 void windowmanager_deactivatewindow(afb_req req) noexcept
341 std::lock_guard<std::mutex> guard(binding_m);
342 if (g_afb_instance == nullptr)
344 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
350 const char *a_drawing_name = afb_req_value(req, "drawing_name");
353 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
357 char* appid = afb_req_get_application_id(req);
360 auto reply = [&req](const char *errmsg) {
361 if (errmsg != nullptr)
364 afb_req_fail(req, "failed", errmsg);
367 afb_req_success(req, NULL, "success");
370 // TODO: Check whether role is tbtnavi to request remote invisible
371 if (g_afb_instance->wmgr.wmcon.isMasterMode() ||
372 ("tbtnavi" != std::string(a_drawing_name)))
374 g_afb_instance->wmgr.api_deactivate_window(
375 appid, a_drawing_name, reply);
379 // If Window Manager is slave and this request is for master,
380 // request deactivateWindow to master
381 g_afb_instance->wmgr.api_deactivate_surface_to_master(
382 appid, a_drawing_name, reply);
387 catch (std::exception &e)
389 HMI_WARNING("failed: Uncaught exception while calling deactivatesurface: %s", e.what());
390 g_afb_instance->wmgr.exceptionProcessForTransition();
395 void windowmanager_enddraw(afb_req req) noexcept
397 std::lock_guard<std::mutex> guard(binding_m);
398 if (g_afb_instance == nullptr)
400 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
406 const char *a_drawing_name = afb_req_value(req, "drawing_name");
409 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
412 afb_req_success(req, NULL, "success");
414 char* appid = afb_req_get_application_id(req);
417 if (g_afb_instance->wmgr.wmcon.isMasterMode() ||
418 !g_afb_instance->wmgr.wmcon.isSyncDrawingForRemote(appid))
420 g_afb_instance->wmgr.api_enddraw(appid, a_drawing_name);
424 // If Window Manager is slave and requesting app is syncDrawing,
425 // request endDraw to master
426 g_afb_instance->wmgr.api_enddraw_for_remote(appid, a_drawing_name);
431 catch (std::exception &e)
433 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
434 g_afb_instance->wmgr.exceptionProcessForTransition();
439 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
441 std::lock_guard<std::mutex> guard(binding_m);
442 if (g_afb_instance == nullptr)
444 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
450 auto ret = g_afb_instance->wmgr.api_get_display_info();
453 afb_req_fail(req, "failed", ret.unwrap_err());
457 afb_req_success(req, ret.unwrap(), "success");
459 catch (std::exception &e)
461 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
466 void windowmanager_getareainfo_thunk(afb_req req) noexcept
468 std::lock_guard<std::mutex> guard(binding_m);
469 if (g_afb_instance == nullptr)
471 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
477 json_object *jreq = afb_req_json(req);
479 json_object *j_drawing_name = nullptr;
480 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
482 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
485 char const *a_drawing_name = json_object_get_string(j_drawing_name);
487 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
490 afb_req_fail(req, "failed", ret.unwrap_err());
494 afb_req_success(req, ret.unwrap(), "success");
496 catch (std::exception &e)
498 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
503 void windowmanager_getcarinfo_thunk(afb_req req) noexcept
505 std::lock_guard<std::mutex> guard(binding_m);
506 if (g_afb_instance == nullptr)
508 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
514 json_object *jreq = afb_req_json(req);
516 json_object *j_label = nullptr;
517 if (! json_object_object_get_ex(jreq, "label", &j_label))
519 afb_req_fail(req, "failed", "Need char const* argument label");
522 char const* a_label = json_object_get_string(j_label);
524 auto ret = g_afb_instance->wmgr.api_get_car_info(a_label);
527 afb_req_fail(req, "failed", ret.unwrap_err());
531 afb_req_success(req, ret.unwrap(), "success");
533 catch (std::exception &e)
535 afb_req_fail_f(req, "failed", "Uncaught exception while calling getcarinfo: %s", e.what());
540 void windowmanager_set_render_order(afb_req req) noexcept
542 std::lock_guard<std::mutex> guard(binding_m);
543 if (g_afb_instance == nullptr)
545 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
549 char* appid = afb_req_get_application_id(req);
552 json_object *jreq = afb_req_json(req);
553 json_object *j_ro; // Do not free this. binder frees jreq, then free j_ro
554 if (json_object_object_get_ex(jreq, "render_order", &j_ro))
556 int size = json_object_array_length(j_ro);
557 std::vector<std::string> ro(size);
558 for(int i = 0; i < size; i++)
560 ro[i] = json_object_get_string(json_object_array_get_idx(j_ro, i));
563 auto ret = g_afb_instance->wmgr.api_client_set_render_order(appid, ro);
566 afb_req_fail(req, "failed", nullptr);
570 afb_req_success(req, nullptr, nullptr);
577 afb_req_fail(req, "failed", nullptr);
581 void windowmanager_attach_app(afb_req req) noexcept
583 std::lock_guard<std::mutex> guard(binding_m);
584 if (g_afb_instance == nullptr)
586 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
590 char* appid = afb_req_get_application_id(req);
593 json_object *jreq = afb_req_json(req);
594 json_object *j_dest, *j_id; // Do not free this. binder frees jreq, then free j_ro
595 if (json_object_object_get_ex(jreq, "destination", &j_dest) &&
596 json_object_object_get_ex(jreq, "service_surface", &j_id))
598 const char* dest_app = json_object_get_string(j_dest);
599 const char* service = json_object_get_string(j_id);
601 std::string uuid = g_afb_instance->wmgr.api_client_attach_service_surface(appid, dest_app, service);
604 afb_req_fail(req, "failed", nullptr);
608 json_object *resp = json_object_new_object();
609 json_object_object_add(resp, "uuid", json_object_new_string(uuid.c_str()));
610 afb_req_success(req, resp, nullptr);
617 afb_req_fail(req, "failed", nullptr);
621 void windowmanager_wm_subscribe(afb_req req) noexcept
623 std::lock_guard<std::mutex> guard(binding_m);
624 if (g_afb_instance == nullptr)
626 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
632 json_object *jreq = afb_req_json(req);
633 json_object *j = nullptr;
634 if (!json_object_object_get_ex(jreq, "event", &j))
636 afb_req_fail(req, "failed", "Need char const* argument event");
639 int event_id = json_object_get_int(j);
640 int ret = g_afb_instance->wmgr.api_subscribe(req, event_id);
644 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
647 afb_req_success(req, NULL, "success");
649 catch (std::exception &e)
651 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
656 void windowmanager_ping(afb_req req) noexcept
658 std::lock_guard<std::mutex> guard(binding_m);
660 if (g_afb_instance == nullptr)
662 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
667 afb_req_success(req, NULL, "success");
671 void windowmanager_debug_terminate(afb_req req) noexcept
673 std::lock_guard<std::mutex> guard(binding_m);
674 if (g_afb_instance == nullptr)
676 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
683 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
685 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
686 // doesn't play well with perf
689 afb_req_success(req, NULL, "success");
691 catch (std::exception &e)
693 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
698 void on_event(const char *event, struct json_object *object)
700 g_afb_instance->wmgr.analyzeReceivedEvent(event, object);
703 const struct afb_verb_v2 windowmanager_verbs[] = {
704 {"requestSurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
705 {"requestSurfaceXdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
706 {"setRole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
707 {"activateWindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
708 {"deactivateWindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
709 {"endDraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
710 {"getDisplayInfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
711 {"getAreaInfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
712 {"getCarInfo", windowmanager_getcarinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE },
713 {"setRenderOrder", windowmanager_set_render_order, nullptr, nullptr, AFB_SESSION_NONE},
714 {"attachApp", windowmanager_attach_app, nullptr, nullptr, AFB_SESSION_NONE},
715 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
716 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
717 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
720 extern "C" const struct afb_binding_v2 afbBindingV2 = {
721 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, on_event, 0};