qtcollider: QcGraph - implement quadratic and cubic envelope shapes
[supercollider.git] / include / lang / SC_TerminalClient.h
blobadc2ce7f19a559b0aa1da8c05b715d7589b2b0e3
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 enum Signal
48 sig_input = 0x01, // there is new input
49 sig_sched = 0x02, // something has been scheduled
50 sig_recompile = 0x04, // class lib recompilation requested
51 sig_stop = 0x08 // call Main:-stop
54 struct Options : public SC_LanguageClient::Options
56 Options()
57 : mLibraryConfigFile(0),
58 mDaemon(false),
59 mCallRun(false),
60 mCallStop(false),
61 mArgc(0), mArgv(0)
62 { }
64 const char* mLibraryConfigFile;
65 bool mDaemon;
66 bool mCallRun;
67 bool mCallStop;
68 int mArgc;
69 char** mArgv;
72 SC_TerminalClient(const char* name);
73 virtual ~SC_TerminalClient();
75 const Options& options() const { return mOptions; }
76 bool shouldBeRunning() const { return mShouldBeRunning; }
78 int run(int argc, char** argv);
79 void quit(int code);
81 virtual void postText(const char* str, size_t len);
82 virtual void postFlush(const char* str, size_t len);
83 virtual void postError(const char* str, size_t len);
84 virtual void flush();
86 // Requests an action to be taken on the main thread.
87 // NOTE: It may be called from any thread, and with interpreter locked.
88 virtual void sendSignal( Signal code );
90 protected:
91 bool parseOptions(int& argc, char**& argv, Options& opt);
92 void printUsage();
94 void interpretCmdLine(PyrSymbol* method, SC_StringBuffer& cmdLine);
95 void interpretCmdLine(PyrSymbol* method, const char* cmdLine);
96 void interpretCmdLine(PyrSymbol* method, const char *buf, size_t size);
98 void lockInput() { pthread_mutex_lock(&mInputMutex); }
99 void unlockInput() { pthread_mutex_unlock(&mInputMutex); }
101 // --------------------------------------------------------------
103 // NOTE: Subclasses should call from main thread
104 // after receiving sig_input, and with input locked:
105 void interpretInput();
107 // --------------------------------------------------------------
109 // Language requested the application to quit
110 // NOTE: It may be called from any thread, and with interpreter locked.
111 virtual void onQuit( int exitCode );
113 // See super class
114 virtual void onLibraryStartup();
116 // --------------------------------------------------------------
118 // NOTE: Subclasses should override:
119 virtual void commandLoop();
120 virtual void daemonLoop();
122 // --------------------------------------------------------------
124 static int prArgv(struct VMGlobals* g, int);
125 static int prExit(struct VMGlobals* g, int);
126 static int prScheduleChanged( struct VMGlobals *, int);
127 static int prRecompile(struct VMGlobals *, int);
129 private:
130 // NOTE: called from input thread:
131 #ifdef HAVE_READLINE
132 static void readlineInit();
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 int mSignals;
158 // command input
159 bool mUseReadline;
160 bool mInputShouldBeRunning;
161 SC_StringBuffer mInputBuf;
162 #ifndef _WIN32
163 int mInputCtlPipe[2];
164 #else
165 HANDLE mQuitInputEvent;
166 #endif
167 pthread_t mInputThread;
168 pthread_mutex_t mInputMutex;
169 pthread_cond_t mInputCond;
172 #endif // SC_TERMINALCLIENT_H_INCLUDED