Check for g_wayland in every API call.
[staging/windowmanager.git] / src / main.cpp
1 #include "util.hpp"
2 #include "wayland.hpp"
3
4 #include <algorithm>
5 #include <json.h>
6
7 #define AFB_BINDING_VERSION 2
8
9 extern "C" {
10 #include <afb/afb-binding.h>
11 #include <systemd/sd-event.h>
12 }
13
14 #include <json.hpp>
15
16 using json = nlohmann::json;
17
18 namespace {
19 struct wayland {
20    std::unique_ptr<wl::display> display;
21    std::unique_ptr<genivi::controller> controller;
22    std::vector<std::unique_ptr<wl::output>> outputs;
23    sd_event_source *event_source;
24
25    wayland()
26       : display(new wl::display),
27         controller(nullptr),
28         outputs(),
29         event_source{} {}
30 };
31
32 struct wayland *g_wayland;
33
34 //  _       _ _       _                         _    ____
35 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
36 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
37 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
38 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
39 //              |_____|       |___/                 \_\/_/
40 char const *init_layout() {
41    if (!g_wayland->controller) {
42       return "ivi_controller global not available";
43    }
44
45    if (g_wayland->outputs.empty()) {
46       return "no output was set up!";
47    }
48
49    auto &c = g_wayland->controller;
50
51    auto &o = g_wayland->outputs.front();
52    auto &s = c->screens.begin()->second;
53    auto &layers = c->layers;
54
55    // XXX: Write output dimensions to ivi controller...
56    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
57
58    // Clear scene
59    layers.clear();
60
61    // Clear screen
62    s->clear();
63
64    // Setup our dummy scene...
65    c->layer_create(100, 0, 0);   // bottom layer, anything else
66    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
67
68    auto &l100 = c->layers[100];
69    auto &l1k = c->layers[1000];
70
71    // Set layers fullscreen
72    l100->set_destination_rectangle(0, 0, o->width, o->height);
73    l1k->set_destination_rectangle(0, 0, o->width, o->height);
74    l100->set_visibility(1);
75    l1k->set_visibility(1);
76
77    // Add layers to screen
78    s->set_render_order({100, 1000});
79
80    c->commit_changes();
81
82    g_wayland->display->flush();
83
84    return nullptr;
85 }
86
87 //  _     _           _ _                 _       _ _    ____
88 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
89 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
90 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
91 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
92 //                              |___/_____|             \_\/_/
93 int binding_init_() {
94    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
95
96    if (g_wayland != nullptr) {
97       AFB_ERROR("Wayland context already initialized?");
98       return -1;
99    }
100
101    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
102       AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
103       return -1;
104    }
105
106    g_wayland = new wayland;
107    if (!g_wayland->display->ok()) {
108       AFB_ERROR("Could not connect to compositor");
109       return -1;
110    }
111
112    auto &d = g_wayland->display;
113    d->r.add_global_handler("wl_output", [](wl_registry *r, uint32_t name,
114                                            uint32_t v) {
115       g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
116    });
117
118    d->r.add_global_handler("ivi_controller", [](wl_registry *r, uint32_t name,
119                                                 uint32_t v) {
120       g_wayland->controller = std::make_unique<genivi::controller>(r, name, v);
121
122       // XXX: This protocol needs the output, so lets just add our mapping
123       // here...
124       g_wayland->controller->add_proxy_to_id_mapping(
125          g_wayland->outputs.back()->proxy.get(),
126          wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
127             g_wayland->outputs.back()->proxy.get())));
128    });
129
130    // First level objects
131    d->roundtrip();
132    // Second level objects
133    d->roundtrip();
134    // Third level objects
135    d->roundtrip();
136
137    if (char const *e = init_layout()) {
138       AFB_ERROR("Could not init layout: %s", e);
139       return -1;
140    }
141
142    sd_event *ev = afb_daemon_get_event_loop();
143    sd_event_add_io(
144       ev, &g_wayland->event_source, g_wayland->display->get_fd(),
145       EPOLLIN | EPOLLHUP,
146       [](sd_event_source *evs, int fd, uint32_t events, void *data) {
147          if ((events & EPOLLHUP) != 0) {
148             sd_event_source_unref(evs);
149             delete g_wayland;
150             g_wayland = nullptr;
151             return 1;
152          }
153
154          if (events & EPOLLIN) {
155             int ret = g_wayland->display->dispatch();
156             g_wayland->display->flush();
157
158             // execute pending tasks, that is layout changes etc.
159             g_wayland->controller->execute_pending();
160             g_wayland->display->roundtrip();
161             return ret == -1 ? -1 : 0;
162          }
163
164          return 0;
165       },
166       g_wayland);
167
168    atexit([] { delete g_wayland; });
169
170    return 0;
171 }
172
173 int binding_init() noexcept {
174    try {
175       return binding_init_();
176    } catch (std::exception &e) {
177       AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
178    }
179    return -1;
180 }
181
182 #define CHECK_WAYLAND()                                                    \
183    do {                                                                    \
184       if (g_wayland == nullptr) {                                          \
185          afb_req_fail(req, "failed",                                       \
186                       "Binding not initialized, did the compositor die?"); \
187          return;                                                           \
188       }                                                                    \
189    } while (0)
190
191 //      _      _                         _        _              ____
192 //   __| | ___| |__  _   _  __ _     ___| |_ __ _| |_ _   _ ___ / /\ \
193 //  / _` |/ _ \ '_ \| | | |/ _` |   / __| __/ _` | __| | | / __| |  | |
194 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
195 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
196 //                         |___/_____|                          \_\/_/
197 void debug_status(struct afb_req req) noexcept {
198    // Quick and dirty, dump current surfaces and layers
199    AFB_REQ_DEBUG(req, "status");
200
201    CHECK_WAYLAND();
202
203    try {
204       json j;
205
206       if (!g_wayland->controller->surfaces.empty()) {
207          auto js = json::array();
208          for (auto const &i : g_wayland->controller->surfaces) {
209             auto const &r = i.second->dst_rect;
210             auto const &s = i.second->size;
211             js.push_back({{"id", i.first},
212                           {"size", {s.w, s.h}},
213                           {"dst_rect", {r.w, r.h, r.x, r.y}}});
214          }
215          j["surfaces"] = js;
216       }
217
218       if (!g_wayland->controller->layers.empty()) {
219          auto js = json::array();
220          for (auto const &i : g_wayland->controller->layers) {
221             auto const &r = i.second->dst_rect;
222             auto const &s = i.second->size;
223             js.push_back({{"id", i.first},
224                           {"size", {s.w, s.h}},
225                           {"dst_rect", {r.w, r.h, r.x, r.y}}});
226          }
227          j["layers"] = js;
228       }
229
230       afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
231    } catch (std::exception &e) {
232       afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
233    }
234 }
235
236 void debug_surfaces(afb_req req) {
237    CHECK_WAYLAND();
238
239    auto a = json_object_new_array();
240
241    if (!g_wayland->controller->surfaces.empty()) {
242       for (auto const &i : g_wayland->controller->surfaces) {
243          json_object_array_add(a, json_object_new_int(i.first));
244       }
245    }
246
247    afb_req_success(req, a, "surfaces");
248 }
249
250 void debug_layers(afb_req req) {
251    CHECK_WAYLAND();
252
253    auto a = json_object_new_array();
254
255    if (!g_wayland->controller->layers.empty()) {
256       for (auto const &i : g_wayland->controller->layers) {
257          json_object_array_add(a, json_object_new_int(i.first));
258       }
259    }
260
261    afb_req_success(req, a, "surfaces");
262 }
263
264 const struct afb_verb_v2 verbs[] = {
265    {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
266    {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
267    {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
268    {},
269 };
270 }  // namespace
271
272 extern "C" const struct afb_binding_v2 afbBindingV2 = {
273    "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};