From 1199b16f028928d8f0c0615bf20e36125d7f1e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= Date: Tue, 27 Nov 2012 13:41:48 -0500 Subject: [PATCH] clients: Move keyboard-utils.[ch] into weston-simple-im This is the only user of this code, so just include it in the simple im source. --- clients/Makefile.am | 2 - clients/keyboard-utils.c | 173 ------------------------------------- clients/keyboard-utils.h | 67 -------------- clients/weston-simple-im.c | 142 +++++++++++++++++++++++++++++- 4 files changed, 141 insertions(+), 243 deletions(-) delete mode 100644 clients/keyboard-utils.c delete mode 100644 clients/keyboard-utils.h diff --git a/clients/Makefile.am b/clients/Makefile.am index fd636c24..507f0ca1 100644 --- a/clients/Makefile.am +++ b/clients/Makefile.am @@ -82,8 +82,6 @@ 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 deleted file mode 100644 index c2c5f463..00000000 --- a/clients/keyboard-utils.c +++ /dev/null @@ -1,173 +0,0 @@ -/* - * 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 -#include -#include - -#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 deleted file mode 100644 index 9b1a260c..00000000 --- a/clients/keyboard-utils.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 -#include - -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 diff --git a/clients/weston-simple-im.c b/clients/weston-simple-im.c index 6c61fe8b..0583ebe2 100644 --- a/clients/weston-simple-im.c +++ b/clients/weston-simple-im.c @@ -23,11 +23,13 @@ #include #include #include +#include +#include + #include #include "window.h" -#include "keyboard-utils.h" #include "input-method-client-protocol.h" enum compose_state { @@ -70,6 +72,144 @@ static const uint32_t ignore_keys_on_compose[] = { XKB_KEY_Shift_R }; +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 { + 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; +}; + +static 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; +} + +static 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"); +} + +static 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); +} + +static 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; + +} + +static void +keyboard_input_set_user_data(struct keyboard_input *keyboard_input, void *data) +{ + keyboard_input->user_data = data; +} + +static void +keyboard_input_set_key_handler(struct keyboard_input *keyboard_input, + keyboard_input_key_handler_t handler) +{ + keyboard_input->key_handler = handler; +} + static void input_method_context_surrounding_text(void *data, struct input_method_context *context,