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

Jan Arne Petersen jpetersen at openismus.com
Mon Sep 17 06:37:17 PDT 2012


Hi,

On 09/14/2012 07:21 PM, Rob Bradford wrote:
> 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.

I plan to add support for real keyboards to the text protocol later, but
even than it will be good to be able to compare the behaviour with the
normal keyboard interface. Looks good to me, it should just also update
the anchor on cursor movement.

> ---
>  clients/editor.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 62 insertions(+)
> 
> diff --git a/clients/editor.c b/clients/editor.c
> index 301cbe2..fba92cb 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 *
> @@ -739,11 +740,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;
>  	}
> @@ -756,6 +760,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,
> @@ -784,10 +789,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--;

It should set entry->anchor = entry->cursor (except when shift key is
pressed).

> +			break;
> +		case XKB_KEY_Right:
> +			if (entry->cursor == strlen(entry->text))
> +				break;
> +			entry->cursor++;

Here also.

> +			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)
>  {
> @@ -821,6 +881,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);
> 



More information about the wayland-devel mailing list