2023-10-31 17:14:30 -04:00
/ *
* SPDX - License - Identifier : GPL - 3.0
* Vesktop , a desktop app aiming to give you a snappier Discord Experience
* Copyright ( c ) 2023 Vendicated and Vencord contributors
* /
import { BrowserWindow , dialog } from "electron" ;
2023-12-22 08:56:11 -05:00
import { writeFile } from "fs/promises" ;
import { join } from "path" ;
2023-10-31 17:14:30 -04:00
import { MessageBoxChoice } from "../constants" ;
2024-01-07 02:26:18 +01:00
import { State } from "../settings" ;
2023-10-31 17:14:30 -04:00
// Bump this to re-show the prompt
2023-11-11 20:28:57 -05:00
const layoutVersion = 2 ;
2023-10-31 17:14:30 -04:00
// Get this from "show details" on the profile after exporting as a shared personal layout or using share with community
2023-11-11 20:28:57 -05:00
const layoutId = "3080264545" ; // Vesktop Layout v2
2023-10-31 17:14:30 -04:00
const numberRegex = /^[0-9]*$/ ;
2023-12-28 00:38:31 +00:00
let steamPipeQueue = Promise . resolve ( ) ;
2023-10-31 17:14:30 -04:00
export const isDeckGameMode = process . env . SteamOS === "1" && process . env . SteamGamepadUI === "1" ;
export function applyDeckKeyboardFix() {
if ( ! isDeckGameMode ) return ;
// Prevent constant virtual keyboard spam that eventually crashes Steam.
process . env . GTK_IM_MODULE = "None" ;
}
// For some reason SteamAppId is always 0 for non-steam apps so we do this insanity instead.
function getAppId ( ) : string | null {
// /home/deck/.local/share/Steam/steamapps/shadercache/APPID/fozmediav1
const path = process . env . STEAM_COMPAT_MEDIA_PATH ;
if ( ! path ) return null ;
const pathElems = path ? . split ( "/" ) ;
const appId = pathElems [ pathElems . length - 2 ] ;
if ( appId . match ( numberRegex ) ) {
console . log ( ` Got Steam App ID ${ appId } ` ) ;
return appId ;
}
return null ;
}
2023-12-28 00:38:31 +00:00
export function execSteamURL ( url : string ) {
2023-12-22 08:56:11 -05:00
// This doesn't allow arbitrary execution despite the weird syntax.
2023-12-28 00:38:31 +00:00
steamPipeQueue = steamPipeQueue . then ( ( ) = >
writeFile (
join ( process . env . HOME || "/home/deck" , ".steam" , "steam.pipe" ) ,
// replace ' to prevent argument injection
` ' ${ process . env . HOME } /.local/share/Steam/ubuntu12_32/steam' '-ifrunning' ' ${ url . replaceAll ( "'" , "%27" ) } ' \ n ` ,
"utf-8"
)
2023-12-22 08:56:11 -05:00
) ;
}
2023-12-28 00:38:31 +00:00
export function steamOpenURL ( url : string ) {
execSteamURL ( ` steam://openurl/ ${ url } ` ) ;
2023-12-22 08:56:11 -05:00
}
export async function showGamePage() {
const appId = getAppId ( ) ;
if ( ! appId ) return ;
await execSteamURL ( ` steam://nav/games/details/ ${ appId } ` ) ;
2023-10-31 17:14:30 -04:00
}
async function showLayout ( appId : string ) {
2023-12-22 08:56:11 -05:00
execSteamURL ( ` steam://controllerconfig/ ${ appId } / ${ layoutId } ` ) ;
2023-10-31 17:14:30 -04:00
}
export async function askToApplySteamLayout ( win : BrowserWindow ) {
const appId = getAppId ( ) ;
if ( ! appId ) return ;
2024-01-07 02:26:18 +01:00
if ( State . store . steamOSLayoutVersion === layoutVersion ) return ;
const update = Boolean ( State . store . steamOSLayoutVersion ) ;
2023-10-31 17:14:30 -04:00
// Touch screen breaks in some menus when native touch mode is enabled on latest SteamOS beta, remove most of the update specific text once that's fixed.
const { response } = await dialog . showMessageBox ( win , {
message : ` ${ update ? "Update" : "Apply" } Vesktop Steam Input Layout? ` ,
detail : ` Would you like to ${ update ? "Update" : "Apply" } Vesktop's recommended Steam Deck controller settings?
$ { update ? "Click yes using the touchpad" : "Tap yes" } , then press the X button or tap Apply Layout to confirm . $ {
update ? " Doing so will undo any customizations you have made." : ""
}
$ { update ? "Click" : "Tap" } no to keep your current layout . ` ,
buttons : [ "Yes" , "No" ] ,
cancelId : MessageBoxChoice.Cancel ,
defaultId : MessageBoxChoice.Default ,
type : "question"
} ) ;
2024-01-07 02:26:18 +01:00
if ( State . store . steamOSLayoutVersion !== layoutVersion ) {
State . store . steamOSLayoutVersion = layoutVersion ;
2023-10-31 17:14:30 -04:00
}
if ( response === MessageBoxChoice . Cancel ) return ;
await showLayout ( appId ) ;
}