2018-01-21 10:44:13 +00:00
|
|
|
_G.requireInjector(_ENV)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
local Peripheral = require('peripheral')
|
|
|
|
|
2017-10-08 21:45:01 +00:00
|
|
|
_G.device = Peripheral.getList()
|
2018-01-06 11:07:49 +00:00
|
|
|
|
2018-01-16 01:38:30 +00:00
|
|
|
_G.device.terminal = _G.kernel.terminal
|
2018-01-07 03:25:33 +00:00
|
|
|
_G.device.terminal.side = 'terminal'
|
|
|
|
_G.device.terminal.type = 'terminal'
|
|
|
|
_G.device.terminal.name = 'terminal'
|
2018-01-10 21:46:37 +00:00
|
|
|
|
|
|
|
_G.device.keyboard = {
|
|
|
|
side = 'keyboard',
|
|
|
|
type = 'keyboard',
|
|
|
|
name = 'keyboard',
|
|
|
|
hotkeys = { },
|
|
|
|
state = { },
|
|
|
|
}
|
|
|
|
|
|
|
|
_G.device.mouse = {
|
|
|
|
side = 'mouse',
|
|
|
|
type = 'mouse',
|
|
|
|
name = 'mouse',
|
|
|
|
state = { },
|
|
|
|
}
|
|
|
|
|
|
|
|
local Input = require('input')
|
|
|
|
local Util = require('util')
|
|
|
|
|
|
|
|
local device = _G.device
|
|
|
|
local kernel = _G.kernel
|
|
|
|
local keyboard = _G.device.keyboard
|
2018-01-12 01:53:32 +00:00
|
|
|
local mouse = _G.device.mouse
|
2018-01-10 21:46:37 +00:00
|
|
|
local os = _G.os
|
|
|
|
|
|
|
|
kernel.hook('peripheral', function(_, eventData)
|
|
|
|
local side = eventData[1]
|
|
|
|
if side then
|
|
|
|
local dev = Peripheral.addDevice(device, side)
|
|
|
|
if dev then
|
|
|
|
os.queueEvent('device_attach', dev.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
kernel.hook('peripheral_detach', function(_, eventData)
|
|
|
|
local side = eventData[1]
|
|
|
|
if side then
|
|
|
|
local dev = Util.find(device, 'side', side)
|
|
|
|
if dev then
|
|
|
|
os.queueEvent('device_detach', dev.name)
|
|
|
|
device[dev.name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
kernel.hook({ 'key', 'key_up', 'char', 'paste' }, function(event, eventData)
|
|
|
|
local code = eventData[1]
|
|
|
|
|
|
|
|
-- maintain global keyboard state
|
|
|
|
if event == 'key' then
|
|
|
|
keyboard.state[code] = true
|
|
|
|
elseif event == 'key_up' then
|
|
|
|
if not keyboard.state[code] then
|
|
|
|
return true -- ensure key ups are only generated if a key down was sent
|
|
|
|
end
|
|
|
|
keyboard.state[code] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- and fire hotkeys
|
|
|
|
local hotkey = Input:translate(event, eventData[1], eventData[2])
|
|
|
|
|
|
|
|
if hotkey and keyboard.hotkeys[hotkey] then
|
|
|
|
keyboard.hotkeys[hotkey](event, eventData)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-01-12 01:53:32 +00:00
|
|
|
kernel.hook({ 'mouse_click', 'mouse_up', 'mouse_drag' }, function(event, eventData)
|
|
|
|
local button = eventData[1]
|
|
|
|
if event == 'mouse_click' then
|
|
|
|
mouse.state[button] = true
|
|
|
|
else
|
|
|
|
if not mouse.state[button] then
|
|
|
|
return true -- ensure mouse ups are only generated if a mouse down was sent
|
|
|
|
end
|
2018-01-13 20:17:26 +00:00
|
|
|
if event == 'mouse_up' then
|
|
|
|
mouse.state[button] = nil
|
|
|
|
end
|
2018-01-12 01:53:32 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
kernel.hook('kernel_focus', function()
|
|
|
|
Util.clear(keyboard.state)
|
|
|
|
Util.clear(mouse.state)
|
|
|
|
end)
|
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
function keyboard.addHotkey(code, fn)
|
|
|
|
keyboard.hotkeys[code] = fn
|
|
|
|
end
|
|
|
|
|
|
|
|
function keyboard.removeHotkey(code)
|
|
|
|
keyboard.hotkeys[code] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
kernel.hook('monitor_touch', function(event, eventData)
|
|
|
|
local monitor = Peripheral.getBySide(eventData[1])
|
|
|
|
if monitor and monitor.eventChannel then
|
|
|
|
monitor.eventChannel(event, table.unpack(eventData))
|
|
|
|
return true -- stop propagation
|
|
|
|
end
|
|
|
|
end)
|