From 888dc5f5c5456d2721f87f01dd1b10d47c6f439e Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:25:39 +0100 Subject: [PATCH] Have libdecor-gtk implement the "GTK Shell" protocol --- libdecor/build/Makefile | 12 +- libdecor/build/fl_libdecor-plugins.c | 115 +++++++++++++++++- libdecor/build/gtk-shell.xml | 109 +++++++++++++++++ src/CMakeLists.txt | 11 ++ src/Makefile | 1 + .../Wayland/Fl_Wayland_Screen_Driver.cxx | 4 + .../Wayland/Fl_Wayland_Window_Driver.cxx | 3 +- 7 files changed, 250 insertions(+), 5 deletions(-) create mode 100644 libdecor/build/gtk-shell.xml diff --git a/libdecor/build/Makefile b/libdecor/build/Makefile index 25116f4c0..946ec6318 100644 --- a/libdecor/build/Makefile +++ b/libdecor/build/Makefile @@ -18,7 +18,7 @@ include ../../makeinclude OBJECTS = fl_libdecor.o libdecor-cairo-blur.o fl_libdecor-plugins.o \ ../../src/xdg-decoration-protocol.o ../../src/xdg-shell-protocol.o \ - ../../src/text-input-protocol.o cursor-settings.o os-compatibility.o + ../../src/text-input-protocol.o ../../src/gtk-shell-protocol.o cursor-settings.o os-compatibility.o PROTOCOLS = `pkg-config --variable=pkgdatadir wayland-protocols` @@ -38,7 +38,7 @@ all : $(OBJECTS) depend: : echo "libdecor/build: make depend..." -fl_libdecor.o : fl_libdecor.c ../src/libdecor.c ../../src/xdg-shell-protocol.c ../../src/xdg-decoration-protocol.c ../../src/text-input-protocol.c +fl_libdecor.o : fl_libdecor.c ../src/libdecor.c ../../src/xdg-shell-protocol.c ../../src/xdg-decoration-protocol.c ../../src/text-input-protocol.c ../../src/gtk-shell-protocol.c $(CC) $(CFLAGS) $(CFLAGS_DECOR) -c fl_libdecor.c -DLIBDECOR_PLUGIN_API_VERSION=1 -DLIBDECOR_PLUGIN_DIR=\"\" fl_libdecor-plugins.o : fl_libdecor-plugins.c ../src/plugins/cairo/libdecor-cairo.c @@ -75,10 +75,16 @@ cursor-settings.o : ../src/cursor-settings.c $(PROTOCOLS)/unstable/text-input/text-input-unstable-v3.xml \ ../../src/text-input-client-protocol.h +../../src/gtk-shell-protocol.c : + wayland-scanner private-code \ + gtk-shell.xml ../../src/gtk-shell-protocol.c + wayland-scanner client-header \ + gtk-shell.xml ../../src/gtk-shell-client-protocol.h + install: echo "Nothing to install" uninstall: clean: - $(RM) *.o ../../src/xdg-*.c ../../src/xdg-*.h ../../src/xdg-*.o ../../src/text-input-* + $(RM) *.o ../../src/xdg-*.c ../../src/xdg-*.h ../../src/xdg-*.o ../../src/text-input-* ../../src/gtk-shell-* diff --git a/libdecor/build/fl_libdecor-plugins.c b/libdecor/build/fl_libdecor-plugins.c index 701ba7abb..e25f79285 100644 --- a/libdecor/build/fl_libdecor-plugins.c +++ b/libdecor/build/fl_libdecor-plugins.c @@ -27,6 +27,10 @@ #include "../src/libdecor.h" #include +#ifndef HAVE_GTK +# define HAVE_GTK 0 +#endif + #if USE_SYSTEM_LIBDECOR #include "../src/libdecor-plugin.h" enum zxdg_toplevel_decoration_v1_mode { @@ -55,7 +59,7 @@ struct buffer { // identical in libdecor-cairo.c and libdecor-gtk.c const struct libdecor_plugin_description *fl_libdecor_plugin_description = NULL; -# ifdef HAVE_GTK +# if HAVE_GTK # include # include "../src/plugins/gtk/libdecor-gtk.c" # else @@ -316,3 +320,112 @@ unsigned char *fl_libdecor_titlebar_buffer(struct libdecor_frame *frame, } return NULL; } + + +/* === Beginning of code to add support of GTK Shell to libdecor-gtk === */ +#if HAVE_GTK && !USE_SYSTEM_LIBDECOR + +# include "gtk-shell-client-protocol.h" + +static struct gtk_shell1 *gtk_shell = NULL; + +// libdecor's button member of wl_pointer_listener objects +static void (*decor_pointer_button)(void*, struct wl_pointer *, + uint32_t , + uint32_t , + uint32_t , + uint32_t); + +// FLTK's replacement for button member of wl_pointer_listener objects +static void fltk_pointer_button(void *data, + struct wl_pointer *wl_pointer, + uint32_t serial, + uint32_t time, + uint32_t button, + uint32_t state) { + struct seat *seat = data; + struct libdecor_frame_gtk *frame_gtk; + if (!seat->pointer_focus || !own_surface(seat->pointer_focus)) + return; + + frame_gtk = wl_surface_get_user_data(seat->pointer_focus); + if (!frame_gtk) + return; + + if (button == BTN_MIDDLE && state == WL_POINTER_BUTTON_STATE_PRESSED) { + struct gtk_surface1 *gtk_surface = gtk_shell1_get_gtk_surface(gtk_shell, + frame_gtk->headerbar.wl_surface); + gtk_surface1_titlebar_gesture(gtk_surface, serial, + seat->wl_seat, GTK_SURFACE1_GESTURE_MIDDLE_CLICK); + gtk_surface1_release(gtk_surface); + return; + } else if (button == BTN_RIGHT && state == WL_POINTER_BUTTON_STATE_PRESSED) { + struct gtk_surface1 *gtk_surface = gtk_shell1_get_gtk_surface(gtk_shell, + frame_gtk->headerbar.wl_surface); + gtk_surface1_titlebar_gesture(gtk_surface, serial, + seat->wl_seat, GTK_SURFACE1_GESTURE_RIGHT_CLICK); + gtk_surface1_release(gtk_surface); + return; + } else if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) { + static uint32_t previous_click = 0; + if (previous_click && time - previous_click < 250) { // < 0.25 sec for dble clicks + previous_click = 0; + struct gtk_surface1 *gtk_surface = gtk_shell1_get_gtk_surface(gtk_shell, + frame_gtk->headerbar.wl_surface); + gtk_surface1_titlebar_gesture(gtk_surface, serial, + seat->wl_seat, GTK_SURFACE1_GESTURE_DOUBLE_CLICK); + gtk_surface1_release(gtk_surface); + return; + } + previous_click = time; + } + decor_pointer_button(data, wl_pointer, serial, time, button, state); +} + + +struct wl_object { // copied from wayland-private.h + const struct wl_interface *interface; + const void *implementation; + uint32_t id; +}; + +#endif // HAVE_GTK && !USE_SYSTEM_LIBDECOR + + +// replace libdecor's pointer_button by FLTK's +void use_FLTK_pointer_button(struct libdecor_frame *frame) { +#if HAVE_GTK && !USE_SYSTEM_LIBDECOR + static struct wl_pointer_listener *fltk_listener = NULL; + if (!gtk_shell || fltk_listener) return; + struct libdecor_frame_gtk *lfg = (struct libdecor_frame_gtk *)frame; + if (wl_list_empty(&lfg->plugin_gtk->seat_list)) return; + struct seat *seat; + wl_list_for_each(seat, &lfg->plugin_gtk->seat_list, link) { + break; + } + struct wl_object *object = (struct wl_object *)seat->wl_pointer; + if (!object) return; + struct wl_pointer_listener *decor_listener = + (struct wl_pointer_listener*)object->implementation; + fltk_listener = + (struct wl_pointer_listener*)malloc(sizeof(struct wl_pointer_listener)); + // initialize FLTK's listener with libdecor's values + *fltk_listener = *decor_listener; + // memorize libdecor's button cb + decor_pointer_button = decor_listener->button; + // replace libdecor's button by FLTK's + fltk_listener->button = fltk_pointer_button; + // replace the pointer listener by a copy whose button member is FLTK's + object->implementation = fltk_listener; +#endif // HAVE_GTK && !USE_SYSTEM_LIBDECOR +} + + +void bind_to_gtk_shell(struct wl_registry *wl_registry, uint32_t id) { +#if HAVE_GTK && !USE_SYSTEM_LIBDECOR + gtk_shell = (struct gtk_shell1*)wl_registry_bind(wl_registry, id, + >k_shell1_interface, 5); +#endif // HAVE_GTK && !USE_SYSTEM_LIBDECOR +} + +/* === End of code to add support of GTK Shell to libdecor-gtk === */ diff --git a/libdecor/build/gtk-shell.xml b/libdecor/build/gtk-shell.xml new file mode 100644 index 000000000..f00684679 --- /dev/null +++ b/libdecor/build/gtk-shell.xml @@ -0,0 +1,109 @@ + + + + + + gtk_shell is a protocol extension providing additional features for + clients implementing it. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ea4f89cc1..0db9631be 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -688,6 +688,17 @@ if (UNIX AND OPTION_USE_WAYLAND) ) list (APPEND STATIC_FILES "xdg-decoration-protocol.c") list (APPEND SHARED_FILES "xdg-decoration-protocol.c") + if (GTK_FOUND AND OPTION_ALLOW_GTK_PLUGIN) + add_custom_command( + OUTPUT gtk-shell-protocol.c gtk-shell-client-protocol.h + COMMAND wayland-scanner private-code ${CMAKE_CURRENT_SOURCE_DIR}/../libdecor/build/gtk-shell.xml gtk-shell-protocol.c + COMMAND wayland-scanner client-header ${CMAKE_CURRENT_SOURCE_DIR}/../libdecor/build/gtk-shell.xml gtk-shell-client-protocol.h + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/../libdecor/build/gtk-shell.xml + VERBATIM + ) + list (APPEND STATIC_FILES "gtk-shell-protocol.c") + list (APPEND SHARED_FILES "gtk-shell-protocol.c") + endif (GTK_FOUND AND OPTION_ALLOW_GTK_PLUGIN) endif (NOT OPTION_USE_SYSTEM_LIBDECOR) add_custom_command( OUTPUT text-input-protocol.c text-input-client-protocol.h diff --git a/src/Makefile b/src/Makefile index de1174e16..a8468e3b3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -424,6 +424,7 @@ CFILES_WAYLANDX11 = $(WLCFILES) EXTRA_OBJECTS_WAYLAND = ../libdecor/build/fl_libdecor.o ../libdecor/build/libdecor-cairo-blur.o \ ../libdecor/build/fl_libdecor-plugins.o \ xdg-decoration-protocol.o xdg-shell-protocol.o text-input-protocol.o \ + gtk-shell-protocol.o \ ../libdecor/build/cursor-settings.o ../libdecor/build/os-compatibility.o EXTRA_OBJECTS_WAYLANDX11 = $(EXTRA_OBJECTS_WAYLAND) EXTRA_CXXFLAGS_WAYLAND = -I. diff --git a/src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx b/src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx index 35a253c38..5b4d97cf2 100644 --- a/src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx +++ b/src/drivers/Wayland/Fl_Wayland_Screen_Driver.cxx @@ -46,6 +46,7 @@ #include // for strerror() extern "C" { bool libdecor_get_cursor_settings(char **theme, int *size); + void bind_to_gtk_shell(struct wl_registry *, uint32_t); } @@ -1146,6 +1147,9 @@ static void registry_handle_global(void *user_data, struct wl_registry *wl_regis } else if (strcmp(interface, "gtk_shell1") == 0) { Fl_Wayland_Screen_Driver::compositor = Fl_Wayland_Screen_Driver::MUTTER; //fprintf(stderr, "Running the Mutter compositor\n"); + if ( version >= 5) { + bind_to_gtk_shell(wl_registry, id); + } } else if (strcmp(interface, "weston_desktop_shell") == 0) { Fl_Wayland_Screen_Driver::compositor = Fl_Wayland_Screen_Driver::WESTON; //fprintf(stderr, "Running the Weston compositor\n"); diff --git a/src/drivers/Wayland/Fl_Wayland_Window_Driver.cxx b/src/drivers/Wayland/Fl_Wayland_Window_Driver.cxx index 1c04bf575..6b5b2f8ab 100644 --- a/src/drivers/Wayland/Fl_Wayland_Window_Driver.cxx +++ b/src/drivers/Wayland/Fl_Wayland_Window_Driver.cxx @@ -45,6 +45,7 @@ struct cursor_image { // as in wayland-cursor.c of the Wayland project source co extern "C" { # include "../../../libdecor/src/libdecor-plugin.h" uchar *fl_libdecor_titlebar_buffer(struct libdecor_frame *frame, int *w, int *h, int *stride); + void use_FLTK_pointer_button(struct libdecor_frame *); } #define fl_max(a,b) ((a) > (b) ? (a) : (b)) @@ -853,7 +854,7 @@ static void handle_configure(struct libdecor_frame *frame, #ifdef LIBDECOR_MR131 if (is_1st_run) use_FLTK_toplevel_configure_cb(frame); #endif - + use_FLTK_pointer_button(frame); struct wl_output *wl_output = NULL; if (window->fl_win->fullscreen_active()) { if (!(window->state & LIBDECOR_WINDOW_STATE_FULLSCREEN)) {