App/API: fix event emission methods
[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    return -1;
76 }
77
78 //  _     _           _ _                 _       _ _    ____
79 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
80 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
81 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
82 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
83 //                              |___/_____|             \_\/_/
84 int binding_init_() {
85    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
86
87    if (g_afb_instance != nullptr) {
88       logerror("Wayland context already initialized?");
89       return 0;
90    }
91
92    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
93       logerror("Environment variable XDG_RUNTIME_DIR not set");
94       goto error;
95    }
96
97    g_afb_instance = new afb_instance;
98    if (g_afb_instance->init() == -1) {
99       logerror("Could not connect to compositor");
100       goto error;
101    }
102
103    {
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);
107       if (ret < 0) {
108          logerror("Could not initialize afb_instance event handler: %d", -ret);
109          goto error;
110       }
111    }
112
113    atexit([] { delete g_afb_instance; });
114
115    return 0;
116
117 error:
118    delete g_afb_instance;
119    g_afb_instance = nullptr;
120    return -1;
121 }
122
123 int binding_init() noexcept {
124    try {
125       return binding_init_();
126    } catch (std::exception &e) {
127       logerror("Uncaught exception in binding_init(): %s", e.what());
128    }
129    return -1;
130 }
131
132 }  // namespace
133
134 #include "afb_binding_glue.inl"
135
136 // XXX implement send_event right here...
137 namespace wm {
138 void binding_api::send_event(char const *evname, char const *label) {
139    logdebug("%s: %s(%s)", __func__, evname, label);
140    int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
141    if (ret != 0) {
142       logdebug("afb_event_broadcast failed: %m");
143    }
144 }
145 }
146
147 extern "C" const struct afb_binding_v2 afbBindingV2 = {
148    "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 1};