From c6b878a8afb44527c778f518c2da7349dfa33078 Mon Sep 17 00:00:00 2001 From: "duna.oh" Date: Wed, 24 Aug 2022 10:12:38 +0900 Subject: [PATCH] clients: 'text-entry' fix formatting Change-Id: I5f175968d063363be2cb6343d92ea3e27ce6b60f --- clients/text-entry.c | 300 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 242 insertions(+), 58 deletions(-) diff --git a/clients/text-entry.c b/clients/text-entry.c index a806f0e..ce21d02 100644 --- a/clients/text-entry.c +++ b/clients/text-entry.c @@ -51,7 +51,7 @@ struct display { struct xdg_wm_base *wm_base; struct wl_shm *shm; struct wl_seat *seat; - struct wayland_tbm_client *wl_tbm; + struct wayland_tbm_client *wl_tbm; bool has_xrgb; struct wl_text_input_manager *text_input_mgr; @@ -69,13 +69,13 @@ struct window { struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; struct wl_callback *callback; - tbm_surface_queue_h surface_queue; + tbm_surface_queue_h surface_queue; bool wait_for_configure; }; struct buffer_info { - struct window *window; - struct wl_buffer *wl_buffer; + struct window *window; + struct wl_buffer *wl_buffer; }; struct text_entry { @@ -580,19 +580,19 @@ text_entry_update(struct text_entry *entry) fprintf(stderr, "text_entry_update()\n"); wl_text_input_set_content_type(entry->text_input, - WL_TEXT_INPUT_CONTENT_HINT_NONE, - entry->content_purpose); + WL_TEXT_INPUT_CONTENT_HINT_NONE, + entry->content_purpose); if (entry->preferred_language) wl_text_input_set_preferred_language(entry->text_input, - entry->preferred_language); + entry->preferred_language); text_entry_get_cursor_rectangle(entry, &cursor_rectangle); wl_text_input_set_cursor_rectangle(entry->text_input, - cursor_rectangle.x, - cursor_rectangle.y, - cursor_rectangle.width, - cursor_rectangle.height); + cursor_rectangle.x, + cursor_rectangle.y, + cursor_rectangle.width, + cursor_rectangle.height); wl_text_input_commit_state(entry->text_input, ++entry->serial); } @@ -600,7 +600,7 @@ text_entry_update(struct text_entry *entry) static void text_entry_reset_preedit(struct text_entry *entry) { - fprintf(stderr, "text_entry_reset_preedit()\n"); + fprintf(stderr, "text_entry_reset_preedit()\n"); entry->preedit.cursor = 0; free(entry->preedit.text); @@ -664,9 +664,9 @@ text_entry_commit_and_reset(struct text_entry *entry) static void text_input_enter(void *data, struct wl_text_input *text_input, - struct wl_surface *surface) + struct wl_surface *surface) { - fprintf(stderr, "text_input_enter()\n"); + fprintf(stderr, "text_input_enter()\n"); struct display *d = data; struct text_entry *entry = d->entry; @@ -676,15 +676,16 @@ text_input_enter(void *data, struct wl_text_input *text_input, text_entry_update(entry); entry->reset_serial = entry->serial; } + static void text_input_leave(void *data, struct wl_text_input *text_input) { - fprintf(stderr, "text_input_leave()\n"); + fprintf(stderr, "text_input_leave()\n"); struct display *d = data; struct text_entry *entry = d->entry; - text_entry_commit_and_reset(entry); + text_entry_commit_and_reset(entry); d->entry->active--; if (!entry->active) { @@ -695,81 +696,264 @@ text_input_leave(void *data, struct wl_text_input *text_input) static void text_input_modifiers_map(void *data, struct wl_text_input *text_input, - struct wl_array *map) + struct wl_array *map) { - fprintf(stderr, "text_input_modifiers_map()\n"); + fprintf(stderr, "text_input_modifiers_map()\n"); } static void text_input_input_panel_state(void *data, struct wl_text_input *text_input, - uint32_t state) + uint32_t state) +{ + fprintf(stderr, "text_input_input_panel_state() state:%u\n", state); +} + +static void +clear_pending_preedit(struct text_entry *entry) +{ + fprintf(stderr, "clear_pending_preedit()\n"); + + memset(&entry->pending_commit, 0, sizeof entry->pending_commit); + + entry->preedit_info.cursor = 0; + + memset(&entry->preedit_info, 0, sizeof entry->preedit_info); +} + +static void +text_entry_delete_text(struct text_entry *entry, + uint32_t index, uint32_t length) +{ + uint32_t l; + + fprintf(stderr, "text_entry_delete_text()\n"); + + assert(index <= strlen(entry->text)); + assert(index + length <= strlen(entry->text)); + assert(index + length >= length); + + l = strlen(entry->text + index + length); + memmove(entry->text + index, + entry->text + index + length, + l + 1); + + if (entry->cursor > (index + length)) + entry->cursor -= length; + else if (entry->cursor > index) + entry->cursor = index; + + entry->anchor = entry->cursor; + + text_entry_update(entry); +} + +static void +text_entry_delete_selected_text(struct text_entry *entry) +{ + uint32_t start_index = entry->anchor < entry->cursor ? entry->anchor : entry->cursor; + uint32_t end_index = entry->anchor < entry->cursor ? entry->cursor : entry->anchor; + + fprintf(stderr, "text_entry_delete_selected_text()\n"); + + if (entry->anchor == entry->cursor) + return; + + text_entry_delete_text(entry, start_index, end_index - start_index); + + entry->anchor = entry->cursor; +} + +static void +text_entry_set_preedit(struct text_entry *entry, + const char *preedit_text, + int preedit_cursor) { - fprintf(stderr, "text_input_input_panel_state() state:%u\n", state); + fprintf(stderr, "text_entry_set_preedit()\n"); + + text_entry_reset_preedit(entry); + + if (!preedit_text) + return; + + entry->preedit.text = strdup(preedit_text); + entry->preedit.cursor = preedit_cursor; } static void text_input_preedit_string(void *data, struct wl_text_input *text_input, - uint32_t serial, const char *text, const char *commit) + uint32_t serial, const char *text, const char *commit) { - fprintf(stderr, "text_input_preedit_string() serial(%u), text(%s), commit(%s)\n", serial, text, commit); + struct display *d = data; + struct text_entry *entry = d->entry; + + fprintf(stderr, "text_input_preedit_string() serial(%u), text(%s), commit(%s)\n", serial, text, commit); + + if ((entry->serial - serial) > (entry->serial - entry->reset_serial)) { + fprintf(stderr, "Ignore preedit_string. Serial: %u, Current: %u, Reset: %u\n", + serial, entry->serial, entry->reset_serial); + clear_pending_preedit(entry); + return; + } + + if (entry->pending_commit.invalid_delete) { + fprintf(stderr, "Ignore preedit_string. Invalid previous delete_surrounding event.\n"); + clear_pending_preedit(entry); + return; + } + + if (entry->pending_commit.delete_length) { + text_entry_delete_text(entry, + entry->pending_commit.delete_index, + entry->pending_commit.delete_length); + } else { + text_entry_delete_selected_text(entry); + } + + text_entry_set_preedit(entry, text, entry->preedit_info.cursor); + entry->preedit.commit = strdup(commit); + + clear_pending_preedit(entry); + + text_entry_update(entry); } static void text_input_preedit_styling(void *data, struct wl_text_input *text_input, - uint32_t index, uint32_t length, uint32_t style) + uint32_t index, uint32_t length, uint32_t style) { - fprintf(stderr, "text_input_preedit_styling() index(%u), length(%u), style(%u)\n", index, length, style); + fprintf(stderr, "text_input_preedit_styling() index(%u), length(%u), style(%u)\n", index, length, style); + + switch (style) { + case WL_TEXT_INPUT_PREEDIT_STYLE_DEFAULT: + fprintf(stderr, "text_input_preedit_styling() style:DEFAULT"); + break; + case WL_TEXT_INPUT_PREEDIT_STYLE_UNDERLINE: + fprintf(stderr, "text_input_preedit_styling() style:UNDERLINE"); + break; + case WL_TEXT_INPUT_PREEDIT_STYLE_INCORRECT: + fprintf(stderr, "text_input_preedit_styling() style:INCORRECT"); + break; + case WL_TEXT_INPUT_PREEDIT_STYLE_SELECTION: + fprintf(stderr, "text_input_preedit_styling() style:SELECTION"); + break; + case WL_TEXT_INPUT_PREEDIT_STYLE_HIGHLIGHT: + fprintf(stderr, "text_input_preedit_styling() style:HIGHLIGHT"); + break; + case WL_TEXT_INPUT_PREEDIT_STYLE_ACTIVE: + fprintf(stderr, "text_input_preedit_styling() style:ACTIVE"); + break; + case WL_TEXT_INPUT_PREEDIT_STYLE_INACTIVE: + fprintf(stderr, "text_input_preedit_styling() style:INACTIVE"); + break; + default: + fprintf(stderr, "text_input_preedit_styling() no style enum found"); + break; + } } static void text_input_preedit_cursor(void *data, struct wl_text_input *text_input, - int32_t index) + int32_t index) { - fprintf(stderr, "text_input_preedit_cursor() index(%u)\n", index); + struct display *d = data; + struct text_entry *entry = d->entry; + + fprintf(stderr, "text_input_preedit_cursor() index(%u)\n", index); + + entry->preedit_info.cursor = index; } static void text_input_commit_string(void *data, struct wl_text_input *text_input, - uint32_t serial, const char *text) + uint32_t serial, const char *text) { - fprintf(stderr, "text_input_commit_string() serial(%u), text(%s)\n", serial, text); + struct display *d = data; + struct text_entry *entry = d->entry; + + fprintf(stderr, "text_input_commit_string() serial(%u), text(%s)\n", serial, text); + + if ((entry->serial - serial) > (entry->serial - entry->reset_serial)) { + fprintf(stderr, "Ignore commit. Serial: %u, Current: %u, Reset: %u\n", + serial, entry->serial, entry->reset_serial); + return; + } + + if (entry->pending_commit.invalid_delete) { + fprintf(stderr, "Ignore commit. Invalid previous delete_surrounding event.\n"); + memset(&entry->pending_commit, 0, sizeof entry->pending_commit); + return; + } + + text_entry_reset_preedit(entry); + + if (entry->pending_commit.delete_length) { + text_entry_delete_text(entry, + entry->pending_commit.delete_index, + entry->pending_commit.delete_length); + } else { + text_entry_delete_selected_text(entry); + } + + text_entry_insert_at_cursor(entry, text, + entry->pending_commit.cursor, + entry->pending_commit.anchor); + + memset(&entry->pending_commit, 0, sizeof entry->pending_commit); } static void text_input_cursor_position(void *data, struct wl_text_input *text_input, - int32_t index, int32_t anchor) + int32_t index, int32_t anchor) { - fprintf(stderr, "text_input_cursor_position() index(%d), anchor(%d)\n", index, anchor); + fprintf(stderr, "text_input_cursor_position() index(%d), anchor(%d)\n", index, anchor); } static void text_input_delete_surrounding_text(void *data, - struct wl_text_input *text_input, int32_t index, uint32_t length) + struct wl_text_input *text_input, int32_t index, uint32_t length) { - fprintf(stderr, "text_input_delete_surrounding_text() index(%d), length(%u)\n", index, length); + struct text_entry *entry = data; + uint32_t text_length; + + fprintf(stderr, "text_input_delete_surrounding_text() index(%d), length(%u)\n", index, length); + + entry->pending_commit.delete_index = entry->cursor + index; + entry->pending_commit.delete_length = length; + entry->pending_commit.invalid_delete = false; + + text_length = strlen(entry->text); + + if (entry->pending_commit.delete_index > text_length || + length > text_length || + entry->pending_commit.delete_index + length > text_length) { + fprintf(stderr, "delete_surrounding_text: Invalid index: %d," \ + "length %u'; cursor: %u text length: %u\n", index, length, entry->cursor, text_length); + entry->pending_commit.invalid_delete = true; + return; + } } static void text_input_keysym(void *data, struct wl_text_input *text_input, - uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, - uint32_t modifiers) + uint32_t serial, uint32_t time, uint32_t sym, uint32_t state, + uint32_t modifiers) { - fprintf(stderr, "text_input_keysym() serial(%u), time(%u), sym(%u), state(%u), modifiers(%u)\n", - serial, time, sym, state, modifiers); + fprintf(stderr, "text_input_keysym() serial(%u), time(%u), sym(%u), state(%u), modifiers(%u)\n", + serial, time, sym, state, modifiers); } static void text_input_language(void *data, struct wl_text_input *text_input, - uint32_t serial, const char *language) + uint32_t serial, const char *language) { - fprintf(stderr, "text_input_language() serial(%u), language(%s)\n", serial, language); + fprintf(stderr, "text_input_language() serial(%u), language(%s)\n", serial, language); } static void text_input_text_direction(void *data, struct wl_text_input *text_input, - uint32_t serial, uint32_t direction) + uint32_t serial, uint32_t direction) { - fprintf(stderr, "text_input_text_direction() serial(%d), direction(%d)\n", serial, direction); + fprintf(stderr, "text_input_text_direction() serial(%d), direction(%d)\n", serial, direction); } static const struct wl_text_input_listener text_input_listener = { @@ -786,7 +970,7 @@ static const struct wl_text_input_listener text_input_listener = { .keysym = text_input_keysym, .language = text_input_language, .text_direction = text_input_text_direction, - // TIZEN_ONLY(20150918): Support to set the selection region + // TIZEN_ONLY .selection_region = NULL, .private_command = NULL, .input_panel_geometry = NULL, @@ -807,7 +991,7 @@ text_entry_activate(struct display *d) struct wl_surface *surface = d->entered_surface; struct text_entry *entry = d->entry; - fprintf(stderr, "text_entry_activate\n"); + fprintf(stderr, "text_entry_activate\n"); if (entry->click_to_show && entry->active) { entry->panel_visible = !entry->panel_visible; @@ -823,8 +1007,8 @@ text_entry_activate(struct display *d) wl_text_input_show_input_panel(entry->text_input); wl_text_input_activate(entry->text_input, - d->seat, - surface); + d->seat, + surface); } static void @@ -832,9 +1016,9 @@ text_entry_deactivate(struct display *d) { struct text_entry *entry = d->entry; - fprintf(stderr, "text_entry_deactivate\n"); + fprintf(stderr, "text_entry_deactivate\n"); wl_text_input_deactivate(entry->text_input, - d->seat); + d->seat); } static void @@ -858,7 +1042,7 @@ text_entry_create(struct display *d, const char *text) entry = calloc(1, sizeof *entry); if (!entry) - return NULL; + return NULL; entry->text = strdup(text); entry->active = 0; @@ -869,10 +1053,10 @@ text_entry_create(struct display *d, const char *text) entry->text_input = wl_text_input_manager_create_text_input(d->text_input_mgr); wl_text_input_add_listener(entry->text_input, - &text_input_listener, d); + &text_input_listener, d); - d->entry = entry; - fprintf(stderr, "text_entry_create() entry(%p) created.\n", entry); + d->entry = entry; + fprintf(stderr, "text_entry_create() entry(%p) created.\n", entry); return entry; } @@ -990,11 +1174,11 @@ create_display(void) exit(1); } - display->wl_tbm = wayland_tbm_client_init(display->display); - if (!display->wl_tbm) { - fprintf(stderr, "failed wayland_tbm_client_init()\n"); - exit(1); - } + display->wl_tbm = wayland_tbm_client_init(display->display); + if (!display->wl_tbm) { + fprintf(stderr, "failed wayland_tbm_client_init()\n"); + exit(1); + } display->notified = -1; @@ -1007,10 +1191,10 @@ destroy_display(struct display *display) text_entry_destroy(display); if (display->text_input_mgr) - wl_text_input_manager_destroy(display->text_input_mgr); + wl_text_input_manager_destroy(display->text_input_mgr); if (display->seat) - wl_seat_destroy(display->seat); + wl_seat_destroy(display->seat); if (display->shm) wl_shm_destroy(display->shm); @@ -1021,7 +1205,7 @@ destroy_display(struct display *display) if (display->compositor) wl_compositor_destroy(display->compositor); - wayland_tbm_client_deinit(display->wl_tbm); + wayland_tbm_client_deinit(display->wl_tbm); wl_registry_destroy(display->registry); wl_display_flush(display->display); wl_display_disconnect(display->display); -- 2.34.1