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_surface(
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);
133 if (g_afb_instance == nullptr)
135 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
141 const char *a_drawing_name = afb_req_value(req, "drawing_name");
144 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
148 char *appid = afb_req_get_application_id(req);
151 auto ret = g_afb_instance->wmgr.api_request_surface(
152 appid, a_drawing_name);
155 afb_req_fail(req, "failed", ret.unwrap_err());
159 createSecurityContext(req, appid, a_drawing_name);
160 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
166 afb_req_fail(req, "failed", nullptr);
169 catch (std::exception &e)
171 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
176 void windowmanager_requestsurfacexdg(afb_req req) noexcept
178 std::lock_guard<std::mutex> guard(binding_m);
180 if (g_afb_instance == nullptr)
182 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
188 json_object *jreq = afb_req_json(req);
190 json_object *j_drawing_name = nullptr;
191 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
193 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
196 char const *a_drawing_name = json_object_get_string(j_drawing_name);
198 json_object *j_ivi_id = nullptr;
199 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
201 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
204 char const *a_ivi_id = json_object_get_string(j_ivi_id);
205 char *appid = afb_req_get_application_id(req);
208 auto ret = g_afb_instance->wmgr.api_request_surface(
209 appid, a_drawing_name, a_ivi_id);
213 afb_req_fail(req, "failed", ret);
217 createSecurityContext(req, appid, a_drawing_name);
218 afb_req_success(req, NULL, "success");
223 catch (std::exception &e)
225 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
230 void windowmanager_setrole(afb_req req) noexcept
232 std::lock_guard<std::mutex> guard(binding_m);
233 if (g_afb_instance == nullptr)
235 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
240 json_object *jreq = afb_req_json(req);
242 json_object *j_role = nullptr;
243 if (!json_object_object_get_ex(jreq, "role", &j_role))
245 afb_req_fail(req, "failed", "Need char const* argument role");
248 char const *a_role = json_object_get_string(j_role);
249 char *appid = afb_req_get_application_id(req);
253 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role);
256 afb_req_fail(req, "failed", "Couldn't register");
260 createSecurityContext(req, appid, a_role);
261 afb_req_success(req, NULL, "success");
266 catch (std::exception &e)
268 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
273 void windowmanager_activatewindow(afb_req req) noexcept
275 std::lock_guard<std::mutex> guard(binding_m);
277 if (g_afb_instance == nullptr)
279 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
285 const char *a_drawing_name = afb_req_value(req, "drawing_name");
288 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
292 const char *a_drawing_area = afb_req_value(req, "drawing_area");
295 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
299 char* appid = afb_req_get_application_id(req);
302 g_afb_instance->wmgr.api_activate_surface(
303 appid, a_drawing_name, a_drawing_area,
304 [&req](const char *errmsg) {
305 if (errmsg != nullptr)
308 afb_req_fail(req, "failed", errmsg);
311 afb_req_success(req, NULL, "success");
316 catch (std::exception &e)
318 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
319 g_afb_instance->wmgr.exceptionProcessForTransition();
324 void windowmanager_deactivatewindow(afb_req req) noexcept
326 std::lock_guard<std::mutex> guard(binding_m);
328 if (g_afb_instance == nullptr)
330 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
336 const char *a_drawing_name = afb_req_value(req, "drawing_name");
339 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
343 char* appid = afb_req_get_application_id(req);
346 g_afb_instance->wmgr.api_deactivate_surface(
347 appid, a_drawing_name,
348 [&req](const char *errmsg) {
349 if (errmsg != nullptr)
352 afb_req_fail(req, "failed", errmsg);
355 afb_req_success(req, NULL, "success");
360 catch (std::exception &e)
362 HMI_WARNING("failed: Uncaught exception while calling deactivatesurface: %s", e.what());
363 g_afb_instance->wmgr.exceptionProcessForTransition();
368 void windowmanager_enddraw(afb_req req) noexcept
370 std::lock_guard<std::mutex> guard(binding_m);
372 if (g_afb_instance == nullptr)
374 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
380 const char *a_drawing_name = afb_req_value(req, "drawing_name");
383 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
386 afb_req_success(req, NULL, "success");
388 char* appid = afb_req_get_application_id(req);
391 g_afb_instance->wmgr.api_enddraw(appid, a_drawing_name);
395 catch (std::exception &e)
397 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
398 g_afb_instance->wmgr.exceptionProcessForTransition();
403 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
405 std::lock_guard<std::mutex> guard(binding_m);
407 if (g_afb_instance == nullptr)
409 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
415 auto ret = g_afb_instance->wmgr.api_get_display_info();
418 afb_req_fail(req, "failed", ret.unwrap_err());
422 afb_req_success(req, ret.unwrap(), "success");
424 catch (std::exception &e)
426 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
431 void windowmanager_getareainfo_thunk(afb_req req) noexcept
433 std::lock_guard<std::mutex> guard(binding_m);
435 if (g_afb_instance == nullptr)
437 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
443 json_object *jreq = afb_req_json(req);
445 json_object *j_drawing_name = nullptr;
446 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
448 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
451 char const *a_drawing_name = json_object_get_string(j_drawing_name);
453 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
456 afb_req_fail(req, "failed", ret.unwrap_err());
460 afb_req_success(req, ret.unwrap(), "success");
462 catch (std::exception &e)
464 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
469 void windowmanager_set_render_order(afb_req req) noexcept
471 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?");
478 char* appid = afb_req_get_application_id(req);
481 json_object *jreq = afb_req_json(req);
482 json_object *j_ro; // Do not free this. binder frees jreq, then free j_ro
483 if (json_object_object_get_ex(jreq, "render_order", &j_ro))
485 int size = json_object_array_length(j_ro);
486 std::vector<std::string> ro(size);
487 for(int i = 0; i < size; i++)
489 ro[i] = json_object_get_string(json_object_array_get_idx(j_ro, i));
492 auto ret = g_afb_instance->wmgr.api_client_set_render_order(appid, ro);
495 afb_req_fail(req, "failed", nullptr);
499 afb_req_success(req, nullptr, nullptr);
506 afb_req_fail(req, "failed", nullptr);
510 void windowmanager_wm_subscribe(afb_req req) noexcept
512 std::lock_guard<std::mutex> guard(binding_m);
514 if (g_afb_instance == nullptr)
516 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
522 json_object *jreq = afb_req_json(req);
523 json_object *j = nullptr;
524 if (!json_object_object_get_ex(jreq, "event", &j))
526 afb_req_fail(req, "failed", "Need char const* argument event");
529 int event_type = json_object_get_int(j);
530 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
531 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
532 int ret = afb_req_subscribe(req, event);
535 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
538 afb_req_success(req, NULL, "success");
540 catch (std::exception &e)
542 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
547 void windowmanager_list_drawing_names(afb_req req) noexcept
549 std::lock_guard<std::mutex> guard(binding_m);
551 /* if (g_afb_instance == nullptr)
553 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
560 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
561 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
564 afb_req_fail(req, "failed", ret.unwrap_err());
568 afb_req_success(req, ret.unwrap(), "success");
570 catch (std::exception &e)
572 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
577 void windowmanager_ping(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?");
588 afb_req_success(req, NULL, "success");
592 void windowmanager_debug_status(afb_req req) noexcept
594 std::lock_guard<std::mutex> guard(binding_m);
596 /* if (g_afb_instance == nullptr)
598 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
605 json_object *jr = json_object_new_object();
606 json_object_object_add(jr, "surfaces",
607 to_json(g_afb_instance->wmgr.controller->sprops));
608 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
610 afb_req_success(req, jr, "success");
612 catch (std::exception &e)
614 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
619 void windowmanager_debug_layers(afb_req req) noexcept
621 std::lock_guard<std::mutex> guard(binding_m);
623 /* if (g_afb_instance == nullptr)
625 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
631 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
633 afb_req_success(req, ret, "success");
635 catch (std::exception &e)
637 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
642 void windowmanager_debug_surfaces(afb_req req) noexcept
644 std::lock_guard<std::mutex> guard(binding_m);
646 /* if (g_afb_instance == nullptr)
648 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
655 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
658 afb_req_fail(req, "failed", ret.unwrap_err());
662 afb_req_success(req, ret.unwrap(), "success");
664 catch (std::exception &e)
666 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
671 void windowmanager_debug_terminate(afb_req req) noexcept
673 std::lock_guard<std::mutex> guard(binding_m);
675 if (g_afb_instance == nullptr)
677 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
684 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
686 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
687 // doesn't play well with perf
690 afb_req_success(req, NULL, "success");
692 catch (std::exception &e)
694 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
699 const struct afb_verb_v2 windowmanager_verbs[] = {
700 {"requestSurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
701 {"requestSurfaceXdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
702 {"setRole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
703 {"activateWindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
704 {"deactivateWindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
705 {"endDraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
706 {"getDisplayInfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
707 {"getAreaInfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
708 {"setRenderOrder", windowmanager_set_render_order, nullptr, nullptr, AFB_SESSION_NONE},
709 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
710 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
711 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
712 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
713 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
714 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
715 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
718 extern "C" const struct afb_binding_v2 afbBindingV2 = {
719 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};