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