Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

play: emulated display mode changes as a per-program setting (#72)

Gauthier Piarrette719465184ff2
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index 6151350..9404835 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -532,6 +532,7 @@ final class AppState {
}
func refresh() {
+ executableCache.removeAll()
if !prunedLogsThisRun {
prunedLogsThisRun = true
let n = LogPruner.prune(directory: paths.logs)
@@ -1099,7 +1100,16 @@ final class AppState {
}
/// The program an item runs: the largest .exe in a Steam or Epic game's folder, a pin's own.
+ /// Finding it walks the game's folder, so the answer is kept per item until the next
+ /// library refresh: views ask on every evaluation.
+ private var executableCache: [String: URL] = [:]
func programExecutable(for item: LibraryItem) -> URL? {
+ if let hit = executableCache[item.id] { return hit }
+ let found = findProgramExecutable(for: item)
+ if let found { executableCache[item.id] = found }
+ return found
+ }
+ private func findProgramExecutable(for item: LibraryItem) -> URL? {
guard let bottleName = item.bottleName, let bottle = bottles.first(where: { $0.name == bottleName }) else { return nil }
switch item.source {
case .steam:
@@ -1112,6 +1122,45 @@ final class AppState {
}
}
+ /// Emulated display mode changes for a program (`DisplayModeEmulation`): the bottle's
+ /// registry is the state, `registryVersion` makes views re-read it after a write.
+ private(set) var registryVersion = 0
+ /// user.reg is a megabyte in a used bottle; keep the answer until the file changes.
+ private var displayModeCache: [String: (modified: Date, on: Bool)] = [:]
+ func displayModeEmulation(in bottle: Bottle, executable exe: URL) -> Bool {
+ _ = registryVersion
+ let reg = bottle.url.appending(path: "user.reg")
+ let modified = (try? reg.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate) ?? .distantPast
+ let key = bottle.name + "|" + exe.lastPathComponent
+ if let hit = displayModeCache[key], hit.modified == modified { return hit.on }
+ let on = DisplayModeEmulation.isOn(in: bottle, executable: exe)
+ displayModeCache[key] = (modified, on)
+ return on
+ }
+ func displayModeEmulation(for item: LibraryItem) -> Bool? {
+ guard let bottleName = item.bottleName, let bottle = bottles.first(where: { $0.name == bottleName }),
+ let exe = programExecutable(for: item) else { return nil }
+ return displayModeEmulation(in: bottle, executable: exe)
+ }
+ func setDisplayModeEmulation(_ on: Bool, in bottle: Bottle, executable exe: URL) {
+ guard let engine = engine(for: bottle) else { return }
+ Task { @MainActor in
+ let runner = WineRunner(paths: paths, engine: engine, bottle: bottle)
+ do {
+ try await DisplayModeEmulation.set(on, in: runner, executable: exe)
+ appendLog("\(exe.lastPathComponent): emulated display mode changes \(on ? "on" : "off") in \(bottle.name); the next launch uses it.")
+ } catch {
+ fail(error, bottle: bottle)
+ }
+ registryVersion += 1
+ }
+ }
+ func setDisplayModeEmulation(_ on: Bool, for item: LibraryItem) {
+ guard let bottleName = item.bottleName, let bottle = bottles.first(where: { $0.name == bottleName }),
+ let exe = programExecutable(for: item) else { return }
+ setDisplayModeEmulation(on, in: bottle, executable: exe)
+ }
+
/// Whether the item's program needs Direct3D 12 (`ProgramNeeds.wantsDirect3D12`), remembered
/// per executable and modification date: finding the executable walks the game's folder.
private var direct3D12Cache: [String: (exe: URL, modified: Date, verdict: Bool)] = [:]
diff --git a/Sources/HighballApp/BottleView.swift b/Sources/HighballApp/BottleView.swift
index 53b4b0a..9d0db1a 100644
--- a/Sources/HighballApp/BottleView.swift
+++ b/Sources/HighballApp/BottleView.swift
@@ -595,6 +595,16 @@ struct PinSettingsSheet: View {
}
}
.pickerStyle(.menu)
+ // The registry is the state for this one, so it applies as soon as it is toggled,
+ // not on Save: the same value a database fix writes.
+ VStack(alignment: .leading, spacing: 4) {
+ Toggle(L("Emulate display mode changes"), isOn: Binding(
+ get: { state.displayModeEmulation(in: bottle, executable: pin.executableURL(driveC: bottle.driveC)) },
+ set: { state.setDisplayModeEmulation($0, in: bottle, executable: pin.executableURL(driveC: bottle.driveC)) }))
+ .toggleStyle(.checkbox)
+ Text(L("For a game that opens small, off-centre, or refuses its fullscreen mode. The Mac cannot switch its display for it, so Wine pretends and scales the picture instead. A fix from the database may have set this already."))
+ .font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
+ }
HStack {
Spacer()
Button(L("Cancel")) { dismiss() }
diff --git a/Sources/HighballApp/GameDetailView.swift b/Sources/HighballApp/GameDetailView.swift
index fe0d599..a26ceb6 100644
--- a/Sources/HighballApp/GameDetailView.swift
+++ b/Sources/HighballApp/GameDetailView.swift
@@ -230,6 +230,18 @@ struct GameDetailView: View {
GraphicsModePicker(bottle: bottle)
}
}
+ if let emulated = state.displayModeEmulation(for: item) {
+ HStack(alignment: .top, spacing: 12) {
+ Text(L("Display mode")).font(.caption).foregroundStyle(.secondary).frame(width: 110, alignment: .leading).padding(.top, 2)
+ VStack(alignment: .leading, spacing: 4) {
+ Toggle(L("Emulate display mode changes"), isOn: Binding(
+ get: { emulated }, set: { state.setDisplayModeEmulation($0, for: item) }))
+ .toggleStyle(.checkbox)
+ Text(L("For a game that opens small, off-centre, or refuses its fullscreen mode. The Mac cannot switch its display for it, so Wine pretends and scales the picture instead. A fix from the database may have set this already."))
+ .font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
+ }
+ }
+ }
row(L("Engine"), (state.engine(for: bottle)?.displayName).map { "\($0) · \(bottle.settings.engineID)" } ?? bottle.settings.engineID)
HStack(alignment: .firstTextBaseline, spacing: 12) {
Text(L("Environment")).font(.caption).foregroundStyle(.secondary).frame(width: 110, alignment: .leading)
diff --git a/Sources/HighballApp/L10n.swift b/Sources/HighballApp/L10n.swift
index c8e3d65..11cbd19 100644
--- a/Sources/HighballApp/L10n.swift
+++ b/Sources/HighballApp/L10n.swift
@@ -140,6 +140,9 @@ enum L10n {
"Highball keeps its engines and environments in %@, and that location is not available right now. Connect the drive and relaunch, or go back to the default location on this Mac.": "Highball garde ses moteurs et environnements dans %@, et cet emplacement n'est pas disponible pour le moment. Branchez le disque et relancez, ou revenez à l'emplacement par défaut sur ce Mac.",
"Engines, environments and downloads copy to %@, get checked, and are then removed from %@. Highball relaunches when it is done. Nothing is removed until the copy checks out.": "Les moteurs, environnements et téléchargements sont copiés vers %@, vérifiés, puis supprimés de %@. Highball se relance ensuite. Rien n'est supprimé avant vérification de la copie.",
"Mode for this game": "Mode pour ce jeu",
+ "Display mode": "Mode d'affichage",
+ "Emulate display mode changes": "Émuler les changements de mode d'affichage",
+ "For a game that opens small, off-centre, or refuses its fullscreen mode. The Mac cannot switch its display for it, so Wine pretends and scales the picture instead. A fix from the database may have set this already.": "Pour un jeu qui s'ouvre en petit, décentré, ou refuse son mode plein écran. Le Mac ne peut pas changer l'affichage pour lui, alors Wine fait comme si et met l'image à l'échelle. Un correctif de la base de données l'a peut-être déjà activé.",
"Environment's mode": "Mode de l'environnement",
"Use %@ for this game": "Utiliser %@ pour ce jeu",
"Try %@ for %@ next time?": "Essayer %@ pour %@ la prochaine fois ?",
diff --git a/Sources/HighballKit/DisplayModeEmulation.swift b/Sources/HighballKit/DisplayModeEmulation.swift
new file mode 100644
index 0000000..ec273ff
--- /dev/null
+++ b/Sources/HighballKit/DisplayModeEmulation.swift
@@ -0,0 +1,42 @@
+import Foundation
+
+/// Wine's emulated display mode changes, per program. A game that asks Windows for a fullscreen
+/// mode the Mac cannot switch to gets "success" while the display stays as it is, so it opens
+/// small, off-centre, with black bars, or refuses to start; with EmulateModeset Wine pretends
+/// the change happened and scales the window to the real screen. win32u reads the value under
+/// `Software\Wine\X11 Driver` whatever the display driver (the Mac driver included, measured
+/// in the engine's win32u.so), and `AppDefaults\<exe>` scopes it to one program, which is what
+/// the Five Nights at Freddy's recipe writes and what was verified by eye. Never a default: games
+/// that switch modes for real exist, so the value is set per program, by a recipe or by hand.
+/// The bottle's registry is the only state; nothing is stored beside it.
+public enum DisplayModeEmulation {
+ public static let valueName = "EmulateModeset"
+
+ /// The per-program key, as `reg add` wants it.
+ public static func key(forExecutable exe: URL) -> String {
+ "HKCU\\Software\\Wine\\AppDefaults\\\(exe.lastPathComponent)\\X11 Driver"
+ }
+
+ /// Whether the bottle turns it on for `exe`, read from user.reg without running Wine.
+ public static func isOn(in bottle: Bottle, executable exe: URL) -> Bool {
+ guard let text = try? String(contentsOf: bottle.url.appending(path: "user.reg"), encoding: .utf8) else { return false }
+ return isOn(userReg: text, executableName: exe.lastPathComponent)
+ }
+
+ /// Pure: the registry text says `"EmulateModeset"="y"` under the program's X11 Driver key.
+ public static func isOn(userReg: String, executableName: String) -> Bool {
+ let raw = RegistryText.value(in: userReg, key: "Software\\Wine\\AppDefaults\\\(executableName)\\X11 Driver", name: valueName) ?? ""
+ return raw.lowercased() == "\"y\""
+ }
+
+ /// Turns it on or off for `exe` through the bottle's Wine. Programs read the registry when
+ /// they start, so a running Steam client needs no restart for the next launch to see it.
+ public static func set(_ on: Bool, in runner: WineRunner, executable exe: URL) async throws {
+ let key = key(forExecutable: exe)
+ if on {
+ try await runner.regAdd(key: key, name: valueName, type: "REG_SZ", data: "y")
+ } else {
+ try await runner.regDelete(key: key, name: valueName)
+ }
+ }
+}
diff --git a/Tests/HighballKitTests/DisplayModeEmulationTests.swift b/Tests/HighballKitTests/DisplayModeEmulationTests.swift
new file mode 100644
index 0000000..644b303
--- /dev/null
+++ b/Tests/HighballKitTests/DisplayModeEmulationTests.swift
@@ -0,0 +1,46 @@
+import XCTest
+@testable import HighballKit
+
+/// Emulated display mode changes are one registry value per program, the same one the Five
+/// Nights at Freddy's fix writes; the toggle reads and writes nothing else.
+final class DisplayModeEmulationTests: XCTestCase {
+ func testKeyIsThePerProgramX11DriverKey() {
+ let exe = URL(fileURLWithPath: "/b/drive_c/Program Files (x86)/Steam/steamapps/common/Five Nights at Freddy's/FiveNightsatFreddys.exe")
+ XCTAssertEqual(DisplayModeEmulation.key(forExecutable: exe), "HKCU\\Software\\Wine\\AppDefaults\\FiveNightsatFreddys.exe\\X11 Driver")
+ }
+
+ func testReadsTheValueTheRecipeWrites() {
+ let reg = """
+ [Software\\\\Wine\\\\AppDefaults\\\\DarkOmen.exe\\\\X11 Driver] 1789674692
+ #time=1dd46ddf05dfeaa
+ "EmulateModeset"="y"
+
+ [Software\\\\Wine\\\\AppDefaults\\\\deskjob.exe\\\\DllOverrides] 1787678145
+ "d3d9"="native,builtin"
+
+ [Software\\\\Wine\\\\AppDefaults\\\\Other.exe\\\\X11 Driver] 1789141270
+ "EmulateModeset"="n"
+ """
+ XCTAssertTrue(DisplayModeEmulation.isOn(userReg: reg, executableName: "DarkOmen.exe"))
+ XCTAssertTrue(DisplayModeEmulation.isOn(userReg: reg, executableName: "darkomen.exe"), "the registry is case-insensitive")
+ XCTAssertFalse(DisplayModeEmulation.isOn(userReg: reg, executableName: "Other.exe"), "an explicit n is off")
+ XCTAssertFalse(DisplayModeEmulation.isOn(userReg: reg, executableName: "deskjob.exe"), "a program with other AppDefaults only")
+ XCTAssertFalse(DisplayModeEmulation.isOn(userReg: reg, executableName: "Missing.exe"))
+ }
+
+ func testAMissingRegistryIsOff() {
+ let bottle = Bottle(url: URL(fileURLWithPath: "/tmp/hb-no-such-bottle-\(UUID().uuidString)"), settings: BottleSettings(name: "t", engineID: "e"))
+ XCTAssertFalse(DisplayModeEmulation.isOn(in: bottle, executable: URL(fileURLWithPath: "/x/Game.exe")))
+ }
+}
+
+extension DisplayModeEmulationTests {
+ /// The real Gaming bottle on this Mac: the FNAF recipe set the value, PEAK never had it.
+ func testTheRealBottleReadsTheRecipeValue() throws {
+ let home = URL(fileURLWithPath: NSHomeDirectory()).appending(path: "Library/Application Support/Highball/bottles/Gaming")
+ try XCTSkipUnless(FileManager.default.fileExists(atPath: home.appending(path: "user.reg").path), "no Gaming bottle here")
+ let bottle = Bottle(url: home, settings: BottleSettings(name: "Gaming", engineID: "e"))
+ XCTAssertTrue(DisplayModeEmulation.isOn(in: bottle, executable: URL(fileURLWithPath: "/x/FiveNightsatFreddys.exe")))
+ XCTAssertFalse(DisplayModeEmulation.isOn(in: bottle, executable: URL(fileURLWithPath: "/x/PEAK.exe")))
+ }
+}