Skip to content

Commit

Permalink
Add Switch.networkInfo() function
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 28, 2023
1 parent abb38ff commit 9dd5af9
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-boxes-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Add `Switch.networkInfo()` function
2 changes: 1 addition & 1 deletion apps/tcp-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
6 changes: 5 additions & 1 deletion packages/runtime/src/$.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -14,6 +14,10 @@ export interface Init {
fn: (promise: Promise<unknown>, reason: any) => number
): void;

// nifm.c
nifmInitialize(): () => void;
networkInfo(): NetworkInfo;

// tcp.c
connect(cb: Callback<number>, ip: string, port: number): void;
write(cb: Callback<number>, fd: number, data: ArrayBuffer): void;
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime/src/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ interface Internal {
vibrationPattern?: (number | Vibration)[];
vibrationTimeoutId?: number;
renderingMode?: RenderingMode;
nifmInitialized?: boolean;
setRenderingMode: (
mode: RenderingMode,
ctx?: CanvasRenderingContext2DState
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ export interface ConnectOpts {
*/
port: number;
}

export interface NetworkInfo {
ip: string;
subnetMask: string;
gateway: string;
}
4 changes: 3 additions & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion source/tcp.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 9dd5af9

Please sign in to comment.