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>
33 //std::mutex binding_m;
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 if (!this->display->ok()) {
51 return this->app.init();
54 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
57 // std::lock_guard<std::mutex> guard(binding_m);
59 if ((events & EPOLLHUP) != 0) {
60 logerror("The compositor hung up, dying now.");
61 delete g_afb_instance;
62 g_afb_instance = nullptr;
66 if ((events & EPOLLIN) != 0u) {
68 STN(display_read_events);
69 g_afb_instance->app.display->read_events();
70 g_afb_instance->app.pending_events.store(true, std::memory_order_release);
73 STN(winman_ping_api_call);
74 afb_service_call("winman", "ping", json_object_new_object(), [](void *c, int st, json_object* j) {
75 STN(winman_ping_api_call_return);
83 sd_event_source_unref(evs);
84 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
90 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
91 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
92 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
93 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
96 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
98 if (g_afb_instance != nullptr) {
99 logerror("Wayland context already initialized?");
103 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
104 logerror("Environment variable XDG_RUNTIME_DIR not set");
108 g_afb_instance = new afb_instance;
109 if (g_afb_instance->init() == -1) {
110 logerror("Could not connect to compositor");
115 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
116 g_afb_instance->display->get_fd(), EPOLLIN,
117 display_event_callback, g_afb_instance);
119 logerror("Could not initialize afb_instance event handler: %d", -ret);
124 atexit([] { delete g_afb_instance; });
129 delete g_afb_instance;
130 g_afb_instance = nullptr;
134 int binding_init() noexcept {
136 return binding_init_();
137 } catch (std::exception &e) {
138 logerror("Uncaught exception in binding_init(): %s", e.what());
145 #include "afb_binding_glue.inl"
147 // XXX implement send_event right here...
149 void binding_api::send_event(char const *evname, char const *label) {
150 logdebug("%s: %s(%s)", __func__, evname, label);
151 int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
153 logdebug("afb_event_broadcast failed: %m");
158 extern "C" const struct afb_binding_v2 afbBindingV2 = {
159 "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 1};