#include "util.h"
+#include "wayland.hpp"
-#include "ivi-controller-client-protocol.h"
+#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
+#include <signal.h>
+#include <sys/poll.h>
+#include <sys/signalfd.h>
-#include <wayland-client.h>
+#include <algorithm>
-#include <map>
-#include <memory>
-#include <string>
-#include <vector>
+struct connection {
+ std::vector<std::unique_ptr<wl::output>> outputs;
+ std::unique_ptr<genivi::controller> c;
+};
-struct ivi_surface;
-struct ivi_layer;
+// _ _ ____ _ _
+// ___| |_ _ __ _ _ ___| |_ | _ \ ___ | | | ___ _ __
+// / __| __| '__| | | |/ __| __| | |_) / _ \| | |/ _ \ '__|
+// \__ \ |_| | | |_| | (__| |_ | __/ (_) | | | __/ |
+// |___/\__|_| \__,_|\___|\__| |_| \___/|_|_|\___|_|
+//
+struct Poller {
+ std::vector<std::function<int(int)>> handlers;
+ std::vector<struct pollfd> pfds;
+
+ Poller() = default;
+ void add_fd(int fd, std::function<int(int)> handler);
+ int check_events();
+};
-struct conn {
- wl_display *d;
- wl_registry *r;
- ivi_controller *c;
- std::vector<std::unique_ptr<wl_output, void (*)(wl_output *)>> outputs;
- std::map<uint32_t, std::unique_ptr<ivi_layer>> layers;
- std::map<uint32_t, std::unique_ptr<ivi_surface>> surfaces;
+void Poller::add_fd(int fd, std::function<int(int)> handler) {
+ pfds.emplace_back(pollfd{.fd = fd, .events = POLLIN, .revents = 0});
+ handlers.emplace_back(std::move(handler));
+}
- ~conn();
-};
+int Poller::check_events() {
+ int ret = 0;
+ if ((ret = poll(this->pfds.data(), this->pfds.size(), -1)) != -1 &&
+ errno != EINTR) {
+ for (unsigned i = 0; i < pfds.size(); i++) {
+ if (pfds[i].revents & POLLIN) {
+ if (handlers[i](pfds[i].fd) == -1) {
+ return -1;
+ }
+ pfds[i].revents = 0;
+ pfds[i].events = POLLIN;
+ }
+ }
+ }
+ return ret;
+}
-struct ivi_surface {
- ivi_controller_surface *controller;
- uint32_t id;
- conn *con;
- ivi_surface(ivi_controller_surface *c, uint32_t i, conn *co)
- : controller(c), id(i), con(co) {}
- ~ivi_surface() { ivi_controller_surface_destroy(this->controller, 1); }
+struct unique_fd {
+ int fd {-1};
+ unique_fd() = default;
+ explicit unique_fd(int f) : fd{f} {}
+ operator int() const { return fd; }
+ ~unique_fd() {
+ if (this->fd != -1)
+ close(this->fd);
+ }
+ unique_fd(unique_fd const &) = delete;
+ unique_fd &operator=(unique_fd const &) = delete;
+ unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
+ unique_fd &operator=(unique_fd &&o) {
+ std::swap(this->fd, o.fd);
+ return *this;
+ }
};
-struct ivi_layer {
- ivi_controller_layer *controller;
- uint32_t id;
- conn *con;
- ivi_layer(ivi_controller_layer *c, uint32_t i, conn *co)
- : controller(c), id(i), con(co) {}
- ~ivi_layer() { ivi_controller_layer_destroy(this->controller, 1); }
-};
-conn::~conn() {
- this->layers.clear();
- this->surfaces.clear();
- ivi_controller_destroy(this->c);
- this->outputs.clear();
- wl_registry_destroy(this->r);
- wl_display_disconnect(this->d);
-}
+namespace {
+// _ _ _ _ _ ____
+// (_)_ __ (_) |_ | | __ _ _ _ ___ _ _| |_ / /\ \
+// | | '_ \| | __| | |/ _` | | | |/ _ \| | | | __| | | |
+// | | | | | | |_ | | (_| | |_| | (_) | |_| | |_| | | |
+// |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| | | |
+// |_____| |___/ \_\/_/
+char const *init_layout(struct connection &c) {
+ if (!c.c) {
+ return "ivi_controller global not available";
+ }
-static ivi_controller_surface_listener cs_listener = {};
+ if (c.outputs.empty()) {
+ return "no output was set up!";
+ }
-static ivi_controller_layer_listener cl_listener = {};
+ auto &o = c.outputs.front();
+ auto &s = c.c->screens.begin()->second;
+ auto &layers = c.c->layers;
-static void c_screen(void *data, struct ivi_controller *ivi_controller,
- uint32_t id_screen, struct ivi_controller_screen *screen) {
- lognotice("ivi_controller @ %p screen %u (%x) @ %p", ivi_controller,
- id_screen, id_screen, screen);
-}
+ // XXX: Write output dimensions to ivi controller...
+ c.c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
-static void c_layer(void *data, struct ivi_controller *ivi_controller,
- uint32_t id_layer) {
- lognotice("ivi_controller @ %p layer %u (%x)", ivi_controller, id_layer,
- id_layer);
- auto c = static_cast<conn *>(data);
- auto i = std::make_unique<ivi_layer>(
- ivi_controller_layer_create(c->c, id_layer, 0, 0), id_layer, c);
- ivi_controller_layer_add_listener(i->controller, &cl_listener, i.get());
- c->layers[id_layer] = std::move(i);
-}
+ // Clear scene
+ layers.clear();
-static void c_surface(void *data, struct ivi_controller *ivi_controller,
- uint32_t id_surface) {
- lognotice("ivi_controller @ %p surface %u (%x)", ivi_controller, id_surface,
- id_surface);
- auto c = static_cast<conn *>(data);
- auto i = std::make_unique<ivi_surface>(
- ivi_controller_surface_create(c->c, id_surface), id_surface, c);
- ivi_controller_surface_add_listener(i->controller, &cs_listener, i.get());
- c->surfaces[id_surface] = std::move(i);
-}
+ // Clear screen
+ s->clear();
-static void c_error(void *data, struct ivi_controller *ivi_controller,
- int32_t object_id, int32_t object_type, int32_t error_code,
- const char *error_text) {
- lognotice("ivi_controller @ %p error o %i t %i c %i text %s", ivi_controller,
- object_id, object_type, error_code, error_text);
-}
+ // Setup our dummy scene...
+ c.c->layer_create(100, 0, 0); // bottom layer, anything else
+ c.c->layer_create(1000, 0, 0); // top layer, mandelbrot
-static struct ivi_controller_listener c_listener = {c_screen, c_layer,
- c_surface, c_error};
+ auto &l100 = c.c->layers[100];
+ auto &l1k = c.c->layers[1000];
-static void o_geometry(void *data, struct wl_output *wl_output, int32_t x,
- int32_t y, int32_t physical_width,
- int32_t physical_height, int32_t subpixel,
- const char *make, const char *model, int32_t transform) {
- lognotice("output @ %p x %i y %i w %i h %i spel %x make %s model %s tx %i",
- wl_output, x, y, physical_width, physical_height, subpixel, make,
- model, transform);
-}
+ // Set layers fullscreen
+ l100->set_destination_rectangle(0, 0, o->width, o->height);
+ l1k->set_destination_rectangle(0, 0, o->width, o->height);
+ l100->set_visibility(1);
+ l1k->set_visibility(1);
-static void o_mode(void *data, struct wl_output *wl_output, uint32_t flags,
- int32_t width, int32_t height, int32_t refresh) {
- lognotice("output @ %p mode f %x w %i h %i r %i", wl_output, flags, width,
- height, refresh);
-}
+ // Add layers to screen
+ s->set_render_order({100, 1000});
-static void o_done(void *data, struct wl_output *wl_output) {
- lognotice("output @ %p done");
-}
+ c.c->commit_changes();
+ // Note: this does not flush the display!
-static void o_scale(void *data, struct wl_output *wl_output, int32_t factor) {
- lognotice("output @ %p scale %i", wl_output, factor);
+ return nullptr;
}
+} // namespace
+
+// _ ____
+// _ __ ___ __ _(_)_ __ / /\ \
+// | '_ ` _ \ / _` | | '_ \| | | |
+// | | | | | | (_| | | | | | | | |
+// |_| |_| |_|\__,_|_|_| |_| | | |
+// \_\/_/
+int main(int /*argc*/, char ** /*argv*/) {
+ lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
-static struct wl_output_listener o_listener = {o_geometry, o_mode, o_done,
- o_scale};
-
-static void r_global(void *data, struct wl_registry *r, uint32_t name,
- char const *iface, uint32_t v) {
- struct conn *c = static_cast<conn *>(data);
-
- if (strcmp(iface, "ivi_controller") == 0) {
- c->c = static_cast<ivi_controller *>(
- wl_registry_bind(r, name, &ivi_controller_interface, v));
- ivi_controller_add_listener(c->c, &c_listener, c);
- } else if (strcmp(iface, "wl_output") == 0) {
- auto o = static_cast<wl_output *>(
- wl_registry_bind(r, name, &wl_output_interface, v));
- c->outputs.emplace_back(std::unique_ptr<wl_output, void (*)(wl_output *)>(
- o, wl_output_destroy));
- wl_output_add_listener(o, &o_listener, c);
- } else {
- lognotice("registry @ %p global n %u i %s v %u", r, name, iface, v);
+ if (getenv("XDG_RUNTIME_DIR") == nullptr) {
+ fatal("Environment variable XDG_RUNTIME_DIR not set");
}
-}
-static void r_global_remove(void *data, struct wl_registry *r, uint32_t name) {}
-
-static struct wl_registry_listener r_listener = {r_global, r_global_remove};
+ struct wl::display d {};
+ if (!d.ok()) {
+ fatal("Could not connect to compositor");
+ }
-int main(int argc, char **argv) {
- lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
+ struct connection c {};
- if (!getenv("XDG_RUNTIME_DIR"))
- fatal("Environment variable XDG_RUNTIME_DIR not set");
+ d.r.add_global_handler(
+ "wl_output", [&c](wl_registry *r, uint32_t name, uint32_t v) {
+ c.outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
+ });
- struct conn c = {};
+ d.r.add_global_handler(
+ "ivi_controller", [&c](wl_registry *r, uint32_t name, uint32_t v) {
+ c.c = std::make_unique<genivi::controller>(r, name, v);
- c.d = wl_display_connect(NULL);
- if (!c.d)
- fatal("Could not connect to compositor");
- c.r = wl_display_get_registry(c.d);
- wl_registry_add_listener(c.r, &r_listener, &c);
+ // XXX: This protocol needs the output, so lets just add our mapping here...
+ c.c->add_proxy_to_id_mapping(c.outputs.back()->proxy.get(),
+ wl_proxy_get_id(
+ reinterpret_cast<struct wl_proxy *>(
+ c.outputs.back()->proxy.get())));
+ });
// First level objects
- wl_display_roundtrip(c.d);
+ d.roundtrip();
// Second level objects
- wl_display_roundtrip(c.d);
+ d.roundtrip();
+ // Third level objects
+ d.roundtrip();
- if (!c.c)
- fatal("ivi_controller global not available");
+ if (char const *e = init_layout(c)) {
+ fatal("Could not init layout: %s", e);
+ }
+
+ struct Poller p{};
+ p.add_fd(STDIN_FILENO, [&c](int fd) {
+ int buf;
+ ssize_t ret;
+ ret = read(fd, &buf, sizeof(buf));
+ c.c->debug_dump_current_status();
+ return ret == 0 ? -1 : 0;
+ });
+
+ p.add_fd(d.get_fd(), [&d](int fd) {
+ return d.dispatch();
+ });
+
+ sigset_t sset{};
+ sigemptyset(&sset);
+ sigaddset(&sset, SIGINT);
+ sigaddset(&sset, SIGTERM);
+
+ auto sfd = unique_fd(signalfd(-1, &sset, SFD_NONBLOCK | SFD_CLOEXEC));
+ sigprocmask(SIG_BLOCK, &sset, NULL);
+ p.add_fd(sfd.fd, [](int fd) {
+ struct signalfd_siginfo si;
+ while (read(fd, &si, sizeof(si)) == sizeof(si)) {
+ lognotice("Received signal %u", si.ssi_signo);
+ }
+ return -1;
+ });
+
+ while ((d.flush(), p.check_events()) != -1) {
+ c.c->execute_pending();
+ }
- // main loop
+ c.c->commit_changes();
+ d.roundtrip();
return 0;
}