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>
34 std::unique_ptr<wl::display> display;
35 wm::WindowManager wmgr;
37 afb_instance() : display{new wl::display}, wmgr{this->display.get()} {}
42 struct afb_instance *g_afb_instance;
45 int afb_instance::init()
47 return this->wmgr.init();
50 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
55 if ((events & EPOLLHUP) != 0)
57 HMI_ERROR("wm", "The compositor hung up, dying now.");
58 delete g_afb_instance;
59 g_afb_instance = nullptr;
63 if ((events & EPOLLIN) != 0u)
66 STN(display_read_events);
67 g_afb_instance->wmgr.display->read_events();
68 g_afb_instance->wmgr.set_pending_events();
71 // We want do dispatch pending wayland events from within
73 STN(winman_ping_api_call);
74 afb_service_call("windowmanager", "ping", json_object_new_object(),
75 [](void *c, int st, json_object *j) {
76 STN(winman_ping_api_call_return);
85 sd_event_source_unref(evs);
86 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
95 HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
97 if (g_afb_instance != nullptr)
99 HMI_ERROR("wm", "Wayland context already initialized?");
103 if (getenv("XDG_RUNTIME_DIR") == nullptr)
105 HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
110 // wait until wayland compositor starts up.
112 g_afb_instance = new afb_instance;
113 while (!g_afb_instance->display->ok())
118 HMI_ERROR("wm", "Could not connect to compositor");
121 HMI_ERROR("wm", "Wait to start weston ...");
123 delete g_afb_instance;
124 g_afb_instance = new afb_instance;
128 if (g_afb_instance->init() == -1)
130 HMI_ERROR("wm", "Could not connect to compositor");
135 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
136 g_afb_instance->display->get_fd(), EPOLLIN,
137 display_event_callback, g_afb_instance);
140 HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
145 atexit([] { delete g_afb_instance; });
150 delete g_afb_instance;
151 g_afb_instance = nullptr;
155 int binding_init() noexcept
159 return _binding_init();
161 catch (std::exception &e)
163 HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
168 static void cbRemoveClientCtxt(void *data)
170 wm::WMClientCtxt *ctxt = (wm::WMClientCtxt *)data;
175 HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
176 g_afb_instance->wmgr.onApplicationTerminated(*ctxt);
180 static void createSecurityContext(afb_req req, const char* appid, const char* role)
182 wm::WMClientCtxt *ctxt = (wm::WMClientCtxt *)afb_req_context_get(req);
185 // Create Security Context at first time
186 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
187 wm::WMClientCtxt *ctxt = new wm::WMClientCtxt(appid, new_role);
188 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
189 afb_req_session_set_LOA(req, 1);
190 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
194 void windowmanager_requestsurface(afb_req req) noexcept
196 std::lock_guard<std::mutex> guard(binding_m);
200 if (g_afb_instance == nullptr)
202 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
208 const char *a_drawing_name = afb_req_value(req, "drawing_name");
211 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
215 const char *appid = afb_req_get_application_id(req);
216 auto ret = g_afb_instance->wmgr.api_request_surface(
217 appid, a_drawing_name);
220 afb_req_fail(req, "failed", ret.unwrap_err());
224 createSecurityContext(req, appid, a_drawing_name);
226 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
228 catch (std::exception &e)
230 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
235 void windowmanager_requestsurfacexdg(afb_req req) noexcept
237 std::lock_guard<std::mutex> guard(binding_m);
241 if (g_afb_instance == nullptr)
243 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
249 json_object *jreq = afb_req_json(req);
251 json_object *j_drawing_name = nullptr;
252 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
254 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
257 char const *a_drawing_name = json_object_get_string(j_drawing_name);
259 json_object *j_ivi_id = nullptr;
260 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
262 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
265 char const *a_ivi_id = json_object_get_string(j_ivi_id);
266 char const *appid = afb_req_get_application_id(req);
267 auto ret = g_afb_instance->wmgr.api_request_surface(
268 appid, a_drawing_name, a_ivi_id);
272 afb_req_fail(req, "failed", ret);
276 createSecurityContext(req, appid, a_drawing_name);
278 afb_req_success(req, NULL, "success");
280 catch (std::exception &e)
282 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
287 void windowmanager_activatewindow(afb_req req) noexcept
289 std::lock_guard<std::mutex> guard(binding_m);
293 if (g_afb_instance == nullptr)
295 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
301 const char *a_drawing_name = afb_req_value(req, "drawing_name");
304 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
308 const char *a_drawing_area = afb_req_value(req, "drawing_area");
311 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
315 g_afb_instance->wmgr.api_activate_surface(
316 afb_req_get_application_id(req),
317 a_drawing_name, a_drawing_area,
318 [&req](const char *errmsg) {
319 if (errmsg != nullptr)
321 HMI_ERROR("wm", errmsg);
322 afb_req_fail(req, "failed", errmsg);
325 afb_req_success(req, NULL, "success");
328 catch (std::exception &e)
330 HMI_WARNING("wm", "failed: Uncaught exception while calling activatesurface: %s", e.what());
331 g_afb_instance->wmgr.exceptionProcessForTransition();
336 void windowmanager_deactivatewindow(afb_req req) noexcept
338 std::lock_guard<std::mutex> guard(binding_m);
342 if (g_afb_instance == nullptr)
344 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
350 const char *a_drawing_name = afb_req_value(req, "drawing_name");
353 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
357 g_afb_instance->wmgr.api_deactivate_surface(
358 afb_req_get_application_id(req), a_drawing_name,
359 [&req](const char *errmsg) {
360 if (errmsg != nullptr)
362 HMI_ERROR("wm", errmsg);
363 afb_req_fail(req, "failed", errmsg);
366 afb_req_success(req, NULL, "success");
369 catch (std::exception &e)
371 HMI_WARNING("wm", "failed: Uncaught exception while calling deactivatesurface: %s", e.what());
372 g_afb_instance->wmgr.exceptionProcessForTransition();
377 void windowmanager_enddraw(afb_req req) noexcept
379 std::lock_guard<std::mutex> guard(binding_m);
383 if (g_afb_instance == nullptr)
385 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
391 const char *a_drawing_name = afb_req_value(req, "drawing_name");
394 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
397 afb_req_success(req, NULL, "success");
399 g_afb_instance->wmgr.api_enddraw(
400 afb_req_get_application_id(req), a_drawing_name);
402 catch (std::exception &e)
404 HMI_WARNING("wm", "failed: Uncaught exception while calling enddraw: %s", e.what());
405 g_afb_instance->wmgr.exceptionProcessForTransition();
410 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
412 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);
446 if (g_afb_instance == nullptr)
448 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
454 json_object *jreq = afb_req_json(req);
456 json_object *j_drawing_name = nullptr;
457 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
459 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
462 char const *a_drawing_name = json_object_get_string(j_drawing_name);
464 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
467 afb_req_fail(req, "failed", ret.unwrap_err());
471 afb_req_success(req, ret.unwrap(), "success");
473 catch (std::exception &e)
475 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
480 void windowmanager_wm_subscribe(afb_req req) noexcept
482 std::lock_guard<std::mutex> guard(binding_m);
486 if (g_afb_instance == nullptr)
488 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
494 json_object *jreq = afb_req_json(req);
495 json_object *j = nullptr;
496 if (!json_object_object_get_ex(jreq, "event", &j))
498 afb_req_fail(req, "failed", "Need char const* argument event");
501 int event_type = json_object_get_int(j);
502 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
503 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
504 int ret = afb_req_subscribe(req, event);
507 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
510 afb_req_success(req, NULL, "success");
512 catch (std::exception &e)
514 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
519 void windowmanager_list_drawing_names(afb_req req) noexcept
521 std::lock_guard<std::mutex> guard(binding_m);
525 if (g_afb_instance == nullptr)
527 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
534 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
535 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
538 afb_req_fail(req, "failed", ret.unwrap_err());
542 afb_req_success(req, ret.unwrap(), "success");
544 catch (std::exception &e)
546 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
551 void windowmanager_ping(afb_req req) noexcept
553 std::lock_guard<std::mutex> guard(binding_m);
557 if (g_afb_instance == nullptr)
559 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
566 g_afb_instance->wmgr.api_ping();
568 afb_req_success(req, NULL, "success");
570 catch (std::exception &e)
572 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
577 void windowmanager_debug_status(afb_req req) noexcept
579 std::lock_guard<std::mutex> guard(binding_m);
583 if (g_afb_instance == nullptr)
585 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
592 json_object *jr = json_object_new_object();
593 json_object_object_add(jr, "surfaces",
594 to_json(g_afb_instance->wmgr.controller->sprops));
595 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
597 afb_req_success(req, jr, "success");
599 catch (std::exception &e)
601 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
606 void windowmanager_debug_layers(afb_req req) noexcept
608 std::lock_guard<std::mutex> guard(binding_m);
612 if (g_afb_instance == nullptr)
614 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
620 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
622 afb_req_success(req, ret, "success");
624 catch (std::exception &e)
626 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
631 void windowmanager_debug_surfaces(afb_req req) noexcept
633 std::lock_guard<std::mutex> guard(binding_m);
637 if (g_afb_instance == nullptr)
639 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
646 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
649 afb_req_fail(req, "failed", ret.unwrap_err());
653 afb_req_success(req, ret.unwrap(), "success");
655 catch (std::exception &e)
657 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
662 void windowmanager_debug_terminate(afb_req req) noexcept
664 std::lock_guard<std::mutex> guard(binding_m);
668 if (g_afb_instance == nullptr)
670 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
677 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
679 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
680 // doesn't play well with perf
683 afb_req_success(req, NULL, "success");
685 catch (std::exception &e)
687 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
692 const struct afb_verb_v2 windowmanager_verbs[] = {
693 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
694 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
695 {"activatewindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
696 {"deactivatewindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
697 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
698 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
699 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
700 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
701 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
702 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
703 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
704 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
705 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
706 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
709 extern "C" const struct afb_binding_v2 afbBindingV2 = {
710 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};