diff --git a/gui/app/public/i18n/en/translation.ftl b/gui/app/public/i18n/en/translation.ftl index b49a1e03c..850b5f2e4 100644 --- a/gui/app/public/i18n/en/translation.ftl +++ b/gui/app/public/i18n/en/translation.ftl @@ -562,6 +562,27 @@ settings-stay_aligned-debug-label = Debugging settings-stay_aligned-debug-description = Please include your settings when reporting problems about Stay Aligned. settings-stay_aligned-debug-copy-label = Copy settings to clipboard +## Keybinds Page +settings-keybinds = Keybind settings +settings-keybinds-description = Change keybinds for various shortcuts +keybind_config-keybind_name = Keybind +keybind_config-keybind_value = Combination +keybind_config-keybind_delay = Delay before trigger (s) +settings-keybinds_full-reset = Full Reset +settings-keybinds_yaw-reset = Yaw Reset +settings-keybinds_mounting-reset = Mounting Reset +settings-keybinds_feet-mounting-reset = Feet Mounting Reset +settings-keybinds_pause-tracking = Pause Tracking +settings-keybinds_reset-all-button = Reset all +settings-keybinds-system-managed-description = Your desktop manages global shortcuts itself, so they have to be changed from your system settings. +settings-keybinds-open-system-settings-button = Open system settings +settings-keybinds-unsupported-description = Global keybinds are not supported on this platform. +settings-sidebar-keybinds = Keybinds +settings-keybinds-recorder-modal-title = Assign keybind for +settings-keybinds-recorder-modal-reset-button = Reset +settings-keybinds-recorder-modal-unbind-button = Unbind +settings-keybinds-recorder-modal-done-button = Done + ## FK/Tracking settings settings-general-fk_settings = Tracking Settings diff --git a/gui/app/src/App.tsx b/gui/app/src/App.tsx index 2d7524707..0cb842cfe 100644 --- a/gui/app/src/App.tsx +++ b/gui/app/src/App.tsx @@ -44,6 +44,7 @@ import { useDiscordPresence } from './hooks/discord-presence'; import { withSentryReactRouterV6Routing } from '@sentry/react'; import { ScaledProportionsPage } from './components/onboarding/pages/body-proportions/ScaledProportions'; import { AdvancedSettings } from './components/settings/pages/AdvancedSettings'; +import { KeybindSettings } from './components/settings/pages/KeybindSettings'; import { FirmwareUpdate } from './components/firmware-update/FirmwareUpdate'; import { ConnectionLost } from './components/onboarding/pages/ConnectionLost'; import { VRCWarningsPage } from './components/vrc/VRCWarningsPage'; @@ -143,6 +144,7 @@ function Layout() { } /> } /> } /> + } /> = { + CONTROL: 'CTRL', + META: 'SUPER', + OS: 'SUPER', +}; + +export const KeybindRecorder = forwardRef< + HTMLInputElement, + { + keys: string[]; + onKeysChange: (v: string[]) => void; + error?: string; + } +>(function KeybindRecorder({ keys, onKeysChange, error }) { + const [localKeys, setLocalKeys] = useState(keys); + const [isRecording, setIsRecording] = useState(false); + const [oldKeys, setOldKeys] = useState([]); + const [invalidSlot, setInvalidSlot] = useState(null); + const [errorText, setErrorText] = useState(''); + const inputRef = useRef(null); + const displayKeys = isRecording ? localKeys : keys; + const activeIndex = isRecording ? displayKeys.length : -1; + const displayError = errorText || error; + + const { clearErrors } = useFormContext(); + + const handleKeyDown = (e: React.KeyboardEvent) => { + e.preventDefault(); + const rawKey = e.key.toUpperCase(); + const key = keyAliases[rawKey] ?? rawKey; + const errorMsg = excludedKeys.includes(key) + ? `Cannot use ${key}!` + : displayKeys.includes(key) + ? `${key} is a Duplicate Key!` + : null; + if (errorMsg) { + setErrorText(errorMsg); + setInvalidSlot(activeIndex); + setTimeout(() => { + setInvalidSlot(null); + }, 350); + return; + } + + if (displayKeys.length < maxKeybindLength) { + const updatedKeys = [...displayKeys, key]; + setLocalKeys(updatedKeys); + onKeysChange(updatedKeys); + if (updatedKeys.length == maxKeybindLength) { + inputRef.current?.blur(); + } + } + }; + + const handleOnBlur = () => { + setIsRecording(false); + if (displayKeys.length < maxKeybindLength - 2 || error) { + onKeysChange(oldKeys); + setLocalKeys(oldKeys); + } + }; + + const handleOnFocus = () => { + clearErrors('keybinds'); + const initialKeys: string[] = []; + setOldKeys(keys); + setLocalKeys(initialKeys); + onKeysChange(initialKeys); + setIsRecording(true); + }; + + return ( +
+
+ +
+ {Array.from({ length: maxKeybindLength }).map((_, i) => { + const key = displayKeys[i]; + const isActive = isRecording && i === activeIndex; + const isInvalid = invalidSlot === i; + return ( +
+
+ {key ?? ''} +
+
+ {i < maxKeybindLength - 1 ? '+' : ''} +
+
+ ); + })} +
+
+ {displayError && ( +
+ {`${errorText} ${error}`} +
+ )} +
+ ); +}); diff --git a/gui/app/src/components/commons/KeybindRecorderModal.tsx b/gui/app/src/components/commons/KeybindRecorderModal.tsx new file mode 100644 index 000000000..e465bf8ed --- /dev/null +++ b/gui/app/src/components/commons/KeybindRecorderModal.tsx @@ -0,0 +1,93 @@ +import { BaseModal } from './BaseModal'; +import { + Controller, + Control, + useFormContext, + FieldValues, + FieldPath, +} from 'react-hook-form'; +import { KeybindRecorder } from './KeybindRecorder'; +import { Typography } from './Typography'; +import { Button } from './Button'; +import './KeybindRow.scss'; +import { useLocalization } from '@fluent/react'; + +export function KeybindRecorderModal({ + id, + control, + name, + isVisisble, + onClose, + onUnbind, + onSubmit, +}: { + id?: string; + control: Control; + name: FieldPath; + isVisisble: boolean; + onClose: () => void; + onUnbind: () => void; + onSubmit: () => void; +}) { + const { l10n } = useLocalization(); + const keybindlocalization = 'settings-keybinds_' + id; + const { + formState: { errors }, + resetField, + handleSubmit, + } = useFormContext(); + + return ( + +
+ + {l10n.getString('settings-keybinds-recorder-modal-title')}{' '} + {l10n.getString(keybindlocalization)} + + ( + + )} + /> +
+
+
+
+
+
+
+
+ ); +} diff --git a/gui/app/src/components/commons/KeybindRow.scss b/gui/app/src/components/commons/KeybindRow.scss new file mode 100644 index 000000000..aac57c22d --- /dev/null +++ b/gui/app/src/components/commons/KeybindRow.scss @@ -0,0 +1,65 @@ +.keybind-row { + display: grid; + grid-column: 1 / -1; + grid-template-columns: subgrid; + height: auto; + align-items: center; + gap: 10px; +} + +@keyframes keyslot { + 0%, + 100% { + transform: scale(1); + opacity: 0.6; + } + + 50% { + transform: scale(1.08); + opacity: 1; + } +} + +@keyframes shake { + 0% { + transform: translate(1px, 1px) rotate(0deg); + } + 10% { + transform: translate(-1px, -2px) rotate(-1deg); + } + 20% { + transform: translate(-3px, 0px) rotate(1deg); + } + 30% { + transform: translate(3px, 2px) rotate(0deg); + } + 40% { + transform: translate(1px, -1px) rotate(1deg); + } + 50% { + transform: translate(-1px, 2px) rotate(-1deg); + } + 60% { + transform: translate(-3px, 1px) rotate(0deg); + } + 70% { + transform: translate(3px, 1px) rotate(-1deg); + } + 80% { + transform: translate(-1px, -1px) rotate(1deg); + } + 90% { + transform: translate(1px, 2px) rotate(0deg); + } + 100% { + transform: translate(1px, -2px) rotate(-1deg); + } +} + +.keyslot-animate { + animation: keyslot 1s ease-in-out infinite; +} + +.keyslot-invalid { + animation: shake 0.35s ease; +} diff --git a/gui/app/src/components/commons/KeybindsRow.tsx b/gui/app/src/components/commons/KeybindsRow.tsx new file mode 100644 index 000000000..0c5e930c5 --- /dev/null +++ b/gui/app/src/components/commons/KeybindsRow.tsx @@ -0,0 +1,79 @@ +import { Typography } from './Typography'; +import './KeybindRow.scss'; +import { Control, FieldPath, FieldValues, useWatch } from 'react-hook-form'; +import { NumberSelector } from './NumberSelector'; +import { useLocaleConfig } from '@/i18n/config'; + +function KeyBindKeyList({ keybind }: { keybind: string[] }) { + if (keybind.length <= 1) { + return ( +
+ Click to edit keybind +
+ ); + } + return keybind.map((key, i) => { + return ( +
+
+ {key ?? ''} +
+
+ {i < keybind.length - 1 ? '+' : ''} +
+
+ ); + }); +} + +export function KeybindsRow({ + id, + control, + index, + openKeybindRecorderModal, +}: { + id?: string; + control: Control; + index: number; + openKeybindRecorderModal: (index: number) => void; +}) { + const binding = useWatch({ + control, + name: `keybinds.${index}.binding` as FieldPath, + }) as string[] | undefined; + const { currentLocales } = useLocaleConfig(); + const secondsFormat = new Intl.NumberFormat(currentLocales, { + style: 'unit', + unit: 'second', + unitDisplay: 'narrow', + maximumFractionDigits: 2, + }); + + const handleOpenModal = () => { + openKeybindRecorderModal(index); + }; + + return ( +
+ +
+
+ {binding != null && } +
+
+ } + valueLabelFormat={(value) => secondsFormat.format(value)} + min={0} + max={10} + step={0.2} + /> +
+ ); +} diff --git a/gui/app/src/components/commons/NumberSelector.tsx b/gui/app/src/components/commons/NumberSelector.tsx index 93553824b..4a34af464 100644 --- a/gui/app/src/components/commons/NumberSelector.tsx +++ b/gui/app/src/components/commons/NumberSelector.tsx @@ -57,60 +57,62 @@ export function NumberSelector({ ( -
- {label} -
-
- {doubleStep !== undefined && ( + render={({ field: { onChange, value } }) => { + return ( +
+ {label?.length != 0 ? {label} : <>} +
+
+ {doubleStep !== undefined && ( + + )} - )} - -
-
- {valueLabelFormat ? valueLabelFormat(value) : value} -
-
- - {doubleStep !== undefined && ( +
+
+ {valueLabelFormat ? valueLabelFormat(value) : value} +
+
- )} + {doubleStep !== undefined && ( + + )} +
-
- )} + ); + }} /> ); } diff --git a/gui/app/src/components/settings/SettingsLayout.tsx b/gui/app/src/components/settings/SettingsLayout.tsx index 8b97d7b20..46810de56 100644 --- a/gui/app/src/components/settings/SettingsLayout.tsx +++ b/gui/app/src/components/settings/SettingsLayout.tsx @@ -58,6 +58,10 @@ export function SettingSelectorMobile() { label: l10n.getString('settings-sidebar-advanced'), value: { url: '/settings/advanced' }, }, + { + label: l10n.getString('settings-sidebar-keybinds'), + value: { url: '/settings/keybinds' }, + }, { label: l10n.getString('navbar-onboarding'), value: { url: '/onboarding/home' }, diff --git a/gui/app/src/components/settings/SettingsSidebar.tsx b/gui/app/src/components/settings/SettingsSidebar.tsx index d50179569..67ad2be27 100644 --- a/gui/app/src/components/settings/SettingsSidebar.tsx +++ b/gui/app/src/components/settings/SettingsSidebar.tsx @@ -144,6 +144,11 @@ export function SettingsSidebar() { to="/settings/advanced" id="settings-sidebar-advanced" /> +
diff --git a/gui/app/src/components/settings/pages/KeybindSettings.scss b/gui/app/src/components/settings/pages/KeybindSettings.scss new file mode 100644 index 000000000..b73610c1a --- /dev/null +++ b/gui/app/src/components/settings/pages/KeybindSettings.scss @@ -0,0 +1,15 @@ +.keybind-settings { + display: grid; + + grid-template: + 'n v d ' auto + 'k k k ' auto + 'b . . ' auto + / 1fr 1fr 90px; + + grid-template-columns: subgrid; + grid-template-rows: subgrid; + + align-items: left; + gap: 10px; +} diff --git a/gui/app/src/components/settings/pages/KeybindSettings.tsx b/gui/app/src/components/settings/pages/KeybindSettings.tsx new file mode 100644 index 000000000..14571e479 --- /dev/null +++ b/gui/app/src/components/settings/pages/KeybindSettings.tsx @@ -0,0 +1,265 @@ +import { KeybindRecorderModal } from '@/components/commons/KeybindRecorderModal'; +import { + SettingsPageLayout, + SettingsPagePaneLayout, +} from '@/components/settings/SettingsPageLayout'; +import { WrenchIcon } from '@/components/commons/icon/WrenchIcon'; +import { Typography } from '@/components/commons/Typography'; +import { useLocalization } from '@fluent/react'; +import './KeybindSettings.scss'; +import { Button } from '@/components/commons/Button'; +import { KeybindsRow } from '@/components/commons/KeybindsRow'; +import { useWebsocketAPI } from '@/hooks/websocket-api'; +import { ReactNode, useEffect, useRef, useState } from 'react'; +import { + ChangeKeybindRequestT, + KeybindRequestT, + KeybindResponseT, + KeybindSupport, + KeybindT, + OpenKeybindSettingsRequestT, + RpcMessage, +} from 'solarxr-protocol'; +import { FormProvider, useFieldArray, useForm } from 'react-hook-form'; + +export type KeybindForm = { + keybinds: { + id: number; + name: string; + binding: string[]; + delay: number; + }[]; +}; + +export function KeybindSettings() { + const { l10n } = useLocalization(); + const { sendRPCPacket, useRPCPacket } = useWebsocketAPI(); + const [isOpen, setIsOpen] = useState(false); + const [defaultKeybindsState, setDefaultKeybindsState] = useState( + { + keybinds: [], + } + ); + const currentIndex = useRef(null); + const [support, setSupport] = useState( + KeybindSupport.UNSUPPORTED + ); + + const methods = useForm({ + defaultValues: defaultKeybindsState, + }); + + const { + control, + handleSubmit, + reset, + setValue, + getValues, + setError, + clearErrors, + resetField, + watch, + } = methods; + + const { fields } = useFieldArray({ + control, + name: 'keybinds', + }); + + const onSubmit = () => { + const value = getValues(); + if (checkDuplicates(value)) { + return; + } + clearErrors('keybinds'); + + value.keybinds.forEach((kb) => { + const changeKeybindRequest = new ChangeKeybindRequestT(); + + const keybind = new KeybindT(); + keybind.keybindId = kb.id; + keybind.keybindValue = kb.binding.join('+'); + keybind.keybindDelay = kb.delay; + + changeKeybindRequest.keybind = keybind; + + sendRPCPacket(RpcMessage.ChangeKeybindRequest, changeKeybindRequest); + setIsOpen(false); + }); + }; + + const checkDuplicates = (value: KeybindForm) => { + const normalized = value.keybinds + .filter((kb) => kb.binding.length > 0) + .map((kb) => JSON.stringify([...kb.binding].sort())); + + const unique = new Set(normalized); + + if (unique.size !== normalized.length) { + setError('keybinds', { + type: 'manual', + message: 'Duplicate keybind combinations are not allowed', + }); + return true; + } + + return false; + }; + + const handleOpenSystemSettingsButton = () => { + sendRPCPacket( + RpcMessage.OpenKeybindSettingsRequest, + new OpenKeybindSettingsRequestT() + ); + }; + + useRPCPacket( + RpcMessage.KeybindResponse, + ({ keybind, defaultKeybinds, support }: KeybindResponseT) => { + setSupport(support); + if (!keybind) return; + + const mappedDefaults = defaultKeybinds.map((kb) => ({ + id: kb.keybindId, + name: kb.keybindNameId?.toString() ?? '', + binding: kb.keybindValue?.toString().split('+') ?? [], + delay: kb.keybindDelay, + })); + + setDefaultKeybindsState({ keybinds: mappedDefaults }); + reset({ keybinds: mappedDefaults }); + + const mapped = keybind.map((kb) => ({ + id: kb.keybindId, + name: kb.keybindNameId?.toString() ?? '', + binding: kb.keybindValue?.toString().split('+') ?? [], + delay: kb.keybindDelay, + })); + + mapped.forEach((keybind, index) => { + setValue(`keybinds.${index}.binding`, keybind.binding); + setValue(`keybinds.${index}.delay`, keybind.delay); + }); + } + ); + + const handleOpenRecorderModal = (index: number) => { + currentIndex.current = index; + setIsOpen(true); + }; + + const onClose = () => { + if (currentIndex.current != null) { + resetField(`keybinds.${currentIndex.current}.binding`); + } + setIsOpen(false); + }; + + // Captured so the modal's callbacks keep the index they were opened with + const editedIndex = currentIndex.current; + + const createKeybindRows = (): ReactNode => { + return fields.map((field, index) => { + return ( +
+ +
+ ); + }); + }; + + useEffect(() => { + sendRPCPacket(RpcMessage.KeybindRequest, new KeybindRequestT()); + }, []); + + // Only the recorder modal submits, so a delay change would otherwise never reach the server + useEffect(() => { + const subscription = watch((_, { name, type }) => { + if (type === 'change' && name?.endsWith('.delay')) onSubmit(); + }); + return () => subscription.unsubscribe(); + }, [watch]); + + return ( + + } id="keybinds"> +
+ +
+ {l10n + .getString('settings-keybinds-description') + .split('\n') + .map((line, i) => ( + {line} + ))} +
+ {support === KeybindSupport.UNSUPPORTED && ( + + )} + {support === KeybindSupport.SYSTEM_MANAGED && ( +
+ +
+
+
+ )} + {support === KeybindSupport.APP_MANAGED && ( + <> + +
+ + + + {createKeybindRows()} +
+
+
+ {editedIndex != null && ( + + setValue(`keybinds.${editedIndex}.binding`, []) + } + onSubmit={onSubmit} + /> + )} +
+ + )} +
+
+
+ ); +} diff --git a/gui/app/tailwind.config.ts b/gui/app/tailwind.config.ts index 07004e88c..f3a9de3fd 100644 --- a/gui/app/tailwind.config.ts +++ b/gui/app/tailwind.config.ts @@ -183,6 +183,7 @@ const config = { lg: '1300px', xl: '1600px', tall: { raw: '(min-height: 860px)' }, + 'keybinds-small': { raw: 'not (min-width: 1230px)' }, }, extend: { colors: { @@ -236,10 +237,10 @@ const config = { }, }, 'timer-tick': { - "0%, 40%": { + '0%, 40%': { transform: 'scale(1)', }, - "20%": { + '20%': { transform: 'scale(1.3)', }, }, @@ -294,7 +295,7 @@ const config = { light: `linear-gradient(135deg, ${colors['light-accent'][100]} 50%, ${colors['light-background'][700]} 50% 100%)`, 'trans-flag': `linear-gradient(135deg, ${colors['trans-blue'][800]} 40%, ${colors['trans-blue'][700]} 40% 70%, ${colors['trans-blue'][600]} 70% 100%)`, 'asexual-flag': `linear-gradient(135deg, ${colors['asexual'][100]} 30%, ${colors['asexual'][200]} 30% 50%, ${colors['asexual'][300]} 50% 70%, ${colors['asexual'][400]} 70% 100%)`, - 'snep': `linear-gradient(135deg, ${colors['snep'][100]} 40%, ${colors['snep'][200]} 40% 70%, ${colors['snep'][300]} 70% 100%)`, + snep: `linear-gradient(135deg, ${colors['snep'][100]} 40%, ${colors['snep'][200]} 40% 70%, ${colors['snep'][300]} 70% 100%)`, }, animation: { 'spin-ccw': 'spin-ccw 1s linear infinite', diff --git a/server/core/src/main/java/dev/slimevr/config/serializers.kt b/server/core/src/main/java/dev/slimevr/config/serializers.kt index c3a5dfecc..4cc77d94d 100644 --- a/server/core/src/main/java/dev/slimevr/config/serializers.kt +++ b/server/core/src/main/java/dev/slimevr/config/serializers.kt @@ -16,6 +16,22 @@ import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.jsonPrimitive import solarxr_protocol.datatypes.BodyPart +import solarxr_protocol.rpc.KeybindId + +object KeybindIdSerializer : KSerializer { + override val descriptor = PrimitiveSerialDescriptor("KeybindId", PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: KeybindId) { + (encoder as JsonEncoder).encodeJsonElement(JsonPrimitive(value.name)) + } + + override fun deserialize(decoder: Decoder): KeybindId { + val element = (decoder as JsonDecoder).decodeJsonElement() + // Falling back to a real keybind would silently turn an unknown entry into a duplicate + return KeybindId.entries.firstOrNull { it.name == element.jsonPrimitive.content } + ?: KeybindId.NONE + } +} object BodyPartSerializer : KSerializer { @OptIn(ExperimentalSerializationApi::class) diff --git a/server/core/src/main/java/dev/slimevr/config/settings.kt b/server/core/src/main/java/dev/slimevr/config/settings.kt index e7bb7def7..4336ced05 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -16,6 +16,7 @@ import kotlinx.serialization.json.jsonPrimitive import solarxr_protocol.datatypes.BodyPart import solarxr_protocol.rpc.ArmsResetMode import solarxr_protocol.rpc.FilteringType +import solarxr_protocol.rpc.KeybindId private const val SETTINGS_CONFIG_VERSION = 2 @@ -91,6 +92,24 @@ data class ResetsConfig( val lastMountingMethod: MountingMethods = MountingMethods.AUTOMATIC, // TODO ) +@Serializable +data class KeybindConfig( + @Serializable(with = KeybindIdSerializer::class) + val id: KeybindId, + /** Key combination, keys joined with '+' (e.g. "CTRL+ALT+SHIFT+Y") */ + val binding: String, + /** Delay in seconds before the keybind triggers */ + val delay: Float = 0f, +) + +fun defaultKeybinds(): List = listOf( + KeybindConfig(KeybindId.FULL_RESET, "CTRL+ALT+SHIFT+Y"), + KeybindConfig(KeybindId.YAW_RESET, "CTRL+ALT+SHIFT+U"), + KeybindConfig(KeybindId.MOUNTING_RESET, "CTRL+ALT+SHIFT+I"), + KeybindConfig(KeybindId.FEET_MOUNTING_RESET, "CTRL+ALT+SHIFT+P"), + KeybindConfig(KeybindId.PAUSE_TRACKING, "CTRL+ALT+SHIFT+O"), +) + // Used in SkeletonConfig @Serializable data class SkeletonTogglesConfig( @@ -170,6 +189,7 @@ data class SettingsConfigState( val skeletonConfig: SkeletonConfig = SkeletonConfig(), val vrcOscConfig: VRCOSCConfig = VRCOSCConfig(), val vmcConfig: VMCConfig = VMCConfig(), + val keybinds: List = defaultKeybinds(), ) private fun migrateSettingsConfig(json: JsonObject): JsonObject { diff --git a/server/core/src/main/java/dev/slimevr/feature-flags.kt b/server/core/src/main/java/dev/slimevr/feature-flags.kt index bfbd94396..526c63c61 100644 --- a/server/core/src/main/java/dev/slimevr/feature-flags.kt +++ b/server/core/src/main/java/dev/slimevr/feature-flags.kt @@ -1,7 +1,10 @@ package dev.slimevr +import solarxr_protocol.rpc.KeybindSupport + data class FeatureFlags( var steam: Boolean = false, var skipCheckUdev: Boolean = false, var udevRulesInstalled: Boolean? = null, + var keybindSupport: KeybindSupport = KeybindSupport.UNSUPPORTED, ) diff --git a/server/core/src/main/java/dev/slimevr/logger.kt b/server/core/src/main/java/dev/slimevr/logger.kt index 070a74b4d..b03bd11b0 100644 --- a/server/core/src/main/java/dev/slimevr/logger.kt +++ b/server/core/src/main/java/dev/slimevr/logger.kt @@ -24,6 +24,7 @@ object AppLogger { val oscQuery = logger("OSCQuery") val install = logger("Install") val resets = logger("Resets") + val keybind = logger("Keybind") val coroutines = noCoLogger("Coroutines") init { diff --git a/server/core/src/main/java/dev/slimevr/solarxr/keybinds.kt b/server/core/src/main/java/dev/slimevr/solarxr/keybinds.kt new file mode 100644 index 000000000..50927dc3e --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/solarxr/keybinds.kt @@ -0,0 +1,58 @@ +package dev.slimevr.solarxr + +import dev.slimevr.config.KeybindConfig +import dev.slimevr.config.Settings +import dev.slimevr.config.SettingsActions +import dev.slimevr.config.defaultKeybinds +import solarxr_protocol.rpc.ChangeKeybindRequest +import solarxr_protocol.rpc.Keybind +import solarxr_protocol.rpc.KeybindId +import solarxr_protocol.rpc.KeybindRequest +import solarxr_protocol.rpc.KeybindResponse + +private fun keybindNameId(id: KeybindId): String = id.name.lowercase().replace('_', '-') + +private fun keybindConfigToProto(config: KeybindConfig): Keybind = Keybind( + keybindId = config.id, + keybindNameId = keybindNameId(config.id), + keybindValue = config.binding, + keybindDelay = config.delay, +) + +class KeybindsBehaviour( + private val settings: Settings, +) : SolarXRBridgeBehaviour { + override fun observe(receiver: SolarXRBridge) { + receiver.rpcDispatcher.on { + val keybinds = settings.context.state.value.data.keybinds + receiver.sendRpc( + KeybindResponse( + keybind = keybinds.map { keybindConfigToProto(it) }, + defaultKeybinds = defaultKeybinds().map { keybindConfigToProto(it) }, + support = receiver.appContext.featureFlags.keybindSupport, + ), + ) + } + + receiver.rpcDispatcher.on { req -> + val keybind = req.keybind ?: return@on + val id = keybind.keybindId?.takeUnless { it == KeybindId.NONE } ?: return@on + settings.context.dispatch( + SettingsActions.Update { + copy( + keybinds = keybinds.map { + if (it.id == id) { + it.copy( + binding = keybind.keybindValue ?: it.binding, + delay = keybind.keybindDelay ?: 0f, + ) + } else { + it + } + }, + ) + }, + ) + } + } +} diff --git a/server/core/src/main/java/dev/slimevr/solarxr/module.kt b/server/core/src/main/java/dev/slimevr/solarxr/module.kt index ad5a8f012..a8b95e6a0 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/module.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/module.kt @@ -86,6 +86,7 @@ class SolarXRBridge( add(KnownTrackersBehaviour(appContext.config.settings)) add(BvhBehaviour(appContext.bvhManager)) add(InstalledInfoBehaviour()) + add(KeybindsBehaviour(appContext.config.settings)) add(SessionCalibrationBehaviour(appContext.resetsManager)) } diff --git a/server/core/src/main/java/dev/slimevr/tracker/behaviours.kt b/server/core/src/main/java/dev/slimevr/tracker/behaviours.kt index 59db381d2..6ff847cd7 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/behaviours.kt @@ -1,5 +1,6 @@ package dev.slimevr.tracker +import dev.slimevr.AppLogger import dev.slimevr.skeleton.SkeletonActions import dev.slimevr.util.safeLaunch import io.github.axisangles.ktmath.Quaternion @@ -7,7 +8,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChangedBy -import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map @@ -196,7 +196,7 @@ class TrackerTPSBehaviour : TrackerBehaviour { receiver.context.dispatch(TrackerActions.Update { copy(tps = tps.toUShort()) }) mark = TimeSource.Monotonic.markNow() } catch (e: Exception) { - dev.slimevr.AppLogger.coroutines.error(e, "Error in TrackerTPSBehaviour") + AppLogger.coroutines.error(e, "Error in TrackerTPSBehaviour") } } } diff --git a/server/desktop/build.gradle.kts b/server/desktop/build.gradle.kts index 45cddc94f..ceafe6146 100644 --- a/server/desktop/build.gradle.kts +++ b/server/desktop/build.gradle.kts @@ -88,6 +88,10 @@ dependencies { implementation("org.hid4java:hid4java:0.8.0") implementation("io.klogging:klogging:0.11.7") + // Global keybinds: JIntellitype on Windows, dbus xdg-desktop-portal on Linux + implementation("com.melloware:jintellitype:1.+") + implementation("com.github.HannahPadd:DbusGlobalShortcutsWayland:v0.1.0") + val ktorVersion = "3.4.1" implementation("io.ktor:ktor-client-core:$ktorVersion") implementation("io.ktor:ktor-client-cio:$ktorVersion") @@ -103,6 +107,8 @@ tasks.shadowJar { exclude(dependency("com.fazecast:jSerialComm:.*")) exclude(dependency("net.java.dev.jna:.*:.*")) exclude(dependency("com.google.flatbuffers:flatbuffers-java:.*")) + exclude(dependency("com.melloware:jintellitype:.*")) + exclude(dependency("com.github.HannahPadd:DbusGlobalShortcutsWayland:.*")) exclude(project(":solarxr-protocol:generated")) } diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt index 8a0f02429..feb64e96c 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt @@ -17,6 +17,9 @@ import dev.slimevr.desktop.install.executeShellCommand import dev.slimevr.desktop.install.runInstaller import dev.slimevr.desktop.ipc.createIpcServers import dev.slimevr.desktop.ipc.createSolarXRWebsocketServer +import dev.slimevr.desktop.keybind.createDesktopKeybindManager +import dev.slimevr.desktop.keybind.isGnome +import dev.slimevr.desktop.keybind.resolveGnomeAppId import dev.slimevr.desktop.networkprofile.setupDesktopNetworkProfileChecker import dev.slimevr.desktop.serial.DesktopFirmwareFlasher import dev.slimevr.desktop.serial.createDesktopSerialServer @@ -38,8 +41,12 @@ import dev.slimevr.udp.UdpServer import dev.slimevr.util.safeLaunch import dev.slimevr.vmc.VMCManager import dev.slimevr.vrcosc.VRCOSCManager +import kotlinx.coroutines.Job +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import solarxr_protocol.rpc.KeybindSupport fun main(args: Array) = runBlocking { contextDebugEnabled = System.getProperty("slimevr.debug.context") == "true" || @@ -60,6 +67,20 @@ fun main(args: Array) = runBlocking { } if (CURRENT_PLATFORM != Platform.LINUX) featureFlags.skipCheckUdev = true + // Windows registers hotkeys itself, and on GNOME we can rewrite the triggers it stores in dconf. + // Other compositors own the bindings, so there the user rebinds from the system settings. + featureFlags.keybindSupport = when (CURRENT_PLATFORM) { + Platform.WINDOWS -> KeybindSupport.APP_MANAGED + + Platform.LINUX -> if (isGnome() && resolveGnomeAppId() != null) { + KeybindSupport.APP_MANAGED + } else { + KeybindSupport.SYSTEM_MANAGED + } + + else -> KeybindSupport.UNSUPPORTED + } + val isInstallDisabled = System.getenv("SLIME_SERVER_DISABLE_INSTALLER")?.toIntOrNull() if (featureFlags.steam && isInstallDisabled != 1) runInstaller() @@ -125,16 +146,27 @@ fun main(args: Array) = runBlocking { tapDetectionManager = tapDetectionManager, ) + // JVM shutdown hooks fire on SIGINT/SIGTERM but coroutine `finally` blocks do not, + // so cancel the root scope on shutdown to let structured concurrency run every cleanup. + val rootJob = coroutineContext[Job]!! + Runtime.getRuntime().addShutdownHook( + Thread { + rootJob.cancel() + runBlocking { rootJob.join() } + }, + ) + try { appContext.startObserving() safeLaunch { createDesktopHIDManager(appContext, this) } + safeLaunch { createDesktopKeybindManager(appContext, this) } safeLaunch { createSolarXRWebsocketServer(appContext) } safeLaunch { createIpcServers(appContext) } safeLaunch { setupDesktopNetworkProfileChecker(this, networkProfileManager) } awaitCancellation() } finally { - appContext.dispose() + withContext(NonCancellable) { appContext.dispose() } } } diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt b/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt index b7cde9d9f..b04b8329a 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt @@ -1,7 +1,12 @@ package dev.slimevr.desktop.ipc import com.google.flatbuffers.FlatBufferBuilder +import dev.hannah.portals.PortalManager import dev.slimevr.AppContextProvider +import dev.slimevr.AppLogger +import dev.slimevr.CURRENT_PLATFORM +import dev.slimevr.Platform +import dev.slimevr.SLIMEVR_IDENTIFIER import dev.slimevr.VRServerActions import dev.slimevr.desktop.unblockSteamVRDriver import dev.slimevr.fbscodegen.runtime.JvmFlatBufferReader @@ -13,10 +18,13 @@ import dev.slimevr.util.safeLaunch import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import io.ktor.util.moveToByteArray +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.withContext import solarxr_protocol.MessageBundle import solarxr_protocol.rpc.EnableSteamVRDriverRequest +import solarxr_protocol.rpc.OpenKeybindSettingsRequest import java.nio.ByteBuffer class EnableSteamVRDriverBehaviour : SolarXRBridgeBehaviour { @@ -31,6 +39,21 @@ class EnableSteamVRDriverBehaviour : SolarXRBridgeBehaviour { } } +class OpenKeybindSettingsBehaviour : SolarXRBridgeBehaviour { + override fun observe(receiver: SolarXRBridge) { + receiver.rpcDispatcher.on { + if (CURRENT_PLATFORM != Platform.LINUX) return@on + receiver.context.scope.safeLaunch { + withContext(Dispatchers.IO) { + runCatching { + PortalManager(SLIMEVR_IDENTIFIER).openGlobalShortcutsSettings() + }.onFailure { AppLogger.keybind.error(it, "Failed to open global shortcuts settings") } + } + } + } + } +} + suspend fun handleSolarXRBridge( appContext: AppContextProvider, messages: Flow, @@ -43,6 +66,7 @@ suspend fun handleSolarXRBridge( extraBehaviours = { _ -> buildList { add(EnableSteamVRDriverBehaviour()) + add(OpenKeybindSettingsBehaviour()) } }, ) diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/keybind/gnome.kt b/server/desktop/src/main/java/dev/slimevr/desktop/keybind/gnome.kt new file mode 100644 index 000000000..0485dbd65 --- /dev/null +++ b/server/desktop/src/main/java/dev/slimevr/desktop/keybind/gnome.kt @@ -0,0 +1,72 @@ +package dev.slimevr.desktop.keybind + +import dev.slimevr.AppLogger +import dev.slimevr.config.KeybindConfig +import dev.slimevr.desktop.install.executeShellCommand +import java.io.File + +private const val GLOBAL_SHORTCUTS_PATH = "/org/gnome/settings-daemon/global-shortcuts" + +private val MODIFIERS = linkedMapOf( + "SHIFT" to "", + "CTRL" to "", + "ALT" to "", + "SUPER" to "", +) + +fun isGnome(): Boolean = System.getenv("XDG_CURRENT_DESKTOP")?.uppercase()?.contains("GNOME") == true + +fun resolveGnomeAppId(): String? { + val cgroup = runCatching { File("/proc/self/cgroup").readText() }.getOrNull() ?: return null + + val scope = cgroup.lineSequence() + .map { line -> line.substringAfterLast('/', "") } + .firstOrNull { it.startsWith("app-") && it.endsWith(".scope") } + ?: return null + + // app--.scope, where instance is the pid that launched it + return scope + .removePrefix("app-") + .removeSuffix(".scope") + .substringBeforeLast('-') + .ifEmpty { null } +} + +/** Converts our `CTRL+ALT+SHIFT+Y` form into GTK's `y` form. */ +private fun toGnomeAccelerator(binding: String): String? { + val parts = binding.split('+').map { it.trim().uppercase() }.filter { it.isNotEmpty() } + val key = parts.firstOrNull { it !in MODIFIERS } ?: return null + + val modifiers = parts.filter { it in MODIFIERS }.toSet() + return MODIFIERS.filterKeys { it in modifiers }.values.joinToString("") + key.lowercase() +} + +private fun escapeGVariant(value: String): String = value.replace("\\", "\\\\").replace("'", "\\'") + +private fun buildGnomeShortcutsVariant(keybinds: List): String = keybinds + .mapNotNull { keybind -> + val accelerator = toGnomeAccelerator(keybind.binding) ?: return@mapNotNull null + "('${escapeGVariant(keybind.id.name)}', {'shortcuts': <['${escapeGVariant(accelerator)}']>, " + + "'description': <'${escapeGVariant(keybindLabel(keybind.id))}'>})" + } + .joinToString(", ", prefix = "[", postfix = "]") + +suspend fun gnomeShortcutsExist(appId: String): Boolean { + val (exitCode, output) = executeShellCommand("dconf", "read", "$GLOBAL_SHORTCUTS_PATH/$appId/shortcuts") ?: return false + return exitCode == 0 && output.isNotBlank() +} + +suspend fun writeGnomeShortcuts(appId: String, keybinds: List): Boolean { + val bound = keybinds.filter { toGnomeAccelerator(it.binding) != null } + if (bound.isEmpty()) return false + + val variant = buildGnomeShortcutsVariant(bound) + val (exitCode, output) = executeShellCommand("dconf", "write", "$GLOBAL_SHORTCUTS_PATH/$appId/shortcuts", variant) + ?: return false + + if (exitCode != 0) { + AppLogger.keybind.warn("Failed to write GNOME shortcuts for $appId: ${output.trim()}") + return false + } + return true +} diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/keybind/keybinds.kt b/server/desktop/src/main/java/dev/slimevr/desktop/keybind/keybinds.kt new file mode 100644 index 000000000..3aa62fe9d --- /dev/null +++ b/server/desktop/src/main/java/dev/slimevr/desktop/keybind/keybinds.kt @@ -0,0 +1,154 @@ +package dev.slimevr.desktop.keybind + +import com.melloware.jintellitype.JIntellitype +import dev.hannah.portals.PortalManager +import dev.hannah.portals.globalShortcuts.GlobalShortcutsHandler +import dev.hannah.portals.globalShortcuts.Shortcut +import dev.hannah.portals.globalShortcuts.ShortcutTuple +import dev.slimevr.AppContextProvider +import dev.slimevr.AppLogger +import dev.slimevr.CURRENT_PLATFORM +import dev.slimevr.Platform +import dev.slimevr.SLIMEVR_IDENTIFIER +import dev.slimevr.config.KeybindConfig +import dev.slimevr.skeleton.SkeletonActions +import dev.slimevr.util.safeLaunch +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.distinctUntilChangedBy +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach +import solarxr_protocol.datatypes.BodyPart +import solarxr_protocol.rpc.KeybindId +import solarxr_protocol.rpc.ResetType + +private const val KEYBIND_SOURCE = "Keybind" + +private val FEET_BODY_PARTS = listOf(BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT) + +private fun currentKeybinds(appContext: AppContextProvider): List = appContext.config.settings.context.state.value.data.keybinds + +private fun bindingsFlow(appContext: AppContextProvider): Flow> = appContext.config.settings.context.state + .map { it.data.keybinds } + .distinctUntilChangedBy { keybinds -> keybinds.map { it.id to it.binding } } + +fun keybindLabel(id: KeybindId): String = id.name + .split('_') + .joinToString(" ") { word -> word.lowercase().replaceFirstChar { it.uppercase() } } + +private fun toJIntellitypeAccelerator(binding: String): String = binding + .split('+') + .joinToString("+") { if (it == "SUPER") "WIN" else it } + +private fun triggerKeybind(appContext: AppContextProvider, scope: CoroutineScope, id: KeybindId) { + val delaySeconds = currentKeybinds(appContext).find { it.id == id }?.delay ?: 0f + scope.safeLaunch { + when (id) { + KeybindId.FULL_RESET -> + appContext.resetsManager.scheduleReset(KEYBIND_SOURCE, ResetType.FULL, delaySeconds) + + KeybindId.YAW_RESET -> + appContext.resetsManager.scheduleReset(KEYBIND_SOURCE, ResetType.YAW, delaySeconds) + + KeybindId.MOUNTING_RESET -> + appContext.resetsManager.scheduleReset(KEYBIND_SOURCE, ResetType.MOUNTING, delaySeconds) + + KeybindId.FEET_MOUNTING_RESET -> + appContext.resetsManager.scheduleReset(KEYBIND_SOURCE, ResetType.MOUNTING, delaySeconds, FEET_BODY_PARTS) + + KeybindId.PAUSE_TRACKING -> { + if (delaySeconds > 0f) delay((delaySeconds * 1000).toLong()) + val skeleton = appContext.skeleton.context + skeleton.dispatch(SkeletonActions.PauseTracking(!skeleton.state.value.paused)) + } + + KeybindId.NONE -> Unit + } + } +} + +suspend fun createDesktopKeybindManager(appContext: AppContextProvider, scope: CoroutineScope) { + when (CURRENT_PLATFORM) { + Platform.WINDOWS -> setupWindowsKeybinds(appContext, scope) + Platform.LINUX -> setupLinuxKeybinds(appContext, scope) + else -> AppLogger.keybind.info("Keybinds are not supported on $CURRENT_PLATFORM") + } +} + +private suspend fun setupWindowsKeybinds(appContext: AppContextProvider, scope: CoroutineScope) { + val instance = try { + JIntellitype.getInstance() + } catch (e: Throwable) { + AppLogger.keybind.error(e, "Failed to initialize JIntellitype, keybinds will be disabled") + return + } + + instance.addHotKeyListener { identifier -> + KeybindId.fromValue(identifier.toUByte())?.let { triggerKeybind(appContext, scope, it) } + } + + var registered: List = emptyList() + bindingsFlow(appContext).onEach { keybinds -> + registered.forEach { runCatching { instance.unregisterHotKey(it.id.value.toInt()) } } + keybinds.forEach { keybind -> + runCatching { instance.registerHotKey(keybind.id.value.toInt(), toJIntellitypeAccelerator(keybind.binding)) } + .onFailure { AppLogger.keybind.warn("Failed to bind ${keybind.id.name} to ${keybind.binding}") } + } + registered = keybinds + }.launchIn(scope) +} + +private suspend fun setupLinuxKeybinds(appContext: AppContextProvider, scope: CoroutineScope) { + var handler: GlobalShortcutsHandler? = null + + // Closing a handler disconnects its dbus connection, so every session gets its own manager + suspend fun bind(keybinds: List) { + runCatching { handler?.close() } + val shortcuts = keybinds.map { keybind -> + ShortcutTuple(keybind.id.name, Shortcut(keybindLabel(keybind.id), keybind.binding).shortcut) + }.toMutableList() + + handler = runCatching { + PortalManager(SLIMEVR_IDENTIFIER).globalShortcutsRequest(shortcuts).apply { + onShortcutActivated = { shortcutId -> + KeybindId.entries.firstOrNull { it.name == shortcutId }?.let { triggerKeybind(appContext, scope, it) } + } + } + }.onFailure { + AppLogger.keybind.error(it, "Failed to register global shortcuts, keybinds will not work") + }.getOrNull() + } + + val gnomeAppId = if (isGnome()) resolveGnomeAppId() else null + if (gnomeAppId == null) { + // The compositor owns the triggers and we have no way to change them, so bind once and + // leave it alone. The user rebinds from their system settings instead. + bind(currentKeybinds(appContext)) + } else { + // GNOME keeps the triggers in dconf and only reads them when a session is created, so a + // change means rewriting dconf and starting a new session. Its stored values also win over + // the ones we pass, so push the config on startup too or the gui would disagree with what + // actually fires. The exception is a first run, where the portal seeds them and prompts. + val needsSeeding = !gnomeShortcutsExist(gnomeAppId) + + var isInitialBind = true + bindingsFlow(appContext).onEach { keybinds -> + if (!(isInitialBind && needsSeeding)) { + writeGnomeShortcuts(gnomeAppId, keybinds) + } + bind(keybinds) + isInitialBind = false + }.launchIn(scope) + } + + scope.safeLaunch { + try { + awaitCancellation() + } finally { + runCatching { handler?.close() } + } + } +} diff --git a/solarxr-protocol b/solarxr-protocol index 2f00c44f4..c41224149 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 2f00c44f448b14b4be2f1ef7b3fda4e0cb4353cf +Subproject commit c41224149617edb21bcd231c14a82a589670cdc9