Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Platform / linux / GLID.sc
blob616b99e0c42759e9f3da158110544080742f5353
1 /* Wrapper for LID for General HID support */
3 GLID{
4         classvar extraClasses;
5         classvar <debug = false;
6         classvar <deviceList;
7         var <device;
8         // dummy action, not used, but here for compatibility with osx
9         var <>hidDeviceAction;
11         *initClass{
12                 if ( \LID.asClass.notNil, {
13                         Class.initClassTree( Event );
14                         extraClasses = Event.new;
15                         Class.initClassTree( GeneralHID );
16                         GeneralHID.add( this );
17                 });
18         }
20         *id { ^\linux_hid }
22         *put { arg key, object;
23                 extraClasses.put( key, object );
24         }
26         *doesNotUnderstand { arg selector ... args;
27                 ^extraClasses.perform( selector, *args );
28         }
30         // ------------ functions ------------
32         *buildDeviceList{
33                 deviceList = LID.buildDeviceList.collect{ |dev,i|
34                         [ dev[0], this.getInfo( dev ) ]
35                 };
36                 ^deviceList;
37         }
39         *postDevices {
40                 "HID devices at your disposal:".postln;
41                 deviceList.do{ |dev,i|
42                         "\t%:\t[%], vendor: %, product: %, locID: [\"%\"], path: [\"%\"]\n".postf( i, dev[1].name, dev[1].vendor, dev[1].product, dev[1].physical, dev[0] );
43                 };
44         }
46         *postDevicesAndProperties {
47                 LID.deviceList.do({arg dev;
48                         "".postln;
49                         if ( dev[1].isKindOf( LIDInfo ), {
51                                 [ dev[1].vendor, dev[1].asString, dev[0]].postcs;
52                                 //[dev[0], dev[1].name, dev[1].product, dev[1].vendor, dev[1].version].postcs;
53                                 dev[2].keysValuesDo{ |key,slotgroup,i|
54                                         ("\t"++key+LIDSlot.slotTypeStrings[key]).postln;
55                                         slotgroup.do{ |slot|
56                                                 "\t\t".post;
57                                                 if ( slot.isKindOf( LIDAbsSlot ),{
58                                                         [ slot.type, LIDSlot.slotTypeStrings[slot.type], slot.code, slot.info.asString ].postcs;
59                                                 },{
60                                                         [ slot.type, LIDSlot.slotTypeStrings[slot.type], slot.code ].postcs;
61                                                 });
62                                         };
63                                 };
64                         },{
65                                 dev.postcs;
66                         });
67                 });
68         }
70         *startEventLoop{ |rate|
71                 // event loop starts at startup with LID
72         }
74         *stopEventLoop{
75                 // event loop is stopped at shutdown with LID
76         }
78         *eventLoopIsRunning{
79                 ^LID.eventLoopIsRunning;
80         }
82         *debug_{ |onoff|
83                 debug = onoff;
84         }
86         *open { arg dev;
87                 ^super.new.init( dev );
88         }
90         *getInfo{ |dev|
91                 var info;
92                 if ( dev[1].isKindOf( LIDInfo ), {
93                         info = GeneralHIDInfo.new(
94                                 dev[1].name,
95                                 dev[1].bustype,
96                                 dev[1].vendor,
97                                 dev[1].product,
98                                 dev[1].version,
99                                 dev[1].physical,
100                                 dev[1].unique
101                         );
102                 },{
103                         info = GeneralHIDInfo.new( "could not open device" );
104                 }
105                 );
106                 ^info;
107         }
109         init{ |dev|
110                 //              var mydev;
111                 //              mydev = dev[0];
112                 //mydev.postln;
113                 if ( dev[1].isKindOf( LIDInfo ) or: dev[1].isKindOf( GeneralHIDInfo ),
114                                 {
115                                         device = LID.new( dev[0] );
116                                         ^GeneralHIDDevice.new( this );
117                                 },{
118                                         "not a valid device or could not open device".warn;
119                                         ^nil;
120                                 });
121         }
123         getInfo{
124                 var info;
125                 info = GeneralHIDInfo.new(
126                         device.info.name,
127                         device.info.bustype,
128                         device.info.vendor,
129                         device.info.product,
130                         device.info.version,
131                         device.info.physical,
132                         device.info.unique
133                 );
134                 ^info;
135         }
137         getSlots{
138                 var mySlots = IdentityDictionary.new;
139                 var devSlots = device.slots;
140                 devSlots.keysValuesDo{ |key,value,i|
141                         //              (""++i+"key"+key+"value"+value).postcs;
142                         mySlots[key] = IdentityDictionary.new;
143                         value.keysValuesDo{ |key2,value2,i2|
144                                 mySlots[key][key2] = GeneralHIDSlot.new( key, key2, device, value2 );
145                         };
146                 };
147                 ^mySlots;
148         }
150         close{
151                 if ( device.notNil,
152                         {
153                                 device.close;
154                         });
155         }
157         isOpen{
158                 if ( device.notNil,
159                         {
160                                 ^device.isOpen;
161                         },{
162                                 ^false;
163                         }
164                 );
165         }
167         info{
168                 if ( device.notNil,
169                         {
170                                 ^device.info;
171                         },{
172                                 ^nil;
173                         }
174                 );
175         }
177         action_{ |act|
178                 device.action = act;
179         }
181         grab{
182                 device.grab;
183         }
184         ungrab{
185                 device.ungrab;
186         }