1ac26f117df2669694cd83095cd50e00ef08b876
[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 0;
158    }
159
160    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
161       AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
162       goto error;
163    }
164
165    g_wayland = new wayland;
166    if (g_wayland->init() == -1) {
167       AFB_ERROR("Could not connect to compositor");
168       goto error;
169    }
170
171    if (char const *e = init_layout()) {
172       AFB_ERROR("Could not init layout: %s", e);
173       goto error;
174    }
175
176    {
177       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
178                                 g_wayland->display->get_fd(), EPOLLIN,
179                                 display_event_callback, g_wayland);
180       if (ret < 0) {
181                  AFB_ERROR("Could not initialize wayland event handler: %s",
182                            std::strerror(-ret));
183          goto error;
184       }
185    }
186
187    atexit([] { delete g_wayland; });
188
189    return 0;
190
191 error:
192    delete g_wayland;
193    g_wayland = nullptr;
194    return -1;
195 }
196
197 int binding_init() noexcept {
198    try {
199       return binding_init_();
200    } catch (std::exception &e) {
201       AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
202    }
203    return -1;
204 }
205
206 #define CHECK_WAYLAND()                                                    \
207    do {                                                                    \
208       if (g_wayland == nullptr) {                                          \
209          afb_req_fail(req, "failed",                                       \
210                       "Binding not initialized, did the compositor die?"); \
211          return;                                                           \
212       }                                                                    \
213    } while (0)
214
215 //      _      _                         _        _              ____
216 //   __| | ___| |__  _   _  __ _     ___| |_ __ _| |_ _   _ ___ / /\ \
217 //  / _` |/ _ \ '_ \| | | |/ _` |   / __| __/ _` | __| | | / __| |  | |
218 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
219 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
220 //                         |___/_____|                          \_\/_/
221 void debug_status(struct afb_req req) noexcept {
222    // Quick and dirty, dump current surfaces and layers
223    AFB_REQ_DEBUG(req, "status");
224
225    CHECK_WAYLAND();
226
227    try {
228       using json = nlohmann::json;
229
230       json j;
231
232       if (!g_wayland->controller->surfaces.empty()) {
233          auto a = json::array();
234          for (auto const &i : g_wayland->controller->surfaces) {
235             auto const &r = i.second->dst_rect;
236             auto const &s = i.second->size;
237             a.push_back({{"id", i.first},
238                          {"size", {s.w, s.h}},
239                          {"dst_rect", {r.w, r.h, r.x, r.y}}});
240          }
241          j["surfaces"] = a;
242       }
243
244       if (!g_wayland->controller->layers.empty()) {
245          auto a = json::array();
246          for (auto const &i : g_wayland->controller->layers) {
247             auto const &r = i.second->dst_rect;
248             auto const &s = i.second->size;
249             a.push_back({{"id", i.first},
250                           {"size", {s.w, s.h}},
251                           {"dst_rect", {r.w, r.h, r.x, r.y}}});
252          }
253          j["layers"] = a;
254       }
255
256       afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
257    } catch (std::exception &e) {
258       afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
259    }
260 }
261
262 void debug_surfaces(afb_req req) noexcept {
263    CHECK_WAYLAND();
264
265    auto a = json_object_new_array();
266
267    if (!g_wayland->controller->surfaces.empty()) {
268       for (auto const &i : g_wayland->controller->surfaces) {
269          json_object_array_add(a, json_object_new_int(i.first));
270       }
271    }
272
273    afb_req_success(req, a, "surfaces");
274 }
275
276 void debug_layers(afb_req req) noexcept {
277    CHECK_WAYLAND();
278
279    auto a = json_object_new_array();
280
281    if (!g_wayland->controller->layers.empty()) {
282       for (auto const &i : g_wayland->controller->layers) {
283          json_object_array_add(a, json_object_new_int(i.first));
284       }
285    }
286
287    afb_req_success(req, a, "surfaces");
288 }
289
290 const struct afb_verb_v2 verbs[] = {
291    {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
292    {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
293    {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
294    {},
295 };
296 }  // namespace
297
298 extern "C" const struct afb_binding_v2 afbBindingV2 = {
299    "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};