1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
|
#include <fcntl.h>
#include <unistd.h>
#include <set>
#include <wayfire/plugin.hpp>
#include <wayfire/core.hpp>
#include <wayfire/unstable/wlr-text-input-v3-popup.hpp>
#include "input-method-unstable-v1-protocol.h"
#include "wayfire/option-wrapper.hpp"
#include "wayfire/nonstd/wlroots-full.hpp"
#include "wayfire/nonstd/wlroots.hpp"
#include "wayfire/debug.hpp"
#include "wayfire/signal-definitions.hpp"
#include "wayfire/seat.hpp"
#include "text-input-v1-v3.hpp"
#include "input-method-v1.hpp"
class wayfire_input_method_v1_context
{
public:
wayfire_input_method_v1_context(wayfire_im_text_input_base_t *text_input, wl_resource *current_im,
const struct zwp_input_method_context_v1_interface *context_impl)
{
this->text_input = text_input;
this->current_im = current_im;
context = wl_resource_create(wl_resource_get_client(current_im),
&zwp_input_method_context_v1_interface, 1, 0);
wl_resource_set_implementation(context, context_impl, this, handle_ctx_destruct_final);
zwp_input_method_v1_send_activate(current_im, context);
}
static void handle_ctx_destruct_final(wl_resource *resource)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context)
{
context->deactivate(true);
}
}
void handle_text_input_v3_commit()
{
auto ti_v3 = dynamic_cast<wayfire_im_v1_text_input_v3*>(text_input);
wf::dassert(ti_v3, "handle_text_input_v3_commit called without text_input_v3");
auto text_input_v3 = ti_v3->text_input_v3;
zwp_input_method_context_v1_send_content_type(context,
text_input_v3->current.content_type.hint, text_input_v3->current.content_type.purpose);
zwp_input_method_context_v1_send_surrounding_text(context,
text_input_v3->current.surrounding.text ?: "",
text_input_v3->current.surrounding.cursor,
text_input_v3->current.surrounding.anchor);
zwp_input_method_context_v1_send_commit_state(context, ctx_serial++);
}
void deactivate(bool im_killed = false)
{
wl_resource_set_user_data(context, NULL);
auto& seat = wf::get_core().seat;
if (im_killed)
{
// Remove keys which core still thinks are pressed down physically: they will be sent as release
// events to the client at a later point.
for (auto& hw_pressed : seat->get_pressed_keys())
{
if (currently_pressed_keys_client.count(hw_pressed))
{
currently_pressed_keys_client.erase(
currently_pressed_keys_client.find(hw_pressed));
}
}
// For the other keys (where we potentially swallowed the release event, but the IM did not
// respond yet with a release), release those keys.
for (auto& key : currently_pressed_keys_client)
{
wlr_seat_keyboard_notify_key(seat->seat, wf::get_current_time(),
key, WL_KEYBOARD_KEY_STATE_RELEASED);
}
currently_pressed_keys_client.clear();
if (active_grab_keyboard)
{
wl_resource_set_user_data(active_grab_keyboard, NULL);
}
this->text_input = NULL;
return;
}
this->text_input = NULL;
zwp_input_method_v1_send_deactivate(current_im, context);
if (active_grab_keyboard)
{
for (auto& key : currently_pressed_keys_im)
{
wl_keyboard_send_key(active_grab_keyboard, vkbd_serial++, wf::get_current_time(),
key, WL_KEYBOARD_KEY_STATE_RELEASED);
}
currently_pressed_keys_im.clear();
wl_resource_destroy(active_grab_keyboard);
}
}
void handle_im_key(uint32_t time, uint32_t key, uint32_t state)
{
auto& seat = wf::get_core().seat;
wlr_seat_keyboard_notify_key(seat->seat, time, key, state);
update_pressed_keys(currently_pressed_keys_client, key, state);
}
void handle_im_modifiers(uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group)
{
wlr_keyboard_modifiers mods{
.depressed = mods_depressed,
.latched = mods_latched,
.locked = mods_locked,
.group = group
};
auto& seat = wf::get_core().seat;
wlr_seat_keyboard_notify_modifiers(seat->seat, &mods);
}
void grab_keyboard(wl_client *client, uint32_t id)
{
this->active_grab_keyboard = wl_resource_create(client, &wl_keyboard_interface, 1, id);
wl_resource_set_implementation(active_grab_keyboard, NULL, this, unbind_keyboard);
wf::get_core().connect(&on_keyboard_key);
wf::get_core().connect(&on_keyboard_modifiers);
}
static void unbind_keyboard(wl_resource *keyboard)
{
auto self = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(keyboard));
if (!self)
{
return;
}
self->active_grab_keyboard = NULL;
self->last_sent_keymap_keyboard = NULL;
self->on_keyboard_key.disconnect();
self->on_keyboard_modifiers.disconnect();
self->currently_pressed_keys_im.clear();
}
void update_pressed_keys(std::multiset<uint32_t>& set, uint32_t key, uint32_t state)
{
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
{
set.insert(key);
} else if (set.count(key))
{
set.erase(set.find(key));
}
}
wf::signal::connection_t<wf::pre_client_input_event_signal<wlr_keyboard_key_event>> on_keyboard_key =
[=] (wf::pre_client_input_event_signal<wlr_keyboard_key_event> *ev)
{
if (active_grab_keyboard && !ev->carried_out)
{
check_send_keymap(wlr_keyboard_from_input_device(ev->device));
ev->carried_out = true;
wl_keyboard_send_key(active_grab_keyboard, vkbd_serial++, ev->event->time_msec,
ev->event->keycode, ev->event->state);
// Keep track of pressed keys so that we can release all of them at the end.
// Otherwise the IM gets stuck thinking that some modifiers are pressed, etc.
update_pressed_keys(currently_pressed_keys_im, ev->event->keycode, ev->event->state);
}
};
wf::signal::connection_t<wf::input_event_signal<mwlr_keyboard_modifiers_event>> on_keyboard_modifiers =
[=] (wf::input_event_signal<mwlr_keyboard_modifiers_event> *ev)
{
if (active_grab_keyboard)
{
auto kbd = wlr_keyboard_from_input_device(ev->device);
check_send_keymap(kbd);
wl_keyboard_send_modifiers(active_grab_keyboard, vkbd_serial++,
kbd->modifiers.depressed, kbd->modifiers.latched, kbd->modifiers.locked,
kbd->modifiers.group);
}
};
void check_send_keymap(wlr_keyboard *current_kbd)
{
if (current_kbd == last_sent_keymap_keyboard)
{
return;
}
last_sent_keymap_keyboard = current_kbd;
if (current_kbd->keymap != NULL)
{
wl_keyboard_send_keymap(active_grab_keyboard,
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, current_kbd->keymap_fd, current_kbd->keymap_size);
} else
{
int fd = open("/dev/null", O_RDONLY | O_CLOEXEC);
wl_keyboard_send_keymap(active_grab_keyboard,
WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP, fd, 0);
close(fd);
}
// Send new modifiers
wl_keyboard_send_modifiers(active_grab_keyboard, vkbd_serial++,
current_kbd->modifiers.depressed, current_kbd->modifiers.latched, current_kbd->modifiers.locked,
current_kbd->modifiers.group);
}
std::multiset<uint32_t> currently_pressed_keys_im;
std::multiset<uint32_t> currently_pressed_keys_client;
wlr_keyboard *last_sent_keymap_keyboard = NULL;
wl_resource *active_grab_keyboard = NULL;
uint32_t ctx_serial = 0;
uint32_t vkbd_serial = 0;
wl_resource *current_im = NULL;
wl_resource *context = NULL;
// NULL if inactive
wayfire_im_text_input_base_t *text_input = NULL;
};
void handle_im_context_destroy(wl_client *client, wl_resource *resource)
{
wl_resource_destroy(resource);
}
void handle_im_context_commit_string(wl_client *client, wl_resource *resource,
uint32_t serial, const char *text)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_commit_string(serial, text);
}
}
void handle_im_context_preedit_string(wl_client *client, wl_resource *resource,
uint32_t serial, const char *text, const char *commit)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_preedit_string(serial, text, commit);
}
}
void handle_im_context_preedit_styling(wl_client *client, wl_resource *resource,
uint32_t index, uint32_t length, uint32_t style)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_preedit_styling(index, length, style);
}
}
void handle_im_context_preedit_cursor(wl_client *client, wl_resource *resource, int32_t index)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_preedit_cursor(index);
}
}
void handle_im_context_delete_surrounding_text(wl_client *client, wl_resource *resource,
int32_t index, uint32_t length)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_delete_surrounding_text(index, length);
}
}
void handle_im_context_cursor_position(wl_client *client, wl_resource *resource,
int32_t index, int32_t anchor)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_cursor_position(index, anchor);
}
}
void handle_im_context_modifiers_map(wl_client *client, wl_resource *resource,
struct wl_array *map)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_modifiers_map(map);
}
}
void handle_im_context_keysym(wl_client *client, wl_resource *resource,
uint32_t serial, uint32_t time, uint32_t sym, uint32_t state,
uint32_t modifiers)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_keysym(serial, time, sym, state, modifiers);
}
}
void handle_im_context_grab_keyboard(wl_client *client, wl_resource *resource,
uint32_t keyboard_id)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context)
{
context->grab_keyboard(client, keyboard_id);
} else
{
// Create a dummy resource to avoid Wayland protocol errors.
// But, we have already moved on from this context, so we won't send any events.
auto resource = wl_resource_create(client, &wl_keyboard_interface, 1, keyboard_id);
wl_resource_set_implementation(resource, NULL, NULL, NULL);
}
}
void handle_im_context_key(wl_client*, wl_resource *resource,
uint32_t, uint32_t time, uint32_t key, uint32_t state)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context)
{
context->handle_im_key(time, key, state);
}
}
void handle_im_context_modifiers(wl_client *client, wl_resource *resource,
uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context)
{
context->handle_im_modifiers(serial, mods_depressed, mods_latched, mods_locked, group);
}
}
void handle_im_context_language(wl_client *client, wl_resource *resource,
uint32_t serial, const char *language)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_language(serial, language);
}
}
void handle_im_context_text_direction(wl_client *client, wl_resource *resource,
uint32_t serial, uint32_t direction)
{
auto context = static_cast<wayfire_input_method_v1_context*>(wl_resource_get_user_data(resource));
if (context && context->text_input)
{
context->text_input->send_text_direction(serial, direction);
}
}
static const struct zwp_input_method_context_v1_interface context_implementation = {
.destroy = handle_im_context_destroy,
.commit_string = handle_im_context_commit_string,
.preedit_string = handle_im_context_preedit_string,
.preedit_styling = handle_im_context_preedit_styling,
.preedit_cursor = handle_im_context_preedit_cursor,
.delete_surrounding_text = handle_im_context_delete_surrounding_text,
.cursor_position = handle_im_context_cursor_position,
.modifiers_map = handle_im_context_modifiers_map,
.keysym = handle_im_context_keysym,
.grab_keyboard = handle_im_context_grab_keyboard,
.key = handle_im_context_key,
.modifiers = handle_im_context_modifiers,
.language = handle_im_context_language,
.text_direction = handle_im_context_text_direction
};
void handle_input_panel_get_input_panel_surface(wl_client *client, wl_resource *resource,
uint32_t id, struct wl_resource *surface);
static const struct zwp_input_panel_v1_interface panel_implementation = {
.get_input_panel_surface = handle_input_panel_get_input_panel_surface
};
void handle_input_panel_surface_set_toplevel(wl_client *client, wl_resource *resource,
struct wl_resource *output, uint32_t position);
void handle_input_panel_surface_set_overlay_panel(wl_client *client, wl_resource *resource);
static const struct zwp_input_panel_surface_v1_interface panel_surface_implementation = {
.set_toplevel = handle_input_panel_surface_set_toplevel,
.set_overlay_panel = handle_input_panel_surface_set_overlay_panel
};
class wayfire_input_method_v1_panel_surface
{
public:
wayfire_input_method_v1_panel_surface(wl_client *client, uint32_t id,
wf::text_input_v3_im_relay_interface_t *relay, wlr_surface *surface)
{
LOGC(IM, "Input method panel surface created.");
resource = wl_resource_create(client, &zwp_input_panel_surface_v1_interface, 1, id);
wl_resource_set_implementation(resource, &panel_surface_implementation, this, handle_destroy);
this->surface = surface;
this->relay = relay;
on_surface_commit.set_callback([=] (void*)
{
if (wlr_surface_has_buffer(surface) && !surface->mapped)
{
wlr_surface_map(surface);
} else if (!wlr_surface_has_buffer(surface) && surface->mapped)
{
wlr_surface_unmap(surface);
}
});
on_surface_commit.connect(&surface->events.commit);
// Initial commit, maybe already ok?
on_surface_commit.emit(NULL);
on_surface_destroy.set_callback([=] (void*)
{
if (surface->mapped)
{
wlr_surface_unmap(surface);
}
on_surface_destroy.disconnect();
on_surface_commit.disconnect();
});
on_surface_destroy.connect(&surface->events.destroy);
}
void set_overlay_panel()
{
LOGC(IM, "Input method panel surface set to overlay.");
popup = wf::text_input_v3_popup::create(relay, surface);
if (surface->mapped)
{
popup->map();
}
}
~wayfire_input_method_v1_panel_surface()
{
if (popup && popup->is_mapped())
{
popup->unmap();
}
}
private:
wl_resource *resource;
wlr_surface *surface;
wf::text_input_v3_im_relay_interface_t *relay;
std::shared_ptr<wf::text_input_v3_popup> popup = nullptr;
wf::wl_listener_wrapper on_surface_commit;
wf::wl_listener_wrapper on_surface_destroy;
static void handle_destroy(wl_resource *destroy)
{
auto panel = (wayfire_input_method_v1_panel_surface*)wl_resource_get_user_data(destroy);
delete panel;
}
};
void handle_input_panel_get_input_panel_surface(wl_client *client, wl_resource *resource,
uint32_t id, struct wl_resource *surface)
{
new wayfire_input_method_v1_panel_surface(client, id,
(wf::text_input_v3_im_relay_interface_t*)wl_resource_get_user_data(resource),
(wlr_surface*)wl_resource_get_user_data(surface));
}
void handle_input_panel_surface_set_toplevel(wl_client *client, wl_resource *resource,
wl_resource *output, uint32_t position)
{
LOGE("The set toplevel request is not supported by the IM-v1 implementation!");
}
void handle_input_panel_surface_set_overlay_panel(wl_client *client, wl_resource *resource)
{
auto panel = (wayfire_input_method_v1_panel_surface*)wl_resource_get_user_data(resource);
if (panel)
{
panel->set_overlay_panel();
}
}
class wayfire_input_method_v1 : public wf::plugin_interface_t, public wf::text_input_v3_im_relay_interface_t
{
public:
void init() override
{
if (enable_input_method_v2)
{
LOGE("Enabling both input-method-v2 and input-method-v1 is a bad idea!");
return;
}
input_method_manager = wl_global_create(wf::get_core().display, &zwp_input_method_v1_interface, 1,
this, handle_bind_im_v1);
input_panel_manager = wl_global_create(wf::get_core().display, &zwp_input_panel_v1_interface, 1,
this, handle_bind_im_panel_v1);
if (enable_text_input_v1)
{
text_input_v1_manager = wl_global_create(wf::get_core().display,
&zwp_text_input_manager_v1_interface, 1, this, handle_bind_text_input_v1);
}
if (enable_text_input_v3)
{
wf::get_core().protocols.text_input = wlr_text_input_manager_v3_create(wf::get_core().display);
on_text_input_v3_created.connect(&wf::get_core().protocols.text_input->events.text_input);
on_text_input_v3_created.set_callback([&] (void *data)
{
handle_text_input_v3_created(static_cast<wlr_text_input_v3*>(data));
});
}
wf::get_core().connect(&on_keyboard_focus_changed);
}
void fini() override
{
if (input_method_manager)
{
reset_current_im_context();
wl_global_destroy(input_method_manager);
if (current_im)
{
wl_resource_set_user_data(current_im, NULL);
}
}
if (text_input_v1_manager)
{
wl_global_destroy(text_input_v1_manager);
for (auto& [input, _] : im_text_inputs_v1)
{
wl_resource_set_user_data(input, NULL);
}
}
}
bool is_unloadable() override
{
return false;
}
wf::signal::connection_t<wf::keyboard_focus_changed_signal> on_keyboard_focus_changed =
[=] (wf::keyboard_focus_changed_signal *ev)
{
auto view = wf::node_to_view(ev->new_focus);
auto surf = view ? view->get_wlr_surface() : nullptr;
if (last_focus_surface != surf)
{
reset_current_im_context();
last_focus_surface = surf;
for_each_text_input([=] (wayfire_im_text_input_base_t *text_input)
{
text_input->set_focus_surface(last_focus_surface);
});
}
};
// Handlers for text-input-v1
private:
wl_global *text_input_v1_manager = nullptr;
static void handle_bind_text_input_v1(wl_client *client, void *data, uint32_t version, uint32_t id)
{
((wayfire_input_method_v1*)data)->bind_text_input_v1_manager(client, id);
}
void bind_text_input_v1_manager(wl_client *client, uint32_t id)
{
wl_resource *resource = wl_resource_create(client, &zwp_text_input_manager_v1_interface, 1, id);
static const struct zwp_text_input_manager_v1_interface text_input_manager_implementation = {
handle_create_text_input_v1
};
wl_resource_set_implementation(resource, &text_input_manager_implementation, this, NULL);
}
static void handle_create_text_input_v1(wl_client *client, wl_resource *resource, uint32_t id)
{
auto self = (wayfire_input_method_v1*)wl_resource_get_user_data(resource);
auto ti_resource = wl_resource_create(client, &zwp_text_input_v1_interface, 1, id);
// Define the struct with function pointers
static const struct zwp_text_input_v1_interface text_input_v1_impl = {
.activate = handle_text_input_v1_activate,
.deactivate = handle_text_input_v1_deactivate,
.show_input_panel = handle_text_input_v1_show_input_panel,
.hide_input_panel = handle_text_input_v1_hide_input_panel,
.reset = handle_text_input_v1_reset,
.set_surrounding_text = handle_text_input_v1_set_surrounding_text,
.set_content_type = handle_text_input_v1_set_content_type,
.set_cursor_rectangle = handle_text_input_v1_set_cursor_rectangle,
.set_preferred_language = handle_text_input_v1_set_preferred_language,
.commit_state = handle_text_input_v1_commit_state,
.invoke_action = handle_text_input_v1_invoke_action
};
wl_resource_set_implementation(ti_resource, &text_input_v1_impl, self, handle_text_input_v1_destroy);
self->im_text_inputs_v1[ti_resource] = std::make_unique<wayfire_im_v1_text_input_v1>(ti_resource);
}
static void handle_text_input_v1_destroy(wl_resource *resource)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self)
{
self->im_handle_text_input_disable(self->im_text_inputs_v1[resource].get());
self->im_text_inputs_v1.erase(resource);
}
}
static void handle_text_input_v1_activate(wl_client *client, wl_resource *resource, wl_resource *seat,
wl_resource *surface)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
auto *ti = self->im_text_inputs_v1[resource].get();
if (!ti->can_focus() || (ti->current_focus->resource != surface))
{
LOGC(IM, "text-input-v1: ignore activate request for wrong focus surface!");
// Only for focused surfaces!
return;
}
if (self->current_im_context)
{
self->im_handle_text_input_disable(self->current_im_context->text_input);
}
self->im_handle_text_input_enable(ti);
}
static void handle_text_input_v1_deactivate(wl_client *client, wl_resource *resource, wl_resource *seat)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
auto *ti = self->im_text_inputs_v1[resource].get();
self->im_handle_text_input_disable(ti);
}
static void handle_text_input_v1_show_input_panel(wl_client *client, wl_resource *resource)
{}
static void handle_text_input_v1_hide_input_panel(wl_client *client, wl_resource *resource)
{}
static void handle_text_input_v1_reset(wl_client *client, wl_resource *resource)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self->current_im_context)
{
zwp_input_method_context_v1_send_reset(self->current_im_context->context);
}
}
static void handle_text_input_v1_set_surrounding_text(wl_client *client, wl_resource *resource,
const char *text, uint32_t cursor, uint32_t anchor)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self->current_im_context)
{
zwp_input_method_context_v1_send_surrounding_text(self->current_im_context->context,
text, cursor, anchor);
}
}
static void handle_text_input_v1_set_content_type(wl_client *client, wl_resource *resource,
uint32_t hint, uint32_t purpose)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self->current_im_context)
{
zwp_input_method_context_v1_send_content_type(self->current_im_context->context,
hint, purpose);
}
}
static void handle_text_input_v1_set_cursor_rectangle(wl_client *client, wl_resource *resource,
int32_t x, int32_t y, int32_t width, int32_t height)
{
// ignore, we do not support cursor rectangle or input popups for text-input-v1 yet
}
static void handle_text_input_v1_set_preferred_language(wl_client *client, wl_resource *resource,
const char *language)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self->current_im_context)
{
zwp_input_method_context_v1_send_preferred_language(self->current_im_context->context, language);
}
}
static void handle_text_input_v1_commit_state(wl_client *client, wl_resource *resource, uint32_t serial)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self->current_im_context)
{
zwp_input_method_context_v1_send_commit_state(self->current_im_context->context, serial);
}
}
static void handle_text_input_v1_invoke_action(wl_client *client, wl_resource *resource,
uint32_t button, uint32_t index)
{
auto self = static_cast<wayfire_input_method_v1*>(wl_resource_get_user_data(resource));
if (self->current_im_context)
{
zwp_input_method_context_v1_send_invoke_action(self->current_im_context->context, button, index);
}
}
// Handlers for text-input-v3
private:
void handle_text_input_v3_created(wlr_text_input_v3 *input)
{
im_text_inputs_v3[input] = std::make_unique<wayfire_im_v1_text_input_v3>(input);
im_text_inputs_v3[input]->on_enable.set_callback([=] (void *data)
{
im_handle_text_input_enable(im_text_inputs_v3[input].get());
});
im_text_inputs_v3[input]->on_disable.set_callback([=] (void *data)
{
im_handle_text_input_disable(im_text_inputs_v3[input].get());
});
im_text_inputs_v3[input]->on_destroy.set_callback([=] (void *data)
{
handle_text_input_v3_destroyed(input);
});
im_text_inputs_v3[input]->on_commit.set_callback([=] (void *data)
{
handle_text_input_v3_commit(input);
});
im_text_inputs_v3[input]->set_focus_surface(last_focus_surface);
}
void handle_text_input_v3_destroyed(wlr_text_input_v3 *input)
{
im_handle_text_input_disable(im_text_inputs_v3[input].get());
im_text_inputs_v3.erase(input);
}
void handle_text_input_v3_commit(wlr_text_input_v3 *input)
{
if (current_im_context && (current_im_context->text_input == im_text_inputs_v3[input].get()))
{
current_im_context->handle_text_input_v3_commit();
wf::text_input_commit_signal data;
data.cursor_rect = input->current.cursor_rectangle;
emit(&data);
}
}
wlr_text_input_v3 *find_focused_text_input_v3() override
{
if (!current_im_context)
{
return nullptr;
}
auto as_v3 = dynamic_cast<wayfire_im_v1_text_input_v3*>(current_im_context->text_input);
return as_v3 ? as_v3->text_input_v3 : nullptr;
}
// Implementation of input-method-v1
private:
void bind_input_method_manager(wl_client *client, uint32_t id)
{
wl_resource *resource = wl_resource_create(client, &zwp_input_method_v1_interface, 1, id);
if (current_im)
{
LOGE("Trying to bind to input-method-v1 while another input method is active is not supported!");
wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, "Input method already bound");
return;
}
LOGC(IM, "Input method bound");
wl_resource_set_implementation(resource, NULL, this, handle_destroy_im);
current_im = resource;
for (auto& [_, im] : im_text_inputs_v3)
{
if (im->is_enabled())
{
im_handle_text_input_enable(im.get());
}
}
}
static void handle_bind_im_v1(wl_client *client, void *data, uint32_t version, uint32_t id)
{
((wayfire_input_method_v1*)data)->bind_input_method_manager(client, id);
}
static void handle_destroy_im(wl_resource *resource)
{
LOGC(IM, "Input method unbound");
auto data = wl_resource_get_user_data(resource);
if (data)
{
((wayfire_input_method_v1*)data)->reset_current_im_context(true);
((wayfire_input_method_v1*)data)->current_im = nullptr;
}
}
void im_handle_text_input_enable(wayfire_im_text_input_base_t *text_input)
{
wf::input_method_v1_activate_signal data;
wf::get_core().emit(&data);
if (!current_im)
{
LOGC(IM, "No IM currently connected: ignoring enable request.");
return;
}
if (!last_focus_surface || (text_input->current_focus != last_focus_surface))
{
LOGC(IM, "Ignoring enable request for text input ", text_input->dbg_handle, ": stale request");
return;
}
if (current_im_context)
{
LOGC(IM, "Text input activated while old context is still around?");
return;
}
LOGC(IM, "Enabling IM context for ", text_input->dbg_handle);
current_im_context = std::make_unique<wayfire_input_method_v1_context>(
text_input, current_im, &context_implementation);
}
void im_handle_text_input_disable(wayfire_im_text_input_base_t *input)
{
wf::input_method_v1_deactivate_signal data;
wf::get_core().emit(&data);
if (!current_im_context || (current_im_context->text_input != input))
{
return;
}
reset_current_im_context();
}
void reset_current_im_context(bool im_killed = false)
{
if (!current_im_context)
{
return;
}
LOGC(IM, "Disabling IM context for ", current_im_context->text_input->dbg_handle);
current_im_context->deactivate(im_killed);
current_im_context.reset();
}
// input-method-panel impl
private:
void bind_input_method_panel(wl_client *client, uint32_t id)
{
LOGC(IM, "Input method panel interface bound");
wl_resource *resource = wl_resource_create(client, &zwp_input_panel_v1_interface, 1, id);
wl_resource_set_implementation(resource, &panel_implementation,
dynamic_cast<wf::text_input_v3_im_relay_interface_t*>(this), handle_destroy_im_panel);
}
static void handle_bind_im_panel_v1(wl_client *client, void *data, uint32_t version, uint32_t id)
{
((wayfire_input_method_v1*)data)->bind_input_method_panel(client, id);
}
static void handle_destroy_im_panel(wl_resource *resource)
{
LOGC(IM, "Input method panel interface unbound");
}
private:
wf::option_wrapper_t<bool> enable_input_method_v2{"workarounds/enable_input_method_v2"};
wf::option_wrapper_t<bool> enable_text_input_v1{"input-method-v1/enable_text_input_v1"};
wf::option_wrapper_t<bool> enable_text_input_v3{"input-method-v1/enable_text_input_v3"};
wl_global *input_method_manager = NULL;
wl_global *input_panel_manager = NULL;
wl_resource *current_im = NULL;
wf::wl_listener_wrapper on_text_input_v3_created;
wlr_surface *last_focus_surface = NULL;
std::unique_ptr<wayfire_input_method_v1_context> current_im_context = NULL;
std::map<wl_resource*, std::unique_ptr<wayfire_im_v1_text_input_v1>> im_text_inputs_v1;
std::map<wlr_text_input_v3*, std::unique_ptr<wayfire_im_v1_text_input_v3>> im_text_inputs_v3;
void for_each_text_input(std::function<void(wayfire_im_text_input_base_t*)> func)
{
for (auto& [_, im] : im_text_inputs_v1)
{
func(im.get());
}
for (auto& [_, im] : im_text_inputs_v3)
{
func(im.get());
}
}
};
DECLARE_WAYFIRE_PLUGIN(wayfire_input_method_v1);
|