class ScanMode(Timer): """ Abstract base class for all scanning modes. Specifies how the scanner moves between chunks of keys and when to activate them. Scan mode subclasses define a set of actions they support and the base class translates input device events into scan actions. Hierarchy: ScanMode --> AutoScan --> UserScan --> OverScan --> StepScan --> DirectScan """ """ Scan actions """ ACTION_STEP = 0 ACTION_LEFT = 1 ACTION_RIGHT = 2 ACTION_UP = 3 ACTION_DOWN = 4 ACTION_ACTIVATE = 5 ACTION_STEP_START = 6 ACTION_STEP_STOP = 7 ACTION_UNHANDLED = 8 """ Time between key activation flashes (in sec) """ ACTIVATION_FLASH_INTERVAL = 0.1 """ Number of key activation flashes """ ACTIVATION_FLASH_COUNT = 4 def __init__(self, redraw_callback, activate_callback): super(ScanMode, self).__init__() logger.debug("ScanMode.__init__()") """ Activation timer instance """ self._activation_timer = Timer() """ Counter for key flash animation """ self._flash = 0 """ Callback for key redraws """ self._redraw_callback = redraw_callback """ Callback for key activation """ self._activate_callback = activate_callback """ A Chunker instance """ self.chunker = None def __del__(self): logger.debug("ScanMode.__del__()") def map_actions(self, detail, pressed): """ Abstract: Convert input events into scan actions. """ raise NotImplementedError() def do_action(self, action): """ Abstract: Handle scan actions. """ raise NotImplementedError() def scan(self): """ Abstract: Move between chunks. """ raise NotImplementedError() def create_chunker(self): """ Abstract: Create a chunker instance. """ raise NotImplementedError() def init_position(self): """ Virtual: Called if a new layer was set or a key activated. """ pass def handle_event(self, event): """ Translate device events into scan actions. """ # Ignore events during key activation if self._activation_timer.is_running(): return event_type = event.xi_type if event_type == XIEventType.ButtonPress: button_map = config.scanner.device_button_map action = self.map_actions(button_map, event.button, True) elif event_type == XIEventType.ButtonRelease: button_map = config.scanner.device_button_map action = self.map_actions(button_map, event.button, False) elif event_type == XIEventType.KeyPress: key_map = config.scanner.device_key_map action = self.map_actions(key_map, event.keyval, True) elif event_type == XIEventType.KeyRelease: key_map = config.scanner.device_key_map action = self.map_actions(key_map, event.keyval, False) else: action = self.ACTION_UNHANDLED if action != self.ACTION_UNHANDLED: self.do_action(action) def on_timer(self): """ Override: Timer() callback. """ return self.scan() def max_cycles_reached(self): """ Check if the maximum number of scan cycles is reached. """ return self.chunker.cycles >= config.scanner.cycles def set_layer(self, layout, layer): """ Set the layer that should be scanned. """ self.reset() self.chunker = self.create_chunker() self.chunker.chunk(layout, layer) self.init_position() def _on_activation_timer(self, key): """ Timer callback: Flashes the key and finally activates it. """ if self._flash > 0: key.scanned = not key.scanned self._flash -= 1 self.redraw([key]) return True else: self._activate_callback(key) self.init_position() return False def activate(self): """ Activates a key and triggers feedback. """ key = self.chunker.get_key() if not key: return if config.scanner.feedback_flash: self._flash = self.ACTIVATION_FLASH_COUNT self._activation_timer.start(self.ACTIVATION_FLASH_INTERVAL, self._on_activation_timer, key) else: self._activate_callback(key) self.init_position() def reset(self): """ Stop scanning and clear all highlights. """ if self.is_running(): self.stop() if self.chunker: self.redraw(self.chunker.highlight_all(False)) def redraw(self, keys=None): """ Update individual keys or the entire keyboard. """ self._redraw_callback(keys) def finalize(self): """ Clean up the ScanMode instance. """ self.reset() self._activation_timer = None
class AtspiTextContext(TextContext): """ Keep track of the current text context with AT-SPI """ _state_tracker = AtspiStateTracker() def __init__(self, wp): self._wp = wp self._accessible = None self._can_insert_text = False self._text_domains = TextDomains() self._text_domain = self._text_domains.get_nop_domain() self._changes = TextChanges() self._entering_text = False self._text_changed = False self._context = "" self._line = "" self._line_cursor = 0 self._span_at_cursor = TextSpan() self._begin_of_text = False # context starts at begin of text? self._begin_of_text_offset = None # offset of text begin self._last_context = None self._last_line = None self._update_context_timer = Timer() def cleanup(self): self._register_atspi_listeners(False) def enable(self, enable): self._register_atspi_listeners(enable) def get_text_domain(self): return self._text_domain def get_context(self): """ Returns the predictions context, i.e. some range of text before the cursor position. """ if self._accessible is None: return "" # Don't update suggestions in scrolling terminals if self._entering_text or \ not self._text_changed or \ self.can_suggest_before_typing(): return self._context return "" def get_bot_context(self): """ Returns the predictions context with begin of text marker (at text begin). """ context = "" if self._accessible: context = self.get_context() # prepend domain specific begin-of-text marker if self._begin_of_text: marker = self.get_text_begin_marker() if marker: context = marker + " " + context return context def get_line(self): return self._line \ if self._accessible else "" def get_line_cursor_pos(self): return self._line_cursor \ if self._accessible else 0 def get_line_past_cursor(self): return self._line[self._line_cursor:] \ if self._accessible else "" def get_span_at_cursor(self): return self._span_at_cursor \ if self._accessible else None def get_cursor(self): return self._span_at_cursor.begin() \ if self._accessible else 0 def get_text_begin_marker(self): domain = self.get_text_domain() if domain: return domain.get_text_begin_marker() return "" def can_record_insertion(self, accessible, pos, length): domain = self.get_text_domain() if domain: return domain.can_record_insertion(accessible, pos, length) return True def can_suggest_before_typing(self): domain = self.get_text_domain() if domain: return domain.can_suggest_before_typing() return True def get_begin_of_text_offset(self): return self._begin_of_text_offset \ if self._accessible else None def get_changes(self): return self._changes def has_changes(self): """ Are there any changes to learn? """ return not self._changes.is_empty() def clear_changes(self): self._changes.clear() def can_insert_text(self): """ Can delete or insert text into the accessible? """ #return False # support for inserting is spotty: not in firefox, terminal return bool(self._accessible) and self._can_insert_text def delete_text(self, offset, length=1): """ Delete directly, without going through faking key presses. """ self._accessible.delete_text(offset, offset + length) def delete_text_before_cursor(self, length=1): """ Delete directly, without going through faking key presses. """ offset = self._accessible.get_caret_offset() self.delete_text(offset - length, length) def insert_text(self, offset, text): """ Insert directly, without going through faking key presses. """ self._accessible.insert_text(offset, text, -1) def insert_text_at_cursor(self, text): """ Insert directly, without going through faking key presses. Fails for terminal and firefox, unfortunately. """ offset = self._accessible.get_caret_offset() self.insert_text(offset, text) def _register_atspi_listeners(self, register=True): st = self._state_tracker if register: st.connect("text-entry-activated", self._on_text_entry_activated) st.connect("text-changed", self._on_text_changed) st.connect("text-caret-moved", self._on_text_caret_moved) #st.connect("key-pressed", self._on_atspi_key_pressed) else: st.disconnect("text-entry-activated", self._on_text_entry_activated) st.disconnect("text-changed", self._on_text_changed) st.disconnect("text-caret-moved", self._on_text_caret_moved) #st.disconnect("key-pressed", self._on_atspi_key_pressed) def get_accessible_capabilities(accessible, **kwargs): can_insert_text = False attributes = kwargs.get("attributes", {}) interfaces = kwargs.get("interfaces", []) if accessible: # Can insert text via Atspi? # Advantages: - faster, no individual key presses # - full trouble-free insertion of all unicode characters if "EditableText" in interfaces: # Support for atspi text insertion is spotty. # Firefox, LibreOffice Writer, gnome-terminal don't support it, # even if they claim to implement the EditableText interface. # Allow direct text insertion by gtk widgets if "toolkit" in attributes and attributes["toolkit"] == "gtk": can_insert_text = True return can_insert_text def _on_text_entry_activated(self, accessible): # old text_domain still valid here self._wp.on_text_entry_deactivated() #print("_on_text_entry_activated", accessible) # keep track of the active accessible asynchronously self._accessible = accessible self._entering_text = False self._text_changed = False # select text domain matching this accessible state = self._state_tracker.get_state() \ if self._accessible else {} self._text_domain = self._text_domains.find_match(**state) self._text_domain.init_domain() # determine capabilities of this accessible self._can_insert_text = self.get_accessible_capabilities(**state) # log accessible info if _logger.isEnabledFor(logging.DEBUG): log = _logger.debug log("-" * 70) log("Accessible focused: ") if self._accessible: state = self._state_tracker.get_state() for key, value in sorted(state.items()): msg = str(key) + "=" if key == "state-set": msg += repr(AtspiStateType.to_strings(value)) else: msg += str(value) log(msg) log("text_domain: {}".format(self._text_domain)) log("can_insert_text: {}".format(self._can_insert_text)) log("") else: log("None") log("") self._update_context() self._wp.on_text_entry_activated() def _on_text_changed(self, event): insertion_span = self._record_text_change(event.pos, event.length, event.insert) # synchrounously notify of text insertion if insertion_span: try: cursor_offset = self._accessible.get_caret_offset() except: # gi._glib.GError pass else: self._wp.on_text_inserted(insertion_span, cursor_offset) self._update_context() def _on_text_caret_moved(self, event): self._update_context() def _on_atspi_key_pressed(self, event): """ disabled, Francesco didn't receive any AT-SPI key-strokes. """ keycode = event.hw_code # uh oh, only keycodes... # hopefully "c" doesn't move around a lot. modifiers = event.modifiers #self._handle_key_press(keycode, modifiers) def on_onboard_typing(self, key, mod_mask): if key.is_text_changing(): keycode = 0 if key.is_return(): keycode = KeyCode.KP_Enter else: label = key.get_label() if label == "C" or label == "c": keycode = KeyCode.C self._handle_key_press(keycode, mod_mask) def _handle_key_press(self, keycode, modifiers): if self._accessible: domain = self.get_text_domain() if domain: self._entering_text, end_of_editing = \ domain.handle_key_press(keycode, modifiers) if end_of_editing == True: self._wp.commit_changes() elif end_of_editing == False: self._wp.discard_changes() def _record_text_change(self, pos, length, insert): accessible = self._accessible insertion_span = None char_count = None if accessible: try: char_count = accessible.get_character_count() except: # gi._glib.GError: The application no longer exists # when closing a tab in gnome-terminal. char_count = None if not char_count is None: # record the change spans_to_update = [] if insert: #print("insert", pos, length) if self._entering_text and \ self.can_record_insertion(accessible, pos, length): if self._wp.is_typing() or length < 30: # Remember all of the insertion, might have been # a pressed snippet or wordlist button. include_length = -1 else: # Remember only the first few characters. # Large inserts can be paste, reload or scroll # operations. Only learn the first word of these. include_length = 2 # simple span for current insertion begin = max(pos - 100, 0) end = min(pos + length + 100, char_count) text = Atspi.Text.get_text(accessible, begin, end) insertion_span = TextSpan(pos, length, text, begin) else: # Remember nothing, just update existing spans. include_length = None spans_to_update = self._changes.insert(pos, length, include_length) else: #print("delete", pos, length) spans_to_update = self._changes.delete(pos, length, self._entering_text) # update text of the modified spans for span in spans_to_update: # Get some more text around the span to hopefully # include whole words at beginning and end. begin = max(span.begin() - 100, 0) end = min(span.end() + 100, char_count) span.text = Atspi.Text.get_text(accessible, begin, end) span.text_pos = begin #print(self._changes) self._text_changed = True return insertion_span def _update_context(self): self._update_context_timer.start(0.01, self.on_text_context_changed) def on_text_context_changed(self): result = self._text_domain.read_context(self._accessible) if not result is None: (self._context, self._line, self._line_cursor, self._span_at_cursor, self._begin_of_text, self._begin_of_text_offset) = result context = self.get_bot_context( ) # make sure to include bot-markers if self._last_context != context or \ self._last_line != self._line: self._last_context = context self._last_line = self._line self._wp.on_text_context_changed() return False
class LayoutPopup(KeyboardPopup, LayoutView, TouchInput): """ Popup showing a (sub-)layout tree. """ IDLE_CLOSE_DELAY = 0 # seconds of inactivity until window closes def __init__(self, keyboard, notify_done_callback): self._layout = None self._notify_done_callback = notify_done_callback self._drag_selected = False # grazed by the pointer? KeyboardPopup.__init__(self) LayoutView.__init__(self, keyboard) TouchInput.__init__(self) self.connect("draw", self._on_draw) self.connect("destroy", self._on_destroy_event) self._close_timer = Timer() self.start_close_timer() def cleanup(self): self.stop_close_timer() # fix label popup staying visible on double click self.keyboard.hide_touch_feedback() LayoutView.cleanup(self) # deregister from keyboard def get_toplevel(self): return self def set_layout(self, layout, frame_width): self._layout = layout self._frame_width = frame_width self.update_labels() # set window size layout_canvas_rect = layout.get_canvas_border_rect() canvas_rect = layout_canvas_rect.inflate(frame_width) w, h = canvas_rect.get_size() self.set_default_size(w + 1, h + 1) def get_layout(self): return self._layout def get_frame_width(self): return self._frame_width def got_motion(self): """ Has the pointer ever entered the popup? """ return self._drag_selected def handle_realize_event(self): self.set_override_redirect(True) super(LayoutPopup, self).handle_realize_event() def _on_destroy_event(self, user_data): self.cleanup() def on_enter_notify(self, widget, event): self.stop_close_timer() def on_leave_notify(self, widget, event): self.start_close_timer() def on_input_sequence_begin(self, sequence): self.stop_close_timer() key = self.get_key_at_location(sequence.point) if key: sequence.active_key = key self.keyboard.key_down(key, self, sequence) def on_input_sequence_update(self, sequence): if sequence.state & BUTTON123_MASK: key = self.get_key_at_location(sequence.point) # drag-select new active key active_key = sequence.active_key if not active_key is key and \ (active_key is None or not active_key.activated): sequence.active_key = key self.keyboard.key_up(active_key, self, sequence, False) self.keyboard.key_down(key, self, sequence, False) self._drag_selected = True def on_input_sequence_end(self, sequence): key = sequence.active_key if key: keyboard = self.keyboard keyboard.key_up(key, self, sequence) if key and \ not self._drag_selected: Timer(config.UNPRESS_DELAY, self.close_window) else: self.close_window() def _on_draw(self, widget, context): decorated = LayoutView.draw(self, widget, context) def draw_window_frame(self, context, lod): corner_radius = config.CORNER_RADIUS border_rgba = self.get_popup_window_rgba("border") alpha = border_rgba[3] colors = [ [[0.5, 0.5, 0.5, alpha], 0, 1], [border_rgba, 1.5, 2.0], ] rect = Rect(0, 0, self.get_allocated_width(), self.get_allocated_height()) for rgba, pos, width in colors: r = rect.deflate(width) roundrect_arc(context, r, corner_radius) context.set_line_width(width) context.set_source_rgba(*rgba) context.stroke() def close_window(self): self._notify_done_callback() def start_close_timer(self): if self.IDLE_CLOSE_DELAY: self._close_timer.start(self.IDLE_CLOSE_DELAY, self.close_window) def stop_close_timer(self): self._close_timer.stop()
class WindowRectPersist(WindowRectTracker): """ Save and restore window position and size. """ def __init__(self): WindowRectTracker.__init__(self) self._screen_orientation = None self._save_position_timer = Timer() # init detection of screen "rotation" screen = self.get_screen() screen.connect('size-changed', self.on_screen_size_changed) def cleanup(self): self._save_position_timer.finish() def is_visible(self): """ This is overloaded in KbdWindow """ return Gtk.Window.get_visible(self) def on_screen_size_changed(self, screen): """ detect screen rotation (tablets)""" # Give the screen time to settle, the window manager # may block the move to previously invalid positions and # when docked, the slide animation may be drowned out by all # the action in other processes. Timer(1.5, self.on_screen_size_changed_delayed, screen) def on_screen_size_changed_delayed(self, screen): self.restore_window_rect() def get_screen_orientation(self): """ Current orientation of the screen (tablet rotation). Only the aspect ratio is taken into account at this time. This appears to cover more cases than looking at monitor rotation, in particular with multi-monitor screens. """ screen = self.get_screen() if screen.get_width() >= screen.get_height(): return Orientation.LANDSCAPE else: return Orientation.PORTRAIT def restore_window_rect(self, startup = False): """ Restore window size and position. """ # Run pending save operations now, so they don't # interfere with the window rect after it was restored. self._save_position_timer.finish() orientation = self.get_screen_orientation() rect = self.read_window_rect(orientation) self._screen_orientation = orientation self._window_rect = rect _logger.debug("restore_window_rect {rect}, {orientation}" \ .format(rect = rect, orientation = orientation)) # Give the derived class a chance to modify the rect, # for example to correct the position for auto-show. rect = self.on_restore_window_rect(rect) self._window_rect = rect # move/resize the window if startup: # gnome-shell doesn't take kindly to an initial move_resize(). # The window ends up at (0, 0) on and goes back there # repeatedly when hiding and unhiding. self.set_default_size(rect.w, rect.h) self.move(rect.x, rect.y) else: self.move_resize(rect.x, rect.y, rect.w, rect.h) # Initialize shadow variables with valid values so they # don't get taken from the unreliable window. # Fixes bad positioning of the very first auto-show. if startup: self._window_rect = rect.copy() # Ignore frame dimensions; still better than asking the window. self._origin = rect.left_top() self._screen_orientation = self.get_screen_orientation() def on_restore_window_rect(self, rect): return rect def save_window_rect(self, orientation = None, rect = None): """ Save window size and position. """ if orientation is None: orientation = self._screen_orientation if rect is None: rect = self._window_rect # Give the derived class a chance to modify the rect, # for example to override it for auto-show. rect = self.on_save_window_rect(rect) self.write_window_rect(orientation, rect) _logger.debug("save_window_rect {rect}, {orientation}" \ .format(rect = rect, orientation = orientation)) def on_save_window_rect(self, rect): return rect def read_window_rect(self, orientation, rect): """ Read orientation dependent rect. Overload this in derived classes. """ raise NotImplementedError() def write_window_rect(self, orientation, rect): """ Write orientation dependent rect. Overload this in derived classes. """ raise NotImplementedError() def start_save_position_timer(self): """ Trigger saving position and size to gsettings Delay this a few seconds to avoid excessive disk writes. Remember the current rect and rotation as the screen may have been rotated when the saving happens. """ self._save_position_timer.start(5, self.save_window_rect, self.get_screen_orientation(), self.get_rect()) def stop_save_position_timer(self): self._save_position_timer.stop()
class KbdWindow(KbdWindowBase, WindowRectTracker, Gtk.Window): # Minimum window size (for resizing in system mode, see handle_motion()) MINIMUM_SIZE = 20 home_rect = None def __init__(self, keyboard_widget, icp): self._last_ignore_configure_time = None self._last_configures = [] self._was_visible = False Gtk.Window.__init__(self, urgency_hint=False, width_request=self.MINIMUM_SIZE, height_request=self.MINIMUM_SIZE) KbdWindowBase.__init__(self, keyboard_widget, icp) WindowRectTracker.__init__(self) GObject.signal_new("quit-onboard", KbdWindow, GObject.SIGNAL_RUN_LAST, GObject.TYPE_BOOLEAN, ()) self._auto_position_poll_timer = Timer() self.restore_window_rect(startup=True) self.connect("map", self._on_map_event) self.connect("unmap", self._on_unmap_event) self.connect("delete-event", self._on_delete_event) self.connect("configure-event", self._on_configure_event) # Connect_after seems broken in Quantal, the callback is never called. #self.connect_after("configure-event", self._on_configure_event_after) self._osk_util.connect_root_property_notify( ["_NET_WORKAREA", "_NET_CURRENT_DESKTOP"], self._on_root_property_notify) once = CallOnce(100).enqueue # call at most once per 100ms rect_changed = lambda x: once(self._on_config_rect_changed) config.window.position_notify_add(rect_changed) config.window.size_notify_add(rect_changed) dock_size_changed = lambda x: once(self._on_config_dock_size_changed) config.window.dock_size_notify_add(dock_size_changed) def cleanup(self): WindowRectTracker.cleanup(self) KbdWindowBase.cleanup(self) if self.icp: self.icp.cleanup() self.icp.destroy() self.icp = None def _on_root_property_notify(self, property): """ Fixme: Exceptions get lost in here.""" if property == "_NET_WORKAREA": if config.is_docking_enabled() and \ not config.xid_mode: mon = self.get_docking_monitor() new_area = self.get_monitor_workarea() area = self._monitor_workarea.get(0) if area: # Only check for x changes, y is too dangerous for now, # too easy to get the timing wrong and end up with double docks. if area.x != new_area.x or \ area.w != new_area.w: area.x = new_area.x area.w = new_area.w _logger.info("workarea changed to {}, " "using {} for docking." \ .format(str(new_area), str(area))) self.update_docking() elif property == "_NET_CURRENT_DESKTOP": # OpenBox: Make sure to move the keyboard to the new desktop # on the next occasion. # Unity: Never reached (Raring), _NET_CURRENT_DESKTOP isn't # set when switching desktops there. However we do get a # configure event, so the transitioning code moves the # window and it is brought to the current desktop anyway. self._desktop_switch_count += 1 def _on_map_event(self, user_data): pass def _on_unmap_event(self, user_data): # Turn off struts in case this unmap is in response to # changes in window options, force-to-top in particular. if config.is_docking_enabled(): self.clear_struts() # untity starts onboard before the desktops # workarea has settled, reset it here on hiding, # as we know our struts are gone from this point. self.reset_monitor_workarea() def on_visibility_changed(self, visible): if not self._visible and visible and \ not config.is_docking_enabled() and \ not config.xid_mode: rect = self.get_current_rect() if not rect is None: # shouldn't happen, fix this self.move_resize(*rect) # sync position KbdWindowBase.on_visibility_changed(self, visible) def _on_config_rect_changed(self): """ Gsettings position or size changed """ if not config.xid_mode and \ not config.is_docking_enabled(): orientation = self.get_screen_orientation() rect = self.read_window_rect(orientation) # Only apply the new rect if it isn't the one we just wrote to # gsettings. Someone has to have manually changed the values # in gsettings to allow moving the window. rects = list(self._written_window_rects.values()) if not any(rect == r for r in rects): self.restore_window_rect() def _on_config_dock_size_changed(self): """ Gsettings size changed """ if not config.xid_mode and \ config.is_docking_enabled(): size = self.get_dock_size() # Only apply the new rect if it isn't the one we just wrote to # gsettings. Someone has to have manually changed the values # in gsettings to allow moving the window. sizes = list(self._written_dock_sizes.values()) if not any(size == sz for sz in sizes): self.restore_window_rect() def on_user_positioning_begin(self): self.stop_save_position_timer() self.stop_auto_position() self.keyboard_widget.freeze_auto_show() def on_user_positioning_done(self): self.update_window_rect() #self.detect_docking() if config.is_docking_enabled(): self.write_docking_size(self.get_screen_orientation(), self.get_size()) self.update_docking() else: self.update_home_rect() # Thaw auto show after a short delay to stop the window # from hiding due to spurios focus events after a system resize. self.keyboard_widget.thaw_auto_show(1.0) def detect_docking(self): if self.keyboard_widget.was_moving(): config.window.docking_enabled = False def _on_configure_event(self, widget, event): self.update_window_rect() if not config.is_docking_enabled(): # Connect_after seems broken in Quantal, but we still need to # get in after the default configure handler is done. Try to run # _on_configure_event_after in an idle handler instead. GLib.idle_add(self._on_configure_event_after, widget, event.copy()) def _on_configure_event_after(self, widget, event): """ Run this after KeyboardWidget's configure handler. After resizing Keyboard.update_layout() has to be called before limit_position() or the window jumps when it was close to the opposite screen edge of the resize handle. """ # Configure event due to user positioning? result = self._filter_configure_event(self._window_rect) if result == 0: self.update_home_rect() def _filter_configure_event(self, rect): """ Returns 0 for detected user positioning/sizing. Multiple defenses against false positives, i.e. window movement by autoshow, screen rotation, whathaveyou. """ # There is no user positioning in xembed mode. if config.xid_mode: return -1 # There is no system provided way to move/resize in # force-to-top mode. Solely rely on on_user_positioning_done(). if config.is_force_to_top(): return -2 # There is no user positioning for invisible windows. if not self.is_visible(): return -3 # There is no user positioning for iconified windows. if self.is_iconified(): return -4 # There is no user positioning for maximized windows. if self.is_maximized(): return -5 # Remember past n configure events. now = time.time() max_events = 4 self._last_configures = self._last_configures[-(max_events - 1):] # Same rect as before? if len(self._last_configures) and \ self._last_configures[-1][0] == rect: return 1 self._last_configures.append([rect, now]) # Only just started? if len(self._last_configures) < max_events: return 2 # Did we just move the window by auto-show? if not self._last_ignore_configure_time is None and \ time.time() - self._last_ignore_configure_time < 0.5: return 3 # Is the new window rect one of our known ones? if self.is_known_rect(self._window_rect): return 4 # Dragging the decorated frame doesn't produce continous # configure-events anymore as in Oneriric (Precise). # Disable all affected checks based on this. # The home rect will probably get lost occasionally. if not config.has_window_decoration(): # Less than n configure events in the last x seconds? first = self._last_configures[0] intervall = now - first[1] if intervall > 1.0: return 5 # Is there a jump > threshold in past positions? r0 = self._last_configures[-1][0] r1 = self._last_configures[-2][0] dx = r1.x - r0.x dy = r1.y - r0.y d2 = dx * dx + dy * dy if d2 > 50**2: self._last_configures = [] # restart return 6 return 0 def ignore_configure_events(self): self._last_ignore_configure_time = time.time() def remember_rect(self, rect): """ Remember the last 3 rectangles of auto-show repositioning. Time and order of configure events is somewhat unpredictable, so don't rely only on a single remembered rect. """ self._known_window_rects = self._known_window_rects[-3:] self._known_window_rects.append(rect) # Remembering the rects doesn't help if respositioning outside # of the work area in compiz with force-to-top mode disabled. # WM corrects window positions to fit into the viewable area. # -> add timing based block self.ignore_configure_events() def get_known_rects(self): """ Return all rects that may have resulted from internal window moves, not from user controlled drag operations. """ rects = list(self._known_window_rects) co = config.window.landscape rects.append(Rect(co.x, co.y, co.width, co.height)) co = config.window.portrait rects.append(Rect(co.x, co.y, co.width, co.height)) rects.append(self.home_rect) return rects def is_known_rect(self, rect): """ The home rect should be updated in response to user positiong/resizing. However we are unable to detect the end of window movement/resizing when window decoration is enabled. Instead we check if the current window rect is different from the ones auto-show knows and assume the user has changed it in this case. """ return any(rect == r for r in self.get_known_rects()) def move_home_rect_into_view(self): """ Make sure the home rect is valid, move it if necessary. This function may be called even if the window is invisible. """ rect = self._window_rect.copy() x, y = rect.x, rect.y _x, _y = self.keyboard_widget.limit_position(x, y) if _x != x or _y != y: self.update_home_rect() def update_home_rect(self): if config.is_docking_enabled(): return # update home rect rect = self._window_rect.copy() # Make sure the move button stays visible if self.can_move_into_view(): rect.x, rect.y = self.keyboard_widget.limit_position( rect.x, rect.y) self.home_rect = rect.copy() self.start_save_position_timer() # Make transitions aware of the new position, # undoubtedly reached by user positioning. # Else, window snaps back to the last transition position. self.keyboard_widget.sync_transition_position(rect) def get_home_rect(self): """ Get the un-repositioned rect, the one auto-show falls back to when there is nowhere else to move. """ if config.is_docking_enabled(): rect = self.get_dock_rect() else: rect = self.home_rect return rect def get_visible_rect(self): """ Returns the rect of the visible window rect with auto-show repositioning taken into account. """ home_rect = self.get_home_rect() # aware of docking rect = home_rect if config.is_auto_show_enabled(): r = self.get_repositioned_window_rect(home_rect) if not r is None: rect = r return rect def auto_position(self): self.update_position() # With docking enabled, when focusing the search entry of a # maximized firefox window, it changes position when the work # area shrinks and ends up below Onboard. # -> periodically update the window position for a little while, # this way slow systems can catch up too eventually (Nexus 7). self._poll_auto_position_start_time = time.time() start_delay = 0.1 self._auto_position_poll_timer.start(start_delay, self._on_auto_position_poll, start_delay) def _on_auto_position_poll(self, delay): self.update_position() # start another timer for progressively longer intervals delay = min(delay * 2.0, 1.0) if time.time() + delay < self._poll_auto_position_start_time + 3.0: self._auto_position_poll_timer.start(delay, self._on_auto_position_poll, delay) return True else: return False def stop_auto_position(self): self._auto_position_poll_timer.stop() def update_position(self): home_rect = self.get_home_rect() rect = self.get_repositioned_window_rect(home_rect) if rect is None: # move back home rect = home_rect if self.get_position() != rect.get_position(): self.keyboard_widget.transition_position_to(rect.x, rect.y) self.keyboard_widget.commit_transition() def get_repositioned_window_rect(self, home_rect): clearance = config.auto_show.widget_clearance test_clearance = clearance move_clearance = clearance limit_rects = None # None: all monitors # No test clearance when docking. Make it harder to jump # out of the dock, for example for the bottom search box # in maximized firefox. if config.is_docking_enabled(): test_clearance = (clearance[0], 0, clearance[2], 0) # limit the horizontal freedom to the docking monitor area, geom = self.get_docking_monitor_rects() limit_rects = [area] horizontal, vertical = self.get_repositioning_constraints() return self.keyboard_widget.auto_show.get_repositioned_window_rect( \ home_rect, limit_rects, test_clearance, move_clearance, horizontal, vertical) def reposition(self, x, y): """ Move the window from a transition, not meant for user positioning. """ # remember rects to distimguish from user move/resize w, h = self.get_size() self.remember_rect(Rect(x, y, w, h)) self.move(x, y) def get_repositioning_constraints(self): """ Return allowed respositioning directions for auto-show. """ if config.is_docking_enabled() and \ self.get_dock_expand(): return False, True else: return True, True def get_hidden_rect(self): """ Returns the rect of the hidden window rect with auto-show repositioning taken into account. """ if config.is_docking_enabled(): return self.get_docking_hideout_rect() return self.get_visible_rect() def get_current_rect(self): """ Returns the window rect with auto-show repositioning taken into account. """ if self.is_visible(): rect = self.get_visible_rect() else: rect = self.get_hidden_rect() return rect def on_restore_window_rect(self, rect): """ Overload for WindowRectTracker. """ if not config.is_docking_enabled(): self.home_rect = rect.copy() # check for alternative auto-show position r = self.get_current_rect() if r != rect: # remember our rects to distinguish from user move/resize self.remember_rect(r) rect = r self.keyboard_widget.sync_transition_position(rect) return rect def on_save_window_rect(self, rect): """ Overload for WindowRectTracker. """ # Ignore <rect> (self._window_rect), it may just be a temporary one # set by auto-show. Save the user selected home_rect instead. return self.home_rect def read_window_rect(self, orientation): """ Read orientation dependent rect. Overload for WindowRectTracker. """ if orientation == Orientation.LANDSCAPE: co = config.window.landscape else: co = config.window.portrait rect = Rect(co.x, co.y, co.width, co.height) return rect def write_window_rect(self, orientation, rect): """ Write orientation dependent rect. Overload for WindowRectTracker. """ # There are separate rects for normal and rotated screen (tablets). if orientation == Orientation.LANDSCAPE: co = config.window.landscape else: co = config.window.portrait # remember that we wrote this rect to gsettings self._written_window_rects[orientation] = rect.copy() # write to gsettings and trigger notifications co.settings.delay() co.x, co.y, co.width, co.height = rect co.settings.apply() def write_docking_size(self, orientation, size): co = self.get_orientation_config_object() expand = self.get_dock_expand() # remember that we wrote this rect to gsettings self._written_dock_sizes[orientation] = tuple(size) # write to gsettings and trigger notifications co.settings.delay() if not expand: co.dock_width = size[0] co.dock_height = size[1] co.settings.apply() def get_orientation_config_object(self): orientation = self.get_screen_orientation() if orientation == Orientation.LANDSCAPE: co = config.window.landscape else: co = config.window.portrait return co def on_transition_done(self, visible_before, visible_now): if visible_now: self.assure_on_current_desktop() self.update_docking() def on_screen_size_changed(self, screen): """ Screen rotation, etc. """ if config.is_docking_enabled(): # Can't correctly position the window while struts are active # -> turn them off for a moment self.clear_struts() # Attempt to hide the keyboard now. This won't work that well # as the system doesn't refresh the screen anymore until # after the rotation. self._was_visible = self.is_visible() self._screen_resizing = True keyboard_widget = self.keyboard_widget if keyboard_widget: keyboard_widget.transition_visible_to(False, 0.0) keyboard_widget.commit_transition() WindowRectTracker.on_screen_size_changed(self, screen) def on_screen_size_changed_delayed(self, screen): if config.is_docking_enabled(): self._screen_resizing = False self.reset_monitor_workarea() # The keyboard size may have changed, draw with the new size now, # while it's still in the hideout, so we don't have to watch. self.restore_window_rect() self.keyboard_widget.process_updates() keyboard_widget = self.keyboard_widget if keyboard_widget and self._was_visible: keyboard_widget.transition_visible_to(True, 0.0, 0.4) keyboard_widget.commit_transition() else: self.restore_window_rect() def limit_size(self, rect): """ Limits the given window rect to fit on screen. """ if self.keyboard_widget: return self.keyboard_widget.limit_size(rect) return rect def _emit_quit_onboard(self, event, data=None): self.emit("quit-onboard") def _on_delete_event(self, event, data=None): if config.lockdown.disable_quit: if self.keyboard_widget: return True else: self._emit_quit_onboard(event) def on_docking_notify(self): self.update_docking() self.keyboard_widget.update_resize_handles() def update_docking(self, force_update=False): enable = config.is_docking_enabled() if enable: rect = self.get_dock_rect() else: rect = Rect() shrink = config.window.docking_shrink_workarea edge = config.window.docking_edge expand = self.get_dock_expand() if self._docking_enabled != enable or \ (self._docking_enabled and \ (self._docking_rect != rect or \ self._shrink_work_area != shrink or \ self._dock_expand != expand or \ bool(self._current_struts) != shrink) ): self.enable_docking(enable) self._shrink_work_area = shrink self._dock_expand = expand self._docking_edge = edge self._docking_enabled = enable self._docking_rect = rect def enable_docking(self, enable): if enable: self._set_docking_struts(config.window.docking_shrink_workarea, config.window.docking_edge, self.get_dock_expand()) self.restore_window_rect() # knows about docking else: self.restore_window_rect() self.clear_struts() def clear_struts(self): self._set_docking_struts(False) def _set_docking_struts(self, enable, edge=None, expand=True): if not self.get_realized(): # no window, no xid return win = self.get_window() xid = win.get_xid() # requires GdkX11 import if not enable: self._apply_struts(xid, None) return area, geom = self.get_docking_monitor_rects() root = self.get_rootwin_rect() rect = self.get_dock_rect() top_start_x = top_end_x = 0 bottom_start_x = bottom_end_x = 0 #print("geom", geom, "area", area, "rect", rect) if edge: # Bottom top = 0 bottom = geom.h - area.bottom() + rect.h #bottom = root.h - area.bottom() + rect.h bottom_start_x = rect.left() bottom_end_x = rect.right() else: # Top top = area.top() + rect.h bottom = 0 top_start_x = rect.left() top_end_x = rect.right() struts = [ 0, 0, top, bottom, 0, 0, 0, 0, top_start_x, top_end_x, bottom_start_x, bottom_end_x ] self._apply_struts(xid, struts) def _apply_struts(self, xid, struts=None): if self._current_struts != struts: if struts is None: self._osk_struts.clear(xid) else: self._osk_struts.set(xid, struts) self._current_struts = struts def get_dock_size(self): co = self.get_orientation_config_object() return co.dock_width, co.dock_height def get_dock_expand(self): co = self.get_orientation_config_object() return co.dock_expand def get_dock_rect(self): area, geom = self.get_docking_monitor_rects() edge = config.window.docking_edge width, height = self.get_dock_size() rect = Rect(area.x, 0, area.w, height) if edge: # Bottom rect.y = area.y + area.h - height else: # Top rect.y = area.y expand = self.get_dock_expand() if expand: rect.w = area.w rect.x = area.x else: rect.w = min(width, area.w) rect.x = rect.x + (area.w - rect.w) // 2 return rect def get_docking_hideout_rect(self, reference_rect=None): """ Where the keyboard goes to hide when it slides off-screen. """ area, geom = self.get_docking_monitor_rects() rect = self.get_dock_rect() hideout = rect mcx, mcy = geom.get_center() if reference_rect: cx, cy = reference_rect.get_center() else: cx, cy = rect.get_center() clearance = 10 if cy > mcy: hideout.y = geom.bottom() + clearance # below Bottom else: hideout.y = geom.top() - rect.h - clearance # above Top return hideout def get_docking_monitor_rects(self): screen = self.get_screen() mon = self.get_docking_monitor() area = self._monitor_workarea.get(mon) if area is None: area = self.update_monitor_workarea() geom = screen.get_monitor_geometry(mon) geom = Rect(geom.x, geom.y, geom.width, geom.height) return area, geom def get_docking_monitor(self): screen = self.get_screen() return screen.get_primary_monitor() def reset_monitor_workarea(self): self._monitor_workarea = {} def update_monitor_workarea(self): """ Save the workarea, so we don't have to check all the time if our strut is already installed. """ mon = self.get_docking_monitor() area = self.get_monitor_workarea() self._monitor_workarea[mon] = area return area def get_monitor_workarea(self): screen = self.get_screen() mon = self.get_docking_monitor() area = screen.get_monitor_workarea(mon) area = Rect(area.x, area.y, area.width, area.height) return area @staticmethod def get_rootwin_rect(): rootwin = Gdk.get_default_root_window() return Rect.from_position_size( rootwin.get_position(), (rootwin.get_width(), rootwin.get_height())) def is_override_redirect_mode(self): return config.is_force_to_top() and \ self._wm_quirks.can_set_override_redirect(self) def assure_on_current_desktop(self): """ Make sure the window is visible in the current desktop in OpenBox and perhaps other WMs, except Compiz/Unity. Dbus Show() then makes Onboard appear after switching desktop (LP: 1092166, Raring). """ if self._moved_desktop_switch_count != self._desktop_switch_count: win = self.get_window() if win: win.move_to_current_desktop() self._moved_desktop_switch_count = self._desktop_switch_count
class ScanMode(Timer): """ Abstract base class for all scanning modes. Specifies how the scanner moves between chunks of keys and when to activate them. Scan mode subclasses define a set of actions they support and the base class translates input device events into scan actions. Hierarchy: ScanMode --> AutoScan --> UserScan --> OverScan --> StepScan --> DirectScan """ """ Scan actions """ ACTION_STEP = 0 ACTION_LEFT = 1 ACTION_RIGHT = 2 ACTION_UP = 3 ACTION_DOWN = 4 ACTION_ACTIVATE = 5 ACTION_STEP_START = 6 ACTION_STEP_STOP = 7 ACTION_UNHANDLED = 8 """ Handles Key Events (Multiple Press at a time) """ #In MUL_KEY = 0 #In SCAN_PREV_ACTION = ACTION_UNHANDLED #In SCAN_ACTION_DO = True #In """ Time between key activation flashes (in sec) """ ACTIVATION_FLASH_INTERVAL = 0.1 """ Number of key activation flashes """ ACTIVATION_FLASH_COUNT = 2 def __init__(self, redraw_callback, activate_callback): super(ScanMode, self).__init__() logger.debug("ScanMode.__init__()") """ Activation timer instance """ self._activation_timer = Timer() """ Counter for key flash animation """ self._flash = 0 """ Counter for key popup animation """ self._popup_display = 0 #In """ Callback for key redraws """ self._redraw_callback = redraw_callback """ Callback for key activation """ self._activate_callback = activate_callback """ A Chunker instance """ self.chunker = None """ Time between key activation flashes (in sec) """ self.ACTIVATION_FLASH_INTERVAL = config.scanner.activation_flash_interval #0.1 #In """ Number of key activation flashes """ self.ACTIVATION_FLASH_COUNT = config.scanner.activation_flash_count #2 #In def __del__(self): logger.debug("ScanMode.__del__()") def map_actions(self, detail, pressed): """ Abstract: Convert input events into scan actions. """ raise NotImplementedError() def do_action(self, action): """ Abstract: Handle scan actions. """ raise NotImplementedError() def scan(self): """ Abstract: Move between chunks. """ raise NotImplementedError() def create_chunker(self): """ Abstract: Create a chunker instance. """ raise NotImplementedError() def init_position(self): """ Virtual: Called if a new layer was set or a key activated. """ pass def handle_event(self, event): """ Translate device events into scan actions. """ # Ignore events during key activation if self._activation_timer.is_running(): return event_type = event.xi_type if event_type == XIEventType.ButtonPress: button_map = config.scanner.device_button_map action = self.map_actions(button_map, event.button, True) elif event_type == XIEventType.ButtonRelease: button_map = config.scanner.device_button_map action = self.map_actions(button_map, event.button, False) elif event_type == XIEventType.KeyPress: if self.MUL_KEY >= 0: #In key_map = config.scanner.device_key_map #In self.SCAN_PREV_ACTION = self.map_actions( key_map, event.keyval, True) #In self.MUL_KEY = self.MUL_KEY + 1 #In return #In else: action = self.map_actions(key_map, event.keyval, False) elif event_type == XIEventType.KeyRelease: if self.MUL_KEY > 0: #In self.MUL_KEY = self.MUL_KEY - 1 #In key_map = config.scanner.device_key_map action = self.map_actions(key_map, event.keyval, True) #In if action != self.SCAN_PREV_ACTION: #In self.SCAN_ACTION_DO = False #In if self.MUL_KEY > 0 or (self.MUL_KEY == 0 and self.SCAN_ACTION_DO != True): #In action = self.map_actions(key_map, event.keyval, False) if self.MUL_KEY == 0 and self.SCAN_ACTION_DO != True: #In """ print("E R R O R : PLEASE DONT PRESS BOTH FUNCTIONALITIES TOGETHER!!!")#In TODO : Show Error Message """ self.SCAN_ACTION_DO = True #In else: action = self.map_actions(key_map, event.keyval, False) else: action = self.ACTION_UNHANDLED if action != self.ACTION_UNHANDLED: self.do_action(action) def on_timer(self): """ Override: Timer() callback. """ return self.scan() def max_cycles_reached(self): """ Check if the maximum number of scan cycles is reached. """ return self.chunker.cycles >= config.scanner.cycles def set_layer(self, layout, layer): """ Set the layer that should be scanned. """ self.reset() self.chunker = self.create_chunker() self.chunker.chunk(layout, layer) self.init_position() def _on_activation_timer(self, key): """ Timer callback: Flashes the key and finally activates it. """ if self._flash > 0: key.scanned = not key.scanned self._flash -= 1 self.redraw([key]) return True else: self._activate_callback(key) self.init_position() return False def _on_activation_timer_popup(self, key): #In """ Timer callback: Reset Scanner. """ self.init_position() return False def activate(self): """ Activates a key and triggers feedback. """ key = self.chunker.get_key() if not key: return if config.scanner.feedback_flash: """ Scanner Blinking """ self._flash = self.ACTIVATION_FLASH_COUNT * 2 #In self._activation_timer.start(self.ACTIVATION_FLASH_INTERVAL, self._on_activation_timer, key) else: #In """ Scanner Popup """ delay = config.UNPRESS_DELAY #In config.UNPRESS_DELAY = config.scanner.scanner_popup_unpress_delay #In self._activate_callback(key) self._activation_timer.start( config.scanner.scanner_popup_unpress_delay, self._on_activation_timer_popup, key) #In config.UNPRESS_DELAY = delay #In #self.init_position() def reset(self): """ Stop scanning and clear all highlights. """ if self.is_running(): self.stop() if self.chunker: self.redraw(self.chunker.highlight_all(False)) def redraw(self, keys=None): """ Update individual keys or the entire keyboard. """ self._redraw_callback(keys) def finalize(self): """ Clean up the ScanMode instance. """ self.reset() self._activation_timer = None
class TouchInput(InputEventSource): """ Unified handling of multi-touch sequences and conventional pointer input. """ GESTURE_DETECTION_SPAN = 100 # [ms] until two finger tap&drag is detected GESTURE_DELAY_PAUSE = 3000 # [ms] Suspend delayed sequence begin for this # amount of time after the last key press. delay_sequence_begin = True # No delivery, i.e. no key-presses after # gesture detection, but delays press-down. def __init__(self): InputEventSource.__init__(self) self._input_sequences = {} self._touch_events_enabled = self.is_touch_enabled() self._multi_touch_enabled = config.keyboard.touch_input == \ TouchInputEnum.MULTI self._gestures_enabled = self._touch_events_enabled self._last_event_was_touch = False self._last_sequence_time = 0 self._gesture = NO_GESTURE self._gesture_begin_point = (0, 0) self._gesture_begin_time = 0 self._gesture_detected = False self._gesture_cancelled = False self._num_tap_sequences = 0 self._gesture_timer = Timer() def is_touch_enabled(self): return config.keyboard.touch_input != TouchInputEnum.NONE def has_input_sequences(self): """ Are any clicks/touches still ongoing? """ return bool(self._input_sequences) def last_event_was_touch(self): """ Was there just a touch event? """ return self._last_event_was_touch def has_touch_source(self, event): """ Was source device of event a touch screen? """ source_device = event.get_source_device() source = source_device.get_source() return source == Gdk.InputSource.TOUCHSCREEN def _on_button_press_event(self, widget, event): if self._touch_events_enabled and \ self.has_touch_source(event): return # - Ignore double clicks (GDK_2BUTTON_PRESS), # we're handling them ourselves. # - no mouse wheel buttons if event.type == Gdk.EventType.BUTTON_PRESS and \ 1 <= event.button <= 3: sequence = InputSequence() sequence.init_from_button_event(event) sequence.primary = True self._last_event_was_touch = False self._input_sequence_begin(sequence) def _on_button_release_event(self, widget, event): sequence = self._input_sequences.get(POINTER_SEQUENCE) if not sequence is None: sequence.point = (event.x, event.y) sequence.root_point = (event.x_root, event.y_root) sequence.time = event.get_time() self._input_sequence_end(sequence) def _on_motion_event(self, widget, event): if self._touch_events_enabled and \ self.has_touch_source(event): return sequence = self._input_sequences.get(POINTER_SEQUENCE) if sequence is None and \ not event.state & BUTTON123_MASK: sequence = InputSequence() sequence.primary = True if sequence: sequence.init_from_motion_event(event) self._last_event_was_touch = False self._input_sequence_update(sequence) def _on_enter_notify(self, widget, event): self.on_enter_notify(widget, event) def _on_leave_notify(self, widget, event): self.on_leave_notify(widget, event) def _on_touch_event(self, widget, event): if not self.has_touch_source(event): return touch = event.touch id = str(touch.sequence) self._last_event_was_touch = True event_type = event.type if event_type == Gdk.EventType.TOUCH_BEGIN: sequence = InputSequence() sequence.init_from_touch_event(touch, id) if len(self._input_sequences) == 0: sequence.primary = True self._input_sequence_begin(sequence) elif event_type == Gdk.EventType.TOUCH_UPDATE: sequence = self._input_sequences.get(id) if not sequence is None: sequence.point = (touch.x, touch.y) sequence.root_point = (touch.x_root, touch.y_root) sequence.time = event.get_time() sequence.update_time = time.time() self._input_sequence_update(sequence) else: if event_type == Gdk.EventType.TOUCH_END: pass elif event_type == Gdk.EventType.TOUCH_CANCEL: pass sequence = self._input_sequences.get(id) if not sequence is None: sequence.time = event.get_time() self._input_sequence_end(sequence) def _input_sequence_begin(self, sequence): """ Button press/touch begin """ self._gesture_sequence_begin(sequence) first_sequence = len(self._input_sequences) == 0 if first_sequence or \ self._multi_touch_enabled: self._input_sequences[sequence.id] = sequence if not self._gesture_detected: if first_sequence and \ self._multi_touch_enabled and \ self.delay_sequence_begin and \ sequence.time - self._last_sequence_time > \ self.GESTURE_DELAY_PAUSE: # Delay the first tap; we may have to stop it # from reaching the keyboard. self._gesture_timer.start(self.GESTURE_DETECTION_SPAN / 1000.0, self.on_delayed_sequence_begin, sequence, sequence.point) else: # Tell the keyboard right away. self.deliver_input_sequence_begin(sequence) self._last_sequence_time = sequence.time def on_delayed_sequence_begin(self, sequence, point): if not self._gesture_detected: # work around race condition sequence.point = point # return to the original begin point self.deliver_input_sequence_begin(sequence) self._gesture_cancelled = True return False def deliver_input_sequence_begin(self, sequence): self.on_input_sequence_begin(sequence) sequence.delivered = True def _input_sequence_update(self, sequence): """ Pointer motion/touch update """ self._gesture_sequence_update(sequence) if not sequence.state & BUTTON123_MASK or \ not self.in_gesture_detection_delay(sequence): self._gesture_timer.finish() # run delayed begin before update self.on_input_sequence_update(sequence) def _input_sequence_end(self, sequence): """ Button release/touch end """ self._gesture_sequence_end(sequence) self._gesture_timer.finish() # run delayed begin before end if sequence.id in self._input_sequences: del self._input_sequences[sequence.id] if sequence.delivered: self.on_input_sequence_end(sequence) if self._input_sequences: self._discard_stuck_input_sequences() self._last_sequence_time = sequence.time def _discard_stuck_input_sequences(self): """ Input sequence handling requires guaranteed balancing of begin, update and end events. There is no indication yet this isn't always the case, but still, at this time it seems like a good idea to prepare for the worst. -> Clear out aged input sequences, so Onboard can start from a fresh slate and not become terminally unresponsive. """ expired_time = time.time() - 30 for id, sequence in list(self._input_sequences.items()): if sequence.update_time < expired_time: _logger.warning("discarding expired input sequence " + str(id)) del self._input_sequences[id] def in_gesture_detection_delay(self, sequence): """ Are we still in the time span where sequence begins aren't delayed and can't be undone after gesture detection? """ span = sequence.time - self._gesture_begin_time return span < self.GESTURE_DETECTION_SPAN def _gesture_sequence_begin(self, sequence): # first tap? if self._num_tap_sequences == 0: self._gesture = NO_GESTURE self._gesture_detected = False self._gesture_cancelled = False self._gesture_begin_point = sequence.point self._gesture_begin_time = sequence.time # event time else: # subsequent taps if self.in_gesture_detection_delay(sequence) and \ not self._gesture_cancelled: self._gesture_timer.stop() # cancel delayed sequence begin self._gesture_detected = True self._num_tap_sequences += 1 def _gesture_sequence_update(self, sequence): if self._gesture_detected and \ sequence.state & BUTTON123_MASK and \ self._gesture == NO_GESTURE: point = sequence.point dx = self._gesture_begin_point[0] - point[0] dy = self._gesture_begin_point[1] - point[1] d2 = dx * dx + dy * dy # drag gesture? if d2 >= DRAG_GESTURE_THRESHOLD2: num_touches = len(self._input_sequences) self._gesture = DRAG_GESTURE self.on_drag_gesture_begin(num_touches) return True def _gesture_sequence_end(self, sequence): if len(self._input_sequences) == 1: # last sequence of the gesture? if self._gesture_detected: gesture = self._gesture if gesture == NO_GESTURE: # tap gesture? elapsed = sequence.time - self._gesture_begin_time if elapsed <= 300: self.on_tap_gesture(self._num_tap_sequences) elif gesture == DRAG_GESTURE: self.on_drag_gesture_end(0) self._num_tap_sequences = 0 def on_tap_gesture(self, num_touches): return False def on_drag_gesture_begin(self, num_touches): return False def on_drag_gesture_end(self, num_touches): return False def redirect_sequence_update(self, sequence, func): """ redirect input sequence update to self. """ sequence = self._get_redir_sequence(sequence) func(sequence) def redirect_sequence_end(self, sequence, func): """ Redirect input sequence end to self. """ sequence = self._get_redir_sequence(sequence) # Make sure has_input_sequences() returns False inside of func(). # The keyboard needs this to detect the end of input. if sequence.id in self._input_sequences: del self._input_sequences[sequence.id] func(sequence) def _get_redir_sequence(self, sequence): """ Return a copy of <sequence>, managed in the target window. """ redir_sequence = self._input_sequences.get(sequence.id) if redir_sequence is None: redir_sequence = sequence.copy() redir_sequence.initial_active_key = None redir_sequence.active_key = None redir_sequence.cancel_key_action = False # was canceled by long press self._input_sequences[redir_sequence.id] = redir_sequence # convert to the new window client coordinates pos = self.get_position() rp = sequence.root_point redir_sequence.point = (rp[0] - pos[0], rp[1] - pos[1]) return redir_sequence
class AutoShow(object): """ Auto-show and hide Onboard. """ # Delay from the last focus event until the keyboard is shown/hidden. # Raise it to reduce unnecessary transitions (flickering). # Lower it for more immediate reactions. SHOW_REACTION_TIME = 0.0 HIDE_REACTION_TIME = 0.3 _lock_visible = False _frozen = False _keyboard_widget = None _state_tracker = AtspiStateTracker() def __init__(self, keyboard_widget): self._keyboard_widget = keyboard_widget self._auto_show_timer = Timer() self._thaw_timer = Timer() self._active_accessible = None def cleanup(self): self._auto_show_timer.stop() self._thaw_timer.stop() def enable(self, enable): if enable: self._state_tracker.connect("text-entry-activated", self._on_text_entry_activated) self._state_tracker.connect("text-caret-moved", self._on_text_caret_moved) else: self._state_tracker.disconnect("text-entry-activated", self._on_text_entry_activated) self._state_tracker.disconnect("text-caret-moved", self._on_text_caret_moved) if enable: self._lock_visible = False self._frozen = False def is_frozen(self): return self._frozen def freeze(self, thaw_time=None): """ Stop showing and hiding the keyboard window. thaw_time in seconds, None to freeze forever. """ self._frozen = True self._thaw_timer.stop() if not thaw_time is None: self._thaw_timer.start(thaw_time, self._on_thaw) # Discard pending hide/show actions. self._auto_show_timer.stop() def thaw(self, thaw_time=None): """ Allow hiding and showing the keyboard window again. thaw_time in seconds, None to thaw immediately. """ self._thaw_timer.stop() if thaw_time is None: self._thaw() else: self._thaw_timer.start(thaw_time, self._on_thaw) def _on_thaw(self): self._thaw_timer.stop() self._frozen = False return False def lock_visible(self, lock, thaw_time=1.0): """ Lock window permanetly visible in response to the user showing it. Optionally freeze hiding/showing for a limited time. """ # Permanently lock visible. self._lock_visible = lock # Temporarily stop showing/hiding. if thaw_time: self.freeze(thaw_time) # Leave the window in its current state, # discard pending hide/show actions. self._auto_show_timer.stop() # Stop pending auto-repositioning if lock: window = self._keyboard_widget.get_kbd_window() if window: window.stop_auto_position() def _on_text_caret_moved(self, event): """ Show the keyboard on click of an already focused text entry (LP: 1078602). Do this only for single line text entries to still allow clicking longer documents without having onboard show up. """ if config.auto_show.enabled and \ not self._keyboard_widget.is_visible(): accessible = self._active_accessible if accessible: if self._state_tracker.is_single_line(): self._on_text_entry_activated(accessible) def _on_text_entry_activated(self, accessible): window = self._keyboard_widget.get_kbd_window() self._active_accessible = accessible active = bool(accessible) # show/hide the keyboard window if not active is None: # Always allow to show the window even when locked. # Mitigates right click on unity-2d launcher hiding # onboard before _lock_visible is set (Precise). if self._lock_visible: active = True if not self.is_frozen(): self.show_keyboard(active) # The active accessible changed, stop trying to # track the position of the previous one. # -> less erratic movement during quick focus changes if window: window.stop_auto_position() # reposition the keyboard window if active and \ not accessible is None and \ not self._lock_visible and \ not self.is_frozen(): if window: window.auto_position() def show_keyboard(self, show): """ Begin AUTO_SHOW or AUTO_HIDE transition """ # Don't act on each and every focus message. Delay the start # of the transition slightly so that only the last of a bunch of # focus messages is acted on. delay = self.SHOW_REACTION_TIME if show else \ self.HIDE_REACTION_TIME self._auto_show_timer.start(delay, self._begin_transition, show) def _begin_transition(self, show): self._keyboard_widget.transition_visible_to(show) self._keyboard_widget.commit_transition() return False def get_repositioned_window_rect(self, home, limit_rects, test_clearance, move_clearance, horizontal=True, vertical=True): """ Get the alternative window rect suggested by auto-show or None if no repositioning is required. """ accessible = self._active_accessible if accessible: rect = self._state_tracker.get_accessible_extents(accessible) if not rect.is_empty() and \ not self._lock_visible: return self._get_window_rect_for_accessible_rect( \ home, rect, limit_rects, test_clearance, move_clearance, horizontal, vertical) return None def _get_window_rect_for_accessible_rect(self, home, rect, limit_rects, test_clearance, move_clearance, horizontal=True, vertical=True): """ Find new window position based on the screen rect of the accessible. """ mode = "nooverlap" x = y = None if mode == "closest": x, y = rect.left(), rect.bottom() if mode == "nooverlap": x, y = self._find_non_occluding_position(home, rect, limit_rects, test_clearance, move_clearance, horizontal, vertical) if not x is None: return Rect(x, y, home.w, home.h) else: return None def _find_non_occluding_position(self, home, acc_rect, limit_rects, test_clearance, move_clearance, horizontal=True, vertical=True): # The home_rect doesn't include window decoration, # make sure to add decoration for correct clearance. rh = home.copy() window = self._keyboard_widget.get_kbd_window() if window: offset = window.get_client_offset() rh.w += offset[0] rh.h += offset[1] # Leave some clearance around the accessible to account for # window frames and position errors of firefox entries. ra = acc_rect.apply_border(*test_clearance) if rh.intersects(ra): # Leave a different clearance for the new to be found positions. ra = acc_rect.apply_border(*move_clearance) x, y = rh.get_position() # candidate positions vp = [] if horizontal: vp.append([ra.left() - rh.w, y]) vp.append([ra.right(), y]) if vertical: vp.append([x, ra.top() - rh.h]) vp.append([x, ra.bottom()]) # limited, non-intersecting candidate rectangles vr = [] for p in vp: pl = self._keyboard_widget.limit_position( p[0], p[1], self._keyboard_widget.canvas_rect, limit_rects) r = Rect(pl[0], pl[1], rh.w, rh.h) if not r.intersects(ra): vr.append(r) # candidate with smallest center-to-center distance wins chx, chy = rh.get_center() dmin = None rmin = None for r in vr: cx, cy = r.get_center() dx, dy = cx - chx, cy - chy d2 = dx * dx + dy * dy if dmin is None or dmin > d2: dmin = d2 rmin = r if not rmin is None: return rmin.get_position() return None, None