[PATCH 12/15] window: Add utils for xkb keyboard input

Jan Arne Petersen jpetersen at openismus.com
Sun Nov 4 18:26:50 PST 2012


From: Jan Arne Petersen <jpetersen at openismus.com>

Signed-off-by: Jan Arne Petersen <jpetersen at openismus.com>
---
 clients/Makefile.am      |   2 +
 clients/keyboard-utils.c | 173 +++++++++++++++++++++++++++++++++++++++++++++++
 clients/keyboard-utils.h |  67 ++++++++++++++++++
 3 files changed, 242 insertions(+)
 create mode 100644 clients/keyboard-utils.c
 create mode 100644 clients/keyboard-utils.h

diff --git a/clients/Makefile.am b/clients/Makefile.am
index a6927d7..3555228 100644
--- a/clients/Makefile.am
+++ b/clients/Makefile.am
@@ -76,6 +76,8 @@ noinst_LIBRARIES = libtoytoolkit.a
 libtoytoolkit_a_SOURCES =			\
 	window.c				\
 	window.h				\
+	keyboard-utils.c			\
+	keyboard-utils.h			\
 	text-cursor-position-protocol.c		\
 	text-cursor-position-client-protocol.h	\
 	workspaces-protocol.c			\
diff --git a/clients/keyboard-utils.c b/clients/keyboard-utils.c
new file mode 100644
index 0000000..c2c5f46
--- /dev/null
+++ b/clients/keyboard-utils.c
@@ -0,0 +1,173 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+#include "keyboard-utils.h"
+
+struct keyboard_input {
+	struct xkb_context *xkb_context;
+
+	uint32_t modifiers;
+
+	struct xkb_keymap *keymap;
+	struct xkb_state *state;
+	xkb_mod_mask_t control_mask;
+	xkb_mod_mask_t alt_mask;
+	xkb_mod_mask_t shift_mask;
+
+	void *user_data;
+	keyboard_input_key_handler_t key_handler;
+};
+
+struct keyboard_input*
+keyboard_input_create(struct xkb_context *xkb_context)
+{
+	struct keyboard_input *keyboard_input;
+
+	keyboard_input = calloc(1, sizeof *keyboard_input);
+	keyboard_input->xkb_context = xkb_context;
+
+	return keyboard_input;
+}
+
+void
+keyboard_input_destroy(struct keyboard_input *keyboard_input)
+{
+	free(keyboard_input);
+}
+
+void
+keyboard_input_handle_keymap(struct keyboard_input *keyboard_input,
+			     uint32_t format, int fd, uint32_t size)
+{
+	char *map_str;
+
+	if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
+		close(fd);
+		return;
+	}
+
+	map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
+	if (map_str == MAP_FAILED) {
+		close(fd);
+		return;
+	}
+
+	keyboard_input->keymap = xkb_map_new_from_string(keyboard_input->xkb_context,
+							 map_str,
+							 XKB_KEYMAP_FORMAT_TEXT_V1,
+							 0);
+
+	munmap(map_str, size);
+	close(fd);
+
+	if (!keyboard_input->keymap) {
+		fprintf(stderr, "failed to compile keymap\n");
+		return;
+	}
+
+	keyboard_input->state = xkb_state_new(keyboard_input->keymap);
+	if (!keyboard_input->state) {
+		fprintf(stderr, "failed to create XKB state\n");
+		xkb_map_unref(keyboard_input->keymap);
+		return;
+	}
+
+	keyboard_input->control_mask =
+		1 << xkb_map_mod_get_index(keyboard_input->keymap, "Control");
+	keyboard_input->alt_mask =
+		1 << xkb_map_mod_get_index(keyboard_input->keymap, "Mod1");
+	keyboard_input->shift_mask =
+		1 << xkb_map_mod_get_index(keyboard_input->keymap, "Shift");
+}
+
+void
+keyboard_input_handle_key(struct keyboard_input *keyboard_input,
+			  uint32_t serial, uint32_t time,
+			  uint32_t key, uint32_t state_w)
+{
+	uint32_t code;
+	uint32_t num_syms;
+	const xkb_keysym_t *syms;
+	xkb_keysym_t sym;
+	enum wl_keyboard_key_state state = state_w;
+
+	if (!keyboard_input->state)
+		return;
+
+	code = key + 8;
+	num_syms = xkb_key_get_syms(keyboard_input->state, code, &syms);
+
+	sym = XKB_KEY_NoSymbol;
+	if (num_syms == 1)
+		sym = syms[0];
+
+	if (keyboard_input->key_handler)
+		(*keyboard_input->key_handler)(keyboard_input, time, key, sym,
+					       state, keyboard_input->user_data);
+}
+
+void
+keyboard_input_handle_modifiers(struct keyboard_input *keyboard_input,
+				uint32_t serial, uint32_t mods_depressed,
+				uint32_t mods_latched, uint32_t mods_locked,
+				uint32_t group)
+{
+	xkb_mod_mask_t mask;
+
+	xkb_state_update_mask(keyboard_input->state, mods_depressed, mods_latched,
+			      mods_locked, 0, 0, group);
+	mask = xkb_state_serialize_mods(keyboard_input->state,
+					XKB_STATE_DEPRESSED |
+					XKB_STATE_LATCHED);
+
+	keyboard_input->modifiers = 0;
+	if (mask & keyboard_input->control_mask)
+		keyboard_input->modifiers |= MOD_CONTROL_MASK;
+	if (mask & keyboard_input->alt_mask)
+		keyboard_input->modifiers |= MOD_ALT_MASK;
+	if (mask & keyboard_input->shift_mask)
+		keyboard_input->modifiers |= MOD_SHIFT_MASK;
+
+}
+
+void
+keyboard_input_set_user_data(struct keyboard_input *keyboard_input, void *data)
+{
+	keyboard_input->user_data = data;
+}
+
+void *
+keyboard_input_get_user_data(struct keyboard_input *keyboard_input)
+{
+	return keyboard_input->user_data;
+}
+
+void
+keyboard_input_set_key_handler(struct keyboard_input *keyboard_input,
+			       keyboard_input_key_handler_t handler)
+{
+	keyboard_input->key_handler = handler;
+}
diff --git a/clients/keyboard-utils.h b/clients/keyboard-utils.h
new file mode 100644
index 0000000..9b1a260
--- /dev/null
+++ b/clients/keyboard-utils.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ */
+
+#ifndef _KEYBOARD_UTILS_H_
+#define _KEYBOARD_UTILS_H_
+
+#include <xkbcommon/xkbcommon.h>
+#include <wayland-client.h>
+
+struct keyboard_input;
+
+typedef void (*keyboard_input_key_handler_t)(struct keyboard_input *keyboard_input,
+					     uint32_t time, uint32_t key, uint32_t unicode,
+					     enum wl_keyboard_key_state state, void *data);
+
+struct keyboard_input*
+keyboard_input_create(struct xkb_context *xkb_context);
+void
+keyboard_input_destroy(struct keyboard_input *keyboard_input);
+
+void
+keyboard_input_handle_keymap(struct keyboard_input *keyboard_input,
+			     uint32_t format, int fd, uint32_t size);
+void
+keyboard_input_handle_key(struct keyboard_input *keyboard_input,
+			  uint32_t serial, uint32_t time,
+			  uint32_t key, uint32_t state_w);
+void
+keyboard_input_handle_modifiers(struct keyboard_input *keyboard_input,
+				uint32_t serial, uint32_t mods_depressed,
+				uint32_t mods_latched, uint32_t mods_locked,
+				uint32_t group);
+
+void
+keyboard_input_set_user_data(struct keyboard_input *keyboard_input, void *data);
+
+void *
+keyboard_input_get_user_data(struct keyboard_input *keyboard_input);
+
+void
+keyboard_input_set_key_handler(struct keyboard_input *keyboard_input,
+			       keyboard_input_key_handler_t handler);
+
+#define MOD_SHIFT_MASK		0x01
+#define MOD_ALT_MASK		0x02
+#define MOD_CONTROL_MASK	0x04
+
+#endif
-- 
1.7.11.7



More information about the wayland-devel mailing list