SDL.cpp 70.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
// Copyright (C) 1999-2003 Forgotten
// Copyright (C) 2005-2006 Forgotten and the VBA development team

// 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, 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, write to the Free Software Foundation,
squall-leonhart's avatar
squall-leonhart committed
17
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18

19
20
//OpenGL library
#if (defined _MSC_VER)
21
#pragma comment(lib, "OpenGL32")
22
23
24
#include <windows.h>
#endif

25
#include <cmath>
26
27
#include <stdarg.h>
#include <stdio.h>
28
#include <stdlib.h>
29
30
#include <string.h>
#include <sys/stat.h>
31
#include <sys/types.h>
32
#ifdef __APPLE__
33
#include <OpenGL/OpenGL.h>
34
35
#include <OpenGL/glext.h>
#include <OpenGL/glu.h>
36
#else
EoD's avatar
EoD committed
37
#include <GL/gl.h>
38
39
#include <GL/glext.h>
#include <GL/glu.h>
40
#endif
41

42
43
#include <time.h>

44
#include "../AutoBuild.h"
Rafael Kitover's avatar
Rafael Kitover committed
45
#include "version.h"
46

EoD's avatar
EoD committed
47
#include "SDL.h"
48

49
#include "../Util.h"
50
#include "../common/ConfigManager.h"
51
#include "../common/Patch.h"
bgk's avatar
bgk committed
52
53
#include "../gb/gb.h"
#include "../gb/gbCheats.h"
54
#include "../gb/gbGlobals.h"
bgk's avatar
bgk committed
55
#include "../gb/gbSound.h"
56
57
58
59
60
61
#include "../gba/Cheats.h"
#include "../gba/Flash.h"
#include "../gba/GBA.h"
#include "../gba/RTC.h"
#include "../gba/Sound.h"
#include "../gba/agbprint.h"
62

63
#include "../common/SoundSDL.h"
mudlord's avatar
mudlord committed
64
#include "filters.h"
bgk's avatar
SDL:    
bgk committed
65
#include "inputSDL.h"
66
#include "text.h"
mudlord's avatar
mudlord committed
67

68
#ifndef _WIN32
69
70
#include <unistd.h>
#define GETCWD getcwd
71
#else // _WIN32
72
73
74
#include <direct.h>
#define GETCWD _getcwd
#define snprintf sprintf
75
76
77
#endif // _WIN32

#ifndef __GNUC__
78
79
80
#define HAVE_DECL_GETOPT 0
#define __STDC__ 1
#include "getopt.h"
81
#else // ! __GNUC__
82
83
#define HAVE_DECL_GETOPT 1
#include <getopt.h>
84
85
#endif // ! __GNUC__

86
#if WITH_LIRC
mudlord's avatar
mudlord committed
87
#include <lirc/lirc_client.h>
88
#include <sys/poll.h>
mudlord's avatar
mudlord committed
89
90
#endif

91
92
93
extern void remoteInit();
extern void remoteCleanUp();
extern void remoteStubMain();
94
extern void remoteStubSignal(int, int);
95
extern void remoteOutput(const char*, uint32_t);
96
97
98
99
extern void remoteSetProtocol(int);
extern void remoteSetPort(int);

struct EmulatedSystem emulator = {
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    false,
    0
114
115
};

116
117
118
119
SDL_Surface* surface = NULL;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;
120
SDL_GLContext glcontext;
121
122
123
124
125
126
127
128
129
130
131
132
133

int systemSpeed = 0;
int systemRedShift = 0;
int systemBlueShift = 0;
int systemGreenShift = 0;
int systemColorDepth = 0;
int systemVerbose = 0;
int systemFrameSkip = 0;
int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;

int srcPitch = 0;
int destWidth = 0;
int destHeight = 0;
mudlord's avatar
mudlord committed
134
135
int desktopWidth = 0;
int desktopHeight = 0;
136

137
uint8_t* delta = NULL;
138
static const int delta_size = 322 * 242 * 4;
139

mudlord's avatar
mudlord committed
140
int filter_enlarge = 2;
141

142
143
int cartridgeType = 3;

144
145
int textureSize = 256;
GLuint screenTexture = 0;
146
uint8_t* filterPix = 0;
147

148
int emulating = 0;
149
int RGB_LOW_BITS_MASK = 0x821;
150
151
152
uint32_t systemColorMap32[0x10000];
uint16_t systemColorMap16[0x10000];
uint16_t systemGbPalette[24];
153

154
char filename[2048];
155

156
static int rewindSerial = 0;
157

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
static int sdlSaveKeysSwitch = 0;
// if 0, then SHIFT+F# saves, F# loads (old VBA, ...)
// if 1, then SHIFT+F# loads, F# saves (linux snes9x, ...)
// if 2, then F5 decreases slot number, F6 increases, F7 saves, F8 loads

static int saveSlotPosition = 0; // default is the slot from normal F1
// internal slot number for undoing the last load
#define SLOT_POS_LOAD_BACKUP 8
// internal slot number for undoing the last save
#define SLOT_POS_SAVE_BACKUP 9

static int sdlOpenglScale = 1;
// will scale window on init by this much
static int sdlSoundToggledOff = 0;

173
extern int autoFireMaxCount;
174

175
#define REWIND_NUM 8
176
177
#define REWIND_SIZE 400000

178
179
180
181
182
183
184
185
186
187
188
189
190
enum VIDEO_SIZE {
    VIDEO_1X,
    VIDEO_2X,
    VIDEO_3X,
    VIDEO_4X,
    VIDEO_5X,
    VIDEO_6X,
    VIDEO_320x240,
    VIDEO_640x480,
    VIDEO_800x600,
    VIDEO_1024x768,
    VIDEO_1280x1024,
    VIDEO_OTHER
191
};
192

193
#define _stricmp strcasecmp
194

195
uint32_t throttleLastTime = 0;
196
197

bool pauseNextFrame = false;
198
int sdlMirroringEnable = 1;
199

200
//static int ignore_first_resize_event = 0;
201
202
203
204

/* forward */
void systemConsoleMessage(const char*);

205
char* home;
206
207

char screenMessageBuffer[21];
208
uint32_t screenMessageTime = 0;
209

210
#define SOUND_MAX_VOLUME 2.0
211
212
#define SOUND_ECHO 0.2
#define SOUND_STEREO 0.15
213

bgk's avatar
SDL :    
bgk committed
214
215
static void sdlChangeVolume(float d)
{
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    float oldVolume = soundGetVolume();
    float newVolume = oldVolume + d;

    if (newVolume < 0.0)
        newVolume = 0.0;
    if (newVolume > SOUND_MAX_VOLUME)
        newVolume = SOUND_MAX_VOLUME;

    if (fabs(newVolume - oldVolume) > 0.001) {
        char tmp[32];
        sprintf(tmp, "Volume: %i%%", (int)(newVolume * 100.0 + 0.5));
        systemScreenMessage(tmp);
        soundSetVolume(newVolume);
    }
bgk's avatar
SDL :    
bgk committed
230
231
}

232
#if WITH_LIRC
233
234
//LIRC code
bool LIRCEnabled = false;
235
236
int LIRCfd = 0;
static struct lirc_config* LIRCConfigInfo;
237
238
239

void StartLirc(void)
{
240
241
242
243
244
245
    fprintf(stdout, "Trying to start LIRC: ");
    //init LIRC and Record output
    LIRCfd = lirc_init("vbam", 1);
    if (LIRCfd == -1) {
        //it failed
        fprintf(stdout, "Failed\n");
246
    } else {
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
        fprintf(stdout, "Success\n");
        //read the config file
        char LIRCConfigLoc[2048];
        sprintf(LIRCConfigLoc, "%s/%s/%s", homeDir, DOT_DIR, "lircrc");
        fprintf(stdout, "LIRC Config file:");
        if (lirc_readconfig(LIRCConfigLoc, &LIRCConfigInfo, NULL) == 0) {
            //check vbam dir for lircrc
            fprintf(stdout, "Loaded (%s)\n", LIRCConfigLoc);
        } else if (lirc_readconfig(NULL, &LIRCConfigInfo, NULL) == 0) {
            //check default lircrc location
            fprintf(stdout, "Loaded\n");
        } else {
            //it all failed
            fprintf(stdout, "Failed\n");
            LIRCEnabled = false;
        }
        LIRCEnabled = true;
264
265
266
267
268
    }
}

void StopLirc(void)
{
269
270
271
272
273
274
275
276
277
    //did we actually get lirc working at the start
    if (LIRCEnabled) {
        //if so free the config and deinit lirc
        fprintf(stdout, "Shuting down LIRC\n");
        lirc_freeconfig(LIRCConfigInfo);
        lirc_deinit();
        //set lirc enabled to false
        LIRCEnabled = false;
    }
278
279
280
}
#endif

281
282
283
284
285
#ifdef __MSC__
#define stat _stat
#define S_IFDIR _S_IFDIR
#endif

286
bool sdlCheckDirectory(const char* dir)
287
{
288
289
    bool res = false;

290
    if (!dir || !dir[0]) {
291
        return false;
292
    }
293

294
    struct stat buf;
295

296
    int len = strlen(dir);
297

298
    char* p = (char*)dir + len - 1;
299

300
    while (p != dir && (*p == '/' || *p == '\\')) {
301
        *p = 0;
302
303
        p--;
    }
304

305
306
307
308
    if (stat(dir, &buf) == 0) {
        if (!(buf.st_mode & S_IFDIR)) {
            fprintf(stderr, "Error: %s is not a directory\n", dir);
        }
309
        res = true;
310
311
    } else {
        fprintf(stderr, "Error: %s does not exist\n", dir);
312
    }
313
314

    return res;
315
316
}

317
char* sdlGetFilename(char* name)
318
{
319
    static char filebuffer[2048];
320

321
    int len = strlen(name);
322

323
    char* p = name + len - 1;
324

325
326
327
328
329
330
331
332
333
    while (true) {
        if (*p == '/' || *p == '\\') {
            p++;
            break;
        }
        len--;
        p--;
        if (len == 0)
            break;
334
    }
335

336
337
338
339
340
    if (len == 0)
        strcpy(filebuffer, name);
    else
        strcpy(filebuffer, p);
    return filebuffer;
341
342
}

343
FILE* sdlFindFile(const char* name)
344
{
345
346
    char buffer[4096];
    char path[2048];
347
348
349
350

#ifdef _WIN32
#define PATH_SEP ";"
#define FILE_SEP '\\'
mudlord's avatar
mudlord committed
351
#define EXE_NAME "vbam.exe"
352
353
354
#else // ! _WIN32
#define PATH_SEP ":"
#define FILE_SEP '/'
mudlord's avatar
mudlord committed
355
#define EXE_NAME "vbam"
356
357
#endif // ! _WIN32

358
    fprintf(stdout, "Searching for file %s\n", name);
359

360
361
362
    if (GETCWD(buffer, 2048)) {
        fprintf(stdout, "Searching current directory: %s\n", buffer);
    }
363

364
365
366
367
    FILE* f = fopen(name, "r");
    if (f != NULL) {
        return f;
    }
368

369
370
371
372
373
374
375
    if (homeDir) {
        fprintf(stdout, "Searching home directory: %s%c%s\n", homeDir, FILE_SEP, DOT_DIR);
        sprintf(path, "%s%c%s%c%s", homeDir, FILE_SEP, DOT_DIR, FILE_SEP, name);
        f = fopen(path, "r");
        if (f != NULL)
            return f;
    }
376
377

#ifdef _WIN32
378
379
380
381
    char* home = getenv("USERPROFILE");
    if (home != NULL) {
        fprintf(stdout, "Searching user profile directory: %s\n", home);
        sprintf(path, "%s%c%s", home, FILE_SEP, name);
382
        f = fopen(path, "r");
383
        if (f != NULL)
384
385
            return f;
    }
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

    if (!strchr(home, '/') && !strchr(home, '\\')) {
        char* path = getenv("PATH");

        if (path != NULL) {
            fprintf(stdout, "Searching PATH\n");
            strncpy(buffer, path, 4096);
            buffer[4095] = 0;
            char* tok = strtok(buffer, PATH_SEP);

            while (tok) {
                sprintf(path, "%s%c%s", tok, FILE_SEP, EXE_NAME);
                f = fopen(path, "r");
                if (f != NULL) {
                    char path2[2048];
                    fclose(f);
                    sprintf(path2, "%s%c%s", tok, FILE_SEP, name);
                    f = fopen(path2, "r");
                    if (f != NULL) {
                        fprintf(stdout, "Found at %s\n", path2);
                        return f;
                    }
                }
                tok = strtok(NULL, PATH_SEP);
            }
        }
    } else {
        // executable is relative to some directory
        fprintf(stdout, "Searching executable directory\n");
        strcpy(buffer, home);
        char* p = strrchr(buffer, FILE_SEP);
        if (p) {
            *p = 0;
            sprintf(path, "%s%c%s", buffer, FILE_SEP, name);
            f = fopen(path, "r");
            if (f != NULL)
                return f;
        }
424
    }
425
#else // ! _WIN32
426
427
428
429
430
431
    fprintf(stdout, "Searching data directory: %s\n", PKGDATADIR);
    sprintf(path, "%s%c%s", PKGDATADIR, FILE_SEP, name);
    f = fopen(path, "r");
    if (f != NULL)
        return f;

432
433
    fprintf(stdout, "Searching system config directory: %s\n", SYSCONF_INSTALL_DIR);
    sprintf(path, "%s%c%s", SYSCONF_INSTALL_DIR, FILE_SEP, name);
434
435
436
    f = fopen(path, "r");
    if (f != NULL)
        return f;
437
438
#endif // ! _WIN32

439
    return NULL;
440
441
442
}

static void sdlOpenGLScaleWithAspect(int w, int h)
443
{
444
445
446
447
448
449
450
451
452
453
454
455
    float screenAspect = (float)sizeX / sizeY,
          windowAspect = (float)w / h;

    if (windowAspect == screenAspect)
        glViewport(0, 0, w, h);
    else if (windowAspect < screenAspect) {
        int height = (int)(w / screenAspect);
        glViewport(0, (h - height) / 2, w, height);
    } else {
        int width = (int)(h * screenAspect);
        glViewport((w - width) / 2, 0, width, h);
    }
456
457
458
459
}

static void sdlOpenGLVideoResize()
{
460
461
    if (glIsTexture(screenTexture))
        glDeleteTextures(1, &screenTexture);
462

463
464
    glGenTextures(1, &screenTexture);
    glBindTexture(GL_TEXTURE_2D, screenTexture);
465

466
467
468
469
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
        openGL == 2 ? GL_LINEAR : GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
        openGL == 2 ? GL_LINEAR : GL_NEAREST);
470

471
472
473
474
    // Calculate texture size as a the smallest working power of two
    float n1 = log10((float)destWidth) / log10(2.0f);
    float n2 = log10((float)destHeight) / log10(2.0f);
    float n = (n1 > n2) ? n1 : n2;
475
476

    // round up
477
478
    if (((float)((int)n)) != n)
        n = ((float)((int)n)) + 1.0f;
479

480
    textureSize = (int)pow(2.0f, n);
481

482
483
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSize, textureSize, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, NULL);
484

485
    glClear(GL_COLOR_BUFFER_BIT);
486

487
    sdlOpenGLScaleWithAspect(destWidth, destHeight);
488
489
490
491
492
493
494
495
496
497
498
499
}

void sdlOpenGLInit(int w, int h)
{

#if 0
  float screenAspect = (float) sizeX / sizeY,
        windowAspect = (float) w / h;

  if(glIsTexture(screenTexture))
  glDeleteTextures(1, &screenTexture);
#endif
500
501
    glDisable(GL_CULL_FACE);
    glEnable(GL_TEXTURE_2D);
502

503
#if 0
504
505
506
507
508
509
510
511
512
  if(windowAspect == screenAspect)
    glViewport(0, 0, w, h);
  else if (windowAspect < screenAspect) {
    int height = (int)(w / screenAspect);
    glViewport(0, (h - height) / 2, w, height);
  } else {
    int width = (int)(h * screenAspect);
    glViewport((w - width) / 2, 0, width, h);
  }
513
#endif
514

515
516
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
517

518
    glOrtho(0.0, 1.0, 1.0, 0.0, 0.0, 1.0);
519

520
521
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
522

523
#if 0
524
525
526
527
528
529
530
531
  glGenTextures(1, &screenTexture);
  glBindTexture(GL_TEXTURE_2D, screenTexture);

  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                  openGL == 2 ? GL_LINEAR : GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                  openGL == 2 ? GL_LINEAR : GL_NEAREST);

mudlord's avatar
mudlord committed
532
533
534
535
536
537
538
539
540
541
542
  // Calculate texture size as a the smallest working power of two
  float n1 = log10((float)destWidth ) / log10( 2.0f);
  float n2 = log10((float)destHeight ) / log10( 2.0f);
  float n = (n1 > n2)? n1 : n2;

    // round up
  if (((float)((int)n)) != n)
    n = ((float)((int)n)) + 1.0f;

  textureSize = (int)pow(2.0f, n);

543
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSize, textureSize, 0,
544
545
               GL_RGBA, GL_UNSIGNED_BYTE, NULL);
#endif
mudlord's avatar
mudlord committed
546

547
    glClearColor(0.0, 0.0, 0.0, 1.0);
548

549
    sdlOpenGLVideoResize();
550
551
}

552
553
static void sdlApplyPerImagePreferences()
{
554
555
556
557
558
559
    FILE* f = sdlFindFile("vba-over.ini");
    if (!f) {
        fprintf(stdout, "vba-over.ini NOT FOUND (using emulator settings)\n");
        return;
    } else
        fprintf(stdout, "Reading vba-over.ini\n");
560

561
562
563
564
565
566
567
568
    char buffer[7];
    buffer[0] = '[';
    buffer[1] = rom[0xac];
    buffer[2] = rom[0xad];
    buffer[3] = rom[0xae];
    buffer[4] = rom[0xaf];
    buffer[5] = ']';
    buffer[6] = 0;
569

570
    char readBuffer[2048];
571

572
    bool found = false;
573

574
575
    while (1) {
        char* s = fgets(readBuffer, 2048, f);
576

577
578
        if (s == NULL)
            break;
579

580
        char* p = strchr(s, ';');
581

582
583
        if (p)
            *p = 0;
584

585
        char* token = strtok(s, " \t\n\r=");
586

587
588
589
590
591
592
593
594
595
        if (!token)
            continue;
        if (strlen(token) == 0)
            continue;

        if (!strcmp(token, buffer)) {
            found = true;
            break;
        }
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

    if (found) {
        while (1) {
            char* s = fgets(readBuffer, 2048, f);

            if (s == NULL)
                break;

            char* p = strchr(s, ';');
            if (p)
                *p = 0;

            char* token = strtok(s, " \t\n\r=");
            if (!token)
                continue;
            if (strlen(token) == 0)
                continue;

            if (token[0] == '[') // starting another image settings
                break;
            char* value = strtok(NULL, "\t\n\r=");
            if (value == NULL)
                continue;

            if (!strcmp(token, "rtcEnabled"))
                rtcEnable(atoi(value) == 0 ? false : true);
            else if (!strcmp(token, "flashSize")) {
                int size = atoi(value);
                if (size == 0x10000 || size == 0x20000)
                    flashSetSize(size);
            } else if (!strcmp(token, "saveType")) {
                int save = atoi(value);
                if (save >= 0 && save <= 5)
                    cpuSaveType = save;
            } else if (!strcmp(token, "mirroringEnabled")) {
                mirroringEnable = (atoi(value) == 0 ? false : true);
            }
        }
    }
    fclose(f);
637
638
}

639
static int sdlCalculateShift(uint32_t mask)
640
{
641
    int m = 0;
642

643
644
645
646
    while (mask) {
        m++;
        mask >>= 1;
    }
647

648
    return m - 5;
649
650
}

651
652
653
654
/* returns filename of savestate num, in static buffer (not reentrant, no need to free,
 * but value won't survive much - so if you want to remember it, dup it)
 * You may use the buffer for something else though - until you call sdlStateName again
 */
655
static char* sdlStateName(int num)
656
{
657
    static char stateName[2048];
658

659
    if (saveDir)
660
661
662
663
664
665
        sprintf(stateName, "%s/%s%d.sgm", saveDir, sdlGetFilename(filename),
            num + 1);
    else if (homeDir)
        sprintf(stateName, "%s/%s/%s%d.sgm", homeDir, DOT_DIR, sdlGetFilename(filename), num + 1);
    else
        sprintf(stateName, "%s%d.sgm", filename, num + 1);
666

667
    return stateName;
668
669
670
671
}

void sdlWriteState(int num)
{
672
    char* stateName;
673

674
    stateName = sdlStateName(num);
675

676
677
    if (emulator.emuWriteState)
        emulator.emuWriteState(stateName);
678

679
680
681
682
683
684
685
686
    // now we reuse the stateName buffer - 2048 bytes fit in a lot
    if (num == SLOT_POS_LOAD_BACKUP) {
        sprintf(stateName, "Current state backed up to %d", num + 1);
        systemScreenMessage(stateName);
    } else if (num >= 0) {
        sprintf(stateName, "Wrote state %d", num + 1);
        systemScreenMessage(stateName);
    }
687

688
    systemDrawScreen();
689
690
691
692
}

void sdlReadState(int num)
{
693
    char* stateName;
694

695
696
697
698
699
700
701
702
703
704
705
706
707
708
    stateName = sdlStateName(num);
    if (emulator.emuReadState)
        emulator.emuReadState(stateName);

    if (num == SLOT_POS_LOAD_BACKUP) {
        sprintf(stateName, "Last load UNDONE");
    } else if (num == SLOT_POS_SAVE_BACKUP) {
        sprintf(stateName, "Last save UNDONE");
    } else {
        sprintf(stateName, "Loaded state %d", num + 1);
    }
    systemScreenMessage(stateName);

    systemDrawScreen();
709
710
}

711
712
713
714
715
716
717
/*
 * perform savestate exchange
 * - put the savestate in slot "to" to slot "backup" (unless backup == to)
 * - put the savestate in slot "from" to slot "to" (unless from == to)
 */
void sdlWriteBackupStateExchange(int from, int to, int backup)
{
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
    char* dmp;
    char* stateNameOrig = NULL;
    char* stateNameDest = NULL;
    char* stateNameBack = NULL;

    dmp = sdlStateName(from);
    stateNameOrig = (char*)realloc(stateNameOrig, strlen(dmp) + 1);
    strcpy(stateNameOrig, dmp);
    dmp = sdlStateName(to);
    stateNameDest = (char*)realloc(stateNameDest, strlen(dmp) + 1);
    strcpy(stateNameDest, dmp);
    dmp = sdlStateName(backup);
    stateNameBack = (char*)realloc(stateNameBack, strlen(dmp) + 1);
    strcpy(stateNameBack, dmp);

    /* on POSIX, rename would not do anything anyway for identical names, but let's check it ourselves anyway */
    if (to != backup) {
        if (-1 == rename(stateNameDest, stateNameBack)) {
            fprintf(stdout, "savestate backup: can't backup old state %s to %s", stateNameDest, stateNameBack);
            perror(": ");
        }
    }
    if (to != from) {
        if (-1 == rename(stateNameOrig, stateNameDest)) {
            fprintf(stdout, "savestate backup: can't move new state %s to %s", stateNameOrig, stateNameDest);
            perror(": ");
        }
    }
746

747
748
    systemConsoleMessage("Savestate store and backup committed"); // with timestamp and newline
    fprintf(stdout, "to slot %d, backup in %d, using temporary slot %d\n", to + 1, backup + 1, from + 1);
749

750
751
752
    free(stateNameOrig);
    free(stateNameDest);
    free(stateNameBack);
753
754
}

755
756
void sdlWriteBattery()
{
757
    char buffer[1048];
758

759
760
761
762
763
764
    if (batteryDir)
        sprintf(buffer, "%s/%s.sav", batteryDir, sdlGetFilename(filename));
    else if (homeDir)
        sprintf(buffer, "%s/%s/%s.sav", homeDir, DOT_DIR, sdlGetFilename(filename));
    else
        sprintf(buffer, "%s.sav", filename);
765

766
    emulator.emuWriteBattery(buffer);
767

768
    systemScreenMessage("Wrote battery");
769
770
771
772
}

void sdlReadBattery()
{
773
    char buffer[1048];
774

775
776
777
778
779
780
    if (batteryDir)
        sprintf(buffer, "%s/%s.sav", batteryDir, sdlGetFilename(filename));
    else if (homeDir)
        sprintf(buffer, "%s/%s/%s.sav", homeDir, DOT_DIR, sdlGetFilename(filename));
    else
        sprintf(buffer, "%s.sav", filename);
781

782
    bool res = false;
783

784
    res = emulator.emuReadBattery(buffer);
785

786
787
    if (res)
        systemScreenMessage("Loaded battery");
788
789
}

790
791
792
793
794
795
void sdlReadDesktopVideoMode()
{
    SDL_DisplayMode dm;
    SDL_GetDesktopDisplayMode(SDL_GetWindowDisplayIndex(window), &dm);
    desktopWidth = dm.w;
    desktopHeight = dm.h;
796
797
}

798
799
800
static void sdlResizeVideo()
{
    filter_enlarge = getFilterEnlargeFactor(filter);
801

802
803
    destWidth = filter_enlarge * sizeX;
    destHeight = filter_enlarge * sizeY;
804

805
806
    if (openGL) {
        free(filterPix);
807
        filterPix = (uint8_t*)calloc(1, (systemColorDepth >> 3) * destWidth * destHeight);
808
809
        sdlOpenGLVideoResize();
    }
810

811
812
813
814
815
816
817
818
819
820
821
822
823
    if (surface)
        SDL_FreeSurface(surface);
    if (texture)
        SDL_DestroyTexture(texture);

    if (!openGL) {
        surface = SDL_CreateRGBSurface(0, destWidth, destHeight, 32,
            0x00FF0000, 0x0000FF00,
            0x000000FF, 0xFF000000);
        texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
            SDL_TEXTUREACCESS_STREAMING,
            destWidth, destHeight);
    }
824

825
826
827
828
829
    if (!openGL && surface == NULL) {
        systemMessage(0, "Failed to set video mode");
        SDL_Quit();
        exit(-1);
    }
mudlord's avatar
mudlord committed
830
831
}

832
833
834
835
836
void sdlInitVideo()
{
    int flags;
    int screenWidth;
    int screenHeight;
mudlord's avatar
mudlord committed
837

838
    filter_enlarge = getFilterEnlargeFactor(filter);
839

840
841
    destWidth = filter_enlarge * sizeX;
    destHeight = filter_enlarge * sizeY;
mudlord's avatar
mudlord committed
842

843
844
845
846
847
    flags = fullScreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
    if (openGL) {
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
    }
mudlord's avatar
mudlord committed
848

mudlord's avatar
mudlord committed
849
850
    screenWidth = destWidth;
    screenHeight = destHeight;
851

852
853
854
855
856
857
858
859
860
    if (window)
        SDL_DestroyWindow(window);
    if (renderer)
        SDL_DestroyRenderer(renderer);
    window = SDL_CreateWindow("VBA-M", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        screenWidth, screenHeight, flags);
    if (!openGL) {
        renderer = SDL_CreateRenderer(window, -1, 0);
    }
mudlord's avatar
mudlord committed
861

862
863
864
865
866
    if (window == NULL) {
        systemMessage(0, "Failed to set video mode");
        SDL_Quit();
        exit(-1);
    }
mudlord's avatar
mudlord committed
867

868
    uint32_t rmask, gmask, bmask;
869

870
#if 0
871
  if(openGL) {
872
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
873
874
875
      rmask = 0x000000FF;
      gmask = 0x0000FF00;
      bmask = 0x00FF0000;
876
#else
877
      rmask = 0xFF000000;
878
879
      gmask = 0x00FF0000;
      bmask = 0x0000FF00;
880
#endif
881
882
883
884
885
  } else {
      rmask = surface->format->Rmask;
      gmask = surface->format->Gmask;
      bmask = surface->format->Bmask;
  }
886
887
#endif

888
889
890
891
892
893
894
895
896
    if (openGL) {
        rmask = 0xFF000000;
        gmask = 0x00FF0000;
        bmask = 0x0000FF00;
    } else {
        rmask = 0x00FF0000;
        gmask = 0x0000FF00;
        bmask = 0x000000FF;
    }
897

898
899
900
    systemRedShift = sdlCalculateShift(rmask);
    systemGreenShift = sdlCalculateShift(gmask);
    systemBlueShift = sdlCalculateShift(bmask);
901

902
903
904
    //printf("systemRedShift %d, systemGreenShift %d, systemBlueShift %d\n",
    //         systemRedShift, systemGreenShift, systemBlueShift);
    //  originally 3, 11, 19 -> 27, 19, 11
905

906
907
908
909
910
911
    if (openGL) {
        // Align to BGRA instead of ABGR
        systemRedShift += 8;
        systemGreenShift += 8;
        systemBlueShift += 8;
    }
mudlord's avatar
mudlord committed
912

913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#if 0
  if (openGL) {
    systemColorDepth = 0;
    int i;
    glcontext = SDL_GL_CreateContext(window);
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &i);
    systemColorDepth += i;
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &i);
    systemColorDepth += i;
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &i);
    systemColorDepth += i;
    printf("color depth (without alpha) is %d\n", systemColorDepth);
    SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &i);
    systemColorDepth += i;
    printf("color depth is %d\n", systemColorDepth);
  }
  else
    systemColorDepth = 32;
mudlord's avatar
mudlord committed
931
932

  if(systemColorDepth == 16) {
933
    srcPitch = sizeX*2 + 4;
mudlord's avatar
mudlord committed
934
935
  } else {
    if(systemColorDepth == 32)
936
      srcPitch = sizeX*4 + 4;
mudlord's avatar
mudlord committed
937
    else
938
      srcPitch = sizeX*3;
mudlord's avatar
mudlord committed
939
  }
mudlord's avatar
mudlord committed
940

941
942
#endif

943
944
    systemColorDepth = 32;
    srcPitch = sizeX * 4 + 4;
945

946
947
948
949
    if (openGL) {
        glcontext = SDL_GL_CreateContext(window);
        sdlOpenGLInit(screenWidth, screenHeight);
    }
950

951
    sdlResizeVideo();
952
953

#if 0
mudlord's avatar
mudlord committed
954
  if(openGL) {
955
956
957
    int scaledWidth = screenWidth * sdlOpenglScale;
    int scaledHeight = screenHeight * sdlOpenglScale;

mudlord's avatar
mudlord committed
958
    free(filterPix);
959
    filterPix = (uint8_t *)calloc(1, (systemColorDepth >> 3) * destWidth * destHeight);
mudlord's avatar
mudlord committed
960
    sdlOpenGLInit(screenWidth, screenHeight);
961

962
    if (	(!fullScreen)
963
964
965
966
967
968
	&&	sdlOpenglScale	> 1
	&&	scaledWidth	< desktopWidth
	&&	scaledHeight	< desktopHeight
    ) {
        SDL_SetVideoMode(scaledWidth, scaledHeight, 0,
                       SDL_OPENGL | SDL_RESIZABLE |
969
                       (fullScreen ? SDL_FULLSCREEN : 0));
970
971
972
973
974
975
        sdlOpenGLInit(scaledWidth, scaledHeight);
	/* xKiv: it would seem that SDL_RESIZABLE causes the *previous* dimensions to be immediately
	 * reported back via the SDL_VIDEORESIZE event
	 */
	ignore_first_resize_event	= 1;
    }
mudlord's avatar
mudlord committed
976
  }
977
#endif
mudlord's avatar
mudlord committed
978
}
979
980
981
#if defined(KMOD_GUI)
#define KMOD_META KMOD_GUI
#endif
mudlord's avatar
mudlord committed
982

983
984
985
986
#define MOD_KEYS (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT | KMOD_META)
#define MOD_NOCTRL (KMOD_SHIFT | KMOD_ALT | KMOD_META)
#define MOD_NOALT (KMOD_CTRL | KMOD_SHIFT | KMOD_META)
#define MOD_NOSHIFT (KMOD_CTRL | KMOD_ALT | KMOD_META)
987

988
989
990
991
992
993
/*
 * 04.02.2008 (xKiv): factored out from sdlPollEvents
 *
 */
void change_rewind(int howmuch)
{
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
    if (emulating && emulator.emuReadMemState && rewindMemory
        && rewindCount) {
        rewindPos = (rewindPos + rewindCount + howmuch) % rewindCount;
        emulator.emuReadMemState(
            &rewindMemory[REWIND_SIZE * rewindPos],
            REWIND_SIZE);
        rewindCounter = 0;
        {
            char rewindMsgBuffer[50];
            sprintf(rewindMsgBuffer, "Rewind to %1d [%d]", rewindPos + 1, rewindSerials[rewindPos]);
            rewindMsgBuffer[49] = 0;
            systemConsoleMessage(rewindMsgBuffer);
        }
    }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
}

/*
 * handle the F* keys (for savestates)
 * given the slot number and state of the SHIFT modifier, save or restore
 * (in savemode 3, saveslot is stored in saveSlotPosition and num means:
 *  4 .. F5: decrease slot number (down to 0)
 *  5 .. F6: increase slot number (up to 7, because 8 and 9 are reserved for backups)
 *  6 .. F7: save state
 *  7 .. F8: load state
 *  (these *should* be configurable)
 *  other keys are ignored
 * )
 */
static void sdlHandleSavestateKey(int num, int shifted)
{
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
    int action = -1;
    // 0: load
    // 1: save
    int backuping = 1; // controls whether we are doing savestate backups

    if (sdlSaveKeysSwitch == 2) {
        // ignore "shifted"
        switch (num) {
        // nb.: saveSlotPosition is base 0, but to the user, we show base 1 indexes (F## numbers)!
        case 4:
            if (saveSlotPosition > 0) {
                saveSlotPosition--;
                fprintf(stdout, "Changed savestate slot to %d.\n", saveSlotPosition + 1);
            } else
                fprintf(stderr, "Can't decrease slotnumber below 1.\n");
            return; // handled
        case 5:
            if (saveSlotPosition < 7) {
                saveSlotPosition++;
                fprintf(stdout, "Changed savestate slot to %d.\n", saveSlotPosition + 1);
            } else
                fprintf(stderr, "Can't increase slotnumber above 8.\n");
            return; // handled
        case 6:
            action = 1; // save
            break;
        case 7:
            action = 0; // load
            break;
        default:
            // explicitly ignore
            return; // handled
        }
    }

    if (sdlSaveKeysSwitch == 0) /* "classic" VBA: shifted is save */
    {
        if (shifted)
            action = 1; // save
        else
            action = 0; // load
        saveSlotPosition = num;
    }
    if (sdlSaveKeysSwitch == 1) /* "xKiv" VBA: shifted is load */
    {
        if (!shifted)
            action = 1; // save
        else
            action = 0; // load
        saveSlotPosition = num;
    }

    if (action < 0 || action > 1) {
        fprintf(
            stderr,
            "sdlHandleSavestateKey(%d,%d), mode %d: unexpected action %d.\n",
            num,
            shifted,
            sdlSaveKeysSwitch,
            action);
    }

    if (action) { /* save */
        if (backuping) {
            sdlWriteState(-1); // save to a special slot
            sdlWriteBackupStateExchange(-1, saveSlotPosition, SLOT_POS_SAVE_BACKUP); // F10
        } else {
            sdlWriteState(saveSlotPosition);
        }
    } else { /* load */
        if (backuping) {
            /* first back up where we are now */
            sdlWriteState(SLOT_POS_LOAD_BACKUP); // F9
1097
        }
1098
1099
        sdlReadState(saveSlotPosition);
    }
1100
1101
1102

} // sdlHandleSavestateKey

1103
1104
void sdlPollEvents()
{
1105
1106
1107
1108
1109
1110
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        switch (event.type) {
        case SDL_QUIT:
            emulating = 0;
            break;
1111
#if 0
mudlord's avatar
mudlord committed
1112
    case SDL_VIDEORESIZE:
1113
1114
1115
1116
1117
      if (ignore_first_resize_event)
      {
	      ignore_first_resize_event	= 0;
	      break;
      }
mudlord's avatar
mudlord committed
1118
1119
      if (openGL)
      {
mudlord's avatar
mudlord committed
1120
        SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
mudlord's avatar
mudlord committed
1121
                       SDL_OPENGL | SDL_RESIZABLE |
1122
					   (fullScreen ? SDL_FULLSCREEN : 0));
mudlord's avatar
mudlord committed
1123
1124