Generating binding API glue code using generate-binding.py
[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    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    // Init the app's members
60    this->app.display = this->display.get();
61    this->app.controller = this->controller.get();
62
63    return 0;
64 }
65
66 //  _       _ _       _                         _    ____
67 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
68 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
69 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
70 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
71 //              |_____|       |___/                 \_\/_/
72 char const *init_layout() {
73    if (!g_afb_instance->controller) {
74       return "ivi_controller global not available";
75    }
76
77    if (g_afb_instance->outputs.empty()) {
78       return "no output was set up!";
79    }
80
81    auto &c = g_afb_instance->controller;
82
83    auto &o = g_afb_instance->outputs.front();
84    auto &s = c->screens.begin()->second;
85    auto &layers = c->layers;
86
87    // XXX: Write output dimensions to ivi controller...
88    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
89
90    // Clear scene
91    layers.clear();
92
93    // Clear screen
94    s->clear();
95
96    // Setup our dummy scene...
97    c->layer_create(100, 0, 0);   // bottom layer, anything else
98    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
99
100    auto &l100 = c->layers[100];
101    auto &l1k = c->layers[1000];
102
103    // Set layers fullscreen
104    l100->set_destination_rectangle(0, 0, o->width, o->height);
105    l1k->set_destination_rectangle(0, 0, o->width, o->height);
106    l100->set_visibility(1);
107    l1k->set_visibility(1);
108
109    // Add layers to screen
110    s->set_render_order({100, 1000});
111
112    c->commit_changes();
113
114    g_afb_instance->display->flush();
115
116    return nullptr;
117 }
118
119 int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
120                            void *data) {
121    if ((events & EPOLLHUP) != 0) {
122       logerror("The compositor hung up, dying now.");
123       delete g_afb_instance;
124       g_afb_instance = nullptr;
125       goto error;
126    }
127
128    if (events & EPOLLIN) {
129       int ret = g_afb_instance->display->dispatch();
130       if (ret == -1) {
131          logerror("wl_display_dipatch() returned error %d",
132                    g_afb_instance->display->get_error());
133          goto error;
134       }
135       g_afb_instance->display->flush();
136
137       // execute pending tasks, that is layout changes etc.
138       g_afb_instance->controller->execute_pending();
139       g_afb_instance->display->roundtrip();
140    }
141
142    return 0;
143
144 error:
145    sd_event_source_unref(evs);
146    return -1;
147 }
148
149 //  _     _           _ _                 _       _ _    ____
150 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
151 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
152 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
153 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
154 //                              |___/_____|             \_\/_/
155 int binding_init_() {
156    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
157
158    if (g_afb_instance != nullptr) {
159       logerror("Wayland context already initialized?");
160       return 0;
161    }
162
163    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
164       logerror("Environment variable XDG_RUNTIME_DIR not set");
165       goto error;
166    }
167
168    g_afb_instance = new afb_instance;
169    if (g_afb_instance->init() == -1) {
170       logerror("Could not connect to compositor");
171       goto error;
172    }
173
174    if (char const *e = init_layout()) {
175       logerror("Could not init layout: %s", e);
176       goto error;
177    }
178
179    {
180       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
181                                 g_afb_instance->display->get_fd(), EPOLLIN,
182                                 display_event_callback, g_afb_instance);
183       if (ret < 0) {
184          logerror("Could not initialize afb_instance event handler: %d", -ret);
185          goto error;
186       }
187    }
188
189    atexit([] { delete g_afb_instance; });
190
191    return 0;
192
193 error:
194    delete g_afb_instance;
195    g_afb_instance = nullptr;
196    return -1;
197 }
198
199 int binding_init() noexcept {
200    try {
201       return binding_init_();
202    } catch (std::exception &e) {
203       logerror("Uncaught exception in binding_init(): %s", e.what());
204    }
205    return -1;
206 }
207
208 } // namespace
209
210 #include "afb_binding.inl"
211
212 extern "C" const struct afb_binding_v2 afbBindingV2 = {
213    "winman", NULL, NULL, winman_verbs, NULL, binding_init, NULL, 1};