From 7f3856fa66953609ee26a4ed7e72c56d63ee58b5 Mon Sep 17 00:00:00 2001 From: Laurin Neff Date: Thu, 25 Nov 2021 17:19:11 +0100 Subject: [PATCH] Add hybrid GPU integration --- package.json | 1 + public/html/settings.html | 20 ++++++++++++ public/locales/de.json | 1 + public/locales/en-us.json | 1 + public/locales/en.json | 1 + public/locales/es.json | 1 + public/locales/fr.json | 1 + public/locales/id.json | 1 + public/locales/ja.json | 1 + public/locales/ko.json | 1 + public/locales/pt.json | 1 + public/locales/ru.json | 1 + public/locales/th.json | 1 + public/locales/vi.json | 1 + public/locales/zh-cn.json | 1 + public/locales/zh-tw.json | 1 + src/ts/index.ts | 11 +++++++ src/ts/lib/LauncherLib.ts | 1 + src/ts/lib/SwitcherooControl.ts | 54 +++++++++++++++++++++++++++++++++ src/ts/settings.ts | 21 +++++++++++++ 20 files changed, 122 insertions(+) create mode 100644 src/ts/lib/SwitcherooControl.ts diff --git a/package.json b/package.json index f870bfd..bef54dc 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "cash-dom": "^8.1.0", "command-exists": "^1.2.9", "discord-rpc": "^4.0.1", + "dbus-next": "^0.10.2", "electron-store": "^8.0.1", "follow-redirects": "^1.14.5", "get-pixels": "^3.3.3", diff --git a/public/html/settings.html b/public/html/settings.html index 8ea2633..bc19342 100644 --- a/public/html/settings.html +++ b/public/html/settings.html @@ -118,6 +118,26 @@
+
+ +
+ GPU + +
+
    +
  • Default
  • +
+
+ +
+ Default + + + + +

diff --git a/public/locales/de.json b/public/locales/de.json index 351e5aa..8a68d87 100644 --- a/public/locales/de.json +++ b/public/locales/de.json @@ -11,6 +11,7 @@ "Shaders": "Shadern", "ReshadeNotInstalled": "Sie haben vkBasalt und Reshade-Shader nicht installiert", "GameModeNotInstalled": "Sie haben GameMode nicht installiert", + "SwitcherooNotInstalled": "Sie haben switcheroo-control nicht installiert", "Author": "Autor", "NoImages": "Bilder sind nicht verfügbar", "SettingsTitle": "Einstellungen", diff --git a/public/locales/en-us.json b/public/locales/en-us.json index 77c8b2c..217de60 100644 --- a/public/locales/en-us.json +++ b/public/locales/en-us.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/en.json b/public/locales/en.json index ab04190..e4280cb 100644 --- a/public/locales/en.json +++ b/public/locales/en.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/es.json b/public/locales/es.json index ab04190..e4280cb 100644 --- a/public/locales/es.json +++ b/public/locales/es.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/fr.json b/public/locales/fr.json index ab04190..e4280cb 100644 --- a/public/locales/fr.json +++ b/public/locales/fr.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/id.json b/public/locales/id.json index e341ae9..d371bc4 100644 --- a/public/locales/id.json +++ b/public/locales/id.json @@ -11,6 +11,7 @@ "Shaders": "Shader", "ReshadeNotInstalled": "Kamu belum memasang vkBasalt dan library reshade-shaders", "GameModeNotInstalled": "Kamu belum memasang GameMode", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Pembuat", "NoImages": "Tidak ada gambar ditambahkan", "SettingsTitle": "Pengaturan", diff --git a/public/locales/ja.json b/public/locales/ja.json index 16c5747..19b733e 100644 --- a/public/locales/ja.json +++ b/public/locales/ja.json @@ -11,6 +11,7 @@ "Shaders": "シェーダ", "ReshadeNotInstalled": "vkBasaltとreshade-shadersライブラリをインストールしていません", "GameModeNotInstalled": "GameModeドがインストールされていません", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "著者", "NoImages": "画像は追加されていません", "SettingsTitle": "設定", diff --git a/public/locales/ko.json b/public/locales/ko.json index 177bc45..b2c7478 100644 --- a/public/locales/ko.json +++ b/public/locales/ko.json @@ -11,6 +11,7 @@ "Shaders": "쉐이더", "ReshadeNotInstalled": "vkBasalt 및 reshade-shaders 라이브러리를 설치하지 않았습니다", "GameModeNotInstalled": "GameMode 가 설치되어 있지 않습니다", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "저자", "NoImages": "추가 된 이미지 없음", "SettingsTitle": "설정", diff --git a/public/locales/pt.json b/public/locales/pt.json index ab04190..e4280cb 100644 --- a/public/locales/pt.json +++ b/public/locales/pt.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/ru.json b/public/locales/ru.json index a276142..01e5db3 100644 --- a/public/locales/ru.json +++ b/public/locales/ru.json @@ -11,6 +11,7 @@ "Shaders": "Шейдеры", "ReshadeNotInstalled": "Вы не установили vkBasalt и библиотеку reshade-shaders", "GameModeNotInstalled": "Вы не установили GameMode", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Автор", "NoImages": "Изображения не добавлены", "SettingsTitle": "Настройки", diff --git a/public/locales/th.json b/public/locales/th.json index ab04190..e4280cb 100644 --- a/public/locales/th.json +++ b/public/locales/th.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/vi.json b/public/locales/vi.json index ab04190..e4280cb 100644 --- a/public/locales/vi.json +++ b/public/locales/vi.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "Settings", diff --git a/public/locales/zh-cn.json b/public/locales/zh-cn.json index 5947d7e..a661f59 100644 --- a/public/locales/zh-cn.json +++ b/public/locales/zh-cn.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "设置", diff --git a/public/locales/zh-tw.json b/public/locales/zh-tw.json index 3b3b5c0..0e43f5e 100644 --- a/public/locales/zh-tw.json +++ b/public/locales/zh-tw.json @@ -11,6 +11,7 @@ "Shaders": "Shaders", "ReshadeNotInstalled": "You haven't installed vkBasalt and reshade-shaders library", "GameModeNotInstalled": "You don't have GameMode installed", + "SwitcherooNotInstalled": "You don't have switcheroo-control installed", "Author": "Author", "NoImages": "No images added", "SettingsTitle": "設定", diff --git a/src/ts/index.ts b/src/ts/index.ts index 173aa18..86e8bef 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -12,6 +12,7 @@ import LauncherLib from './lib/LauncherLib'; import LauncherUI from './lib/LauncherUI'; import Tools from './lib/Tools'; import DiscordRPC from './lib/DiscordRPC'; +import SwitcherooControl from './lib/SwitcherooControl'; const launcher_version = require('../../package.json').version; @@ -186,6 +187,16 @@ $(() => { if (LauncherLib.getConfig('gamemode')) command = `gamemoderun ${command}`; + // GPU + if (LauncherLib.getConfig('gpu') != 'default') { + const gpu = await SwitcherooControl.getGpuByName(LauncherLib.getConfig('gpu')); + if (gpu) { + env = { ...env, ...SwitcherooControl.getEnvAsObject(gpu) }; + } else { + console.warn(`GPU ${LauncherLib.getConfig('gpu')} not found. Launching on the default GPU.`); + } + } + // Starting the game const startTime = Date.now(); diff --git a/src/ts/lib/LauncherLib.ts b/src/ts/lib/LauncherLib.ts index 6e33de5..fca2e0a 100644 --- a/src/ts/lib/LauncherLib.ts +++ b/src/ts/lib/LauncherLib.ts @@ -33,6 +33,7 @@ const config = new store ({ hud: 'none', // none / dxvk / mangohud shaders: 'none', // none / shader's folder gamemode: false, + gpu: 'default', // Version of the game we asked about analytics last time, // or null if user said don't ask him again diff --git a/src/ts/lib/SwitcherooControl.ts b/src/ts/lib/SwitcherooControl.ts new file mode 100644 index 0000000..1c057b9 --- /dev/null +++ b/src/ts/lib/SwitcherooControl.ts @@ -0,0 +1,54 @@ +import * as dbus from 'dbus-next'; +const bus = dbus.systemBus(); + +let switcherooProxy: dbus.ProxyObject, + switcherooRunning: boolean; + +bus.getProxyObject('net.hadess.SwitcherooControl', '/net/hadess/SwitcherooControl').then(proxy => { + switcherooProxy = proxy; + switcherooRunning = true; +}, () => switcherooRunning = false); + +interface GPU { + Default: dbus.Variant, + Environment: dbus.Variant, + Name: dbus.Variant +} + +export default class SwitcherooControl { + public static waitReady() { + const poll = (resolve: () => void, reject: () => void) => { + if (switcherooRunning === false) reject(); + if (switcherooProxy) resolve(); + else setTimeout(poll, 100, resolve, reject); + }; + + return new Promise(poll) + } + + public static getGpus() { + if (!switcherooRunning) return null; + + const properties = switcherooProxy.getInterface("org.freedesktop.DBus.Properties"); + const gpus: Promise> = properties.Get('net.hadess.SwitcherooControl', 'GPUs'); + return gpus; + } + + public static async getGpuByName(name: string) { + const gpus = await SwitcherooControl.getGpus(); + if (!gpus) return null; + + return gpus.value.find(gpu => gpu.Name.value === name); + } + + //switcheroo-control returns env vars as an array like this: ["NAME1", "VALUE1", "NAME2", "VALUE2"], but we want them as an object + public static getEnvAsObject(gpu: GPU) { + const env: any = {}; + + for (let i = 0; i < gpu.Environment.value.length; i += 2) { + env[gpu.Environment.value[i]] = gpu.Environment.value[i + 1]; + } + + return env; + } +} \ No newline at end of file diff --git a/src/ts/settings.ts b/src/ts/settings.ts index dc4e7e2..9765a3c 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -12,6 +12,8 @@ import LauncherLib from './lib/LauncherLib'; import LauncherUI from './lib/LauncherUI'; import Tools from './lib/Tools'; +import SwitcherooControl from './lib/SwitcherooControl'; + $(() => { // Make sure settings is shown in correct language. LauncherUI.updateLang(LauncherLib.getConfig('lang.launcher') ?? 'en-us'); @@ -130,6 +132,25 @@ $(() => { LauncherLib.updateConfig('gamemode', $('#gamemode').hasClass('checkbox-active')); }); + SwitcherooControl.waitReady().then(async () => { + const gpus = await SwitcherooControl.getGpus() + if (gpus) { + console.log(gpus); + for (const gpu of gpus.value) { + $(`
  • ${gpu.Name.value}
  • `).appendTo("#gpu .select-options ul"); + } + } + }, () => { + console.log("switcheroo-control not running"); + $("#gpu .selected-item") + .addClass("hint--top hint--medium") + .attr("data-hint", LauncherUI.i18n.translate("SwitcherooNotInstalled")); + }); + + $('#gpu').on('selectionChanged', (e, data: any) => { + LauncherLib.updateConfig('gpu', data.value); + }) + /** * Shaders */