(self, report)
Calculates relative mouse values from a report and writes them.
Calculates relative mouse values from a report and writes them.
Calculates relative mouse values from a report and writes them .
def emit_mouse(self, report):
        """Calculates relative mouse values from a report and writes them."""
        for name, attr in self.layout.mouse.items():
            # If the attr is a tuple like (left_analog_y, "-")
            # then set the attr to just be the first item
            attr, modifier = attr

            if attr.startswith("trackpad_touch"):
                active_attr = attr[:16] + "active"
                if not getattr(report, active_attr):
                    self.mouse_pos.pop(name, None)
                    continue

                pos = getattr(report, attr)
                if name not in self.mouse_pos:
                    self.mouse_pos[name] = pos

                sensitivity = 0.5
                self.mouse_rel[name] += (pos - self.mouse_pos[name]) * sensitivity
                self.mouse_pos[name] = pos

            elif "analog" in attr:
                pos = getattr(report, attr)
                if (pos > (128 + self.mouse_analog_deadzone)
                    or pos < (128 - self.mouse_analog_deadzone)):
                    accel = (pos - 128) / 10
                else:
                    continue

                # If a minus modifier has been given then minus the acceleration
                # to invert the direction.
                if (modifier and modifier == "-"):
                    accel = -accel

                sensitivity = self.mouse_analog_sensitivity
                self.mouse_rel[name] += accel * sensitivity

            # Emulate mouse wheel (needs special handling)
            if name in (ecodes.REL_WHEELUP, ecodes.REL_WHEELDOWN):
                ecode = ecodes.REL_WHEEL # The real event we need to emit
                write = False
                if getattr(report, attr):
                    self._scroll_details['direction'] = name
                    now = time.time()
                    last_write = self._scroll_details.get('last_write')
                    if not last_write:
                        # No delay for the first button press for fast feedback
                        write = True
                        self._scroll_details['count'] = 0
                    if name == ecodes.REL_WHEELUP:
                        value = 1
                    elif name == ecodes.REL_WHEELDOWN:
                        value = -1
                    if last_write:
                        # Delay at least one cycle before continual scrolling
                        if self._scroll_details['count'] > 1:
                            if now - last_write > self.scroll_delay:
                                write = True
                        elif now - last_write > self.scroll_repeat_delay:
                            write = True
                    if write:
                        self.device.write(ecodes.EV_REL, ecode, value)
                        self._scroll_details['last_write'] = now
                        self._scroll_details['count'] += 1
                        continue # No need to proceed further
                else:
                    # Reset so you can quickly tap the button to scroll
                    if self._scroll_details.get('direction') == name:
                        self._scroll_details['last_write'] = 0
                        self._scroll_details['count'] = 0

            rel = int(self.mouse_rel[name])
            self.mouse_rel[name] = self.mouse_rel[name] - rel
            self.device.write(ecodes.EV_REL, name, rel)

        self.device.syn()
def emit_mouse ( self , report ) : for name , attr in self . layout . mouse . items ( ) : # If the attr is a tuple like (left_analog_y, "-") # then set the attr to just be the first item attr , modifier = attr if attr . startswith ( "trackpad_touch" ) : active_attr = attr [ : 16 ] + "active" if not getattr ( report , active_attr ) : self . mouse_pos . pop ( name , None ) continue pos = getattr ( report , attr ) if name not in self . mouse_pos : self . mouse_pos [ name ] = pos sensitivity = 0.5 self . mouse_rel [ name ] += ( pos - self . mouse_pos [ name ] ) * sensitivity self . mouse_pos [ name ] = pos elif "analog" in attr : pos = getattr ( report , attr ) if ( pos > ( 128 + self . mouse_analog_deadzone ) or pos < ( 128 - self . mouse_analog_deadzone ) ) : accel = ( pos - 128 ) / 10 else : continue # If a minus modifier has been given then minus the acceleration # to invert the direction. if ( modifier and modifier == "-" ) : accel = - accel sensitivity = self . mouse_analog_sensitivity self . mouse_rel [ name ] += accel * sensitivity # Emulate mouse wheel (needs special handling) if name in ( ecodes . REL_WHEELUP , ecodes . REL_WHEELDOWN ) : ecode = ecodes . REL_WHEEL # The real event we need to emit write = False if getattr ( report , attr ) : self . _scroll_details [ 'direction' ] = name now = time . time ( ) last_write = self . _scroll_details . get ( 'last_write' ) if not last_write : # No delay for the first button press for fast feedback write = True self . _scroll_details [ 'count' ] = 0 if name == ecodes . REL_WHEELUP : value = 1 elif name == ecodes . REL_WHEELDOWN : value = - 1 if last_write : # Delay at least one cycle before continual scrolling if self . _scroll_details [ 'count' ] > 1 : if now - last_write > self . scroll_delay : write = True elif now - last_write > self . scroll_repeat_delay : write = True if write : self . device . write ( ecodes . EV_REL , ecode , value ) self . _scroll_details [ 'last_write' ] = now self . _scroll_details [ 'count' ] += 1 continue # No need to proceed further else : # Reset so you can quickly tap the button to scroll if self . _scroll_details . get ( 'direction' ) == name : self . _scroll_details [ 'last_write' ] = 0 self . _scroll_details [ 'count' ] = 0 rel = int ( self . mouse_rel [ name ] ) self . mouse_rel [ name ] = self . mouse_rel [ name ] - rel self . device . write ( ecodes . EV_REL , name , rel ) self . device . syn ( )