ime: make IME compile-time optional

This commit is contained in:
Daniel Eklöf 2020-12-03 18:36:56 +01:00
parent 8c3d48c5cd
commit 05083110c3
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
9 changed files with 103 additions and 29 deletions

63
ime.c
View file

@ -1,5 +1,7 @@
#include "ime.h"
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
#include <string.h>
#include "text-input-unstable-v3.h"
@ -67,11 +69,7 @@ leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
zwp_text_input_v3_commit(seat->wl_text_input);
seat->ime.serial++;
free(seat->ime.preedit.pending.text);
seat->ime.preedit.pending.text = NULL;
free(seat->ime.commit.pending.text);
seat->ime.commit.pending.text = NULL;
ime_reset(seat);
}
static void
@ -82,10 +80,13 @@ preedit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
struct seat *seat = data;
free(seat->ime.preedit.pending.text);
seat->ime.preedit.pending.text = text != NULL ? xstrdup(text) : NULL;
seat->ime.preedit.pending.cursor_begin = cursor_begin;
seat->ime.preedit.pending.cursor_end = cursor_end;
ime_reset_preedit(seat);
if (text != NULL) {
seat->ime.preedit.pending.text = xstrdup(text);
seat->ime.preedit.pending.cursor_begin = cursor_begin;
seat->ime.preedit.pending.cursor_end = cursor_end;
}
}
static void
@ -95,8 +96,11 @@ commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
LOG_DBG("commit: text=%s", text);
struct seat *seat = data;
free(seat->ime.commit.pending.text);
seat->ime.commit.pending.text = text != NULL ? xstrdup(text) : NULL;
ime_reset_commit(seat);
if (text != NULL)
seat->ime.commit.pending.text = xstrdup(text);
}
static void
@ -142,9 +146,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
/* 1. Delete existing pre-edit text */
if (term->ime.preedit.cells != NULL) {
free(term->ime.preedit.cells);
term->ime.preedit.cells = NULL;
term->ime.preedit.count = 0;
term_reset_ime(term);
render_refresh(term);
}
@ -162,9 +164,7 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
term,
seat->ime.commit.pending.text,
strlen(seat->ime.commit.pending.text));
free(seat->ime.commit.pending.text);
seat->ime.commit.pending.text = NULL;
ime_reset_commit(seat);
}
/* 4. Calculate surrounding text to send - not supported */
@ -292,10 +292,31 @@ done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
render_refresh(term);
}
ime_reset_preedit(seat);
}
void
ime_reset_preedit(struct seat *seat)
{
free(seat->ime.preedit.pending.text);
seat->ime.preedit.pending.text = NULL;
}
void
ime_reset_commit(struct seat *seat)
{
free(seat->ime.commit.pending.text);
seat->ime.commit.pending.text = NULL;
}
void
ime_reset(struct seat *seat)
{
ime_reset_preedit(seat);
ime_reset_commit(seat);
}
const struct zwp_text_input_v3_listener text_input_listener = {
.enter = &enter,
.leave = &leave,
@ -304,3 +325,11 @@ const struct zwp_text_input_v3_listener text_input_listener = {
.delete_surrounding_text = &delete_surrounding_text,
.done = &done,
};
#else /* !FOOT_IME_ENABLED */
void ime_reset_preedit(struct seat *seat) {}
void ime_reset_commit(struct seat *seat) {}
void ime_reset(struct seat *seat) {}
#endif

10
ime.h
View file

@ -1,5 +1,15 @@
#pragma once
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
#include "text-input-unstable-v3.h"
extern const struct zwp_text_input_v3_listener text_input_listener;
#endif /* FOOT_IME_ENABLED */
struct seat;
void ime_reset_preedit(struct seat *seat);
void ime_reset_commit(struct seat *seat);
void ime_reset(struct seat *seat);

View file

@ -17,6 +17,9 @@ add_project_arguments(
(is_debug_build
? ['-D_DEBUG']
: [cc.get_supported_arguments('-fno-asynchronous-unwind-tables')]) +
(get_option('ime')
? ['-DFOOT_IME_ENABLED=1']
: []) +
cc.get_supported_arguments(
['-pedantic',
'-fstrict-aliasing',
@ -198,9 +201,9 @@ subdir('completions')
subdir('doc')
subdir('icons')
# summary(
# {
# '<feature>': false,
# },
# bool_yn: true
# )
summary(
{
'IME': get_option('ime'),
},
bool_yn: true
)

1
meson_options.txt Normal file
View file

@ -0,0 +1 @@
option('ime', type: 'boolean', value: true, description: 'IME (Input Method Editor) support')

View file

@ -1025,6 +1025,8 @@ static void
render_ime_preedit(struct terminal *term, struct buffer *buf,
struct coord cursor)
{
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (likely(term->ime.preedit.cells == NULL))
return;
@ -1112,6 +1114,7 @@ render_ime_preedit(struct terminal *term, struct buffer *buf,
term->margins.top + row_idx * term->cell_height,
term->width - term->margins.left - term->margins.right,
1 * term->cell_height);
#endif
}
static void

View file

@ -1403,7 +1403,7 @@ term_destroy(struct terminal *term)
tll_free(term->alt.sixel_images);
sixel_fini(term);
free(term->ime.preedit.cells);
term_reset_ime(term);
free(term->foot_exe);
free(term->cwd);
@ -2218,12 +2218,12 @@ term_kbd_focus_out(struct terminal *term)
if (it->item.kbd_focus == term)
return;
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (term->ime.preedit.cells != NULL) {
free(term->ime.preedit.cells);
term->ime.preedit.cells = NULL;
term->ime.preedit.count = 0;
term_reset_ime(term);
render_refresh(term);
}
#endif
term->kbd_focus = false;
cursor_refresh(term);
@ -2753,3 +2753,13 @@ term_view_to_text(const struct terminal *term, char **text, size_t *len)
int end = grid_row_absolute_in_view(term->grid, term->rows - 1);
return rows_to_text(term, start, end, text, len);
}
void
term_reset_ime(struct terminal *term)
{
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
free(term->ime.preedit.cells);
term->ime.preedit.cells = NULL;
term->ime.preedit.count = 0;
#endif
}

View file

@ -470,6 +470,7 @@ struct terminal {
unsigned max_height; /* Maximum image height, in pixels */
} sixel;
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
struct {
struct {
struct cell *cells;
@ -482,6 +483,7 @@ struct terminal {
} cursor;
} preedit;
} ime;
#endif
bool quit;
bool is_shutting_down;
@ -605,3 +607,5 @@ bool term_scrollback_to_text(
const struct terminal *term, char **text, size_t *len);
bool term_view_to_text(
const struct terminal *term, char **text, size_t *len);
void term_reset_ime(struct terminal *term);

View file

@ -116,6 +116,7 @@ seat_add_primary_selection(struct seat *seat)
static void
seat_add_text_input(struct seat *seat)
{
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (seat->wayl->text_input_manager == NULL)
return;
@ -128,6 +129,7 @@ seat_add_text_input(struct seat *seat)
seat->wl_text_input = text_input;
zwp_text_input_v3_add_listener(text_input, &text_input_listener, seat);
#endif
}
static void
@ -184,15 +186,18 @@ seat_destroy(struct seat *seat)
wl_keyboard_release(seat->wl_keyboard);
if (seat->wl_pointer != NULL)
wl_pointer_release(seat->wl_pointer);
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (seat->wl_text_input != NULL)
zwp_text_input_v3_destroy(seat->wl_text_input);
#endif
if (seat->wl_seat != NULL)
wl_seat_release(seat->wl_seat);
ime_reset(seat);
free(seat->clipboard.text);
free(seat->primary.text);
free(seat->ime.preedit.pending.text);
free(seat->ime.commit.pending.text);
free(seat->name);
}
@ -920,6 +925,7 @@ handle_global(void *data, struct wl_registry *registry,
}
}
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
const uint32_t required = 1;
if (!verify_iface_version(interface, version, required))
@ -931,6 +937,7 @@ handle_global(void *data, struct wl_registry *registry,
tll_foreach(wayl->seats, it)
seat_add_text_input(&it->item);
}
#endif
}
static void
@ -1190,8 +1197,11 @@ wayl_destroy(struct wayland *wayl)
seat_destroy(&it->item);
tll_free(wayl->seats);
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
if (wayl->text_input_manager != NULL)
zwp_text_input_manager_v3_destroy(wayl->text_input_manager);
#endif
if (wayl->xdg_output_manager != NULL)
zxdg_output_manager_v1_destroy(wayl->xdg_output_manager);
if (wayl->shell != NULL)

View file

@ -224,6 +224,7 @@ struct seat {
struct wl_clipboard clipboard;
struct wl_primary primary;
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
/* Input Method Editor */
struct zwp_text_input_v3 *wl_text_input;
struct {
@ -250,6 +251,7 @@ struct seat {
uint32_t serial;
} ime;
#endif
};
enum csd_surface {
@ -404,7 +406,9 @@ struct wayland {
struct wp_presentation *presentation;
uint32_t presentation_clock_id;
#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
struct zwp_text_input_manager_v3 *text_input_manager;
#endif
bool have_argb8888;
tll(struct monitor) monitors; /* All available outputs */