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>
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,
58 if ((events & EPOLLHUP) != 0) {
59 logerror("The compositor hung up, dying now.");
60 delete g_afb_instance;
61 g_afb_instance = nullptr;
65 if ((events & EPOLLIN) != 0u) {
67 STN(display_read_events);
68 g_afb_instance->app.display->read_events();
69 g_afb_instance->app.pending_events.store(true, std::memory_order_release);
72 STN(winman_ping_api_call);
73 afb_service_call("winman", "ping", json_object_new_object(), [](void *c, int st, json_object* j) {
74 STN(winman_ping_api_call_return);
82 sd_event_source_unref(evs);
83 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
89 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
90 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
91 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
92 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
95 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
97 if (g_afb_instance != nullptr) {
98 logerror("Wayland context already initialized?");
102 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
103 logerror("Environment variable XDG_RUNTIME_DIR not set");
107 g_afb_instance = new afb_instance;
108 if (g_afb_instance->init() == -1) {
109 logerror("Could not connect to compositor");
114 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
115 g_afb_instance->display->get_fd(), EPOLLIN,
116 display_event_callback, g_afb_instance);
118 logerror("Could not initialize afb_instance event handler: %d", -ret);
123 atexit([] { delete g_afb_instance; });
128 delete g_afb_instance;
129 g_afb_instance = nullptr;
133 int binding_init() noexcept {
135 return binding_init_();
136 } catch (std::exception &e) {
137 logerror("Uncaught exception in binding_init(): %s", e.what());
144 #include "afb_binding_glue.inl"
146 // XXX implement send_event right here...
148 void binding_api::send_event(char const *evname, char const *label) {
149 logdebug("%s: %s(%s)", __func__, evname, label);
150 int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
152 logdebug("afb_event_broadcast failed: %m");
157 extern "C" const struct afb_binding_v2 afbBindingV2 = {
158 "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 0};