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.
19 #include "json_helper.hpp"
21 #include "wayland.hpp"
29 #include <afb/afb-binding.h>
30 #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 return this->app.init();
50 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
54 if ((events & EPOLLHUP) != 0) {
55 logerror("The compositor hung up, dying now.");
56 delete g_afb_instance;
57 g_afb_instance = nullptr;
61 if ((events & EPOLLIN) != 0u) {
63 STN(display_read_events);
64 g_afb_instance->app.display->read_events();
65 g_afb_instance->app.set_pending_events();
68 // We want do dispatch pending wayland events from within
70 STN(winman_ping_api_call);
71 afb_service_call("windowmanager", "ping", json_object_new_object(),
72 [](void *c, int st, json_object *j) {
73 STN(winman_ping_api_call_return);
82 sd_event_source_unref(evs);
83 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");
109 // wait until wayland compositor starts up.
111 g_afb_instance = new afb_instance;
112 while (!g_afb_instance->display->ok()) {
115 logerror("Could not connect to compositor");
118 logerror("Wait to start weston ...");
120 delete g_afb_instance;
121 g_afb_instance = new afb_instance;
125 if (g_afb_instance->init() == -1) {
126 logerror("Could not connect to compositor");
131 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
132 g_afb_instance->display->get_fd(), EPOLLIN,
133 display_event_callback, g_afb_instance);
135 logerror("Could not initialize afb_instance event handler: %d", -ret);
140 atexit([] { delete g_afb_instance; });
145 delete g_afb_instance;
146 g_afb_instance = nullptr;
150 int binding_init() noexcept {
152 return binding_init_();
153 } catch (std::exception &e) {
154 logerror("Uncaught exception in binding_init(): %s", e.what());
161 #include "afb_binding_glue.inl"
164 void binding_api::send_event(char const *evname, char const *label) {
165 logdebug("%s: %s(%s)", __func__, evname, label);
167 json_object *j = json_object_new_object();
168 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
170 int ret = afb_daemon_broadcast_event(evname, j);
172 logdebug("afb_event_broadcast failed: %m");
176 void binding_api::send_event(char const *evname, char const *label, char const *area) {
177 logdebug("%s: %s(%s, %s)", __func__, evname, label, area);
179 json_object *j = json_object_new_object();
180 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
181 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
183 int ret = afb_daemon_broadcast_event(evname, j);
185 logdebug("afb_event_broadcast failed: %m");
190 extern "C" const struct afb_binding_v2 afbBindingV2 = {
191 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};