From 9dd5af972c9459a5754bca4ae12094603399b00b Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 28 Oct 2023 01:29:27 -0700 Subject: [PATCH] Add `Switch.networkInfo()` function --- .changeset/shaggy-boxes-rescue.md | 5 +++++ apps/tcp-server/src/main.ts | 2 +- packages/runtime/src/$.ts | 6 +++++- packages/runtime/src/switch.ts | 9 +++++++++ packages/runtime/src/types.ts | 6 ++++++ source/main.c | 4 +++- source/tcp.h | 2 +- 7 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 .changeset/shaggy-boxes-rescue.md diff --git a/.changeset/shaggy-boxes-rescue.md b/.changeset/shaggy-boxes-rescue.md new file mode 100644 index 00000000..c3a35de4 --- /dev/null +++ b/.changeset/shaggy-boxes-rescue.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Add `Switch.networkInfo()` function diff --git a/apps/tcp-server/src/main.ts b/apps/tcp-server/src/main.ts index 2c37006f..f7c71b2e 100644 --- a/apps/tcp-server/src/main.ts +++ b/apps/tcp-server/src/main.ts @@ -26,4 +26,4 @@ server.addEventListener('accept', async ({ fd }) => { } }); -console.log('TCP server listening on port %d', port); +console.log('TCP server listening on "%s:%d"', Switch.networkInfo().ip, port); diff --git a/packages/runtime/src/$.ts b/packages/runtime/src/$.ts index fc424f48..e42b21e7 100644 --- a/packages/runtime/src/$.ts +++ b/packages/runtime/src/$.ts @@ -1,4 +1,4 @@ -import type { Callback } from './types'; +import type { Callback, NetworkInfo } from './types'; import type { Server } from './tcp'; import type { MemoryDescriptor, Memory } from './wasm'; @@ -14,6 +14,10 @@ export interface Init { fn: (promise: Promise, reason: any) => number ): void; + // nifm.c + nifmInitialize(): () => void; + networkInfo(): NetworkInfo; + // tcp.c connect(cb: Callback, ip: string, port: number): void; write(cb: Callback, fd: number, data: ArrayBuffer): void; diff --git a/packages/runtime/src/switch.ts b/packages/runtime/src/switch.ts index 617e4f82..e2f91db6 100644 --- a/packages/runtime/src/switch.ts +++ b/packages/runtime/src/switch.ts @@ -294,6 +294,7 @@ interface Internal { vibrationPattern?: (number | Vibration)[]; vibrationTimeoutId?: number; renderingMode?: RenderingMode; + nifmInitialized?: boolean; setRenderingMode: ( mode: RenderingMode, ctx?: CanvasRenderingContext2DState @@ -653,6 +654,14 @@ export class SwitchClass extends EventTarget { return toPromise($.write, fd, ab); } + networkInfo() { + if (!this[INTERNAL_SYMBOL].nifmInitialized) { + this.addEventListener('exit', $.nifmInitialize()); + this[INTERNAL_SYMBOL].nifmInitialized = true; + } + return $.networkInfo(); + } + /** * Vibrates the main gamepad for the specified number of milliseconds or pattern. * diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.ts index 8bed636d..b54e96bf 100644 --- a/packages/runtime/src/types.ts +++ b/packages/runtime/src/types.ts @@ -72,3 +72,9 @@ export interface ConnectOpts { */ port: number; } + +export interface NetworkInfo { + ip: string; + subnetMask: string; + gateway: string; +} diff --git a/source/main.c b/source/main.c index dccedf9c..5ad937bd 100644 --- a/source/main.c +++ b/source/main.c @@ -17,6 +17,7 @@ #include "canvas.h" #include "font.h" #include "fs.h" +#include "nifm.h" #include "wasm.h" #include "image.h" #include "tcp.h" @@ -468,11 +469,12 @@ int main(int argc, char *argv[]) JS_SetContextOpaque(ctx, nx_ctx); JS_SetHostPromiseRejectionTracker(rt, nx_promise_rejection_handler, ctx); - /* The internal `$` object contains native functions that are wrapped in the JS runtime */ + // The internal `$` object contains native functions that are wrapped in the JS runtime JSValue global_obj = JS_GetGlobalObject(ctx); JSValue init_obj = JS_NewObject(ctx); nx_init_error(ctx, init_obj); nx_init_battery(ctx, init_obj); + nx_init_nifm(ctx, init_obj); nx_init_tcp(ctx, init_obj); nx_init_wasm(ctx, init_obj); JS_SetPropertyStr(ctx, global_obj, "$", init_obj); diff --git a/source/tcp.h b/source/tcp.h index b3a7568d..8db9b679 100644 --- a/source/tcp.h +++ b/source/tcp.h @@ -1,4 +1,4 @@ #pragma once #include "types.h" -void nx_init_tcp(JSContext *ctx, JSValueConst native_obj); +void nx_init_tcp(JSContext *ctx, JSValueConst init_obj);