Skip to content

Commit

Permalink
#5 refactor: extract KeyboardGrabber and InputFocusHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
atennert committed Feb 20, 2021
1 parent 74790e2 commit 1eae384
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 39 deletions.
40 changes: 40 additions & 0 deletions src/nativeMain/kotlin/de/atennert/lcarswm/keys/KeyboardGrabber.kt
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()
)
}
}
}
}
45 changes: 6 additions & 39 deletions src/nativeMain/kotlin/de/atennert/lcarswm/lifecycle/startup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import de.atennert.lcarswm.atom.AtomLibrary
import de.atennert.lcarswm.atom.TextAtomReader
import de.atennert.lcarswm.drawing.*
import de.atennert.lcarswm.events.*
import de.atennert.lcarswm.keys.FocusSessionKeyboardGrabber
import de.atennert.lcarswm.keys.KeyConfiguration
import de.atennert.lcarswm.keys.KeyManager
import de.atennert.lcarswm.keys.KeySessionManager
Expand Down Expand Up @@ -181,42 +182,10 @@ fun startup(system: SystemApi, logger: Logger): RuntimeResources? {
windowList
)

val keyboardGrabber = object : 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
system.grabKeyboard(rootWindowPropertyHandler.ewmhSupportWindow, grabTime)
} else {
system.ungrabKeyboard(
if (eventTime.lastEventTime > grabTime)
eventTime.lastEventTime
else
CurrentTime.convert()
)
}
}
}
}
focusHandler.registerObserver(
FocusSessionKeyboardGrabber(system, eventTime, rootWindowPropertyHandler.ewmhSupportWindow))

focusHandler.registerObserver(keyboardGrabber)

focusHandler.registerObserver { activeWindow, _, toggleSessionActive ->
if (!toggleSessionActive) {
if (activeWindow != null) {
logger.logDebug("::startup::set input focus to $activeWindow")
system.setInputFocus(activeWindow, RevertToNone, eventTime.lastEventTime)
} else {
logger.logDebug("::startup::set input focus to root")
system.setInputFocus(screen.root, RevertToPointerRoot, eventTime.lastEventTime)
}
}
}
focusHandler.registerObserver(InputFocusHandler(logger, system, eventTime, screen.root))

focusHandler.registerObserver { activeWindow, oldWindow, _ ->
listOf(oldWindow, activeWindow).forEach {
Expand Down Expand Up @@ -269,10 +238,8 @@ fun startup(system: SystemApi, logger: Logger): RuntimeResources? {
windowRegistration
)

val xEventResources =
XEventResources(eventManager, eventTime, eventBuffer)
val appMenuResources =
AppMenuResources(appMenuMessageHandler, appMenuMessageQueue)
val xEventResources = XEventResources(eventManager, eventTime, eventBuffer)
val appMenuResources = AppMenuResources(appMenuMessageHandler, appMenuMessageQueue)

return RuntimeResources(xEventResources, appMenuResources)
}
Expand Down
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)
}
}
}
}

0 comments on commit 1eae384

Please sign in to comment.