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)
92 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
93 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
94 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
95 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
98 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
100 if (g_afb_instance != nullptr) {
101 logerror("Wayland context already initialized?");
105 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
106 logerror("Environment variable XDG_RUNTIME_DIR not set");
110 g_afb_instance = new afb_instance;
111 if (g_afb_instance->init() == -1) {
112 logerror("Could not connect to compositor");
117 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
118 g_afb_instance->display->get_fd(), EPOLLIN,
119 display_event_callback, g_afb_instance);
121 logerror("Could not initialize afb_instance event handler: %d", -ret);
126 atexit([] { delete g_afb_instance; });
131 delete g_afb_instance;
132 g_afb_instance = nullptr;
136 int binding_init() noexcept {
138 return binding_init_();
139 } catch (std::exception &e) {
140 logerror("Uncaught exception in binding_init(): %s", e.what());
147 #include "afb_binding_glue.inl"
149 // XXX implement send_event right here...
151 void binding_api::send_event(char const *evname, char const *label) {
152 logdebug("%s: %s(%s)", __func__, evname, label);
153 int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
155 logdebug("afb_event_broadcast failed: %m");
160 extern "C" const struct afb_binding_v2 afbBindingV2 = {
161 "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 0};