Move all nlohmann::json to json_helper.cpp
[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 #define AFB_BINDING_VERSION 2
9
10 extern "C" {
11 #include <afb/afb-binding.h>
12 #include <systemd/sd-event.h>
13 }
14
15 namespace {
16 struct wayland {
17    std::unique_ptr<wl::display> display;
18    std::unique_ptr<genivi::controller> controller;
19    std::vector<std::unique_ptr<wl::output>> outputs;
20
21    wayland() : display(new wl::display), controller(nullptr), outputs() {}
22
23    int init();
24 };
25
26 struct wayland *g_wayland;
27
28 int wayland::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_wayland->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_wayland->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_wayland->controller->add_proxy_to_id_mapping(
47             g_wayland->outputs.back()->proxy.get(),
48             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
49                g_wayland->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_wayland->controller) {
70       return "ivi_controller global not available";
71    }
72
73    if (g_wayland->outputs.empty()) {
74       return "no output was set up!";
75    }
76
77    auto &c = g_wayland->controller;
78
79    auto &o = g_wayland->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_wayland->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       AFB_ERROR("The compositor hung up, dying now.");
119       delete g_wayland;
120       g_wayland = nullptr;
121       goto error;
122    }
123
124    if (events & EPOLLIN) {
125       int ret = g_wayland->display->dispatch();
126       if (ret == -1) {
127          AFB_ERROR("wl_display_dipatch() returned error %d",
128                    g_wayland->display->get_error());
129          goto error;
130       }
131       g_wayland->display->flush();
132
133       // execute pending tasks, that is layout changes etc.
134       g_wayland->controller->execute_pending();
135       g_wayland->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_wayland != nullptr) {
155       AFB_ERROR("Wayland context already initialized?");
156       return 0;
157    }
158
159    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
160       AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
161       goto error;
162    }
163
164    g_wayland = new wayland;
165    if (g_wayland->init() == -1) {
166       AFB_ERROR("Could not connect to compositor");
167       goto error;
168    }
169
170    if (char const *e = init_layout()) {
171       AFB_ERROR("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_wayland->display->get_fd(), EPOLLIN,
178                                 display_event_callback, g_wayland);
179       if (ret < 0) {
180          AFB_ERROR("Could not initialize wayland event handler: %d", -ret);
181          goto error;
182       }
183    }
184
185    atexit([] { delete g_wayland; });
186
187    return 0;
188
189 error:
190    delete g_wayland;
191    g_wayland = nullptr;
192    return -1;
193 }
194
195 int binding_init() noexcept {
196    try {
197       return binding_init_();
198    } catch (std::exception &e) {
199       AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
200    }
201    return -1;
202 }
203
204 #define CHECK_WAYLAND()                                                    \
205    do {                                                                    \
206       if (g_wayland == nullptr) {                                          \
207          afb_req_fail(req, "failed",                                       \
208                       "Binding not initialized, did the compositor die?"); \
209          return;                                                           \
210       }                                                                    \
211    } while (0)
212
213 //      _      _                         _        _              ____
214 //   __| | ___| |__  _   _  __ _     ___| |_ __ _| |_ _   _ ___ / /\ \
215 //  / _` |/ _ \ '_ \| | | |/ _` |   / __| __/ _` | __| | | / __| |  | |
216 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
217 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
218 //                         |___/_____|                          \_\/_/
219 void debug_status(struct afb_req req) noexcept {
220    // Quick and dirty, dump current surfaces and layers
221    AFB_REQ_DEBUG(req, "status");
222
223    CHECK_WAYLAND();
224
225    auto o = json_object_new_object();
226    json_object_object_add(o, "surfaces",
227                           to_json(g_wayland->controller->surfaces));
228    json_object_object_add(o, "layers", to_json(g_wayland->controller->layers));
229    json_object_object_add(o, "screens",
230                           to_json(g_wayland->controller->screens));
231
232    afb_req_success(req, o, "status");
233 }
234
235 void debug_surfaces(afb_req req) noexcept {
236    CHECK_WAYLAND();
237
238    afb_req_success(req, to_json(g_wayland->controller->surfaces), "surfaces");
239 }
240
241 void debug_layers(afb_req req) noexcept {
242    CHECK_WAYLAND();
243
244    afb_req_success(req, to_json(g_wayland->controller->layers), "layers");
245 }
246
247 const struct afb_verb_v2 verbs[] = {
248    {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
249    {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
250    {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
251    {},
252 };
253 }  // namespace
254
255 extern "C" const struct afb_binding_v2 afbBindingV2 = {
256    "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};