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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
/*
Copyright 2007-2008 Robert Knight <robertknight@gmail.com>
Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
Copyright 1996 by Matthias Ettrich <ettrich@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Own
#include "Emulation.h"
// System
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <string>
// Qt
#include <QApplication>
#include <QClipboard>
#include <QHash>
#include <QKeyEvent>
#include <QRegExp>
#include <QTextStream>
#include <QThread>
#include <QList>
#include <QTime>
#include <QDebug>
// KDE
//#include <kdebug.h>
// Konsole
#include "KeyboardTranslator.h"
#include "Screen.h"
#include "TerminalCharacterDecoder.h"
#include "ScreenWindow.h"
#include "Session.h"
#include "SessionManager.h"
#include "TerminalDisplay.h"
#include "encodes/detectcode.h"
using namespace Konsole;
Emulation::Emulation() :
_currentScreen(nullptr),
_codec(nullptr),
_decoder(nullptr),
_keyTranslator(nullptr),
_usesMouse(false),
_alternateScrolling(true),
_bracketedPasteMode(false)
{
// create screens with a default size
_screen[0] = new Screen(40, 80);
_screen[1] = new Screen(40, 80);
_currentScreen = _screen[0];
QObject::connect(&_bulkTimer1, SIGNAL(timeout()), this, SLOT(showBulk()));
QObject::connect(&_bulkTimer2, SIGNAL(timeout()), this, SLOT(showBulk()));
// listen for mouse status changes
connect(this, SIGNAL(programUsesMouseChanged(bool)),
SLOT(usesMouseChanged(bool)));
connect(this, SIGNAL(programBracketedPasteModeChanged(bool)),
SLOT(bracketedPasteModeChanged(bool)));
connect(this, &Emulation::cursorChanged, [this](KeyboardCursorShape cursorShape, bool blinkingCursorEnabled) {
emit titleChanged(50, QString(QLatin1String("CursorShape=%1;BlinkingCursorEnabled=%2"))
.arg(static_cast<int>(cursorShape)).arg(blinkingCursorEnabled));
});
/******** Add by ut001000 renfeixiang 2020-07-16:增加初始化保存开始的屏幕行列数 Begin***************/
// _lastcol = _currentScreen->getColumns();
// _lastline = _currentScreen->getLines();
/******** Add by ut001000 renfeixiang 2020-07-16:增加 End***************/
}
bool Emulation::programUsesMouse() const
{
return _usesMouse;
}
void Emulation::usesMouseChanged(bool usesMouse)
{
_usesMouse = usesMouse;
}
void Emulation::setAlternateScrolling(bool enable)
{
_alternateScrolling = enable;
}
bool Emulation::programBracketedPasteMode() const
{
return _bracketedPasteMode;
}
void Emulation::bracketedPasteModeChanged(bool bracketedPasteMode)
{
_bracketedPasteMode = bracketedPasteMode;
}
ScreenWindow *Emulation::createWindow()
{
ScreenWindow *window = new ScreenWindow();
window->setScreen(_currentScreen);
_windows << window;
connect(window, SIGNAL(selectionChanged()),
this, SLOT(bufferedUpdate()));
connect(this, SIGNAL(outputChanged()),
window, SLOT(notifyOutputChanged()));
return window;
}
void Emulation::checkScreenInUse()
{
emit primaryScreenInUse(_currentScreen == _screen[0]);
}
Emulation::~Emulation()
{
QListIterator<ScreenWindow *> windowIter(_windows);
while (windowIter.hasNext()) {
delete windowIter.next();
}
delete _screen[0];
delete _screen[1];
delete _decoder;
if (nullptr != _keyTranslator) {
delete _keyTranslator;
_keyTranslator = nullptr;
}
}
void Emulation::setScreen(int n)
{
Screen *old = _currentScreen;
_currentScreen = _screen[n & 1];
if (_currentScreen != old) {
// tell all windows onto this emulation to switch to the newly active screen
for (ScreenWindow *window : qAsConst(_windows))
window->setScreen(_currentScreen);
checkScreenInUse();
}
}
void Emulation::clearHistory()
{
_screen[0]->setScroll(_screen[0]->getScroll(), false);
}
void Emulation::setHistory(const HistoryType &t)
{
_screen[0]->setScroll(t);
showBulk();
}
const HistoryType &Emulation::history() const
{
return _screen[0]->getScroll();
}
void Emulation::setCodec(const QTextCodec *qtc)
{
if (qtc)
_codec = qtc;
else
setCodec(LocaleCodec);
delete _decoder;
_decoder = _codec->makeDecoder();
emit useUtf8Request(utf8());
}
void Emulation::setCodec(EmulationCodec codec)
{
if (codec == Utf8Codec)
setCodec(QTextCodec::codecForName("utf8"));
else if (codec == LocaleCodec)
setCodec(QTextCodec::codecForLocale());
}
void Emulation::setSessionId(int sessionId)
{
_sessionId = sessionId;
}
void Emulation::setBackspaceMode(char *key, int length)
{
KeyboardTranslator::Entry entry = _keyTranslator->findEntry(
Qt::Key_Backspace,
Qt::NoModifier,
KeyboardTranslator::NoState);
KeyboardTranslator::Entry newEntry;
KeyboardTranslator::States flags = KeyboardTranslator::NoState;
KeyboardTranslator::States flagMask = KeyboardTranslator::NoState;
Qt::KeyboardModifiers modifiers = Qt::NoModifier;
Qt::KeyboardModifiers modifierMask = Qt::NoModifier;
KeyboardTranslator::Command command = KeyboardTranslator::NoCommand;
newEntry.setKeyCode(Qt::Key_Backspace);
newEntry.setState(flags);
newEntry.setStateMask(flagMask);
newEntry.setModifiers(modifiers);
newEntry.setModifierMask(modifierMask);
newEntry.setText(QByteArray(key, length));
newEntry.setCommand(command);
_keyTranslator->replaceEntry(entry, newEntry);
}
void Emulation::setDeleteMode(char *key, int length)
{
KeyboardTranslator::Entry entry = _keyTranslator->findEntry(
Qt::Key_Delete,
Qt::NoModifier,
KeyboardTranslator::NoState);
KeyboardTranslator::Entry newEntry;
KeyboardTranslator::States flags = KeyboardTranslator::NoState;
KeyboardTranslator::States flagMask = KeyboardTranslator::NoState;
Qt::KeyboardModifiers modifiers = Qt::NoModifier;
Qt::KeyboardModifiers modifierMask = Qt::NoModifier;
KeyboardTranslator::Command command = KeyboardTranslator::NoCommand;
newEntry.setKeyCode(Qt::Key_Delete);
newEntry.setState(flags);
newEntry.setStateMask(flagMask);
newEntry.setModifiers(modifiers);
newEntry.setModifierMask(modifierMask);
newEntry.setText(QByteArray(key, length));
newEntry.setCommand(command);
_keyTranslator->replaceEntry(entry, newEntry);
}
void Emulation::setKeyBindings(const QString &name)
{
_keyTranslator = KeyboardTranslatorManager::instance()->getTranslator(name);
}
QString Emulation::keyBindings() const
{
return _keyTranslator->name();
}
void Emulation::receiveChar(wchar_t c)
// process application unicode input to terminal
// this is a trivial scanner
{
c &= 0xff;
switch (c) {
case '\b' : _currentScreen->backspace(); break;
case '\t' : _currentScreen->tab(); break;
case '\n' : _currentScreen->newLine(); break;
case '\r' : _currentScreen->toStartOfLine(); break;
case 0x07 : emit stateSet(NOTIFYBELL);
break;
default : _currentScreen->displayCharacter(c); break;
};
}
void Emulation::sendKeyEvent(QKeyEvent *ev)
{
emit stateSet(NOTIFYNORMAL);
if (!ev->text().isEmpty()) {
// A block of text
// Note that the text is proper unicode.
// We should do a conversion here
emit sendData(ev->text().toUtf8().constData(), ev->text().length(), _codec);
}
}
void Emulation::sendString(const char *, int)
{
// default implementation does nothing
}
void Emulation::sendMouseEvent(int /*buttons*/, int /*column*/, int /*row*/, int /*eventType*/)
{
// default implementation does nothing
}
static int isUse2005Standard = -1;
/**
@brief 检测当前iconv使用的GB18030编码是否为2005标准,2005标准强制使用上层补丁版本
通过检测2005和2022编码转的差异,以附录D中的编码为例验证
2005标准 0xFE51 --> \u20087
2022标准 0xFE51 --> \uE816
@return iconv使用GB18030编码是否为2005标准,默认返回true
*/
bool Emulation::detectIconvUse2005Standard()
{
iconv_t handle = iconv_open("UTF-8", "GB18030");
if (handle == reinterpret_cast<iconv_t>(-1)) {
return true;
}
QByteArray input("\xFE\x51");
QByteArray output(input.size() * 2, 0);
char *inputData = input.data();
char *outputData = output.data();
size_t inputLen = static_cast<size_t>(input.count());
size_t outputLen = static_cast<size_t>(output.count());
const size_t ret = iconv(handle, &inputData, &inputLen, &outputData, &outputLen);
iconv_close(handle);
if (ret == static_cast<size_t>(-1)) {
return true;
}
if (!output.contains("\uE816")) {
qInfo() << "Current iconv gb18030 standard is 2005.";
return true;
}
qInfo() << "Current iconv gb18030 standard is 2022.";
return false;
}
/*
We are doing code conversion from locale to unicode first.
TODO: Character composition from the old code. See #96536
*/
void Emulation::receiveData(const char *text, int length, bool isCommandExec)
{
emit stateSet(NOTIFYACTIVITY);
bufferedUpdate();
/* XXX: the following code involves encoding & decoding of "UTF-16
* surrogate pairs", which does not work with characters higher than
* U+10FFFF
* https://unicodebook.readthedocs.io/unicode_encodings.html#surrogates
*/
QString utf16Text = "";
if (QString(_codec->name()).toUpper().startsWith("GB") && !isCommandExec) {
if (_decoder != nullptr) {
delete _decoder;
_decoder = nullptr;
}
QTextCodec *textCodec = QTextCodec::codecForName("UTF-8");
_decoder = textCodec->makeDecoder();
utf16Text = _decoder->toUnicode(text, length);
QTextCodec* gbk = QTextCodec::codecForName(_codec->name());
QByteArray gbkarr = gbk->fromUnicode(utf16Text);
if (_decoder != nullptr) {
delete _decoder;
_decoder = nullptr;
}
textCodec = QTextCodec::codecForName(_codec->name());
_decoder = textCodec->makeDecoder();
utf16Text = _decoder->toUnicode(gbkarr);
//setIsCodecGB18030(false);
}
else {
if(isUse2005Standard == -1){
isUse2005Standard = detectIconvUse2005Standard();
qInfo() << "Is Used 2005 standard's gb18030 iconv?" << isUse2005Standard;
}
if(_codec->name().toUpper().contains("GB18030") && isUse2005Standard == 1) {
//setIsCodecGB18030(true);
QByteArray gbkarr(text, length);
QByteArray Outdata;
DetectCode::ChangeFileEncodingFormat(gbkarr, Outdata, QString("GB18030"), QString("UTF-8"));
utf16Text = QString(Outdata);
} else {
utf16Text = _decoder->toUnicode(text, length);
//setIsCodecGB18030(false);
}
}
//fix bug 67102 打开超长名称的文件夹,终端界面光标位置不在最后一位
//bash 提示符很长的情况下,会有较大概率以五个\b字符结尾,导致光标错位
if (utf16Text.startsWith("\u001B]0;") && utf16Text.endsWith("\b\b\b\b\b")) {
Session *currSession = SessionManager::instance()->idToSession(_sessionId);
if (currSession && (QStringLiteral("bash") == currSession->foregroundProcessName())) {
utf16Text.replace("\b\b\b\b\b", "");
}
}
std::wstring unicodeText = utf16Text.toStdWString();
//send characters to terminal emulator
for (size_t i = 0; i < unicodeText.length(); i++)
receiveChar(unicodeText[i]);
//look for z-modem indicator
//-- someone who understands more about z-modems that I do may be able to move
//this check into the above for loop?
for (int i = 0; i < length; i++) {
if (text[i] == '\030') {
if ((length - i - 1 > 3) && (strncmp(text + i + 1, "B00", 3) == 0))
emit zmodemDetected();
}
}
}
//OLDER VERSION
//This version of onRcvBlock was commented out because
// a) It decoded incoming characters one-by-one, which is slow in the current version of Qt (4.2 tech preview)
// b) It messed up decoding of non-ASCII characters, with the result that (for example) chinese characters
// were not printed properly.
//
//There is something about stopping the _decoder if "we get a control code halfway a multi-byte sequence" (see below)
//which hasn't been ported into the newer function (above). Hopefully someone who understands this better
//can find an alternative way of handling the check.
/*void Emulation::onRcvBlock(const char *s, int len)
{
emit notifySessionState(NOTIFYACTIVITY);
bufferedUpdate();
for (int i = 0; i < len; i++)
{
QString result = _decoder->toUnicode(&s[i],1);
int reslen = result.length();
// If we get a control code halfway a multi-byte sequence
// we flush the _decoder and continue with the control code.
if ((s[i] < 32) && (s[i] > 0))
{
// Flush _decoder
while(!result.length())
result = _decoder->toUnicode(&s[i],1);
reslen = 1;
result.resize(reslen);
result[0] = QChar(s[i]);
}
for (int j = 0; j < reslen; j++)
{
if (result[j].characterategory() == QChar::Mark_NonSpacing)
_currentScreen->compose(result.mid(j,1));
else
onRcvChar(result[j].unicode());
}
if (s[i] == '\030')
{
if ((len-i-1 > 3) && (strncmp(s+i+1, "B00", 3) == 0))
emit zmodemDetected();
}
}
}*/
void Emulation::writeToStream(TerminalCharacterDecoder *_decoder,
int startLine,
int endLine)
{
_currentScreen->writeLinesToStream(_decoder, startLine, endLine);
}
int Emulation::lineCount() const
{
// sum number of lines currently on _screen plus number of lines in history
return _currentScreen->getLines() + _currentScreen->getHistLines();
}
int Emulation::columnCount() const
{
return _currentScreen->getColumns();
}
#define BULK_TIMEOUT1 10
#define BULK_TIMEOUT2 40
void Emulation::showBulk()
{
_bulkTimer1.stop();
_bulkTimer2.stop();
emit outputChanged();
_currentScreen->resetScrolledLines();
_currentScreen->resetDroppedLines();
}
void Emulation::bufferedUpdate()
{
_bulkTimer1.setSingleShot(true);
_bulkTimer1.start(BULK_TIMEOUT1);
if (!_bulkTimer2.isActive()) {
_bulkTimer2.setSingleShot(true);
_bulkTimer2.start(BULK_TIMEOUT2);
}
}
char Emulation::eraseChar() const
{
return '\b';
}
void Emulation::setImageSize(int lines, int columns)
{
if ((lines < 1) || (columns < 1))
return;
QSize screenSize[2] = { QSize(_screen[0]->getColumns(),
_screen[0]->getLines()),
QSize(_screen[1]->getColumns(),
_screen[1]->getLines())
};
QSize newSize(columns, lines);
if (newSize == screenSize[0] && newSize == screenSize[1])
return;
_screen[0]->resizeImage(lines, columns);
_screen[1]->resizeImage(lines, columns);
emit imageSizeChanged(lines, columns);
bufferedUpdate();
}
QSize Emulation::imageSize() const
{
return {_currentScreen->getColumns(), _currentScreen->getLines()};
}
uint ExtendedCharTable::extendedCharHash(uint *unicodePoints, ushort length) const
{
uint hash = 0;
for (ushort i = 0 ; i < length ; i++) {
hash = 31 * hash + unicodePoints[i];
}
return hash;
}
bool ExtendedCharTable::extendedCharMatch(uint hash, uint *unicodePoints, ushort length) const
{
uint *entry = extendedCharTable[hash];
// compare given length with stored sequence length ( given as the first ushort in the
// stored buffer )
if (entry == nullptr || entry[0] != length)
return false;
// if the lengths match, each character must be checked. the stored buffer starts at
// entry[1]
for (int i = 0 ; i < length ; i++) {
if (entry[i + 1] != unicodePoints[i])
return false;
}
return true;
}
uint ExtendedCharTable::createExtendedChar(uint *unicodePoints, ushort length)
{
// look for this sequence of points in the table
uint hash = extendedCharHash(unicodePoints, length);
const uint initialHash = hash;
bool triedCleaningSolution = false;
// check existing entry for match
while (extendedCharTable.contains(hash) && hash != 0) { // 0 has a special meaning for chars so we don't use it
if (extendedCharMatch(hash, unicodePoints, length)) {
// this sequence already has an entry in the table,
// return its hash
return hash;
}
// if hash is already used by another, different sequence of unicode character
// points then try next hash
hash++;
if (hash == initialHash) {
if (!triedCleaningSolution) {
triedCleaningSolution = true;
// All the hashes are full, go to all Screens and try to free any
// This is slow but should happen very rarely
QSet<uint> usedExtendedChars;
const QList<Session *> sessionsList = SessionManager::instance()->sessions();
for (const Session *s : sessionsList) {
const QList<TerminalDisplay *> displayList = s->views();
for (const TerminalDisplay *display : displayList) {
usedExtendedChars += display->screenWindow()->screen()->usedExtendedChars();
}
}
QHash<uint, uint *>::iterator it = extendedCharTable.begin();
QHash<uint, uint *>::iterator itEnd = extendedCharTable.end();
while (it != itEnd) {
if (usedExtendedChars.contains(it.key())) {
++it;
} else {
it = extendedCharTable.erase(it);
}
}
} else {
qDebug() << "Using all the extended char hashes, going to miss this extended character";
return 0;
}
}
}
// add the new sequence to the table and
// return that index
auto buffer = new uint[length + 1];
buffer[0] = length;
for (int i = 0; i < length; i++) {
buffer[i + 1] = unicodePoints[i];
}
extendedCharTable.insert(hash, buffer);
return hash;
}
uint *ExtendedCharTable::lookupExtendedChar(uint hash, ushort &length) const
{
// lookup index in table and if found, set the length
// argument and return a pointer to the character sequence
uint *buffer = extendedCharTable[hash];
if (buffer != nullptr) {
length = ushort(buffer[0]);
return buffer + 1;
}
length = 0;
return nullptr;
}
ExtendedCharTable::ExtendedCharTable()
{
}
ExtendedCharTable::~ExtendedCharTable()
{
// free all allocated character buffers
QHashIterator<uint, uint *> iter(extendedCharTable);
while (iter.hasNext()) {
iter.next();
delete[] iter.value();
}
}
// global instance
ExtendedCharTable ExtendedCharTable::instance;
//#include "Emulation.moc"
|