This commit is contained in:
chirsz 2025-04-14 23:37:25 +00:00 committed by GitHub
commit e845d8a07f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 960 additions and 19 deletions

19
cage.c
View file

@ -52,6 +52,7 @@
#endif
#include "idle_inhibit_v1.h"
#include "ime.h"
#include "output.h"
#include "seat.h"
#include "server.h"
@ -527,6 +528,24 @@ main(int argc, char *argv[])
goto end;
}
server.input_method_manager_v2 = wlr_input_method_manager_v2_create(server.wl_display);
if (!server.input_method_manager_v2) {
wlr_log(WLR_ERROR, "Unable to create the input method manager");
ret = 1;
goto end;
}
server.new_input_method.notify = cg_ime_handle_new_input_method;
wl_signal_add(&server.input_method_manager_v2->events.input_method, &server.new_input_method);
server.text_input_manager_v3 = wlr_text_input_manager_v3_create(server.wl_display);
if (!server.text_input_manager_v3) {
wlr_log(WLR_ERROR, "Unable to create the text input manager");
ret = 1;
goto end;
}
server.new_text_input.notify = cg_ime_handle_new_text_input;
wl_signal_add(&server.text_input_manager_v3->events.text_input, &server.new_text_input);
#if CAGE_HAS_XWAYLAND
struct wlr_xcursor_manager *xcursor_manager = NULL;
struct wlr_xwayland *xwayland = wlr_xwayland_create(server.wl_display, compositor, true);

737
ime.c Normal file
View file

@ -0,0 +1,737 @@
/*
* Cage: A Wayland kiosk.
*
* Copyright (C) 2025 Cage Authors
* Copyright (C) 2020-2024 Sway Authors
*
* See the LICENSE file accompanying this file.
*/
#define _POSIX_C_SOURCE 200809L
#include "ime.h"
#include "seat.h"
#include "server.h"
#include "view.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/types/wlr_scene.h>
static struct cg_text_input *
relay_get_focusable_text_input(struct cg_ime_relay *relay)
{
struct cg_text_input *text_input = NULL;
wl_list_for_each (text_input, &relay->text_inputs, link) {
if (text_input->pending_focused_surface) {
return text_input;
}
}
return NULL;
}
static struct cg_text_input *
relay_get_focused_text_input(struct cg_ime_relay *relay)
{
struct cg_text_input *text_input = NULL;
wl_list_for_each (text_input, &relay->text_inputs, link) {
if (text_input->input->focused_surface) {
return text_input;
}
}
return NULL;
}
static void
handle_im_commit(struct wl_listener *listener, void *data)
{
struct cg_ime_relay *relay = wl_container_of(listener, relay, input_method_commit);
struct cg_text_input *text_input = relay_get_focused_text_input(relay);
if (!text_input) {
return;
}
struct wlr_input_method_v2 *context = data;
assert(context == relay->input_method);
if (context->current.preedit.text) {
wlr_text_input_v3_send_preedit_string(text_input->input, context->current.preedit.text,
context->current.preedit.cursor_begin,
context->current.preedit.cursor_end);
}
if (context->current.commit_text) {
wlr_text_input_v3_send_commit_string(text_input->input, context->current.commit_text);
}
if (context->current.delete.before_length || context->current.delete.after_length) {
wlr_text_input_v3_send_delete_surrounding_text(text_input->input, context->current.delete.before_length,
context->current.delete.after_length);
}
wlr_text_input_v3_send_done(text_input->input);
}
static void
handle_im_keyboard_grab_destroy(struct wl_listener *listener, void *data)
{
struct cg_ime_relay *relay = wl_container_of(listener, relay, input_method_keyboard_grab_destroy);
struct wlr_input_method_keyboard_grab_v2 *keyboard_grab = data;
struct wlr_seat *wlr_seat = keyboard_grab->input_method->seat;
wl_list_remove(&relay->input_method_keyboard_grab_destroy.link);
if (keyboard_grab->keyboard) {
// send modifier state to original client
wlr_seat_set_keyboard(wlr_seat, keyboard_grab->keyboard);
wlr_seat_keyboard_notify_modifiers(wlr_seat, &keyboard_grab->keyboard->modifiers);
}
}
static void
handle_im_grab_keyboard(struct wl_listener *listener, void *data)
{
struct cg_ime_relay *relay = wl_container_of(listener, relay, input_method_grab_keyboard);
struct wlr_input_method_keyboard_grab_v2 *keyboard_grab = data;
// send modifier state to grab
struct wlr_keyboard *active_keyboard = wlr_seat_get_keyboard(relay->seat->seat);
wlr_input_method_keyboard_grab_v2_set_keyboard(keyboard_grab, active_keyboard);
wl_signal_add(&keyboard_grab->events.destroy, &relay->input_method_keyboard_grab_destroy);
relay->input_method_keyboard_grab_destroy.notify = handle_im_keyboard_grab_destroy;
}
static void
text_input_set_pending_focused_surface(struct cg_text_input *text_input, struct wlr_surface *surface)
{
wl_list_remove(&text_input->pending_focused_surface_destroy.link);
text_input->pending_focused_surface = surface;
if (surface) {
wl_signal_add(&surface->events.destroy, &text_input->pending_focused_surface_destroy);
} else {
wl_list_init(&text_input->pending_focused_surface_destroy.link);
}
}
static void
handle_im_destroy(struct wl_listener *listener, void *data)
{
struct cg_ime_relay *relay = wl_container_of(listener, relay, input_method_destroy);
struct wlr_input_method_v2 *context = data;
assert(context == relay->input_method);
wl_list_remove(&relay->input_method_commit.link);
wl_list_remove(&relay->input_method_grab_keyboard.link);
wl_list_remove(&relay->input_method_destroy.link);
wl_list_remove(&relay->input_method_new_popup_surface.link);
relay->input_method = NULL;
struct cg_text_input *text_input = relay_get_focused_text_input(relay);
if (text_input) {
// keyboard focus is still there, so keep the surface at hand in case
// the input method returns
text_input_set_pending_focused_surface(text_input, text_input->input->focused_surface);
wlr_text_input_v3_send_leave(text_input->input);
}
}
static void
constrain_popup(struct cg_input_popup *popup)
{
struct cg_text_input *text_input = relay_get_focused_text_input(popup->relay);
struct cg_server *server = popup->relay->seat->server;
if (!popup->focused_scene_node) {
return;
}
struct wlr_box parent = {0};
wlr_scene_node_coords(&popup->focused_scene_node->parent->node, &parent.x, &parent.y);
struct wlr_box geo = {0};
struct wlr_output *output;
if (popup->focused_view) {
struct cg_view *view = popup->focused_view;
output = wlr_output_layout_output_at(server->output_layout, view->lx, view->ly);
view->impl->get_geometry(view, &parent.width, &parent.height);
geo.x = view->lx;
geo.y = view->ly;
geo.width = parent.width;
geo.height = parent.height;
} else {
output = popup->fixed_output;
}
struct wlr_box output_box;
wlr_output_layout_get_box(server->output_layout, output, &output_box);
bool cursor_rect = text_input->input->current.features & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
struct wlr_box cursor_area;
if (cursor_rect) {
cursor_area = text_input->input->current.cursor_rectangle;
} else {
cursor_area = (struct wlr_box) {
.width = parent.width,
.height = parent.height,
};
}
int popup_width = popup->popup_surface->surface->current.width;
int popup_height = popup->popup_surface->surface->current.height;
int x1 = parent.x + cursor_area.x;
int x2 = parent.x + cursor_area.x + cursor_area.width;
int y1 = parent.y + cursor_area.y;
int y2 = parent.y + cursor_area.y + cursor_area.height;
int x = x1;
int y = y2;
int available_right = output_box.x + output_box.width - x1;
int available_left = x2 - output_box.x;
if (available_right < popup_width && available_left > available_right) {
x = x2 - popup_width;
}
int available_down = output_box.y + output_box.height - y2;
int available_up = y1 - output_box.y;
if (available_down < popup_height && available_up > available_down) {
y = y1 - popup_height;
}
wlr_scene_node_set_position(popup->focused_scene_node, x - parent.x - geo.x, y - parent.y - geo.y);
if (cursor_rect) {
struct wlr_box box = {
.x = x1 - x,
.y = y1 - y,
.width = cursor_area.width,
.height = cursor_area.height,
};
wlr_input_popup_surface_v2_send_text_input_rectangle(popup->popup_surface, &box);
}
if (popup->view->scene_tree) {
wlr_scene_node_set_position(&popup->view->scene_tree->node, x - geo.x, y - geo.y);
}
}
static void input_popup_set_focus(struct cg_input_popup *popup, struct wlr_surface *surface);
static void
relay_send_im_state(struct cg_ime_relay *relay, struct wlr_text_input_v3 *input)
{
struct wlr_input_method_v2 *input_method = relay->input_method;
if (!input_method) {
wlr_log(WLR_INFO, "Sending IM_DONE but im is gone");
return;
}
// TODO: only send each of those if they were modified
if (input->active_features & WLR_TEXT_INPUT_V3_FEATURE_SURROUNDING_TEXT) {
wlr_input_method_v2_send_surrounding_text(input_method, input->current.surrounding.text,
input->current.surrounding.cursor,
input->current.surrounding.anchor);
}
wlr_input_method_v2_send_text_change_cause(input_method, input->current.text_change_cause);
if (input->active_features & WLR_TEXT_INPUT_V3_FEATURE_CONTENT_TYPE) {
wlr_input_method_v2_send_content_type(input_method, input->current.content_type.hint,
input->current.content_type.purpose);
}
struct cg_text_input *text_input = relay_get_focused_text_input(relay);
struct cg_input_popup *popup;
wl_list_for_each (popup, &relay->input_popups, link) {
if (text_input != NULL) {
input_popup_set_focus(popup, text_input->input->focused_surface);
} else {
input_popup_set_focus(popup, NULL);
}
}
wlr_input_method_v2_send_done(input_method);
// TODO: pass intent, display popup size
}
static void
handle_text_input_enable(struct wl_listener *listener, void *data)
{
struct cg_text_input *text_input = wl_container_of(listener, text_input, text_input_enable);
if (text_input->relay->input_method == NULL) {
wlr_log(WLR_INFO, "Enabling text input when input method is gone");
return;
}
wlr_input_method_v2_send_activate(text_input->relay->input_method);
relay_send_im_state(text_input->relay, text_input->input);
}
static void
handle_text_input_commit(struct wl_listener *listener, void *data)
{
struct cg_text_input *text_input = wl_container_of(listener, text_input, text_input_commit);
if (!text_input->input->current_enabled) {
wlr_log(WLR_INFO, "Inactive text input tried to commit an update");
return;
}
wlr_log(WLR_DEBUG, "Text input committed update");
if (text_input->relay->input_method == NULL) {
wlr_log(WLR_INFO, "Text input committed, but input method is gone");
return;
}
relay_send_im_state(text_input->relay, text_input->input);
}
static void
relay_disable_text_input(struct cg_ime_relay *relay, struct cg_text_input *text_input)
{
if (relay->input_method == NULL) {
wlr_log(WLR_DEBUG, "Disabling text input, but input method is gone");
return;
}
wlr_input_method_v2_send_deactivate(relay->input_method);
relay_send_im_state(relay, text_input->input);
}
static void
handle_text_input_disable(struct wl_listener *listener, void *data)
{
struct cg_text_input *text_input = wl_container_of(listener, text_input, text_input_disable);
if (text_input->input->focused_surface == NULL) {
wlr_log(WLR_DEBUG, "Disabling text input, but no longer focused");
return;
}
relay_disable_text_input(text_input->relay, text_input);
}
static void
handle_text_input_destroy(struct wl_listener *listener, void *data)
{
struct cg_text_input *text_input = wl_container_of(listener, text_input, text_input_destroy);
if (text_input->input->current_enabled) {
relay_disable_text_input(text_input->relay, text_input);
}
text_input_set_pending_focused_surface(text_input, NULL);
wl_list_remove(&text_input->text_input_commit.link);
wl_list_remove(&text_input->text_input_destroy.link);
wl_list_remove(&text_input->text_input_disable.link);
wl_list_remove(&text_input->text_input_enable.link);
wl_list_remove(&text_input->link);
free(text_input);
}
static void
handle_pending_focused_surface_destroy(struct wl_listener *listener, void *data)
{
struct cg_text_input *text_input = wl_container_of(listener, text_input, pending_focused_surface_destroy);
struct wlr_surface *surface = data;
assert(text_input->pending_focused_surface == surface);
text_input->pending_focused_surface = NULL;
wl_list_remove(&text_input->pending_focused_surface_destroy.link);
wl_list_init(&text_input->pending_focused_surface_destroy.link);
}
static struct cg_text_input *
cg_text_input_create(struct cg_ime_relay *relay, struct wlr_text_input_v3 *text_input)
{
struct cg_text_input *input = calloc(1, sizeof(struct cg_text_input));
if (!input) {
return NULL;
}
input->input = text_input;
input->relay = relay;
wl_list_insert(&relay->text_inputs, &input->link);
input->text_input_enable.notify = handle_text_input_enable;
wl_signal_add(&text_input->events.enable, &input->text_input_enable);
input->text_input_commit.notify = handle_text_input_commit;
wl_signal_add(&text_input->events.commit, &input->text_input_commit);
input->text_input_disable.notify = handle_text_input_disable;
wl_signal_add(&text_input->events.disable, &input->text_input_disable);
input->text_input_destroy.notify = handle_text_input_destroy;
wl_signal_add(&text_input->events.destroy, &input->text_input_destroy);
input->pending_focused_surface_destroy.notify = handle_pending_focused_surface_destroy;
wl_list_init(&input->pending_focused_surface_destroy.link);
return input;
}
void
cg_ime_handle_new_text_input(struct wl_listener *listener, void *data)
{
struct cg_server *server = wl_container_of(listener, server, new_text_input);
struct cg_ime_relay *relay = &server->seat->ime_relay;
struct wlr_text_input_v3 *wlr_text_input = data;
if (relay->seat->seat != wlr_text_input->seat) {
return;
}
cg_text_input_create(relay, wlr_text_input);
}
static void
input_popup_set_focus(struct cg_input_popup *popup, struct wlr_surface *surface)
{
wl_list_remove(&popup->focused_surface_unmap.link);
if (!popup->view->scene_tree) {
wl_list_init(&popup->focused_surface_unmap.link);
return;
}
if (popup->focused_scene_node) {
wlr_scene_node_destroy(popup->focused_scene_node);
popup->focused_scene_node = NULL;
}
if (surface == NULL) {
wl_list_init(&popup->focused_surface_unmap.link);
wlr_scene_node_set_enabled(&popup->view->scene_tree->node, false);
return;
}
struct cg_view *view = view_from_wlr_surface(surface);
wl_signal_add(&view->wlr_surface->events.unmap, &popup->focused_surface_unmap);
popup->focused_view = view;
struct wlr_scene_tree *relative = wlr_scene_tree_create(view->scene_tree);
popup->focused_scene_node = &relative->node;
constrain_popup(popup);
wlr_scene_node_set_enabled(&popup->view->scene_tree->node, true);
}
static void
handle_im_popup_destroy(struct wl_listener *listener, void *data)
{
struct cg_input_popup *popup = wl_container_of(listener, popup, popup_destroy);
wl_list_remove(&popup->focused_surface_unmap.link);
wl_list_remove(&popup->popup_surface_commit.link);
wl_list_remove(&popup->popup_surface_map.link);
wl_list_remove(&popup->popup_surface_unmap.link);
wl_list_remove(&popup->popup_destroy.link);
wl_list_remove(&popup->link);
view_destroy(popup->view);
free(popup);
}
static void
handle_im_popup_surface_map(struct wl_listener *listener, void *data)
{
struct cg_input_popup *popup = wl_container_of(listener, popup, popup_surface_map);
struct cg_text_input *text_input = relay_get_focused_text_input(popup->relay);
view_map(popup->view, popup->popup_surface->surface);
if (text_input != NULL) {
input_popup_set_focus(popup, text_input->input->focused_surface);
} else {
input_popup_set_focus(popup, NULL);
}
}
static void
handle_im_popup_surface_unmap(struct wl_listener *listener, void *data)
{
struct cg_input_popup *popup = wl_container_of(listener, popup, popup_surface_unmap);
// relative should already be freed as it should be a child of the just unmapped scene
popup->focused_scene_node = NULL;
view_unmap(popup->view);
input_popup_set_focus(popup, NULL);
}
static void
handle_im_popup_surface_commit(struct wl_listener *listener, void *data)
{
struct cg_input_popup *popup = wl_container_of(listener, popup, popup_surface_commit);
constrain_popup(popup);
}
static void
handle_im_focused_surface_unmap(struct wl_listener *listener, void *data)
{
struct cg_input_popup *popup = wl_container_of(listener, popup, focused_surface_unmap);
input_popup_set_focus(popup, NULL);
}
static char *
get_title(struct cg_view *view)
{
return strdup("Input popup");
}
static void
get_geometry(struct cg_view *view, int *width, int *height)
{
if (view->wlr_surface) {
*width = view->wlr_surface->current.width;
*height = view->wlr_surface->current.height;
} else {
*width = 0;
*height = 0;
}
}
static void
activate(struct cg_view *view, bool activate)
{
// no-op
// wlr_scene_node_set_enabled(&view->scene_tree->node, activate);
}
static void
maximize(struct cg_view *view, int width, int height)
{
// no-op
}
static void
input_popup_view_destroy(struct cg_view *view)
{
free(view);
}
static bool
is_primary(struct cg_view *view)
{
return false;
}
static bool
is_transient_for(struct cg_view *child, struct cg_view *parent)
{
return false;
}
static struct cg_view_impl input_popup_view_impl = {
.get_title = get_title,
.get_geometry = get_geometry,
.is_primary = is_primary,
.is_transient_for = is_transient_for,
.activate = activate,
.maximize = maximize,
.destroy = input_popup_view_destroy,
};
static void
handle_im_new_popup_surface(struct wl_listener *listener, void *data)
{
struct cg_ime_relay *relay = wl_container_of(listener, relay, input_method_new_popup_surface);
struct cg_input_popup *popup = calloc(1, sizeof(*popup));
if (!popup) {
wlr_log(WLR_ERROR, "Failed to allocate an input method popup");
return;
}
popup->relay = relay;
popup->popup_surface = data;
popup->popup_surface->data = popup;
popup->view = calloc(1, sizeof(*popup->view));
if (!popup->view) {
wlr_log(WLR_ERROR, "Failed to allocate cg_view for input popup");
free(popup);
return;
}
view_init(popup->view, relay->seat->server, CAGE_INPUT_POPUP_VIEW, &input_popup_view_impl);
wl_signal_add(&popup->popup_surface->events.destroy, &popup->popup_destroy);
popup->popup_destroy.notify = handle_im_popup_destroy;
wl_signal_add(&popup->popup_surface->surface->events.commit, &popup->popup_surface_commit);
popup->popup_surface_commit.notify = handle_im_popup_surface_commit;
wl_signal_add(&popup->popup_surface->surface->events.map, &popup->popup_surface_map);
popup->popup_surface_map.notify = handle_im_popup_surface_map;
wl_signal_add(&popup->popup_surface->surface->events.unmap, &popup->popup_surface_unmap);
popup->popup_surface_unmap.notify = handle_im_popup_surface_unmap;
wl_list_init(&popup->focused_surface_unmap.link);
popup->focused_surface_unmap.notify = handle_im_focused_surface_unmap;
struct cg_text_input *text_input = relay_get_focused_text_input(relay);
if (text_input != NULL) {
input_popup_set_focus(popup, text_input->input->focused_surface);
} else {
input_popup_set_focus(popup, NULL);
}
wl_list_insert(&relay->input_popups, &popup->link);
}
static void
text_input_send_enter(struct cg_text_input *text_input, struct wlr_surface *surface)
{
wlr_text_input_v3_send_enter(text_input->input, surface);
struct cg_input_popup *popup;
wl_list_for_each (popup, &text_input->relay->input_popups, link) {
input_popup_set_focus(popup, surface);
}
}
void
cg_ime_handle_new_input_method(struct wl_listener *listener, void *data)
{
struct cg_server *server = wl_container_of(listener, server, new_input_method);
struct cg_ime_relay *relay = &server->seat->ime_relay;
struct wlr_input_method_v2 *input_method = data;
if (relay->seat->seat != input_method->seat) {
return;
}
if (relay->input_method != NULL) {
wlr_log(WLR_INFO, "Attempted to connect second input method to a seat");
wlr_input_method_v2_send_unavailable(input_method);
return;
}
relay->input_method = input_method;
wl_signal_add(&relay->input_method->events.commit, &relay->input_method_commit);
relay->input_method_commit.notify = handle_im_commit;
wl_signal_add(&relay->input_method->events.grab_keyboard, &relay->input_method_grab_keyboard);
relay->input_method_grab_keyboard.notify = handle_im_grab_keyboard;
wl_signal_add(&relay->input_method->events.destroy, &relay->input_method_destroy);
relay->input_method_destroy.notify = handle_im_destroy;
wl_signal_add(&relay->input_method->events.new_popup_surface, &relay->input_method_new_popup_surface);
relay->input_method_new_popup_surface.notify = handle_im_new_popup_surface;
struct cg_text_input *text_input = relay_get_focusable_text_input(relay);
if (text_input) {
text_input_send_enter(text_input, text_input->pending_focused_surface);
text_input_set_pending_focused_surface(text_input, NULL);
}
}
void
cg_ime_relay_init(struct cg_seat *seat, struct cg_ime_relay *relay)
{
relay->seat = seat;
wl_list_init(&relay->text_inputs);
wl_list_init(&relay->input_popups);
wl_array_init(&relay->pressed_keys);
}
void
cg_ime_relay_finish(struct cg_ime_relay *relay)
{
wl_array_release(&relay->pressed_keys);
}
void
cg_ime_relay_set_focus(struct cg_ime_relay *relay, struct wlr_surface *surface)
{
struct cg_text_input *text_input;
wl_list_for_each (text_input, &relay->text_inputs, link) {
if (text_input->pending_focused_surface) {
assert(text_input->input->focused_surface == NULL);
if (surface != text_input->pending_focused_surface) {
text_input_set_pending_focused_surface(text_input, NULL);
}
} else if (text_input->input->focused_surface) {
assert(text_input->pending_focused_surface == NULL);
if (surface != text_input->input->focused_surface) {
relay_disable_text_input(relay, text_input);
wlr_text_input_v3_send_leave(text_input->input);
} else {
wlr_log(WLR_DEBUG, "IM relay set_focus already focused");
continue;
}
}
if (surface &&
wl_resource_get_client(text_input->input->resource) == wl_resource_get_client(surface->resource)) {
if (relay->input_method) {
wlr_text_input_v3_send_enter(text_input->input, surface);
} else {
text_input_set_pending_focused_surface(text_input, surface);
}
}
}
}
// Simple implementation of "set" data structure
static bool
pressed_keys_contains(struct wl_array *pressed_keys, uint32_t keycode)
{
uint32_t *key;
wl_array_for_each(key, pressed_keys)
{
if (*key == keycode) {
return true;
}
}
return false;
}
static void
pressed_keys_add(struct wl_array *pressed_keys, uint32_t keycode)
{
if (pressed_keys_contains(pressed_keys, keycode)) {
return;
}
uint32_t *key;
wl_array_for_each(key, pressed_keys)
{
if (*key == 0) {
*key = keycode;
return;
}
}
key = wl_array_add(pressed_keys, sizeof(*key));
if (key) {
*key = keycode;
} else {
wlr_log(WLR_ERROR, "Failed to add pressed key");
}
}
static void
pressed_keys_remove(struct wl_array *pressed_keys, uint32_t keycode)
{
uint32_t *key;
wl_array_for_each(key, pressed_keys)
{
if (*key == keycode) {
*key = 0;
break;
}
}
}
bool
cg_ime_keyboard_grab_forward_key(struct cg_ime_relay *relay, struct wlr_keyboard *keyboard,
struct wlr_keyboard_key_event *event)
{
struct wlr_input_method_v2 *input_method = relay->input_method;
if (!input_method) {
return false;
}
struct wlr_input_method_keyboard_grab_v2 *keyboard_grab = input_method->keyboard_grab;
if (!keyboard_grab) {
return false;
}
// If a key-release event is from zwp_virtual_keyboard_v1 created by IME, and there was not corresponding
// key-press event, wlroots would not send it to clients. So we should not handle the key-release events that
// not have corresponding key-press events, otherwise if a key was pressed before IME enabled, its release event
// would not be sent to clients.
assert(event->keycode != 0);
struct wl_array *pressed_keys = &relay->pressed_keys;
if (event->state == WL_KEYBOARD_KEY_STATE_RELEASED && !pressed_keys_contains(pressed_keys, event->keycode)) {
return false;
}
if (event->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
pressed_keys_add(pressed_keys, event->keycode);
} else {
pressed_keys_remove(pressed_keys, event->keycode);
}
wlr_input_method_keyboard_grab_v2_set_keyboard(keyboard_grab, keyboard);
wlr_input_method_keyboard_grab_v2_send_key(keyboard_grab, event->time_msec, event->keycode, event->state);
return true;
}

100
ime.h Normal file
View file

@ -0,0 +1,100 @@
/*
* Cage: A Wayland kiosk.
*
* Copyright (C) 2025 Cage Authors
* Copyright (C) 2020-2024 Sway Authors
*
* See the LICENSE file accompanying this file.
*/
#ifndef CG_IME_H
#define CG_IME_H
#include <stdbool.h>
#include <wayland-util.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_input_method_v2.h>
#include <wlr/types/wlr_text_input_v3.h>
/**
* The relay structure manages the relationship between text-input and
* input_method interfaces on a given seat. Multiple text-input interfaces may
* be bound to a relay, but at most one will be focused (receiving events) at
* a time. At most one input-method interface may be bound to the seat. The
* relay manages life cycle of both sides. When both sides are present and
* focused, the relay passes messages between them.
*
* Text input focus is a subset of keyboard focus - if the text-input is
* in the focused state, wl_keyboard sent an enter as well. However, having
* wl_keyboard focused doesn't mean that text-input will be focused.
*/
struct cg_ime_relay {
struct cg_seat *seat;
struct wl_list text_inputs; // cg_text_input::link
struct wl_list input_popups; // cg_input_popup::link
struct wlr_input_method_v2 *input_method; // doesn't have to be present
struct wl_array pressed_keys;
struct wl_listener input_method_commit;
struct wl_listener input_method_new_popup_surface;
struct wl_listener input_method_grab_keyboard;
struct wl_listener input_method_destroy;
struct wl_listener input_method_keyboard_grab_destroy;
};
struct cg_text_input {
struct cg_ime_relay *relay;
struct wlr_text_input_v3 *input;
// The surface getting seat's focus. Stored for when text-input cannot
// be sent an enter event immediately after getting focus, e.g. when
// there's no input method available. Cleared once text-input is entered.
struct wlr_surface *pending_focused_surface;
struct wl_list link;
struct wl_listener pending_focused_surface_destroy;
struct wl_listener text_input_enable;
struct wl_listener text_input_commit;
struct wl_listener text_input_disable;
struct wl_listener text_input_destroy;
};
struct cg_input_popup {
struct cg_ime_relay *relay;
struct wlr_scene_node *focused_scene_node;
struct cg_view *focused_view;
struct wlr_input_popup_surface_v2 *popup_surface;
struct wlr_output *fixed_output;
struct cg_view *view;
struct wl_list link;
struct wl_listener popup_destroy;
struct wl_listener popup_surface_commit;
struct wl_listener popup_surface_map;
struct wl_listener popup_surface_unmap;
struct wl_listener focused_surface_unmap;
};
void cg_ime_handle_new_input_method(struct wl_listener *listener, void *data);
void cg_ime_handle_new_text_input(struct wl_listener *listener, void *data);
void cg_ime_relay_init(struct cg_seat *seat, struct cg_ime_relay *relay);
void cg_ime_relay_finish(struct cg_ime_relay *relay);
// Updates currently focused surface. Surface must belong to the same seat.
void cg_ime_relay_set_focus(struct cg_ime_relay *relay, struct wlr_surface *surface);
bool cg_ime_keyboard_grab_forward_key(struct cg_ime_relay *relay, struct wlr_keyboard *keyboard,
struct wlr_keyboard_key_event *event);
#endif

View file

@ -117,6 +117,7 @@ cage_sources = [
'seat.c',
'view.c',
'xdg_shell.c',
'ime.c',
]
cage_headers = [

104
seat.c
View file

@ -35,6 +35,7 @@
#include <wlr/xwayland.h>
#endif
#include "ime.h"
#include "output.h"
#include "seat.h"
#include "server.h"
@ -276,9 +277,47 @@ handle_keybinding(struct cg_server *server, xkb_keysym_t sym)
return true;
}
static void
handle_key_event(struct wlr_keyboard *keyboard, struct cg_seat *seat, void *data)
static bool
is_keyboard_emulated(struct cg_keyboard_group *cg_group)
{
if (!cg_group->is_virtual) {
return false;
}
assert(cg_group->original_keyboard);
struct wlr_input_method_v2 *input_method = cg_group->seat->ime_relay.input_method;
struct wlr_virtual_keyboard_v1 *virtual_keyboard =
wlr_input_device_get_virtual_keyboard(&cg_group->original_keyboard->base);
assert(virtual_keyboard);
return wl_resource_get_client(input_method->resource) == wl_resource_get_client(virtual_keyboard->resource);
}
/**
* Get keyboard grab of the seat from cg_keyboard_group if we should forward events
* to it.
*
* Returns NULL if the keyboard is not grabbed by an input method,
* or if event is from virtual keyboard of the same client as grab.
* TODO: see https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/2322
*/
static struct wlr_input_method_keyboard_grab_v2 *
get_ime_keyboard_grab(struct cg_keyboard_group *group)
{
struct wlr_input_method_v2 *input_method = group->seat->ime_relay.input_method;
if (input_method && !is_keyboard_emulated(group)) {
return input_method->keyboard_grab;
}
return NULL;
}
static void
handle_key_event(struct cg_keyboard_group *cg_group, void *data)
{
struct cg_seat *seat = cg_group->seat;
struct wlr_keyboard *keyboard = &cg_group->wlr_group->keyboard;
struct wlr_keyboard_key_event *event = data;
/* Translate from libinput keycode to an xkbcommon keycode. */
@ -298,6 +337,11 @@ handle_key_event(struct wlr_keyboard *keyboard, struct cg_seat *seat, void *data
}
}
// try to forward the key to the input method
if (!handled && get_ime_keyboard_grab(cg_group)) {
handled = cg_ime_keyboard_grab_forward_key(&seat->ime_relay, keyboard, event);
}
if (!handled) {
/* Otherwise, we pass it along to the client. */
wlr_seat_set_keyboard(seat->seat, keyboard);
@ -311,14 +355,29 @@ static void
handle_keyboard_group_key(struct wl_listener *listener, void *data)
{
struct cg_keyboard_group *cg_group = wl_container_of(listener, cg_group, key);
handle_key_event(&cg_group->wlr_group->keyboard, cg_group->seat, data);
handle_key_event(cg_group, data);
}
static void
handle_ime_modifier_event(struct wlr_input_method_keyboard_grab_v2 *keyboard_grab, struct wlr_keyboard *keyboard,
struct cg_seat *seat)
{
wlr_input_method_keyboard_grab_v2_set_keyboard(keyboard_grab, keyboard);
wlr_input_method_keyboard_grab_v2_send_modifiers(keyboard_grab, &keyboard->modifiers);
wlr_idle_notifier_v1_notify_activity(seat->server->idle, seat->seat);
}
static void
handle_keyboard_group_modifiers(struct wl_listener *listener, void *data)
{
struct cg_keyboard_group *group = wl_container_of(listener, group, modifiers);
handle_modifier_event(&group->wlr_group->keyboard, group->seat);
struct wlr_keyboard *keyboard = &group->wlr_group->keyboard;
struct wlr_input_method_keyboard_grab_v2 *keyboard_grab = get_ime_keyboard_grab(group);
if (keyboard_grab) {
handle_ime_modifier_event(keyboard_grab, keyboard, group->seat);
} else {
handle_modifier_event(keyboard, group->seat);
}
}
static void
@ -371,6 +430,10 @@ cg_keyboard_group_add(struct wlr_keyboard *keyboard, struct cg_seat *seat, bool
wl_signal_add(&cg_group->wlr_group->keyboard.events.modifiers, &cg_group->modifiers);
cg_group->modifiers.notify = handle_keyboard_group_modifiers;
if (virtual) {
cg_group->original_keyboard = keyboard;
}
return;
cleanup:
@ -836,6 +899,8 @@ seat_create(struct cg_server *server, struct wlr_backend *backend)
}
}
cg_ime_relay_init(seat, &seat->ime_relay);
seat->cursor_motion_relative.notify = handle_cursor_motion_relative;
wl_signal_add(&seat->cursor->events.motion, &seat->cursor_motion_relative);
seat->cursor_motion_absolute.notify = handle_cursor_motion_absolute;
@ -893,6 +958,8 @@ seat_destroy(struct cg_seat *seat)
wl_list_remove(&seat->request_start_drag.link);
wl_list_remove(&seat->start_drag.link);
cg_ime_relay_finish(&seat->ime_relay);
// Destroying the wlr seat will trigger the destroy handler on our seat,
// which will in turn free it.
wlr_seat_destroy(seat->seat);
@ -940,19 +1007,24 @@ seat_set_focus(struct cg_seat *seat, struct cg_view *view)
}
view_activate(view, true);
char *title = view_get_title(view);
struct cg_output *output;
wl_list_for_each (output, &server->outputs, link) {
output_set_window_title(output, title);
}
free(title);
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(wlr_seat, view->wlr_surface, keyboard->keycodes, keyboard->num_keycodes,
&keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(wlr_seat, view->wlr_surface, NULL, 0, NULL);
if (view->type != CAGE_INPUT_POPUP_VIEW) {
char *title = view_get_title(view);
struct cg_output *output;
wl_list_for_each (output, &server->outputs, link) {
output_set_window_title(output, title);
}
free(title);
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(wlr_seat);
if (keyboard) {
wlr_seat_keyboard_notify_enter(wlr_seat, view->wlr_surface, keyboard->keycodes,
keyboard->num_keycodes, &keyboard->modifiers);
} else {
wlr_seat_keyboard_notify_enter(wlr_seat, view->wlr_surface, NULL, 0, NULL);
}
cg_ime_relay_set_focus(&seat->ime_relay, view->wlr_surface);
}
process_cursor_motion(seat, -1, 0, 0, 0, 0);

5
seat.h
View file

@ -8,6 +8,7 @@
#include <wlr/types/wlr_seat.h>
#include <wlr/types/wlr_xcursor_manager.h>
#include "ime.h"
#include "server.h"
#include "view.h"
@ -48,11 +49,15 @@ struct cg_seat {
struct wl_listener request_set_cursor;
struct wl_listener request_set_selection;
struct wl_listener request_set_primary_selection;
struct cg_ime_relay ime_relay;
};
struct cg_keyboard_group {
struct wlr_keyboard_group *wlr_group;
struct cg_seat *seat;
// only useful for virtual keyboards
struct wlr_keyboard *original_keyboard;
struct wl_listener key;
struct wl_listener modifiers;
struct wl_list link; // cg_seat::keyboard_groups

View file

@ -61,6 +61,12 @@ struct cg_server {
struct wlr_relative_pointer_manager_v1 *relative_pointer_manager;
struct wlr_input_method_manager_v2 *input_method_manager_v2;
struct wl_listener new_input_method;
struct wlr_text_input_manager_v3 *text_input_manager_v3;
struct wl_listener new_text_input;
bool xdg_decoration;
bool allow_vt_switch;
bool return_app_code;

2
view.c
View file

@ -137,7 +137,7 @@ view_map(struct cg_view *view, struct wlr_surface *surface)
#if CAGE_HAS_XWAYLAND
/* We shouldn't position override-redirect windows. They set
their own (x,y) coordinates in handle_wayland_surface_map. */
if (view->type != CAGE_XWAYLAND_VIEW || xwayland_view_should_manage(view))
if (view->type == CAGE_XDG_SHELL_VIEW || (view->type == CAGE_XWAYLAND_VIEW && xwayland_view_should_manage(view)))
#endif
{
view_position(view);

5
view.h
View file

@ -15,10 +15,11 @@
#include "server.h"
enum cg_view_type {
CAGE_XDG_SHELL_VIEW,
CAGE_XDG_SHELL_VIEW = 0,
#if CAGE_HAS_XWAYLAND
CAGE_XWAYLAND_VIEW,
CAGE_XWAYLAND_VIEW = 1,
#endif
CAGE_INPUT_POPUP_VIEW = 2,
};
struct cg_view {