uvmac/src/UI/SDL2/OSGLUSD2.c

1121 lines
20 KiB
C
Raw Normal View History

2019-07-23 02:50:34 +00:00
/*
OSGLUSD2.c
Copyright (C) 2012 Paul C. Pratt, Manuel Alfayate
You can redistribute this file and/or modify it under the terms
of version 2 of the GNU General Public License as published by
the Free Software Foundation. You should have received a copy
of the license along with this file; see the file COPYING.
This file 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
license for more details.
*/
/*
Operating System GLUe for SDl 2.0 library
All operating system dependent code for the
SDL Library should go here.
*/
2020-07-01 01:33:55 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2023-09-23 20:45:18 +00:00
#include <SDL.h>
2019-07-23 02:50:34 +00:00
#include "CNFGRAPI.h"
#include "SYSDEPNS.h"
#include "UTIL/ENDIANAC.h"
2020-07-01 01:33:55 +00:00
#include "UI/MYOSGLUE.h"
#include "STRCONST.h"
#include "OSGLUSD2.h"
#include "LANG/INTLCHAR.h"
#include "HW/SCREEN/SCRNEMDV.h"
#include "HW/ROM/ROMEMDEV.h"
#include "CFGMAN.h"
2019-07-23 02:50:34 +00:00
2020-07-01 01:33:55 +00:00
/* --- some simple utilities --- */
2019-07-23 02:50:34 +00:00
2020-07-01 01:33:55 +00:00
GLOBALOSGLUPROC MoveBytes(anyp srcPtr, anyp destPtr, int32_t byteCount)
2019-07-23 02:50:34 +00:00
{
2020-07-01 01:33:55 +00:00
(void) memcpy((char *)destPtr, (char *)srcPtr, byteCount);
2019-07-23 02:50:34 +00:00
}
2020-07-01 01:33:55 +00:00
/* --- information about the environment --- */
2019-07-23 02:50:34 +00:00
2020-07-01 01:33:55 +00:00
#define WantColorTransValid 0
2019-07-23 02:50:34 +00:00
2020-07-01 01:33:55 +00:00
#include "UI/COMOSGLU.h"
#include "UTIL/PBUFSTDC.h"
#include "UI/CONTROLM.h"
2019-07-23 02:50:34 +00:00
/* --- basic dialogs --- */
static void CheckSavedMacMsg(void)
2019-07-23 02:50:34 +00:00
{
/* called only on quit, if error saved but not yet reported */
if (nullpr != SavedBriefMsg) {
char briefMsg0[ClStrMaxLength + 1];
char longMsg0[ClStrMaxLength + 1];
NativeStrFromCStr(briefMsg0, SavedBriefMsg);
NativeStrFromCStr(longMsg0, SavedLongMsg);
if (0 != SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR,
SavedBriefMsg,
SavedLongMsg,
2020-02-11 05:34:32 +00:00
main_wind
2019-07-23 02:50:34 +00:00
))
{
fprintf(stderr, "%s\n", briefMsg0);
fprintf(stderr, "%s\n", longMsg0);
}
SavedBriefMsg = nullpr;
}
}
/* --- event handling for main window --- */
#define UseMotionEvents 1
#if UseMotionEvents
static bool CaughtMouse = false;
2019-07-23 02:50:34 +00:00
#endif
static void HandleTheEvent(SDL_Event *event)
2019-07-23 02:50:34 +00:00
{
switch (event->type) {
case SDL_QUIT:
2020-02-11 05:34:32 +00:00
RequestMacOff = true;
2019-07-23 02:50:34 +00:00
break;
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_FOCUS_GAINED:
gTrueBackgroundFlag = 0;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
gTrueBackgroundFlag = 1;
break;
case SDL_WINDOWEVENT_ENTER:
CaughtMouse = 1;
break;
case SDL_WINDOWEVENT_LEAVE:
CaughtMouse = 0;
break;
}
break;
case SDL_MOUSEMOTION:
#if EnableFSMouseMotion && ! HaveWorkingWarp
if (HaveMouseMotion) {
MousePositionNotifyRelative(
event->motion.xrel, event->motion.yrel);
} else
#endif
{
MousePositionNotify(
event->motion.x, event->motion.y);
}
break;
case SDL_MOUSEBUTTONDOWN:
/* any mouse button, we don't care which */
#if EnableFSMouseMotion && ! HaveWorkingWarp
if (HaveMouseMotion) {
/* ignore position */
} else
#endif
{
MousePositionNotify(
event->button.x, event->button.y);
}
2020-02-11 05:34:32 +00:00
MouseButtonSet(true);
2019-07-23 02:50:34 +00:00
break;
case SDL_MOUSEBUTTONUP:
#if EnableFSMouseMotion && ! HaveWorkingWarp
if (HaveMouseMotion) {
/* ignore position */
} else
#endif
{
MousePositionNotify(
event->button.x, event->button.y);
}
2020-02-11 05:34:32 +00:00
MouseButtonSet(false);
2019-07-23 02:50:34 +00:00
break;
case SDL_KEYDOWN:
2020-02-11 05:34:32 +00:00
DoKeyCode(&event->key.keysym, true);
2019-07-23 02:50:34 +00:00
break;
case SDL_KEYUP:
2020-02-11 05:34:32 +00:00
DoKeyCode(&event->key.keysym, false);
2019-07-23 02:50:34 +00:00
break;
/*case SDL_MOUSEWHEEL:
2019-07-23 02:50:34 +00:00
if (event->wheel.x < 0) {
2020-02-11 05:34:32 +00:00
Keyboard_UpdateKeyMap2(MKC_Left, true);
Keyboard_UpdateKeyMap2(MKC_Left, false);
2019-07-23 02:50:34 +00:00
} else if (event->wheel.x > 0) {
2020-02-11 05:34:32 +00:00
Keyboard_UpdateKeyMap2(MKC_Right, true);
Keyboard_UpdateKeyMap2(MKC_Right, false);
2019-07-23 02:50:34 +00:00
}
if (event->wheel.y < 0) {
2020-02-11 05:34:32 +00:00
Keyboard_UpdateKeyMap2(MKC_Down, true);
Keyboard_UpdateKeyMap2(MKC_Down, false);
2019-07-23 02:50:34 +00:00
} else if(event->wheel.y > 0) {
2020-02-11 05:34:32 +00:00
Keyboard_UpdateKeyMap2(MKC_Up, true);
Keyboard_UpdateKeyMap2(MKC_Up, false);
2019-07-23 02:50:34 +00:00
}
break;*/
2019-07-23 02:50:34 +00:00
case SDL_DROPFILE:
{
char *s = event->drop.file;
2020-02-11 05:34:32 +00:00
(void) Sony_Insert1a(s, false);
SDL_RaiseWindow(main_wind);
2019-07-23 02:50:34 +00:00
SDL_free(s);
}
break;
#if 0
case Expose: /* SDL doesn't have an expose event */
int x0 = event->expose.x;
int y0 = event->expose.y;
int x1 = x0 + event->expose.width;
int y1 = y0 + event->expose.height;
if (x0 < 0) {
x0 = 0;
}
if (x1 > vMacScreenWidth) {
x1 = vMacScreenWidth;
}
if (y0 < 0) {
y0 = 0;
}
if (y1 > vMacScreenHeight) {
y1 = vMacScreenHeight;
}
if ((x0 < x1) && (y0 < y1)) {
HaveChangedScreenBuff(y0, x0, y1, x1);
}
break;
#endif
}
}
/* --- main window creation and disposal --- */
static int argc;
static char **argv;
2019-07-23 02:50:34 +00:00
static bool SDL_InitDisplay(void)
2019-07-23 02:50:34 +00:00
{
2020-02-11 05:34:32 +00:00
bool v = false;
2019-07-23 02:50:34 +00:00
InitKeyCodes();
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
} else {
2020-02-11 05:34:32 +00:00
v = true;
2019-07-23 02:50:34 +00:00
}
return v;
}
#if MayFullScreen
static bool GrabMachine = false;
2019-07-23 02:50:34 +00:00
#endif
#if MayFullScreen
static void GrabTheMachine(void)
2019-07-23 02:50:34 +00:00
{
#if GrabKeysFullScreen
2020-02-11 05:34:32 +00:00
SDL_SetWindowGrab(main_wind, SDL_TRUE);
2019-07-23 02:50:34 +00:00
#endif
#if EnableFSMouseMotion
#if HaveWorkingWarp
/*
if magnification changes, need to reset,
even if HaveMouseMotion already true
*/
if (MoveMouse(ViewHStart + (ViewHSize / 2),
2019-07-23 02:50:34 +00:00
ViewVStart + (ViewVSize / 2)))
{
SavedMouseH = ViewHStart + (ViewHSize / 2);
SavedMouseV = ViewVStart + (ViewVSize / 2);
2020-02-11 05:34:32 +00:00
HaveMouseMotion = true;
2019-07-23 02:50:34 +00:00
}
#else
if (0 == SDL_SetRelativeMouseMode(SDL_ENABLE)) {
2020-02-11 05:34:32 +00:00
HaveMouseMotion = true;
2019-07-23 02:50:34 +00:00
}
#endif
#endif /* EnableFSMouseMotion */
}
#endif
#if MayFullScreen
static void UngrabMachine(void)
2019-07-23 02:50:34 +00:00
{
#if EnableFSMouseMotion
if (HaveMouseMotion) {
#if HaveWorkingWarp
(void) MoveMouse(CurMouseH, CurMouseV);
2019-07-23 02:50:34 +00:00
#else
SDL_SetRelativeMouseMode(SDL_DISABLE);
#endif
2020-02-11 05:34:32 +00:00
HaveMouseMotion = false;
2019-07-23 02:50:34 +00:00
}
#endif /* EnableFSMouseMotion */
#if GrabKeysFullScreen
2020-02-11 05:34:32 +00:00
SDL_SetWindowGrab(main_wind, SDL_FALSE);
2019-07-23 02:50:34 +00:00
#endif
}
#endif
#if EnableFSMouseMotion && HaveWorkingWarp
static void MouseConstrain(void)
2019-07-23 02:50:34 +00:00
{
int16_t shiftdh;
int16_t shiftdv;
2019-07-23 02:50:34 +00:00
if (SavedMouseH < ViewHStart + (ViewHSize / 4)) {
shiftdh = ViewHSize / 2;
} else if (SavedMouseH > ViewHStart + ViewHSize - (ViewHSize / 4)) {
shiftdh = - ViewHSize / 2;
} else {
shiftdh = 0;
}
if (SavedMouseV < ViewVStart + (ViewVSize / 4)) {
shiftdv = ViewVSize / 2;
} else if (SavedMouseV > ViewVStart + ViewVSize - (ViewVSize / 4)) {
shiftdv = - ViewVSize / 2;
} else {
shiftdv = 0;
}
if ((shiftdh != 0) || (shiftdv != 0)) {
SavedMouseH += shiftdh;
SavedMouseV += shiftdv;
if (! MoveMouse(SavedMouseH, SavedMouseV)) {
2020-02-11 05:34:32 +00:00
HaveMouseMotion = false;
2019-07-23 02:50:34 +00:00
}
}
}
#endif
enum {
kMagStateNormal,
#if 1
2019-07-23 02:50:34 +00:00
kMagStateMagnifgy,
#endif
kNumMagStates
};
#define kMagStateAuto kNumMagStates
#if MayNotFullScreen
static int CurWinIndx;
static bool HavePositionWins[kNumMagStates];
static int WinPositionsX[kNumMagStates];
static int WinPositionsY[kNumMagStates];
2019-07-23 02:50:34 +00:00
#endif
static bool CreateMainWindow(void)
2019-07-23 02:50:34 +00:00
{
int NewWindowX;
int NewWindowY;
int NewWindowHeight = vMacScreenHeight;
int NewWindowWidth = vMacScreenWidth;
Uint32 flags = 0 /* SDL_WINDOW_HIDDEN */;
2020-02-11 05:34:32 +00:00
bool v = false;
2019-07-23 02:50:34 +00:00
#if 1
2019-07-23 02:50:34 +00:00
if (UseFullScreen)
#endif
#if MayFullScreen
{
/*
We don't want physical screen mode to be changed in modern
displays, so we pass this _DESKTOP flag.
*/
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
NewWindowX = SDL_WINDOWPOS_UNDEFINED;
NewWindowY = SDL_WINDOWPOS_UNDEFINED;
}
#endif
#if 1
2019-07-23 02:50:34 +00:00
else
#endif
#if MayNotFullScreen
{
int WinIndx;
#if 1
2019-07-23 02:50:34 +00:00
if (UseMagnify) {
WinIndx = kMagStateMagnifgy;
} else
#endif
{
WinIndx = kMagStateNormal;
}
if (! HavePositionWins[WinIndx]) {
NewWindowX = SDL_WINDOWPOS_CENTERED;
NewWindowY = SDL_WINDOWPOS_CENTERED;
} else {
NewWindowX = WinPositionsX[WinIndx];
NewWindowY = WinPositionsY[WinIndx];
}
CurWinIndx = WinIndx;
}
#endif
#if 0
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
#endif
2020-02-11 05:34:32 +00:00
if (NULL == (main_wind = SDL_CreateWindow(
2020-07-01 01:33:55 +00:00
kStrAppName,
2019-07-23 02:50:34 +00:00
NewWindowX, NewWindowY,
NewWindowWidth, NewWindowHeight,
flags)))
{
fprintf(stderr, "SDL_CreateWindow fails: %s\n", SDL_GetError());
2019-07-23 02:50:34 +00:00
} else
2020-02-11 05:34:32 +00:00
if (NULL == (renderer = SDL_CreateRenderer(
main_wind, -1,
2019-07-23 02:50:34 +00:00
0 /* SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC */
/*
SDL_RENDERER_ACCELERATED not needed
"no flags gives priority to available
SDL_RENDERER_ACCELERATED renderers"
*/
/* would rather not require vsync */
)))
{
fprintf(stderr, "SDL_CreateRenderer fails: %s\n", SDL_GetError());
2019-07-23 02:50:34 +00:00
} else
2020-02-11 05:34:32 +00:00
if (NULL == (texture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_RGBX8888,
2019-07-23 02:50:34 +00:00
SDL_TEXTUREACCESS_STREAMING,
vMacScreenWidth, vMacScreenHeight
)))
{
fprintf(stderr, "SDL_CreateTexture fails: %s\n", SDL_GetError());
2019-07-23 02:50:34 +00:00
} else
2020-02-11 05:34:32 +00:00
if (NULL == (format = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888)))
2019-07-23 02:50:34 +00:00
{
fprintf(stderr, "SDL_AllocFormat fails: %s\n", SDL_GetError());
2019-07-23 02:50:34 +00:00
} else
{
2020-02-11 05:34:32 +00:00
SDL_RenderClear(renderer);
2019-07-23 02:50:34 +00:00
SDL_DisplayMode info;
if (0 != SDL_GetCurrentDisplayMode(0, &info)) {
fprintf(stderr, "SDL_GetCurrentDisplayMode fails: %s\n",
SDL_GetError());
2020-02-11 05:34:32 +00:00
return false;
2019-07-23 02:50:34 +00:00
}
#if 1
2019-07-23 02:50:34 +00:00
if (UseFullScreen)
#endif
#if MayFullScreen
{
int wr;
int hr;
2020-02-11 05:34:32 +00:00
SDL_GL_GetDrawableSize(main_wind, &wr, &hr);
2019-07-23 02:50:34 +00:00
ViewHSize = wr;
ViewVSize = hr;
#if 1
2019-07-23 02:50:34 +00:00
if (UseMagnify) {
ViewHSize /= WindowScale;
ViewVSize /= WindowScale;
2019-07-23 02:50:34 +00:00
}
#endif
if (ViewHSize >= vMacScreenWidth) {
ViewHStart = 0;
ViewHSize = vMacScreenWidth;
} else {
ViewHSize &= ~ 1;
}
if (ViewVSize >= vMacScreenHeight) {
ViewVStart = 0;
ViewVSize = vMacScreenHeight;
} else {
ViewVSize &= ~ 1;
}
if (wr > NewWindowWidth) {
hOffset = (wr - NewWindowWidth) / 2;
} else {
hOffset = 0;
}
if (hr > NewWindowHeight) {
vOffset = (hr - NewWindowHeight) / 2;
} else {
vOffset = 0;
}
}
#endif
#if 0 != vMacScreenDepth
2020-02-11 05:34:32 +00:00
ColorModeWorks = true;
2019-07-23 02:50:34 +00:00
#endif
2020-02-11 05:34:32 +00:00
v = true;
2019-07-23 02:50:34 +00:00
}
return v;
}
static void CloseMainWindow(void)
2019-07-23 02:50:34 +00:00
{
2020-02-11 05:34:32 +00:00
if (NULL != format) {
SDL_FreeFormat(format);
format = NULL;
2019-07-23 02:50:34 +00:00
}
2020-02-11 05:34:32 +00:00
if (NULL != texture) {
SDL_DestroyTexture(texture);
texture = NULL;
2019-07-23 02:50:34 +00:00
}
2020-02-11 05:34:32 +00:00
if (NULL != renderer) {
SDL_DestroyRenderer(renderer);
renderer = NULL;
2019-07-23 02:50:34 +00:00
}
2020-02-11 05:34:32 +00:00
if (NULL != main_wind) {
SDL_DestroyWindow(main_wind);
main_wind = NULL;
2019-07-23 02:50:34 +00:00
}
}
#if EnableRecreateW
static void ZapWState(void)
2019-07-23 02:50:34 +00:00
{
2020-02-11 05:34:32 +00:00
main_wind = NULL;
renderer = NULL;
texture = NULL;
format = NULL;
2019-07-23 02:50:34 +00:00
}
#endif
#if EnableRecreateW
struct WState {
2019-07-23 02:50:34 +00:00
#if MayFullScreen
uint16_t f_ViewHSize;
uint16_t f_ViewVSize;
uint16_t f_ViewHStart;
uint16_t f_ViewVStart;
2019-07-23 02:50:34 +00:00
int f_hOffset;
int f_vOffset;
#endif
#if 1
2020-02-11 05:34:32 +00:00
bool f_UseFullScreen;
2019-07-23 02:50:34 +00:00
#endif
#if 1
2020-02-11 05:34:32 +00:00
bool f_UseMagnify;
2019-07-23 02:50:34 +00:00
#endif
#if MayNotFullScreen
int f_CurWinIndx;
#endif
2020-02-11 05:34:32 +00:00
SDL_Window *f_main_wind;
SDL_Renderer *f_renderer;
SDL_Texture *f_texture;
SDL_PixelFormat *f_format;
2019-07-23 02:50:34 +00:00
};
typedef struct WState WState;
2019-07-23 02:50:34 +00:00
#endif
#if EnableRecreateW
static void GetWState(WState *r)
2019-07-23 02:50:34 +00:00
{
#if MayFullScreen
r->f_ViewHSize = ViewHSize;
r->f_ViewVSize = ViewVSize;
r->f_ViewHStart = ViewHStart;
r->f_ViewVStart = ViewVStart;
r->f_hOffset = hOffset;
r->f_vOffset = vOffset;
#endif
#if 1
2019-07-23 02:50:34 +00:00
r->f_UseFullScreen = UseFullScreen;
#endif
#if 1
2019-07-23 02:50:34 +00:00
r->f_UseMagnify = UseMagnify;
#endif
#if MayNotFullScreen
r->f_CurWinIndx = CurWinIndx;
#endif
2020-02-11 05:34:32 +00:00
r->f_main_wind = main_wind;
r->f_renderer = renderer;
r->f_texture = texture;
r->f_format = format;
2019-07-23 02:50:34 +00:00
}
#endif
#if EnableRecreateW
static void SetWState(WState *r)
2019-07-23 02:50:34 +00:00
{
#if MayFullScreen
ViewHSize = r->f_ViewHSize;
ViewVSize = r->f_ViewVSize;
ViewHStart = r->f_ViewHStart;
ViewVStart = r->f_ViewVStart;
hOffset = r->f_hOffset;
vOffset = r->f_vOffset;
#endif
#if 1
2019-07-23 02:50:34 +00:00
UseFullScreen = r->f_UseFullScreen;
#endif
#if 1
2019-07-23 02:50:34 +00:00
UseMagnify = r->f_UseMagnify;
#endif
#if MayNotFullScreen
CurWinIndx = r->f_CurWinIndx;
#endif
2020-02-11 05:34:32 +00:00
main_wind = r->f_main_wind;
renderer = r->f_renderer;
texture = r->f_texture;
format = r->f_format;
2019-07-23 02:50:34 +00:00
}
#endif
#if 1 && 1
2019-07-23 02:50:34 +00:00
enum {
kWinStateWindowed,
#if 1
2019-07-23 02:50:34 +00:00
kWinStateFullScreen,
#endif
kNumWinStates
};
#endif
#if 1 && 1
static int WinMagStates[kNumWinStates];
2019-07-23 02:50:34 +00:00
#endif
#if EnableRecreateW
static bool ReCreateMainWindow(void)
2019-07-23 02:50:34 +00:00
{
WState old_state;
WState new_state;
2019-07-23 02:50:34 +00:00
#if HaveWorkingWarp
2020-02-11 05:34:32 +00:00
bool HadCursorHidden = HaveCursorHidden;
2019-07-23 02:50:34 +00:00
#endif
#if 1 && 1
2019-07-23 02:50:34 +00:00
int OldWinState =
UseFullScreen ? kWinStateFullScreen : kWinStateWindowed;
int OldMagState =
UseMagnify ? kMagStateMagnifgy : kMagStateNormal;
WinMagStates[OldWinState] =
OldMagState;
#endif
#if 1
2019-07-23 02:50:34 +00:00
if (! UseFullScreen)
#endif
#if MayNotFullScreen
{
2020-02-11 05:34:32 +00:00
SDL_GetWindowPosition(main_wind,
2019-07-23 02:50:34 +00:00
&WinPositionsX[CurWinIndx],
&WinPositionsY[CurWinIndx]);
2020-02-11 05:34:32 +00:00
HavePositionWins[CurWinIndx] = true;
2019-07-23 02:50:34 +00:00
}
#endif
ForceShowCursor(); /* hide/show cursor api is per window */
#if MayFullScreen
if (GrabMachine) {
2020-02-11 05:34:32 +00:00
GrabMachine = false;
2019-07-23 02:50:34 +00:00
UngrabMachine();
}
#endif
GetWState(&old_state);
2019-07-23 02:50:34 +00:00
ZapWState();
2019-07-23 02:50:34 +00:00
#if 1
2019-07-23 02:50:34 +00:00
UseMagnify = WantMagnify;
#endif
#if 1
2019-07-23 02:50:34 +00:00
UseFullScreen = WantFullScreen;
#endif
if (! CreateMainWindow()) {
CloseMainWindow();
SetWState(&old_state);
2019-07-23 02:50:34 +00:00
/* avoid retry */
#if 1
2019-07-23 02:50:34 +00:00
WantFullScreen = UseFullScreen;
#endif
#if 1
2019-07-23 02:50:34 +00:00
WantMagnify = UseMagnify;
#endif
} else {
GetWState(&new_state);
SetWState(&old_state);
2019-07-23 02:50:34 +00:00
CloseMainWindow();
SetWState(&new_state);
2019-07-23 02:50:34 +00:00
#if HaveWorkingWarp
if (HadCursorHidden) {
(void) MoveMouse(CurMouseH, CurMouseV);
2019-07-23 02:50:34 +00:00
}
#endif
}
2020-02-11 05:34:32 +00:00
return true;
2019-07-23 02:50:34 +00:00
}
#endif
static void ZapWinStateVars(void)
2019-07-23 02:50:34 +00:00
{
#if MayNotFullScreen
{
int i;
for (i = 0; i < kNumMagStates; ++i) {
2020-02-11 05:34:32 +00:00
HavePositionWins[i] = false;
2019-07-23 02:50:34 +00:00
}
}
#endif
#if 1 && 1
2019-07-23 02:50:34 +00:00
{
int i;
for (i = 0; i < kNumWinStates; ++i) {
WinMagStates[i] = kMagStateAuto;
}
}
#endif
}
#if 1
2020-07-01 01:33:55 +00:00
void ToggleWantFullScreen(void)
2019-07-23 02:50:34 +00:00
{
WantFullScreen = ! WantFullScreen;
#if 1
2019-07-23 02:50:34 +00:00
{
int OldWinState =
UseFullScreen ? kWinStateFullScreen : kWinStateWindowed;
int OldMagState =
UseMagnify ? kMagStateMagnifgy : kMagStateNormal;
int NewWinState =
WantFullScreen ? kWinStateFullScreen : kWinStateWindowed;
int NewMagState = WinMagStates[NewWinState];
WinMagStates[OldWinState] = OldMagState;
if (kMagStateAuto != NewMagState) {
WantMagnify = (kMagStateMagnifgy == NewMagState);
} else {
2020-02-11 05:34:32 +00:00
WantMagnify = false;
2019-07-23 02:50:34 +00:00
if (WantFullScreen) {
SDL_Rect r;
if (0 == SDL_GetDisplayBounds(0, &r)) {
if ((r.w >= vMacScreenWidth * WindowScale)
&& (r.h >= vMacScreenHeight * WindowScale)
2019-07-23 02:50:34 +00:00
)
{
2020-02-11 05:34:32 +00:00
WantMagnify = true;
2019-07-23 02:50:34 +00:00
}
}
}
}
}
#endif
}
#endif
/* --- SavedTasks --- */
static void LeaveBackground(void)
2019-07-23 02:50:34 +00:00
{
ReconnectKeyCodes3();
DisableKeyRepeat();
}
static void EnterBackground(void)
2019-07-23 02:50:34 +00:00
{
RestoreKeyRepeat();
DisconnectKeyCodes3();
ForceShowCursor();
}
void LeaveSpeedStopped(void)
2019-07-23 02:50:34 +00:00
{
#if SoundEnabled
Sound_Start();
2019-07-23 02:50:34 +00:00
#endif
StartUpTimeAdjust();
}
void EnterSpeedStopped(void)
2019-07-23 02:50:34 +00:00
{
#if SoundEnabled
Sound_Stop();
2019-07-23 02:50:34 +00:00
#endif
}
static void CheckForSavedTasks(void)
2019-07-23 02:50:34 +00:00
{
if (EvtQNeedRecover) {
2020-02-11 05:34:32 +00:00
EvtQNeedRecover = false;
2019-07-23 02:50:34 +00:00
/* attempt cleanup, EvtQNeedRecover may get set again */
EvtQTryRecoverFromFull();
2019-07-23 02:50:34 +00:00
}
#if EnableFSMouseMotion && HaveWorkingWarp
if (HaveMouseMotion) {
MouseConstrain();
2019-07-23 02:50:34 +00:00
}
#endif
if (RequestMacOff) {
2020-02-11 05:34:32 +00:00
RequestMacOff = false;
/*if (AnyDiskInserted()) {
2019-07-23 02:50:34 +00:00
MacMsgOverride(kStrQuitWarningTitle,
kStrQuitWarningMessage);
} else {*/
2020-02-11 05:34:32 +00:00
ForceMacOff = true;
//}
2019-07-23 02:50:34 +00:00
}
if (ForceMacOff) {
return;
}
if (gTrueBackgroundFlag != gBackgroundFlag) {
gBackgroundFlag = gTrueBackgroundFlag;
if (gTrueBackgroundFlag) {
EnterBackground();
} else {
LeaveBackground();
}
}
// TODO: fix this
/*if (CurSpeedStopped != (SpeedStopped ||
(gBackgroundFlag && ! RunInBackground))){
2019-07-23 02:50:34 +00:00
} else {
LeaveSpeedStopped();
}*/
2019-07-23 02:50:34 +00:00
#if EnableRecreateW
if (0
#if 1
2019-07-23 02:50:34 +00:00
|| (UseMagnify != WantMagnify)
#endif
#if 1
2019-07-23 02:50:34 +00:00
|| (UseFullScreen != WantFullScreen)
#endif
)
{
(void) ReCreateMainWindow();
}
#endif
#if MayFullScreen
if (GrabMachine != (
#if 1
2019-07-23 02:50:34 +00:00
UseFullScreen &&
#endif
! (gTrueBackgroundFlag || CurSpeedStopped)))
{
GrabMachine = ! GrabMachine;
if (GrabMachine) {
GrabTheMachine();
} else {
UngrabMachine();
}
}
#endif
if (NeedWholeScreenDraw) {
2020-02-11 05:34:32 +00:00
NeedWholeScreenDraw = false;
2019-07-23 02:50:34 +00:00
ScreenChangedAll();
}
#if NeedRequestIthDisk
if (0 != RequestIthDisk) {
Sony_InsertIth(RequestIthDisk);
RequestIthDisk = 0;
}
#endif
if (HaveCursorHidden != (WantCursorHidden
&& ! (gTrueBackgroundFlag || CurSpeedStopped)))
{
HaveCursorHidden = ! HaveCursorHidden;
(void) SDL_ShowCursor(
HaveCursorHidden ? SDL_DISABLE : SDL_ENABLE);
}
}
/* --- command line parsing --- */
2020-07-01 01:33:55 +00:00
// TODO: reimplement with an actual argument parsing library
static bool ScanCommandLine(void)
2019-07-23 02:50:34 +00:00
{
2020-02-11 05:34:32 +00:00
return true;
2019-07-23 02:50:34 +00:00
}
/* --- main program flow --- */
2020-02-11 05:34:32 +00:00
GLOBALOSGLUFUNC bool ExtraTimeNotOver(void)
2019-07-23 02:50:34 +00:00
{
UpdateTrueEmulatedTime();
return TrueEmulatedTime == OnTrueTime;
}
static void WaitForTheNextEvent(void)
2019-07-23 02:50:34 +00:00
{
SDL_Event event;
if (SDL_WaitEvent(&event)) {
HandleTheEvent(&event);
}
}
static void CheckForSystemEvents(void)
2019-07-23 02:50:34 +00:00
{
SDL_Event event;
int i = 10;
while ((--i >= 0) && SDL_PollEvent(&event)) {
HandleTheEvent(&event);
}
}
GLOBALOSGLUPROC WaitForNextTick(void)
{
label_retry:
CheckForSystemEvents();
CheckForSavedTasks();
if (ForceMacOff) {
return;
}
if (CurSpeedStopped) {
DoneWithDrawingForTick();
WaitForTheNextEvent();
goto label_retry;
}
if (ExtraTimeNotOver()) {
(void) SDL_Delay(NextIntTime - LastTime);
goto label_retry;
}
if (CheckDateTime()) {
#if SoundEnabled
Sound_SecondNotify();
2019-07-23 02:50:34 +00:00
#endif
}
if ((! gBackgroundFlag)
#if UseMotionEvents
&& (! CaughtMouse)
#endif
)
{
CheckMouseState();
}
OnTrueTime = TrueEmulatedTime;
#if dbglog_TimeStuff
dbglog_writelnNum("WaitForNextTick, OnTrueTime", OnTrueTime);
#endif
}
/* --- platform independent code can be thought of as going here --- */
#include "PROGMAIN.h"
static void ZapOSGLUVars(void)
2019-07-23 02:50:34 +00:00
{
InitDrives();
ZapWinStateVars();
}
static void ReserveAllocAll(void)
2019-07-23 02:50:34 +00:00
{
#if dbglog_HAVE
dbglog_ReserveAlloc();
#endif
2020-02-11 05:34:32 +00:00
ReserveAllocOneBlock(&ROM, kROM_Size, 5, false);
2019-07-23 02:50:34 +00:00
ReserveAllocOneBlock(&screencomparebuff,
2020-02-11 05:34:32 +00:00
vMacScreenNumBytes, 5, true);
2019-07-23 02:50:34 +00:00
#if UseControlKeys
ReserveAllocOneBlock(&CntrlDisplayBuff,
2020-02-11 05:34:32 +00:00
vMacScreenNumBytes, 5, false);
2019-07-23 02:50:34 +00:00
#endif
2020-02-11 05:34:32 +00:00
ReserveAllocOneBlock(&CLUT_final, CLUT_finalsz, 5, false);
#if SoundEnabled
2020-02-11 05:34:32 +00:00
ReserveAllocOneBlock((uint8_t * *)&TheSoundBuffer,
dbhBufferSize, 5, false);
2019-07-23 02:50:34 +00:00
#endif
EmulationReserveAlloc();
}
static bool AllocMemory(void)
2019-07-23 02:50:34 +00:00
{
uimr n;
2020-02-11 05:34:32 +00:00
bool IsOk = false;
2019-07-23 02:50:34 +00:00
ReserveAllocOffset = 0;
ReserveAllocBigBlock = nullpr;
ReserveAllocAll();
n = ReserveAllocOffset;
2020-02-11 05:34:32 +00:00
ReserveAllocBigBlock = (uint8_t *)calloc(1, n);
2019-07-23 02:50:34 +00:00
if (NULL == ReserveAllocBigBlock) {
2020-02-11 05:34:32 +00:00
MacMsg(kStrOutOfMemTitle, kStrOutOfMemMessage, true);
2019-07-23 02:50:34 +00:00
} else {
ReserveAllocOffset = 0;
ReserveAllocAll();
if (n != ReserveAllocOffset) {
/* oops, program error */
} else {
2020-02-11 05:34:32 +00:00
IsOk = true;
2019-07-23 02:50:34 +00:00
}
}
return IsOk;
}
static void UnallocMemory(void)
2019-07-23 02:50:34 +00:00
{
if (nullpr != ReserveAllocBigBlock) {
free((char *)ReserveAllocBigBlock);
}
}
#if CanGetAppPath
static bool InitWhereAmI(void)
2019-07-23 02:50:34 +00:00
{
app_parent = SDL_GetBasePath();
pref_dir = SDL_GetPrefPath("gryphel", "minivmac");
2020-02-11 05:34:32 +00:00
return true; /* keep going regardless */
2019-07-23 02:50:34 +00:00
}
#endif
#if CanGetAppPath
static void UninitWhereAmI(void)
2019-07-23 02:50:34 +00:00
{
SDL_free(pref_dir);
SDL_free(app_parent);
}
#endif
static bool InitOSGLU(void)
2019-07-23 02:50:34 +00:00
{
if (Config_TryInit())
if (AllocMemory())
2019-07-23 02:50:34 +00:00
#if CanGetAppPath
if (InitWhereAmI())
#endif
#if dbglog_HAVE
if (dbglog_open())
#endif
if (ScanCommandLine())
if (LoadMacRom())
if (LoadInitialImages())
if (InitLocationDat())
#if SoundEnabled
if (Sound_Init())
2019-07-23 02:50:34 +00:00
#endif
if (SDL_InitDisplay())
2019-07-23 02:50:34 +00:00
if (CreateMainWindow())
if (WaitForRom())
{
2020-02-11 05:34:32 +00:00
return true;
2019-07-23 02:50:34 +00:00
}
2020-02-11 05:34:32 +00:00
return false;
2019-07-23 02:50:34 +00:00
}
static void UnInitOSGLU(void)
2019-07-23 02:50:34 +00:00
{
RestoreKeyRepeat();
#if MayFullScreen
UngrabMachine();
#endif
#if SoundEnabled
Sound_Stop();
2019-07-23 02:50:34 +00:00
#endif
#if SoundEnabled
Sound_UnInit();
2019-07-23 02:50:34 +00:00
#endif
#if IncludePbufs
UnInitPbufs();
#endif
UnInitDrives();
ForceShowCursor();
#if dbglog_HAVE
dbglog_close();
#endif
#if CanGetAppPath
UninitWhereAmI();
#endif
UnallocMemory();
2019-07-23 02:50:34 +00:00
CheckSavedMacMsg();
CloseMainWindow();
SDL_Quit();
}
int main(int argc, char **argv)
{
2020-02-11 05:34:32 +00:00
argc = argc;
argv = argv;
2019-07-23 02:50:34 +00:00
ZapOSGLUVars();
if (InitOSGLU()) {
ProgramMain();
}
UnInitOSGLU();
return 0;
}