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"
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;
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->role.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);
207 void windowmanager_requestsurface(afb_req req) noexcept
209 std::lock_guard<std::mutex> guard(binding_m);
213 if (g_afb_instance == nullptr)
215 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
221 const char *a_drawing_name = afb_req_value(req, "drawing_name");
224 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
228 /* Create Security Context */
229 bool isFirstReq = checkFirstReq(req);
232 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
233 HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
234 if (ctxt->name != std::string(a_drawing_name))
236 afb_req_fail_f(req, "failed", "Don't request with other name: %s for now", a_drawing_name);
237 HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
242 auto ret = g_afb_instance->app.api_request_surface(a_drawing_name);
246 WMClientCtxt *ctxt = new WMClientCtxt(afb_req_get_application_id(req), a_drawing_name);
247 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
248 afb_req_session_set_LOA(req, 1);
249 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
253 HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
258 afb_req_fail(req, "failed", ret.unwrap_err());
262 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
264 catch (std::exception &e)
266 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
271 void windowmanager_requestsurfacexdg(afb_req req) noexcept
273 std::lock_guard<std::mutex> guard(binding_m);
277 if (g_afb_instance == nullptr)
279 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
285 json_object *jreq = afb_req_json(req);
287 json_object *j_drawing_name = nullptr;
288 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
290 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
293 char const *a_drawing_name = json_object_get_string(j_drawing_name);
295 json_object *j_ivi_id = nullptr;
296 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
298 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
301 char const *a_ivi_id = json_object_get_string(j_ivi_id);
303 auto ret = g_afb_instance->app.api_request_surface(a_drawing_name, a_ivi_id);
306 afb_req_fail(req, "failed", ret);
310 afb_req_success(req, NULL, "success");
312 catch (std::exception &e)
314 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
319 void windowmanager_activatesurface(afb_req req) noexcept
321 std::lock_guard<std::mutex> guard(binding_m);
325 if (g_afb_instance == nullptr)
327 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
333 const char *a_drawing_name = afb_req_value(req, "drawing_name");
336 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
340 const char *a_drawing_area = afb_req_value(req, "drawing_area");
343 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
347 g_afb_instance->app.api_activate_surface(a_drawing_name, a_drawing_area,
348 [&req](const char *errmsg) {
349 if (errmsg != nullptr)
351 HMI_ERROR("wm", errmsg);
352 afb_req_fail(req, "failed", errmsg);
355 afb_req_success(req, NULL, "success");
358 catch (std::exception &e)
360 HMI_WARNING("wm", "failed", "Uncaught exception while calling activatesurface: %s", e.what());
365 void windowmanager_deactivatesurface(afb_req req) noexcept
367 std::lock_guard<std::mutex> guard(binding_m);
371 if (g_afb_instance == nullptr)
373 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
379 const char *a_drawing_name = afb_req_value(req, "drawing_name");
382 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
386 g_afb_instance->app.api_deactivate_surface(a_drawing_name,
387 [&req](const char *errmsg) {
388 if (errmsg != nullptr)
390 HMI_ERROR("wm", errmsg);
391 afb_req_fail(req, "failed", errmsg);
394 afb_req_success(req, NULL, "success");
397 catch (std::exception &e)
399 HMI_WARNING("wm", "Uncaught exception while calling deactivatesurface: %s", e.what());
404 void windowmanager_enddraw(afb_req req) noexcept
406 std::lock_guard<std::mutex> guard(binding_m);
410 if (g_afb_instance == nullptr)
412 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
418 const char *a_drawing_name = afb_req_value(req, "drawing_name");
421 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
424 afb_req_success(req, NULL, "success");
426 g_afb_instance->app.api_enddraw(a_drawing_name);
428 catch (std::exception &e)
430 HMI_WARNING("wm", "failed", "Uncaught exception while calling enddraw: %s", e.what());
435 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
437 std::lock_guard<std::mutex> guard(binding_m);
441 if (g_afb_instance == nullptr)
443 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
449 auto ret = g_afb_instance->app.api_get_display_info();
452 afb_req_fail(req, "failed", ret.unwrap_err());
456 afb_req_success(req, ret.unwrap(), "success");
458 catch (std::exception &e)
460 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
465 void windowmanager_getareainfo_thunk(afb_req req) noexcept
467 std::lock_guard<std::mutex> guard(binding_m);
471 if (g_afb_instance == nullptr)
473 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
479 json_object *jreq = afb_req_json(req);
481 json_object *j_drawing_name = nullptr;
482 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
484 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
487 char const *a_drawing_name = json_object_get_string(j_drawing_name);
489 auto ret = g_afb_instance->app.api_get_area_info(a_drawing_name);
492 afb_req_fail(req, "failed", ret.unwrap_err());
496 afb_req_success(req, ret.unwrap(), "success");
498 catch (std::exception &e)
500 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
505 void windowmanager_wm_subscribe(afb_req req) noexcept
507 std::lock_guard<std::mutex> guard(binding_m);
511 if (g_afb_instance == nullptr)
513 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
519 json_object *jreq = afb_req_json(req);
520 json_object *j = nullptr;
521 if (!json_object_object_get_ex(jreq, "event", &j))
523 afb_req_fail(req, "failed", "Need char const* argument event");
526 int event_type = json_object_get_int(j);
527 const char *event_name = g_afb_instance->app.kListEventName[event_type];
528 struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
529 int ret = afb_req_subscribe(req, event);
532 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
535 afb_req_success(req, NULL, "success");
537 catch (std::exception &e)
539 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
544 void windowmanager_list_drawing_names(afb_req req) noexcept
546 std::lock_guard<std::mutex> guard(binding_m);
550 if (g_afb_instance == nullptr)
552 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
559 nlohmann::json j = g_afb_instance->app.id_alloc.name2id;
560 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
563 afb_req_fail(req, "failed", ret.unwrap_err());
567 afb_req_success(req, ret.unwrap(), "success");
569 catch (std::exception &e)
571 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
576 void windowmanager_ping(afb_req req) noexcept
578 std::lock_guard<std::mutex> guard(binding_m);
582 if (g_afb_instance == nullptr)
584 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
591 g_afb_instance->app.api_ping();
593 afb_req_success(req, NULL, "success");
595 catch (std::exception &e)
597 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
602 void windowmanager_debug_status(afb_req req) noexcept
604 std::lock_guard<std::mutex> guard(binding_m);
608 if (g_afb_instance == nullptr)
610 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
617 json_object *jr = json_object_new_object();
618 json_object_object_add(jr, "surfaces",
619 to_json(g_afb_instance->app.controller->sprops));
620 json_object_object_add(jr, "layers", to_json(g_afb_instance->app.controller->lprops));
622 afb_req_success(req, jr, "success");
624 catch (std::exception &e)
626 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
631 void windowmanager_debug_layers(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?");
645 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->app.layers.to_json().dump().c_str()));
647 afb_req_success(req, ret, "success");
649 catch (std::exception &e)
651 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
656 void windowmanager_debug_surfaces(afb_req req) noexcept
658 std::lock_guard<std::mutex> guard(binding_m);
662 if (g_afb_instance == nullptr)
664 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
671 auto ret = wm::Ok(to_json(g_afb_instance->app.controller->sprops));
674 afb_req_fail(req, "failed", ret.unwrap_err());
678 afb_req_success(req, ret.unwrap(), "success");
680 catch (std::exception &e)
682 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
687 void windowmanager_debug_terminate(afb_req req) noexcept
689 std::lock_guard<std::mutex> guard(binding_m);
693 if (g_afb_instance == nullptr)
695 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
702 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
704 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
705 // doesn't play well with perf
708 afb_req_success(req, NULL, "success");
710 catch (std::exception &e)
712 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
717 const struct afb_verb_v2 windowmanager_verbs[] = {
718 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
719 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
720 {"activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_NONE},
721 {"deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_NONE},
722 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
723 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
724 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
725 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
726 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
727 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
728 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
729 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
730 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
731 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
734 extern "C" const struct afb_binding_v2 afbBindingV2 = {
735 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};