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 WindowRectTracker: """ Keeps track of the window rectangle when moving/resizing. Gtk only updates the position and size asynchrounously on configure events and hidden windows return invalid values. Auto-show et al need valid values from get_position and get_size at all times. """ def __init__(self): self._window_rect = None self._origin = None 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 move(self, x, y): """ Overload Gtk.Window.move to reliably keep track of the window position. """ Gtk.Window.move(self, x, y) def resize(self, w, h): """ Overload Gtk.Window.size to reliably keep track of the window size. """ Gtk.Window.resize(self, w, h) def move_resize(self, x, y, w, h): win = self.get_window() if win: win.move_resize(x, y, w, h) def get_position(self): if self._window_rect is None: return Gtk.Window.get_position(self) else: return self._window_rect.get_position() def get_size(self): if self._window_rect is None: return Gtk.Window.get_size(self) else: return self._window_rect.get_size() def get_origin(self): if self._origin is None: return self.get_window().get_origin() else: return self._origin def get_rect(self): return self._window_rect 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. Timer(0.3, 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 loocking 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 update_window_rect(self): """ Call this on configure event, the only time when get_position, get_size, etc. can be trusted. """ visible = self.is_visible() if visible: self._window_rect = Rect.from_position_size(Gtk.Window.get_position(self), Gtk.Window.get_size(self)) self._origin = self.get_window().get_origin() self._screen_orientation = self.get_screen_orientation() 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) # 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 startup 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) 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 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