src/platform/qt/DolphinConnector.cpp (view raw)
1/* Copyright (c) 2013-2021 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 "DolphinConnector.h"
7
8#include <QMessageBox>
9
10#include "CoreController.h"
11#include "Window.h"
12#include "utils.h"
13
14using namespace QGBA;
15
16DolphinConnector::DolphinConnector(Window* window, QWidget* parent)
17 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
18 , m_window(window)
19{
20 m_ui.setupUi(this);
21
22 connect(window, &QObject::destroyed, this, &QWidget::close);
23 connect(m_ui.connect, &QAbstractButton::clicked, this, &DolphinConnector::attach);
24 connect(m_ui.disconnect, &QAbstractButton::clicked, this, &DolphinConnector::detach);
25
26 updateAttached();
27}
28
29void DolphinConnector::attach() {
30 QHostAddress qaddress;
31 Address address;
32 if (m_ui.specLocal->isChecked()) {
33 qaddress.setAddress("127.0.0.1");
34 } else if (m_ui.specIPAddr->isChecked()) {
35 if (!qaddress.setAddress(m_ui.ipAddr->text())) {
36 return;
37 }
38
39 }
40 bool reset = m_ui.doReset->isChecked();
41 if (!m_window->controller()) {
42 m_window->bootBIOS();
43 reset = false;
44 if (!m_window->controller() || m_window->controller()->platform() != mPLATFORM_GBA) {
45 return;
46 }
47 }
48
49 convertAddress(&qaddress, &address);
50 m_controller = m_window->controller();
51 CoreController::Interrupter interrupter(m_controller);
52 m_controller->attachDolphin(address);
53 connect(m_controller.get(), &CoreController::stopping, this, &DolphinConnector::detach);
54 interrupter.resume();
55
56 if (!m_controller->isDolphinConnected()) {
57 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Connect"),
58 tr("Could not connect to Dolphin."),
59 QMessageBox::Ok);
60 fail->setAttribute(Qt::WA_DeleteOnClose);
61 fail->show();
62 } else if (reset) {
63 m_controller->reset();
64 }
65
66 updateAttached();
67}
68
69void DolphinConnector::detach() {
70 if (m_controller) {
71 m_controller->detachDolphin();
72 m_controller.reset();
73 }
74 updateAttached();
75}
76
77void DolphinConnector::updateAttached() {
78 bool attached = m_window->controller() && m_window->controller()->isDolphinConnected();
79 m_ui.connect->setDisabled(attached);
80 m_ui.disconnect->setEnabled(attached);
81 m_ui.specLocal->setDisabled(attached);
82 m_ui.specIPAddr->setDisabled(attached);
83}