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 *errmsg) {
193 if (errmsg != nullptr)
195 HMI_ERROR("wm", errmsg);
200 g_afb_instance->wmgr.removeClient(ctxt->name);
204 static void createSecurityContext(afb_req req, const char* appid, const char* role)
206 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
209 // Create Security Context at first time
210 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
211 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
212 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
213 afb_req_session_set_LOA(req, 1);
214 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
218 void windowmanager_requestsurface(afb_req req) noexcept
220 std::lock_guard<std::mutex> guard(binding_m);
224 if (g_afb_instance == nullptr)
226 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
232 const char *a_drawing_name = afb_req_value(req, "drawing_name");
235 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
239 const char *appid = afb_req_get_application_id(req);
240 auto ret = g_afb_instance->wmgr.api_request_surface(
241 appid, a_drawing_name);
244 afb_req_fail(req, "failed", ret.unwrap_err());
248 createSecurityContext(req, appid, a_drawing_name);
250 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
252 catch (std::exception &e)
254 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
259 void windowmanager_requestsurfacexdg(afb_req req) noexcept
261 std::lock_guard<std::mutex> guard(binding_m);
265 if (g_afb_instance == nullptr)
267 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
273 json_object *jreq = afb_req_json(req);
275 json_object *j_drawing_name = nullptr;
276 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
278 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
281 char const *a_drawing_name = json_object_get_string(j_drawing_name);
283 json_object *j_ivi_id = nullptr;
284 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
286 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
289 char const *a_ivi_id = json_object_get_string(j_ivi_id);
290 char const *appid = afb_req_get_application_id(req);
291 auto ret = g_afb_instance->wmgr.api_request_surface(
292 appid, a_drawing_name, a_ivi_id);
296 afb_req_fail(req, "failed", ret);
300 createSecurityContext(req, appid, a_drawing_name);
302 afb_req_success(req, NULL, "success");
304 catch (std::exception &e)
306 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
311 void windowmanager_activatewindow(afb_req req) noexcept
313 std::lock_guard<std::mutex> guard(binding_m);
317 if (g_afb_instance == nullptr)
319 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
325 const char *a_drawing_name = afb_req_value(req, "drawing_name");
328 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
332 const char *a_drawing_area = afb_req_value(req, "drawing_area");
335 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
339 g_afb_instance->wmgr.api_activate_surface(
340 afb_req_get_application_id(req),
341 a_drawing_name, a_drawing_area,
342 [&req](const char *errmsg) {
343 if (errmsg != nullptr)
345 HMI_ERROR("wm", errmsg);
346 afb_req_fail(req, "failed", errmsg);
349 afb_req_success(req, NULL, "success");
352 catch (std::exception &e)
354 HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
355 g_afb_instance->wmgr.exceptionProcessForTransition();
360 void windowmanager_deactivatewindow(afb_req req) noexcept
362 std::lock_guard<std::mutex> guard(binding_m);
366 if (g_afb_instance == nullptr)
368 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
374 const char *a_drawing_name = afb_req_value(req, "drawing_name");
377 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
381 g_afb_instance->wmgr.api_deactivate_surface(
382 afb_req_get_application_id(req), a_drawing_name,
383 [&req](const char *errmsg) {
384 if (errmsg != nullptr)
386 HMI_ERROR("wm", errmsg);
387 afb_req_fail(req, "failed", errmsg);
390 afb_req_success(req, NULL, "success");
393 catch (std::exception &e)
395 HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
396 g_afb_instance->wmgr.exceptionProcessForTransition();
401 void windowmanager_enddraw(afb_req req) noexcept
403 std::lock_guard<std::mutex> guard(binding_m);
407 if (g_afb_instance == nullptr)
409 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
415 const char *a_drawing_name = afb_req_value(req, "drawing_name");
418 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
421 afb_req_success(req, NULL, "success");
423 g_afb_instance->wmgr.api_enddraw(
424 afb_req_get_application_id(req), a_drawing_name);
426 catch (std::exception &e)
428 HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
429 g_afb_instance->wmgr.exceptionProcessForTransition();
434 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
436 std::lock_guard<std::mutex> guard(binding_m);
440 if (g_afb_instance == nullptr)
442 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
448 auto ret = g_afb_instance->wmgr.api_get_display_info();
451 afb_req_fail(req, "failed", ret.unwrap_err());
455 afb_req_success(req, ret.unwrap(), "success");
457 catch (std::exception &e)
459 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
464 void windowmanager_getareainfo_thunk(afb_req req) noexcept
466 std::lock_guard<std::mutex> guard(binding_m);
470 if (g_afb_instance == nullptr)
472 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
478 json_object *jreq = afb_req_json(req);
480 json_object *j_drawing_name = nullptr;
481 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
483 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
486 char const *a_drawing_name = json_object_get_string(j_drawing_name);
488 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
491 afb_req_fail(req, "failed", ret.unwrap_err());
495 afb_req_success(req, ret.unwrap(), "success");
497 catch (std::exception &e)
499 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
504 void windowmanager_wm_subscribe(afb_req req) noexcept
506 std::lock_guard<std::mutex> guard(binding_m);
510 if (g_afb_instance == nullptr)
512 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
518 json_object *jreq = afb_req_json(req);
519 json_object *j = nullptr;
520 if (!json_object_object_get_ex(jreq, "event", &j))
522 afb_req_fail(req, "failed", "Need char const* argument event");
525 int event_type = json_object_get_int(j);
526 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
527 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
528 int ret = afb_req_subscribe(req, event);
531 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
534 afb_req_success(req, NULL, "success");
536 catch (std::exception &e)
538 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
543 void windowmanager_list_drawing_names(afb_req req) noexcept
545 std::lock_guard<std::mutex> guard(binding_m);
549 if (g_afb_instance == nullptr)
551 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
558 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
559 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
562 afb_req_fail(req, "failed", ret.unwrap_err());
566 afb_req_success(req, ret.unwrap(), "success");
568 catch (std::exception &e)
570 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
575 void windowmanager_ping(afb_req req) noexcept
577 std::lock_guard<std::mutex> guard(binding_m);
581 if (g_afb_instance == nullptr)
583 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
590 g_afb_instance->wmgr.api_ping();
592 afb_req_success(req, NULL, "success");
594 catch (std::exception &e)
596 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
601 void windowmanager_debug_status(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 json_object *jr = json_object_new_object();
617 json_object_object_add(jr, "surfaces",
618 to_json(g_afb_instance->wmgr.controller->sprops));
619 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
621 afb_req_success(req, jr, "success");
623 catch (std::exception &e)
625 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
630 void windowmanager_debug_layers(afb_req req) noexcept
632 std::lock_guard<std::mutex> guard(binding_m);
636 if (g_afb_instance == nullptr)
638 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
644 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
646 afb_req_success(req, ret, "success");
648 catch (std::exception &e)
650 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
655 void windowmanager_debug_surfaces(afb_req req) noexcept
657 std::lock_guard<std::mutex> guard(binding_m);
661 if (g_afb_instance == nullptr)
663 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
670 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
673 afb_req_fail(req, "failed", ret.unwrap_err());
677 afb_req_success(req, ret.unwrap(), "success");
679 catch (std::exception &e)
681 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
686 void windowmanager_debug_terminate(afb_req req) noexcept
688 std::lock_guard<std::mutex> guard(binding_m);
692 if (g_afb_instance == nullptr)
694 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
701 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
703 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
704 // doesn't play well with perf
707 afb_req_success(req, NULL, "success");
709 catch (std::exception &e)
711 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
716 const struct afb_verb_v2 windowmanager_verbs[] = {
717 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
718 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
719 {"activatewindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
720 {"deactivatewindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
721 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
722 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
723 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
724 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
725 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
726 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
727 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
728 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
729 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
730 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
733 extern "C" const struct afb_binding_v2 afbBindingV2 = {
734 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};