2018-01-21 10:44:13 +00:00
|
|
|
_G.requireInjector(_ENV)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2019-06-28 17:50:02 +00:00
|
|
|
local Peripheral = require('opus.peripheral')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
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',
|
2018-01-24 22:39:38 +00:00
|
|
|
hotkeys = { },
|
|
|
|
state = { },
|
2018-01-10 21:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_G.device.mouse = {
|
|
|
|
side = 'mouse',
|
|
|
|
type = 'mouse',
|
|
|
|
name = 'mouse',
|
2018-01-24 22:39:38 +00:00
|
|
|
state = { },
|
2018-01-10 21:46:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 17:50:02 +00:00
|
|
|
local Input = require('opus.input')
|
|
|
|
local Util = require('opus.util')
|
2018-01-10 21:46:37 +00:00
|
|
|
|
|
|
|
local device = _G.device
|
|
|
|
local kernel = _G.kernel
|
|
|
|
local keyboard = _G.device.keyboard
|
2019-03-24 20:29:49 +00:00
|
|
|
local keys = _G.keys
|
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)
|
2018-01-24 22:39:38 +00:00
|
|
|
local side = eventData[1]
|
|
|
|
if side then
|
|
|
|
local dev = Peripheral.addDevice(device, side)
|
|
|
|
if dev then
|
2019-07-16 02:08:30 +00:00
|
|
|
os.queueEvent('device_attach', dev.name)
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-10 21:46:37 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
kernel.hook('peripheral_detach', function(_, eventData)
|
2018-01-24 22:39:38 +00:00
|
|
|
local side = eventData[1]
|
|
|
|
if side then
|
2019-03-15 12:26:56 +00:00
|
|
|
for _, dev in pairs(Util.findAll(device, 'side', side)) do
|
2019-07-16 02:08:30 +00:00
|
|
|
os.queueEvent('device_detach', dev.name)
|
2018-01-24 22:39:38 +00:00
|
|
|
device[dev.name] = nil
|
|
|
|
end
|
|
|
|
end
|
2018-01-10 21:46:37 +00:00
|
|
|
end)
|
|
|
|
|
2019-03-24 20:29:49 +00:00
|
|
|
local modifiers = Util.transpose {
|
|
|
|
keys.leftCtrl, keys.rightCtrl,
|
|
|
|
keys.leftShift, keys.rightShift,
|
2019-03-28 11:29:01 +00:00
|
|
|
keys.leftAlt, keys.rightAlt,
|
2019-03-24 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 14:19:08 +00:00
|
|
|
kernel.hook({ 'key', 'char', 'paste' }, function(event, eventData)
|
2018-01-10 21:46:37 +00:00
|
|
|
local code = eventData[1]
|
|
|
|
|
2019-03-24 21:00:42 +00:00
|
|
|
-- maintain global keyboard modifier state
|
2019-07-02 14:19:08 +00:00
|
|
|
if event == 'key' and modifiers[code] then
|
|
|
|
keyboard.state[code] = true
|
2018-01-10 21:46:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- and fire hotkeys
|
2018-01-24 22:39:38 +00:00
|
|
|
local hotkey = Input:translate(event, eventData[1], eventData[2])
|
2018-01-10 21:46:37 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if hotkey and keyboard.hotkeys[hotkey.code] then
|
|
|
|
keyboard.hotkeys[hotkey.code](event, eventData)
|
2019-03-24 21:00:42 +00:00
|
|
|
return true
|
2018-01-24 22:39:38 +00:00
|
|
|
end
|
2018-01-10 21:46:37 +00:00
|
|
|
end)
|
|
|
|
|
2019-07-02 14:19:08 +00:00
|
|
|
kernel.hook('key_up', function(_, eventData)
|
|
|
|
local code = eventData[1]
|
|
|
|
|
|
|
|
if modifiers[code] then
|
|
|
|
keyboard.state[code] = nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-01-12 01:53:32 +00:00
|
|
|
kernel.hook({ 'mouse_click', 'mouse_up', 'mouse_drag' }, function(event, eventData)
|
2018-01-24 22:39:38 +00:00
|
|
|
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
|
|
|
|
if event == 'mouse_up' then
|
|
|
|
mouse.state[button] = nil
|
|
|
|
end
|
|
|
|
end
|
2018-01-12 01:53:32 +00:00
|
|
|
end)
|
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
function keyboard.addHotkey(code, fn)
|
2018-01-24 22:39:38 +00:00
|
|
|
keyboard.hotkeys[code] = fn
|
2018-01-10 21:46:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function keyboard.removeHotkey(code)
|
2018-01-24 22:39:38 +00:00
|
|
|
keyboard.hotkeys[code] = nil
|
2018-01-10 21:46:37 +00:00
|
|
|
end
|