Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Control / SystemActions.sc
blob680b455f7e09fa0722832e80291cb002ba7509fa
1 AbstractSystemAction {
3         *init {
4                 this.objects = List.new;
5         }
7         *add { arg object;
8                 if(this.objects.isNil) { this.init }; // lazy init
9                 if(this.objects.includes(object).not) { this.objects.add(object) }
10         }
12         *remove { arg object;
13                 this.objects.remove(object)
14         }
16         *removeAll {
17                 this.init
18         }
21         *objects { ^this.shouldNotImplement(thisMethod) }
22         *objects_ { arg obj; ^this.shouldNotImplement(thisMethod) }
27 // things to clear when hitting cmd-.
29 CmdPeriod : AbstractSystemAction {
30         classvar <>objects;
31         classvar <era = 0;
32         classvar <>clearClocks = true;
33         classvar <>freeServers = true;
34         classvar <>freeRemote = false;
38         *doOnce { arg object;
39                 var f = { this.remove(f); object.doOnCmdPeriod  };
40                 this.add(f);
41         }
42         *run {
43                 if(clearClocks, {
44                         SystemClock.clear;
45                         AppClock.clear;
46         //              TempoClock.default.clear;
47                 });
49                 objects.copy.do({ arg item; item.doOnCmdPeriod;  });
51                 if(freeServers, {
52                         Server.freeAll(freeRemote); // stop all sounds on local, or remote servers
53                         Server.resumeThreads;
54                 });
56                 era = era + 1;
58         }
60         *hardRun {
62                 SystemClock.clear;
63                 AppClock.clear;
64                 TempoClock.default.clear;
66                 objects.copy.do({ arg item; item.doOnCmdPeriod;  });
70                 Server.hardFreeAll; // stop all sounds on local servers
71                 Server.resumeThreads;
72                 era = era + 1;
74         }
80 // things to do after startup file executed
82 StartUp : AbstractSystemAction {
85         classvar <>objects, <done=false;
88         *run {
89                 done = true;
90                 objects.copy.do({ arg item; item.doOnStartUp  });
91                 // "StartUp done.".postln;
92         }
95         *defer { arg object;
96                  if(done) { object.doOnStartUp } { this.add(object) }
97         }
103 // things to do before system shuts down
105 ShutDown : AbstractSystemAction {
107         classvar <>objects;
109         *run {
110                 objects.copy.do({ arg item; item.doOnShutDown;  });
111         //      "ShutDown done.".postln;
112         }
116 // things to do on a system reset
117 OnError : AbstractSystemAction {
118         classvar <>objects;
120         *run {
121                 objects.copy.do({ arg item; item.doOnError;  });
122         }
126 AbstractServerAction : AbstractSystemAction {
128         *init {
129                 this.objects = IdentityDictionary.new;
130         }
132         *performFunction { arg server, function;
133                 if (this.objects.notNil) {
134                         this.objects.at(server).copy.do(function);
135                         if(server === Server.default) {
136                                 this.objects.at(\default).copy.do(function)
137                         };
138                         this.objects.at(\all).copy.do(function);
139                 }
140         }
142         *run { arg server;
143                 var selector = this.functionSelector;
144                 // selector.postln;
145                 this.performFunction(server, { arg obj; obj.perform(selector, server) });
146         }
148         *functionSelector {
149                 ^this.subclassResponsibility(thisMethod)
150         }
152         *add { arg object, server;
153                 var list;
154                 if (server.isNil)  { server = \all };
155                 if (this.objects.isNil) { this.init };
156                 list = this.objects.at(server);
157                 if (list.isNil) { list = List.new; this.objects.put(server, list) };
158                 if (list.includes(object).not) { list.add(object) };
159         }
161         *addToAll { arg object;
162                 Server.all.do({ arg s; this.add(object, s) });
163         }
165         *remove { arg object, server;
166                 if(server.isNil) { server = \default };
167                 this.objects !? { this.objects.at(server).remove(object) };
168         }
170         *removeServer { arg server;
171                 this.objects.removeAt(server)
172         }
175 // things to do after server has booted
178 ServerBoot : AbstractServerAction {
180         classvar <>objects;
182         *functionSelector {
183                 ^\doOnServerBoot
184         }
187 // things to do after server has quit
190 ServerQuit : AbstractServerAction {
192         classvar <>objects;
194         *functionSelector {
195                 ^\doOnServerQuit
196         }
200 // things to do after server has booted and initialised
203 ServerTree : AbstractServerAction {
205         classvar <>objects;
207         *functionSelector {
208                 ^\doOnServerTree
209         }