wlroots: add support for zwp_text_input_unstable_v3

master
Dorota Czaplejewicz 7 years ago
parent 0347d542ee
commit 427735fcd9

@ -94,6 +94,10 @@ examples = {
'src': 'toplevel-decoration.c',
'dep': [wayland_client, wlr_protos, wlroots],
},
'text-input': {
'src': 'text-input.c',
'dep': [wayland_cursor, wayland_client, wlr_protos, wlroots],
},
}
foreach name, info : examples

@ -0,0 +1,394 @@
#define _POSIX_C_SOURCE 200809L
#include <GLES2/gl2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include <wlr/render/egl.h>
#include "text-input-unstable-v3-client-protocol.h"
#include "xdg-shell-client-protocol.h"
const char usage[] = "Usage: text-input [seconds [width height]]\n\
\n\
Creates a xdg-toplevel using the text-input protocol.\n\
It will be solid black when it has no text input focus, yellow when it\n\
has focus, and red when it was notified that the focus moved away\n\
but still didn't give up the text input ability.\n\
\n\
The \"seconds\" argument is optional and defines the delay between getting\n\
notified of lost focus and releasing text input.\n\
\n\
The \"width\" and \"height\" arguments define the window shape.\n\
\n\
The console will print the internal state of the text field:\n\
- the text in the 1st line\n\
- \".\" under each preedit character\n\
- \"_\" under each selected preedit character\n\
- \"|\" at the cursor position if there are no selected characters in the\n\
preedit.\n\
\n\
The cursor positions may be inaccurate, especially in presence of zero-width\n\
characters or non-monospaced fonts.\n";
struct text_input_state {
char *commit;
struct {
char *text;
int32_t cursor_begin;
int32_t cursor_end;
} preedit;
struct {
uint32_t after_length;
uint32_t before_length;
} delete_surrounding;
};
static struct text_input_state pending = {0};
static struct text_input_state current = {0};
static bool entered = false;
static uint32_t serial;
static char *buffer; // text buffer
// cursor is not present, there's no way to move it outside of preedit
static int sleeptime = 0;
static int width = 100, height = 200;
static int enabled = 0;
static struct wl_display *display = NULL;
static struct wl_compositor *compositor = NULL;
static struct wl_seat *seat = NULL;
static struct xdg_wm_base *wm_base = NULL;
static struct zwp_text_input_manager_v3 *text_input_manager = NULL;
static struct zwp_text_input_v3 *text_input = NULL;
struct wlr_egl egl;
struct wl_egl_window *egl_window;
struct wlr_egl_surface *egl_surface;
static void draw(void) {
eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context);
float color[] = {1.0, 1.0, 0.0, 1.0};
color[0] = enabled * 1.0;
color[1] = entered * 1.0;
glViewport(0, 0, width, height);
glClearColor(color[0], color[1], color[2], 1.0);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(egl.display, egl_surface);
}
static size_t utf8_strlen(char *str) {
size_t cp_count = 0;
for (; *str != '\0'; str++) {
if ((*str & 0xc0) != 0x80) {
cp_count++;
}
}
return cp_count;
}
static size_t utf8_offset(char *utf8_str, size_t byte_offset) {
size_t cp_count = 0;
for (char *c = utf8_str; c < utf8_str + byte_offset; c++) {
if ((*c & 0xc0) != 0x80) {
cp_count++;
}
}
return cp_count;
}
// TODO: would be nicer to have this text display inside the window
static void show_status() {
printf("State %d:", serial);
if (!enabled) {
printf(" disabled");
}
char *preedit_text = current.preedit.text;
if (!preedit_text) {
preedit_text = "";
}
printf("\n");
printf("%s", buffer);
printf("%s\n", preedit_text);
// Positioning of the cursor requires UTF8 offsets to match monospaced
// glyphs
for (unsigned i = 0; i < utf8_strlen(buffer); i++) {
printf(" ");
}
char *cursor_mark = calloc(utf8_strlen(preedit_text) + 2, sizeof(char));
for (unsigned i = 0; i < utf8_strlen(preedit_text); i++) {
cursor_mark[i] = '.';
}
if (current.preedit.cursor_begin == -1
&& current.preedit.cursor_end == -1) {
goto end;
}
if (current.preedit.cursor_begin == -1
|| current.preedit.cursor_end == -1) {
printf("Only one cursor side is defined: %d to %d\n",
current.preedit.cursor_begin, current.preedit.cursor_end);
goto end;
}
if ((unsigned)current.preedit.cursor_begin > strlen(preedit_text)
|| (unsigned)current.preedit.cursor_begin > strlen(preedit_text)) {
printf("Cursor out of bounds\n");
goto end;
}
if (current.preedit.cursor_begin == current.preedit.cursor_end) {
cursor_mark[utf8_offset(preedit_text, current.preedit.cursor_begin)]
= '|';
goto print;
}
if (current.preedit.cursor_begin > current.preedit.cursor_end) {
printf("End cursor is before start cursor\n");
goto end;
}
// negative offsets already checked before
for (unsigned i = utf8_offset(preedit_text, current.preedit.cursor_begin);
i < utf8_offset(preedit_text, current.preedit.cursor_end); i++) {
cursor_mark[i] = '_';
}
print:
printf("%s\n", cursor_mark);
end:
free(cursor_mark);
}
static void commit(struct zwp_text_input_v3 *text_input) {
zwp_text_input_v3_commit(text_input);
serial++;
}
static void send_status_update(struct zwp_text_input_v3 *text_input) {
zwp_text_input_v3_set_surrounding_text(text_input, buffer, strlen(buffer), strlen(buffer));
zwp_text_input_v3_set_text_change_cause(text_input, ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD);
commit(text_input);
}
static void text_input_handle_enter(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface) {
entered = true;
zwp_text_input_v3_enable(zwp_text_input_v3);
commit(zwp_text_input_v3);
enabled = true;
draw();
show_status();
}
static void text_input_handle_leave(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface) {
entered = false;
draw();
wl_display_roundtrip(display);
sleep(sleeptime);
zwp_text_input_v3_disable(zwp_text_input_v3);
commit(zwp_text_input_v3);
enabled = false;
draw();
show_status();
}
static void text_input_commit_string(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text) {
free(pending.commit);
pending.commit = strdup(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) {
pending.delete_surrounding.before_length = before_length;
pending.delete_surrounding.after_length = after_length;
}
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) {
free(pending.preedit.text);
pending.preedit.text = strdup(text);
pending.preedit.cursor_begin = cursor_begin;
pending.preedit.cursor_end = cursor_end;
}
static void text_input_handle_done(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t incoming_serial) {
if (serial != incoming_serial) {
fprintf(stderr, "Received serial %d while expecting %d\n", incoming_serial, serial);
return;
}
free(current.preedit.text);
free(current.commit);
current = pending;
struct text_input_state empty = {0};
pending = empty;
if (current.delete_surrounding.after_length + current.delete_surrounding.before_length > 0) {
// cursor is always after committed text, after_length != 0 will never happen
unsigned delete_before = current.delete_surrounding.before_length;
if (delete_before > strlen(buffer)) {
delete_before = strlen(buffer);
}
buffer[strlen(buffer) - delete_before] = '\0';
}
char *commit_string = current.commit;
if (!commit_string) {
commit_string = "";
}
char *old_buffer = buffer;
buffer = calloc(strlen(buffer) + strlen(commit_string) + 1, sizeof(char)); // realloc may fail anyway
strcpy(buffer, old_buffer);
free(old_buffer);
strcat(buffer, commit_string);
send_status_update(zwp_text_input_v3);
show_status();
}
static const struct zwp_text_input_v3_listener text_input_listener = {
.enter = text_input_handle_enter,
.leave = text_input_handle_leave,
.commit_string = text_input_commit_string,
.delete_surrounding_text = text_input_delete_surrounding_text,
.preedit_string = text_input_preedit_string,
.done = text_input_handle_done,
};
static void xdg_surface_handle_configure(void *data,
struct xdg_surface *xdg_surface, uint32_t serial) {
xdg_surface_ack_configure(xdg_surface, serial);
wl_egl_window_resize(egl_window, width, height, 0, 0);
draw();
}
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_handle_configure,
};
static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h,
struct wl_array *states) {
width = w;
height = h;
}
static void xdg_toplevel_handle_close(void *data,
struct xdg_toplevel *xdg_toplevel) {
exit(EXIT_SUCCESS);
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_handle_configure,
.close = xdg_toplevel_handle_close,
};
static void handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) {
if (strcmp(interface, "wl_compositor") == 0) {
compositor = wl_registry_bind(registry, name,
&wl_compositor_interface, 1);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
} else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) {
text_input_manager = wl_registry_bind(registry, name,
&zwp_text_input_manager_v3_interface, 1);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
}
}
static void handle_global_remove(void *data, struct wl_registry *registry,
uint32_t name) {
// who cares
}
static const struct wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = handle_global_remove,
};
int main(int argc, char **argv) {
if (argc > 1) {
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
printf(usage);
return 0;
}
sleeptime = atoi(argv[1]);
if (argc > 3) {
width = atoi(argv[2]);
height = atoi(argv[3]);
}
}
buffer = calloc(1, sizeof(char));
display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "Failed to create display\n");
return EXIT_FAILURE;
}
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, NULL);
wl_display_dispatch(display);
wl_display_roundtrip(display);
if (compositor == NULL) {
fprintf(stderr, "wl-compositor not available\n");
return EXIT_FAILURE;
}
if (wm_base == NULL) {
fprintf(stderr, "xdg-shell not available\n");
return EXIT_FAILURE;
}
if (text_input_manager == NULL) {
fprintf(stderr, "text-input not available\n");
return EXIT_FAILURE;
}
text_input = zwp_text_input_manager_v3_get_text_input(text_input_manager, seat);
zwp_text_input_v3_add_listener(text_input, &text_input_listener, NULL);
wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL,
WL_SHM_FORMAT_ARGB8888);
struct wl_surface *surface = wl_compositor_create_surface(compositor);
struct xdg_surface *xdg_surface =
xdg_wm_base_get_xdg_surface(wm_base, surface);
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
wl_surface_commit(surface);
egl_window = wl_egl_window_create(surface, width, height);
egl_surface = wlr_egl_create_surface(&egl, egl_window);
wl_display_roundtrip(display);
draw();
while (wl_display_dispatch(display) != -1) {
// This space intentionally left blank
}
return EXIT_SUCCESS;
}

@ -30,6 +30,7 @@ install_headers(
'wlr_tablet_pad.h',
'wlr_tablet_tool.h',
'wlr_tablet_v2.h',
'wlr_text_input_v3.h',
'wlr_touch.h',
'wlr_virtual_keyboard_v1.h',
'wlr_wl_shell.h',

@ -0,0 +1,93 @@
/*
* This an unstable interface of wlroots. No guarantees are made regarding the
* future consistency of this API.
*/
#ifndef WLR_USE_UNSTABLE
#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features"
#endif
#ifndef WLR_TYPES_WLR_TEXT_INPUT_V3_H
#define WLR_TYPES_WLR_TEXT_INPUT_V3_H
#include <wayland-server.h>
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_surface.h>
struct wlr_text_input_v3_state {
struct {
char *text; // NULL is allowed and equivalent to empty string
uint32_t cursor;
uint32_t anchor;
} surrounding;
uint32_t text_change_cause;
struct {
uint32_t hint;
uint32_t purpose;
} content_type;
struct {
int32_t x;
int32_t y;
int32_t width;
int32_t height;
} cursor_rectangle;
};
struct wlr_text_input_v3 {
struct wlr_seat *seat; // becomes null when seat destroyed
struct wl_resource *resource;
struct wlr_surface *focused_surface;
struct wlr_text_input_v3_state pending;
struct wlr_text_input_v3_state current;
uint32_t current_serial; // next in line to send
bool pending_enabled;
bool current_enabled;
struct wl_list link;
struct wl_listener surface_destroy;
struct wl_listener seat_destroy;
struct {
struct wl_signal enable; // (struct wlr_text_input_v3*)
struct wl_signal commit; // (struct wlr_text_input_v3*)
struct wl_signal disable; // (struct wlr_text_input_v3*)
struct wl_signal destroy; // (struct wlr_text_input_v3*)
} events;
};
struct wlr_text_input_manager_v3 {
struct wl_global *global;
struct wl_list bound_resources; // struct wl_resource*::link
struct wl_list text_inputs; // struct wlr_text_input_v3::resource::link
struct wl_listener display_destroy;
struct {
struct wl_signal text_input; // (struct wlr_text_input_v3*)
struct wl_signal destroy; // (struct wlr_input_method_manager_v3*)
} events;
};
struct wlr_text_input_manager_v3 *wlr_text_input_manager_v3_create(
struct wl_display *wl_display);
void wlr_text_input_manager_v3_destroy(
struct wlr_text_input_manager_v3 *manager);
// Sends enter to the surface and saves it
void wlr_text_input_v3_send_enter(struct wlr_text_input_v3 *text_input,
struct wlr_surface *wlr_surface);
// Sends leave to the currently focused surface and clears it
void wlr_text_input_v3_send_leave(struct wlr_text_input_v3 *text_input);
void wlr_text_input_v3_send_preedit_string(struct wlr_text_input_v3 *text_input,
const char *text, uint32_t cursor_begin, uint32_t cursor_end);
void wlr_text_input_v3_send_commit_string(struct wlr_text_input_v3 *text_input,
const char *text);
void wlr_text_input_v3_send_delete_surrounding_text(
struct wlr_text_input_v3 *text_input, uint32_t before_length,
uint32_t after_length);
void wlr_text_input_v3_send_done(struct wlr_text_input_v3 *text_input);
#endif

@ -24,6 +24,7 @@ protocols = [
'idle.xml',
'screenshooter.xml',
'server-decoration.xml',
'text-input-unstable-v3.xml',
'virtual-keyboard-unstable-v1.xml',
'wlr-export-dmabuf-unstable-v1.xml',
'wlr-gamma-control-unstable-v1.xml',
@ -40,6 +41,7 @@ client_protocols = [
[wl_protocol_dir, 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'],
'idle.xml',
'screenshooter.xml',
'text-input-unstable-v3.xml',
'wlr-export-dmabuf-unstable-v1.xml',
'wlr-gamma-control-unstable-v1.xml',
'wlr-input-inhibitor-unstable-v1.xml',

@ -0,0 +1,441 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="text_input_unstable_v3">
<copyright>
Copyright © 2012, 2013 Intel Corporation
Copyright © 2015, 2016 Jan Arne Petersen
Copyright © 2017, 2018 Red Hat, Inc.
Copyright © 2018 Purism SPC
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of
the copyright holders not be used in advertising or publicity
pertaining to distribution of the software without specific,
written prior permission. The copyright holders make no
representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied
warranty.
THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
</copyright>
<description summary="Protocol for composing text">
This protocol allows compositors to act as input methods and to send text
to applications. A text input object is used to manage state of what are
typically text entry fields in the application.
This document adheres to the RFC 2119 when using words like "must",
"should", "may", etc.
Warning! The protocol described in this file is experimental and
backward incompatible changes may be made. Backward compatible changes
may be added together with the corresponding interface version bump.
Backward incompatible changes are done by bumping the version number in
the protocol and interface names and resetting the interface version.
Once the protocol is to be declared stable, the 'z' prefix and the
version number in the protocol and interface names are removed and the
interface version number is reset.
</description>
<interface name="zwp_text_input_v3" version="1">
<description summary="text input">
The zwp_text_input_v3 interface represents text input and input methods
associated with a seat. It provides enter/leave events to follow the
text input focus for a seat.
Requests are used to enable/disable the text-input object and set
state information like surrounding and selected text or the content type.
The information about the entered text is sent to the text-input object
via the preedit_string and commit_string events.
Text is valid UTF-8 encoded, indices and lengths are in bytes. Indices
must not point to middle bytes inside a code point: they must either
point to the first byte of a code point or to the end of the buffer.
Lengths must be measured between two valid indices.
Focus moving throughout surfaces will result in the emission of
zwp_text_input_v3.enter and zwp_text_input_v3.leave events. The focused
surface must commit zwp_text_input_v3.enable and
zwp_text_input_v3.disable requests as the keyboard focus moves across
editable and non-editable elements of the UI. Those two requests are not
expected to be paired with each other, the compositor must be able to
handle consecutive series of the same request.
State is sent by the state requests (set_surrounding_text,
set_content_type and set_cursor_rectangle) and a commit request. After an
enter event or disable request all state information is invalidated and
needs to be resent by the client.
</description>
<request name="destroy" type="destructor">
<description summary="Destroy the wp_text_input">
Destroy the wp_text_input object. Also disables all surfaces enabled
through this wp_text_input object.
</description>
</request>
<request name="enable">
<description summary="Request text input to be enabled">
Requests text input on the surface previously obtained from the enter
event.
This request must be issued every time the active text input changes
to a new one, including within the current surface. Use
zwp_text_input_v3.disable when there is no longer any input focus on
the current surface.
This request resets all state associated with previous enable, disable,
set_surrounding_text, set_text_change_cause, set_content_type, and
set_cursor_rectangle requests, as well as the state associated with
preedit_string, commit_string, and delete_surrounding_text events.
The set_surrounding_text, set_content_type and set_cursor_rectangle
requests must follow if the text input supports the necessary
functionality.
State set with this request is double-buffered. It will get applied on
the next zwp_text_input_v3.commit request, and stay valid until the
next committed enable or disable request.
The changes must be applied by the compositor after issuing a
zwp_text_input_v3.commit request.
</description>
</request>
<request name="disable">
<description summary="Disable text input on a surface">
Explicitly disable text input on the current surface (typically when
there is no focus on any text entry inside the surface).
State set with this request is double-buffered. It will get applied on
the next zwp_text_input_v3.commit request.
</description>
</request>
<request name="set_surrounding_text">
<description summary="sets the surrounding text">
Sets the surrounding plain text around the input, excluding the preedit
text.
The client should notify the compositor of any changes in any of the
values carried with this request, including changes caused by handling
incoming text-input events as well as changes caused by other
mechanisms like keyboard typing.
If the client is unaware of the text around the cursor, it should not
issue this request, to signify lack of support to the compositor.
Text is UTF-8 encoded, and should include the cursor position, the
complete selection and additional characters before and after them.
There is a maximum length of wayland messages, so text can not be
longer than 4000 bytes.
Cursor is the byte offset of the cursor within text buffer.
Anchor is the byte offset of the selection anchor within text buffer.
If there is no selected text, anchor is the same as cursor.
If any preedit text is present, it is replaced with a cursor for the
purpose of this event.
Values set with this request are double-buffered. They will get applied
on the next zwp_text_input_v3.commit request, and stay valid until the
next committed enable or disable request.
The initial state for affected fields is empty, meaning that the text
input does not support sending surrounding text. If the empty values
get applied, subsequent attempts to change them may have no effect.
</description>
<arg name="text" type="string"/>
<arg name="cursor" type="int"/>
<arg name="anchor" type="int"/>
</request>
<enum name="change_cause">
<description summary="text change reason">
Reason for the change of surrounding text or cursor posision.
</description>
<entry name="input_method" value="0" summary="input method caused the change"/>
<entry name="other" value="1" summary="something else than the input method caused the change"/>
</enum>
<request name="set_text_change_cause">
<description summary="indicates the cause of surrounding text change">
Tells the compositor why the text surrounding the cursor changed.
Whenever the client detects an external change in text, cursor, or
anchor posision, it must issue this request to the compositor. This
request is intended to give the input method a chance to update the
preedit text in an appropriate way, e.g. by removing it when the user
starts typing with a keyboard.
cause describes the source of the change.
The value set with this request is double-buffered. It must be applied
and reset to initial at the next zwp_text_input_v3.commit request.
The initial value of cause is input_method.
</description>
<arg name="cause" type="uint" enum="change_cause"/>
</request>
<enum name="content_hint" bitfield="true">
<description summary="content hint">
Content hint is a bitmask to allow to modify the behavior of the text
input.
</description>
<entry name="none" value="0x0" summary="no special behavior"/>
<entry name="completion" value="0x1" summary="suggest word completions"/>
<entry name="spellcheck" value="0x2" summary="suggest word corrections"/>
<entry name="auto_capitalization" value="0x4" summary="switch to uppercase letters at the start of a sentence"/>
<entry name="lowercase" value="0x8" summary="prefer lowercase letters"/>
<entry name="uppercase" value="0x10" summary="prefer uppercase letters"/>
<entry name="titlecase" value="0x20" summary="prefer casing for titles and headings (can be language dependent)"/>
<entry name="hidden_text" value="0x40" summary="characters should be hidden"/>
<entry name="sensitive_data" value="0x80" summary="typed text should not be stored"/>
<entry name="latin" value="0x100" summary="just Latin characters should be entered"/>
<entry name="multiline" value="0x200" summary="the text input is multiline"/>
</enum>
<enum name="content_purpose">
<description summary="content purpose">
The content purpose allows to specify the primary purpose of a text
input.
This allows an input method to show special purpose input panels with
extra characters or to disallow some characters.
</description>
<entry name="normal" value="0" summary="default input, allowing all characters"/>
<entry name="alpha" value="1" summary="allow only alphabetic characters"/>
<entry name="digits" value="2" summary="allow only digits"/>
<entry name="number" value="3" summary="input a number (including decimal separator and sign)"/>
<entry name="phone" value="4" summary="input a phone number"/>
<entry name="url" value="5" summary="input an URL"/>
<entry name="email" value="6" summary="input an email address"/>
<entry name="name" value="7" summary="input a name of a person"/>
<entry name="password" value="8" summary="input a password (combine with sensitive_data hint)"/>
<entry name="pin" value="9" summary="input is a numeric password (combine with sensitive_data hint)"/>
<entry name="date" value="10" summary="input a date"/>
<entry name="time" value="11" summary="input a time"/>
<entry name="datetime" value="12" summary="input a date and time"/>
<entry name="terminal" value="13" summary="input for a terminal"/>
</enum>
<request name="set_content_type">
<description summary="set content purpose and hint">
Sets the content purpose and content hint. While the purpose is the
basic purpose of an input field, the hint flags allow to modify some of
the behavior.
Values set with this request are double-buffered. They will get applied
on the next zwp_text_input_v3.commit request.
Subsequent attempts to update them may have no effect. The values
remain valid until the next committed enable or disable request.
The initial value for hint is none, and the initial value for purpose
is normal.
</description>
<arg name="hint" type="uint" enum="content_hint"/>
<arg name="purpose" type="uint" enum="content_purpose"/>
</request>
<request name="set_cursor_rectangle">
<description summary="set cursor position">
Marks an area around the cursor as a x, y, width, height rectangle in
surface local coordinates.
Allows the compositor to put a window with word suggestions near the
cursor, without obstructing the text being input.
If the client is unaware of the position of edited text, it should not
issue this request, to signify lack of support to the compositor.
Values set with this request are double-buffered. They will get applied
on the next zwp_text_input_v3.commit request, and stay valid until the
next committed enable or disable request.
The initial values describing a cursor rectangle are empty. That means
the text input does not support describing the cursor area. If the
empty values get applied, subsequent attempts to change them may have
no effect.
</description>
<arg name="x" type="int"/>
<arg name="y" type="int"/>
<arg name="width" type="int"/>
<arg name="height" type="int"/>
</request>
<request name="commit">
<description summary="commit state">
Atomically applies state changes recently sent to the compositor.
The commit request establishes and updates the state of the client, and
must be issued after any changes to apply them.
Text input state (enabled status, content purpose, content hint,
surrounding text and change cause, cursor rectangle) is conceptually
double-buffered within the context of a text input, i.e. between a
committed enable request and the following committed enable or disable
request.
Protocol requests modify the pending state, as opposed to the current
state in use by the input method. A commit request atomically applies
all pending state, replacing the current state. After commit, the new
pending state is as documented for each related request.
Requests are applied in the order of arrival.
Neither current nor pending state are modified unless noted otherwise.
The compositor must count the number of commit requests coming from
each zwp_text_input_v3 object and use the count as the serial in done
events.
</description>
</request>
<event name="enter">
<description summary="enter event">
Notification that this seat's text-input focus is on a certain surface.
When the seat has the keyboard capability the text-input focus follows
the keyboard focus. This event sets the current surface for the
text-input object.
</description>
<arg name="surface" type="object" interface="wl_surface"/>
</event>
<event name="leave">
<description summary="leave event">
Notification that this seat's text-input focus is no longer on a
certain surface. The client should reset any preedit string previously
set.
The leave notification clears the current surface. It is sent before
the enter notification for the new focus.
When the seat has the keyboard capability the text-input focus follows
the keyboard focus.
</description>
<arg name="surface" type="object" interface="wl_surface"/>
</event>
<event name="preedit_string">
<description summary="pre-edit">
Notify when a new composing text (pre-edit) should be set at the
current cursor position. Any previously set composing text must be
removed. Any previously existing selected text must be removed.
The argument text contains the pre-edit string buffer.
The parameters cursor_begin and cursor_end are counted in bytes
relative to the beginning of the submitted text buffer. Cursor should
be hidden when both are equal to -1.
They could be represented by the client as a line if both values are
the same, or as a text highlight otherwise.
Values set with this event are double-buffered. They must be applied
and reset to initial on the next zwp_text_input_v3.done event.
The initial value of text is an empty string, and cursor_begin,
cursor_end and cursor_hidden are all 0.
</description>
<arg name="text" type="string" allow-null="true"/>
<arg name="cursor_begin" type="int"/>
<arg name="cursor_end" type="int"/>
</event>
<event name="commit_string">
<description summary="text commit">
Notify when text should be inserted into the editor widget. The text to
commit could be either just a single character after a key press or the
result of some composing (pre-edit).
Values set with this event are double-buffered. They must be applied
and reset to initial on the next zwp_text_input_v3.done event.
The initial value of text is an empty string.
</description>
<arg name="text" type="string" allow-null="true"/>
</event>
<event name="delete_surrounding_text">
<description summary="delete surrounding text">
Notify when the text around the current cursor position should be
deleted.
Before_length and after_length are the number of bytes before and after
the current cursor index (excluding the selection) to delete.
If a preedit text is present, in effect before_length is counted from
the beginning of it, and after_length from its end (see done event
sequence).
Values set with this event are double-buffered. They must be applied
and reset to initial on the next zwp_text_input_v3.done event.
The initial values of both before_length and after_length are 0.
</description>
<arg name="before_length" type="uint" summary="length of text before current cursor position"/>
<arg name="after_length" type="uint" summary="length of text after current cursor position"/>
</event>
<event name="done">
<description summary="apply changes">
Instruct the application to apply changes to state requested by the
preedit_string, commit_string and delete_surrounding_text events. The
state relating to these events is double-buffered, and each one
modifies the pending state. This event replaces the current state with
the pending state.
The application must proceed by evaluating the changes in the following
order:
1. Replace existing preedit string with the cursor.
2. Delete requested surrounding text.
3. Insert commit string with the cursor at its end.
4. Calculate surrounding text to send.
5. Insert new preedit text in cursor position.
6. Place cursor inside preedit text.
The serial number reflects the last state of the zwp_text_input_v3
object known to the compositor. The value of the serial argument must
be equal to the number of commit requests already issued on that object.
When the client receives a done event with a serial different than the
number of past commit requests, it must proceed as normal, except it
should not change the current state of the zwp_text_input_v3 object.
</description>
<arg name="serial" type="uint"/>
</event>
</interface>
<interface name="zwp_text_input_manager_v3" version="1">
<description summary="text input manager">
A factory for text-input objects. This object is a global singleton.
</description>
<request name="destroy" type="destructor">
<description summary="Destroy the wp_text_input_manager">
Destroy the wp_text_input_manager object.
</description>
</request>
<request name="get_text_input">
<description summary="create a new text input object">
Creates a new text-input object for a given seat.
</description>
<arg name="id" type="new_id" interface="zwp_text_input_v3"/>
<arg name="seat" type="object" interface="wl_seat"/>
</request>
</interface>
</protocol>

@ -51,6 +51,7 @@ lib_wlr_types = static_library(
'tablet_v2/wlr_tablet_v2_tool.c',
'wlr_tablet_pad.c',
'wlr_tablet_tool.c',
'wlr_text_input_v3.c',
'wlr_touch.c',
'wlr_virtual_keyboard_v1.c',
'wlr_wl_shell.c',

@ -0,0 +1,331 @@
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
#include <wlr/types/wlr_text_input_v3.h>
#include <wlr/util/log.h>
#include "text-input-unstable-v3-protocol.h"
#include "util/signal.h"
static const struct zwp_text_input_v3_interface text_input_impl;
static struct wlr_text_input_v3 *text_input_from_resource(
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource, &zwp_text_input_v3_interface,
&text_input_impl));
return wl_resource_get_user_data(resource);
}
void wlr_text_input_v3_send_enter(struct wlr_text_input_v3 *text_input,
struct wlr_surface *surface) {
assert(wl_resource_get_client(text_input->resource)
== wl_resource_get_client(surface->resource));
text_input->focused_surface = surface;
wl_signal_add(&text_input->focused_surface->events.destroy,
&text_input->surface_destroy);
zwp_text_input_v3_send_enter(text_input->resource,
text_input->focused_surface->resource);
}
void wlr_text_input_v3_send_leave(struct wlr_text_input_v3 *text_input) {
zwp_text_input_v3_send_leave(text_input->resource,
text_input->focused_surface->resource);
wl_list_remove(&text_input->surface_destroy.link);
text_input->focused_surface = NULL;
}
void wlr_text_input_v3_send_preedit_string(struct wlr_text_input_v3 *text_input,
const char *text, uint32_t cursor_begin, uint32_t cursor_end) {
zwp_text_input_v3_send_preedit_string(text_input->resource, text,
cursor_begin, cursor_end);
}
void wlr_text_input_v3_send_commit_string(struct wlr_text_input_v3 *text_input,
const char *text) {
zwp_text_input_v3_send_commit_string(text_input->resource, text);
}
void wlr_text_input_v3_send_delete_surrounding_text(
struct wlr_text_input_v3 *text_input, uint32_t before_length,
uint32_t after_length) {
zwp_text_input_v3_send_delete_surrounding_text(text_input->resource,
before_length, after_length);
}
void wlr_text_input_v3_send_done(struct wlr_text_input_v3 *text_input) {
zwp_text_input_v3_send_done(text_input->resource,
text_input->current_serial);
}
static void wlr_text_input_destroy(struct wlr_text_input_v3 *text_input) {
wlr_signal_emit_safe(&text_input->events.destroy, text_input);
wl_list_remove(&text_input->seat_destroy.link);
// remove from manager::text_inputs
wl_list_remove(&text_input->link);
free(text_input->current.surrounding.text);
free(text_input->pending.surrounding.text);
free(text_input);
}
static void text_input_resource_destroy(struct wl_resource *resource) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
wlr_text_input_destroy(text_input);
}
static void text_input_destroy(struct wl_client *client,
struct wl_resource *resource) {
wl_resource_destroy(resource);
}
static void text_input_enable(struct wl_client *client,
struct wl_resource *resource) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
struct wlr_text_input_v3_state defaults = {0};
free(text_input->pending.surrounding.text);
text_input->pending = defaults;
text_input->pending_enabled = true;
}
static void text_input_disable(struct wl_client *client,
struct wl_resource *resource) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
text_input->pending_enabled = false;
}
static void text_input_set_surrounding_text(struct wl_client *client,
struct wl_resource *resource, const char *text, int32_t cursor,
int32_t anchor) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
free(text_input->pending.surrounding.text);
text_input->pending.surrounding.text = strdup(text);
if (!text_input->pending.surrounding.text) {
wl_client_post_no_memory(client);
}
text_input->pending.surrounding.cursor = cursor;
text_input->pending.surrounding.anchor = anchor;
}
static void text_input_set_text_change_cause(struct wl_client *client,
struct wl_resource *resource, uint32_t cause) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
text_input->pending.text_change_cause = cause;
}
static void text_input_set_content_type(struct wl_client *client,
struct wl_resource *resource, uint32_t hint, uint32_t purpose) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
text_input->pending.content_type.hint = hint;
text_input->pending.content_type.purpose = purpose;
}
static void text_input_set_cursor_rectangle(struct wl_client *client,
struct wl_resource *resource, int32_t x, int32_t y, int32_t width,
int32_t height) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
text_input->pending.cursor_rectangle.x = x;
text_input->pending.cursor_rectangle.y = y;
text_input->pending.cursor_rectangle.width = width;
text_input->pending.cursor_rectangle.height = height;
}
static void text_input_commit(struct wl_client *client,
struct wl_resource *resource) {
struct wlr_text_input_v3 *text_input = text_input_from_resource(resource);
if (!text_input) {
return;
}
free(text_input->current.surrounding.text);
text_input->current = text_input->pending;
if (text_input->pending.surrounding.text) {
text_input->current.surrounding.text =
strdup(text_input->pending.surrounding.text);
}
bool old_enabled = text_input->current_enabled;
text_input->current_enabled = text_input->pending_enabled;
text_input->current_serial++;
if (text_input->current_enabled && text_input->focused_surface == NULL) {
wl_resource_post_error(text_input->resource, 0, "Text input was not"
"entered, and cannot be enabled\n");
return;
}
if (!old_enabled && text_input->current_enabled) {
wlr_signal_emit_safe(&text_input->events.enable, text_input);
} else if (old_enabled && !text_input->current_enabled) {
wlr_signal_emit_safe(&text_input->events.disable, text_input);
} else { // including never enabled
wlr_signal_emit_safe(&text_input->events.commit, text_input);
}
}
static const struct zwp_text_input_v3_interface text_input_impl = {
.destroy = text_input_destroy,
.enable = text_input_enable,
.disable = text_input_disable,
.set_surrounding_text = text_input_set_surrounding_text,
.set_text_change_cause = text_input_set_text_change_cause,
.set_content_type = text_input_set_content_type,
.set_cursor_rectangle = text_input_set_cursor_rectangle,
.commit = text_input_commit,
};
static const struct zwp_text_input_manager_v3_interface text_input_manager_impl;
static struct wlr_text_input_manager_v3 *text_input_manager_from_resource(
struct wl_resource *resource) {
assert(wl_resource_instance_of(resource,
&zwp_text_input_manager_v3_interface, &text_input_manager_impl));
return wl_resource_get_user_data(resource);
}
static void text_input_manager_destroy(struct wl_client *client,
struct wl_resource *resource) {
wl_resource_destroy(resource);
}
static void text_input_handle_seat_destroy(struct wl_listener *listener,
void *data) {
struct wlr_text_input_v3 *text_input = wl_container_of(listener, text_input,
seat_destroy);
struct wl_resource *resource = text_input->resource;
wlr_text_input_destroy(text_input);
wl_resource_set_user_data(resource, NULL);
}
static void text_input_handle_focused_surface_destroy(
struct wl_listener *listener, void *data) {
struct wlr_text_input_v3 *text_input = wl_container_of(listener, text_input,
surface_destroy);
text_input->focused_surface = NULL;
}
static void text_input_manager_get_text_input(struct wl_client *client,
struct wl_resource *resource, uint32_t id, struct wl_resource *seat) {
struct wlr_text_input_v3 *text_input =
calloc(1, sizeof(struct wlr_text_input_v3));
if (text_input == NULL) {
wl_client_post_no_memory(client);
return;
}
wl_signal_init(&text_input->events.enable);
wl_signal_init(&text_input->events.commit);
wl_signal_init(&text_input->events.disable);
wl_signal_init(&text_input->events.destroy);
int version = wl_resource_get_version(resource);
struct wl_resource *text_input_resource = wl_resource_create(client,
&zwp_text_input_v3_interface, version, id);
if (text_input_resource == NULL) {
free(text_input);
wl_client_post_no_memory(client);
return;
}
text_input->resource = text_input_resource;
wl_resource_set_implementation(text_input->resource, &text_input_impl,
text_input, text_input_resource_destroy);
struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat);
struct wlr_seat *wlr_seat = seat_client->seat;
text_input->seat = wlr_seat;
wl_signal_add(&seat_client->events.destroy,
&text_input->seat_destroy);
text_input->seat_destroy.notify =
text_input_handle_seat_destroy;
text_input->surface_destroy.notify =
text_input_handle_focused_surface_destroy;
struct wlr_text_input_manager_v3 *manager =
text_input_manager_from_resource(resource);
wl_list_insert(&manager->text_inputs, &text_input->link);
wlr_signal_emit_safe(&manager->events.text_input, text_input);
}
static const struct zwp_text_input_manager_v3_interface
text_input_manager_impl = {
.destroy = text_input_manager_destroy,
.get_text_input = text_input_manager_get_text_input,
};
static void text_input_manager_unbind(struct wl_resource *resource) {
wl_list_remove(wl_resource_get_link(resource));
}
static void text_input_manager_bind(struct wl_client *wl_client, void *data,
uint32_t version, uint32_t id) {
struct wlr_text_input_manager_v3 *manager = data;
assert(wl_client && manager);
struct wl_resource *resource = wl_resource_create(wl_client,
&zwp_text_input_manager_v3_interface, version, id);
if (resource == NULL) {
wl_client_post_no_memory(wl_client);
return;
}
wl_list_insert(&manager->bound_resources, wl_resource_get_link(resource));
wl_resource_set_implementation(resource, &text_input_manager_impl,
manager, text_input_manager_unbind);
}
struct wlr_text_input_manager_v3 *wlr_text_input_manager_v3_create(
struct wl_display *wl_display) {
struct wlr_text_input_manager_v3 *manager =
calloc(1, sizeof(struct wlr_text_input_manager_v3));
wl_list_init(&manager->bound_resources);
wl_list_init(&manager->text_inputs);
wl_signal_init(&manager->events.text_input);
manager->global = wl_global_create(wl_display,
&zwp_text_input_manager_v3_interface, 1, manager,
text_input_manager_bind);
if (!manager->global) {
free(manager);
return NULL;
}
return manager;
}
void wlr_text_input_manager_v3_destroy(
struct wlr_text_input_manager_v3 *manager) {
wlr_signal_emit_safe(&manager->events.destroy, manager);
wl_list_remove(&manager->display_destroy.link);
struct wl_resource *resource, *resource_tmp;
wl_resource_for_each_safe(resource, resource_tmp,
&manager->bound_resources) {
wl_resource_destroy(resource);
}
struct wlr_text_input_v3 *text_input, *text_input_tmp;
wl_list_for_each_safe(text_input, text_input_tmp, &manager->text_inputs,
link) {
wl_resource_destroy(text_input->resource);
}
wl_global_destroy(manager->global);
free(manager);
}
Loading…
Cancel
Save