Key event from input method may send key from text_input_keysym, which
should share the same logic with real keyboard event. Right now keysym
like latin characters are not handled by text_input_keysym.

Signed-off-by: Weng Xuetian <wen...@gmail.com>
---
 clients/editor.c | 150 +++++++++++++++++--------------------------------------
 1 file changed, 45 insertions(+), 105 deletions(-)

diff --git a/clients/editor.c b/clients/editor.c
index a0cc97a..b84687b 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -47,6 +47,8 @@
 struct text_entry {
        struct widget *widget;
        struct window *window;
+       struct input *input;
+       struct editor *editor;
        char *text;
        int active;
        uint32_t cursor;
@@ -185,6 +187,11 @@ static void text_entry_get_cursor_rectangle(struct 
text_entry *entry, struct rec
 static void text_entry_update(struct text_entry *entry);
 
 static void
+handle_key(struct text_entry *entry, uint32_t time,
+          uint32_t sym, enum wl_keyboard_key_state state,
+          uint32_t modifiers);
+
+static void
 text_input_commit_string(void *data,
                         struct zwp_text_input_v1 *text_input,
                         uint32_t serial,
@@ -395,81 +402,8 @@ text_input_keysym(void *data,
                  uint32_t modifiers)
 {
        struct text_entry *entry = data;
-       const char *new_char;
-
-       if (key == XKB_KEY_Left ||
-           key == XKB_KEY_Right) {
-               if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
-                       return;
-
-               if (key == XKB_KEY_Left)
-                       new_char = utf8_prev_char(entry->text, entry->text + 
entry->cursor);
-               else
-                       new_char = utf8_next_char(entry->text + entry->cursor);
-
-               if (new_char != NULL) {
-                       entry->cursor = new_char - entry->text;
-               }
-
-               if (!(modifiers & entry->keysym.shift_mask))
-                       entry->anchor = entry->cursor;
-               widget_schedule_redraw(entry->widget);
-
-               return;
-       }
-
-       if (key == XKB_KEY_Up ||
-           key == XKB_KEY_Down) {
-               if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
-                       return;
-
-               if (key == XKB_KEY_Up)
-                       move_up(entry->text, &entry->cursor);
-               else
-                       move_down(entry->text, &entry->cursor);
-
-               if (!(modifiers & entry->keysym.shift_mask))
-                       entry->anchor = entry->cursor;
-               widget_schedule_redraw(entry->widget);
-
-               return;
-       }
-
-       if (key == XKB_KEY_BackSpace) {
-               const char *start, *end;
-
-               if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
-                       return;
-
-               text_entry_commit_and_reset(entry);
-
-               start = utf8_prev_char(entry->text, entry->text + 
entry->cursor);
-               if (start == NULL)
-                       return;
-
-               end = utf8_next_char(start);
-
-               text_entry_delete_text(entry,
-                                      start - entry->text,
-                                      end - start);
-
-               return;
-       }
-
-       if (key == XKB_KEY_Tab ||
-           key == XKB_KEY_KP_Enter ||
-           key == XKB_KEY_Return) {
-               char text[16];
-
-               if (state != WL_KEYBOARD_KEY_STATE_RELEASED)
-                       return;
-
-               xkb_keysym_to_utf8(key, text, sizeof(text));
-
-               text_entry_insert_at_cursor(entry, text, 0, 0);
 
-               return;
-       }
+       handle_key(entry, time, key, state, modifiers);
 }
 
 static void
@@ -697,6 +631,7 @@ text_entry_create(struct editor *editor, const char *text)
 
        entry->widget = widget_add_widget(editor->widget, entry);
        entry->window = editor->window;
+       entry->editor = editor;
        entry->text = strdup(text);
        entry->active = 0;
        entry->cursor = strlen(text);
@@ -782,8 +717,9 @@ resize_handler(struct widget *widget,
 
 static void
 text_entry_activate(struct text_entry *entry,
-                   struct wl_seat *seat)
+                   struct input *input)
 {
+       struct wl_seat *seat = input_get_seat(input);
        struct wl_surface *surface = window_get_wl_surface(entry->window);
 
        if (entry->click_to_show && entry->active) {
@@ -795,6 +731,7 @@ text_entry_activate(struct text_entry *entry,
        if (!entry->click_to_show)
                zwp_text_input_v1_show_input_panel(entry->text_input);
 
+       entry->input = input;
        zwp_text_input_v1_activate(entry->text_input,
                                   seat,
                                   surface);
@@ -802,8 +739,10 @@ text_entry_activate(struct text_entry *entry,
 
 static void
 text_entry_deactivate(struct text_entry *entry,
-                     struct wl_seat *seat)
+                     struct input *input)
 {
+       struct wl_seat *seat = input_get_seat(input);
+       entry->input = NULL;
        zwp_text_input_v1_deactivate(entry->text_input,
                                     seat);
 }
@@ -1285,9 +1224,7 @@ text_entry_button_handler(struct widget *widget,
 
        if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
            button == BTN_LEFT) {
-               struct wl_seat *seat = input_get_seat(input);
-
-               text_entry_activate(entry, seat);
+               text_entry_activate(entry, input);
                editor->active_entry = entry;
 
                text_entry_set_cursor_position(entry, x, y, true);
@@ -1300,7 +1237,6 @@ text_entry_touch_handler(struct widget *widget, struct 
input *input,
                         float tx, float ty, void *data)
 {
        struct text_entry *entry = data;
-       struct wl_seat *seat = input_get_seat(input);
        struct rectangle allocation;
        struct editor *editor;
        int32_t x, y;
@@ -1311,7 +1247,7 @@ text_entry_touch_handler(struct widget *widget, struct 
input *input,
        y = ty - (allocation.y + text_offset_top(&allocation));
 
        editor = window_get_user_data(entry->window);
-       text_entry_activate(entry, seat);
+       text_entry_activate(entry, input);
        editor->active_entry = entry;
 
        text_entry_set_cursor_position(entry, x, y, true);
@@ -1330,10 +1266,8 @@ editor_button_handler(struct widget *widget,
        }
 
        if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
-               struct wl_seat *seat = input_get_seat(input);
-
-               text_entry_deactivate(editor->entry, seat);
-               text_entry_deactivate(editor->editor, seat);
+               text_entry_deactivate(editor->entry, input);
+               text_entry_deactivate(editor->editor, input);
                editor->active_entry = NULL;
        }
 }
@@ -1345,10 +1279,8 @@ editor_touch_handler(struct widget *widget, struct input 
*input,
 {
        struct editor *editor = data;
 
-       struct wl_seat *seat = input_get_seat(input);
-
-       text_entry_deactivate(editor->entry, seat);
-       text_entry_deactivate(editor->editor, seat);
+       text_entry_deactivate(editor->entry, input);
+       text_entry_deactivate(editor->editor, input);
        editor->active_entry = NULL;
 }
 
@@ -1381,29 +1313,19 @@ handle_bound_key(struct editor *editor,
 }
 
 static void
-key_handler(struct window *window,
-           struct input *input, uint32_t time,
-           uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
-           void *data)
-{
-       struct editor *editor = data;
-       struct text_entry *entry;
+handle_key(struct text_entry *entry, uint32_t time,
+          uint32_t sym, enum wl_keyboard_key_state state,
+          uint32_t modifiers) {
+       struct input *input = entry->input;
        const char *new_char;
        char text[16];
-       uint32_t modifiers;
-
-       if (!editor->active_entry)
-               return;
-
-       entry = editor->active_entry;
-
-       if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
+       if (!input || state != WL_KEYBOARD_KEY_STATE_PRESSED)
                return;
 
        modifiers = input_get_modifiers(input);
        if ((modifiers & MOD_CONTROL_MASK) &&
            (modifiers & MOD_SHIFT_MASK) &&
-           handle_bound_key(editor, input, sym, time))
+           handle_bound_key(entry->editor, input, sym, time))
                return;
 
        switch (sym) {
@@ -1476,6 +1398,24 @@ key_handler(struct window *window,
        }
 
        widget_schedule_redraw(entry->widget);
+
+}
+
+static void
+key_handler(struct window *window,
+           struct input *input, uint32_t time,
+           uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
+           void *data)
+{
+       struct editor *editor = data;
+       struct text_entry *entry;
+
+       if (!editor->active_entry)
+               return;
+
+       entry = editor->active_entry;
+       entry->input = input;
+       handle_key(entry, time, sym, state, input_get_modifiers(input));
 }
 
 static void
-- 
2.10.0

_______________________________________________
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to