Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion theseus/desktop/desktop_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern char s_steamPath[512];
extern char s_retroarchPath[512];
extern char s_esdePath[512];
extern bool g_showRetroArchTab, g_showSteamTab, g_showEsdeTab;
extern bool g_retroarchFullscreen;
extern bool g_retroarchFullscreen, g_returnAfterGame;
extern int g_audioBufferFrames;
extern int g_startupMode;
extern bool g_bUseOnScreenKeyboard, g_bShowBootAnimation;
Expand Down Expand Up @@ -116,6 +116,7 @@ static const SettingDef kDefs[] = {
S_PATH("EsdePath", "Desktop", "Games", "ES-DE Folder", s_esdePath, 0),
S_BOOL("ShowEsdeTab", "Desktop", "Games", "ES-DE Import Tab", g_showEsdeTab, "1", 0),
S_BOOL("RetroArchFullscreen", "Desktop", "Games", "RetroArch Fullscreen", g_retroarchFullscreen, "1", 0),
S_BOOL("ReturnAfterGame", "Desktop", "Games", "Back to UIX When a Game Closes", g_returnAfterGame, "1", 0),
{ "AudioBufferFrames", "Desktop", "Audio", "Audio Buffer", SK_INT, &g_audioBufferFrames, 0, "1024",
SF_RESTART | SF_HIDDEN, NOENUM, 0, 0, 256, NULL, NULL },
S_BOOL("ShowRetroArchTab", "Desktop", "Games", "RetroArch Tab", g_showRetroArchTab, "1", 0),
Expand Down
11 changes: 11 additions & 0 deletions theseus/desktop/guide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ static void GameListRows(int list, std::vector<int>& out)
});
}

static std::string PlayTime(long long s)
{
char buf[32];
if (s <= 0) return "None yet";
if (s < 3600) snprintf(buf, sizeof(buf), "%lld min", s < 60 ? 1 : s / 60);
else snprintf(buf, sizeof(buf), "%lld h %02lld min", s / 3600, (s % 3600) / 60);
return buf;
}

static std::string PlayedOn(long long t)
{
if (!t) return "Never";
Expand Down Expand Up @@ -543,6 +552,8 @@ static void Build()
Add("Hidden", vg->hidden ? "Yes" : "No", "Hidden games stay out of the launcher until you bring them back.",
GA_HIDE);
Add("Last Played", PlayedOn(vg->lastPlayed).c_str(), NULL);
Add("Time Played", PlayTime(vg->playTime).c_str(),
"Counted when the game closes. Games another launcher starts for us can't be timed.");
std::string where = vg->group[0] ? std::string(vg->category) + "/" + vg->group : std::string(vg->category);
Add("Category", where.c_str(), NULL);
break;
Expand Down
14 changes: 10 additions & 4 deletions theseus/desktop/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,23 @@ void Host_InstallCrashHandler(const char* userDataDir, const char* shippedDir);
// What bgfx needs to draw into the SDL window. ndt is the display
// connection (X11/Wayland), NULL elsewhere.
bool Host_NativeWindow(SDL_Window* window, void** nwh, void** ndt, bool* wayland);
// Fire and forget: a URL goes to the OS handler, anything else runs as a
// shell command from workdir (may be NULL). err gets a line on failure.
// A URL goes to the OS handler, anything else runs as a shell command from
// workdir (may be NULL). err gets a line on failure. When proc isn't NULL it
// gets the child to watch, or NULL when there's nothing to watch (a URL on
// Windows); hand every one back to Host_ProcFree.
struct HostProc;
bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workdir,
char* err, size_t errSize);
char* err, size_t errSize, HostProc** proc);
// True once the child has exited. Never blocks.
bool Host_ProcDone(HostProc* proc);
void Host_ProcFree(HostProc* proc);
// Native "open file" dialog. False on cancel or when there isn't one.
bool Host_PickFile(const char* title, char* out, size_t outSize);
// Full path of the running executable.
bool Host_ExePath(char* out, size_t outSize);
// Show a folder in the OS file manager. Doesn't wait.
void Host_OpenFolder(const char* dir);
// A string value under HKEY_CURRENT_USER. Always false off Windows.
// A value under HKEY_CURRENT_USER as text (numbers in decimal). Always false off Windows.
bool Host_RegistryString(const char* subKey, const char* value, char* out, size_t outSize);
// Audio CD in the first optical drive: track count and per-track lengths
// in seconds. -1 when there's no readable disc.
Expand Down
24 changes: 22 additions & 2 deletions theseus/desktop/host_posix_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
Expand Down Expand Up @@ -493,8 +494,11 @@ static void ExecLaunch(const char* spec, bool isUrl, bool isSteamUrl) {
}
}

struct HostProc { pid_t pid; bool done; };

bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workdir,
char* err, size_t errSize) {
char* err, size_t errSize, HostProc** proc) {
if (proc) *proc = NULL;
pid_t pid = fork();
if (pid == 0) {
if (workdir) (void)chdir(workdir);
Expand All @@ -506,10 +510,26 @@ bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workd
snprintf(err, errSize, "fork() failed: %s", strerror(errno));
return false;
}
// Parent: don't wait. Fire-and-forget.
if (proc) {
*proc = new HostProc;
(*proc)->pid = pid;
(*proc)->done = false;
}
return true;
}

bool Host_ProcDone(HostProc* proc) {
if (!proc || proc->done) return true;
int status;
pid_t r = waitpid(proc->pid, &status, WNOHANG);
if (r == proc->pid || (r < 0 && errno == ECHILD)) proc->done = true;
return proc->done;
}

void Host_ProcFree(HostProc* proc) {
delete proc;
}

bool Host_PickFile(const char* title, char* out, size_t outSize) {
char cmd[512];
#if defined(__APPLE__)
Expand Down
32 changes: 28 additions & 4 deletions theseus/desktop/host_win_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,12 @@ bool Host_NativeWindow(SDL_Window* window, void** nwh, void** ndt, bool* wayland
// URLs go through ShellExecute. Raw commands run through cmd /C; no /S, so
// cmd preserves outer quotes for the 2-quote/exists case and strips
// first/last otherwise.
struct HostProc { HANDLE h; };

bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workdir,
char* err, size_t errSize) {
char* err, size_t errSize, HostProc** proc) {
(void)isSteamUrl;
if (proc) *proc = NULL;
if (isUrl) {
HINSTANCE rc = ShellExecuteA(NULL, "open", spec, NULL, workdir, SW_SHOWNORMAL);
if ((INT_PTR)rc <= 32) {
Expand Down Expand Up @@ -452,11 +455,26 @@ bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workd
snprintf(err, errSize, "CreateProcess failed (error %lu): %s", e, spec);
return false;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (proc) {
*proc = new HostProc;
(*proc)->h = pi.hProcess;
} else {
CloseHandle(pi.hProcess);
}
return true;
}

bool Host_ProcDone(HostProc* proc) {
return !proc || WaitForSingleObject(proc->h, 0) != WAIT_TIMEOUT;
}

void Host_ProcFree(HostProc* proc) {
if (!proc) return;
CloseHandle(proc->h);
delete proc;
}

bool Host_PickFile(const char* title, char* out, size_t outSize) {
OPENFILENAMEA ofn = {};
char path[MAX_PATH] = {};
Expand Down Expand Up @@ -484,9 +502,15 @@ bool Host_RegistryString(const char* subKey, const char* value, char* out, size_
if (RegOpenKeyExA(HKEY_CURRENT_USER, subKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS) return false;
DWORD size = (DWORD)outSize;
DWORD type = 0;
bool ok = RegQueryValueExA(hKey, value, NULL, &type, (BYTE*)out, &size) == ERROR_SUCCESS &&
(type == REG_SZ || type == REG_EXPAND_SZ);
bool ok = RegQueryValueExA(hKey, value, NULL, &type, (BYTE*)out, &size) == ERROR_SUCCESS;
RegCloseKey(hKey);
if (ok && type == REG_DWORD && size == sizeof(DWORD)) {
DWORD n;
memcpy(&n, out, sizeof(n));
snprintf(out, outSize, "%lu", (unsigned long)n);
return true;
}
ok = ok && (type == REG_SZ || type == REG_EXPAND_SZ);
if (ok) out[outSize - 1] = '\0';
return ok;
}
Expand Down
156 changes: 150 additions & 6 deletions theseus/desktop/launch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "stb_image.h"
#include "path_template.h"
#include "launchers/launcher.h"
#include "launchers/steam.h"

#include <vector>

extern "C" void DashAudio_MuteAll(void);
extern SDL_Window* g_pSDLWindow;
Expand Down Expand Up @@ -133,7 +136,7 @@ static bool DeriveWorkDir(const char* cmd, char* out, size_t outSize)
// e.g. Title Maker Test Launch) and SpawnLaunchSpec (overlay-driven game
// launch) call this. Returns true if the OS handed us back a process /
// shell-exec handle; the parent doesn't wait on the child either way.
static bool Launch_DoSpawn(const char* expanded)
static bool Launch_DoSpawn(const char* expanded, HostProc** proc)
{
if (!expanded || !*expanded) return false;

Expand All @@ -147,7 +150,7 @@ static bool Launch_DoSpawn(const char* expanded)
bool haveWorkdir = DeriveWorkDir(spec, workdir, sizeof(workdir));

if (!Host_Spawn(spec, IsUrl(spec), IsSteamUrl(spec), haveWorkdir ? workdir : NULL,
g_launchLastResult, sizeof(g_launchLastResult))) {
g_launchLastResult, sizeof(g_launchLastResult), proc)) {
fprintf(stderr, "[launch] %s\n", g_launchLastResult);
return false;
}
Expand All @@ -162,7 +165,7 @@ static bool Launch_DoSpawn(const char* expanded)
// through this so the trace, error reporting, and command-form rules
// live in one place.
static bool Launch_ExpandAndSpawn(const char* spec,
char* finalOut, size_t finalSize)
char* finalOut, size_t finalSize, HostProc** proc)
{
if (!spec || !spec[0]) return false;
char expanded[2048];
Expand All @@ -173,13 +176,151 @@ static bool Launch_ExpandAndSpawn(const char* spec,
fprintf(stderr, "[launch] var: %s\n", expanded);
if (strcmp(expanded, finalOut) != 0)
fprintf(stderr, "[launch] cmd: %s\n", finalOut);
return Launch_DoSpawn(finalOut);
return Launch_DoSpawn(finalOut, proc);
}

// ============================================================================
// The game that's running: its play time, and the trip back to the dashboard
// when it closes.
// ============================================================================

bool g_returnAfterGame = true; // desktop.ini ReturnAfterGame

// A launcher that starts the real game and quits this fast was only a
// doorway: the game is still up, so there's nothing to time or come back to.
static const double kHandOff = 20.0;
// How long a steam:// launch gets to show up as running. Updates, shader
// caches and a cold Steam client all count against it.
static const double kSteamStartWait = 300.0;

enum { SESSION_NONE, SESSION_PROC, SESSION_STEAM_WAIT, SESSION_STEAM };
static struct {
int kind;
HostProc* proc;
int appid;
double start;
char launch[1024]; // the games.ini Launch line, for the play time
} s_session;

// Children nobody watches, reaped as they finish.
static std::vector<HostProc*> s_strays;

static double SessionNow()
{
return (double)SDL_GetPerformanceCounter() / (double)SDL_GetPerformanceFrequency();
}

static void Stray(HostProc* proc)
{
if (proc) s_strays.push_back(proc);
}

// The app id in steam://rungameid/220 or steam://run/220. Shortcuts Steam
// made for non-Steam games have 64-bit ids it never reports, so no.
static int SteamAppId(const char* url)
{
const char* p = strrchr(url, '/');
if (!p || !p[1]) return 0;
long long id = 0;
for (p++; *p >= '0' && *p <= '9'; p++) {
id = id * 10 + (*p - '0');
if (id > 0x7fffffff) return 0;
}
return *p == 0 ? (int)id : 0;
}

static void SessionDrop()
{
Stray(s_session.proc);
s_session.proc = NULL;
s_session.kind = SESSION_NONE;
}

static void SessionBegin(const char* launch, const char* final, HostProc* proc)
{
SessionDrop();
strncpy(s_session.launch, launch, sizeof(s_session.launch) - 1);
s_session.launch[sizeof(s_session.launch) - 1] = 0;
s_session.start = SessionNow();
if (IsSteamUrl(final)) {
// What we spawned only hands the URL to Steam, or is Steam itself.
Stray(proc);
s_session.appid = SteamAppId(final);
s_session.kind = s_session.appid ? SESSION_STEAM_WAIT : SESSION_NONE;
} else if (proc) {
s_session.proc = proc;
s_session.kind = SESSION_PROC;
}
if (s_session.kind == SESSION_STEAM_WAIT)
fprintf(stderr, "[launch] watching Steam app %d\n", s_session.appid);
else if (s_session.kind == SESSION_PROC)
fprintf(stderr, "[launch] watching the game\n");
}

static void SessionEnd()
{
double played = SessionNow() - s_session.start;
SessionDrop();
if (played < kHandOff) {
fprintf(stderr, "[launch] closed after %.0fs, taken as a hand-off\n", played);
return;
}
fprintf(stderr, "[launch] game closed after %.0fs\n", played);
VGames_AddPlayTime(s_session.launch, (long long)played);
if (g_returnAfterGame && g_pSDLWindow) {
// Windows won't let a background app come forward without this.
SDL_SetHint(SDL_HINT_FORCE_RAISEWINDOW, "1");
SDL_RestoreWindow(g_pSDLWindow);
SDL_RaiseWindow(g_pSDLWindow);
}
}

static void SessionTick()
{
static double s_next = 0;
double now = SessionNow();
if (now < s_next) return;
s_next = now + 1.0;

for (size_t i = 0; i < s_strays.size();) {
if (Host_ProcDone(s_strays[i])) {
Host_ProcFree(s_strays[i]);
s_strays.erase(s_strays.begin() + i);
} else {
i++;
}
}

switch (s_session.kind) {
case SESSION_PROC:
if (Host_ProcDone(s_session.proc)) SessionEnd();
break;
case SESSION_STEAM_WAIT:
if (Steam_RunningAppId() == s_session.appid) {
s_session.kind = SESSION_STEAM;
s_session.start = now;
} else if (now - s_session.start > kSteamStartWait) {
fprintf(stderr, "[launch] Steam app %d never showed up as running\n", s_session.appid);
SessionDrop();
}
break;
case SESSION_STEAM:
if (Steam_RunningAppId() != s_session.appid) SessionEnd();
break;
}
}

bool Launch_GameRunning()
{
return s_session.kind != SESSION_NONE;
}

void DesktopLaunch(const char* spec)
{
char finalCmd[2048];
Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd));
HostProc* proc = NULL;
Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd), &proc);
Stray(proc);
}

// Spawn called by the launch overlay tick once the fade-in completes.
Expand All @@ -188,8 +329,10 @@ void DesktopLaunch(const char* spec)
static void SpawnLaunchSpec(const char* spec)
{
char finalCmd[2048];
if (!Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd))) return;
HostProc* proc = NULL;
if (!Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd), &proc)) return;
VGames_MarkPlayed(spec);
SessionBegin(spec, finalCmd, proc);

extern SDL_Window* g_pSDLWindow;
if (g_pSDLWindow) SDL_MinimizeWindow(g_pSDLWindow);
Expand Down Expand Up @@ -281,6 +424,7 @@ unsigned long long LaunchOverlay_LogoGLTex(int* outW, int* outH)

void LaunchOverlay_Tick()
{
SessionTick();
if (!s_overlayActive) return;

double t = LaunchOverlay_Now() - s_overlayStart;
Expand Down
Loading
Loading