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