Skip to content

Commit a6704e3

Browse files
committed
Wayland: Gate text-input routing on focus
Map explicit text input focus to text-input-v3 enable/disable and text-input-v1 activate/deactivate. When text input focus is inactive, avoid auto-enabling text input on protocol enter and ignore stale text-input callbacks. Applications that never call glfwSetTextInputFocus keep the previous enter-time activation behavior. On focus out, reset local preedit state and send the available protocol reset/disable requests.
1 parent 478f5e8 commit a6704e3

1 file changed

Lines changed: 71 additions & 6 deletions

File tree

src/wl_window.c

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@ static void deactivateTextInputV1(_GLFWwindow* window)
734734
zwp_text_input_v1_deactivate(window->wl.textInputV1, _glfw.wl.seat);
735735
}
736736

737+
static GLFWbool textInputFocusDisabled(_GLFWwindow* window)
738+
{
739+
return window->textInputFocusInitialized && !window->textInputFocus;
740+
}
741+
737742
static void xdgToplevelHandleConfigure(void* userData,
738743
struct xdg_toplevel* toplevel,
739744
int32_t width,
@@ -761,7 +766,8 @@ static void xdgToplevelHandleConfigure(void* userData,
761766
break;
762767
case XDG_TOPLEVEL_STATE_ACTIVATED:
763768
window->wl.pending.activated = GLFW_TRUE;
764-
activateTextInputV1(window);
769+
if (!textInputFocusDisabled(window))
770+
activateTextInputV1(window);
765771
break;
766772
}
767773
}
@@ -1706,7 +1712,8 @@ static void pointerHandleButton(void* userData,
17061712
// On weston, pressing the title bar will cause leave event and never emit
17071713
// enter event even though back to content area by pressing mouse button
17081714
// just after it. So activate it here explicitly.
1709-
activateTextInputV1(window);
1715+
if (!textInputFocusDisabled(window))
1716+
activateTextInputV1(window);
17101717

17111718
_glfw.wl.serial = serial;
17121719

@@ -2399,8 +2406,13 @@ static void textInputV3Enter(void* data,
23992406
struct zwp_text_input_v3* textInputV3,
24002407
struct wl_surface* surface)
24012408
{
2402-
zwp_text_input_v3_enable(textInputV3);
2403-
zwp_text_input_v3_commit(textInputV3);
2409+
_GLFWwindow* window = (_GLFWwindow*) data;
2410+
2411+
if (!textInputFocusDisabled(window))
2412+
{
2413+
zwp_text_input_v3_enable(textInputV3);
2414+
zwp_text_input_v3_commit(textInputV3);
2415+
}
24042416
}
24052417

24062418
static void textInputV3Reset(_GLFWwindow* window)
@@ -2440,6 +2452,9 @@ static void textInputV3PreeditString(void* data,
24402452
const char* cur = text;
24412453
unsigned int cursorLength = 0;
24422454

2455+
if (textInputFocusDisabled(window))
2456+
return;
2457+
24432458
preedit->textCount = 0;
24442459
preedit->blockSizesCount = 0;
24452460
preedit->focusedBlockIndex = 0;
@@ -2513,6 +2528,9 @@ static void textInputV3CommitString(void* data,
25132528
_GLFWwindow* window = (_GLFWwindow*) data;
25142529
const char* cur = text;
25152530

2531+
if (textInputFocusDisabled(window))
2532+
return;
2533+
25162534
if (!window->callbacks.character)
25172535
return;
25182536

@@ -2535,6 +2553,9 @@ static void textInputV3Done(void* data,
25352553
uint32_t serial)
25362554
{
25372555
_GLFWwindow* window = (_GLFWwindow*) data;
2556+
if (textInputFocusDisabled(window))
2557+
return;
2558+
25382559
_glfwUpdatePreeditCursorRectangleWayland(window);
25392560
_glfwInputPreedit(window);
25402561
}
@@ -2560,7 +2581,9 @@ static void textInputV1Enter(void* data,
25602581
struct wl_surface* surface)
25612582
{
25622583
_GLFWwindow* window = (_GLFWwindow*) data;
2563-
activateTextInputV1(window);
2584+
2585+
if (!textInputFocusDisabled(window))
2586+
activateTextInputV1(window);
25642587
}
25652588

25662589
static void textInputV1Reset(_GLFWwindow* window)
@@ -2586,6 +2609,13 @@ static void textInputV1Leave(void* data,
25862609
_GLFWwindow* window = (_GLFWwindow*) data;
25872610
char* commitText = window->wl.textInputV1Context.commitTextOnReset;
25882611

2612+
if (textInputFocusDisabled(window))
2613+
{
2614+
textInputV1Reset(window);
2615+
deactivateTextInputV1(window);
2616+
return;
2617+
}
2618+
25892619
textInputV3CommitString(data, NULL, commitText);
25902620
textInputV1Reset(window);
25912621
deactivateTextInputV1(window);
@@ -2611,6 +2641,9 @@ static void textInputV1PreeditString(void* data,
26112641
{
26122642
_GLFWwindow* window = (_GLFWwindow*) data;
26132643

2644+
if (textInputFocusDisabled(window))
2645+
return;
2646+
26142647
_glfw_free(window->wl.textInputV1Context.preeditText);
26152648
_glfw_free(window->wl.textInputV1Context.commitTextOnReset);
26162649
window->wl.textInputV1Context.preeditText = strdup(text);
@@ -2637,6 +2670,9 @@ static void textInputV1PreeditCursor(void* data,
26372670
const char* text = window->wl.textInputV1Context.preeditText;
26382671
const char* cur = text;
26392672

2673+
if (textInputFocusDisabled(window))
2674+
return;
2675+
26402676
preedit->caretIndex = 0;
26412677
if (index <= 0 || preedit->textCount == 0)
26422678
return;
@@ -2659,6 +2695,9 @@ static void textInputV1CommitString(void* data,
26592695
{
26602696
_GLFWwindow* window = (_GLFWwindow*) data;
26612697

2698+
if (textInputFocusDisabled(window))
2699+
return;
2700+
26622701
textInputV1Reset(window);
26632702
textInputV3CommitString(data, NULL, text);
26642703
}
@@ -2686,6 +2725,10 @@ static void textInputV1Keysym(void* data,
26862725
uint32_t state,
26872726
uint32_t modifiers)
26882727
{
2728+
_GLFWwindow* window = (_GLFWwindow*) data;
2729+
if (textInputFocusDisabled(window))
2730+
return;
2731+
26892732
uint32_t scancode;
26902733

26912734
// This code supports only weston-keyboard because we aren't aware
@@ -3934,7 +3977,29 @@ void _glfwResetPreeditTextWayland(_GLFWwindow* window)
39343977

39353978
void _glfwSetTextInputFocusWayland(_GLFWwindow* window, GLFWbool focused)
39363979
{
3937-
// TODO: Wire this to text-input-v3 enable/disable or focus integration.
3980+
if (window->wl.textInputV3)
3981+
{
3982+
if (focused)
3983+
zwp_text_input_v3_enable(window->wl.textInputV3);
3984+
else
3985+
{
3986+
zwp_text_input_v3_disable(window->wl.textInputV3);
3987+
textInputV3Reset(window);
3988+
}
3989+
3990+
zwp_text_input_v3_commit(window->wl.textInputV3);
3991+
}
3992+
else if (window->wl.textInputV1)
3993+
{
3994+
if (focused)
3995+
activateTextInputV1(window);
3996+
else
3997+
{
3998+
zwp_text_input_v1_reset(window->wl.textInputV1);
3999+
textInputV1Reset(window);
4000+
deactivateTextInputV1(window);
4001+
}
4002+
}
39384003
}
39394004

39404005
void _glfwSetIMEStatusWayland(_GLFWwindow* window, int active)

0 commit comments

Comments
 (0)