Skip to content
Commits on Source (2)
......@@ -145,7 +145,7 @@ class EngineBraille(IBus.Engine):
debug(2, "focus out")
self.__commit()
self.enabled = False
self.brlapi_disable()
self.brlapi_set()
def do_process_key_event(self, keysym, scancode, state):
"""
......@@ -168,10 +168,8 @@ class EngineBraille(IBus.Engine):
if is_press:
self.enabled = not self.enabled
self.math = keysym == XK_Shift_R or keysym == XK_Control_R
if self.enabled:
self.brlapi_enable()
else:
self.brlapi_disable()
self.brlapi_set()
if not self.enabled:
self.__commit()
debug(2, "enabled" if self.enabled else "disabled")
debug(2, "math" if self.math else "text")
......@@ -320,7 +318,7 @@ class EngineBraille(IBus.Engine):
self.__process_dot_key(keysym - XK_braille_dot_1 + 1, is_press)
return True
elif keysym & ~0xff == XK_braille_blank:
self.__process_pattern(keysym & 0xffff)
self.__process_pattern(keysym & 0xff)
return True
else:
if self.contracted:
......@@ -331,33 +329,66 @@ class EngineBraille(IBus.Engine):
def brlapi_init(self):
"""Initialization, called only once at startup"""
def _brlapi_init():
"""Try to (re)connect to BrlAPI"""
self.brlapi_connection = brlapi.Connection()
self.brlapi_connection.enterTtyModeWithPath()
self.brlapi_set()
GLib.io_add_watch(self.brlapi_connection.fileDescriptor, GLib.IOCondition.IN, process_brlapi)
def _brlapi_reconnect():
"""Callback which tries to reconnect to BrlAPI, to be called at
regular interval"""
try:
_brlapi_init()
debug(4, "Reconnected to BrlAPI")
return False
except:
return True
def process_brlapi(fd, cond):
# TODO: handle brltty restarts
k = self.brlapi_connection.readKey(wait = False)
"""The BrlAPI fd is available for read, process it"""
try:
k = self.brlapi_connection.readKey(wait = False)
except:
debug(4, "BrlAPI connection lost, trying to reconnect...")
try:
self.brlapi_connection.closeConnection()
except:
pass
self.brlapi_connection = None
GLib.timeout_add_seconds(1, _brlapi_reconnect)
return False
if k == None:
# Processed some stuff, but no key event
# Processed some data, but no key event in the pipe
return True
dots = k & brlapi.KEY_CMD_ARG_MASK
debug(4, "got braille %x from BrlAPI" % dots)
self.__process_pattern(dots)
return True
"""At initialization, only try to connect to BrlAPI once, and if not
available, assume that it will never be"""
try:
self.brlapi_connection = brlapi.Connection()
self.brlapi_connection.enterTtyModeWithPath()
self.brlapi_disable()
GLib.io_add_watch(self.brlapi_connection.fileDescriptor, GLib.IOCondition.IN, process_brlapi)
_brlapi_init()
except:
debug(4, "BrlAPI not available")
pass
def brlapi_enable(self):
# TODO: clean and copy this example in python bindings doc
if self.brlapi_connection != None:
self.brlapi_connection.acceptKeyRanges([(brlapi.KEY_TYPE_CMD|brlapi.KEY_CMD_PASSDOTS, brlapi.KEY_TYPE_CMD|brlapi.KEY_CMD_PASSDOTS|brlapi.KEY_CMD_ARG_MASK)])
def brlapi_set(self):
"""Update whether BrlAPI should pass us the dot patterns, or let
something else (e.g. brltty+xbrlapi) process them"""
if self.brlapi_connection == None:
# Not connected
return
def brlapi_disable(self):
if self.brlapi_connection != None:
self.brlapi_connection.ignoreKeys(brlapi.rangeType_all,[0])
if self.enabled:
self.brlapi_connection.acceptKeyRanges([(brlapi.KEY_TYPE_CMD|brlapi.KEY_CMD_PASSDOTS, brlapi.KEY_TYPE_CMD|brlapi.KEY_CMD_PASSDOTS|brlapi.KEY_CMD_ARG_MASK)])
else:
self.brlapi_connection.ignoreAllKeys()
def __process_dot_key(self, dot, is_press):
......