-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
368 lines (336 loc) · 11.3 KB
/
mainwindow.cpp
File metadata and controls
368 lines (336 loc) · 11.3 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
installEventFilter(this);
bs_buttons = std::make_unique<Button_bindingStruct>(ui->AllButtons_ComboBox,
ui->ListWidget_ButtonBindings);
bs_axes = std::make_unique<Axis_bindingStruct>(ui->AllButtonsAndAxesComboBox,
ui->listWidget_AxesBinding);
bs_povs = std::make_unique<POV_bindingStruct>(ui->AllPovs_ComboBox,
ui->ListWidget_POVBindings);
binding_structs.push_back(bs_buttons.get());
binding_structs.push_back(bs_axes.get());
binding_structs.push_back(bs_povs.get());
for(auto &binding_struct : binding_structs)
binding_struct->Initialize_comboboxes(connector.current_device->id);
}
void MainWindow::on_actionAbout_triggered()
{
WORD VerDll, VerDrv;
DriverMatch(&VerDll, &VerDrv);
QString info {"<center>Virtual Joystick feeder v" + VERSION + "</center><br>"};
info += "<center><a href='https://github.com/hobbitinisengard/virtual_joystick'>Link</a></center><br>";
info += "This application uses an updated ";
info += QString::fromWCharArray((PWSTR)GetvJoyProductString()) + " library originally made by ";
info += QString::fromWCharArray((PWSTR)GetvJoyManufacturerString())+ " and updated by <a href='https://github.com/njz3/vJoy'>njz3</a>.<br>";
info += "DLL version: " + QString::number(VerDll) + "<br>";
info += "Driver version: " + QString::number(VerDrv) + " ("+QString::fromWCharArray((PWSTR)GetvJoySerialNumberString()) + ")";
Infobox infobox(info);
}
void MainWindow::Quit()
{
QApplication::quit();
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
{
for(auto& binding : bindings)
{
auto *keyEvent = static_cast<QKeyEvent *>(event);
//event->key() != Qt::Key_Escape
if(binding->key == keyEvent->key())
{
bool success = binding->bindingstruct->Send_data(*binding, event->type() == QEvent::KeyPress);
if(!success){
qWarning("Couldn't send signal to vjoystick");
}
}
}
}
return QMainWindow::eventFilter(object, event);
}
std::vector<std::string> MainWindow::split (std::string s, std::string delimiter)
{
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find (delimiter, pos_start)) != std::string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}
res.push_back (s.substr (pos_start));
return res;
}
// int curr_index = ui->comboBox_configs->currentIndex();
// if(curr_index == -1)
// { // empty combobox
// filepath = QFileDialog::getSaveFileName(
// this,tr("Select vjoy configuration file"), "",
// tr("ini files (*.ini);;All Files (*)")).toStdString();
// if(filepath == "")
// return;
// Add_2_ini_combobox(filepath);
// ++curr_index;
// }
// else
// { // combobox selected
// filepath = ui->comboBox_configs->itemData(curr_index).value<std::string>();
// }
BindingStruct* MainWindow::get_bindingstruct_of_type(const SignalType type) const
{
switch(type){
case SignalType::BUTTON:
for(auto &bs : binding_structs){
if(dynamic_cast<Button_bindingStruct*>(bs) != nullptr){
return bs;
}
}
break;
case SignalType::AXIS:
for(auto &bs : binding_structs){
if(dynamic_cast<Axis_bindingStruct*>(bs) != nullptr){
return bs;
}
}
break;
case SignalType::DISCRETE_POV:
for(auto &bs : binding_structs){
if(dynamic_cast<POV_bindingStruct*>(bs) != nullptr){
return bs;
}
}
break;
case SignalType::CONTINUOUS_POV:
// for(auto &bs : binding_structs){
// if(dynamic_cast<POV_bindingStruct*>(bs) != nullptr){
// return bs;
// }
// }
break;
}
throw std::out_of_range("haven't found correct binding struct");
}
int MainWindow::Convert_bindingstruct_to_type(BindingStruct* bs) const
{
if(dynamic_cast<Button_bindingStruct*>(bs) != nullptr){
return (int)SignalType::BUTTON;
}
if(dynamic_cast<Axis_bindingStruct*>(bs) != nullptr){
return (int)SignalType::AXIS;
}
if(dynamic_cast<POV_bindingStruct*>(bs) != nullptr){
return (int)SignalType::DISCRETE_POV;
}
throw std::out_of_range("Much error yes.");
}
void MainWindow::Load_configuration()
{
std::string filepath;
filepath = QFileDialog::getOpenFileName(
this,tr("Select vjoy configuration file"), "",
tr("ini files (*.ini);;All Files (*)")).toStdString();
if(filepath == "")
return;
bindings.clear();
std::ifstream inf {filepath};
while (inf.good())
{
// read and insert data to cleared bindings QList.
std::string strInput;
std::getline(inf, strInput);
if(strInput == "")
break;
auto ln = split(strInput, " ");
int id = std::stoi(ln[0]);
SignalType type = (SignalType) std::stoi(ln[1]);
int key = std::stoi(ln[2]);
int macro = std::stoi(ln[3]);
Direction direction = (Direction) std::stoi(ln[4]);
BindingStruct* binding_struct = get_bindingstruct_of_type(type);
bindings.append(new Binding(id,binding_struct,key,macro,direction));
}
UpdateName(filepath);
Switch_device(connector.current_device->id-1);
}
//Binding(uint8_t ID, SignalType TYPE, int KEY, uint16_t MACRO, Direction DIR = Direction::POSITIVE)
void MainWindow::Save_configuration()
{
std::string filepath = QFileDialog::getSaveFileName(
this,tr("Save vjoy configuration file"), "",
tr("ini files (*.ini);;All Files (*)")).toStdString();
if(filepath == "")
return;
std::ofstream outf {filepath, std::ios::trunc};
for(auto& bind : bindings)
{
outf << (int)bind->device_id << " ";
outf << Convert_bindingstruct_to_type(bind->bindingstruct) << " ";
outf << bind->key << " ";
outf << (int)bind->macro << " ";
outf << (int)bind->direction << '\n';
}
UpdateName(filepath);
int temp = connector.current_device->id-1;
Switch_device(temp);
}
void MainWindow::UpdateName(std::string filepath)
{
QString ini_name = QString::fromStdString(remove_extension(base_name(filepath)));
ui->lineEdit_Name->setText(ini_name);
}
template<class T>
T MainWindow::base_name(T const & path, T const & delims /*= "/\\"*/)
{
return path.substr(path.find_last_of(delims) + 1);
}
template<class T>
T MainWindow::remove_extension(T const & filename)
{
typename T::size_type const p(filename.find_last_of('.'));
return p > 0 && p != T::npos ? filename.substr(0, p) : filename;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Button_ScanBinding_clicked()
{
Scan(*bs_buttons);
}
void MainWindow::on_Button_ScanButtonOrAxis_clicked()
{
Scan(*bs_axes);
}
void MainWindow::on_Button_ScanPOVs_clicked()
{
Scan(*bs_povs);
}
void MainWindow::Scan(BindingStruct &binding_struct)
{
ScanBox Scanning_monolog_box(binding_struct, connector.current_device, bindings);
Scanning_monolog_box.exec();
}
void MainWindow::Switch_device(int index)
{
qDebug() << "bindings count=" << bindings.count();
int vjoy_index = index+1;
Clear_Listwidgets();
//check if device with id of index exists
auto device = std::find_if(connector.devices.begin(), connector.devices.end(),
[vjoy_index](vJDevice *d){return d->id == vjoy_index;});
if(device == connector.devices.end())
{// if not, create device.
try
{
connector.Create_new_Device(vjoy_index);
}
catch(VJD_STAT_BUSY_Exception &e)
{
Infobox info("This device is already owned by another feeder");
ui->vjoy_tabs->setCurrentIndex(0);
return;
}
catch(VJD_STAT_MISS_Exception &e)
{
Infobox info("This vJoy device is not installed or disabled. Feeder stopped.");
ui->vjoy_tabs->setCurrentIndex(0);
return;
}
catch(...)
{
Infobox info("vJoy device error. Enable the virtual device using 'Configure vJoy' program. After that, restart this application.");
ui->vjoy_tabs->setCurrentIndex(0);
return;
}
}
else
{ // device exists
qDebug() << "device exists";
connector.current_device = *device;
}
for(auto &binding_struct : binding_structs)
binding_struct->Initialize_comboboxes(connector.current_device->id);
// reconstruct listwidget of bindings
for(const auto &bind: bindings)
{
if(bind->device_id != connector.current_device->id)
continue;
qDebug() << "Bind: " << bind->key;
QListWidgetItem *i = new QListWidgetItem();
i->setTextAlignment(Qt::AlignCenter);
bind->bindingstruct->Add_item(*bind);
}
}
void MainWindow::on_vjoy_tabs_currentChanged(int index)
{
Switch_device(index);
}
void MainWindow::Delete_from_bindings(QListWidgetItem *item)
{
int keycode = item->data(100).toInt();
for(int i=0; i<bindings.count(); ++i){
if(keycode == bindings[i]->key){
bindings.removeAt(i);
//if(MainWindow::allow_multiple_keybindings == false)
// --i;
//else //
return;
}
}
}
void MainWindow::on_ListWidget_ButtonBindings_itemDoubleClicked(QListWidgetItem *item)
{
Delete_from_bindings(item);
ui->ListWidget_ButtonBindings->removeItemWidget(item);
delete item;
}
void MainWindow::on_listWidget_AxesBinding_itemDoubleClicked(QListWidgetItem *item)
{
Delete_from_bindings(item);
ui->listWidget_AxesBinding->removeItemWidget(item);
delete item;
}
void MainWindow::on_ListWidget_POVBindings_itemDoubleClicked(QListWidgetItem *item)
{
Delete_from_bindings(item);
ui->ListWidget_POVBindings->removeItemWidget(item);
delete item;
}
void MainWindow::on_Checkbox_AllowMultipleKeybinding_stateChanged(int arg1)
{
this->allow_multiple_keybindings = arg1;
}
void MainWindow::Clear_Listwidgets()
{
ui->listWidget_AxesBinding->clear();
ui->ListWidget_POVBindings->clear();
ui->ListWidget_ButtonBindings->clear();
}
void MainWindow::on_button_CLEAR_clicked()
{
bindings.removeIf([this](Binding *a) { return a->device_id == connector.current_device->id; });
Clear_Listwidgets();
}
void MainWindow::on_button_LOAD_clicked()
{
Load_configuration();
}
void MainWindow::on_button_SAVE_clicked()
{
Save_configuration();
}
void MainWindow::on_ListWidget_ButtonBindings_itemClicked(QListWidgetItem *item)
{
}
void MainWindow::on_listWidget_AxesBinding_itemClicked(QListWidgetItem *item)
{
}
void MainWindow::on_ListWidget_POVBindings_itemClicked(QListWidgetItem *item)
{
}