Skip to content

Commit 3cd0d50

Browse files
committed
Translate touch gestures in the native Red Alert hosts
1 parent 46a5f20 commit 3cd0d50

4 files changed

Lines changed: 246 additions & 0 deletions

File tree

‎dm/ios/RA2Native/README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ port still needs the game's file-write paths, touch input, lifecycle handling,
4242
and a reliable way back to the library. The desktop main function is not yet
4343
an application integration API.
4444

45+
For the private iOS SDL2 build, `prepare_touch_overlay.py` generates a reviewed
46+
replacement for `SDL_uikitview.m`; compile it with this directory on the include
47+
path and the original UIKit source directory on the quote include path. Replace
48+
only that object in a copy of the SDL2 archive used by the two Red Alert
49+
frameworks. The overlay maps taps to left click, drags to box selection, and
50+
stationary two-finger taps to right click at their midpoint. It passes actual
51+
mouse and trackpad input to the original SDL path. This affects neither the
52+
BoxedWine fallback nor Switch games. Device gesture usability still needs review.
53+
4554
Device evidence on M2 / iPadOS 16.1 so far: dynamic 4 GiB guest address-space
4655
reservation and heap checks, native framework loading and entry resolution,
4756
and actual CoreText file discovery plus FreeType Latin/Chinese text raster.

‎dm/ios/RA2Native/TouchPointer.h‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef TONTON_RA_TOUCH_POINTER_H
2+
#define TONTON_RA_TOUCH_POINTER_H
3+
4+
#include <stdint.h>
5+
6+
enum ra_touch_phase { RA_BEGIN, RA_MOVE, RA_END, RA_CANCEL };
7+
enum ra_pointer_event { RA_POINTER_MOVE, RA_LEFT_DOWN, RA_LEFT_UP, RA_RIGHT_DOWN, RA_RIGHT_UP };
8+
typedef void (*ra_pointer_emit)(void *, enum ra_pointer_event, float, float);
9+
10+
typedef struct {
11+
uintptr_t first, second;
12+
float x, y, x2, y2;
13+
unsigned chord : 1, drag : 1, cancelled : 1;
14+
} ra_touch_pointer;
15+
16+
static inline int ra_touch_moved(float x0, float y0, float x, float y) {
17+
const float dx = x - x0, dy = y - y0;
18+
return dx * dx + dy * dy >= 64.0f;
19+
}
20+
21+
/* Points are UIKit points, not pixels. Wait for a tap or a deliberate drag
22+
* before emitting a left click so a two-finger command never selects a unit. */
23+
static inline void ra_touch_update(ra_touch_pointer *s, ra_pointer_emit emit, void *ctx,
24+
uintptr_t id, enum ra_touch_phase phase, float x, float y) {
25+
if (phase == RA_BEGIN) {
26+
if (!s->first && !s->second) {
27+
s->first = id;
28+
s->x = x;
29+
s->y = y;
30+
} else if (!s->chord && !s->drag && s->first != id) {
31+
s->second = id;
32+
s->chord = 1;
33+
s->x2 = x;
34+
s->y2 = y;
35+
} else if (s->chord && id != s->first && id != s->second) {
36+
s->cancelled = 1;
37+
}
38+
return;
39+
}
40+
if (!id || (id != s->first && id != s->second)) return;
41+
42+
if (s->chord) {
43+
const int first = id == s->first;
44+
if (phase == RA_CANCEL || ra_touch_moved(first ? s->x : s->x2, first ? s->y : s->y2, x, y))
45+
s->cancelled = 1;
46+
if (phase == RA_MOVE) return;
47+
if (first) s->first = 0;
48+
else s->second = 0;
49+
if (s->first || s->second) return;
50+
if (!s->cancelled) {
51+
const float cx = (s->x + s->x2) * 0.5f, cy = (s->y + s->y2) * 0.5f;
52+
emit(ctx, RA_POINTER_MOVE, cx, cy);
53+
emit(ctx, RA_RIGHT_DOWN, cx, cy);
54+
emit(ctx, RA_RIGHT_UP, cx, cy);
55+
}
56+
} else {
57+
int moved_to_touch = 0;
58+
if (!s->drag && phase != RA_CANCEL &&
59+
(phase == RA_END || ra_touch_moved(s->x, s->y, x, y))) {
60+
const int moved = ra_touch_moved(s->x, s->y, x, y);
61+
const float down_x = moved ? s->x : x, down_y = moved ? s->y : y;
62+
emit(ctx, RA_POINTER_MOVE, down_x, down_y);
63+
emit(ctx, RA_LEFT_DOWN, down_x, down_y);
64+
s->drag = 1;
65+
moved_to_touch = !moved;
66+
}
67+
if (s->drag && phase != RA_CANCEL && !moved_to_touch)
68+
emit(ctx, RA_POINTER_MOVE, x, y);
69+
if (phase == RA_MOVE) return;
70+
if (s->drag) emit(ctx, RA_LEFT_UP, x, y);
71+
}
72+
*s = (ra_touch_pointer){0};
73+
}
74+
75+
static inline void ra_touch_reset(ra_touch_pointer *s, ra_pointer_emit emit, void *ctx) {
76+
if (s->drag) emit(ctx, RA_LEFT_UP, s->x, s->y);
77+
*s = (ra_touch_pointer){0};
78+
}
79+
80+
#endif
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Generate a UIKit SDL view for the two private native Red Alert hosts."""
2+
3+
import argparse
4+
import hashlib
5+
from pathlib import Path
6+
7+
8+
VIEW_SHA256 = "e1b9c46b50c5a2199ee5cfc35b3cac0b25df781d6cdea4ef0140b99251ed1326"
9+
BRIDGE = """#if !TARGET_OS_TV
10+
static void RAEmitPointer(void *context, enum ra_pointer_event event, float x, float y)
11+
{
12+
SDL_Window *window = context;
13+
if (!window || window->w < 1 || window->h < 1) return;
14+
if (event == RA_POINTER_MOVE) {
15+
SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0,
16+
(int)SDL_clamp(x, 0, window->w - 1),
17+
(int)SDL_clamp(y, 0, window->h - 1));
18+
return;
19+
}
20+
SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID,
21+
(event == RA_LEFT_DOWN || event == RA_RIGHT_DOWN) ? SDL_PRESSED : SDL_RELEASED,
22+
(event == RA_RIGHT_DOWN || event == RA_RIGHT_UP) ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT);
23+
}
24+
#endif
25+
26+
"""
27+
METHOD = """#if !TARGET_OS_TV
28+
- (BOOL)raHandleDirectTouch:(UITouch *)touch phase:(enum ra_touch_phase)phase
29+
{
30+
if (touch.type != UITouchTypeDirect) return NO;
31+
if (!sdlwindow) return YES;
32+
CGPoint point = [self touchLocation:touch shouldNormalize:NO];
33+
CGSize bounds = self.bounds.size;
34+
if (bounds.width > 0 && bounds.height > 0) {
35+
/* Keep the gesture threshold in UIKit points. SDL maps the synthetic
36+
* mouse position from window units to the game's letterboxed area. */
37+
const float sx = sdlwindow->w / bounds.width, sy = sdlwindow->h / bounds.height;
38+
if (sx > 0 && sy > 0) {
39+
struct RAWindowScale scale = {sdlwindow, sx, sy};
40+
ra_touch_update(&raPointer, RAEmitScaledPointer, &scale,
41+
(uintptr_t)(__bridge void *)touch, phase, point.x, point.y);
42+
}
43+
} else if (phase == RA_CANCEL || phase == RA_END) {
44+
ra_touch_reset(&raPointer, RAEmitPointer, sdlwindow);
45+
}
46+
return YES;
47+
}
48+
#endif
49+
50+
"""
51+
52+
53+
def replace_one(text, old, new):
54+
if text.count(old) != 1:
55+
raise ValueError(f"Expected exactly one UIKit source anchor: {old}")
56+
return text.replace(old, new, 1)
57+
58+
59+
def make_overlay(raw):
60+
if hashlib.sha256(raw).hexdigest() != VIEW_SHA256:
61+
raise ValueError("UIKit source differs from the reviewed SDL view")
62+
text = raw.decode()
63+
text = replace_one(text, '#include "SDL_uikitwindow.h"',
64+
'#include "SDL_uikitwindow.h"\n#include "TouchPointer.h"')
65+
scaled = """struct RAWindowScale { SDL_Window *window; float x, y; };
66+
static void RAEmitScaledPointer(void *context, enum ra_pointer_event event, float x, float y)
67+
{
68+
struct RAWindowScale *s = context;
69+
RAEmitPointer(s->window, event, x * s->x, y * s->y);
70+
}
71+
"""
72+
bridge = replace_one(BRIDGE, "#endif\n", scaled + "#endif\n")
73+
text = replace_one(text, "@implementation SDL_uikitview {",
74+
bridge + "@implementation SDL_uikitview {\n ra_touch_pointer raPointer;")
75+
text = replace_one(text, " /* Remove ourself from the old window. */",
76+
"#if !TARGET_OS_TV\n ra_touch_reset(&raPointer, RAEmitPointer, sdlwindow);\n#endif\n"
77+
" /* Remove ourself from the old window. */")
78+
for name, phase in (("Began", "BEGIN"), ("Moved", "MOVE"), ("Ended", "END")):
79+
sig = f"- (void)touches{name}:(NSSet *)touches withEvent:(UIEvent *)event"
80+
start = sig + "\n{\n for (UITouch *touch in touches) {\n"
81+
guard = f"#if !TARGET_OS_TV\n if ([self raHandleDirectTouch:touch phase:RA_{phase}]) continue;\n#endif\n"
82+
text = replace_one(text, start, (METHOD if name == "Began" else "") + start + guard)
83+
text = replace_one(text, " [self touchesEnded:touches withEvent:event];",
84+
"""#if !TARGET_OS_TV
85+
NSMutableSet *remaining = [NSMutableSet setWithCapacity:touches.count];
86+
for (UITouch *touch in touches) {
87+
if (![self raHandleDirectTouch:touch phase:RA_CANCEL]) [remaining addObject:touch];
88+
}
89+
[self touchesEnded:remaining withEvent:event];
90+
#else
91+
[self touchesEnded:touches withEvent:event];
92+
#endif""")
93+
return text
94+
95+
96+
if __name__ == "__main__":
97+
parser = argparse.ArgumentParser(description=__doc__)
98+
parser.add_argument("source", type=Path)
99+
parser.add_argument("overlay", type=Path)
100+
args = parser.parse_args()
101+
output = make_overlay(args.source.read_bytes())
102+
with args.overlay.open("x", encoding="utf-8") as f:
103+
f.write(output)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "TouchPointer.h"
2+
#include <assert.h>
3+
4+
typedef struct { enum ra_pointer_event event; float x, y; } observation;
5+
static observation observed[16];
6+
static unsigned count;
7+
8+
static void observe(void *unused, enum ra_pointer_event event, float x, float y) {
9+
(void)unused;
10+
assert(count < sizeof observed / sizeof *observed);
11+
observed[count++] = (observation){event, x, y};
12+
}
13+
14+
int main(void) {
15+
ra_touch_pointer state = {0};
16+
#define F(id, phase, x, y) ra_touch_update(&state, observe, 0, id, RA_##phase, x, y)
17+
F(1, BEGIN, 20, 10);
18+
F(1, MOVE, 21, 11);
19+
assert(count == 0);
20+
F(1, END, 21, 11);
21+
assert(count == 3 && observed[1].event == RA_LEFT_DOWN && observed[2].event == RA_LEFT_UP);
22+
assert(observed[0].x == 21 && observed[0].y == 11);
23+
24+
count = 0;
25+
F(1, BEGIN, 20, 10);
26+
F(1, MOVE, 45, 11);
27+
assert(count == 3 && observed[0].x == 20 && observed[1].event == RA_LEFT_DOWN);
28+
assert(observed[2].event == RA_POINTER_MOVE && observed[2].x == 45);
29+
F(2, BEGIN, 50, 11); /* A stray finger must not interrupt the box selection. */
30+
F(2, END, 50, 11);
31+
F(1, CANCEL, 45, 11);
32+
assert(count == 4 && observed[3].event == RA_LEFT_UP);
33+
34+
count = 0;
35+
F(1, BEGIN, 20, 10);
36+
F(2, BEGIN, 40, 20);
37+
F(1, END, 20, 10);
38+
assert(count == 0);
39+
F(2, END, 40, 20);
40+
assert(count == 3 && observed[1].event == RA_RIGHT_DOWN && observed[2].event == RA_RIGHT_UP);
41+
assert(observed[0].x == 30 && observed[0].y == 15);
42+
43+
count = 0;
44+
F(1, BEGIN, 20, 10);
45+
F(2, BEGIN, 40, 20);
46+
F(2, MOVE, 70, 20);
47+
F(2, END, 70, 20);
48+
F(1, END, 20, 10);
49+
assert(count == 0); /* A two-finger pan is not a right click. */
50+
F(1, BEGIN, 20, 10);
51+
F(1, CANCEL, 20, 10);
52+
assert(count == 0);
53+
return 0;
54+
}

0 commit comments

Comments
 (0)