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 "../include/json.hpp"
22 #include "window_manager.hpp"
23 #include "json_helper.hpp"
24 #include "wayland_ivi_wm.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 std::unique_ptr<wl::display> display;
46 wm::WindowManager wmgr;
48 afb_instance() : display{new wl::display}, wmgr{this->display.get()} {}
53 struct afb_instance *g_afb_instance;
56 int afb_instance::init()
58 return this->wmgr.init();
61 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
66 if ((events & EPOLLHUP) != 0)
68 HMI_ERROR("wm", "The compositor hung up, dying now.");
69 delete g_afb_instance;
70 g_afb_instance = nullptr;
74 if ((events & EPOLLIN) != 0u)
77 STN(display_read_events);
78 g_afb_instance->wmgr.display->read_events();
79 g_afb_instance->wmgr.set_pending_events();
82 // We want do dispatch pending wayland events from within
84 STN(winman_ping_api_call);
85 afb_service_call("windowmanager", "ping", json_object_new_object(),
86 [](void *c, int st, json_object *j) {
87 STN(winman_ping_api_call_return);
96 sd_event_source_unref(evs);
97 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
106 HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
108 if (g_afb_instance != nullptr)
110 HMI_ERROR("wm", "Wayland context already initialized?");
114 if (getenv("XDG_RUNTIME_DIR") == nullptr)
116 HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
121 // wait until wayland compositor starts up.
123 g_afb_instance = new afb_instance;
124 while (!g_afb_instance->display->ok())
129 HMI_ERROR("wm", "Could not connect to compositor");
132 HMI_ERROR("wm", "Wait to start weston ...");
134 delete g_afb_instance;
135 g_afb_instance = new afb_instance;
139 if (g_afb_instance->init() == -1)
141 HMI_ERROR("wm", "Could not connect to compositor");
146 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
147 g_afb_instance->display->get_fd(), EPOLLIN,
148 display_event_callback, g_afb_instance);
151 HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
156 atexit([] { delete g_afb_instance; });
161 delete g_afb_instance;
162 g_afb_instance = nullptr;
166 int binding_init() noexcept
170 return _binding_init();
172 catch (std::exception &e)
174 HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
179 static void cbRemoveClientCtxt(void *data)
181 WMClientCtxt *ctxt = (WMClientCtxt *)data;
186 HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
187 // Lookup surfaceID and remove it because App is dead.
188 auto pSid = g_afb_instance->wmgr.id_alloc.lookup(ctxt->role.c_str());
192 auto o_state = *g_afb_instance->wmgr.layers.get_layout_state(sid);
193 if (o_state != nullptr)
195 if (o_state->main == sid)
199 else if (o_state->sub == sid)
204 g_afb_instance->wmgr.id_alloc.remove_id(sid);
205 g_afb_instance->wmgr.layers.remove_surface(sid);
206 g_afb_instance->wmgr.controller->sprops.erase(sid);
207 g_afb_instance->wmgr.controller->surfaces.erase(sid);
208 HMI_DEBUG("wm", "delete surfaceID %d", sid);
210 g_afb_instance->wmgr.removeClient(ctxt->name);
214 static void createSecurityContext(afb_req req, const char* appid, const char* role)
216 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
219 // Create Security Context at first time
220 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
221 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
222 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
223 afb_req_session_set_LOA(req, 1);
224 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
228 void windowmanager_requestsurface(afb_req req) noexcept
230 std::lock_guard<std::mutex> guard(binding_m);
234 if (g_afb_instance == nullptr)
236 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
242 const char *a_drawing_name = afb_req_value(req, "drawing_name");
245 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
249 const char *appid = afb_req_get_application_id(req);
250 auto ret = g_afb_instance->wmgr.api_request_surface(
251 appid, a_drawing_name);
254 afb_req_fail(req, "failed", ret.unwrap_err());
258 createSecurityContext(req, appid, a_drawing_name);
260 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
262 catch (std::exception &e)
264 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
269 void windowmanager_requestsurfacexdg(afb_req req) noexcept
271 std::lock_guard<std::mutex> guard(binding_m);
275 if (g_afb_instance == nullptr)
277 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
283 json_object *jreq = afb_req_json(req);
285 json_object *j_drawing_name = nullptr;
286 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
288 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
291 char const *a_drawing_name = json_object_get_string(j_drawing_name);
293 json_object *j_ivi_id = nullptr;
294 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
296 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
299 char const *a_ivi_id = json_object_get_string(j_ivi_id);
300 char const *appid = afb_req_get_application_id(req);
301 auto ret = g_afb_instance->wmgr.api_request_surface(
302 appid, a_drawing_name, a_ivi_id);
306 afb_req_fail(req, "failed", ret);
310 createSecurityContext(req, appid, a_drawing_name);
312 afb_req_success(req, NULL, "success");
314 catch (std::exception &e)
316 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
321 void windowmanager_setrole(afb_req req) noexcept
323 std::lock_guard<std::mutex> guard(binding_m);
324 if (g_afb_instance == nullptr)
326 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
332 char const *appid = afb_req_get_application_id(req);
333 json_object *jreq = afb_req_json(req);
335 json_object *j_role = nullptr;
336 if (!json_object_object_get_ex(jreq, "role", &j_role))
338 afb_req_fail(req, "failed", "Need char const* argument role");
341 char const *a_role = json_object_get_string(j_role);
343 json_object *j_pid = nullptr;
344 if (json_object_object_get_ex(jreq, "pid", &j_pid))
346 HMI_DEBUG("wm", "PID is set");
347 char const *a_pid = json_object_get_string(j_pid);
348 pid = std::stol(a_pid);
351 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role, pid);
354 afb_req_fail(req, "failed", "Couldn't register");
358 createSecurityContext(req, appid, a_role);
360 afb_req_success(req, NULL, "success");
362 catch (std::exception &e)
364 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
369 void windowmanager_activatewindow(afb_req req) noexcept
371 std::lock_guard<std::mutex> guard(binding_m);
375 if (g_afb_instance == nullptr)
377 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
383 const char *a_drawing_name = afb_req_value(req, "drawing_name");
386 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
390 const char *a_drawing_area = afb_req_value(req, "drawing_area");
393 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
397 g_afb_instance->wmgr.api_activate_surface(
398 afb_req_get_application_id(req),
399 a_drawing_name, a_drawing_area,
400 [&req](const char *errmsg) {
401 if (errmsg != nullptr)
403 HMI_ERROR("wm", errmsg);
404 afb_req_fail(req, "failed", errmsg);
407 afb_req_success(req, NULL, "success");
410 catch (std::exception &e)
412 HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
413 g_afb_instance->wmgr.exceptionProcessForTransition();
418 void windowmanager_deactivatewindow(afb_req req) noexcept
420 std::lock_guard<std::mutex> guard(binding_m);
424 if (g_afb_instance == nullptr)
426 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
432 const char *a_drawing_name = afb_req_value(req, "drawing_name");
435 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
439 g_afb_instance->wmgr.api_deactivate_surface(
440 afb_req_get_application_id(req), a_drawing_name,
441 [&req](const char *errmsg) {
442 if (errmsg != nullptr)
444 HMI_ERROR("wm", errmsg);
445 afb_req_fail(req, "failed", errmsg);
448 afb_req_success(req, NULL, "success");
451 catch (std::exception &e)
453 HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
454 g_afb_instance->wmgr.exceptionProcessForTransition();
459 void windowmanager_enddraw(afb_req req) noexcept
461 std::lock_guard<std::mutex> guard(binding_m);
465 if (g_afb_instance == nullptr)
467 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
473 const char *a_drawing_name = afb_req_value(req, "drawing_name");
476 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
479 afb_req_success(req, NULL, "success");
481 g_afb_instance->wmgr.api_enddraw(
482 afb_req_get_application_id(req), a_drawing_name);
484 catch (std::exception &e)
486 HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
487 g_afb_instance->wmgr.exceptionProcessForTransition();
492 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
494 std::lock_guard<std::mutex> guard(binding_m);
498 if (g_afb_instance == nullptr)
500 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
506 auto ret = g_afb_instance->wmgr.api_get_display_info();
509 afb_req_fail(req, "failed", ret.unwrap_err());
513 afb_req_success(req, ret.unwrap(), "success");
515 catch (std::exception &e)
517 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
522 void windowmanager_getareainfo_thunk(afb_req req) noexcept
524 std::lock_guard<std::mutex> guard(binding_m);
528 if (g_afb_instance == nullptr)
530 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
536 json_object *jreq = afb_req_json(req);
538 json_object *j_drawing_name = nullptr;
539 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
541 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
544 char const *a_drawing_name = json_object_get_string(j_drawing_name);
546 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
549 afb_req_fail(req, "failed", ret.unwrap_err());
553 afb_req_success(req, ret.unwrap(), "success");
555 catch (std::exception &e)
557 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
562 void windowmanager_wm_subscribe(afb_req req) noexcept
564 std::lock_guard<std::mutex> guard(binding_m);
568 if (g_afb_instance == nullptr)
570 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
576 json_object *jreq = afb_req_json(req);
577 json_object *j = nullptr;
578 if (!json_object_object_get_ex(jreq, "event", &j))
580 afb_req_fail(req, "failed", "Need char const* argument event");
583 int event_type = json_object_get_int(j);
584 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
585 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
586 int ret = afb_req_subscribe(req, event);
589 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
592 afb_req_success(req, NULL, "success");
594 catch (std::exception &e)
596 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
601 void windowmanager_list_drawing_names(afb_req req) noexcept
603 std::lock_guard<std::mutex> guard(binding_m);
607 if (g_afb_instance == nullptr)
609 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
616 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
617 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
620 afb_req_fail(req, "failed", ret.unwrap_err());
624 afb_req_success(req, ret.unwrap(), "success");
626 catch (std::exception &e)
628 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
633 void windowmanager_ping(afb_req req) noexcept
635 std::lock_guard<std::mutex> guard(binding_m);
639 if (g_afb_instance == nullptr)
641 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
648 g_afb_instance->wmgr.api_ping();
650 afb_req_success(req, NULL, "success");
652 catch (std::exception &e)
654 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
659 void windowmanager_debug_status(afb_req req) noexcept
661 std::lock_guard<std::mutex> guard(binding_m);
665 if (g_afb_instance == nullptr)
667 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
674 json_object *jr = json_object_new_object();
675 json_object_object_add(jr, "surfaces",
676 to_json(g_afb_instance->wmgr.controller->sprops));
677 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
679 afb_req_success(req, jr, "success");
681 catch (std::exception &e)
683 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
688 void windowmanager_debug_layers(afb_req req) noexcept
690 std::lock_guard<std::mutex> guard(binding_m);
694 if (g_afb_instance == nullptr)
696 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
702 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
704 afb_req_success(req, ret, "success");
706 catch (std::exception &e)
708 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
713 void windowmanager_debug_surfaces(afb_req req) noexcept
715 std::lock_guard<std::mutex> guard(binding_m);
719 if (g_afb_instance == nullptr)
721 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
728 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
731 afb_req_fail(req, "failed", ret.unwrap_err());
735 afb_req_success(req, ret.unwrap(), "success");
737 catch (std::exception &e)
739 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
744 void windowmanager_debug_terminate(afb_req req) noexcept
746 std::lock_guard<std::mutex> guard(binding_m);
750 if (g_afb_instance == nullptr)
752 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
759 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
761 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
762 // doesn't play well with perf
765 afb_req_success(req, NULL, "success");
767 catch (std::exception &e)
769 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
774 const struct afb_verb_v2 windowmanager_verbs[] = {
775 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
776 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
777 {"setrole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
778 {"activatewindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
779 {"deactivatewindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
780 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
781 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
782 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
783 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
784 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
785 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
786 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
787 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
788 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
789 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
792 extern "C" const struct afb_binding_v2 afbBindingV2 = {
793 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};