-
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathmain-menu.cc
More file actions
1994 lines (1717 loc) · 67.7 KB
/
Copy pathmain-menu.cc
File metadata and controls
1994 lines (1717 loc) · 67.7 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
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// xemu User Interface
//
// Copyright (C) 2020-2022 Matt Borgerson
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "common.hh"
#include "scene-manager.hh"
#include "widgets.hh"
#include "main-menu.hh"
#include "font-manager.hh"
#include "input-manager.hh"
#include "snapshot-manager.hh"
#include "viewport-manager.hh"
#include "xemu-hud.h"
#include "misc.hh"
#include "gl-helpers.hh"
#include "reporting.hh"
#include "qapi/error.h"
#include "actions.hh"
#include <SDL3/SDL.h>
#include <vector>
#include <string>
#include "../xemu-input.h"
#include "../xemu-notifications.h"
#include "../xemu-settings.h"
#include "../xemu-monitor.h"
#include "../xemu-version.h"
#include "../xemu-net.h"
#include "../xemu-os-utils.h"
#include "../xemu-xbe.h"
#include "../thirdparty/fatx/fatx.h"
#define DEFAULT_XMU_SIZE 8388608
MainMenuScene g_main_menu;
MainMenuTabView::~MainMenuTabView() {}
void MainMenuTabView::Draw()
{
}
void MainMenuGeneralView::Draw()
{
#if defined(_WIN32)
SectionTitle("Updates");
Toggle("Check for updates", &g_config.general.updates.check,
"Check for updates whenever xemu is opened");
#endif
#if defined(__x86_64__)
SectionTitle("Performance");
Toggle("Hard FPU emulation", &g_config.perf.hard_fpu,
"Use hardware-accelerated floating point emulation (requires restart)");
#endif
Toggle("Cache shaders to disk", &g_config.perf.cache_shaders,
"Reduce stutter in games by caching previously generated shaders");
SectionTitle("Miscellaneous");
Toggle("Skip startup animation", &g_config.general.skip_boot_anim,
"Skip the full Xbox boot animation sequence");
FilePicker("Screenshot output directory", g_config.general.screenshot_dir,
nullptr, 0, true, [](const char *path) {
xemu_settings_set_string(&g_config.general.screenshot_dir, path);
});
FilePicker("Games directory", g_config.general.games_dir, nullptr, 0, true,
[](const char *path) {
xemu_settings_set_string(&g_config.general.games_dir, path);
});
// toggle("Throttle DVD/HDD speeds", &g_config.general.throttle_io,
// "Limit DVD/HDD throughput to approximate Xbox load times");
}
bool MainMenuInputView::ConsumeRebindEvent(SDL_Event *event)
{
if (!m_rebinding) {
return false;
}
RebindEventResult rebind_result = m_rebinding->ConsumeRebindEvent(event);
if (rebind_result == RebindEventResult::Complete) {
m_rebinding = nullptr;
}
return rebind_result == RebindEventResult::Ignore;
}
bool MainMenuInputView::IsInputRebinding()
{
return m_rebinding != nullptr;
}
void MainMenuInputView::Draw()
{
SectionTitle("Controllers");
ImGui::PushFont(g_font_mgr.m_menu_font_small);
static int active = 0;
// Output dimensions of texture
float t_w = 512, t_h = 512;
// Dimensions of (port+label)s
float b_x = 0, b_x_stride = 100, b_y = 400;
float b_w = 68, b_h = 81;
// Dimensions of controller (rendered at origin)
float controller_width = 477.0f;
float controller_height = 395.0f;
// Dimensions of XMU
float xmu_x = 0, xmu_x_stride = 256, xmu_y = 0;
float xmu_w = 256, xmu_h = 256;
// Setup rendering to fbo for controller and port images
controller_fbo->Target();
ImTextureID id = (ImTextureID)(intptr_t)controller_fbo->Texture();
//
// Render buttons with icons of the Xbox style port sockets with
// circular numbers above them. These buttons can be activated to
// configure the associated port, like a tabbed interface.
//
ImVec4 color_active(0.50, 0.86, 0.54, 0.12);
ImVec4 color_inactive(0, 0, 0, 0);
// Begin a 4-column layout to render the ports
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,
g_viewport_mgr.Scale(ImVec2(0, 12)));
ImGui::Columns(4, "mixed", false);
const int port_padding = 8;
for (int i = 0; i < 4; i++) {
bool is_selected = (i == active);
bool port_is_bound = (xemu_input_get_bound(i) != NULL);
// Set an X offset to center the image button within the column
ImGui::SetCursorPosX(
ImGui::GetCursorPosX() +
(int)((ImGui::GetColumnWidth() - b_w * g_viewport_mgr.m_scale -
2 * port_padding * g_viewport_mgr.m_scale) /
2));
// We are using the same texture for all buttons, but ImageButton
// uses the texture as a unique ID. Push a new ID now to resolve
// the conflict.
ImGui::PushID(i);
float x = b_x + i * b_x_stride;
ImGui::PushStyleColor(ImGuiCol_Button,
is_selected ? color_active : color_inactive);
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding,
g_viewport_mgr.Scale(ImVec2(port_padding, port_padding)));
bool activated = ImGui::ImageButton(
"port_image_button",
id,
ImVec2(b_w * g_viewport_mgr.m_scale, b_h * g_viewport_mgr.m_scale),
ImVec2(x / t_w, (b_y + b_h) / t_h),
ImVec2((x + b_w) / t_w, b_y / t_h));
ImGui::PopStyleVar();
ImGui::PopStyleColor();
if (activated) {
active = i;
m_rebinding = nullptr;
}
uint32_t port_color = 0xafafafff;
bool is_hovered = ImGui::IsItemHovered();
if (is_hovered) {
port_color = 0xffffffff;
} else if (is_selected || port_is_bound) {
port_color = 0x81dc8a00;
}
RenderControllerPort(x, b_y, i, port_color);
ImGui::PopID();
ImGui::NextColumn();
}
ImGui::PopStyleVar(); // ItemSpacing
ImGui::Columns(1);
//
// Render device driver combo
//
// List available device drivers
const char *driver = bound_drivers[active];
if (strcmp(driver, DRIVER_DUKE) == 0)
driver = DRIVER_DUKE_DISPLAY_NAME;
else if (strcmp(driver, DRIVER_S) == 0)
driver = DRIVER_S_DISPLAY_NAME;
ImGui::Columns(2, "", false);
ImGui::SetColumnWidth(0, ImGui::GetWindowWidth()*0.25);
ImGui::Text("Emulated Device");
ImGui::SameLine(0, 0);
ImGui::NextColumn();
ImGui::SetNextItemWidth(-FLT_MIN);
if (ImGui::BeginCombo("###InputDrivers", driver,
ImGuiComboFlags_NoArrowButton)) {
const char *available_drivers[] = { DRIVER_DUKE, DRIVER_S };
const char *driver_display_names[] = { DRIVER_DUKE_DISPLAY_NAME,
DRIVER_S_DISPLAY_NAME };
bool is_selected = false;
int num_drivers = sizeof(driver_display_names) / sizeof(driver_display_names[0]);
for (int i = 0; i < num_drivers; i++) {
const char *iter = driver_display_names[i];
is_selected = strcmp(driver, iter) == 0;
ImGui::PushID(iter);
if (ImGui::Selectable(iter, is_selected)) {
for (int j = 0; j < num_drivers; j++) {
if (iter == driver_display_names[j])
bound_drivers[active] = available_drivers[j];
}
xemu_input_bind(active, bound_controllers[active], 1);
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
ImGui::PopID();
}
ImGui::EndCombo();
}
DrawComboChevron();
ImGui::NextColumn();
//
// Render input device combo
//
ImGui::Text("Input Device");
ImGui::SameLine(0, 0);
ImGui::NextColumn();
// List available input devices
const char *not_connected = "Not Connected";
ControllerState *bound_state = xemu_input_get_bound(active);
// Get current controller name
const char *name;
if (bound_state == NULL) {
name = not_connected;
} else {
name = bound_state->name;
}
ImGui::SetNextItemWidth(-FLT_MIN);
if (ImGui::BeginCombo("###InputDevices", name, ImGuiComboFlags_NoArrowButton))
{
// Handle "Not connected"
bool is_selected = bound_state == NULL;
if (ImGui::Selectable(not_connected, is_selected)) {
xemu_input_bind(active, NULL, 1);
bound_state = NULL;
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
// Handle all available input devices
ControllerState *iter;
QTAILQ_FOREACH(iter, &available_controllers, entry) {
is_selected = bound_state == iter;
ImGui::PushID(iter);
const char *selectable_label = iter->name;
char buf[128];
if (iter->bound >= 0) {
snprintf(buf, sizeof(buf), "%s (Port %d)", iter->name, iter->bound+1);
selectable_label = buf;
}
if (ImGui::Selectable(selectable_label, is_selected)) {
xemu_input_bind(active, iter, 1);
// FIXME: We want to bind the XMU here, but we can't because we
// just unbound it and we need to wait for Qemu to release the
// file
// If we previously had no controller connected, we can rebind
// the XMU
if (bound_state == NULL)
xemu_input_rebind_xmu(active);
bound_state = iter;
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
ImGui::PopID();
}
ImGui::EndCombo();
}
DrawComboChevron();
ImGui::Columns(1);
//
// Add a separator between input selection and controller graphic
//
ImGui::Dummy(ImVec2(0.0f, ImGui::GetStyle().WindowPadding.y / 2));
//
// Render controller image
//
bool device_selected = false;
if (bound_state) {
device_selected = true;
RenderController(0, 0, 0x81dc8a00, 0x0f0f0f00, bound_state);
} else {
static ControllerState state{};
RenderController(0, 0, 0x1f1f1f00, 0x0f0f0f00, &state);
}
ImVec2 cur = ImGui::GetCursorPos();
ImVec2 controller_display_size;
if (ImGui::GetContentRegionMax().x < controller_width*g_viewport_mgr.m_scale) {
controller_display_size.x = ImGui::GetContentRegionMax().x;
controller_display_size.y =
controller_display_size.x * controller_height / controller_width;
} else {
controller_display_size =
ImVec2(controller_width * g_viewport_mgr.m_scale,
controller_height * g_viewport_mgr.m_scale);
}
ImGui::SetCursorPosX(
ImGui::GetCursorPosX() +
(int)((ImGui::GetColumnWidth() - controller_display_size.x) / 2.0));
ImGui::Image(id,
controller_display_size,
ImVec2(0, controller_height/t_h),
ImVec2(controller_width/t_w, 0));
ImVec2 pos = ImGui::GetCursorPos();
if (!device_selected) {
const char *msg = "Please select an available input device";
ImVec2 dim = ImGui::CalcTextSize(msg);
ImGui::SetCursorPosX(cur.x + (controller_display_size.x-dim.x)/2);
ImGui::SetCursorPosY(cur.y + (controller_display_size.y-dim.y)/2);
ImGui::Text("%s", msg);
}
controller_fbo->Restore();
ImGui::PopFont();
ImGui::SetCursorPos(pos);
if (bound_state) {
ImGui::PushID(active);
SectionTitle("Expansion Slots");
// Begin a 2-column layout to render the expansion slots
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,
g_viewport_mgr.Scale(ImVec2(0, 12)));
ImGui::Columns(2, "mixed", false);
xmu_fbo->Target();
id = (ImTextureID)(intptr_t)xmu_fbo->Texture();
static const SDL_DialogFileFilter img_file_filters[] = {
{ ".img Files", "img" },
{ "All Files", "*" }
};
const char *comboLabels[2] = { "###ExpansionSlotA",
"###ExpansionSlotB" };
for (int i = 0; i < 2; i++) {
// Display a combo box to allow the user to choose the type of
// peripheral they want to use
enum peripheral_type selected_type =
bound_state->peripheral_types[i];
const char *peripheral_type_names[2] = { "None", "Memory Unit" };
const char *selected_peripheral_type =
peripheral_type_names[selected_type];
ImGui::SetNextItemWidth(-FLT_MIN);
if (ImGui::BeginCombo(comboLabels[i], selected_peripheral_type,
ImGuiComboFlags_NoArrowButton)) {
// Handle all available peripheral types
for (int j = 0; j < 2; j++) {
bool is_selected = selected_type == j;
ImGui::PushID(j);
const char *selectable_label = peripheral_type_names[j];
if (ImGui::Selectable(selectable_label, is_selected)) {
// Free any existing peripheral
if (bound_state->peripherals[i] != NULL) {
if (bound_state->peripheral_types[i] ==
PERIPHERAL_XMU) {
// Another peripheral was already bound.
// Unplugging
xemu_input_unbind_xmu(active, i);
}
// Free the existing state
g_free((void *)bound_state->peripherals[i]);
bound_state->peripherals[i] = NULL;
}
// Change the peripheral type to the newly selected type
bound_state->peripheral_types[i] =
(enum peripheral_type)j;
// Allocate state for the new peripheral
if (j == PERIPHERAL_XMU) {
bound_state->peripherals[i] =
g_malloc(sizeof(XmuState));
memset(bound_state->peripherals[i], 0,
sizeof(XmuState));
}
xemu_save_peripheral_settings(
active, i, bound_state->peripheral_types[i], NULL);
}
if (is_selected) {
ImGui::SetItemDefaultFocus();
}
ImGui::PopID();
}
ImGui::EndCombo();
}
DrawComboChevron();
// Set an X offset to center the image button within the column
ImGui::SetCursorPosX(
ImGui::GetCursorPosX() +
(int)((ImGui::GetColumnWidth() -
xmu_w * g_viewport_mgr.m_scale -
2 * port_padding * g_viewport_mgr.m_scale) /
2));
selected_type = bound_state->peripheral_types[i];
if (selected_type == PERIPHERAL_XMU) {
float x = xmu_x + i * xmu_x_stride;
float y = xmu_y;
XmuState *xmu = (XmuState *)bound_state->peripherals[i];
if (xmu->filename != NULL && strlen(xmu->filename) > 0) {
RenderXmu(x, y, 0x81dc8a00, 0x0f0f0f00);
} else {
RenderXmu(x, y, 0x1f1f1f00, 0x0f0f0f00);
}
ImVec2 xmu_display_size;
if (ImGui::GetContentRegionMax().x <
xmu_h * g_viewport_mgr.m_scale) {
xmu_display_size.x = ImGui::GetContentRegionMax().x / 2;
xmu_display_size.y = xmu_display_size.x * xmu_h / xmu_w;
} else {
xmu_display_size = ImVec2(xmu_w * g_viewport_mgr.m_scale,
xmu_h * g_viewport_mgr.m_scale);
}
ImGui::SetCursorPosX(
ImGui::GetCursorPosX() +
(int)((ImGui::GetColumnWidth() - xmu_display_size.x) /
2.0));
ImGui::Image(id, xmu_display_size, ImVec2(0.5f * i, 1),
ImVec2(0.5f * (i + 1), 0));
// Button to generate a new XMU
ImGui::PushID(i);
if (ImGui::Button("New Image", ImVec2(250, 0))) {
int port = active;
int slot = i;
ShowSaveFileDialog(img_file_filters, 2, nullptr, [port, slot](const char *new_path) {
if (create_fatx_image(new_path, DEFAULT_XMU_SIZE)) {
// XMU was created successfully. Bind it
xemu_input_bind_xmu(port, slot, new_path, false);
} else {
// Show alert message
char *msg = g_strdup_printf(
"Unable to create XMU image at %s", new_path);
xemu_queue_error_message(msg);
g_free(msg);
}
});
}
int port = active;
int slot = i;
FilePicker("Image", xmu->filename, img_file_filters, 2, false,
[port, slot](const char *path) {
if (strlen(path) > 0) {
xemu_input_bind_xmu(port, slot, path, false);
} else {
xemu_input_unbind_xmu(port, slot);
}
});
ImGui::PopID();
}
ImGui::NextColumn();
}
xmu_fbo->Restore();
ImGui::PopStyleVar(); // ItemSpacing
ImGui::Columns(1);
SectionTitle("Mapping");
ImVec4 tc = ImGui::GetStyle().Colors[ImGuiCol_Header];
tc.w = 0.0f;
ImGui::PushStyleColor(ImGuiCol_Header, tc);
if (ImGui::CollapsingHeader("Input Mapping")) {
float p = ImGui::GetFrameHeight() * 0.3;
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(p, p));
if (ImGui::BeginTable("input_remap_tbl", 2,
ImGuiTableFlags_RowBg |
ImGuiTableFlags_Borders)) {
ImGui::TableSetupColumn("Emulated Input");
ImGui::TableSetupColumn("Host Input");
ImGui::TableHeadersRow();
PopulateTableController(bound_state);
ImGui::EndTable();
}
ImGui::PopStyleVar();
}
if (bound_state->type == INPUT_DEVICE_SDL_GAMEPAD) {
Toggle("Enable Rumble",
&bound_state->controller_map->enable_rumble);
Toggle("Invert Left X Axis",
&bound_state->controller_map->controller_mapping
.invert_axis_left_x);
Toggle("Invert Left Y Axis",
&bound_state->controller_map->controller_mapping
.invert_axis_left_y);
Toggle("Invert Right X Axis",
&bound_state->controller_map->controller_mapping
.invert_axis_right_x);
Toggle("Invert Right Y Axis",
&bound_state->controller_map->controller_mapping
.invert_axis_right_y);
}
if (ImGui::Button("Reset to Default")) {
xemu_input_reset_input_mapping(bound_state);
}
ImGui::PopStyleColor();
ImGui::PopID();
}
SectionTitle("Options");
Toggle("Auto-bind controllers", &g_config.input.auto_bind,
"Bind newly connected controllers to any open port");
Toggle("Background controller input capture",
&g_config.input.background_input_capture,
"Capture even if window is unfocused (requires restart)");
}
void MainMenuInputView::Hide()
{
m_rebinding = nullptr;
}
void MainMenuInputView::PopulateTableController(ControllerState *state)
{
if (!state)
return;
// Must match g_keyboard_scancode_map and the controller
// button map below.
static constexpr const char *face_button_index_to_name_map[15] = {
"A",
"B",
"X",
"Y",
"Back",
"Guide",
"Start",
"Left Stick Button",
"Right Stick Button",
"White",
"Black",
"DPad Up",
"DPad Down",
"DPad Left",
"DPad Right",
};
// Must match g_keyboard_scancode_map[15:]. Each axis requires
// two keys for the positive and negative direction with the
// exception of the triggers, which only require one each.
static constexpr const char *keyboard_stick_index_to_name_map[10] = {
"Left Stick Up",
"Left Stick Left",
"Left Stick Right",
"Left Stick Down",
"Left Trigger",
"Right Stick Up",
"Right Stick Left",
"Right Stick Right",
"Right Stick Down",
"Right Trigger",
};
// Must match controller axis map below.
static constexpr const char *gamepad_axis_index_to_name_map[6] = {
"Left Stick Axis X",
"Left Stick Axis Y",
"Right Stick Axis X",
"Right Stick Axis Y",
"Left Trigger Axis",
"Right Trigger Axis",
};
bool is_keyboard = state->type == INPUT_DEVICE_SDL_KEYBOARD;
int num_axis_mappings;
const char *const *axis_index_to_name_map;
if (is_keyboard) {
num_axis_mappings = std::size(keyboard_stick_index_to_name_map);
axis_index_to_name_map = keyboard_stick_index_to_name_map;
} else {
num_axis_mappings = std::size(gamepad_axis_index_to_name_map);
axis_index_to_name_map = gamepad_axis_index_to_name_map;
}
constexpr int num_face_buttons = std::size(face_button_index_to_name_map);
const int table_rows = num_axis_mappings + num_face_buttons;
for (int i = 0; i < table_rows; ++i) {
ImGui::TableNextRow();
// Button/Axis Name Column
ImGui::TableSetColumnIndex(0);
if (i < num_face_buttons) {
ImGui::Text("%s", face_button_index_to_name_map[i]);
} else {
ImGui::Text("%s", axis_index_to_name_map[i - num_face_buttons]);
}
// Button Binding Column
ImGui::TableSetColumnIndex(1);
if (m_rebinding && m_rebinding->GetTableRow() == i) {
ImGui::Text("Press a key to rebind");
continue;
}
const char *remap_button_text = "Invalid";
if (is_keyboard) {
// g_keyboard_scancode_map includes both face buttons and axis buttons.
int keycode = *(g_keyboard_scancode_map[i]);
if (keycode != SDL_SCANCODE_UNKNOWN) {
remap_button_text =
SDL_GetScancodeName(static_cast<SDL_Scancode>(keycode));
}
} else if (i < num_face_buttons) {
int *button_map[num_face_buttons] = {
&state->controller_map->controller_mapping.a,
&state->controller_map->controller_mapping.b,
&state->controller_map->controller_mapping.x,
&state->controller_map->controller_mapping.y,
&state->controller_map->controller_mapping.back,
&state->controller_map->controller_mapping.guide,
&state->controller_map->controller_mapping.start,
&state->controller_map->controller_mapping.lstick_btn,
&state->controller_map->controller_mapping.rstick_btn,
&state->controller_map->controller_mapping.lshoulder,
&state->controller_map->controller_mapping.rshoulder,
&state->controller_map->controller_mapping.dpad_up,
&state->controller_map->controller_mapping.dpad_down,
&state->controller_map->controller_mapping.dpad_left,
&state->controller_map->controller_mapping.dpad_right,
};
int button = *(button_map[i]);
if (button != SDL_GAMEPAD_BUTTON_INVALID) {
remap_button_text = SDL_GetGamepadStringForButton(
static_cast<SDL_GamepadButton>(button));
}
} else {
int *axis_map[6] = {
&state->controller_map->controller_mapping.axis_left_x,
&state->controller_map->controller_mapping.axis_left_y,
&state->controller_map->controller_mapping.axis_right_x,
&state->controller_map->controller_mapping.axis_right_y,
&state->controller_map->controller_mapping
.axis_trigger_left,
&state->controller_map->controller_mapping
.axis_trigger_right,
};
int axis = *(axis_map[i - num_face_buttons]);
if (axis != SDL_GAMEPAD_AXIS_INVALID) {
remap_button_text = SDL_GetGamepadStringForAxis(
static_cast<SDL_GamepadAxis>(axis));
}
}
ImGui::PushID(i);
float tw = ImGui::CalcTextSize(remap_button_text).x;
auto &style = ImGui::GetStyle();
float max_button_width =
tw + g_viewport_mgr.m_scale * 2 * style.FramePadding.x;
float min_button_width = ImGui::GetColumnWidth(1) / 2;
float button_width = std::max(min_button_width, max_button_width);
if (ImGui::Button(remap_button_text, ImVec2(button_width, 0))) {
if (is_keyboard) {
m_rebinding =
std::make_unique<ControllerKeyboardRebindingMap>(i);
} else {
m_rebinding =
std::make_unique<ControllerGamepadRebindingMap>(i,
state);
}
}
ImGui::PopID();
}
}
void MainMenuDisplayView::Draw()
{
SectionTitle("Renderer");
ChevronCombo("Backend", &g_config.display.renderer,
"Null\0"
"OpenGL\0"
#ifdef CONFIG_VULKAN
"Vulkan\0"
#endif
,
"Select desired renderer implementation");
int rendering_scale = nv2a_get_surface_scale_factor() - 1;
if (ChevronCombo("Internal resolution scale", &rendering_scale,
"1x\0"
"2x\0"
"3x\0"
"4x\0"
"5x\0"
"6x\0"
"7x\0"
"8x\0"
"9x\0"
"10x\0",
"Increase surface scaling factor for higher quality")) {
nv2a_set_surface_scale_factor(rendering_scale+1);
}
SectionTitle("Window");
bool fs = xemu_is_fullscreen();
if (Toggle("Fullscreen", &fs, "Enable fullscreen now")) {
xemu_toggle_fullscreen();
}
Toggle("Fullscreen on startup",
&g_config.display.window.fullscreen_on_startup,
"Start xemu in fullscreen when opened");
Toggle("Exclusive fullscreen",
&g_config.display.window.fullscreen_exclusive,
"May improve responsiveness, but slows window switching");
if (g_config.display.window.fullscreen_exclusive) {
// Get available fullscreen display modes
SDL_DisplayID display = SDL_GetDisplayForWindow(xemu_get_window());
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes && num_modes > 0) {
// Create a list of mode strings
std::vector<std::string> mode_strings;
for (int i = 0; i < num_modes; i++) {
char buf[64];
snprintf(buf, sizeof(buf), "%dx%d @ %.0fHz", modes[i]->w, modes[i]->h, modes[i]->refresh_rate);
mode_strings.push_back(buf);
}
// Create null-separated string for ChevronCombo
std::string items;
for (const auto& str : mode_strings) {
items += str;
items += '\0';
}
items += '\0'; // Double null terminate
ChevronCombo("Fullscreen resolution", &g_config.display.window.fullscreen_resolution,
items.c_str(), "Select preferred fullscreen resolution");
}
SDL_free(modes);
}
if (ChevronCombo("Window size", &g_config.display.window.startup_size,
"Last Used\0"
"640x480\0"
"720x480\0"
"1280x720\0"
"1280x800\0"
"1280x960\0"
"1920x1080\0"
"2560x1440\0"
"2560x1600\0"
"2560x1920\0"
"3840x2160\0",
"Select preferred startup window size")) {
}
Toggle("Vertical refresh sync", &g_config.display.window.vsync,
"Sync to screen vertical refresh to reduce tearing artifacts");
SectionTitle("Interface");
Toggle("Show main menu bar", &g_config.display.ui.show_menubar,
"Show main menu bar when mouse is activated");
Toggle("Show notifications", &g_config.display.ui.show_notifications,
"Display notifications in upper-right corner");
Toggle("Hide mouse cursor", &g_config.display.ui.hide_cursor,
"Hide the mouse cursor when it is not moving");
int ui_scale_idx;
if (g_config.display.ui.auto_scale) {
ui_scale_idx = 0;
} else {
ui_scale_idx = g_config.display.ui.scale;
if (ui_scale_idx < 0) ui_scale_idx = 0;
else if (ui_scale_idx > 2) ui_scale_idx = 2;
}
if (ChevronCombo("UI scale", &ui_scale_idx,
"Auto\0"
"1x\0"
"2x\0",
"Interface element scale")) {
if (ui_scale_idx == 0) {
g_config.display.ui.auto_scale = true;
} else {
g_config.display.ui.auto_scale = false;
g_config.display.ui.scale = ui_scale_idx;
}
}
Toggle("Animations", &g_config.display.ui.use_animations,
"Enable xemu user interface animations");
ChevronCombo("Display mode", &g_config.display.ui.fit,
"Center\0"
"Scale\0"
"Stretch\0",
"Select how the framebuffer should fit or scale into the window");
ChevronCombo("Aspect ratio", &g_config.display.ui.aspect_ratio,
"Native\0"
"Auto (Default)\0"
"4:3\0"
"16:9\0",
"Select the displayed aspect ratio");
}
void MainMenuAudioView::Draw()
{
SectionTitle("Volume");
char buf[32];
snprintf(buf, sizeof(buf), "Limit output volume (%d%%)",
(int)(g_config.audio.volume_limit * 100));
Slider("Output volume limit", &g_config.audio.volume_limit, buf);
SectionTitle("Quality");
Toggle("Real-time DSP processing", &g_config.audio.use_dsp,
"Enable improved audio accuracy (experimental)");
}
NetworkInterface::NetworkInterface(pcap_if_t *pcap_desc, char *_friendlyname)
{
m_pcap_name = pcap_desc->name;
m_description = pcap_desc->description ?: pcap_desc->name;
if (_friendlyname) {
char *tmp =
g_strdup_printf("%s (%s)", _friendlyname, m_description.c_str());
m_friendly_name = tmp;
g_free((gpointer)tmp);
} else {
m_friendly_name = m_description;
}
}
NetworkInterfaceManager::NetworkInterfaceManager()
{
m_current_iface = NULL;
m_failed_to_load_lib = false;
}
void NetworkInterfaceManager::Refresh(void)
{
pcap_if_t *alldevs, *iter;
char err[PCAP_ERRBUF_SIZE];
if (xemu_net_is_enabled()) {
return;
}
#if defined(_WIN32)
if (pcap_load_library()) {
m_failed_to_load_lib = true;
return;
}
#endif
m_ifaces.clear();
m_current_iface = NULL;
if (pcap_findalldevs(&alldevs, err)) {
return;
}
for (iter=alldevs; iter != NULL; iter=iter->next) {
#if defined(_WIN32)
char *friendly_name = get_windows_interface_friendly_name(iter->name);
m_ifaces.emplace_back(new NetworkInterface(iter, friendly_name));
if (friendly_name) {
g_free((gpointer)friendly_name);
}
#else
m_ifaces.emplace_back(new NetworkInterface(iter));
#endif
if (!strcmp(g_config.net.pcap.netif, iter->name)) {
m_current_iface = m_ifaces.back().get();
}
}
pcap_freealldevs(alldevs);
}
void NetworkInterfaceManager::Select(NetworkInterface &iface)
{
m_current_iface = &iface;
xemu_settings_set_string(&g_config.net.pcap.netif,
iface.m_pcap_name.c_str());
}
bool NetworkInterfaceManager::IsCurrent(NetworkInterface &iface)
{
return &iface == m_current_iface;
}
MainMenuNetworkView::MainMenuNetworkView()
{
should_refresh = true;
}
void MainMenuNetworkView::Draw()
{
SectionTitle("Adapter");
bool enabled = xemu_net_is_enabled();
g_config.net.enable = enabled;
if (Toggle("Enable", &g_config.net.enable,
enabled ? "Virtual network connected (disable to change network "
"settings)" :
"Connect virtual network cable to machine")) {
if (enabled) {
xemu_net_disable();
} else {
xemu_net_enable();
}
}
bool appearing = ImGui::IsWindowAppearing();
if (enabled) ImGui::BeginDisabled();
if (ChevronCombo(
"Attached to", &g_config.net.backend,
"NAT\0"
"UDP Tunnel\0"
"Bridged Adapter\0",
"Controls what the virtual network controller interfaces with")) {
appearing = true;
}
SectionTitle("Options");
switch (g_config.net.backend) {
case CONFIG_NET_BACKEND_PCAP:
DrawPcapOptions(appearing);
break;
case CONFIG_NET_BACKEND_NAT:
DrawNatOptions(appearing);
break;
case CONFIG_NET_BACKEND_UDP:
DrawUdpOptions(appearing);
break;
default: break;