From 582fbcb74263b032cbc047e92cbc7415508d621c Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 2 Jan 2007 05:37:07 +0000 Subject: I cleaned up the return values. This is what I used: * EXIT_SUCCESS for successful conditions * -1 for failure when a device number is being returned * EXIT_FAILURE for all other failures EXIT_SUCCESS and EXIT_FAILURE are defined in stdlib.h svn path=/trunk/externals/io/hidio/; revision=7121 --- hidio.c | 43 ++++++++++++++++++++----------------------- hidio.h | 39 +++++++++++++++++++++------------------ hidio_darwin.c | 52 +++++++++++++++++++++++----------------------------- hidio_linux.c | 34 ++++++++++++++++------------------ hidio_windows.c | 48 ++++++++++++++++-------------------------------- 5 files changed, 96 insertions(+), 120 deletions(-) diff --git a/hidio.c b/hidio.c index 1ffad69..8503220 100644 --- a/hidio.c +++ b/hidio.c @@ -86,7 +86,7 @@ t_symbol *relative_symbols[RELATIVE_ARRAY_MAX]; //static void hidio_poll(t_hidio *x, t_float f); static void hidio_open(t_hidio *x, t_symbol *s, int argc, t_atom *argv); -//static t_int hidio_close(t_hidio *x); +//static void hidio_close(t_hidio *x); //static void hidio_float(t_hidio* x, t_floatarg f); @@ -129,7 +129,7 @@ static void output_status(t_hidio *x, t_symbol *selector, t_float output_value) t_atom *output_atom = (t_atom *)getbytes(sizeof(t_atom)); #ifdef PD SETFLOAT(output_atom, output_value); -#else +#else /* Max */ atom_setlong(output_atom, (long)output_value); #endif /* PD */ outlet_anything( x->x_status_outlet, selector, 1, output_atom); @@ -184,14 +184,14 @@ static void output_element_ranges(t_hidio *x) static unsigned int name_to_usage(char *usage_name) { // output usagepage << 16 + usage - if(strcmp(usage_name,"pointer") == 0) return(0x00010001); - if(strcmp(usage_name,"mouse") == 0) return(0x00010002); - if(strcmp(usage_name,"joystick") == 0) return(0x00010004); - if(strcmp(usage_name,"gamepad") == 0) return(0x00010005); - if(strcmp(usage_name,"keyboard") == 0) return(0x00010006); - if(strcmp(usage_name,"keypad") == 0) return(0x00010007); - if(strcmp(usage_name,"multiaxiscontroller") == 0) return(0x00010008); - return(0); + if(strcmp(usage_name,"pointer") == 0) return 0x00010001; + if(strcmp(usage_name,"mouse") == 0) return 0x00010002; + if(strcmp(usage_name,"joystick") == 0) return 0x00010004; + if(strcmp(usage_name,"gamepad") == 0) return 0x00010005; + if(strcmp(usage_name,"keyboard") == 0) return 0x00010006; + if(strcmp(usage_name,"keypad") == 0) return 0x00010007; + if(strcmp(usage_name,"multiaxiscontroller") == 0) return 0x00010008; + return 0; } @@ -282,7 +282,7 @@ static short get_device_number_from_arguments(int argc, t_atom *argv) device_number = get_device_number_by_id(vendor_id,product_id); } } - return(device_number); + return device_number; } @@ -372,7 +372,7 @@ static void hidio_set_from_float(t_hidio *x, t_floatarg f) } /* close the device */ -t_int hidio_close(t_hidio *x) +static void hidio_close(t_hidio *x) { debug_post(LOG_DEBUG,"hidio_close"); @@ -383,10 +383,7 @@ t_int hidio_close(t_hidio *x) { debug_post(LOG_INFO,"[hidio] closed device %d",x->x_device_number); x->x_device_open = 0; - return (0); } - - return (1); } @@ -414,12 +411,7 @@ static void hidio_open(t_hidio *x, t_symbol *s, int argc, t_atom *argv) /* no device open, so open one now */ if (!x->x_device_open) { - if(hidio_open_device(x, new_device_number)) - { - x->x_device_number = -1; - error("[hidio] can not open device %d",new_device_number); - } - else + if(hidio_open_device(x, new_device_number) == EXIT_SUCCESS) { x->x_device_open = 1; x->x_device_number = new_device_number; @@ -431,6 +423,11 @@ static void hidio_open(t_hidio *x, t_symbol *s, int argc, t_atom *argv) debug_post(LOG_DEBUG,"[hidio] set device# to %d",new_device_number); output_device_number(x); } + else + { + x->x_device_number = -1; + error("[hidio] can not open device %d",new_device_number); + } } } else debug_post(LOG_WARNING,"[hidio] device does not exist"); @@ -566,7 +563,7 @@ static void *hidio_new(t_symbol *s, int argc, t_atom *argv) x->x_instance = hidio_instance_count; hidio_instance_count++; - return (x); + return x; } #ifdef PD @@ -725,7 +722,7 @@ int main() generate_type_symbols(); generate_event_symbols(); - return 0; + return EXIT_SUCCESS; } #endif /* PD */ diff --git a/hidio.h b/hidio.h index ab7ebc8..5087a94 100644 --- a/hidio.h +++ b/hidio.h @@ -42,7 +42,7 @@ typedef void t_clock; #define HIDIO_MAJOR_VERSION 0 #define HIDIO_MINOR_VERSION 0 -/* static char *version = "$Revision: 1.14 $"; */ +/* static char *version = "$Revision: 1.15 $"; */ /*------------------------------------------------------------------------------ * MACRO DEFINES @@ -70,19 +70,6 @@ typedef void t_clock; * kDeviceQueueSize */ #define MAX_EVENTS_PER_POLL 50 -/*------------------------------------------------------------------------------ - * THREADING RELATED DEFINES - */ - -#define REQUEST_NOTHING 0 -#define REQUEST_OPEN 1 -#define REQUEST_READ 2 -#define REQUEST_SEND 3 -#define REQUEST_PRINT 4 -#define REQUEST_INFO 5 -#define REQUEST_CLOSE 6 -#define REQUEST_QUIT 7 - /*------------------------------------------------------------------------------ * CLASS DEF @@ -91,7 +78,7 @@ typedef struct _hidio { t_object x_obj; -#ifndef PD +#ifndef PD /* Max */ void *x_obex; #endif #ifdef _WIN32 @@ -146,7 +133,7 @@ typedef struct _hid_element #endif /* __APPLE__ */ t_symbol *type; // Linux "type"; HID "usagePage", but using the hidio scheme t_symbol *name; // Linux "code"; HID "usage", but using the hidio scheme - unsigned char polled; // is it polled or queued? (maybe only on Mac OS X?) + unsigned char polled; // is it polled or queued? unsigned char relative; // relative data gets output everytime t_int min; // from device report t_int max; // from device report @@ -164,6 +151,22 @@ extern t_hid_element *element[MAX_DEVICES][MAX_ELEMENTS]; /* number of active elements per device */ extern unsigned short element_count[MAX_DEVICES]; + +/* Each instance registers itself with a hidio_instances[] linked list when it + * opens a device. Whichever instance gets the events from the OS will then + * go thru this linked list and call its output function. */ + +/* basic element of a linked list of hidio instances */ +typedef struct _hidio_instance +{ + t_hidio *x; + struct _hidio_instance *x_next; +} t_hidio_instance; + +/* array of linked lists of instances wanting events from a given device. */ +extern t_hidio_instance *hidio_instances[MAX_DEVICES]; + + /*------------------------------------------------------------------------------ * FUNCTION PROTOTYPES FOR DIFFERENT PLATFORMS */ @@ -191,8 +194,8 @@ extern short get_device_number_from_usage(short device_number, unsigned short usage); /* cross-platform force feedback functions */ -extern t_int hidio_ff_autocenter(t_hidio *x, t_float value); -extern t_int hidio_ff_gain(t_hidio *x, t_float value); +extern void hidio_ff_autocenter(t_hidio *x, t_float value); +extern void hidio_ff_gain(t_hidio *x, t_float value); extern t_int hidio_ff_motors(t_hidio *x, t_float value); extern t_int hidio_ff_continue(t_hidio *x); extern t_int hidio_ff_pause(t_hidio *x); diff --git a/hidio_darwin.c b/hidio_darwin.c index 16a9fd1..a48885e 100644 --- a/hidio_darwin.c +++ b/hidio_darwin.c @@ -64,10 +64,12 @@ pRecDevice device_pointer[MAX_DEVICES]; // temp hack for measuring latency -#define LATENCY_MAX 8192 +/*#define LATENCY_MAX 8192 int latency[LATENCY_MAX]; int latency_i; int latency_average; +//end latency hack +*/ // this stuff is moving to the t_hid_element struct @@ -281,6 +283,8 @@ static t_float get_type_name_instance(t_symbol *type, t_symbol *name, /* DARWIN-SPECIFIC SUPPORT FUNCTIONS */ /* ============================================================================== */ +/* +// temp hack for measuring latency double calculate_event_latency( uint64_t endTime, uint64_t startTime ) { uint64_t difference = endTime - startTime; @@ -298,7 +302,7 @@ double calculate_event_latency( uint64_t endTime, uint64_t startTime ) } return conversion * (double) difference; } - +//end latency hack */ short get_device_number_by_id(unsigned short vendor_id, unsigned short product_id) { @@ -379,9 +383,9 @@ short get_device_number_from_usage(short device_number, pCurrentHIDDevice = HIDGetNextDevice(pCurrentHIDDevice); } if(i < total_devices) - return(return_device_number); + return return_device_number; else - return(-1); + return -1; } @@ -676,8 +680,9 @@ void hidio_get_events(t_hidio *x) // debug_post(LOG_DEBUG,"timestamp: %u %u", event.timestamp.hi, event.timestamp.lo); timestamp = * (uint64_t *) &(event.timestamp); now = mach_absolute_time(); +/* +// temp hack for measuring latency difference = calculate_event_latency(now, timestamp); -// temp hack to measure latency if( latency_i < LATENCY_MAX) { latency[latency_i] = (int) difference; @@ -692,6 +697,8 @@ void hidio_get_events(t_hidio *x) latency_i = 0; latency_average = 0; } +// end latency hack +*/ } /* absolute axes don't need to be queued, they can just be polled */ for(i=0; i< element_count[x->x_device_number]; ++i) @@ -706,18 +713,15 @@ void hidio_get_events(t_hidio *x) } } -// TODO: return the same as POSIX open()/close() - 0=success, -1=fail t_int hidio_open_device(t_hidio *x, short device_number) { debug_post(LOG_DEBUG,"hidio_open_device"); - t_int result = 0; pRecDevice pCurrentHIDDevice = NULL; - io_service_t hidDevice = 0; FFDeviceObjectReference ffDeviceReference = NULL; - latency_i = 0;latency_average = 0; // temp hack, to be removed +// latency_i = 0;latency_average = 0; // temp hack for measuring latency /* rebuild device list to make sure the list is current */ if( !HIDHaveDeviceList() ) hidio_build_device_list(); @@ -731,7 +735,7 @@ t_int hidio_open_device(t_hidio *x, short device_number) else { debug_error(x,LOG_ERR,"[hidio]: device %d is not a valid device\n",device_number); - return(1); + return EXIT_FAILURE; } debug_post(LOG_WARNING,"[hidio] opened device %d: %s %s", device_number, pCurrentHIDDevice->manufacturer, pCurrentHIDDevice->product); @@ -751,26 +755,19 @@ t_int hidio_open_device(t_hidio *x, short device_number) { x->x_has_ff = 0; post("[hidio]: FF device creation failed!"); - return( -1 ); + return EXIT_FAILURE; } } - return(result); + return EXIT_SUCCESS; } -// TODO: return the same as POSIX open()/close() - 0=success, -1=fail + t_int hidio_close_device(t_hidio *x) { debug_post(LOG_DEBUG,"hidio_close_device"); - t_int result = 0; -// pRecDevice pCurrentHIDDevice = hidio_get_device_by_number(x->x_device_number); pRecDevice pCurrentHIDDevice = device_pointer[x->x_device_number]; - - HIDDequeueDevice(pCurrentHIDDevice); -// this doesn't seem to be needed at all, but why not use it? -// result = HIDCloseReleaseInterface(pCurrentHIDDevice); - - return(result); + return HIDDequeueDevice(pCurrentHIDDevice); } @@ -828,7 +825,8 @@ void hidio_platform_specific_free(t_hidio *x) /* for(j=0;jx_fd = -1; - return 1; + return EXIT_FAILURE; } } @@ -476,7 +476,7 @@ t_int hidio_open_device(t_hidio *x, short device_number) post("pre hidio_build_element_list"); hidio_build_element_list(x); - return (0); + return EXIT_SUCCESS; } /* Under GNU/Linux, the device is a filehandle */ @@ -484,10 +484,10 @@ t_int hidio_open_device(t_hidio *x, short device_number) t_int hidio_close_device(t_hidio *x) { debug_post(LOG_DEBUG,"hidio_close_device"); - if(x->x_fd <0) - return 0; - else + if(x->x_fd > -1) return (close(x->x_fd)); + else + return EXIT_SUCCESS; } @@ -519,12 +519,10 @@ void hidio_build_device_list(void) if(fd < 0 ) { /* post("Nothing on %s.", &block_device); */ fd = -1; -/* return 0; */ } else { /* read input_events from the LINUX_BLOCK_DEVICE stream - * It seems that is just there to flush the event input buffer? - */ - while (read (fd, &(x_input_event), sizeof(struct input_event)) > -1); + * It seems that is just there to flush the event input buffer? */ + while( read(fd, &(x_input_event),sizeof(struct input_event)) > -1 ); /* get name of device */ ioctl(fd, EVIOCGNAME(sizeof(device_name)), device_name); @@ -598,43 +596,43 @@ short get_device_number_from_usage(short device_number, /* cross-platform force feedback functions */ t_int hidio_ff_autocenter( t_hidio *x, t_float value ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_gain( t_hidio *x, t_float value ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_motors( t_hidio *x, t_float value ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_continue( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_pause( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_reset( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_stopall( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } @@ -642,7 +640,7 @@ t_int hidio_ff_stopall( t_hidio *x ) // these are just for testing... t_int hidio_ff_fftest ( t_hidio *x, t_float value) { - return ( 0 ); + return EXIT_SUCCESS; } diff --git a/hidio_windows.c b/hidio_windows.c index 45a12ef..b7a8120 100644 --- a/hidio_windows.c +++ b/hidio_windows.c @@ -59,26 +59,20 @@ extern t_int hidio_instance_count; /* WINDOWS DDK HID SPECIFIC SUPPORT FUNCTIONS */ /* ============================================================================== */ -void hidio_get_device_by_number(t_int device_number) -{ - -} - - void hidio_build_element_list(t_hidio *x) { } -t_int hidio_print_element_list(t_hidio *x) +static void hidio_print_element_list(t_hidio *x) { debug_post(LOG_DEBUG,"hidio_print_element_list"); - return (0); + return EXIT_SUCCESS; } -t_int hidio_print_device_list(t_hidio *x) +static t_int hidio_print_device_list(t_hidio *x) { struct _GUID GUID; SP_INTERFACE_DEVICE_DATA DeviceInterfaceData; @@ -165,7 +159,7 @@ t_int hidio_print_device_list(t_hidio *x) } // for (i = 0; i < 32; i++) SetupDiDestroyDeviceInfoList(PnPHandle); #endif - return 0; + return EXIT_SUCCESS; } void hidio_output_device_name(t_hidio *x, char *manufacturer, char *product) @@ -190,43 +184,43 @@ void hidio_output_device_name(t_hidio *x, char *manufacturer, char *product) /* cross-platform force feedback functions */ t_int hidio_ff_autocenter( t_hidio *x, t_float value ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_gain( t_hidio *x, t_float value ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_motors( t_hidio *x, t_float value ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_continue( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_pause( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_reset( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } t_int hidio_ff_stopall( t_hidio *x ) { - return ( 0 ); + return EXIT_SUCCESS; } @@ -234,7 +228,7 @@ t_int hidio_ff_stopall( t_hidio *x ) // these are just for testing... t_int hidio_ff_fftest ( t_hidio *x, t_float value) { - return ( 0 ); + return EXIT_SUCCESS; } @@ -246,21 +240,17 @@ void hidio_ff_print( t_hidio *x ) /* Pd [hidio] FUNCTIONS */ /* ============================================================================== */ -t_int hidio_get_events(t_hidio *x) +void hidio_get_events(t_hidio *x) { //debug_post(LOG_DEBUG,"hidio_get_events"); - - return (0); } t_int hidio_open_device(t_hidio *x, t_int device_number) { debug_post(LOG_DEBUG,"hidio_open_device"); - t_int result = 0; - - return(result); + return EXIT_SUCCESS; } @@ -268,13 +258,11 @@ t_int hidio_close_device(t_hidio *x) { debug_post(LOG_DEBUG,"hidio_close_device"); - t_int result = 0; - - return(result); + return EXIT_SUCCESS; } -t_int hidio_build_device_list(t_hidio *x) +void hidio_build_device_list(t_hidio *x) { debug_post(LOG_DEBUG,"hidio_build_device_list"); @@ -292,7 +280,6 @@ t_int hidio_build_device_list(t_hidio *x) if( !hidDllPathLength ) { error("[hidio] ERROR: cannot get SystemRoot"); - return 0; } strcat(hidDllPath,"\\hid.dll"); post("hidDllPath: %s",hidDllPath); @@ -300,10 +287,7 @@ t_int hidio_build_device_list(t_hidio *x) if ( !hModule ) { error("[hidio] ERROR: couldn't load %s: error %d",hidDllPath,GetLastError()); - return 0; } - - return 1; } -- cgit v1.2.1