/
wxvbam.cpp
1070 lines (889 loc) · 32.7 KB
/
wxvbam.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
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
// mainline:
// parse cmd line
// load xrc file (guiinit.cpp does most of instantiation)
// create & display main frame
#include "wxvbam.h"
#include <stdio.h>
#include <wx/cmdline.h>
#include <wx/file.h>
#include <wx/filesys.h>
#include <wx/fs_arc.h>
#include <wx/fs_mem.h>
#include <wx/mstream.h>
#include <wx/progdlg.h>
#include <wx/protocol/http.h>
#include <wx/regex.h>
#include <wx/sstream.h>
#include <wx/txtstrm.h>
#include <wx/url.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include "wayland.h"
#include "strutils.h"
// The built-in xrc file
#include "builtin-xrc.h"
// The built-in vba-over.ini
#include "../common/ConfigManager.h"
#include "builtin-over.h"
IMPLEMENT_APP(wxvbamApp)
IMPLEMENT_DYNAMIC_CLASS(MainFrame, wxFrame)
// generate config file path
static void get_config_path(wxPathList& path, bool exists = true)
{
// we want paths with "visualboyadvance-m" not "vbam", so change appname temporarily
wxString current_app_name = wxGetApp().GetAppName();
// keep config path as ~/.vbam on UNIX for now for backcompat
#if defined(__WXMSW__) || defined(__WXMAC__)
wxGetApp().SetAppName(_("visualboyadvance-m"));
#endif
// local config dir first, then global
// locale-specific res first, then main
wxStandardPathsBase& stdp = wxStandardPaths::Get();
#define add_path(p) \
do { \
const wxString& s = stdp.p; \
wxFileName parent = wxFileName::DirName(s + wxT("//..")); \
parent.MakeAbsolute(); \
if ((wxDirExists(s) && wxIsWritable(s)) || ((!exists || !wxDirExists(s)) && parent.IsDirWritable())) \
path.Add(s); \
} while (0)
static bool debug_dumped = false;
if (!debug_dumped) {
wxLogDebug(wxT("GetUserLocalDataDir(): %s"), stdp.GetUserLocalDataDir().mb_str());
wxLogDebug(wxT("GetUserDataDir(): %s"), stdp.GetUserDataDir().mb_str());
wxLogDebug(wxT("GetLocalizedResourcesDir(wxGetApp().locale.GetCanonicalName()): %s"), stdp.GetLocalizedResourcesDir(wxGetApp().locale.GetCanonicalName()).mb_str());
wxLogDebug(wxT("GetResourcesDir(): %s"), stdp.GetResourcesDir().mb_str());
wxLogDebug(wxT("GetDataDir(): %s"), stdp.GetDataDir().mb_str());
wxLogDebug(wxT("GetLocalDataDir(): %s"), stdp.GetLocalDataDir().mb_str());
wxLogDebug(wxT("GetPluginsDir(): %s"), stdp.GetPluginsDir().mb_str());
debug_dumped = true;
}
// NOTE: this does not support XDG (freedesktop.org) paths
add_path(GetUserLocalDataDir());
add_path(GetUserDataDir());
add_path(GetLocalizedResourcesDir(wxGetApp().locale.GetCanonicalName()));
add_path(GetResourcesDir());
add_path(GetDataDir());
add_path(GetLocalDataDir());
add_path(GetPluginsDir());
wxGetApp().SetAppName(current_app_name);
}
static void tack_full_path(wxString& s, const wxString& app = wxEmptyString)
{
// regenerate path, including nonexistent dirs this time
wxPathList full_config_path;
get_config_path(full_config_path, false);
for (int i = 0; i < full_config_path.size(); i++)
s += wxT("\n\t") + full_config_path[i] + app;
}
wxString wxvbamApp::GetConfigurationPath()
{
// first check if config files exists in reverse order
// (from system paths to more local paths.)
if (data_path.empty()) {
get_config_path(config_path);
for (int i = config_path.size() - 1; i >= 0; i--) {
wxFileName fn(config_path[i], wxT("vbam.ini"));
if (fn.FileExists() && fn.IsFileWritable()) {
data_path = config_path[i];
break;
}
}
}
// if no config file was not found, search for writable config
// dir or parent to create it in in OnInit in normal order
// (from user paths to system paths.)
if (data_path.empty()) {
for (int i = 0; i < config_path.size(); i++) {
// Check if path is writeable
if (wxIsWritable(config_path[i])) {
data_path = config_path[i];
break;
}
// check if parent of path is writable, so we can
// create the path in OnInit
wxFileName parent_dir = wxFileName::DirName(config_path[i] + wxT("//.."));
parent_dir.MakeAbsolute();
if (parent_dir.IsDirWritable()) {
data_path = config_path[i];
break;
}
}
}
return data_path;
}
wxString wxvbamApp::GetAbsolutePath(wxString path)
{
wxFileName fn(path);
if (fn.IsRelative()) {
fn.MakeRelativeTo(GetConfigurationPath());
fn.Normalize();
return fn.GetFullPath();
}
return path;
}
bool wxvbamApp::OnInit()
{
// set up logging
#ifndef NDEBUG
wxLog::SetLogLevel(wxLOG_Trace);
#endif
// turn off output buffering on Windows to support mintty
#ifdef __WXMSW__
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
dup2(1, 2); // redirect stderr to stdout
#endif
using_wayland = IsItWayland();
// use consistent names for config
SetAppName(_("vbam"));
#if (wxMAJOR_VERSION >= 3)
SetAppDisplayName(_T("VisualBoyAdvance-M"));
#endif
// load system default locale, if available
locale.Init();
locale.AddCatalog(_T("wxvbam"));
// make built-in xrc file available
// this has to be done before parent OnInit() so xrc dump works
wxFileSystem::AddHandler(new wxMemoryFSHandler);
wxFileSystem::AddHandler(new wxArchiveFSHandler);
wxMemoryFSHandler::AddFileWithMimeType(wxT("wxvbam.xrs"), builtin_xrs, sizeof(builtin_xrs), wxT("application/zip"));
if (!wxApp::OnInit())
return false;
// prepare for loading xrc files
wxXmlResource* xr = wxXmlResource::Get();
// note: if linking statically, next 2 pull in lot of unused code
// maybe in future if not wxSHARED, load only builtin-needed handlers
xr->InitAllHandlers();
wxInitAllImageHandlers();
get_config_path(config_path);
// first, load override xrcs
// this can only override entire root nodes
// 2.9 has LoadAllFiles(), but this is 2.8, so we'll do it manually
wxString cwd = wxGetCwd();
for (int i = 0; i < config_path.size(); i++)
if (wxDirExists(config_path[i]) && wxSetWorkingDirectory(config_path[i])) {
// *.xr[cs] doesn't work (double the number of scans)
// 2.9 gives errors for no files found, so manual precheck needed
// (yet another double the number of scans)
if (!wxFindFirstFile(wxT("*.xrc")).empty())
xr->Load(wxT("*.xrc"));
if (!wxFindFirstFile(wxT("*.xrs")).empty())
xr->Load(wxT("*.xrs"));
}
wxFileName xrcDir(GetConfigurationPath() + wxT("//xrc"), wxEmptyString);
if (xrcDir.DirExists() && wxSetWorkingDirectory(xrcDir.GetFullPath()) && !wxFindFirstFile(wxT("*.xrc")).empty()) {
xr->Load(wxT("*.xrc"));
} else {
// finally, load built-in xrc
xr->Load(wxT("memory:wxvbam.xrs"));
}
wxSetWorkingDirectory(cwd);
// set up config file
// this needs to be in a subdir to support other config as well
// but subdir flag behaves differently 2.8 vs. 2.9. Oh well.
// NOTE: this does not support XDG (freedesktop.org) paths
#if defined(__WXMSW__) || defined(__APPLE__)
wxFileName vbamconf(GetConfigurationPath(), _T("vbam.ini"));
cfg = new wxFileConfig(wxT("vbam"), wxEmptyString,
vbamconf.GetFullPath(),
wxEmptyString, wxCONFIG_USE_LOCAL_FILE);
#else
cfg = new wxFileConfig(wxEmptyString, wxEmptyString, wxEmptyString,
wxEmptyString,
// style =
wxCONFIG_USE_GLOBAL_FILE | wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_SUBDIR);
#endif
// set global config for e.g. Windows font mapping
wxFileConfig::Set(cfg);
// yet another bug/deficiency in wxConfig: dirs are not created if needed
// since a default config is always written, dirs are always needed
// Can't figure out statically if using wxFileConfig w/o duplicating wx's
// logic, so do it at run-time
// wxFileConfig *f = wxDynamicCast(cfg, wxFileConfig);
// wxConfigBase does not derive from wxObject!!! so no wxDynamicCast
wxFileConfig* fc = dynamic_cast<wxFileConfig*>(cfg);
if (fc) {
wxFileName s(wxFileConfig::GetLocalFileName(GetAppName()));
// at least up to 2.8.12, GetLocalFileName returns the dir if
// SUBDIR is specified instead of actual file name
// and SUBDIR only affects UNIX
#if defined(__UNIX__) && !wxCHECK_VERSION(2, 9, 0)
s.AppendDir(s.GetFullName());
#endif
// only the path part gets created
// note that 0777 is default (assumes umask will do og-w)
s.Mkdir(0777, wxPATH_MKDIR_FULL);
s = wxFileName::DirName(GetConfigurationPath());
s.Mkdir(0777, wxPATH_MKDIR_FULL);
}
load_opts();
// wxGLCanvas segfaults under wayland
if (UsingWayland() && gopts.render_method == RND_OPENGL) {
gopts.render_method = RND_SIMPLE;
}
// process command-line options
for (int i = 0; i < pending_optset.size(); i++) {
auto parts = str_split(pending_optset[i], wxT('='));
opt_set(parts[0], parts[1]);
}
pending_optset.clear();
wxFileName vba_over(GetConfigurationPath(), wxT("vba-over.ini"));
wxFileName rdb(GetConfigurationPath(), wxT("Nintendo - Game Boy Advance*.dat"));
wxFileName scene_rdb(GetConfigurationPath(), wxT("Nintendo - Game Boy Advance (Scene)*.dat"));
wxFileName nointro_rdb(GetConfigurationPath(), wxT("Official No-Intro Nintendo Gameboy Advance Number (Date).xml"));
wxString f = wxFindFirstFile(nointro_rdb.GetFullPath(), wxFILE);
if (!f.empty() && wxFileName(f).IsFileReadable())
rom_database_nointro = f;
f = wxFindFirstFile(scene_rdb.GetFullPath(), wxFILE);
if (!f.empty() && wxFileName(f).IsFileReadable())
rom_database_scene = f;
f = wxFindFirstFile(rdb.GetFullPath(), wxFILE);
while (!f.empty()) {
if (f == rom_database_scene.GetFullPath()) {
f = wxFindNextFile();
} else if (wxFileName(f).IsFileReadable()) {
rom_database = f;
break;
}
}
// load vba-over.ini
// rather than dealing with wxConfig's broken search path, just use
// the same one that the xrc overrides use
// this also allows us to override a group at a time, add commments, and
// add the file from which the group came
wxMemoryInputStream mis(builtin_over, sizeof(builtin_over));
overrides = new wxFileConfig(mis);
wxRegEx cmtre;
// not the most efficient thing to do: read entire file into a string
// just to parse the comments out
wxString bovs((const char*)builtin_over, wxConvUTF8, sizeof(builtin_over));
bool cont;
wxString s;
long grp_idx;
#define CMT_RE_START wxT("(^|[\n\r])# ?([^\n\r]*)(\r?\n|\r)\\[")
for (cont = overrides->GetFirstGroup(s, grp_idx); cont;
cont = overrides->GetNextGroup(s, grp_idx)) {
// apparently even MacOSX sometimes uses the old \r by itself
wxString cmt(CMT_RE_START);
cmt += s + wxT("\\]");
if (cmtre.Compile(cmt) && cmtre.Matches(bovs))
cmt = cmtre.GetMatch(bovs, 2);
else
cmt = wxEmptyString;
overrides->Write(s + wxT("/comment"), cmt);
}
if (vba_over.FileExists()) {
wxStringOutputStream sos;
wxFileInputStream fis(vba_over.GetFullPath());
// not the most efficient thing to do: read entire file into a string
// just to parse the comments out
fis.Read(sos);
// rather than assuming the file is seekable, use the string we just
// read as an input stream
wxStringInputStream sis(sos.GetString());
wxFileConfig ov(sis);
for (cont = ov.GetFirstGroup(s, grp_idx); cont;
cont = ov.GetNextGroup(s, grp_idx)) {
overrides->DeleteGroup(s);
overrides->SetPath(s);
ov.SetPath(s);
overrides->Write(wxT("path"), GetConfigurationPath());
// apparently even MacOSX sometimes uses \r by itself
wxString cmt(CMT_RE_START);
cmt += s + wxT("\\]");
if (cmtre.Compile(cmt) && cmtre.Matches(sos.GetString()))
cmt = cmtre.GetMatch(sos.GetString(), 2);
else
cmt = wxEmptyString;
overrides->Write(wxT("comment"), cmt);
long ent_idx;
for (cont = ov.GetFirstEntry(s, ent_idx); cont;
cont = ov.GetNextEntry(s, ent_idx))
overrides->Write(s, ov.Read(s, wxEmptyString));
ov.SetPath(wxT("/"));
overrides->SetPath(wxT("/"));
}
}
// create the main window
frame = wxDynamicCast(xr->LoadFrame(NULL, wxT("MainFrame")), MainFrame);
if (!frame) {
wxLogError(_("Could not create main window"));
return false;
}
// Create() cannot be overridden easily
if (!frame->BindControls())
return false;
frame->Show(true);
return true;
}
void wxvbamApp::OnInitCmdLine(wxCmdLineParser& cl)
{
wxApp::OnInitCmdLine(cl);
cl.SetLogo(wxT("VisualBoyAdvance-M\n"));
// 2.9 decided to change all of these from wxChar to char for some
// reason
#if wxCHECK_VERSION(2, 9, 0)
// N_(x) returns x
#define t(x) x
#else
// N_(x) returns wxT(x)
#define t(x) wxT(x)
#endif
// while I would rather the long options be translated, there is merit
// to the idea that command-line syntax should not change based on
// locale
static wxCmdLineEntryDesc opttab[] = {
{ wxCMD_LINE_OPTION, NULL, t("save-xrc"),
N_("Save built-in XRC file and exit") },
{ wxCMD_LINE_OPTION, NULL, t("save-over"),
N_("Save built-in vba-over.ini and exit") },
{ wxCMD_LINE_SWITCH, NULL, t("print-cfg-path"),
N_("Print configuration path and exit") },
{ wxCMD_LINE_SWITCH, t("f"), t("fullscreen"),
N_("Start in full-screen mode") },
#if !defined(NO_LINK) && !defined(__WXMSW__)
{ wxCMD_LINE_SWITCH, t("s"), t("delete-shared-state"),
N_("Delete shared link state first, if it exists") },
#endif
// stupid wx cmd line parser doesn't support duplicate options
// { wxCMD_LINE_OPTION, t("o"), t("option"),
// _("Set configuration option; <opt>=<value> or help for list"),
{
wxCMD_LINE_SWITCH, t("o"), t("list-options"),
N_("List all settable options and exit") },
{ wxCMD_LINE_PARAM, NULL, NULL,
N_("ROM file"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_PARAM, NULL, NULL,
N_("<config>=<value>"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_NONE }
};
// 2.9 automatically translates desc, but 2.8 doesn't
#if !wxCHECK_VERSION(2, 9, 0)
for (int i = 0; opttab[i].kind != wxCMD_LINE_NONE; i++)
opttab[i].description = wxGetTranslation(opttab[i].description);
#endif
// note that "SetDesc" actually Adds rather than replaces
// so system standard (port-dependent) options are still included:
// -h/--help --verbose --theme --mode
cl.SetDesc(opttab);
}
bool wxvbamApp::OnCmdLineParsed(wxCmdLineParser& cl)
{
if (!wxApp::OnCmdLineParsed(cl))
return false;
wxString s;
if (cl.Found(wxT("save-xrc"), &s)) {
// This was most likely done on a command line, so use
// stderr instead of gui for messages
wxLog::SetActiveTarget(new wxLogStderr);
wxFileSystem fs;
wxFSFile* f = fs.OpenFile(wxT("memory:wxvbam.xrs#zip:wxvbam.xrs$wxvbam.xrc"));
if (!f) {
wxLogError(_("Configuration/build error: can't find built-in xrc"));
return false;
}
wxFileOutputStream os(s);
os.Write(*f->GetStream());
delete f;
wxString lm;
lm.Printf(_("Wrote built-in configuration to %s.\n"
"To override, remove all but changed root node(s). "
"First found root node of correct name in any .xrc or "
".xrs files in following search path overrides built-in:"),
s.mb_str());
tack_full_path(lm);
wxLogMessage(lm);
return false;
}
if (cl.Found(wxT("print-cfg-path"))) {
// This was most likely done on a command line, so use
// stderr instead of gui for messages
wxLog::SetActiveTarget(new wxLogStderr);
wxString lm(_("Configuration is read from, in order:"));
tack_full_path(lm);
wxLogMessage(lm);
return false;
}
if (cl.Found(wxT("save-over"), &s)) {
// This was most likely done on a command line, so use
// stderr instead of gui for messages
wxLog::SetActiveTarget(new wxLogStderr);
wxFileOutputStream os(s);
os.Write(builtin_over, sizeof(builtin_over));
wxString lm;
lm.Printf(_("Wrote built-in override file to %s\n"
"To override, delete all but changed section. First found section is used from search path:"),
s.mb_str());
wxString oi = wxFileName::GetPathSeparator();
oi += wxT("vba-over.ini");
tack_full_path(lm, oi);
lm.append(_("\n\tbuilt-in"));
wxLogMessage(lm);
return false;
}
if (cl.Found(wxT("f"))) {
pending_fullscreen = true;
}
if (cl.Found(wxT("o"))) {
wxPrintf(_("Options set from the command line are saved if any"
" configuration changes are made in the user interface.\n\n"
"For flag options, true and false are specified as 1 and 0, respectively.\n\n"));
for (int i = 0; i < num_opts; i++) {
wxPrintf(wxT("%s (%s"), opts[i].opt,
opts[i].boolopt ? wxT("flag") : opts[i].stropt ? wxT("string") : !opts[i].enumvals.empty() ? opts[i].enumvals : (wxString)(opts[i].intopt ? wxT("int") : opts[i].doubleopt ? wxT("decimal") : wxT("string")));
if (!opts[i].enumvals.empty()) {
const wxString evx = wxGetTranslation(opts[i].enumvals);
if (wxStrcmp(evx, opts[i].enumvals))
wxPrintf(wxT(" = %s"), evx);
}
wxPrintf(wxT(")\n\t%s\n\n"), opts[i].desc);
if (!opts[i].enumvals.empty())
opts[i].enumvals = wxGetTranslation(opts[i].enumvals);
}
wxPrintf(_("The commands available for the Keyboard/* option are:\n\n"));
for (int i = 0; i < ncmds; i++)
wxPrintf(wxT("%s (%s)\n"), cmdtab[i].cmd, cmdtab[i].name);
return false;
}
#if !defined(NO_LINK) && !defined(__WXMSW__)
if (cl.Found(wxT("s"))) {
CleanLocalLink();
}
#endif
int nparm = cl.GetParamCount();
bool complained = false, gotfile = false;
for (int i = 0; i < nparm; i++) {
auto p = cl.GetParam(i);
auto parts = str_split(p, wxT('='));
if (parts.size() > 1) {
opt_set(parts[0], parts[1]);
pending_optset.push_back(p);
}
else {
if (!gotfile) {
pending_load = p;
gotfile = true;
} else {
if (!complained) {
wxFprintf(stderr, _("Bad configuration option or multiple ROM files given:\n"));
wxFprintf(stderr, wxT("%s\n"), pending_load.mb_str());
complained = true;
}
wxFprintf(stderr, wxT("%s\n"), p);
}
}
}
home = strdup(wxString(wxApp::argv[0]).char_str());
SetHome(home);
LoadConfig(); // Parse command line arguments (overrides ini)
ReadOpts(argc, (char**)argv);
return true;
}
wxvbamApp::~wxvbamApp() {
free(home);
}
MainFrame::MainFrame()
: wxFrame()
, focused(false)
, paused(false)
, menus_opened(0)
, dialog_opened(0)
{
}
MainFrame::~MainFrame()
{
#ifndef NO_LINK
CloseLink();
#endif
}
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
#include "cmd-evtable.h"
EVT_CONTEXT_MENU(MainFrame::OnMenu)
// this is the main window focus? Or EVT_SET_FOCUS/EVT_KILL_FOCUS?
EVT_ACTIVATE(MainFrame::OnActivate)
// requires DragAcceptFiles(true); even then may not do anything
EVT_DROP_FILES(MainFrame::OnDropFile)
// pause game if menu pops up
//
// This is a feature most people don't like, and it causes problems with
// keyboard game keys on mac, so we will disable it for now.
//
// On Winodws, there will still be a pause because of how the windows event
// model works, in addition the audio will loop with SDL, so we still pause on
// Windows, TODO: this needs to be fixed properly
//
#ifdef __WXMSW__
EVT_MENU_OPEN(MainFrame::MenuPopped)
EVT_MENU_CLOSE(MainFrame::MenuPopped)
EVT_MENU_HIGHLIGHT_ALL(MainFrame::MenuPopped)
#endif
END_EVENT_TABLE()
void MainFrame::OnActivate(wxActivateEvent& event)
{
focused = event.GetActive();
if (panel && focused)
panel->SetFocus();
if (pauseWhenInactive) {
if (panel && focused) {
panel->Resume();
}
else if (panel && !focused) {
panel->Pause();
}
}
}
void MainFrame::OnDropFile(wxDropFilesEvent& event)
{
wxString* f = event.GetFiles();
// ignore all but last
wxGetApp().pending_load = f[event.GetNumberOfFiles() - 1];
}
void MainFrame::OnMenu(wxContextMenuEvent& event)
{
if (IsFullScreen() && ctx_menu) {
wxPoint p(event.GetPosition());
#if 0 // wx actually recommends ignoring the position
if (p != wxDefaultPosition)
p = ScreenToClient(p);
#endif
PopupMenu(ctx_menu, p);
}
}
wxString MainFrame::GetGamePath(wxString path)
{
wxString game_path = path;
if (game_path.size()) {
game_path = wxGetApp().GetAbsolutePath(game_path);
} else {
game_path = panel->game_dir();
wxFileName::Mkdir(game_path, 0777, wxPATH_MKDIR_FULL);
}
if (!wxFileName::DirExists(game_path))
game_path = wxFileName::GetCwd();
if (!wxIsWritable(game_path))
game_path = wxGetApp().GetConfigurationPath();
return game_path;
}
void MainFrame::SetJoystick()
{
bool anyjoy = false;
joy.Remove();
if (!emulating)
return;
for (int i = 0; i < 4; i++)
for (int j = 0; j < NUM_KEYS; j++) {
wxJoyKeyBinding_v b = gopts.joykey_bindings[i][j];
for (int k = 0; k < b.size(); k++) {
int jn = b[k].joy;
if (jn) {
if (!anyjoy) {
anyjoy = true;
joy.Attach(panel);
}
joy.Add(jn - 1);
}
}
}
}
void MainFrame::enable_menus()
{
for (int i = 0; i < ncmds; i++)
if (cmdtab[i].mask_flags && cmdtab[i].mi)
cmdtab[i].mi->Enable((cmdtab[i].mask_flags & cmd_enable) != 0);
if (cmd_enable & CMDEN_SAVST)
for (int i = 0; i < 10; i++)
if (loadst_mi[i])
loadst_mi[i]->Enable(state_ts[i].IsValid());
}
void MainFrame::SetRecentAccels()
{
for (int i = 0; i < 10; i++) {
wxMenuItem* mi = recent->FindItem(i + wxID_FILE1);
if (!mi)
break;
// if command is 0, there is no accel
DoSetAccel(mi, recent_accel[i].GetCommand() ? &recent_accel[i] : NULL);
}
}
void MainFrame::update_state_ts(bool force)
{
bool any_states = false;
for (int i = 0; i < 10; i++) {
if (force)
state_ts[i] = wxInvalidDateTime;
if (panel->game_type() != IMAGE_UNKNOWN) {
wxString fn;
fn.Printf(SAVESLOT_FMT, panel->game_name().mb_str(), i + 1);
wxFileName fp(panel->state_dir(), fn);
wxDateTime ts; // = wxInvalidDateTime
if (fp.IsFileReadable()) {
ts = fp.GetModificationTime();
any_states = true;
}
// if(ts != state_ts[i] || (force && !ts.IsValid())) {
// to prevent assertions (stupid wx):
if (ts.IsValid() != state_ts[i].IsValid() || (ts.IsValid() && ts != state_ts[i]) || (force && !ts.IsValid())) {
// wx has no easy way of doing the -- bit independent
// of locale
// so use a real date and substitute all digits
wxDateTime fts = ts.IsValid() ? ts : wxDateTime::Now();
wxString df = fts.Format(wxT("0&0 %x %X"));
if (!ts.IsValid())
for (int j = 0; j < df.size(); j++)
if (wxIsdigit(df[j]))
df[j] = wxT('-');
df[0] = i == 9 ? wxT('1') : wxT(' ');
df[2] = wxT('0') + (i + 1) % 10;
if (loadst_mi[i]) {
wxString lab = loadst_mi[i]->GetItemLabel();
size_t tab = lab.find(wxT('\t')), dflen = df.size();
if (tab != wxString::npos)
df.append(lab.substr(tab));
loadst_mi[i]->SetItemLabel(df);
loadst_mi[i]->Enable(ts.IsValid());
if (tab != wxString::npos)
df.resize(dflen);
}
if (savest_mi[i]) {
wxString lab = savest_mi[i]->GetItemLabel();
size_t tab = lab.find(wxT('\t'));
if (tab != wxString::npos)
df.append(lab.substr(tab));
savest_mi[i]->SetItemLabel(df);
}
}
state_ts[i] = ts;
}
}
int cmd_flg = any_states ? CMDEN_SAVST : 0;
if ((cmd_enable & CMDEN_SAVST) != cmd_flg) {
cmd_enable = (cmd_enable & ~CMDEN_SAVST) | cmd_flg;
enable_menus();
}
}
int MainFrame::oldest_state_slot()
{
wxDateTime ot;
int os = -1;
for (int i = 0; i < 10; i++) {
if (!state_ts[i].IsValid())
return i + 1;
if (os < 0 || state_ts[i] < ot) {
os = i;
ot = state_ts[i];
}
}
return os + 1;
}
int MainFrame::newest_state_slot()
{
wxDateTime nt;
int ns = -1;
for (int i = 0; i < 10; i++) {
if (!state_ts[i].IsValid())
continue;
if (ns < 0 || state_ts[i] > nt) {
ns = i;
nt = state_ts[i];
}
}
return ns + 1;
}
// disable emulator loop while menus are popped up
// not sure how to match up w/ down other than counting...
// only msw is guaranteed to only give one up & one down event for entire
// menu browsing
// if there is ever a mismatch, the game will freeze and there is no way
// to detect if it's doing that
// FIXME: this does not work.
// Not all open events are followed by close events.
// Removing the nesting counter may help, but on wxGTK I still get lockups.
void MainFrame::MenuPopped(wxMenuEvent& evt)
{
bool popped = evt.GetEventType() != wxEVT_MENU_CLOSE;
#if 0
if (popped)
++menus_opened;
else
--menus_opened;
if (menus_opened < 0) // how could this ever be???
menus_opened = 0;
#else
if (popped)
menus_opened = 1;
else
menus_opened = 0;
#endif
// workaround for lack of wxGTK mouse motion events: unblank
// pointer when menu popped up
// of course this does not help in finding the menus in the first place
// the user is better off clicking in the window or entering/
// exiting the window (which does generate a mouse event)
// it will auto-hide again once game resumes
if (popped)
panel->ShowPointer();
if (menus_opened)
panel->Pause();
else if (!IsPaused())
panel->Resume();
}
void MainFrame::SetMenusOpened(bool state)
{
if (state) {
menus_opened = 1;
paused = true;
panel->Pause();
}
else {
menus_opened = 0;
paused = false;
pause_next = false;
panel->Resume();
}
}
// ShowModal that also disables emulator loop
// uses dialog_opened as a nesting counter
int MainFrame::ShowModal(wxDialog* dlg)
{
dlg->SetWindowStyle(dlg->GetWindowStyle() | wxCAPTION | wxRESIZE_BORDER);
if (gopts.keep_on_top)
dlg->SetWindowStyle(dlg->GetWindowStyle() | wxSTAY_ON_TOP);
else
dlg->SetWindowStyle(dlg->GetWindowStyle() & ~wxSTAY_ON_TOP);
CheckPointer(dlg);
StartModal();
int ret = dlg->ShowModal();
StopModal();
return ret;
}
void MainFrame::StartModal()
{
// workaround for lack of wxGTK mouse motion events: unblank
// pointer when dialog popped up
// it will auto-hide again once game resumes
panel->ShowPointer();
panel->Pause();
++dialog_opened;
}
void MainFrame::StopModal()
{
if (!dialog_opened) // technically an error in the caller
return;
--dialog_opened;
if (!IsPaused())
panel->Resume();
}
LinkMode MainFrame::GetConfiguredLinkMode()
{
switch (gopts.gba_link_type) {
case 0:
return LINK_DISCONNECTED;
break;
case 1:
if (gopts.link_proto)
return LINK_CABLE_IPC;
else
return LINK_CABLE_SOCKET;
break;
case 2:
if (gopts.link_proto)
return LINK_RFU_IPC;
else
return LINK_RFU_SOCKET;
break;
case 3:
return LINK_GAMECUBE_DOLPHIN;
break;
case 4:
if (gopts.link_proto)
return LINK_GAMEBOY_IPC;
else
return LINK_GAMEBOY_SOCKET;
break;
default:
return LINK_DISCONNECTED;
break;
}
return LINK_DISCONNECTED;
}
void MainFrame::IdentifyRom()
{
if (!panel->rom_name.empty())
return;
panel->rom_name = panel->loaded_game.GetFullName();
wxString name;
wxString scene_rls;
wxString scene_name;
wxString rom_crc32_str;
rom_crc32_str.Printf(wxT("%08X"), panel->rom_crc32);
if (wxGetApp().rom_database_nointro.FileExists()) {
wxFileInputStream input(wxGetApp().rom_database_nointro.GetFullPath());
wxTextInputStream text(input, wxT("\x09"), wxConvUTF8);
while (input.IsOk() && !input.Eof()) {
wxString line = text.ReadLine();
if (line.Contains(wxT("<releaseNumber>"))) {
scene_rls = line.AfterFirst('>').BeforeLast('<');
}
if (line.Contains(wxT("romCRC ")) && line.Contains(rom_crc32_str)) {