From 59a232c1613c08a98d4a13238c0189f1b660e3e5 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 16 Aug 2015 14:36:32 +0100 Subject: [PATCH] GTK3 port: use gdk_device_grab() in gtkask.c. This replaces the old gdk_keyboard_grab(), and is what a GTK3 app has to use for grabbing the keyboard away from all other X clients. --- unix/gtkask.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/unix/gtkask.c b/unix/gtkask.c index 4ee1c8ed..b58cc42b 100644 --- a/unix/gtkask.c +++ b/unix/gtkask.c @@ -42,6 +42,9 @@ struct askpass_ctx { #endif char *passphrase; int passlen, passsize; +#if GTK_CHECK_VERSION(3,0,0) + GdkDevice *keyboard; /* for gdk_device_grab */ +#endif }; static void visually_acknowledge_keypress(struct askpass_ctx *ctx) @@ -212,8 +215,45 @@ static gint expose_area(GtkWidget *widget, GdkEventExpose *event, static int try_grab_keyboard(struct askpass_ctx *ctx) { - int ret = gdk_keyboard_grab(gtk_widget_get_window(ctx->dialog), - FALSE, GDK_CURRENT_TIME); + int ret; + +#if GTK_CHECK_VERSION(3,0,0) + /* + * Grabbing the keyboard is quite complicated in GTK 3. + */ + GdkDeviceManager *dm; + GdkDevice *pointer, *keyboard; + + dm = gdk_display_get_device_manager + (gtk_widget_get_display(ctx->dialog)); + if (!dm) + return FALSE; + + pointer = gdk_device_manager_get_client_pointer(dm); + if (!pointer) + return FALSE; + keyboard = gdk_device_get_associated_device(pointer); + if (!keyboard) + return FALSE; + if (gdk_device_get_source(keyboard) != GDK_SOURCE_KEYBOARD) + return FALSE; + + ctx->keyboard = keyboard; + ret = gdk_device_grab(ctx->keyboard, + gtk_widget_get_window(ctx->dialog), + GDK_OWNERSHIP_NONE, + TRUE, + GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, + NULL, + GDK_CURRENT_TIME); +#else + /* + * It's much simpler in GTK 1 and 2! + */ + ret = gdk_keyboard_grab(gtk_widget_get_window(ctx->dialog), + FALSE, GDK_CURRENT_TIME); +#endif + return ret == GDK_GRAB_SUCCESS; } @@ -361,7 +401,11 @@ static const char *gtk_askpass_setup(struct askpass_ctx *ctx, static void gtk_askpass_cleanup(struct askpass_ctx *ctx) { +#if GTK_CHECK_VERSION(3,0,0) + gdk_device_ungrab(ctx->keyboard, GDK_CURRENT_TIME); +#else gdk_keyboard_ungrab(GDK_CURRENT_TIME); +#endif gtk_grab_remove(ctx->promptlabel); if (ctx->passphrase) { -- 2.45.2