Created
December 17, 2012 15:26
-
-
Save cheery/4319107 to your computer and use it in GitHub Desktop.
WIP demonstration desktop environment written over the bindings in my repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
class EvDevIO | |
constructor: (path, callback) -> | |
@buffer = new Buffer 16 | |
@view = new DataView @buffer | |
fs.open path, "r+", (err, @fd) => | |
if err then return callback(err, null) | |
@_read_loop(callback) | |
_decode_buffer: () -> | |
return { | |
sec: @view.getInt32(0, true) | |
usec: @view.getInt32(4, true) | |
type: @view.getUint16(8, true) | |
code: @view.getUint16(10, true) | |
value: @view.getInt32(12, true) | |
} | |
_read_loop: (callback) -> | |
fs.read @fd, @buffer, 0, 16, null, (err, bytesRead) => | |
if err and err.code == "ENODEV" | |
return @close() | |
if err then return callback(err, null) | |
if bytesRead != 16 then return callback("not enough bytes read", null) | |
callback null, @_decode_buffer() | |
@_read_loop(callback) | |
close: () -> | |
if @fd? then fs.close(@fd) | |
delete @fd | |
module.exports = EvDevIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dg = require 'dg' | |
#surface = dg.getFullscreen {} | |
#gl = surface.getContext 'webgl' | |
# | |
#gl.clearColor 1.0, 1.0, 1.0, 1.0 | |
#gl.clear gl.COLOR_BUFFER_BIT | |
#gl.swapBuffers() | |
# | |
#exit = () -> | |
# | |
#setTimeout exit, 2000 | |
udev = require 'udev' | |
fs = require 'fs' | |
Mouse = require './mouse' | |
register_mouse = (mouse) -> | |
mouse.on "motion", (position, velocity) -> | |
console.log "motion", position, velocity | |
mouse.on "wheel_motion", (position, velocity) -> | |
console.log "wheel_motion", position, velocity | |
mouse.on "button_press", (button, buttons) -> | |
console.log "button_press", button, buttons | |
mouse.on "button_release", (button, buttons) -> | |
console.log "button_release", button, buttons | |
mice = {} | |
ismouse = (device) -> | |
ok = true | |
ok &&= device.syspath.match(/event[0-9]+$/) | |
ok &&= device.SUBSYSTEM == "input" | |
ok &&= device.ID_INPUT == "1" | |
ok &&= device.ID_INPUT_MOUSE == "1" | |
return ok | |
for device in udev.list() | |
if ismouse device | |
console.log "#{device.ID_SERIAL} exists at path #{device.ID_PATH}" | |
mice[device.ID_PATH] = mouse = new Mouse device | |
register_mouse mouse | |
monitor = udev.monitor() | |
monitor.on 'add', (device) -> | |
if ismouse device | |
console.log "#{device.ID_SERIAL} added at path #{device.ID_PATH}" | |
mice[device.ID_PATH] = mouse = new Mouse device | |
register_mouse mouse | |
monitor.on 'remove', (device) -> | |
if ismouse device | |
console.log "#{device.ID_SERIAL} removed at path #{device.ID_PATH}" | |
mouse = mice[device.ID_PATH] | |
if mouse? then mouse.close() | |
delete mice[device.ID_PATH] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EvDevIO = require './evdev' | |
EventEmitter = require("events").EventEmitter | |
class Mouse extends EventEmitter | |
constructor: (@device) -> | |
@path = @device.ID_PATH | |
@position = x: 0, y: 0 | |
@accum = x: 0, y: 0 | |
@wheel = 0 | |
@wheel_accum = 0 | |
@buttons = left: false, middle: false, right: false | |
@io = new EvDevIO @device.DEVNAME, (err, event) => | |
if err then throw err | |
switch event.type | |
when 2 then @_motion(event) | |
when 0 then @_motion_flush(event) | |
when 1 then @_button(event) | |
when 4 | |
else | |
console.log event | |
_motion: (event) -> | |
switch event.code | |
when 0 then @accum.x += event.value | |
when 1 then @accum.y += event.value | |
when 8 then @wheel_accum += event.value | |
_motion_flush: (event) -> | |
@position.x += @accum.x | |
@position.y += @accum.y | |
@wheel += @wheel_accum | |
if @accum.x != 0 or @accum.y != 0 | |
@emit "motion", @position, @accum | |
if @wheel_accum != 0 | |
@emit "wheel_motion", @wheel, @wheel_accum | |
@accum = x: 0, y: 0 | |
@wheel_accum = 0 | |
_button: (event) -> | |
button = { name: null, value: (event.value == 1) } | |
switch event.code | |
when 272 then button.name = "left" | |
when 274 then button.name = "middle" | |
when 273 then button.name = "right" | |
@buttons[button.name] = button.value | |
if button.value | |
@emit "button_press", button.name, @buttons | |
else | |
@emit "button_release", button.name, @buttons | |
close: () -> | |
@io.close() | |
module.exports = Mouse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment