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.
21 #include "window_manager.hpp"
22 #include "json_helper.hpp"
26 #include <afb/afb-binding.h>
27 #include <systemd/sd-event.h>
30 typedef struct WMClientCtxt
34 WMClientCtxt(const char *appName, const char* appRole)
43 wm::WindowManager wmgr;
45 afb_instance() : wmgr() {}
46 ~afb_instance() = default;
51 struct afb_instance *g_afb_instance;
54 int afb_instance::init()
56 return this->wmgr.init();
61 HMI_NOTICE("WinMan ver. %s", WINMAN_VERSION_STRING);
63 g_afb_instance = new afb_instance;
65 if (g_afb_instance->init() == -1)
67 HMI_ERROR("Could not connect to compositor");
71 atexit([] { delete g_afb_instance; });
76 delete g_afb_instance;
77 g_afb_instance = nullptr;
81 int binding_init() noexcept
85 return _binding_init();
87 catch (std::exception &e)
89 HMI_ERROR("Uncaught exception in binding_init(): %s", e.what());
94 static void cbRemoveClientCtxt(void *data)
96 WMClientCtxt *ctxt = (WMClientCtxt *)data;
101 HMI_DEBUG("remove app %s", ctxt->name.c_str());
103 // Policy Manager does not know this app was killed,
104 // so notify it by deactivate request.
105 g_afb_instance->wmgr.api_deactivate_surface(
106 ctxt->name.c_str(), ctxt->role.c_str(),
107 [](const char *) {});
109 g_afb_instance->wmgr.removeClient(ctxt->name);
113 static void createSecurityContext(afb_req req, const char* appid, const char* role)
115 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
118 // Create Security Context at first time
119 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
120 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
121 HMI_DEBUG("create session for %s", ctxt->name.c_str());
122 afb_req_session_set_LOA(req, 1);
123 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
127 void windowmanager_requestsurface(afb_req req) noexcept
129 std::lock_guard<std::mutex> guard(binding_m);
131 if (g_afb_instance == nullptr)
133 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
139 const char *a_drawing_name = afb_req_value(req, "drawing_name");
142 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
146 const char *appid = afb_req_get_application_id(req);
147 auto ret = g_afb_instance->wmgr.api_request_surface(
148 appid, a_drawing_name);
151 afb_req_fail(req, "failed", ret.unwrap_err());
155 createSecurityContext(req, appid, a_drawing_name);
157 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
159 catch (std::exception &e)
161 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
166 void windowmanager_requestsurfacexdg(afb_req req) noexcept
168 std::lock_guard<std::mutex> guard(binding_m);
170 if (g_afb_instance == nullptr)
172 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
178 json_object *jreq = afb_req_json(req);
180 json_object *j_drawing_name = nullptr;
181 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
183 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
186 char const *a_drawing_name = json_object_get_string(j_drawing_name);
188 json_object *j_ivi_id = nullptr;
189 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
191 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
194 char const *a_ivi_id = json_object_get_string(j_ivi_id);
195 char const *appid = afb_req_get_application_id(req);
196 auto ret = g_afb_instance->wmgr.api_request_surface(
197 appid, a_drawing_name, a_ivi_id);
201 afb_req_fail(req, "failed", ret);
205 createSecurityContext(req, appid, a_drawing_name);
207 afb_req_success(req, NULL, "success");
209 catch (std::exception &e)
211 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
216 void windowmanager_setrole(afb_req req) noexcept
218 std::lock_guard<std::mutex> guard(binding_m);
219 if (g_afb_instance == nullptr)
221 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
227 char const *appid = afb_req_get_application_id(req);
228 json_object *jreq = afb_req_json(req);
230 json_object *j_role = nullptr;
231 if (!json_object_object_get_ex(jreq, "role", &j_role))
233 afb_req_fail(req, "failed", "Need char const* argument role");
236 char const *a_role = json_object_get_string(j_role);
238 json_object *j_pid = nullptr;
239 if (json_object_object_get_ex(jreq, "pid", &j_pid))
241 HMI_DEBUG("PID is set");
242 char const *a_pid = json_object_get_string(j_pid);
243 pid = std::stol(a_pid);
246 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role, pid);
249 afb_req_fail(req, "failed", "Couldn't register");
253 createSecurityContext(req, appid, a_role);
255 afb_req_success(req, NULL, "success");
257 catch (std::exception &e)
259 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
264 void windowmanager_activatewindow(afb_req req) noexcept
266 std::lock_guard<std::mutex> guard(binding_m);
268 if (g_afb_instance == nullptr)
270 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
276 const char *a_drawing_name = afb_req_value(req, "drawing_name");
279 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
283 const char *a_drawing_area = afb_req_value(req, "drawing_area");
286 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
290 g_afb_instance->wmgr.api_activate_surface(
291 afb_req_get_application_id(req),
292 a_drawing_name, a_drawing_area,
293 [&req](const char *errmsg) {
294 if (errmsg != nullptr)
297 afb_req_fail(req, "failed", errmsg);
300 afb_req_success(req, NULL, "success");
303 catch (std::exception &e)
305 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
306 g_afb_instance->wmgr.exceptionProcessForTransition();
311 void windowmanager_deactivatewindow(afb_req req) noexcept
313 std::lock_guard<std::mutex> guard(binding_m);
315 if (g_afb_instance == nullptr)
317 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
323 const char *a_drawing_name = afb_req_value(req, "drawing_name");
326 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
330 g_afb_instance->wmgr.api_deactivate_surface(
331 afb_req_get_application_id(req), a_drawing_name,
332 [&req](const char *errmsg) {
333 if (errmsg != nullptr)
336 afb_req_fail(req, "failed", errmsg);
339 afb_req_success(req, NULL, "success");
342 catch (std::exception &e)
344 HMI_WARNING("failed: Uncaught exception while calling deactivatesurface: %s", e.what());
345 g_afb_instance->wmgr.exceptionProcessForTransition();
350 void windowmanager_enddraw(afb_req req) noexcept
352 std::lock_guard<std::mutex> guard(binding_m);
354 if (g_afb_instance == nullptr)
356 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
362 const char *a_drawing_name = afb_req_value(req, "drawing_name");
365 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
368 afb_req_success(req, NULL, "success");
370 g_afb_instance->wmgr.api_enddraw(
371 afb_req_get_application_id(req), a_drawing_name);
373 catch (std::exception &e)
375 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
376 g_afb_instance->wmgr.exceptionProcessForTransition();
381 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
383 std::lock_guard<std::mutex> guard(binding_m);
385 if (g_afb_instance == nullptr)
387 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
393 auto ret = g_afb_instance->wmgr.api_get_display_info();
396 afb_req_fail(req, "failed", ret.unwrap_err());
400 afb_req_success(req, ret.unwrap(), "success");
402 catch (std::exception &e)
404 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
409 void windowmanager_getareainfo_thunk(afb_req req) noexcept
411 std::lock_guard<std::mutex> guard(binding_m);
413 if (g_afb_instance == nullptr)
415 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
421 json_object *jreq = afb_req_json(req);
423 json_object *j_drawing_name = nullptr;
424 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
426 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
429 char const *a_drawing_name = json_object_get_string(j_drawing_name);
431 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
434 afb_req_fail(req, "failed", ret.unwrap_err());
438 afb_req_success(req, ret.unwrap(), "success");
440 catch (std::exception &e)
442 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
447 void windowmanager_wm_subscribe(afb_req req) noexcept
449 std::lock_guard<std::mutex> guard(binding_m);
451 if (g_afb_instance == nullptr)
453 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
459 json_object *jreq = afb_req_json(req);
460 json_object *j = nullptr;
461 if (!json_object_object_get_ex(jreq, "event", &j))
463 afb_req_fail(req, "failed", "Need char const* argument event");
466 int event_type = json_object_get_int(j);
467 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
468 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
469 int ret = afb_req_subscribe(req, event);
472 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
475 afb_req_success(req, NULL, "success");
477 catch (std::exception &e)
479 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
484 void windowmanager_list_drawing_names(afb_req req) noexcept
486 std::lock_guard<std::mutex> guard(binding_m);
488 /* if (g_afb_instance == nullptr)
490 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
497 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
498 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
501 afb_req_fail(req, "failed", ret.unwrap_err());
505 afb_req_success(req, ret.unwrap(), "success");
507 catch (std::exception &e)
509 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
514 void windowmanager_ping(afb_req req) noexcept
516 std::lock_guard<std::mutex> guard(binding_m);
518 if (g_afb_instance == nullptr)
520 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
527 g_afb_instance->wmgr.api_ping();
529 afb_req_success(req, NULL, "success");
531 catch (std::exception &e)
533 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
538 void windowmanager_debug_status(afb_req req) noexcept
540 std::lock_guard<std::mutex> guard(binding_m);
542 /* if (g_afb_instance == nullptr)
544 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
551 json_object *jr = json_object_new_object();
552 json_object_object_add(jr, "surfaces",
553 to_json(g_afb_instance->wmgr.controller->sprops));
554 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
556 afb_req_success(req, jr, "success");
558 catch (std::exception &e)
560 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
565 void windowmanager_debug_layers(afb_req req) noexcept
567 std::lock_guard<std::mutex> guard(binding_m);
569 /* if (g_afb_instance == nullptr)
571 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
577 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
579 afb_req_success(req, ret, "success");
581 catch (std::exception &e)
583 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
588 void windowmanager_debug_surfaces(afb_req req) noexcept
590 std::lock_guard<std::mutex> guard(binding_m);
592 /* if (g_afb_instance == nullptr)
594 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
601 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
604 afb_req_fail(req, "failed", ret.unwrap_err());
608 afb_req_success(req, ret.unwrap(), "success");
610 catch (std::exception &e)
612 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
617 void windowmanager_debug_terminate(afb_req req) noexcept
619 std::lock_guard<std::mutex> guard(binding_m);
621 if (g_afb_instance == nullptr)
623 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
630 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
632 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
633 // doesn't play well with perf
636 afb_req_success(req, NULL, "success");
638 catch (std::exception &e)
640 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
645 const struct afb_verb_v2 windowmanager_verbs[] = {
646 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
647 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
648 {"setrole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
649 {"activatewindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
650 {"deactivatewindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
651 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
652 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
653 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
654 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
655 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
656 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
657 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
658 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
659 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
660 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
663 extern "C" const struct afb_binding_v2 afbBindingV2 = {
664 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};