Skip to content

Commit

Permalink
support passing on environmental variables to homebrew
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed Jun 23, 2024
1 parent 4ea153e commit 686ca85
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 33 deletions.
10 changes: 9 additions & 1 deletion assets/apiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ class ApiClient {
/**
* @param {string} path
* @param {(string | string[])?} [args]
* @param {(string | string[])?} [env]
* @returns {Promise<ReadableStream<Uint8Array>?>} only returns true, throws error if not success code
*/
static async launchApp(path, args = null) {
static async launchApp(path, args = null, env = null) {
let params = new URLSearchParams({
"pipe": "1",
"path": path
Expand All @@ -61,6 +62,13 @@ class ApiClient {
params.append("args", args.map(arg => arg.replaceAll(" ", "\\ ")).join(" "));
}

if (env != null) {
// @ts-ignore
params.append("env", Object.entries(env).map(([key, val]) =>
`${key}=${val}`.replaceAll(" ", "\\ ")
).join(" "));
}

let uri = baseURL + "/hbldr?" + params.toString();

let response = await fetch(uri);
Expand Down
2 changes: 1 addition & 1 deletion assets/carouselView.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ async function renderMainContentCarousel(items, fadeout = true) {
let logStream = null;

if (res && res.path) {
logStream = await ApiClient.launchApp(res.path, res.args);
logStream = await ApiClient.launchApp(res.path, res.args, res.env);
res = logStream != null;
}

Expand Down
2 changes: 1 addition & 1 deletion assets/modalView.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function renderModalOverlay(items) {
let logStream = null;

if (res && res.path) {
logStream = await ApiClient.launchApp(res.path, res.args);
logStream = await ApiClient.launchApp(res.path, res.args, res.env);
res = logStream != null;
}

Expand Down
8 changes: 6 additions & 2 deletions homebrew/Mednafen/homebrew.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ along with this program; see the file COPYING. If not, see

async function main() {
const PAYLOAD = window.workingDir + '/mednafen.elf';
const ENVVARS = {MEDNAFEN_HOME: window.workingDir};

const ROMDIR = window.workingDir + '/roms/';
const ROMTYPES = ['cue', 'dsk', 'gb', 'gba', 'gbc', 'gen', 'gg', 'lnx',
'nes', 'ngp', 'pce', 'psx', 'sfc', 'smc', 'sms', 'vb',
Expand Down Expand Up @@ -92,7 +94,8 @@ async function main() {
onclick: async() => {
return {
path: PAYLOAD,
args: ROMDIR + entry.name
args: ROMDIR + entry.name,
env: ENVVARS
}
}
};
Expand All @@ -115,7 +118,8 @@ async function main() {
if(await checkApiVersion()) {
return {
path: PAYLOAD,
args: await pickFile(window.workingDir)
args: await pickFile(window.workingDir),
env: ENVVARS
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10878,16 +10878,16 @@ snes_faust.input.port1.gamepad.left joystick 0xe4e9845b887ab7fd0006001100010000
snes_faust.input.port1.gamepad.r joystick 0xe4e9845b887ab7fd0006001100010000 button_10

;snes_faust, Virtual Port 1, Gamepad: Rapid A (right)
snes_faust.input.port1.gamepad.rapid_a keyboard 0x0 94
snes_faust.input.port1.gamepad.rapid_a joystick 0xe4e9845b887ab7fd0006001100010000 button_1

;snes_faust, Virtual Port 1, Gamepad: Rapid B (center, lower)
snes_faust.input.port1.gamepad.rapid_b keyboard 0x0 90
snes_faust.input.port1.gamepad.rapid_b joystick 0xe4e9845b887ab7fd0006001100010000 button_0

;snes_faust, Virtual Port 1, Gamepad: Rapid X (center, upper)
snes_faust.input.port1.gamepad.rapid_x keyboard 0x0 96
snes_faust.input.port1.gamepad.rapid_x joystick 0xe4e9845b887ab7fd0006001100010000 button_3

;snes_faust, Virtual Port 1, Gamepad: Rapid Y (left)
snes_faust.input.port1.gamepad.rapid_y keyboard 0x0 92
snes_faust.input.port1.gamepad.rapid_y joystick 0xe4e9845b887ab7fd0006001100010000 button_2

;snes_faust, Virtual Port 1, Gamepad: RIGHT →
snes_faust.input.port1.gamepad.right joystick 0xe4e9845b887ab7fd0006001100010000 button_14
Expand Down
2 changes: 1 addition & 1 deletion src/pc/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sys_launch_title(const char* title_id, char* args) {


int
sys_launch_homebrew(const char* path, char* args) {
sys_launch_homebrew(const char* path, const char* args, const char* env) {
char cmd[255];
FILE *pf;

Expand Down
35 changes: 14 additions & 21 deletions src/ps5/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,22 @@ ps5_find_pid(const char* name) {


int
sys_launch_homebrew(const char* path, const char* args) {
char home[PATH_MAX];
char pwd[PATH_MAX];
char buf[PATH_MAX];
sys_launch_homebrew(const char* path, const char* args, const char* env) {
char* argv[255];
char* envp[255];
int optval = 1;
char* envp[3];
int argc = 0;
int fds[2];
pid_t pid;
char* dir;

if(!args) {
args = "";
}

printf("launch homebrew: %s %s\n", path, args);
if(!env) {
env = "";
}

printf("launch homebrew: %s %s %s\n", env, path, args);

if(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
perror("socketpair");
Expand All @@ -193,24 +192,18 @@ sys_launch_homebrew(const char* path, const char* args) {
return -11;
}

strncpy(buf, path, sizeof(buf));
dir = dirname(buf);

snprintf(pwd, sizeof(pwd), "PWD=%s", dir);
snprintf(home, sizeof(home), "HOME=%s", dir);

envp[0] = pwd;
envp[1] = home;
envp[2] = 0;

argc = args_split(args, argv, 255);
args_split(args, argv, 255);
args_split(env, envp, 255);
pid = hbldr_launch(path, fds[1], argv, envp);

close(fds[1]);
for(int i=0; i<argc; i++) {
for(int i=0; argv[i]; i++) {
free(argv[i]);
}
for(int i=0; envp[i]; i++) {
free(envp[i]);
}

close(fds[1]);
if(pid < 0) {
close(fds[0]);
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ along with this program; see the file COPYING. If not, see
#pragma once

int sys_launch_title(const char* title_id, const char* args);
int sys_launch_homebrew(const char* path, const char* args);
int sys_launch_homebrew(const char* path, const char* args, const char* env);

4 changes: 3 additions & 1 deletion src/websrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ hbldr_request(struct MHD_Connection *conn) {
const char* path;
const char *args;
const char *pipe;
const char *env;
int fd;

path = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "path");
args = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "args");
env = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "env");
pipe = MHD_lookup_connection_value(conn, MHD_GET_ARGUMENT_KIND, "pipe");

if(!path) {
Expand All @@ -119,7 +121,7 @@ hbldr_request(struct MHD_Connection *conn) {
MHD_destroy_response(resp);
}

} else if((fd=sys_launch_homebrew(path, args)) < 0) {
} else if((fd=sys_launch_homebrew(path, args, env)) < 0) {
if((resp=MHD_create_response_from_buffer(0, "", MHD_RESPMEM_PERSISTENT))) {
ret = websrv_queue_response(conn, MHD_HTTP_SERVICE_UNAVAILABLE, resp);
MHD_destroy_response(resp);
Expand Down

0 comments on commit 686ca85

Please sign in to comment.