-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnwaccessstatut.cpp
167 lines (146 loc) · 5.07 KB
/
nwaccessstatut.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "nwaccessstatut.h"
#include "ui_nwaccessstatut.h"
#include <QLoggingCategory>
#include <Button-Mash/localcontrollermanager.h>
#include <Button-Mash/mapbuttondialog.h>
Q_LOGGING_CATEGORY(log_NWAccessstatut, "NWAccessStatut")
#define sDebug() qCDebug(log_NWAccessstatut)
const QString SETTING_CONTROLLERID = "NWAControllerID";
const QString SETTING_CONTROLLERMAPPING = "NWAControllerMapping";
NWAccessStatut::NWAccessStatut(QWidget *parent) :
QWidget(parent),
ui(new Ui::NWAccessStatut)
{
ui->setupUi(this);
client = new EmuNWAccessClient(this);
checkingInfo = false;
checkingStatus = false;
localController = nullptr;
checkStatusTimer.setInterval(3000);
connect(&checkStatusTimer, &QTimer::timeout, this, &NWAccessStatut::onTimerTimeout);
connect(client, &EmuNWAccessClient::readyRead, this, [=] {
auto reply = client->readReply();
//sDebug() << reply.toMap();
if (checkingStatus)
{
checkingStatus = false;
if (reply["state"] == "no_game" || reply["game"].isEmpty())
{
ui->gameLabel->setText(tr("No game loaded"));
emit unReadyForSaveState();
} else {
ui->gameLabel->setText(QString(tr("Running : %1").arg(reply["game"])));
checkStatusTimer.stop();
emit readyForSaveState();
}
}
if (checkingInfo)
{
checkingStatus = true;
checkingInfo = false;
ui->emulatorLabel->setText(reply["name"] + " " + reply["version"]);
if (reply["commands"].split(',').contains("SAVE_STATE_TO_NETWORK"))
{
client->cmdEmulationStatus();
}
}
});
connect(client, &EmuNWAccessClient::connected, this, [=] {
sDebug() << "Connected";
client->cmdMyNameIs("Savestate2Snes status check");
});
connect(client, &EmuNWAccessClient::disconnected, this, [=] {
sDebug() << "Disconnected";
ui->gameLabel->setText(QString("No emulator running"));
ui->emulatorLabel->setText(QString("No emulator running"));
checkStatusTimer.start();
emit unReadyForSaveState();
});
}
void NWAccessStatut::stop()
{
client->disconnectFromHost();
checkStatusTimer.stop();
}
QString NWAccessStatut::readyString()
{
return tr("Emulator ready for Savestate");
}
QString NWAccessStatut::unreadyString()
{
return tr("Emulator not ready for Savestate");
}
void NWAccessStatut::setSettings(QSettings *set)
{
settings = set;
}
void NWAccessStatut::setToShow()
{
if (settings->contains(SETTING_CONTROLLERID))
{
sDebug() << "Creating localcontroller" << settings->value(SETTING_CONTROLLERID).toString();
localController = LocalControllerManager::getManager()->createProvider(settings->value(SETTING_CONTROLLERID).toString());
mapping = LocalControllerManager::getManager()->loadMapping(*settings, SETTING_CONTROLLERMAPPING);
localController->setMapping(mapping);
ui->controllerComboBox->addItem(localController->name());
ui->controllerComboBox->setItemData(0, localController->id(), Qt::UserRole + 1);
emit localControllerChanged();
}
listController();
}
void NWAccessStatut::onTimerTimeout()
{
sDebug() << client->isConnected();
if (client->isConnected())
{
sDebug() << "Cmd info";
client->cmdEmulatorInfo();
checkingInfo = true;
} else {
client->connectToHost("127.0.0.1", 0xBEEF);
}
}
void NWAccessStatut::listController()
{
controllersInfos = LocalControllerManager::getManager()->listController();
quint8 cpt = 0;
ui->controllerComboBox->clear();
for (auto& info : controllersInfos)
{
ui->controllerComboBox->addItem(info.name);
ui->controllerComboBox->setItemData(cpt, info.id, Qt::UserRole + 1);
cpt++;
}
}
void NWAccessStatut::start()
{
client->connectToHost("127.0.0.1", 0xBEEF);
checkStatusTimer.start();
}
NWAccessStatut::~NWAccessStatut()
{
delete ui;
}
void NWAccessStatut::on_refreshPushButton_clicked()
{
listController();
}
void NWAccessStatut::on_mappingButton_clicked()
{
MapButtonDialog diag;
diag.setMapping(mapping);
int idx = ui->controllerComboBox->currentIndex();
diag.setDevice(controllersInfos.at(idx));
if (diag.exec())
{
mapping = diag.mapping();
if (localController == nullptr)
localController = LocalControllerManager::getManager()->createProvider(ui->controllerComboBox->itemData(idx, Qt::UserRole + 1).toString());
localController->setMapping(mapping);
localController = LocalControllerManager::getManager()->createProvider(
ui->controllerComboBox->itemData(ui->controllerComboBox->currentIndex(), Qt::UserRole + 1).toString());
settings->setValue(SETTING_CONTROLLERID, ui->controllerComboBox->itemData(idx, Qt::UserRole + 1).toString());
LocalControllerManager::getManager()->saveMapping(*settings, SETTING_CONTROLLERMAPPING, mapping);
emit localControllerChanged();
}
}