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.
24 #include "json_helper.hpp"
26 #include "wayland_ivi_wm.hpp"
30 #include <afb/afb-binding.h>
31 #include <systemd/sd-event.h>
34 typedef struct wmClientCtxt
37 wmClientCtxt(const char *appName)
45 std::unique_ptr<wl::display> display;
48 afb_instance() : display{new wl::display}, app{this->display.get()} {}
53 struct afb_instance *g_afb_instance;
56 int afb_instance::init()
58 return this->app.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->app.display->read_events();
79 g_afb_instance->app.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->app.id_alloc.lookup(ctxt->name.c_str());
198 g_afb_instance->app.id_alloc.remove_id(sid);
199 g_afb_instance->app.layers.remove_surface(sid);
200 g_afb_instance->app.controller->sprops.erase(sid);
201 g_afb_instance->app.controller->surfaces.erase(sid);
202 HMI_DEBUG("wm", "delete surfaceID %d", sid);
204 g_afb_instance->app.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->app.api_request_surface(
244 afb_req_get_application_id(req), a_drawing_name);
248 wmClientCtxt *ctxt = new wmClientCtxt(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->app.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_activatesurface(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->app.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());
370 void windowmanager_deactivatesurface(afb_req req) noexcept
372 std::lock_guard<std::mutex> guard(binding_m);
376 if (g_afb_instance == nullptr)
378 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
384 const char *a_drawing_name = afb_req_value(req, "drawing_name");
387 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
391 g_afb_instance->app.api_deactivate_surface(
392 afb_req_get_application_id(req), a_drawing_name,
393 [&req](const char *errmsg) {
394 if (errmsg != nullptr)
396 HMI_ERROR("wm", errmsg);
397 afb_req_fail(req, "failed", errmsg);
400 afb_req_success(req, NULL, "success");
403 catch (std::exception &e)
405 HMI_WARNING("wm", "Uncaught exception while calling deactivatesurface: %s", e.what());
410 void windowmanager_enddraw(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 const char *a_drawing_name = afb_req_value(req, "drawing_name");
427 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
430 afb_req_success(req, NULL, "success");
432 g_afb_instance->app.api_enddraw(
433 afb_req_get_application_id(req), a_drawing_name);
435 catch (std::exception &e)
437 HMI_WARNING("wm", "failed", "Uncaught exception while calling enddraw: %s", e.what());
442 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
444 std::lock_guard<std::mutex> guard(binding_m);
448 if (g_afb_instance == nullptr)
450 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
456 auto ret = g_afb_instance->app.api_get_display_info();
459 afb_req_fail(req, "failed", ret.unwrap_err());
463 afb_req_success(req, ret.unwrap(), "success");
465 catch (std::exception &e)
467 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
472 void windowmanager_getareainfo_thunk(afb_req req) noexcept
474 std::lock_guard<std::mutex> guard(binding_m);
478 if (g_afb_instance == nullptr)
480 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
486 json_object *jreq = afb_req_json(req);
488 json_object *j_drawing_name = nullptr;
489 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
491 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
494 char const *a_drawing_name = json_object_get_string(j_drawing_name);
496 auto ret = g_afb_instance->app.api_get_area_info(a_drawing_name);
499 afb_req_fail(req, "failed", ret.unwrap_err());
503 afb_req_success(req, ret.unwrap(), "success");
505 catch (std::exception &e)
507 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
512 void windowmanager_wm_subscribe(afb_req req) noexcept
514 std::lock_guard<std::mutex> guard(binding_m);
518 if (g_afb_instance == nullptr)
520 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
526 json_object *jreq = afb_req_json(req);
527 json_object *j = nullptr;
528 if (!json_object_object_get_ex(jreq, "event", &j))
530 afb_req_fail(req, "failed", "Need char const* argument event");
533 int event_type = json_object_get_int(j);
534 const char *event_name = g_afb_instance->app.kListEventName[event_type];
535 struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
536 int ret = afb_req_subscribe(req, event);
539 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
542 afb_req_success(req, NULL, "success");
544 catch (std::exception &e)
546 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
551 void windowmanager_list_drawing_names(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 nlohmann::json j = g_afb_instance->app.id_alloc.name2id;
567 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
570 afb_req_fail(req, "failed", ret.unwrap_err());
574 afb_req_success(req, ret.unwrap(), "success");
576 catch (std::exception &e)
578 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
583 void windowmanager_ping(afb_req req) noexcept
585 std::lock_guard<std::mutex> guard(binding_m);
589 if (g_afb_instance == nullptr)
591 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
598 g_afb_instance->app.api_ping();
600 afb_req_success(req, NULL, "success");
602 catch (std::exception &e)
604 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
609 void windowmanager_debug_status(afb_req req) noexcept
611 std::lock_guard<std::mutex> guard(binding_m);
615 if (g_afb_instance == nullptr)
617 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
624 json_object *jr = json_object_new_object();
625 json_object_object_add(jr, "surfaces",
626 to_json(g_afb_instance->app.controller->sprops));
627 json_object_object_add(jr, "layers", to_json(g_afb_instance->app.controller->lprops));
629 afb_req_success(req, jr, "success");
631 catch (std::exception &e)
633 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
638 void windowmanager_debug_layers(afb_req req) noexcept
640 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?");
652 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->app.layers.to_json().dump().c_str()));
654 afb_req_success(req, ret, "success");
656 catch (std::exception &e)
658 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
663 void windowmanager_debug_surfaces(afb_req req) noexcept
665 std::lock_guard<std::mutex> guard(binding_m);
669 if (g_afb_instance == nullptr)
671 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
678 auto ret = wm::Ok(to_json(g_afb_instance->app.controller->sprops));
681 afb_req_fail(req, "failed", ret.unwrap_err());
685 afb_req_success(req, ret.unwrap(), "success");
687 catch (std::exception &e)
689 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
694 void windowmanager_debug_terminate(afb_req req) noexcept
696 std::lock_guard<std::mutex> guard(binding_m);
700 if (g_afb_instance == nullptr)
702 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
709 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
711 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
712 // doesn't play well with perf
715 afb_req_success(req, NULL, "success");
717 catch (std::exception &e)
719 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
724 const struct afb_verb_v2 windowmanager_verbs[] = {
725 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
726 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
727 {"activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_LOA_1},
728 {"deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_LOA_1},
729 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_LOA_1},
730 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
731 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_LOA_1},
732 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
733 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
734 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
735 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
736 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
737 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
738 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
741 extern "C" const struct afb_binding_v2 afbBindingV2 = {
742 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};