Added libevdev

This commit is contained in:
2020-10-02 21:37:41 -07:00
parent 26f132e866
commit 158dde5351
2 changed files with 70 additions and 35 deletions

View File

@ -3,7 +3,7 @@
obj-m += hanvon.o obj-m += hanvon.o
all: all:
gcc hanvon-libusb.c ./hidapi/linux/hid.o -L/usr/local/lib -lusb-1.0 -ludev -g gcc hanvon-libusb.c -I/usr/include/libevdev-1.0/ -L/usr/local/lib -lusb-1.0 -ludev -levdev -g
archive: archive:
tar f - --exclude=.git -C ../ -c hanvon | gzip -c9 > ../hanvon-`date +%Y%m%d`.tgz tar f - --exclude=.git -C ../ -c hanvon | gzip -c9 > ../hanvon-`date +%Y%m%d`.tgz

View File

@ -19,9 +19,9 @@
#include <stdio.h> #include <stdio.h>
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
#include "hidapi/hidapi.h" #include <libevdev/libevdev.h>
#include <wchar.h> #include <libevdev/libevdev-uinput.h>
#include <locale.h> // #include <asm/unaligned.h>
#define STATE_SUCCESS 0 #define STATE_SUCCESS 0
#define STATE_NOT_FOUND 1 #define STATE_NOT_FOUND 1
@ -70,6 +70,7 @@ struct hanvon {
}; };
int find_device(libusb_device **list, unsigned int count) { int find_device(libusb_device **list, unsigned int count) {
if (count < 0) { if (count < 0) {
return -1; return -1;
@ -107,30 +108,83 @@ int find_device(libusb_device **list, unsigned int count) {
return found; return found;
} }
void display_packets(const unsigned char* buf) {
for(int i = 0; i < AM_PACKET_LEN; i++) {
printf("0x%x, ", buf[i]);
}
printf("\n");
}
void callback(struct libusb_transfer *transfer) { void callback(struct libusb_transfer *transfer) {
unsigned char *data = transfer -> buffer; unsigned char *data = transfer -> buffer;
for(int i = 0; i < 10; i++) { display_packets(data);
printf("0x%x, ", data[i]); }
void callback_gp0504 (struct libusb_transfer *transfer) { // for callback
unsigned char *data = transfer -> buffer;
struct libevdev_uinput *ud = transfer -> user_data;
switch(data[0]) {
case 0x01:
break;
case 0x02:
if((data[1] & 0xf0) != 0) {
;
// libevdev_uinput_write_event(ud, ABS_X, )
}
break;
default:
display_packets(data);
break;
} }
printf("placeholder\n"); }
// determine which callback devices to use and how to initialize libevdev
int init_ctrl(struct libusb_device * const d, struct libevdev * evdev, struct libevdev_uinput *uidev) {
int is_ok = 0;
if (d == NULL) {
return is_ok;
}
struct libusb_device_descriptor desc;
libusb_get_device_descriptor(d, &desc);
evdev = libevdev_new();
switch(desc.idProduct) {
default: // every tablet has these features - allow fall through
libevdev_enable_event_type(evdev, EV_ABS); // enable absolute position
libevdev_enable_event_type(evdev, EV_KEY); // enable pen button
libevdev_enable_event_code(evdev, EV_KEY, BTN_LEFT, NULL); // pen tap
libevdev_enable_event_code(evdev, EV_KEY, BTN_RIGHT, NULL); // pen button
case PRODUCT_ID_GP0906:
break;
case PRODUCT_ID_APPIV0906:
break;
case PRODUCT_ID_GP0504:
libevdev_set_name(evdev, "Hanvon Graphicpal GP0504");
break;
}
int err = libevdev_uinput_create_from_device(evdev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev);
if (err == 0) {
is_ok = 1;
}
return is_ok;
} }
int handle_device_lusb(libusb_device *d) { int handle_device_lusb(libusb_device *d) {
libusb_device_handle *h; libusb_device_handle *h;
int status = libusb_open(d, &h); int status = libusb_open(d, &h);
if (status < 0 || h == NULL) { if (status < 0 || h == NULL) {
printf("Error opening device, %i.\n", status); printf("Error opening device, %i.\n", status);
libusb_exit(NULL); libusb_exit(NULL);
return 0; return 0;
} }
// Wait and handle interrupts? struct libevdev *evdev = NULL;
struct libusb_transfer *tx; struct libevdev_uinput *uidev = NULL;
init_ctrl(d, evdev, uidev);
struct libusb_transfer *tx;
const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v const int ENDPOINT_ADDR = 0x81; // bEndpointAddress from lsusb -v
const unsigned int LEN = 10; // wMaxPacketSize from lsusb -v // AM_PACKET_LEN = 10; // wMaxPacketSize from lsusb -v
unsigned char buffer[LEN]; unsigned char buffer[AM_PACKET_LEN];
// Allocate memory for transfer, configure, then submit // Allocate memory for transfer, configure, then submit
tx = libusb_alloc_transfer(0); tx = libusb_alloc_transfer(0);
@ -138,10 +192,10 @@ int handle_device_lusb(libusb_device *d) {
h, h,
ENDPOINT_ADDR, ENDPOINT_ADDR,
buffer, buffer,
LEN, AM_PACKET_LEN,
callback, callback,
NULL, uidev, // extra data to send in tx
130); // timeout in milliseconds 130); // timeout in milliseconds
do { do {
status = libusb_submit_transfer(tx); status = libusb_submit_transfer(tx);
if (status < 0 ) { if (status < 0 ) {
@ -149,6 +203,7 @@ int handle_device_lusb(libusb_device *d) {
} }
libusb_handle_events(NULL); libusb_handle_events(NULL);
} while (1); } while (1);
libevdev_uinput_destroy(uidev);
return 0; return 0;
} }
@ -179,27 +234,7 @@ int main()
} }
libusb_device *device = devs[index]; libusb_device *device = devs[index];
libusb_free_device_list (devs, UNREF_DEVICE); libusb_free_device_list (devs, UNREF_DEVICE);
int res = hid_init();
hid_device *handle = hid_open(VENDOR_ID_HANVON, PRODUCT_ID_GP0504, NULL);
if (handle != NULL) {
// wchar_t wstr[80];
// setlocale(LC_CTYPE, "");
unsigned char buf[10];
res = hid_read(handle, buf, 10);
for (unsigned int i = 0; i < 10; i++) {
printf("0x%x, ", buf[i]);
}
} else {
printf("hidapi: Could not open device\n");
}
hid_close(handle);
res = hid_exit();
int s = handle_device_lusb(device); int s = handle_device_lusb(device);
libusb_exit(NULL); libusb_exit(NULL);
return 0; return 0;
} }