Permalink
Browse files

modified: ../EventLoop.lua

	modified:   ../Keyboard.lua
	modified:   ../input.lua
	modified:   test_event_loop.lua
  • Loading branch information...
1 parent 0fa278a commit e9feba88c4b95f914dd1e3c6652d95fc99ab1213 @Wiladams committed Nov 6, 2012
Showing with 69 additions and 19 deletions.
  1. +14 −7 EventLoop.lua
  2. +27 −4 Keyboard.lua
  3. +3 −2 input.lua
  4. +25 −6 tests/test_event_loop.lua
View
21 EventLoop.lua
@@ -9,6 +9,7 @@ EventLoop.new = function()
local obj = {
Observers = {},
Emitter = IOAlertEmitter.new(),
+ Running = false;
}
setmetatable(obj, EventLoop_mt);
@@ -17,17 +18,23 @@ EventLoop.new = function()
end
-EventLoop.AddEmitter = function(self, emitter, observer)
- observer = observer or emitter
- self.Observers[emitter.AlertHandle:getfd()] = observer;
+EventLoop.AddObservable = function(self, observable, observer)
+ observer = observer or observable
+ self.Observers[observable.AlertHandle:getfd()] = observer;
- return self.Emitter:AddAlertable(emitter.AlertHandle, observer.OnAlert, emitter.WhichAlerts);
+ return self.Emitter:AddAlertable(observable.AlertHandle, observer.OnAlert, observable.WhichAlerts);
+end
+
+EventLoop.Halt = function(self)
+ self.Running = false;
end
EventLoop.Run = function(self, timeout)
timeout = timeout or 0
- while true do
+ self.Running = true;
+
+ while self.Running do
local alerts, err = self.Emitter:Wait(timeout)
if alerts and #alerts > 0 then
@@ -37,14 +44,14 @@ EventLoop.Run = function(self, timeout)
-- get the appropriate observer
local observer = self.Observers[alerts[i].fd];
if observer and observer.OnAlert then
- observer:OnAlert(alerts[i])
+ observer:OnAlert(self, alerts[i].fd, alerts[i].events)
end
end
end
-- Allow some idle work to occur
if self.OnIdle then
- self.OnIdle()
+ self.OnIdle(self)
end
end
end
View
31 Keyboard.lua
@@ -29,14 +29,37 @@ Keyboard.new = function(devicename)
return obj
end
-Keyboard.OnAlert = function(self, alert)
- -- fd, events
- print("Keyboard.OnAlert: ", alert);
+
+
+Keyboard.OnAlert = function(self, loop, fd, events)
+ --print("Keyboard.OnAlert: ", fd, events);
-- Read the keyboard device
local event = input_event();
- local bytesread = S.read(alert.fd, event, ffi.sizeof(event));
+ local bytesread, err = S.read(fd, event, ffi.sizeof(event));
+ if not bytesread then
+ return false, err
+ end
+
+ if event.type == EV_MSC then
+ if event.code == MSC_SCAN then
+ --print("MSC_SCAN: ", string.format("0x%x",event.value));
+ else
+ --print("MSC: ", event.code, event.value);
+ end
+ elseif event.type == EV_KEY then
+ if event.value == 1 and self.OnKeyDown then
+ self:OnKeyDown(event.code);
+ elseif event.value == 0 and self.OnKeyUp then
+ self:OnKeyUp(event.code);
+ elseif event.value == 2 and self.OnKeyRepeat then
+ self:OnKeyRepeat(event.code, 1);
+ end
+ else
+ --print("EVENT TYPE: ", UI.EventTypes[event.type][2], "CODE:",event.code, "VALUE: ", string.format("0x%x",event.value));
+ end
end
return Keyboard;
+
View
5 input.lua
@@ -12,8 +12,9 @@ local ffi = require "ffi"
--]]
--[[
- * The event structure itself
- --]]
+ The event structure itself
+--]]
+
ffi.cdef[[
struct input_event {
struct timeval time;
View
31 tests/test_event_loop.lua
@@ -4,17 +4,36 @@ local Keyboard = require "Keyboard"
local EventLoop = require "EventLoop"
---[[
- Create Keyboard Device
+--[[
+ React to Keyboard activity
--]]
-local loop = EventLoop.new();
-local kbd = Keyboard.new();
+OnKeyDown = function(kbd, keycode)
+ print("KEYDOWN: ", keycode);
+end
+OnKeyUp = function(kbd, keycode)
+ print("KEYUP: ", keycode);
-print("AddEmitter: ", loop:AddEmitter(kbd));
+ -- Halt the loop if they press the "Esc" key
+ if keycode == KEY_ESC then
+ loop:Halt();
+ end
+end
-loop:Run(15);
+OnKeyRepeat = function(kbd, keycode, count)
+ print("KEYREP: ", keycode, count);
+end
+-- Setup an event loop and keyboard
+loop = EventLoop.new();
+local kbd = Keyboard.new();
+-- Setup some keyboard with event handlers
+kbd.OnKeyDown = OnKeyDown;
+kbd.OnKeyUp = OnKeyUp;
+kbd.OnKeyRepeat = OnKeyRepeat;
+loop:AddObservable(kbd);
+
+loop:Run(15);

0 comments on commit e9feba8

Please sign in to comment.