Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Control / GeneralHIDSpec.sc
blobcd4e040be49b0280fc4d7c839f64b2d6054fe94d
1 GeneralHIDSpec{
2         classvar <>all,<folder;
4         var <map, <device, <info;
6         *initClass {
7                 folder = (Platform.userAppSupportDir +/+ "GeneralHIDSpecs").standardizePath;
8                 if ( this.checkSaveFolder ){ this.loadSavedInfo; };
9                 all     = all ? IdentityDictionary.new;
10         }
12         *loadSavedInfo{
13                 var filename;
14                 filename = (folder+/+"allspecs.info");
15                 if ( File.exists(filename) ){
16                         all = filename.load;
17                 }
18         }
20         *checkSaveFolder{
21                 ^File.exists( folder );
22         }
24         *makeSaveFolder {
25                 var testfile, testname = "zzz_generalhid_test_delete_me.txt";
26                 folder = (Platform.userAppSupportDir +/+ "GeneralHIDSpecs").standardizePath;
27                 testfile = File(folder +/+ testname, "w");
29                 if (testfile.isOpen.not)
30                         { folder.mkdir }
31                         { testfile.close;  unixCmd("rm" + folder.escapeChar($ ) +/+ testname) }
32         }
34         *new { |dev|
35                 ^super.new.init(dev);
36         }
38         init{ |dev|
39                 device = dev;
40                 map = IdentityDictionary.new;
41                 info = IdentityDictionary.new;
42                 info.put( \vendor, device.info.vendor );
43                 info.put( \name, device.info.name );
44                 info.put( \product, device.info.product );
45                 info.put( \version, device.info.version );
46                 info.put( \scheme, GeneralHID.scheme );
47         }
49         find{
50                 ^all.select{ |thisspec,key|
51                         ( thisspec[\vendor] == device.info.vendor ) and:
52                         ( thisspec[\name] == device.info.name ) and:
53                         ( thisspec[\product] == device.info.product ) and:
54                         ( thisspec[\version] == device.info.version ) and:
55                         ( thisspec[\scheme] == GeneralHID.current )
56                 }.keys.asArray;
57         }
59         add{ |key, slot|
60                 map.put( key, slot );
61                 this.at( key ).key = key;
62         }
64         // returns the slot
65         at{ |key|
66                 var id1,id2;
67                 id1 = map.at(key)[0];
68                 id2 = map.at(key)[1];
69                 ^device.slots[id1][id2];
70                 // map.at(key)
71         }
73         value{ |key|
74                 ^this.at(key).value;
75         }
77         value_{ |key,value|
78                 var slot;
79                 slot = this.at(key);
80                 slot.value_(value);
81                 ^slot;
82         }
84         action_{ |key,action|
85                 var slot;
86                 slot = this.at(key);
87                 slot.action_(action);
88                 ^slot;
89         }
91         bus{ |key|
92                 ^this.at(key).bus;
93         }
95         createBus{ |name,server|
96                 this.at( name ).createBus( server );
97         }
99         freeBus{ |name|
100                 this.at( name ).freeBus;
101         }
103         /*      setAllActions{ |action|
104                 map.do{ |it|
105                         device.slots.at( it[0] ).at( it[1] ).action_( action );
106                 };
107                 }*/
109         createAllBuses{ |server|
110                 map.do{ |it|
111                         device.slots.at( it[0] ).at( it[1] ).createBus( server );
112                 };
113         }
115         freeAllBuses{
116                 map.do{ |it|
117                         device.slots.at( it[0] ).at( it[1] ).freeBus;
118                 };
119         }
121         save{ |name|
122                 var file, res = false;
123                 var filename;
124                 all.put( name.asSymbol, info );
125                 if (  GeneralHIDSpec.checkSaveFolder.not ) {  GeneralHIDSpec.makeSaveFolder };
126                 filename = folder +/+ name ++ ".spec";
127                 file = File(filename, "w");
128                 if (file.isOpen) {
129                         res = file.write(map.asCompileString);
130                         file.close;
131                 };
132                 this.class.saveAll;
133                 ^res;
134         }
136         *saveAll{
137                 var file, res = false;
138                 var filename;
139                 if ( GeneralHIDSpec.checkSaveFolder.not ) {  GeneralHIDSpec.makeSaveFolder };
140                 filename = folder +/+ "allspecs.info";
141                 file = File(filename, "w");
142                 if (file.isOpen) {
143                         res = file.write(all.asCompileString);
144                         file.close;
145                 };
146                 ^res;
147         }
149         fromFile { |name|
150                 if (  GeneralHIDSpec.checkSaveFolder.not ) {  GeneralHIDSpec.makeSaveFolder };
151                 map = (folder +/+ name++".spec").load;
152                 map.keysValuesDo{ |key,it|
153                         this.at( key ).key = key;
154                 }
155         }