Changeset 168:71af690e0c26
- Timestamp:
- 05/12/12 11:52:03 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/mlx
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/fsuipc.py
r162 r168 308 308 self._waitConnectionRequest() 309 309 310 if self._connect() :310 if self._connect()>0: 311 311 self._handleConnection() 312 312 … … 319 319 self._requestCondition.wait() 320 320 321 def _connect(self, autoReconnection = False ):321 def _connect(self, autoReconnection = False, attempts = 0): 322 322 """Try to connect to the flight simulator via FSUIPC 323 323 … … 325 325 not due to no longer requested. 326 326 """ 327 attempts = 0328 327 while self._connectionRequested: 328 if attempts>=self.NUM_CONNECTATTEMPTS: 329 self._connectionRequested = False 330 if autoReconnection: 331 Handler._callSafe(lambda: 332 self._connectionListener.disconnected()) 333 else: 334 Handler._callSafe(lambda: 335 self._connectionListener.connectionFailed()) 336 return 0 337 329 338 try: 330 339 attempts += 1 … … 338 347 description)) 339 348 self._connected = True 340 return True349 return attempts 341 350 except Exception, e: 342 print "fsuipc.Handler._connect: connection failed: " + str(e) 351 print "fsuipc.Handler._connect: connection failed: " + str(e) + \ 352 " (attempts: %d)" % (attempts,) 343 353 if attempts<self.NUM_CONNECTATTEMPTS: 344 354 time.sleep(self.CONNECT_INTERVAL) 345 else:346 self._connectionRequested = False347 if autoReconnection:348 Handler._callSafe(lambda:349 self._connectionListener.disconnected())350 else:351 Handler._callSafe(lambda:352 self._connectionListener.connectionFailed())353 354 return False355 355 356 356 def _handleConnection(self): … … 386 386 self._connected = False 387 387 388 def _processRequest(self, request, time ):388 def _processRequest(self, request, time, attempts): 389 389 """Process the given request. 390 390 … … 409 409 except Exception as e: 410 410 print "fsuipc.Handler._processRequest: FSUIPC connection failed (" + \ 411 str(e) + "), reconnecting ."411 str(e) + "), reconnecting (attempts=%d)." % (attempts,) 412 412 needReconnect = True 413 413 … … 416 416 self._requests.insert(0, request) 417 417 self._disconnect() 418 self._connect(autoReconnection = True) 418 return self._connect(autoReconnection = True, attempts = attempts) 419 else: 420 return 0 419 421 finally: 420 422 self._requestCondition.acquire() … … 424 426 425 427 Will be called with the request lock held.""" 428 attempts = 0 426 429 while self._connectionRequested and self._periodicRequests: 427 430 self._periodicRequests.sort() … … 433 436 break 434 437 435 self._processRequest(request, t)438 attempts = self._processRequest(request, t, attempts) 436 439 437 440 while self._connectionRequested and self._requests: … … 439 442 del self._requests[0] 440 443 441 self._processRequest(request, None)444 attempts = self._processRequest(request, None, attempts) 442 445 443 446 return self._connectionRequested … … 490 493 491 494 return timestamp 495 496 @staticmethod 497 def _appendHotkeyData(data, offset, hotkey): 498 """Append the data for the given hotkey to the given array, that is 499 intended to be passed to requestWrite call on the handler.""" 500 data.append((offset + 0, "b", ord(hotkey.key))) 501 502 modifiers = 0 503 if hotkey.ctrl: modifiers |= 0x02 504 if hotkey.shift: modifiers |= 0x01 505 data.append((offset + 1, "b", modifiers)) 506 507 data.append((offset + 2, "b", 0)) 508 509 data.append((offset + 3, "b", 0)) 492 510 493 511 def __init__(self, connectionListener, connectAttempts = -1, … … 515 533 self._aircraft = None 516 534 517 self._handler = Handler( connectionListener,535 self._handler = Handler(self, 518 536 connectAttempts = connectAttempts, 519 537 connectInterval = connectInterval) 538 self._connectionListener = connectionListener 520 539 self._handler.start() 521 540 … … 537 556 self._flareStart = None 538 557 self._flareStartFS = None 558 559 self._hotkeyLock = threading.Lock() 560 self._hotkeys = None 561 self._hotkeySetID = 0 562 self._hotkeySetGeneration = 0 563 self._hotkeyOffets = None 564 self._hotkeyRequestID = None 565 self._hotkeyCallback = None 539 566 540 567 self._latin1decoder = codecs.getdecoder("iso-8859-1") … … 665 692 self._syncTime = False 666 693 self._nextSyncTime = -1 694 695 def listenHotkeys(self, hotkeys, callback): 696 """Start listening to the given hotkeys. 697 698 callback is function expecting two arguments: 699 - the ID of the hotkey set as returned by this function, 700 - the list of the indexes of the hotkeys that were pressed.""" 701 with self._hotkeyLock: 702 assert self._hotkeys is None 703 704 self._hotkeys = hotkeys 705 self._hotkeySetID += 1 706 self._hotkeySetGeneration = 0 707 self._hotkeyCallback = callback 708 709 self._handler.requestRead([(0x320c, "u")], 710 self._handleNumHotkeys, 711 (self._hotkeySetID, 712 self._hotkeySetGeneration)) 713 714 return self._hotkeySetID 715 716 def clearHotkeys(self): 717 """Clear the current hotkey set. 718 719 Note that it is possible, that the callback function set either 720 previously or after calling this function by listenHotkeys() will be 721 called with data from the previous hotkey set. 722 723 Therefore it is recommended to store the hotkey set ID somewhere and 724 check that in the callback function. Right before calling 725 clearHotkeys(), this stored ID should be cleared so that the check 726 fails for sure.""" 727 with self._hotkeyLock: 728 if self._hotkeys is not None: 729 self._hotkeys = None 730 self._hotkeySetID += 1 731 self._hotkeyCallback = None 732 self._clearHotkeyRequest() 667 733 668 734 def disconnect(self, closingMessage = None, duration = 3): … … 673 739 674 740 self._stopNormal() 741 self.clearHotkeys() 675 742 if closingMessage is None: 676 743 self._handler.disconnect() … … 678 745 self.sendMessage(closingMessage, duration = duration, 679 746 _disconnect = True) 747 748 def connected(self, fsType, descriptor): 749 """Called when a connection has been established to the flight 750 simulator of the given type.""" 751 with self._hotkeyLock: 752 if self._hotkeys is not None: 753 self._hotkeySetGeneration += 1 754 755 self._handler.requestRead([(0x320c, "u")], 756 self._handleNumHotkeys, 757 (self._hotkeySetID, 758 self._hotkeySetGeneration)) 759 self._connectionListener.connected(fsType, descriptor) 760 761 def connectionFailed(self): 762 """Called when the connection could not be established.""" 763 with self._hotkeyLock: 764 self._clearHotkeyRequest() 765 self._connectionListener.connectionFailed() 766 767 def disconnected(self): 768 """Called when a connection to the flight simulator has been broken.""" 769 with self._hotkeyLock: 770 self._clearHotkeyRequest() 771 self._connectionListener.disconnected() 680 772 681 773 def _startDefaultNormal(self): … … 915 1007 """Callback for a fuel setting request.""" 916 1008 pass 1009 1010 def _handleNumHotkeys(self, data, (id, generation)): 1011 """Handle the result of the query of the number of hotkeys""" 1012 with self._hotkeyLock: 1013 if id==self._hotkeySetID and generation==self._hotkeySetGeneration: 1014 numHotkeys = data[0] 1015 print "fsuipc.Simulator._handleNumHotkeys: numHotkeys:", numHotkeys 1016 data = [(0x3210 + i*4, "d") for i in range(0, numHotkeys)] 1017 self._handler.requestRead(data, self._handleHotkeyTable, 1018 (id, generation)) 1019 1020 def _setupHotkeys(self, data): 1021 """Setup the hiven hotkeys and return the data to be written. 1022 1023 If there were hotkeys set previously, they are reused as much as 1024 possible. Any of them not reused will be cleared.""" 1025 hotkeys = self._hotkeys 1026 numHotkeys = len(hotkeys) 1027 1028 oldHotkeyOffsets = set([] if self._hotkeyOffets is None else 1029 self._hotkeyOffets) 1030 1031 self._hotkeyOffets = [] 1032 numOffsets = 0 1033 1034 while oldHotkeyOffsets: 1035 offset = oldHotkeyOffsets.pop() 1036 self._hotkeyOffets.append(offset) 1037 numOffsets += 1 1038 1039 if numOffsets>=numHotkeys: 1040 break 1041 1042 for i in range(0, len(data)): 1043 if numOffsets>=numHotkeys: 1044 break 1045 1046 if data[i]==0: 1047 self._hotkeyOffets.append(0x3210 + i*4) 1048 numOffsets += 1 1049 1050 writeData = [] 1051 for i in range(0, numOffsets): 1052 Simulator._appendHotkeyData(writeData, 1053 self._hotkeyOffets[i], 1054 hotkeys[i]) 1055 1056 for offset in oldHotkeyOffsets: 1057 writeData.append((offset, "u", long(0))) 1058 1059 return writeData 1060 1061 def _handleHotkeyTable(self, data, (id, generation)): 1062 """Handle the result of the query of the hotkey table.""" 1063 with self._hotkeyLock: 1064 if id==self._hotkeySetID and generation==self._hotkeySetGeneration: 1065 writeData = self._setupHotkeys(data) 1066 self._handler.requestWrite(writeData, 1067 self._handleHotkeysWritten, 1068 (id, generation)) 1069 1070 def _handleHotkeysWritten(self, success, (id, generation)): 1071 """Handle the result of the hotkeys having been written.""" 1072 with self._hotkeyLock: 1073 if success and id==self._hotkeySetID and \ 1074 generation==self._hotkeySetGeneration: 1075 data = [(offset + 3, "b") for offset in self._hotkeyOffets] 1076 1077 self._hotkeyRequestID = \ 1078 self._handler.requestPeriodicRead(0.5, data, 1079 self._handleHotkeys, 1080 (id, generation)) 1081 1082 def _handleHotkeys(self, data, (id, generation)): 1083 """Handle the hotkeys.""" 1084 with self._hotkeyLock: 1085 if id!=self._hotkeySetID or generation!=self._hotkeySetGeneration: 1086 return 1087 1088 callback = self._hotkeyCallback 1089 offsets = self._hotkeyOffets 1090 1091 hotkeysPressed = [] 1092 for i in range(0, len(data)): 1093 if data[i]!=0: 1094 hotkeysPressed.append(i) 1095 1096 if hotkeysPressed: 1097 data = [] 1098 for index in hotkeysPressed: 1099 data.append((offsets[index]+3, "b", int(0))) 1100 self._handler.requestWrite(data, self._handleHotkeysCleared) 1101 1102 callback(id, hotkeysPressed) 1103 1104 def _handleHotkeysCleared(self, sucess, extra): 1105 """Callback for the hotkey-clearing write request.""" 1106 1107 def _clearHotkeyRequest(self): 1108 """Clear the hotkey request in the handler if there is any.""" 1109 if self._hotkeyRequestID is not None: 1110 self._handler.clearPeriodic(self._hotkeyRequestID) 1111 self._hotkeyRequestID = None 917 1112 918 1113 #------------------------------------------------------------------------------ -
src/mlx/gui/gui.py
r164 r168 152 152 self._lastLoadedPIREP = None 153 153 154 self._hotkeySetID = None 155 154 156 @property 155 157 def mainWindow(self): … … 320 322 self._wizard.connected(fsType, descriptor) 321 323 self._reconnecting = False 324 self._listenHotkeys() 322 325 323 326 def connectionFailed(self): … … 402 405 """Disconnect from the simulator if connected.""" 403 406 self.stopMonitoring() 407 self._clearHotkeys() 404 408 405 409 if self._connected: … … 875 879 def _editPreferences(self, menuItem): 876 880 """Callback for editing the preferences.""" 881 self._clearHotkeys() 877 882 self._preferences.run(self.config) 878 883 self._setupTimeSync() 884 self._listenHotkeys() 879 885 880 886 def _setupTimeSync(self): … … 1106 1112 if callback is not None: 1107 1113 callback(returned, result) 1114 1115 def _listenHotkeys(self): 1116 """Setup the hotkeys based on the configuration.""" 1117 if self._hotkeySetID is None and self._simulator is not None: 1118 self._hotkeySetID = \ 1119 self._simulator.listenHotkeys([self.config.pilotHotkey, 1120 self.config.checklistHotkey], 1121 self._handleHotkeys) 1122 1123 def _clearHotkeys(self): 1124 """Clear the hotkeys.""" 1125 if self._hotkeySetID is not None: 1126 self._hotkeySetID=None 1127 self._simulator.clearHotkeys() 1128 1129 def _handleHotkeys(self, id, hotkeys): 1130 """Handle the hotkeys.""" 1131 if id==self._hotkeySetID: 1132 print "gui.GUI._handleHotkeys", hotkeys -
src/mlx/gui/prefs.py
r167 r168 40 40 labelAlignment = gtk.Alignment(xalign = 0.0, yalign = 0.5, 41 41 xscale = 0.0, yscale = 0.0) 42 labelAlignment.set_padding(padding_top = 0, padding_bottom = 0, 43 padding_left = 0, padding_right = 4) 42 44 labelAlignment.add(label) 43 self.pack_start(labelAlignment, False, False, 8)45 self.pack_start(labelAlignment, False, False, 0) 44 46 45 47 self._ctrl = gtk.CheckButton("Ctrl") … … 54 56 55 57 self._hotkeyModel = gtk.ListStore(str) 56 for keyCode in range(ord("0"), ord("9") ) + range(ord("A"), ord("Z")):58 for keyCode in range(ord("0"), ord("9")+1) + range(ord("A"), ord("Z")+1): 57 59 self._hotkeyModel.append([chr(keyCode)]) 58 60
Note:
See TracChangeset
for help on using the changeset viewer.