Skip to content

Commit

Permalink
modified: EventLoop.lua
Browse files Browse the repository at this point in the history
	modified:   Keyboard.lua
	renamed:    ev.lua -> experimental/ev.lua
	renamed:    ev_utils.lua -> experimental/ev_utils.lua
	renamed:    libev.lua -> experimental/libev.lua
	renamed:    test_libev.lua -> experimental/test_libev.lua
	modified:   include/ioctl.lua
	modified:   input.lua
	modified:   tests/test_event_loop.lua
	modified:   tests/test_input.c
  • Loading branch information
Wiladams committed Nov 8, 2012
1 parent 7806a42 commit 41974c9
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 48 deletions.
33 changes: 23 additions & 10 deletions EventLoop.lua
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,26 +33,39 @@ EventLoop.Halt = function(self)
self.Running = false; self.Running = false;
end end


EventLoop.OnPollEnd = function(self, alerts, count)
if alerts and count > 0 then
for i=0,count-1 do
--print("Event: ", alerts[i].data.fd, alerts[i].events);

-- get the appropriate observer
local observer = self.Observers[alerts[i].data.fd];
if observer and observer.OnAlert then
observer:OnAlert(self, alerts[i].data.fd, alerts[i].events)
end
end
end
end


EventLoop.Run = function(self, timeout) EventLoop.Run = function(self, timeout)
timeout = timeout or 0 timeout = timeout or 0


self.Running = true; self.Running = true;


while self.Running do while self.Running do
if self.OnPollBegin then
self.OnPollBegin(self)
end

local alerts, count = self.Emitter:EPollWait() local alerts, count = self.Emitter:EPollWait()


if alerts and count > 0 then
for i=0,count-1 do if self.OnPollEnd then
--print("Event: ", alerts[i].data.fd, alerts[i].events); self.OnPollEnd(self, alerts, count)

-- get the appropriate observer
local observer = self.Observers[alerts[i].data.fd];
if observer and observer.OnAlert then
observer:OnAlert(self, alerts[i].data.fd, alerts[i].events)
end
end
end end



-- Allow some idle work to occur -- Allow some idle work to occur
if self.OnIdle then if self.OnIdle then
self.OnIdle(self) self.OnIdle(self)
Expand Down
71 changes: 71 additions & 0 deletions Keyboard.lua
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,17 @@
local ffi = require "ffi" local ffi = require "ffi"
local bit = require "bit"
local band = bit.band
local lshift = bit.lshift



local S = require "syscall" local S = require "syscall"
local UI = require "input" local UI = require "input"


test_bit = function(yalv, abs_b)
return (band(ffi.cast("const uint8_t *",abs_b)[yalv/8], lshift(1, yalv%8)) > 0)
end


local Keyboard = {} local Keyboard = {}
local Keyboard_mt = { local Keyboard_mt = {
__index = Keyboard, __index = Keyboard,
Expand Down Expand Up @@ -61,5 +70,67 @@ Keyboard.OnAlert = function(self, loop, fd, events)
end end
end end


--[[
Get the current state of all the keys
What is returned is a bitfield with KEY_MAX entries
You can find out the state of any key by checking to
see if the appropriate bit is set.
--]]
Keyboard.GetKeys = function(self)
-- Maybe this buffer should be allocated
-- only once, and simply zeroed for each call
local key_b = ffi.new("unsigned char [?]",KEY_MAX/8 + 1);

success, err = S.ioctl(self.AlertHandle:getfd(), EVIOCGKEY(ffi.sizeof(key_b)), key_b);

if not success then
return false, err
end

return key_b;
end

Keyboard.IsKeyPressed = function(self, keycode)
local keys, err = self:GetKeys()

if not keys then
return false, err
end

return test_bit(keycode, keys);
end

--[[
Return the state of all the LEDs on the keyboard
--]]
Keyboard.GetLEDs = function(self)
end

Keyboard.SetLED = function(self, whichled, state)
local event = input_event();
event.type = EV_LED;
event.code = whichled;

if state then
event.value = 1;
else
event.value = 0;
end

return self.AlertHandle:write(event, ffi.sizeof(event));
end

Keyboard.SetCapsLock = function(self, state)
self:SetLED(LED_CAPSL, state)
end

Keyboard.SetNumLock = function(self, state)
self:SetLED(LED_NUML, state)
end

Keyboard.SetScrollLock(state)
self:SetLED(LED_SCROLLL, state)
end

return Keyboard; return Keyboard;


File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion include/ioctl.lua
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ _IOC_WRITE = 1
_IOC_READ = 2 _IOC_READ = 2


_IOC = function(dir,type,nr,size) _IOC = function(dir,type,nr,size)
bor(lshift(dir, _IOC_DIRSHIFT), return bor(lshift(dir, _IOC_DIRSHIFT),
lshift(type, _IOC_TYPESHIFT), lshift(type, _IOC_TYPESHIFT),
lshift(nr, _IOC_NRSHIFT), lshift(nr, _IOC_NRSHIFT),
lshift(size, _IOC_SIZESHIFT)) lshift(size, _IOC_SIZESHIFT))
Expand Down
2 changes: 1 addition & 1 deletion input.lua
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@


local ffi = require "ffi" local ffi = require "ffi"
--require "include/headers" --require "include/headers"
--require "include/ioctl" require "include/ioctl"


--[[ --[[
* Copyright (c) 1999-2002 Vojtech Pavlik * Copyright (c) 1999-2002 Vojtech Pavlik
Expand Down
53 changes: 47 additions & 6 deletions tests/test_event_loop.lua
Original file line number Original file line Diff line number Diff line change
@@ -1,23 +1,56 @@
package.path = package.path..";../?.lua" package.path = package.path..";../?.lua"


local ffi = require "ffi"
local bit = require "bit"
local band = bit.band
local lshift = bit.lshift

local Keyboard = require "Keyboard" local Keyboard = require "Keyboard"
local EventLoop = require "EventLoop" local EventLoop = require "EventLoop"


test_bit = function(yalv, abs_b)
return (band(ffi.cast("const uint8_t *",abs_b)[yalv/8], lshift(1, yalv%8)) > 0)
end


--[[ --[[
React to Keyboard activity React to Keyboard activity
--]] --]]
OnKeyDown = function(kbd, keycode) OnKeyDown = function(kbd, keycode)
print("KEYDOWN: ", keycode); keys,err = kbd:GetKeys();
if not keys then
print("GetKeys Error: ", err);
end

if kbd:IsKeyPressed(KEY_LEFTSHIFT) then
print("LSHIFT");
elseif kbd:IsKeyPressed(KEY_RIGHTSHIFT) then
print("RSHIFT");
else
print("KEYDOWN: ", keycode);
end
end end


OnKeyUp = function(kbd, keycode) OnKeyUp = function(kbd, keycode)
print("KEYUP: ", keycode); keys,err = kbd:GetKeys();
if not keys then
print("GetKeys Error: ", err);
end

if kbd:IsKeyPressed(KEY_LEFTSHIFT) then
print("LSHIFT");
end

if kbd:IsKeyPressed(KEY_RIGHTSHIFT) then
print("RSHIFT");
end

print("KEYUP: ", keycode);


-- Halt the loop if they press the "Esc" key
if keycode == KEY_ESC then -- Halt the loop if they press the "Esc" key
loop:Halt(); if keycode == KEY_ESC then
end loop:Halt();
end
end end


OnKeyRepeat = function(kbd, keycode, count) OnKeyRepeat = function(kbd, keycode, count)
Expand All @@ -37,3 +70,11 @@ kbd.OnKeyRepeat = OnKeyRepeat;
loop:AddObservable(kbd); loop:AddObservable(kbd);


loop:Run(15); loop:Run(15);

--[[
print("Type of _IOC: ", type(_IOC));
print("Type of EVIOCGKEY: ", type(EVIOCGKEY));
local key_b = ffi.new("unsigned char [?]",KEY_MAX/8 + 1);
print("Size of key_b: ", ffi.sizeof(key_b));
print("VALUE of EVIOCGKEY(): ", string.format("0x%x",EVIOCGKEY(ffi.sizeof(key_b))));
--]]
Loading

0 comments on commit 41974c9

Please sign in to comment.