Add finer engine tracing.
[tagua/yd.git] / src / engine.cpp
blob26096b267362c3549b9cab9aaea187fa4a8d07bc
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 <iostream>
18 #include "common.h"
19 #endif
21 using namespace boost;
23 Engine::Engine(const QString& path, const QStringList& arguments)
24 : m_path(path)
25 , m_arguments(arguments)
26 ,m_side(-1) {
27 connect(&m_engine, SIGNAL(readyReadStandardOutput()), this, SLOT(processInput()));
28 connect(&m_engine, SIGNAL(started()), this, SLOT(engineRunning()));
29 connect(&m_engine, SIGNAL(finished(int, QProcess::ExitStatus)),
30 this, SIGNAL(lostConnection()));
33 Engine::~Engine() { }
35 void Engine::start() {
36 if (!m_workPath.isNull())
37 m_engine.setWorkingDirectory(m_workPath);
39 m_engine.setOutputChannelMode(KProcess::OnlyStdoutChannel);
40 m_engine.setProgram(m_path, m_arguments);
41 m_engine.start();
42 initializeEngine();
45 void Engine::engineRunning() {
46 Q_ASSERT(m_engine.state() == QProcess::Running);
47 while (!m_command_queue.empty()) {
48 sendCommand(m_command_queue.front(), false);
49 m_command_queue.pop();
53 void Engine::sendCommand(const QString& command, bool echo) {
54 if (echo && m_console)
55 m_console->echo(command);
57 if (m_engine.state() == QProcess::Running) {
58 QTextStream os(&m_engine);
59 os << command << "\n";
60 #ifdef ENGINE_DEBUG
61 if (m_side == -1)
62 std::cout << "?";
63 else
64 std::cout << m_side;
65 std::cout << "> " << command << std::endl;
66 #endif
68 else {
69 m_command_queue.push(command);
73 void Engine::processInput() {
74 // process only full lines
75 while (m_engine.canReadLine()) {
76 QString line = m_engine.readLine();
77 line.remove("\n").remove("\r");
78 #ifdef ENGINE_DEBUG
79 if (m_side == -1)
80 std::cout << "?";
81 else
82 std::cout << m_side;
83 std::cout << "< " << line << std::endl;
84 #endif
85 if (m_console)
86 m_console->displayText(line + "\n", 0);
87 receivedCommand(line);
91 void Engine::textNotify(const QString& text) {
92 sendCommand(text, false);
96 void Engine::setNotifier(const shared_ptr<EngineNotifier>& notifier) {
97 m_notifier = notifier;
100 void Engine::setConsole(const shared_ptr<Console>& console) {
101 m_console = console;