|
| 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) |
0 commit comments