-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathSteamDeckUtil.java
More file actions
115 lines (94 loc) · 4.32 KB
/
Copy pathSteamDeckUtil.java
File metadata and controls
115 lines (94 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package dev.isxander.controlify.driver.steamdeck;
import dev.isxander.controlify.debug.DebugProperties;
import dev.isxander.controlify.utils.CUtil;
import dev.isxander.controlify.utils.log.ControlifyLogger;
import dev.isxander.deckapi.api.SteamDeck;
import dev.isxander.deckapi.api.SteamDeckException;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.Stream;
public final class SteamDeckUtil {
private static final ControlifyLogger logger = CUtil.LOGGER.createSubLogger("SteamDeckUtil");
private static @Nullable SteamDeck deckInstance;
private static boolean triedToLoad = false;
public static final boolean IS_STEAM_DECK = isHardwareSteamDeck();
public static final SteamDeckMode DECK_MODE = getSteamDeckMode();
// flatpak sets env variable 'container' when containerised
// https://stackoverflow.com/a/75284996
public static final boolean IS_SANDBOXED = "1".equals(System.getenv("container"));
public static final Identifier STEAM_DECK_NAMESPACE = CUtil.rl("steam_deck");
public static Optional<SteamDeck> getDeckInstance() {
if (triedToLoad) {
return Optional.ofNullable(deckInstance);
}
triedToLoad = true;
if (!DECK_MODE.isGamingMode() && DebugProperties.STEAM_DECK_CUSTOM_CEF_URL == null) {
logger.warn("Device is not a Steam Deck or not in gaming mode, skipping Steam Deck driver initialization.");
return Optional.empty();
}
try {
String url = DebugProperties.STEAM_DECK_CUSTOM_CEF_URL;
if (url == null) url = "http://127.0.0.1:8080";
deckInstance = SteamDeck.create(url);
} catch (SteamDeckException e) {
logger.error("Failed to create SteamDeck instance", e);
deckInstance = null;
}
return Optional.ofNullable(deckInstance);
}
private static boolean isHardwareSteamDeck() {
if (true) {
logger.error("Skipping Steam Deck checks as steamOS has temporarily broken the enhanced driver.");
return false;
}
logger.debugLog("Checking if hardware is Steam Deck.");
// even if "Linux" isn't a defacto way to check for all linux distros, it's the value returned on a steam deck
String platformName = System.getProperty("os.name");
logger.debugLog("os.name: {}", platformName);
boolean isLinux = "Linux".equals(platformName);
if (!isLinux) return false;
String kernelVersion = System.getProperty("os.version");
logger.debugLog("os.version: {}", kernelVersion);
// steam decks use a special kernel from valve
// this check is not used because i believe the board information is more reliable
// as i'm not sure if it's common for people to use other kernels on steam decks
boolean valveKernel = kernelVersion.contains("valve");
if (valveKernel) logger.debugLog("Detected valve kernel.");
String boardVendor = readFile("/sys/class/dmi/id/board_vendor");
if (boardVendor == null) return false;
logger.debugLog("Board vendor: {}", boardVendor);
String boardName = readFile("/sys/class/dmi/id/board_name");
if (boardName == null) return false;
logger.debugLog("Board name: {}", boardName);
var validBoardNames = Stream.of(
"Jupiter", // LCD
"Galileo" // OLED
);
// Jupiter is the codename for the steam deck
return boardVendor.contains("Valve") && validBoardNames.anyMatch(boardName::contains);
}
private static SteamDeckMode getSteamDeckMode() {
if (IS_STEAM_DECK) {
String steamDeck = System.getenv("SteamDeck");
// only set if in gaming mode
if (steamDeck != null && steamDeck.equals("1")) {
return SteamDeckMode.GAMING_MODE;
} else {
return SteamDeckMode.DESKTOP_MODE;
}
} else {
return SteamDeckMode.NOT_STEAM_DECK;
}
}
private static String readFile(String path) {
try {
return Files.readString(Paths.get(path));
} catch (IOException e) {
return null;
}
}
}