diff options
author | Access <ShootingStarDragons@protonmail.com> | 2024-02-20 17:53:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-20 10:53:20 +0100 |
commit | 7c11c463a3e7f821ed9f3c6de59e37358441952e (patch) | |
tree | 1b57ec2c30b93269391490ffae0f84d0e8016218 /sway | |
parent | xdg-activation: distinguish activation and urgency requests (diff) | |
download | sway-7c11c463a3e7f821ed9f3c6de59e37358441952e.tar.gz sway-7c11c463a3e7f821ed9f3c6de59e37358441952e.tar.zst sway-7c11c463a3e7f821ed9f3c6de59e37358441952e.zip |
text_input: Implement input-method popups
Co-authored-by: tadeokondrak <me@tadeo.ca>
Diffstat (limited to 'sway')
-rw-r--r-- | sway/desktop/layer_shell.c | 3 | ||||
-rw-r--r-- | sway/input/text_input.c | 228 |
2 files changed, 230 insertions, 1 deletions
diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index f0134396..4b2584b6 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c | |||
@@ -137,6 +137,7 @@ static struct sway_layer_surface *sway_layer_surface_create( | |||
137 | surface->scene = scene; | 137 | surface->scene = scene; |
138 | surface->layer_surface = scene->layer_surface; | 138 | surface->layer_surface = scene->layer_surface; |
139 | surface->popups = popups; | 139 | surface->popups = popups; |
140 | surface->layer_surface->data = surface; | ||
140 | 141 | ||
141 | return surface; | 142 | return surface; |
142 | } | 143 | } |
@@ -210,6 +211,8 @@ static void handle_node_destroy(struct wl_listener *listener, void *data) { | |||
210 | wl_list_remove(&layer->node_destroy.link); | 211 | wl_list_remove(&layer->node_destroy.link); |
211 | wl_list_remove(&layer->output_destroy.link); | 212 | wl_list_remove(&layer->output_destroy.link); |
212 | 213 | ||
214 | layer->layer_surface->data = NULL; | ||
215 | |||
213 | free(layer); | 216 | free(layer); |
214 | } | 217 | } |
215 | 218 | ||
diff --git a/sway/input/text_input.c b/sway/input/text_input.c index 58911c2d..d80e34ac 100644 --- a/sway/input/text_input.c +++ b/sway/input/text_input.c | |||
@@ -2,7 +2,14 @@ | |||
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include "log.h" | 3 | #include "log.h" |
4 | #include "sway/input/seat.h" | 4 | #include "sway/input/seat.h" |
5 | #include "sway/scene_descriptor.h" | ||
6 | #include "sway/tree/root.h" | ||
7 | #include "sway/tree/view.h" | ||
8 | #include "sway/output.h" | ||
5 | #include "sway/input/text_input.h" | 9 | #include "sway/input/text_input.h" |
10 | #include "sway/input/text_input_popup.h" | ||
11 | #include "sway/layers.h" | ||
12 | static void input_popup_update(struct sway_input_popup *popup); | ||
6 | 13 | ||
7 | static struct sway_text_input *relay_get_focusable_text_input( | 14 | static struct sway_text_input *relay_get_focusable_text_input( |
8 | struct sway_input_method_relay *relay) { | 15 | struct sway_input_method_relay *relay) { |
@@ -102,6 +109,7 @@ static void handle_im_destroy(struct wl_listener *listener, void *data) { | |||
102 | input_method_destroy); | 109 | input_method_destroy); |
103 | struct wlr_input_method_v2 *context = data; | 110 | struct wlr_input_method_v2 *context = data; |
104 | assert(context == relay->input_method); | 111 | assert(context == relay->input_method); |
112 | wl_list_remove(&relay->input_method_new_popup_surface.link); | ||
105 | relay->input_method = NULL; | 113 | relay->input_method = NULL; |
106 | struct sway_text_input *text_input = relay_get_focused_text_input(relay); | 114 | struct sway_text_input *text_input = relay_get_focused_text_input(relay); |
107 | if (text_input) { | 115 | if (text_input) { |
@@ -133,6 +141,11 @@ static void relay_send_im_state(struct sway_input_method_relay *relay, | |||
133 | input->current.content_type.hint, | 141 | input->current.content_type.hint, |
134 | input->current.content_type.purpose); | 142 | input->current.content_type.purpose); |
135 | } | 143 | } |
144 | struct sway_input_popup *popup; | ||
145 | wl_list_for_each(popup, &relay->input_popups, link) { | ||
146 | // send_text_input_rectangle is called in this function | ||
147 | input_popup_update(popup); | ||
148 | } | ||
136 | wlr_input_method_v2_send_done(input_method); | 149 | wlr_input_method_v2_send_done(input_method); |
137 | // TODO: pass intent, display popup size | 150 | // TODO: pass intent, display popup size |
138 | } | 151 | } |
@@ -255,6 +268,215 @@ static void relay_handle_text_input(struct wl_listener *listener, | |||
255 | sway_text_input_create(relay, wlr_text_input); | 268 | sway_text_input_create(relay, wlr_text_input); |
256 | } | 269 | } |
257 | 270 | ||
271 | static void input_popup_update(struct sway_input_popup *popup) { | ||
272 | struct sway_text_input *text_input = | ||
273 | relay_get_focused_text_input(popup->relay); | ||
274 | |||
275 | if (text_input == NULL || text_input->input->focused_surface == NULL) { | ||
276 | return; | ||
277 | } | ||
278 | |||
279 | if (popup->scene_tree != NULL) { | ||
280 | wlr_scene_node_destroy(&popup->scene_tree->node); | ||
281 | popup->scene_tree = NULL; | ||
282 | } | ||
283 | if (popup->desc.relative != NULL) { | ||
284 | wlr_scene_node_destroy(popup->desc.relative); | ||
285 | popup->desc.relative = NULL; | ||
286 | } | ||
287 | popup->desc.view = NULL; | ||
288 | |||
289 | if (!popup->popup_surface->surface->mapped) { | ||
290 | return; | ||
291 | } | ||
292 | |||
293 | wlr_scene_node_destroy(&popup->scene_tree->node); | ||
294 | wlr_scene_node_destroy(popup->desc.relative); | ||
295 | popup->scene_tree = NULL; | ||
296 | |||
297 | bool cursor_rect = text_input->input->current.features | ||
298 | & WLR_TEXT_INPUT_V3_FEATURE_CURSOR_RECTANGLE; | ||
299 | struct wlr_surface *focused_surface = text_input->input->focused_surface; | ||
300 | struct wlr_box cursor_area = text_input->input->current.cursor_rectangle; | ||
301 | |||
302 | struct wlr_box output_box; | ||
303 | struct wlr_box parent; | ||
304 | struct wlr_layer_surface_v1 *layer_surface = | ||
305 | wlr_layer_surface_v1_try_from_wlr_surface(focused_surface); | ||
306 | struct wlr_scene_tree *relative_parent; | ||
307 | |||
308 | struct wlr_box geo = {0}; | ||
309 | |||
310 | popup->scene_tree = wlr_scene_subsurface_tree_create(root->layers.popup, popup->popup_surface->surface); | ||
311 | if (layer_surface != NULL) { | ||
312 | struct sway_layer_surface *layer = | ||
313 | layer_surface->data; | ||
314 | if (layer == NULL) { | ||
315 | return; | ||
316 | } | ||
317 | |||
318 | relative_parent = layer->scene->tree; | ||
319 | struct wlr_output *output = layer->layer_surface->output; | ||
320 | wlr_output_layout_get_box(root->output_layout, output, &output_box); | ||
321 | int lx, ly; | ||
322 | wlr_scene_node_coords(&layer->tree->node, &lx, &ly); | ||
323 | parent.x = lx; | ||
324 | parent.y = ly; | ||
325 | popup->desc.view = NULL; | ||
326 | } else { | ||
327 | struct sway_view *view = view_from_wlr_surface(focused_surface); | ||
328 | relative_parent = view->scene_tree; | ||
329 | geo = view->geometry; | ||
330 | int lx, ly; | ||
331 | wlr_scene_node_coords(&view->scene_tree->node, &lx, &ly); | ||
332 | struct wlr_output *output = wlr_output_layout_output_at(root->output_layout, | ||
333 | view->container->pending.content_x + view->geometry.x, | ||
334 | view->container->pending.content_y + view->geometry.y); | ||
335 | wlr_output_layout_get_box(root->output_layout, output, &output_box); | ||
336 | parent.x = lx; | ||
337 | parent.y = ly; | ||
338 | |||
339 | parent.width = view->geometry.width; | ||
340 | parent.height = view->geometry.height; | ||
341 | popup->desc.view = view; | ||
342 | } | ||
343 | |||
344 | struct wlr_scene_tree *relative = wlr_scene_tree_create(relative_parent); | ||
345 | |||
346 | popup->desc.relative = &relative->node; | ||
347 | if (!scene_descriptor_assign(&popup->scene_tree->node, | ||
348 | SWAY_SCENE_DESC_POPUP, &popup->desc)) { | ||
349 | wlr_scene_node_destroy(&popup->scene_tree->node); | ||
350 | popup->scene_tree = NULL; | ||
351 | return; | ||
352 | } | ||
353 | |||
354 | if (!cursor_rect) { | ||
355 | cursor_area.x = 0; | ||
356 | cursor_area.y = 0; | ||
357 | cursor_area.width = parent.width; | ||
358 | cursor_area.height = parent.height; | ||
359 | } | ||
360 | |||
361 | int popup_width = popup->popup_surface->surface->current.width; | ||
362 | int popup_height = popup->popup_surface->surface->current.height; | ||
363 | int x1 = parent.x + cursor_area.x; | ||
364 | int x2 = parent.x + cursor_area.x + cursor_area.width; | ||
365 | int y1 = parent.y + cursor_area.y; | ||
366 | int y2 = parent.y + cursor_area.y + cursor_area.height; | ||
367 | int x = x1; | ||
368 | int y = y2; | ||
369 | |||
370 | int available_right = output_box.x + output_box.width - x1; | ||
371 | int available_left = x2 - output_box.x; | ||
372 | if (available_right < popup_width && available_left > available_right) { | ||
373 | x = x2 - popup_width; | ||
374 | } | ||
375 | |||
376 | int available_down = output_box.y + output_box.height - y2; | ||
377 | int available_up = y1 - output_box.y; | ||
378 | if (available_down < popup_height && available_up > available_down) { | ||
379 | y = y1 - popup_height; | ||
380 | } | ||
381 | |||
382 | wlr_scene_node_set_position(&relative->node, x - parent.x - geo.x, y - parent.y - geo.y); | ||
383 | if (cursor_rect) { | ||
384 | struct wlr_box box = { | ||
385 | .x = x1 - x, | ||
386 | .y = y1 - y, | ||
387 | .width = cursor_area.width, | ||
388 | .height = cursor_area.height, | ||
389 | }; | ||
390 | wlr_input_popup_surface_v2_send_text_input_rectangle( | ||
391 | popup->popup_surface, &box); | ||
392 | } | ||
393 | wlr_scene_node_set_position(&popup->scene_tree->node, x - geo.x, y - geo.y); | ||
394 | } | ||
395 | |||
396 | static void input_popup_set_focus(struct sway_input_popup *popup, | ||
397 | struct wlr_surface *surface) { | ||
398 | wl_list_remove(&popup->focused_surface_unmap.link); | ||
399 | |||
400 | if (surface == NULL) { | ||
401 | wl_list_init(&popup->focused_surface_unmap.link); | ||
402 | input_popup_update(popup); | ||
403 | return; | ||
404 | } | ||
405 | struct wlr_layer_surface_v1 *layer_surface = | ||
406 | wlr_layer_surface_v1_try_from_wlr_surface(surface); | ||
407 | if (layer_surface != NULL) { | ||
408 | wl_signal_add( | ||
409 | &layer_surface->surface->events.unmap, &popup->focused_surface_unmap); | ||
410 | input_popup_update(popup); | ||
411 | return; | ||
412 | } | ||
413 | |||
414 | struct sway_view *view = view_from_wlr_surface(surface); | ||
415 | wl_signal_add(&view->events.unmap, &popup->focused_surface_unmap); | ||
416 | } | ||
417 | |||
418 | static void handle_im_popup_destroy(struct wl_listener *listener, void *data) { | ||
419 | struct sway_input_popup *popup = | ||
420 | wl_container_of(listener, popup, popup_destroy); | ||
421 | wl_list_remove(&popup->focused_surface_unmap.link); | ||
422 | wl_list_remove(&popup->popup_surface_commit.link); | ||
423 | wl_list_remove(&popup->popup_destroy.link); | ||
424 | wl_list_remove(&popup->link); | ||
425 | |||
426 | free(popup); | ||
427 | } | ||
428 | |||
429 | static void handle_im_popup_surface_commit(struct wl_listener *listener, | ||
430 | void *data) { | ||
431 | struct sway_input_popup *popup = | ||
432 | wl_container_of(listener, popup, popup_surface_commit); | ||
433 | input_popup_update(popup); | ||
434 | } | ||
435 | |||
436 | static void handle_im_focused_surface_unmap( | ||
437 | struct wl_listener *listener, void *data) { | ||
438 | struct sway_input_popup *popup = | ||
439 | wl_container_of(listener, popup, focused_surface_unmap); | ||
440 | input_popup_update(popup); | ||
441 | } | ||
442 | |||
443 | static void handle_im_new_popup_surface(struct wl_listener *listener, | ||
444 | void *data) { | ||
445 | struct sway_input_method_relay *relay = wl_container_of(listener, relay, | ||
446 | input_method_new_popup_surface); | ||
447 | struct sway_input_popup *popup = calloc(1, sizeof(*popup)); | ||
448 | popup->relay = relay; | ||
449 | popup->popup_surface = data; | ||
450 | popup->popup_surface->data = popup; | ||
451 | |||
452 | wl_signal_add( | ||
453 | &popup->popup_surface->events.destroy, &popup->popup_destroy); | ||
454 | popup->popup_destroy.notify = handle_im_popup_destroy; | ||
455 | wl_signal_add(&popup->popup_surface->surface->events.commit, | ||
456 | &popup->popup_surface_commit); | ||
457 | popup->popup_surface_commit.notify = handle_im_popup_surface_commit; | ||
458 | wl_list_init(&popup->focused_surface_unmap.link); | ||
459 | popup->focused_surface_unmap.notify = handle_im_focused_surface_unmap; | ||
460 | |||
461 | struct sway_text_input *text_input = relay_get_focused_text_input(relay); | ||
462 | if (text_input != NULL) { | ||
463 | input_popup_set_focus(popup, text_input->input->focused_surface); | ||
464 | } else { | ||
465 | input_popup_set_focus(popup, NULL); | ||
466 | } | ||
467 | |||
468 | wl_list_insert(&relay->input_popups, &popup->link); | ||
469 | } | ||
470 | |||
471 | static void text_input_send_enter(struct sway_text_input *text_input, | ||
472 | struct wlr_surface *surface) { | ||
473 | wlr_text_input_v3_send_enter(text_input->input, surface); | ||
474 | struct sway_input_popup *popup; | ||
475 | wl_list_for_each(popup, &text_input->relay->input_popups, link) { | ||
476 | input_popup_set_focus(popup, surface); | ||
477 | } | ||
478 | } | ||
479 | |||
258 | static void relay_handle_input_method(struct wl_listener *listener, | 480 | static void relay_handle_input_method(struct wl_listener *listener, |
259 | void *data) { | 481 | void *data) { |
260 | struct sway_input_method_relay *relay = wl_container_of(listener, relay, | 482 | 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, | |||
280 | wl_signal_add(&relay->input_method->events.destroy, | 502 | wl_signal_add(&relay->input_method->events.destroy, |
281 | &relay->input_method_destroy); | 503 | &relay->input_method_destroy); |
282 | relay->input_method_destroy.notify = handle_im_destroy; | 504 | relay->input_method_destroy.notify = handle_im_destroy; |
505 | wl_signal_add(&relay->input_method->events.new_popup_surface, | ||
506 | &relay->input_method_new_popup_surface); | ||
507 | relay->input_method_new_popup_surface.notify = handle_im_new_popup_surface; | ||
283 | 508 | ||
284 | struct sway_text_input *text_input = relay_get_focusable_text_input(relay); | 509 | struct sway_text_input *text_input = relay_get_focusable_text_input(relay); |
285 | if (text_input) { | 510 | if (text_input) { |
286 | wlr_text_input_v3_send_enter(text_input->input, | 511 | text_input_send_enter(text_input, |
287 | text_input->pending_focused_surface); | 512 | text_input->pending_focused_surface); |
288 | text_input_set_pending_focused_surface(text_input, NULL); | 513 | text_input_set_pending_focused_surface(text_input, NULL); |
289 | } | 514 | } |
@@ -293,6 +518,7 @@ void sway_input_method_relay_init(struct sway_seat *seat, | |||
293 | struct sway_input_method_relay *relay) { | 518 | struct sway_input_method_relay *relay) { |
294 | relay->seat = seat; | 519 | relay->seat = seat; |
295 | wl_list_init(&relay->text_inputs); | 520 | wl_list_init(&relay->text_inputs); |
521 | wl_list_init(&relay->input_popups); | ||
296 | 522 | ||
297 | relay->text_input_new.notify = relay_handle_text_input; | 523 | relay->text_input_new.notify = relay_handle_text_input; |
298 | wl_signal_add(&server.text_input->events.text_input, | 524 | wl_signal_add(&server.text_input->events.text_input, |