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