main: use our own log functions
[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 #define CHECK_WAYLAND()                                                    \
203    do {                                                                    \
204       if (g_wayland == nullptr) {                                          \
205          afb_req_fail(req, "failed",                                       \
206                       "Binding not initialized, did the compositor die?"); \
207          return;                                                           \
208       }                                                                    \
209    } while (0)
210
211 //      _      _                         _        _              ____
212 //   __| | ___| |__  _   _  __ _     ___| |_ __ _| |_ _   _ ___ / /\ \
213 //  / _` |/ _ \ '_ \| | | |/ _` |   / __| __/ _` | __| | | / __| |  | |
214 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
215 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
216 //                         |___/_____|                          \_\/_/
217 void debug_status(struct afb_req req) {
218    // Quick and dirty, dump current surfaces and layers
219    AFB_REQ_DEBUG(req, "status");
220
221    auto o = json_object_new_object();
222    json_object_object_add(o, "surfaces",
223                           to_json(g_wayland->controller->surfaces));
224    json_object_object_add(o, "layers", to_json(g_wayland->controller->layers));
225    json_object_object_add(o, "screens",
226                           to_json(g_wayland->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_wayland->controller->surfaces), "surfaces");
233 }
234
235 void debug_layers(afb_req req) {
236    afb_req_success(req, to_json(g_wayland->controller->layers), "layers");
237 }
238
239 #define WRAP(F)                                                             \
240    [](afb_req req) noexcept {                                               \
241       CHECK_WAYLAND();                                                      \
242       try {                                                                 \
243          F(req);                                                            \
244       } catch (std::exception & e) {                                        \
245          afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
246       }                                                                     \
247    }
248
249 const struct afb_verb_v2 verbs[] = {
250    {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
251    {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
252    {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
253    {},
254 };
255 }  // namespace
256
257 extern "C" const struct afb_binding_v2 afbBindingV2 = {
258    "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};