sugar-toolkit-gtk3/bindings/globalkeys/sugar-key-grabber.c

127 lines
3.0 KiB
C
Raw Normal View History

2006-08-25 11:19:27 +02:00
/*
* Copyright (C) 2006 Red Hat, Inc
*
* Sugar is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sugar 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
* General Public License for more details.
*
* You should have received a copy of the GNU 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.
*/
2006-08-25 12:28:52 +02:00
#include <X11/X.h>
#include <gdk/gdkscreen.h>
#include <gdk/gdkx.h>
2006-08-25 11:19:27 +02:00
#include "sugar-key-grabber.h"
2006-08-25 12:28:52 +02:00
/* we exclude shift, GDK_CONTROL_MASK and GDK_MOD1_MASK since we know what
these modifiers mean
these are the mods whose combinations are bound by the keygrabbing code */
#define IGNORED_MODS (0x2000 /*Xkb modifier*/ | GDK_LOCK_MASK | \
GDK_MOD2_MASK | GDK_MOD3_MASK | GDK_MOD4_MASK | GDK_MOD5_MASK)
2006-08-25 11:19:27 +02:00
struct _SugarKeyGrabber {
2006-08-25 12:28:52 +02:00
GObject base_instance;
GdkWindow *root;
2006-08-25 11:19:27 +02:00
};
struct _SugarKeyGrabberClass {
GObjectClass base_class;
};
G_DEFINE_TYPE(SugarKeyGrabber, sugar_key_grabber, G_TYPE_OBJECT)
static void
2006-08-25 12:28:52 +02:00
sugar_key_grabber_class_init(SugarKeyGrabberClass *key_grabber_class)
2006-08-25 11:19:27 +02:00
{
}
2006-08-25 12:28:52 +02:00
static GdkFilterReturn
filter_events(GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
SugarKeyGrabber *grabber = (SugarKeyGrabber *)data;
XEvent *xev = (XEvent *)xevent;
XAnyEvent *xanyev = (XAnyEvent *)xevent;
guint keycode, state;
int i;
keycode = xev->xkey.keycode;
state = xev->xkey.state;
g_print("KeyCode %d", keycode);
}
2006-08-25 11:19:27 +02:00
static void
2006-08-25 12:28:52 +02:00
sugar_key_grabber_init(SugarKeyGrabber *grabber)
{
GdkScreen *screen;
screen = gdk_screen_get_default();
grabber->root = gdk_screen_get_root_window(screen);
gdk_window_add_filter(grabber->root, filter_events, grabber);
}
/* inspired from all_combinations from gnome-panel/gnome-panel/global-keys.c */
#define N_BITS 32
static int
get_modifier(guint state)
2006-08-25 11:19:27 +02:00
{
2006-08-25 12:28:52 +02:00
int indexes[N_BITS];/*indexes of bits we need to flip*/
int i, bit, bits_set_cnt;
int uppervalue;
int result = 0;
guint mask_to_traverse = IGNORED_MODS & ~ state;
bit = 0;
for (i = 0; i < N_BITS; i++) {
if (mask_to_traverse & (1<<i))
indexes[bit++]=i;
}
bits_set_cnt = bit;
uppervalue = 1<<bits_set_cnt;
for (i = 0; i < uppervalue; i++) {
int j;
for (j = 0; j < bits_set_cnt; j++) {
if (i & (1<<j))
result |= (1<<indexes[j]);
}
}
return (result | state);
}
void
sugar_key_grabber_grab(SugarKeyGrabber *grabber, const char *key)
{
guint keysym;
guint state;
guint keycode;
gdk_error_trap_push ();
egg_accelerator_parse_virtual (key, &keysym, &keycode, &state);
XGrabKey (GDK_DISPLAY(), keycode, get_modifier(state),
GDK_WINDOW_XID (grabber->root), True,
GrabModeAsync, GrabModeAsync);
gdk_flush ();
gdk_error_trap_pop ();
2006-08-25 11:19:27 +02:00
}