mirror of
https://github.com/ZDoom/raze-gles.git
synced 2024-11-11 23:31:56 +00:00
405 lines
8.9 KiB
C++
405 lines
8.9 KiB
C++
/*
|
|
** i_system.cpp
|
|
** Main startup code
|
|
**
|
|
**---------------------------------------------------------------------------
|
|
** Copyright 1999-2016 Randy Heit
|
|
** Copyright 2019-2020 Christoph Oelckers
|
|
** All rights reserved.
|
|
**
|
|
** Redistribution and use in source and binary forms, with or without
|
|
** modification, are permitted provided that the following conditions
|
|
** are met:
|
|
**
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
** notice, this list of conditions and the following disclaimer.
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
** documentation and/or other materials provided with the distribution.
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
** derived from this software without specific prior written permission.
|
|
**
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
**---------------------------------------------------------------------------
|
|
**
|
|
*/
|
|
|
|
#include "i_system.h"
|
|
|
|
#include <dirent.h>
|
|
#include <sys/wait.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <fnmatch.h>
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
|
#include <fcntl.h>
|
|
#include <time.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include "version.h"
|
|
#include "cmdlib.h"
|
|
#include "m_argv.h"
|
|
#include "i_sound.h"
|
|
|
|
#include "c_dispatch.h"
|
|
#include "gamecontrol.h"
|
|
#include "gameconfigfile.h"
|
|
|
|
extern "C"
|
|
{
|
|
double SecondsPerCycle = 1e-8;
|
|
double CyclesPerSecond = 1e8;
|
|
}
|
|
|
|
#ifndef NO_GTK
|
|
bool I_GtkAvailable ();
|
|
int I_PickIWad_Gtk (WadStuff *wads, int numwads, bool showwin, int defaultiwad);
|
|
void I_ShowFatalError_Gtk(const char* errortext);
|
|
#elif defined(__APPLE__)
|
|
int I_PickIWad_Cocoa (WadStuff *wads, int numwads, bool showwin, int defaultiwad);
|
|
#endif
|
|
|
|
double PerfToSec, PerfToMillisec;
|
|
|
|
void I_BeginRead(void)
|
|
{
|
|
}
|
|
|
|
void I_EndRead(void)
|
|
{
|
|
}
|
|
|
|
|
|
//
|
|
// I_Init
|
|
//
|
|
void I_Init (void)
|
|
{
|
|
#if 0 // do we need this?
|
|
CheckCPUID (&CPU);
|
|
DumpCPUInfo (&CPU);
|
|
#endif
|
|
}
|
|
|
|
//
|
|
// I_Error
|
|
//
|
|
extern FILE *Logfile;
|
|
|
|
#ifdef __APPLE__
|
|
void Mac_I_FatalError(const char* errortext);
|
|
#endif
|
|
|
|
#ifdef __linux__
|
|
void Linux_I_FatalError(const char* errortext)
|
|
{
|
|
// Close window or exit fullscreen and release mouse capture
|
|
SDL_Quit();
|
|
|
|
const char *str;
|
|
if((str=getenv("KDE_FULL_SESSION")) && strcmp(str, "true") == 0)
|
|
{
|
|
FString cmd;
|
|
cmd << "kdialog --title \"" GAMENAME " " << GetVersionString()
|
|
<< "\" --msgbox \"" << errortext << "\"";
|
|
popen(cmd, "r");
|
|
}
|
|
#ifndef NO_GTK
|
|
else if (I_GtkAvailable())
|
|
{
|
|
I_ShowFatalError_Gtk(errortext);
|
|
}
|
|
#endif
|
|
else
|
|
{
|
|
FString title;
|
|
title << GAMENAME " " << GetVersionString();
|
|
|
|
if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, errortext, NULL) < 0)
|
|
{
|
|
printf("\n%s\n", errortext);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
void I_ShowFatalError(const char *message)
|
|
{
|
|
#ifdef __APPLE__
|
|
Mac_I_FatalError(message);
|
|
#elif defined __linux__
|
|
Linux_I_FatalError(message);
|
|
#else
|
|
// ???
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
void I_SetIWADInfo ()
|
|
{
|
|
}
|
|
|
|
void I_DebugPrint(const char *cp)
|
|
{
|
|
}
|
|
|
|
void I_PrintStr(const char *cp)
|
|
{
|
|
// Strip out any color escape sequences before writing to debug output
|
|
TArray<char> copy(strlen(cp) + 1, true);
|
|
const char * srcp = cp;
|
|
char * dstp = copy.Data();
|
|
|
|
while (*srcp != 0)
|
|
{
|
|
if (*srcp != 0x1c && *srcp != 0x1d && *srcp != 0x1e && *srcp != 0x1f)
|
|
{
|
|
*dstp++ = *srcp++;
|
|
}
|
|
else
|
|
{
|
|
if (srcp[1] != 0) srcp += 2;
|
|
else break;
|
|
}
|
|
}
|
|
*dstp = 0;
|
|
|
|
fputs(copy.Data(), stdout);
|
|
fflush(stdout);
|
|
}
|
|
|
|
int I_PickIWad (WadStuff *wads, int numwads, bool showwin, int defaultiwad)
|
|
{
|
|
int i;
|
|
|
|
if (!showwin)
|
|
{
|
|
return defaultiwad;
|
|
}
|
|
|
|
#ifndef __APPLE__
|
|
const char *str;
|
|
if((str=getenv("KDE_FULL_SESSION")) && strcmp(str, "true") == 0)
|
|
{
|
|
FString cmd("kdialog --title \"" GAMENAME " ");
|
|
cmd << GetVersionString() << ": Select an IWAD to use\""
|
|
" --menu \"" GAMENAME " found more than one IWAD\n"
|
|
"Select from the list below to determine which one to use:\"";
|
|
|
|
for(i = 0; i < numwads; ++i)
|
|
{
|
|
const char *filepart = strrchr(wads[i].Path, '/');
|
|
if(filepart == NULL)
|
|
filepart = wads[i].Path;
|
|
else
|
|
filepart++;
|
|
// Menu entries are specified in "tag" "item" pairs, where when a
|
|
// particular item is selected (and the Okay button clicked), its
|
|
// corresponding tag is printed to stdout for identification.
|
|
cmd.AppendFormat(" \"%d\" \"%s (%s)\"", i, wads[i].Name.GetChars(), filepart);
|
|
}
|
|
|
|
if(defaultiwad >= 0 && defaultiwad < numwads)
|
|
{
|
|
const char *filepart = strrchr(wads[defaultiwad].Path, '/');
|
|
if(filepart == NULL)
|
|
filepart = wads[defaultiwad].Path;
|
|
else
|
|
filepart++;
|
|
cmd.AppendFormat(" --default \"%s (%s)\"", wads[defaultiwad].Name.GetChars(), filepart);
|
|
}
|
|
|
|
FILE *f = popen(cmd, "r");
|
|
if(f != NULL)
|
|
{
|
|
char gotstr[16];
|
|
|
|
if(fgets(gotstr, sizeof(gotstr), f) == NULL ||
|
|
sscanf(gotstr, "%d", &i) != 1)
|
|
i = -1;
|
|
|
|
// Exit status = 1 means the selection was canceled (either by
|
|
// Cancel/Esc or the X button), not that there was an error running
|
|
// the program. In that case, nothing was printed so fgets will
|
|
// have failed. Other values can indicate an error running the app,
|
|
// so fall back to whatever else can be used.
|
|
int status = pclose(f);
|
|
if(WIFEXITED(status) && (WEXITSTATUS(status) == 0 || WEXITSTATUS(status) == 1))
|
|
return i;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifndef NO_GTK
|
|
if (I_GtkAvailable())
|
|
{
|
|
return I_PickIWad_Gtk (wads, numwads, showwin, defaultiwad);
|
|
}
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
return I_PickIWad_Cocoa (wads, numwads, showwin, defaultiwad);
|
|
#endif
|
|
|
|
if (!isatty(fileno(stdin)))
|
|
{
|
|
return defaultiwad;
|
|
}
|
|
|
|
printf ("Please select a game wad (or 0 to exit):\n");
|
|
for (i = 0; i < numwads; ++i)
|
|
{
|
|
const char *filepart = strrchr (wads[i].Path, '/');
|
|
if (filepart == NULL)
|
|
filepart = wads[i].Path;
|
|
else
|
|
filepart++;
|
|
printf ("%d. %s (%s)\n", i+1, wads[i].Name.GetChars(), filepart);
|
|
}
|
|
printf ("Which one? ");
|
|
if (scanf ("%d", &i) != 1 || i > numwads)
|
|
return -1;
|
|
return i-1;
|
|
}
|
|
|
|
bool I_WriteIniFailed ()
|
|
{
|
|
printf ("The config file %s could not be saved:\n%s\n", GameConfig->GetPathName(), strerror(errno));
|
|
return false;
|
|
// return true to retry
|
|
}
|
|
|
|
static const char *pattern;
|
|
|
|
#if defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED < 1080
|
|
static int matchfile (struct dirent *ent)
|
|
#else
|
|
static int matchfile (const struct dirent *ent)
|
|
#endif
|
|
{
|
|
return fnmatch (pattern, ent->d_name, FNM_NOESCAPE) == 0;
|
|
}
|
|
|
|
void *I_FindFirst (const char *filespec, findstate_t *fileinfo)
|
|
{
|
|
FString dir;
|
|
|
|
const char *slash = strrchr (filespec, '/');
|
|
if (slash)
|
|
{
|
|
pattern = slash+1;
|
|
dir = FString(filespec, slash-filespec+1);
|
|
fileinfo->path = dir;
|
|
}
|
|
else
|
|
{
|
|
pattern = filespec;
|
|
dir = ".";
|
|
}
|
|
|
|
fileinfo->current = 0;
|
|
fileinfo->count = scandir (dir.GetChars(), &fileinfo->namelist,
|
|
matchfile, alphasort);
|
|
if (fileinfo->count > 0)
|
|
{
|
|
return fileinfo;
|
|
}
|
|
return (void*)-1;
|
|
}
|
|
|
|
int I_FindNext (void *handle, findstate_t *fileinfo)
|
|
{
|
|
findstate_t *state = (findstate_t *)handle;
|
|
if (state->current < fileinfo->count)
|
|
{
|
|
return ++state->current < fileinfo->count ? 0 : -1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int I_FindClose (void *handle)
|
|
{
|
|
findstate_t *state = (findstate_t *)handle;
|
|
if (handle != (void*)-1 && state->count > 0)
|
|
{
|
|
for(int i = 0;i < state->count;++i)
|
|
free (state->namelist[i]);
|
|
state->count = 0;
|
|
free (state->namelist);
|
|
state->namelist = NULL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int I_FindAttr(findstate_t* const fileinfo)
|
|
{
|
|
dirent* const ent = fileinfo->namelist[fileinfo->current];
|
|
bool isdir;
|
|
|
|
if (DirEntryExists(fileinfo->path + ent->d_name, &isdir))
|
|
{
|
|
return isdir ? FA_DIREC : 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void I_PutInClipboard (const char *str)
|
|
{
|
|
SDL_SetClipboardText(str);
|
|
}
|
|
|
|
FString I_GetFromClipboard (bool use_primary_selection)
|
|
{
|
|
if(char *ret = SDL_GetClipboardText())
|
|
{
|
|
FString text(ret);
|
|
SDL_free(ret);
|
|
return text;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
// Return a random seed, preferably one with lots of entropy.
|
|
unsigned int I_MakeRNGSeed()
|
|
{
|
|
unsigned int seed;
|
|
int file;
|
|
|
|
// Try reading from /dev/urandom first, then /dev/random, then
|
|
// if all else fails, use a crappy seed from time().
|
|
seed = time(NULL);
|
|
file = open("/dev/urandom", O_RDONLY);
|
|
if (file < 0)
|
|
{
|
|
file = open("/dev/random", O_RDONLY);
|
|
}
|
|
if (file >= 0)
|
|
{
|
|
read(file, &seed, sizeof(seed));
|
|
close(file);
|
|
}
|
|
return seed;
|
|
}
|
|
|
|
TArray<FString> I_GetGogPaths()
|
|
{
|
|
// GOG's Doom games are Windows only at the moment
|
|
return TArray<FString>();
|
|
}
|
|
|