Files
d2r-mobile/dm/host/front_mac.m
T
2026-10-12 16:51:08 +08:00

139 lines
10 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Mac 前端:DM_WINDOW=1 时开窗口(CAMetalLayer,供图形后端出图)+ 键盘 → dm_input;客体改在大栈线程上跑,主线程归 AppKit。
// 不开 DM_WINDOW:客体直接在当前线程跑,行为与无前端时相同(无窗口测试用这个)。
// 键位(任天堂布局):WASD 左摇杆、方向键十字键、J=A K=B U=X I=Y、Q=L E=R、Z=ZL C=ZR、F=按左摇杆 G=按右摇杆、回车=+ Tab=−。
// Mac front end: with DM_WINDOW=1 open a window (CAMetalLayer, for the graphics backend to present frames) + keyboard → dm_input; the guest runs on a large-stack thread instead and the main thread belongs to AppKit.
// Without DM_WINDOW: the guest runs directly on the current thread, behaving the same as with no front end (windowless tests use this).
// Key map (Nintendo layout): WASD left stick, arrow keys D-pad, J=A K=B U=X I=Y, Q=L E=R, Z=ZL C=ZR, F=press left stick G=press right stick, Return=+ Tab=−.
#import <Cocoa/Cocoa.h>
#import <QuartzCore/CAMetalLayer.h>
#import <Metal/Metal.h>
#include "dm.h"
#include "dm_input.h"
#include "dm_uiscreen.h"
void mtl_set_ui_rect_fn(void (*fn)(double *y, double *h));
static void ui_rect_frac(double *fy, double *fh) { int x, y, w, h, rw = 0, rh = 0; dm_ui_rect(&x, &y, &w, &h); if (dm_screen_real(&rw, &rh) && rh > 0) { *fy = (double)y / rh; *fh = (double)h / rh; } } // 同 front_ios.m:界面矩形的纵向位置与高(占画面高的比例) | Same as front_ios.m: vertical position and height of the UI rectangle (as fractions of the frame height)
__attribute__((constructor)) static void ui_shift_reg(void) { mtl_set_ui_rect_fn(ui_rect_frac); } // 不开窗口的跑法(截图 / 闸门)也要生效,所以不放在建图层那里 | Must also take effect when running without a window (screenshots / gate checks), so it is not placed where the layer is created
#include "front.h"
#include "hle.h"
#include <signal.h>
static CAMetalLayer *g_layer;
static uint64_t g_btn;
static int g_w, g_a, g_s, g_d;
void *front_metal_layer(void) { return (__bridge void *)g_layer; }
// 原版 16:9 常数,行为不变;DM_GAME_RES=宽x高(主机模式,掌机按 720/900 同比)只供与 iOS 宽屏对拍
// The original game's 16:9 constants, behavior unchanged; DM_GAME_RES=<width>x<height> (docked mode; handheld scaled by 720/900) is only for side-by-side comparison with iOS widescreen
uint64_t front_game_res(int docked) {
unsigned w, h; const char *e = getenv("DM_GAME_RES");
if (e && sscanf(e, "%ux%u", &w, &h) == 2) return docked ? (uint64_t)h << 32 | w : (uint64_t)(h * 4 / 5) << 32 | (w * 4 / 5 + 3) / 4 * 4;
return docked ? 900ull << 32 | 1600 : 720ull << 32 | 1280;
}
static void push(void) {
int32_t lx = (g_d - g_a) * 32767, ly = (g_w - g_s) * 32767;
dm_input_set(g_btn, lx, ly, 0, 0);
}
static int key_bit(unsigned short code) {
switch (code) {
case 38: return 0; case 40: return 1; case 32: return 2; case 34: return 3; // J K U I → A B X Y
case 3: return 4; case 5: return 5; // F G → 按摇杆 | F G → press stick
case 12: return 6; case 14: return 7; case 6: return 8; case 8: return 9; // Q E Z C → L R ZL ZR
case 36: return 10; case 48: return 11; // 回车 Tab → + − | Return Tab → + −
case 123: return 12; case 126: return 13; case 124: return 14; case 125: return 15; // ← ↑ → ↓
}
return -1;
}
static void on_key(NSEvent *e, int down) {
unsigned short c = e.keyCode;
switch (c) { case 13: g_w = down; break; case 0: g_a = down; break; case 1: g_s = down; break; case 2: g_d = down; break; }
int b = key_bit(c);
if (b >= 0) g_btn = down ? (g_btn | 1ull << b) : (g_btn & ~(1ull << b));
push();
}
typedef struct { void (*fn)(void); } guest_arg;
static void *guest_tramp(void *p) {
static char alt[65536]; // 信号备用栈是每线程的;崩溃打印要在客体线程上也生效 | The signal alternate stack is per thread; crash printing must also work on the guest thread
stack_t ss = {.ss_sp = alt, .ss_size = sizeof alt}; sigaltstack(&ss, 0);
((guest_arg *)p)->fn(); LOG("客体线程结束"); exit(0);
}
void front_run(void (*guest)(void)) {
if (!getenv("DM_WINDOW")) { guest(); return; }
@autoreleasepool {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
NSRect r = NSMakeRect(0, 0, 1280, 720);
NSWindow *win = [[NSWindow alloc] initWithContentRect:r
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered defer:NO];
win.title = @"D2R Mobile";
NSView *v = win.contentView;
v.wantsLayer = YES;
g_layer = [CAMetalLayer layer];
g_layer.device = MTLCreateSystemDefaultDevice();
g_layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
g_layer.contentsScale = win.backingScaleFactor;
v.layer = g_layer;
[win center];
[win makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp handler:^NSEvent *(NSEvent *e) {
if (e.window != win) return e; // 只吃游戏窗口的键;软键盘输入框等其他窗口照常收字 | Only swallow keys for the game window; other windows such as the software keyboard input box receive typing as usual
if (!e.isARepeat) on_key(e, e.type == NSEventTypeKeyDown);
return nil; // 吃掉,免得系统提示音 | Swallow it to avoid the system beep
}];
// 鼠标当单点触屏(本机测触屏用,front.h dm_touch_set):左键按下 / 拖动 = 手指 0 在归一化坐标处,松开 = 抬起
// Mouse as a single-point touchscreen (for testing touch on the local machine, front.h dm_touch_set): left button down / drag = finger 0 at the normalized coordinates, release = lifted
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDown | NSEventMaskLeftMouseDragged | NSEventMaskLeftMouseUp handler:^NSEvent *(NSEvent *e) {
if (e.window != win) return e;
if (e.type == NSEventTypeLeftMouseUp) { dm_touch_set(NULL, 0); return nil; }
NSPoint p = [v convertPoint:e.locationInWindow fromView:nil]; NSSize sz = v.bounds.size;
if (sz.width <= 0 || sz.height <= 0) return e;
dm_touch t = {0, (float)(p.x / sz.width), (float)(1.0 - p.y / sz.height)}; // AppKit 视图原点在左下,触屏原点在左上 | The AppKit view origin is at bottom left, the touchscreen origin is at top left
dm_touch_set(&t, 1); return nil;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:NSWindowWillCloseNotification object:win queue:nil
usingBlock:^(NSNotification *n) { (void)n; exit(0); }];
// 生命周期(hle.h / dm.h):失焦 → 2、拿回焦点 → 1;最小化 → 3 + 音频暂停 + GPU 挂起,还原 → 恢复(随后拿回焦点 → 1)
// Lifecycle (hle.h / dm.h): focus lost → 2, focus regained → 1; minimized → 3 + audio paused + GPU suspended, restored → resume (followed by focus regained → 1)
NSNotificationCenter *nc = NSNotificationCenter.defaultCenter;
[nc addObserverForName:NSWindowDidResignKeyNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; if (!win.miniaturized) dm_focus_set(2); }];
[nc addObserverForName:NSWindowDidBecomeKeyNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; dm_focus_set(1); }];
[nc addObserverForName:NSWindowDidMiniaturizeNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; dm_focus_set(3); dm_audio_suspend(1); dm_gfx_suspend(1); }];
[nc addObserverForName:NSWindowDidDeminiaturizeNotification object:win queue:nil usingBlock:^(NSNotification *n) { (void)n; dm_gfx_suspend(0); dm_audio_suspend(0); dm_focus_set(2); }];
// 客体线程:栈 64 MiB(Switch 主线程栈由 npdm 指定,宿主给足)
// Guest thread: 64 MiB stack (the Switch main thread stack is specified by npdm; the host gives plenty)
static guest_arg ga; ga.fn = guest;
pthread_attr_t a; pthread_attr_init(&a); pthread_attr_setstacksize(&a, 64u << 20);
pthread_t th; if (pthread_create(&th, &a, guest_tramp, &ga)) die("pthread_create guest");
[NSApp run];
}
}
// 软键盘输入框(front.h):主线程弹 NSAlert + 文本框,客体线程同步等结果。无窗口模式不弹(返回 -1,调用方按取消)
// Software keyboard input box (front.h): the main thread shows an NSAlert + text field and the guest thread waits synchronously for the result. Not shown in windowless mode (returns -1, the caller treats it as cancel)
int front_text_input(const char *title, const char *sub, const char *guide, const char *initial, int max_chars, char *out, int out_cap) {
if (!g_layer) return -1;
__block int r = 0;
__block NSString *text = nil;
dispatch_sync(dispatch_get_main_queue(), ^{
NSAlert *a = [NSAlert new];
a.messageText = title && *title ? @(title) : @"输入";
NSString *info = [NSString stringWithFormat:@"%s%s%s", sub ? sub : "", sub && *sub && guide && *guide ? "\n" : "", guide ? guide : ""];
a.informativeText = info;
[a addButtonWithTitle:@"确定"]; [a addButtonWithTitle:@"取消"];
NSTextField *f = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
f.stringValue = initial ? @(initial) : @"";
if (guide && *guide) f.placeholderString = @(guide);
a.accessoryView = f; a.window.initialFirstResponder = f;
r = [a runModal] == NSAlertFirstButtonReturn;
text = f.stringValue;
});
if (!r) return 0;
(void)max_chars; // 截断按 UTF-16 单元在 hle_swkbd.c 统一做 | Truncation is done uniformly in hle_swkbd.c in UTF-16 units
snprintf(out, out_cap, "%s", text.UTF8String);
return 1;
}