Skip to content
Closed
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
66 changes: 39 additions & 27 deletions dm/host/front_mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// 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 (with Shift held: the right stick, for the camera), 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_KBD_MAP moves any of them, and gives the right stick keys of its own (pad_kbd.h); it is read from env.txt (`DM_KBD_MAP=A=C,B=X,…`).
// 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 @@ -25,6 +26,7 @@
#include "pad_jc1_mac.h"
#include "pad_apple.h"
#include "pad_hid_mac.h"
#include "pad_kbd.h"
#include "pad_switch2_apple.h"
static int s2pad_player(s2_pad *d); // the Switch 2 pads' player slots (below, beside the raw HID pads')
static void s2pad_read(s2_pad *d);
Expand All @@ -39,9 +41,9 @@
#include <sys/stat.h>

static CAMetalLayer *g_layer;
static uint64_t g_btn;
static int g_w, g_a, g_s, g_d;
static int g_rl, g_ru, g_rr, g_rd; // arrow keys pressed with Shift held: the right stick (each goes back up as it went down, whatever Shift does meanwhile)
static pad_kbd g_kbd; // the keyboard's map (pad_kbd.h): DM_KBD_MAP over the default
static uint64_t g_kheld; // one bit per pad_kbd action held now (g_held below is the controllers)
static uint8_t g_krole[256]; // the action each key code went down as, +1 (0: none), so an arrow with Shift comes back up as it went down, whatever Shift does meanwhile
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 @@ -144,21 +146,24 @@ static void cursor_hide_for_input(void) {
NSView *v = g_win.contentView;
if (NSPointInRect([v convertPoint:g_win.mouseLocationOutsideOfEventStream fromView:nil], v.bounds)) [NSCursor setHiddenUntilMouseMoves:YES];
}
// DM_KBD_MAP (pad_kbd.h): read once, on the first key the game window sees, so the map the app started with is the one used
static void kbd_init(void) {
static int done;
if (done) return;
done = 1;
const char *map = getenv("DM_KBD_MAP");
pad_kbd_init(&g_kbd, map);
if (map && *map) LOG("front end: keyboard map from DM_KBD_MAP");
}
static int32_t kbd_axis(uint64_t h, int pos, int neg) { return (int32_t)(((h >> pos) & 1) - ((h >> neg) & 1)) * DM_STICK_MAX; }
static void push(void) {
int32_t lx = (g_d - g_a) * 32767, ly = (g_w - g_s) * 32767;
int32_t rx = (g_rr - g_rl) * 32767, ry = (g_ru - g_rd) * 32767;
dm_input_set(g_btn, lx, ly, rx, ry);
}
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;
const uint64_t h = g_kheld; // PK_A..PK_DOWN are dm_input.h's button bits in that order, so the low 16 are the buttons as they are
dm_input_set((h & 0xFFFF) | (((h >> PK_SHAKE) & 1) ? DM_BTN_SHAKE : 0),
kbd_axis(h, PK_LRIGHT, PK_LLEFT), kbd_axis(h, PK_LUP, PK_LDOWN),
kbd_axis(h, PK_RRIGHT, PK_RLEFT), kbd_axis(h, PK_RUP, PK_RDOWN));
}
// Whatever the keyboard held goes up (the window lost focus, a text box took it, the guest asked for input)
static void kbd_release_all(void) { g_kheld = 0; memset(g_krole, 0, sizeof g_krole); push(); }
// fn held with a letter: the arrow and function keys carry the same modifier flag, so the key must be a letter for this to be a Globe shortcut
static int fn_shortcut(NSEvent *e) {
NSString *s = e.charactersIgnoringModifiers;
Expand All @@ -168,20 +173,27 @@ static int fn_shortcut(NSEvent *e) {
static int cp_poll(void);
static void on_key(NSEvent *e, int down) {
if (!g_input_on) return;
kbd_init();
unsigned short c = e.keyCode;
if (cp_blocks_input(g_cp.phase)) { // the prompt has the keyboard: Return or J (A) is done, Escape or K (B) closes it, no key reaches the game
if (down && g_cp.phase == CP_OPEN) { if (c == 53 || c == 40) g_cp_cancel = 1; else if (c == 36 || c == 38) g_cp_confirm = 1; cp_poll(); }
return;
}
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 a = pad_kbd_action(&g_kbd, c);
if (a < 0 || c > 0xFF) return;
// An arrow key pressed with Shift held is the right stick until it is released; without Shift, the D-pad. The keyboard had no right stick: a 3D game's camera
// could be turned only with a controller (Donkey Kong Bananza)
int *rs = c == 123 ? &g_rl : c == 126 ? &g_ru : c == 124 ? &g_rr : c == 125 ? &g_rd : NULL;
if (rs && (down ? (e.modifierFlags & NSEventModifierFlagShift) != 0 : *rs)) { *rs = down; push(); return; }
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);
if (down && (m || c == 13 || c == 0 || c == 1 || c == 2)) cursor_hide_for_input(); // a key the game takes (WASD: the left stick)
// could be turned only with a controller (Donkey Kong Bananza). Only a D-pad action moves: a right stick given keys of its own (DM_KBD_MAP) keeps them
if (down) {
if ((e.modifierFlags & NSEventModifierFlagShift) != 0) a = pad_kbd_as_right_stick(a);
g_krole[c] = (uint8_t)(a + 1);
g_kheld |= 1ull << a;
cursor_hide_for_input(); // a key the game takes
} else {
int was = g_krole[c];
if (!was) return; // went down before this window had the keyboard: nothing to release
g_krole[c] = 0;
g_kheld &= ~(1ull << (was - 1));
}
push();
}

Expand Down Expand Up @@ -355,7 +367,7 @@ static void input_release(void) {
g_input_on = 0;
dm_motion_pro_active(0); hidpad_motion_clear();
dm_switch2_active(0); dm_jc1_active(0);
g_btn = 0; g_w = g_a = g_s = g_d = 0; g_rl = g_ru = g_rr = g_rd = 0; push();
kbd_release_all(); // clears what the map holds and pushes once
if (g_mouse_down) { g_mouse_down = 0; dm_touch_set(NULL, 0); }
pads_release();
}
Expand Down Expand Up @@ -655,7 +667,7 @@ int front_ctrl_support(int min, int max) {
if (!g_layer) return -1;
dispatch_semaphore_t done = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
g_btn = 0; g_w = g_a = g_s = g_d = 0; g_rl = g_ru = g_rr = g_rd = 0; push(); // nothing the game held stays pressed while the prompt has the input
kbd_release_all(); // nothing the game held stays pressed while the prompt has the input
if (g_mouse_down) { g_mouse_down = 0; dm_touch_set(NULL, 0); }
pads_release();
int a, b; cp_buttons(&a, &b);
Expand Down Expand Up @@ -1767,7 +1779,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; g_rl = g_ru = g_rr = g_rd = 0; push();
kbd_release_all();
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
165 changes: 165 additions & 0 deletions dm/host/pad_kbd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// The Mac keyboard's map to Switch input (front_mac.m): which macOS virtual key code presses each Switch button and stick direction.
// The actions are dm_input.h's NpadButton bits in order (A B X Y LS RS L R ZL ZR PLUS MINUS LEFT UP RIGHT DOWN), then the left stick's four
// directions, the right stick's four and the shake. Pure, so tests/t_pad_kbd.c checks it without a window.
//
// DM_KBD_MAP: comma-separated ACTION=KEY[|KEY...] entries over the default map. A named action's keys are replaced, not added to, and a key
// given to one action is taken away from any other, so the last entry wins; `A=` maps A to no key. An unknown action or key is skipped, as
// pad_hid.h's DM_HID_MAP does. A key is named by its letter or digit, by the character it makes on a US keyboard (so `-`, `=`, `[`, `;`, `'`
// and the like are keys too), or by name: UP DOWN LEFT RIGHT SPACE RETURN TAB ESC DELETE, MINUS EQUAL LBRACKET RBRACKET SEMICOLON QUOTE COMMA
// PERIOD SLASH BACKSLASH GRAVE, KEYPAD_PLUS KEYPAD_MINUS KEYPAD_ENTER, F1..F12.
//
// The default map, Nintendo's layout: WASD left stick, arrows D-pad (with Shift held: the right stick), J=A K=B U=X I=Y, Q=L E=R, Z=ZL C=ZR,
// F=press left stick, Return=+ Tab=-, Space=shake.
#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

enum {
PK_A, PK_B, PK_X, PK_Y, PK_LS, PK_RS, PK_L, PK_R, PK_ZL, PK_ZR, PK_PLUS, PK_MINUS,
PK_LEFT, PK_UP, PK_RIGHT, PK_DOWN, // the D-pad; the 16 above and these are dm_input.h's button bits, in its order
PK_LUP, PK_LDOWN, PK_LLEFT, PK_LRIGHT, // the left stick's four directions
PK_RUP, PK_RDOWN, PK_RLEFT, PK_RRIGHT, // the right stick's four
PK_SHAKE, // the controller is shaken while held (DM_BTN_SHAKE)
PK_COUNT
};

// macOS virtual key codes (HIToolbox/Events.h's kVK_*). PK_KEY_NONE: no key
enum {
PK_KEY_NONE = 0xFFFF,
PK_KEY_A = 0, PK_KEY_S = 1, PK_KEY_D = 2, PK_KEY_F = 3, PK_KEY_H = 4, PK_KEY_G = 5, PK_KEY_Z = 6, PK_KEY_X = 7,
PK_KEY_C = 8, PK_KEY_V = 9, PK_KEY_B = 11, PK_KEY_Q = 12, PK_KEY_W = 13, PK_KEY_E = 14, PK_KEY_R = 15,
PK_KEY_Y = 16, PK_KEY_T = 17, PK_KEY_1 = 18, PK_KEY_2 = 19, PK_KEY_3 = 20, PK_KEY_4 = 21, PK_KEY_6 = 22,
PK_KEY_5 = 23, PK_KEY_EQUAL = 24, PK_KEY_9 = 25, PK_KEY_7 = 26, PK_KEY_MINUS = 27, PK_KEY_8 = 28, PK_KEY_0 = 29,
PK_KEY_RBRACKET = 30, PK_KEY_O = 31, PK_KEY_U = 32, PK_KEY_LBRACKET = 33, PK_KEY_I = 34, PK_KEY_P = 35,
PK_KEY_RETURN = 36, PK_KEY_L = 37, PK_KEY_J = 38, PK_KEY_QUOTE = 39, PK_KEY_K = 40, PK_KEY_SEMICOLON = 41,
PK_KEY_BACKSLASH = 42, PK_KEY_COMMA = 43, PK_KEY_SLASH = 44, PK_KEY_N = 45, PK_KEY_M = 46, PK_KEY_PERIOD = 47,
PK_KEY_TAB = 48, PK_KEY_SPACE = 49, PK_KEY_GRAVE = 50, PK_KEY_DELETE = 51, PK_KEY_ESC = 53,
PK_KEY_F1 = 122, PK_KEY_F2 = 120, PK_KEY_F3 = 99, PK_KEY_F4 = 118, PK_KEY_F5 = 96, PK_KEY_F6 = 97,
PK_KEY_F7 = 98, PK_KEY_F8 = 100, PK_KEY_F9 = 101, PK_KEY_F10 = 109, PK_KEY_F11 = 103, PK_KEY_F12 = 111,
PK_KEY_LEFT = 123, PK_KEY_RIGHT = 124, PK_KEY_DOWN = 125, PK_KEY_UP = 126,
PK_KEY_KEYPAD_PLUS = 69, PK_KEY_KEYPAD_MINUS = 78, PK_KEY_KEYPAD_ENTER = 76
};

#define PK_MAX_KEYS 4 // keys one action can have (an alternate is the keypad's + beside =, say)

typedef struct {
uint16_t key[PK_COUNT][PK_MAX_KEYS]; // key codes of each action, PK_KEY_NONE past the last
} pad_kbd;

static const char *const pk_names[PK_COUNT] = {
"A", "B", "X", "Y", "LS", "RS", "L", "R", "ZL", "ZR", "PLUS", "MINUS", "LEFT", "UP", "RIGHT", "DOWN",
"LSTICK_UP", "LSTICK_DOWN", "LSTICK_LEFT", "LSTICK_RIGHT", "RSTICK_UP", "RSTICK_DOWN", "RSTICK_LEFT", "RSTICK_RIGHT", "SHAKE"
};

typedef struct { const char *name; uint16_t code; } pk_keyname;
static const pk_keyname pk_keynames[] = {
{"A", PK_KEY_A}, {"S", PK_KEY_S}, {"D", PK_KEY_D}, {"F", PK_KEY_F}, {"H", PK_KEY_H}, {"G", PK_KEY_G},
{"Z", PK_KEY_Z}, {"X", PK_KEY_X}, {"C", PK_KEY_C}, {"V", PK_KEY_V}, {"B", PK_KEY_B}, {"Q", PK_KEY_Q},
{"W", PK_KEY_W}, {"E", PK_KEY_E}, {"R", PK_KEY_R}, {"Y", PK_KEY_Y}, {"T", PK_KEY_T}, {"O", PK_KEY_O},
{"U", PK_KEY_U}, {"I", PK_KEY_I}, {"P", PK_KEY_P}, {"L", PK_KEY_L}, {"J", PK_KEY_J}, {"K", PK_KEY_K},
{"N", PK_KEY_N}, {"M", PK_KEY_M},
{"1", PK_KEY_1}, {"2", PK_KEY_2}, {"3", PK_KEY_3}, {"4", PK_KEY_4}, {"5", PK_KEY_5}, {"6", PK_KEY_6},
{"7", PK_KEY_7}, {"8", PK_KEY_8}, {"9", PK_KEY_9}, {"0", PK_KEY_0},
{"-", PK_KEY_MINUS}, {"MINUS", PK_KEY_MINUS}, {"=", PK_KEY_EQUAL}, {"EQUAL", PK_KEY_EQUAL},
{"[", PK_KEY_LBRACKET}, {"LBRACKET", PK_KEY_LBRACKET}, {"]", PK_KEY_RBRACKET}, {"RBRACKET", PK_KEY_RBRACKET},
{";", PK_KEY_SEMICOLON}, {"SEMICOLON", PK_KEY_SEMICOLON}, {"'", PK_KEY_QUOTE}, {"QUOTE", PK_KEY_QUOTE},
{",", PK_KEY_COMMA}, {"COMMA", PK_KEY_COMMA}, {".", PK_KEY_PERIOD}, {"PERIOD", PK_KEY_PERIOD},
{"/", PK_KEY_SLASH}, {"SLASH", PK_KEY_SLASH}, {"\\", PK_KEY_BACKSLASH}, {"BACKSLASH", PK_KEY_BACKSLASH},
{"`", PK_KEY_GRAVE}, {"GRAVE", PK_KEY_GRAVE},
{"UP", PK_KEY_UP}, {"DOWN", PK_KEY_DOWN}, {"LEFT", PK_KEY_LEFT}, {"RIGHT", PK_KEY_RIGHT},
{"SPACE", PK_KEY_SPACE}, {"RETURN", PK_KEY_RETURN}, {"ENTER", PK_KEY_RETURN}, {"TAB", PK_KEY_TAB},
{"ESC", PK_KEY_ESC}, {"ESCAPE", PK_KEY_ESC}, {"DELETE", PK_KEY_DELETE}, {"BACKSPACE", PK_KEY_DELETE},
{"F1", PK_KEY_F1}, {"F2", PK_KEY_F2}, {"F3", PK_KEY_F3}, {"F4", PK_KEY_F4}, {"F5", PK_KEY_F5}, {"F6", PK_KEY_F6},
{"F7", PK_KEY_F7}, {"F8", PK_KEY_F8}, {"F9", PK_KEY_F9}, {"F10", PK_KEY_F10}, {"F11", PK_KEY_F11}, {"F12", PK_KEY_F12},
{"KEYPAD_PLUS", PK_KEY_KEYPAD_PLUS}, {"KEYPAD_MINUS", PK_KEY_KEYPAD_MINUS}, {"KEYPAD_ENTER", PK_KEY_KEYPAD_ENTER}
};

static int pk_name_eq(const char *s, size_t n, const char *name) {
return strlen(name) == n && !strncasecmp(s, name, n);
}

// An action by name: its own, or an LS_UP / RS_UP shorthand for LSTICK_UP / RSTICK_UP
static int pk_action_of(const char *s, size_t n) {
for (int a = 0; a < PK_COUNT; a++) if (pk_name_eq(s, n, pk_names[a])) return a;
if (n > 3 && n < 32) {
char buf[32];
if (!strncasecmp(s, "LS_", 3)) snprintf(buf, sizeof buf, "LSTICK_%.*s", (int)(n - 3), s + 3);
else if (!strncasecmp(s, "RS_", 3)) snprintf(buf, sizeof buf, "RSTICK_%.*s", (int)(n - 3), s + 3);
else return -1;
for (int a = 0; a < PK_COUNT; a++) if (!strcasecmp(buf, pk_names[a])) return a;
}
return -1;
}

static int pk_key_of(const char *s, size_t n) {
for (size_t i = 0; i < sizeof pk_keynames / sizeof pk_keynames[0]; i++)
if (pk_name_eq(s, n, pk_keynames[i].name)) return pk_keynames[i].code;
return -1;
}

// One action's keys: a key code goes to the action that names it last, so it leaves any other action that had it
static void pk_clear(pad_kbd *k, int a) { for (int i = 0; i < PK_MAX_KEYS; i++) k->key[a][i] = PK_KEY_NONE; }
static void pk_drop_key(pad_kbd *k, uint16_t code) {
for (int a = 0; a < PK_COUNT; a++)
for (int i = 0; i < PK_MAX_KEYS; i++)
if (k->key[a][i] == code) {
for (int j = i; j + 1 < PK_MAX_KEYS; j++) k->key[a][j] = k->key[a][j + 1];
k->key[a][PK_MAX_KEYS - 1] = PK_KEY_NONE;
i--;
}
}
static void pk_put(pad_kbd *k, int a, uint16_t code) {
pk_drop_key(k, code);
for (int i = 0; i < PK_MAX_KEYS; i++) if (k->key[a][i] == PK_KEY_NONE) { k->key[a][i] = code; return; }
}

static void pad_kbd_init(pad_kbd *k, const char *map) {
memset(k, 0xFF, sizeof *k); // every key PK_KEY_NONE
pk_put(k, PK_A, PK_KEY_J); pk_put(k, PK_B, PK_KEY_K); pk_put(k, PK_X, PK_KEY_U); pk_put(k, PK_Y, PK_KEY_I);
pk_put(k, PK_LS, PK_KEY_F); pk_put(k, PK_RS, PK_KEY_G);
pk_put(k, PK_L, PK_KEY_Q); pk_put(k, PK_R, PK_KEY_E); pk_put(k, PK_ZL, PK_KEY_Z); pk_put(k, PK_ZR, PK_KEY_C);
pk_put(k, PK_PLUS, PK_KEY_RETURN); pk_put(k, PK_MINUS, PK_KEY_TAB);
pk_put(k, PK_LEFT, PK_KEY_LEFT); pk_put(k, PK_UP, PK_KEY_UP); pk_put(k, PK_RIGHT, PK_KEY_RIGHT); pk_put(k, PK_DOWN, PK_KEY_DOWN);
pk_put(k, PK_LUP, PK_KEY_W); pk_put(k, PK_LDOWN, PK_KEY_S); pk_put(k, PK_LLEFT, PK_KEY_A); pk_put(k, PK_LRIGHT, PK_KEY_D);
pk_put(k, PK_SHAKE, PK_KEY_SPACE); // the right stick has no key of its own: Shift + an arrow is it (front_mac.m)
for (const char *e = map; e && *e; ) {
const char *comma = strchr(e, ',');
size_t n = comma ? (size_t)(comma - e) : strlen(e);
const char *eq = memchr(e, '=', n);
if (eq) {
int a = pk_action_of(e, (size_t)(eq - e));
if (a >= 0) {
pk_clear(k, a);
const char *v = eq + 1, *end = e + n;
while (v < end) {
const char *bar = memchr(v, '|', (size_t)(end - v));
size_t kn = bar ? (size_t)(bar - v) : (size_t)(end - v);
int code = pk_key_of(v, kn);
if (code >= 0) pk_put(k, a, (uint16_t)code);
v = bar ? bar + 1 : end;
}
}
}
e = comma ? comma + 1 : e + n;
}
}

// A D-pad action taken as the right stick instead: an arrow key held with Shift is the camera on a keyboard with no right stick of its own (front_mac.m).
// The D-pad's actions are in LEFT UP RIGHT DOWN order and the right stick's in UP DOWN LEFT RIGHT, so they cannot be offset from each other
static int pad_kbd_as_right_stick(int a) {
switch (a) {
case PK_LEFT: return PK_RLEFT; case PK_UP: return PK_RUP;
case PK_RIGHT: return PK_RRIGHT; case PK_DOWN: return PK_RDOWN;
}
return a;
}

// The action a key code presses, -1 for none
static int pad_kbd_action(const pad_kbd *k, unsigned code) {
for (int a = 0; a < PK_COUNT; a++)
for (int i = 0; i < PK_MAX_KEYS; i++)
if (k->key[a][i] == code) return a;
return -1;
}
Loading
Loading