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 g_afb_instance->wmgr.id_alloc.remove_id(sid);
199 g_afb_instance->wmgr.layers.remove_surface(sid);
200 g_afb_instance->wmgr.controller->sprops.erase(sid);
201 g_afb_instance->wmgr.controller->surfaces.erase(sid);
202 HMI_DEBUG("wm", "delete surfaceID %d", sid);
204 g_afb_instance->wmgr.removeClient(ctxt->name);
208 void windowmanager_requestsurface(afb_req req) noexcept
210 std::lock_guard<std::mutex> guard(binding_m);
214 if (g_afb_instance == nullptr)
216 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
222 const char *a_drawing_name = afb_req_value(req, "drawing_name");
225 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
229 /* Create Security Context */
230 bool isFirstReq = checkFirstReq(req);
233 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
234 HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
235 if (ctxt->name != std::string(a_drawing_name))
237 afb_req_fail_f(req, "failed", "Dont request with other name: %s for now", a_drawing_name);
238 HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
243 auto ret = g_afb_instance->wmgr.api_request_surface(
244 afb_req_get_application_id(req), a_drawing_name);
248 WMClientCtxt *ctxt = new WMClientCtxt(afb_req_get_application_id(req), a_drawing_name);
249 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
250 afb_req_session_set_LOA(req, 1);
251 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
255 HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
260 afb_req_fail(req, "failed", ret.unwrap_err());
264 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
266 catch (std::exception &e)
268 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
273 void windowmanager_requestsurfacexdg(afb_req req) noexcept
275 std::lock_guard<std::mutex> guard(binding_m);
279 if (g_afb_instance == nullptr)
281 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
287 json_object *jreq = afb_req_json(req);
289 json_object *j_drawing_name = nullptr;
290 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
292 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
295 char const *a_drawing_name = json_object_get_string(j_drawing_name);
297 json_object *j_ivi_id = nullptr;
298 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
300 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
303 char const *a_ivi_id = json_object_get_string(j_ivi_id);
305 auto ret = g_afb_instance->wmgr.api_request_surface(
306 afb_req_get_application_id(req), a_drawing_name, a_ivi_id);
309 afb_req_fail(req, "failed", ret);
313 afb_req_success(req, NULL, "success");
315 catch (std::exception &e)
317 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
322 void windowmanager_activatewindow(afb_req req) noexcept
324 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 const char *a_drawing_area = afb_req_value(req, "drawing_area");
346 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
350 g_afb_instance->wmgr.api_activate_surface(
351 afb_req_get_application_id(req),
352 a_drawing_name, a_drawing_area,
353 [&req](const char *errmsg) {
354 if (errmsg != nullptr)
356 HMI_ERROR("wm", errmsg);
357 afb_req_fail(req, "failed", errmsg);
360 afb_req_success(req, NULL, "success");
363 catch (std::exception &e)
365 HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
366 g_afb_instance->wmgr.exceptionProcessForTransition();
371 void windowmanager_deactivatewindow(afb_req req) noexcept
373 std::lock_guard<std::mutex> guard(binding_m);
377 if (g_afb_instance == nullptr)
379 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
385 const char *a_drawing_name = afb_req_value(req, "drawing_name");
388 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
392 g_afb_instance->wmgr.api_deactivate_surface(
393 afb_req_get_application_id(req), a_drawing_name,
394 [&req](const char *errmsg) {
395 if (errmsg != nullptr)
397 HMI_ERROR("wm", errmsg);
398 afb_req_fail(req, "failed", errmsg);
401 afb_req_success(req, NULL, "success");
404 catch (std::exception &e)
406 HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
407 g_afb_instance->wmgr.exceptionProcessForTransition();
412 void windowmanager_enddraw(afb_req req) noexcept
414 std::lock_guard<std::mutex> guard(binding_m);
418 if (g_afb_instance == nullptr)
420 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
426 const char *a_drawing_name = afb_req_value(req, "drawing_name");
429 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
432 afb_req_success(req, NULL, "success");
434 g_afb_instance->wmgr.api_enddraw(
435 afb_req_get_application_id(req), a_drawing_name);
437 catch (std::exception &e)
439 HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
440 g_afb_instance->wmgr.exceptionProcessForTransition();
445 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
447 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 auto ret = g_afb_instance->wmgr.api_get_display_info();
462 afb_req_fail(req, "failed", ret.unwrap_err());
466 afb_req_success(req, ret.unwrap(), "success");
468 catch (std::exception &e)
470 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
475 void windowmanager_getareainfo_thunk(afb_req req) noexcept
477 std::lock_guard<std::mutex> guard(binding_m);
481 if (g_afb_instance == nullptr)
483 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
489 json_object *jreq = afb_req_json(req);
491 json_object *j_drawing_name = nullptr;
492 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
494 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
497 char const *a_drawing_name = json_object_get_string(j_drawing_name);
499 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
502 afb_req_fail(req, "failed", ret.unwrap_err());
506 afb_req_success(req, ret.unwrap(), "success");
508 catch (std::exception &e)
510 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
515 void windowmanager_wm_subscribe(afb_req req) noexcept
517 std::lock_guard<std::mutex> guard(binding_m);
521 if (g_afb_instance == nullptr)
523 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
529 json_object *jreq = afb_req_json(req);
530 json_object *j = nullptr;
531 if (!json_object_object_get_ex(jreq, "event", &j))
533 afb_req_fail(req, "failed", "Need char const* argument event");
536 int event_type = json_object_get_int(j);
537 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
538 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
539 int ret = afb_req_subscribe(req, event);
542 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
545 afb_req_success(req, NULL, "success");
547 catch (std::exception &e)
549 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
554 void windowmanager_list_drawing_names(afb_req req) noexcept
556 std::lock_guard<std::mutex> guard(binding_m);
560 if (g_afb_instance == nullptr)
562 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
569 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
570 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
573 afb_req_fail(req, "failed", ret.unwrap_err());
577 afb_req_success(req, ret.unwrap(), "success");
579 catch (std::exception &e)
581 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
586 void windowmanager_ping(afb_req req) noexcept
588 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 g_afb_instance->wmgr.api_ping();
603 afb_req_success(req, NULL, "success");
605 catch (std::exception &e)
607 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
612 void windowmanager_debug_status(afb_req req) noexcept
614 std::lock_guard<std::mutex> guard(binding_m);
618 if (g_afb_instance == nullptr)
620 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
627 json_object *jr = json_object_new_object();
628 json_object_object_add(jr, "surfaces",
629 to_json(g_afb_instance->wmgr.controller->sprops));
630 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
632 afb_req_success(req, jr, "success");
634 catch (std::exception &e)
636 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
641 void windowmanager_debug_layers(afb_req req) noexcept
643 std::lock_guard<std::mutex> guard(binding_m);
647 if (g_afb_instance == nullptr)
649 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
655 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
657 afb_req_success(req, ret, "success");
659 catch (std::exception &e)
661 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
666 void windowmanager_debug_surfaces(afb_req req) noexcept
668 std::lock_guard<std::mutex> guard(binding_m);
672 if (g_afb_instance == nullptr)
674 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
681 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
684 afb_req_fail(req, "failed", ret.unwrap_err());
688 afb_req_success(req, ret.unwrap(), "success");
690 catch (std::exception &e)
692 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
697 void windowmanager_debug_terminate(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 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
714 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
715 // doesn't play well with perf
718 afb_req_success(req, NULL, "success");
720 catch (std::exception &e)
722 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
727 const struct afb_verb_v2 windowmanager_verbs[] = {
728 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
729 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
730 {"activatesurface", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
731 {"activatewindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
732 {"deactivatewindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
733 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
734 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
735 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
736 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
737 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
738 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
739 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
740 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
741 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
742 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
745 extern "C" const struct afb_binding_v2 afbBindingV2 = {
746 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};