aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/hammerspoon/init.lua
blob: 1245a0921c6ff3b1fb09d685664f697ccc6b72e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- ~/.hammerspoon/init.lua

local function refreshWidget()
    hs.http.asyncGet("http://127.0.0.1:7776/widget/sound/refresh", nil, function(status, body, headers)
        -- optional: log failures
        if status ~= 200 then
            print("Widget refresh failed with status: " .. tostring(status))
        end
    end)
end

local function audioWatcherCallback(uid, eventName, scope, element)
    -- "vmvc" = volume changed, "mute" = mute toggled
    if eventName == "vmvc" or eventName == "mute" then
        refreshWidget()
    end
end

local function setupAudioWatcher()
    local device = hs.audiodevice.defaultOutputDevice()
    if device then
        device:watcherCallback(audioWatcherCallback)
        device:watcherStart()
    end
end

setupAudioWatcher()

-- Re-attach the watcher if the default output device changes (e.g. plugging in headphones)
hs.audiodevice.watcher.setCallback(function(event)
    if event == "dOut " then  -- note: the trailing space is intentional, it's part of the event name
        setupAudioWatcher()
    end
end)
hs.audiodevice.watcher.start()