More error vs Error().throw cleanups
[supercollider.git] / QtCollider / LanguageClient.cpp
blob21bbd45c4a4d7680f8df6ddaf85e195ddf93ba1d
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"
26 #include <qmath.h>
28 extern double elapsedTime();
30 using namespace QtCollider;
32 QC_PUBLIC
33 int QtCollider::run(int argc, char** argv) {
34 QtCollider::LangClient app("sclang");
35 return app.run(argc, argv);
38 LangClient::LangClient( const char* name )
39 : SC_TerminalClient( name )
43 void LangClient::doInput()
45 qcDebugMsg(2, "input");
46 lockInput();
47 interpretInput();
48 QApplication::removePostedEvents( this, Event_SCRequest_Input );
49 unlockInput();
52 void LangClient::doSchedule()
54 qcDebugMsg(2, "tick");
56 double t;
57 bool next;
59 lock();
60 next = tickLocked( &t );
61 QApplication::removePostedEvents( this, Event_SCRequest_Sched );
62 unlock();
64 flush();
66 if( next ) {
67 t -= elapsedTime();
68 t *= 1000;
69 int ti = qMax(0,qCeil(t));
70 qcDebugMsg(2, QString("next at %1").arg(ti) );
71 appClockTimer.start( ti, this );
73 else {
74 appClockTimer.stop();
78 void LangClient::commandLoop()
80 doSchedule();
81 QcApplication::instance()->exec();
84 void LangClient::daemonLoop()
86 commandLoop();
89 void LangClient::sendSignal( Signal sig )
91 QtCollider::EventType type;
92 switch( sig )
94 case sig_input:
95 type = Event_SCRequest_Input; break;
96 case sig_sched:
97 type = Event_SCRequest_Sched; break;
98 case sig_recompile:
99 type = Event_SCRequest_Recompile; break;
100 case sig_stop:
101 type = Event_SCRequest_Stop; break;
102 default:
103 return;
106 QApplication::postEvent( this, new SCRequestEvent(type) );
109 void LangClient::onQuit( int exitCode )
111 QApplication::postEvent( this,
112 new SCRequestEvent( Event_SCRequest_Quit, exitCode ) );
115 void LangClient::customEvent( QEvent *e )
117 int type = e->type();
118 switch( type ) {
119 case Event_SCRequest_Input:
120 doInput();
121 break;
122 case Event_SCRequest_Sched:
123 doSchedule();
124 break;
125 case Event_SCRequest_Stop:
126 stopMain();
127 break;
128 case Event_SCRequest_Quit:
130 int code = static_cast<SCRequestEvent*>(e)->data.toInt();
131 qcDebugMsg( 1, QString("Quit requested with code %1").arg(code) );
132 qApp->exit( code );
133 break;
135 case Event_SCRequest_Recompile:
136 recompileLibrary();
137 break;
138 default: ;
142 void LangClient::timerEvent( QTimerEvent *e )
144 if( e->timerId() == appClockTimer.timerId() ) doSchedule();