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.
19 #include "json_helper.hpp"
21 #include "wayland.hpp"
29 #include <afb/afb-binding.h>
30 #include <systemd/sd-event.h>
36 std::unique_ptr<wl::display> display;
39 afb_instance() : display{new wl::display}, app{this->display.get()} {}
44 struct afb_instance *g_afb_instance;
46 int afb_instance::init() {
47 return this->app.init();
50 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
54 if ((events & EPOLLHUP) != 0) {
55 HMI_ERROR("wm", "The compositor hung up, dying now.");
56 delete g_afb_instance;
57 g_afb_instance = nullptr;
61 if ((events & EPOLLIN) != 0u) {
63 STN(display_read_events);
64 g_afb_instance->app.display->read_events();
65 g_afb_instance->app.set_pending_events();
68 // We want do dispatch pending wayland events from within
70 STN(winman_ping_api_call);
71 afb_service_call("windowmanager", "ping", json_object_new_object(),
72 [](void *c, int st, json_object *j) {
73 STN(winman_ping_api_call_return);
82 sd_event_source_unref(evs);
83 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr) {
93 HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
95 if (g_afb_instance != nullptr) {
96 HMI_ERROR("wm", "Wayland context already initialized?");
100 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
101 HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
106 // wait until wayland compositor starts up.
108 g_afb_instance = new afb_instance;
109 while (!g_afb_instance->display->ok()) {
112 HMI_ERROR("wm", "Could not connect to compositor");
115 HMI_ERROR("wm", "Wait to start weston ...");
117 delete g_afb_instance;
118 g_afb_instance = new afb_instance;
122 if (g_afb_instance->init() == -1) {
123 HMI_ERROR("wm", "Could not connect to compositor");
128 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
129 g_afb_instance->display->get_fd(), EPOLLIN,
130 display_event_callback, g_afb_instance);
132 HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
137 atexit([] { delete g_afb_instance; });
142 delete g_afb_instance;
143 g_afb_instance = nullptr;
147 int binding_init() noexcept {
149 return binding_init_();
150 } catch (std::exception &e) {
151 HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
158 #include "afb_binding_glue.inl"
161 void binding_api::send_event(char const *evname, char const *label) {
162 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
164 json_object *j = json_object_new_object();
165 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
167 int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
169 HMI_DEBUG("wm", "afb_event_push failed: %m");
173 void binding_api::send_event(char const *evname, char const *label, char const *area) {
174 HMI_DEBUG("wm", "%s: %s(%s, %s)", __func__, evname, label, area);
176 json_object *j = json_object_new_object();
177 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
178 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
180 int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
182 HMI_DEBUG("wm", "afb_event_push failed: %m");
187 extern "C" const struct afb_binding_v2 afbBindingV2 = {
188 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};