Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions platform/linux/WebViewWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stdexcept>
#include <string>

Expand All @@ -20,6 +21,8 @@ struct NativeWindow {
WebKitUserContentManager* manager = nullptr;
// Set while the user drags or resizes the window: the window follows the pointer, and the frame it began with is kept here.
bool snapping = false;
// Set between a press and its release on the window, since a compositor may not report the button as held.
bool buttonDown = false;
ScreenRect dragStart;
// The pointer where the drag began, and whether it resizes from edge.
int pressX = 0;
Expand Down Expand Up @@ -88,6 +91,9 @@ struct GtkApi {
void* (*cairo_region_create)() = nullptr;
void (*cairo_region_destroy)(void*) = nullptr;
guint32 (*x11_server_time)(GdkWindow*) = nullptr;
GdkDisplay* (*window_get_display)(GdkWindow*) = nullptr;
gsize (*x11_display_get_type)() = nullptr;
gboolean (*type_check_instance_is_a)(void*, gsize) = nullptr;
void (*window_begin_resize_drag)(GtkWindow*, GdkWindowEdge, gint, gint, gint, guint32) = nullptr;
void (*window_begin_move_drag)(GtkWindow*, gint, gint, gint, guint32) = nullptr;
void (*window_iconify)(GtkWindow*) = nullptr;
Expand Down Expand Up @@ -228,6 +234,10 @@ GtkApi loadApi() {
}
// Only on X11. Without it the focus request carries no time, and the window manager may refuse it.
api.x11_server_time = reinterpret_cast<decltype(api.x11_server_time)>(dlsym(library, "gdk_x11_get_server_time"));
api.window_get_display = reinterpret_cast<decltype(api.window_get_display)>(dlsym(library, "gdk_window_get_display"));
api.x11_display_get_type = reinterpret_cast<decltype(api.x11_display_get_type)>(dlsym(library, "gdk_x11_display_get_type"));
api.type_check_instance_is_a =
reinterpret_cast<decltype(api.type_check_instance_is_a)>(dlsym(library, "g_type_check_instance_is_a"));
bind(api.widget_hide, library, "gtk_widget_hide");
bind(api.widget_destroy, library, "gtk_widget_destroy");
bind(api.main_loop, library, "gtk_main");
Expand Down Expand Up @@ -279,8 +289,17 @@ const GtkApi& api() {
return loaded;
}

// A Wayland client can't place or read its own window, so under Wayland the windows run through XWayland, where they can.
// Must run before GTK initialises, which fixes the backend.
void preferXWayland() {
if (!getenv("GDK_BACKEND") && getenv("WAYLAND_DISPLAY") && getenv("DISPLAY")) {
setenv("GDK_BACKEND", "x11", 0);
}
}

void ensureGtk() {
static const bool initialized = [] {
preferXWayland();
if (!api().init_check(nullptr, nullptr)) {
throw std::runtime_error("GTK couldn't open a display.");
}
Expand All @@ -294,11 +313,20 @@ GdkPixbuf* applicationIcon = nullptr;

// The X server's time, which a window manager needs on a focus or move request. GDK_CURRENT_TIME when it isn't available.
guint32 serverTime(GtkWidget* widget) {
if (!api().x11_server_time) {
const GtkApi& gtk = api();
if (!gtk.x11_server_time || !gtk.window_get_display || !gtk.x11_display_get_type || !gtk.type_check_instance_is_a) {
return GDK_CURRENT_TIME;
}
GdkWindow* gdkWindow = gtk.widget_get_window(widget);
if (!gdkWindow) {
return GDK_CURRENT_TIME;
}
// The symbol exists under Wayland too, and calling it there dereferences a null X display.
GdkDisplay* display = gtk.window_get_display(gdkWindow);
if (!gtk.type_check_instance_is_a(display, gtk.x11_display_get_type())) {
return GDK_CURRENT_TIME;
}
GdkWindow* gdkWindow = api().widget_get_window(widget);
return gdkWindow ? api().x11_server_time(gdkWindow) : GDK_CURRENT_TIME;
return gtk.x11_server_time(gdkWindow);
}

// Shows the window once its page has reported ready, if it's wanted yet. Focus goes to it only if it was asked for.
Expand Down Expand Up @@ -338,6 +366,16 @@ gboolean onFocusOut(GtkWidget*, GdkEvent*, gpointer data) {
return FALSE;
}

gboolean onButtonPress(GtkWidget*, GdkEvent*, gpointer data) {
static_cast<NativeWindow*>(data)->buttonDown = true;
return FALSE;
}

gboolean onButtonRelease(GtkWidget*, GdkEvent*, gpointer data) {
static_cast<NativeWindow*>(data)->buttonDown = false;
return FALSE;
}

// WebKit runs the page in a process of its own, which can die. The page is loaded again from the window's own page, since
// it asks the host for its state as it loads, and the user is told why the page blanked for a moment.
gboolean onWebProcessCrashed(WebKitWebView* view, gpointer data) {
Expand Down Expand Up @@ -564,6 +602,10 @@ void WebViewWindow::platformOpen() {
static_cast<GConnectFlags>(0));
gtk.g_signal_connect_data(native.window, "focus-out-event", reinterpret_cast<GCallback>(onFocusOut), this, nullptr,
static_cast<GConnectFlags>(0));
gtk.g_signal_connect_data(native.window, "button-press-event", reinterpret_cast<GCallback>(onButtonPress), &native, nullptr,
static_cast<GConnectFlags>(0));
gtk.g_signal_connect_data(native.window, "button-release-event", reinterpret_cast<GCallback>(onButtonRelease), &native, nullptr,
static_cast<GConnectFlags>(0));

const std::string url = devUrl();
if (!url.empty()) {
Expand Down Expand Up @@ -608,10 +650,10 @@ int snapToGrid(int logical, int scale) {
}

// The pointer's position on the screen, and whether the left button is held.
bool pointerState(int& x, int& y) {
bool pointerState(const NativeWindow& native, int& x, int& y) {
guint mask = 0;
api().display_get_pointer(api().display_get_default(), nullptr, &x, &y, &mask);
return (mask & kButton1Mask) != 0;
return (mask & kButton1Mask) != 0 || native.buttonDown;
}

// The window's frame as GTK reports it, in logical pixels.
Expand Down Expand Up @@ -739,7 +781,7 @@ gboolean onDragPoll(gpointer data) {
auto* native = static_cast<NativeWindow*>(data);
int x = 0;
int y = 0;
if (pointerState(x, y)) {
if (pointerState(*native, x, y)) {
if (native->manual && native->window) {
applyTarget(native, dragTarget(*native, x - native->pressX, y - native->pressY));
}
Expand All @@ -756,7 +798,7 @@ gboolean onDragPoll(gpointer data) {
void beginDrag(NativeWindow* native, bool resizing, ResizeEdge edge, bool manual) {
int x = 0;
int y = 0;
if (!native->window || !pointerState(x, y)) {
if (!native->window || !pointerState(*native, x, y)) {
return;
}
native->resizing = resizing;
Expand Down Expand Up @@ -947,6 +989,7 @@ std::optional<RuntimeProblem> missingWindowingRuntime() {
return RuntimeProblem{"WebKitGTK 4.1 isn't installed, so the windows can't open. Install it (libwebkit2gtk-4.1-0 on Debian and Ubuntu, webkit2gtk4.1 on Fedora, webkit2gtk-4.1 on Arch) and start the application again.", ""};
}
try {
preferXWayland();
if (!api().init_check(nullptr, nullptr)) {
return RuntimeProblem{"GTK couldn't open a display. Start the application from a desktop session.", ""};
}
Expand Down
11 changes: 9 additions & 2 deletions tests/linux_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,16 @@ last_state() {
grep "^visible=" "$work/out.txt" | tail -n 1
}

# Whether the window is on screen, as the window manager reports it.
# Whether the window is on screen. On Wayland xdotool can't see the window, so the example is asked for its visible flag.
window_shown() {
xdotool search --onlyvisible --name "^${title}$" 2>/dev/null | head -n 1 | grep -q .
if [[ -n "${WAYLAND_DISPLAY:-}" ]]; then
local before
before="$(lines_so_far)"
echo report >&3
wait_for_line_after "$before" '^visible=' && last_state | grep -q '^visible=1 '
else
xdotool search --onlyvisible --name "^${title}$" 2>/dev/null | head -n 1 | grep -q .
fi
}

# Waits until the window is shown or hidden on screen.
Expand Down
Loading