-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusb.lua
More file actions
33 lines (28 loc) · 680 Bytes
/
Copy pathusb.lua
File metadata and controls
33 lines (28 loc) · 680 Bytes
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
local usb = {}
local fnsOnChange = {}
usb.changeWatcher = hs.usb.watcher.new(function (device)
for _, fn in ipairs(fnsOnChange) do
fn(device)
end
end):start()
-- Adds a USB watcher function
--
-- A given function is immediately called for each device that is
-- currently attached.
usb.onChange = function (fn)
table.insert(fnsOnChange, fn)
for _, device in ipairs(hs.usb.attachedDevices()) do
device.eventType = "added"
fn(device)
end
end
-- Removes a USB watcher function
usb.offChange = function (fn)
for i = #fnsOnChange, 1, -1 do
if fnsOnChange[i] == fn then
table.remove(fnsOnChange, i)
return
end
end
end
return usb