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