X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.cpp;h=5eb211cb41b7d4a976e7d55cc3ae2780d2d45816;hb=9ce5644a2350f8acbf606b2ee1073727578111ee;hp=794d9af1cfab6049ed205a493881a89add510d58;hpb=11369e6592800fcf03138b109fee3d4c0736f21a;p=staging%2Fwindowmanager.git diff --git a/src/main.cpp b/src/main.cpp index 794d9af..5eb211c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,172 +1,115 @@ -#include "util.h" +#include "app.hpp" +#include "json_helper.hpp" +#include "util.hpp" +#include "wayland.hpp" -#include "ivi-controller-client-protocol.h" +#include +#include -#include -#include - -#include - -#include -#include -#include -#include +extern "C" { +#include +#include +} -struct ivi_surface; -struct ivi_layer; +namespace { +struct afb_instance { + std::unique_ptr display; + wm::App app; -struct conn { - wl_display *d; - wl_registry *r; - ivi_controller *c; - std::vector> outputs; - std::map> layers; - std::map> surfaces; + afb_instance() : display{new wl::display}, app{this->display.get()} {} - ~conn(); + int init(); }; -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 afb_instance *g_afb_instance; -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); } -}; +int afb_instance::init() { + if (!this->display->ok()) { + return -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); + return this->app.init(); } -static ivi_controller_surface_listener cs_listener = {}; +int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events, + void * /*data*/) { + if ((events & EPOLLHUP) != 0) { + logerror("The compositor hung up, dying now."); + delete g_afb_instance; + g_afb_instance = nullptr; + goto error; + } -static ivi_controller_layer_listener cl_listener = {}; + if ((events & EPOLLIN) != 0u) { + if (g_afb_instance->app.dispatch_events() == -1) { + goto error; + } + } -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); -} + return 0; -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(data); - auto i = std::make_unique( - 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); +error: + sd_event_source_unref(evs); + return -1; } -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(data); - auto i = std::make_unique( - 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); -} +// _ _ _ _ _ _ _ ____ +// | |__ (_)_ __ __| (_)_ __ __ _ (_)_ __ (_) |_ / /\ \ +// | '_ \| | '_ \ / _` | | '_ \ / _` | | | '_ \| | __| | | | +// | |_) | | | | | (_| | | | | | (_| | | | | | | | |_| | | | +// |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| | | | +// |___/_____| \_\/_/ +int binding_init_() { + lognotice("WinMan ver. %s", WINMAN_VERSION_STRING); -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); -} + if (g_afb_instance != nullptr) { + logerror("Wayland context already initialized?"); + return 0; + } -static struct ivi_controller_listener c_listener = {c_screen, c_layer, - c_surface, c_error}; + if (getenv("XDG_RUNTIME_DIR") == nullptr) { + logerror("Environment variable XDG_RUNTIME_DIR not set"); + goto error; + } -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); -} + g_afb_instance = new afb_instance; + if (g_afb_instance->init() == -1) { + logerror("Could not connect to compositor"); + goto error; + } -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); -} + { + int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr, + g_afb_instance->display->get_fd(), EPOLLIN, + display_event_callback, g_afb_instance); + if (ret < 0) { + logerror("Could not initialize afb_instance event handler: %d", -ret); + goto error; + } + } -static void o_done(void *data, struct wl_output *wl_output) { - lognotice("output @ %p done"); -} + atexit([] { delete g_afb_instance; }); -static void o_scale(void *data, struct wl_output *wl_output, int32_t factor) { - lognotice("output @ %p scale %i", wl_output, factor); + return 0; + +error: + delete g_afb_instance; + g_afb_instance = nullptr; + return -1; } -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(data); - - if (strcmp(iface, "ivi_controller") == 0) { - c->c = static_cast( - 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_registry_bind(r, name, &wl_output_interface, v)); - c->outputs.emplace_back(std::unique_ptr( - 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); +int binding_init() noexcept { + try { + return binding_init_(); + } catch (std::exception &e) { + logerror("Uncaught exception in binding_init(): %s", e.what()); } + return -1; } -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}; - -int main(int argc, char **argv) { - lognotice("WinMan ver. %s", WINMAN_VERSION_STRING); - - if (!getenv("XDG_RUNTIME_DIR")) - fatal("Environment variable XDG_RUNTIME_DIR not set"); - - struct conn c = {}; - - 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); +} // namespace - // First level objects - wl_display_roundtrip(c.d); - // Second level objects - wl_display_roundtrip(c.d); +#include "afb_binding_glue.inl" - if (!c.c) - fatal("ivi_controller global not available"); - - // main loop - - return 0; -} +extern "C" const struct afb_binding_v2 afbBindingV2 = { + "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 1};