Skip to content

Instantly share code, notes, and snippets.

@alejandro
Created August 19, 2012 14:21
Show Gist options
  • Select an option

  • Save alejandro/3395054 to your computer and use it in GitHub Desktop.

Select an option

Save alejandro/3395054 to your computer and use it in GitHub Desktop.
Mouse input parser with luvit
local Mouse = require('./mouse').Mouse
local table = require('table')
local mouse = Mouse:new('/dev/input/mouse1')
mouse:on('connected', function (dt)
debug('mouse connected')
local actions = {'moved', 'button'}
-- register actions
table.foreach(actions, function (i, v)
mouse:on(v, debug)
end)
end)
process:on('error', function (err)
p('Error', err)
end)
--[[
@ Mouse bindings with luvit
@ As seen on http://www.computer-engineering.org/ps2mouse/ and
@ https://github.com/luvit/luvit/blob/master/examples/linux-joystick.lua
@ Alejandro Morales <vamg008@gmail.com>
@ license WTFPL
]]
local fs = require('fs')
local Emitter = require('core').Emitter
local Buffer = require('buffer').Buffer
local find = require('string').find
local Bit = require('bit')
local Mouse = Emitter:extend() -- Emitter instance
function Mouse:initialize(id, target, ...)
-- Emitter:wrap ->
-- Utility that binds the named method `self[name]` for use as a callback. The
-- first argument (`err`) is re-routed to the "error" event instead.
self:wrap('on_open')
self:wrap('on_read')
-- default configuration
self.id = id or 1 -- default to mouse1
self.target = target or '/dev/input/mouse'
self.fullId = self.target .. self.id
if target == nil and find(id, '/') then -- check for full path on first arg
p('oh full path:', id)
self.fullId = id
end
fs.open(self.fullId, 'r', '0644', self.on_open) -- "open" the device
end
function Mouse:on_open(fd)
self.fd = fd
self:emit('connected', { id = self.fullId, fd = self.fd, constructor = self })
self:start_read()
end
function Mouse:start_read()
fs.read(self.fd, nil, 8, self.on_read)
end
function Mouse:parse(buffer)
local event = {
-- Bit.band -> bitwise AND
leftBtn = Bit.band(buffer[1], 1 ) > 0,
rightBtn = Bit.band(buffer[1], 2 ) > 0,
middleBtn = Bit.band(buffer[1], 4 ) > 0,
xSign = Bit.band(buffer[1], 16 ) > 0,
ySign = Bit.band(buffer[1], 32 ) > 0,
xOverflow = Bit.band(buffer[1], 64 ) > 0,
yOverflow = Bit.band(buffer[1], 128) > 0,
xDelta = buffer:readInt8(1),
yDelta = buffer:readInt8(2)
}
if event.leftBtn or event.rightBtn or event.middleBtn then
event.type = 'button'
else
event.type = 'moved'
end
return event
end
function Mouse:on_read(chunk)
local event = Mouse:parse(Buffer:new(chunk))
self:emit(event.type, event)
if self.fd then self:start_read() end
end
function Mouse:close(callback)
local fd = self.fd
self.fd = nil
fs.close(fd, callback)
end
-- Expose the Api
return { Mouse = Mouse}
return {
author = 'Alejandro Morales <vamg008@gmail.com>',
name = 'Mouse handler',
version = '0.0.1',
description = 'Mouse input parser for luvit',
main = 'mouse.lua'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment