Debian Codegraph @

lib/Emulation.cpp

raw symbols SCIP
1 /*
2 Copyright 2007-2008 Robert Knight <[email protected]>
3 Copyright 1997,1998 by Lars Doelle <[email protected]>
4 Copyright 1996 by Matthias Ettrich <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 */
21
22 // Own
23 #include "Emulation.h"
24
25 // System
26 #include <cstdio>
27 #include <cstdlib>
28 #include <unistd.h>
29 #include <string>
30
31 // Qt
32 #include <QApplication>
33 #include <QClipboard>
34 #include <QHash>
35 #include <QKeyEvent>
36 #include <QRegExp>
37 #include <QTextStream>
38 #include <QThread>
39
40 #include <QTime>
41
42 // KDE
43 //#include <kdebug.h>
44
45 // Konsole
46 #include "KeyboardTranslator.h"
47 #include "Screen.h"
48 #include "TerminalCharacterDecoder.h"
49 #include "ScreenWindow.h"
50
51 using namespace Konsole;
52
53 Emulation::Emulation() :
54 _currentScreen(nullptr),
55 _codec(nullptr),
56 _decoder(nullptr),
57 _keyTranslator(nullptr),
58 _usesMouse(false),
59 _bracketedPasteMode(false)
60 {
61 // create screens with a default size
62 _screen[0] = new Screen(40,80);
63 _screen[1] = new Screen(40,80);
64 _currentScreen = _screen[0];
65
66 QObject::connect(&_bulkTimer1, SIGNAL(timeout()), this, SLOT(showBulk()) );
67 QObject::connect(&_bulkTimer2, SIGNAL(timeout()), this, SLOT(showBulk()) );
68
69 // listen for mouse status changes
70 connect(this , SIGNAL(programUsesMouseChanged(bool)) ,
71 SLOT(usesMouseChanged(bool)));
72 connect(this , SIGNAL(programBracketedPasteModeChanged(bool)) ,
73 SLOT(bracketedPasteModeChanged(bool)));
74
75 connect(this, &Emulation::cursorChanged, [this] (KeyboardCursorShape cursorShape, bool blinkingCursorEnabled) {
76 emit titleChanged( 50, QString(QLatin1String("CursorShape=%1;BlinkingCursorEnabled=%2"))
77 .arg(static_cast<int>(cursorShape)).arg(blinkingCursorEnabled) );
78 });
79 }
80
81 bool Emulation::programUsesMouse() const
82 {
83 return _usesMouse;
84 }
85
86 void Emulation::usesMouseChanged(bool usesMouse)
87 {
88 _usesMouse = usesMouse;
89 }
90
91 bool Emulation::programBracketedPasteMode() const
92 {
93 return _bracketedPasteMode;
94 }
95
96 void Emulation::bracketedPasteModeChanged(bool bracketedPasteMode)
97 {
98 _bracketedPasteMode = bracketedPasteMode;
99 }
100
101 ScreenWindow* Emulation::createWindow()
102 {
103 ScreenWindow* window = new ScreenWindow();
104 window->setScreen(_currentScreen);
105 _windows << window;
106
107 connect(window , SIGNAL(selectionChanged()),
108 this , SLOT(bufferedUpdate()));
109
110 connect(this , SIGNAL(outputChanged()),
111 window , SLOT(notifyOutputChanged()) );
112
113 connect(this, &Emulation::handleCommandFromKeyboard,
114 window, &ScreenWindow::handleCommandFromKeyboard);
115 connect(this, &Emulation::outputFromKeypressEvent,
116 window, &ScreenWindow::scrollToEnd);
117
118 return window;
119 }
120
121 Emulation::~Emulation()
122 {
123 QListIterator<ScreenWindow*> windowIter(_windows);
124
125 while (windowIter.hasNext())
126 {
127 delete windowIter.next();
128 }
129
130 delete _screen[0];
131 delete _screen[1];
132 delete _decoder;
133 }
134
135 void Emulation::setScreen(int n)
136 {
137 Screen *old = _currentScreen;
138 _currentScreen = _screen[n & 1];
139 if (_currentScreen != old)
140 {
141 // tell all windows onto this emulation to switch to the newly active screen
142 for(ScreenWindow* window : qAsConst(_windows))
143 window->setScreen(_currentScreen);
144 }
145 }
146
147 void Emulation::clearHistory()
148 {
149 _screen[0]->setScroll( _screen[0]->getScroll() , false );
150 }
151 void Emulation::setHistory(const HistoryType& t)
152 {
153 _screen[0]->setScroll(t);
154
155 showBulk();
156 }
157
158 const HistoryType& Emulation::history() const
159 {
160 return _screen[0]->getScroll();
161 }
162
163 void Emulation::setCodec(const QTextCodec * qtc)
164 {
165 if (qtc)
166 _codec = qtc;
167 else
168 setCodec(LocaleCodec);
169
170 delete _decoder;
171 _decoder = _codec->makeDecoder();
172
173 emit useUtf8Request(utf8());
174 }
175
176 void Emulation::setCodec(EmulationCodec codec)
177 {
178 if ( codec == Utf8Codec )
179 setCodec( QTextCodec::codecForName("utf8") );
180 else if ( codec == LocaleCodec )
181 setCodec( QTextCodec::codecForLocale() );
182 }
183
184 void Emulation::setKeyBindings(const QString& name)
185 {
186 _keyTranslator = KeyboardTranslatorManager::instance()->findTranslator(name);
187 if (!_keyTranslator)
188 {
189 _keyTranslator = KeyboardTranslatorManager::instance()->defaultTranslator();
190 }
191 }
192
193 QString Emulation::keyBindings() const
194 {
195 return _keyTranslator->name();
196 }
197
198 void Emulation::receiveChar(wchar_t c)
199 // process application unicode input to terminal
200 // this is a trivial scanner
201 {
202 c &= 0xff;
203 switch (c)
204 {
205 case '\b' : _currentScreen->backspace(); break;
206 case '\t' : _currentScreen->tab(); break;
207 case '\n' : _currentScreen->newLine(); break;
208 case '\r' : _currentScreen->toStartOfLine(); break;
209 case 0x07 : emit stateSet(NOTIFYBELL);
210 break;
211 default : _currentScreen->displayCharacter(c); break;
212 };
213 }
214
215 void Emulation::sendKeyEvent(QKeyEvent* ev, bool)
216 {
217 emit stateSet(NOTIFYNORMAL);
218
219 if (!ev->text().isEmpty())
220 { // A block of text
221 // Note that the text is proper unicode.
222 // We should do a conversion here
223 emit sendData(ev->text().toUtf8().constData(),ev->text().length());
224 }
225 }
226
227 void Emulation::sendString(const char*,int)
228 {
229 // default implementation does nothing
230 }
231
232 void Emulation::sendMouseEvent(int /*buttons*/, int /*column*/, int /*row*/, int /*eventType*/)
233 {
234 // default implementation does nothing
235 }
236
237 /*
238 We are doing code conversion from locale to unicode first.
239 TODO: Character composition from the old code. See #96536
240 */
241
242 void Emulation::receiveData(const char* text, int length)
243 {
244 emit stateSet(NOTIFYACTIVITY);
245
246 bufferedUpdate();
247
248 /* XXX: the following code involves encoding & decoding of "UTF-16
249 * surrogate pairs", which does not work with characters higher than
250 * U+10FFFF
251 * https://unicodebook.readthedocs.io/unicode_encodings.html#surrogates
252 */
253 QString utf16Text = _decoder->toUnicode(text,length);
254 std::wstring unicodeText = utf16Text.toStdWString();
255
256 //send characters to terminal emulator
257 for (size_t i=0;i<unicodeText.length();i++)
258 receiveChar(unicodeText[i]);
259
260 //look for z-modem indicator
261 //-- someone who understands more about z-modems that I do may be able to move
262 //this check into the above for loop?
263 for (int i=0;i<length;i++)
264 {
265 if (text[i] == '\030')
266 {
267 if ((length-i-1 > 3) && (strncmp(text+i+1, "B00", 3) == 0))
268 emit zmodemDetected();
269 }
270 }
271 }
272
273 //OLDER VERSION
274 //This version of onRcvBlock was commented out because
275 // a) It decoded incoming characters one-by-one, which is slow in the current version of Qt (4.2 tech preview)
276 // b) It messed up decoding of non-ASCII characters, with the result that (for example) chinese characters
277 // were not printed properly.
278 //
279 //There is something about stopping the _decoder if "we get a control code halfway a multi-byte sequence" (see below)
280 //which hasn't been ported into the newer function (above). Hopefully someone who understands this better
281 //can find an alternative way of handling the check.
282
283
284 /*void Emulation::onRcvBlock(const char *s, int len)
285 {
286 emit notifySessionState(NOTIFYACTIVITY);
287
288 bufferedUpdate();
289 for (int i = 0; i < len; i++)
290 {
291
292 QString result = _decoder->toUnicode(&s[i],1);
293 int reslen = result.length();
294
295 // If we get a control code halfway a multi-byte sequence
296 // we flush the _decoder and continue with the control code.
297 if ((s[i] < 32) && (s[i] > 0))
298 {
299 // Flush _decoder
300 while(!result.length())
301 result = _decoder->toUnicode(&s[i],1);
302 reslen = 1;
303 result.resize(reslen);
304 result[0] = QChar(s[i]);
305 }
306
307 for (int j = 0; j < reslen; j++)
308 {
309 if (result[j].characterategory() == QChar::Mark_NonSpacing)
310 _currentScreen->compose(result.mid(j,1));
311 else
312 onRcvChar(result[j].unicode());
313 }
314 if (s[i] == '\030')
315 {
316 if ((len-i-1 > 3) && (strncmp(s+i+1, "B00", 3) == 0))
317 emit zmodemDetected();
318 }
319 }
320 }*/
321
322 void Emulation::writeToStream( TerminalCharacterDecoder* _decoder ,
323 int startLine ,
324 int endLine)
325 {
326 _currentScreen->writeLinesToStream(_decoder,startLine,endLine);
327 }
328
329 void Emulation::writeToStream( TerminalCharacterDecoder* _decoder)
330 {
331 _currentScreen->writeLinesToStream(_decoder, 0, _currentScreen->getHistLines());
332 }
333
334 int Emulation::lineCount() const
335 {
336 // sum number of lines currently on _screen plus number of lines in history
337 return _currentScreen->getLines() + _currentScreen->getHistLines();
338 }
339
340 #define BULK_TIMEOUT1 10
341 #define BULK_TIMEOUT2 40
342
343 void Emulation::showBulk()
344 {
345 _bulkTimer1.stop();
346 _bulkTimer2.stop();
347
348 emit outputChanged();
349
350 _currentScreen->resetScrolledLines();
351 _currentScreen->resetDroppedLines();
352 }
353
354 void Emulation::bufferedUpdate()
355 {
356 _bulkTimer1.setSingleShot(true);
357 _bulkTimer1.start(BULK_TIMEOUT1);
358 if (!_bulkTimer2.isActive())
359 {
360 _bulkTimer2.setSingleShot(true);
361 _bulkTimer2.start(BULK_TIMEOUT2);
362 }
363 }
364
365 char Emulation::eraseChar() const
366 {
367 return '\b';
368 }
369
370 void Emulation::setImageSize(int lines, int columns)
371 {
372 if ((lines < 1) || (columns < 1))
373 return;
374
375 QSize screenSize[2] = { QSize(_screen[0]->getColumns(),
376 _screen[0]->getLines()),
377 QSize(_screen[1]->getColumns(),
378 _screen[1]->getLines()) };
379 QSize newSize(columns,lines);
380
381 if (newSize == screenSize[0] && newSize == screenSize[1])
382 return;
383
384 _screen[0]->resizeImage(lines,columns);
385 _screen[1]->resizeImage(lines,columns);
386
387 emit imageSizeChanged(lines,columns);
388
389 bufferedUpdate();
390 }
391
392 QSize Emulation::imageSize() const
393 {
394 return {_currentScreen->getColumns(), _currentScreen->getLines()};
395 }
396
397 ushort ExtendedCharTable::extendedCharHash(ushort* unicodePoints , ushort length) const
398 {
399 ushort hash = 0;
400 for ( ushort i = 0 ; i < length ; i++ )
401 {
402 hash = 31*hash + unicodePoints[i];
403 }
404 return hash;
405 }
406 bool ExtendedCharTable::extendedCharMatch(ushort hash , ushort* unicodePoints , ushort length) const
407 {
408 ushort* entry = extendedCharTable[hash];
409
410 // compare given length with stored sequence length ( given as the first ushort in the
411 // stored buffer )
412 if ( entry == nullptr || entry[0] != length )
413 return false;
414 // if the lengths match, each character must be checked. the stored buffer starts at
415 // entry[1]
416 for ( int i = 0 ; i < length ; i++ )
417 {
418 if ( entry[i+1] != unicodePoints[i] )
419 return false;
420 }
421 return true;
422 }
423 ushort ExtendedCharTable::createExtendedChar(ushort* unicodePoints , ushort length)
424 {
425 // look for this sequence of points in the table
426 ushort hash = extendedCharHash(unicodePoints,length);
427
428 // check existing entry for match
429 while ( extendedCharTable.contains(hash) )
430 {
431 if ( extendedCharMatch(hash,unicodePoints,length) )
432 {
433 // this sequence already has an entry in the table,
434 // return its hash
435 return hash;
436 }
437 else
438 {
439 // if hash is already used by another, different sequence of unicode character
440 // points then try next hash
441 hash++;
442 }
443 }
444
445
446 // add the new sequence to the table and
447 // return that index
448 ushort* buffer = new ushort[length+1];
449 buffer[0] = length;
450 for ( int i = 0 ; i < length ; i++ )
451 buffer[i+1] = unicodePoints[i];
452
453 extendedCharTable.insert(hash,buffer);
454
455 return hash;
456 }
457
458 ushort* ExtendedCharTable::lookupExtendedChar(ushort hash , ushort& length) const
459 {
460 // lookup index in table and if found, set the length
461 // argument and return a pointer to the character sequence
462
463 ushort* buffer = extendedCharTable[hash];
464 if ( buffer )
465 {
466 length = buffer[0];
467 return buffer+1;
468 }
469 else
470 {
471 length = 0;
472 return nullptr;
473 }
474 }
475
476 ExtendedCharTable::ExtendedCharTable()
477 {
478 }
479 ExtendedCharTable::~ExtendedCharTable()
480 {
481 // free all allocated character buffers
482 QHashIterator<ushort,ushort*> iter(extendedCharTable);
483 while ( iter.hasNext() )
484 {
485 iter.next();
486 delete[] iter.value();
487 }
488 }
489
490 // global instance
491 ExtendedCharTable ExtendedCharTable::instance;
492
493
494 //#include "Emulation.moc"
495