-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.coffee
54 lines (48 loc) · 1.7 KB
/
mouse.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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