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"
23 #include "wayland_ivi_wm.hpp"
27 #include <afb/afb-binding.h>
28 #include <systemd/sd-event.h>
31 typedef struct WMClientCtxt
35 WMClientCtxt(const char *appName, const char* appRole)
44 std::unique_ptr<wl::display> display;
45 wm::WindowManager wmgr;
47 afb_instance() : display{new wl::display}, wmgr{this->display.get()} {}
52 struct afb_instance *g_afb_instance;
55 int afb_instance::init()
57 return this->wmgr.init();
60 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
63 if ((events & EPOLLHUP) != 0)
65 HMI_ERROR("The compositor hung up, dying now.");
66 delete g_afb_instance;
67 g_afb_instance = nullptr;
71 if ((events & EPOLLIN) != 0u)
74 g_afb_instance->wmgr.display->read_events();
75 g_afb_instance->wmgr.set_pending_events();
78 // We want do dispatch pending wayland events from within
80 afb_service_call("windowmanager", "ping", json_object_new_object(),
81 [](void *c, int st, json_object *j) {
90 sd_event_source_unref(evs);
91 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
100 HMI_NOTICE("WinMan ver. %s", WINMAN_VERSION_STRING);
102 if (g_afb_instance != nullptr)
104 HMI_ERROR("Wayland context already initialized?");
108 if (getenv("XDG_RUNTIME_DIR") == nullptr)
110 HMI_ERROR("Environment variable XDG_RUNTIME_DIR not set");
115 // wait until wayland compositor starts up.
117 g_afb_instance = new afb_instance;
118 while (!g_afb_instance->display->ok())
123 HMI_ERROR("Could not connect to compositor");
126 HMI_ERROR("Wait to start weston ...");
128 delete g_afb_instance;
129 g_afb_instance = new afb_instance;
133 if (g_afb_instance->init() == -1)
135 HMI_ERROR("Could not connect to compositor");
140 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
141 g_afb_instance->display->get_fd(), EPOLLIN,
142 display_event_callback, g_afb_instance);
145 HMI_ERROR("Could not initialize afb_instance event handler: %d", -ret);
150 atexit([] { delete g_afb_instance; });
155 delete g_afb_instance;
156 g_afb_instance = nullptr;
160 int binding_init() noexcept
164 return _binding_init();
166 catch (std::exception &e)
168 HMI_ERROR("Uncaught exception in binding_init(): %s", e.what());
173 static void cbRemoveClientCtxt(void *data)
175 WMClientCtxt *ctxt = (WMClientCtxt *)data;
180 HMI_DEBUG("remove app %s", ctxt->name.c_str());
182 // Policy Manager does not know this app was killed,
183 // so notify it by deactivate request.
184 g_afb_instance->wmgr.api_deactivate_surface(
185 ctxt->name.c_str(), ctxt->role.c_str(),
186 [](const char *errmsg) {
187 if (errmsg != nullptr)
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("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);
215 if (g_afb_instance == nullptr)
217 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
223 const char *a_drawing_name = afb_req_value(req, "drawing_name");
226 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
230 const char *appid = afb_req_get_application_id(req);
231 auto ret = g_afb_instance->wmgr.api_request_surface(
232 appid, a_drawing_name);
235 afb_req_fail(req, "failed", ret.unwrap_err());
239 createSecurityContext(req, appid, a_drawing_name);
241 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
243 catch (std::exception &e)
245 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
250 void windowmanager_requestsurfacexdg(afb_req req) noexcept
252 std::lock_guard<std::mutex> guard(binding_m);
253 if (g_afb_instance == nullptr)
255 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
261 json_object *jreq = afb_req_json(req);
263 json_object *j_drawing_name = nullptr;
264 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
266 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
269 char const *a_drawing_name = json_object_get_string(j_drawing_name);
271 json_object *j_ivi_id = nullptr;
272 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
274 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
277 char const *a_ivi_id = json_object_get_string(j_ivi_id);
278 char const *appid = afb_req_get_application_id(req);
279 auto ret = g_afb_instance->wmgr.api_request_surface(
280 appid, a_drawing_name, a_ivi_id);
284 afb_req_fail(req, "failed", ret);
288 createSecurityContext(req, appid, a_drawing_name);
290 afb_req_success(req, NULL, "success");
292 catch (std::exception &e)
294 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
299 void windowmanager_activatewindow(afb_req req) noexcept
301 std::lock_guard<std::mutex> guard(binding_m);
302 if (g_afb_instance == nullptr)
304 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
310 const char *a_drawing_name = afb_req_value(req, "drawing_name");
313 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
317 const char *a_drawing_area = afb_req_value(req, "drawing_area");
320 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
324 g_afb_instance->wmgr.api_activate_surface(
325 afb_req_get_application_id(req),
326 a_drawing_name, a_drawing_area,
327 [&req](const char *errmsg) {
328 if (errmsg != nullptr)
331 afb_req_fail(req, "failed", errmsg);
334 afb_req_success(req, NULL, "success");
337 catch (std::exception &e)
339 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
340 g_afb_instance->wmgr.exceptionProcessForTransition();
345 void windowmanager_deactivatewindow(afb_req req) noexcept
347 std::lock_guard<std::mutex> guard(binding_m);
348 if (g_afb_instance == nullptr)
350 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
356 const char *a_drawing_name = afb_req_value(req, "drawing_name");
359 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
363 g_afb_instance->wmgr.api_deactivate_surface(
364 afb_req_get_application_id(req), a_drawing_name,
365 [&req](const char *errmsg) {
366 if (errmsg != nullptr)
369 afb_req_fail(req, "failed", errmsg);
372 afb_req_success(req, NULL, "success");
375 catch (std::exception &e)
377 HMI_WARNING("failed: Uncaught exception while calling deactivatesurface: %s", e.what());
378 g_afb_instance->wmgr.exceptionProcessForTransition();
383 void windowmanager_enddraw(afb_req req) noexcept
385 std::lock_guard<std::mutex> guard(binding_m);
386 if (g_afb_instance == nullptr)
388 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
394 const char *a_drawing_name = afb_req_value(req, "drawing_name");
397 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
400 afb_req_success(req, NULL, "success");
402 g_afb_instance->wmgr.api_enddraw(
403 afb_req_get_application_id(req), a_drawing_name);
405 catch (std::exception &e)
407 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
408 g_afb_instance->wmgr.exceptionProcessForTransition();
413 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
415 std::lock_guard<std::mutex> guard(binding_m);
416 if (g_afb_instance == nullptr)
418 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
424 auto ret = g_afb_instance->wmgr.api_get_display_info();
427 afb_req_fail(req, "failed", ret.unwrap_err());
431 afb_req_success(req, ret.unwrap(), "success");
433 catch (std::exception &e)
435 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
440 void windowmanager_getareainfo_thunk(afb_req req) noexcept
442 std::lock_guard<std::mutex> guard(binding_m);
443 if (g_afb_instance == nullptr)
445 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
451 json_object *jreq = afb_req_json(req);
453 json_object *j_drawing_name = nullptr;
454 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
456 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
459 char const *a_drawing_name = json_object_get_string(j_drawing_name);
461 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
464 afb_req_fail(req, "failed", ret.unwrap_err());
468 afb_req_success(req, ret.unwrap(), "success");
470 catch (std::exception &e)
472 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
477 void windowmanager_wm_subscribe(afb_req req) noexcept
479 std::lock_guard<std::mutex> guard(binding_m);
480 if (g_afb_instance == nullptr)
482 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
488 json_object *jreq = afb_req_json(req);
489 json_object *j = nullptr;
490 if (!json_object_object_get_ex(jreq, "event", &j))
492 afb_req_fail(req, "failed", "Need char const* argument event");
495 int event_type = json_object_get_int(j);
496 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
497 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
498 int ret = afb_req_subscribe(req, event);
501 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
504 afb_req_success(req, NULL, "success");
506 catch (std::exception &e)
508 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
513 void windowmanager_list_drawing_names(afb_req req) noexcept
515 std::lock_guard<std::mutex> guard(binding_m);
516 if (g_afb_instance == nullptr)
518 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
525 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
526 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
529 afb_req_fail(req, "failed", ret.unwrap_err());
533 afb_req_success(req, ret.unwrap(), "success");
535 catch (std::exception &e)
537 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
542 void windowmanager_ping(afb_req req) noexcept
544 std::lock_guard<std::mutex> guard(binding_m);
545 if (g_afb_instance == nullptr)
547 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
554 g_afb_instance->wmgr.api_ping();
556 afb_req_success(req, NULL, "success");
558 catch (std::exception &e)
560 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
565 void windowmanager_debug_status(afb_req req) noexcept
567 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?");
577 json_object *jr = json_object_new_object();
578 json_object_object_add(jr, "surfaces",
579 to_json(g_afb_instance->wmgr.controller->sprops));
580 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
582 afb_req_success(req, jr, "success");
584 catch (std::exception &e)
586 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
591 void windowmanager_debug_layers(afb_req req) noexcept
593 std::lock_guard<std::mutex> guard(binding_m);
594 if (g_afb_instance == nullptr)
596 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
602 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
604 afb_req_success(req, ret, "success");
606 catch (std::exception &e)
608 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
613 void windowmanager_debug_surfaces(afb_req req) noexcept
615 std::lock_guard<std::mutex> guard(binding_m);
616 if (g_afb_instance == nullptr)
618 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
625 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
628 afb_req_fail(req, "failed", ret.unwrap_err());
632 afb_req_success(req, ret.unwrap(), "success");
634 catch (std::exception &e)
636 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
641 void windowmanager_debug_terminate(afb_req req) noexcept
643 std::lock_guard<std::mutex> guard(binding_m);
644 if (g_afb_instance == nullptr)
646 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
653 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
655 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
656 // doesn't play well with perf
659 afb_req_success(req, NULL, "success");
661 catch (std::exception &e)
663 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
668 const struct afb_verb_v2 windowmanager_verbs[] = {
669 {"requestSurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
670 {"requestSurfaceXDG", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
671 {"activateWindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
672 {"deactivateWindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
673 {"endDraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
674 {"getDisplayInfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
675 {"getAreaInfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
676 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
677 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
678 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
679 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
680 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
681 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
682 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
685 extern "C" const struct afb_binding_v2 afbBindingV2 = {
686 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};