mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Add Iris Studio app
- Add the Electron studio (electron-vite, React, three) - Ignore node_modules and build output under studio/
This commit is contained in:
1 parent
832070cd69
commit
564dcdcc72
252 files changed
+24496
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
const NVIDIA_VENDOR_ID = '0x10de'
|
||||
const DRM_FOLDER = '/sys/class/drm'
|
||||
const RENDER_NODE_PREFIX = 'renderD'
|
||||
// Where the Vulkan loader looks for drivers, NixOS's first.
|
||||
const VULKAN_DRIVER_FOLDERS = ['/run/opengl-driver/share/vulkan/icd.d', '/usr/share/vulkan/icd.d', '/etc/vulkan/icd.d']
|
||||
const NVIDIA_VULKAN_DRIVER_PREFIX = 'nvidia_icd'
|
||||
const VULKAN_DRIVER_VARIABLES = ['VK_DRIVER_FILES', 'VK_ICD_FILENAMES']
|
||||
|
||||
/**
|
||||
* Chromium on Wayland hands the desktop buffers made on the GPU it draws with, and a desktop that composites on the
|
||||
* integrated GPU cannot show buffers of an NVIDIA GPU: the window stays empty. Through XWayland with Chromium drawing
|
||||
* on Vulkan, NVIDIA's Vulkan driver copies each frame to the integrated GPU itself, as it does for games.
|
||||
*/
|
||||
export const DEDICATED_GPU_SWITCHES = ['--ozone-platform=x11', '--use-angle=vulkan', '--enable-features=Vulkan,VulkanFromANGLE,DefaultANGLEVulkan']
|
||||
|
||||
/** NVIDIA's Vulkan driver file on a Linux machine with an NVIDIA GPU; null where there is no such GPU or driver. */
|
||||
export function dedicatedGpuDriver(): string | null {
|
||||
if (process.platform !== 'linux' || !hasNvidiaGpu()) {
|
||||
return null
|
||||
}
|
||||
for (const folder of VULKAN_DRIVER_FOLDERS) {
|
||||
const driver = driverIn(folder)
|
||||
if (driver !== null) {
|
||||
return driver
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** Shows Chromium only the given Vulkan driver, so it draws on that driver's GPU. */
|
||||
export function showOnlyVulkanDriver(env: NodeJS.ProcessEnv, driver: string): void {
|
||||
for (const variable of VULKAN_DRIVER_VARIABLES) {
|
||||
env[variable] = driver
|
||||
}
|
||||
}
|
||||
|
||||
export function showEveryVulkanDriver(env: NodeJS.ProcessEnv): void {
|
||||
for (const variable of VULKAN_DRIVER_VARIABLES) {
|
||||
delete env[variable]
|
||||
}
|
||||
}
|
||||
|
||||
export function runsOnDedicatedGpu(argv: string[]): boolean {
|
||||
return DEDICATED_GPU_SWITCHES.every((option) => argv.includes(option))
|
||||
}
|
||||
|
||||
/** The arguments with the dedicated GPU's switches in place of any the studio was started with under the same names. */
|
||||
export function withDedicatedGpuSwitches(argv: string[]): string[] {
|
||||
const names = DEDICATED_GPU_SWITCHES.map(switchName)
|
||||
return [...argv.filter((option) => !names.includes(switchName(option))), ...DEDICATED_GPU_SWITCHES]
|
||||
}
|
||||
|
||||
export function withoutDedicatedGpuSwitches(argv: string[]): string[] {
|
||||
return argv.filter((option) => !DEDICATED_GPU_SWITCHES.includes(option))
|
||||
}
|
||||
|
||||
function switchName(option: string): string {
|
||||
return option.split('=')[0]
|
||||
}
|
||||
|
||||
function hasNvidiaGpu(): boolean {
|
||||
try {
|
||||
return readdirSync(DRM_FOLDER)
|
||||
.filter((name) => name.startsWith(RENDER_NODE_PREFIX))
|
||||
.some((name) => readFileSync(join(DRM_FOLDER, name, 'device', 'vendor'), 'utf8').trim() === NVIDIA_VENDOR_ID)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function driverIn(folder: string): string | null {
|
||||
if (!existsSync(folder)) {
|
||||
return null
|
||||
}
|
||||
const file = readdirSync(folder).find((name) => name.startsWith(NVIDIA_VULKAN_DRIVER_PREFIX))
|
||||
return file === undefined ? null : join(folder, file)
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
import { app, BrowserWindow, clipboard, ipcMain, nativeTheme, shell } from 'electron'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises'
|
||||
import { homedir } from 'node:os'
|
||||
import { dirname, join } from 'node:path'
|
||||
import type { BridgeConnection, GraphicsInfo, StoredShortcuts, ThemeChoice } from '../shared/bridge'
|
||||
import {
|
||||
dedicatedGpuDriver,
|
||||
runsOnDedicatedGpu,
|
||||
showEveryVulkanDriver,
|
||||
showOnlyVulkanDriver,
|
||||
withDedicatedGpuSwitches,
|
||||
withoutDedicatedGpuSwitches
|
||||
} from './dedicatedGpu'
|
||||
|
||||
const CONNECTION_FOLDER = join(homedir(), '.iris', 'studio', 'bridges')
|
||||
const SETTINGS_FILE = 'studio.json'
|
||||
const SHORTCUTS_KEY = 'shortcuts'
|
||||
const DEDICATED_GPU_KEY = 'dedicatedGpu'
|
||||
const SETTINGS_INDENT = 2
|
||||
const LINK_PREFIX = 'iris-studio://'
|
||||
const DARK_BACKGROUND = '#1e1f22'
|
||||
const WINDOW_SIZE = { width: 1440, height: 880, minWidth: 820, minHeight: 520 }
|
||||
|
||||
let window: BrowserWindow | null = null
|
||||
let pendingLink: string | null = findLink(process.argv)
|
||||
const dedicatedGpu = dedicatedGpuDriver()
|
||||
|
||||
function findLink(argv: string[]): string | null {
|
||||
return argv.find((argument) => argument.startsWith(LINK_PREFIX)) ?? null
|
||||
}
|
||||
|
||||
function isRunning(pid: unknown): boolean {
|
||||
if (typeof pid !== 'number') {
|
||||
return true
|
||||
}
|
||||
try {
|
||||
process.kill(pid, 0)
|
||||
return true
|
||||
} catch (error) {
|
||||
return (error as NodeJS.ErrnoException).code === 'EPERM'
|
||||
}
|
||||
}
|
||||
|
||||
/** The bridges announced on this machine whose server still runs, newest first. */
|
||||
async function listBridges(): Promise<BridgeConnection[]> {
|
||||
let files: string[]
|
||||
try {
|
||||
files = (await readdir(CONNECTION_FOLDER)).filter((name) => name.endsWith('.json'))
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
const bridges = await Promise.all(files.map(readConnection))
|
||||
return bridges
|
||||
.filter((bridge): bridge is BridgeConnection => bridge !== null)
|
||||
.sort((first, second) => (second.startedMillis ?? 0) - (first.startedMillis ?? 0))
|
||||
}
|
||||
|
||||
async function readConnection(name: string): Promise<BridgeConnection | null> {
|
||||
try {
|
||||
const file = JSON.parse(await readFile(join(CONNECTION_FOLDER, name), 'utf8'))
|
||||
if (!isRunning(file.pid) || typeof file.port !== 'number' || typeof file.token !== 'string') {
|
||||
return null
|
||||
}
|
||||
return { world: String(file.world ?? ''), port: file.port, token: file.token, startedMillis: file.startedMillis }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function settingsPath(): string {
|
||||
return join(app.getPath('userData'), SETTINGS_FILE)
|
||||
}
|
||||
|
||||
async function readSettings(): Promise<Record<string, unknown>> {
|
||||
try {
|
||||
const settings: unknown = JSON.parse(await readFile(settingsPath(), 'utf8'))
|
||||
return typeof settings === 'object' && settings !== null && !Array.isArray(settings) ? (settings as Record<string, unknown>) : {}
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSetting(key: string, value: unknown): Promise<void> {
|
||||
try {
|
||||
const settings = { ...(await readSettings()), [key]: value }
|
||||
await mkdir(dirname(settingsPath()), { recursive: true })
|
||||
await writeFile(settingsPath(), JSON.stringify(settings, null, SETTINGS_INDENT))
|
||||
} catch (failure) {
|
||||
console.error(`Could not save the studio setting ${key} to ${settingsPath()}`, failure)
|
||||
}
|
||||
}
|
||||
|
||||
/** Read before the app is ready, as the GPU the studio draws on is chosen by how it was started. */
|
||||
function choosesDedicatedGpu(): boolean {
|
||||
try {
|
||||
const settings: unknown = JSON.parse(readFileSync(settingsPath(), 'utf8'))
|
||||
return typeof settings === 'object' && settings !== null && (settings as Record<string, unknown>)[DEDICATED_GPU_KEY] === true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function restartOnGpu(dedicated: boolean): void {
|
||||
const args = process.argv.slice(1)
|
||||
if (dedicated && dedicatedGpu !== null) {
|
||||
showOnlyVulkanDriver(process.env, dedicatedGpu)
|
||||
app.relaunch({ args: withDedicatedGpuSwitches(args) })
|
||||
} else {
|
||||
showEveryVulkanDriver(process.env)
|
||||
app.relaunch({ args: withoutDedicatedGpuSwitches(args) })
|
||||
}
|
||||
app.exit(0)
|
||||
}
|
||||
|
||||
async function useDedicatedGpu(dedicated: boolean): Promise<void> {
|
||||
await saveSetting(DEDICATED_GPU_KEY, dedicated)
|
||||
restartOnGpu(dedicated)
|
||||
}
|
||||
|
||||
function graphicsInfo(): GraphicsInfo {
|
||||
return { canUseDedicatedGpu: dedicatedGpu !== null, usesDedicatedGpu: runsOnDedicatedGpu(process.argv) }
|
||||
}
|
||||
|
||||
function createWindow(): void {
|
||||
window = new BrowserWindow({
|
||||
...WINDOW_SIZE,
|
||||
title: 'Iris Studio',
|
||||
backgroundColor: DARK_BACKGROUND,
|
||||
autoHideMenuBar: true,
|
||||
show: false,
|
||||
webPreferences: {
|
||||
preload: join(import.meta.dirname, '../preload/index.cjs'),
|
||||
sandbox: true,
|
||||
contextIsolation: true
|
||||
}
|
||||
})
|
||||
window.once('ready-to-show', () => window?.show())
|
||||
window.webContents.setWindowOpenHandler(({ url }) => {
|
||||
void shell.openExternal(url)
|
||||
return { action: 'deny' }
|
||||
})
|
||||
if (process.env.ELECTRON_RENDERER_URL) {
|
||||
void window.loadURL(process.env.ELECTRON_RENDERER_URL)
|
||||
} else {
|
||||
void window.loadFile(join(import.meta.dirname, '../renderer/index.html'))
|
||||
}
|
||||
}
|
||||
|
||||
function deliverLink(link: string): void {
|
||||
if (window === null) {
|
||||
pendingLink = link
|
||||
return
|
||||
}
|
||||
window.webContents.send('studio:link', link)
|
||||
if (window.isMinimized()) {
|
||||
window.restore()
|
||||
}
|
||||
window.focus()
|
||||
}
|
||||
|
||||
if (dedicatedGpu !== null && choosesDedicatedGpu() && !runsOnDedicatedGpu(process.argv)) {
|
||||
restartOnGpu(true)
|
||||
} else if (!app.requestSingleInstanceLock()) {
|
||||
app.quit()
|
||||
} else {
|
||||
app.setAsDefaultProtocolClient('iris-studio')
|
||||
app.on('second-instance', (_event, argv) => {
|
||||
const link = findLink(argv)
|
||||
if (link !== null) {
|
||||
deliverLink(link)
|
||||
} else {
|
||||
window?.focus()
|
||||
}
|
||||
})
|
||||
app.on('open-url', (event, url) => {
|
||||
event.preventDefault()
|
||||
deliverLink(url)
|
||||
})
|
||||
|
||||
ipcMain.handle('studio:list-bridges', listBridges)
|
||||
ipcMain.handle('studio:initial-link', () => {
|
||||
const link = pendingLink
|
||||
pendingLink = null
|
||||
return link
|
||||
})
|
||||
ipcMain.on('studio:set-theme', (_event, theme: ThemeChoice) => {
|
||||
nativeTheme.themeSource = theme
|
||||
})
|
||||
ipcMain.handle('studio:load-shortcuts', async () => (await readSettings())[SHORTCUTS_KEY] ?? {})
|
||||
ipcMain.on('studio:save-shortcuts', (_event, shortcuts: StoredShortcuts) => void saveSetting(SHORTCUTS_KEY, shortcuts))
|
||||
ipcMain.handle('studio:graphics', graphicsInfo)
|
||||
ipcMain.on('studio:use-dedicated-gpu', (_event, dedicated: boolean) => void useDedicatedGpu(dedicated))
|
||||
ipcMain.on('studio:copy-text', (_event, text: string) => clipboard.writeText(text))
|
||||
|
||||
void app.whenReady().then(createWindow)
|
||||
app.on('window-all-closed', () => app.quit())
|
||||
}
|
||||
Reference in new issue
Block a user