# Copyright 2010, Eric Piel # This file contains part of touchd - 2008, Scott Shawcroft, scott.shawcroft@gmail.com # This file uses pyinputevent available http://github.com/rmt/pyinputevent/ by Robert Thomson and individual contributors. # FingerViewer is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # FingerViewer is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with FingerViewer. If not, see . # Runs the finger view window. # use a command like: # sudo python fingerviewer.py /dev/input/event1 import pygtk import gtk import math import time import threading, thread from pyinputevent import * gtk.gdk.threads_init() class MouseDevice(SimpleDevice): """ Report Multitouch events """ def __init__(self, viewer, *args, **kwargs): SimpleDevice.__init__(self, *args, **kwargs) self.viewer = viewer self.x = 0 self.y = 0 self.num = 0 def receive(self, event): etype, ecode, evalue = ( event.etype, event.ecode, event.evalue, ) #print "%s" % event if etype == 3: if ecode == 53: self.x = evalue elif ecode == 54: self.y = evalue elif etype == 0: if ecode == 2: viewer.got_finger(self.num, self.x, self.y, 0, 0, 10) self.num += 1 elif ecode == 0: self.num = 0 class FingerViewer: def __init__(self): self.window = gtk.Window() self.window.set_title("Fingers") self.width = 400 self.height = 200 self.window.width = self.width self.window.height = self.height self.window.connect("destroy",self.hide) self.window.show() self.image = gtk.Image() self.image.show() self.window.add(self.image) pixmap = gtk.gdk.Pixmap(None,self.width,self.height,24) self.image.set_from_pixmap(pixmap,None) self.context = pixmap.cairo_create() self.context.rectangle(0,0,self.width,self.height) self.context.set_source_rgb(1,1,1) self.context.fill() self.context.set_line_width(2) self.fingers = [] def got_finger(self,num,x,y,dx,dy,p): if num==0: # update the image self.draw_all() self.fingers.append((num,x,y,dx,dy,p)) def draw_all(self): self.context.rectangle(0,0,self.width,self.height) # fill with white self.context.set_source_rgb(1,1,1) self.context.fill() for i in range(len(self.fingers)): num,x,y,dx,dy,p = self.fingers[i] self.draw_finger(num,x,y,dx,dy,p) self.image.queue_draw() self.fingers = [] def draw_finger(self,num,x,y,dx,dy,p): dy*=-1 # draw the finger tx = self.translate_x(x) ty = self.translate_y(y) self.context.arc(tx,ty,10+p/500.0*20,0,2*math.pi) self.context.set_source_rgba(0,0,1) self.context.fill() # draw the velocity VRES = 500 VSCALE = 100 self.context.set_source_rgb(0,0,0) self.context.move_to(tx,ty) self.context.line_to(tx+(float(dx)/VRES)*VSCALE,ty-(float(dy)/VRES)*VSCALE) self.context.stroke() # draw number label self.context.set_source_rgb(1,1,1) self.context.move_to(tx-5,ty+5) self.context.set_font_size(15) self.context.show_text(str(num+1)) def num_fingers(self,i): if i==0: self._wipe() # translate to the graphical coordinates def translate_x(self,v): XRES = 1152 return ((float(v)/XRES)*(self.width)) # translate to the graphical coordinates def translate_y(self,v): YRES = 768 YOFF = 0 return ((float(v+YOFF)/YRES)*(self.height)) def hide(self,widget): #self.window.hide() gtk.main_quit() def readValues(self, args): import select controller = Controller("Controller") fds = {} poll = select.poll() dev = args print dev dev = MouseDevice(self, dev) fds[dev.fileno()] = dev poll.register(dev, select.POLLIN | select.POLLPRI) while True: for x,e in poll.poll(): dev = fds[x] dev.read() if __name__ == "__main__": viewer = FingerViewer() threading.Thread(target=viewer.readValues, args=sys.argv[1:]).start() gtk.main() # vim:shiftwidth=4:expandtab:spelllang=en_gb:spell: