Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 41974c95eefd4aff7f1ad096877d28e329d61f8f 1 parent 7806a42
William Adams authored
33 EventLoop.lua
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)
71 Keyboard.lua
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;
0  ev.lua → experimental/ev.lua
View
File renamed without changes
0  ev_utils.lua → experimental/ev_utils.lua
View
File renamed without changes
0  libev.lua → experimental/libev.lua
View
File renamed without changes
0  test_libev.lua → experimental/test_libev.lua
View
File renamed without changes
2  include/ioctl.lua
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))
2  input.lua
View
@@ -1,7 +1,7 @@
local ffi = require "ffi"
--require "include/headers"
---require "include/ioctl"
+require "include/ioctl"
--[[
* Copyright (c) 1999-2002 Vojtech Pavlik
53 tests/test_event_loop.lua
View
@@ -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))));
+--]]
112 tests/test_input.c
View
@@ -15,7 +15,8 @@
#include <linux/input.h>
-#define KEYBOARDEVENTS "/dev/input/event1"
+#define KEYBOARDEVENTS "/dev/input/event0"
+#define MOUSEEVENTS "/dev/input/event1"
#define test_bit(yalv, abs_b) ((((char *)abs_b)[yalv/8] & (1<<yalv%8)) > 0)
@@ -104,10 +105,10 @@ void Listing6(fd)
printf("The device on says its identity is %s\n", uniq);
}
-/*
+
void Listing7(fd)
{
- struct input_event evtype_b;
+ unsigned char evtype_b[EV_MAX/8 + 1];
int yalv;
memset(evtype_b, 0, sizeof(evtype_b));
@@ -173,7 +174,7 @@ void Listing7(fd)
}
}
}
-*/
+
void Listing8(fd)
{
@@ -211,13 +212,14 @@ void Listing9(fd)
/* we turn off all the LEDs to start */
ev.type = EV_LED;
- ev.code = LED_CAPSL;
ev.value = 0;
+ ev.code = LED_CAPSL;
int retval = write(fd, &ev, sizeof(struct input_event));
ev.code = LED_NUML;
retval = write(fd, &ev, sizeof(struct input_event));
+
ev.code = LED_SCROLLL;
retval = write(fd, &ev, sizeof(struct input_event));
@@ -236,6 +238,14 @@ void Listing9(fd)
usleep(200000);
ev.value = 0;
write(fd, &ev, sizeof(struct input_event));
+
+ ev.code = LED_SCROLLL;
+ ev.value = 1;
+ write(fd, &ev, sizeof(struct input_event));
+ usleep(200000);
+ ev.value = 0;
+ write(fd, &ev, sizeof(struct input_event));
+
}
}
@@ -385,27 +395,30 @@ void Listing17(fd)
{
printf(" Absolute axis 0x%02x ", yalv);
switch ( yalv)
- {
+ {
case ABS_X :
- printf("(X Axis) ");
- break;
+ printf("(X Axis) ");
+ break;
+
case ABS_Y :
- printf("(Y Axis) ");
- break;
+ printf("(Y Axis) ");
+ break;
+
default:
printf("(Unknown abs feature)");
- }
+ }
+
if(ioctl(fd, EVIOCGABS(yalv), &abs_feat))
{
perror("evdev EVIOCGABS ioctl");
}
printf("%d (min:%d max:%d flat:%d fuzz:%d)",
- abs_feat.value,
- abs_feat.minimum,
- abs_feat.maximum,
- abs_feat.flat,
- abs_feat.fuzz);
+ abs_feat.value,
+ abs_feat.minimum,
+ abs_feat.maximum,
+ abs_feat.flat,
+ abs_feat.fuzz);
printf("\n");
}
@@ -413,30 +426,69 @@ void Listing17(fd)
}
+int test_device(const char * devicename)
+{
+ int dev;
+
+ if ((dev = open(devicename, O_RDWR)) == -1) {
+ perror("opening device");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("Testing Device: %s\n", devicename);
+
+ Listing1(dev);
+ Listing3(dev);
+ Listing4(dev);
+ Listing5(dev);
+ Listing6(dev);
+ Listing7(dev);
+ Listing12(dev);
+
+ close(dev);
+
+ return 0;
+}
+
+int test_mouse(const char *devicename)
+{
+ int dev;
+
+ if ((dev = open(devicename, O_RDWR)) == -1) {
+ perror("opening device");
+ exit(EXIT_FAILURE);
+ }
+ Listing17(dev);
-int main (void)
+ close(dev);
+
+ return 0;
+}
+
+int flash_keyboard(const char *devicename)
{
- static const bufflen = 256;
- char buff[bufflen];
- int kbd;
- struct input_event ie;
+ int dev;
- if ((kbd = open(KEYBOARDEVENTS, O_RDONLY)) == -1) {
+ if ((dev = open(devicename, O_RDWR)) == -1) {
perror("opening device");
exit(EXIT_FAILURE);
}
-
- Listing1(kbd);
- Listing3(kbd);
- Listing4(kbd);
- Listing5(kbd);
- Listing6(kbd);
- Listing11(kbd);
+ Listing9(dev);
- close(kbd);
+ return 0;
+}
+
+int main (void)
+{
+ //test_device(KEYBOARDEVENTS);
+ //test_device(MOUSEEVENTS);
+ test_mouse(MOUSEEVENTS);
+
+ //flash_keyboard(KEYBOARDEVENTS);
+
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.