Intial commit with original files, docs and modified Linux source.

This commit is contained in:
2019-04-13 00:24:49 -04:00
commit 754b8ccb3a
111 changed files with 13099 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
#ifndef __GLOBAL_H__
#define __GLOBAL_H__
#define INVALID_NUMBER -9999
struct token_t
{
const char* token;
int value;
};
#define BLOCK_NONE -1
#define BLOCK_LAYERDEF 0
#define BLOCK_REMAP 1
#define BLOCK_MACRO 2
#ifndef _MSC_VER
#define strnicmp strncasecmp
#define stricmp strcasecmp
#endif
#endif // __GLOBAL_H__
+381
View File
@@ -0,0 +1,381 @@
/* Simple Raw HID functions for Linux - for use with Teensy RawHID example
* http://www.pjrc.com/teensy/rawhid.html
* Copyright (c) 2009 PJRC.COM, LLC
*
* rawhid_open - open 1 or more devices
* rawhid_recv - receive a packet
* rawhid_send - send a packet
* rawhid_close - close a device
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above description, website URL and copyright notice and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Version 1.0: Initial Release
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/param.h>
#include <usb.h>
#include "rawhid.h"
// On Linux there are several options to access HID devices.
//
// libusb 0.1 - the only way that works well on all distributions
// libusb 1.0 - someday will become standard on most distributions
// hidraw driver - relatively new, not supported on many distributions (yet)
// hiddev driver - old, ubuntu, fedora, others dropping support
// usbfs - low level usb API: http://www.kernel.org/doc/htmldocs/usb.html#usbfs
//
// This code uses libusb 0.1, which is well supported on all linux distributions
// and very stable and widely used by many programs. libusb 0.1 only provides a
// simple synchronous interface, basically the same as this code needs. However,
// if you want non-blocking I/O, libusb 0.1 simply does not provide that. There
// is also no kernel-level buffering, so performance is poor.
//
// UPDATE: As of November 2011, hidraw support seems to be working well in all
// major linux distributions. Embedded and "small" distros, used on ARM boards,
// routers and embedded hardware stil seem to omit the hidraw driver.
//
// The hidraw driver is a great solution. However, it has only been supported
// by relatively recent (in 2009) kernels. Here is a quick survey of the status
// of hidraw support in various distributions as of Sept 2009:
//
// Fedora 11: works, kernel 2.6.29.4
// Mandiva 2009.1: works, kernel 2.6.29.1
// Ubuntu 9.10-alpha6: works, kernel 2.6.31
// Ubuntu 9.04: sysfs attrs chain broken (hidraw root only), 2.6.28 kernel
// openSUSE 11.1: sysfs attrs chain broken (hidraw root only), 2.6.27.7 kernel
// Debian Live, Lenny 5.0.2: sysfs attrs chain broken (hidraw root only), 2.6.26
// SimplyMEPIS 8.0.10: sysfs attrs chain broken (hidraw root only), 2.6.27
// Mint 7: sysfs attrs chain broken (hidraw root only), 2.6.28 kernel
// Gentoo 2008.0-r1: sysfs attrs chain broken (hidraw root only), 2.6.24 kernel
// Centos 5: no hidraw or hiddev devices, 2.6.18 kernel
// Slitaz 2.0: no hidraw devices (has hiddev), 2.6.25.5 kernel
// Puppy 4.3: no hidraw devices (has hiddev), 2.6.30.5 kernel
// Damn Small 4.4.10: (would not boot)
// Gentoo 10.0-test20090926: (would not boot)
// PCLinuxOS 2009.2: (would not boot)
// Slackware: (no live cd available? www.slackware-live.org dead)
#define printf(...) // comment this out for lots of info
// a list of all opened HID devices, so the caller can
// simply refer to them by number
typedef struct hid_struct hid_t;
static hid_t *first_hid = NULL;
static hid_t *last_hid = NULL;
struct hid_struct {
usb_dev_handle *usb;
int open;
int iface;
int ep_in;
int ep_out;
struct hid_struct *prev;
struct hid_struct *next;
};
// private functions, not intended to be used from outside this file
static void add_hid(hid_t *h);
static hid_t * get_hid(int num);
static void free_all_hid(void);
static void hid_close(hid_t *hid);
static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end);
// rawhid_recv - receive a packet
// Inputs:
// num = device to receive from (zero based)
// buf = buffer to receive packet
// len = buffer's size
// timeout = time to wait, in milliseconds
// Output:
// number of bytes received, or -1 on error
//
int rawhid_recv(int num, void *buf, int len, int timeout)
{
hid_t *hid;
int r;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
r = usb_interrupt_read(hid->usb, hid->ep_in, buf, len, timeout);
if (r >= 0) return r;
if (r == -110) return 0; // timeout
return -1;
}
// rawhid_send - send a packet
// Inputs:
// num = device to transmit to (zero based)
// buf = buffer containing packet to send
// len = number of bytes to transmit
// timeout = time to wait, in milliseconds
// Output:
// number of bytes sent, or -1 on error
//
int rawhid_send(int num, void *buf, int len, int timeout)
{
hid_t *hid;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
if (hid->ep_out) {
return usb_interrupt_write(hid->usb, hid->ep_out, buf, len, timeout);
} else {
return usb_control_msg(hid->usb, 0x21, 9, 0, hid->iface, buf, len, timeout);
}
}
// rawhid_open - open 1 or more devices
//
// Inputs:
// max = maximum number of devices to open
// vid = Vendor ID, or -1 if any
// pid = Product ID, or -1 if any
// usage_page = top level usage page, or -1 if any
// usage = top level usage number, or -1 if any
// Output:
// actual number of devices opened
//
int rawhid_open(int max, int vid, int pid, int usage_page, int usage)
{
struct usb_bus *bus;
struct usb_device *dev;
struct usb_interface *iface;
struct usb_interface_descriptor *desc;
struct usb_endpoint_descriptor *ep;
usb_dev_handle *u;
uint8_t buf[1024], *p;
int i, n, len, tag, ep_in, ep_out, count=0, claimed;
uint32_t val=0, parsed_usage, parsed_usage_page;
hid_t *hid;
if (first_hid) free_all_hid();
printf("rawhid_open, max=%d\n", max);
if (max < 1) return 0;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (vid > 0 && dev->descriptor.idVendor != vid) continue;
if (pid > 0 && dev->descriptor.idProduct != pid) continue;
if (!dev->config) continue;
if (dev->config->bNumInterfaces < 1) continue;
printf("device: vid=%04X, pic=%04X, with %d iface\n",
dev->descriptor.idVendor,
dev->descriptor.idProduct,
dev->config->bNumInterfaces);
iface = dev->config->interface;
u = NULL;
claimed = 0;
for (i=0; i<dev->config->bNumInterfaces && iface; i++, iface++) {
desc = iface->altsetting;
if (!desc) continue;
printf(" type %d, %d, %d\n", desc->bInterfaceClass,
desc->bInterfaceSubClass, desc->bInterfaceProtocol);
if (desc->bInterfaceClass != 3) continue;
if (desc->bInterfaceSubClass != 0) continue;
if (desc->bInterfaceProtocol != 0) continue;
ep = desc->endpoint;
ep_in = ep_out = 0;
for (n = 0; n < desc->bNumEndpoints; n++, ep++) {
if (ep->bEndpointAddress & 0x80) {
if (!ep_in) ep_in = ep->bEndpointAddress & 0x7F;
printf(" IN endpoint %d\n", ep_in);
} else {
if (!ep_out) ep_out = ep->bEndpointAddress;
printf(" OUT endpoint %d\n", ep_out);
}
}
if (!ep_in) continue;
if (!u) {
u = usb_open(dev);
if (!u) {
printf(" unable to open device\n");
break;
}
}
printf(" hid interface (generic)\n");
if (usb_get_driver_np(u, i, (char *)buf, sizeof(buf)) >= 0) {
printf(" in use by driver \"%s\"\n", buf);
if (usb_detach_kernel_driver_np(u, i) < 0) {
printf(" unable to detach from kernel\n");
continue;
}
}
if (usb_claim_interface(u, i) < 0) {
printf(" unable claim interface %d\n", i);
continue;
}
len = usb_control_msg(u, 0x81, 6, 0x2200, i, (char *)buf, sizeof(buf), 250);
printf(" descriptor, len=%d\n", len);
if (len < 2) {
usb_release_interface(u, i);
continue;
}
p = buf;
parsed_usage_page = parsed_usage = 0;
while ((tag = hid_parse_item(&val, &p, buf + len)) >= 0) {
printf(" tag: %X, val %X\n", tag, val);
if (tag == 4) parsed_usage_page = val;
if (tag == 8) parsed_usage = val;
if (parsed_usage_page && parsed_usage) break;
}
if ((!parsed_usage_page) || (!parsed_usage) ||
(usage_page > 0 && parsed_usage_page != usage_page) ||
(usage > 0 && parsed_usage != usage)) {
usb_release_interface(u, i);
continue;
}
hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
if (!hid) {
usb_release_interface(u, i);
continue;
}
hid->usb = u;
hid->iface = i;
hid->ep_in = ep_in;
hid->ep_out = ep_out;
hid->open = 1;
add_hid(hid);
claimed++;
count++;
if (count >= max) return count;
}
if (u && !claimed) usb_close(u);
}
}
return count;
}
// rawhid_close - close a device
//
// Inputs:
// num = device to close (zero based)
// Output
// (nothing)
//
void rawhid_close(int num)
{
hid_t *hid;
hid = get_hid(num);
if (!hid || !hid->open) return;
hid_close(hid);
}
// Chuck Robey wrote a real HID report parser
// (chuckr@telenix.org) chuckr@chuckr.org
// http://people.freebsd.org/~chuckr/code/python/uhidParser-0.2.tbz
// this tiny thing only needs to extract the top-level usage page
// and usage, and even then is may not be truly correct, but it does
// work with the Teensy Raw HID example.
static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end)
{
const uint8_t *p = *data;
uint8_t tag;
int table[4] = {0, 1, 2, 4};
int len;
if (p >= end) return -1;
if (p[0] == 0xFE) {
// long item, HID 1.11, 6.2.2.3, page 27
if (p + 5 >= end || p + p[1] >= end) return -1;
tag = p[2];
*val = 0;
len = p[1] + 5;
} else {
// short item, HID 1.11, 6.2.2.2, page 26
tag = p[0] & 0xFC;
len = table[p[0] & 0x03];
if (p + len + 1 >= end) return -1;
switch (p[0] & 0x03) {
case 3: *val = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24); break;
case 2: *val = p[1] | (p[2] << 8); break;
case 1: *val = p[1]; break;
case 0: *val = 0; break;
}
}
*data += len + 1;
return tag;
}
static void add_hid(hid_t *h)
{
if (!first_hid || !last_hid) {
first_hid = last_hid = h;
h->next = h->prev = NULL;
return;
}
last_hid->next = h;
h->prev = last_hid;
h->next = NULL;
last_hid = h;
}
static hid_t * get_hid(int num)
{
hid_t *p;
for (p = first_hid; p && num > 0; p = p->next, num--) ;
return p;
}
static void free_all_hid(void)
{
hid_t *p, *q;
for (p = first_hid; p; p = p->next) {
hid_close(p);
}
p = first_hid;
while (p) {
q = p;
p = p->next;
free(q);
}
first_hid = last_hid = NULL;
}
static void hid_close(hid_t *hid)
{
hid_t *p;
int others=0;
usb_release_interface(hid->usb, hid->iface);
for (p = first_hid; p; p = p->next) {
if (p->open && p->usb == hid->usb) others++;
}
if (!others) usb_close(hid->usb);
hid->usb = NULL;
}
+409
View File
@@ -0,0 +1,409 @@
/* Simple Raw HID functions for Linux - for use with Teensy RawHID example
* http://www.pjrc.com/teensy/rawhid.html
* Copyright (c) 2009 PJRC.COM, LLC
*
* rawhid_open - open 1 or more devices
* rawhid_recv - receive a packet
* rawhid_send - send a packet
* rawhid_close - close a device
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above description, website URL and copyright notice and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Version 1.0: Initial Release
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/hid/IOHIDLib.h>
#include "rawhid.h"
#define BUFFER_SIZE 64
#define printf(...) // comment this out to get lots of info printed
// a list of all opened HID devices, so the caller can
// simply refer to them by number
typedef struct hid_struct hid_t;
typedef struct buffer_struct buffer_t;
static hid_t *first_hid = NULL;
static hid_t *last_hid = NULL;
struct hid_struct {
IOHIDDeviceRef ref;
int open;
uint8_t buffer[BUFFER_SIZE];
buffer_t *first_buffer;
buffer_t *last_buffer;
struct hid_struct *prev;
struct hid_struct *next;
};
struct buffer_struct {
struct buffer_struct *next;
uint32_t len;
uint8_t buf[BUFFER_SIZE];
};
// private functions, not intended to be used from outside this file
static void add_hid(hid_t *);
static hid_t * get_hid(int);
static void free_all_hid(void);
static void hid_close(hid_t *);
static void attach_callback(void *, IOReturn, void *, IOHIDDeviceRef);
static void detach_callback(void *, IOReturn, void *hid_mgr, IOHIDDeviceRef dev);
static void timeout_callback(CFRunLoopTimerRef, void *);
static void input_callback(void *, IOReturn, void *, IOHIDReportType,
uint32_t, uint8_t *, CFIndex);
// rawhid_recv - receive a packet
// Inputs:
// num = device to receive from (zero based)
// buf = buffer to receive packet
// len = buffer's size
// timeout = time to wait, in milliseconds
// Output:
// number of bytes received, or -1 on error
//
int rawhid_recv(int num, void *buf, int len, int timeout)
{
hid_t *hid;
buffer_t *b;
CFRunLoopTimerRef timer=NULL;
CFRunLoopTimerContext context;
int ret=0, timeout_occurred=0;
if (len < 1) return 0;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
if ((b = hid->first_buffer) != NULL) {
if (len > b->len) len = b->len;
memcpy(buf, b->buf, len);
hid->first_buffer = b->next;
free(b);
return len;
}
memset(&context, 0, sizeof(context));
context.info = &timeout_occurred;
timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent() +
(double)timeout / 1000.0, 0, 0, 0, timeout_callback, &context);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);
while (1) {
CFRunLoopRun();
if ((b = hid->first_buffer) != NULL) {
if (len > b->len) len = b->len;
memcpy(buf, b->buf, len);
hid->first_buffer = b->next;
free(b);
ret = len;
break;
}
if (!hid->open) {
printf("rawhid_recv, device not open\n");
ret = -1;
break;
}
if (timeout_occurred) break;
}
CFRunLoopTimerInvalidate(timer);
CFRelease(timer);
return ret;
}
static void input_callback(void *context, IOReturn ret, void *sender,
IOHIDReportType type, uint32_t id, uint8_t *data, CFIndex len)
{
buffer_t *n;
hid_t *hid;
printf("input_callback\n");
if (ret != kIOReturnSuccess || len < 1) return;
hid = context;
if (!hid || hid->ref != sender) return;
n = (buffer_t *)malloc(sizeof(buffer_t));
if (!n) return;
if (len > BUFFER_SIZE) len = BUFFER_SIZE;
memcpy(n->buf, data, len);
n->len = len;
n->next = NULL;
if (!hid->first_buffer || !hid->last_buffer) {
hid->first_buffer = hid->last_buffer = n;
} else {
hid->last_buffer->next = n;
hid->last_buffer = n;
}
CFRunLoopStop(CFRunLoopGetCurrent());
}
static void timeout_callback(CFRunLoopTimerRef timer, void *info)
{
printf("timeout_callback\n");
*(int *)info = 1;
CFRunLoopStop(CFRunLoopGetCurrent());
}
void output_callback(void *context, IOReturn ret, void *sender,
IOHIDReportType type, uint32_t id, uint8_t *data, CFIndex len)
{
printf("output_callback, r=%d\n", ret);
if (ret == kIOReturnSuccess) {
*(int *)context = len;
} else {
// timeout if not success?
*(int *)context = 0;
}
CFRunLoopStop(CFRunLoopGetCurrent());
}
// rawhid_send - send a packet
// Inputs:
// num = device to transmit to (zero based)
// buf = buffer containing packet to send
// len = number of bytes to transmit
// timeout = time to wait, in milliseconds
// Output:
// number of bytes sent, or -1 on error
//
int rawhid_send(int num, void *buf, int len, int timeout)
{
hid_t *hid;
int result=-100;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
#if 1
#warning "Send timeout not implemented on MACOSX"
IOReturn ret = IOHIDDeviceSetReport(hid->ref, kIOHIDReportTypeOutput, 0, buf, len);
result = (ret == kIOReturnSuccess) ? len : -1;
#endif
#if 0
// No matter what I tried this never actually sends an output
// report and output_callback never gets called. Why??
// Did I miss something? This is exactly the same params as
// the sync call that works. Is it an Apple bug?
// (submitted to Apple on 22-sep-2009, problem ID 7245050)
//
IOHIDDeviceScheduleWithRunLoop(hid->ref, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
// should already be scheduled with run loop by attach_callback,
// sadly this doesn't make any difference either way
//
IOHIDDeviceSetReportWithCallback(hid->ref, kIOHIDReportTypeOutput,
0, buf, len, (double)timeout / 1000.0, output_callback, &result);
while (1) {
printf("enter run loop (send)\n");
CFRunLoopRun();
printf("leave run loop (send)\n");
if (result > -100) break;
if (!hid->open) {
result = -1;
break;
}
}
#endif
return result;
}
// rawhid_open - open 1 or more devices
//
// Inputs:
// max = maximum number of devices to open
// vid = Vendor ID, or -1 if any
// pid = Product ID, or -1 if any
// usage_page = top level usage page, or -1 if any
// usage = top level usage number, or -1 if any
// Output:
// actual number of devices opened
//
int rawhid_open(int max, int vid, int pid, int usage_page, int usage)
{
static IOHIDManagerRef hid_manager=NULL;
CFMutableDictionaryRef dict;
CFNumberRef num;
IOReturn ret;
hid_t *p;
int count=0;
if (first_hid) free_all_hid();
printf("rawhid_open, max=%d\n", max);
if (max < 1) return 0;
// Start the HID Manager
// http://developer.apple.com/technotes/tn2007/tn2187.html
if (!hid_manager) {
hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (hid_manager == NULL || CFGetTypeID(hid_manager) != IOHIDManagerGetTypeID()) {
if (hid_manager) CFRelease(hid_manager);
return 0;
}
}
if (vid > 0 || pid > 0 || usage_page > 0 || usage > 0) {
// Tell the HID Manager what type of devices we want
dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (!dict) return 0;
if (vid > 0) {
num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &vid);
CFDictionarySetValue(dict, CFSTR(kIOHIDVendorIDKey), num);
CFRelease(num);
}
if (pid > 0) {
num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &pid);
CFDictionarySetValue(dict, CFSTR(kIOHIDProductIDKey), num);
CFRelease(num);
}
if (usage_page > 0) {
num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage_page);
CFDictionarySetValue(dict, CFSTR(kIOHIDPrimaryUsagePageKey), num);
CFRelease(num);
}
if (usage > 0) {
num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
CFDictionarySetValue(dict, CFSTR(kIOHIDPrimaryUsageKey), num);
CFRelease(num);
}
IOHIDManagerSetDeviceMatching(hid_manager, dict);
CFRelease(dict);
} else {
IOHIDManagerSetDeviceMatching(hid_manager, NULL);
}
// set up a callbacks for device attach & detach
IOHIDManagerScheduleWithRunLoop(hid_manager, CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, attach_callback, NULL);
IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, detach_callback, NULL);
ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
if (ret != kIOReturnSuccess) {
IOHIDManagerUnscheduleFromRunLoop(hid_manager,
CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(hid_manager);
return 0;
}
printf("run loop\n");
// let it do the callback for all devices
while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) == kCFRunLoopRunHandledSource) ;
// count up how many were added by the callback
for (p = first_hid; p; p = p->next) count++;
return count;
}
// rawhid_close - close a device
//
// Inputs:
// num = device to close (zero based)
// Output
// (nothing)
//
void rawhid_close(int num)
{
hid_t *hid;
hid = get_hid(num);
if (!hid || !hid->open) return;
hid_close(hid);
hid->open = 0;
}
static void add_hid(hid_t *h)
{
if (!first_hid || !last_hid) {
first_hid = last_hid = h;
h->next = h->prev = NULL;
return;
}
last_hid->next = h;
h->prev = last_hid;
h->next = NULL;
last_hid = h;
}
static hid_t * get_hid(int num)
{
hid_t *p;
for (p = first_hid; p && num > 0; p = p->next, num--) ;
return p;
}
static void free_all_hid(void)
{
hid_t *p, *q;
for (p = first_hid; p; p = p->next) {
hid_close(p);
}
p = first_hid;
while (p) {
q = p;
p = p->next;
free(q);
}
first_hid = last_hid = NULL;
}
static void hid_close(hid_t *hid)
{
if (!hid || !hid->open || !hid->ref) return;
IOHIDDeviceUnscheduleFromRunLoop(hid->ref, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode);
IOHIDDeviceClose(hid->ref, kIOHIDOptionsTypeNone);
hid->ref = NULL;
}
static void detach_callback(void *context, IOReturn r, void *hid_mgr, IOHIDDeviceRef dev)
{
hid_t *p;
printf("detach callback\n");
for (p = first_hid; p; p = p->next) {
if (p->ref == dev) {
p->open = 0;
CFRunLoopStop(CFRunLoopGetCurrent());
return;
}
}
}
static void attach_callback(void *context, IOReturn r, void *hid_mgr, IOHIDDeviceRef dev)
{
struct hid_struct *h;
printf("attach callback\n");
if (IOHIDDeviceOpen(dev, kIOHIDOptionsTypeNone) != kIOReturnSuccess) return;
h = (hid_t *)malloc(sizeof(hid_t));
if (!h) return;
memset(h, 0, sizeof(hid_t));
IOHIDDeviceScheduleWithRunLoop(dev, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
IOHIDDeviceRegisterInputReportCallback(dev, h->buffer, sizeof(h->buffer),
input_callback, h);
h->ref = dev;
h->open = 1;
add_hid(h);
}
+330
View File
@@ -0,0 +1,330 @@
/* Simple Raw HID functions for Windows - for use with Teensy RawHID example
* http://www.pjrc.com/teensy/rawhid.html
* Copyright (c) 2009 PJRC.COM, LLC
*
* rawhid_open - open 1 or more devices
* rawhid_recv - receive a packet
* rawhid_send - send a packet
* rawhid_close - close a device
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above description, website URL and copyright notice and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Version 1.0: Initial Release
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <windows.h>
#include <setupapi.h>
#include <ddk/hidsdi.h>
#include <ddk/hidclass.h>
#include "rawhid.h"
// a list of all opened HID devices, so the caller can
// simply refer to them by number
typedef struct hid_struct hid_t;
static hid_t *first_hid = NULL;
static hid_t *last_hid = NULL;
struct hid_struct {
HANDLE handle;
int open;
struct hid_struct *prev;
struct hid_struct *next;
};
static HANDLE rx_event=NULL;
static HANDLE tx_event=NULL;
static CRITICAL_SECTION rx_mutex;
static CRITICAL_SECTION tx_mutex;
// private functions, not intended to be used from outside this file
static void add_hid(hid_t *h);
static hid_t * get_hid(int num);
static void free_all_hid(void);
static void hid_close(hid_t *hid);
void print_win32_err(void);
// rawhid_recv - receive a packet
// Inputs:
// num = device to receive from (zero based)
// buf = buffer to receive packet
// len = buffer's size
// timeout = time to wait, in milliseconds
// Output:
// number of bytes received, or -1 on error
//
int RAWHIDFN rawhid_recv(int num, void *buf, int len, int timeout)
{
hid_t *hid;
unsigned char tmpbuf[516];
OVERLAPPED ov;
DWORD n, r;
if (sizeof(tmpbuf) < len + 1) return -1;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
EnterCriticalSection(&rx_mutex);
ResetEvent(&rx_event);
memset(&ov, 0, sizeof(ov));
ov.hEvent = rx_event;
if (!ReadFile(hid->handle, tmpbuf, len + 1, NULL, &ov)) {
if (GetLastError() != ERROR_IO_PENDING) goto return_error;
r = WaitForSingleObject(rx_event, timeout);
if (r == WAIT_TIMEOUT) goto return_timeout;
if (r != WAIT_OBJECT_0) goto return_error;
}
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
LeaveCriticalSection(&rx_mutex);
if (n <= 0) return -1;
n--;
if (n > len) n = len;
memcpy(buf, tmpbuf + 1, n);
return n;
return_timeout:
CancelIo(hid->handle);
LeaveCriticalSection(&rx_mutex);
return 0;
return_error:
print_win32_err();
LeaveCriticalSection(&rx_mutex);
return -1;
}
// rawhid_send - send a packet
// Inputs:
// num = device to transmit to (zero based)
// buf = buffer containing packet to send
// len = number of bytes to transmit
// timeout = time to wait, in milliseconds
// Output:
// number of bytes sent, or -1 on error
//
int RAWHIDFN rawhid_send(int num, void *buf, int len, int timeout)
{
hid_t *hid;
unsigned char tmpbuf[516];
OVERLAPPED ov;
DWORD n, r;
if (sizeof(tmpbuf) < len + 1) return -1;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
EnterCriticalSection(&tx_mutex);
ResetEvent(&tx_event);
memset(&ov, 0, sizeof(ov));
ov.hEvent = tx_event;
tmpbuf[0] = 0;
memcpy(tmpbuf + 1, buf, len);
if (!WriteFile(hid->handle, tmpbuf, len + 1, NULL, &ov)) {
if (GetLastError() != ERROR_IO_PENDING) goto return_error;
r = WaitForSingleObject(tx_event, timeout);
if (r == WAIT_TIMEOUT) goto return_timeout;
if (r != WAIT_OBJECT_0) goto return_error;
}
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
LeaveCriticalSection(&tx_mutex);
if (n <= 0) return -1;
return n - 1;
return_timeout:
CancelIo(hid->handle);
LeaveCriticalSection(&tx_mutex);
return 0;
return_error:
print_win32_err();
LeaveCriticalSection(&tx_mutex);
return -1;
}
// rawhid_open - open 1 or more devices
//
// Inputs:
// max = maximum number of devices to open
// vid = Vendor ID, or -1 if any
// pid = Product ID, or -1 if any
// usage_page = top level usage page, or -1 if any
// usage = top level usage number, or -1 if any
// Output:
// actual number of devices opened
//
int RAWHIDFN rawhid_open(int max, int vid, int pid, int usage_page, int usage)
{
GUID guid;
HDEVINFO info;
DWORD index=0, reqd_size;
SP_DEVICE_INTERFACE_DATA iface;
SP_DEVICE_INTERFACE_DETAIL_DATA *details;
HIDD_ATTRIBUTES attrib;
PHIDP_PREPARSED_DATA hid_data;
HIDP_CAPS capabilities;
HANDLE h;
BOOL ret;
hid_t *hid;
int count=0;
if (first_hid) free_all_hid();
if (max < 1) return 0;
if (!rx_event) {
rx_event = CreateEvent(NULL, TRUE, TRUE, NULL);
tx_event = CreateEvent(NULL, TRUE, TRUE, NULL);
InitializeCriticalSection(&rx_mutex);
InitializeCriticalSection(&tx_mutex);
}
HidD_GetHidGuid(&guid);
info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (info == INVALID_HANDLE_VALUE) return 0;
for (index=0; 1 ;index++) {
iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface);
if (!ret) return count;
SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL);
details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(reqd_size);
if (details == NULL) continue;
memset(details, 0, reqd_size);
details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details,
reqd_size, NULL, NULL);
if (!ret) {
free(details);
continue;
}
h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
free(details);
if (h == INVALID_HANDLE_VALUE) continue;
attrib.Size = sizeof(HIDD_ATTRIBUTES);
ret = HidD_GetAttributes(h, &attrib);
//printf("vid: %4x\n", attrib.VendorID);
if (!ret || (vid > 0 && attrib.VendorID != vid) ||
(pid > 0 && attrib.ProductID != pid) ||
!HidD_GetPreparsedData(h, &hid_data)) {
CloseHandle(h);
continue;
}
if (!HidP_GetCaps(hid_data, &capabilities) ||
(usage_page > 0 && capabilities.UsagePage != usage_page) ||
(usage > 0 && capabilities.Usage != usage)) {
HidD_FreePreparsedData(hid_data);
CloseHandle(h);
continue;
}
HidD_FreePreparsedData(hid_data);
hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
if (!hid) {
CloseHandle(h);
continue;
}
hid->handle = h;
hid->open = 1;
add_hid(hid);
count++;
if (count >= max) return count;
}
return count;
}
// rawhid_close - close a device
//
// Inputs:
// num = device to close (zero based)
// Output
// (nothing)
//
void RAWHIDFN rawhid_close(int num)
{
hid_t *hid;
hid = get_hid(num);
if (!hid || !hid->open) return;
hid_close(hid);
}
static void add_hid(hid_t *h)
{
if (!first_hid || !last_hid) {
first_hid = last_hid = h;
h->next = h->prev = NULL;
return;
}
last_hid->next = h;
h->prev = last_hid;
h->next = NULL;
last_hid = h;
}
static hid_t * get_hid(int num)
{
hid_t *p;
for (p = first_hid; p && num > 0; p = p->next, num--) ;
return p;
}
static void free_all_hid(void)
{
hid_t *p, *q;
for (p = first_hid; p; p = p->next) {
hid_close(p);
}
p = first_hid;
while (p) {
q = p;
p = p->next;
free(q);
}
first_hid = last_hid = NULL;
}
static void hid_close(hid_t *hid)
{
CloseHandle(hid->handle);
hid->handle = NULL;
}
void print_win32_err(void)
{
char buf[256];
DWORD err;
err = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
0, buf, sizeof(buf), NULL);
printf("err %ld: %s\n", err, buf);
}
+319
View File
@@ -0,0 +1,319 @@
#include "hid_tokens.h"
#include "global.h"
#include <string.h>
token_t meta_token_list[] =
{
{ "LCTRL", 0x01 }, // Left Control
{ "LSHIFT", 0x02 }, // Left Shift
{ "LALT", 0x04 }, // Left Alt
{ "LGUI", 0x08 }, // Left GUI
{ "RCTRL", 0x10 }, // Right Control
{ "RSHIFT", 0x20 }, // Right Shift
{ "RALT", 0x40 }, // Right Alt
{ "RGUI", 0x80 }, // Right GUI
{ "CTRL", 0x11 }, // Either/Both Control
{ "SHIFT", 0x22 }, // Either/Both Shift
{ "ALT", 0x44 }, // Either/Both Alt
{ "GUI", 0x88 }, // Either/Both GUI
{ "ALL", 0xFF }, // Any/All
};
token_t hid_token_list[] =
{
{ "UNASSIGNED", 0x00 }, // No Event
{ "OVERRUN_ERROR", 0x01 }, // Overrun Error
{ "POST_FAIL", 0x02 }, // POST Fail
{ "ERROR_UNDEFINED", 0x03 }, // ErrorUndefined
{ "A", 0x04 }, // a A
{ "B", 0x05 }, // b B
{ "C", 0x06 }, // c C
{ "D", 0x07 }, // d D
{ "E", 0x08 }, // e E
{ "F", 0x09 }, // f F
{ "G", 0x0A }, // g G
{ "H", 0x0B }, // h H
{ "I", 0x0C }, // i I
{ "J", 0x0D }, // j J
{ "K", 0x0E }, // k K
{ "L", 0x0F }, // l L
{ "M", 0x10 }, // m M
{ "N", 0x11 }, // n N
{ "O", 0x12 }, // o O
{ "P", 0x13 }, // p P
{ "Q", 0x14 }, // q Q
{ "R", 0x15 }, // r R
{ "S", 0x16 }, // s S
{ "T", 0x17 }, // t T
{ "U", 0x18 }, // u U
{ "V", 0x19 }, // v V
{ "W", 0x1A }, // w W
{ "X", 0x1B }, // x X
{ "Y", 0x1C }, // y Y
{ "Z", 0x1D }, // z Z
{ "1", 0x1E }, // 1 !
{ "2", 0x1F }, // 2 @
{ "3", 0x20 }, // 3 #
{ "4", 0x21 }, // 4 $
{ "5", 0x22 }, // 5 %
{ "6", 0x23 }, // 6 ^
{ "7", 0x24 }, // 7 &
{ "8", 0x25 }, // 8 *
{ "9", 0x26 }, // 9 (
{ "0", 0x27 }, // 0 )
{ "ENTER", 0x28 }, // Return
{ "ESC", 0x29 }, // Escape
{ "BACKSPACE", 0x2A }, // Backspace
{ "TAB", 0x2B }, // Tab
{ "SPACE", 0x2C }, // Space
{ "MINUS", 0x2D }, // minus _
{ "EQUAL", 0x2E }, // = +
{ "LEFT_BRACE", 0x2F }, // [ {
{ "RIGHT_BRACE", 0x30 }, // ] }
{ "BACKSLASH", 0x31 }, // \ |
{ "EUROPE_1", 0x32 }, // Europe 1
{ "SEMICOLON", 0x33 }, // ; :
{ "QUOTE", 0x34 }, // quotes
{ "BACK_QUOTE", 0x35 }, // ` ~
{ "COMMA", 0x36 }, // comma <
{ "PERIOD", 0x37 }, // . >
{ "SLASH", 0x38 }, // / ?
{ "CAPS_LOCK", 0x39 }, // Caps Lock
{ "F1", 0x3A }, // F1
{ "F2", 0x3B }, // F2
{ "F3", 0x3C }, // F3
{ "F4", 0x3D }, // F4
{ "F5", 0x3E }, // F5
{ "F6", 0x3F }, // F6
{ "F7", 0x40 }, // F7
{ "F8", 0x41 }, // F8
{ "F9", 0x42 }, // F9
{ "F10", 0x43 }, // F10
{ "F11", 0x44 }, // F11
{ "F12", 0x45 }, // F12
{ "PRINTSCREEN", 0x46 }, // Print Screen
{ "SCROLL_LOCK", 0x47 }, // Scroll Lock
{ "PAUSE", 0x48 }, // Pause
{ "INSERT", 0x49 }, // Insert
{ "HOME", 0x4A }, // Home
{ "PAGE_UP", 0x4B }, // Page Up
{ "DELETE", 0x4C }, // Delete
{ "END", 0x4D }, // End
{ "PAGE_DOWN", 0x4E }, // Page Down
{ "RIGHT", 0x4F }, // Right Arrow
{ "LEFT", 0x50 }, // Left Arrow
{ "DOWN", 0x51 }, // Down Arrow
{ "UP", 0x52 }, // Up Arrow
{ "NUM_LOCK", 0x53 }, // Num Lock
{ "PAD_SLASH", 0x54 }, // Keypad /
{ "PAD_ASTERIX", 0x55 }, // Keypad *
{ "PAD_MINUS", 0x56 }, // Keypad -
{ "PAD_PLUS", 0x57 }, // Keypad +
{ "PAD_ENTER", 0x58 }, // Keypad Enter
{ "PAD_1", 0x59 }, // Keypad 1 End
{ "PAD_2", 0x5A }, // Keypad 2 Down
{ "PAD_3", 0x5B }, // Keypad 3 PageDn
{ "PAD_4", 0x5C }, // Keypad 4 Left
{ "PAD_5", 0x5D }, // Keypad 5
{ "PAD_6", 0x5E }, // Keypad 6 Right
{ "PAD_7", 0x5F }, // Keypad 7 Home
{ "PAD_8", 0x60 }, // Keypad 8 Up
{ "PAD_9", 0x61 }, // Keypad 9 PageUp
{ "PAD_0", 0x62 }, // Keypad 0 Insert
{ "PAD_PERIOD", 0x63 }, // Keypad . Delete
{ "EUROPE_2", 0x64 }, // Europe 2
{ "APP", 0x65 }, // App
{ "POWER", 0x66 }, // Keyboard Power
{ "PAD_EQUALS", 0x67 }, // Keypad =
{ "F13", 0x68 }, // F13
{ "F14", 0x69 }, // F14
{ "F15", 0x6A }, // F15
{ "F16", 0x6B }, // F16
{ "F17", 0x6C }, // F17
{ "F18", 0x6D }, // F18
{ "F19", 0x6E }, // F19
{ "F20", 0x6F }, // F20
{ "F21", 0x70 }, // F21
{ "F22", 0x71 }, // F22
{ "F23", 0x72 }, // F23
{ "F24", 0x73 }, // F24
{ "EXECUTE", 0x74 }, // Keyboard Execute
{ "HELP", 0x75 }, // Keyboard Help
{ "MENU", 0x76 }, // Keyboard Menu
{ "SELECT", 0x77 }, // Keyboard Select
{ "STOP", 0x78 }, // Keyboard Stop
{ "AGAIN", 0x79 }, // Keyboard Again
{ "UNDO", 0x7A }, // Keyboard Undo
{ "CUT", 0x7B }, // Keyboard Cut
{ "COPY", 0x7C }, // Keyboard Copy
{ "PASTE", 0x7D }, // Keyboard Paste
{ "FIND", 0x7E }, // Keyboard Find
{ "MUTE", 0x7F }, // Keyboard Mute
{ "VOLUME_UP", 0x80 }, // Keyboard Volume Up
{ "VOLUME_DOWN", 0x81 }, // Keyboard Volume Dn
{ "LOCKING_CAPS_LOCK", 0x82 }, // Keyboard Locking Caps Lock
{ "LOCKING_NUM_LOCK", 0x83 }, // Keyboard Locking Num Lock
{ "LOCKING_SCROLL_LOCK", 0x84 }, // Keyboard Locking Scroll Lock
{ "PAD_COMMA", 0x85 }, // Keypad comma (Brazilian Keypad .)
{ "EQUAL_SIGN", 0x86 }, // Keyboard Equal Sign
{ "INTERNATIONAL_1", 0x87 }, // Keyboard Int'l 1 (Ro)
{ "INTERNATIONAL_2", 0x88 }, // Keyboard Intl'2 (Katakana/Hiragana)
{ "INTERNATIONAL_3", 0x89 }, // Keyboard Int'l 2 (Yen)
{ "INTERNATIONAL_4", 0x8A }, // Keyboard Int'l 4 (Henkan)
{ "INTERNATIONAL_5", 0x8B }, // Keyboard Int'l 5 (Muhenkan)
{ "INTERNATIONAL_6", 0x8C }, // Keyboard Int'l 6 (PC9800 Keypad comma)
{ "INTERNATIONAL_7", 0x8D }, // Keyboard Int'l 7
{ "INTERNATIONAL_8", 0x8E }, // Keyboard Int'l 8
{ "INTERNATIONAL_9", 0x8F }, // Keyboard Int'l 9
{ "LANG_1", 0x90 }, // Keyboard Lang 1 (Hanguel/English)
{ "LANG_2", 0x91 }, // Keyboard Lang 2 (Hanja)
{ "LANG_3", 0x92 }, // Keyboard Lang 3 (Katakana)
{ "LANG_4", 0x93 }, // Keyboard Lang 4 (Hiragana)
{ "LANG_5", 0x94 }, // Keyboard Lang 5 (Zenkaku/Hankaku)
{ "LANG_6", 0x95 }, // Keyboard Lang 6
{ "LANG_7", 0x96 }, // Keyboard Lang 7
{ "LANG_8", 0x97 }, // Keyboard Lang 8
{ "LANG_9", 0x98 }, // Keyboard Lang 9
{ "ALTERNATE_ERASE", 0x99 }, // Keyboard Alternate Erase
{ "SYSREQ_ATTN", 0x9A }, // Keyboard SysReq/Attention
{ "CANCEL", 0x9B }, // Keyboard Cancel
{ "CLEAR", 0x9C }, // Keyboard Clear
{ "PRIOR", 0x9D }, // Keyboard Prior
{ "RETURN", 0x9E }, // Keyboard Return
{ "SEPARATOR", 0x9F }, // Keyboard Separator
{ "OUT", 0xA0 }, // Keyboard Out
{ "OPER", 0xA1 }, // Keyboard Oper
{ "CLEAR_AGAIN", 0xA2 }, // Keyboard Clear/Again
{ "CRSEL_PROPS", 0xA3 }, // Keyboard CrSel/Props
{ "EXSEL", 0xA4 }, // Keyboard ExSel
{ "SYSTEM_POWER", 0xA8 }, // System Power
{ "SYSTEM_SLEEP", 0xA9 }, // System Sleep
{ "SYSTEM_WAKE", 0xAA }, // System Wake
{ "AUX1", 0xAB }, // Auxiliary key 1
{ "AUX2", 0xAC }, // Auxiliary key 2
{ "AUX3", 0xAD }, // Auxiliary key 3
{ "AUX4", 0xAE }, // Auxiliary key 4
{ "AUX5", 0xAF }, // Auxiliary key 5
//{ "EXTRA_UNUSED_1", 0xB0 }, // extra
{ "EXTRA_LALT", 0xB1 }, // AT-F extra pad lhs of space
{ "EXTRA_PAD_PLUS", 0xB2 }, // Term extra pad bottom of keypad +
{ "EXTRA_RALT", 0xB3 }, // AT-F extra pad rhs of space
{ "EXTRA_EUROPE_2", 0xB4 }, // AT-F extra pad lhs of enter
{ "EXTRA_BACKSLASH", 0xB5 }, // AT-F extra pad top of enter
{ "EXTRA_INSERT", 0xB6 }, // AT-F extra pad lhs of Insert
{ "EXTRA_F1", 0xB7 }, // Term F1
{ "EXTRA_F2", 0xB8 }, // Term F2
{ "EXTRA_F3", 0xB9 }, // Term F3
{ "EXTRA_F4", 0xBA }, // Term F4
{ "EXTRA_F5", 0xBB }, // Term F5
{ "EXTRA_F6", 0xBC }, // Term F6
{ "EXTRA_F7", 0xBD }, // Term F7
{ "EXTRA_F8", 0xBE }, // Term F8
{ "EXTRA_F9", 0xBF }, // Term F9
{ "EXTRA_F10", 0xC0 }, // Term F10
//{ "EXTRA_UNUSED_2", 0xC1 }, // extra
{ "EXTRA_SYSRQ", 0xC2 }, // Sys Req (AT 84-key)
{ "FAKE_01", 0xB0 }, // extra
{ "FAKE_02", 0xB1 }, // AT-F extra pad lhs of space
{ "FAKE_03", 0xB2 }, // Term extra pad bottom of keypad +
{ "FAKE_04", 0xB3 }, // AT-F extra pad rhs of space
{ "FAKE_05", 0xB4 }, // AT-F extra pad lhs of enter
{ "FAKE_06", 0xB5 }, // AT-F extra pad top of enter
{ "FAKE_07", 0xB6 }, // AT-F extra pad lhs of Insert
{ "FAKE_08", 0xB7 }, // Term F1
{ "FAKE_09", 0xB8 }, // Term F2
{ "FAKE_10", 0xB9 }, // Term F3
{ "FAKE_11", 0xBA }, // Term F4
{ "FAKE_12", 0xBB }, // Term F5
{ "FAKE_13", 0xBC }, // Term F6
{ "FAKE_14", 0xBD }, // Term F7
{ "FAKE_15", 0xBE }, // Term F8
{ "FAKE_16", 0xBF }, // Term F9
{ "FAKE_17", 0xC0 }, // Term F10
{ "FAKE_18", 0xC1 }, // extra
{ "FAKE_19", 0xC2 }, // Sys Req (AT 84-key)
{ "FN1", 0xD0 }, // Function layer key 1
{ "FN2", 0xD1 }, // Function layer key 2
{ "FN3", 0xD2 }, // Function layer key 3
{ "FN4", 0xD3 }, // Function layer key 4
{ "FN5", 0xD4 }, // Function layer key 5
{ "FN6", 0xD5 }, // Function layer key 6
{ "FN7", 0xD6 }, // Function layer key 7
{ "FN8", 0xD7 }, // Function layer key 8
{ "SELECT_0", 0xD8 }, // Select reset
{ "SELECT_1", 0xD9 }, // Select 1
{ "SELECT_2", 0xDA }, // Select 2
{ "SELECT_3", 0xDB }, // Select 3
{ "SELECT_4", 0xDC }, // Select 4
{ "SELECT_5", 0xDD }, // Select 5
{ "SELECT_6", 0xDE }, // Select 6
{ "SELECT_7", 0xDF }, // Select 7
{ "LCTRL", 0xE0 }, // Left Control
{ "LSHIFT", 0xE1 }, // Left Shift
{ "LALT", 0xE2 }, // Left Alt
{ "LGUI", 0xE3 }, // Left GUI
{ "RCTRL", 0xE4 }, // Right Control
{ "RSHIFT", 0xE5 }, // Right Shift
{ "RALT", 0xE6 }, // Right Alt
{ "RGUI", 0xE7 }, // Right GUI
{ "MEDIA_NEXT_TRACK", 0xE8 }, // Scan Next Track
{ "MEDIA_PREV_TRACK", 0xE9 }, // Scan Previous Track
{ "MEDIA_STOP", 0xEA }, // Stop
{ "MEDIA_PLAY_PAUSE", 0xEB }, // Play/ Pause
{ "MEDIA_MUTE", 0xEC }, // Mute
{ "MEDIA_BASS_BOOST", 0xED }, // Bass Boost
{ "MEDIA_LOUDNESS", 0xEE }, // Loudness
{ "MEDIA_VOLUME_UP", 0xEF }, // Volume Up
{ "MEDIA_VOLUME_DOWN", 0xF0 }, // Volume Down
{ "MEDIA_BASS_UP", 0xF1 }, // Bass Up
{ "MEDIA_BASS_DOWN", 0xF2 }, // Bass Down
{ "MEDIA_TREBLE_UP", 0xF3 }, // Treble Up
{ "MEDIA_TREBLE_DOWN", 0xF4 }, // Treble Down
{ "MEDIA_MEDIA_SELECT", 0xF5 }, // Media Select
{ "MEDIA_MAIL", 0xF6 }, // Mail
{ "MEDIA_CALCULATOR", 0xF7 }, // Calculator
{ "MEDIA_MY_COMPUTER", 0xF8 }, // My Computer
{ "MEDIA_WWW_SEARCH", 0xF9 }, // WWW Search
{ "MEDIA_WWW_HOME", 0xFA }, // WWW Home
{ "MEDIA_WWW_BACK", 0xFB }, // WWW Back
{ "MEDIA_WWW_FORWARD", 0xFC }, // WWW Forward
{ "MEDIA_WWW_STOP", 0xFD }, // WWW Stop
{ "MEDIA_WWW_REFRESH", 0xFE }, // WWW Refresh
{ "MEDIA_WWW_FAVORITES", 0xFF }, // WWW Favorites
};
const char* lookup_hid_token(int value)
{
int n = sizeof(hid_token_list) / sizeof(token_t);
for ( int i = 0; i < n; ++i ) {
if ( hid_token_list[i].value == value ) {
return hid_token_list[i].token;
}
}
return "INVALID";
}
int lookup_hid_token(const char* s)
{
int n = sizeof(hid_token_list) / sizeof(token_t);
for ( int i = 0; i < n; ++i ) {
if ( 0 == stricmp(hid_token_list[i].token, s) ) {
return hid_token_list[i].value;
}
}
return INVALID_NUMBER;
}
int lookup_meta_token(const char* s)
{
int n = sizeof(meta_token_list) / sizeof(token_t);
for ( int i = 0; i < n; ++i ) {
if ( 0 == stricmp(meta_token_list[i].token, s) ) {
return meta_token_list[i].value;
}
}
return INVALID_NUMBER;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef __HID_TOKENS_H__
#define __HID_TOKENS_H__
int lookup_hid_token(const char* s);
const char* lookup_hid_token(int value);
int lookup_meta_token(const char* s);
inline bool is_meta_handed(int meta)
{
return !(meta & (meta >> 4));
}
#endif // __HID_TOKENS_H__
+69
View File
@@ -0,0 +1,69 @@
#include "macro_tokens.h"
#include "global.h"
#include <string.h>
token_t macro_token_list[] =
{
{ "NOP", Q_NOP }, // value = ignored
{ "PRESS", Q_KEY_PRESS }, // value = hid code
{ "MAKE", Q_KEY_MAKE }, // value = hid code
{ "BREAK", Q_KEY_RELEASE }, // value = hid code
{ "ASSIGN_META", Q_ASSIGN_META }, // value = metas
{ "SET_META", Q_SET_META }, // value = metas
{ "CLEAR_META", Q_CLEAR_META }, // value = metas
{ "TOGGLE_META", Q_TOGGLE_META }, // value = metas
{ "POP_META", Q_POP_META }, // value = ignored
{ "POP_ALL_META", Q_POP_ALL_META }, // value = ignored
{ "DELAY", Q_DELAY_MS }, // value = delay count
{ "CLEAR_ALL", Q_CLEAR_ALL }, // value = ignored // internal use
{ "BOOT", Q_BOOT }, // value = ignored
{ "PUSH_META", Q_PUSH_META }, // can be combined with any other command. value = value for other command
};
const char* lookup_macro_token(int value)
{
int n = sizeof(macro_token_list) / sizeof(token_t);
for ( int i = 0; i < n; ++i ) {
if ( macro_token_list[i].value == value ) {
return macro_token_list[i].token;
}
}
return "INVALID";
}
int lookup_macro_token(const char* s)
{
int n = sizeof(macro_token_list) / sizeof(token_t);
for ( int i = 0; i < n; ++i ) {
if ( 0 == stricmp(macro_token_list[i].token, s) ) {
return macro_token_list[i].value;
}
}
return INVALID_NUMBER;
}
int get_macro_arg_type(int cmd)
{
switch ( cmd & ~Q_PUSH_META ) {
case Q_KEY_PRESS:
case Q_KEY_MAKE:
case Q_KEY_RELEASE:
return MACRO_ARG_HID;
case Q_ASSIGN_META:
case Q_SET_META:
case Q_CLEAR_META:
case Q_TOGGLE_META:
return MACRO_ARG_META;
case Q_DELAY_MS:
return MACRO_ARG_DELAY;
case Q_NOP:
case Q_POP_META:
case Q_POP_ALL_META:
case Q_CLEAR_ALL:
case Q_BOOT:
return MACRO_ARG_NONE;
default:
return INVALID_NUMBER;
}
}
+30
View File
@@ -0,0 +1,30 @@
#ifndef __MACRO_TOKENS_H__
#define __MACRO_TOKENS_H__
enum queue_command_t {
Q_NOP = 0, // value = ignored
Q_KEY_PRESS = 1, // value = hid code
Q_KEY_MAKE = 2, // value = hid code
Q_KEY_RELEASE = 3, // value = hid code
Q_ASSIGN_META = 4, // value = metas
Q_SET_META = 5, // value = metas
Q_CLEAR_META = 6, // value = metas
Q_TOGGLE_META = 7, // value = metas
Q_POP_META = 8, // value = ignored
Q_POP_ALL_META = 9, // value = ignored
Q_DELAY_MS = 10, // value = delay count
Q_CLEAR_ALL = 11, // value = ignored
Q_BOOT = 12, // value = ignored
Q_PUSH_META = 0x80, // can be or'ed with any other command
};
#define MACRO_ARG_NONE 0
#define MACRO_ARG_HID 1
#define MACRO_ARG_META 2
#define MACRO_ARG_DELAY 3
const char* lookup_macro_token(int value);
int lookup_macro_token(const char* s);
int get_macro_arg_type(int cmd);
#endif // __MACRO_TOKENS_H__
+62
View File
@@ -0,0 +1,62 @@
#ifndef __RAWHID_H__
#define __RAWHID_H__
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _MSC_VER
#define RAWHIDFN __stdcall
#else
#define RAWHIDFN
#endif
// rawhid_open - open 1 or more devices
//
// Inputs:
// max = maximum number of devices to open
// vid = Vendor ID, or -1 if any
// pid = Product ID, or -1 if any
// usage_page = top level usage page, or -1 if any
// usage = top level usage number, or -1 if any
// Output:
// actual number of devices opened
//
int RAWHIDFN rawhid_open(int max, int vid, int pid, int usage_page, int usage);
// rawhid_recv - receive a packet
// Inputs:
// num = device to receive from (zero based)
// buf = buffer to receive packet
// len = buffer's size
// timeout = time to wait, in milliseconds
// Output:
// number of bytes received, or -1 on error
//
int RAWHIDFN rawhid_recv(int num, void *buf, int len, int timeout);
// rawhid_send - send a packet
// Inputs:
// num = device to transmit to (zero based)
// buf = buffer containing packet to send
// len = number of bytes to transmit
// timeout = time to wait, in milliseconds
// Output:
// number of bytes sent, or -1 on error
//
int RAWHIDFN rawhid_send(int num, void *buf, int len, int timeout);
// rawhid_close - close a device
//
// Inputs:
// num = device to close (zero based)
// Output
// (nothing)
//
void RAWHIDFN rawhid_close(int num);
#ifdef __cplusplus
};
#endif
#endif // __RAWHID_H__
+50
View File
@@ -0,0 +1,50 @@
#ifndef __RAWHID_DEFS_H__
#define __RAWHID_DEFS_H__
// enum settings_state_t {
// SS_IDLE,
// SS_WRITE_INIT,
// SS_WRITE,
// SS_WRITING,
// SS_WRITTEN,
// SS_READ,
// };
enum request_code_t {
RQ_INFO = 1,
RQ_WRITE = 2,
RQ_READ = 3,
RQ_BOOT = 4,
RQ_CONTINUATION = 0x80
};
enum response_code_t {
RC_ERROR = 1,
RC_OK = 2,
RC_READY = 3,
RC_COMPLETED = 4,
};
enum info_code_t {
IC_END = 0,
IC_CODE_VERSION = 1,
IC_CONFIG_MAX_VERSION = 2,
IC_PROTOCOL_VERSION = 3,
IC_CONFIG_VERSION = 4,
IC_RAM_SIZE = 5,
IC_EEPROM_SIZE = 6,
IC_RAM_FREE = 7,
IC_EEPROM_FREE = 8,
};
#define PACKET_LEN 64
#define SC_VID 0x16C0
#define SC_PID 0x047D
#define SC_USAGE_PAGE 0xFF99
#define SC_USAGE 0x2468
#define PROTOCOL_VERSION_MAJOR 1
#define PROTOCOL_VERSION_MINOR 0
#endif // __RAWHID_DEFS_H__