Skip to content

Commit

Permalink
video/out/wayland_common: support IME usage via text-input-v3
Browse files Browse the repository at this point in the history
This is useful for text input in, for example, console.lua. Each
character in the commit string gets turned into an mpv key press.
Pre-edit strings are not handled, since there's currently no good way to
handle that or make it useful to text input scripts. Like win32, which I
tested in wine, another limitation is that the composition window is
always positioned at the top left of the window, since we cannot get
useful positioning hints from mpv scripts. It allows the composition
window to be within the window and avoids obstructing the console
prompt.

This can be enabled/disabled with --wayland-ime=<yes|no> (default: yes).
  • Loading branch information
layercak3 committed Jan 22, 2025
1 parent c438732 commit 7b9ad9f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
6 changes: 6 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6116,6 +6116,12 @@ them.
Defines the size of an edge border (default: 32) to initiate client side
resizes events in the wayland contexts with touch events.

``--wayland-ime=<yes|no>``
Enable keyboard input via an active input method (IME) (default: yes). The
input popup window, if there is any, is always positioned at the top left of the
window. There is no support for displaying pre-edit text, so you may want to set
your IME to display pre-edit inside of the popup window.

``--wayland-present=<yes|no>``
Enable the use of wayland's presentation time protocol for more accurate
frame presentation if it is supported by the compositor (default: ``yes``).
Expand Down
2 changes: 2 additions & 0 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ static const m_option_t mp_vo_opt_list[] = {
M_RANGE(0, INT_MAX)},
{"wayland-edge-pixels-touch", OPT_INT(wl_edge_pixels_touch),
M_RANGE(0, INT_MAX)},
{"wayland-ime", OPT_BOOL(wl_ime)},
{"wayland-present", OPT_BOOL(wl_present)},
#endif
#if HAVE_WIN32_DESKTOP
Expand Down Expand Up @@ -272,6 +273,7 @@ const struct m_sub_options vo_sub_opts = {
.wl_content_type = -1,
.wl_edge_pixels_pointer = 16,
.wl_edge_pixels_touch = 32,
.wl_ime = true,
.wl_present = true,
.mmcss_profile = "Playback",
.ontop_level = -1,
Expand Down
1 change: 1 addition & 0 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct mp_vo_opts {
bool wl_disable_vsync;
int wl_edge_pixels_pointer;
int wl_edge_pixels_touch;
bool wl_ime;
bool wl_present;

float panscan;
Expand Down
1 change: 1 addition & 0 deletions video/out/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ protocols = [[wl_protocol_dir, 'stable/presentation-time/presentation-time.xml']
[wl_protocol_dir, 'stable/viewporter/viewporter.xml'],
[wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'],
[wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'],
[wl_protocol_dir, 'unstable/text-input/text-input-unstable-v3.xml'],
[wl_protocol_dir, 'unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml'],
[wl_protocol_dir, 'unstable/xdg-decoration/xdg-decoration-unstable-v1.xml'],
[wl_protocol_dir, 'staging/content-type/content-type-v1.xml'],
Expand Down
104 changes: 104 additions & 0 deletions video/out/wayland_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

// Generated from wayland-protocols
#include "idle-inhibit-unstable-v1.h"
#include "text-input-unstable-v3.h"
#include "linux-dmabuf-unstable-v1.h"
#include "presentation-time.h"
#include "xdg-decoration-unstable-v1.h"
Expand Down Expand Up @@ -191,6 +192,7 @@ struct vo_wayland_seat {
struct wl_pointer *pointer;
struct wl_touch *touch;
struct wl_data_device *dnd_ddev;
struct vo_wayland_text_input *text_input;
/* TODO: unvoid this if required wayland protocols is bumped to 1.32+ */
void *cursor_shape_device;
uint32_t pointer_enter_serial;
Expand Down Expand Up @@ -230,6 +232,12 @@ struct vo_wayland_data_offer {
bool offered_plain_text;
};

struct vo_wayland_text_input {
struct zwp_text_input_v3 *text_input;
uint32_t serial;
char *commit_string;
};

static bool single_output_spanned(struct vo_wayland_state *wl);

static int check_for_resize(struct vo_wayland_state *wl, int edge_pixels,
Expand Down Expand Up @@ -877,6 +885,80 @@ static const struct wl_data_device_listener data_device_listener = {
data_device_handle_selection,
};

static void text_input_enter(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
struct vo_wayland_seat *s = data;
struct vo_wayland_text_input *ti = s->text_input;
struct vo_wayland_state *wl = s->wl;

if (!wl->opts->wl_ime)
return;

zwp_text_input_v3_enable(ti->text_input);
zwp_text_input_v3_set_content_type(
ti->text_input,
ZWP_TEXT_INPUT_V3_CONTENT_HINT_NONE,
ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL);
zwp_text_input_v3_set_cursor_rectangle(ti->text_input, 0, 0, 0, 0);
zwp_text_input_v3_commit(ti->text_input);
ti->serial++;
}

static void text_input_leave(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
struct vo_wayland_seat *s = data;
struct vo_wayland_text_input *ti = s->text_input;

zwp_text_input_v3_disable(ti->text_input);
zwp_text_input_v3_commit(ti->text_input);
ti->serial++;
}

static void text_input_preedit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text, int32_t cursor_begin, int32_t cursor_end)
{
}

static void text_input_commit_string(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text)
{
struct vo_wayland_seat *s = data;
struct vo_wayland_text_input *ti = s->text_input;

talloc_replace(ti, ti->commit_string, text);
}

static void text_input_delete_surrounding_text(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t before_length, uint32_t after_length)
{
}

static void text_input_done(void *data, struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t serial)
{
struct vo_wayland_seat *s = data;
struct vo_wayland_text_input *ti = s->text_input;
struct vo_wayland_state *wl = s->wl;

if (ti->serial == serial && ti->commit_string)
mp_input_put_key_utf8(wl->vo->input_ctx, 0, bstr0(ti->commit_string));

if (ti->commit_string)
TA_FREEP(&ti->commit_string);
}

static const struct zwp_text_input_v3_listener text_input_listener = {
text_input_enter,
text_input_leave,
text_input_preedit_string,
text_input_commit_string,
text_input_delete_surrounding_text,
text_input_done,
};

static void output_handle_geometry(void *data, struct wl_output *wl_output,
int32_t x, int32_t y, int32_t phys_width,
int32_t phys_height, int32_t subpixel,
Expand Down Expand Up @@ -1805,6 +1887,11 @@ static void registry_handle_add(void *data, struct wl_registry *reg, uint32_t id
wl->idle_inhibit_manager = wl_registry_bind(reg, id, &zwp_idle_inhibit_manager_v1_interface, ver);
}

if (!strcmp(interface, zwp_text_input_manager_v3_interface.name) && found++) {
ver = 1;
wl->text_input_manager = wl_registry_bind(reg, id, &zwp_text_input_manager_v3_interface, ver);
}

if (found > 1)
MP_VERBOSE(wl, "Registered interface %s at version %d\n", interface, ver);
}
Expand Down Expand Up @@ -2433,6 +2520,8 @@ static void remove_seat(struct vo_wayland_seat *seat)
wl_touch_destroy(seat->touch);
if (seat->dnd_ddev)
wl_data_device_destroy(seat->dnd_ddev);
if (seat->text_input)
zwp_text_input_v3_destroy(seat->text_input->text_input);
#if HAVE_WAYLAND_PROTOCOLS_1_32
if (seat->cursor_shape_device)
wp_cursor_shape_device_v1_destroy(seat->cursor_shape_device);
Expand Down Expand Up @@ -3248,6 +3337,18 @@ bool vo_wayland_init(struct vo *vo)
zwp_idle_inhibit_manager_v1_interface.name);
}

if (wl->text_input_manager) {
struct vo_wayland_seat *seat;
wl_list_for_each(seat, &wl->seat_list, link) {
seat->text_input = talloc_zero(seat, struct vo_wayland_text_input);
seat->text_input->text_input = zwp_text_input_manager_v3_get_text_input(wl->text_input_manager, seat->seat);
zwp_text_input_v3_add_listener(seat->text_input->text_input, &text_input_listener, seat);
}
} else {
MP_VERBOSE(wl, "Compositor doesn't support the %s protocol!\n",
zwp_text_input_manager_v3_interface.name);
}

wl->display_fd = wl_display_get_fd(wl->display);

update_app_id(wl);
Expand Down Expand Up @@ -3406,6 +3507,9 @@ void vo_wayland_uninit(struct vo *vo)
if (wl->idle_inhibit_manager)
zwp_idle_inhibit_manager_v1_destroy(wl->idle_inhibit_manager);

if (wl->text_input_manager)
zwp_text_input_manager_v3_destroy(wl->text_input_manager);

if (wl->presentation)
wp_presentation_destroy(wl->presentation);

Expand Down
3 changes: 3 additions & 0 deletions video/out/wayland_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ struct vo_wayland_state {
struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager;
struct zwp_idle_inhibitor_v1 *idle_inhibitor;

/* text-input */
struct zwp_text_input_manager_v3 *text_input_manager;

/* linux-dmabuf */
struct wl_list tranche_list;
struct vo_wayland_tranche *current_tranche;
Expand Down

0 comments on commit 7b9ad9f

Please sign in to comment.