mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-24 21:55:57 +00:00
Merge pull request #211: Show Xbox button icons in the prompts, with a Button prompts setting that follows the pad layout (waxyfeet)
# Conflicts: # docs/installer.md # runtime/cfg.c # runtime/ui/menu.cpp # runtime/ui_text.h
This commit is contained in:
commit
4524fed813
31 files changed
+1485
-274
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include <linux/fs.h>
|
||||
#include <linux/futex.h>
|
||||
#include <linux/userfaultfd.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <signal.h>
|
||||
@@ -1650,3 +1651,85 @@ long plat_child_read(PlatChild *c, void *buf, size_t cap) {
|
||||
while (k < 0 && errno == EINTR);
|
||||
return (long)k;
|
||||
}
|
||||
|
||||
// ---- restart
|
||||
// TOTK_RESTART_WAIT names the process that called plat_restart, as its id and start time (field 22 of /proc/<pid>/stat,
|
||||
// clock ticks after boot): a process that has only reused the id is not waited for. Waiting keeps the new process off
|
||||
// the old one's files (the log it moves at start, the saves) until the old one is gone. The new process gets no
|
||||
// descriptor past stderr (glibc 2.34's closefrom file action), and the default signal mask and dispositions, whatever
|
||||
// the calling thread had.
|
||||
#define RESTART_WAIT "TOTK_RESTART_WAIT"
|
||||
|
||||
// The start time of process pid (field 22 of its /proc stat line), 0 when it is gone.
|
||||
static unsigned long long process_start(int pid) {
|
||||
char path[64], buf[1024];
|
||||
snprintf(path, sizeof path, "/proc/%d/stat", pid);
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return 0;
|
||||
size_t n = fread(buf, 1, sizeof buf - 1, f);
|
||||
fclose(f);
|
||||
buf[n] = 0;
|
||||
char *p = strrchr(buf, ')'); // the command name may hold spaces
|
||||
unsigned long long start = 0;
|
||||
if (!p || sscanf(p + 2, "%*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %llu",
|
||||
&start) != 1)
|
||||
return 0;
|
||||
return start;
|
||||
}
|
||||
|
||||
int plat_restart(void) {
|
||||
// the arguments, NUL-separated, from /proc/self/cmdline
|
||||
static char args[16384];
|
||||
char *argv[256];
|
||||
int argc = 0;
|
||||
FILE *f = fopen("/proc/self/cmdline", "rb");
|
||||
size_t n = f ? fread(args, 1, sizeof args - 1, f) : 0;
|
||||
if (f) fclose(f);
|
||||
args[n] = 0;
|
||||
for (size_t i = 0; i < n && argc < 255; i += strlen(args + i) + 1) argv[argc++] = args + i;
|
||||
argv[argc] = NULL;
|
||||
char exe[PATH_MAX], wait[64];
|
||||
if (!argc || !plat_exe_path(exe, sizeof exe)) return 0;
|
||||
snprintf(wait, sizeof wait, "%d.%llu", (int)getpid(), process_start((int)getpid()));
|
||||
posix_spawn_file_actions_t fa;
|
||||
posix_spawn_file_actions_init(&fa);
|
||||
posix_spawn_file_actions_addclosefrom_np(&fa, 3);
|
||||
posix_spawnattr_t at;
|
||||
posix_spawnattr_init(&at);
|
||||
sigset_t none, all;
|
||||
sigemptyset(&none);
|
||||
sigfillset(&all);
|
||||
posix_spawnattr_setsigmask(&at, &none);
|
||||
posix_spawnattr_setsigdefault(&at, &all);
|
||||
posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF);
|
||||
setenv(RESTART_WAIT, wait, 1);
|
||||
pid_t pid;
|
||||
int e = posix_spawn(&pid, exe, &fa, &at, argv, environ);
|
||||
unsetenv(RESTART_WAIT);
|
||||
posix_spawnattr_destroy(&at);
|
||||
posix_spawn_file_actions_destroy(&fa);
|
||||
return e == 0;
|
||||
}
|
||||
|
||||
int plat_restart_wait(uint32_t timeout_ms) {
|
||||
const char *v = getenv(RESTART_WAIT);
|
||||
if (!v) return 0;
|
||||
int pid = 0;
|
||||
unsigned long long start = 0;
|
||||
int ok = sscanf(v, "%d.%llu", &pid, &start) == 2 && pid > 0 && start;
|
||||
unsetenv(RESTART_WAIT); // the processes this one starts do not wait again
|
||||
if (!ok) return 0;
|
||||
// The descriptor is taken first: a start time that still matches then proves it is that process, not a new one with
|
||||
// the id. It reads as ready once the process has exited, reaped or not.
|
||||
int fd = (int)syscall(SYS_pidfd_open, pid, 0);
|
||||
if (fd < 0) return 0; // already gone
|
||||
int r = 0;
|
||||
if (process_start(pid) == start) {
|
||||
struct pollfd p = {fd, POLLIN, 0};
|
||||
do r = poll(&p, 1, (int)timeout_ms);
|
||||
while (r < 0 && errno == EINTR);
|
||||
r = r > 0 ? 1 : -1;
|
||||
}
|
||||
close(fd);
|
||||
return r;
|
||||
}
|
||||
Reference in new issue
Block a user