From 28c61b63edc9010dfead9efce23cf6c3726dc029 Mon Sep 17 00:00:00 2001 From: Sy Le Date: Sun, 3 Jul 2022 09:00:54 -0700 Subject: [PATCH 1/8] fixed volume for mac --- src/main/utils/SoundUtils.Darwin.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/utils/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 8032df7..6b2ee77 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,6 +1,6 @@ import loudness from 'loudness'; -const SoundUtils = { +const IntelMacSoundUtils = { getVolume: async () => { return { value: await loudness.getVolume(), @@ -11,4 +11,18 @@ const SoundUtils = { setMuted: async (muted: boolean) => loudness.setMuted(muted), }; -export default SoundUtils; +const M1MacSoundUtils = { + getVolume: async () => { + return { + value: await loudness.getVolume(), + muted: await loudness.getMuted(), + }; + }, + setVolume: async (value: number) => loudness.setVolume(value), + setMuted: async (muted: boolean) => loudness.setMuted(muted), +}; + +// export default M1MacSoundUtils; +export default IntelMacSoundUtils; + +// osascript -e "set Volume 0" From 9c445c46c3563397050fad9d1603afddf105e53a Mon Sep 17 00:00:00 2001 From: Sy Le Date: Sun, 3 Jul 2022 09:03:36 -0700 Subject: [PATCH 2/8] fixed volume --- src/main/utils/SoundUtils.Darwin.ts | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/main/utils/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 6b2ee77..6e0b500 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,28 +1,14 @@ import loudness from 'loudness'; -const IntelMacSoundUtils = { +const SoundUtils = { getVolume: async () => { 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) => executeBash(`osascript -e "set Volume ${Math.floor(value / 10)}"`), + setMuted: async (muted: boolean) => executeBash(`osascript -e "set Volume 0"`), }; -const M1MacSoundUtils = { - getVolume: async () => { - return { - value: await loudness.getVolume(), - muted: await loudness.getMuted(), - }; - }, - setVolume: async (value: number) => loudness.setVolume(value), - setMuted: async (muted: boolean) => loudness.setMuted(muted), -}; - -// export default M1MacSoundUtils; -export default IntelMacSoundUtils; - -// osascript -e "set Volume 0" +export default SoundUtils; From 40625e94191ef166f65dbb41e019a168aa6c8459 Mon Sep 17 00:00:00 2001 From: Sy Le Date: Sun, 3 Jul 2022 09:09:03 -0700 Subject: [PATCH 3/8] use raw osascript to get away from using the loudness level - works in m1 --- package.json | 3 +-- prebuild.js | 1 - src/main/utils/SoundUtils.Darwin.ts | 21 +++++++++++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index dde8201..238ee2a 100644 --- a/package.json +++ b/package.json @@ -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", 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/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 6e0b500..cd044db 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,7 +1,24 @@ -import loudness from 'loudness'; - const SoundUtils = { getVolume: async () => { + try{ + const stdout = await executeBash(`osascript -e 'get volume settings'`); + stdout = stdout.substr(stdout.indexOf(`output volume:`) + `output volume:`.length) + + const volume = parseInt(stdout.substr(0, stdout.indexOf(`,`))); + + if(volume === 0){ + return { + value: 0, + muted: true + } + } + + return { + value: volume, + muted: false + } + }catch(err){} + return { value: 50, muted: true, From 25a2f2d049828eebd1c9e6c5e2988cc0798567b9 Mon Sep 17 00:00:00 2001 From: Sy Le Date: Mon, 4 Jul 2022 08:01:45 -0700 Subject: [PATCH 4/8] m1 sound fix --- src/main/utils/SoundUtils.Darwin.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/utils/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index cd044db..41baa98 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,7 +1,9 @@ +import { executeBash } from 'src/main/utils/ShellUtils'; + const SoundUtils = { getVolume: async () => { try{ - const stdout = await executeBash(`osascript -e 'get volume settings'`); + let stdout = await executeBash(`osascript -e 'get volume settings'`); stdout = stdout.substr(stdout.indexOf(`output volume:`) + `output volume:`.length) const volume = parseInt(stdout.substr(0, stdout.indexOf(`,`))); From e825e3648d0f8c8721a917879dc7df8dbb98d931 Mon Sep 17 00:00:00 2001 From: Sy Le Date: Mon, 4 Jul 2022 08:11:08 -0700 Subject: [PATCH 5/8] wip --- src/main/utils/Endpoints.js | 4 +++- src/main/utils/SoundUtils.Darwin.ts | 31 +++++++++++++++++++++++++---- src/renderer/pages/Home.tsx | 1 + 3 files changed, 31 insertions(+), 5 deletions(-) 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/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 41baa98..87b7f00 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -2,8 +2,9 @@ import { executeBash } from 'src/main/utils/ShellUtils'; const SoundUtils = { getVolume: async () => { + let stdout= ''; try{ - let stdout = await executeBash(`osascript -e 'get volume settings'`); + stdout = await executeBash(`osascript -e 'get volume settings'`); stdout = stdout.substr(stdout.indexOf(`output volume:`) + `output volume:`.length) const volume = parseInt(stdout.substr(0, stdout.indexOf(`,`))); @@ -19,15 +20,37 @@ const SoundUtils = { value: volume, muted: false } - }catch(err){} + }catch(err){ + console.error('SoundUtils.getVolume', stdout, err); + } return { value: 50, muted: true, }; }, - setVolume: async (value: number) => executeBash(`osascript -e "set Volume ${Math.floor(value / 10)}"`), - setMuted: async (muted: boolean) => executeBash(`osascript -e "set Volume 0"`), + setVolume: async (value: number) => { + let command = ''; + try { + command = `osascript -e "set Volume ${Math.floor(value / 10)}"`; + console.debug('SoundUtils.setVolume', command); + await executeBash(command) + } catch(err){ + console.error('SoundUtils.setMuted',value,stdout, err); + } + }, + setMuted: async (muted: boolean) => { + let command = ''; + try { + if(muted){ + command = `osascript -e "set Volume 0"` + } + console.debug('SoundUtils.setMuted', command); + await executeBash(command) + }catch(err){ + console.error('SoundUtils.setMuted',muted, stdout, 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 && } ); From 3104d78fcbd68cbff412468aef074239a3b5f77d Mon Sep 17 00:00:00 2001 From: Sy Le Date: Mon, 4 Jul 2022 08:18:41 -0700 Subject: [PATCH 6/8] wip osascripts --- src/main/utils/ShellUtils.ts | 23 +++++++++++++++++++++++ src/main/utils/SoundUtils.Darwin.ts | 21 +++++++++++---------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/utils/ShellUtils.ts b/src/main/utils/ShellUtils.ts index 94ff18a..ba7fe10 100644 --- a/src/main/utils/ShellUtils.ts +++ b/src/main/utils/ShellUtils.ts @@ -35,3 +35,26 @@ 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', [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); + } else { + resolve(data); + } + }); + }, delay); + }); +} diff --git a/src/main/utils/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 87b7f00..c964130 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,10 +1,11 @@ -import { executeBash } from 'src/main/utils/ShellUtils'; - +import { executeOsaScript } from 'src/main/utils/ShellUtils'; +// /usr/bin/osascript const SoundUtils = { getVolume: async () => { let stdout= ''; + let command = `get volume settings` try{ - stdout = await executeBash(`osascript -e 'get volume settings'`); + stdout = await executeOsaScript(command); stdout = stdout.substr(stdout.indexOf(`output volume:`) + `output volume:`.length) const volume = parseInt(stdout.substr(0, stdout.indexOf(`,`))); @@ -21,7 +22,7 @@ const SoundUtils = { muted: false } }catch(err){ - console.error('SoundUtils.getVolume', stdout, err); + console.error('SoundUtils.getVolume',command, stdout, err); } return { @@ -32,23 +33,23 @@ const SoundUtils = { setVolume: async (value: number) => { let command = ''; try { - command = `osascript -e "set Volume ${Math.floor(value / 10)}"`; + command = `set Volume ${Math.floor(value / 10)}`; console.debug('SoundUtils.setVolume', command); - await executeBash(command) + await executeOsaScript(command) } catch(err){ - console.error('SoundUtils.setMuted',value,stdout, err); + console.error('SoundUtils.setMuted',value,command, err); } }, setMuted: async (muted: boolean) => { let command = ''; try { if(muted){ - command = `osascript -e "set Volume 0"` + command = `set Volume 0` } console.debug('SoundUtils.setMuted', command); - await executeBash(command) + await executeOsaScript(command) }catch(err){ - console.error('SoundUtils.setMuted',muted, stdout, err); + console.error('SoundUtils.setMuted',muted, command, err); } }, }; From 9f3faada1485109e8647d6abf943dc9876c5901f Mon Sep 17 00:00:00 2001 From: Sy Le Date: Mon, 4 Jul 2022 08:32:23 -0700 Subject: [PATCH 7/8] wip --- package.json | 1 + src/main/utils/ShellUtils.ts | 4 ++-- src/main/utils/SoundUtils.Darwin.ts | 35 +++++++++++++++++------------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 238ee2a..b2003f5 100644 --- a/package.json +++ b/package.json @@ -84,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/src/main/utils/ShellUtils.ts b/src/main/utils/ShellUtils.ts index ba7fe10..f7eaa68 100644 --- a/src/main/utils/ShellUtils.ts +++ b/src/main/utils/ShellUtils.ts @@ -40,7 +40,7 @@ export function executeBash(shellToRun: string, delay = 25): Promise { export function executeOsaScript(shellToRun: string, delay = 25): Promise { return new Promise((resolve, reject) => { setTimeout(() => { - const child = spawn('/usr/bin/osascript', [shellToRun]); + const child = spawn('/usr/bin/osascript', ['-e', shellToRun]); let data = ''; child.stdout.on('data', function (msg) { @@ -50,7 +50,7 @@ export function executeOsaScript(shellToRun: string, delay = 25): Promise { + osascript.execute(command, function(err: any, result: any){ + if (err) return reject(err) + resolve(result) + }); + }) +} + const SoundUtils = { getVolume: async () => { - let stdout= ''; let command = `get volume settings` - try{ - stdout = await executeOsaScript(command); - stdout = stdout.substr(stdout.indexOf(`output volume:`) + `output volume:`.length) - const volume = parseInt(stdout.substr(0, stdout.indexOf(`,`))); + try{ + const volume = await executeOsaScript(command).then(resp => resp['input volume']); if(volume === 0){ return { @@ -18,11 +25,11 @@ const SoundUtils = { } return { - value: volume, - muted: false - } + value: volume, + muted: false + } }catch(err){ - console.error('SoundUtils.getVolume',command, stdout, err); + console.error('SoundUtils.getVolume', command, err); } return { @@ -33,7 +40,7 @@ const SoundUtils = { setVolume: async (value: number) => { let command = ''; try { - command = `set Volume ${Math.floor(value / 10)}`; + command = `set volume ${Math.floor(value / 10)}`; console.debug('SoundUtils.setVolume', command); await executeOsaScript(command) } catch(err){ @@ -44,12 +51,12 @@ const SoundUtils = { let command = ''; try { if(muted){ - command = `set Volume 0` + command = `set volume 0` } console.debug('SoundUtils.setMuted', command); await executeOsaScript(command) }catch(err){ - console.error('SoundUtils.setMuted',muted, command, err); + console.error('SoundUtils.setMuted', muted, command, err); } }, }; From aff1d1f6f0fb891ff69964ba71e812adb1b10ec7 Mon Sep 17 00:00:00 2001 From: synle Date: Mon, 4 Jul 2022 15:44:17 +0000 Subject: [PATCH 8/8] CI / CD - Prettier Automatic Commit --- package.json | 2 +- src/main/utils/ShellUtils.ts | 2 -- src/main/utils/SoundUtils.Darwin.ts | 48 ++++++++++++++--------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index b2003f5..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", diff --git a/src/main/utils/ShellUtils.ts b/src/main/utils/ShellUtils.ts index f7eaa68..0ded631 100644 --- a/src/main/utils/ShellUtils.ts +++ b/src/main/utils/ShellUtils.ts @@ -35,8 +35,6 @@ export function executeBash(shellToRun: string, delay = 25): Promise { }, delay); }); } - - export function executeOsaScript(shellToRun: string, delay = 25): Promise { return new Promise((resolve, reject) => { setTimeout(() => { diff --git a/src/main/utils/SoundUtils.Darwin.ts b/src/main/utils/SoundUtils.Darwin.ts index 0c5aafd..bf24053 100644 --- a/src/main/utils/SoundUtils.Darwin.ts +++ b/src/main/utils/SoundUtils.Darwin.ts @@ -1,34 +1,34 @@ // @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) +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 = { +const SoundUtils = { getVolume: async () => { - let command = `get volume settings` + let command = `get volume settings`; - try{ - const volume = await executeOsaScript(command).then(resp => resp['input volume']); + try { + const volume = await executeOsaScript(command).then((resp) => resp['input volume']); - if(volume === 0){ + if (volume === 0) { return { value: 0, - muted: true - } + muted: true, + }; } return { value: volume, - muted: false - } - }catch(err){ + muted: false, + }; + } catch (err) { console.error('SoundUtils.getVolume', command, err); } @@ -42,20 +42,20 @@ const SoundUtils = { 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); + await executeOsaScript(command); + } catch (err) { + console.error('SoundUtils.setMuted', value, command, err); } }, - setMuted: async (muted: boolean) => { + setMuted: async (muted: boolean) => { let command = ''; try { - if(muted){ - command = `set volume 0` + if (muted) { + command = `set volume 0`; } console.debug('SoundUtils.setMuted', command); - await executeOsaScript(command) - }catch(err){ + await executeOsaScript(command); + } catch (err) { console.error('SoundUtils.setMuted', muted, command, err); } },