| 1 |
2 |
sergeykhbr |
#include "ConsoleWidget.h"
|
| 2 |
|
|
#include "moc_ConsoleWidget.h"
|
| 3 |
|
|
|
| 4 |
|
|
#include <QtCore/QDate>
|
| 5 |
|
|
#include <QtGui/QPainter>
|
| 6 |
|
|
#include <QtWidgets/QScrollBar>
|
| 7 |
|
|
#include <memory>
|
| 8 |
|
|
|
| 9 |
|
|
namespace debugger {
|
| 10 |
|
|
|
| 11 |
|
|
static const char CONSOLE_ENTRY[] = "riscv# ";
|
| 12 |
|
|
|
| 13 |
|
|
ConsoleWidget::ConsoleWidget(IGui *igui, QWidget *parent)
|
| 14 |
|
|
: QPlainTextEdit(parent) {
|
| 15 |
|
|
igui_ = igui;
|
| 16 |
|
|
|
| 17 |
|
|
RISCV_mutex_init(&mutexOutput_);
|
| 18 |
|
|
sizeConv_ = 1024;
|
| 19 |
|
|
wcsConv_ = new wchar_t[sizeConv_];
|
| 20 |
|
|
mbsConv_ = new char[sizeConv_];
|
| 21 |
|
|
|
| 22 |
|
|
clear();
|
| 23 |
|
|
fontMainText_ = QFont("Courier");
|
| 24 |
|
|
fontMainText_.setStyleHint(QFont::Monospace);
|
| 25 |
|
|
fontMainText_.setPointSize(9);
|
| 26 |
|
|
fontMainText_.setFixedPitch(true);
|
| 27 |
|
|
setFont(fontMainText_);
|
| 28 |
|
|
|
| 29 |
|
|
fontRISCV_ = fontMainText_;
|
| 30 |
|
|
fontRISCV_.setBold(true);
|
| 31 |
|
|
|
| 32 |
|
|
ensureCursorVisible();
|
| 33 |
|
|
|
| 34 |
|
|
|
| 35 |
|
|
QTextCursor cursor = textCursor();
|
| 36 |
|
|
QTextCharFormat charFormat = cursor.charFormat();
|
| 37 |
|
|
charFormat.setFont(fontRISCV_);
|
| 38 |
|
|
cursor.setCharFormat(charFormat);
|
| 39 |
|
|
cursor.insertText(tr(CONSOLE_ENTRY));
|
| 40 |
|
|
cursorMinPos_ = cursor.selectionStart();
|
| 41 |
|
|
|
| 42 |
|
|
charFormat.setFont(fontMainText_);
|
| 43 |
|
|
cursor.setCharFormat(charFormat);
|
| 44 |
|
|
setTextCursor(cursor);
|
| 45 |
|
|
setWindowTitle(tr("simconsole"));
|
| 46 |
|
|
|
| 47 |
|
|
cursorPos_.make_list(2);
|
| 48 |
|
|
cursorPos_[0u].make_int64(0);
|
| 49 |
|
|
cursorPos_[1].make_int64(0);
|
| 50 |
|
|
|
| 51 |
|
|
connect(this, SIGNAL(signalNewData()), this, SLOT(slotUpdateByData()));
|
| 52 |
|
|
}
|
| 53 |
|
|
|
| 54 |
|
|
ConsoleWidget::~ConsoleWidget() {
|
| 55 |
|
|
igui_->removeFromQueue(static_cast<IGuiCmdHandler *>(this));
|
| 56 |
|
|
RISCV_remove_default_output(static_cast<IRawListener *>(this));
|
| 57 |
|
|
RISCV_mutex_destroy(&mutexOutput_);
|
| 58 |
|
|
delete [] wcsConv_;
|
| 59 |
|
|
delete [] mbsConv_;
|
| 60 |
|
|
}
|
| 61 |
|
|
|
| 62 |
|
|
void ConsoleWidget::handleResponse(AttributeType *req, AttributeType *resp) {
|
| 63 |
|
|
if (resp->is_nil() || resp->is_invalid()) {
|
| 64 |
|
|
return;
|
| 65 |
|
|
}
|
| 66 |
|
|
RISCV_mutex_lock(&mutexOutput_);
|
| 67 |
|
|
strOutput_ += QString(resp->to_config()) + "\n";
|
| 68 |
|
|
RISCV_mutex_unlock(&mutexOutput_);
|
| 69 |
|
|
emit signalNewData();
|
| 70 |
|
|
}
|
| 71 |
|
|
|
| 72 |
|
|
void ConsoleWidget::keyPressEvent(QKeyEvent *e) {
|
| 73 |
|
|
AttributeType cmd;
|
| 74 |
|
|
QTextCursor cursor = textCursor();
|
| 75 |
|
|
uint32_t vt_key = static_cast<uint32_t>(e->nativeVirtualKey());
|
| 76 |
|
|
char vt_char = static_cast<char>(vt_key);
|
| 77 |
|
|
if (vt_char >= 'A' && vt_char <= 'Z' && e->modifiers() == Qt::NoModifier) {
|
| 78 |
|
|
vt_key -= static_cast<uint32_t>('A');
|
| 79 |
|
|
vt_key += static_cast<uint32_t>('a');
|
| 80 |
|
|
}
|
| 81 |
|
|
//printf("vt_key = %08x\n", vt_key);
|
| 82 |
|
|
bool cmd_ready = iauto_->processKey(vt_key, &cmd, &cursorPos_);
|
| 83 |
|
|
|
| 84 |
|
|
moveCursor(QTextCursor::End);
|
| 85 |
|
|
cursor = textCursor();
|
| 86 |
|
|
cursor.setPosition(cursorMinPos_, QTextCursor::KeepAnchor);
|
| 87 |
|
|
cursor.insertText(cmd.to_string());
|
| 88 |
|
|
if (cursorPos_[0u].to_int()) {
|
| 89 |
|
|
cursor.movePosition(QTextCursor::Left,
|
| 90 |
|
|
QTextCursor::MoveAnchor, cursorPos_[0u].to_int());
|
| 91 |
|
|
setTextCursor(cursor);
|
| 92 |
|
|
}
|
| 93 |
|
|
|
| 94 |
|
|
if (!cmd_ready) {
|
| 95 |
|
|
return;
|
| 96 |
|
|
}
|
| 97 |
|
|
cursor.movePosition(QTextCursor::End);
|
| 98 |
|
|
cursor.insertText(tr("\r"));
|
| 99 |
|
|
|
| 100 |
|
|
QTextCharFormat charFormat = cursor.charFormat();
|
| 101 |
|
|
cursor.insertText(tr(CONSOLE_ENTRY));
|
| 102 |
|
|
cursorMinPos_ = cursor.selectionStart();
|
| 103 |
|
|
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
|
| 104 |
|
|
|
| 105 |
|
|
igui_->registerCommand(
|
| 106 |
|
|
static_cast<IGuiCmdHandler *>(this), &cmd, false);
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
void ConsoleWidget::slotPostInit(AttributeType *cfg) {
|
| 110 |
|
|
const char *autoobj = (*cfg)["AutoComplete"].to_string();
|
| 111 |
|
|
iauto_ = static_cast<IAutoComplete *>(
|
| 112 |
|
|
RISCV_get_service_iface(autoobj, IFACE_AUTO_COMPLETE));
|
| 113 |
|
|
|
| 114 |
|
|
RISCV_add_default_output(static_cast<IRawListener *>(this));
|
| 115 |
|
|
}
|
| 116 |
|
|
|
| 117 |
|
|
void ConsoleWidget::slotUpdateByData() {
|
| 118 |
|
|
if (strOutput_.size() == 0) {
|
| 119 |
|
|
return;
|
| 120 |
|
|
}
|
| 121 |
|
|
// Keep current line value:
|
| 122 |
|
|
QTextCursor cursor = textCursor();
|
| 123 |
|
|
cursor.movePosition(QTextCursor::End);
|
| 124 |
|
|
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
|
| 125 |
|
|
QString cur_line = cursor.selectedText();
|
| 126 |
|
|
// Insert raw string:
|
| 127 |
|
|
RISCV_mutex_lock(&mutexOutput_);
|
| 128 |
|
|
cursor.insertText(strOutput_);
|
| 129 |
|
|
cursorMinPos_ += strOutput_.size();
|
| 130 |
|
|
strOutput_.clear();
|
| 131 |
|
|
RISCV_mutex_unlock(&mutexOutput_);
|
| 132 |
|
|
|
| 133 |
|
|
// Restore line:
|
| 134 |
|
|
cursor.movePosition(QTextCursor::End);
|
| 135 |
|
|
cursor.insertText(cur_line);
|
| 136 |
|
|
// Restore cursor position:
|
| 137 |
|
|
cursor.movePosition(QTextCursor::End);
|
| 138 |
|
|
cursor.movePosition(QTextCursor::Left,
|
| 139 |
|
|
QTextCursor::MoveAnchor, cursorPos_[0u].to_int());
|
| 140 |
|
|
setTextCursor(cursor);
|
| 141 |
|
|
|
| 142 |
|
|
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
void ConsoleWidget::updateData(const char *buf, int bufsz) {
|
| 146 |
|
|
RISCV_mutex_lock(&mutexOutput_);
|
| 147 |
|
|
strOutput_ += QString(buf);
|
| 148 |
|
|
RISCV_mutex_unlock(&mutexOutput_);
|
| 149 |
|
|
emit signalNewData();
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
} // namespace debugger
|