setting of keyboard with trackpoint

完整程式請看 https://github.com/bseibold/tpkbdctl

針對聯想/IBM的小紅帽鍵盤,控制小紅帽的敏感度的程式如下。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/input.h>
#include <linux/hiddev.h>
#include <linux/hidraw.h>
#define HIDIOCSFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
#define HIDIOCGFEATURE(len)    _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
int find_device() {
    int i, fd, ret, descsize;
    char name[255];
    struct hidraw_devinfo devinfo;
    for (i=0; i<255; i++) {
        snprintf(name, 255, "/dev/hidraw%d", i);
        fd = open(name, O_RDWR|O_NONBLOCK);
        if (!fd)
            continue;
        ret = ioctl(fd, HIDIOCGRAWINFO, &devinfo);
        if (ret) {
            close(fd);
            continue;
        }
        if (devinfo.vendor != 0x17ef || devinfo.product != 0x6009) {
            close(fd);
            continue;
        }
        ret = ioctl(fd, HIDIOCGRDESCSIZE, &descsize);
        if (ret || descsize < 100) {
            close(fd);
            continue;
        }
        fprintf(stderr, "Found device at %s\n", name);
        return fd;
    }
    return 0;
}
int main(int argc, char* argv[]) {
    int val1 = 0;
    int val2 = 0;
    int val3 = 0;
    int fd, ret;
    char buf[5];
    if (argc > 1) {
        val1 = atoi(argv[1]);
        val2 = atoi(argv[2]);
        val3 = atoi(argv[3]);
    /*    sensitivity = atoi(argv[1]); */
    }
   
    fd = find_device();
    if (!fd) {
        fprintf(stderr, "No device found.\n");
        exit(1);
    }
    buf[0] = 4;
    buf[1] = 0x6a;
    buf[2] = val1;
    buf[3] = val2;
    buf[4] = val3;
    fprintf(stderr, "Setting values %d, %d, %d\n", val1, val2, val3);
    ret = ioctl(fd, HIDIOCSFEATURE(5), buf);
    if (ret < 0) {
        close(fd);
        fprintf(stderr, "Error, could not set sensitivity (%d)\n", ret);
        exit(1);
    }   
    close(fd);
}