forked from mirror/dwl
implement input popup rendering
Based on https://github.com/swaywm/sway/pull/5890
This commit is contained in:
parent
c86f2f73fd
commit
27bf627a97
1 changed files with 268 additions and 0 deletions
268
dwl.c
268
dwl.c
|
@ -218,6 +218,7 @@ typedef struct {
|
|||
*/
|
||||
struct dwl_input_method_relay {
|
||||
struct wl_list text_inputs; // dwl_text_input::link
|
||||
struct wl_list input_popups; // dwl_input_popup::link
|
||||
struct wlr_input_method_v2 *input_method; // doesn't have to be present
|
||||
|
||||
struct wl_listener text_input_new;
|
||||
|
@ -225,6 +226,7 @@ struct dwl_input_method_relay {
|
|||
struct wl_listener input_method_new;
|
||||
struct wl_listener input_method_commit;
|
||||
struct wl_listener input_method_destroy;
|
||||
struct wl_listener input_method_new_popup_surface;
|
||||
};
|
||||
|
||||
struct dwl_text_input {
|
||||
|
@ -246,6 +248,27 @@ struct dwl_text_input {
|
|||
struct wl_listener text_input_destroy;
|
||||
};
|
||||
|
||||
|
||||
struct dwl_input_popup {
|
||||
struct dwl_input_method_relay *relay;
|
||||
struct wlr_input_popup_surface_v2 *popup_surface;
|
||||
|
||||
struct wlr_scene_node *scene;
|
||||
struct wlr_scene_node *scene_surface;
|
||||
|
||||
int x, y;
|
||||
bool visible;
|
||||
|
||||
struct wl_list link;
|
||||
struct wl_list view_link;
|
||||
|
||||
struct wl_listener popup_map;
|
||||
struct wl_listener popup_unmap;
|
||||
struct wl_listener popup_destroy;
|
||||
struct wl_listener popup_surface_commit;
|
||||
struct wl_listener focused_surface_unmap;
|
||||
};
|
||||
|
||||
/* Used to move all of the data necessary to render a surface from the top-level
|
||||
* frame handler to the per-surface render function. */
|
||||
struct render_data {
|
||||
|
@ -370,6 +393,9 @@ static struct wlr_xdg_shell *xdg_shell;
|
|||
static struct wlr_xdg_activation_v1 *activation;
|
||||
static struct wl_list clients; /* tiling order */
|
||||
static struct wl_list fstack; /* focus order */
|
||||
|
||||
static struct wl_list input_popups; /* dwl_input_popup::view_link to be rendered for the input method */
|
||||
|
||||
static struct wlr_idle *idle;
|
||||
static struct wlr_idle_inhibit_manager_v1 *idle_inhibit_mgr;
|
||||
static struct wlr_input_inhibit_manager *input_inhibit_mgr;
|
||||
|
@ -1022,6 +1048,7 @@ createnotify(struct wl_listener *listener, void *data)
|
|||
} else if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_NONE)
|
||||
return;
|
||||
|
||||
|
||||
/* Allocate a Client for this surface */
|
||||
c = xdg_surface->data = ecalloc(1, sizeof(*c));
|
||||
c->surface.xdg = xdg_surface;
|
||||
|
@ -2200,6 +2227,241 @@ static void relay_handle_text_input(struct wl_listener *listener,
|
|||
dwl_text_input_create(relay, wlr_text_input);
|
||||
}
|
||||
|
||||
static LayerSurface* layer_surface_from_wlr_layer_surface_v1(
|
||||
struct wlr_layer_surface_v1* layer_surface) {
|
||||
return layer_surface->data;
|
||||
}
|
||||
|
||||
static Client* client_from_wlr_surface(struct wlr_surface* surface) {
|
||||
return wlr_xdg_surface_from_wlr_surface(surface)->data;
|
||||
}
|
||||
|
||||
static void get_parent_and_output_box(struct wlr_surface *focused_surface,
|
||||
struct wlr_box *parent, struct wlr_box *output_box) {
|
||||
struct wlr_output *output;
|
||||
struct wlr_box *output_box_tmp;
|
||||
|
||||
if (wlr_surface_is_layer_surface(focused_surface)) {
|
||||
struct wlr_layer_surface_v1 *layer_surface =
|
||||
wlr_layer_surface_v1_from_wlr_surface(focused_surface);
|
||||
LayerSurface* layer =
|
||||
layer_surface_from_wlr_layer_surface_v1(layer_surface);
|
||||
output = layer->layer_surface->output;
|
||||
output_box_tmp = wlr_output_layout_get_box(output_layout, output);
|
||||
*parent = layer->geo;
|
||||
parent->x += output_box_tmp->x;
|
||||
parent->y += output_box_tmp->y;
|
||||
} else {
|
||||
Client *client = client_from_wlr_surface(focused_surface);
|
||||
output = wlr_output_layout_output_at(output_layout,
|
||||
client->geom.x, client->geom.y);
|
||||
output_box_tmp = wlr_output_layout_get_box(output_layout, output);
|
||||
parent->x = client->geom.x + client->bw;
|
||||
parent->y = client->geom.y + client->bw;
|
||||
parent->width = client->geom.width;
|
||||
parent->height = client->geom.height;
|
||||
}
|
||||
|
||||
*output_box = *output_box_tmp;
|
||||
}
|
||||
|
||||
static void input_popup_update(struct dwl_input_popup *popup) {
|
||||
struct wlr_surface* focused_surface;
|
||||
struct wlr_box output_box, parent, cursor;
|
||||
int x1, x2, y1, y2, x, y, available_right, available_left, available_down,
|
||||
available_up, popup_width, popup_height;
|
||||
bool cursor_rect, x1_in_bounds, y1_in_bounds, x2_in_bounds, y2_in_bounds;
|
||||
|
||||
struct dwl_text_input *text_input =
|
||||
relay_get_focused_text_input(popup->relay);
|
||||
if (!text_input|| !text_input->input->focused_surface) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!popup->popup_surface->mapped) {
|
||||
return;
|
||||
}
|
||||
|
||||
cursor_rect = text_input->input->current.features
|
||||
& WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE;
|
||||
focused_surface = text_input->input->focused_surface;
|
||||
cursor = text_input->input->current.cursor_rectangle;
|
||||
|
||||
get_parent_and_output_box(focused_surface, &parent, &output_box);
|
||||
|
||||
if (!cursor_rect) {
|
||||
cursor.x = 0;
|
||||
cursor.y = 0;
|
||||
cursor.width = parent.width;
|
||||
cursor.height = parent.height;
|
||||
}
|
||||
|
||||
popup_width = popup->popup_surface->surface->current.width;
|
||||
popup_height = popup->popup_surface->surface->current.height;
|
||||
x1 = parent.x + cursor.x;
|
||||
x2 = parent.x + cursor.x + cursor.width;
|
||||
y1 = parent.y + cursor.y;
|
||||
y2 = parent.y + cursor.y + cursor.height;
|
||||
x = x1;
|
||||
y = y2;
|
||||
|
||||
available_right = output_box.x + output_box.width - x1;
|
||||
available_left = x2 - output_box.x;
|
||||
if (available_right < popup_width && available_left > available_right) {
|
||||
x = x2 - popup_width;
|
||||
}
|
||||
|
||||
available_down = output_box.y + output_box.height - y2;
|
||||
available_up = y1 - output_box.y;
|
||||
if (available_down < popup_height && available_up > available_down) {
|
||||
y = y1 - popup_height;
|
||||
}
|
||||
|
||||
popup->x = x;
|
||||
popup->y = y;
|
||||
|
||||
// Hide popup if cursor position is completely out of bounds
|
||||
x1_in_bounds = (cursor.x >= 0 && cursor.x < parent.width);
|
||||
y1_in_bounds = (cursor.y >= 0 && cursor.y < parent.height);
|
||||
x2_in_bounds = (cursor.x + cursor.width >= 0
|
||||
&& cursor.x + cursor.width < parent.width);
|
||||
y2_in_bounds = (cursor.y + cursor.height >= 0
|
||||
&& cursor.y + cursor.height < parent.height);
|
||||
popup->visible =
|
||||
(x1_in_bounds && y1_in_bounds) || (x2_in_bounds && y2_in_bounds);
|
||||
|
||||
if (cursor_rect) {
|
||||
struct wlr_box box = {
|
||||
.x = x1 - x,
|
||||
.y = y1 - y,
|
||||
.width = cursor.width,
|
||||
.height = cursor.height,
|
||||
};
|
||||
wlr_input_popup_surface_v2_send_text_input_rectangle(
|
||||
popup->popup_surface, &box);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_im_popup_map(struct wl_listener *listener, void *data) {
|
||||
struct dwl_input_popup *popup =
|
||||
wl_container_of(listener, popup, popup_map);
|
||||
|
||||
popup->scene = &wlr_scene_tree_create(layers[LyrFloat])->node;
|
||||
popup->scene_surface = wlr_scene_subsurface_tree_create(popup->scene,
|
||||
popup->popup_surface->surface);
|
||||
popup->scene_surface->data = popup;
|
||||
|
||||
input_popup_update(popup);
|
||||
|
||||
wlr_scene_node_set_position(popup->scene, popup->x, popup->y);
|
||||
}
|
||||
|
||||
static void handle_im_popup_unmap(struct wl_listener *listener, void *data) {
|
||||
struct dwl_input_popup *popup =
|
||||
wl_container_of(listener, popup, popup_unmap);
|
||||
input_popup_update(popup);
|
||||
|
||||
wlr_scene_node_destroy(popup->scene);
|
||||
}
|
||||
|
||||
static void handle_im_popup_destroy(struct wl_listener *listener, void *data) {
|
||||
struct dwl_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->popup_unmap.link);
|
||||
wl_list_remove(&popup->popup_map.link);
|
||||
wl_list_remove(&popup->view_link);
|
||||
wl_list_remove(&popup->link);
|
||||
free(popup);
|
||||
}
|
||||
|
||||
static void handle_im_popup_surface_commit(struct wl_listener *listener,
|
||||
void *data) {
|
||||
struct dwl_input_popup *popup =
|
||||
wl_container_of(listener, popup, popup_surface_commit);
|
||||
input_popup_update(popup);
|
||||
}
|
||||
|
||||
static void handle_im_focused_surface_destroy(
|
||||
struct wl_listener *listener, void *data) {
|
||||
struct dwl_input_popup *popup =
|
||||
wl_container_of(listener, popup, focused_surface_unmap);
|
||||
wl_list_remove(&popup->view_link);
|
||||
wl_list_init(&popup->view_link);
|
||||
input_popup_update(popup);
|
||||
}
|
||||
|
||||
static void input_popup_set_focus(struct dwl_input_popup *popup,
|
||||
struct wlr_surface *surface) {
|
||||
Client* client;
|
||||
struct wlr_layer_surface_v1* layer_surface;
|
||||
LayerSurface* layer;
|
||||
|
||||
wl_list_remove(&popup->view_link);
|
||||
wl_list_remove(&popup->focused_surface_unmap.link);
|
||||
|
||||
if (wlr_surface_is_layer_surface(surface)) {
|
||||
layer_surface = wlr_layer_surface_v1_from_wlr_surface(surface);
|
||||
layer = layer_surface_from_wlr_layer_surface_v1(layer_surface);
|
||||
wl_list_insert(&input_popups, &popup->view_link);
|
||||
wl_signal_add(&layer->layer_surface->events.unmap,
|
||||
&popup->focused_surface_unmap);
|
||||
input_popup_update(popup);
|
||||
return;
|
||||
}
|
||||
|
||||
client = client_from_wlr_surface(surface);
|
||||
wl_list_insert(&input_popups, &popup->view_link);
|
||||
wl_signal_add(&client->surface.xdg->events.unmap,
|
||||
&popup->focused_surface_unmap);
|
||||
input_popup_update(popup);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void handle_im_new_popup_surface(struct wl_listener *listener, void *data) {
|
||||
struct dwl_text_input* text_input;
|
||||
|
||||
struct dwl_input_method_relay *relay = wl_container_of(listener, relay,
|
||||
input_method_new_popup_surface);
|
||||
struct dwl_input_popup *popup = calloc(1, sizeof(*popup));
|
||||
popup->relay = relay;
|
||||
popup->popup_surface = data;
|
||||
popup->popup_surface->data = popup;
|
||||
|
||||
wl_list_init(&popup->view_link);
|
||||
|
||||
wl_signal_add(&popup->popup_surface->events.map, &popup->popup_map);
|
||||
popup->popup_map.notify = handle_im_popup_map;
|
||||
|
||||
wl_signal_add(
|
||||
&popup->popup_surface->events.unmap, &popup->popup_unmap);
|
||||
popup->popup_unmap.notify = handle_im_popup_unmap;
|
||||
|
||||
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_destroy;
|
||||
|
||||
text_input = relay_get_focused_text_input(relay);
|
||||
if (text_input) {
|
||||
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 relay_handle_input_method(struct wl_listener *listener,
|
||||
void *data) {
|
||||
struct dwl_text_input *text_input;
|
||||
|
@ -2222,6 +2484,9 @@ static void relay_handle_input_method(struct wl_listener *listener,
|
|||
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.new_popup_surface,
|
||||
&relay->input_method_new_popup_surface);
|
||||
relay->input_method_new_popup_surface.notify = handle_im_new_popup_surface;
|
||||
wl_signal_add(&relay->input_method->events.destroy,
|
||||
&relay->input_method_destroy);
|
||||
relay->input_method_destroy.notify = handle_im_destroy;
|
||||
|
@ -2236,6 +2501,7 @@ static void relay_handle_input_method(struct wl_listener *listener,
|
|||
|
||||
void dwl_input_method_relay_init(struct dwl_input_method_relay *relay) {
|
||||
wl_list_init(&relay->text_inputs);
|
||||
wl_list_init(&relay->input_popups);
|
||||
|
||||
relay->text_input_new.notify = relay_handle_text_input;
|
||||
wl_signal_add(&text_input_manager->events.text_input,
|
||||
|
@ -2359,6 +2625,8 @@ setup(void)
|
|||
wl_list_init(&clients);
|
||||
wl_list_init(&fstack);
|
||||
|
||||
wl_list_init(&input_popups);
|
||||
|
||||
idle = wlr_idle_create(dpy);
|
||||
|
||||
idle_inhibit_mgr = wlr_idle_inhibit_v1_create(dpy);
|
||||
|
|
Loading…
Reference in a new issue