Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / QtCollider / LanguageClient.cpp
blob93cfa2a6c07a65c2fcdf17bed078f36aee2e5fb7
1 /************************************************************************
3 * Copyright 2011 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #include "LanguageClient.h"
23 #include "QcApplication.h"
24 #include "QtCollider.h"
25 #include "QObjectProxy.h"
27 #include <PyrKernel.h>
28 #include <PyrLexer.h>
30 #include <qmath.h>
31 #include <QWidget>
33 extern double elapsedTime();
35 using namespace QtCollider;
37 QC_PUBLIC
38 int QtCollider::run(int argc, char** argv) {
39 QtCollider::LangClient app("sclang");
40 return app.run(argc, argv);
43 LangClient::LangClient( const char* name )
44 : SC_TerminalClient( name )
48 void LangClient::doInput()
50 qcDebugMsg(2, "input");
51 lockInput();
52 interpretInput();
53 QApplication::removePostedEvents( this, Event_SCRequest_Input );
54 unlockInput();
57 void LangClient::doSchedule()
59 qcDebugMsg(2, "tick");
61 double t;
62 bool next;
64 lock();
65 next = tickLocked( &t );
66 QApplication::removePostedEvents( this, Event_SCRequest_Sched );
67 unlock();
69 flush();
71 if( next ) {
72 t -= elapsedTime();
73 t *= 1000;
74 int ti = qMax(0,qCeil(t));
75 qcDebugMsg(2, QString("next at %1").arg(ti) );
76 appClockTimer.start( ti, this );
78 else {
79 appClockTimer.stop();
83 void LangClient::commandLoop()
85 doSchedule();
86 QcApplication::instance()->exec();
89 void LangClient::daemonLoop()
91 commandLoop();
94 void LangClient::sendSignal( Signal sig )
96 QtCollider::EventType type;
97 switch( sig )
99 case sig_input:
100 type = Event_SCRequest_Input; break;
101 case sig_sched:
102 type = Event_SCRequest_Sched; break;
103 case sig_recompile:
104 type = Event_SCRequest_Recompile; break;
105 case sig_stop:
106 type = Event_SCRequest_Stop; break;
107 default:
108 return;
111 QApplication::postEvent( this, new SCRequestEvent(type) );
114 void LangClient::onQuit( int exitCode )
116 QApplication::postEvent( this,
117 new SCRequestEvent( Event_SCRequest_Quit, exitCode ) );
120 void LangClient::onLibraryShutdown()
122 QWidgetList windows = QApplication::topLevelWidgets();
123 Q_FOREACH( QWidget *w, windows )
124 w->hide();
127 void LangClient::customEvent( QEvent *e )
129 int type = e->type();
130 switch( type ) {
131 case Event_SCRequest_Input:
132 doInput();
133 break;
134 case Event_SCRequest_Sched:
135 doSchedule();
136 break;
137 case Event_SCRequest_Stop:
138 stopMain();
139 break;
140 case Event_SCRequest_Quit:
142 int code = static_cast<SCRequestEvent*>(e)->data.toInt();
143 qcDebugMsg( 1, QString("Quit requested with code %1").arg(code) );
144 qApp->exit( code );
145 break;
147 case Event_SCRequest_Recompile:
148 recompileLibrary();
149 break;
150 default: ;
154 void LangClient::timerEvent( QTimerEvent *e )
156 if( e->timerId() == appClockTimer.timerId() ) doSchedule();