You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					525 lines
				
				13 KiB
			
		
		
			
		
	
	
					525 lines
				
				13 KiB
			| 
								 
											13 years ago
										 
									 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * Copyright © 2012 Intel Corporation
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								 * Permission is hereby granted, free of charge, to any person obtaining a
							 | 
						||
| 
								 | 
							
								 * copy of this software and associated documentation files (the "Software"),
							 | 
						||
| 
								 | 
							
								 * to deal in the Software without restriction, including without limitation
							 | 
						||
| 
								 | 
							
								 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
							 | 
						||
| 
								 | 
							
								 * and/or sell copies of the Software, and to permit persons to whom the
							 | 
						||
| 
								 | 
							
								 * Software is furnished to do so, subject to the following conditions:
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 *
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								 * The above copyright notice and this permission notice (including the next
							 | 
						||
| 
								 | 
							
								 * paragraph) shall be included in all copies or substantial portions of the
							 | 
						||
| 
								 | 
							
								 * Software.
							 | 
						||
| 
								 | 
							
								 *
							 | 
						||
| 
								 | 
							
								 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
							 | 
						||
| 
								 | 
							
								 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
							 | 
						||
| 
								 | 
							
								 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
							 | 
						||
| 
								 | 
							
								 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
							 | 
						||
| 
								 | 
							
								 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
							 | 
						||
| 
								 | 
							
								 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
							 | 
						||
| 
								 | 
							
								 * DEALINGS IN THE SOFTWARE.
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								 */
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
								#include "config.h"
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								#include <stdint.h>
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								#include <stdio.h>
							 | 
						||
| 
								 | 
							
								#include <stdlib.h>
							 | 
						||
| 
								 | 
							
								#include <string.h>
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								#include <unistd.h>
							 | 
						||
| 
								 | 
							
								#include <sys/mman.h>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								#include <linux/input.h>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "window.h"
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
								#include "input-method-unstable-v1-client-protocol.h"
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								#include "shared/helpers.h"
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								enum compose_state {
							 | 
						||
| 
								 | 
							
									state_normal,
							 | 
						||
| 
								 | 
							
									state_compose
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								struct compose_seq {
							 | 
						||
| 
								 | 
							
									uint32_t keys[4];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									const char *text;
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								struct simple_im;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								typedef void (*keyboard_input_key_handler_t)(struct simple_im *keyboard,
							 | 
						||
| 
								 | 
							
													     uint32_t serial,
							 | 
						||
| 
								 | 
							
													     uint32_t time, uint32_t key, uint32_t unicode,
							 | 
						||
| 
								 | 
							
													     enum wl_keyboard_key_state state);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								struct simple_im {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									struct zwp_input_method_v1 *input_method;
							 | 
						||
| 
								 | 
							
									struct zwp_input_method_context_v1 *context;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									struct wl_display *display;
							 | 
						||
| 
								 | 
							
									struct wl_registry *registry;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									struct wl_keyboard *keyboard;
							 | 
						||
| 
								 | 
							
									enum compose_state compose_state;
							 | 
						||
| 
								 | 
							
									struct compose_seq compose_seq;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									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;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									keyboard_input_key_handler_t key_handler;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									uint32_t serial;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const struct compose_seq compose_seqs[] = {
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_quotedbl, XKB_KEY_A, 0 },  "Ä" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_quotedbl, XKB_KEY_O, 0 },  "Ö" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_quotedbl, XKB_KEY_U, 0 },  "Ü" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_quotedbl, XKB_KEY_a, 0 },  "ä" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_quotedbl, XKB_KEY_o, 0 },  "ö" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_quotedbl, XKB_KEY_u, 0 },  "ü" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_apostrophe, XKB_KEY_A, 0 },  "Á" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_apostrophe, XKB_KEY_a, 0 },  "á" },
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{ { XKB_KEY_slash, XKB_KEY_O, 0 },     "Ø" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_slash, XKB_KEY_o, 0 },     "ø" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_less, XKB_KEY_3, 0 },  "♥" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_A, XKB_KEY_A, 0 },  "Å" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_A, XKB_KEY_E, 0 },  "Æ" },
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{ { XKB_KEY_O, XKB_KEY_C, 0 },  "©" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_O, XKB_KEY_R, 0 },  "®" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_s, XKB_KEY_s, 0 },  "ß" },
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{ { XKB_KEY_a, XKB_KEY_e, 0 },  "æ" },
							 | 
						||
| 
								 | 
							
									{ { XKB_KEY_a, XKB_KEY_a, 0 },  "å" },
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const uint32_t ignore_keys_on_compose[] = {
							 | 
						||
| 
								 | 
							
									XKB_KEY_Shift_L,
							 | 
						||
| 
								 | 
							
									XKB_KEY_Shift_R
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								handle_surrounding_text(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
											struct zwp_input_method_context_v1 *context,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											const char *text,
							 | 
						||
| 
								 | 
							
											uint32_t cursor,
							 | 
						||
| 
								 | 
							
											uint32_t anchor)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									fprintf(stderr, "Surrounding text updated: %s\n", text);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								handle_reset(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									     struct zwp_input_method_context_v1 *context)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									fprintf(stderr, "Reset pre-edit buffer\n");
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									keyboard->compose_state = state_normal;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								handle_content_type(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										    struct zwp_input_method_context_v1 *context,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										    uint32_t hint,
							 | 
						||
| 
								 | 
							
										    uint32_t purpose)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								handle_invoke_action(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										     struct zwp_input_method_context_v1 *context,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										     uint32_t button,
							 | 
						||
| 
								 | 
							
										     uint32_t index)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								handle_commit_state(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										    struct zwp_input_method_context_v1 *context,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										    uint32_t serial)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									keyboard->serial = serial;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 | 
							
								handle_preferred_language(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
											  struct zwp_input_method_context_v1 *context,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											  const char *language)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
								static const struct zwp_input_method_context_v1_listener input_method_context_listener = {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									handle_surrounding_text,
							 | 
						||
| 
								 | 
							
									handle_reset,
							 | 
						||
| 
								 | 
							
									handle_content_type,
							 | 
						||
| 
								 | 
							
									handle_invoke_action,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									handle_commit_state,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									handle_preferred_language
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 | 
							
								input_method_keyboard_keymap(void *data,
							 | 
						||
| 
								 | 
							
											     struct wl_keyboard *wl_keyboard,
							 | 
						||
| 
								 | 
							
											     uint32_t format,
							 | 
						||
| 
								 | 
							
											     int32_t fd,
							 | 
						||
| 
								 | 
							
											     uint32_t size)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									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;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->keymap =
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
										xkb_keymap_new_from_string(keyboard->xkb_context,
							 | 
						||
| 
								 | 
							
													   map_str,
							 | 
						||
| 
								 | 
							
													   XKB_KEYMAP_FORMAT_TEXT_V1,
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
													   XKB_KEYMAP_COMPILE_NO_FLAGS);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									munmap(map_str, size);
							 | 
						||
| 
								 | 
							
									close(fd);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									if (!keyboard->keymap) {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										fprintf(stderr, "Failed to compile keymap\n");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->state = xkb_state_new(keyboard->keymap);
							 | 
						||
| 
								 | 
							
									if (!keyboard->state) {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										fprintf(stderr, "Failed to create XKB state\n");
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
										xkb_keymap_unref(keyboard->keymap);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->control_mask =
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
										1 << xkb_keymap_mod_get_index(keyboard->keymap, "Control");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->alt_mask =
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
										1 << xkb_keymap_mod_get_index(keyboard->keymap, "Mod1");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->shift_mask =
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
										1 << xkb_keymap_mod_get_index(keyboard->keymap, "Shift");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 | 
							
								input_method_keyboard_key(void *data,
							 | 
						||
| 
								 | 
							
											  struct wl_keyboard *wl_keyboard,
							 | 
						||
| 
								 | 
							
											  uint32_t serial,
							 | 
						||
| 
								 | 
							
											  uint32_t time,
							 | 
						||
| 
								 | 
							
											  uint32_t key,
							 | 
						||
| 
								 | 
							
											  uint32_t state_w)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									uint32_t code;
							 | 
						||
| 
								 | 
							
									uint32_t num_syms;
							 | 
						||
| 
								 | 
							
									const xkb_keysym_t *syms;
							 | 
						||
| 
								 | 
							
									xkb_keysym_t sym;
							 | 
						||
| 
								 | 
							
									enum wl_keyboard_key_state state = state_w;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									if (!keyboard->state)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									code = key + 8;
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
									num_syms = xkb_state_key_get_syms(keyboard->state, code, &syms);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									sym = XKB_KEY_NoSymbol;
							 | 
						||
| 
								 | 
							
									if (num_syms == 1)
							 | 
						||
| 
								 | 
							
										sym = syms[0];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									if (keyboard->key_handler)
							 | 
						||
| 
								 | 
							
										(*keyboard->key_handler)(keyboard, serial, time, key, sym,
							 | 
						||
| 
								 | 
							
													 state);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 | 
							
								input_method_keyboard_modifiers(void *data,
							 | 
						||
| 
								 | 
							
												struct wl_keyboard *wl_keyboard,
							 | 
						||
| 
								 | 
							
												uint32_t serial,
							 | 
						||
| 
								 | 
							
												uint32_t mods_depressed,
							 | 
						||
| 
								 | 
							
												uint32_t mods_latched,
							 | 
						||
| 
								 | 
							
												uint32_t mods_locked,
							 | 
						||
| 
								 | 
							
												uint32_t group)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									struct zwp_input_method_context_v1 *context = keyboard->context;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									xkb_mod_mask_t mask;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									xkb_state_update_mask(keyboard->state, mods_depressed,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											      mods_latched, mods_locked, 0, 0, group);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									mask = xkb_state_serialize_mods(keyboard->state,
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
													XKB_STATE_MODS_DEPRESSED |
							 | 
						||
| 
								 | 
							
													XKB_STATE_MODS_LATCHED);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->modifiers = 0;
							 | 
						||
| 
								 | 
							
									if (mask & keyboard->control_mask)
							 | 
						||
| 
								 | 
							
										keyboard->modifiers |= MOD_CONTROL_MASK;
							 | 
						||
| 
								 | 
							
									if (mask & keyboard->alt_mask)
							 | 
						||
| 
								 | 
							
										keyboard->modifiers |= MOD_ALT_MASK;
							 | 
						||
| 
								 | 
							
									if (mask & keyboard->shift_mask)
							 | 
						||
| 
								 | 
							
										keyboard->modifiers |= MOD_SHIFT_MASK;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									zwp_input_method_context_v1_modifiers(context, serial,
							 | 
						||
| 
								 | 
							
													      mods_depressed, mods_depressed,
							 | 
						||
| 
								 | 
							
													      mods_latched, group);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const struct wl_keyboard_listener input_method_keyboard_listener = {
							 | 
						||
| 
								 | 
							
									input_method_keyboard_keymap,
							 | 
						||
| 
								 | 
							
									NULL, /* enter */
							 | 
						||
| 
								 | 
							
									NULL, /* leave */
							 | 
						||
| 
								 | 
							
									input_method_keyboard_key,
							 | 
						||
| 
								 | 
							
									input_method_keyboard_modifiers
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 | 
							
								input_method_activate(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										      struct zwp_input_method_v1 *input_method,
							 | 
						||
| 
								 | 
							
										      struct zwp_input_method_context_v1 *context)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (keyboard->context)
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										zwp_input_method_context_v1_destroy(keyboard->context);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									keyboard->compose_state = state_normal;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->serial = 0;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->context = context;
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									zwp_input_method_context_v1_add_listener(context,
							 | 
						||
| 
								 | 
							
														 &input_method_context_listener,
							 | 
						||
| 
								 | 
							
														 keyboard);
							 | 
						||
| 
								 | 
							
									keyboard->keyboard = zwp_input_method_context_v1_grab_keyboard(context);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									wl_keyboard_add_listener(keyboard->keyboard,
							 | 
						||
| 
								 | 
							
												 &input_method_keyboard_listener,
							 | 
						||
| 
								 | 
							
												 keyboard);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 | 
							
								input_method_deactivate(void *data,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
											struct zwp_input_method_v1 *input_method,
							 | 
						||
| 
								 | 
							
											struct zwp_input_method_context_v1 *context)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (!keyboard->context)
							 | 
						||
| 
								 | 
							
										return;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									zwp_input_method_context_v1_destroy(keyboard->context);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									keyboard->context = NULL;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
								static const struct zwp_input_method_v1_listener input_method_listener = {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									input_method_activate,
							 | 
						||
| 
								 | 
							
									input_method_deactivate
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								registry_handle_global(void *data, struct wl_registry *registry,
							 | 
						||
| 
								 | 
							
										       uint32_t name, const char *interface, uint32_t version)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im *keyboard = data;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									if (!strcmp(interface, "zwp_input_method_v1")) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										keyboard->input_method =
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											wl_registry_bind(registry, name,
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
													 &zwp_input_method_v1_interface, 1);
							 | 
						||
| 
								 | 
							
										zwp_input_method_v1_add_listener(keyboard->input_method,
							 | 
						||
| 
								 | 
							
														 &input_method_listener, keyboard);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								static void
							 | 
						||
| 
								 | 
							
								registry_handle_global_remove(void *data, struct wl_registry *registry,
							 | 
						||
| 
								 | 
							
											      uint32_t name)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								static const struct wl_registry_listener registry_listener = {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									registry_handle_global,
							 | 
						||
| 
								 | 
							
									registry_handle_global_remove
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								static int
							 | 
						||
| 
								 | 
							
								compare_compose_keys(const void *c1, const void *c2)
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									const struct compose_seq *cs1 = c1;
							 | 
						||
| 
								 | 
							
									const struct compose_seq *cs2 = c2;
							 | 
						||
| 
								 | 
							
									int i;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									for (i = 0; cs1->keys[i] != 0 && cs2->keys[i] != 0; i++) {
							 | 
						||
| 
								 | 
							
										if (cs1->keys[i] != cs2->keys[i])
							 | 
						||
| 
								 | 
							
											return cs1->keys[i] - cs2->keys[i];
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (cs1->keys[i] == cs2->keys[i]
							 | 
						||
| 
								 | 
							
									    || cs1->keys[i] == 0)
							 | 
						||
| 
								 | 
							
										return 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									return cs1->keys[i] - cs2->keys[i];
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static void
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								simple_im_key_handler(struct simple_im *keyboard,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										      uint32_t serial, uint32_t time, uint32_t key, uint32_t sym,
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										      enum wl_keyboard_key_state state)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									struct zwp_input_method_context_v1 *context = keyboard->context;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									char text[64];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (sym == XKB_KEY_Multi_key &&
							 | 
						||
| 
								 | 
							
									    state == WL_KEYBOARD_KEY_STATE_RELEASED &&
							 | 
						||
| 
								 | 
							
									    keyboard->compose_state == state_normal) {
							 | 
						||
| 
								 | 
							
										keyboard->compose_state = state_compose;
							 | 
						||
| 
								 | 
							
										memset(&keyboard->compose_seq, 0, sizeof(struct compose_seq));
							 | 
						||
| 
								 | 
							
										return;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (keyboard->compose_state == state_compose) {
							 | 
						||
| 
								 | 
							
										uint32_t i = 0;
							 | 
						||
| 
								 | 
							
										struct compose_seq *cs;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
							 | 
						||
| 
								 | 
							
											return;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
										for (i = 0; i < ARRAY_LENGTH(ignore_keys_on_compose); i++) {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											if (sym == ignore_keys_on_compose[i]) {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
												zwp_input_method_context_v1_key(context,
							 | 
						||
| 
								 | 
							
																keyboard->serial,
							 | 
						||
| 
								 | 
							
																time,
							 | 
						||
| 
								 | 
							
																key,
							 | 
						||
| 
								 | 
							
																state);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												return;
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										for (i = 0; keyboard->compose_seq.keys[i] != 0; i++);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										keyboard->compose_seq.keys[i] = sym;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										cs = bsearch (&keyboard->compose_seq, compose_seqs,
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
											      ARRAY_LENGTH(compose_seqs),
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											      sizeof(compose_seqs[0]), compare_compose_keys);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										if (cs) {
							 | 
						||
| 
								 | 
							
											if (cs->keys[i + 1] == 0) {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
												zwp_input_method_context_v1_preedit_cursor(keyboard->context,
							 | 
						||
| 
								 | 
							
																	   0);
							 | 
						||
| 
								 | 
							
												zwp_input_method_context_v1_preedit_string(keyboard->context,
							 | 
						||
| 
								 | 
							
																	   keyboard->serial,
							 | 
						||
| 
								 | 
							
																	   "", "");
							 | 
						||
| 
								 | 
							
												zwp_input_method_context_v1_cursor_position(keyboard->context,
							 | 
						||
| 
								 | 
							
																	    0, 0);
							 | 
						||
| 
								 | 
							
												zwp_input_method_context_v1_commit_string(keyboard->context,
							 | 
						||
| 
								 | 
							
																	  keyboard->serial,
							 | 
						||
| 
								 | 
							
																	  cs->text);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
												keyboard->compose_state = state_normal;
							 | 
						||
| 
								 | 
							
											} else {
							 | 
						||
| 
								 | 
							
												uint32_t j = 0, idx = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
												for (; j <= i; j++) {
							 | 
						||
| 
								 | 
							
													idx += xkb_keysym_to_utf8(cs->keys[j], text + idx, sizeof(text) - idx);
							 | 
						||
| 
								 | 
							
												}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
												zwp_input_method_context_v1_preedit_cursor(keyboard->context,
							 | 
						||
| 
								 | 
							
																	   strlen(text));
							 | 
						||
| 
								 | 
							
												zwp_input_method_context_v1_preedit_string(keyboard->context,
							 | 
						||
| 
								 | 
							
																	   keyboard->serial,
							 | 
						||
| 
								 | 
							
																	   text,
							 | 
						||
| 
								 | 
							
																	   text);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											}
							 | 
						||
| 
								 | 
							
										} else {
							 | 
						||
| 
								 | 
							
											uint32_t j = 0, idx = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											for (; j <= i; j++) {
							 | 
						||
| 
								 | 
							
												idx += xkb_keysym_to_utf8(keyboard->compose_seq.keys[j], text + idx, sizeof(text) - idx);
							 | 
						||
| 
								 | 
							
											}
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
											zwp_input_method_context_v1_preedit_cursor(keyboard->context,
							 | 
						||
| 
								 | 
							
																   0);
							 | 
						||
| 
								 | 
							
											zwp_input_method_context_v1_preedit_string(keyboard->context,
							 | 
						||
| 
								 | 
							
																   keyboard->serial,
							 | 
						||
| 
								 | 
							
																   "", "");
							 | 
						||
| 
								 | 
							
											zwp_input_method_context_v1_cursor_position(keyboard->context,
							 | 
						||
| 
								 | 
							
																    0, 0);
							 | 
						||
| 
								 | 
							
											zwp_input_method_context_v1_commit_string(keyboard->context,
							 | 
						||
| 
								 | 
							
																  keyboard->serial,
							 | 
						||
| 
								 | 
							
																  text);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											keyboard->compose_state = state_normal;
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										return;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0) {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										zwp_input_method_context_v1_key(context, serial, time, key, state);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
							 | 
						||
| 
								 | 
							
										return;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
									zwp_input_method_context_v1_cursor_position(keyboard->context,
							 | 
						||
| 
								 | 
							
														    0, 0);
							 | 
						||
| 
								 | 
							
									zwp_input_method_context_v1_commit_string(keyboard->context,
							 | 
						||
| 
								 | 
							
														  keyboard->serial,
							 | 
						||
| 
								 | 
							
														  text);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int
							 | 
						||
| 
								 | 
							
								main(int argc, char *argv[])
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									struct simple_im simple_im;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									int ret = 0;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									memset(&simple_im, 0, sizeof(simple_im));
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									simple_im.display = wl_display_connect(NULL);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									if (simple_im.display == NULL) {
							 | 
						||
| 
								 
											10 years ago
										 
									 | 
							
										fprintf(stderr, "Failed to connect to server: %m\n");
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										return -1;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									simple_im.registry = wl_display_get_registry(simple_im.display);
							 | 
						||
| 
								 | 
							
									wl_registry_add_listener(simple_im.registry,
							 | 
						||
| 
								 | 
							
												 ®istry_listener, &simple_im);
							 | 
						||
| 
								 | 
							
									wl_display_roundtrip(simple_im.display);
							 | 
						||
| 
								 | 
							
									if (simple_im.input_method == NULL) {
							 | 
						||
| 
								 | 
							
										fprintf(stderr, "No input_method global\n");
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
										return -1;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
									simple_im.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									if (simple_im.xkb_context == NULL) {
							 | 
						||
| 
								 | 
							
										fprintf(stderr, "Failed to create XKB context\n");
							 | 
						||
| 
								 | 
							
										return -1;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									simple_im.context = NULL;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									simple_im.key_handler =  simple_im_key_handler;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									while (ret != -1)
							 | 
						||
| 
								 | 
							
										ret = wl_display_dispatch(simple_im.display);
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									if (ret == -1) {
							 | 
						||
| 
								 | 
							
										fprintf(stderr, "Dispatch error: %m\n");
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
										return -1;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									}
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
									return 0;
							 | 
						||
| 
								 | 
							
								}
							 |