Skip to content

Commit

Permalink
Merge pull request #209 from mr-mustash/mote_mics_when_system_muted
Browse files Browse the repository at this point in the history
More Muting
  • Loading branch information
mr-mustash authored Jul 1, 2024
2 parents 6772279 + 166f698 commit 4f114b8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions tilde/.hammerspoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Dock = require("system/dock")
networking = require("system/networking")
-- Ping = require("system/Ping") -- needs work
videoCalls = require("system/videoCalls")
-- Keytrap = require("system/keytrap")


audioControl.init()
Expand All @@ -120,6 +121,7 @@ Dock.init()
networking.init()
-- Ping.init() -- needs work
videoCalls.init()
-- Keytrap.init()
-- ========================================================================= }}}

-- App configuration ======================================================= {{{
Expand Down
40 changes: 34 additions & 6 deletions tilde/.hammerspoon/system/audioControl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ local function internalOrExternalMic()
_log("Unable to set any mic as default")
return 1
end
local function perDeviceWatcher(dev_uid, event_name, event_scope, event_element)
local device = hs.audiodevice.findDeviceByUID(dev_uid)
if device and event_name == "mute" then
_log("\"" .. device:name() .. "\" mute state has changed.")
audioControl.matchInputMuteToOutputMute()
sleep(1)
end
end

local function startOutputWatcher()
local defaultOutput = hs.audiodevice.defaultOutputDevice()

if defaultOutput:watcherIsRunning() then
_log("Audio watcher for " .. defaultOutput:name() .. " already exists.")
else
_log("No audio watcher found for " .. defaultOutput:name() .. " starting one.")
defaultOutput:watcherCallback(perDeviceWatcher)
defaultOutput:watcherStart()
end
end

local function audioDeviceChanged(arg)
local outputRetval = 1
Expand All @@ -66,6 +86,7 @@ local function audioDeviceChanged(arg)
sleep(5)

_log("New audio device detected. Current values: Speaker: " .. hs.audiodevice.defaultOutputDevice():name() .. " Mic: " .. hs.audiodevice.defaultInputDevice():name())
startOutputWatcher()

outputRetval = internalOrExternalSpeaker()
micRetval = internalOrExternalMic()
Expand All @@ -89,14 +110,12 @@ local function trapVolumeControls()

-- Send mute to external monitor if connected and it's the default audio output
if event["key"] == "MUTE" then
if isMuted == false then
isMuted = true
run.cmd("/Users/patrickking/bin/m1ddc", { "set", "mute", "on" })
_log("Muted external monitor.")
else
isMuted = false
if hs.audiodevice.defaultOutputDevice():outputMuted() then
run.cmd("/Users/patrickking/bin/m1ddc", { "set", "mute", "off" })
_log("Unmuted external monitor.")
else
run.cmd("/Users/patrickking/bin/m1ddc", { "set", "mute", "on" })
_log("Muted external monitor.")
end
return true
end
Expand Down Expand Up @@ -128,6 +147,7 @@ function audioControl.init()
local initStart = os.clock()
hs.audiodevice.watcher.setCallback(audioDeviceChanged)
hs.audiodevice.watcher.start()
startOutputWatcher()

trapVolumeControls()
hs.hotkey.bind({'cmd', 'shift'}, "k", function() audioControl.mediaControls("PLAY") end)
Expand All @@ -137,6 +157,14 @@ function audioControl.init()
_log(debug.getinfo(1, "S").short_src:gsub(".*/", "") .. " loaded in " .. (os.clock() - initStart) .. " seconds.")
end

function audioControl.matchInputMuteToOutputMute()
if hs.audiodevice.defaultOutputDevice():muted() then
audioControl.muteInputs()
else
audioControl.unmuteInputs()
end
end

function audioControl.muteInputs()
for _, device in pairs(hs.audiodevice.allInputDevices()) do
device:setInputMuted(true)
Expand Down
16 changes: 16 additions & 0 deletions tilde/.hammerspoon/system/keytrap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local keytrap = {}

local function keytrap_router()
systemeventtap = hs.eventtap.new({ hs.eventtap.event.types.systemDefined }, function(mainEvent)
local event = mainEvent:systemKey()
local flags = hs.eventtap.checkKeyboardModifiers()

end)
systemeventtap:start()
end

function keytrap.init()
keytrap_router()
end

return keytrap

0 comments on commit 4f114b8

Please sign in to comment.