binding: add ping() API call
[staging/windowmanager.git] / src / main.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "app.hpp"
18 #include "json_helper.hpp"
19 #include "util.hpp"
20 #include "wayland.hpp"
21
22 #include <algorithm>
23 #include <mutex>
24
25 #include <json.h>
26
27 extern "C" {
28 #include <afb/afb-binding.h>
29 #include <systemd/sd-event.h>
30 }
31
32 namespace {
33 std::mutex binding_m;
34
35 struct afb_instance {
36    std::unique_ptr<wl::display> display;
37    wm::App app;
38
39    afb_instance() : display{new wl::display}, app{this->display.get()} {}
40
41    int init();
42 };
43
44 struct afb_instance *g_afb_instance;
45
46 int afb_instance::init() {
47    if (!this->display->ok()) {
48       return -1;
49    }
50
51    return this->app.init();
52 }
53
54 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
55                            void * /*data*/) {
56    std::lock_guard<std::mutex> guard(binding_m);
57
58    if ((events & EPOLLHUP) != 0) {
59       logerror("The compositor hung up, dying now.");
60       delete g_afb_instance;
61       g_afb_instance = nullptr;
62       goto error;
63    }
64
65    if ((events & EPOLLIN) != 0u) {
66       if (g_afb_instance->app.dispatch_events() == -1) {
67          goto error;
68       }
69    }
70
71    return 0;
72
73 error:
74    sd_event_source_unref(evs);
75    if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
76       exit(1);
77    return -1;
78 }
79
80 //  _     _           _ _                 _       _ _    ____
81 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
82 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
83 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
84 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
85 //                              |___/_____|             \_\/_/
86 int binding_init_() {
87    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
88
89    if (g_afb_instance != nullptr) {
90       logerror("Wayland context already initialized?");
91       return 0;
92    }
93
94    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
95       logerror("Environment variable XDG_RUNTIME_DIR not set");
96       goto error;
97    }
98
99    g_afb_instance = new afb_instance;
100    if (g_afb_instance->init() == -1) {
101       logerror("Could not connect to compositor");
102       goto error;
103    }
104
105    {
106       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
107                                 g_afb_instance->display->get_fd(), EPOLLIN,
108                                 display_event_callback, g_afb_instance);
109       if (ret < 0) {
110          logerror("Could not initialize afb_instance event handler: %d", -ret);
111          goto error;
112       }
113    }
114
115    atexit([] { delete g_afb_instance; });
116
117    return 0;
118
119 error:
120    delete g_afb_instance;
121    g_afb_instance = nullptr;
122    return -1;
123 }
124
125 int binding_init() noexcept {
126    try {
127       return binding_init_();
128    } catch (std::exception &e) {
129       logerror("Uncaught exception in binding_init(): %s", e.what());
130    }
131    return -1;
132 }
133
134 }  // namespace
135
136 #include "afb_binding_glue.inl"
137
138 // XXX implement send_event right here...
139 namespace wm {
140 void binding_api::send_event(char const *evname, char const *label) {
141    logdebug("%s: %s(%s)", __func__, evname, label);
142    int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
143    if (ret != 0) {
144       logdebug("afb_event_broadcast failed: %m");
145    }
146 }
147 }
148
149 extern "C" const struct afb_binding_v2 afbBindingV2 = {
150    "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 1};