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
1 change: 1 addition & 0 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ DESKTOP_SRCS_PLATFORM = \
$(THESEUS_SRC)/desktop/settings_schema.cpp \
$(THESEUS_SRC)/desktop/desktop_settings.cpp \
$(THESEUS_SRC)/desktop/guide.cpp \
$(THESEUS_SRC)/desktop/console_mode.cpp \
$(THESEUS_SRC)/desktop/ui_overlay.cpp \
$(THESEUS_SRC)/desktop/ui_overlay_style.cpp \
$(THESEUS_SRC)/desktop/ui_font.cpp \
Expand Down
129 changes: 129 additions & 0 deletions theseus/desktop/console_mode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// console_mode.cpp: console mode's state and the guide's leave and power
// options (console_mode.h).

#include "console_mode.h"
#include "host.h"
#include "app_paths.h"

#include <SDL.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <string>
#include <vector>

extern SDL_Window* g_pSDLWindow;
extern bool g_displayChangeRequested;
void SaveDesktopSettings();

bool g_consoleMode = false;
bool g_consoleFlag = false;
bool g_consoleSupervised = false;

bool Console_Active() { return g_consoleMode || g_consoleFlag; }

void Console_GoToDesktop()
{
if (!g_pSDLWindow) return;
// A fullscreen window won't minimize everywhere, so drop it first. The
// restore puts it back.
SDL_SetWindowFullscreen(g_pSDLWindow, 0);
SDL_MinimizeWindow(g_pSDLWindow);
}

bool Console_Power(int action)
{
SaveDesktopSettings();
fflush(stdout);
fflush(stderr);
return Host_Power(action);
}

// What a child's exit code asks the watchdog for. Anything else nonzero is a crash.
enum { RELAUNCH_SAME = 75, RELAUNCH_DASHBOARD, RELAUNCH_DEVELOPMENT };

// The watchdog's own lines go to the terminal and the end of theseus.log.
// The child is gone by then, and its next run keeps this log as theseus.prev.log.
static void WatchdogLog(const char* fmt, ...)
{
char line[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(line, sizeof(line), fmt, ap);
va_end(ap);
fprintf(stderr, "[Console] %s\n", line);
char path[1024];
snprintf(path, sizeof(path), "%s/theseus.log", Plat_UserDataDir());
if (FILE* f = fopen(path, "a")) {
fprintf(f, "[Console] %s\n", line);
fclose(f);
}
}

static bool IsModeFlag(const char* a)
{
return !strcmp(a, "--dashboard") || !strcmp(a, "--development") || !strcmp(a, "--preview");
}

bool Console_Supervise(int argc, char** argv, int* code)
{
bool console = false;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--supervised")) g_consoleSupervised = true;
if (!strcmp(argv[i], "--console")) console = true;
}
if (!console || g_consoleSupervised) return false;

std::vector<std::string> args(argv, argv + argc);
args.push_back("--supervised");
time_t lastCrash = 0;
for (;;) {
std::vector<const char*> list;
for (size_t i = 0; i < args.size(); i++) list.push_back(args[i].c_str());
list.push_back(NULL);

int exitCode = 0;
char how[128];
int outcome = Host_RunSelf(list.data(), &exitCode, how, sizeof(how));
if (outcome == HOST_CHILD_FAILED) {
WatchdogLog("couldn't start UIX Desktop");
*code = 1;
return true;
}
if (outcome == HOST_CHILD_EXITED && exitCode == 0) {
*code = 0;
return true;
}
if (outcome == HOST_CHILD_EXITED && exitCode >= RELAUNCH_SAME && exitCode <= RELAUNCH_DEVELOPMENT) {
if (exitCode != RELAUNCH_SAME) {
std::vector<std::string> kept;
for (size_t i = 0; i < args.size(); i++)
if (!IsModeFlag(args[i].c_str())) kept.push_back(args[i]);
kept.push_back(exitCode == RELAUNCH_DASHBOARD ? "--dashboard" : "--development");
args.swap(kept);
}
WatchdogLog("restarting UIX Desktop");
continue;
}
time_t now = time(NULL);
if (lastCrash && now - lastCrash < 60) {
WatchdogLog("UIX Desktop crashed again within a minute (%s). Not starting it again.", how);
*code = 1;
return true;
}
lastCrash = now;
WatchdogLog("UIX Desktop crashed (%s). Starting it again.", how);
}
}

void Console_RestartViaWatchdog(const char* mode)
{
if (!g_consoleSupervised || Host_ExecKeepsProcess()) return;
int code = !mode ? RELAUNCH_SAME : !strcmp(mode, "--development") ? RELAUNCH_DEVELOPMENT : RELAUNCH_DASHBOARD;
fflush(stdout);
fflush(stderr);
// Like exec: nothing else runs on the way out.
_Exit(code);
}
26 changes: 26 additions & 0 deletions theseus/desktop/console_mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// console_mode.h: UIX Desktop standing in for the machine's own front end.
// Fullscreen, no frame, straight into the dashboard. The OS shell stays
// where it was underneath.
#pragma once

extern bool g_consoleMode; // desktop.ini ConsoleMode
extern bool g_consoleFlag; // --console, this run only
extern bool g_consoleSupervised; // --supervised: a watchdog started us

// Either of the two.
bool Console_Active();

// Minimizes the window. It comes back fullscreen when it's restored.
void Console_GoToDesktop();
// Saves settings and flushes the log, then asks the host (host.h HOST_POWER_).
bool Console_Power(int action);

// --console starts a small watchdog first: it runs the app as its child
// (adding --supervised) and starts it again after a crash, unless it
// crashed twice within a minute. True when this process was the watchdog
// and main should return code.
bool Console_Supervise(int argc, char** argv, int* code);
// Under the watchdog where exec can't keep the process, a restart is the
// watchdog's job: this exits asking for one. mode is the new mode flag, or
// NULL for the same. Returns when it isn't the case.
void Console_RestartViaWatchdog(const char* mode);
21 changes: 21 additions & 0 deletions theseus/desktop/desktop_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "media_player.h"
#include "virtual_games.h"
#include "pads.h"
#include "console_mode.h"
#include "host.h"

extern char s_xemuPath[512];
extern char s_pcsx2Path[512];
Expand Down Expand Up @@ -52,6 +54,15 @@ static void ApplyStartupMode() { g_startupMode = s_startupModeSaved; }
bool g_hideDuplicateGames = true;
static void ApplyDuplicates() { g_vgames.generation++; }

// What the OS says, not desktop.ini: the login entry is the setting.
static bool s_startAtLogin = false;
static void ApplyStartAtLogin()
{
if (!Host_SetStartAtLogin(s_startAtLogin))
fprintf(stderr, "[Console] couldn't %s the login entry\n", s_startAtLogin ? "add" : "remove");
s_startAtLogin = Host_StartAtLogin();
}

static void ApplyMsaa() { g_msaaChangeRequested = true; }
static void ApplyVsync() { g_vsyncChangeRequested = true; }
static void ApplyDisplay() { g_displayChangeRequested = true; }
Expand Down Expand Up @@ -133,6 +144,10 @@ static const SettingDef kDefs[] = {
S_BOOL("ShowSteamTab", "Desktop", "Games", "Steam Tab", g_showSteamTab, "1", 0),
{ "StartupMode", "Desktop", "General", "When UIX Desktop Starts", SK_ENUM, &s_startupModeSaved, 0, "", 0,
ENUM(kStartupVals, kStartupLbl, kStartupTok), 0, 0, 0, NULL, ApplyStartupMode },
{ "ConsoleMode", "Desktop", "General", "Console Mode", SK_BOOL, &g_consoleMode, 0, "0", 0,
NOENUM, 0, 0, 0, NULL, ApplyDisplay },
{ "StartAtLogin", "Desktop", "General", "Start at Login", SK_BOOL, &s_startAtLogin, 0, "0", SF_LIVE,
NOENUM, 0, 0, 0, NULL, ApplyStartAtLogin },
S_BOOL("UseOnScreenKeyboard", "Desktop", "General", "On-Screen Keyboard Only", g_bUseOnScreenKeyboard, "0", 0),
S_BOOL("ShowBootAnimation", "Desktop", "General", "Boot Animation", g_bShowBootAnimation, "1", 0),
{ "MSAA", "Desktop", "Display", "MSAA", SK_ENUM, &g_msaaSamples, 0, "4", SF_STRICT,
Expand Down Expand Up @@ -214,9 +229,15 @@ const SettingDef* DesktopSettings_Find(const char* key)
return NULL;
}

void DesktopSettings_ReadLive()
{
s_startAtLogin = Host_StartAtLogin();
}

void LoadDesktopSettings()
{
Settings_Load(DesktopSettings_Table(), AppPath("Configs/desktop.ini"));
DesktopSettings_ReadLive();
ApplyStartupMode();
TheseusSetProjectionDirty();
}
Expand Down
Loading
Loading