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
|
|
|
|
|
2018-12-02 00:09:24 +00:00
|
|
|
local drivers = { }
|
|
|
|
|
2018-01-10 21:46:37 +00:00
|
|
|
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
|
2018-12-02 00:09:24 +00:00
|
|
|
if drivers[dev.type] then
|
|
|
|
local e = drivers[dev.type](dev)
|
|
|
|
if type(e) == 'table' then
|
|
|
|
for _, v in pairs(e) do
|
|
|
|
os.queueEvent('device_attach', v.name)
|
|
|
|
end
|
|
|
|
elseif e then
|
|
|
|
os.queueEvent('device_attach', e.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
os.queueEvent('device_attach', dev.name, dev)
|
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
|
2018-12-02 00:09:24 +00:00
|
|
|
os.queueEvent('device_detach', dev.name, dev)
|
|
|
|
if dev._children then
|
|
|
|
for _,v in pairs(dev._children) do
|
|
|
|
os.queueEvent('peripheral_detach', v.name)
|
|
|
|
end
|
|
|
|
end
|
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
|
|
|
|
|
2018-12-02 19:35:58 +00:00
|
|
|
local function createDevice(name, devType, method, manipulator)
|
|
|
|
local dev = {
|
|
|
|
name = name,
|
|
|
|
side = name,
|
|
|
|
type = devType,
|
|
|
|
}
|
|
|
|
local methods = {
|
|
|
|
'drop', 'getDocs', 'getItem', 'getItemMeta', 'getTransferLocations',
|
|
|
|
'list', 'pullItems', 'pushItems', 'size', 'suck',
|
|
|
|
}
|
|
|
|
if manipulator[method] then
|
|
|
|
for _,k in pairs(methods) do
|
|
|
|
dev[k] = function(...)
|
|
|
|
return manipulator[method]()[k](...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not manipulator._children then
|
|
|
|
manipulator._children = { dev }
|
|
|
|
else
|
|
|
|
table.insert(manipulator._children, dev)
|
|
|
|
end
|
|
|
|
device[name] = dev
|
2018-12-02 00:09:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
drivers['manipulator'] = function(dev)
|
2018-12-02 20:10:21 +00:00
|
|
|
if dev.getName then
|
2018-12-07 04:13:43 +00:00
|
|
|
pcall(function()
|
2018-12-22 01:58:41 +00:00
|
|
|
local name = dev.getName()
|
|
|
|
if name then
|
|
|
|
if dev.getInventory then
|
|
|
|
createDevice(name .. ':inventory', 'inventory', 'getInventory', dev)
|
|
|
|
end
|
|
|
|
if dev.getEquipment then
|
|
|
|
createDevice(name .. ':equipment', 'equipment', 'getEquipment', dev)
|
|
|
|
end
|
|
|
|
if dev.getEnder then
|
|
|
|
createDevice(name .. ':enderChest', 'enderChest', 'getEnder', dev)
|
|
|
|
end
|
2018-12-02 19:35:58 +00:00
|
|
|
|
2018-12-22 01:58:41 +00:00
|
|
|
return dev._children
|
|
|
|
end
|
|
|
|
end)
|
2018-12-02 20:10:21 +00:00
|
|
|
end
|
2018-12-02 00:09:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- initialize drivers
|
|
|
|
for _,v in pairs(device) do
|
|
|
|
if drivers[v.type] then
|
2018-12-06 02:21:30 +00:00
|
|
|
local s, m = pcall(drivers[v.type], v)
|
|
|
|
if not s and m then
|
|
|
|
_G.printError(m)
|
|
|
|
end
|
2018-12-02 00:09:24 +00:00
|
|
|
end
|
|
|
|
end
|