diff options
Diffstat (limited to 'src/utilities/focusGrabber.vala')
-rw-r--r-- | src/utilities/focusGrabber.vala | 85 |
1 files changed, 49 insertions, 36 deletions
diff --git a/src/utilities/focusGrabber.vala b/src/utilities/focusGrabber.vala index 4a3212f..8424f12 100644 --- a/src/utilities/focusGrabber.vala +++ b/src/utilities/focusGrabber.vala @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////// -// Copyright (c) 2011-2016 by Simon Schneegans +// Copyright (c) 2011-2017 by Simon Schneegans // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -28,18 +28,16 @@ public class FocusGrabber : GLib.Object { /// Code roughly from Gnome-Do/Synapse. ///////////////////////////////////////////////////////////////////// - public static void grab(Gdk.Window window, bool keyboard = true, bool pointer = true, bool owner_events = true) { - if (keyboard || pointer) { - window.raise(); - window.focus(Gdk.CURRENT_TIME); - - if (!try_grab_window(window, keyboard, pointer, owner_events)) { - int i = 0; - Timeout.add(100, () => { - if (++i >= 100) return false; - return !try_grab_window(window, keyboard, pointer, owner_events); - }); - } + public static void grab(Gdk.Window window) { + window.raise(); + window.focus(Gdk.CURRENT_TIME); + + if (!try_grab_window(window)) { + int i = 0; + Timeout.add(100, () => { + if (++i >= 100) return false; + return !try_grab_window(window); + }); } } @@ -47,50 +45,65 @@ public class FocusGrabber : GLib.Object { /// Code roughly from Gnome-Do/Synapse. ///////////////////////////////////////////////////////////////////// - public static void ungrab(bool keyboard = true, bool pointer = true) { - var display = Gdk.Display.get_default(); - var manager = display.get_device_manager(); - - GLib.List<weak Gdk.Device?> list = manager.list_devices(Gdk.DeviceType.MASTER); + public static void ungrab() { + #if HAVE_GTK_3_20 + var seat = Gdk.Display.get_default().get_default_seat(); + seat.ungrab(); + #else + var display = Gdk.Display.get_default(); + var manager = display.get_device_manager(); - foreach(var device in list) { - if ((device.input_source == Gdk.InputSource.KEYBOARD && keyboard) - || (device.input_source != Gdk.InputSource.KEYBOARD && pointer)) + GLib.List<weak Gdk.Device?> list = manager.list_devices(Gdk.DeviceType.MASTER); + foreach(var device in list) { device.ungrab(Gdk.CURRENT_TIME); - } + } + #endif } ///////////////////////////////////////////////////////////////////// /// Code roughly from Gnome-Do/Synapse. ///////////////////////////////////////////////////////////////////// - private static bool try_grab_window(Gdk.Window window, bool keyboard, bool pointer, bool owner_events) { - var display = Gdk.Display.get_default(); - var manager = display.get_device_manager(); + private static bool try_grab_window(Gdk.Window window) { + #if HAVE_GTK_3_20 + // try again if window is not yet viewable + if (!window.is_viewable()) return false; - bool grabbed_all = true; + var seat = Gdk.Display.get_default().get_default_seat(); + var caps = Gdk.SeatCapabilities.POINTER | Gdk.SeatCapabilities.KEYBOARD; + var result = seat.grab(window, caps, true, null, null, null); - GLib.List<weak Gdk.Device?> list = manager.list_devices(Gdk.DeviceType.MASTER); + // for some reason GDK hides the window if the grab fails... + if (result != Gdk.GrabStatus.SUCCESS) { + window.show(); + } + + // continue trying to grab if it failed! + return result == Gdk.GrabStatus.SUCCESS; + #else + var display = Gdk.Display.get_default(); + var manager = display.get_device_manager(); - foreach(var device in list) { - if ((device.input_source == Gdk.InputSource.KEYBOARD && keyboard) - || (device.input_source != Gdk.InputSource.KEYBOARD && pointer)) { + bool grabbed_all = true; - var status = device.grab(window, Gdk.GrabOwnership.APPLICATION, owner_events, + GLib.List<weak Gdk.Device?> list = manager.list_devices(Gdk.DeviceType.MASTER); + + foreach(var device in list) { + var status = device.grab(window, Gdk.GrabOwnership.APPLICATION, true, Gdk.EventMask.ALL_EVENTS_MASK, null, Gdk.CURRENT_TIME); if (status != Gdk.GrabStatus.SUCCESS) grabbed_all = false; } - } - if (grabbed_all) - return true; + if (grabbed_all) + return true; - ungrab(keyboard, pointer); + ungrab(); - return false; + return false; + #endif } } |