[PATCH] editor: Add support for editing text using the keyboard

Rob Bradford robert.bradford at intel.com
Thu Oct 11 07:59:29 PDT 2012


From: Rob Bradford <rob at linux.intel.com>

This simple change allows you to drive the editor using the keyboard
(supporting backspace and delete and left and right arrow keys.) The idea
behind this change is to allow the testing of the interoperation between a
virtual keyboard and real one.

Signed-off-by: Rob Bradford <rob at linux.intel.com>
---
 clients/editor.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/clients/editor.c b/clients/editor.c
index ce0692f..3314507 100644
--- a/clients/editor.c
+++ b/clients/editor.c
@@ -64,6 +64,7 @@ struct editor {
 	struct widget *widget;
 	struct text_entry *entry;
 	struct text_entry *editor;
+	struct text_entry *active_entry;
 };
 
 static struct text_layout *
@@ -786,11 +787,14 @@ text_entry_button_handler(struct widget *widget,
 {
 	struct text_entry *entry = data;
 	struct rectangle allocation;
+	struct editor *editor;
 	int32_t x, y;
 
 	widget_get_allocation(entry->widget, &allocation);
 	input_get_position(input, &x, &y);
 
+	editor = window_get_user_data(entry->window);
+
 	if (button != BTN_LEFT) {
 		return;
 	}
@@ -803,6 +807,7 @@ text_entry_button_handler(struct widget *widget,
 		struct wl_seat *seat = input_get_seat(input);
 
 		text_entry_activate(entry, seat);
+		editor->active_entry = entry;
 
 		text_entry_set_anchor_position(entry,
 					       x - allocation.x - text_offset_left,
@@ -831,10 +836,65 @@ editor_button_handler(struct widget *widget,
 
 		text_entry_deactivate(editor->entry, seat);
 		text_entry_deactivate(editor->editor, seat);
+		editor->active_entry = NULL;
 	}
 }
 
 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;
+	char text[16];
+
+	if (!editor->active_entry)
+		return;
+
+	entry = editor->active_entry;
+
+	if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
+		return;
+
+	switch (sym) {
+		case XKB_KEY_BackSpace:
+			if (entry->cursor < 1)
+				break;
+			text_entry_delete_text(entry,
+					       entry->cursor - 1,
+					       1);
+			break;
+		case XKB_KEY_Delete:
+			if (entry->cursor == strlen(entry->text))
+				break;
+			text_entry_delete_text(entry,
+					       entry->cursor,
+					       1);
+			break;
+		case XKB_KEY_Left:
+			if (entry->cursor < 1)
+				break;
+			entry->cursor--;
+			break;
+		case XKB_KEY_Right:
+			if (entry->cursor == strlen(entry->text))
+				break;
+			entry->cursor++;
+			break;
+		default:
+			if (xkb_keysym_to_utf8(sym, text, sizeof(text)) <= 0)
+				break;
+
+			text_entry_insert_at_cursor(entry, text);
+			break;
+	}
+
+	widget_schedule_redraw(entry->widget);
+}
+
+static void
 global_handler(struct wl_display *display, uint32_t id,
 	       const char *interface, uint32_t version, void *data)
 {
@@ -868,6 +928,8 @@ main(int argc, char *argv[])
 	text_entry_set_preedit(editor.editor, "preedit", strlen("preedit"));
 
 	window_set_title(editor.window, "Text Editor");
+	window_set_key_handler(editor.window, key_handler);
+	window_set_user_data(editor.window, &editor);
 
 	widget_set_redraw_handler(editor.widget, redraw_handler);
 	widget_set_resize_handler(editor.widget, resize_handler);
-- 
1.7.11.2



More information about the wayland-devel mailing list