Package brailleDisplayDrivers :: Module brltty
[hide private]
[frames] | no frames]

Source Code for Module brailleDisplayDrivers.brltty

  1  #brailleDisplayDrivers/brltty.py 
  2  #A part of NonVisual Desktop Access (NVDA) 
  3  #This file is covered by the GNU General Public License. 
  4  #See the file COPYING for more details. 
  5  #Copyright (C) 2008-2010 James Teh <jamie@jantrid.net> 
  6   
  7  import time 
  8  import wx 
  9  import braille 
 10  from logHandler import log 
 11  import inputCore 
 12  try: 
 13          import brlapi 
 14          BRLAPI_CMD_KEYS = dict((code, name[8:].lower()) 
 15                  for name, code in brlapi.__dict__.iteritems() if name.startswith("KEY_CMD_")) 
 16  except ImportError: 
 17          pass 
 18   
 19  KEY_CHECK_INTERVAL = 50 
20 21 -class BrailleDisplayDriver(braille.BrailleDisplayDriver):
22 """brltty braille display driver. 23 """ 24 name = "brltty" 25 description = "brltty" 26 27 @classmethod
28 - def check(cls):
29 try: 30 brlapi 31 return True 32 except NameError: 33 pass 34 return False
35
36 - def __init__(self):
37 super(BrailleDisplayDriver, self).__init__() 38 self._con = brlapi.Connection() 39 self._con.enterTtyModeWithPath() 40 self._keyCheckTimer = wx.PyTimer(self._handleKeyPresses) 41 self._keyCheckTimer.Start(KEY_CHECK_INTERVAL) 42 # BRLTTY simulates key presses for braille typing keys, so let BRLTTY handle them. 43 # NVDA may eventually implement this itself, but there's no reason to deny BRLTTY users this functionality in the meantime. 44 self._con.ignoreKeys(brlapi.rangeType_type, (long(brlapi.KEY_TYPE_SYM),))
45
46 - def terminate(self):
47 super(BrailleDisplayDriver, self).terminate() 48 # Exceptions might be raised if initialisation failed. Just ignore them. 49 try: 50 self._keyCheckTimer.Stop() 51 self._keyCheckTimer = None 52 except: 53 pass 54 try: 55 # Give BRLTTY a chance to write the last piece of data to the display. 56 time.sleep(0.05) 57 self._con.leaveTtyMode() 58 except: 59 pass
60
61 - def _get_numCells(self):
62 return self._con.displaySize[0]
63
64 - def display(self, cells):
65 cells = "".join(chr(cell) for cell in cells) 66 # HACK: Temporarily work around a bug which causes brltty to freeze if data is written while there are key presses waiting. 67 # Simply consume and act upon any waiting key presses. 68 self._handleKeyPresses() 69 self._con.writeDots(cells)
70
71 - def _handleKeyPresses(self):
72 while True: 73 try: 74 key = self._con.readKey(False) 75 except: 76 log.error("Error reading key press from brlapi", exc_info=True) 77 return 78 if not key: 79 break 80 key = self._con.expandKeyCode(key) 81 self._onKeyPress(key)
82
83 - def _onKeyPress(self, key):
84 keyType = key["type"] 85 command = key["command"] 86 argument = key["argument"] 87 if keyType == brlapi.KEY_TYPE_CMD: 88 try: 89 inputCore.manager.executeGesture(InputGesture(command, argument)) 90 except inputCore.NoInputGestureAction: 91 pass
92 93 gestureMap = inputCore.GlobalGestureMap({ 94 "globalCommands.GlobalCommands": { 95 "braille_scrollBack": ("br(brltty):fwinlt",), 96 "braille_scrollForward": ("br(brltty):fwinrt",), 97 "braille_previousLine": ("br(brltty):lnup",), 98 "braille_nextLine": ("br(brltty):lndn",), 99 "braille_routeTo": ("br(brltty):route",), 100 } 101 })
102
103 -class InputGesture(braille.BrailleDisplayGesture):
104 105 source = BrailleDisplayDriver.name 106
107 - def __init__(self, command, argument):
108 super(InputGesture, self).__init__() 109 self.id = BRLAPI_CMD_KEYS[command] 110 if command == brlapi.KEY_CMD_ROUTE: 111 self.routingIndex = argument
112