class library: SynthDef - lazy implementation of removeUGen
[supercollider.git] / include / lang / SC_TerminalClient.h
blob90c04b0d343217146d7e5864818a5d233f362e57
1 /* -*- c++ -*-
2 Commandline interpreter interface.
3 Copyright (c) 2003 2004 stefan kersten.
5 ====================================================================
7 SuperCollider real time audio synthesis system
8 Copyright (c) 2002 James McCartney. All rights reserved.
9 http://www.audiosynth.com
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #ifndef SC_TERMINALCLIENT_H_INCLUDED
27 #define SC_TERMINALCLIENT_H_INCLUDED
29 #include "SC_LanguageClient.h"
30 #include "SC_StringBuffer.h"
32 // =====================================================================
33 // SC_TerminalClient - command line sclang client.
34 // =====================================================================
36 class SC_DLLEXPORT SC_TerminalClient : public SC_LanguageClient
38 public:
39 enum
41 kInterpretCmdLine = 0x1b,
42 kInterpretPrintCmdLine = 0x0c,
43 kRecompileLibrary = 0x18 // ctrl+x
46 struct Options : public SC_LanguageClient::Options
48 Options()
49 : mLibraryConfigFile(0),
50 mDaemon(false),
51 mCallRun(false),
52 mCallStop(false),
53 mArgc(0), mArgv(0)
54 { }
56 const char* mLibraryConfigFile;
57 bool mDaemon;
58 bool mCallRun;
59 bool mCallStop;
60 int mArgc;
61 char** mArgv;
64 SC_TerminalClient(const char* name);
65 virtual ~SC_TerminalClient();
67 const Options& options() const { return mOptions; }
68 bool shouldBeRunning() const { return mShouldBeRunning; }
70 int run(int argc, char** argv);
71 void quit(int code);
73 virtual void postText(const char* str, size_t len);
74 virtual void postFlush(const char* str, size_t len);
75 virtual void postError(const char* str, size_t len);
76 virtual void flush();
78 protected:
79 bool parseOptions(int& argc, char**& argv, Options& opt);
80 void printUsage();
82 void interpretCmdLine(PyrSymbol* method, SC_StringBuffer& cmdLine);
83 void interpretCmdLine(PyrSymbol* method, const char* cmdLine);
84 void interpretCmdLine(PyrSymbol* method, const char *buf, size_t size);
86 void lockInput() { pthread_mutex_lock(&mInputMutex); }
87 void unlockInput() { pthread_mutex_unlock(&mInputMutex); }
89 // --------------------------------------------------------------
91 // NOTE: Subclasses should call when signalled:
92 // Must be called with input locked:
93 void interpretInput();
94 // Must be called with interpreter locked:
95 // SC_LanguageClinet::tickLocked()
97 // --------------------------------------------------------------
99 // NOTE: Subclasses should respond to following signals:
101 // Called from input thread with input locked.
102 // Calls for interpretInput() on main thread:
103 virtual void onInput();
105 // The following are called from language,
106 // locked, and may be called from any thread:
108 // Calls for tickLocked() and adjusting timing according to new schedule
109 virtual void onScheduleChanged();
110 // Language requested the application to quit
111 virtual void onQuit( int exitCode );
112 // Request to recompile the class library
113 virtual void onRecompileLibrary();
114 // See super class
115 virtual void onLibraryStartup();
117 // --------------------------------------------------------------
119 // NOTE: Subclasses should override:
120 virtual void commandLoop();
121 virtual void daemonLoop();
123 // --------------------------------------------------------------
125 static int prArgv(struct VMGlobals* g, int);
126 static int prExit(struct VMGlobals* g, int);
127 static int prScheduleChanged( struct VMGlobals *, int);
128 static int prRecompile(struct VMGlobals *, int);
130 private:
131 // NOTE: called from input thread:
132 #ifdef HAVE_READLINE
133 static void *readlineFunc(void *);
134 static int readlineRecompile(int, int);
135 static void readlineCmdLine(char *cmdLine);
136 #endif
137 static void *pipeFunc( void * );
138 void pushCmdLine( SC_StringBuffer &buf, const char *newData, size_t size );
140 void initInput();
141 void startInput();
142 void endInput();
143 void cleanupInput();
145 // helpers
146 void lockSignal() { pthread_mutex_lock(&mSignalMutex); }
147 void unlockSignal() { pthread_mutex_unlock(&mSignalMutex); }
149 bool mShouldBeRunning;
150 int mReturnCode;
151 Options mOptions;
153 // signals to main thread
154 pthread_mutex_t mSignalMutex;
155 pthread_cond_t mCond;
156 bool mInput; // there is new input
157 bool mSched; // something has been scheduled
158 bool mRecompile; // class lib recompilation requested
160 // command input
161 bool mUseReadline;
162 bool mInputShouldBeRunning;
163 SC_StringBuffer mInputBuf;
164 #ifndef _WIN32
165 int mInputCtlPipe[2];
166 #else
167 HANDLE mQuitInputEvent;
168 #endif
169 pthread_t mInputThread;
170 pthread_mutex_t mInputMutex;
171 pthread_cond_t mInputCond;
174 #endif // SC_TERMINALCLIENT_H_INCLUDED