|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Haptic feedback daemon for Synaptics touchpads with Manual Trigger. |
| 4 | +
|
| 5 | +Monitors touchpad button press events and sends haptic pulses via HID |
| 6 | +feature reports. Required because the kernel's HID haptic subsystem only |
| 7 | +supports Auto Trigger with waveform enumeration, not the simpler Manual |
| 8 | +Trigger protocol used by these Synaptics touchpads. |
| 9 | +""" |
| 10 | + |
| 11 | +import fcntl, glob, os, struct, sys |
| 12 | + |
| 13 | +VENDOR = "06CB" |
| 14 | +PRODUCT = "D01A" |
| 15 | +REPORT_ID = 0x37 |
| 16 | +INTENSITY = 40 # 0-100 |
| 17 | + |
| 18 | +# input_event: struct timeval (16 bytes on 64-bit) + type(H) + code(H) + value(i) |
| 19 | +EVENT_FORMAT = "llHHi" |
| 20 | +EVENT_SIZE = struct.calcsize(EVENT_FORMAT) |
| 21 | +EV_KEY = 0x01 |
| 22 | +BTN_LEFT = 272 |
| 23 | +BTN_RIGHT = 273 |
| 24 | +BTN_MIDDLE = 274 |
| 25 | + |
| 26 | +# ioctl: HIDIOCSFEATURE(len) = _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len) |
| 27 | +def HIDIOCSFEATURE(length): |
| 28 | + return 0xC0000000 | (length << 16) | (ord("H") << 8) | 0x06 |
| 29 | + |
| 30 | + |
| 31 | +def find_hidraw(): |
| 32 | + for path in sorted(glob.glob("/sys/class/hidraw/hidraw*")): |
| 33 | + uevent = os.path.join(path, "device", "uevent") |
| 34 | + try: |
| 35 | + with open(uevent) as f: |
| 36 | + content = f.read().upper() |
| 37 | + if f"0000{VENDOR}" in content and f"0000{PRODUCT}" in content: |
| 38 | + return os.path.join("/dev", os.path.basename(path)) |
| 39 | + except OSError: |
| 40 | + continue |
| 41 | + return None |
| 42 | + |
| 43 | + |
| 44 | +def find_touchpad_event(): |
| 45 | + for path in sorted(glob.glob("/sys/class/input/event*/device/name")): |
| 46 | + try: |
| 47 | + with open(path) as f: |
| 48 | + name = f.read().strip().upper() |
| 49 | + if VENDOR in name and PRODUCT in name and "TOUCHPAD" in name: |
| 50 | + event = path.split("/")[-3] |
| 51 | + return os.path.join("/dev/input", event) |
| 52 | + except OSError: |
| 53 | + continue |
| 54 | + return None |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + hidraw = find_hidraw() |
| 59 | + if not hidraw: |
| 60 | + print("No Synaptics haptic touchpad hidraw device found", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + event = find_touchpad_event() |
| 64 | + if not event: |
| 65 | + print("No Synaptics haptic touchpad input device found", file=sys.stderr) |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | + print(f"Haptic touchpad: hidraw={hidraw} input={event} intensity={INTENSITY}", flush=True) |
| 69 | + |
| 70 | + haptic_report = struct.pack("BB", REPORT_ID, INTENSITY) |
| 71 | + ioctl_req = HIDIOCSFEATURE(len(haptic_report)) |
| 72 | + |
| 73 | + hidraw_fd = os.open(hidraw, os.O_RDWR) |
| 74 | + event_fd = os.open(event, os.O_RDONLY) |
| 75 | + |
| 76 | + try: |
| 77 | + while True: |
| 78 | + data = os.read(event_fd, EVENT_SIZE) |
| 79 | + if len(data) < EVENT_SIZE: |
| 80 | + continue |
| 81 | + _, _, ev_type, code, value = struct.unpack(EVENT_FORMAT, data) |
| 82 | + if ev_type == EV_KEY and code in (BTN_LEFT, BTN_RIGHT, BTN_MIDDLE) and value == 1: |
| 83 | + try: |
| 84 | + fcntl.ioctl(hidraw_fd, ioctl_req, haptic_report) |
| 85 | + except OSError: |
| 86 | + pass |
| 87 | + except KeyboardInterrupt: |
| 88 | + pass |
| 89 | + finally: |
| 90 | + os.close(event_fd) |
| 91 | + os.close(hidraw_fd) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments