summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-console/emulation_handler.cpp
blob: 2087f5762f9916a5c9c6087edcb93e5daad131ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "TEmuVt102.h"
#include "profile.h"
#include "emulation_handler.h"
#include "script.h"
#include "logger.h"

/* OPIE */
#include <qpe/config.h>

/* QT */
#include <qfile.h>
#include <qtextstream.h>

EmulationHandler::EmulationHandler( const Profile& prof, QWidget* parent,const char* name )
    : QObject(0, name )
{
    m_teWid = new TEWidget( parent, "TerminalMain");
	// use setWrapAt(0) for classic behaviour (wrap at screen width, no scrollbar)
	// use setWrapAt(80) for normal console with scrollbar
	setWrap(prof.readNumEntry("Wrap", 80) ? 0 : 80);
    m_teWid->setMinimumSize(150, 70 );
    m_script = 0;
    m_log = 0;
    parent->resize( m_teWid->calcSize(80, 24 ) );
    m_teEmu = new TEmuVt102(m_teWid );

    connect(m_teEmu,SIGNAL(ImageSizeChanged(int,int) ),
            this, SIGNAL(changeSize(int,int) ) );
    connect(m_teEmu, SIGNAL(sndBlock(const char*,int) ),
            this, SLOT(recvEmulation(const char*,int) ) );
    m_teEmu->setConnect( true );
    m_teEmu->setHistory( TRUE );
    load( prof );



}
TEmulation* EmulationHandler::emulation() {
    return m_teEmu;
}
EmulationHandler::~EmulationHandler() {
    if (isRecording())
        clearScript();
    delete m_teEmu;
    delete m_teWid;
    delete m_log;
}

void EmulationHandler::load( const Profile& prof) {

    // try to read the fontconfig from the profile
    QString aFont = prof.readEntry( "Font" );
    int aFontSize = prof.readNumEntry( "FontSize" );
    // use defaults from qpe.conf if no profile yet
    if ( ( aFontSize == -1 ) || ( aFont == QString::null ) )
    {
        Config c( "qpe" );
        c.setGroup( "Appearance" );
        aFont = c.readEntry( "FixedFontFamily", "Fixed" );
        aFontSize = c.readNumEntry( "FixedFontSize", 7 );
    }
    QFont font( aFont, aFontSize );
    font.setFixedPitch( TRUE );
    m_teWid->setVTFont( font );

    int num = prof.readNumEntry("Color", 0);
    setColor( foreColor(num), backColor(num) );
    m_teWid->setBackgroundColor(backColor(num) );

    int term = prof.readNumEntry("Terminal",  0) ;
    switch(term) {
    default:
    case Profile::VT102:
    case Profile::VT100:
        m_teEmu->setKeytrans("vt100.keytab");
        break;
    case Profile::Linux:
        m_teEmu->setKeytrans("linux.keytab");
        break;
    case Profile::XTerm:
        m_teEmu->setKeytrans("default.Keytab");
        break;
    }
}
void EmulationHandler::recv( const QByteArray& ar) {
    m_teEmu->onRcvBlock(ar.data(), ar.count() );
     	if ( isLogging() ) {
		m_log->append( ar );
	}
}

void EmulationHandler::recvEmulation(const char* src, int len ) {
    QByteArray ar(len);

    memcpy(ar.data(), src, sizeof(char) * len );
    if (isRecording())
        m_script->append(ar);
    emit send(ar);
}


QWidget* EmulationHandler::widget() {
    return m_teWid;
}
/*
 * allocate a new table of colors
 */
void EmulationHandler::setColor( const QColor& fore, const QColor& back ) {
    ColorEntry table[TABLE_COLORS];
    const ColorEntry *defaultCt = m_teWid->getdefaultColorTable();

    for (int i = 0; i < TABLE_COLORS; i++ ) {
        if ( i == 0 || i == 10 ) {
            table[i].color = fore;
        }else if ( i == 1 || i == 11 ) {
            table[i].color = back;
            table[i].transparent = 0;
        }else {
            table[i].color = defaultCt[i].color;
        }
    }
    m_teWid->setColorTable(table );
    m_teWid->update();
}
QFont EmulationHandler::font( int id ) {
    QString name;
    int size = 0;
    switch(id ) {
    default: // fall through
    case 0:
        name = QString::fromLatin1("Micro");
        size = 4;
        break;
    case 1:
        name = QString::fromLatin1("Fixed");
        size = 7;
        break;
    case 2:
        name = QString::fromLatin1("Fixed");
        size = 12;
        break;
    }
    QFont font(name, size, QFont::Normal );
    font.setFixedPitch(TRUE );
    return font;
}
QColor EmulationHandler::foreColor(int col) {
    QColor co;
    /* we need to switch it */
    switch( col ) {
    default:
    case Profile::White:
        /* color is black */
        co = Qt::white;
        break;
    case Profile::Black:
        co = Qt::black;
        break;
    case Profile::Green:
        co = Qt::green;
        break;
    case Profile::Orange:
        co.setRgb( 231, 184, 98 );
        break;
    }

    return co;
}
QColor EmulationHandler::backColor(int col ) {
  QColor co;
    /* we need to switch it */
    switch( col ) {
    default:
    case Profile::White:
        /* color is white */
        co = Qt::black;
        break;
    case Profile::Black:
        co = Qt::white;
        break;
    case Profile::Green:
        co = Qt::black;
        break;
    case Profile::Orange:
        co = Qt::black;
        break;
    }

    return co;
}

QPushButton*  EmulationHandler::cornerButton() {
    return m_teWid->cornerButton();
}


Script *EmulationHandler::script() {
    return m_script;
}

bool EmulationHandler::isRecording() {
    return (m_script != 0);
}

bool EmulationHandler::isLogging() {
	return (m_log != 0);
}

void EmulationHandler::startRecording() {
    if (!isRecording())
        m_script = new Script();
}

void EmulationHandler::startLogging(const QString fileName) {
	m_logFileName = fileName;
	if (!isLogging())
		m_log = new Logger(m_logFileName);
}

QString EmulationHandler::logFileName() {
	return m_logFileName;
}

void EmulationHandler::clearScript() {
    if (isRecording()) {
        delete m_script;
        m_script = 0;
    }
}

void EmulationHandler::clearLog() {
	if (isLogging()) {
		delete m_log;
		m_log = 0;
	}
}

void EmulationHandler::runScript(const Script *script) {
    emit send(script->script());
}

void EmulationHandler::copy() {
    m_teWid->emitSelection();
}
void EmulationHandler::paste() {
    m_teWid->pasteClipboard();
}

void EmulationHandler::setWrap(int columns) {
  m_teWid->setWrapAt(columns);
}