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());
188 // Policy Manager does not know this app was killed,
189 // so notify it by deactivate request.
190 g_afb_instance->wmgr.api_deactivate_surface(
191 ctxt->name.c_str(), ctxt->role.c_str(),
192 [](const char *) {});
194 g_afb_instance->wmgr.removeClient(ctxt->name);
198 static void createSecurityContext(afb_req req, const char* appid, const char* role)
200 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
203 // Create Security Context at first time
204 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
205 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
206 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
207 afb_req_session_set_LOA(req, 1);
208 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
212 void windowmanager_requestsurface(afb_req req) noexcept
214 std::lock_guard<std::mutex> guard(binding_m);
218 if (g_afb_instance == nullptr)
220 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
226 const char *a_drawing_name = afb_req_value(req, "drawing_name");
229 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
233 const char *appid = afb_req_get_application_id(req);
234 auto ret = g_afb_instance->wmgr.api_request_surface(
235 appid, a_drawing_name);
238 afb_req_fail(req, "failed", ret.unwrap_err());
242 createSecurityContext(req, appid, a_drawing_name);
244 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
246 catch (std::exception &e)
248 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
253 void windowmanager_requestsurfacexdg(afb_req req) noexcept
255 std::lock_guard<std::mutex> guard(binding_m);
259 if (g_afb_instance == nullptr)
261 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
267 json_object *jreq = afb_req_json(req);
269 json_object *j_drawing_name = nullptr;
270 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
272 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
275 char const *a_drawing_name = json_object_get_string(j_drawing_name);
277 json_object *j_ivi_id = nullptr;
278 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
280 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
283 char const *a_ivi_id = json_object_get_string(j_ivi_id);
284 char const *appid = afb_req_get_application_id(req);
285 auto ret = g_afb_instance->wmgr.api_request_surface(
286 appid, a_drawing_name, a_ivi_id);
290 afb_req_fail(req, "failed", ret);
294 createSecurityContext(req, appid, a_drawing_name);
296 afb_req_success(req, NULL, "success");
298 catch (std::exception &e)
300 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
305 void windowmanager_setrole(afb_req req) noexcept
307 std::lock_guard<std::mutex> guard(binding_m);
308 if (g_afb_instance == nullptr)
310 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
316 char const *appid = afb_req_get_application_id(req);
317 json_object *jreq = afb_req_json(req);
319 json_object *j_role = nullptr;
320 if (!json_object_object_get_ex(jreq, "role", &j_role))
322 afb_req_fail(req, "failed", "Need char const* argument role");
325 char const *a_role = json_object_get_string(j_role);
327 json_object *j_pid = nullptr;
328 if (json_object_object_get_ex(jreq, "pid", &j_pid))
330 HMI_DEBUG("wm", "PID is set");
331 char const *a_pid = json_object_get_string(j_pid);
332 pid = std::stol(a_pid);
335 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role, pid);
338 afb_req_fail(req, "failed", "Couldn't register");
342 createSecurityContext(req, appid, a_role);
344 afb_req_success(req, NULL, "success");
346 catch (std::exception &e)
348 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
353 void windowmanager_activatewindow(afb_req req) noexcept
355 std::lock_guard<std::mutex> guard(binding_m);
359 if (g_afb_instance == nullptr)
361 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
367 const char *a_drawing_name = afb_req_value(req, "drawing_name");
370 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
374 const char *a_drawing_area = afb_req_value(req, "drawing_area");
377 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
381 g_afb_instance->wmgr.api_activate_surface(
382 afb_req_get_application_id(req),
383 a_drawing_name, a_drawing_area,
384 [&req](const char *errmsg) {
385 if (errmsg != nullptr)
387 HMI_ERROR("wm", errmsg);
388 afb_req_fail(req, "failed", errmsg);
391 afb_req_success(req, NULL, "success");
394 catch (std::exception &e)
396 HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
397 g_afb_instance->wmgr.exceptionProcessForTransition();
402 void windowmanager_deactivatewindow(afb_req req) noexcept
404 std::lock_guard<std::mutex> guard(binding_m);
408 if (g_afb_instance == nullptr)
410 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
416 const char *a_drawing_name = afb_req_value(req, "drawing_name");
419 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
423 g_afb_instance->wmgr.api_deactivate_surface(
424 afb_req_get_application_id(req), a_drawing_name,
425 [&req](const char *errmsg) {
426 if (errmsg != nullptr)
428 HMI_ERROR("wm", errmsg);
429 afb_req_fail(req, "failed", errmsg);
432 afb_req_success(req, NULL, "success");
435 catch (std::exception &e)
437 HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
438 g_afb_instance->wmgr.exceptionProcessForTransition();
443 void windowmanager_enddraw(afb_req req) noexcept
445 std::lock_guard<std::mutex> guard(binding_m);
449 if (g_afb_instance == nullptr)
451 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
457 const char *a_drawing_name = afb_req_value(req, "drawing_name");
460 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
463 afb_req_success(req, NULL, "success");
465 g_afb_instance->wmgr.api_enddraw(
466 afb_req_get_application_id(req), a_drawing_name);
468 catch (std::exception &e)
470 HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
471 g_afb_instance->wmgr.exceptionProcessForTransition();
476 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
478 std::lock_guard<std::mutex> guard(binding_m);
482 if (g_afb_instance == nullptr)
484 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
490 auto ret = g_afb_instance->wmgr.api_get_display_info();
493 afb_req_fail(req, "failed", ret.unwrap_err());
497 afb_req_success(req, ret.unwrap(), "success");
499 catch (std::exception &e)
501 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
506 void windowmanager_getareainfo_thunk(afb_req req) noexcept
508 std::lock_guard<std::mutex> guard(binding_m);
512 if (g_afb_instance == nullptr)
514 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
520 json_object *jreq = afb_req_json(req);
522 json_object *j_drawing_name = nullptr;
523 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
525 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
528 char const *a_drawing_name = json_object_get_string(j_drawing_name);
530 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
533 afb_req_fail(req, "failed", ret.unwrap_err());
537 afb_req_success(req, ret.unwrap(), "success");
539 catch (std::exception &e)
541 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
546 void windowmanager_wm_subscribe(afb_req req) noexcept
548 std::lock_guard<std::mutex> guard(binding_m);
552 if (g_afb_instance == nullptr)
554 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
560 json_object *jreq = afb_req_json(req);
561 json_object *j = nullptr;
562 if (!json_object_object_get_ex(jreq, "event", &j))
564 afb_req_fail(req, "failed", "Need char const* argument event");
567 int event_type = json_object_get_int(j);
568 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
569 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
570 int ret = afb_req_subscribe(req, event);
573 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
576 afb_req_success(req, NULL, "success");
578 catch (std::exception &e)
580 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
585 void windowmanager_list_drawing_names(afb_req req) noexcept
587 std::lock_guard<std::mutex> guard(binding_m);
591 if (g_afb_instance == nullptr)
593 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
600 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
601 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
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 list_drawing_names: %s", e.what());
617 void windowmanager_ping(afb_req req) noexcept
619 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?");
632 g_afb_instance->wmgr.api_ping();
634 afb_req_success(req, NULL, "success");
636 catch (std::exception &e)
638 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
643 void windowmanager_debug_status(afb_req req) noexcept
645 std::lock_guard<std::mutex> guard(binding_m);
649 if (g_afb_instance == nullptr)
651 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
658 json_object *jr = json_object_new_object();
659 json_object_object_add(jr, "surfaces",
660 to_json(g_afb_instance->wmgr.controller->sprops));
661 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
663 afb_req_success(req, jr, "success");
665 catch (std::exception &e)
667 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
672 void windowmanager_debug_layers(afb_req req) noexcept
674 std::lock_guard<std::mutex> guard(binding_m);
678 if (g_afb_instance == nullptr)
680 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
686 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
688 afb_req_success(req, ret, "success");
690 catch (std::exception &e)
692 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
697 void windowmanager_debug_surfaces(afb_req req) noexcept
699 std::lock_guard<std::mutex> guard(binding_m);
703 if (g_afb_instance == nullptr)
705 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
712 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
715 afb_req_fail(req, "failed", ret.unwrap_err());
719 afb_req_success(req, ret.unwrap(), "success");
721 catch (std::exception &e)
723 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
728 void windowmanager_debug_terminate(afb_req req) noexcept
730 std::lock_guard<std::mutex> guard(binding_m);
734 if (g_afb_instance == nullptr)
736 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
743 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
745 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
746 // doesn't play well with perf
749 afb_req_success(req, NULL, "success");
751 catch (std::exception &e)
753 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
758 const struct afb_verb_v2 windowmanager_verbs[] = {
759 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
760 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
761 {"setrole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
762 {"activatewindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
763 {"deactivatewindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
764 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
765 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
766 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
767 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
768 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
769 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
770 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
771 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
772 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
773 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
776 extern "C" const struct afb_binding_v2 afbBindingV2 = {
777 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};