diff --git a/halyde/kernel/modules/evmgr.lua b/halyde/kernel/modules/evmgr.lua index 4930c94..e3d03b3 100644 --- a/halyde/kernel/modules/evmgr.lua +++ b/halyde/kernel/modules/evmgr.lua @@ -10,7 +10,7 @@ local process function module.init() _G.evmgr = {} - _G.evmgr.eventQueue = {} + _G.evmgr.eventQueue = { kernel = {} } local maxEventQueueLength = 10 -- increase if events start getting dropped local computer = require("computer") @@ -31,11 +31,14 @@ function module.init() _, process = _PUBLIC.tsched.addTask(function() while true do + -- check for events local args repeat args = { computer.uptime(), computer.pullSignal(0) } if args and args[2] then - table.insert(evmgr.eventQueue, args) + for pid in pairs(evmgr.eventQueue) do + table.insert(evmgr.eventQueue[pid], args) + end if _PUBLIC.keyboard then if args[2] == "key_down" then local keycode = args[5] @@ -64,13 +67,16 @@ function module.init() end end end - while #evmgr.eventQueue > maxEventQueueLength do - --ocelot.log("Queue length breach, removing first signal") - table.remove(evmgr.eventQueue, 1) + for pid in pairs(evmgr.eventQueue) do + while #evmgr.eventQueue[pid] > maxEventQueueLength do + --ocelot.log("Queue length breach, removing first signal") + table.remove(evmgr.eventQueue[pid], 1) + end end end until not args or not args[1] - --ocelot.log("done") + -- TODO: check for processes that have ended + -- run other tasks coroutine.yield() end end, "evmgr") diff --git a/lib/event.lua b/lib/event.lua index dc936be..61b1399 100644 --- a/lib/event.lua +++ b/lib/event.lua @@ -5,9 +5,14 @@ local bufferTime = 0.1 -- A little bit of buffer time so events won't be skipped --local ocelot = component.proxy(component.list("ocelot")()) function event.pull(...) - local args = {...} + local pid = _PUBLIC.tsched and _PUBLIC.tsched.getCurrentTask() and _PUBLIC.tsched.getCurrentTask().id or "kernel" + if not evmgr.eventQueue[pid] then + evmgr.eventQueue[pid] = {} + end + local eventQueue = evmgr.eventQueue[pid] + local args = { ... } local evtypes, timeout = {}, nil - + for _, arg in pairs(args) do if type(arg) == "number" and not timeout then -- It's a timeout timeout = arg @@ -15,41 +20,43 @@ function event.pull(...) table.insert(evtypes, tostring(arg)) end end - + local startTime = computer.uptime() - + while true do -- Check event queue for matching event - for i = 1, #evmgr.eventQueue do + for i = 1, #eventQueue do local foundevent = false if evtypes[1] then -- event type(s) specified for _, evtype in pairs(evtypes) do - if evmgr.eventQueue[i][2] == evtype and evmgr.eventQueue[i][1] >= startTime - bufferTime then + if eventQueue[i][2] == evtype and eventQueue[i][1] >= startTime - bufferTime then foundevent = true end end else - if evmgr.eventQueue[i][1] >= startTime - bufferTime then + if eventQueue[i][1] >= startTime - bufferTime then foundevent = true end end if foundevent then -- Found matching event (or any event if no type specified) - local result = table.copy(evmgr.eventQueue[i]) - table.remove(evmgr.eventQueue, i) + local result = table.copy(eventQueue[i]) + table.remove(eventQueue, i) table.remove(result, 1) -- remove the time of event argument return table.unpack(result) end end - + -- Check if we've timed out if timeout and computer.uptime() >= startTime + timeout then - return nil -- Timed out, return nil + return nil -- Timed out, return nil end - + -- Yield to allow other processes to run and more events to be added if timeout and timeout > 0 then coroutine.yield() + elseif not timeout then + coroutine.yield() end end end