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>
34 std::unique_ptr<wl::display> display;
37 afb_instance() : display{new wl::display}, app{this->display.get()} {}
42 struct afb_instance *g_afb_instance;
45 int afb_instance::init() {
46 return this->app.init();
49 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
53 if ((events & EPOLLHUP) != 0) {
54 HMI_ERROR("wm", "The compositor hung up, dying now.");
55 delete g_afb_instance;
56 g_afb_instance = nullptr;
60 if ((events & EPOLLIN) != 0u) {
62 STN(display_read_events);
63 g_afb_instance->app.display->read_events();
64 g_afb_instance->app.set_pending_events();
67 // We want do dispatch pending wayland events from within
69 STN(winman_ping_api_call);
70 afb_service_call("windowmanager", "ping", json_object_new_object(),
71 [](void *c, int st, json_object *j) {
72 STN(winman_ping_api_call_return);
81 sd_event_source_unref(evs);
82 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr) {
92 HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
94 if (g_afb_instance != nullptr) {
95 HMI_ERROR("wm", "Wayland context already initialized?");
99 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
100 HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
105 // wait until wayland compositor starts up.
107 g_afb_instance = new afb_instance;
108 while (!g_afb_instance->display->ok()) {
111 HMI_ERROR("wm", "Could not connect to compositor");
114 HMI_ERROR("wm", "Wait to start weston ...");
116 delete g_afb_instance;
117 g_afb_instance = new afb_instance;
121 if (g_afb_instance->init() == -1) {
122 HMI_ERROR("wm", "Could not connect to compositor");
127 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
128 g_afb_instance->display->get_fd(), EPOLLIN,
129 display_event_callback, g_afb_instance);
131 HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
136 atexit([] { delete g_afb_instance; });
141 delete g_afb_instance;
142 g_afb_instance = nullptr;
146 int binding_init() noexcept {
148 return binding_init_();
149 } catch (std::exception &e) {
150 HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
155 void windowmanager_requestsurface(afb_req req) noexcept {
156 std::lock_guard<std::mutex> guard(binding_m);
160 if (g_afb_instance == nullptr) {
161 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
167 const char* a_drawing_name = afb_req_value(req, "drawing_name");
169 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
173 auto ret = g_afb_instance->app.api.requestsurface(a_drawing_name);
175 afb_req_fail(req, "failed", ret.unwrap_err());
179 afb_req_success(req, ret.unwrap(), "success");
181 } catch (std::exception &e) {
182 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
188 void windowmanager_requestsurfacexdg(afb_req req) noexcept {
189 std::lock_guard<std::mutex> guard(binding_m);
193 if (g_afb_instance == nullptr) {
194 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
200 json_object *jreq = afb_req_json(req);
202 json_object *j_drawing_name = nullptr;
203 if (! json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name)) {
204 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
207 char const* a_drawing_name = json_object_get_string(j_drawing_name);
209 json_object *j_ivi_id = nullptr;
210 if (! json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id)) {
211 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
214 char const* a_ivi_id = json_object_get_string(j_ivi_id);
216 auto ret = g_afb_instance->app.api.requestsurfacexdg(a_drawing_name, a_ivi_id);
218 afb_req_fail(req, "failed", ret.unwrap_err());
222 afb_req_success(req, ret.unwrap(), "success");
224 } catch (std::exception &e) {
225 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
231 void windowmanager_activatesurface(afb_req req) noexcept {
232 std::lock_guard<std::mutex> guard(binding_m);
236 if (g_afb_instance == nullptr) {
237 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
243 const char* a_drawing_name = afb_req_value(req, "drawing_name");
245 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
249 const char* a_drawing_area = afb_req_value(req, "drawing_area");
251 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
255 auto ret = g_afb_instance->app.api.activatesurface(a_drawing_name, a_drawing_area);
257 afb_req_fail(req, "failed", ret.unwrap_err());
261 afb_req_success(req, ret.unwrap(), "success");
263 } catch (std::exception &e) {
264 afb_req_fail_f(req, "failed", "Uncaught exception while calling activatesurface: %s", e.what());
270 void windowmanager_deactivatesurface(afb_req req) noexcept {
271 std::lock_guard<std::mutex> guard(binding_m);
275 if (g_afb_instance == nullptr) {
276 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
282 const char* a_drawing_name = afb_req_value(req, "drawing_name");
284 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
288 auto ret = g_afb_instance->app.api.deactivatesurface(a_drawing_name);
290 afb_req_fail(req, "failed", ret.unwrap_err());
294 afb_req_success(req, ret.unwrap(), "success");
296 } catch (std::exception &e) {
297 afb_req_fail_f(req, "failed", "Uncaught exception while calling deactivatesurface: %s", e.what());
303 void windowmanager_enddraw(afb_req req) noexcept {
304 std::lock_guard<std::mutex> guard(binding_m);
308 if (g_afb_instance == nullptr) {
309 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
315 const char* a_drawing_name = afb_req_value(req, "drawing_name");
317 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
321 auto ret = g_afb_instance->app.api.enddraw(a_drawing_name);
323 afb_req_fail(req, "failed", ret.unwrap_err());
327 afb_req_success(req, ret.unwrap(), "success");
329 } catch (std::exception &e) {
330 afb_req_fail_f(req, "failed", "Uncaught exception while calling enddraw: %s", e.what());
336 void windowmanager_wm_subscribe(afb_req req) noexcept {
337 std::lock_guard<std::mutex> guard(binding_m);
341 if (g_afb_instance == nullptr) {
342 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
348 json_object *jreq = afb_req_json(req);
349 json_object *j = nullptr;
350 if (! json_object_object_get_ex(jreq, "event", &j)) {
351 afb_req_fail(req, "failed", "Need char const* argument event");
354 int event_type = json_object_get_int(j);
355 const char *event_name = g_afb_instance->app.kListEventName[event_type];
356 struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
357 int ret = afb_req_subscribe(req, event);
359 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
362 afb_req_success(req, NULL, "success");
364 } catch (std::exception &e) {
365 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
371 void windowmanager_list_drawing_names(afb_req req) noexcept {
372 std::lock_guard<std::mutex> guard(binding_m);
376 if (g_afb_instance == nullptr) {
377 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
383 auto ret = g_afb_instance->app.api.list_drawing_names();
385 afb_req_fail(req, "failed", ret.unwrap_err());
389 afb_req_success(req, ret.unwrap(), "success");
391 } catch (std::exception &e) {
392 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
398 void windowmanager_ping(afb_req req) noexcept {
399 std::lock_guard<std::mutex> guard(binding_m);
403 if (g_afb_instance == nullptr) {
404 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
410 auto ret = g_afb_instance->app.api.ping();
412 afb_req_fail(req, "failed", ret.unwrap_err());
416 afb_req_success(req, ret.unwrap(), "success");
418 } catch (std::exception &e) {
419 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
425 void windowmanager_debug_status(afb_req req) noexcept {
426 std::lock_guard<std::mutex> guard(binding_m);
430 if (g_afb_instance == nullptr) {
431 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
437 auto ret = g_afb_instance->app.api.debug_status();
439 afb_req_fail(req, "failed", ret.unwrap_err());
443 afb_req_success(req, ret.unwrap(), "success");
445 } catch (std::exception &e) {
446 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
452 void windowmanager_debug_layers(afb_req req) noexcept {
453 std::lock_guard<std::mutex> guard(binding_m);
457 if (g_afb_instance == nullptr) {
458 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
464 auto ret = g_afb_instance->app.api.debug_layers();
466 afb_req_fail(req, "failed", ret.unwrap_err());
470 afb_req_success(req, ret.unwrap(), "success");
472 } catch (std::exception &e) {
473 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
479 void windowmanager_debug_surfaces(afb_req req) noexcept {
480 std::lock_guard<std::mutex> guard(binding_m);
484 if (g_afb_instance == nullptr) {
485 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
491 auto ret = g_afb_instance->app.api.debug_surfaces();
493 afb_req_fail(req, "failed", ret.unwrap_err());
497 afb_req_success(req, ret.unwrap(), "success");
499 } catch (std::exception &e) {
500 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
506 void windowmanager_debug_terminate(afb_req req) noexcept {
507 std::lock_guard<std::mutex> guard(binding_m);
511 if (g_afb_instance == nullptr) {
512 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
518 auto ret = g_afb_instance->app.api.debug_terminate();
520 afb_req_fail(req, "failed", ret.unwrap_err());
524 afb_req_success(req, ret.unwrap(), "success");
526 } catch (std::exception &e) {
527 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
533 const struct afb_verb_v2 windowmanager_verbs[] = {
534 { "requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE },
535 { "requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE },
536 { "activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_NONE },
537 { "deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_NONE },
538 { "enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE },
539 { "wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE },
540 { "list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE },
541 { "ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE },
542 { "debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE },
543 { "debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE },
544 { "debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE },
545 { "debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE },
550 void binding_api::send_event(char const *evname, char const *label) {
551 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
553 json_object *j = json_object_new_object();
554 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
556 int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
558 HMI_DEBUG("wm", "afb_event_push failed: %m");
562 void binding_api::send_event(char const *evname, char const *label, char const *area) {
563 HMI_DEBUG("wm", "%s: %s(%s, %s)", __func__, evname, label, area);
565 json_object *j = json_object_new_object();
566 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
567 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
569 int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
571 HMI_DEBUG("wm", "afb_event_push failed: %m");
576 extern "C" const struct afb_binding_v2 afbBindingV2 = {
577 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};