lwe.handlers.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { ipcMain, type BrowserWindow } from 'electron'
  2. import { IpcChannels } from '@shared/ipc-channels'
  3. import { getLweStatus, detectDistro, installLweDeps, installLwe, uninstallLwe, launchLweAsync, stopLwe, isLweRunning, killAllLweProcesses, listLweObjects, hotswapLweSettings, listLweProperties, listLweAudioObjects } from '../services/lwe.service'
  4. import { invalidateEnvCache } from '../services/wallpaper.service'
  5. import { getConnectedScreens } from '../utils/platform'
  6. export function registerLweHandlers(win: BrowserWindow): void {
  7. ipcMain.handle(IpcChannels.LWE_STATUS, () => {
  8. return getLweStatus()
  9. })
  10. ipcMain.handle(IpcChannels.LWE_DETECT_DISTRO, () => {
  11. return detectDistro()
  12. })
  13. ipcMain.handle(IpcChannels.LWE_INSTALL_DEPS, async () => {
  14. await installLweDeps(win)
  15. return { ok: true }
  16. })
  17. ipcMain.handle(IpcChannels.LWE_INSTALL, async () => {
  18. await installLwe(win)
  19. invalidateEnvCache()
  20. return getLweStatus()
  21. })
  22. ipcMain.handle(IpcChannels.LWE_UNINSTALL, async () => {
  23. const result = await uninstallLwe()
  24. invalidateEnvCache()
  25. return result
  26. })
  27. ipcMain.handle(IpcChannels.LWE_LAUNCH, async (_e, wallpaperPath: string, options?: { screenRoot?: string; fps?: number }) => {
  28. await launchLweAsync(wallpaperPath, options)
  29. return { ok: true, running: isLweRunning() }
  30. })
  31. ipcMain.handle(IpcChannels.LWE_STOP, async () => {
  32. await stopLwe()
  33. return { ok: true }
  34. })
  35. ipcMain.handle(IpcChannels.LWE_KILL_ALL, async () => {
  36. return await killAllLweProcesses()
  37. })
  38. ipcMain.handle(IpcChannels.LWE_LIST_OBJECTS, async (_e, wallpaperPath: string) => {
  39. // Some wallpapers have project data linux-wallpaperengine itself can't parse; treat that as "no objects" rather than a hard error
  40. try {
  41. return await listLweObjects(wallpaperPath)
  42. } catch (err) {
  43. console.warn('[LWE] list-objects failed for', wallpaperPath, ':', (err as Error).message)
  44. return []
  45. }
  46. })
  47. ipcMain.handle(
  48. IpcChannels.LWE_HOTSWAP_SETTINGS,
  49. (
  50. _e,
  51. options: {
  52. disabledObjects?: string[]
  53. enabledObjects?: string[]
  54. volume?: number
  55. xray?: boolean
  56. scaling?: string
  57. zoom?: number
  58. disableParallax?: boolean
  59. cornerColor?: string
  60. speed?: number
  61. propertyOverrides?: Record<string, string>
  62. audioScreen?: string
  63. ambientVolume?: number
  64. audioSensitivity?: Record<string, number>
  65. }
  66. ) => {
  67. return { ok: hotswapLweSettings(options) }
  68. }
  69. )
  70. ipcMain.handle(IpcChannels.LWE_LIST_SCREENS, () => {
  71. return getConnectedScreens()
  72. })
  73. ipcMain.handle(IpcChannels.LWE_LIST_PROPERTIES, async (_e, wallpaperPath: string) => {
  74. // Same reasoning as list-objects: some wallpapers have properties linux-wallpaperengine
  75. // itself can't parse, treat that as "no properties" rather than a hard error.
  76. try {
  77. return await listLweProperties(wallpaperPath)
  78. } catch (err) {
  79. console.warn('[LWE] list-properties failed for', wallpaperPath, ':', (err as Error).message)
  80. return []
  81. }
  82. })
  83. ipcMain.handle(IpcChannels.LWE_LIST_AUDIO_OBJECTS, async (_e, wallpaperPath: string) => {
  84. // Same reasoning as list-objects/list-properties: treat a parse failure as "nothing detected"
  85. // rather than a hard error.
  86. try {
  87. return await listLweAudioObjects(wallpaperPath)
  88. } catch (err) {
  89. console.warn('[LWE] list-audio-objects failed for', wallpaperPath, ':', (err as Error).message)
  90. return []
  91. }
  92. })
  93. }