diff options
Diffstat (limited to 'users/ryan/hammerspoon/init.lua')
| -rw-r--r-- | users/ryan/hammerspoon/init.lua | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/users/ryan/hammerspoon/init.lua b/users/ryan/hammerspoon/init.lua new file mode 100644 index 0000000..1245a09 --- /dev/null +++ b/users/ryan/hammerspoon/init.lua | |||
| @@ -0,0 +1,35 @@ | |||
| 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() | ||
