cmdevents.cpp 80.8 KB
Newer Older
1
2
3
4
#ifndef NO_FFMPEG
#define __STDC_LIMIT_MACROS // required for ffmpeg
#define __STDC_CONSTANT_MACROS // required for ffmpeg
#endif
5

6
7
8
9
10
11
#include "wxvbam.h"
#include <algorithm>
#include <wx/aboutdlg.h>
#include <wx/ffile.h>
#include <wx/numdlg.h>
#include <wx/progdlg.h>
12
13
#include <wx/regex.h>
#include <wx/sstream.h>
14
#include <wx/url.h>
15
#include <wx/wfstream.h>
16

17
18
19
20
#ifndef NO_FFMPEG
extern "C" {
#include <libavformat/avformat.h>
}
21
22
// For compatibility with 3.0+ ffmpeg
#include <libavcodec/version.h>
23
#if LIBAVCODEC_VERSION_MAJOR >= 56
24
25
#define CODEC_ID_NONE AV_CODEC_ID_NONE
#endif
26
#endif
Rafael Kitover's avatar
Rafael Kitover committed
27
#include "version.h"
28
#include "../common/ConfigManager.h"
29
30
31
#include "../gb/gbPrinter.h"
#include "../gba/agbprint.h"

skidau's avatar
skidau committed
32
#if (wxMAJOR_VERSION < 3)
33
#define GetXRCDialog(n) \
skidau's avatar
skidau committed
34
35
36
    wxStaticCast(wxGetApp().frame->FindWindow(XRCID(n)), wxDialog)
#else
#define GetXRCDialog(n) \
37
    wxStaticCast(wxGetApp().frame->FindWindowByName(n), wxDialog)
skidau's avatar
skidau committed
38
#endif
39

40
41
void GDBBreak(MainFrame* mf);

42
bool cmditem_lt(const struct cmditem& cmd1, const struct cmditem& cmd2)
43
{
44
    return wxStrcmp(cmd1.cmd, cmd2.cmd) < 0;
45
46
}

47
void MainFrame::GetMenuOptionBool(const char* menuName, bool& field)
48
{
49
50
    field = !field;
    int id = wxXmlResource::GetXRCID(wxString(menuName, wxConvUTF8));
51

52
53
54
    for (int i = 0; i < checkable_mi.size(); i++) {
        if (checkable_mi[i].cmd != id)
            continue;
55

56
57
58
        field = checkable_mi[i].mi->IsChecked();
        break;
    }
59
}
60

61
void MainFrame::GetMenuOptionInt(const char* menuName, int& field, int mask)
62
{
63
64
65
    int value = mask;
    bool is_checked = ((field) & (mask)) != (value);
    int id = wxXmlResource::GetXRCID(wxString(menuName, wxConvUTF8));
66

67
68
69
    for (int i = 0; i < checkable_mi.size(); i++) {
        if (checkable_mi[i].cmd != id)
            continue;
70

71
72
73
        is_checked = checkable_mi[i].mi->IsChecked();
        break;
    }
74

75
    field = ((field) & ~(mask)) | (is_checked ? (value) : 0);
76
}
77

78
79
void MainFrame::SetMenuOption(const char* menuName, int value)
{
80
    int id = wxXmlResource::GetXRCID(wxString(menuName, wxConvUTF8));
81

82
83
84
    for (int i = 0; i < checkable_mi.size(); i++) {
        if (checkable_mi[i].cmd != id)
            continue;
85

86
87
88
        checkable_mi[i].mi->Check(value);
        break;
    }
89
}
90
91
92
93
94
95
96
97

//// File menu

static int open_ft = 0;
static wxString open_dir;

EVT_HANDLER(wxID_OPEN, "Open ROM...")
{
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
    open_dir = wxGetApp().GetAbsolutePath(gopts.gba_rom_dir);
    // FIXME: ignore if non-existent or not a dir
    wxString pats = _(
        "GameBoy Advance Files (*.agb;*.gba;*.bin;*.elf;*.mb;*.zip;*.7z;*.rar)|"
        "*.agb;*.gba;*.bin;*.elf;*.mb;"
        "*.agb.gz;*.gba.gz;*.bin.gz;*.elf.gz;*.mb.gz;"
        "*.agb.z;*.gba.z;*.bin.z;*.elf.z;*.mb.z;"
        "*.zip;*.7z;*.rar"
        "|GameBoy Files (*.dmg;*.gb;*.gbc;*.cgb;*.sgb;*.zip;*.7z;*.rar)|"
        "*.dmg;*.gb;*.gbc;*.cgb;*.sgb;"
        "*.dmg.gz;*.gb.gz;*.gbc.gz;*.cgb.gz;*.sgb.gz;"
        "*.dmg.z;*.gb.z;*.gbc.z;*.cgb.z;*.sgb.z;"
        "*.zip;*.7z;*.rar"
        "|");
    pats.append(wxALL_FILES);
    wxFileDialog dlg(this, _("Open ROM file"), open_dir, wxT(""),
        pats,
        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    dlg.SetFilterIndex(open_ft);

    if (ShowModal(&dlg) == wxID_OK)
        wxGetApp().pending_load = dlg.GetPath();

    open_ft = dlg.GetFilterIndex();
    open_dir = dlg.GetDirectory();
123
124
}

125
126
EVT_HANDLER(OpenGB, "Open GB...")
{
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    open_dir = wxGetApp().GetAbsolutePath(gopts.gb_rom_dir);
    // FIXME: ignore if non-existent or not a dir
    wxString pats = _(
        "GameBoy Files (*.dmg;*.gb;*.gbc;*.cgb;*.sgb;*.zip;*.7z;*.rar)|"
        "*.dmg;*.gb;*.gbc;*.cgb;*.sgb;"
        "*.dmg.gz;*.gb.gz;*.gbc.gz;*.cgb.gz;*.sgb.gz;"
        "*.dmg.z;*.gb.z;*.gbc.z;*.cgb.z;*.sgb.z;"
        "*.zip;*.7z;*.rar"
        "|");
    pats.append(wxALL_FILES);
    wxFileDialog dlg(this, _("Open GB ROM file"), open_dir, wxT(""),
        pats,
        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    dlg.SetFilterIndex(open_ft);

    if (ShowModal(&dlg) == wxID_OK)
        wxGetApp().pending_load = dlg.GetPath();

    open_ft = dlg.GetFilterIndex();
    open_dir = dlg.GetDirectory();
147
148
149
150
}

EVT_HANDLER(OpenGBC, "Open GBC...")
{
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    open_dir = wxGetApp().GetAbsolutePath(gopts.gbc_rom_dir);
    // FIXME: ignore if non-existent or not a dir
    wxString pats = _(
        "GameBoy Color Files (*.dmg;*.gb;*.gbc;*.cgb;*.sgb;*.zip;*.7z;*.rar)|"
        "*.dmg;*.gb;*.gbc;*.cgb;*.sgb;"
        "*.dmg.gz;*.gb.gz;*.gbc.gz;*.cgb.gz;*.sgb.gz;"
        "*.dmg.z;*.gb.z;*.gbc.z;*.cgb.z;*.sgb.z;"
        "*.zip;*.7z;*.rar"
        "|");
    pats.append(wxALL_FILES);
    wxFileDialog dlg(this, _("Open GBC ROM file"), open_dir, wxT(""),
        pats,
        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    dlg.SetFilterIndex(open_ft);

    if (ShowModal(&dlg) == wxID_OK)
        wxGetApp().pending_load = dlg.GetPath();

    open_ft = dlg.GetFilterIndex();
    open_dir = dlg.GetDirectory();
171
172
}

173
174
EVT_HANDLER(RecentReset, "Reset recent ROM list")
{
175
176
177
178
    // only save config if there were items to remove
    if (gopts.recent->GetCount()) {
        while (gopts.recent->GetCount())
            gopts.recent->RemoveFileFromHistory(0);
179

180
181
182
183
184
185
        wxFileConfig* cfg = wxGetApp().cfg;
        cfg->SetPath(wxT("/Recent"));
        gopts.recent->Save(*cfg);
        cfg->SetPath(wxT("/"));
        cfg->Flush();
    }
186
187
188
189
}

EVT_HANDLER(RecentFreeze, "Freeze recent ROM list (toggle)")
{
190
191
    GetMenuOptionBool("RecentFreeze", gopts.recent_freeze);
    update_opts();
192
193
194
195
196
197
}

// following 10 should really be a single ranged handler
// former names: Recent01 .. Recent10
EVT_HANDLER(wxID_FILE1, "Load recent ROM 1")
{
198
    panel->LoadGame(gopts.recent->GetHistoryFile(0));
199

200
201
    if (gdbBreakOnLoad)
        GDBBreak();
202
203
204
205
}

EVT_HANDLER(wxID_FILE2, "Load recent ROM 2")
{
206
    panel->LoadGame(gopts.recent->GetHistoryFile(1));
207

208
209
    if (gdbBreakOnLoad)
        GDBBreak();
210
211
212
213
}

EVT_HANDLER(wxID_FILE3, "Load recent ROM 3")
{
214
    panel->LoadGame(gopts.recent->GetHistoryFile(2));
215

216
217
    if (gdbBreakOnLoad)
        GDBBreak();
218
219
220
221
}

EVT_HANDLER(wxID_FILE4, "Load recent ROM 4")
{
222
    panel->LoadGame(gopts.recent->GetHistoryFile(3));
223

224
225
    if (gdbBreakOnLoad)
        GDBBreak();
226
227
228
229
}

EVT_HANDLER(wxID_FILE5, "Load recent ROM 5")
{
230
    panel->LoadGame(gopts.recent->GetHistoryFile(4));
231

232
233
    if (gdbBreakOnLoad)
        GDBBreak();
234
235
236
237
}

EVT_HANDLER(wxID_FILE6, "Load recent ROM 6")
{
238
    panel->LoadGame(gopts.recent->GetHistoryFile(5));
239

240
241
    if (gdbBreakOnLoad)
        GDBBreak();
242
243
244
245
}

EVT_HANDLER(wxID_FILE7, "Load recent ROM 7")
{
246
    panel->LoadGame(gopts.recent->GetHistoryFile(6));
247

248
249
    if (gdbBreakOnLoad)
        GDBBreak();
250
251
252
253
}

EVT_HANDLER(wxID_FILE8, "Load recent ROM 8")
{
254
    panel->LoadGame(gopts.recent->GetHistoryFile(7));
255

256
257
    if (gdbBreakOnLoad)
        GDBBreak();
258
259
260
261
}

EVT_HANDLER(wxID_FILE9, "Load recent ROM 9")
{
262
    panel->LoadGame(gopts.recent->GetHistoryFile(8));
263

264
265
    if (gdbBreakOnLoad)
        GDBBreak();
266
267
268
269
}

EVT_HANDLER(wxID_FILE10, "Load recent ROM 10")
{
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
    panel->LoadGame(gopts.recent->GetHistoryFile(9));

    if (gdbBreakOnLoad)
        GDBBreak();
}

static const struct rom_maker {
    const wxChar *code, *name;
} makers[] = {
    { wxT("01"), wxT("Nintendo") },
    { wxT("02"), wxT("Rocket Games") },
    { wxT("08"), wxT("Capcom") },
    { wxT("09"), wxT("Hot B Co.") },
    { wxT("0A"), wxT("Jaleco") },
    { wxT("0B"), wxT("Coconuts Japan") },
    { wxT("0C"), wxT("Coconuts Japan/G.X.Media") },
    { wxT("0H"), wxT("Starfish") },
    { wxT("0L"), wxT("Warashi Inc.") },
    { wxT("0N"), wxT("Nowpro") },
    { wxT("0P"), wxT("Game Village") },
    { wxT("13"), wxT("Electronic Arts Japan") },
    { wxT("18"), wxT("Hudson Soft Japan") },
    { wxT("19"), wxT("S.C.P.") },
    { wxT("1A"), wxT("Yonoman") },
    { wxT("1G"), wxT("SMDE") },
    { wxT("1P"), wxT("Creatures Inc.") },
    { wxT("1Q"), wxT("TDK Deep Impresion") },
    { wxT("20"), wxT("Destination Software") },
    { wxT("22"), wxT("VR 1 Japan") },
    { wxT("25"), wxT("San-X") },
    { wxT("28"), wxT("Kemco Japan") },
    { wxT("29"), wxT("Seta") },
    { wxT("2H"), wxT("Ubisoft Japan") },
    { wxT("2K"), wxT("NEC InterChannel") },
    { wxT("2L"), wxT("Tam") },
    { wxT("2M"), wxT("Jordan") },
    { wxT("2N"), wxT("Smilesoft") },
    { wxT("2Q"), wxT("Mediakite") },
    { wxT("36"), wxT("Codemasters") },
    { wxT("37"), wxT("GAGA Communications") },
    { wxT("38"), wxT("Laguna") },
    { wxT("39"), wxT("Telstar Fun and Games") },
    { wxT("41"), wxT("Ubi Soft Entertainment") },
    { wxT("42"), wxT("Sunsoft") },
    { wxT("47"), wxT("Spectrum Holobyte") },
    { wxT("49"), wxT("IREM") },
    { wxT("4D"), wxT("Malibu Games") },
    { wxT("4F"), wxT("Eidos/U.S. Gold") },
    { wxT("4J"), wxT("Fox Interactive") },
    { wxT("4K"), wxT("Time Warner Interactive") },
    { wxT("4Q"), wxT("Disney") },
    { wxT("4S"), wxT("Black Pearl") },
    { wxT("4X"), wxT("GT Interactive") },
    { wxT("4Y"), wxT("RARE") },
    { wxT("4Z"), wxT("Crave Entertainment") },
    { wxT("50"), wxT("Absolute Entertainment") },
    { wxT("51"), wxT("Acclaim") },
    { wxT("52"), wxT("Activision") },
    { wxT("53"), wxT("American Sammy Corp.") },
    { wxT("54"), wxT("Take 2 Interactive") },
    { wxT("55"), wxT("Hi Tech") },
    { wxT("56"), wxT("LJN LTD.") },
    { wxT("58"), wxT("Mattel") },
    { wxT("5A"), wxT("Mindscape/Red Orb Ent.") },
    { wxT("5C"), wxT("Taxan") },
    { wxT("5D"), wxT("Midway") },
    { wxT("5F"), wxT("American Softworks") },
    { wxT("5G"), wxT("Majesco Sales Inc") },
    { wxT("5H"), wxT("3DO") },
    { wxT("5K"), wxT("Hasbro") },
    { wxT("5L"), wxT("NewKidCo") },
    { wxT("5M"), wxT("Telegames") },
    { wxT("5N"), wxT("Metro3D") },
    { wxT("5P"), wxT("Vatical Entertainment") },
    { wxT("5Q"), wxT("LEGO Media") },
    { wxT("5S"), wxT("Xicat Interactive") },
    { wxT("5T"), wxT("Cryo Interactive") },
    { wxT("5W"), wxT("Red Storm Ent./BKN Ent.") },
    { wxT("5X"), wxT("Microids") },
    { wxT("5Z"), wxT("Conspiracy Entertainment Corp.") },
    { wxT("60"), wxT("Titus Interactive Studios") },
    { wxT("61"), wxT("Virgin Interactive") },
    { wxT("62"), wxT("Maxis") },
    { wxT("64"), wxT("LucasArts Entertainment") },
    { wxT("67"), wxT("Ocean") },
    { wxT("69"), wxT("Electronic Arts") },
    { wxT("6E"), wxT("Elite Systems Ltd.") },
    { wxT("6F"), wxT("Electro Brain") },
    { wxT("6G"), wxT("The Learning Company") },
    { wxT("6H"), wxT("BBC") },
    { wxT("6J"), wxT("Software 2000") },
    { wxT("6L"), wxT("BAM! Entertainment") },
    { wxT("6M"), wxT("Studio 3") },
    { wxT("6Q"), wxT("Classified Games") },
    { wxT("6S"), wxT("TDK Mediactive") },
    { wxT("6U"), wxT("DreamCatcher") },
    { wxT("6V"), wxT("JoWood Productions") },
    { wxT("6W"), wxT("SEGA") },
    { wxT("6X"), wxT("Wannado Edition") },
    { wxT("6Y"), wxT("LSP") },
    { wxT("6Z"), wxT("ITE Media") },
    { wxT("70"), wxT("Infogrames") },
    { wxT("71"), wxT("Interplay") },
    { wxT("72"), wxT("JVC Musical Industries Inc") },
    { wxT("73"), wxT("Parker Brothers") },
    { wxT("75"), wxT("SCI") },
    { wxT("78"), wxT("THQ") },
    { wxT("79"), wxT("Accolade") },
    { wxT("7A"), wxT("Triffix Ent. Inc.") },
    { wxT("7C"), wxT("Microprose Software") },
    { wxT("7D"), wxT("Universal Interactive Studios") },
    { wxT("7F"), wxT("Kemco") },
    { wxT("7G"), wxT("Rage Software") },
    { wxT("7H"), wxT("Encore") },
    { wxT("7J"), wxT("Zoo") },
    { wxT("7K"), wxT("BVM") },
    { wxT("7L"), wxT("Simon & Schuster Interactive") },
    { wxT("7M"), wxT("Asmik Ace Entertainment Inc./AIA") },
    { wxT("7N"), wxT("Empire Interactive") },
    { wxT("7Q"), wxT("Jester Interactive") },
    { wxT("7T"), wxT("Scholastic") },
    { wxT("7U"), wxT("Ignition Entertainment") },
    { wxT("7W"), wxT("Stadlbauer") },
    { wxT("80"), wxT("Misawa") },
    { wxT("83"), wxT("LOZC") },
    { wxT("8B"), wxT("Bulletproof Software") },
    { wxT("8C"), wxT("Vic Tokai Inc.") },
    { wxT("8J"), wxT("General Entertainment") },
    { wxT("8N"), wxT("Success") },
    { wxT("8P"), wxT("SEGA Japan") },
    { wxT("91"), wxT("Chun Soft") },
    { wxT("92"), wxT("Video System") },
    { wxT("93"), wxT("BEC") },
    { wxT("96"), wxT("Yonezawa/S'pal") },
    { wxT("97"), wxT("Kaneko") },
    { wxT("99"), wxT("Victor Interactive Software") },
    { wxT("9A"), wxT("Nichibutsu/Nihon Bussan") },
    { wxT("9B"), wxT("Tecmo") },
    { wxT("9C"), wxT("Imagineer") },
    { wxT("9F"), wxT("Nova") },
    { wxT("9H"), wxT("Bottom Up") },
    { wxT("9L"), wxT("Hasbro Japan") },
    { wxT("9N"), wxT("Marvelous Entertainment") },
    { wxT("9P"), wxT("Keynet Inc.") },
    { wxT("9Q"), wxT("Hands-On Entertainment") },
    { wxT("A0"), wxT("Telenet") },
    { wxT("A1"), wxT("Hori") },
    { wxT("A4"), wxT("Konami") },
    { wxT("A6"), wxT("Kawada") },
    { wxT("A7"), wxT("Takara") },
    { wxT("A9"), wxT("Technos Japan Corp.") },
    { wxT("AA"), wxT("JVC") },
    { wxT("AC"), wxT("Toei Animation") },
    { wxT("AD"), wxT("Toho") },
    { wxT("AF"), wxT("Namco") },
    { wxT("AG"), wxT("Media Rings Corporation") },
    { wxT("AH"), wxT("J-Wing") },
    { wxT("AK"), wxT("KID") },
    { wxT("AL"), wxT("MediaFactory") },
    { wxT("AP"), wxT("Infogrames Hudson") },
    { wxT("AQ"), wxT("Kiratto. Ludic Inc") },
    { wxT("B0"), wxT("Acclaim Japan") },
    { wxT("B1"), wxT("ASCII") },
    { wxT("B2"), wxT("Bandai") },
    { wxT("B4"), wxT("Enix") },
    { wxT("B6"), wxT("HAL Laboratory") },
    { wxT("B7"), wxT("SNK") },
    { wxT("B9"), wxT("Pony Canyon Hanbai") },
    { wxT("BA"), wxT("Culture Brain") },
    { wxT("BB"), wxT("Sunsoft") },
    { wxT("BD"), wxT("Sony Imagesoft") },
    { wxT("BF"), wxT("Sammy") },
    { wxT("BG"), wxT("Magical") },
    { wxT("BJ"), wxT("Compile") },
    { wxT("BL"), wxT("MTO Inc.") },
    { wxT("BN"), wxT("Sunrise Interactive") },
    { wxT("BP"), wxT("Global A Entertainment") },
    { wxT("BQ"), wxT("Fuuki") },
    { wxT("C0"), wxT("Taito") },
    { wxT("C2"), wxT("Kemco") },
    { wxT("C3"), wxT("Square Soft") },
    { wxT("C5"), wxT("Data East") },
    { wxT("C6"), wxT("Tonkin House") },
    { wxT("C8"), wxT("Koei") },
    { wxT("CA"), wxT("Konami/Palcom/Ultra") },
    { wxT("CB"), wxT("Vapinc/NTVIC") },
    { wxT("CC"), wxT("Use Co.,Ltd.") },
    { wxT("CD"), wxT("Meldac") },
    { wxT("CE"), wxT("FCI/Pony Canyon") },
    { wxT("CF"), wxT("Angel") },
    { wxT("CM"), wxT("Konami Computer Entertainment Osaka") },
    { wxT("CP"), wxT("Enterbrain") },
    { wxT("D1"), wxT("Sofel") },
    { wxT("D2"), wxT("Quest") },
    { wxT("D3"), wxT("Sigma Enterprises") },
    { wxT("D4"), wxT("Ask Kodansa") },
    { wxT("D6"), wxT("Naxat") },
    { wxT("D7"), wxT("Copya System") },
    { wxT("D9"), wxT("Banpresto") },
    { wxT("DA"), wxT("TOMY") },
    { wxT("DB"), wxT("LJN Japan") },
    { wxT("DD"), wxT("NCS") },
    { wxT("DF"), wxT("Altron Corporation") },
    { wxT("DH"), wxT("Gaps Inc.") },
    { wxT("DN"), wxT("ELF") },
    { wxT("E2"), wxT("Yutaka") },
    { wxT("E3"), wxT("Varie") },
    { wxT("E5"), wxT("Epoch") },
    { wxT("E7"), wxT("Athena") },
    { wxT("E8"), wxT("Asmik Ace Entertainment Inc.") },
    { wxT("E9"), wxT("Natsume") },
    { wxT("EA"), wxT("King Records") },
    { wxT("EB"), wxT("Atlus") },
    { wxT("EC"), wxT("Epic/Sony Records") },
    { wxT("EE"), wxT("IGS") },
    { wxT("EL"), wxT("Spike") },
    { wxT("EM"), wxT("Konami Computer Entertainment Tokyo") },
    { wxT("EN"), wxT("Alphadream Corporation") },
    { wxT("F0"), wxT("A Wave") },
    { wxT("G1"), wxT("PCCW") },
    { wxT("G4"), wxT("KiKi Co Ltd") },
    { wxT("G5"), wxT("Open Sesame Inc.") },
    { wxT("G6"), wxT("Sims") },
    { wxT("G7"), wxT("Broccoli") },
    { wxT("G8"), wxT("Avex") },
    { wxT("G9"), wxT("D3 Publisher") },
    { wxT("GB"), wxT("Konami Computer Entertainment Japan") },
    { wxT("GD"), wxT("Square-Enix") },
    { wxT("HY"), wxT("Sachen") }
499
};
500
501
#define num_makers (sizeof(makers) / sizeof(makers[0]))
static bool maker_lt(const rom_maker& r1, const rom_maker& r2)
502
{
503
    return wxStrcmp(r1.code, r2.code) < 0;
504
505
}

506
507
void SetDialogLabel(wxDialog* dlg, wxChar* id, wxString ts, size_t l)
{
508
509
    ts.Replace(wxT("&"), wxT("&&"), true);
    (dynamic_cast<wxControl*>((*dlg).FindWindow(wxXmlResource::GetXRCID(id))))->SetLabel(ts);
510
511
}

512
EVT_HANDLER_MASK(RomInformation, "ROM information...", CMDEN_GB | CMDEN_GBA)
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
    wxString s;
#define setlab(id)                            \
    do {                                      \
        /* SetLabelText is not in 2.8 */      \
        s.Replace(wxT("&"), wxT("&&"), true); \
        XRCCTRL(*dlg, id, wxControl)          \
            ->SetLabel(s);                    \
    } while (0)
#define setblab(id, b)                          \
    do {                                        \
        s.Printf(wxT("%02x"), (unsigned int)b); \
        setlab(id);                             \
    } while (0)
#define setblabs(id, b, ts)                              \
    do {                                                 \
        s.Printf(wxT("%02x (%s)"), (unsigned int)b, ts); \
        setlab(id);                                      \
    } while (0)
#define setlabs(id, ts, l)                               \
    do {                                                 \
        s = wxString((const char*)&(ts), wxConvLibc, l); \
        setlab(id);                                      \
    } while (0)

    switch (panel->game_type()) {
    case IMAGE_GB: {
        wxDialog* dlg = GetXRCDialog("GBROMInfo");
        setlabs("Title", gbRom[0x134], 15);
        setblab("Color", gbRom[0x143]);

        if (gbRom[0x14b] == 0x33)
            s = wxString((const char*)&gbRom[0x144], wxConvUTF8, 2);
        else
            s.Printf(wxT("%02x"), gbRom[0x14b]);

        setlab("MakerCode");
        const rom_maker m = { s.c_str() }, *rm;
        rm = std::lower_bound(&makers[0], &makers[num_makers], m, maker_lt);

        if (rm < &makers[num_makers] && !wxStrcmp(m.code, rm->code))
            s = rm->name;
        else
            s = _("Unknown");

        setlab("MakerName");
        setblab("UnitCode", gbRom[0x146]);
        const wxChar* type;

        switch (gbRom[0x147]) {
        case 0x00:
            type = _("ROM");
            break;

        case 0x01:
            type = _("ROM+MBC1");
            break;

        case 0x02:
            type = _("ROM+MBC1+RAM");
            break;

        case 0x03:
            type = _("ROM+MBC1+RAM+BATT");
            break;

        case 0x05:
            type = _("ROM+MBC2");
            break;

        case 0x06:
            type = _("ROM+MBC2+BATT");
            break;

        case 0x0b:
            type = _("ROM+MMM01");
            break;

        case 0x0c:
            type = _("ROM+MMM01+RAM");
            break;

        case 0x0d:
            type = _("ROM+MMM01+RAM+BATT");
            break;

        case 0x0f:
            type = _("ROM+MBC3+TIMER+BATT");
            break;

        case 0x10:
            type = _("ROM+MBC3+TIMER+RAM+BATT");
            break;

        case 0x11:
            type = _("ROM+MBC3");
            break;

        case 0x12:
            type = _("ROM+MBC3+RAM");
            break;

        case 0x13:
            type = _("ROM+MBC3+RAM+BATT");
            break;

        case 0x19:
            type = _("ROM+MBC5");
            break;

        case 0x1a:
            type = _("ROM+MBC5+RAM");
            break;

        case 0x1b:
            type = _("ROM+MBC5+RAM+BATT");
            break;

        case 0x1c:
            type = _("ROM+MBC5+RUMBLE");
            break;

        case 0x1d:
            type = _("ROM+MBC5+RUMBLE+RAM");
            break;

        case 0x1e:
            type = _("ROM+MBC5+RUMBLE+RAM+BATT");
            break;

        case 0x22:
            type = _("ROM+MBC7+BATT");
            break;

        case 0x55:
            type = _("GameGenie");
            break;

        case 0x56:
            type = _("GameShark V3.0");
            break;

        case 0xfc:
            type = _("ROM+POCKET CAMERA");
            break;

        case 0xfd:
            type = _("ROM+BANDAI TAMA5");
            break;

        case 0xfe:
            type = _("ROM+HuC-3");
            break;

        case 0xff:
            type = _("ROM+HuC-1");
            break;

        default:
            type = _("Unknown");
        }

        setblabs("DeviceType", gbRom[0x147], type);

        switch (gbRom[0x148]) {
        case 0:
            type = wxT("32K");
            break;

        case 1:
            type = wxT("64K");
            break;

        case 2:
            type = wxT("128K");
            break;

        case 3:
            type = wxT("256K");
            break;

        case 4:
            type = wxT("512K");
            break;

        case 5:
            type = wxT("1M");
            break;

        case 6:
            type = wxT("2M");
            break;

        case 7:
            type = wxT("4M");
            break;

        default:
            type = _("Unknown");
        }

        setblabs("ROMSize", gbRom[0x148], type);

        switch (gbRom[0x149]) {
        case 0:
            type = _("None");
            break;

        case 1:
            type = wxT("2K");
            break;

        case 2:
            type = wxT("8K");
            break;

        case 3:
            type = wxT("32K");
            break;

        case 4:
            type = wxT("128K");
            break;

        case 5:
            type = wxT("64K");
            break;
        }

        setblabs("RAMSize", gbRom[0x149], type);
        setblab("DestCode", gbRom[0x14a]);
        setblab("LicCode", gbRom[0x14b]);
        setblab("Version", gbRom[0x14c]);
746
        uint8_t crc = 25;
747
748
749
750
751
752
753

        for (int i = 0x134; i < 0x14d; i++)
            crc += gbRom[i];

        crc = 256 - crc;
        s.Printf(wxT("%02x (%02x)"), crc, gbRom[0x14d]);
        setlab("CRC");
754
        uint16_t crc16 = 0;
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

        for (int i = 0; i < gbRomSize; i++)
            crc16 += gbRom[i];

        crc16 -= gbRom[0x14e] + gbRom[0x14f];
        s.Printf(wxT("%04x (%04x)"), crc16, gbRom[0x14e] * 256 + gbRom[0x14f]);
        setlab("Checksum");
        dlg->Fit();
        ShowModal(dlg);
    } break;

    case IMAGE_GBA: {
        IdentifyRom();
        wxDialog* dlg = GetXRCDialog("GBAROMInfo");
        wxString rom_crc32;
        rom_crc32.Printf(wxT("%08X"), panel->rom_crc32);
        SetDialogLabel(dlg, wxT("Title"), panel->rom_name, 30);
        setlabs("IntTitle", rom[0xa0], 12);
        SetDialogLabel(dlg, wxT("Scene"), panel->rom_scene_rls_name, 30);
        SetDialogLabel(dlg, wxT("Release"), panel->rom_scene_rls, 4);
        SetDialogLabel(dlg, wxT("CRC32"), rom_crc32, 8);
        setlabs("GameCode", rom[0xac], 4);
        setlabs("MakerCode", rom[0xb0], 2);
        const rom_maker m = { s.c_str() }, *rm;
        rm = std::lower_bound(&makers[0], &makers[num_makers], m, maker_lt);

        if (rm < &makers[num_makers] && !wxStrcmp(m.code, rm->code))
            s = rm->name;
        else
            s = _("Unknown");

        setlab("MakerName");
        setblab("UnitCode", rom[0xb3]);
        s.Printf(wxT("%02x"), (unsigned int)rom[0xb4]);

        if (rom[0xb4] & 0x80)
            s.append(wxT(" (DACS)"));

        setlab("DeviceType");
        setblab("Version", rom[0xbc]);
795
        uint8_t crc = 0x19;
796
797
798
799
800
801
802
803
804
805
806

        for (int i = 0xa0; i < 0xbd; i++)
            crc += rom[i];

        crc = -crc;
        s.Printf(wxT("%02x (%02x)"), crc, rom[0xbd]);
        setlab("CRC");
        dlg->Fit();
        ShowModal(dlg);
    } break;
    }
807
808
}

809
810
811
812
813
static wxString loaddotcodefile_path;
static wxString savedotcodefile_path;

EVT_HANDLER_MASK(LoadDotCodeFile, "Load e-Reader Dot Code...", CMDEN_GBA)
{
814
815
816
817
818
819
    wxFileDialog dlg(this, _("Select Dot Code file"), loaddotcodefile_path, wxEmptyString,
        _(
                         "e-Reader Dot Code (*.bin;*.raw)|"
                         "*.bin;*.raw"),
        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    int ret = ShowModal(&dlg);
820

821
822
    if (ret != wxID_OK)
        return;
823

824
825
    loaddotcodefile_path = dlg.GetPath();
    SetLoadDotCodeFile(loaddotcodefile_path.mb_str(wxConvUTF8));
826
827
828
829
}

EVT_HANDLER_MASK(SaveDotCodeFile, "Save e-Reader Dot Code...", CMDEN_GBA)
{
830
831
832
833
834
835
    wxFileDialog dlg(this, _("Select Dot Code file"), savedotcodefile_path, wxEmptyString,
        _(
                         "e-Reader Dot Code (*.bin;*.raw)|"
                         "*.bin;*.raw"),
        wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    int ret = ShowModal(&dlg);
836

837
838
    if (ret != wxID_OK)
        return;
839

840
841
    savedotcodefile_path = dlg.GetPath();
    SetSaveDotCodeFile(savedotcodefile_path.mb_str(wxConvUTF8));
842
843
}

844
845
static wxString batimp_path;

846
847
EVT_HANDLER_MASK(ImportBatteryFile, "Import battery file...", CMDEN_GB | CMDEN_GBA)
{
848
849
    if (!batimp_path.size())
        batimp_path = panel->bat_dir();
850

851
852
853
854
    wxFileDialog dlg(this, _("Select battery file"), batimp_path, wxEmptyString,
        _("Battery file (*.sav)|*.sav|Flash save (*.dat)|*.dat"), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    int ret = ShowModal(&dlg);
    batimp_path = dlg.GetDirectory();
855

856
857
    if (ret != wxID_OK)
        return;
858

859
860
861
    wxString fn = dlg.GetPath();
    ret = wxMessageBox(_("Importing a battery file will erase any saved games (permanently after the next write).  Do you want to continue?"),
        _("Confirm import"), wxYES_NO | wxICON_EXCLAMATION);
862

863
864
    if (ret == wxYES) {
        wxString msg;
865

866
867
868
869
        if (panel->emusys->emuReadBattery(fn.mb_fn_str()))
            msg.Printf(_("Loaded battery %s"), fn.c_str());
        else
            msg.Printf(_("Error loading battery %s"), fn.c_str());
870

871
872
        systemScreenMessage(msg);
    }
873
874
875
876
}

EVT_HANDLER_MASK(ImportGamesharkCodeFile, "Import GameShark code file...", CMDEN_GB | CMDEN_GBA)
{
877
878
879
880
881
882
    static wxString path;
    wxFileDialog dlg(this, _("Select code file"), path, wxEmptyString,
        panel->game_type() == IMAGE_GBA ? _("Gameshark Code File (*.spc;*.xpc)|*.spc;*.xpc") : _("Gameshark Code File (*.gcf)|*.gcf"),
        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    int ret = ShowModal(&dlg);
    path = dlg.GetDirectory();
883

884
885
    if (ret != wxID_OK)
        return;
886

887
888
889
    wxString fn = dlg.GetPath();
    ret = wxMessageBox(_("Importing a code file will replace any loaded cheats.  Do you want to continue?"),
        _("Confirm import"), wxYES_NO | wxICON_EXCLAMATION);
890

891
892
893
    if (ret == wxYES) {
        wxString msg;
        bool res;
894

895
896
897
898
899
900
901
902
        if (panel->game_type() == IMAGE_GB)
            // FIXME: this routine will not work on big-endian systems
            // if the underlying file format is little-endian
            // (fix in gb/gbCheats.cpp)
            res = gbCheatReadGSCodeFile(fn.mb_fn_str());
        else {
            // need to select game first
            wxFFile f(fn, wxT("rb"));
903

904
905
906
907
            if (!f.IsOpened()) {
                wxLogError(_("Cannot open file %s"), fn.c_str());
                return;
            }
908

909
910
            // FIXME: in my code, I assume file format is little-endian
            // however, in core code, it is assumed to be native-endian
911
            uint32_t len;
912
            char buf[14];
913

914
915
916
917
            if (f.Read(&len, sizeof(len)) != sizeof(len) || wxUINT32_SWAP_ON_BE(len) != 14 || f.Read(buf, 14) != 14 || memcmp(buf, "SharkPortCODES", 14)) {
                wxLogError(_("Unsupported code file %s"), fn.c_str());
                return;
            }
918

919
            f.Seek(0x1e);
920

921
922
            if (f.Read(&len, sizeof(len)) != sizeof(len))
                len = 0;
923

924
            uint32_t game = 0;
925

926
927
928
929
            if (len > 1) {
                wxDialog* seldlg = GetXRCDialog("CodeSelect");
                wxControlWithItems* lst = XRCCTRL(*seldlg, "CodeList", wxControlWithItems);
                lst->Clear();
930

931
                while (len-- > 0) {
932
                    uint32_t slen;
933

934
935
                    if (f.Read(&slen, sizeof(slen)) != sizeof(slen) || slen > 1024) // arbitrary upper bound
                        break;
936

937
                    char buf[1024];
938

939
940
                    if (f.Read(buf, slen) != slen)
                        break;
941

942
                    lst->Append(wxString(buf, wxConvLibc, slen));
943
                    uint32_t ncodes;
944

945
946
                    if (f.Read(&ncodes, sizeof(ncodes)) != sizeof(ncodes))
                        break;
947

948
949
950
                    for (; ncodes > 0; ncodes--) {
                        if (f.Read(&slen, sizeof(slen)) != sizeof(slen))
                            break;
951

952
                        f.Seek(slen, wxFromCurrent);
953

954
955
                        if (f.Read(&slen, sizeof(slen)) != sizeof(slen))
                            break;
956

957
                        f.Seek(slen + 4, wxFromCurrent);
958

959
960
                        if (f.Read(&slen, sizeof(slen)) != sizeof(slen))
                            break;
961

962
963
964
                        f.Seek(slen * 12, wxFromCurrent);
                    }
                }
965

966
                int sel = ShowModal(seldlg);
967

968
969
                if (sel != wxID_OK)
                    return;
970

971
                game = lst->GetSelection();
972

973
974
975
                if (game == wxNOT_FOUND)
                    game = 0;
            }
976

977
978
979
980
981
982
            bool v3 = fn.size() >= 4 && wxString(fn.substr(fn.size() - 4)).IsSameAs(wxT(".xpc"), false);
            // FIXME: this routine will not work on big-endian systems
            // if the underlying file format is little-endian
            // (fix in gba/Cheats.cpp)
            res = cheatsImportGSACodeFile(fn.mb_fn_str(), game, v3);
        }
983

984
985
986
987
        if (res)
            msg.Printf(_("Loaded code file %s"), fn.c_str());
        else
            msg.Printf(_("Error loading code file %s"), fn.c_str());
988

989
990
        systemScreenMessage(msg);
    }
991
992
993
994
995
}

static wxString gss_path;

EVT_HANDLER_MASK(ImportGamesharkActionReplaySnapshot,
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
    "Import GameShark Action Replay snapshot...", CMDEN_GB | CMDEN_GBA)
{
    wxFileDialog dlg(this, _("Select snapshot file"), gss_path, wxEmptyString,
        panel->game_type() == IMAGE_GBA ? _("GS & PAC Snapshots (*.sps;*.xps)|*.sps;*.xps|GameShark SP Snapshots (*.gsv)|*.gsv") : _("Gameboy Snapshot (*.gbs)|*.gbs"),
        wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    int ret = ShowModal(&dlg);
    gss_path = dlg.GetDirectory();

    if (ret != wxID_OK)
        return;

    wxString fn = dlg.GetPath();
    ret = wxMessageBox(_("Importing a snapshot file will erase any saved games (permanently after the next write).  Do you want to continue?"),
        _("Confirm import"), wxYES_NO | wxICON_EXCLAMATION);

    if (ret == wxYES) {
        wxString msg;
        bool res;

        if (panel->game_type() == IMAGE_GB)
            res = gbReadGSASnapshot(fn.mb_fn_str());
        else {
            bool gsv = fn.size() >= 4 && wxString(fn.substr(fn.size() - 4)).IsSameAs(wxT(".gsv"), false);

            if (gsv)
                // FIXME: this will fail on big-endian machines if
                // file format is little-endian
                // fix in GBA.cpp
                res = CPUReadGSASPSnapshot(fn.mb_fn_str());
            else
                // FIXME: this will fail on big-endian machines if
                // file format is little-endian
                // fix in GBA.cpp
                res = CPUReadGSASnapshot(fn.mb_fn_str());
        }

        if (res)
            msg.Printf(_("Loaded snapshot file %s"), fn.c_str());
        else
            msg.Printf(_("Error loading snapshot file %s"), fn.c_str());

        systemScreenMessage(msg);
    }
1039
1040
1041
1042
}

EVT_HANDLER_MASK(ExportBatteryFile, "Export battery file...", CMDEN_GB | CMDEN_GBA)
{
1043
1044
    if (!batimp_path.size())
        batimp_path = panel->bat_dir();
1045

1046
1047
1048
1049
    wxFileDialog dlg(this, _("Select battery file"), batimp_path, wxEmptyString,
        _("Battery file (*.sav)|*.sav|Flash save (*.dat)|*.dat"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    int ret = ShowModal(&dlg);
    batimp_path = dlg.GetDirectory();
1050

1051
1052
    if (ret != wxID_OK)
        return;
1053

1054
1055
    wxString fn = dlg.GetPath();
    wxString msg;
1056

1057
1058
1059
1060
    if (panel->emusys->emuWriteBattery(fn.mb_fn_str()))
        msg.Printf(_("Wrote battery %s"), fn.c_str());
    else
        msg.Printf(_("Error writing battery %s"), fn.c_str());
1061

1062
    systemScreenMessage(msg);
1063
1064
1065
1066
}

EVT_HANDLER_MASK(ExportGamesharkSnapshot, "Export GameShark snapshot...", CMDEN_GBA)
{
1067
1068
1069
1070
    if (eepromInUse) {
        wxLogError(_("EEPROM saves cannot be exported"));
        return;
    }
1071

1072
1073
1074
1075
1076
1077
    wxString def_name = panel->game_name();
    def_name.append(wxT(".sps"));
    wxFileDialog dlg(this, _("Select snapshot file"), gss_path, def_name,
        _("Gameshark Snapshot (*.sps)|*.sps"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    int ret = ShowModal(&dlg);
    gss_path = dlg.GetDirectory();
1078

1079
1080
    if (ret != wxID_OK)
        return;
1081

1082
1083
1084
1085
1086
1087
1088
1089
    wxString fn = dlg.GetPath();
    wxDialog* infodlg = GetXRCDialog("ExportSPS");
    wxTextCtrl *tit = XRCCTRL(*infodlg, "Title", wxTextCtrl),
               *dsc = XRCCTRL(*infodlg, "Description", wxTextCtrl),
               *n = XRCCTRL(*infodlg, "Notes", wxTextCtrl);
    tit->SetValue(wxString((const char*)&rom[0xa0], wxConvLibc, 12));
    dsc->SetValue(wxDateTime::Now().Format(wxT("%c")));
    n->SetValue(_("Exported from VisualBoyAdvance-M"));
1090

1091
1092
    if (ShowModal(infodlg) != wxID_OK)
        return;
1093

1094
    wxString msg;
1095

1096
1097
1098
1099
1100
1101
1102
1103
    // FIXME: this will fail on big-endian machines if file format is
    // little-endian
    // fix in GBA.cpp
    if (CPUWriteGSASnapshot(fn.mb_fn_str(), tit->GetValue().utf8_str(),
            dsc->GetValue().utf8_str(), n->GetValue().utf8_str()))
        msg.Printf(_("Saved snapshot file %s"), fn.c_str());
    else
        msg.Printf(_("Error saving snapshot file %s"), fn.c_str());
1104

1105
    systemScreenMessage(msg);
1106
1107
}

1108
EVT_HANDLER_MASK(ScreenCapture, "Screen capture...", CMDEN_GB | CMDEN_GBA)
1109
{
1110
1111
    wxString scap_path = GetGamePath(gopts.scrshot_dir);
    wxString def_name = panel->game_name();
1112

1113
1114
1115
1116
    if (captureFormat == 0)
        def_name.append(wxT(".png"));
    else
        def_name.append(wxT(".bmp"));
1117

1118
1119
1120
1121
1122
    wxFileDialog dlg(this, _("Select output file"), scap_path, def_name,
        _("PNG images|*.png|BMP images|*.bmp"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    dlg.SetFilterIndex(captureFormat);
    int ret = ShowModal(&dlg);
    scap_path = dlg.GetDirectory();
1123

1124
1125
    if (ret != wxID_OK)
        return;
1126

1127
1128
    wxString fn = dlg.GetPath();
    int fmt = dlg.GetFilterIndex();
1129

1130
1131
1132
1133
1134
1135
    if (fn.size() >= 4) {
        if (wxString(fn.substr(fn.size() - 4)).IsSameAs(wxT(".bmp"), false))
            fmt = 1;
        else if (wxString(fn.substr(fn.size() - 4)).IsSameAs(wxT(".png"), false))
            fmt = 0;
    }
1136

1137
1138
1139
1140
    if (fmt == 0)
        panel->emusys->emuWritePNG(fn.mb_fn_str());
    else
        panel->emusys->emuWriteBMP(fn.mb_fn_str());
1141

1142
1143
1144
    wxString msg;
    msg.Printf(_("Wrote snapshot %s"), fn.c_str());
    systemScreenMessage(msg);
1145
1146
}

1147
EVT_HANDLER_MASK(RecordSoundStartRecording, "Start sound recording...", CMDEN_NSREC)
1148
{
1149
#ifndef NO_FFMPEG
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
    static wxString sound_exts;
    static int sound_extno;
    static wxString sound_path;

    if (!sound_exts.size()) {
        sound_extno = -1;
        int extno;
        AVOutputFormat* fmt;

        for (fmt = NULL, extno = 0; (fmt = av_oformat_next(fmt));) {
            if (!fmt->extensions)
                continue;

            if (fmt->audio_codec == CODEC_ID_NONE)
                continue;

            sound_exts.append(wxString(fmt->long_name ? fmt->long_name : fmt->name, wxConvLibc));
            sound_exts.append(_(" files ("));
            wxString ext(fmt->extensions, wxConvLibc);
            ext.Replace(wxT(","), wxT(";*."));
            ext.insert(0, wxT("*."));

            if (sound_extno < 0 && ext.find(wxT("*.wav")) != wxString::npos)
                sound_extno = extno;

            sound_exts.append(ext);
            sound_exts.append(wxT(")|"));
            sound_exts.append(ext);
            sound_exts.append(wxT('|'));
            extno++;
        }

        sound_exts.append(wxALL_FILES);

        if (sound_extno < 0)
            sound_extno = extno;
    }

    sound_path = GetGamePath(gopts.recording_dir);
    wxString def_name = panel->game_name();
    const wxChar* extoff = sound_exts.c_str();

    for (int i = 0; i < sound_extno; i++) {
        extoff = wxStrchr(extoff, wxT('|')) + 1;
        extoff = wxStrchr(extoff, wxT('|')) + 1;
    }

    extoff = wxStrchr(extoff, wxT('|')) + 2; // skip *
    def_name += wxString(extoff, wxStrcspn(extoff, wxT(";|")));
    wxFileDialog dlg(this, _("Select output file"), sound_path, def_name,
        sound_exts, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    dlg.SetFilterIndex(sound_extno);
    int ret = ShowModal(&dlg);
    sound_extno = dlg.GetFilterIndex();
    sound_path = dlg.GetDirectory();

    if (ret != wxID_OK)
        return;

    panel->StartSoundRecording(dlg.GetPath());
1210
#endif
1211
1212
}

1213
1214
1215
EVT_HANDLER_MASK(RecordSoundStopRecording, "Stop sound recording", CMDEN_SREC)
{
#ifndef NO_FFMPEG
1216
    panel->StopSoundRecording();
1217
1218
1219
1220
1221
1222
#endif
}

EVT_HANDLER_MASK(RecordAVIStartRecording, "Start video recording...", CMDEN_NVREC)
{
#ifndef NO_FFMPEG
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
    static wxString vid_exts;
    static int vid_extno;
    static wxString vid_path;

    if (!vid_exts.size()) {
        vid_extno = -1;
        int extno;
        AVOutputFormat* fmt;

        for (fmt = NULL, extno = 0; (fmt = av_oformat_next(fmt));) {
            if (!fmt->extensions)
                continue;

            if (fmt->video_codec == CODEC_ID_NONE)
                continue;

            vid_exts.append(wxString(fmt->long_name ? fmt->long_name : fmt->name, wxConvLibc));
            vid_exts.append(_(" files ("));
            wxString ext(fmt->extensions, wxConvLibc);
            ext.Replace(wxT(","), wxT(";*."));
            ext.insert(0, wxT("*."));

            if (vid_extno < 0 && ext.find(wxT("*.avi")) != wxString::npos)
                vid_extno = extno;

            vid_exts.append(ext);
            vid_exts.append(wxT(")|"));
            vid_exts.append(ext);
            vid_exts.append(wxT('|'));
            extno++;
        }

        vid_exts.append(wxALL_FILES);

        if (vid_extno < 0)
            vid_extno = extno;
    }

    vid_path = GetGamePath(gopts.recording_dir);
    wxString def_name = panel->game_name();
    const wxChar* extoff = vid_exts.c_str();

    for (int i = 0; i < vid_extno; i++) {
        extoff = wxStrchr(extoff, wxT('|')) + 1;
        extoff = wxStrchr(extoff, wxT('|')) + 1;
    }

    extoff = wxStrchr(extoff, wxT('|')) + 2; // skip *
    def_name += wxString(extoff, wxStrcspn(extoff, wxT(";|")));
    wxFileDialog dlg(this, _("Select output file"), vid_path, def_name,
        vid_exts, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    dlg.SetFilterIndex(vid_extno);
    int ret = ShowModal(&dlg);
    vid_extno = dlg.GetFilterIndex();
    vid_path = dlg.GetDirectory();

    if (ret != wxID_OK)
        return;

    panel->StartVidRecording(dlg.GetPath());
1283
1284
1285
1286
1287
1288
#endif
}

EVT_HANDLER_MASK(RecordAVIStopRecording, "Stop video recording", CMDEN_VREC)
{
#ifndef NO_FFMPEG
1289
    panel->StopVidRecording();
1290
1291
1292
1293
1294
1295
1296
#endif
}

static wxString mov_path;

EVT_HANDLER_MASK(RecordMovieStartRecording, "Start game recording...", CMDEN_NGREC)
{
1297
1298
1299
1300
1301
1302
    mov_path = GetGamePath(gopts.recording_dir);
    wxString def_name = panel->game_name() + wxT(".vmv");
    wxFileDialog dlg(this, _("Select output file"), mov_path, def_name,
        _("VBA Movie files|*.vmv"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
    int ret = ShowModal(&dlg);
    mov_path = dlg.GetDirectory();
1303

1304
1305
    if (ret != wxID_OK)
        return;
1306

1307
    systemStartGameRecording(dlg.GetPath());
1308
1309
1310
1311
}

EVT_HANDLER_MASK(RecordMovieStopRecording, "Stop game recording", CMDEN_GREC)
{
1312
    systemStopGameRecording();
1313
1314
}

1315
EVT_HANDLER_MASK(PlayMovieStartPlaying, "Start playing movie...", CMDEN_NGREC | CMDEN_NGPLAY)
1316
{
1317
1318
1319
1320
1321
1322
1323
    mov_path = GetGamePath(gopts.recording_dir);
    systemStopGamePlayback();
    wxString def_name = panel->game_name() + wxT(".vmv");
    wxFileDialog dlg(this, _("Select file"), mov_path, def_name,
        _("VBA Movie files|*.vmv"), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
    int ret = ShowModal(&dlg);
    mov_path = dlg.GetDirectory();
1324

1325
1326
    if (ret != wxID_OK)
        return;
1327

1328
    systemStartGamePlayback(dlg.GetPath());
1329
1330
1331
1332
}

EVT_HANDLER_MASK(PlayMovieStopPlaying, "Stop playing movie", CMDEN_GPLAY)
{
1333
    systemStopGamePlayback();
1334
1335
1336
}

// formerly Close
1337
EVT_HANDLER_MASK(wxID_CLOSE, "Close", CMDEN_GB | CMDEN_GBA)
1338
{
1339
    panel->UnloadGame();
1340
1341
1342
1343
1344
}

// formerly Exit
EVT_HANDLER(wxID_EXIT, "Exit")
{
1345
    Close(false);
1346
1347
1348
1349
1350
}

// Emulation menu
EVT_HANDLER(Pause, "Pause (toggle)")
{
1351
    GetMenuOptionBool("Pause", paused);
1352

1353
1354
1355
1356
    if (paused)
        panel->Pause();
    else if (!IsPaused())
        panel->Resume();
1357

1358
1359
    // undo next-frame's zeroing of frameskip
    int fs = frameSkip