diff options
author | Nat <natcl@hotmail.com> | 2007-06-20 02:20:03 (GMT) |
---|---|---|
committer | Nat <natcl@hotmail.com> | 2007-06-20 02:20:03 (GMT) |
commit | 28185285179f3e3579514e1122d80fa5288f8927 (patch) | |
tree | f1a3ca20fb021c1a1199bba45d84ad3aaff9143f /Util | |
parent | c933de48d1c89d166d9d086bfe737ecc195aa69a (diff) | |
parent | 309c7356fb5586f41786ea199db6d63da0221e34 (diff) |
Merge branch 'master' of git+ssh://natcl@dev.laptop.org/git/projects/tamtam
Diffstat (limited to 'Util')
-rw-r--r-- | Util/CSoundClient.py | 7 | ||||
-rw-r--r-- | Util/Network.py | 148 | ||||
-rw-r--r-- | Util/Trackpad.py | 46 |
3 files changed, 127 insertions, 74 deletions
diff --git a/Util/CSoundClient.py b/Util/CSoundClient.py index f4c8d84..1ef6f24 100644 --- a/Util/CSoundClient.py +++ b/Util/CSoundClient.py @@ -256,7 +256,12 @@ class _CSoundClientPlugin: instrument_id_offset = 0 elif instrument.csoundInstrumentId == Config.INST_TIED and not tied and mode == 'mini': instrument_id_offset = 0 - elif instrument.csoundInstrumentId == Config.INST_TIED and mode == 'edit': + elif instrument.csoundInstrumentId == Config.INST_TIED and tied and mode == 'edit' and duration < 0: + duration = -1 + instrument_id_offset = 0 + elif instrument.csoundInstrumentId == Config.INST_TIED and tied and mode == 'edit' and duration > 0: + instrument_id_offset = 0 + elif instrument.csoundInstrumentId == Config.INST_TIED and not tied and mode == 'edit': instrument_id_offset = 100 if instrument.csoundInstrumentId == Config.INST_SIMP and mode == 'mini': diff --git a/Util/Network.py b/Util/Network.py index 5e4cf53..85e8e5a 100644 --- a/Util/Network.py +++ b/Util/Network.py @@ -15,6 +15,7 @@ import socket import select import threading import xdrlib +import random import time import gtk @@ -37,9 +38,13 @@ MD_PEER = 2 # -1 == dynamic, first byte of data containes size # -2 == dynamic, first uint32 of data contains size message_enum = [ -("HT_LATENCY_REPLY", 0), # reply to latency test +("HT_LATENCY_REPLY", 4), # reply to latency test +("HT_SYNC_REPLY", 8), # reply to sync test +("HT_TEMPO_UPDATE", 4), # reply to sync test -("PR_LATENCY_QUERY", 0), # test latency +("PR_LATENCY_QUERY", 4), # test latency +("PR_SYNC_QUERY", 4), # test sync +("PR_TEMPO_QUERY", 0), # test sync ("MAX_MSG_ID", 0) ] @@ -50,7 +55,6 @@ MSG_SIZE = [0] i = 1 for m in message_enum: exec "%s = %d" % (m[0],i) - print m[0], i MSG_NAME.append(m[0]) MSG_SIZE.append(m[1]) i += 1 @@ -70,7 +74,6 @@ class Listener( threading.Thread ): self.exceptSockets = exceptSockets def updateSockets( self, inputSockets, outputSockets, exceptSockets ): - print "update sockets!" self.inputSockets = inputSockets self.outputSockets = outputSockets self.exceptSockets = exceptSockets @@ -78,25 +81,20 @@ class Listener( threading.Thread ): def run(self): while 1: # rely on the owner to kill us when necessary try: - print "ruuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuun" inputReady, outputReady, exceptReady = select.select( self.inputSockets, self.outputSockets, self.exceptSockets ) if self.listenerSocket in inputReady: data, s = self.listenerSocket.recvfrom(MAX_SIZE) if data == "REFRESH": - print "reeeeeeeeeeeeeeeeeeeeeeeeeeeefresh" continue if data == "CLEAR": - print "cleeeeeeeeeeeeeeeeeeeeeeeeeeear" self.inputSockets = [ self.listenerSocket ] self.outputSockets = [] self.exceptSockets = [] continue else: - print "exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxit" break # exit thread gtk.gdk.threads_enter() self.owner.processSockets( inputReady, outputReady, exceptReady ) - print "dooooooooooooooooooooooooooooooooooooooooooooooooooooooone" gtk.gdk.threads_leave() except socket.error, (value, message): print "Listener:: socket error: " + message @@ -130,10 +128,9 @@ class Network: # prepare message handlers self.processMessage = {} for i in range(1,MAX_MSG_ID): - try: - exec "self.processMessage[" + str(i) + "] = self.process" + MSG_NAME[i] - except: - print "Network:: message handler not defined for " + MSG_NAME[i] + self.processMessage[i] = [] + + self.statusWatcher = [] # data packing classes self.packer = xdrlib.Packer() @@ -152,7 +149,12 @@ class Network: self.outputSockets = [] self.exceptSockets = [] self.connection = {} # dict of connections indexed by socket - # self.processTimeout = False + + self.latencyQueryHandler = {} + self.latencyQueryStart = {} + + self.connectMessage( HT_LATENCY_REPLY, self.processHT_LATENCY_REPLY ) + self.connectMessage( PR_LATENCY_QUERY, self.processPR_LATENCY_QUERY ) self.setMode( mode, hostaddress ) @@ -194,10 +196,6 @@ class Network: self.socket = None self.hostAddress = None -# if self.processTimeout: -# gobject.source_remove( self.processTimeout ) -# self.processTimeout = False - # initialize new mode self.mode = mode @@ -206,7 +204,7 @@ class Network: try: self.socket = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) address = ("",PORT) - self.connection[socket] = Connection( self.socket, address ) + self.connection[self.socket] = Connection( self.socket, address ) self.socket.bind(address) # self.socket.setblocking(0) self.socket.listen(BACKLOG) @@ -242,7 +240,6 @@ class Network: else: self.listener = Listener( self, self.listenerSocket, self.inputSockets, self.outputSockets, self.exceptSockets ) self.listener.start() - self.send( PR_LATENCY_QUERY ) except socket.error, (value, message): if self.socket: self.socket.close() @@ -260,9 +257,6 @@ class Network: self.listenerSocket.sendto( "EXIT", ("localhost", LISTENER_PORT) ) self.listener = None - #if self.mode != MD_OFFLINE: - # self.processTimeout = gobject.timeout_add( 10, self.process ) - def addPeer( self, peer, address ): if Config.DEBUG > 1: print "Network:: adding peer: %s" % address[0] self.connection[peer] = Connection( peer, address ) @@ -275,6 +269,53 @@ class Network: self.inputSockets.remove(peer) self.listener.updateSockets( self.inputSockets, self.outputSockets, self.exceptSockets ) + # register a status watcher, format: func( self, status, args ) + def addWatcher( self, func ): + self.statusWatcher.append( func ) + + def removeWatcher( self, func ): + self.statusWatcher.remove( func ) + + # connect a message handler, format: func( self, sock, message, data ) + def connectMessage( self, message, func ): + self.processMessage[message].append(func) + + def connectMessageAfter( self, message, func, after ): + try: + ind = self.processMessage[message].index(after) + self.processMessage[message].insert(ind+1,func) + except: + print "Network::connectMessageAfter:: function not registered: " + str(after) + + def connectMessageBefore( self, message, func, before ): + try: + ind = self.processMessage[message].index(before) + self.processMessage[message].insert(ind,func) + except: + print "Network::connectMessageBefore:: function not registered: " + str(before) + + def disconnectMessage( self, message, func ): + try: + self.processMessage[message].remove(func) + except: + print "Network::disconnectMessage:: function not registered: " + str(func) + + def isOffline( self ): + if self.mode == MD_OFFLINE: return True + return False + + def isOnline( self ): + if self.mode != MD_OFFLINE: return True + return False + + def isHost( self ): + if self.mode == MD_HOST: return True + return False + + def isPeer( self ): + if self.mode == MD_PEER: return True + return False + #----------------------------------------------------------------------- # Message Senders @@ -291,7 +332,7 @@ class Network: if size >= 0: if length != size: - print "Network:: message wrong length! Got %d expected %d: %s %s" % (MSG_SIZE[message], len(data), MSG_NAME[message], data) + print "Network:: message wrong length! Got %d expected %d: %s %s" % (len(data), MSG_SIZE[message], MSG_NAME[message], data) return msg = chr(message) + data elif size == -1: @@ -307,14 +348,14 @@ class Network: if self.mode == MD_PEER: try: self.socket.send( msg ) + #print "Network:: sent %d bytes: %s" % (len(msg),msg) except socket.error, (value, errmsg): print "Network:: FAILED to send message (%s) to %s: %s" % (MSG_NAME[message], self.hostAddress[0], errmsg) # TODO something intelligent else: # MD_HOST try: to.send( msg ) - print msg - print len(msg) + #print "Network:: sent %d bytes: %s" % (len(msg),msg) except socket.error, (value, errmsg): print "Network:: FAILED to send message (%s) to %s: %s" % (MSG_NAME[message], self.connection[to].address[0], errmsg) # TODO something intelligent @@ -342,23 +383,26 @@ class Network: msg = chr(message) + self.packer.get_buffer() + data self.packer.reset() - for con in self.connection: - if con.socket == self.socket: + for sock in self.connection: + if sock == self.socket: continue try: - con.socket.send( msg ) + sock.send( msg ) except socket.error, (value, errmsg): - print "Network:: FAILED to send message (%s) to %s: %s" % (MSG_NAME[message], self.connection[to].address[0], errmsg) + print "Network:: FAILED to send message (%s) to %s: %s" % (MSG_NAME[message], self.connection[sock].address[0], errmsg) # TODO something intelligent - - - def queryLatency( self ): + + def sendLatencyQuery( self, handler ): if self.mode != MD_PEER: return - self.latencyQueryStart = time.time() - self.send(PR_LATENCY_QUERY) - + self.packer.pack_float(random.random()) + hash = self.packer.get_buffer() + self.packer.reset() + self.latencyQueryHandler[hash] = handler + self.latencyQueryStart[hash] = time.time() + self.send(PR_LATENCY_QUERY,hash) + #----------------------------------------------------------------------- # Message Handlers @@ -378,8 +422,7 @@ class Network: else: try: data = s.recv(MAX_SIZE) - print data - print len(data) + #print "Network:: recv %d bytes: %s" % (len(data), data) if not len(data): # no data to read, socket must be closed self.removePeer(s) else: @@ -395,8 +438,7 @@ class Network: if not len(data): # no data to read, socket must be closed self.setMode( MD_OFFLINE ) else: - print data - print len(data) + #print "Network:: recv %d bytes: %s" % (len(data), data) self.processStream( s, data ) except socket.error, (value, message): print "Network:: error reading data: " + message @@ -405,9 +447,6 @@ class Network: def processStream( self, sock, newData = "" ): con = self.connection[sock] con.recvBuf += newData - print "------------------------------------" - print con.recvBuf - print len(con.recvBuf), ord(con.recvBuf[0]) if con.waitingForData == -1: # message size in char con.waitingForData = ord(con.recvBuf[0]) @@ -426,7 +465,8 @@ class Network: data = con.recvBuf[0:con.waitingForData] con.recvBuf = con.recvBuf[con.waitingForData:] con.waitingForData = 0 - self.processMessage[con.message]( sock, data ) + for func in self.processMessage[con.message]: + func( sock, con.message, data ) else: return # wait for more data @@ -434,22 +474,26 @@ class Network: con.message = ord(con.recvBuf[0]) if MSG_SIZE[con.message] == 0: con.recvBuf = con.recvBuf[1:] - self.processMessage[con.message]( sock, "" ) + for func in self.processMessage[con.message]: + func( sock, con.message, "" ) else: con.waitingForData = MSG_SIZE[con.message] + con.recvBuf = con.recvBuf[1:] if len(con.recvBuf): self.processStream( sock ) #-- HOST handlers ------------------------------------------------------ - def processPR_LATENCY_QUERY( self, sock, data ): - self.send( HT_LATENCY_REPLY, to = sock ) - print "got latency query from %s" % self.connection[sock].address[0] + def processPR_LATENCY_QUERY( self, sock, message, data ): + self.send( HT_LATENCY_REPLY, data, sock ) #-- PEER handlers ------------------------------------------------------ - def processHT_LATENCY_REPLY( self, sock, data ): - latency = time.time() - self.latencyQueryStart - print "got latency reply %d" % latency*1000 + def processHT_LATENCY_REPLY( self, sock, message, data ): + t = time.time() + latency = t - self.latencyQueryStart[data] + #print "got latency reply %d" % (latency*1000) + self.latencyQueryHandler[data]( latency ) + self.latencyQueryHandler.pop(data) + self.latencyQueryStart.pop(data) - diff --git a/Util/Trackpad.py b/Util/Trackpad.py index 8d92b4a..ea2990d 100644 --- a/Util/Trackpad.py +++ b/Util/Trackpad.py @@ -33,8 +33,11 @@ class Trackpad: self.display = self.win.get_display() self.screen = gtk.gdk.Display.get_default_screen(self.display) - - + self.context = None + + def setContext(self, context): + self.context = context + def create_invisible_cursor(self): pix_data = """/* XPM */ static char * invisible_xpm[] = { @@ -46,27 +49,28 @@ class Trackpad: self.invisible_cursor = gtk.gdk.Cursor(pix,pix,color,color,0,0) def handle_motion(self,widget,event): - if event.x < 0: - X = 0 - elif event.x > self.screen.get_width(): - X = self.screen.get_width() - else: - X = event.x + if self.context != 'edit': + if event.x < 0: + X = 0 + elif event.x > self.screen.get_width(): + X = self.screen.get_width() + else: + X = event.x - if event.y < 0: - Y = 0 - elif event.y > self.screen.get_height(): - Y = self.screen.get_height() - else: - Y = event.y + if event.y < 0: + Y = 0 + elif event.y > self.screen.get_height(): + Y = self.screen.get_height() + else: + Y = event.y - self.current_x = X - self.current_y = Y - if self.buttonPressed: - self.final_x = X - self.first_x - self.final_y = Y - self.first_y - self.csnd.setTrackpadX(self.final_x) - self.csnd.setTrackpadY(self.final_y) + self.current_x = X + self.current_y = Y + if self.buttonPressed: + self.final_x = X - self.first_x + self.final_y = Y - self.first_y + self.csnd.setTrackpadX(self.final_x) + self.csnd.setTrackpadY(self.final_y) def handle_keyPress(self,widget,event): if KEY_MAP_PIANO.has_key(event.hardware_keycode) and self.buttonPressed == False: |