diff --git a/package.json b/package.json index dde8201..3f31a4b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "display-dj", "private": true, - "version": "1.11.7", + "version": "1.11.8", "description": "A cross platform desktop application that supports brightness adjustment for integrated laptop monitor as well as external monitors and dark mode toggle supporting Windows and MacOSX at the moment.", "scripts": { "clean-dist": "rimraf dist", @@ -53,8 +53,7 @@ "dark-mode": "^4.0.0", "electron-installer-dmg": "^4.0.0", "electron-squirrel-startup": "^1.0.0", - "electron-winstaller": "^5.0.0", - "loudness": "^0.4.1" + "electron-winstaller": "^5.0.0" }, "dependencies": { "@emotion/react": "^11.9.0", @@ -85,6 +84,7 @@ "electron-packager": "^15.5.1", "eslint": "^8.16.0", "jest": "^27.5.1", + "node-osascript": "^2.1.0", "prettier": "^2.6.2", "rimraf": "^3.0.2", "sass": "^1.52.1", diff --git a/prebuild.js b/prebuild.js index 9a63a0c..76885c8 100644 --- a/prebuild.js +++ b/prebuild.js @@ -27,7 +27,6 @@ switch (process.platform) { packages = ` dark-mode - loudness electron-installer-dmg `; files.push([`src/main/utils/DisplayAdapter.Darwin.ts`, DEST_IMPL_DISPLAY_UTILS]); diff --git a/src/main/utils/Endpoints.js b/src/main/utils/Endpoints.js index 2a8c7e4..95c06f5 100644 --- a/src/main/utils/Endpoints.js +++ b/src/main/utils/Endpoints.js @@ -58,7 +58,9 @@ export function setUpDataEndpoints() { ...(await SoundUtils.getVolume()), isDisabled: false, }; - } catch (err1) {} + } catch (err1) { + console.error('Get sound failed in Endpoints', err1); + } res.status(200).json({ darkMode: (await DisplayUtils.getDarkMode()) === true, diff --git a/src/main/utils/ShellUtils.ts b/src/main/utils/ShellUtils.ts index 94ff18a..0ded631 100644 --- a/src/main/utils/ShellUtils.ts +++ b/src/main/utils/ShellUtils.ts @@ -35,3 +35,24 @@ export function executeBash(shellToRun: string, delay = 25): Promise { }, delay); }); } +export function executeOsaScript(shellToRun: string, delay = 25): Promise { + return new Promise((resolve, reject) => { + setTimeout(() => { + const child = spawn('/usr/bin/osascript', ['-e', shellToRun]); + + let data = ''; + child.stdout.on('data', function (msg) { + data += msg.toString(); + }); + + child.on('exit', function (exitCode: string) { + if (parseInt(exitCode) !== 0) { + //Handle non-zero exit + reject(exitCode + `"'/usr/bin/osascript' -e ${shellToRun}"`); + } else { + resolve(data); + } + }); + }, delay); + }); +} diff --git a/src/main/utils/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 8032df7..bf24053 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,14 +1,64 @@ -import loudness from 'loudness'; +// @ts-nocheck +import osascript from 'node-osascript'; + +function executeOsaScript(command: string) { + return new Promise((resolve, reject) => { + osascript.execute(command, function (err: any, result: any) { + if (err) return reject(err); + resolve(result); + }); + }); +} const SoundUtils = { getVolume: async () => { + let command = `get volume settings`; + + try { + const volume = await executeOsaScript(command).then((resp) => resp['input volume']); + + if (volume === 0) { + return { + value: 0, + muted: true, + }; + } + + return { + value: volume, + muted: false, + }; + } catch (err) { + console.error('SoundUtils.getVolume', command, err); + } + return { - value: await loudness.getVolume(), - muted: await loudness.getMuted(), + value: 50, + muted: true, }; }, - setVolume: async (value: number) => loudness.setVolume(value), - setMuted: async (muted: boolean) => loudness.setMuted(muted), + setVolume: async (value: number) => { + let command = ''; + try { + command = `set volume ${Math.floor(value / 10)}`; + console.debug('SoundUtils.setVolume', command); + await executeOsaScript(command); + } catch (err) { + console.error('SoundUtils.setMuted', value, command, err); + } + }, + setMuted: async (muted: boolean) => { + let command = ''; + try { + if (muted) { + command = `set volume 0`; + } + console.debug('SoundUtils.setMuted', command); + await executeOsaScript(command); + } catch (err) { + console.error('SoundUtils.setMuted', muted, command, err); + } + }, }; export default SoundUtils; diff --git a/src/renderer/pages/Home.tsx b/src/renderer/pages/Home.tsx index 1afe30c..d49c54a 100644 --- a/src/renderer/pages/Home.tsx +++ b/src/renderer/pages/Home.tsx @@ -102,6 +102,7 @@ export function Home(props: HomeProps) { <>
+ {configs.volume.isDisabled === false && } );