aboutsummaryrefslogtreecommitdiff
path: root/hidin/hidin.c
blob: a41c563b2e0044e70190351ea99efc4bbb63eaa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/* hidin.c - read in data from USB HID device */
/* Copyright 2003 Olaf Matthes, see README for a detailed licence */

/* 
   'hidin' is used to read in data from any HID (human interface device) connected 
   to the computers USB port(s).
   However, it does not work with mice or keyboards ('could not open' error)!

   Needs the Windows Driver Development Kit (DDK) to compile!

 */

#define     REQUEST_NOTHING 0
#define     REQUEST_READ 1
#define     REQUEST_QUIT 2

#include "m_pd.h"
#define SETSYM SETSYMBOL

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "pthread.h"

#include "hidin.h"

static char *hidin_version = "hidin v1.0test2, Human Interface Device on USB, (c) 2003-2004 Olaf Matthes";

static t_class *hidin_class;

typedef struct _hidin
{
    t_object      x_obj;
	t_outlet      *x_outlet;            /* outlet for received data */
	t_outlet      *x_devout;            /* outlet for list of devices */
	long          x_interval;           /* interval in ms to read */
	long          *x_data;				/* data read from the interface */
	long          *x_prev_data;			/* data we read the time bofore */
	void	      *x_qelem;             /* qelem for outputing the results */
	long          x_device;             /* the HID device number we're connecting to */
	short         x_elements;           /* number of elements (i.e. buttons, axes...)
	
		/* HID specific */
	t_hid_device  *x_hid;               /* a struct containing all the needed stuff about the HID device */

		/* tread stuff */
    long              x_requestcode;	      /* pending request from parent to I/O thread */
    pthread_mutex_t   x_mutex;
    pthread_cond_t    x_requestcondition;
    pthread_cond_t    x_answercondition;
    pthread_t         x_thread;
} t_hidin;


/* Prototypes and short descriptions */

#ifndef PD
static void *hidin_output(t_hidin *x);
static void *hidin_doit(void *z);
static void hidin_tick(t_hidin *x);
static void hidin_clock_reset(t_hidin *x);
static void hidin_int(t_hidin *x, long l);
/* what/when to read */
static void hidin_interval(t_hidin *x, long l);	/* set time interval for reading ports */
/* direct response to user */
static void hidin_show(t_hidin *x);
static void hidin_start(t_hidin *x);
static void hidin_stop(t_hidin *x);
static void hidin_bang(t_hidin *x);
static void hidin_open(t_hidin *x, long l);
/* helper functions */
static void hidin_alloc(t_hidin *x, short elem);
/* standard Max/MSP related stuff */
static void *hidin_new(t_symbol *s, short argc, t_atom *argv);
static void hidin_free(t_hidin *x);
static void hidin_info(t_hidin *x, void *p, void *c);
static void hidin_assist(t_hidin *x, void *b, long m, long a, char *s);
void main(void);
#endif


/* -------------------  general support routines  --------------------- */

static void thread_post(t_hidin *x, char *p)
{
    post("hidin: %s", p);
}

/* ---------------------  data I/O support stuff  --------------------- */

/* output values using qelem */
static void *hidin_output(t_hidin *x)
{
	int i;
	static t_atom list[2];	/* output list format is: '(int)<port> (int)<value>' */
	long d;

	pthread_mutex_lock(&x->x_mutex);
	/* check every element for new value */
	for (i = 0; i < x->x_elements; i++)
	{
		d = x->x_data[i];
		if (d != x->x_prev_data[i])
		{
			SETFLOAT(list, (t_float)i+1);
			SETFLOAT(list+1, (t_float)d);
			outlet_list(x->x_outlet, NULL, 2, list);
			x->x_prev_data[i] = d;
		}
	}
	pthread_cond_signal(&x->x_requestcondition);	/* request previous state again */
	pthread_mutex_unlock(&x->x_mutex);
	return NULL;
}

static void hidin_parse_input(t_hidin *x)
{
	ULONG		i, j;
	PUSAGE		pUsage;
	t_hid_data	*inputData;

	unpackReport(x->x_hid->inputReportBuffer, x->x_hid->caps.InputReportByteLength, HidP_Input,
		x->x_hid->inputData, x->x_hid->inputDataLength, x->x_hid->ppd);

	inputData = x->x_hid->inputData;

    for (j = 0; j < x->x_hid->inputDataLength; j++)
    {
		if (inputData->IsButtonData)	// state of buttons changed
		{
			// post("Usage Page: 0x%x, Usages: ", inputData->UsagePage);

			// set all buttons to zero
			for (i = 0; i < inputData->ButtonData.MaxUsageLength; i++)
				x->x_data[i + x->x_hid->caps.NumberInputValueCaps] = 0;

			for (i = 0, pUsage = inputData->ButtonData.Usages;
						 i < inputData->ButtonData.MaxUsageLength;
							 i++, pUsage++) 
			{
				if (0 == *pUsage)
				{
					break; // A usage of zero is a non button.
				}
				else
				{
					x->x_data[*pUsage + x->x_hid->caps.NumberInputValueCaps - 1] = 1;
				}
			}   
		}
		else	// values changed
		{
			/* post("Usage Page: 0x%x, Usage: 0x%x, Scaled: %d Value: %d",
					  inputData->UsagePage,
					  inputData->ValueData.Usage,
					  inputData->ValueData.ScaledValue,
					  inputData->ValueData.Value); */
			// x->x_data[j - x->x_hid->caps.NumberInputButtonCaps] = inputData->ValueData.ScaledValue;
			x->x_data[j - x->x_hid->caps.NumberInputButtonCaps] = inputData->ValueData.Value;
		}
        inputData++;
    }
}

/*
 * this is the actual code that reads from the file handle to the HID device.
 * it's a second thread to avoid audio dropouts because it might take some time
 * for the read call to complete.
 */
static void *hidin_doit(void *z)
{
	t_hidin *x = (t_hidin *)z;
	int i, interval;
	long bytes;
	long ret;
	
	if (x->x_hid->event == 0)
	{
		x->x_hid->event = CreateEvent(NULL, TRUE, FALSE, "");
	}

	/* prepare overlapped structute */
	x->x_hid->overlapped.Offset     = 0; 
	x->x_hid->overlapped.OffsetHigh = 0; 
	x->x_hid->overlapped.hEvent     = x->x_hid->event; 
 
	pthread_mutex_lock(&x->x_mutex);
	while (1)
	{
		if (x->x_requestcode == REQUEST_NOTHING)
		{
			pthread_cond_signal(&x->x_answercondition);
			pthread_cond_wait(&x->x_requestcondition, &x->x_mutex);
		}
		else if (x->x_requestcode == REQUEST_READ)
		{
			interval = x->x_interval;
			if (x->x_device != -1)
			{
				pthread_mutex_unlock(&x->x_mutex);

				/* read in data from device */
				bytes = readHidOverlapped(x->x_hid);

				ret = WaitForSingleObject(x->x_hid->event, interval);
 
				pthread_mutex_lock(&x->x_mutex);
				if (ret == WAIT_OBJECT_0)	/* hey, we got signalled ! => data */
				{
					hidin_parse_input(x);	/* parse received data */
					clock_delay(x->x_qelem, 0);	/* we don't have qelems on PD ;-(  */
					/* wait for the data output to complete */
					pthread_cond_wait(&x->x_requestcondition, &x->x_mutex);
				}
				else	/* if no data, cancel read */
				{
					if (!CancelIo(x->x_hid->device) && (x->x_device != -1))
						thread_post(x, "-- error cancelling read");
				}
				if (!ResetEvent(x->x_hid->event))
					thread_post(x, "-- error resetting event");

				pthread_cond_signal(&x->x_answercondition);
			}
			else
			{
				/* if device is closed don't try to read and don't alter
				   the request code, stay in current state of operation */
				pthread_cond_signal(&x->x_answercondition);
				pthread_cond_wait(&x->x_requestcondition, &x->x_mutex);
			}
		}
		else if (x->x_requestcode == REQUEST_QUIT)
		{
			x->x_requestcode = REQUEST_NOTHING;
			pthread_cond_signal(&x->x_answercondition);
			break;
		}
		else	/* error if we get here! */
		{
			error("hidin: -- internal error, please report to <olaf.matthes@gmx.de>!");
		}
	}
	pthread_mutex_unlock(&x->x_mutex);
	return (0);
}

/*
 * set the update interval time in ms
 */
static void hidin_interval(t_hidin *x, t_floatarg l)
{
	int n = (int)l;

	pthread_mutex_lock(&x->x_mutex);
	if (n >= 2)
	{
		x->x_interval = n;
		post("hidin: >> interval: polling every %d msec", n);
	}
	else post("hidin: -- interval: wrong parameter value (minimum 2)");
	pthread_mutex_unlock(&x->x_mutex);
}

/*
 * print the actual config on the console window
 * LATER reduce to the absolute minimum, this is just for easier debugging!
 */
static void hidin_show(t_hidin *x)
{
	pthread_mutex_lock(&x->x_mutex);
	if (x->x_device == -1)
	{
		post("hidin: -- no open HID device");
	}
	else
	{
		post("hidin: ****** HID device %d ******", x->x_device);
		post("hidin: ** usage page: %X", x->x_hid->caps.UsagePage);
		post("hidin: ** input report byte length: %d", x->x_hid->caps.InputReportByteLength);
		post("hidin: ** output report byte length: %d", x->x_hid->caps.OutputReportByteLength);
		post("hidin: ** feature report byte length: %d", x->x_hid->caps.FeatureReportByteLength);
		post("hidin: ** number of link collection nodes: %d", x->x_hid->caps.NumberLinkCollectionNodes);
		post("hidin: ** number of input button caps: %d", x->x_hid->caps.NumberInputButtonCaps);
		post("hidin: ** number of inputValue caps: %d", x->x_hid->caps.NumberInputValueCaps);
		post("hidin: ** number of inputData indices: %d", x->x_hid->caps.NumberInputDataIndices);
		post("hidin: ** number of output button caps: %d", x->x_hid->caps.NumberOutputButtonCaps);
		post("hidin: ** number of output value caps: %d", x->x_hid->caps.NumberOutputValueCaps);
		post("hidin: ** number of output data indices: %d", x->x_hid->caps.NumberOutputDataIndices);
		post("hidin: ** number of feature button caps: %d", x->x_hid->caps.NumberFeatureButtonCaps);
		post("hidin: ** number of feature value caps: %d", x->x_hid->caps.NumberFeatureValueCaps);
		post("hidin: ** number of feature data indices: %d", x->x_hid->caps.NumberFeatureDataIndices);
	}
	pthread_mutex_unlock(&x->x_mutex);
}

/*
 * start / stop reading
 */
static void hidin_int(t_hidin *x, t_floatarg l)
{
	pthread_mutex_lock(&x->x_mutex);
	if (l)
	{
		x->x_requestcode = REQUEST_READ;
		pthread_cond_signal(&x->x_requestcondition);
	}
	else
	{
		x->x_requestcode = REQUEST_NOTHING;
		pthread_cond_signal(&x->x_requestcondition);
	}
	pthread_mutex_unlock(&x->x_mutex);
}

	/* start reading */
static void hidin_start(t_hidin *x)
{
	pthread_mutex_lock(&x->x_mutex);
	x->x_requestcode = REQUEST_READ;
	pthread_cond_signal(&x->x_requestcondition);
	pthread_mutex_unlock(&x->x_mutex);
}

	/* stop reading */
static void hidin_stop(t_hidin *x)
{
	pthread_mutex_lock(&x->x_mutex);
	x->x_requestcode = REQUEST_NOTHING;
	pthread_cond_signal(&x->x_requestcondition);
	pthread_mutex_unlock(&x->x_mutex);
}

	/* get list of devices */
static void hidin_bang(t_hidin *x)
{
	t_atom list[2];
	int numdev, i;

	pthread_mutex_lock(&x->x_mutex);
	// check for connected devices
	numdev = findHidDevices();
	post("hidin: ** found %d devices on your system", numdev);

	SETFLOAT(list, -1);
	SETSYM(list+1, gensym("None"));
	outlet_list(x->x_devout, NULL, 2, list);
	for (i = 0; i < numdev; i++)
	{
		SETFLOAT(list, i + 1);
		SETSYM(list+1, findDeviceName(i));
		outlet_list(x->x_devout, NULL, 2, list);
	}
	pthread_mutex_unlock(&x->x_mutex);
}

/*
 * allocate memory for output data (according to number of elements)
 */
static void hidin_alloc(t_hidin *x, short elem)
{
	int i;

	if (x->x_device != -1)
	{
		// free memory in case we already have some
		if (x->x_data && x->x_elements > 0)
		{
			freebytes(x->x_data, (short)(x->x_elements * sizeof(long)));
		}
		if (x->x_prev_data && x->x_elements > 0)
		{
			freebytes(x->x_prev_data, (short)(x->x_elements * sizeof(long)));
		}

		if (elem > 0)
		{
			// allocate memory to new size
			x->x_data = (long *)getbytes((short)(elem * sizeof(long)));
			x->x_prev_data = (long *)getbytes((short)(elem * sizeof(long)));
			if (!x->x_data || !x->x_prev_data)
			{
				post("hidin: -- out of memory");
				x->x_elements = 0;
				return;
			}
			// set newly allocated memory to zero
			for (i = 0; i < elem; i++)
			{
				x->x_data[i] = 0;
				x->x_prev_data[i] = 0;
			}
		}
		x->x_elements = elem;
	}
}

/*
 * init the device data structures
 */
static void hidin_initdevice(t_hidin *x)
{
	int i;
	short numElements;     
	short numValues;     
    short numCaps;
    PHIDP_BUTTON_CAPS   buttonCaps;
    PHIDP_VALUE_CAPS    valueCaps;
    USAGE               usage;
	t_hid_data			*data;

	// get device info and show some printout
	getDeviceInfo(x->x_hid->device);
	
	// read in device's capabilities and allocate memory accordingly
	getDeviceCapabilities(x->x_hid);

	// allocate memory for input field
	x->x_hid->inputReportBuffer = (char*)getbytes((short)(x->x_hid->caps.InputReportByteLength*sizeof(char)));

	// allocate memory for input info
	x->x_hid->inputButtonCaps = buttonCaps = (PHIDP_BUTTON_CAPS)getbytes((short)(x->x_hid->caps.NumberInputButtonCaps * sizeof(HIDP_BUTTON_CAPS)));
	x->x_hid->inputValueCaps = valueCaps = (PHIDP_VALUE_CAPS)getbytes((short)(x->x_hid->caps.NumberInputValueCaps * sizeof(HIDP_VALUE_CAPS)));

    //
    // Have the HidP_X functions fill in the capability structure arrays.
    //

    numCaps = x->x_hid->caps.NumberInputButtonCaps;

    HidP_GetButtonCaps (HidP_Input,
                        buttonCaps,
                        &numCaps,
                        x->x_hid->ppd);

    numCaps = x->x_hid->caps.NumberInputValueCaps;

    HidP_GetValueCaps (HidP_Input,
                       valueCaps,
                       &numCaps,
                       x->x_hid->ppd);
	//
	// Depending on the device, some value caps structures may represent more
	// than one value.  (A range).  In the interest of being verbose, over
	// efficient, we will expand these so that we have one and only one
	// struct _HID_DATA for each value.
	//
	// To do this we need to count up the total number of values are listed
	// in the value caps structure.  For each element in the array we test
	// for range if it is a range then UsageMax and UsageMin describe the
	// usages for this range INCLUSIVE.
	//

	numValues = 0;
	for (i = 0; i < x->x_hid->caps.NumberInputValueCaps; i++, valueCaps++) 
	{
		if (valueCaps->IsRange) 
		{
			numValues += valueCaps->Range.UsageMax - valueCaps->Range.UsageMin + 1;
		}
		else
		{
			numValues++;
		}
	}
	valueCaps = x->x_hid->inputValueCaps;

	numElements = x->x_hid->caps.NumberInputValueCaps + 
					x->x_hid->caps.NumberInputButtonCaps * x->x_hid->caps.NumberInputDataIndices;

	post("hidin: >> device has %i input elements", numElements);
	hidin_alloc(x, (short)(numElements));	// allocate memory for all these elements

	x->x_hid->inputDataLength = numValues + x->x_hid->caps.NumberInputButtonCaps;

    x->x_hid->inputData = data = (t_hid_data *)getbytes((short)(x->x_hid->inputDataLength * sizeof(t_hid_data)));
	
    //
    // Fill in the button data
    //

    for (i = 0;
         i < x->x_hid->caps.NumberInputButtonCaps;
         i++, data++, buttonCaps++) 
    {
        data->IsButtonData = TRUE;
        data->Status = HIDP_STATUS_SUCCESS;
        data->UsagePage = buttonCaps->UsagePage;
        if (buttonCaps->IsRange) 
        {
            data->ButtonData.UsageMin = buttonCaps -> Range.UsageMin;
            data->ButtonData.UsageMax = buttonCaps -> Range.UsageMax;
        }
        else
        {
            data -> ButtonData.UsageMin = data -> ButtonData.UsageMax = buttonCaps -> NotRange.Usage;
        }
        
        data->ButtonData.MaxUsageLength = HidP_MaxUsageListLength(
                                                HidP_Input,
                                                buttonCaps->UsagePage,
                                                x->x_hid->ppd);
        data->ButtonData.Usages = (PUSAGE)
            calloc (data->ButtonData.MaxUsageLength, sizeof (USAGE));

        data->ReportID = buttonCaps -> ReportID;
    }

    //
    // Fill in the value data
    //

    for (i = 0; i < numValues; i++, valueCaps++)
    {
        if (valueCaps->IsRange) 
        {
            for (usage = valueCaps->Range.UsageMin;
                 usage <= valueCaps->Range.UsageMax;
                 usage++) 
            {
                data->IsButtonData = FALSE;
                data->Status = HIDP_STATUS_SUCCESS;
                data->UsagePage = valueCaps->UsagePage;
                data->ValueData.Usage = usage;
                data->ReportID = valueCaps->ReportID;
                data++;
            }
        } 
        else
        {
            data->IsButtonData = FALSE;
            data->Status = HIDP_STATUS_SUCCESS;
            data->UsagePage = valueCaps->UsagePage;
            data->ValueData.Usage = valueCaps->NotRange.Usage;
            data->ReportID = valueCaps->ReportID;
            data++;
        }
    }

	// getFeature(x->x_hid);
}

static void hidin_doopen(t_hidin *x, long deviceID)
{
	short elements;	// number of elements of HID device
	long oldrequest;

	pthread_mutex_lock(&x->x_mutex);
	oldrequest = x->x_requestcode;
	x->x_requestcode = REQUEST_NOTHING;
	pthread_cond_signal(&x->x_requestcondition);
	pthread_cond_wait(&x->x_answercondition, &x->x_mutex);

	// close old device if any
	if (x->x_device != -1)
	{
		CloseHandle(x->x_hid->device);

		// free allocated memory 
		freebytes(x->x_hid->inputData, (short)(x->x_hid->inputDataLength * sizeof(t_hid_data)));
		freebytes(x->x_hid->inputButtonCaps, (short)(x->x_hid->caps.NumberInputButtonCaps * sizeof(HIDP_BUTTON_CAPS)));
		freebytes(x->x_hid->inputValueCaps, (short)(x->x_hid->caps.NumberInputValueCaps * sizeof(HIDP_VALUE_CAPS)));
		freebytes(x->x_hid->inputReportBuffer, (short)(x->x_hid->caps.InputReportByteLength * sizeof(char)));
		x->x_hid->caps.InputReportByteLength = 0;
	}

	if (deviceID != -1)
	{
		// open new device
		x->x_hid->device = connectDeviceNumber(deviceID);

		if (x->x_hid->device != INVALID_HANDLE_VALUE)
		{
			x->x_device = deviceID;
			hidin_initdevice(x);
		}
		else	// open failed...
		{
			hidin_alloc(x, 0);	// free data memory
			x->x_device = -1;
		}
	}
	else
	{
		hidin_alloc(x, 0);	// free data memory
		post("hidin: << device closed");
		x->x_device = -1;
	}

	x->x_requestcode = oldrequest;	/* set back to old requestcode */
	pthread_cond_signal(&x->x_requestcondition);	/* go on again */
	pthread_mutex_unlock(&x->x_mutex);
}

/*
 * select & open device to use
 */
static void hidin_open(t_hidin *x, t_floatarg l)
{
	long device = (long)l - 1;	/* get new internal device number */
	hidin_doopen(x, device);
}

/*
 * close open device
 */
static void hidin_close(t_hidin *x)
{
	if (x->x_device != -1)
		hidin_doopen(x, -1);
}

/*
 * the object's new function
 */
static void *hidin_new(t_symbol *s, short argc, t_atom *argv)
{
	int i, k, n;
	int deviceID, intv;

	t_hidin *x;

	deviceID = -1;	/* default HID device number: none */
	intv = 10;		/* read every 10 ms */
	
	if (argc >= 1 && argv->a_type == A_FLOAT)	/* just one argument (long): device ID */
	{
		deviceID = (int)(argv[0].a_w.w_float - 1);
		if (argc >= 2 && argv[1].a_type == A_FLOAT)	/* second argument (long): interval */
		{
			intv = (int)argv[1].a_w.w_float;
		}
	}

	x = (t_hidin*)pd_new(hidin_class);
		
	// zero out the struct, to be careful
	if (x)
	{
    	for (i = sizeof(t_object); i < sizeof(t_hidin); i++)
    		((char*)x)[i] = 0;
	}

	x->x_outlet = outlet_new(&x->x_obj, gensym("list"));	// outputs received data
	x->x_devout = outlet_new(&x->x_obj, gensym("list"));	// outputs list of devices

	x->x_qelem = clock_new(x, (t_method)hidin_output);

    pthread_mutex_init(&x->x_mutex, 0);
    pthread_cond_init(&x->x_requestcondition, 0);
    pthread_cond_init(&x->x_answercondition, 0);

	x->x_requestcode = REQUEST_NOTHING;

	x->x_interval = intv;
	x->x_device = -1;
	x->x_elements = 0;
	x->x_data = NULL;
	x->x_prev_data = NULL;

	// allocate memory for the t_hid_device struct 
	x->x_hid = (t_hid_device *)getbytes(sizeof(t_hid_device));

	// create I/O thread
	pthread_create(&x->x_thread, 0, hidin_doit, x);

	if (deviceID != -1)
		hidin_doopen(x, deviceID);

	return (x);
}

static void hidin_free(t_hidin *x)
{
    void *threadrtn;
	t_atom atom;

	// close the HID device
	hidin_doopen(x, -1);

	// stop IO thread
    pthread_mutex_lock(&x->x_mutex);
    x->x_requestcode = REQUEST_QUIT;
    pthread_cond_signal(&x->x_requestcondition);
	startpost("hidin: >> stopping HID I/O thread...");
	SETSYMBOL(&atom, gensym("signalling..."));
    while (x->x_requestcode != REQUEST_NOTHING)
    {
		postatom(1, &atom);
		pthread_cond_signal(&x->x_requestcondition);
    	pthread_cond_wait(&x->x_answercondition, &x->x_mutex);
    }
    pthread_mutex_unlock(&x->x_mutex);
    if (pthread_join(x->x_thread, &threadrtn))
    	error("shoutcast_free: join failed");
	SETSYMBOL(&atom, gensym("done."));
	postatom(1, &atom);
	endpost();

	freebytes(x->x_hid, sizeof(t_hid_device));

	clock_free(x->x_qelem);

    pthread_cond_destroy(&x->x_requestcondition);
    pthread_cond_destroy(&x->x_answercondition);
    pthread_mutex_destroy(&x->x_mutex);
}

/*
 * the object's info function
 */
static void hidin_info(t_hidin *x)
{
	post(hidin_version);
	pthread_mutex_lock(&x->x_mutex);
	if (x->x_device != -1)
	{
		post("hidin: ** using device #%d: \"%s\"", x->x_device + 1, 
			 findDeviceName(x->x_device)->s_name);
		post("hidin: ** device has %d values and %d buttons", 
			 x->x_hid->caps.NumberInputValueCaps, 
			 x->x_hid->caps.NumberInputButtonCaps * x->x_hid->caps.NumberInputDataIndices);
		// getDeviceInfo(x->x_hid->device);
	}
	else post("hidin: -- no open device");
	pthread_mutex_unlock(&x->x_mutex);
}

void hidin_setup(void)
{
    hidin_class = class_new(gensym("hidin"),(t_newmethod)hidin_new, (t_method)hidin_free,
    	sizeof(t_hidin), 0, A_GIMME, 0);
	class_addbang(hidin_class, (t_method)hidin_bang);
	class_addfloat(hidin_class, (t_method)hidin_int);
	class_addmethod(hidin_class, (t_method)hidin_start, gensym("start"), 0);
	class_addmethod(hidin_class, (t_method)hidin_stop, gensym("stop"), 0);
	class_addmethod(hidin_class, (t_method)hidin_show, gensym("show"), 0);
	class_addmethod(hidin_class, (t_method)hidin_info, gensym("print"), 0);
	class_addmethod(hidin_class, (t_method)hidin_open, gensym("open"), A_FLOAT, 0);
	class_addmethod(hidin_class, (t_method)hidin_close, gensym("close"), 0);
	class_addmethod(hidin_class, (t_method)hidin_interval, gensym("interval"), A_FLOAT, 0);
	class_sethelpsymbol(hidin_class, gensym("help-hidin.pd"));
	post(hidin_version);
}