text_input: Implement input-method popups

Co-authored-by: tadeokondrak <me@tadeo.ca>
This commit is contained in:
Access 2024-02-20 17:53:20 +08:00 committed by GitHub
parent d19810eba8
commit 7c11c463a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 253 additions and 1 deletions

View file

@ -21,18 +21,21 @@ struct sway_input_method_relay {
struct sway_seat *seat; struct sway_seat *seat;
struct wl_list text_inputs; // sway_text_input::link struct wl_list text_inputs; // sway_text_input::link
struct wl_list input_popups; // sway_input_popup::link
struct wlr_input_method_v2 *input_method; // doesn't have to be present struct wlr_input_method_v2 *input_method; // doesn't have to be present
struct wl_listener text_input_new; struct wl_listener text_input_new;
struct wl_listener input_method_new; struct wl_listener input_method_new;
struct wl_listener input_method_commit; 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_grab_keyboard;
struct wl_listener input_method_destroy; struct wl_listener input_method_destroy;
struct wl_listener input_method_keyboard_grab_destroy; struct wl_listener input_method_keyboard_grab_destroy;
}; };
struct sway_text_input { struct sway_text_input {
struct sway_input_method_relay *relay; struct sway_input_method_relay *relay;

View file

@ -0,0 +1,20 @@
#ifndef _SWAY_INPUT_TEXT_INPUT_POPUP_H
#define _SWAY_INPUT_TEXT_INPUT_POPUP_H
#include "sway/tree/view.h"
struct sway_input_popup {
struct sway_input_method_relay *relay;
struct wlr_scene_tree *scene_tree;
struct sway_popup_desc desc;
struct wlr_input_popup_surface_v2 *popup_surface;
struct wl_list link;
struct wl_listener popup_destroy;
struct wl_listener popup_surface_commit;
struct wl_listener focused_surface_unmap;
};
#endif

View file

@ -137,6 +137,7 @@ static struct sway_layer_surface *sway_layer_surface_create(
surface->scene = scene; surface->scene = scene;
surface->layer_surface = scene->layer_surface; surface->layer_surface = scene->layer_surface;
surface->popups = popups; surface->popups = popups;
surface->layer_surface->data = surface;
return surface; return surface;
} }
@ -210,6 +211,8 @@ static void handle_node_destroy(struct wl_listener *listener, void *data) {
wl_list_remove(&layer->node_destroy.link); wl_list_remove(&layer->node_destroy.link);
wl_list_remove(&layer->output_destroy.link); wl_list_remove(&layer->output_destroy.link);
layer->layer_surface->data = NULL;
free(layer); free(layer);
} }

View file

@ -2,7 +2,14 @@
#include <stdlib.h> #include <stdlib.h>
#include "log.h" #include "log.h"
#include "sway/input/seat.h" #include "sway/input/seat.h"
#include "sway/scene_descriptor.h"
#include "sway/tree/root.h"
#include "sway/tree/view.h"
#include "sway/output.h"
#include "sway/input/text_input.h" #include "sway/input/text_input.h"
#include "sway/input/text_input_popup.h"
#include "sway/layers.h"
static void input_popup_update(struct sway_input_popup *popup);
static struct sway_text_input *relay_get_focusable_text_input( static struct sway_text_input *relay_get_focusable_text_input(
struct sway_input_method_relay *relay) { struct sway_input_method_relay *relay) {
@ -102,6 +109,7 @@ static void handle_im_destroy(struct wl_listener *listener, void *data) {
input_method_destroy); input_method_destroy);
struct wlr_input_method_v2 *context = data; struct wlr_input_method_v2 *context = data;
assert(context == relay->input_method); assert(context == relay->input_method);
wl_list_remove(&relay->input_method_new_popup_surface.link);
relay->input_method = NULL; relay->input_method = NULL;
struct sway_text_input *text_input = relay_get_focused_text_input(relay); struct sway_text_input *text_input = relay_get_focused_text_input(relay);
if (text_input) { if (text_input) {
@ -133,6 +141,11 @@ static void relay_send_im_state(struct sway_input_method_relay *relay,
input->current.content_type.hint, input->current.content_type.hint,
input->current.content_type.purpose); input->current.content_type.purpose);
} }
struct sway_input_popup *popup;
wl_list_for_each(popup, &relay->input_popups, link) {
// send_text_input_rectangle is called in this function
input_popup_update(popup);
}
wlr_input_method_v2_send_done(input_method); wlr_input_method_v2_send_done(input_method);
// TODO: pass intent, display popup size // TODO: pass intent, display popup size
} }
@ -255,6 +268,215 @@ static void relay_handle_text_input(struct wl_listener *listener,
sway_text_input_create(relay, wlr_text_input); sway_text_input_create(relay, wlr_text_input);
} }
static void input_popup_update(struct sway_input_popup *popup) {
struct sway_text_input *text_input =
relay_get_focused_text_input(popup->relay);
if (text_input == NULL || text_input->input->focused_surface == NULL) {
return;
}
if (popup->scene_tree != NULL) {
wlr_scene_node_destroy(&popup->scene_tree->node);
popup->scene_tree = NULL;
}
if (popup->desc.relative != NULL) {
wlr_scene_node_destroy(popup->desc.relative);
popup->desc.relative = NULL;
}
popup->desc.view = NULL;
if (!popup->popup_surface->surface->mapped) {
return;
}
wlr_scene_node_destroy(&popup->scene_tree->node);
wlr_scene_node_destroy(popup->desc.relative);
popup->scene_tree = NULL;
bool cursor_rect = text_input->input->current.features
& WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
struct wlr_surface *focused_surface = text_input->input->focused_surface;
struct wlr_box cursor_area = text_input->input->current.cursor_rectangle;
struct wlr_box output_box;
struct wlr_box parent;
struct wlr_layer_surface_v1 *layer_surface =
wlr_layer_surface_v1_try_from_wlr_surface(focused_surface);
struct wlr_scene_tree *relative_parent;
struct wlr_box geo = {0};
popup->scene_tree = wlr_scene_subsurface_tree_create(root->layers.popup, popup->popup_surface->surface);
if (layer_surface != NULL) {
struct sway_layer_surface *layer =
layer_surface->data;
if (layer == NULL) {
return;
}
relative_parent = layer->scene->tree;
struct wlr_output *output = layer->layer_surface->output;
wlr_output_layout_get_box(root->output_layout, output, &output_box);
int lx, ly;
wlr_scene_node_coords(&layer->tree->node, &lx, &ly);
parent.x = lx;
parent.y = ly;
popup->desc.view = NULL;
} else {
struct sway_view *view = view_from_wlr_surface(focused_surface);
relative_parent = view->scene_tree;
geo = view->geometry;
int lx, ly;
wlr_scene_node_coords(&view->scene_tree->node, &lx, &ly);
struct wlr_output *output = wlr_output_layout_output_at(root->output_layout,
view->container->pending.content_x + view->geometry.x,
view->container->pending.content_y + view->geometry.y);
wlr_output_layout_get_box(root->output_layout, output, &output_box);
parent.x = lx;
parent.y = ly;
parent.width = view->geometry.width;
parent.height = view->geometry.height;
popup->desc.view = view;
}
struct wlr_scene_tree *relative = wlr_scene_tree_create(relative_parent);
popup->desc.relative = &relative->node;
if (!scene_descriptor_assign(&popup->scene_tree->node,
SWAY_SCENE_DESC_POPUP, &popup->desc)) {
wlr_scene_node_destroy(&popup->scene_tree->node);
popup->scene_tree = NULL;
return;
}
if (!cursor_rect) {
cursor_area.x = 0;
cursor_area.y = 0;
cursor_area.width = parent.width;
cursor_area.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(&relative->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);
}
wlr_scene_node_set_position(&popup->scene_tree->node, x - geo.x, y - geo.y);
}
static void input_popup_set_focus(struct sway_input_popup *popup,
struct wlr_surface *surface) {
wl_list_remove(&popup->focused_surface_unmap.link);
if (surface == NULL) {
wl_list_init(&popup->focused_surface_unmap.link);
input_popup_update(popup);
return;
}
struct wlr_layer_surface_v1 *layer_surface =
wlr_layer_surface_v1_try_from_wlr_surface(surface);
if (layer_surface != NULL) {
wl_signal_add(
&layer_surface->surface->events.unmap, &popup->focused_surface_unmap);
input_popup_update(popup);
return;
}
struct sway_view *view = view_from_wlr_surface(surface);
wl_signal_add(&view->events.unmap, &popup->focused_surface_unmap);
}
static void handle_im_popup_destroy(struct wl_listener *listener, void *data) {
struct sway_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_destroy.link);
wl_list_remove(&popup->link);
free(popup);
}
static void handle_im_popup_surface_commit(struct wl_listener *listener,
void *data) {
struct sway_input_popup *popup =
wl_container_of(listener, popup, popup_surface_commit);
input_popup_update(popup);
}
static void handle_im_focused_surface_unmap(
struct wl_listener *listener, void *data) {
struct sway_input_popup *popup =
wl_container_of(listener, popup, focused_surface_unmap);
input_popup_update(popup);
}
static void handle_im_new_popup_surface(struct wl_listener *listener,
void *data) {
struct sway_input_method_relay *relay = wl_container_of(listener, relay,
input_method_new_popup_surface);
struct sway_input_popup *popup = calloc(1, sizeof(*popup));
popup->relay = relay;
popup->popup_surface = data;
popup->popup_surface->data = popup;
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_list_init(&popup->focused_surface_unmap.link);
popup->focused_surface_unmap.notify = handle_im_focused_surface_unmap;
struct sway_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 sway_text_input *text_input,
struct wlr_surface *surface) {
wlr_text_input_v3_send_enter(text_input->input, surface);
struct sway_input_popup *popup;
wl_list_for_each(popup, &text_input->relay->input_popups, link) {
input_popup_set_focus(popup, surface);
}
}
static void relay_handle_input_method(struct wl_listener *listener, static void relay_handle_input_method(struct wl_listener *listener,
void *data) { void *data) {
struct sway_input_method_relay *relay = wl_container_of(listener, relay, struct sway_input_method_relay *relay = wl_container_of(listener, relay,
@ -280,10 +502,13 @@ static void relay_handle_input_method(struct wl_listener *listener,
wl_signal_add(&relay->input_method->events.destroy, wl_signal_add(&relay->input_method->events.destroy,
&relay->input_method_destroy); &relay->input_method_destroy);
relay->input_method_destroy.notify = handle_im_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 sway_text_input *text_input = relay_get_focusable_text_input(relay); struct sway_text_input *text_input = relay_get_focusable_text_input(relay);
if (text_input) { if (text_input) {
wlr_text_input_v3_send_enter(text_input->input, text_input_send_enter(text_input,
text_input->pending_focused_surface); text_input->pending_focused_surface);
text_input_set_pending_focused_surface(text_input, NULL); text_input_set_pending_focused_surface(text_input, NULL);
} }
@ -293,6 +518,7 @@ void sway_input_method_relay_init(struct sway_seat *seat,
struct sway_input_method_relay *relay) { struct sway_input_method_relay *relay) {
relay->seat = seat; relay->seat = seat;
wl_list_init(&relay->text_inputs); wl_list_init(&relay->text_inputs);
wl_list_init(&relay->input_popups);
relay->text_input_new.notify = relay_handle_text_input; relay->text_input_new.notify = relay_handle_text_input;
wl_signal_add(&server.text_input->events.text_input, wl_signal_add(&server.text_input->events.text_input,