mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-21 09:31:20 -08:00
Driver status and enable
This commit is contained in:
1 parent
aa3ab3942a
commit
bbcfc99061
18 files changed
+317
-81
No files matched your search
@@ -530,6 +530,19 @@ settings-routing-hands-warning-done = Route hands
|
||||
|
||||
settings-driver = SteamVR / Monado
|
||||
settings-driver-description = Settings for the SlimeVR driver, used by SteamVR and Monado alike.
|
||||
settings-driver-enable = Enable
|
||||
settings-driver-enable-description = Send your trackers to SteamVR or Monado through the SlimeVR driver. While this is off, they cannot connect to SlimeVR at all.
|
||||
settings-driver-enable-label = Enable SteamVR / Monado
|
||||
settings-driver-status-title = Status
|
||||
settings-driver-status-connection = Driver connection
|
||||
settings-driver-status-badge-connected = Connected
|
||||
settings-driver-status-badge-waiting = Waiting
|
||||
settings-driver-status-badge-disabled = Off
|
||||
settings-driver-status-badge-unavailable = Unavailable
|
||||
settings-driver-status-connection-connected = A driver is connected and receiving your trackers.
|
||||
settings-driver-status-connection-waiting = Waiting for SteamVR or Monado to connect through the SlimeVR driver.
|
||||
settings-driver-status-connection-disabled = Turned off, so nothing can connect.
|
||||
settings-driver-status-connection-unavailable = This output does not exist on this platform.
|
||||
settings-driver-bones = Bones sent
|
||||
settings-driver-bones-description = Which bones this output sends is configured in Bone Routing.
|
||||
settings-driver-bones-link = Open Bone Routing
|
||||
|
||||
@@ -196,9 +196,9 @@ function OutputStatusRow({
|
||||
case RoutingOutputState.UNSUPPORTED:
|
||||
return 'unavailable';
|
||||
case RoutingOutputState.INACTIVE:
|
||||
return status.output === RoutingOutput.DRIVER ? 'stopped' : 'off';
|
||||
return 'off';
|
||||
case RoutingOutputState.ENABLED:
|
||||
return 'idle';
|
||||
return status.output === RoutingOutput.DRIVER ? 'stopped' : 'idle';
|
||||
default:
|
||||
return routed === 0 ? 'empty' : 'sending';
|
||||
}
|
||||
|
||||
@@ -4,12 +4,20 @@ import { DefaultValues, useForm } from 'react-hook-form';
|
||||
import { Button } from '@/components/commons/Button';
|
||||
import {
|
||||
ChangeDriverSettingsRequestT,
|
||||
DriverConnectionState,
|
||||
DriverSettingsRequestT,
|
||||
DriverSettingsResponseT,
|
||||
DriverStatusChangeResponseT,
|
||||
DriverStatusRequestT,
|
||||
RpcMessage,
|
||||
} from 'solarxr-protocol';
|
||||
import { useWebsocketAPI } from '@/hooks/websocket-api';
|
||||
import { CheckBox } from '@/components/commons/Checkbox';
|
||||
import {
|
||||
StatusBadge,
|
||||
StatusRow,
|
||||
type StatusVariant,
|
||||
} from '@/components/commons/StatusBadge';
|
||||
import { Typography } from '@/components/commons/Typography';
|
||||
import {
|
||||
SettingsPageLayout,
|
||||
@@ -18,15 +26,67 @@ import {
|
||||
import { SteamIcon } from '@/components/commons/icon/SteamIcon';
|
||||
|
||||
type DriverForm = {
|
||||
enabled: boolean;
|
||||
sendDerivedVelocity: boolean;
|
||||
};
|
||||
|
||||
const defaultValues: DriverForm = {
|
||||
enabled: true,
|
||||
sendDerivedVelocity: false,
|
||||
};
|
||||
|
||||
type Badge = 'connected' | 'waiting' | 'disabled' | 'unavailable';
|
||||
|
||||
const BADGE_VARIANTS: Record<Badge, StatusVariant> = {
|
||||
connected: 'success',
|
||||
waiting: 'special',
|
||||
disabled: 'neutral',
|
||||
unavailable: 'neutral',
|
||||
};
|
||||
|
||||
const CONNECTION_BADGES: Record<DriverConnectionState, Badge> = {
|
||||
[DriverConnectionState.UNSUPPORTED]: 'unavailable',
|
||||
[DriverConnectionState.DISABLED]: 'disabled',
|
||||
[DriverConnectionState.WAITING]: 'waiting',
|
||||
[DriverConnectionState.CONNECTED]: 'connected',
|
||||
};
|
||||
|
||||
function StatusCard({ status }: { status: DriverStatusChangeResponseT }) {
|
||||
const state = status.state ?? DriverConnectionState.UNSUPPORTED;
|
||||
const badge = CONNECTION_BADGES[state];
|
||||
|
||||
return (
|
||||
<div className="flex flex-col bg-background-80 px-4 py-2 mb-5 rounded-md divide-y divide-background-60">
|
||||
<StatusRow
|
||||
label={
|
||||
<Typography
|
||||
variant="section-title"
|
||||
id="settings-driver-status-connection"
|
||||
/>
|
||||
}
|
||||
badge={
|
||||
<StatusBadge
|
||||
variant={BADGE_VARIANTS[badge]}
|
||||
id={`settings-driver-status-badge-${badge}`}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<Typography
|
||||
color="secondary"
|
||||
id={`settings-driver-status-connection-${badge}`}
|
||||
/>
|
||||
</StatusRow>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function DriverSettings() {
|
||||
const [settings, setSettings] = useState(new DriverSettingsResponseT());
|
||||
const [settings, setSettings] = useState<DriverSettingsResponseT | null>(
|
||||
null
|
||||
);
|
||||
const [status, setStatus] = useState<DriverStatusChangeResponseT | null>(
|
||||
null
|
||||
);
|
||||
const { l10n } = useLocalization();
|
||||
const { sendRPCPacket, useRPCPacket } = useWebsocketAPI();
|
||||
|
||||
@@ -39,10 +99,13 @@ export function DriverSettings() {
|
||||
|
||||
const onSubmit = (values: DriverForm) => {
|
||||
const settingsReq = new ChangeDriverSettingsRequestT();
|
||||
settingsReq.enabled = values.enabled;
|
||||
settingsReq.sendDerivedVelocity = values.sendDerivedVelocity;
|
||||
sendRPCPacket(RpcMessage.ChangeDriverSettingsRequest, settingsReq);
|
||||
};
|
||||
|
||||
const enabled = watch('enabled');
|
||||
|
||||
useEffect(() => {
|
||||
const subscription = watch((_, { type }) => {
|
||||
if (type === 'change') handleSubmit(onSubmit)();
|
||||
@@ -55,10 +118,13 @@ export function DriverSettings() {
|
||||
RpcMessage.DriverSettingsRequest,
|
||||
new DriverSettingsRequestT()
|
||||
);
|
||||
sendRPCPacket(RpcMessage.DriverStatusRequest, new DriverStatusRequestT());
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!settings) return;
|
||||
const formData: DefaultValues<DriverForm> = {
|
||||
enabled: settings.enabled,
|
||||
sendDerivedVelocity: settings.sendDerivedVelocity,
|
||||
};
|
||||
reset({ ...getValues(), ...formData });
|
||||
@@ -69,6 +135,11 @@ export function DriverSettings() {
|
||||
(res: DriverSettingsResponseT) => setSettings(res)
|
||||
);
|
||||
|
||||
useRPCPacket(
|
||||
RpcMessage.DriverStatusChangeResponse,
|
||||
(res: DriverStatusChangeResponseT) => setStatus(res)
|
||||
);
|
||||
|
||||
return (
|
||||
<SettingsPageLayout>
|
||||
<SettingsPagePaneLayout icon={<SteamIcon size={24} />} id="driver">
|
||||
@@ -78,6 +149,30 @@ export function DriverSettings() {
|
||||
<Typography id="settings-driver-description" />
|
||||
</div>
|
||||
|
||||
<Typography variant="section-title" id="settings-driver-enable" />
|
||||
<div className="flex flex-col pt-1 pb-2">
|
||||
<Typography id="settings-driver-enable-description" />
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-3 pb-5">
|
||||
<CheckBox
|
||||
variant="toggle"
|
||||
outlined
|
||||
control={control}
|
||||
name="enabled"
|
||||
label={l10n.getString('settings-driver-enable-label')}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{enabled && status && (
|
||||
<>
|
||||
<Typography
|
||||
variant="section-title"
|
||||
id="settings-driver-status-title"
|
||||
/>
|
||||
<StatusCard status={status} />
|
||||
</>
|
||||
)}
|
||||
|
||||
<Typography variant="section-title" id="settings-driver-bones" />
|
||||
<div className="flex flex-col gap-2 pt-1 pb-4">
|
||||
<Typography id="settings-driver-bones-description" />
|
||||
|
||||
@@ -56,6 +56,7 @@ data class BoneRoutingConfig(
|
||||
|
||||
@Serializable
|
||||
data class DriverConfig(
|
||||
val enabled: Boolean = true,
|
||||
val sendDerivedVelocity: Boolean = false, // TODO do we actually need that or can we disable OpenVR's prediction
|
||||
)
|
||||
|
||||
|
||||
@@ -41,8 +41,14 @@ sealed interface DriverBridgeOutbound {
|
||||
typealias DriverBridgeContext = Context<DriverBridgeState, DriverBridgeActions>
|
||||
typealias DriverBridgeBehaviour = Behaviour<DriverBridgeState, DriverBridgeActions, DriverBridge>
|
||||
|
||||
enum class DriverBridgeSource {
|
||||
DRIVER,
|
||||
FEEDER,
|
||||
}
|
||||
|
||||
class DriverBridge(
|
||||
val id: Int,
|
||||
val source: DriverBridgeSource,
|
||||
val context: DriverBridgeContext,
|
||||
val appContext: AppContextProvider,
|
||||
val inbound: EventDispatcher<DriverBridgeInbound> = EventDispatcher("Driver.inbound", context.scope, capacity = 128),
|
||||
@@ -62,7 +68,7 @@ class DriverBridge(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create(id: Int, appContext: AppContextProvider, scope: CoroutineScope): DriverBridge {
|
||||
fun create(id: Int, source: DriverBridgeSource, appContext: AppContextProvider, scope: CoroutineScope): DriverBridge {
|
||||
val behaviours = listOf(DriverOutgoingTrackersBehaviour(), DriverIncomingTrackersBehaviour())
|
||||
|
||||
val managedContext = ManagedContext.create(
|
||||
@@ -75,7 +81,7 @@ class DriverBridge(
|
||||
name = "Driver[$id]",
|
||||
)
|
||||
|
||||
val bridge = DriverBridge(id = id, context = managedContext.context, appContext = appContext, managedContext = managedContext)
|
||||
val bridge = DriverBridge(id = id, source = source, context = managedContext.context, appContext = appContext, managedContext = managedContext)
|
||||
bridge.startObserving()
|
||||
appContext.server.context.dispatch(VRServerActions.DriverConnected(bridge))
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.config.BoneRoutingConfig
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.driver.DRIVER_SUPPORTED_BONES
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.vmc.VMC_SUPPORTED_BONES
|
||||
import dev.slimevr.vrcosc.VRC_OSC_SUPPORTED_BONES
|
||||
@@ -55,44 +56,46 @@ fun overrideRoutes(config: BoneRoutingConfig): Routes = config.manualRoutes.orEm
|
||||
|
||||
fun isActive(states: OutputStates, output: RoutingOutput): Boolean = states[output] == RoutingOutputState.ACTIVE
|
||||
|
||||
fun platformSupports(appContext: AppContextProvider, output: RoutingOutput): Boolean = when (output) {
|
||||
RoutingOutput.DRIVER -> appContext.featureFlags.supportsDriver
|
||||
RoutingOutput.VRC_OSC, RoutingOutput.VMC -> true
|
||||
}
|
||||
fun driverStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = combine(
|
||||
appContext.config.settings.context.state.map { it.data.driverConfig.enabled },
|
||||
appContext.server.context.state.map { state ->
|
||||
state.drivers.values.any { it.source == DriverBridgeSource.DRIVER }
|
||||
},
|
||||
) { enabled, connected ->
|
||||
when {
|
||||
!appContext.featureFlags.supportsDriver -> RoutingOutputState.UNSUPPORTED
|
||||
!enabled -> RoutingOutputState.INACTIVE
|
||||
connected -> RoutingOutputState.ACTIVE
|
||||
else -> RoutingOutputState.ENABLED
|
||||
}
|
||||
}.distinctUntilChanged()
|
||||
|
||||
private fun vrcOscStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = combine(
|
||||
appContext.config.settings.context.state.map { it.data.vrcOscConfig.enabled },
|
||||
appContext.vrcOscManager.context.state.map { it.status.outputState == VRCOSCOutputState.READY },
|
||||
) { enabled, hasTarget ->
|
||||
when {
|
||||
!enabled -> RoutingOutputState.INACTIVE
|
||||
hasTarget -> RoutingOutputState.ACTIVE
|
||||
else -> RoutingOutputState.ENABLED
|
||||
}
|
||||
}.distinctUntilChanged()
|
||||
|
||||
private fun vmcStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = appContext.config.settings.context.state
|
||||
.map { if (it.data.vmcConfig.enabled) RoutingOutputState.ACTIVE else RoutingOutputState.INACTIVE }
|
||||
.distinctUntilChanged()
|
||||
|
||||
/** Emits the output states as the driver connects and the output configs change. */
|
||||
fun outputStatesFlow(appContext: AppContextProvider): Flow<OutputStates> {
|
||||
val settings = appContext.config.settings
|
||||
|
||||
return combine(
|
||||
appContext.server.context.state.map { it.drivers.isNotEmpty() }.distinctUntilChanged(),
|
||||
settings.context.state.map { it.data.vrcOscConfig.enabled }.distinctUntilChanged(),
|
||||
settings.context.state.map { it.data.vmcConfig.enabled }.distinctUntilChanged(),
|
||||
appContext.vrcOscManager.context.state
|
||||
.map { it.status.outputState == VRCOSCOutputState.READY }
|
||||
.distinctUntilChanged(),
|
||||
) { driverConnected, vrcOscEnabled, vmcEnabled, vrcOscHasTarget ->
|
||||
RoutingOutput.entries.associateWith { output ->
|
||||
if (!platformSupports(appContext, output)) {
|
||||
RoutingOutputState.UNSUPPORTED
|
||||
} else {
|
||||
when (output) {
|
||||
RoutingOutput.DRIVER -> if (driverConnected) RoutingOutputState.ACTIVE else RoutingOutputState.INACTIVE
|
||||
|
||||
RoutingOutput.VRC_OSC -> when {
|
||||
!vrcOscEnabled -> RoutingOutputState.INACTIVE
|
||||
vrcOscHasTarget -> RoutingOutputState.ACTIVE
|
||||
else -> RoutingOutputState.ENABLED
|
||||
}
|
||||
|
||||
// Writes to a fixed address whether or not anything is there, so being
|
||||
// on is the same as transmitting.
|
||||
// FIXME: Maybe we should assume that we need to receive data from VMC for it to be considered working?
|
||||
RoutingOutput.VMC -> if (vmcEnabled) RoutingOutputState.ACTIVE else RoutingOutputState.INACTIVE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fun outputStatesFlow(appContext: AppContextProvider): Flow<OutputStates> = combine(
|
||||
driverStateFlow(appContext),
|
||||
vrcOscStateFlow(appContext),
|
||||
vmcStateFlow(appContext),
|
||||
) { driver, vrcOsc, vmc ->
|
||||
mapOf(
|
||||
RoutingOutput.DRIVER to driver,
|
||||
RoutingOutput.VRC_OSC to vrcOsc,
|
||||
RoutingOutput.VMC to vmc,
|
||||
)
|
||||
}
|
||||
|
||||
/** What the user asked for, including outputs that are switched off. */
|
||||
|
||||
@@ -1,28 +1,47 @@
|
||||
package dev.slimevr.solarxr
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.config.SettingsActions
|
||||
import dev.slimevr.routing.driverStateFlow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import solarxr_protocol.rpc.ChangeDriverSettingsRequest
|
||||
import solarxr_protocol.rpc.DriverConnectionState
|
||||
import solarxr_protocol.rpc.DriverSettingsRequest
|
||||
import solarxr_protocol.rpc.DriverSettingsResponse
|
||||
import solarxr_protocol.rpc.DriverStatusChangeResponse
|
||||
import solarxr_protocol.rpc.DriverStatusRequest
|
||||
import solarxr_protocol.rpc.RoutingOutputState
|
||||
|
||||
class DriverSettingsBehaviour(
|
||||
private val settings: Settings,
|
||||
private val appContext: AppContextProvider,
|
||||
) : SolarXRBridgeBehaviour {
|
||||
private val settings: Settings get() = appContext.config.settings
|
||||
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
receiver.rpcDispatcher.on<DriverSettingsRequest> {
|
||||
val config = settings.context.state.value.data.driverConfig
|
||||
receiver.sendRpc(DriverSettingsResponse(sendDerivedVelocity = config.sendDerivedVelocity))
|
||||
receiver.sendRpc(
|
||||
DriverSettingsResponse(
|
||||
sendDerivedVelocity = config.sendDerivedVelocity,
|
||||
enabled = config.enabled,
|
||||
),
|
||||
)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
settings.context.state
|
||||
.drop(1)
|
||||
.map { DriverSettingsResponse(sendDerivedVelocity = it.data.driverConfig.sendDerivedVelocity) }
|
||||
.map {
|
||||
DriverSettingsResponse(
|
||||
sendDerivedVelocity = it.data.driverConfig.sendDerivedVelocity,
|
||||
enabled = it.data.driverConfig.enabled,
|
||||
)
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.onEach(receiver::sendRpc)
|
||||
.launchIn(receiver.context.scope)
|
||||
@@ -30,9 +49,32 @@ class DriverSettingsBehaviour(
|
||||
receiver.rpcDispatcher.on<ChangeDriverSettingsRequest> { req ->
|
||||
settings.context.dispatch(
|
||||
SettingsActions.Update {
|
||||
copy(driverConfig = driverConfig.copy(sendDerivedVelocity = req.sendDerivedVelocity == true))
|
||||
copy(
|
||||
driverConfig = driverConfig.copy(
|
||||
sendDerivedVelocity = req.sendDerivedVelocity == true,
|
||||
enabled = req.enabled == true,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
val status = driverStateFlow(appContext).map { state ->
|
||||
when (state) {
|
||||
RoutingOutputState.UNSUPPORTED -> DriverConnectionState.UNSUPPORTED
|
||||
RoutingOutputState.INACTIVE -> DriverConnectionState.DISABLED
|
||||
RoutingOutputState.ENABLED -> DriverConnectionState.WAITING
|
||||
RoutingOutputState.ACTIVE -> DriverConnectionState.CONNECTED
|
||||
}
|
||||
}
|
||||
|
||||
receiver.rpcDispatcher.on<DriverStatusRequest> {
|
||||
receiver.sendRpc(DriverStatusChangeResponse(state = status.first()))
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
status
|
||||
.drop(1)
|
||||
.onEach { receiver.sendRpc(DriverStatusChangeResponse(state = it)) }
|
||||
.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
@@ -78,7 +78,7 @@ class SolarXRBridge(
|
||||
add(HeightCalibrationBehaviour(appContext.heightCalibrationManager))
|
||||
add(ProvisioningBehaviour(appContext.server, appContext.provisioningManager))
|
||||
add(BoneRoutingBehaviour(appContext))
|
||||
add(DriverSettingsBehaviour(appContext.config.settings))
|
||||
add(DriverSettingsBehaviour(appContext))
|
||||
add(SkeletonSettingsBehaviour(appContext.config.settings))
|
||||
add(SkeletonProportionsBehaviour(appContext.config.userConfig, appContext.skeleton))
|
||||
add(TrackingChecklistBehaviour(appContext.trackingChecklist, appContext.config.settings))
|
||||
|
||||
@@ -8,6 +8,7 @@ import dev.slimevr.config.MountingMethods
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.device.DeviceState
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.networkprofile.NetworkProfileManager
|
||||
import dev.slimevr.resets.ResetBodyParts
|
||||
import dev.slimevr.resets.ResetsManager
|
||||
@@ -180,7 +181,9 @@ class SteamVRHandsCheckBehaviour(
|
||||
combine(
|
||||
trackerStatesFlow(server),
|
||||
boneRouting.context.state.map { state -> state.routes },
|
||||
server.context.state.map { state -> state.drivers.isNotEmpty() }.distinctUntilChanged(),
|
||||
server.context.state
|
||||
.map { state -> state.drivers.values.any { it.source == DriverBridgeSource.DRIVER } }
|
||||
.distinctUntilChanged(),
|
||||
::computeStep,
|
||||
)
|
||||
.distinctUntilChanged()
|
||||
|
||||
@@ -17,6 +17,7 @@ import dev.slimevr.context.Context
|
||||
import dev.slimevr.device.DeviceOrigin
|
||||
import dev.slimevr.driver.DriverBridge
|
||||
import dev.slimevr.driver.DriverBridgeActions
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.driver.DriverBridgeState
|
||||
import dev.slimevr.firmware.FirmwareManager
|
||||
import dev.slimevr.heightcalibration.HeightCalibrationActions
|
||||
@@ -200,7 +201,7 @@ fun buildTestDriverBridge(server: VRServer, appContext: AppContextProvider, id:
|
||||
behaviours = emptyList(),
|
||||
name = "TestDriver[$id]",
|
||||
)
|
||||
return DriverBridge(id = id, context = context, appContext = appContext)
|
||||
return DriverBridge(id = id, source = DriverBridgeSource.DRIVER, context = context, appContext = appContext)
|
||||
}
|
||||
|
||||
fun buildTestHeightCalibration(server: VRServer, userConfig: UserConfig, scope: CoroutineScope): HeightCalibrationManager {
|
||||
|
||||
@@ -114,7 +114,7 @@ fun main(args: Array<String>) = runBlocking<Unit>(appCoroutineExceptionHandler +
|
||||
val heightCalibrationManager = HeightCalibrationManager.create(ctx = phase1, scope = this)
|
||||
val trackingChecklist = TrackingChecklist.create(scope = this, extraBehaviours = { appContext ->
|
||||
buildList {
|
||||
add(SteamVRCheckBehaviour(appContext.server))
|
||||
add(SteamVRCheckBehaviour(appContext.server, appContext.config.settings))
|
||||
}
|
||||
})
|
||||
val udpServer = UdpServer.create(scope = this, addressResolver = ::resolveDesktopUdpAddress)
|
||||
|
||||
@@ -4,6 +4,9 @@ import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.CURRENT_PLATFORM
|
||||
import dev.slimevr.Platform
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
const val DRIVER_SOCKET_NAME = "SlimeVRDriver"
|
||||
@@ -15,25 +18,38 @@ const val FEEDER_PIPE = "\\\\.\\pipe\\SlimeVRInput"
|
||||
const val SOLARXR_PIPE = "\\\\.\\pipe\\SlimeVRRpc"
|
||||
|
||||
suspend fun createIpcServers(appContext: AppContextProvider) = coroutineScope {
|
||||
val driver = appContext.featureFlags.supportsDriver
|
||||
if (appContext.featureFlags.supportsDriver) {
|
||||
launch { createDriverServers(appContext) }
|
||||
}
|
||||
|
||||
when (CURRENT_PLATFORM) {
|
||||
Platform.LINUX, Platform.OSX -> {
|
||||
if (driver) {
|
||||
launch { createUnixDriverSocket(appContext) }
|
||||
launch { createUnixFeederSocket(appContext) }
|
||||
}
|
||||
launch { createUnixSolarXRSocket(appContext) }
|
||||
}
|
||||
|
||||
Platform.WINDOWS -> {
|
||||
if (driver) {
|
||||
launch { createWindowsDriverPipe(appContext) }
|
||||
launch { createWindowsFeederPipe(appContext) }
|
||||
}
|
||||
launch { createWindowsSolarXRPipe(appContext) }
|
||||
}
|
||||
|
||||
Platform.LINUX, Platform.OSX -> launch { createUnixSolarXRSocket(appContext) }
|
||||
Platform.WINDOWS -> launch { createWindowsSolarXRPipe(appContext) }
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun createDriverServers(appContext: AppContextProvider) {
|
||||
appContext.config.settings.context.state
|
||||
.map { it.data.driverConfig.enabled }
|
||||
.distinctUntilChanged()
|
||||
.collectLatest { enabled ->
|
||||
if (!enabled) return@collectLatest
|
||||
|
||||
coroutineScope {
|
||||
when (CURRENT_PLATFORM) {
|
||||
Platform.LINUX, Platform.OSX -> {
|
||||
launch { createUnixDriverSocket(appContext) }
|
||||
launch { createUnixFeederSocket(appContext) }
|
||||
}
|
||||
|
||||
Platform.WINDOWS -> {
|
||||
launch { createWindowsDriverPipe(appContext) }
|
||||
launch { createWindowsFeederPipe(appContext) }
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.slimevr.desktop.ipc
|
||||
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.getSocketDirectory
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import kotlinx.coroutines.CancellationException
|
||||
@@ -9,6 +10,7 @@ import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runInterruptible
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.IOException
|
||||
@@ -17,6 +19,7 @@ import java.net.StandardProtocolFamily
|
||||
import java.net.UnixDomainSocketAddress
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.nio.channels.ClosedByInterruptException
|
||||
import java.nio.channels.ServerSocketChannel
|
||||
import java.nio.channels.SocketChannel
|
||||
import kotlin.io.path.Path
|
||||
@@ -24,6 +27,7 @@ import kotlin.io.path.Path
|
||||
suspend fun createUnixDriverSocket(appContext: AppContextProvider) = acceptUnixClients(DRIVER_SOCKET_NAME) { channel ->
|
||||
handleDriverConnection(
|
||||
appContext = appContext,
|
||||
source = DriverBridgeSource.DRIVER,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
@@ -32,6 +36,7 @@ suspend fun createUnixDriverSocket(appContext: AppContextProvider) = acceptUnixC
|
||||
suspend fun createUnixFeederSocket(appContext: AppContextProvider) = acceptUnixClients(FEEDER_SOCKET_NAME) { channel ->
|
||||
handleDriverConnection(
|
||||
appContext = appContext,
|
||||
source = DriverBridgeSource.FEEDER,
|
||||
messages = readFramedMessages(channel),
|
||||
send = { bytes -> writeFramed(channel, bytes) },
|
||||
)
|
||||
@@ -60,17 +65,19 @@ private fun readFramedMessages(channel: SocketChannel) = flow {
|
||||
try {
|
||||
while (true) {
|
||||
lenBuf.clear()
|
||||
if (channel.read(lenBuf) == -1) break
|
||||
if (runInterruptible { channel.read(lenBuf) } == -1) break
|
||||
lenBuf.flip()
|
||||
|
||||
val dataBuf = ByteBuffer.allocate(lenBuf.int - 4)
|
||||
while (dataBuf.hasRemaining()) {
|
||||
if (channel.read(dataBuf) == -1) break
|
||||
if (runInterruptible { channel.read(dataBuf) } == -1) break
|
||||
}
|
||||
emit(dataBuf.array())
|
||||
}
|
||||
} catch (e: SocketException) {
|
||||
AppLogger.ipc.warn("Exception on socket: ${e.message}")
|
||||
} catch (e: ClosedByInterruptException) {
|
||||
AppLogger.ipc.info("Socket read interrupted, dropping connection")
|
||||
}
|
||||
}.flowOn(Dispatchers.IO)
|
||||
|
||||
@@ -98,7 +105,11 @@ private suspend fun acceptUnixClients(
|
||||
server.bind(UnixDomainSocketAddress.of(path))
|
||||
supervisorScope {
|
||||
while (isActive) {
|
||||
val client = server.accept()
|
||||
val client = try {
|
||||
runInterruptible { server.accept() }
|
||||
} catch (e: ClosedByInterruptException) {
|
||||
break
|
||||
}
|
||||
AppLogger.ipc.info("$name client connected")
|
||||
launch {
|
||||
try {
|
||||
|
||||
@@ -9,6 +9,9 @@ import com.sun.jna.platform.win32.WinError
|
||||
import com.sun.jna.platform.win32.WinNT
|
||||
import com.sun.jna.win32.StdCallLibrary
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.isActive
|
||||
import java.io.IOException
|
||||
|
||||
private val k32 = Kernel32.INSTANCE
|
||||
@@ -16,6 +19,9 @@ private val k32 = Kernel32.INSTANCE
|
||||
private const val HEADER_SIZE = 4
|
||||
private const val MAX_FRAME_SIZE = 256 * 1024
|
||||
|
||||
private const val WAIT_SLICE_MILLIS = 200
|
||||
private const val WAIT_TIMEOUT = 0x102
|
||||
|
||||
@Suppress("FunctionName")
|
||||
private object Kernel32IO : StdCallLibrary {
|
||||
init {
|
||||
@@ -29,6 +35,8 @@ private object Kernel32IO : StdCallLibrary {
|
||||
external fun GetOverlappedResult(handle: Pointer, overlapped: Pointer, moved: Pointer, wait: Boolean): Boolean
|
||||
|
||||
external fun WaitForSingleObject(handle: Pointer, millis: Int): Int
|
||||
|
||||
external fun CancelIoEx(handle: Pointer, overlapped: Pointer?): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +64,7 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
private val overlappedPointer = overlapped.pointer
|
||||
private val transferred = Memory(4)
|
||||
|
||||
fun awaitClient(): Boolean {
|
||||
suspend fun awaitClient(): Boolean {
|
||||
// On an overlapped handle ConnectNamedPipe returns straight away, so we do the waiting
|
||||
val connected = k32.ConnectNamedPipe(handle, overlapped)
|
||||
val err = Native.getLastError()
|
||||
@@ -122,8 +130,14 @@ internal class PipeSlot(private val handle: WinNT.HANDLE) : AutoCloseable {
|
||||
return awaitCompletion()
|
||||
}
|
||||
|
||||
private fun awaitCompletion(): Int {
|
||||
Kernel32IO.WaitForSingleObject(eventPointer, WinBase.INFINITE)
|
||||
private suspend fun awaitCompletion(): Int {
|
||||
while (Kernel32IO.WaitForSingleObject(eventPointer, WAIT_SLICE_MILLIS) == WAIT_TIMEOUT) {
|
||||
if (currentCoroutineContext().isActive) continue
|
||||
|
||||
Kernel32IO.CancelIoEx(handlePointer, overlappedPointer)
|
||||
Kernel32IO.GetOverlappedResult(handlePointer, overlappedPointer, transferred, true)
|
||||
currentCoroutineContext().ensureActive()
|
||||
}
|
||||
if (!Kernel32IO.GetOverlappedResult(handlePointer, overlappedPointer, transferred, true)) return -1
|
||||
return transferred.getInt(0)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import dev.slimevr.desktop.platform.TrackerStatus
|
||||
import dev.slimevr.desktop.platform.Version
|
||||
import dev.slimevr.driver.DriverBridge
|
||||
import dev.slimevr.driver.DriverBridgeInbound
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.driver.DriverBridgeOutbound
|
||||
import dev.slimevr.driver.TrackerRole
|
||||
import dev.slimevr.driver.bodyPartToRole
|
||||
@@ -93,6 +94,7 @@ suspend fun startBindingProvider() = withContext(Dispatchers.IO) {
|
||||
|
||||
suspend fun handleDriverConnection(
|
||||
appContext: AppContextProvider,
|
||||
source: DriverBridgeSource,
|
||||
messages: Flow<ByteArray>,
|
||||
send: suspend (ByteArray) -> Unit,
|
||||
) = coroutineScope {
|
||||
@@ -108,6 +110,7 @@ suspend fun handleDriverConnection(
|
||||
|
||||
val bridge = DriverBridge.create(
|
||||
id = appContext.server.nextHandle(),
|
||||
source = source,
|
||||
appContext = appContext,
|
||||
scope = this,
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.sun.jna.platform.win32.Kernel32
|
||||
import com.sun.jna.platform.win32.WinBase
|
||||
import com.sun.jna.platform.win32.WinNT
|
||||
import dev.slimevr.AppContextProvider
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -25,11 +26,11 @@ private const val PIPE_BUFFER_SIZE = 64 * 1024
|
||||
private typealias ConnectionHandler = suspend (messages: Flow<ByteArray>, send: suspend (ByteArray) -> Unit) -> Unit
|
||||
|
||||
suspend fun createWindowsDriverPipe(appContext: AppContextProvider) = acceptWindowsClients(DRIVER_PIPE) { messages, send ->
|
||||
handleDriverConnection(appContext, messages, send)
|
||||
handleDriverConnection(appContext, DriverBridgeSource.DRIVER, messages, send)
|
||||
}
|
||||
|
||||
suspend fun createWindowsFeederPipe(appContext: AppContextProvider) = acceptWindowsClients(FEEDER_PIPE) { messages, send ->
|
||||
handleDriverConnection(appContext, messages, send)
|
||||
handleDriverConnection(appContext, DriverBridgeSource.FEEDER, messages, send)
|
||||
}
|
||||
|
||||
suspend fun createWindowsSolarXRPipe(appContext: AppContextProvider) = acceptWindowsClients(SOLARXR_PIPE) { messages, send ->
|
||||
@@ -75,7 +76,16 @@ private suspend fun acceptWindowsClients(
|
||||
while (isActive) {
|
||||
val pipe = createSecurePipe(pipeName)
|
||||
val writer = PipeSlot(pipe)
|
||||
if (!writer.awaitClient()) {
|
||||
val connected = try {
|
||||
writer.awaitClient()
|
||||
} catch (e: CancellationException) {
|
||||
// The pipe goes down with the driver output, so the one waiting for a client has to
|
||||
// be released here: no handler owns it yet
|
||||
writer.close()
|
||||
k32.CloseHandle(pipe)
|
||||
throw e
|
||||
}
|
||||
if (!connected) {
|
||||
writer.close()
|
||||
k32.CloseHandle(pipe)
|
||||
continue
|
||||
|
||||
@@ -9,7 +9,9 @@ import com.sun.jna.platform.win32.WinDef
|
||||
import dev.slimevr.CURRENT_PLATFORM
|
||||
import dev.slimevr.Platform
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.desktop.getSteamVRDriversList
|
||||
import dev.slimevr.driver.DriverBridgeSource
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import dev.slimevr.trackingchecklist.TrackingChecklist
|
||||
import dev.slimevr.trackingchecklist.TrackingChecklistActions
|
||||
@@ -20,6 +22,7 @@ import io.ktor.client.engine.cio.CIO
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
@@ -92,7 +95,7 @@ private fun Throwable.isConnectionRefused(): Boolean = generateSequence(this, Th
|
||||
|
||||
data class SteamVRDriverState(val connected: Boolean = false, val installed: Boolean = true, val blocked: Boolean = false, val enabled: Boolean = true)
|
||||
|
||||
class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBehaviourType {
|
||||
class SteamVRCheckBehaviour(private val server: VRServer, private val settings: Settings) : TrackingChecklistBehaviourType {
|
||||
private val steamVRProcName = when (CURRENT_PLATFORM) {
|
||||
Platform.WINDOWS -> "vrserver.exe"
|
||||
else -> "vrserver"
|
||||
@@ -100,6 +103,8 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
private val driverState = MutableStateFlow(SteamVRDriverState())
|
||||
private val standableState = MutableStateFlow(false)
|
||||
|
||||
private fun driverEnabled() = settings.context.state.value.data.driverConfig.enabled
|
||||
|
||||
override fun observe(receiver: TrackingChecklist) {
|
||||
receiver.context.scope.launch {
|
||||
HttpClient(CIO).use { client ->
|
||||
@@ -107,7 +112,16 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
var lastScan: TimeMark? = null
|
||||
|
||||
while (isActive) {
|
||||
val connected = server.context.state.value.drivers.isNotEmpty()
|
||||
// Nothing to look at while the output is off: no driver can connect, and the step
|
||||
// below is switched off anyway. Reset so switching it back on does not report
|
||||
// whatever SteamVR was doing beforehand.
|
||||
if (!driverEnabled()) {
|
||||
driverState.value = SteamVRDriverState()
|
||||
delay(3000)
|
||||
continue
|
||||
}
|
||||
|
||||
val connected = server.context.state.value.drivers.values.any { it.source == DriverBridgeSource.DRIVER }
|
||||
|
||||
val drivers = try {
|
||||
getSteamVRDriversList(client)
|
||||
@@ -149,11 +163,15 @@ class SteamVRCheckBehaviour(private val server: VRServer) : TrackingChecklistBeh
|
||||
}
|
||||
}
|
||||
|
||||
driverState
|
||||
.map { state ->
|
||||
combine(
|
||||
driverState,
|
||||
settings.context.state.map { it.data.driverConfig.enabled }.distinctUntilChanged(),
|
||||
::Pair,
|
||||
)
|
||||
.map { (state, outputEnabled) ->
|
||||
TrackingChecklistStep(
|
||||
valid = state.connected,
|
||||
enabled = true,
|
||||
enabled = outputEnabled,
|
||||
ignorable = true,
|
||||
extraData = if (!state.connected) {
|
||||
TrackingChecklistSteamVRDisconnected(
|
||||
|
||||
+1
-1
Submodule solarxr-protocol updated: 457e352046...0412977eee.
Reference in new issue
Block a user