[PATCH 6/6] text: add a composing input method demo

Philipp Brüschweiler blei42 at gmail.com
Wed Jul 11 13:25:34 PDT 2012


---
 clients/.gitignore     |   1 +
 clients/Makefile.am    |   9 ++
 clients/hiragana-ime.c | 381 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 Dateien geändert, 391 Zeilen hinzugefügt(+)
 create mode 100644 clients/hiragana-ime.c

diff --git a/clients/.gitignore b/clients/.gitignore
index b43992b..2235a18 100644
--- a/clients/.gitignore
+++ b/clients/.gitignore
@@ -26,6 +26,7 @@ weston-terminal
 weston-screensaver
 wscreensaver
 editor
+hiragana-ime
 text-protocol.c
 text-client-protocol.h
 keyboard
diff --git a/clients/Makefile.am b/clients/Makefile.am
index 133eaca..7048c02 100644
--- a/clients/Makefile.am
+++ b/clients/Makefile.am
@@ -54,6 +54,7 @@ clients_programs =				\
 	clickdot				\
 	editor					\
 	keyboard				\
+	hiragana-ime				\
 	$(full_gl_client_programs)
 
 desktop_shell = weston-desktop-shell
@@ -114,6 +115,14 @@ keyboard_SOURCES = 				\
 	text-protocol.c
 keyboard_LDADD = $(toolkit_libs)
 
+hiragana_ime_SOURCES = 				\
+	hiragana-ime.c				\
+	desktop-shell-client-protocol.h		\
+	desktop-shell-protocol.c		\
+	text-client-protocol.h			\
+	text-protocol.c
+hiragana_ime_LDADD = $(toolkit_libs)
+
 weston_desktop_shell_SOURCES =			\
 	desktop-shell.c				\
 	desktop-shell-client-protocol.h		\
diff --git a/clients/hiragana-ime.c b/clients/hiragana-ime.c
new file mode 100644
index 0000000..a860082
--- /dev/null
+++ b/clients/hiragana-ime.c
@@ -0,0 +1,381 @@
+/*
+ * Copyright © 2012 Philipp Brüschweiler
+ *
+ * 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 <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "desktop-shell-client-protocol.h"
+#include "text-client-protocol.h"
+#include "window.h"
+
+struct hiragana_ime {
+	struct display *display;
+	struct input_method *input_method;
+
+	uint32_t modifiers;
+	struct {
+		struct xkb_context *context;
+		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;
+	} xkb;
+
+	// -1 if turned off,
+	// 0 if no previous state,
+	// >0 state contains previous key to be combined
+	int32_t state;
+};
+
+// FIXME: The logic in these tables and the following function is sorely incomplete.
+
+static const char *vowel_row[] = {"あ", "い", "う", "え", "お"};
+static const char *small_vowel_row[] = {"ぁ", "ぃ", "ぅ", "ぇ", "ぉ"};
+
+static const char *b_row[] = {"ば", "び", "ぶ", "べ", "ぼ"};
+static const char *d_row[] = {"だ", "ぢ", "づ", "で", "ど"};
+static const char *f_row[] = {"ふぁ", "ふぃ", "ふ", "ふぇ", "ふぉ"};
+static const char *g_row[] = {"が", "ぎ", "ぐ", "げ", "ご"};
+static const char *h_row[] = {"は", "ひ", "ふ", "へ", "ほ"};
+static const char *k_row[] = {"か", "き", "く", "け", "こ"};
+static const char *m_row[] = {"ま", "み", "む", "め", "も"};
+static const char *n_row[] = {"な", "に", "ぬ", "ね", "の"};
+static const char *p_row[] = {"ぱ", "ぴ", "ぷ", "ぺ", "ぽ"};
+static const char *r_row[] = {"ら", "り", "る", "れ", "ろ"};
+static const char *s_row[] = {"さ", "し", "す", "せ", "そ"};
+static const char *t_row[] = {"た", "ち", "つ", "て", "と"};
+static const char *y_row[] = {"や", NULL, "ゆ", NULL, "よ"};
+static const char *z_row[] = {"ざ", "じ", "ず", "ぜ", "ぞ"};
+
+static void
+ime_handle_key(struct hiragana_ime *ime, uint32_t key, uint32_t sym,
+	       enum wl_keyboard_key_state state)
+{
+	const char *output = NULL;
+	char onechar_output[] = {0, 0};
+
+	if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
+		return;
+
+	// FIXME works only with ascii input at the moment
+	char c = sym & 0x7f;
+
+	if (c == ' ' && (ime->modifiers & ime->xkb.control_mask)) {
+		ime->state = ime->state == -1 ? 0 : -1;
+		fprintf(stderr, "turning ime %s\n",
+			ime->state == -1 ? "off" : "on");
+		return;
+	}
+
+	if (ime->state < 0)
+		return;
+
+	if (c == 'a' || c == 'i' || c == 'u' || c == 'e' || c == 'o') {
+		int vowel_index;
+		switch (c) {
+		case 'a': vowel_index = 0; break;
+		case 'i': vowel_index = 1; break;
+		case 'u': vowel_index = 2; break;
+		case 'e': vowel_index = 3; break;
+		case 'o': vowel_index = 4; break;
+		default: assert(false);
+		}
+
+		switch (ime->state) {
+		case 0:
+			output = vowel_row[vowel_index];
+			break;
+		case 'x':
+		case 'l':
+			output = small_vowel_row[vowel_index];
+			ime->state = 0;
+			break;
+
+		case 'b':
+			output = b_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'd':
+			output = d_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'f':
+			output = f_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'g':
+			output = g_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'h':
+			output = h_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'c':
+		case 'k':
+			output = k_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'm':
+			output = m_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'n':
+			output = n_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'p':
+			output = p_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'r':
+			output = r_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 's':
+			output = s_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 't':
+			output = t_row[vowel_index];
+			ime->state = 0;
+			break;
+		case 'y':
+			output = y_row[vowel_index];
+			if (output == NULL) {
+				onechar_output[0] = ime->state;
+				output = onechar_output;
+			}
+			ime->state = 0;
+			break;
+		case 'z':
+			output = z_row[vowel_index];
+			ime->state = 0;
+			break;
+		default:
+			onechar_output[0] = ime->state;
+			output = onechar_output;
+			ime->state = c;
+			break;
+		}
+	} else {
+		if (c < ' ' || c > '~')
+			return;
+
+		if ((char) ime->state == 'n') {
+			output = "ん";
+			if (c == 'n')
+				ime->state = 0;
+			else
+				ime->state = c;
+		} else if ((char) ime->state == c) {
+			output = "っ";
+		} else if (ime->state == 0) {
+			ime->state = c;
+		} else {
+			onechar_output[0] = ime->state;
+			output = onechar_output;
+			ime->state = c;
+		}
+	}
+
+	if (output) {
+		fprintf(stderr, "committing '%s'\n", output);
+		input_method_commit_string(ime->input_method, output, -1);
+	}
+}
+
+static void
+input_method_keymap(void *data,
+		    struct input_method *input_method,
+		    uint32_t format,
+		    int32_t fd,
+		    uint32_t size)
+{
+	struct hiragana_ime *ime = data;
+	fprintf(stderr, "received keymap\n");
+
+	// XXX: this is copy-pasted from window.c
+	char *map_str;
+
+	if (!data) {
+		close(fd);
+		return;
+	}
+
+	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;
+	}
+
+	ime->xkb.keymap = xkb_map_new_from_string(ime->xkb.context,
+						  map_str,
+						  XKB_KEYMAP_FORMAT_TEXT_V1,
+						  0);
+	munmap(map_str, size);
+	close(fd);
+
+	if (!ime->xkb.keymap) {
+		fprintf(stderr, "failed to compile keymap\n");
+		return;
+	}
+
+	ime->xkb.state = xkb_state_new(ime->xkb.keymap);
+	if (!ime->xkb.state) {
+		fprintf(stderr, "failed to create XKB state\n");
+		xkb_map_unref(ime->xkb.keymap);
+		ime->xkb.keymap = NULL;
+		return;
+	}
+
+	ime->xkb.control_mask =
+		1 << xkb_map_mod_get_index(ime->xkb.keymap, "Control");
+	ime->xkb.alt_mask =
+		1 << xkb_map_mod_get_index(ime->xkb.keymap, "Mod1");
+	ime->xkb.shift_mask =
+		1 << xkb_map_mod_get_index(ime->xkb.keymap, "Shift");
+}
+
+static void
+input_method_key(void *data,
+		 struct input_method *input_method,
+		 uint32_t serial,
+		 uint32_t time,
+		 uint32_t key,
+		 uint32_t state_w)
+{
+	struct hiragana_ime *ime = data;
+	//fprintf(stderr, "received key %d\n", key);
+
+	// XXX: copied from window.c
+	uint32_t code, num_syms;
+	enum wl_keyboard_key_state state = state_w;
+	const xkb_keysym_t *syms;
+	xkb_keysym_t sym;
+	xkb_mod_mask_t mask;
+
+	code = key + 8;
+	if (!ime->xkb.state)
+		return;
+
+	num_syms = xkb_key_get_syms(ime->xkb.state, code, &syms);
+
+	mask = xkb_state_serialize_mods(ime->xkb.state,
+					XKB_STATE_DEPRESSED |
+					XKB_STATE_LATCHED);
+	ime->modifiers = 0;
+	if (mask & ime->xkb.control_mask)
+		ime->modifiers |= MOD_CONTROL_MASK;
+	if (mask & ime->xkb.alt_mask)
+		ime->modifiers |= MOD_ALT_MASK;
+	if (mask & ime->xkb.shift_mask)
+		ime->modifiers |= MOD_SHIFT_MASK;
+
+	sym = XKB_KEY_NoSymbol;
+	if (num_syms == 1)
+		sym = syms[0];
+
+	ime_handle_key(ime, key, sym, state);
+}
+
+static void
+input_method_modifiers(void *data,
+		       struct input_method *input_method,
+		       uint32_t serial,
+		       uint32_t mods_depressed,
+		       uint32_t mods_latched,
+		       uint32_t mods_locked,
+		       uint32_t group)
+{
+	struct hiragana_ime *ime = data;
+	//fprintf(stderr, "received modifiers %d\n", mods_depressed);
+
+	// XXX: copied from window.c
+	xkb_state_update_mask(ime->xkb.state, mods_depressed, mods_latched,
+			      mods_locked, 0, 0, group);
+}
+
+static const struct input_method_listener input_method_listener = {
+	input_method_keymap,
+	input_method_key,
+	input_method_modifiers
+};
+
+static void
+global_handler(struct wl_display *display, uint32_t id,
+	       const char *interface, uint32_t version, void *data)
+{
+	struct hiragana_ime *ime = data;
+
+	if (!strcmp(interface, "input_method")) {
+		ime->input_method = wl_display_bind(display, id, &input_method_interface);
+		input_method_add_listener(ime->input_method,
+					  &input_method_listener,
+					  ime);
+		input_method_request_keyboard_input(ime->input_method, 1);
+	}
+}
+
+int
+main(int argc, char *argv[])
+{
+	struct hiragana_ime ime;
+	memset(&ime, 0, sizeof ime);
+
+	ime.state = -1;
+
+	ime.xkb.context = xkb_context_new(0);
+	if (ime.xkb.context == NULL) {
+		fprintf(stderr, "Failed to create XKB context\n");
+		return 1;
+	}
+
+	ime.display = display_create(argc, argv);
+	if (ime.display == NULL) {
+		fprintf(stderr, "failed to create display: %m\n");
+		return 1;
+	}
+
+	wl_display_add_global_listener(display_get_display(ime.display),
+				       global_handler, &ime);
+
+	display_set_user_data(ime.display, &ime);
+
+	display_run(ime.display);
+
+	xkb_state_unref(ime.xkb.state);
+	xkb_map_unref(ime.xkb.keymap);
+
+	return 0;
+}
-- 
1.7.11.1



More information about the wayland-devel mailing list