Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions dm/host/front_mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// Cheats (hle_cheats.c): the Cheats menu's rows toggle, reload (⌘R) and open the cheats folder; its first item "Show Cheats
// Window" (⌘⇧C, or DM_CHEATS_WINDOW=1 at launch) opens a Cheats panel beside the game: sections, search, the master switch,
// safe mode, values, details and the import of cheat files (see the code below).
// 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=−, Space=shake the controller.
// 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=−, Space=shake the controller;
// DM_KEYMAP moves a function to another key (the right stick too), DM_PAD_MAP changes what a controller's buttons send (input_map.h).
// Controllers: face buttons as on the iPhone, by position (right A, bottom B, top X, left Y) or on a Nintendo pad by its labels (pad_apple.h), shoulders L R, triggers ZL ZR, Menu + Options −, both sticks and their presses.
// Each controller is a player of its own (local co-op): the first one connected is player 1, together with the keyboard, the next player 2, and so on up to 8 (pad_slots.h);
// DM_PAD_MERGE=1 merges every controller into player 1 instead, DM_KBD_PLAYER=own makes the keyboard player 1 on its own, with the controllers from player 2 on.
Expand All @@ -17,6 +18,7 @@
#include "front.h"
#include "hle.h"
#include "pad_slots.h"
#include "input_map.h"
#include "haptics_apple.h"
#include "motion_apple.h"
#include "motion_pro_mac.h"
Expand All @@ -27,7 +29,6 @@

static CAMetalLayer *g_layer;
static uint64_t g_btn;
static int g_w, g_a, g_s, g_d;
static int g_input_on = 1; // main thread: the game window is key, so its keyboard, mouse and controllers reach the game (input_release / input_resume)

void *front_metal_layer(void) { return (__bridge void *)g_layer; }
Expand Down Expand Up @@ -91,27 +92,20 @@ void front_screen_match(int fps) {
if (!g_layer || g_match_want == fps) return; // no window (a windowless run's main thread runs the guest), or the rate as before
g_match_want = fps; dispatch_async(dispatch_get_main_queue(), ^{ screen_match_apply(); });
}
// Keyboard: each key holds one function of the key map (input_map.h; DM_KEYMAP changes it), the held functions make the buttons and both sticks
static key_map g_keys;
static uint32_t g_held_fn; // bit f: a key holding function f is down
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 → 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; // Return Tab → + −
case 123: return 12; case 126: return 13; case 124: return 14; case 125: return 15; // ← ↑ → ↓
}
return -1;
uint32_t h = g_held_fn;
#define D(f) ((int32_t)(h >> (f) & 1))
dm_input_set(g_btn, (D(IM_LRIGHT) - D(IM_LLEFT)) * 32767, (D(IM_LUP) - D(IM_LDOWN)) * 32767, (D(IM_RRIGHT) - D(IM_RLEFT)) * 32767, (D(IM_RUP) - D(IM_RDOWN)) * 32767);
#undef D
}
static void on_key(NSEvent *e, int down) {
if (!g_input_on) return;
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);
uint64_t m = b >= 0 ? 1ull << b : c == 49 ? DM_BTN_SHAKE : 0; // Space: shaken while held
g_btn = down ? (g_btn | m) : (g_btn & ~m);
int f = key_map_fn(&g_keys, e.keyCode); if (f < 0) return;
g_held_fn = down ? (g_held_fn | 1u << f) : (g_held_fn & ~(1u << f));
g_btn = (g_held_fn & 0xffff) | (g_held_fn >> IM_SHAKE & 1 ? DM_BTN_SHAKE : 0);
push();
}

Expand All @@ -123,6 +117,7 @@ static void on_key(NSEvent *e, int down) {
static pad_slots g_slots;
static GCController *g_held[DM_PLAYERS]; // the controller of each slot, kept alive while it holds it (the slot table keys on its pointer)
static int g_merge, g_kbd_own;
static pad_map g_padmap; // DM_PAD_MAP (input_map.h): what each button sends, applied to every controller
static int32_t pad_axis(float v) { return (int32_t)(fmaxf(-1.f, fminf(1.f, v)) * DM_STICK_MAX); }
static void pad_read(GCExtendedGamepad *g) {
if (!g_input_on) return;
Expand All @@ -134,6 +129,7 @@ static void pad_read(GCExtendedGamepad *g) {
K(g.buttonMenu, DM_BTN_PLUS); K(g.buttonOptions, DM_BTN_MINUS); K(g.leftThumbstickButton, DM_BTN_LS); K(g.rightThumbstickButton, DM_BTN_RS); // nil on pads without them: not pressed
K(g.dpad.left, DM_BTN_LEFT); K(g.dpad.up, DM_BTN_UP); K(g.dpad.right, DM_BTN_RIGHT); K(g.dpad.down, DM_BTN_DOWN);
#undef K
m = pad_map_apply(&g_padmap, m);
dm_pad_set_player(p, m, pad_axis(g.leftThumbstick.xAxis.value), pad_axis(g.leftThumbstick.yAxis.value), pad_axis(g.rightThumbstick.xAxis.value), pad_axis(g.rightThumbstick.yAxis.value));
}
// Home and Capture: without this macOS takes them as system gestures (Home opens the Games app); the host gives them no function
Expand Down Expand Up @@ -190,7 +186,7 @@ static void pads_changed(GCController *gone) {
static void input_release(void) {
g_input_on = 0;
dm_motion_pro_active(0);
g_btn = 0; g_w = g_a = g_s = g_d = 0; push();
g_btn = 0; g_held_fn = 0; push();
if (g_mouse_down) { g_mouse_down = 0; dm_touch_set(NULL, 0); }
if (g_merge) { for (GCController *c in GCController.controllers) if (c.extendedGamepad) { dm_pad_set(0, 0, 0, 0, 0); break; } }
else for (int p = 0; p < DM_PLAYERS; p++) if (g_held[p]) dm_pad_set_player(p, 0, 0, 0, 0, 0);
Expand All @@ -202,6 +198,10 @@ static void input_resume(void) {
else for (int p = 0; p < DM_PLAYERS; p++) if (g_held[p].extendedGamepad) pad_read(g_held[p].extendedGamepad);
}
static void pads_start(void) {
key_map_default(&g_keys); pad_map_default(&g_padmap);
const char *km = getenv("DM_KEYMAP"), *pm = getenv("DM_PAD_MAP");
if (km && *km) { int bad = key_map_parse(&g_keys, km); LOG("front end: keyboard map from DM_KEYMAP (%s)%s", km, bad ? ", malformed entries left out" : ""); }
if (pm && *pm) { int bad = pad_map_parse(&g_padmap, pm); LOG("front end: controller buttons from DM_PAD_MAP (%s)%s", pm, bad ? ", malformed entries left out" : ""); }
const char *e = getenv("DM_PAD_MERGE"); g_merge = e && atoi(e);
e = getenv("DM_KBD_PLAYER"); g_kbd_own = !g_merge && e && !strcmp(e, "own");
static const char kbd = 0;
Expand Down Expand Up @@ -1225,7 +1225,7 @@ int front_text_input(const char *title, const char *sub, const char *guide, cons
dispatch_sync(dispatch_get_main_queue(), ^{
// The box takes the keyboard and the mouse, so the game window never sees the key-up of the button or the touch-up of the click that opened it: nothing may stay pressed
// while it is up, or the game reads the stuck button / touch the moment the box closes (Stardew Valley's character creation re-opened the keyboard endlessly)
g_btn = 0; g_w = g_a = g_s = g_d = 0; push();
g_btn = 0; g_held_fn = 0; push();
dm_touch_set(NULL, 0);
int min_chars = g_swkbd_min_chars > 0 ? g_swkbd_min_chars : 0;
NSString *info = [NSString stringWithFormat:@"%s%s%s", sub ? sub : "", sub && *sub && guide && *guide ? "\n" : "", guide ? guide : ""];
Expand Down
89 changes: 89 additions & 0 deletions dm/host/input_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// The Mac front end's button assignments (front_mac.m; pure, so tests/t_input_map.c checks it without a keyboard or controller), named as DM_KEYS names them (hle_hid.c).
// DM_KEYMAP=<name>=<key code>,… puts a function on another key: <key code> is the macOS virtual key code (NSEvent.keyCode, decimal), or "none" to leave the function on no key.
// The names are the 16 buttons, the four directions of each stick (LUp … RRight) and Shake; the left stick is on W A S D, the right stick on no key, unless DM_KEYMAP says so.
// One key holds one function: the last assignment of a key wins, and the function that held it before is left on no key, so "A=40,B=38" swaps A and B.
// DM_PAD_MAP=<name>=<name>,… changes what a controller's buttons send: the button that sends <first> by default (as front_mac.m maps the pad: face buttons by position, a Nintendo pad's by its labels, pad_apple.h) sends <second> instead,
// "none" sends nothing; buttons not named stay as they are, so "A=B,B=A" swaps them. Only the 16 buttons take part (the sticks stay sticks).
// A malformed entry is left out and named in the log; the rest of the list still applies.
// input_map_valid() is the same parser without a map or a log line: the Mac launcher (dm/mac/launcher.c) checks a saved DM_KEYMAP / DM_PAD_MAP with it, so a saved value is used only when the host takes every entry of it.
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dm_input.h"

enum { IM_BUTTONS = 16, IM_LUP = 16, IM_LDOWN, IM_LLEFT, IM_LRIGHT, IM_RUP, IM_RDOWN, IM_RLEFT, IM_RRIGHT, IM_SHAKE, IM_N }; // functions 0..15 are the DM_BTN_* bits
#define IM_KEYS 128 // virtual key codes are below 128
typedef struct { int8_t fn[IM_KEYS]; } key_map; // the function each key code holds, -1 none
typedef struct { int8_t to[IM_BUTTONS]; } pad_map; // the button each default button sends, -1 none

static const char *const im_names[IM_N] = {"A", "B", "X", "Y", "LS", "RS", "L", "R", "ZL", "ZR", "Plus", "Minus", "Left", "Up", "Right", "Down",
"LUp", "LDown", "LLeft", "LRight", "RUp", "RDown", "RLeft", "RRight", "Shake"};
static int im_name(const char *s, size_t n) {
for (int f = 0; f < IM_N; f++) if (strlen(im_names[f]) == n && !strncasecmp(s, im_names[f], n)) return f;
return -1;
}
static void key_map_default(key_map *m) {
memset(m->fn, -1, sizeof m->fn);
static const struct { uint8_t code; int8_t fn; } d[] = {
{38, 0}, {40, 1}, {32, 2}, {34, 3}, // J K U I → A B X Y
{3, 4}, {5, 5}, // F G → press stick
{12, 6}, {14, 7}, {6, 8}, {8, 9}, // Q E Z C → L R ZL ZR
{36, 10}, {48, 11}, // Return Tab → + −
{123, 12}, {126, 13}, {124, 14}, {125, 15}, // ← ↑ → ↓
{13, IM_LUP}, {1, IM_LDOWN}, {0, IM_LLEFT}, {2, IM_LRIGHT}, // W S A D → left stick
{49, IM_SHAKE}, // Space: shaken while held
};
for (size_t i = 0; i < sizeof d / sizeof *d; i++) m->fn[d[i].code] = d[i].fn;
}
static void pad_map_default(pad_map *m) { for (int b = 0; b < IM_BUTTONS; b++) m->to[b] = (int8_t)b; }

// Walks "<name>=<value>,…", calling one(name's function, value text, its length) for each entry; returns the number of malformed entries, each logged here
static int im_parse(const char *spec, int max_fn, int (*one)(void *, int, const char *, size_t), void *ctx, int log) {
int bad = 0;
for (const char *p = spec; p && *p; ) {
const char *end = strchr(p, ','); size_t n = end ? (size_t)(end - p) : strlen(p);
const char *eq = memchr(p, '=', n);
int f = eq ? im_name(p, (size_t)(eq - p)) : -1;
if (f < 0 || f >= max_fn || !one(ctx, f, eq + 1, n - (size_t)(eq + 1 - p))) {
if (log) fprintf(stderr, "[dm] [RED] input map: '%.*s' is not <name>=<value>, left out\n", (int)n, p); // dm.h LOG's form (this header stays free of dm.h for the test)
bad++;
}
p = end ? end + 1 : NULL;
}
return bad;
}
static int im_key_one(void *ctx, int f, const char *v, size_t n) {
key_map *m = ctx; char buf[8];
if (n == 4 && !strncasecmp(v, "none", 4)) { for (int c = 0; c < IM_KEYS; c++) if (m->fn[c] == f) m->fn[c] = -1; return 1; }
if (!n || n >= sizeof buf || strspn(v, "0123456789") < n) return 0;
memcpy(buf, v, n); buf[n] = 0; int code = atoi(buf);
if (code >= IM_KEYS) return 0;
for (int c = 0; c < IM_KEYS; c++) if (m->fn[c] == f) m->fn[c] = -1; // the function leaves its old key
m->fn[code] = (int8_t)f; // the key's old function is left on no key
return 1;
}
static int im_pad_one(void *ctx, int f, const char *v, size_t n) {
pad_map *m = ctx;
if (n == 4 && !strncasecmp(v, "none", 4)) { m->to[f] = -1; return 1; }
int t = im_name(v, n); if (t < 0 || t >= IM_BUTTONS) return 0;
m->to[f] = (int8_t)t; return 1;
}
static int key_map_parse(key_map *m, const char *spec) { return im_parse(spec, IM_N, im_key_one, m, 1); }
static int pad_map_parse(pad_map *m, const char *spec) { return im_parse(spec, IM_BUTTONS, im_pad_one, m, 1); }
// 1 when spec is a DM_KEYMAP (pad = 0) or DM_PAD_MAP (pad = 1) value the host takes whole: not empty, every entry well-formed. Pure: nothing is logged, no map is kept
static int input_map_valid(const char *spec, int pad) {
if (!spec || !*spec) return 0;
key_map k; pad_map m; key_map_default(&k); pad_map_default(&m);
return pad ? !im_parse(spec, IM_BUTTONS, im_pad_one, &m, 0) : !im_parse(spec, IM_N, im_key_one, &k, 0);
}
static int key_map_fn(const key_map *m, unsigned code) { return code < IM_KEYS ? m->fn[code] : -1; }
// The buttons a controller sends for the buttons the default mapping read (DM_BTN_* bits); bits above the 16 buttons (DM_BTN_SHAKE) pass through
static uint64_t pad_map_apply(const pad_map *m, uint64_t btn) {
uint64_t out = btn & ~0xffffull;
for (int b = 0; b < IM_BUTTONS; b++) if (btn >> b & 1 && m->to[b] >= 0) out |= 1ull << m->to[b];
return out;
}
static int pad_map_is_default(const pad_map *m) { for (int b = 0; b < IM_BUTTONS; b++) if (m->to[b] != b) return 0; return 1; }
5 changes: 4 additions & 1 deletion dm/host/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ QUIET = $(if $(findstring -fsanitize,$(SAN)),,2>/dev/null)
GINC = $(if $(strip $(GAME)),-I../gen/$(GAME) -I../../games/$(GAME),$(error GAME is not set: make GAME=<base game title ID>: $(shell python3 ../../games/game.py list | awk -F'\t' '{printf "%s (%s) ", $$1, $$3}')))
$(B)/t_fs $(B)/t_fs_case $(B)/t_fs_commit_list $(B)/t_fs_commit_list_tsan: ../romfs_image.h
GAME_H = ../gen/$(GAME)/game_profile.h
run: $(B)/t_language $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(B)/t_fs $(B)/t_fs_commit_list $(B)/t_thread_leak $(B)/t_fs_case $(B)/t_audio $(B)/t_audio_spsc $(B)/t_swkbd $(B)/t_altstack $(B)/t_condvar $(B)/t_condvar_old $(B)/t_pollwait $(B)/t_pctl $(B)/t_time $(B)/t_shared_font $(B)/t_vorbis $(B)/t_vorbis_divides $(B)/t_hid $(B)/t_haptics $(B)/t_haptics_apple $(B)/t_cheats
run: $(B)/t_language $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(B)/t_fs $(B)/t_fs_commit_list $(B)/t_thread_leak $(B)/t_fs_case $(B)/t_audio $(B)/t_audio_spsc $(B)/t_swkbd $(B)/t_altstack $(B)/t_condvar $(B)/t_condvar_old $(B)/t_pollwait $(B)/t_pctl $(B)/t_time $(B)/t_shared_font $(B)/t_vorbis $(B)/t_vorbis_divides $(B)/t_hid $(B)/t_haptics $(B)/t_haptics_apple $(B)/t_cheats $(B)/t_input_map
$(B)/t_language
$(B)/t_heap_size
$(B)/t_touch
Expand Down Expand Up @@ -52,6 +52,7 @@ run: $(B)/t_language $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture
$(B)/t_motion_pro
$(B)/t_motion_pro_mac
TMPDIR="$(abspath $(B))" $(B)/t_hid
$(B)/t_input_map
$(B)/t_haptics
$(B)/t_haptics_apple
TMPDIR="$(abspath $(B))" $(B)/t_cheats $(QUIET)
Expand Down Expand Up @@ -173,6 +174,8 @@ $(B)/t_motion_pro_mac: t_motion_pro_mac.m ../motion_pro_mac.h ../motion_pro.h ..
$(CLANG) -O1 -g -fobjc-arc -Wall -Werror -Wno-unused-function -fsanitize=address,undefined -I.. -o $@ t_motion_pro_mac.m -framework Foundation -framework GameController -framework IOKit
$(B)/t_hid: t_hid.c ../hle_hid.c ../motion_fusion.h ../hle_haptics.c ../hle_haptics.h ../pad_slots.h ../dm_input.h ../hle.h ../dm.h $(GAME_H) | $(B)
$(CLANG) -O1 -Wall -Werror -Wno-unused-function -I.. $(GINC) -o $@ t_hid.c ../hle_haptics.c
$(B)/t_input_map: t_input_map.c ../input_map.h ../dm_input.h | $(B)
$(CLANG) -O1 -Wall -Werror -Wno-unused-function -I.. -o $@ t_input_map.c
$(B)/t_hid_tsan: t_hid.c ../hle_hid.c ../motion_fusion.h ../hle_haptics.c ../hle_haptics.h ../pad_slots.h ../dm_input.h ../hle.h ../dm.h $(GAME_H) | $(B)
$(CLANG) -O1 -g -fsanitize=thread -Wall -Werror -Wno-unused-function -I.. $(GINC) -o $@ t_hid.c ../hle_haptics.c
$(B)/t_haptics: t_haptics.c ../hle_haptics.c ../hle_haptics.h ../dm_input.h | $(B)
Expand Down
Loading