1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-14 20:54:50 +00:00
opus/sys/init/1.device.lua

109 lines
2.4 KiB
Lua
Raw Normal View History

2018-01-21 10:44:13 +00:00
_G.requireInjector(_ENV)
2016-12-11 19:24:52 +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'
_G.device.keyboard = {
side = 'keyboard',
type = 'keyboard',
name = 'keyboard',
2018-01-24 22:39:38 +00:00
hotkeys = { },
state = { },
}
_G.device.mouse = {
side = 'mouse',
type = 'mouse',
name = 'mouse',
2018-01-24 22:39:38 +00:00
state = { },
}
local Input = require('opus.input')
local Util = require('opus.util')
local device = _G.device
local kernel = _G.kernel
local keyboard = _G.device.keyboard
local keys = _G.keys
2018-01-12 01:53:32 +00:00
local mouse = _G.device.mouse
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
os.queueEvent('device_attach', dev.name)
2018-01-24 22:39:38 +00:00
end
end
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
os.queueEvent('device_detach', dev.name)
2018-01-24 22:39:38 +00:00
device[dev.name] = nil
end
end
end)
local modifiers = Util.transpose {
keys.leftCtrl, keys.rightCtrl,
keys.leftShift, keys.rightShift,
keys.leftAlt, keys.rightAlt,
}
kernel.hook({ 'key', 'char', 'paste' }, function(event, eventData)
local code = eventData[1]
2019-03-24 21:00:42 +00:00
-- maintain global keyboard modifier state
if event == 'key' and modifiers[code] then
keyboard.state[code] = true
end
-- and fire hotkeys
2018-01-24 22:39:38 +00:00
local hotkey = Input:translate(event, eventData[1], eventData[2])
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
end)
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)
function keyboard.addHotkey(code, fn)
2018-01-24 22:39:38 +00:00
keyboard.hotkeys[code] = fn
end
function keyboard.removeHotkey(code)
2018-01-24 22:39:38 +00:00
keyboard.hotkeys[code] = nil
end