Removed AlgebraicNotation from the variant API.
[tagua/yd.git] / src / engine.cpp
blobba9ab231d00d3ce0558b6f0ba9953cda6338cf8e
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 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 std::cout << "> " << command << std::endl;
61 #endif
63 else {
64 m_command_queue.push(command);
68 void Engine::processInput() {
69 // process only full lines
70 while (m_engine.canReadLine()) {
71 QString line = m_engine.readLine();
72 line.remove("\n").remove("\r");
73 #ifdef ENGINE_DEBUG
74 std::cout << "< " << line << std::endl;
75 #endif
76 if (m_console)
77 m_console->displayText(line + "\n", 0);
78 receivedCommand(line);
82 void Engine::textNotify(const QString& text) {
83 sendCommand(text, false);
87 void Engine::setNotifier(const shared_ptr<EngineNotifier>& notifier) {
88 m_notifier = notifier;
91 void Engine::setConsole(const shared_ptr<Console>& console) {
92 m_console = console;