main: pass struct conn by-reference to init_layout()
[staging/windowmanager.git] / src / wayland.cpp
index c2f55fa..1078a14 100644 (file)
@@ -15,14 +15,18 @@ namespace wl {
 //  \__,_|_|___/ .__/|_|\__,_|\__, |
 //             |_|            |___/
 display::display()
-   : d(std::unique_ptr<struct wl_display,
-                       std::function<void(struct wl_display *)>>(
+   : d(std::unique_ptr<struct wl_display, void (*)(struct wl_display *)>(
         wl_display_connect(NULL),
+#ifdef DEBUG_OUTPUT
         [](struct wl_display *d) {
            logdebug("wl::display ~display @ %p", d);
            wl_display_disconnect(d);
         })),
-     r(std::make_unique<struct registry>(d.get())) {}
+#else
+        &wl_display_disconnect)),
+#endif
+     r(d.get()) {
+}
 
 display::~display() {}
 
@@ -32,6 +36,10 @@ void display::roundtrip() { wl_display_roundtrip(this->d.get()); }
 
 int display::dispatch() { return wl_display_dispatch(this->d.get()); }
 
+void display::flush() { wl_display_flush(this->d.get()); }
+
+int display::get_fd() const { return wl_display_get_fd(this->d.get()); }
+
 //                 _     _
 //  _ __ ___  __ _(_)___| |_ _ __ _   _
 // | '__/ _ \/ _` | / __| __| '__| | | |
@@ -53,8 +61,9 @@ constexpr struct wl_registry_listener registry_listener = {
 }
 
 registry::registry(struct wl_display *d)
-   : wayland_proxy(wl_display_get_registry(d)) {
-   wl_registry_add_listener(this->proxy, &registry_listener, this);
+   : wayland_proxy(!d ? nullptr : wl_display_get_registry(d)) {
+   if (this->proxy)
+      wl_registry_add_listener(this->proxy, &registry_listener, this);
 }
 
 registry::~registry() {
@@ -69,9 +78,8 @@ void registry::global(uint32_t name, char const *iface, uint32_t v) {
    auto b = this->bindings.find(iface);
    if (b != this->bindings.end())
       b->second(this->proxy, name, v);
-   else
-      logdebug("wl::registry @ %p global n %u i %s v %u", this->proxy, name,
-               iface, v);
+   logdebug("wl::registry @ %p global n %u i %s v %u", this->proxy, name, iface,
+            v);
 }
 
 void registry::global_remove(uint32_t name) {}
@@ -124,6 +132,11 @@ void output::geometry(int32_t x, int32_t y, int32_t pw, int32_t ph,
 void output::mode(uint32_t flags, int32_t w, int32_t h, int32_t r) {
    logdebug("wl::output %s @ %p f %x w %i h %i r %i", __func__, this->proxy,
             flags, w, h, r);
+   if (flags & WL_OUTPUT_MODE_CURRENT) {
+      this->width = w;
+      this->height = h;
+      this->refresh = r;
+   }
 }
 
 void output::done() {
@@ -158,24 +171,24 @@ namespace {
 void controller_screen(void *data, struct ivi_controller *ivi_controller,
                        uint32_t id_screen,
                        struct ivi_controller_screen *screen) {
-   static_cast<struct controller *>(data)->screen(id_screen, screen);
+   static_cast<struct controller *>(data)->controller_screen(id_screen, screen);
 }
 
 void controller_layer(void *data, struct ivi_controller *ivi_controller,
                       uint32_t id_layer) {
-   static_cast<struct controller *>(data)->layer(id_layer);
+   static_cast<struct controller *>(data)->controller_layer(id_layer);
 }
 
 void controller_surface(void *data, struct ivi_controller *ivi_controller,
                         uint32_t id_surface) {
-   static_cast<struct controller *>(data)->surface(id_surface);
+   static_cast<struct controller *>(data)->controller_surface(id_surface);
 }
 
 void controller_error(void *data, struct ivi_controller *ivi_controller,
                       int32_t object_id, int32_t object_type,
                       int32_t error_code, const char *error_text) {
-   static_cast<struct controller *>(data)->error(object_id, object_type,
-                                                 error_code, error_text);
+   static_cast<struct controller *>(data)->controller_error(
+      object_id, object_type, error_code, error_text);
 }
 
 constexpr struct ivi_controller_listener listener = {
@@ -184,30 +197,54 @@ constexpr struct ivi_controller_listener listener = {
 
 controller::controller(struct wl_registry *r, uint32_t name, uint32_t version)
    : wayland_proxy(
-        wl_registry_bind(r, name, &ivi_controller_interface, version)) {
+        wl_registry_bind(r, name, &ivi_controller_interface, version)),
+     surfaces{},
+     layers{},
+     screens{},
+     pending{},
+     output_size{} {
    ivi_controller_add_listener(this->proxy, &listener, this);
 }
 
 controller::~controller() {}
 
-void controller::screen(uint32_t id, struct ivi_controller_screen *screen) {
+void controller::layer_create(uint32_t id, int32_t w, int32_t h) {
+   this->layers[id] = std::make_unique<layer>(id, w, h, this);
+}
+
+void controller::surface_create(uint32_t id) {
+   this->surfaces[id] = std::make_unique<surface>(id, this);
+}
+
+void controller::controller_screen(uint32_t id,
+                                   struct ivi_controller_screen *screen) {
    logdebug("genivi::controller @ %p screen %u (%x) @ %p", this->proxy, id, id,
             screen);
    this->screens[id] = std::make_unique<struct screen>(id, this, screen);
 }
 
-void controller::layer(uint32_t id) {
+void controller::controller_layer(uint32_t id) {
    logdebug("genivi::controller @ %p layer %u (%x)", this->proxy, id, id);
    this->layers[id] = std::make_unique<struct layer>(id, this);
 }
 
-void controller::surface(uint32_t id) {
+void controller::controller_surface(uint32_t id) {
    logdebug("genivi::controller @ %p surface %u (%x)", this->proxy, id, id);
    this->surfaces[id] = std::make_unique<struct surface>(id, this);
+
+   add_task("fullscreen surface", [id](struct controller *c) {
+      c->surfaces[id]->set_destination_rectangle(0, 0, c->output_size.w,
+                                                 c->output_size.h);
+      c->surfaces[id]->set_visibility(1);
+      uint32_t lid = id == 0x16180 ? 1000 : 100;
+      c->layers[lid]->add_surface(c->surfaces[id].get());
+      c->layers[lid]->set_visibility(1);
+      logdebug("Surface %u now fullscreen on layer %u", id, lid);
+   });
 }
 
-void controller::error(int32_t object_id, int32_t object_type,
-                       int32_t error_code, const char *error_text) {
+void controller::controller_error(int32_t object_id, int32_t object_type,
+                                  int32_t error_code, const char *error_text) {
    logdebug("genivi::controller @ %p error o %i t %i c %i text %s", this->proxy,
             object_id, object_type, error_code, error_text);
 }
@@ -230,7 +267,8 @@ void layer_opacity(void *data,
                    struct ivi_controller_layer *ivi_controller_layer,
                    wl_fixed_t opacity) {
    static_cast<struct layer *>(data)->parent->layer_opacity(
-      static_cast<struct layer *>(data)->id, wl_fixed_to_double(opacity));
+      static_cast<struct layer *>(data)->id,
+      float(wl_fixed_to_double(opacity)));
 }
 
 void layer_source_rectangle(void *data,
@@ -282,9 +320,17 @@ constexpr struct ivi_controller_layer_listener layer_listener = {
 };
 }
 
-layer::layer(uint32_t i, struct controller *c)
-   : wayland_proxy(ivi_controller_layer_create(c->proxy, i, 0, 0)),
-     controlled_entity(c, i) {
+layer::layer(uint32_t i, struct controller *c) : layer(i, 0, 0, c) {}
+
+layer::layer(uint32_t i, int32_t w, int32_t h, struct controller *c)
+   : wayland_proxy(ivi_controller_layer_create(c->proxy, i, w, h)),
+     controlled_entity(c, i),
+     dst_rect{},
+     src_rect{},
+     size{},
+     orientation{},
+     visibility{},
+     opacity{} {
    ivi_controller_layer_add_listener(this->proxy, &layer_listener, this);
 }
 
@@ -296,32 +342,38 @@ layer::~layer() {
 
 void controller::layer_visibility(uint32_t id, int32_t visibility) {
    logdebug("genivi::layer %s @ %p v %i", __func__, this->proxy, visibility);
+   this->layers[id]->visibility = visibility;
 }
 
 void controller::layer_opacity(uint32_t id, float opacity) {
    logdebug("genivi::layer %s @ %p o %f", __func__, this->proxy, opacity);
+   this->layers[id]->opacity = opacity;
 }
 
 void controller::layer_source_rectangle(uint32_t id, int32_t x, int32_t y,
                                         int32_t width, int32_t height) {
    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
             x, y, width, height);
+   this->layers[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
 }
 
 void controller::layer_destination_rectangle(uint32_t id, int32_t x, int32_t y,
                                              int32_t width, int32_t height) {
    logdebug("genivi::layer %s @ %p x %i y %i w %i h %i", __func__, this->proxy,
             x, y, width, height);
+   this->layers[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
 }
 
 void controller::layer_configuration(uint32_t id, int32_t width,
                                      int32_t height) {
    logdebug("genivi::layer %s @ %p w %i h %i", __func__, this->proxy, width,
             height);
+   this->layers[id]->size = size{uint32_t(width), uint32_t(height)};
 }
 
 void controller::layer_orientation(uint32_t id, int32_t orientation) {
    logdebug("genivi::layer %s @ %p o %i", __func__, this->proxy, orientation);
+   this->layers[id]->orientation = orientation;
 }
 
 void controller::layer_screen(uint32_t id, struct wl_output *screen) {
@@ -351,7 +403,8 @@ void surface_opacity(void *data,
                      struct ivi_controller_surface *ivi_controller_surface,
                      wl_fixed_t opacity) {
    static_cast<struct surface *>(data)->parent->surface_opacity(
-      static_cast<struct surface *>(data)->id, wl_fixed_to_double(opacity));
+      static_cast<struct surface *>(data)->id,
+      float(wl_fixed_to_double(opacity)));
 }
 
 void surface_source_rectangle(
@@ -436,7 +489,13 @@ constexpr struct ivi_controller_surface_listener surface_listener = {
 
 surface::surface(uint32_t i, struct controller *c)
    : wayland_proxy(ivi_controller_surface_create(c->proxy, i)),
-     controlled_entity(c, i) {
+     controlled_entity(c, i),
+     dst_rect{},
+     src_rect{},
+     size{},
+     orientation{},
+     visibility{},
+     opacity{1.f} {
    ivi_controller_surface_add_listener(this->proxy, &surface_listener, this);
 }
 
@@ -448,16 +507,19 @@ surface::~surface() {
 
 void controller::surface_visibility(uint32_t id, int32_t visibility) {
    logdebug("genivi::surface %s @ %p v %i", __func__, this->proxy, visibility);
+   this->surfaces[id]->visibility = visibility;
 }
 
 void controller::surface_opacity(uint32_t id, float opacity) {
    logdebug("genivi::surface %s @ %p o %f", __func__, this->proxy, opacity);
+   this->surfaces[id]->opacity = opacity;
 }
 
 void controller::surface_source_rectangle(uint32_t id, int32_t x, int32_t y,
                                           int32_t width, int32_t height) {
    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
             this->proxy, x, y, width, height);
+   this->surfaces[id]->src_rect = rect{uint32_t(width), uint32_t(height), x, y};
 }
 
 void controller::surface_destination_rectangle(uint32_t id, int32_t x,
@@ -465,16 +527,19 @@ void controller::surface_destination_rectangle(uint32_t id, int32_t x,
                                                int32_t height) {
    logdebug("genivi::surface %s @ %p x %i y %i w %i h %i", __func__,
             this->proxy, x, y, width, height);
+   this->surfaces[id]->dst_rect = rect{uint32_t(width), uint32_t(height), x, y};
 }
 
 void controller::surface_configuration(uint32_t id, int32_t width,
                                        int32_t height) {
    logdebug("genivi::surface %s @ %p w %i h %i", __func__, this->proxy, width,
             height);
+   this->surfaces[id]->size = size{uint32_t(width), uint32_t(height)};
 }
 
 void controller::surface_orientation(uint32_t id, int32_t orientation) {
    logdebug("genivi::surface %s @ %p o %i", __func__, this->proxy, orientation);
+   this->surfaces[id]->orientation = orientation;
 }
 
 void controller::surface_pixelformat(uint32_t id, int32_t pixelformat) {
@@ -496,11 +561,16 @@ void controller::surface_stats(uint32_t id, uint32_t redraw_count,
 
 void controller::surface_destroyed(uint32_t id) {
    logdebug("genivi::surface %s @ %p", __func__, this->proxy);
+   this->surfaces.erase(id);
 }
 
 void controller::surface_content(uint32_t id, int32_t content_state) {
    logdebug("genivi::surface %s @ %p s %i", __func__, this->proxy,
             content_state);
+   if (content_state == IVI_CONTROLLER_SURFACE_CONTENT_STATE_CONTENT_REMOVED) {
+      add_task("remove surface",
+               [id](struct controller *c) { c->surfaces.erase(id); });
+   }
 }
 
 //
@@ -511,5 +581,7 @@ void controller::surface_content(uint32_t id, int32_t content_state) {
 //
 screen::screen(uint32_t i, struct controller *c,
                struct ivi_controller_screen *p)
-   : wayland_proxy(p), controlled_entity(c, i) {}
+   : wayland_proxy(p), controlled_entity(c, i) {
+   logdebug("genivi::screen @ %p id %u", p, i);
+}
 }