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 if (!m_window->controller()) {
41 m_window->bootBIOS();
42 if (!m_window->controller() || m_window->controller()->platform() != mPLATFORM_GBA) {
43 return;
44 }
45 }
46
47 convertAddress(&qaddress, &address);
48 m_controller = m_window->controller();
49 CoreController::Interrupter interrupter(m_controller);
50 m_controller->attachDolphin(address);
51 connect(m_controller.get(), &CoreController::stopping, this, &DolphinConnector::detach);
52
53 if (!m_controller->isDolphinConnected()) {
54 QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Connect"),
55 tr("Could not connect to Dolphin."),
56 QMessageBox::Ok);
57 fail->setAttribute(Qt::WA_DeleteOnClose);
58 fail->show();
59 }
60
61 updateAttached();
62}
63
64void DolphinConnector::detach() {
65 if (m_controller) {
66 m_controller->detachDolphin();
67 m_controller.reset();
68 }
69 updateAttached();
70}
71
72void DolphinConnector::updateAttached() {
73 bool attached = m_window->controller() && m_window->controller()->isDolphinConnected();
74 m_ui.connect->setDisabled(attached);
75 m_ui.disconnect->setEnabled(attached);
76 m_ui.specLocal->setDisabled(attached);
77 m_ui.specIPAddr->setDisabled(attached);
78}