binding: ping() dispatches only pending events
[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
34 struct afb_instance {
35    std::unique_ptr<wl::display> display;
36    wm::App app;
37
38    afb_instance() : display{new wl::display}, app{this->display.get()} {}
39
40    int init();
41 };
42
43 struct afb_instance *g_afb_instance;
44
45 int afb_instance::init() {
46    if (!this->display->ok()) {
47       return -1;
48    }
49
50    return this->app.init();
51 }
52
53 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
54                            void * /*data*/) {
55    ST();
56
57    if ((events & EPOLLHUP) != 0) {
58       logerror("The compositor hung up, dying now.");
59       delete g_afb_instance;
60       g_afb_instance = nullptr;
61       goto error;
62    }
63
64    if ((events & EPOLLIN) != 0u) {
65       {
66          STN(display_read_events);
67          g_afb_instance->app.display->read_events();
68          g_afb_instance->app.pending_events.store(true, std::memory_order_release);
69       }
70       {
71          STN(winman_ping_api_call);
72          afb_service_call("winman", "ping", json_object_new_object(), [](void *c, int st,  json_object* j) {
73             STN(winman_ping_api_call_return);
74          }, nullptr);
75       }
76    }
77
78    return 0;
79
80 error:
81    sd_event_source_unref(evs);
82    if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
83       exit(1);
84    return -1;
85 }
86
87 //  _     _           _ _                 _       _ _    ____
88 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
89 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
90 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
91 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
92 //                              |___/_____|             \_\/_/
93 int binding_init_() {
94    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
95
96    if (g_afb_instance != nullptr) {
97       logerror("Wayland context already initialized?");
98       return 0;
99    }
100
101    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
102       logerror("Environment variable XDG_RUNTIME_DIR not set");
103       goto error;
104    }
105
106    g_afb_instance = new afb_instance;
107    if (g_afb_instance->init() == -1) {
108       logerror("Could not connect to compositor");
109       goto error;
110    }
111
112    {
113       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
114                                 g_afb_instance->display->get_fd(), EPOLLIN,
115                                 display_event_callback, g_afb_instance);
116       if (ret < 0) {
117          logerror("Could not initialize afb_instance event handler: %d", -ret);
118          goto error;
119       }
120    }
121
122    atexit([] { delete g_afb_instance; });
123
124    return 0;
125
126 error:
127    delete g_afb_instance;
128    g_afb_instance = nullptr;
129    return -1;
130 }
131
132 int binding_init() noexcept {
133    try {
134       return binding_init_();
135    } catch (std::exception &e) {
136       logerror("Uncaught exception in binding_init(): %s", e.what());
137    }
138    return -1;
139 }
140
141 }  // namespace
142
143 #include "afb_binding_glue.inl"
144
145 // XXX implement send_event right here...
146 namespace wm {
147 void binding_api::send_event(char const *evname, char const *label) {
148    logdebug("%s: %s(%s)", __func__, evname, label);
149    int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
150    if (ret != 0) {
151       logdebug("afb_event_broadcast failed: %m");
152    }
153 }
154 }
155
156 extern "C" const struct afb_binding_v2 afbBindingV2 = {
157    "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 0};