Implement window manager as an AGL App-FW binding
[staging/windowmanager.git] / src / main.cpp
1 #include "util.hpp"
2 #include "wayland.hpp"
3
4 #include <algorithm>
5 #include <json.h>
6
7 #define AFB_BINDING_VERSION 2
8
9 extern "C" {
10 #include <afb/afb-binding.h>
11 #include <systemd/sd-event.h>
12 }
13
14 #include <json.hpp>
15
16 using json = nlohmann::json;
17
18 namespace {
19 struct wayland {
20    std::unique_ptr<wl::display> display;
21    std::unique_ptr<genivi::controller> controller;
22    std::vector<std::unique_ptr<wl::output>> outputs;
23    sd_event_source *event_source;
24
25    wayland()
26       : display(new wl::display),
27         controller(nullptr),
28         outputs(),
29         event_source{} {}
30 };
31
32 struct wayland *g_wayland;
33
34 //  _       _ _       _                         _    ____
35 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
36 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
37 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
38 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
39 //              |_____|       |___/                 \_\/_/
40 char const *init_layout() {
41    if (!g_wayland->controller) {
42       return "ivi_controller global not available";
43    }
44
45    if (g_wayland->outputs.empty()) {
46       return "no output was set up!";
47    }
48
49    auto &c = g_wayland->controller;
50
51    auto &o = g_wayland->outputs.front();
52    auto &s = c->screens.begin()->second;
53    auto &layers = c->layers;
54
55    // XXX: Write output dimensions to ivi controller...
56    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
57
58    // Clear scene
59    layers.clear();
60
61    // Clear screen
62    s->clear();
63
64    // Setup our dummy scene...
65    c->layer_create(100, 0, 0);   // bottom layer, anything else
66    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
67
68    auto &l100 = c->layers[100];
69    auto &l1k = c->layers[1000];
70
71    // Set layers fullscreen
72    l100->set_destination_rectangle(0, 0, o->width, o->height);
73    l1k->set_destination_rectangle(0, 0, o->width, o->height);
74    l100->set_visibility(1);
75    l1k->set_visibility(1);
76
77    // Add layers to screen
78    s->set_render_order({100, 1000});
79
80    c->commit_changes();
81
82    g_wayland->display->flush();
83
84    return nullptr;
85 }
86
87 //  _     _           _ _                 _       _ _    ____
88 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
89 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
90 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
91 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
92 //                              |___/_____|             \_\/_/
93 int binding_init_() {
94    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
95
96    if (g_wayland != nullptr) {
97       AFB_ERROR("Wayland context already initialized?");
98       return -1;
99    }
100
101    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
102       AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
103       return -1;
104    }
105
106    g_wayland = new wayland;
107    if (!g_wayland->display->ok()) {
108       AFB_ERROR("Could not connect to compositor");
109       return -1;
110    }
111
112    auto &d = g_wayland->display;
113    d->r.add_global_handler("wl_output", [](wl_registry *r, uint32_t name,
114                                            uint32_t v) {
115       g_wayland->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
116    });
117
118    d->r.add_global_handler("ivi_controller", [](wl_registry *r, uint32_t name,
119                                                 uint32_t v) {
120       g_wayland->controller = std::make_unique<genivi::controller>(r, name, v);
121
122       // XXX: This protocol needs the output, so lets just add our mapping
123       // here...
124       g_wayland->controller->add_proxy_to_id_mapping(
125          g_wayland->outputs.back()->proxy.get(),
126          wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
127             g_wayland->outputs.back()->proxy.get())));
128    });
129
130    // First level objects
131    d->roundtrip();
132    // Second level objects
133    d->roundtrip();
134    // Third level objects
135    d->roundtrip();
136
137    if (char const *e = init_layout()) {
138       AFB_ERROR("Could not init layout: %s", e);
139       return -1;
140    }
141
142    sd_event *ev = afb_daemon_get_event_loop();
143    sd_event_add_io(
144       ev, &g_wayland->event_source, g_wayland->display->get_fd(),
145       EPOLLIN | EPOLLHUP,
146       [](sd_event_source *evs, int fd, uint32_t events, void *data) {
147          if ((events & EPOLLHUP) != 0) {
148             sd_event_source_unref(evs);
149             delete g_wayland;
150             g_wayland = nullptr;
151             return 1;
152          }
153
154          if (events & EPOLLIN) {
155             int ret = g_wayland->display->dispatch();
156             g_wayland->display->flush();
157
158             // execute pending tasks, that is layout changes etc.
159             g_wayland->controller->execute_pending();
160             g_wayland->display->roundtrip();
161             return ret == -1 ? -1 : 0;
162          }
163
164          return 0;
165       },
166       g_wayland);
167
168    atexit([] { delete g_wayland; });
169
170    return 0;
171 }
172
173 int binding_init() noexcept {
174    try {
175       return binding_init_();
176    } catch (std::exception &e) {
177       AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
178    }
179    return -1;
180 }
181
182 //      _      _                         _        _              ____
183 //   __| | ___| |__  _   _  __ _     ___| |_ __ _| |_ _   _ ___ / /\ \
184 //  / _` |/ _ \ '_ \| | | |/ _` |   / __| __/ _` | __| | | / __| |  | |
185 // | (_| |  __/ |_) | |_| | (_| |   \__ \ || (_| | |_| |_| \__ \ |  | |
186 //  \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ |  | |
187 //                         |___/_____|                          \_\/_/
188 void debug_status(struct afb_req req) noexcept {
189    // Quick and dirty, dump current surfaces and layers
190    AFB_REQ_DEBUG(req, "status");
191
192    if (g_wayland == nullptr) {
193       afb_req_fail(req, "failed",
194                    "Binding not initialized, did the compositor die?");
195       return;
196    }
197
198    try {
199       json j;
200
201       if (!g_wayland->controller->surfaces.empty()) {
202          auto js = json::array();
203          for (auto const &i : g_wayland->controller->surfaces) {
204             auto const &r = i.second->dst_rect;
205             auto const &s = i.second->size;
206             js.push_back({{"id", i.first},
207                           {"size", {s.w, s.h}},
208                           {"dst_rect", {r.w, r.h, r.x, r.y}}});
209          }
210          j["surfaces"] = js;
211       }
212
213       if (!g_wayland->controller->layers.empty()) {
214          auto js = json::array();
215          for (auto const &i : g_wayland->controller->layers) {
216             auto const &r = i.second->dst_rect;
217             auto const &s = i.second->size;
218             js.push_back({{"id", i.first},
219                           {"size", {s.w, s.h}},
220                           {"dst_rect", {r.w, r.h, r.x, r.y}}});
221          }
222          j["layers"] = js;
223       }
224
225       afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
226    } catch (std::exception &e) {
227       afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
228    }
229 }
230
231 const struct afb_verb_v2 verbs[] = {
232    {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2}, {},
233 };
234 }  // namespace
235
236 extern "C" const struct afb_binding_v2 afbBindingV2 = {
237    "winman", NULL, NULL, verbs, NULL, binding_init, NULL, 1};