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,
56 std::lock_guard<std::mutex> guard(binding_m);
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) {
66 if (g_afb_instance->app.dispatch_events() == -1) {
74 sd_event_source_unref(evs);
79 // | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \
80 // | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | |
81 // | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | |
82 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | |
85 lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
87 if (g_afb_instance != nullptr) {
88 logerror("Wayland context already initialized?");
92 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
93 logerror("Environment variable XDG_RUNTIME_DIR not set");
97 g_afb_instance = new afb_instance;
98 if (g_afb_instance->init() == -1) {
99 logerror("Could not connect to compositor");
104 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
105 g_afb_instance->display->get_fd(), EPOLLIN,
106 display_event_callback, g_afb_instance);
108 logerror("Could not initialize afb_instance event handler: %d", -ret);
113 atexit([] { delete g_afb_instance; });
118 delete g_afb_instance;
119 g_afb_instance = nullptr;
123 int binding_init() noexcept {
125 return binding_init_();
126 } catch (std::exception &e) {
127 logerror("Uncaught exception in binding_init(): %s", e.what());
134 #include "afb_binding_glue.inl"
136 // XXX implement send_event right here...
138 void binding_api::send_event(char const *evname, json_object *payload) {
139 logdebug("%s: %s(%s)", __func__, evname, json_object_to_json_string(payload));
140 afb_event ev = afb_daemon_make_event(evname);
141 int ret = afb_event_broadcast(ev, payload);
143 logdebug("afb_event_broadcast failed: %m");
148 extern "C" const struct afb_binding_v2 afbBindingV2 = {
149 "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 1};