@@ -365,15 +365,14 @@ void CWaylandPlatform::initSeat() {
365365 // keyboard focus follows what the compositor focuses, not where the pointer is. without
366366 // this a freshly opened dialog (e.g. a polkit prompt) gets no keys until the pointer
367367 // enters it, so a programmatically focused field could not be typed into.
368- m_waylandState.keyboard ->setEnter ([this ](CCWlKeyboard* r, uint32_t serial, wl_proxy* surf, wl_array* keys) {
369- if (auto w = windowForSurf (surf); w)
370- m_keyboardWindow = w;
371- });
368+ m_waylandState.keyboard ->setEnter ([this ](CCWlKeyboard* r, uint32_t serial, wl_proxy* surf, wl_array* keys) { onKeyboardEnter (surf, keys); });
372369
373- m_waylandState.keyboard ->setLeave ([this ](CCWlKeyboard* r, uint32_t serial, wl_proxy* surf) { m_keyboardWindow. reset (); });
370+ m_waylandState.keyboard ->setLeave ([this ](CCWlKeyboard* r, uint32_t serial, wl_proxy* surf) { onKeyboardLeave (); });
374371
375- } else if (!HAS_KEYBOARD && m_waylandState.keyboard )
372+ } else if (!HAS_KEYBOARD && m_waylandState.keyboard ) {
373+ onKeyboardLeave ();
376374 m_waylandState.keyboard .reset ();
375+ }
377376
378377 if (HAS_POINTER && !m_waylandState.pointer ) {
379378 m_waylandState.pointer = makeShared<CCWlPointer>(m_waylandState.seat ->sendGetPointer ());
@@ -389,12 +388,8 @@ void CWaylandPlatform::initSeat() {
389388 w->mouseEnter (local);
390389 m_currentWindow = w;
391390 m_lastEnterSerial = serial;
392- m_currentMods = 0 ;
393391
394392 setCursor (HT_POINTER_ARROW );
395-
396- m_waylandState.seatState .pressedKeys .clear ();
397- stopRepeatTimer ();
398393 });
399394
400395 m_waylandState.pointer ->setLeave ([this ](CCWlPointer* r, uint32_t serial, wl_proxy* surf) {
@@ -405,10 +400,6 @@ void CWaylandPlatform::initSeat() {
405400
406401 w->mouseLeave ();
407402 m_currentWindow.reset ();
408- m_currentMods = 0 ;
409-
410- m_waylandState.seatState .pressedKeys .clear ();
411- stopRepeatTimer ();
412403 });
413404
414405 m_waylandState.pointer ->setMotion ([this ](CCWlPointer* r, uint32_t time, wl_fixed_t x, wl_fixed_t y) {
@@ -730,6 +721,37 @@ void CWaylandPlatform::onKey(uint32_t keycode, bool state) {
730721 stopRepeatTimer ();
731722}
732723
724+ void CWaylandPlatform::onKeyboardEnter (wl_proxy* surf, wl_array* keys) {
725+ resetKeyboardState ();
726+ m_keyboardWindow = windowForSurf (surf);
727+
728+ if (!keys || !keys->data )
729+ return ;
730+
731+ const auto KEY_COUNT = keys->size / sizeof (uint32_t );
732+ const auto KEYS = sc<const uint32_t *>(keys->data );
733+ m_waylandState.seatState .pressedKeys .assign (KEYS , KEYS + KEY_COUNT );
734+ }
735+
736+ void CWaylandPlatform::onKeyboardLeave () {
737+ m_keyboardWindow.reset ();
738+ resetKeyboardState ();
739+ }
740+
741+ void CWaylandPlatform::resetKeyboardState () {
742+ stopRepeatTimer ();
743+
744+ m_waylandState.seatState .pressedKeys .clear ();
745+ m_waylandState.seatState .repeatKeyEvent = {.down = false };
746+ m_waylandState.seatState .currentLayer = 0 ;
747+ m_currentMods = 0 ;
748+
749+ if (m_waylandState.seatState .xkbState )
750+ xkb_state_update_mask (m_waylandState.seatState .xkbState , 0 , 0 , 0 , 0 , 0 , 0 );
751+ if (m_waylandState.seatState .xkbComposeState )
752+ xkb_compose_state_reset (m_waylandState.seatState .xkbComposeState );
753+ }
754+
733755void CWaylandPlatform::onRepeatTimerFire () {
734756 if (!m_keyboardWindow)
735757 return ;
0 commit comments