<commit_before>/*
 * Copyright (C) 2006-2007, Red Hat, Inc.
 *
 * 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <X11/X.h>
#include <X11/Xlib.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include "sugar-key-grabber.h"
#include "eggaccelerators.h"
#include "sugar-marshal.h"

/* 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)
/* these are the ones we actually use for global keys, we always only check
 * for these set */
#define USED_MODS (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)

enum {
	KEY_PRESSED,
	KEY_RELEASED,
	N_SIGNALS
};

typedef struct {
  char *key;
  guint keysym;
  guint state;
  guint keycode;
} Key;

G_DEFINE_TYPE(SugarKeyGrabber, sugar_key_grabber, G_TYPE_OBJECT)

static guint signals[N_SIGNALS];

static void
free_key_info(Key *key_info)
{
	g_free(key_info->key);
	g_free(key_info);
}

static void
sugar_key_grabber_dispose (GObject *object)
{
	SugarKeyGrabber *grabber = SUGAR_KEY_GRABBER(object);

	if (grabber->keys) {
		g_list_foreach(grabber->keys, (GFunc)free_key_info, NULL);
		g_list_free(grabber->keys);
		grabber->keys = NULL;
	}
}

static void
sugar_key_grabber_class_init(SugarKeyGrabberClass *grabber_class)
{
	GObjectClass *g_object_class = G_OBJECT_CLASS (grabber_class);

	g_object_class->dispose = sugar_key_grabber_dispose;

	signals[KEY_PRESSED] = g_signal_new ("key-pressed",
                         G_TYPE_FROM_CLASS (grabber_class),
                         G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                         G_STRUCT_OFFSET (SugarKeyGrabberClass, key_pressed),
                         NULL, NULL,
                         sugar_marshal_BOOLEAN__UINT_UINT_UINT,
                         G_TYPE_BOOLEAN, 3,
                         G_TYPE_UINT,
                         G_TYPE_UINT,
                         G_TYPE_UINT);
	signals[KEY_RELEASED] = g_signal_new ("key-released",
                         G_TYPE_FROM_CLASS (grabber_class),
                         G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                         G_STRUCT_OFFSET (SugarKeyGrabberClass, key_released),
                         NULL, NULL,
                         sugar_marshal_BOOLEAN__UINT_UINT_UINT,
                         G_TYPE_BOOLEAN, 3,
                         G_TYPE_UINT,
                         G_TYPE_UINT,
                         G_TYPE_UINT);
}

char *
sugar_key_grabber_get_key(SugarKeyGrabber *grabber, guint keycode, guint state)
{
	GList *l;

	for (l = grabber->keys; l != NULL; l = l->next) {
		Key *keyinfo = (Key *)l->data;
		if ((keyinfo->keycode == keycode) &&
			((state & USED_MODS) == keyinfo->state)) {
			return g_strdup(keyinfo->key);
		}
	}

	return NULL;
}

static GdkFilterReturn
filter_events(GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
	SugarKeyGrabber *grabber = (SugarKeyGrabber *)data;
	XEvent *xev = (XEvent *)xevent;

	if (xev->type == KeyRelease) {
		int return_value;
		g_signal_emit (grabber, signals[KEY_RELEASED], 0, xev->xkey.keycode,
					   xev->xkey.state, xev->xkey.time, &return_value);
		if(return_value)
			return GDK_FILTER_REMOVE;
	}

	if (xev->type == KeyPress) {
		int return_value;
		g_signal_emit (grabber, signals[KEY_PRESSED], 0, xev->xkey.keycode,
					   xev->xkey.state, xev->xkey.time, &return_value);
		if(return_value)
			return GDK_FILTER_REMOVE;
	}

	return GDK_FILTER_CONTINUE;
}

static void
sugar_key_grabber_init(SugarKeyGrabber *grabber)
{
	GdkScreen *screen;

	screen = gdk_screen_get_default();
	grabber->root = gdk_screen_get_root_window(screen);
	grabber->keys = NULL;<commit_msg>keygrabber: Handle XI2 key events, part of SL #3895<commit_after>/*
 * Copyright (C) 2006-2007, Red Hat, Inc.
 *
 * 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include "sugar-key-grabber.h"
#include "eggaccelerators.h"
#include "sugar-marshal.h"

/* 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)
/* these are the ones we actually use for global keys, we always only check
 * for these set */
#define USED_MODS (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)

enum {
	KEY_PRESSED,
	KEY_RELEASED,
	N_SIGNALS
};

typedef struct {
  char *key;
  guint keysym;
  guint state;
  guint keycode;
} Key;

G_DEFINE_TYPE(SugarKeyGrabber, sugar_key_grabber, G_TYPE_OBJECT)

static guint signals[N_SIGNALS];

static void
free_key_info(Key *key_info)
{
	g_free(key_info->key);
	g_free(key_info);
}

static void
sugar_key_grabber_dispose (GObject *object)
{
	SugarKeyGrabber *grabber = SUGAR_KEY_GRABBER(object);

	if (grabber->keys) {
		g_list_foreach(grabber->keys, (GFunc)free_key_info, NULL);
		g_list_free(grabber->keys);
		grabber->keys = NULL;
	}
}

static void
sugar_key_grabber_class_init(SugarKeyGrabberClass *grabber_class)
{
	GObjectClass *g_object_class = G_OBJECT_CLASS (grabber_class);

	g_object_class->dispose = sugar_key_grabber_dispose;

	signals[KEY_PRESSED] = g_signal_new ("key-pressed",
                         G_TYPE_FROM_CLASS (grabber_class),
                         G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                         G_STRUCT_OFFSET (SugarKeyGrabberClass, key_pressed),
                         NULL, NULL,
                         sugar_marshal_BOOLEAN__UINT_UINT_UINT,
                         G_TYPE_BOOLEAN, 3,
                         G_TYPE_UINT,
                         G_TYPE_UINT,
                         G_TYPE_UINT);
	signals[KEY_RELEASED] = g_signal_new ("key-released",
                         G_TYPE_FROM_CLASS (grabber_class),
                         G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                         G_STRUCT_OFFSET (SugarKeyGrabberClass, key_released),
                         NULL, NULL,
                         sugar_marshal_BOOLEAN__UINT_UINT_UINT,
                         G_TYPE_BOOLEAN, 3,
                         G_TYPE_UINT,
                         G_TYPE_UINT,
                         G_TYPE_UINT);
}

char *
sugar_key_grabber_get_key(SugarKeyGrabber *grabber, guint keycode, guint state)
{
	GList *l;

	for (l = grabber->keys; l != NULL; l = l->next) {
		Key *keyinfo = (Key *)l->data;
		if ((keyinfo->keycode == keycode) &&
			((state & USED_MODS) == keyinfo->state)) {
			return g_strdup(keyinfo->key);
		}
	}

	return NULL;
}

static GdkFilterReturn
filter_events(GdkXEvent *xevent, GdkEvent *event, gpointer data)
{
	SugarKeyGrabber *grabber = (SugarKeyGrabber *)data;
	XEvent *xev = (XEvent *)xevent;

	if (xev->type == KeyRelease) {
		int return_value;
		g_signal_emit (grabber, signals[KEY_RELEASED], 0, xev->xkey.keycode,
					   xev->xkey.state, xev->xkey.time, &return_value);
		if(return_value)
			return GDK_FILTER_REMOVE;
	}

	if (xev->type == KeyPress) {
		int return_value;
		g_signal_emit (grabber, signals[KEY_PRESSED], 0, xev->xkey.keycode,
					   xev->xkey.state, xev->xkey.time, &return_value);
		if(return_value)
			return GDK_FILTER_REMOVE;
	}

	if (xev->type == GenericEvent) {
		XIDeviceEvent *ev;
		int return_value = FALSE;

		ev = (XIDeviceEvent *) ((XGenericEventCookie *) xev)->data;

		if (ev->evtype == XI_KeyPress) {
			g_signal_emit (grabber, signals[KEY_PRESSED], 0,
				       ev->detail, ev->mods.effective, ev->time, &return_value);
		} else if (ev->evtype == XI_KeyRelease) {
			g_signal_emit (grabber, signals[KEY_RELEASED], 0,
				       ev->detail, ev->mods.effective, ev->time, &return_value);
		}

		if (return_value)
			return GDK_FILTER_REMOVE;
	}


	return GDK_FILTER_CONTINUE;
}

static void
sugar_key_grabber_init(SugarKeyGrabber *grabber)
{
	GdkScreen *screen;

	screen = gdk_screen_get_default();
	grabber->root = gdk_screen_get_root_window(screen);
	grabber->keys = NULL;
{'fraction_non_alphanumeric': 0.09596487560108719, 'fraction_numerical': 0.009826468743466444, 'mean_word_length': 3.071063829787234, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4987235', 'n_tokens_mistral': 3582, 'n_tokens_neox': 3367, 'n_words': 798}