--- /dev/null
+[CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "X11/extensions/XInput2.h")]
+namespace XI {
+ public enum EventType {
+ [CCode (cname = "XI_KeyPress")]
+ KeyPress,
+ [CCode (cname = "XI_KeyRelease")]
+ KeyRelease,
+ }
+
+ [CCode (cname = "XIAllMasterDevices")]
+ public const int AllMasterDevices;
+
+ [CCode (cname = "XI_LASTEVENT")]
+ public const int LASTEVENT;
+
+ [Compact]
+ [CCode (cname = "XIEventMask", destroy_function = "")]
+ public struct EventMask {
+ public int deviceid;
+ [CCode (array_length_cname = "mask_len")]
+ public uchar[] mask;
+ }
+
+ [CCode (cname = "XISetMask")]
+ public void set_mask(void *mask, EventType type);
+
+ [Compact]
+ [CCode (cname = "XIGrabModifiers", destroy_function = "")]
+ public struct GrabModifiers {
+ public int modifiers;
+ public int status;
+ }
+
+ [CCode (cname = "XIGrabKeycode")]
+ public int grab_keycode (X.Display display,
+ int deviceid,
+ int keycode,
+ X.Window grab_window,
+ int grab_mode,
+ int paired_device_mode,
+ bool owner_events,
+ XI.EventMask mask,
+ [CCode (array_length_pos = 8.9)]
+ XI.GrabModifiers[] modifiers);
+
+ [CCode (cname = "XIUngrabKeycode")]
+ public int ungrab_keycode (X.Display display,
+ int deviceid,
+ int keycode,
+ X.Window grab_window,
+ [CCode (array_length_pos = 4.9)]
+ XI.GrabModifiers[] modifiers);
+}
+++ /dev/null
-/* vim:set et sts=4 sw=4:
- *
- * ibus - The Input Bus
- *
- * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or(at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307 USA
- */
-
-#include <glib.h>
-#include <gdk/gdkx.h>
-#include <X11/extensions/XInput2.h>
-
-gboolean grab_keycode (GdkDisplay *display,
- guint keyval,
- guint modifiers) {
- Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
- int keycode = XKeysymToKeycode (xdisplay, keyval);
- if (keycode == 0) {
- g_warning ("Can not convert keyval=%u to keycode!", keyval);
- return FALSE;
- }
-
- XIEventMask mask;
- mask.deviceid = XIAllMasterDevices;
- mask.mask_len = XIMaskLen(XI_RawMotion);
- mask.mask = g_new0 (unsigned char, mask.mask_len);
- XISetMask (mask.mask, XI_KeyPress);
- XISetMask (mask.mask, XI_KeyRelease);
-
- XIGrabModifiers ximodifiers[] = {
- { modifiers, 0 },
- { LockMask | modifiers, 0 },
- { Mod2Mask | modifiers, 0 },
- { Mod5Mask | modifiers, 0 },
- { LockMask | Mod2Mask | modifiers, 0 },
- { Mod2Mask | Mod5Mask | modifiers, 0 },
- { LockMask | Mod5Mask | modifiers, 0 },
- { LockMask | Mod2Mask | Mod5Mask | modifiers, 0 },
- };
-
- int retval = XIGrabKeycode (xdisplay,
- XIAllMasterDevices,
- keycode,
- DefaultRootWindow (xdisplay),
- GrabModeAsync,
- GrabModeAsync,
- True,
- &mask,
- G_N_ELEMENTS (ximodifiers),
- ximodifiers);
-
- g_free (mask.mask);
-
- if (retval == -1)
- return FALSE;
- return TRUE;
-}
-
-gboolean ungrab_keycode (GdkDisplay *display,
- guint keyval,
- guint modifiers) {
- Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
- int keycode = XKeysymToKeycode (xdisplay, keyval);
- if (keycode == 0) {
- g_warning ("Can not convert keyval=%u to keycode!", keyval);
- return FALSE;
- }
-
- XIGrabModifiers ximodifiers[] = {
- {modifiers, 0},
- {Mod2Mask | modifiers, 0},
- {LockMask | modifiers, 0},
- {Mod5Mask | modifiers, 0},
- {Mod2Mask | LockMask | modifiers, 0},
- {Mod2Mask | Mod5Mask | modifiers, 0},
- {LockMask | Mod5Mask | modifiers, 0},
- {Mod2Mask | LockMask | Mod5Mask | modifiers, 0},
- };
-
- int retval = XIUngrabKeycode (xdisplay,
- XIAllMasterDevices,
- keycode,
- DefaultRootWindow (xdisplay),
- G_N_ELEMENTS (ximodifiers),
- ximodifiers);
-
- if (retval == -1)
- return FALSE;
-
- return TRUE;
-}
* @author Oliver Sauder <os@esite.ch>
*/
-extern bool grab_keycode (Gdk.Display display,
- uint keyval,
- uint modifiers);
-
-extern bool ungrab_keycode (Gdk.Display display,
- uint keyval,
- uint modifiers);
-
public class KeybindingManager : GLib.Object {
/**
* list of binded keybindings
} while (false);
Gtk.main_do_event(event);
}
+
+ // Get union of given modifiers and all the combination of the
+ // modifiers in ignored_modifiers.
+ XI.GrabModifiers[] get_grab_modifiers(uint modifiers) {
+ const int[] ignored_modifiers = {
+ X.KeyMask.LockMask,
+ X.KeyMask.Mod2Mask,
+ X.KeyMask.Mod5Mask
+ };
+ int[] masks = {};
+ foreach (var modifier in ignored_modifiers) {
+ masks += modifier;
+
+ int length = masks.length;
+ for (int j = 0; j < length - 1; j++) {
+ masks += masks[j] | modifier;
+ }
+ }
+ masks += 0;
+
+ XI.GrabModifiers[] ximodifiers = {};
+ foreach (var mask in masks) {
+ ximodifiers += XI.GrabModifiers() {
+ modifiers = mask | modifiers,
+ status = 0
+ };
+ }
+
+ return ximodifiers;
+ }
+
+ bool grab_keycode(Gdk.Display display, uint keyval, uint modifiers) {
+ unowned X.Display xdisplay = Gdk.X11Display.get_xdisplay(display);
+ int keycode = xdisplay.keysym_to_keycode(keyval);
+ if (keycode == 0) {
+ warning("Can not convert keyval=%u to keycode!", keyval);
+ return false;
+ }
+
+ XI.EventMask evmask = XI.EventMask() {
+ deviceid = XI.AllMasterDevices,
+ mask = new uchar[(XI.LASTEVENT + 7) / 8]
+ };
+ XI.set_mask(evmask.mask, XI.EventType.KeyPress);
+ XI.set_mask(evmask.mask, XI.EventType.KeyRelease);
+
+ int retval = XI.grab_keycode (xdisplay,
+ XI.AllMasterDevices,
+ keycode,
+ xdisplay.default_root_window(),
+ X.GrabMode.Async,
+ X.GrabMode.Async,
+ true,
+ evmask,
+ get_grab_modifiers(modifiers));
+
+ return retval == 0;
+ }
+
+ bool ungrab_keycode(Gdk.Display display, uint keyval, uint modifiers) {
+ unowned X.Display xdisplay = Gdk.X11Display.get_xdisplay(display);
+ int keycode = xdisplay.keysym_to_keycode(keyval);
+ if (keycode == 0) {
+ warning("Can not convert keyval=%u to keycode!", keyval);
+ return false;
+ }
+
+ int retval = XI.ungrab_keycode (xdisplay,
+ XI.AllMasterDevices,
+ keycode,
+ xdisplay.default_root_window(),
+ get_grab_modifiers(modifiers));
+
+ return retval == 0;
+ }
}
/*