Permalink
Browse files

modified: EventLoop.lua

	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...
1 parent 7806a42 commit 41974c95eefd4aff7f1ad096877d28e329d61f8f @Wiladams committed Nov 8, 2012
View
@@ -33,26 +33,39 @@ EventLoop.Halt = function(self)
self.Running = false;
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)
timeout = timeout or 0
self.Running = true;
while self.Running do
+ if self.OnPollBegin then
+ self.OnPollBegin(self)
+ end
+
local alerts, count = self.Emitter:EPollWait()
- 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
+
+ if self.OnPollEnd then
+ self.OnPollEnd(self, alerts, count)
end
+
-- Allow some idle work to occur
if self.OnIdle then
self.OnIdle(self)
View
@@ -1,8 +1,17 @@
local ffi = require "ffi"
+local bit = require "bit"
+local band = bit.band
+local lshift = bit.lshift
+
local S = require "syscall"
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_mt = {
__index = Keyboard,
@@ -61,5 +70,67 @@ Keyboard.OnAlert = function(self, loop, fd, events)
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;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -57,7 +57,7 @@ _IOC_WRITE = 1
_IOC_READ = 2
_IOC = function(dir,type,nr,size)
- bor(lshift(dir, _IOC_DIRSHIFT),
+ return bor(lshift(dir, _IOC_DIRSHIFT),
lshift(type, _IOC_TYPESHIFT),
lshift(nr, _IOC_NRSHIFT),
lshift(size, _IOC_SIZESHIFT))
View
@@ -1,7 +1,7 @@
local ffi = require "ffi"
--require "include/headers"
---require "include/ioctl"
+require "include/ioctl"
--[[
* Copyright (c) 1999-2002 Vojtech Pavlik
@@ -1,23 +1,56 @@
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 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
--]]
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
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
- loop:Halt();
- end
+
+ -- Halt the loop if they press the "Esc" key
+ if keycode == KEY_ESC then
+ loop:Halt();
+ end
end
OnKeyRepeat = function(kbd, keycode, count)
@@ -37,3 +70,11 @@ kbd.OnKeyRepeat = OnKeyRepeat;
loop:AddObservable(kbd);
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))));
+--]]
Oops, something went wrong.

0 comments on commit 41974c9

Please sign in to comment.