Update of /cvsroot/pure-data/externals/hcs/hid
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19526
Added Files:
Makefile hid.c make-arrays-from-input.h.pl
Log Message:
got the input event array going
--- NEW FILE: Makefile ---
CC=gcc
all: input_arrays pd_darwin
pd_darwin: hid.pd_darwin macosxhid.pd_darwin
clean: ; rm -f *.pd_darwin *.o *~ input_arrays.h
# ----------------------- DARWIN i386 -----------------------
.SUFFIXES: .pd_darwin
PDEXECUTABLE = ../../../pd/bin/pd
CFLAGS = -DUNIX -DPD -O2 -funroll-loops -fomit-frame-pointer \
-Wall -W -Wshadow -Wstrict-prototypes -Werror \
-Wno-unused -Wno-parentheses -Wno-switch
INCLUDE = -I../ -I../../../pd/src -I/usr/local/include -I./HID\ Utilities\ Source
LDFLAGS = -bundle -bundle_loader $(PDEXECUTABLE) -L/sw/lib
.c.pd_darwin:
$(CC) $(CFLAGS) $(INCLUDE) -o $*.o -c $*.c
$(CC) $(LDFLAGS) -o "$*.pd_darwin" "$*.o" -lc -lm
input_arrays:
./make-arrays-from-input.h.pl > input_arrays.h
--- NEW FILE: make-arrays-from-input.h.pl ---
#!/usr/bin/perl -w
use Switch;
# BUGS: FF_STATUS, REP, SND, SYN are not building properly. Its still dropping the first element
#========================================================================
# GLOBAL VARS
#========================================================================
$FILENAME = "linux/input.h";
#========================================================================
# FUNCTIONS
#========================================================================
#------------------------------------------------------------------------
# parse out the types and codes from a line from the header
# usage: @dataArray getDataFromHeaderLine($lineFromHeader);
sub getDataFromHeaderLine
{
$_ = shift;
my @returnArray;
if ( m/#define ([A-Z0-9_]*)\s+(0x)?([0-9a-f]+)/)
{
if ($2) { $index = hex($3); } else { $index = $3; }
if ($index >=0)
{
$returnArray[0] = $index;
$returnArray[1] = "$1";
return @returnArray;
}
# print "$1 \t\t\t $index $#returnArray\n ";
# if ($index == 0) { print("index: $index 3: $3 2: $2 1: $1 value: $returnArray[1]\n"); }
}
}
#------------------------------------------------------------------------
# print an array out in C format
#
sub printCArray
{
my @arrayToPrint = @_;
# print("$arrayToPrint[0] $#arrayToPrint \n");
print("int ${arrayToPrint[0]}_TOTAL = $#arrayToPrint; /* # of elements in array */\n");
print("char *${arrayToPrint[0]}[$#arrayToPrint] = {");
for($i = 1; $i < $#arrayToPrint; $i++)
{
# format nicely in sets of 6
if ( ($i+4)%6 == 5 ) { print("\n "); }
# if the array element's data is null, print NULL
if ($arrayToPrint[$i]) { print("\"$arrayToPrint[$i]\","); }
else { print("NULL,"); }
}
print("\"$arrayToPrint[$#arrayToPrint]\"\n };\n\n\n");
}
#========================================================================
# MAIN
#========================================================================
$FILENAME = "linux/input.h";
open(INPUT_H, "<$FILENAME");
while(<INPUT_H>)
{
if (m/#define (FF_STATUS|[A-Z_]*?)_/)
{
# filter EV_VERSION and *_MAX
m/#define\s+(EV_VERSION|[A-Z_]+_MAX)\s+/;
# print "$1 \n";
switch ($1)
{
# types
case "EV" { ($index, $value) = getDataFromHeaderLine($_); $EV[$index] = $value; }
# codes
case "SYN" { ($index, $value) = getDataFromHeaderLine($_); $SYN[$index] = $value; }
case "KEY" { ($index, $value) = getDataFromHeaderLine($_); $KEY[$index] = $value; }
# BTN codes are actually part of the KEY type
case "BTN" { ($index, $value) = getDataFromHeaderLine($_); $KEY[$index] = $value; }
case "REL" { ($index, $value) = getDataFromHeaderLine($_); $REL[$index] = $value; }
case "ABS" { ($index, $value) = getDataFromHeaderLine($_); $ABS[$index] = $value; }
case "MSC" { ($index, $value) = getDataFromHeaderLine($_); $MSC[$index] = $value; }
case "LED" { ($index, $value) = getDataFromHeaderLine($_); $LED[$index] = $value; }
case "SND" { ($index, $value) = getDataFromHeaderLine($_); $SND[$index] = $value; }
case "REP" { ($index, $value) = getDataFromHeaderLine($_); $REP[$index] = $value; }
case "FF" { ($index, $value) = getDataFromHeaderLine($_); $FF[$index] = $value; }
# there doesn't seem to be any PWR events yet...
case "PWR" { ($index, $value) = getDataFromHeaderLine($_); $PWR[$index] = $value; }
case "FF_STATUS" { ($index, $value) = getDataFromHeaderLine($_); $FF_STATUS[$index] = $value; }
# else { print " none $_"; }
}
}
}
printCArray("EV",@EV);
printCArray("SYN",@SYN);
printCArray("KEY",@KEY);
printCArray("REL",@REL);
printCArray("ABS",@ABS);
printCArray("MSC",@MSC);
printCArray("LED",@LED);
printCArray("SND",@SND);
printCArray("REP",@REP);
printCArray("FF",@FF);
printCArray("PWR",@PWR);
printCArray("FF_STATUS",@FF_STATUS);
# print array of arrays
print("char **EVENTNAMES[",$#EV+1,"] = {");
for($i = 0; $i < $#EV; $i++)
{
# format nicely in sets of 6
if ( ($i+4)%6 == 5 ) { print("\n "); }
# if the array element's data is null, print NULL
if ($EV[$i])
{
$_ = $EV[$i];
m/EV_([A-Z_]+)/;
print("$1,");
}
else { print("NULL,"); }
}
$_ = $EV[$#EV];
m/EV_([A-Z_]+)/;
print("$1\n };\n");
# print "EV: $#EV \n";
# print "SYN: $#SYN \n";
# print "KEY: $#KEY \n";
# print "REL: $#REL \n";
# print "ABS: $#ABS \n";
# print "MSC: $#MSC \n";
# print "LED: $#LED \n";
# print "SND: $#SND \n";
# print "REP: $#REP \n";
# print "FF: $#FF \n";
# #print "PWR: $#PWR \n";
# print "FF_STATUS: $#FF_STATUS \n";
close(INPUT_H);
--- NEW FILE: hid.c ---
/* ---------------------------------------------------------------------------- */
/* */
/* MacOS X object to use HIDs (Human Interface Devices */
/* Written by Hans-Christoph Steiner <hans(a)at.or.at> */
/* */
/* Copyright (c) 2004 Hans-Christoph Steiner */
/* */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation; either version 2 */
/* of the License, or (at your option) any later version. */
/* */
/* See file LICENSE for further informations on licensing terms. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* */
/* Based on PureData by Miller Puckette and others. */
/* */
/* ---------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#ifdef __APPLE__
#include <sys/errno.h>
#include <sysexits.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/hid/IOHIDUsageTables.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
#ifndef __APPLE_CC__
#include "Carbon_Include.h"
#endif
#include <HID_Utilities.h>
#endif
#include "linuxhid.h"
#include "input_arrays.h"
static char *version = "$Revision: 1.1 $";
/*------------------------------------------------------------------------------
* CLASS DEF
*/
static t_class *hid_class;
typedef struct _hid {
t_object x_obj;
t_int x_fd;
t_symbol *x_devname;
t_clock *x_clock;
int x_read_ok;
int x_started;
int x_delay;
#ifdef __gnu_linux__
struct input_event x_input_event;
#elif defined (__APPLE__)
IOHIDEventStruct event;
#endif
t_outlet *x_input_event_time_outlet;
t_outlet *x_input_event_type_outlet;
t_outlet *x_input_event_code_outlet;
t_outlet *x_input_event_value_outlet;
}t_hid;
/*------------------------------------------------------------------------------
* IMPLEMENTATION
*/
void hid_stop(t_hid* x) {
DEBUG(post("hid_stop"););
if (x->x_fd >= 0 && x->x_started) {
clock_unset(x->x_clock);
post("hid: polling stopped");
x->x_started = 0;
}
}
static int hid_close(t_hid *x) {
DEBUG(post("hid_close"););
/* just to be safe, stop it first */
hid_stop(x);
if (x->x_fd <0) return 0;
close (x->x_fd);
post ("[hid] closed %s",x->x_devname->s_name);
return 1;
}
static int hid_open(t_hid *x, t_symbol *s) {
int eventType, eventCode, buttons, rel_axes, abs_axes, ff;
#ifdef __gnu_linux__
unsigned long bitmask[EV_MAX][NBITS(KEY_MAX)];
#endif
char devicename[256] = "Unknown";
DEBUG(post("hid_open");)
hid_close(x);
/* set obj device name to parameter
* otherwise set to default
*/
if (s != &s_)
x->x_devname = s;
#ifdef __gnu_linux__
/* open device */
if (x->x_devname) {
/* open the device read-only, non-exclusive */
x->x_fd = open (x->x_devname->s_name, O_RDONLY | O_NONBLOCK);
/* test if device open */
if (x->x_fd < 0 ) {
post("[hid] open %s failed",x->x_devname->s_name);
x->x_fd = -1;
return 0;
}
} else return 1;
/* read input_events from the HID_DEVICE stream
* It seems that is just there to flush the event input buffer?
*/
while (read (x->x_fd, &(x->x_input_event), sizeof(struct input_event)) > -1);
/* get name of device */
ioctl(x->x_fd, EVIOCGNAME(sizeof(devicename)), devicename);
post ("Configuring %s on %s",devicename,x->x_devname->s_name);
/* get bitmask representing supported events (axes, buttons, etc.) */
memset(bitmask, 0, sizeof(bitmask));
ioctl(x->x_fd, EVIOCGBIT(0, EV_MAX), bitmask[0]);
post("\nSupported events:");
rel_axes = 0;
abs_axes = 0;
buttons = 0;
ff = 0;
/* cycle through all possible event types */
for (eventType = 0; eventType < EV_MAX; eventType++) {
if (test_bit(eventType, bitmask[0])) {
post(" %s (type %d) ", events[eventType] ? events[eventType] : "?", eventType);
// post("Event type %d",eventType);
/* get bitmask representing supported button types */
ioctl(x->x_fd, EVIOCGBIT(eventType, KEY_MAX), bitmask[eventType]);
/* cycle through all possible event codes (axes, keys, etc.)
* testing to see which are supported
*/
for (eventCode = 0; eventCode < KEY_MAX; eventCode++)
if (test_bit(eventCode, bitmask[eventType])) {
post(" Event code %d (%s)", eventCode, names[eventType] ? (names[eventType][eventCode] ? names[eventType][eventCode] : "?") : "?");
switch(eventType) {
// the API changed at some point...
#ifdef EV_RST
case EV_RST:
break;
#else
case EV_SYN:
break;
#endif
case EV_KEY:
buttons++;
break;
case EV_REL:
rel_axes++;
break;
case EV_ABS:
abs_axes++;
break;
case EV_MSC:
break;
case EV_LED:
break;
case EV_SND:
break;
case EV_REP:
break;
case EV_FF:
ff++;
break;
}
}
}
}
post ("\nUsing %d relative axes, %d absolute axes, and %d buttons.", rel_axes, abs_axes, buttons);
if (ff > 0) post ("Detected %d force feedback types",ff);
post ("\nWARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING");
post ("This object is under development! The interface could change at anytime!");
post ("As I write cross-platform versions, the interface might have to change.");
post ("WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING\n");
#endif
return 1;
}
static int hid_read(t_hid *x,int fd)
{
if (x->x_fd < 0) return 0;
#ifdef __gnu_linux__
while (read (x->x_fd, &(x->x_input_event), sizeof(struct input_event)) > -1) {
outlet_float (x->x_input_event_value_outlet, (int)x->x_input_event.value);
outlet_float (x->x_input_event_code_outlet, x->x_input_event.code);
outlet_float (x->x_input_event_type_outlet, x->x_input_event.type);
/* input_event.time is a timeval struct from <sys/time.h> */
/* outlet_float (x->x_input_event_time_outlet, x->x_input_event.time); */
}
#else defined (__APPLE__)
pRecDevice pCurrentHIDDevice = GetSetCurrentDevice (gWindow);
pRecElement pCurrentHIDElement = GetSetCurrentElement (gWindow);
// if we have a good device and element which is not a collecion
if (pCurrentHIDDevice && pCurrentHIDElement && (pCurrentHIDElement->type != kIOHIDElementTypeCollection))
{
SInt32 value = HIDGetElementValue (pCurrentHIDDevice, pCurrentHIDElement);
SInt32 valueCal = HIDCalibrateValue (value, pCurrentHIDElement);
SInt32 valueScale = HIDScaleValue (valueCal, pCurrentHIDElement);
}
#endif
if (x->x_started)
{
clock_delay(x->x_clock, x->x_delay);
}
return 1;
}
/* Actions */
static void hid_float(t_hid* x) {
DEBUG(post("hid_float");)
}
void hid_delay(t_hid* x, t_float f) {
DEBUG(post("hid_DELAY %f",f);)
/* if the user sets the delay less than zero, reset to default */
if ( f > 0 ) {
x->x_delay = (int)f;
} else {
x->x_delay = DEFAULT_DELAY;
}
}
void hid_start(t_hid* x) {
DEBUG(post("hid_start"););
if (x->x_fd >= 0 && !x->x_started) {
clock_delay(x->x_clock, DEFAULT_DELAY);
post("hid: polling started");
x->x_started = 1;
} else {
post("You need to set a input device (i.e /dev/input/event0)");
}
}
/* setup functions */
static void hid_free(t_hid* x) {
DEBUG(post("hid_free");)
if (x->x_fd < 0) return;
hid_stop(x);
clock_free(x->x_clock);
close (x->x_fd);
}
static void *hid_new(t_symbol *s) {
int i;
t_hid *x = (t_hid *)pd_new(hid_class);
DEBUG(post("hid_new");)
post("[hid] %s, written by Hans-Christoph Steiner <hans(a)eds.org>",version);
#ifndef __linux__
post(" !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !!");
post(" This is a dummy, since this object only works with a Linux kernel!");
post(" !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !!");
#endif
/* init vars */
x->x_fd = -1;
x->x_read_ok = 1;
x->x_started = 0;
x->x_delay = DEFAULT_DELAY;
x->x_devname = gensym(HID_DEVICE);
x->x_clock = clock_new(x, (t_method)hid_read);
/* create outlets for each axis */
x->x_input_event_time_outlet = outlet_new(&x->x_obj, &s_float);
x->x_input_event_type_outlet = outlet_new(&x->x_obj, &s_float);
x->x_input_event_code_outlet = outlet_new(&x->x_obj, &s_float);
x->x_input_event_value_outlet = outlet_new(&x->x_obj, &s_float);
/* set to the value from the object argument, if that exists */
if (s != &s_)
x->x_devname = s;
/* Open the device and save settings */
if (!hid_open(x,s)) return x;
return (x);
}
void hid_setup(void) {
DEBUG(post("hid_setup");)
hid_class = class_new(gensym("hid"),
(t_newmethod)hid_new,
(t_method)hid_free,
sizeof(t_hid),0,A_DEFSYM,0);
/* add inlet datatype methods */
class_addfloat(hid_class,(t_method) hid_float);
class_addbang(hid_class,(t_method) hid_read);
/* add inlet message methods */
class_addmethod(hid_class,(t_method) hid_delay,gensym("delay"),A_DEFFLOAT,0);
class_addmethod(hid_class,(t_method) hid_open,gensym("open"),A_DEFSYM,0);
class_addmethod(hid_class,(t_method) hid_close,gensym("close"),0);
class_addmethod(hid_class,(t_method) hid_start,gensym("start"),0);
class_addmethod(hid_class,(t_method) hid_start,gensym("poll"),0);
class_addmethod(hid_class,(t_method) hid_stop,gensym("stop"),0);
class_addmethod(hid_class,(t_method) hid_stop,gensym("nopoll"),0);
}