5c9df33ed8cffa9ff2ad62bd6a2786f79a715128
[staging/windowmanager.git] / src / main.cpp
1 #include "json_helper.hpp"
2 #include "util.hpp"
3 #include "wayland.hpp"
4 #include "app.hpp"
5
6 #include <algorithm>
7 #include <json.h>
8
9 extern "C" {
10 #include <afb/afb-binding.h>
11 #include <systemd/sd-event.h>
12 }
13
14 namespace {
15 struct afb_instance {
16    std::unique_ptr<wl::display> display;
17    std::unique_ptr<genivi::controller> controller;
18    std::vector<std::unique_ptr<wl::output>> outputs;
19
20    wm::App app;
21
22    afb_instance() : display{new wl::display}, controller{nullptr}, outputs{}, app{} {}
23
24    int init();
25 };
26
27 struct afb_instance *g_afb_instance;
28
29 int afb_instance::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_afb_instance->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_afb_instance->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_afb_instance->controller->add_proxy_to_id_mapping(
48             g_afb_instance->outputs.back()->proxy.get(),
49             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
50                g_afb_instance->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_afb_instance->controller) {
71       return "ivi_controller global not available";
72    }
73
74    if (g_afb_instance->outputs.empty()) {
75       return "no output was set up!";
76    }
77
78    auto &c = g_afb_instance->controller;
79
80    auto &o = g_afb_instance->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_afb_instance->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       logerror("The compositor hung up, dying now.");
120       delete g_afb_instance;
121       g_afb_instance = nullptr;
122       goto error;
123    }
124
125    if (events & EPOLLIN) {
126       int ret = g_afb_instance->display->dispatch();
127       if (ret == -1) {
128          logerror("wl_display_dipatch() returned error %d",
129                    g_afb_instance->display->get_error());
130          goto error;
131       }
132       g_afb_instance->display->flush();
133
134       // execute pending tasks, that is layout changes etc.
135       g_afb_instance->controller->execute_pending();
136       g_afb_instance->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_afb_instance != nullptr) {
156       logerror("Wayland context already initialized?");
157       return 0;
158    }
159
160    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
161       logerror("Environment variable XDG_RUNTIME_DIR not set");
162       goto error;
163    }
164
165    g_afb_instance = new afb_instance;
166    if (g_afb_instance->init() == -1) {
167       logerror("Could not connect to compositor");
168       goto error;
169    }
170
171    if (char const *e = init_layout()) {
172       logerror("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_afb_instance->display->get_fd(), EPOLLIN,
179                                 display_event_callback, g_afb_instance);
180       if (ret < 0) {
181          logerror("Could not initialize afb_instance event handler: %d", -ret);
182          goto error;
183       }
184    }
185
186    atexit([] { delete g_afb_instance; });
187
188    return 0;
189
190 error:
191    delete g_afb_instance;
192    g_afb_instance = nullptr;
193    return -1;
194 }
195
196 int binding_init() noexcept {
197    try {
198       return binding_init_();
199    } catch (std::exception &e) {
200       logerror("Uncaught exception in binding_init(): %s", e.what());
201    }
202    return -1;
203 }
204
205 //      _      _                         _        _              ____
206 //   __| | ___| |__  _   _  __ _     ___| |_ __ _| |_ _   _ ___ / /\ \
207 //  / _` |/ _ \ '_ \| | | |/ _` |   / __| __/ _` | __| | | / __| |  | |
208 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
209 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
210 //                         |___/_____|                          \_\/_/
211 void debug_status(struct afb_req req) {
212    // Quick and dirty, dump current surfaces and layers
213    AFB_REQ_DEBUG(req, "status");
214
215    // auto r = g_afb_instance->app.api.debug_status();
216    // if (r.is_err()) {
217    //    afb_req_fail(req, "failed", r.e.value());
218    //    return;
219    // }
220
221    auto o = json_object_new_object();
222    json_object_object_add(o, "surfaces",
223                           to_json(g_afb_instance->controller->sprops));
224    json_object_object_add(o, "layers", to_json(g_afb_instance->controller->lprops));
225 //   json_object_object_add(o, "screens",
226 //                          to_json(g_afb_instance->controller->screens));
227
228    afb_req_success(req, o, "status");
229 }
230
231 void debug_surfaces(afb_req req) {
232    afb_req_success(req, to_json(g_afb_instance->controller->sprops), "surfaces");
233 }
234
235 void debug_layers(afb_req req) {
236    afb_req_success(req, to_json(g_afb_instance->controller->lprops), "layers");
237 }
238
239 // Dummy register_surface implementation
240 void register_surface(afb_req req) {
241    AFB_DEBUG("register_surface");
242
243    auto jo = afb_req_json(req);
244    json_object *jappid;
245    if (! json_object_object_get_ex(jo, "appid", &jappid)) {
246       afb_req_fail(req, "failed", "register_surface needs 'appid' integer argument");
247       return;
248    }
249
250    json_object *jsurfid;
251    if (! json_object_object_get_ex(jo, "surfaceid", &jsurfid)) {
252       afb_req_fail(req, "failed", "register_surface needs 'surfaceid' integer argument");
253       return;
254    }
255
256    uint32_t appid = json_object_get_int(jappid);
257    uint32_t surfid = json_object_get_int(jsurfid);
258
259    if (appid > 0xff) {
260       afb_req_fail(req, "failed", "invalid appid");
261       return;
262    }
263
264    if (surfid > 0xffff) {
265       afb_req_fail(req, "failed", "invalid surfaceid");
266       return;
267    }
268
269    lognotice("register_surface, got appid %d and surfaceid %d", appid, surfid);
270
271    afb_req_success(req, json_object_new_int((appid << 16) + surfid), "success");
272 }
273
274 #define WRAP(F)                                                             \
275    [](afb_req req) noexcept {                                               \
276       if (g_afb_instance == nullptr) {                                           \
277          afb_req_fail(req, "failed",                                        \
278                       "Binding not initialized, did the compositor die?");  \
279          return;                                                            \
280       }                                                                     \
281       try {                                                                 \
282          F(req);                                                            \
283       } catch (std::exception & e) {                                        \
284          afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
285       }                                                                     \
286    }
287
288 const struct afb_verb_v2 verbs[] = {
289    {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
290    {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
291    {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
292
293    {"register_surface", WRAP(register_surface), 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};