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 bool checkFirstReq(afb_req req)
181 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
182 return (ctxt) ? false : true;
185 static void cbRemoveClientCtxt(void *data)
187 WMClientCtxt *ctxt = (WMClientCtxt *)data;
192 HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
193 // Lookup surfaceID and remove it because App is dead.
194 auto pSid = g_afb_instance->wmgr.id_alloc.lookup(ctxt->role.c_str());
198 auto o_state = *g_afb_instance->wmgr.layers.get_layout_state(sid);
199 if (o_state != nullptr)
201 if (o_state->main == sid)
205 else if (o_state->sub == sid)
210 g_afb_instance->wmgr.id_alloc.remove_id(sid);
211 g_afb_instance->wmgr.layers.remove_surface(sid);
212 g_afb_instance->wmgr.controller->sprops.erase(sid);
213 g_afb_instance->wmgr.controller->surfaces.erase(sid);
214 HMI_DEBUG("wm", "delete surfaceID %d", sid);
216 g_afb_instance->wmgr.removeClient(ctxt->name);
220 void windowmanager_requestsurface(afb_req req) noexcept
222 std::lock_guard<std::mutex> guard(binding_m);
226 if (g_afb_instance == nullptr)
228 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
234 const char *a_drawing_name = afb_req_value(req, "drawing_name");
237 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
241 /* Create Security Context */
242 bool isFirstReq = checkFirstReq(req);
245 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
246 HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
247 if (ctxt->name != std::string(a_drawing_name))
249 afb_req_fail_f(req, "failed", "Dont request with other name: %s for now", a_drawing_name);
250 HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
255 auto ret = g_afb_instance->wmgr.api_request_surface(
256 afb_req_get_application_id(req), a_drawing_name);
260 WMClientCtxt *ctxt = new WMClientCtxt(afb_req_get_application_id(req), a_drawing_name);
261 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
262 afb_req_session_set_LOA(req, 1);
263 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
267 HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
272 afb_req_fail(req, "failed", ret.unwrap_err());
276 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
278 catch (std::exception &e)
280 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
285 void windowmanager_requestsurfacexdg(afb_req req) noexcept
287 std::lock_guard<std::mutex> guard(binding_m);
291 if (g_afb_instance == nullptr)
293 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
299 json_object *jreq = afb_req_json(req);
301 json_object *j_drawing_name = nullptr;
302 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
304 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
307 char const *a_drawing_name = json_object_get_string(j_drawing_name);
309 json_object *j_ivi_id = nullptr;
310 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
312 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
315 char const *a_ivi_id = json_object_get_string(j_ivi_id);
317 auto ret = g_afb_instance->wmgr.api_request_surface(
318 afb_req_get_application_id(req), a_drawing_name, a_ivi_id);
321 afb_req_fail(req, "failed", ret);
325 afb_req_success(req, NULL, "success");
327 catch (std::exception &e)
329 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
334 void windowmanager_activatesurface(afb_req req) noexcept
336 std::lock_guard<std::mutex> guard(binding_m);
340 if (g_afb_instance == nullptr)
342 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
348 const char *a_drawing_name = afb_req_value(req, "drawing_name");
351 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
355 const char *a_drawing_area = afb_req_value(req, "drawing_area");
358 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
362 g_afb_instance->wmgr.api_activate_surface(
363 afb_req_get_application_id(req),
364 a_drawing_name, a_drawing_area,
365 [&req](const char *errmsg) {
366 if (errmsg != nullptr)
368 HMI_ERROR("wm", errmsg);
369 afb_req_fail(req, "failed", errmsg);
372 afb_req_success(req, NULL, "success");
375 catch (std::exception &e)
377 HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
378 g_afb_instance->wmgr.exceptionProcessForTransition();
383 void windowmanager_deactivatesurface(afb_req req) noexcept
385 std::lock_guard<std::mutex> guard(binding_m);
389 if (g_afb_instance == nullptr)
391 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
397 const char *a_drawing_name = afb_req_value(req, "drawing_name");
400 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
404 g_afb_instance->wmgr.api_deactivate_surface(
405 afb_req_get_application_id(req), a_drawing_name,
406 [&req](const char *errmsg) {
407 if (errmsg != nullptr)
409 HMI_ERROR("wm", errmsg);
410 afb_req_fail(req, "failed", errmsg);
413 afb_req_success(req, NULL, "success");
416 catch (std::exception &e)
418 HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
419 g_afb_instance->wmgr.exceptionProcessForTransition();
424 void windowmanager_enddraw(afb_req req) noexcept
426 std::lock_guard<std::mutex> guard(binding_m);
430 if (g_afb_instance == nullptr)
432 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
438 const char *a_drawing_name = afb_req_value(req, "drawing_name");
441 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
444 afb_req_success(req, NULL, "success");
446 g_afb_instance->wmgr.api_enddraw(
447 afb_req_get_application_id(req), a_drawing_name);
449 catch (std::exception &e)
451 HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
452 g_afb_instance->wmgr.exceptionProcessForTransition();
457 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
459 std::lock_guard<std::mutex> guard(binding_m);
463 if (g_afb_instance == nullptr)
465 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
471 auto ret = g_afb_instance->wmgr.api_get_display_info();
474 afb_req_fail(req, "failed", ret.unwrap_err());
478 afb_req_success(req, ret.unwrap(), "success");
480 catch (std::exception &e)
482 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
487 void windowmanager_getareainfo_thunk(afb_req req) noexcept
489 std::lock_guard<std::mutex> guard(binding_m);
493 if (g_afb_instance == nullptr)
495 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
501 json_object *jreq = afb_req_json(req);
503 json_object *j_drawing_name = nullptr;
504 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
506 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
509 char const *a_drawing_name = json_object_get_string(j_drawing_name);
511 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
514 afb_req_fail(req, "failed", ret.unwrap_err());
518 afb_req_success(req, ret.unwrap(), "success");
520 catch (std::exception &e)
522 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
527 void windowmanager_wm_subscribe(afb_req req) noexcept
529 std::lock_guard<std::mutex> guard(binding_m);
533 if (g_afb_instance == nullptr)
535 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
541 json_object *jreq = afb_req_json(req);
542 json_object *j = nullptr;
543 if (!json_object_object_get_ex(jreq, "event", &j))
545 afb_req_fail(req, "failed", "Need char const* argument event");
548 int event_type = json_object_get_int(j);
549 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
550 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
551 int ret = afb_req_subscribe(req, event);
554 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
557 afb_req_success(req, NULL, "success");
559 catch (std::exception &e)
561 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
566 void windowmanager_list_drawing_names(afb_req req) noexcept
568 std::lock_guard<std::mutex> guard(binding_m);
572 if (g_afb_instance == nullptr)
574 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
581 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
582 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
585 afb_req_fail(req, "failed", ret.unwrap_err());
589 afb_req_success(req, ret.unwrap(), "success");
591 catch (std::exception &e)
593 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
598 void windowmanager_ping(afb_req req) noexcept
600 std::lock_guard<std::mutex> guard(binding_m);
604 if (g_afb_instance == nullptr)
606 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
613 g_afb_instance->wmgr.api_ping();
615 afb_req_success(req, NULL, "success");
617 catch (std::exception &e)
619 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
624 void windowmanager_debug_status(afb_req req) noexcept
626 std::lock_guard<std::mutex> guard(binding_m);
630 if (g_afb_instance == nullptr)
632 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
639 json_object *jr = json_object_new_object();
640 json_object_object_add(jr, "surfaces",
641 to_json(g_afb_instance->wmgr.controller->sprops));
642 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
644 afb_req_success(req, jr, "success");
646 catch (std::exception &e)
648 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
653 void windowmanager_debug_layers(afb_req req) noexcept
655 std::lock_guard<std::mutex> guard(binding_m);
659 if (g_afb_instance == nullptr)
661 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
667 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
669 afb_req_success(req, ret, "success");
671 catch (std::exception &e)
673 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
678 void windowmanager_debug_surfaces(afb_req req) noexcept
680 std::lock_guard<std::mutex> guard(binding_m);
684 if (g_afb_instance == nullptr)
686 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
693 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
696 afb_req_fail(req, "failed", ret.unwrap_err());
700 afb_req_success(req, ret.unwrap(), "success");
702 catch (std::exception &e)
704 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
709 void windowmanager_debug_terminate(afb_req req) noexcept
711 std::lock_guard<std::mutex> guard(binding_m);
715 if (g_afb_instance == nullptr)
717 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
724 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
726 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
727 // doesn't play well with perf
730 afb_req_success(req, NULL, "success");
732 catch (std::exception &e)
734 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
739 const struct afb_verb_v2 windowmanager_verbs[] = {
740 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
741 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
742 {"activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_LOA_1},
743 {"deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_LOA_1},
744 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_LOA_1},
745 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
746 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_LOA_1},
747 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
748 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
749 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
750 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
751 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
752 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
753 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
756 extern "C" const struct afb_binding_v2 afbBindingV2 = {
757 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};