src/platform/qt/GDBWindow.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "GDBWindow.h"
7
8#include <QGridLayout>
9#include <QGroupBox>
10#include <QLabel>
11#include <QLineEdit>
12#include <QMessageBox>
13#include <QPushButton>
14#include <QVBoxLayout>
15
16#include "GDBController.h"
17
18using namespace QGBA;
19
20GDBWindow::GDBWindow(GDBController* controller, QWidget* parent)
21 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
22 , m_gdbController(controller)
23{
24 setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
25 QVBoxLayout* mainSegment = new QVBoxLayout;
26 setLayout(mainSegment);
27 QGroupBox* settings = new QGroupBox(tr("Server settings"));
28 mainSegment->addWidget(settings);
29
30 QGridLayout* settingsGrid = new QGridLayout;
31 settings->setLayout(settingsGrid);
32
33 QLabel* portLabel = new QLabel(tr("Local port"));
34 settingsGrid->addWidget(portLabel, 0, 0, Qt::AlignRight);
35 QLabel* bindAddressLabel = new QLabel(tr("Bind address"));
36 settingsGrid->addWidget(bindAddressLabel, 1, 0, Qt::AlignRight);
37
38 m_portEdit = new QLineEdit("2345");
39 m_portEdit->setMaxLength(5);
40 connect(m_portEdit, SIGNAL(textChanged(const QString&)), this, SLOT(portChanged(const QString&)));
41 settingsGrid->addWidget(m_portEdit, 0, 1, Qt::AlignLeft);
42
43 m_bindAddressEdit = new QLineEdit("0.0.0.0");
44 m_bindAddressEdit->setMaxLength(15);
45 connect(m_bindAddressEdit, SIGNAL(textChanged(const QString&)), this, SLOT(bindAddressChanged(const QString&)));
46 settingsGrid->addWidget(m_bindAddressEdit, 1, 1, Qt::AlignLeft);
47
48 m_startStopButton = new QPushButton;
49 mainSegment->addWidget(m_startStopButton);
50 connect(m_gdbController, SIGNAL(listening()), this, SLOT(started()));
51 connect(m_gdbController, SIGNAL(listenFailed()), this, SLOT(failed()));
52
53 if (m_gdbController->isAttached()) {
54 started();
55 } else {
56 stopped();
57 }
58}
59
60void GDBWindow::portChanged(const QString& portString) {
61 bool ok = false;
62 ushort port = portString.toUShort(&ok);
63 if (ok) {
64 m_gdbController->setPort(port);
65 }
66}
67
68void GDBWindow::bindAddressChanged(const QString& bindAddressString) {
69 bool ok = false;
70 QStringList parts = bindAddressString.split('.');
71 if (parts.length() != 4) {
72 return;
73 }
74 int i;
75 uint32_t address = 0;
76 for (i = 0; i < 4; ++i) {
77 ushort octet = parts[i].toUShort(&ok);
78 if (!ok) {
79 return;
80 }
81 if (octet > 0xFF) {
82 return;
83 }
84 address <<= 8;
85 address += octet;
86 }
87 m_gdbController->setBindAddress(address);
88}
89
90void GDBWindow::started() {
91 m_portEdit->setEnabled(false);
92 m_bindAddressEdit->setEnabled(false);
93 m_startStopButton->setText(tr("Stop"));
94 disconnect(m_startStopButton, SIGNAL(clicked()), m_gdbController, SLOT(listen()));
95 connect(m_startStopButton, SIGNAL(clicked()), m_gdbController, SLOT(detach()));
96 connect(m_startStopButton, SIGNAL(clicked()), this, SLOT(stopped()));
97}
98
99void GDBWindow::stopped() {
100 m_portEdit->setEnabled(true);
101 m_bindAddressEdit->setEnabled(true);
102 m_startStopButton->setText(tr("Start"));
103 disconnect(m_startStopButton, SIGNAL(clicked()), m_gdbController, SLOT(detach()));
104 disconnect(m_startStopButton, SIGNAL(clicked()), this, SLOT(stopped()));
105 connect(m_startStopButton, SIGNAL(clicked()), m_gdbController, SLOT(listen()));
106}
107
108void GDBWindow::failed() {
109 QMessageBox* failure = new QMessageBox(QMessageBox::Warning, tr("Crash"), tr("Could not start GDB server"),
110 QMessageBox::Ok, this, Qt::Sheet);
111 failure->setAttribute(Qt::WA_DeleteOnClose);
112 failure->show();
113}