def openDevice(self): try: # open in non-blocking mode self.devfp = open(self.rawdevice, 'r+b') fd = self.devfp.fileno() flag = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NONBLOCK) self.device = hidraw.HIDRaw(self.devfp) self.log.debug("Connected to %s", self.rawdevice) return True except Exception as e: self.log.error("Unable to open %s: %s", self.rawdevice, e) return False
def open_hiddevice(hid_id, dev_path=None): """ Open the device representing the mouse. If dev_path is None, it tries to find it. """ if dev_path is None: dev_path = find_device_path(hid_id) try: device = hidraw.HIDRaw(open(dev_path, 'w+')) except PermissionError: print("You don't have write access to %s." % (dev_path)) print(""" Run this script with sudo or ensure that your user belongs to the same group as the device and you have write access. For instance: sudo groupadd rival sudo chown $(ls -l %s | cut -d ' ' -f 4):rival %s sudo adduser $(whoami) rival sudo chmod g+w %s And maybe create an udev rule like (as root): echo 'KERNEL=="hidraw*", GROUP="rival"' > /etc/10-local-rival.rules udevadm trigger """ % (dev_path, dev_path, dev_path)) sys.exit(1) return device
def open_device(dev_path=None): if dev_path is None: dev_path = find_device_path() return hidraw.HIDRaw(open(dev_path, 'w+'))