aboutsummaryrefslogtreecommitdiff
path: root/hid_darwin.c
blob: b4fc29217a1b39f7ecdfdaaa688dc1cb4399cb2b (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
#ifdef __APPLE__
/*
 *  Apple Darwin HID Manager support for Pd [hid] object
 *
 *  based on SC_HID.cpp from SuperCollider3 by Jan Truetzschler v. Falkenstein
 *
 *  Copyright (c) 2004 Hans-Christoph All rights reserved.
 *
 *   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.
 *
 *   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
 *
 *
 */

/* struct IOHIDEventStruct */
/* { */
/*     IOHIDElementType	type; */
/*     IOHIDElementCookie	elementCookie; */
/*     SInt32		value; */
/*     AbsoluteTime	timestamp; */
/*     UInt32		longValueSize; */
/*     void *		longValue; */
/* }; */

/* typedef struct { */
/*         natural_t hi; */
/*         natural_t lo; */
/* } AbsoluteTime; */



#include <Carbon/Carbon.h>

#include "HID_Utilities_External.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 <mach/mach.h>
#include <mach/mach_error.h>

#include "hid.h"

//#define DEBUG(x)
#define DEBUG(x) x 

/*==============================================================================
 *  GLOBAL VARS
 *======================================================================== */

/* count of total number of devices found */
int gNumberOfHIDDevices = 0;

/* 
 *	an array of discovered devices, which is used for selecting the
 * current device, by #, type, or name
 */
pRecDevice discoveredDevices[256];

/* timer for element data updates */
EventLoopTimerRef gTimer = NULL; 

/*==============================================================================
 *  FUNCTIONS
 *==============================================================================
 */

void releaseHIDDevices (void)
{
	DEBUG(post("releaseHIDDevices"););

	if (gTimer)
    {    
		RemoveEventLoopTimer(gTimer);
		gTimer = NULL;
	}
	HIDReleaseAllDeviceQueues();
	HIDReleaseDeviceList();
	gNumberOfHIDDevices = 0;
}

int prHIDBuildElementList(void)
{
	DEBUG(post("prHIDBuildElementList"););

	int locID = NULL;
	int cookieNum = NULL;
	UInt32 i;
	pRecElement	devElement;
	pRecDevice pCurrentHIDDevice;
	UInt32 numElements;
	char cstrElementName [256];

// Apple Trackpad locID for testing
	locID = 50397184;

	// look for the right device using locID 
	pCurrentHIDDevice = HIDGetFirstDevice ();
	while (pCurrentHIDDevice && (pCurrentHIDDevice->locID !=locID))
		pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	if(!pCurrentHIDDevice) return (1);
	
	devElement = HIDGetFirstDeviceElement(pCurrentHIDDevice, kHIDElementTypeInput);
	numElements = HIDCountDeviceElements(pCurrentHIDDevice, kHIDElementTypeInput);
	
	post("[hid] found %d elements",numElements);
	
	for(i=0; i<numElements; i++)
	{
		//type
		HIDGetTypeName((IOHIDElementType) devElement->type, cstrElementName);
		post("Type: %s  %d  0x%x",cstrElementName,devElement->type,devElement->type);
		//usage
		HIDGetUsageName (devElement->usagePage, devElement->usage, cstrElementName);
		post("Usage: %s  %d  0x%x",cstrElementName,devElement->usage,devElement->usage);
		
			//devstring = newPyrString(g->gc, cstrElementName, 0, true);
			//SetObject(devElementArray->slots+devElementArray->size++, devstring);
			//g->gc->GCWrite(devElementArray, (PyrObject*) devstring);
		//cookie
		post("Cookie: %d %d %d",devElement->cookie,devElement->min,devElement->max);
		
		devElement =  HIDGetNextDeviceElement (devElement, kHIDElementTypeInput);
	}
	return (0);	
}

int prHIDBuildDeviceList(void)
{
	DEBUG(post("prHIDBuildDeviceList"););

	int i,err;
	UInt32 usagePage, usage;
	pRecElement devElement;
	pRecDevice pCurrentHIDDevice;

	//pass in usage & usagepage
	//kHIDUsage_GD_Joystick kHIDUsage_GD_GamePad
	usagePage = kHIDPage_GenericDesktop;
	usage = NULL;

	Boolean result = HIDBuildDeviceList (usagePage, usage); 
	// returns false if no device found

	if(result) error("[hid]: no HID devices found\n");
	
	int numdevs = HIDCountDevices();
	gNumberOfHIDDevices = numdevs;
	// exit if no devices found
	if(!numdevs) return (0);

	post("number of devices: %d", numdevs);
	char cstrDeviceName [256];
	
	pCurrentHIDDevice = HIDGetFirstDevice();
	for(i=0; i<numdevs; i++)
	{
		post("'%s' '%s' version %d",
			  pCurrentHIDDevice->manufacturer,pCurrentHIDDevice->product,pCurrentHIDDevice->version);
		//usage
		HIDGetUsageName (pCurrentHIDDevice->usagePage, pCurrentHIDDevice->usage, cstrDeviceName);
		post("vendorID: %d   productID: %d   locID: %d",
			  pCurrentHIDDevice->vendorID,pCurrentHIDDevice->productID,pCurrentHIDDevice->locID);
		pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	}

	UInt32 outnum = HIDCountDeviceElements (pCurrentHIDDevice, kHIDElementTypeOutput);
	post("number of outputs: %d \n", outnum);

	return (0);	
}


int prHIDGetValue(void)
{
	DEBUG(post("prHIDGetValue"););

	int locID = NULL; 
	int cookieNum = NULL;
	SInt32 value;
	/*
	PyrSlot *a = g->sp - 2; //class
	PyrSlot *b = g->sp - 1; //locID device
	PyrSlot *c = g->sp; //element cookie
	int err = slotIntVal(b, &locID);
	if (err) return err;
	err = slotIntVal(c, &cookieNum);
	if (err) return err;
	*/
	IOHIDElementCookie cookie = (IOHIDElementCookie) cookieNum;
	// look for the right device: 
	pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	while (pCurrentHIDDevice && (pCurrentHIDDevice->locID !=locID))
		pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	if(!pCurrentHIDDevice) return (1);
	// look for the right element:
	pRecElement pCurrentHIDElement =  HIDGetFirstDeviceElement (pCurrentHIDDevice, kHIDElementTypeIO);
	// use gElementCookie to find current element
	while (pCurrentHIDElement && (pCurrentHIDElement->cookie != cookie))
		pCurrentHIDElement = HIDGetNextDeviceElement (pCurrentHIDElement, kHIDElementTypeIO);
	
	if (pCurrentHIDElement)
	{
		value = HIDGetElementValue (pCurrentHIDDevice, pCurrentHIDElement);
		// if it's not a button and it's not a hatswitch then calibrate
		if(( pCurrentHIDElement->type != kIOHIDElementTypeInput_Button ) &&
			( pCurrentHIDElement->usagePage == 0x01 && pCurrentHIDElement->usage != kHIDUsage_GD_Hatswitch)) 
			value = HIDCalibrateValue ( value, pCurrentHIDElement );
		//SetInt(a, value);
	}
	//else SetNil(a);
	return (0);	
	
}


void PushQueueEvents_RawValue(void)
{
	DEBUG(post("PushQueueEvents_RawValue"););

	int i;
	
	IOHIDEventStruct event;
	pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	int numdevs = gNumberOfHIDDevices;
	unsigned char result;
	for(i=0; i< numdevs; i++)
	{
		result = HIDGetEvent(pCurrentHIDDevice, (void*) &event);
		if(result) 
		{
			SInt32 value = event.value;
			int vendorID = pCurrentHIDDevice->vendorID;
			int productID = pCurrentHIDDevice->productID;			
			int locID = pCurrentHIDDevice->locID;
			IOHIDElementCookie cookie = (IOHIDElementCookie) event.elementCookie;
			//set arguments: 
//			++g->sp;SetInt(g->sp, vendorID); 
//			++g->sp;SetInt(g->sp, productID); 			
//			++g->sp;SetInt(g->sp, locID); 
//			++g->sp;SetInt(g->sp, (int) cookie); 
//			++g->sp;SetInt(g->sp, value); 
		}
		pCurrentHIDDevice = HIDGetNextDevice(pCurrentHIDDevice);
	}
}


void PushQueueEvents_CalibratedValue(void)
{
	DEBUG(post("PushQueueEvents_CalibratedValue"););

	int i;
	
	IOHIDEventStruct event;
	pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	
	int numdevs = gNumberOfHIDDevices;
	unsigned char result;
	for(i=0; i< numdevs; i++)
	{

		result = HIDGetEvent(pCurrentHIDDevice, (void*) &event);
		if(result) 
		{
			SInt32 value = event.value;
			int vendorID = pCurrentHIDDevice->vendorID;
			int productID = pCurrentHIDDevice->productID;			
			int locID = pCurrentHIDDevice->locID;
			IOHIDElementCookie cookie = (IOHIDElementCookie) event.elementCookie;
			pRecElement pCurrentHIDElement =  HIDGetFirstDeviceElement (pCurrentHIDDevice, kHIDElementTypeIO);
	// use gElementCookie to find current element
			while (pCurrentHIDElement && ( (pCurrentHIDElement->cookie) != cookie))
				pCurrentHIDElement = HIDGetNextDeviceElement (pCurrentHIDElement, kHIDElementTypeIO);
		
			if (pCurrentHIDElement)
			{
			value = HIDCalibrateValue(value, pCurrentHIDElement);
			//find element to calibrate
			//set arguments: 
//			++g->sp;SetInt(g->sp, vendorID); 
//			++g->sp;SetInt(g->sp, productID); 			
//			++g->sp;SetInt(g->sp, locID); 
//			++g->sp;SetInt(g->sp, (int) cookie); 
//			++g->sp;SetInt(g->sp, value); 
			}
		}
		pCurrentHIDDevice = HIDGetNextDevice(pCurrentHIDDevice);
	}
}


static pascal void IdleTimer(EventLoopTimerRef inTimer, void* userData)
{
	DEBUG(post("IdleTimer"););

	#pragma unused (inTimer, userData)
	PushQueueEvents_CalibratedValue ();
}


int prHIDReleaseDeviceList(void)
{
	DEBUG(post("prHIDReleaseDeviceList"););

	releaseHIDDevices();
	return (0);	
}


static EventLoopTimerUPP GetTimerUPP(void)
{
	DEBUG(post("GetTimerUPP"););

	static EventLoopTimerUPP sTimerUPP = NULL;
	
	if (sTimerUPP == NULL)
		sTimerUPP = NewEventLoopTimerUPP(IdleTimer);
	
	return sTimerUPP;
}
/*
typedef void (*IOHIDCallbackFunction)
              (void * target, IOReturn result, void * refcon, void * sender);

*/
/*
void callback  (void * target, IOReturn result, void * refcon, void * sender);
void callback  (void * target, IOReturn result, void * refcon, void * sender)
{
}
*/

int prHIDRunEventLoop(void)
{
	DEBUG(post("prHIDRunEventLoop"););

	//PyrSlot *a = g->sp - 1; //class

	InstallEventLoopTimer(GetCurrentEventLoop(), 0, 0.001, GetTimerUPP (), 0, &gTimer);

	//HIDSetQueueCallback(pCurrentHIDDevice, callback);
	return (0);
}


int prHIDQueueDevice(void)
{
	DEBUG(post("prHIDQueueDevice"););

	int locID = NULL;
	int cookieNum = NULL;
	
	//PyrSlot *a = g->sp - 1; //class
	//PyrSlot *b = g->sp; //locID device
	//int err = slotIntVal(b, &locID);
	//if (err) return err;
	//look for the right device: 
	pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	while (pCurrentHIDDevice && (pCurrentHIDDevice->locID !=locID))
		pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	if(!pCurrentHIDDevice) return (1);
	HIDQueueDevice(pCurrentHIDDevice);
	return (0);	
}


int prHIDQueueElement(void)
{
	DEBUG(post("prHIDQueueElement"););

	int locID = NULL;
	int cookieNum = NULL;
	
	//PyrSlot *a = g->sp - 2; //class
	//PyrSlot *b = g->sp - 1; //locID device
	//PyrSlot *c = g->sp; //element cookie
	//int err = slotIntVal(b, &locID);
	//if (err) return err;
	//err = slotIntVal(c, &cookieNum);
	//if (err) return err;
	IOHIDElementCookie cookie = (IOHIDElementCookie) cookieNum;
	//look for the right device: 
	pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	while (pCurrentHIDDevice && (pCurrentHIDDevice->locID !=locID))
		pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	if(!pCurrentHIDDevice) return (1);
	//look for the right element:
	pRecElement pCurrentHIDElement =  HIDGetFirstDeviceElement (pCurrentHIDDevice, kHIDElementTypeIO);
	// use gElementCookie to find current element
	while (pCurrentHIDElement && (pCurrentHIDElement->cookie != cookie))
		pCurrentHIDElement = HIDGetNextDeviceElement (pCurrentHIDElement, kHIDElementTypeIO);
	if(!pCurrentHIDElement) return (1);
	HIDQueueElement(pCurrentHIDDevice, pCurrentHIDElement);
	return (0);	
}


int prHIDDequeueElement(void)
{
	DEBUG(post("prHIDDequeueElement"););

	int locID = NULL;
	int cookieNum = NULL;
	
	//PyrSlot *a = g->sp - 2; //class
	//PyrSlot *b = g->sp - 1; //locID device
	//PyrSlot *c = g->sp; //element cookie
	//int err = slotIntVal(b, &locID);
	//if (err) return err;
	//err = slotIntVal(c, &cookieNum);
	//if (err) return err;
	IOHIDElementCookie cookie = (IOHIDElementCookie) cookieNum;
	//look for the right device: 
    pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	while (pCurrentHIDDevice && (pCurrentHIDDevice->locID !=locID))
        pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	if(!pCurrentHIDDevice) return (1);
	//look for the right element:
	pRecElement pCurrentHIDElement =  HIDGetFirstDeviceElement (pCurrentHIDDevice, kHIDElementTypeIO);
    while (pCurrentHIDElement && (pCurrentHIDElement->cookie != cookie))
        pCurrentHIDElement = HIDGetNextDeviceElement (pCurrentHIDElement, kHIDElementTypeIO);
	if(!pCurrentHIDElement) return (1);
	HIDDequeueElement(pCurrentHIDDevice, pCurrentHIDElement);
	return (0);	
}


int prHIDDequeueDevice(void)
{
	DEBUG(post("prHIDDequeueDevice"););

	int locID = NULL;
	int cookieNum = NULL;
	
	/*
	PyrSlot *a = g->sp - 1; //class
	PyrSlot *b = g->sp; //locID device
	int err = slotIntVal(b, &locID);
	if (err) return err;
	*/
	//look for the right device: 
	pRecDevice  pCurrentHIDDevice = HIDGetFirstDevice ();
	while (pCurrentHIDDevice && (pCurrentHIDDevice->locID !=locID))
		pCurrentHIDDevice = HIDGetNextDevice (pCurrentHIDDevice);
	if(!pCurrentHIDDevice) return (1);
	HIDDequeueDevice(pCurrentHIDDevice);
	return (0);	
}


int prHIDStopEventLoop(void)
{
	DEBUG(post("prHIDStopEventLoop"););

	if (gTimer)
        RemoveEventLoopTimer(gTimer);
	gTimer = NULL;
	return (0);
}


t_int hid_open_device(t_hid *x, t_int device_number)
{
	post("open_device %d",device_number);

	return (1);
}

t_int hid_open_device(t_hid *x)
{
	return (0);
}


t_int hid_devicelist_refresh(t_hid *x)
{
	/* the device list should be refreshed here */
	if ( (prHIDBuildDeviceList()) && (prHIDBuildElementList()) )
		return (0);
	else
		return (1);
}


/* this is just a rough sketch */

/* getEvents(t_hid *x) { */
/* 	pRecDevice pCurrentHIDDevice = GetSetCurrentDevice (gWindow); */
/* 	pRecElement pCurrentHIDElement = GetSetCurrenstElement (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  /* #ifdef __APPLE__ */