-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgui.c
509 lines (437 loc) · 14.2 KB
/
gui.c
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "gui.h"
#include <stdio.h>
#include <CommCtrl.h>
#include "resource.h"
#include "log.h"
#include "config.h"
#include "gc_adapter.h"
#include "mapping.h"
#define GC_STATUS_REFRESH_MS 200
#define GC_STATUS_REFRESH_ID 1000
#define GC_STATUS_COLOR_OK RGB(64, 160, 0)
#define GC_STATUS_COLOR_ERR RGB(192, 0, 64)
static struct Config cfg_old;
static HINSTANCE hinstance;
static HWND mapping_tab;
int current_tab = 0;
unsigned int gc_status_color[5] = {
0,
GC_STATUS_COLOR_OK,
GC_STATUS_COLOR_OK,
GC_STATUS_COLOR_OK,
GC_STATUS_COLOR_OK
};
void init_mapping(HWND diag, int id)
{
HWND pri = GetDlgItem(diag, id);
HWND sec = GetDlgItem(diag, id + 100);
for (int i = 0; i < BA_MAX; i++) {
const char *str = mapping_get_label(i);
SendMessage(pri, CB_ADDSTRING, 0, (LPARAM)str);
SendMessage(sec, CB_ADDSTRING, 0, (LPARAM)str);
}
}
enum MappingButtonAxis *baptr_from_idc(int id)
{
struct ConfigController *cfgmap = &cfg.controller[current_tab];
struct Mapping *map = 0;
int is_sec = 0;
if (id >= IDC_MAPPING_SECONDARY_A) {
id -= 100;
is_sec = 1;
}
switch (id)
{
case IDC_MAPPING_A: map = &cfgmap->a; break;
case IDC_MAPPING_B: map = &cfgmap->b; break;
case IDC_MAPPING_Z: map = &cfgmap->z; break;
case IDC_MAPPING_L: map = &cfgmap->l; break;
case IDC_MAPPING_R: map = &cfgmap->r; break;
case IDC_MAPPING_START: map = &cfgmap->start; break;
case IDC_MAPPING_UP: map = &cfgmap->analog_up; break;
case IDC_MAPPING_DOWN: map = &cfgmap->analog_down; break;
case IDC_MAPPING_LEFT: map = &cfgmap->analog_left; break;
case IDC_MAPPING_RIGHT: map = &cfgmap->analog_right; break;
case IDC_MAPPING_CUP: map = &cfgmap->c_up; break;
case IDC_MAPPING_CDOWN: map = &cfgmap->c_down; break;
case IDC_MAPPING_CLEFT: map = &cfgmap->c_left; break;
case IDC_MAPPING_CRIGHT: map = &cfgmap->c_right; break;
case IDC_MAPPING_DUP: map = &cfgmap->d_up; break;
case IDC_MAPPING_DDOWN: map = &cfgmap->d_down; break;
case IDC_MAPPING_DLEFT: map = &cfgmap->d_left; break;
case IDC_MAPPING_DRIGHT: map = &cfgmap->d_right; break;
}
if (map) {
return is_sec ? &map->sec : &map->pri;
}
return 0;
}
void update_mapping_selection(HWND diag, int id, struct Mapping map)
{
HWND pri = GetDlgItem(diag, id);
HWND sec = GetDlgItem(diag, id + 100);
SendMessage(pri, CB_SETCURSEL, map.pri, 0);
SendMessage(sec, CB_SETCURSEL, map.sec, 0);
}
void update_mapping_tab(HWND diag)
{
for (int i = IDC_MAPPING_A; i <= IDC_MAPPING_RIGHT; i++) {
struct Mapping *map = (struct Mapping *)baptr_from_idc(i);
update_mapping_selection(diag, i, *map);
}
CheckDlgButton(diag, IDC_MAPPING_ENABLED,
cfg.controller[current_tab].enabled);
CheckDlgButton(diag, IDC_MAPPING_FORCEPLUGGED,
cfg.controller[current_tab].force_plugged);
HWND accessory = GetDlgItem(diag, IDC_MAPPING_ACCESSORY);
int selection = cfg.controller[current_tab].accessory;
SendMessage(accessory, CB_SETCURSEL, selection, 0);
HWND port = GetDlgItem(diag, IDC_MAPPING_PORT);
selection = cfg.controller_ex[current_tab].adapter_port;
SendMessage(port, CB_SETCURSEL, selection, 0);
}
void init_mapping_tab(HWND diag)
{
for (int i = IDC_MAPPING_A; i <= IDC_MAPPING_RIGHT; i++) {
init_mapping(diag, i);
}
static const char *accessory_label[] = {
"No accessory",
"Controller Pak",
"Rumble Pak",
};
HWND accessory = GetDlgItem(diag, IDC_MAPPING_ACCESSORY);
for (size_t i = 0; i < sizeof(accessory_label)/sizeof(char*); i++) {
const char *str = accessory_label[i];
SendMessage(accessory, CB_ADDSTRING, 0, (LPARAM)str);
}
static const char *port_label[] = {
"Adapter port 1",
"Adapter port 2",
"Adapter port 3",
"Adapter port 4",
};
HWND port = GetDlgItem(diag, IDC_MAPPING_PORT);
for (size_t i = 0; i < sizeof(port_label)/sizeof(char*); i++) {
const char *str = port_label[i];
SendMessage(port, CB_ADDSTRING, 0, (LPARAM)str);
}
update_mapping_tab(diag);
}
void update_mapping_config(HWND diag, int id) {
enum MappingButtonAxis *ba = baptr_from_idc(id);
HWND h = GetDlgItem(diag, id);
*ba = SendMessage(h, CB_GETCURSEL, 0, 0);
}
INT_PTR CALLBACK mapping_dlgproc(HWND diag, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
init_mapping_tab(diag);
break;
case WM_COMMAND: ;
int id = LOWORD(wParam);
if (HIWORD(wParam) == CBN_SELCHANGE) {
if (id >= IDC_MAPPING_A && id <= IDC_MAPPING_SECONDARY_RIGHT) {
update_mapping_config(diag, id);
break;
} else if (id == IDC_MAPPING_ACCESSORY) {
cfg.controller[current_tab].accessory = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
break;
} else if (id == IDC_MAPPING_PORT) {
cfg.controller_ex[current_tab].adapter_port = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
break;
}
}
switch (id)
{
case IDC_MAPPING_ENABLED:
cfg.controller[current_tab].enabled = IsDlgButtonChecked(diag, id) ? 1 : 0;
return TRUE;
case IDC_MAPPING_FORCEPLUGGED:
cfg.controller[current_tab].force_plugged = IsDlgButtonChecked(diag, id) ? 1 : 0;
return TRUE;
}
return FALSE;
default:
return FALSE;
}
return TRUE;
}
void init_tabs(HWND diag)
{
HWND tab = GetDlgItem(diag, IDC_TABCONTROL);
for (int i = 0; i < 4; i++) {
char buf[64];
snprintf(buf, sizeof(buf), "Controller %d", i+1);
TCITEM item;
item.mask = TCIF_TEXT;
item.pszText = buf;
TabCtrl_InsertItem(tab, i, &item);
}
mapping_tab = CreateDialogA(hinstance, MAKEINTRESOURCE(IDD_MAPPING_TAB), diag, mapping_dlgproc);
RECT rc_client, rc_window;
GetClientRect(tab, &rc_client);
TabCtrl_AdjustRect(tab, FALSE, &rc_client);
GetWindowRect(tab, &rc_window);
ScreenToClient(diag, (LPPOINT)&rc_window);
OffsetRect(&rc_client, rc_window.left, rc_window.top);
MoveWindow(mapping_tab,
rc_client.left, rc_client.top, rc_client.right-rc_client.left, rc_client.bottom-rc_client.top, FALSE);
ShowWindow(mapping_tab, SW_SHOW);
}
void init_slider(HWND diag, int id, int min, int max, int val)
{
HWND slider = GetDlgItem(diag, id);
SendMessage(slider, TBM_SETRANGE, TRUE,
(LPARAM)MAKELONG(min, max));
SendMessage(slider, TBM_SETPOS, TRUE,
(LPARAM)val);
}
void print_percent(HWND diag, int id, int val)
{
char buf[5];
snprintf(buf, sizeof(buf), "%d%%", val);
HWND label = GetDlgItem(diag, id);
SetWindowText(label, buf);
}
void slider_updatecfg(HWND diag, HWND slider)
{
LRESULT pos = SendMessage(slider, TBM_GETPOS, 0, 0);
switch (GetDlgCtrlID(slider))
{
case IDC_SLIDER_RANGE:
cfg.range = pos;
print_percent(diag, IDC_LABEL_RANGE, pos);
break;
case IDC_SLIDER_TRIGTHRES:
cfg.trig_thres = pos;
print_percent(diag, IDC_LABEL_TRIGTHRES, pos*100/255);
break;
case IDC_SLIDER_CSTICKTHRES:
cfg.stick_a2d_thres = pos;
print_percent(diag, IDC_LABEL_CSTICKTHRES, pos*100/127);
break;
case IDC_SLIDER_DZ:
cfg.dz = pos;
print_percent(diag, IDC_LABEL_DZ, pos);
break;
}
}
void init_all(HWND diag)
{
CheckDlgButton(diag, IDC_ASYNC, cfg.async);
CheckDlgButton(diag, IDC_SCALEDIAGONALS, cfg.scale_diagonals);
CheckDlgButton(diag, IDC_SINGLEMAPPING, cfg.single_mapping);
init_slider(diag, IDC_SLIDER_RANGE, 0, 100, cfg.range);
init_slider(diag, IDC_SLIDER_TRIGTHRES, 0, 255, cfg.trig_thres);
init_slider(diag, IDC_SLIDER_CSTICKTHRES, 0, 127, cfg.stick_a2d_thres);
init_slider(diag, IDC_SLIDER_DZ, 0, 100, cfg.dz);
print_percent(diag, IDC_LABEL_RANGE, cfg.range);
print_percent(diag, IDC_LABEL_TRIGTHRES, cfg.trig_thres*100/255);
print_percent(diag, IDC_LABEL_CSTICKTHRES, cfg.stick_a2d_thres*100/127);
print_percent(diag, IDC_LABEL_DZ, cfg.dz);
}
void mb_pollrate(HWND parent)
{
float result = gc_test_pollrate();
if (result < 0) {
MessageBox(parent,
"Failed to test the pollrate.\n\n"
"Enable async polling, then restart the emulator.",
"sowwie UwU", MB_ICONERROR | MB_OK);
} else {
char buf[128];
snprintf(buf, sizeof(buf),
"Measured pollrate: %.1f Hz",
result);
MessageBox(parent, buf, "Mucho texto", MB_ICONINFORMATION | MB_OK);
}
}
void refresh_gc_status(HWND diag, int force_refresh)
{
static const char *gc_init_status[] = {
"N/A",
"Not initialized",
"OK",
"libusb init fail",
"Not detected",
"libusb claim fail"
"Poll thread error"
};
HWND label = GetDlgItem(diag, IDC_GCSTATUS_ADAPTER);
static enum GCError gcerr_old = -2;
enum GCError gcerr = gc_get_init_error();
const char *status = gc_init_status[gcerr+2];
if (gcerr_old != gcerr || force_refresh) {
SetWindowText(label, status);
gcerr_old = gcerr;
}
switch (gcerr)
{
case GCERR_NOT_INITIALIZED:
EnableWindow(label, FALSE);
break;
case GCERR_LIBUSB_INIT:
case GCERR_LIBUSB_OPEN:
case GCERR_LIBUSB_CLAIM_INTERFACE:
case GCERR_CREATE_THREAD:
EnableWindow(label, TRUE);
gc_status_color[0] = GC_STATUS_COLOR_ERR;
break;
case GCERR_OK:
EnableWindow(label, TRUE);
gc_status_color[0] = GC_STATUS_COLOR_OK;
}
static int status_old[4] = { -1 };
gc_inputs inputs[4];
int err = gc_get_all_inputs(inputs);
int i = 0;
for (int id = IDC_GCSTATUS_1; id <= IDC_GCSTATUS_4; id++) {
label = GetDlgItem(diag, id);
int status;
if (err) {
status = 0;
} else {
if (gc_is_present(inputs[i].status)) {
status = 1;
} else {
status = 2;
}
}
if (status != status_old[i] || force_refresh) {
switch (status)
{
case 0: // err
SetWindowText(label, "N/A");
EnableWindow(label, FALSE);
break;
case 1:
EnableWindow(label, TRUE);
SetWindowText(label, "OK");
break;
case 2:
EnableWindow(label, FALSE);
SetWindowText(label, "Not detected");
}
status_old[i] = status;
}
i++;
}
}
void init_gc_status(HWND diag)
{
SetTimer(diag, GC_STATUS_REFRESH_ID, GC_STATUS_REFRESH_MS, NULL);
refresh_gc_status(diag, 1);
}
int restart_required()
{
if (cfg.async != cfg_old.async)
return 1;
for (size_t i = 0; i < 4; i++) {
if (cfg.controller[i].accessory != cfg_old.controller[i].accessory) {
return 1;
}
}
return 0;
}
INT_PTR CALLBACK dlgproc(HWND diag, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
cfg_old = cfg;
init_all(diag);
init_tabs(diag);
init_gc_status(diag);
break;
case WM_CLOSE:
DestroyWindow(mapping_tab);
EndDialog(diag, 0);
break;
case WM_COMMAND:
switch (wParam)
{
case IDC_ASYNC:
cfg.async = IsDlgButtonChecked(diag, wParam) ? 1 : 0;
if (!cfg.async) {
MessageBox(
diag,
"Synchronous mode is a leftover hack from previous versions "
"of the plugin, is considered deprecated and will be removed "
"in a future version. Usage is discouraged.",
"Warning", MB_OK | MB_ICONWARNING
);
}
break;
case IDC_SINGLEMAPPING:
cfg.single_mapping = IsDlgButtonChecked(diag, wParam) ? 1 : 0;
break;
case IDC_SCALEDIAGONALS:
cfg.scale_diagonals = IsDlgButtonChecked(diag, wParam) ? 1 : 0;
break;
case IDC_DRIVER_SETUP:
ShellExecuteA(0, 0, "https://wermi.neocities.org/emuguide/pj64-wiiu-gcn/#winusb-driver-install", 0, 0, SW_HIDE);
break;
case IDC_TESTPOLL:
mb_pollrate(diag);
break;
case IDC_DEFAULTS:
config_defaults();
init_all(diag);
update_mapping_tab(mapping_tab);
break;
case IDC_SAVE:
config_save();
if (restart_required()) {
MessageBox(
diag,
"Some changes may require emulator restart to take effect.",
"Info", MB_OK | MB_ICONINFORMATION);
}
EndDialog(diag, 0);
break;
case IDC_CANCEL:
cfg = cfg_old;
EndDialog(diag, 0);
break;
}
break;
case WM_HSCROLL:
slider_updatecfg(diag, (HWND)lParam);
break;
case WM_TIMER:
if (wParam == GC_STATUS_REFRESH_ID) {
refresh_gc_status(diag, 0);
}
break;
case WM_CTLCOLORSTATIC: ;
HDC hdc = (HDC)wParam;
int id = GetWindowLong((HWND)lParam, GWL_ID);
if (id >= IDC_GCSTATUS_ADAPTER && id <= IDC_GCSTATUS_4) {
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, gc_status_color[id-IDC_GCSTATUS_ADAPTER]);
return (INT_PTR)GetSysColorBrush(COLOR_BTNFACE);
}
return FALSE;
case WM_NOTIFY: ;
LPNMHDR lpnmhdr = (LPNMHDR)lParam;
if (lpnmhdr->code == TCN_SELCHANGE) {
if (lpnmhdr->idFrom == IDC_TABCONTROL) {
current_tab = TabCtrl_GetCurSel(lpnmhdr->hwndFrom);
update_mapping_tab(mapping_tab);
break;
}
}
return FALSE;
default:
return FALSE;
}
return TRUE;
}
void config_window(HINSTANCE hinst, HWND parent)
{
hinstance = hinst;
DialogBox(hinst, MAKEINTRESOURCE(IDD_DIALOG1), parent, dlgproc);
}