File: HIDDeviceService.sc

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (298 lines) | stat: -rw-r--r-- 8,651 bytes parent folder | download
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
HIDInfo{
	var <name, <bustype=0, <vendor, <product, <version=0;

	*new{arg  product, bustype, vendorID, productID, version;
		^super.newCopyArgs( product, bustype, vendorID, productID, version ).init;
	}

	init{
	}

	printOn { | stream |
		super.printOn(stream);
		stream << $( << name << ", ";
		[
			bustype,
			vendor,
			product,
			version
		].collect({ | x | "0x" ++ x.asHexString(4) }).printItemsOn(stream);
		stream.put($));
	}
}

HIDDevice {
	var <manufacturer, < product, <usage, < vendorID, < productID, < locID, < version, < serial;
	var < elements;
	var < isQueued = false;
	var < info, <> closeAction;

	*new{arg manufacturer, product, usage, vendorID, productID, locID, version, serial;
		^super.newCopyArgs(manufacturer, product, usage, vendorID, productID, locID, version, serial).init;
	}
	init{
		info = HIDInfo.new( product, 0, vendorID, productID, version );
		closeAction = {};
		elements = Array.new;
	}

	value{arg elementNum=0;
		^HIDDeviceService.value(locID,elements.at(elementNum).cookie)
	}
	valueByCookie{arg cookie;
		^HIDDeviceService.value(locID,cookie)
	}
	setValue{|elementNum, val|
		HIDDeviceService.setValue(locID,elements.at(elementNum).cookie, val)
	}
	setValueByCookie{|cookie, val|
		HIDDeviceService.setValue(locID,cookie, val)
	}
	queueDevice{
		isQueued = true;
		HIDDeviceService.queueDevice(locID);
	}

	dequeueDevice{
		isQueued = false;
		HIDDeviceService.dequeueDevice(locID);
	}

	queueElement{arg elementNum=0;
		HIDDeviceService.queueElement(locID, elements.at(elementNum).cookie);
	}

	dequeueElement{arg elementNum=0;
		HIDDeviceService.dequeueElement(locID, elements.at(elementNum).cookie);
	}

	wasClosed{
		("WARNING: Device was removed: " + info).postln;
		closeAction.value;
	}

	//private:
	prAddElement{arg type, usage, cookie, min, max, ioType, usagePage, usageType;
		elements = elements.add(HIDDeviceElement(type, usage, cookie, min, max, ioType, usagePage, usageType));
	}
}

HIDDeviceElement {
	var < type, <usage, < cookie, <min, <max, <ioType, <usagePage, < usageType;
	*new{arg type, usage, cookie, min, max, ioType, usagePage, usageType;
		^super.newCopyArgs(type, usage, cookie, min, max, ioType, usagePage, usageType);
	}
//	value_{|val|
//		HIDDeviceService.setValue(locID, cookie, val)
//
//	}
}

HIDDeviceService{
	classvar < devices, <> action;
	classvar < initialized = false;
	classvar < deviceSpecs;
	classvar < eventLoopIsRunning = false;
	classvar <> closeAction;

	*initClass {
		deviceSpecs = IdentityDictionary.new;
		closeAction = {};
		ShutDown.add( { this.releaseDeviceList } );
	}

	*keyToIndex { arg key, locID=0;
		var device, deviceSpec;
		device = devices.at(locID);
		^if(device.isNil, { ^nil }, {
			deviceSpec = deviceSpecs.at(device.product.asSymbol);
			if(deviceSpec.notNil, { deviceSpec.at(key) }, { nil });
		})
	}

	*value{arg locID, cookie;
		_HIDGetValue
	}

	*setValue{arg locID, cookie, value;
		_HIDSetValue
	}

	*buildDeviceList{arg usagePage=1, usage=4;
		var devlist, elelist;
		var alreadyOpen;
		alreadyOpen = devices.select( _.isQueued ).asArray;
		alreadyOpen.do{ |it| it.dequeueDevice };
		alreadyOpen = alreadyOpen.collect( _.locID ).asArray;
		//alreadyOpen.postln;
		// the following line would solve the bug when rebuilding the device list, but then there is no way to keep track of already opened devices:
		this.releaseDeviceList;
		devices = Array.new;
		devlist = this.prbuildDeviceList(usagePage, usage);
		devlist ?? {"HIDDeviceService: no devices found".warn; ^nil};
		devlist.do({arg dev;
			var newdev;
			// this is an ugly, ugly workaround:
			if ( dev.isKindOf( Array ), {
				if ( dev[0].isKindOf( String ) and: dev[1].isKindOf( String ) and: dev[2].isKindOf( String ) and: dev[7].isKindOf( String ) and: dev[3].isKindOf( Integer ) and: dev[4].isKindOf( Integer ) and: dev[5].isKindOf( Integer ) and: dev[6].isKindOf( Integer ), {
					newdev = HIDDevice(dev.at(0), dev.at(1), dev.at(2), dev.at(3), dev.at(4), dev.at(5), dev.at(6), dev.at(7));
					elelist = this.prbuildElementList(newdev.locID,
						Array.newClear(HIDDeviceService.prGetElementListSize(newdev.locID)));
					elelist.do({arg ele;
						if(ele.notNil){
							newdev.prAddElement(ele.at(0), ele.at(1), ele.at(2), ele.at(3), ele.at(4), ele.at(5), ele.at(6), ele.at(7));
						};
					});
					devices = devices.add(newdev);
					// reopen the device if it already was open
					if ( alreadyOpen.includes( newdev.locID ) ){
						newdev.queueDevice;	
					}
				});
			});
		});
		initialized = true;
	}

	*prbuildDeviceList{arg usagePage=1, usage;
		_HIDBuildDeviceList
		^this.primitiveFailed
	}
	*prbuildElementList{arg locID;
		_HIDBuildElementList
		^this.primitiveFailed
	}
	*prGetElementListSize{arg locID;
		_HIDGetElementListSize
		^this.primitiveFailed
	}
	*releaseDeviceList{
		//must be called before closing the program..
		if(initialized,{
			this.prreleaseDeviceList;
			devices = nil;
		});
	}
	*prreleaseDeviceList{
		//must be called before closing the program..
		_HIDReleaseDeviceList
	}
	*runEventLoop {arg rate=0.002;
		eventLoopIsRunning.if({
			"META_HIDDeviceService-runEventLoop: \n\t stopping and restarting running eventLoop".warn;
		});
		this.stopEventLoop;
		this.pr_runEventLoop(rate);
		eventLoopIsRunning = true;
	}

	*pr_runEventLoop{arg rate;
		_HIDRunEventLoop
	}
	*stopEventLoop{
		this.pr_stopEventLoop;
		eventLoopIsRunning = false;
	}
	*pr_stopEventLoop{
		_HIDStopEventLoop
	}

	*prHidAction{arg vendorID, productID, locID, cookie, val;
		action.value(vendorID, productID, locID, cookie, val);
	}

	*prReadError{ arg locID;
		var dev;
		HIDDeviceService.dequeueDevice( locID );
		dev = devices.detect( { |dv| dv.locID == locID } );
		dev.wasClosed;
		closeAction.value( locID );
	}

	*queueDevice{arg locID;
		_HIDQueueDevice
		^this.primitiveFailed
	}

	*dequeueDevice{arg locID;
		_HIDDequeueDevice
		^this.primitiveFailed
	}

	*queueElement{arg locID, cookie;
		_HIDQueueElement
		^this.primitiveFailed
	}

	*dequeueElement{arg locID, cookie;
		_HIDDequeueElement
		^this.primitiveFailed
	}
}

//HIDDeviceService by jan trutzschler v. falkenstein [10/2003]
/*
these are values to pass in buildDeviceList(usagePage, usage);
this is an incomplete list.
for more look at IOHIDUsageTables.h

most common usagePages for hid:
enum
{
	kHIDPage_Undefined	= 0x00,
	kHIDPage_GenericDesktop	= 0x01,
	kHIDPage_Simulation	= 0x02,
	kHIDPage_VR	= 0x03,
	kHIDPage_Sport	= 0x04,
	kHIDPage_Game	= 0x05,
}
most common usage for hid:

/* GenericDesktop Page (0x01) */
enum
{
	kHIDUsage_GD_Pointer	= 0x01,	/* Physical Collection */
	kHIDUsage_GD_Mouse	= 0x02,	/* Application Collection */
	/* 0x03 Reserved */
	kHIDUsage_GD_Joystick	= 0x04,	/* Application Collection */
	kHIDUsage_GD_GamePad	= 0x05,	/* Application Collection */
	kHIDUsage_GD_Keyboard	= 0x06,	/* Application Collection */
	kHIDUsage_GD_Keypad	= 0x07,	/* Application Collection */
	kHIDUsage_GD_MultiAxisController	= 0x08,	/* Application Collection */
}

/* Simulation Page (0x02) */
/* This section provides detailed descriptions of the usages employed by simulation devices. */
enum
{
	kHIDUsage_Sim_FlightSimulationDevice	= 0x01,	/* Application Collection */
	kHIDUsage_Sim_AutomobileSimulationDevice	= 0x02,	/* Application Collection */
	kHIDUsage_Sim_TankSimulationDevice	= 0x03,	/* Application Collection */
	kHIDUsage_Sim_SpaceshipSimulationDevice	= 0x04,	/* Application Collection */
	kHIDUsage_Sim_SubmarineSimulationDevice	= 0x05,	/* Application Collection */
	kHIDUsage_Sim_SailingSimulationDevice	= 0x06,	/* Application Collection */
	kHIDUsage_Sim_MotorcycleSimulationDevice	= 0x07,	/* Application Collection */
	kHIDUsage_Sim_SportsSimulationDevice	= 0x08,	/* Application Collection */
	kHIDUsage_Sim_AirplaneSimulationDevice	= 0x09,	/* Application Collection */
	kHIDUsage_Sim_HelicopterSimulationDevice	= 0x0A,	/* Application Collection */
	kHIDUsage_Sim_MagicCarpetSimulationDevice	= 0x0B,	/* Application Collection */
	kHIDUsage_Sim_BicycleSimulationDevice	= 0x0C,	/* Application Collection */
	/* 0x0D - 0x1F Reserved */
	kHIDUsage_Sim_FlightControlStick	= 0x20,	/* Application Collection */
	kHIDUsage_Sim_FlightStick	= 0x21,	/* Application Collection */
	kHIDUsage_Sim_CyclicControl	= 0x22,	/* Physical Collection */
	kHIDUsage_Sim_CyclicTrim	= 0x23,	/* Physical Collection */
	kHIDUsage_Sim_FlightYoke	= 0x24,	/* Application Collection */
	kHIDUsage_Sim_TrackControl	= 0x25,	/* Physical Collection */
}

/* Game Page (0x05) */
enum
{
	kHIDUsage_Game_3DGameController	= 0x01,	/* Application Collection */
	kHIDUsage_Game_PinballDevice	= 0x02,	/* Application Collection */
	kHIDUsage_Game_GunDevice	= 0x03,	/* Application Collection */
}


*/