@geastack/apple 0.2.34 → 0.2.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,224 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ #include "watchos_bridge.h"
4
+
5
+ #include "app.h"
6
+ #include "canvas.h"
7
+ #include "css/declarative.h"
8
+ #include "display.h"
9
+ #include "event.h"
10
+ #include "events.h"
11
+ #include "pixel.h"
12
+ #include "ui/style.h"
13
+ #include "ui/tree_internal.h"
14
+
15
+ #include <algorithm>
16
+ #include <cmath>
17
+ #include <cstddef>
18
+ #include <cstdint>
19
+
20
+ // The watchOS shell reuses the iOS host backends for everything below the
21
+ // window: the RGBA8888 framebuffer display (ios_display.mm), the frame clock
22
+ // (ios_timers.mm), touch state and CoreMotion (ios_sensors.mm). watchOS has no
23
+ // UIView hierarchy an app can own, so there is no native view renderer here;
24
+ // gea paints the whole UI into the framebuffer and SwiftUI shows it as an image.
25
+ extern "C" int gea_embedded_now_ms(void);
26
+ extern "C" void gea_ios_display_set_viewport_size(int width, int height);
27
+ extern "C" const std::uint32_t *gea_ios_display_presented_pixels();
28
+ extern "C" std::uint32_t gea_ios_display_presented_generation();
29
+ extern "C" void gea_ios_touch_set_state(int touching, int x, int y);
30
+
31
+ namespace {
32
+
33
+ bool g_started = false;
34
+ int g_width_px = 0;
35
+ int g_height_px = 0;
36
+ double g_scale = 1.0;
37
+ std::uint32_t g_last_generation = 0;
38
+ // Sub-pixel Crown rotation not yet applied as scroll.
39
+ double g_crown_pending_px = 0.0;
40
+
41
+ int clampToCanvas(double value, int extent)
42
+ {
43
+ const int rounded = static_cast<int>(std::lround(value));
44
+ return std::clamp(rounded, 0, std::max(0, extent - 1));
45
+ }
46
+
47
+ // Where a watch screen keeps content clear of the system clock and its rounded
48
+ // corners, in panel pixels. watchOS exposes neither the corner shape nor the
49
+ // clock's extent, and its safe area reserves far more (53pt of a 248pt-tall
50
+ // 46mm screen at the top alone), so these are measured per panel size: corners
51
+ // from each Simulator device type's framebufferMask, the clock from a blank app
52
+ // on watchOS 26.5. `top` clears the clock with a 4px margin; `side` is how far
53
+ // the corner cuts in at that depth; `bottom` is where the bottom corner cuts in
54
+ // no further than `side`; `radius` is how deep the corner curve runs.
55
+ struct WatchScreenInsets {
56
+ int width, height;
57
+ int top, side, bottom, radius;
58
+ };
59
+
60
+ constexpr WatchScreenInsets kWatchScreenInsets[] = {
61
+ {324, 394, 46, 2, 45, 57}, // 40mm SE, Series 4-6
62
+ {352, 430, 56, 4, 54, 78}, // 41mm Series 7-9
63
+ {368, 448, 54, 3, 52, 72}, // 44mm SE, Series 4-6
64
+ {374, 446, 60, 6, 60, 94}, // 42mm Series 10+
65
+ {396, 484, 62, 4, 59, 85}, // 45mm Series 7-9
66
+ {410, 502, 70, 9, 69, 126}, // 49mm Ultra, Ultra 2
67
+ {416, 496, 67, 7, 67, 108}, // 46mm Series 10+
68
+ {422, 514, 72, 10, 71, 135}, // 49mm Ultra 3
69
+ };
70
+
71
+ // An unlisted panel borrows the nearest listed one's insets, scaled by height.
72
+ WatchScreenInsets insetsForPanel(int width, int height)
73
+ {
74
+ const WatchScreenInsets *nearest = &kWatchScreenInsets[0];
75
+ for (const auto &entry : kWatchScreenInsets) {
76
+ if (entry.width == width && entry.height == height) return entry;
77
+ if (std::abs(entry.height - height) < std::abs(nearest->height - height)) nearest = &entry;
78
+ }
79
+ const double k = static_cast<double>(height) / nearest->height;
80
+ auto scaled = [k](int v) { return static_cast<int>(std::lround(v * k)); };
81
+ return {width, height, scaled(nearest->top), scaled(nearest->side), scaled(nearest->bottom), scaled(nearest->radius)};
82
+ }
83
+
84
+ // Publishes the insets as `:root` custom properties in CSS px, registered ahead
85
+ // of the app's own stylesheet so they resolve on the first styled mount. Apps
86
+ // read them with a fallback for other targets: `var(--gea-safe-top, 4px)`.
87
+ void registerScreenInsetProperties(int width, int height)
88
+ {
89
+ using namespace gea::embedded::ui;
90
+ const WatchScreenInsets insets = insetsForPanel(width, height);
91
+ const double cssPx = GEA_EMBEDDED_CSS_LAYOUT_DEVICE_PIXEL_RATIO;
92
+ auto publish = [cssPx](const char *name, int panelPx) {
93
+ StyleSheet::instance().registerStaticCustomLengthRule(StaticStyleSelectorKind::Selector, ":root", name,
94
+ {StaticStyleLengthUnit::Px, static_cast<float>(panelPx / cssPx)}, "");
95
+ };
96
+ publish("--gea-safe-top", insets.top);
97
+ publish("--gea-safe-right", insets.side);
98
+ publish("--gea-safe-bottom", insets.bottom);
99
+ publish("--gea-safe-left", insets.side);
100
+ publish("--gea-screen-radius", insets.radius);
101
+ }
102
+
103
+ } // namespace
104
+
105
+ extern "C" void gea_watchos_start(int widthPx, int heightPx, double scale)
106
+ {
107
+ // Application::init runs the app's top level, so it happens once; a watch
108
+ // screen never changes size under a running app.
109
+ if (g_started || widthPx <= 0 || heightPx <= 0) return;
110
+ g_width_px = widthPx;
111
+ g_height_px = heightPx;
112
+ g_scale = scale > 0 ? scale : 1.0;
113
+ gea_ios_display_set_viewport_size(widthPx, heightPx);
114
+ gea::platform::display::Display::init();
115
+ registerScreenInsetProperties(widthPx, heightPx);
116
+ // The canvas is physical pixels, so `100vw` is the full screen and text is
117
+ // rasterised at panel resolution. CSS `px` scale by the build's layout
118
+ // ratio, which the font atlases were baked at too.
119
+ gea::framework::app::Application::init(widthPx, heightPx, GEA_EMBEDDED_CSS_LAYOUT_DEVICE_PIXEL_RATIO);
120
+ g_started = true;
121
+ g_last_generation = gea_ios_display_presented_generation() - 1;
122
+ }
123
+
124
+ extern "C" int gea_watchos_frame(void)
125
+ {
126
+ if (!g_started) return 0;
127
+ const int nowMs = gea_embedded_now_ms();
128
+ gea::framework::app::Application::frame(nowMs);
129
+ // Drive the CSS animation clock the way the embedded runtime loop
130
+ // (core/runtime.cpp) and macos_main.mm do; this shell does not compile that
131
+ // loop, so without it `data-anim` and @keyframes animations never start and
132
+ // every animated app freezes on its first frame. Declarative animations are
133
+ // started once, after the app has mounted.
134
+ static bool declarativeAnimationsStarted = false;
135
+ if (!declarativeAnimationsStarted && gea::embedded::ui::Tree::instance().mountedRoot() >= 0) {
136
+ declarativeAnimationsStarted = true;
137
+ gea::css::DeclarativeAnimations::scanAndStart(static_cast<std::uint32_t>(nowMs));
138
+ }
139
+ gea::embedded::ui::StyleSheet::instance().startCssAnimations(static_cast<std::uint32_t>(nowMs));
140
+ gea::css::AnimationEngine::instance().tick(static_cast<std::uint32_t>(nowMs));
141
+ const std::uint32_t generation = gea_ios_display_presented_generation();
142
+ if (generation == g_last_generation) return 0;
143
+ g_last_generation = generation;
144
+ return 1;
145
+ }
146
+
147
+ extern "C" CGImageRef gea_watchos_copy_frame_image(void)
148
+ {
149
+ if (!g_started) return nullptr;
150
+ auto *canvas = gea::platform::display::Display::canvas();
151
+ const std::uint32_t *pixels = gea_ios_display_presented_pixels();
152
+ if (!canvas || !pixels) return nullptr;
153
+ const int w = canvas->width();
154
+ const int h = canvas->height();
155
+ if (w <= 0 || h <= 0) return nullptr;
156
+
157
+ // The framebuffer keeps changing under the next frame, so the image owns a
158
+ // copy. Bytes are R,G,B,A per pixel -- the same layout ios_main.mm draws.
159
+ const std::size_t bytes = static_cast<std::size_t>(w) * static_cast<std::size_t>(h) * 4;
160
+ CFDataRef data = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(pixels), static_cast<CFIndex>(bytes));
161
+ if (!data) return nullptr;
162
+ CGDataProviderRef provider = CGDataProviderCreateWithCFData(data);
163
+ CFRelease(data);
164
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
165
+ const uint32_t bitmapInfo = static_cast<uint32_t>(kCGImageAlphaNoneSkipLast) |
166
+ static_cast<uint32_t>(kCGBitmapByteOrder32Big);
167
+ CGImageRef image = CGImageCreate(static_cast<std::size_t>(w), static_cast<std::size_t>(h),
168
+ 8, 32, static_cast<std::size_t>(w) * 4,
169
+ colorSpace, bitmapInfo, provider, nullptr, false,
170
+ kCGRenderingIntentDefault);
171
+ CGColorSpaceRelease(colorSpace);
172
+ CGDataProviderRelease(provider);
173
+ return image;
174
+ }
175
+
176
+ extern "C" void gea_watchos_touch(GeaWatchTouchPhase phase, double xPoints, double yPoints)
177
+ {
178
+ using gea::framework::events::TouchPhase;
179
+ if (!g_started) return;
180
+ TouchPhase touchPhase = TouchPhase::Move;
181
+ switch (phase) {
182
+ case GeaWatchTouchDown: touchPhase = TouchPhase::Down; break;
183
+ case GeaWatchTouchMove: touchPhase = TouchPhase::Move; break;
184
+ case GeaWatchTouchUp: touchPhase = TouchPhase::Up; break;
185
+ }
186
+ const bool touching = touchPhase != TouchPhase::Up;
187
+ const int x = clampToCanvas(xPoints * g_scale, g_width_px);
188
+ const int y = clampToCanvas(yPoints * g_scale, g_height_px);
189
+ gea_ios_touch_set_state(touching ? 1 : 0, x, y);
190
+
191
+ gea::framework::events::Event event{};
192
+ event.type = gea::framework::events::EventType::Touch;
193
+ event.touchPhase = touchPhase;
194
+ event.touching = touching;
195
+ event.x = x;
196
+ event.y = y;
197
+ event.pointerId = 0;
198
+ gea::framework::events::TouchRuntime::dispatchEvent(event);
199
+ }
200
+
201
+ extern "C" void gea_watchos_crown(double deltaPoints)
202
+ {
203
+ if (!g_started) return;
204
+ // The Crown scrolls like the Up/Down buttons on touchless boards: the first
205
+ // vertical scroll container, through the same scroll path as a touch drag,
206
+ // so <virtual-list> slots recycle as they do under a finger. Scroll offsets
207
+ // are canvas pixels; fractions carry over to the next rotation event.
208
+ g_crown_pending_px += deltaPoints * g_scale;
209
+ const int dy = static_cast<int>(g_crown_pending_px);
210
+ if (dy == 0) return;
211
+ g_crown_pending_px -= dy;
212
+ if (!gea::embedded::ui::Tree::instance().scrollByKeyStep(dy)) g_crown_pending_px = 0.0;
213
+ }
214
+
215
+ extern "C" int gea_watchos_scroll_metrics(double *topPoints, double *maxPoints)
216
+ {
217
+ if (!g_started) return 0;
218
+ int top = 0;
219
+ int max = 0;
220
+ if (!gea::embedded::ui::Tree::instance().keyStepScrollMetrics(&top, &max)) return 0;
221
+ *topPoints = top / g_scale;
222
+ *maxPoints = max / g_scale;
223
+ return 1;
224
+ }
@@ -0,0 +1,95 @@
1
+ #import <WatchKit/WatchKit.h>
2
+
3
+ #include "camera.h"
4
+ #include "power.h"
5
+
6
+ #include <cmath>
7
+ #include <string>
8
+
9
+ // Platform services iOS has and the watch either lacks or provides
10
+ // differently: there is no camera, and battery and haptics come from WatchKit.
11
+
12
+ namespace gea::platform::camera {
13
+
14
+ // A watch has no camera. The host bridge (host/camera.cpp) still links, so apps
15
+ // that use one build and simply see `isAvailable() == false`, the same as a
16
+ // board without a sensor; a <camera> element renders nothing.
17
+ bool Camera::isAvailable() { return false; }
18
+ bool Camera::hasPermission() { return false; }
19
+ bool Camera::requestPermission() { return false; }
20
+ bool Camera::open(const std::string &, int, int) { return false; }
21
+ void Camera::close() {}
22
+ bool Camera::isOpen() { return false; }
23
+ int Camera::width() { return 0; }
24
+ int Camera::height() { return 0; }
25
+ int Camera::orientation() { return 0; }
26
+ Facing Camera::currentFacing() { return Facing::Back; }
27
+ std::string Camera::currentFacingString() { return "back"; }
28
+ int Camera::deviceCount() { return 0; }
29
+ DeviceInfo Camera::deviceAt(int) { return {}; }
30
+ void Camera::drawPreview(int, int, int, int) {}
31
+ int Camera::previewMode() { return 0; }
32
+ bool Camera::fillPreview(gea::framework::graphics::pixel::native_t *, int, int, int, bool) { return false; }
33
+ void Camera::positionPreviewLayer(int, int, int, int) {}
34
+ void Camera::hidePreviewLayer() {}
35
+ void Camera::presentNativeOverlay() {}
36
+ int Camera::capture(bool) { return -1; }
37
+ bool Camera::startRecording(const std::string &, double) { return false; }
38
+ double Camera::stopRecording() { return -1.0; }
39
+ bool Camera::isRecording() { return false; }
40
+ void Camera::setFlash(const std::string &) {}
41
+ void Camera::setZoom(double) {}
42
+ void Camera::setMirror(bool) {}
43
+ void Camera::setExposure(const std::string &, double, double, double) {}
44
+ void Camera::setWhiteBalance(const std::string &, double, double) {}
45
+ void Camera::setFocus(const std::string &, double, double) {}
46
+ void Camera::setTorch(const std::string &, double) {}
47
+
48
+ } // namespace gea::platform::camera
49
+
50
+ namespace gea::platform::power {
51
+
52
+ namespace {
53
+
54
+ WKInterfaceDevice *device()
55
+ {
56
+ WKInterfaceDevice *current = WKInterfaceDevice.currentDevice;
57
+ if (!current.batteryMonitoringEnabled) current.batteryMonitoringEnabled = YES;
58
+ return current;
59
+ }
60
+
61
+ } // namespace
62
+
63
+ bool Power::init()
64
+ {
65
+ device();
66
+ return true;
67
+ }
68
+
69
+ // WatchKit reports 0..1, or a negative level when it cannot tell (the
70
+ // simulator); report a full battery then rather than an alarming 0%.
71
+ int Power::batteryPercent()
72
+ {
73
+ const float level = device().batteryLevel;
74
+ if (level < 0) return 100;
75
+ return static_cast<int>(std::lround(level * 100.0f));
76
+ }
77
+
78
+ bool Power::charging()
79
+ {
80
+ const WKInterfaceDeviceBatteryState state = device().batteryState;
81
+ return state == WKInterfaceDeviceBatteryStateCharging || state == WKInterfaceDeviceBatteryStateFull;
82
+ }
83
+
84
+ // The Taptic Engine plays fixed patterns rather than a duration and strength,
85
+ // so a vibration maps to its closest one: a light click for a short buzz, the
86
+ // stronger notification pattern otherwise.
87
+ bool Power::vibrate(double durationMs, int strength)
88
+ {
89
+ if (durationMs <= 0) return false;
90
+ const WKHapticType haptic = (durationMs < 60 && strength < 50) ? WKHapticTypeClick : WKHapticTypeNotification;
91
+ [device() playHaptic:haptic];
92
+ return true;
93
+ }
94
+
95
+ } // namespace gea::platform::power