Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class PreferenceManager(context: Context) {
const val PREFERENCE_KEY_PAUSE_TIMEOUT = "pause_timeout"
const val PREFERENCE_KEY_MOVE_REPEAT = "move_repeat"
const val PREFERENCE_KEY_MOVE_REPEAT_DELAY = "move_repeat_delay"
const val PREFERENCE_KEY_PC_MOUSE_MOVEMENT_SIZE = "pc_mouse_movement_size"
const val PREFERENCE_KEY_PC_CONTROL_SURFACE = "pc_control_surface"
const val PREFERENCE_KEY_PC_MOUSE_REPEAT = "pc_mouse_repeat"
const val PREFERENCE_KEY_PC_MOUSE_REPEAT_INTERVAL = "pc_mouse_repeat_interval"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
Expand All @@ -44,7 +45,8 @@ fun <T> Picker(
items: List<T>,
onItemSelected: (T) -> Unit,
itemToString: (T) -> String,
itemDescription: (T) -> String
itemDescription: (T) -> String,
enabled: Boolean = true
) {
var expanded by remember { mutableStateOf(false) }

Expand All @@ -69,10 +71,12 @@ fun <T> Picker(
.heightIn(min = 56.dp)
.background(triggerColor)
.clickable(
enabled = enabled,
interactionSource = interactionSource,
indication = null,
onClick = { expanded = true }
)
.alpha(if (enabled) 1f else 0.55f)
.padding(Dimens.spaceM),
horizontalArrangement = Arrangement.spacedBy(Dimens.spaceM),
verticalAlignment = Alignment.CenterVertically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sealed class PcControlCommand {
data class ModifierDown(val key: PcKeyboardModifierKey) : PcControlCommand()
data class ModifierUp(val key: PcKeyboardModifierKey) : PcControlCommand()
data class WindowControl(val action: PcWindowControlAction) : PcControlCommand()
data class SetPointerSpeed(val scalePercent: Double) : PcControlCommand()
data object LeftClick : PcControlCommand()
data object DoubleClick : PcControlCommand()
data object RightClick : PcControlCommand()
Expand Down
20 changes: 19 additions & 1 deletion app/src/main/java/com/enaboapps/switchify/pc/PcPointerProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ data class PcPointerCapabilities(
val noAckMouseMove: Boolean = false,
val noAckCommands: Set<String> = emptySet(),
val supportedCommands: Set<String> = emptySet(),
val mouseRepeat: PcMouseRepeatCapabilities = PcMouseRepeatCapabilities()
val mouseRepeat: PcMouseRepeatCapabilities = PcMouseRepeatCapabilities(),
val pointerSpeed: PcPointerSpeedCapabilities = PcPointerSpeedCapabilities()
)

data class PcPointerSpeedCapabilities(
val supported: Boolean = false,
val setSupported: Boolean = false,
val scalePercent: Double = 100.0,
val minScalePercent: Double = 5.0,
val maxScalePercent: Double = 225.0,
val stepPercent: Double = 5.0,
val baseMoveDelta: Int = 128,
val effectiveMoveDelta: Int = 128
)

data class PcMouseRepeatCapabilities(
Expand Down Expand Up @@ -56,3 +68,9 @@ fun PcPointerMovementProfile.supportsModifierToggle(): Boolean {
)
)
}

fun PcPointerMovementProfile.pointerMoveStep(): Int {
val speed = capabilities.pointerSpeed
val candidate = if (speed.supported) speed.baseMoveDelta else recommendedDeltas.medium
return candidate.coerceIn(1, maxDelta)
}
71 changes: 70 additions & 1 deletion app/src/main/java/com/enaboapps/switchify/pc/PcProtocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ object PcProtocol {
)
}

fun pointerSpeedSet(
id: String,
deviceId: String,
token: String,
timestamp: Long,
scalePercent: Double
): String {
return authenticatedCommand(
id = id,
deviceId = deviceId,
token = token,
timestamp = timestamp,
type = "pointer.speed.set",
payload = JSONObject().put("scalePercent", jsonNumber(scalePercent))
)
}

fun authenticatedCommand(
id: String,
deviceId: String,
Expand All @@ -114,6 +131,10 @@ object PcProtocol {
return message.toString()
}

private fun jsonNumber(value: Double): Number {
return if (value.isFinite() && value % 1.0 == 0.0) value.toInt() else value
}

fun mouseMove(
id: String,
deviceId: String,
Expand Down Expand Up @@ -568,7 +589,8 @@ object PcProtocol {
noAckMouseMove = capabilitiesJson?.optBoolean("noAckMouseMove", false) ?: false,
noAckCommands = noAckCommands,
supportedCommands = supportedCommands,
mouseRepeat = parseMouseRepeatCapabilities(capabilitiesJson) ?: return PcProtocolResponse.Invalid
mouseRepeat = parseMouseRepeatCapabilities(capabilitiesJson) ?: return PcProtocolResponse.Invalid,
pointerSpeed = parsePointerSpeedCapabilities(capabilitiesJson) ?: return PcProtocolResponse.Invalid
)
val bounds = PcPointerBounds(
x = boundsJson.optInt("x"),
Expand Down Expand Up @@ -626,6 +648,52 @@ object PcProtocol {
)
}

private fun parsePointerSpeedCapabilities(capabilitiesJson: JSONObject?): PcPointerSpeedCapabilities? {
val speedJson = capabilitiesJson?.opt("pointerSpeed") ?: return PcPointerSpeedCapabilities()
if (speedJson !is JSONObject) return null
if (speedJson.has("supported") && speedJson.opt("supported") !is Boolean) return null
if (speedJson.has("setSupported") && speedJson.opt("setSupported") !is Boolean) return null

val scalePercent = speedJson.optDouble("scalePercent", 100.0)
val minScalePercent = speedJson.optDouble("minScalePercent", 5.0)
val maxScalePercent = speedJson.optDouble("maxScalePercent", 225.0)
val stepPercent = speedJson.optDouble("stepPercent", 5.0)
val baseMoveDeltaValue = speedJson.opt("baseMoveDelta")
val effectiveMoveDeltaValue = speedJson.opt("effectiveMoveDelta")
if (baseMoveDeltaValue != null && baseMoveDeltaValue !is Number) return null
if (effectiveMoveDeltaValue != null && effectiveMoveDeltaValue !is Number) return null
val baseMoveDelta = baseMoveDeltaValue?.toInt() ?: 128
val effectiveMoveDelta = effectiveMoveDeltaValue?.toInt() ?: baseMoveDelta

if (
!scalePercent.isFinite() ||
!minScalePercent.isFinite() ||
!maxScalePercent.isFinite() ||
!stepPercent.isFinite() ||
scalePercent <= 0.0 ||
minScalePercent <= 0.0 ||
maxScalePercent < minScalePercent ||
scalePercent < minScalePercent ||
scalePercent > maxScalePercent ||
stepPercent <= 0.0 ||
baseMoveDelta <= 0 ||
effectiveMoveDelta <= 0
) {
return null
}

return PcPointerSpeedCapabilities(
supported = speedJson.optBoolean("supported", false),
setSupported = speedJson.optBoolean("setSupported", false),
scalePercent = scalePercent,
minScalePercent = minScalePercent,
maxScalePercent = maxScalePercent,
stepPercent = stepPercent,
baseMoveDelta = baseMoveDelta,
effectiveMoveDelta = effectiveMoveDelta
)
}

private fun parseNoAckCommands(capabilitiesJson: JSONObject?): Set<String>? {
if (capabilitiesJson == null || !capabilitiesJson.has("noAckCommands")) return emptySet()
val commandsJson = capabilitiesJson.opt("noAckCommands")
Expand Down Expand Up @@ -681,6 +749,7 @@ object PcProtocol {
"connection.ping",
"connection.disconnecting",
"pointer.profile",
"pointer.speed.set",
"mouse.repeat.start",
"mouse.repeat.stop",
"keyboard.textStream.open",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class SwitchifyPcBleClient(
is PcControlCommand.ModifierDown -> PcProtocol.keyboardModifierDown(id, deviceId, token, timestamp, key, responseMode)
is PcControlCommand.ModifierUp -> PcProtocol.keyboardModifierUp(id, deviceId, token, timestamp, key, responseMode)
is PcControlCommand.WindowControl -> PcProtocol.windowControl(id, deviceId, token, timestamp, action, responseMode)
is PcControlCommand.SetPointerSpeed -> PcProtocol.pointerSpeedSet(id, deviceId, token, timestamp, scalePercent)
}
}

Expand Down Expand Up @@ -424,6 +425,7 @@ private fun PcControlCommand.protocolType(): String {
is PcControlCommand.ModifierDown -> "keyboard.modifierDown"
is PcControlCommand.ModifierUp -> "keyboard.modifierUp"
is PcControlCommand.WindowControl -> "window.control"
is PcControlCommand.SetPointerSpeed -> "pointer.speed.set"
}
}

Expand Down
Loading