-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5 refactor: extract KeyboardGrabber and InputFocusHandler
- Loading branch information
Showing
3 changed files
with
76 additions
and
39 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/nativeMain/kotlin/de/atennert/lcarswm/keys/KeyboardGrabber.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.atennert.lcarswm.keys | ||
|
||
import de.atennert.lcarswm.events.EventTime | ||
import de.atennert.lcarswm.system.api.InputApi | ||
import de.atennert.lcarswm.window.FocusObserver | ||
import kotlinx.cinterop.convert | ||
import xlib.CurrentTime | ||
import xlib.Time | ||
import xlib.Window | ||
|
||
/** | ||
* Used to grab the key during a focus toggle session | ||
* so we also get key release events for modifier keys. | ||
*/ | ||
class FocusSessionKeyboardGrabber( | ||
private val inputApi: InputApi, | ||
private val eventTime: EventTime, | ||
private val ewmhSupportWindow: Window | ||
) : FocusObserver { | ||
private var grabActive = false | ||
private var grabTime: Time = CurrentTime.convert() | ||
|
||
override fun invoke(activeWindow: Window?, oldWindow: Window?, toggleSessionActive: Boolean) { | ||
if (toggleSessionActive != grabActive) { | ||
grabActive = toggleSessionActive | ||
|
||
if (toggleSessionActive) { | ||
grabTime = eventTime.lastEventTime | ||
inputApi.grabKeyboard(ewmhSupportWindow, grabTime) | ||
} else { | ||
inputApi.ungrabKeyboard( | ||
if (eventTime.lastEventTime > grabTime) | ||
eventTime.lastEventTime | ||
else | ||
CurrentTime.convert() | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/nativeMain/kotlin/de/atennert/lcarswm/window/InputFocusHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.atennert.lcarswm.window | ||
|
||
import de.atennert.lcarswm.events.EventTime | ||
import de.atennert.lcarswm.log.Logger | ||
import de.atennert.lcarswm.system.api.InputApi | ||
import xlib.RevertToNone | ||
import xlib.RevertToPointerRoot | ||
import xlib.Window | ||
|
||
/** | ||
* Sets the input focus based on the current window focus. | ||
*/ | ||
class InputFocusHandler( | ||
private val logger: Logger, | ||
private val inputApi: InputApi, | ||
private val eventTime: EventTime, | ||
private val rootWindow: Window | ||
) : FocusObserver { | ||
override fun invoke(activeWindow: Window?, oldWindow: Window?, toggleSessionActive: Boolean) { | ||
if (!toggleSessionActive) { | ||
if (activeWindow != null) { | ||
logger.logDebug("::startup::set input focus to $activeWindow") | ||
inputApi.setInputFocus(activeWindow, RevertToNone, eventTime.lastEventTime) | ||
} else { | ||
logger.logDebug("::startup::set input focus to root") | ||
inputApi.setInputFocus(rootWindow, RevertToPointerRoot, eventTime.lastEventTime) | ||
} | ||
} | ||
} | ||
} |