users/ryan/hammerspoon/init.lua
0ce4331f742863ec03c711c27b0a0dee7e8cc187
· 1.1 KB · 35 lines
raw
| 1 | -- ~/.hammerspoon/init.lua |
| 2 | |
| 3 | local function refreshWidget() |
| 4 | hs.http.asyncGet("http://127.0.0.1:7776/widget/sound/refresh", nil, function(status, body, headers) |
| 5 | -- optional: log failures |
| 6 | if status ~= 200 then |
| 7 | print("Widget refresh failed with status: " .. tostring(status)) |
| 8 | end |
| 9 | end) |
| 10 | end |
| 11 | |
| 12 | local function audioWatcherCallback(uid, eventName, scope, element) |
| 13 | -- "vmvc" = volume changed, "mute" = mute toggled |
| 14 | if eventName == "vmvc" or eventName == "mute" then |
| 15 | refreshWidget() |
| 16 | end |
| 17 | end |
| 18 | |
| 19 | local function setupAudioWatcher() |
| 20 | local device = hs.audiodevice.defaultOutputDevice() |
| 21 | if device then |
| 22 | device:watcherCallback(audioWatcherCallback) |
| 23 | device:watcherStart() |
| 24 | end |
| 25 | end |
| 26 | |
| 27 | setupAudioWatcher() |
| 28 | |
| 29 | -- Re-attach the watcher if the default output device changes (e.g. plugging in headphones) |
| 30 | hs.audiodevice.watcher.setCallback(function(event) |
| 31 | if event == "dOut " then -- note: the trailing space is intentional, it's part of the event name |
| 32 | setupAudioWatcher() |
| 33 | end |
| 34 | end) |
| 35 | hs.audiodevice.watcher.start() |