Replaced all std::cout with kDebug.
[tagua/yd.git] / src / engine.cpp
blob59bdad5da18cd2b62d101cf790a28ced14d37ed1
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include "engine.h"
13 #include <QTextStream>
15 #define ENGINE_DEBUG
16 #ifdef ENGINE_DEBUG
17 #include <KDebug>
18 #endif
20 using namespace boost;
22 Engine::Engine(const QString& path, const QStringList& arguments)
23 : m_path(path)
24 , m_arguments(arguments)
25 ,m_side(-1) {
26 connect(&m_engine, SIGNAL(readyReadStandardOutput()), this, SLOT(processInput()));
27 connect(&m_engine, SIGNAL(started()), this, SLOT(engineRunning()));
28 connect(&m_engine, SIGNAL(finished(int, QProcess::ExitStatus)),
29 this, SIGNAL(lostConnection()));
32 Engine::~Engine() { }
34 void Engine::start() {
35 if (!m_workPath.isNull())
36 m_engine.setWorkingDirectory(m_workPath);
38 m_engine.setOutputChannelMode(KProcess::OnlyStdoutChannel);
39 m_engine.setProgram(m_path, m_arguments);
40 m_engine.start();
41 initializeEngine();
44 void Engine::engineRunning() {
45 Q_ASSERT(m_engine.state() == QProcess::Running);
46 while (!m_command_queue.empty()) {
47 sendCommand(m_command_queue.front(), false);
48 m_command_queue.pop();
52 void Engine::sendCommand(const QString& command, bool echo) {
53 if (echo && m_console)
54 m_console->echo(command);
56 if (m_engine.state() == QProcess::Running) {
57 QTextStream os(&m_engine);
58 os << command << "\n";
59 #ifdef ENGINE_DEBUG
60 if (m_side == -1)
61 kDebug() << "?";
62 else
63 kDebug() << m_side;
64 kDebug() << "> " << command;
65 #endif
67 else {
68 m_command_queue.push(command);
72 void Engine::processInput() {
73 // process only full lines
74 while (m_engine.canReadLine()) {
75 QString line = m_engine.readLine();
76 line.remove("\n").remove("\r");
77 #ifdef ENGINE_DEBUG
78 if (m_side == -1)
79 kDebug() << "?";
80 else
81 kDebug() << m_side;
82 kDebug() << "< " << line;
83 #endif
84 if (m_console)
85 m_console->displayText(line + "\n", 0);
86 receivedCommand(line);
90 void Engine::textNotify(const QString& text) {
91 sendCommand(text, false);
95 void Engine::setNotifier(const shared_ptr<EngineNotifier>& notifier) {
96 m_notifier = notifier;
99 void Engine::setConsole(const shared_ptr<Console>& console) {
100 m_console = console;