From c14f86a894dc019b22beb1054f78551f94f572dd Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Thu, 13 Jun 2024 03:41:32 +0200 Subject: [PATCH] core, wl: Factor out view fullscreening Move view fullscreening to new vfuncs in the CogView class, which get in turn implemented by CogWlView. This moves the entry point to common code, which would allow other platform plug-ins to implement the functionality if desired. Also, handling its key binding can be moved along to common code. --- core/cog-view.c | 52 ++++++++++++++++++++++++++++++ core/cog-view.h | 8 ++++- platform/wayland/cog-platform-wl.c | 13 -------- platform/wayland/cog-view-wl.c | 27 ++++++++++++++++ 4 files changed, 86 insertions(+), 14 deletions(-) diff --git a/core/cog-view.c b/core/cog-view.c index d1a5db87..1ba991df 100644 --- a/core/cog-view.c +++ b/core/cog-view.c @@ -327,6 +327,10 @@ cog_view_try_handle_key_binding(CogView *self, const struct wpe_input_keyboard_e return TRUE; } + /* F11, toggle fullscreen */ + if (!event->modifiers && event->key_code == WPE_KEY_F11) + return cog_view_set_fullscreen(self, !cog_view_is_fullscreen(self)); + return FALSE; } @@ -499,3 +503,51 @@ cog_view_set_visible(CogView *self) return TRUE; } + +/** + * cog_view_set_fullscreen: + * @self: A view. + * @enable: Whether to enable fullscreening. + * + * Change the fullscreening status of the view. + * + * Note that not all platform plug-ins may implement view fullscreening, + * and in that case %FALSE is always returned. + * + * Returns: Whether the view fullscreening state was set to the requested one. + * + * Since: 0.20 + */ +gboolean +cog_view_set_fullscreen(CogView *self, gboolean enable) +{ + g_return_val_if_fail(COG_IS_VIEW(self), FALSE); + + CogViewClass *klass = COG_VIEW_GET_CLASS(self); + if (klass->set_fullscreen) + return (*klass->set_fullscreen)(self, enable); + + return FALSE; +} + +/** + * cog_view_is_fullscreen: + * @self: A view. + * + * Gets whether the view is fullscreened. + * + * Returns: Whether the view is fullscreened. + * + * Since: 0.20 + */ +gboolean +cog_view_is_fullscreen(CogView *self) +{ + g_return_val_if_fail(COG_IS_VIEW(self), FALSE); + + CogViewClass *klass = COG_VIEW_GET_CLASS(self); + if (klass->is_fullscreen) + return (*klass->is_fullscreen)(self); + + return FALSE; +} diff --git a/core/cog-view.h b/core/cog-view.h index f51474bd..70248fc3 100644 --- a/core/cog-view.h +++ b/core/cog-view.h @@ -29,7 +29,10 @@ struct _CogViewClass { /*< private >*/ WebKitWebViewClass parent_class; - WebKitWebViewBackend *(*create_backend)(CogView *); + /*< public >*/ + WebKitWebViewBackend *(*create_backend)(CogView *self); + gboolean (*set_fullscreen)(CogView *self, gboolean enable); + gboolean (*is_fullscreen)(CogView *self); }; #define COG_TYPE_VIEW_IMPL (cog_view_get_impl_type()) @@ -61,4 +64,7 @@ gboolean cog_view_is_visible(CogView *self); COG_API gboolean cog_view_set_visible(CogView *self); +COG_API gboolean cog_view_set_fullscreen(CogView *self, gboolean enable); +COG_API gboolean cog_view_is_fullscreen(CogView *self); + G_END_DECLS diff --git a/platform/wayland/cog-platform-wl.c b/platform/wayland/cog-platform-wl.c index ba047e00..997e73c4 100644 --- a/platform/wayland/cog-platform-wl.c +++ b/platform/wayland/cog-platform-wl.c @@ -579,19 +579,6 @@ handle_key_event(CogWlSeat *seat, uint32_t key, uint32_t state, uint32_t time) return; uint32_t keysym = xkb_state_key_get_one_sym(seat->xkb.state, key); - uint32_t unicode = xkb_state_key_get_utf32(seat->xkb.state, key); - - /* TODO: Move as much as possible from fullscreen handling to common code. */ - if (cog_view_get_use_key_bindings(view) && state == WL_KEYBOARD_KEY_STATE_PRESSED && seat->xkb.modifiers == 0 && - unicode == 0 && keysym == XKB_KEY_F11) { - if (viewport->window.is_fullscreen && viewport->window.was_fullscreen_requested_from_dom) { - struct wpe_view_backend *backend = cog_view_get_backend(view); - wpe_view_backend_dispatch_request_exit_fullscreen(backend); - return; - } - cog_wl_viewport_set_fullscreen(viewport, !viewport->window.is_fullscreen); - return; - } if (seat->xkb.compose_state != NULL && state == WL_KEYBOARD_KEY_STATE_PRESSED && xkb_compose_state_feed(seat->xkb.compose_state, keysym) == XKB_COMPOSE_FEED_ACCEPTED && diff --git a/platform/wayland/cog-view-wl.c b/platform/wayland/cog-view-wl.c index 8b489bc3..2b620b8c 100644 --- a/platform/wayland/cog-view-wl.c +++ b/platform/wayland/cog-view-wl.c @@ -46,6 +46,8 @@ G_DEFINE_DYNAMIC_TYPE(CogWlView, cog_wl_view, COG_TYPE_VIEW) static void cog_wl_view_clear_buffers(CogWlView *); static WebKitWebViewBackend *cog_wl_view_create_backend(CogView *); +static gboolean cog_wl_view_set_fullscreen(CogView *, gboolean); +static gboolean cog_wl_view_is_fullscreen(CogView *); static void cog_wl_view_dispose(GObject *); static bool cog_wl_view_handle_dom_fullscreen_request(void *, bool); static void cog_wl_view_shm_buffer_destroy(CogWlView *, struct shm_buffer *); @@ -89,6 +91,8 @@ cog_wl_view_class_init(CogWlViewClass *klass) CogViewClass *view_class = COG_VIEW_CLASS(klass); view_class->create_backend = cog_wl_view_create_backend; + view_class->set_fullscreen = cog_wl_view_set_fullscreen; + view_class->is_fullscreen = cog_wl_view_is_fullscreen; } static void @@ -195,6 +199,29 @@ cog_wl_view_create_backend(CogView *view) return wk_view_backend; } +gboolean +cog_wl_view_is_fullscreen(CogView *self) +{ + CogWlViewport *viewport = COG_WL_VIEWPORT(cog_view_get_viewport(self)); + return viewport->window.is_fullscreen ? TRUE : FALSE; +} + +gboolean +cog_wl_view_set_fullscreen(CogView *self, gboolean enable) +{ + if (cog_wl_view_is_fullscreen(self) == enable) + return TRUE; + + CogWlViewport *viewport = COG_WL_VIEWPORT(cog_view_get_viewport(self)); + if (viewport->window.is_fullscreen && viewport->window.was_fullscreen_requested_from_dom) { + struct wpe_view_backend *backend = cog_view_get_backend(COG_VIEW(self)); + wpe_view_backend_dispatch_request_exit_fullscreen(backend); + } else { + cog_wl_viewport_set_fullscreen(viewport, enable ? true : false); + } + return TRUE; +} + bool cog_wl_view_does_image_match_win_size(CogWlView *view) {