1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include <QDesktopWidget>
18#include "cpudialog.h"
19#include "ui_cpudialog.h"
20#include "../HighlighterManager.h"
21#include "../mainwindow.h"
22#include "../debugger.h"
23#include "../settings.h"
24#include "../colorscheme.h"
25#include "../iconsmanager.h"
26
27CPUDialog::CPUDialog(QWidget *parent) :
28 QDialog(parent),
29 ui(new Ui::CPUDialog)
30{
31 setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
32 setWindowFlag(Qt::WindowContextHelpButtonHint,false);
33 ui->setupUi(this);
34 ui->txtCode->setHighlighter(highlighterManager.getCppHighlighter());
35 ui->txtCode->setReadOnly(true);
36 ui->txtCode->gutter().setShowLineNumbers(false);
37 ui->txtCode->setCaretUseTextColor(true);
38
39 ui->txtCode->codeFolding().indentGuides = false;
40 ui->txtCode->codeFolding().fillIndents = false;
41 ui->txtCode->setGutterWidth(0);
42 ui->txtCode->setUseCodeFolding(false);
43 highlighterManager.applyColorScheme(ui->txtCode->highlighter(),
44 pSettings->editor().colorScheme());
45 PColorSchemeItem item = pColorManager->getItem(pSettings->editor().colorScheme(),COLOR_SCHEME_ACTIVE_LINE);
46 if (item) {
47 ui->txtCode->setActiveLineColor(item->background());
48 }
49 item = pColorManager->getItem(pSettings->editor().colorScheme(),COLOR_SCHEME_TEXT);
50 if (item) {
51 ui->txtCode->setForegroundColor(item->foreground());
52 ui->txtCode->setBackgroundColor(item->background());
53 } else {
54 ui->txtCode->setForegroundColor(palette().color(QPalette::Text));
55 ui->txtCode->setBackgroundColor(palette().color(QPalette::Base));
56 }
57 resetEditorFont(screenDPI());
58 ui->lstRegister->setModel(pMainWindow->debugger()->registerModel());
59
60 ui->rdIntel->setChecked(pSettings->debugger().useIntelStyle());
61 ui->chkBlendMode->setChecked(pSettings->debugger().blendMode());
62 resize(pSettings->ui().CPUDialogWidth(),pSettings->ui().CPUDialogHeight());
63
64 QList<int> sizes = ui->splitter->sizes();
65 int tabWidth = pSettings->ui().CPUDialogSplitterPos();
66 int totalSize = sizes[0] + sizes[1];
67 sizes[0] = tabWidth;
68 sizes[1] = std::max(1,totalSize - sizes[0]);
69 ui->splitter->setSizes(sizes);
70
71 onUpdateIcons();
72 connect(pIconsManager,&IconsManager::actionIconsUpdated,
73 this, &CPUDialog::onUpdateIcons);
74}
75
76CPUDialog::~CPUDialog()
77{
78 delete ui;
79}
80
81void CPUDialog::updateInfo()
82{
83 if (pMainWindow->debugger()->executing()) {
84 // Load the registers..
85 sendSyntaxCommand();
86 pMainWindow->debugger()->sendCommand("-data-list-register-values", "N");
87 if (ui->chkBlendMode->isChecked())
88 pMainWindow->debugger()->sendCommand("disas", "/s");
89 else
90 pMainWindow->debugger()->sendCommand("disas", "");
91 }
92}
93
94void CPUDialog::updateButtonStates(bool enable)
95{
96 ui->btnStepIntoInstruction->setEnabled(enable);
97 ui->btnStepOverInstruction->setEnabled(enable);
98}
99
100void CPUDialog::updateDPI(float dpi)
101{
102 QFont font(pSettings->environment().interfaceFont());
103 font.setPixelSize(pointToPixel(pSettings->environment().interfaceFontSize(),dpi));
104 font.setStyleStrategy(QFont::PreferAntialias);
105 setFont(font);
106 for (QWidget* p:findChildren<QWidget*>()) {
107 if (p!=ui->txtCode)
108 p->setFont(font);
109 }
110 resetEditorFont(dpi);
111}
112
113void CPUDialog::setDisassembly(const QString& file, const QString& funcName,const QStringList& lines)
114{
115 ui->txtFunctionName->setText(QString("%1:%2").arg(file, funcName));
116 int activeLine = -1;
117 ui->txtCode->document()->clear();
118 for (int i=0;i<lines.size();i++) {
119 QString line = lines[i];
120 if (line.startsWith("=>")) {
121 activeLine = i;
122 }
123 ui->txtCode->document()->add(line);
124 }
125 if (activeLine!=-1)
126 ui->txtCode->setCaretXYEx(true,QSynedit::BufferCoord{1,activeLine+1});
127}
128
129void CPUDialog::resetEditorFont(float dpi)
130{
131 QFont f=QFont(pSettings->editor().fontName());
132 f.setPixelSize(pointToPixel(pSettings->editor().fontSize(),dpi));
133 f.setStyleStrategy(QFont::PreferAntialias);
134 ui->txtCode->setFont(f);
135 QFont f2=QFont(pSettings->editor().nonAsciiFontName());
136 f2.setPixelSize(pointToPixel(pSettings->editor().fontSize(),dpi));
137 f2.setStyleStrategy(QFont::PreferAntialias);
138 ui->txtCode->setFontForNonAscii(f2);
139}
140
141void CPUDialog::sendSyntaxCommand()
142{
143 // Set disassembly flavor
144 if (ui->rdIntel->isChecked()) {
145 pMainWindow->debugger()->sendCommand("-gdb-set", "disassembly-flavor intel");
146 } else {
147 pMainWindow->debugger()->sendCommand("-gdb-set", "disassembly-flavor att");
148 }
149}
150
151void CPUDialog::closeEvent(QCloseEvent *event)
152{
153 pSettings->ui().setCPUDialogWidth(width());
154 pSettings->ui().setCPUDialogHeight(height());
155
156 QList<int> sizes = ui->splitter->sizes();
157 pSettings->ui().setCPUDialogSplitterPos(sizes[0]);
158
159 QDialog::closeEvent(event);
160 emit closed();
161}
162
163void CPUDialog::on_rdIntel_toggled(bool)
164{
165 updateInfo();
166 pSettings->debugger().setUseIntelStyle(ui->rdIntel->isChecked());
167 pSettings->debugger().save();
168}
169
170void CPUDialog::on_rdATT_toggled(bool)
171{
172 updateInfo();
173 pSettings->debugger().setUseIntelStyle(ui->rdIntel->isChecked());
174 pSettings->debugger().save();
175}
176
177void CPUDialog::on_chkBlendMode_stateChanged(int)
178{
179 updateInfo();
180 pSettings->debugger().setBlendMode(ui->chkBlendMode->isCheckable());
181 pSettings->debugger().save();
182}
183
184void CPUDialog::on_btnStepOverInstruction_clicked()
185{
186 pMainWindow->debugger()->sendCommand("-exec-next-instruction","");
187}
188
189
190void CPUDialog::on_btnStepIntoInstruction_clicked()
191{
192 pMainWindow->debugger()->sendCommand("-exec-step-instruction","");
193}
194
195void CPUDialog::onUpdateIcons()
196{
197 pIconsManager->setIcon(ui->btnStepIntoInstruction, IconsManager::ACTION_RUN_STEP_INTO_INSTRUCTION);
198 pIconsManager->setIcon(ui->btnStepOverInstruction, IconsManager::ACTION_RUN_STEP_OVER_INSTRUCTION);
199}
200
201