2 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
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.
18 #include "json_helper.hpp"
20 #include "wayland.hpp"
28 #include <afb/afb-binding.h>
29 #include <systemd/sd-event.h>
35 std::unique_ptr<wl::display> display;
38 afb_instance() : display{new wl::display}, app{this->display.get()} {}
43 struct afb_instance *g_afb_instance;
45 int afb_instance::init() {
46 if (!this->display->ok()) {
50 return this->app.init();
53 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
57 if ((events & EPOLLHUP) != 0) {
58 logerror("The compositor hung up, dying now.");
59 delete g_afb_instance;
60 g_afb_instance = nullptr;
64 if ((events & EPOLLIN) != 0u) {
66 STN(display_read_events);
67 g_afb_instance->app.display->read_events();
68 g_afb_instance->app.set_pending_events();
71 // We want do dispatch pending wayland events from within
73 STN(winman_ping_api_call);
74 afb_service_call("winman", "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) {
93 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
94 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
95 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
96 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
99 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
101 if (g_afb_instance != nullptr) {
102 logerror("Wayland context already initialized?");
106 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
107 logerror("Environment variable XDG_RUNTIME_DIR not set");
111 g_afb_instance = new afb_instance;
112 if (g_afb_instance->init() == -1) {
113 logerror("Could not connect to compositor");
118 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
119 g_afb_instance->display->get_fd(), EPOLLIN,
120 display_event_callback, g_afb_instance);
122 logerror("Could not initialize afb_instance event handler: %d", -ret);
127 atexit([] { delete g_afb_instance; });
132 delete g_afb_instance;
133 g_afb_instance = nullptr;
137 int binding_init() noexcept {
139 return binding_init_();
140 } catch (std::exception &e) {
141 logerror("Uncaught exception in binding_init(): %s", e.what());
148 #include "afb_binding_glue.inl"
150 // XXX implement send_event right here...
152 void binding_api::send_event(char const *evname, char const *label) {
153 logdebug("%s: %s(%s)", __func__, evname, label);
154 int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
156 logdebug("afb_event_broadcast failed: %m");
161 extern "C" const struct afb_binding_v2 afbBindingV2 = {
162 "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 0};