diya-shell/src/wayland.c

180 lines
5.7 KiB
C

#include <wayland-client-protocol.h>
#include <gdk/wayland/gdkwayland.h>
#include <assert.h>
#include "wlr-foreign-toplevel-management-unstable-v1.h"
#include "virtual-keyboard-unstable-v1.h"
#include "wayland.h"
struct _DiyaWayland
{
DiyaShellObject parent;
struct wl_compositor * compositor;
struct wl_shm * shm;
struct wl_seat * seat;
};
G_DEFINE_FINAL_TYPE(DiyaWayland, diya_wayland, DIYA_TYPE_SHELL_OBJECT)
static void diya_wayland_dispose(GObject* object)
{
(void) object;
//DiyaWayland * self = DIYA_WAYLAND(object);
g_debug("diya_wayland_dispose: %s", diya_object_to_string(object));
G_OBJECT_CLASS(diya_wayland_parent_class)->dispose(object);
}
static void diya_wayland_init(DiyaWayland * self)
{
self->compositor = NULL;
self->shm = NULL;
self->seat = NULL;
}
static const gchar* diya_wayland_to_string(DiyaObject* object)
{
(void) object;
return "DiyaWayland - wayland client handle object";
}
static void diya_wayland_class_init(DiyaWaylandClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(class);
DiyaObjectClass *base_class = DIYA_OBJECT_CLASS(class);
gobject_class->dispose = diya_wayland_dispose;
//gobject_class->set_property = diya_lock_session_set_property;
//gobject_class->get_property = diya_lock_session_get_property;
base_class->to_string = diya_wayland_to_string;
}
static void handle_global(void *data, struct wl_registry *registry, uint32_t name, const gchar *interface, uint32_t version)
{
/*if (!g_strcmp0(interface, zxdg_output_manager_v1_interface.name))
xdg_output_register(registry, name);
else if (!g_strcmp0(interface, wl_shm_interface.name))
shm_register(registry, name);
else if (!g_strcmp0(interface, zwlr_layer_shell_v1_interface.name))
layer_shell_register(registry, name, version);
if (!g_strcmp0(interface,zwlr_foreign_toplevel_manager_v1_interface.name))
foreign_toplevel_register(registry,name);
*/
(void) version;
DiyaShell * shell = data;
DiyaShellClass * class = DIYA_SHELL_GET_CLASS(shell);
DiyaWayland * wayland;
g_object_get(shell, DIYA_PROP_SHELL_WAYLAND, &wayland, NULL);
assert(DIYA_IS_WAYLAND(wayland));
g_debug("WAYLAND GLOBAL: %s", interface);
if (g_strcmp0(interface, zwlr_foreign_toplevel_manager_v1_interface.name) == 0)
{
if(class->foreign_register)
{
//diya_session_shell_foreign_toplevel_register(registry, name, data);
g_debug("Wayland: register shell foreign top level manager");
class->foreign_register(registry, name, data);
}
}
if (g_strcmp0(interface, zwp_virtual_keyboard_manager_v1_interface.name) == 0)
{
if(class->virtual_keyboard_register)
{
//diya_session_shell_foreign_toplevel_register(registry, name, data);
g_debug("Wayland: register virtual keyboard manager");
class->virtual_keyboard_register(registry, name, data);
}
}
/*
else if(!g_strcmp0(interface, ext_session_lock_manager_v1_interface.name))
{
diya_session_shell_session_lock_register(registry, name);
}
*/
else if (strcmp(interface, wl_compositor_interface.name) == 0)
{
wayland->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4);
}
else if (strcmp(interface, wl_shm_interface.name) == 0)
{
wayland->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
}
else if (strcmp(interface, wl_seat_interface.name) == 0)
{
wayland->seat = wl_registry_bind(registry, name, &wl_seat_interface, 7);
}
}
static void handle_global_remove(void *data, struct wl_registry *registry,
uint32_t name)
{
(void) data;
(void) registry;
(void) name;
}
static const struct wl_registry_listener registry_listener =
{
.global = handle_global,
.global_remove = handle_global_remove
};
DiyaWayland* diya_wayland_new(DiyaShell *shell)
{
struct wl_display *display;
struct wl_registry *registry;
display = gdk_wayland_display_get_wl_display(gdk_display_get_default());
if (!display)
{
g_error("Can't get wayland display");
return NULL;
}
DiyaWayland * wayland = g_object_new(DIYA_TYPE_WAYLAND, "shell", shell, NULL);
//g_object_set(wayland, "shell", shell, NULL);
registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, (void*)shell);
// wl_display_roundtrip(display);
// wayland_monitor_probe();
// GdkMonitor *mon = wayland_monitor_get_default();
// g_info("default output: %s", (gchar *)g_object_get_data(G_OBJECT(mon), "xdg_name"));
// wl_display_roundtrip(display);
// wl_display_roundtrip(display);
return wayland;
}
struct wl_surface* diya_wayland_create_surface(DiyaWayland * self)
{
assert(self->compositor);
struct wl_surface* surface = wl_compositor_create_surface(self->compositor);
assert(surface);
return surface;
}
struct wl_output* diya_wayland_get_output(DiyaWayland * self, int index)
{
(void) self;
/**
* TODO: listen to wl_ouput interface instead of using gdk
*/
GListModel * list = gdk_display_get_monitors(gdk_display_get_default());
GdkMonitor * monitor = g_list_model_get_item(list,index);
if(! monitor)
{
return NULL;
}
struct wl_output * output = gdk_wayland_monitor_get_wl_output(monitor);
assert(output);
return output;
}
struct wl_shm_pool * diya_wayland_create_shm_pool(DiyaWayland * self, int fd, guint size)
{
assert(self->shm);
return wl_shm_create_pool(self->shm, fd, size);
}
struct wl_seat* diya_wayland_get_seat(DiyaWayland* self)
{
assert(self->seat);
return self->seat;
}