@geastack/apple 0.2.14 → 0.2.16

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.
Files changed (36) hide show
  1. package/dist/index.js +7 -0
  2. package/generated/ActivityKit.d.ts +3 -0
  3. package/generated/AppKit.d.ts +5 -0
  4. package/generated/CoreGraphics.d.ts +3 -0
  5. package/generated/CoreLocation.d.ts +1 -0
  6. package/generated/CoreMedia.d.ts +1 -0
  7. package/generated/Dispatch.d.ts +2 -0
  8. package/generated/Foundation.d.ts +7 -0
  9. package/generated/MapKit.d.ts +1 -0
  10. package/generated/Metal.d.ts +2 -0
  11. package/generated/Security.d.ts +3 -0
  12. package/generated/SwiftData.d.ts +5 -0
  13. package/generated/UIKit.d.ts +1 -0
  14. package/generated/UserNotifications.d.ts +3 -0
  15. package/package.json +7 -7
  16. package/targets/ios/generate-xcode-project.mjs +5 -0
  17. package/targets/macos/app-compiler-plugins.mjs +22 -0
  18. package/targets/macos/build-macos.sh +108 -6
  19. package/targets/macos/generate-resident-entry.mjs +1 -1
  20. package/targets/macos/main/canvas_view.mm +8 -8
  21. package/targets/macos/main/color_convert.h +3 -3
  22. package/targets/macos/main/color_convert.mm +12 -12
  23. package/targets/macos/main/font_registry.h +2 -0
  24. package/targets/macos/main/font_registry.mm +72 -51
  25. package/targets/macos/main/image_bridge.h +5 -2
  26. package/targets/macos/main/image_bridge.mm +47 -9
  27. package/targets/macos/main/macos_background_test.h +54 -0
  28. package/targets/macos/main/macos_control_admission.h +13 -0
  29. package/targets/macos/main/macos_display.mm +29 -27
  30. package/targets/macos/main/macos_key_event.h +73 -0
  31. package/targets/macos/main/macos_main.mm +410 -14
  32. package/targets/macos/main/macos_renderer.h +5 -0
  33. package/targets/macos/main/macos_renderer.mm +717 -80
  34. package/targets/macos/main/press_bridge.h +21 -3
  35. package/targets/macos/main/press_bridge.mm +126 -26
  36. package/targets/macos/sync-resources.mjs +24 -2
@@ -128,66 +128,64 @@ const std::uint8_t *runtimeTtfBytesForFamilyId(int familyId, unsigned long *leng
128
128
  return it->second.data();
129
129
  }
130
130
 
131
- static NSFont *self_resolveFont(int fontId, CGFloat size);
132
-
133
- NSFont *fontForId(int fontId, int sizePx)
134
- {
135
- CGFloat size = sizePx > 0 ? static_cast<CGFloat>(sizePx) : [NSFont systemFontSize];
136
-
137
- // Cache resolved fonts by (fontId, size). `+[NSFont fontWithName:size:]` and
138
- // the CTFontDescriptor fallback below both do a CoreText font-descriptor
139
- // match, which is expensive — and this is called for EVERY text node on
140
- // EVERY frame (renderer sync + layout measurement). Without this cache,
141
- // CoreText font matching dominated the profile and pegged a CPU core. The
142
- // set of (family, size) pairs an app uses is tiny, so the cache stays small.
143
- static NSMutableDictionary<NSNumber *, NSFont *> *cache;
144
- static dispatch_once_t once;
145
- dispatch_once(&once, ^{ cache = [NSMutableDictionary dictionary]; });
146
- NSNumber *key = @(static_cast<long long>(fontId) * 100000LL + llround(size));
147
- @synchronized(cache) {
148
- NSFont *hit = cache[key];
149
- if (hit) return hit;
150
- }
151
- NSFont *resolved = self_resolveFont(fontId, size);
152
- if (resolved) {
153
- @synchronized(cache) { cache[key] = resolved; }
154
- }
155
- return resolved;
156
- }
157
-
158
- static NSFont *self_resolveFont(int fontId, CGFloat size)
131
+ static NSFont *self_resolveFont(int fontId, int sizePx, int weight, int stretch)
159
132
  {
160
133
  NSString *family = familyForId(fontId);
134
+ const CGFloat size = std::clamp(sizePx > 0 ? sizePx : 13, 1, 4096);
161
135
  if (!family || family.length == 0) return [NSFont systemFontOfSize:size];
162
- // CSS system-font keywords map to the actual system UI font — San Francisco
163
- // on macOS. There is no PostScript face literally named "-apple-system", so
164
- // `+[NSFont fontWithName:@"-apple-system"]` returns nil and we'd otherwise
165
- // fall through to a wrong family. systemFontOfSize: IS San Francisco.
166
136
  NSString *lower = family.lowercaseString;
167
137
  if ([lower isEqualToString:@"-apple-system"] || [lower isEqualToString:@"system-ui"] ||
168
138
  [lower isEqualToString:@"blinkmacsystemfont"] || [lower hasPrefix:@".applesystemuifont"] ||
169
139
  [lower hasPrefix:@".sf"] || [lower isEqualToString:@"san francisco"] ||
170
140
  [lower isEqualToString:@"sf pro"] || [lower isEqualToString:@"sf pro text"]) {
171
- return [NSFont systemFontOfSize:size];
141
+ const CGFloat nativeWeight = weight <= 400 ? (weight - 400) / 1000.0 : (weight - 400) / 800.0;
142
+ return [NSFont systemFontOfSize:size weight:nativeWeight];
172
143
  }
173
- NSFont *font = [NSFont fontWithName:family size:size];
174
- if (font) return font;
175
- // CTFont's family lookup is more forgiving than NSFont's PostScript-name
176
- // lookup — fall back to building a CTFont with the family attribute.
177
- NSDictionary *attrs = @{
178
- (__bridge NSString *)kCTFontFamilyNameAttribute: family,
179
- (__bridge NSString *)kCTFontSizeAttribute: @(size),
180
- };
181
- CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attrs);
182
- CTFontRef ct = CTFontCreateWithFontDescriptor(desc, size, NULL);
183
- CFRelease(desc);
184
- if (ct) {
185
- NSFont *resolved = (__bridge_transfer NSFont *)ct;
186
- return resolved;
144
+ NSDictionary *baseAttributes = @{ (__bridge NSString *)kCTFontFamilyNameAttribute: family };
145
+ CTFontDescriptorRef descriptor = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)baseAttributes);
146
+ CTFontRef base = CTFontCreateWithFontDescriptor(descriptor, size, nullptr);
147
+ CFRelease(descriptor);
148
+ if (!base) return [NSFont systemFontOfSize:size];
149
+ NSMutableDictionary *variation = [NSMutableDictionary dictionary];
150
+ CFArrayRef axes = CTFontCopyVariationAxes(base);
151
+ if (axes) {
152
+ for (NSDictionary *axis in (__bridge NSArray *)axes) {
153
+ NSNumber *identifier = axis[(__bridge NSString *)kCTFontVariationAxisIdentifierKey];
154
+ const uint32_t tag = identifier.unsignedIntValue;
155
+ if (tag != 0x77676874 && tag != 0x77647468) continue; // wght / wdth
156
+ const double requested = tag == 0x77676874 ? weight : stretch / 10.0;
157
+ const double minimum = [axis[(__bridge NSString *)kCTFontVariationAxisMinimumValueKey] doubleValue];
158
+ const double maximum = [axis[(__bridge NSString *)kCTFontVariationAxisMaximumValueKey] doubleValue];
159
+ variation[identifier] = @(std::clamp(requested, minimum, maximum));
160
+ }
161
+ CFRelease(axes);
187
162
  }
188
- return [NSFont systemFontOfSize:size];
163
+ NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
164
+ if (variation.count) attributes[(__bridge NSString *)kCTFontVariationAttribute] = variation;
165
+ else attributes[(__bridge NSString *)kCTFontTraitsAttribute] = @{
166
+ (__bridge NSString *)kCTFontWeightTrait: @((weight - 400) / 800.0)
167
+ };
168
+ CTFontDescriptorRef adjustment = CTFontDescriptorCreateWithAttributes((__bridge CFDictionaryRef)attributes);
169
+ CTFontRef selected = CTFontCreateCopyWithAttributes(base, size, nullptr, adjustment);
170
+ CFRelease(adjustment); CFRelease(base);
171
+ return selected ? (__bridge_transfer NSFont *)selected : [NSFont systemFontOfSize:size];
189
172
  }
190
173
 
174
+ NSFont *fontForId(int fontId, int sizePx, int weight, int stretch)
175
+ {
176
+ weight = std::clamp(weight, 1, 1000);
177
+ stretch = std::clamp(stretch, 1, 32767);
178
+ sizePx = std::clamp(sizePx > 0 ? sizePx : 13, 1, 4096);
179
+ static NSMutableDictionary<NSString *, NSFont *> *cache = [NSMutableDictionary dictionary];
180
+ NSString *key = [NSString stringWithFormat:@"%d:%d:%d:%d", fontId, sizePx, weight, stretch];
181
+ @synchronized(cache) { NSFont *hit = cache[key]; if (hit) return hit; }
182
+ NSFont *selected = self_resolveFont(fontId, sizePx, weight, stretch);
183
+ if (selected) { @synchronized(cache) { if (cache.count >= 1024) [cache removeAllObjects]; cache[key] = selected; } }
184
+ return selected;
185
+ }
186
+
187
+ NSFont *fontForId(int fontId, int sizePx) { return fontForId(fontId, sizePx, 400, 1000); }
188
+
191
189
  int registerCustomTtfDirectory(NSString *directoryPath)
192
190
  {
193
191
  NSFileManager *fm = [NSFileManager defaultManager];
@@ -274,10 +272,10 @@ NSLineBreakMode lineBreakModeForText(NSString *text, NSFont *font, CGFloat maxWi
274
272
  // Host text-measurement hook for the AppKit renderer. The Thermalright target
275
273
  // has no AppKit text renderer; it wants the framework's generated-font metrics.
276
274
  #if !GEA_MACOS_THERMALRIGHT_DISPLAY_TARGET
277
- extern "C" bool gea_host_measure_text(const char *text,
275
+ static bool measureNativeText(const char *text,
278
276
  int maxWidth,
279
277
  int fontId,
280
- int fontSize,
278
+ int fontSize, int fontWeight, int fontStretch, int lineHeight,
281
279
  int *outWidth,
282
280
  int *outHeight)
283
281
  {
@@ -304,6 +302,9 @@ extern "C" bool gea_host_measure_text(const char *text,
304
302
  key.append(std::to_string(fontId));
305
303
  key.push_back('\x1f');
306
304
  key.append(std::to_string(fontSize));
305
+ key.push_back('\x1f'); key.append(std::to_string(fontWeight));
306
+ key.push_back('\x1f'); key.append(std::to_string(fontStretch));
307
+ key.push_back('\x1f'); key.append(std::to_string(lineHeight));
307
308
  {
308
309
  std::scoped_lock guard(measureLock);
309
310
  auto it = measureCache.find(key);
@@ -314,12 +315,13 @@ extern "C" bool gea_host_measure_text(const char *text,
314
315
  }
315
316
  }
316
317
 
317
- NSFont *font = gea::macos::fontForId(fontId, fontSize);
318
+ NSFont *font = gea::macos::fontForId(fontId, fontSize, fontWeight, fontStretch);
318
319
  if (!font) return false;
319
320
  NSString *str = [NSString stringWithUTF8String:text] ?: @"";
320
321
  const CGFloat boundedMax = maxWidth > 0 ? maxWidth : CGFLOAT_MAX;
321
322
  NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
322
323
  paragraph.lineBreakMode = gea::macos::lineBreakModeForText(str, font, boundedMax);
324
+ if (lineHeight > 0) { paragraph.minimumLineHeight = lineHeight; paragraph.maximumLineHeight = lineHeight; }
323
325
  NSDictionary *attrs = @{
324
326
  NSFontAttributeName: font,
325
327
  NSParagraphStyleAttributeName: paragraph,
@@ -347,4 +349,23 @@ extern "C" bool gea_host_measure_text(const char *text,
347
349
  }
348
350
  return true;
349
351
  }
352
+ // Readiness must reject CoreText's successful system fallback for a missing face.
353
+ extern "C" bool gea_host_packaged_font_available(int familyId)
354
+ {
355
+ unsigned long length = 0;
356
+ if (!gea::macos::runtimeTtfBytesForFamilyId(familyId, &length) || !length) return false;
357
+ NSString *requested = gea::macos::familyForId(familyId);
358
+ NSFont *selected = gea::macos::fontForId(familyId, 14, 400, 1000);
359
+ if (!requested || !selected || ![selected.familyName isEqualToString:requested]) return false;
360
+ const UniChar characters[] = {'A', 0x00b5};
361
+ CGGlyph glyphs[2] = {};
362
+ return CTFontGetGlyphsForCharacters((__bridge CTFontRef)selected, characters, glyphs, 2)
363
+ && glyphs[0] != 0 && glyphs[1] != 0;
364
+ }
365
+ extern "C" bool gea_host_measure_text(const char *text, int maxWidth, int fontId, int fontSize, int *width, int *height)
366
+ { return measureNativeText(text, maxWidth, fontId, fontSize, 400, 1000, 0, width, height); }
367
+ extern "C" bool gea_host_measure_text_with_style(const char *text, int maxWidth, int fontId, int fontSize, int weight, int lineHeight, int *width, int *height)
368
+ { return measureNativeText(text, maxWidth, fontId, fontSize, weight, 1000, lineHeight, width, height); }
369
+ extern "C" bool gea_host_measure_text_with_variations(const char *text, int maxWidth, int fontId, int fontSize, int weight, int stretch, int lineHeight, int *width, int *height)
370
+ { return measureNativeText(text, maxWidth, fontId, fontSize, weight, stretch, lineHeight, width, height); }
350
371
  #endif
@@ -8,9 +8,12 @@ namespace gea::macos {
8
8
 
9
9
  #ifdef __OBJC__
10
10
  // Returns an NSImage built from the ImageStore slot at imageId, or nil if
11
- // the slot is empty / invalid. Pixels are converted from RGB565 (+ optional
12
- // alpha channel) to RGBA8888. Caller owns the result via ARC.
11
+ // the slot is empty / invalid. Native color channels are preserved as RGBA8888.
12
+ // Caller owns the result via ARC.
13
13
  NSImage *imageForId(int imageId);
14
+ NSImage *imageForNode(int nodeId);
15
+ NSImage *imageForIdRegion(int imageId, int x, int y, int width, int height);
16
+ NSImage *imageForIdCover(int imageId, int width, int height);
14
17
  #endif
15
18
 
16
19
  } // namespace gea::macos
@@ -3,17 +3,54 @@
3
3
  #include "image_bridge.h"
4
4
  #include "image.h"
5
5
  #include "pixel.h"
6
+ #include "ui/tree_internal.h"
7
+ #include <cstdlib>
8
+ #include <algorithm>
9
+ #include <cmath>
6
10
 
7
11
  namespace gea::macos {
8
12
 
13
+ NSImage *imageForNode(int nodeId)
14
+ {
15
+ auto &tree = gea::embedded::ui::Tree::instance();
16
+ if (nodeId < 0 || nodeId >= tree.nodeCount()) return nil;
17
+ const auto &node = tree.node(nodeId);
18
+ const char *width = tree.getAttribute(nodeId, "data-source-width");
19
+ if (width && width[0]) {
20
+ auto crop = [&](const char *name) { const char *value = tree.getAttribute(nodeId, name); return value ? std::atoi(value) : 0; };
21
+ return imageForIdRegion(node.image_id, crop("data-source-x"), crop("data-source-y"), std::atoi(width), crop("data-source-height"));
22
+ }
23
+ if (node.style.image_fit == 2) return imageForIdCover(node.image_id, node.layout.width, node.layout.height);
24
+ return imageForId(node.image_id);
25
+ }
26
+
9
27
  NSImage *imageForId(int imageId)
28
+ {
29
+ auto &store = gea::framework::graphics::ImageStore::instance();
30
+ return imageForIdRegion(imageId, 0, 0, store.width(imageId), store.height(imageId));
31
+ }
32
+
33
+ NSImage *imageForIdCover(int imageId, int width, int height)
34
+ {
35
+ auto &store = gea::framework::graphics::ImageStore::instance();
36
+ const int sw = store.width(imageId), sh = store.height(imageId);
37
+ if (width <= 0 || height <= 0 || sw <= 0 || sh <= 0) return nil;
38
+ const double ratio = static_cast<double>(width) / height;
39
+ int cw = sw, ch = sh;
40
+ if (static_cast<double>(sw) / sh > ratio) cw = std::clamp(static_cast<int>(std::lround(sh * ratio)), 1, sw);
41
+ else ch = std::clamp(static_cast<int>(std::lround(sw / ratio)), 1, sh);
42
+ return imageForIdRegion(imageId, (sw - cw) / 2, (sh - ch) / 2, cw, ch);
43
+ }
44
+
45
+ NSImage *imageForIdRegion(int imageId, int x, int y, int w, int h)
10
46
  {
11
47
  using namespace gea::framework::graphics;
12
48
  if (imageId < 0) return nil;
13
49
  auto &store = ImageStore::instance();
14
- const int w = store.width(imageId);
15
- const int h = store.height(imageId);
16
- const std::uint16_t *src = store.currentPixels(imageId);
50
+ const int sourceWidth = store.width(imageId);
51
+ const int sourceHeight = store.height(imageId);
52
+ if (x < 0 || y < 0 || w <= 0 || h <= 0 || x > sourceWidth - w || y > sourceHeight - h) return nil;
53
+ const pixel::native_t *src = store.currentPixels(imageId);
17
54
  const std::uint8_t *alpha = store.currentAlpha(imageId);
18
55
  if (w <= 0 || h <= 0 || !src) return nil;
19
56
 
@@ -21,12 +58,13 @@ NSImage *imageForId(int imageId)
21
58
  NSMutableData *rgba = [NSMutableData dataWithLength:(NSUInteger)(pixelCount * 4)];
22
59
  std::uint8_t *dst = static_cast<std::uint8_t *>(rgba.mutableBytes);
23
60
  for (NSInteger i = 0; i < pixelCount; i++) {
24
- int r, g, b;
25
- pixel::unpackRgb565(src[i], &r, &g, &b);
26
- dst[i * 4 + 0] = static_cast<std::uint8_t>((r * 255 + 15) / 31);
27
- dst[i * 4 + 1] = static_cast<std::uint8_t>((g * 255 + 31) / 63);
28
- dst[i * 4 + 2] = static_cast<std::uint8_t>((b * 255 + 15) / 31);
29
- dst[i * 4 + 3] = alpha ? alpha[i] : 0xFF;
61
+ int r, g, b, a;
62
+ const NSInteger sourceIndex = (y + i / w) * sourceWidth + x + i % w;
63
+ pixel::unpackNative8(src[sourceIndex], &r, &g, &b, &a);
64
+ dst[i * 4 + 0] = static_cast<std::uint8_t>(r);
65
+ dst[i * 4 + 1] = static_cast<std::uint8_t>(g);
66
+ dst[i * 4 + 2] = static_cast<std::uint8_t>(b);
67
+ dst[i * 4 + 3] = alpha ? alpha[sourceIndex] : static_cast<std::uint8_t>(a);
30
68
  }
31
69
 
32
70
  NSBitmapImageRep *rep =
@@ -0,0 +1,54 @@
1
+ #pragma once
2
+
3
+ #import <AppKit/AppKit.h>
4
+ #include <cstdio>
5
+ #include <cstdlib>
6
+ #include <cstring>
7
+
8
+ // Hidden-window test mode. Ordinary user launches keep normal AppKit
9
+ // presentation. Tests can instantiate the real application without ordering a
10
+ // window, becoming active, or opening Gea audio. This is not a WebView path.
11
+ namespace gea::macos::background_test {
12
+ [[noreturn]] inline void refuse(const char* reason) {
13
+ std::fprintf(stderr, "Gea background UI test refused: %s\n", reason);
14
+ std::fflush(stderr);
15
+ std::_Exit(78);
16
+ }
17
+ inline bool enabled() {
18
+ static const bool value = [] {
19
+ const char* flag = std::getenv("GEA_MACOS_BACKGROUND_TEST");
20
+ if (!flag) return false;
21
+ if (std::strcmp(flag, "1") != 0)
22
+ refuse("GEA_MACOS_BACKGROUND_TEST must be absent or exactly 1");
23
+ return true;
24
+ }();
25
+ return value;
26
+ }
27
+ inline void checkApplication(NSApplication* app) {
28
+ if (!enabled()) return;
29
+ if (!app || app.activationPolicy != NSApplicationActivationPolicyProhibited || app.active)
30
+ refuse("application must stay prohibited and inactive");
31
+ for (NSWindow* window in app.windows) {
32
+ if (window.visible || window.keyWindow || window.mainWindow)
33
+ refuse("all owned windows must stay hidden, nonkey and nonmain");
34
+ }
35
+ }
36
+ inline void configure(NSApplication* app) {
37
+ if (!enabled()) return;
38
+ const char* audio = std::getenv("GEA_AUDIO_DISABLED");
39
+ if (!audio || std::strcmp(audio, "1") != 0)
40
+ refuse("GEA_AUDIO_DISABLED=1 is required before application startup");
41
+ [app setActivationPolicy:NSApplicationActivationPolicyProhibited];
42
+ checkApplication(app);
43
+ }
44
+ inline void present(NSWindow* window) {
45
+ if (enabled()) {
46
+ checkApplication(NSApp);
47
+ if (!window || window.visible || window.keyWindow || window.mainWindow)
48
+ refuse("window presentation is forbidden");
49
+ return;
50
+ }
51
+ [window makeKeyAndOrderFront:nil];
52
+ [NSApp activateIgnoringOtherApps:YES];
53
+ }
54
+ } // namespace gea::macos::background_test
@@ -0,0 +1,13 @@
1
+ #pragma once
2
+ #include "native_identity.h"
3
+ #include "ui/selector.h"
4
+ #include "ui/tree_internal.h"
5
+ namespace gea::macos {
6
+ // Native callbacks keep their original allocation lease. Disabled state is
7
+ // resolved from the live Tree at the mutation boundary, not cached NSControl
8
+ // appearance; cancellation still uses allocation-only admission.
9
+ inline bool controlAdmitted(int node, const gea::framework::identity::Owner &owner) {
10
+ return gea::framework::identity::current(node, owner) &&
11
+ !gea::embedded::ui::selector::disabled(gea::embedded::ui::Tree::instance(), node);
12
+ }
13
+ }
@@ -20,7 +20,7 @@
20
20
  namespace {
21
21
 
22
22
  gea::framework::graphics::Canvas g_canvas;
23
- std::uint16_t *g_framebuffer = nullptr;
23
+ gea::framework::graphics::pixel::native_t *g_framebuffer = nullptr;
24
24
  // Logical canvas size. Starts at the compile-time panel dims and follows the
25
25
  // app's orientation: applyOrientation() below reallocates the surface (and
26
26
  // resizes the window) to the rotated logical size, so window.innerWidth, the
@@ -37,7 +37,7 @@ int g_flush_pixels = 0;
37
37
  void ensure_canvas()
38
38
  {
39
39
  if (g_framebuffer) return;
40
- g_framebuffer = static_cast<std::uint16_t *>(std::calloc((size_t)g_canvas_w * (size_t)g_canvas_h, sizeof(std::uint16_t)));
40
+ g_framebuffer = static_cast<gea::framework::graphics::pixel::native_t *>(std::calloc((size_t)g_canvas_w * (size_t)g_canvas_h, sizeof(gea::framework::graphics::pixel::native_t)));
41
41
  g_canvas.bindPixels(g_framebuffer, g_canvas_w, g_canvas_h);
42
42
  }
43
43
 
@@ -62,9 +62,11 @@ void submit_full_frame()
62
62
  {
63
63
  if (!framebuffer_target()) return;
64
64
  ensure_canvas();
65
+ #if GEA_MACOS_THERMALRIGHT_DISPLAY_TARGET
65
66
  gea::macos::thermalright::submitRgb565(g_framebuffer,
66
67
  gea::platform::display::kWidth,
67
68
  gea::platform::display::kHeight);
69
+ #endif
68
70
  }
69
71
 
70
72
  } // namespace
@@ -132,12 +134,11 @@ void Display::flushRectsRasterized(const DisplayFlushRect *rects, int count, Dis
132
134
  if (x0 > x1 || y0 > y1) continue;
133
135
  const int w = x1 - x0 + 1;
134
136
  const int h = y1 - y0 + 1;
135
- std::vector<std::uint16_t> pixels(static_cast<std::size_t>(w) * static_cast<std::size_t>(h));
137
+ std::vector<gea::framework::graphics::pixel::native_t> pixels(static_cast<std::size_t>(w) * static_cast<std::size_t>(h));
136
138
  raster(pixels.data(), w, h, x0, y0, user);
137
139
  for (int row = 0; row < h; row++) {
138
- std::memcpy(g_framebuffer + static_cast<std::size_t>(y0 + row) * kWidth + x0,
139
- pixels.data() + static_cast<std::size_t>(row) * w,
140
- static_cast<std::size_t>(w) * sizeof(std::uint16_t));
140
+ for (int col = 0; col < w; ++col)
141
+ g_framebuffer[static_cast<std::size_t>(y0 + row) * kWidth + x0 + col] = pixels[static_cast<std::size_t>(row) * w + col];
141
142
  }
142
143
  g_canvas.markDirty(x0, y0, x1, y1);
143
144
  }
@@ -163,12 +164,11 @@ bool Display::streamRect(int x, int y, int w, int h, DisplayStreamRasterFn raste
163
164
 
164
165
  const int rw = x1 - x0 + 1;
165
166
  const int rh = y1 - y0 + 1;
166
- std::vector<std::uint16_t> pixels(static_cast<std::size_t>(rw) * static_cast<std::size_t>(rh));
167
+ std::vector<gea::framework::graphics::pixel::native_t> pixels(static_cast<std::size_t>(rw) * static_cast<std::size_t>(rh));
167
168
  raster(pixels.data(), rw, rh, x0, y0, user);
168
169
  for (int row = 0; row < rh; row++) {
169
- std::memcpy(g_framebuffer + static_cast<std::size_t>(y0 + row) * kWidth + x0,
170
- pixels.data() + static_cast<std::size_t>(row) * rw,
171
- static_cast<std::size_t>(rw) * sizeof(std::uint16_t));
170
+ for (int col = 0; col < rw; ++col)
171
+ g_framebuffer[static_cast<std::size_t>(y0 + row) * kWidth + x0 + col] = pixels[static_cast<std::size_t>(row) * rw + col];
172
172
  }
173
173
  g_canvas.markDirty(x0, y0, x1, y1);
174
174
  flush();
@@ -338,17 +338,17 @@ void Display::clip(int *x0, int *y0, int *x1, int *y1)
338
338
  if (y1) *y1 = g_canvas_h - 1;
339
339
  }
340
340
 
341
- void Display::fillRect(int x, int y, int w, int h, uint16_t c) { if (framebuffer_target()) canvas()->fillRect(x, y, w, h, c); }
341
+ void Display::fillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->fillRect(x, y, w, h, c); }
342
342
  void Display::scrollRect(int x, int y, int w, int h, int dx, int dy) { if (framebuffer_target()) canvas()->scrollRect(x, y, w, h, dx, dy); }
343
343
  void Display::resetScrollRegion() { if (framebuffer_target()) canvas()->setScrollRegion(0, 0, 0); }
344
- void Display::strokeRect(int x, int y, int w, int h, uint16_t c) { if (framebuffer_target()) canvas()->strokeRect(x, y, w, h, c); }
345
- void Display::fillCircle(int cx, int cy, int r, uint16_t c) { if (framebuffer_target()) canvas()->fillCircle(cx, cy, r, c); }
346
- void Display::strokeCircle(int cx, int cy, int r, uint16_t c) { if (framebuffer_target()) canvas()->strokeCircle(cx, cy, r, c); }
347
- void Display::drawLine(int x0, int y0, int x1, int y1, uint16_t c) { if (framebuffer_target()) canvas()->drawLine(x0, y0, x1, y1, c); }
348
- void Display::drawArc(int cx, int cy, int r, int start_deg, int end_deg, uint16_t c) { if (framebuffer_target()) canvas()->drawArc(cx, cy, r, start_deg, end_deg, c); }
349
- void Display::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, uint16_t c) { if (framebuffer_target()) canvas()->fillTriangle(x0, y0, x1, y1, x2, y2, c); }
350
- void Display::drawText(const char *text, int x, int y, uint16_t c, float scale) { if (framebuffer_target()) canvas()->drawText(text, x, y, c, scale); }
351
- void Display::drawTextFont(const char *text, int x, int y, uint16_t c, int font_id)
344
+ void Display::strokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->strokeRect(x, y, w, h, c); }
345
+ void Display::fillCircle(int cx, int cy, int r, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->fillCircle(cx, cy, r, c); }
346
+ void Display::strokeCircle(int cx, int cy, int r, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->strokeCircle(cx, cy, r, c); }
347
+ void Display::drawLine(int x0, int y0, int x1, int y1, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->drawLine(x0, y0, x1, y1, c); }
348
+ void Display::drawArc(int cx, int cy, int r, int start_deg, int end_deg, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->drawArc(cx, cy, r, start_deg, end_deg, c); }
349
+ void Display::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->fillTriangle(x0, y0, x1, y1, x2, y2, c); }
350
+ void Display::drawText(const char *text, int x, int y, gea::framework::graphics::pixel::native_t c, float scale) { if (framebuffer_target()) canvas()->drawText(text, x, y, c, scale); }
351
+ void Display::drawTextFont(const char *text, int x, int y, gea::framework::graphics::pixel::native_t c, int font_id)
352
352
  {
353
353
  if (!framebuffer_target()) return;
354
354
  #ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
@@ -358,14 +358,14 @@ void Display::drawTextFont(const char *text, int x, int y, uint16_t c, int font_
358
358
  canvas()->drawText(text, x, y, c, 1.0f);
359
359
  #endif
360
360
  }
361
- void Display::drawTextFontFamily(const char *text, int x, int y, uint16_t c, int family_id, int size_px) { if (framebuffer_target()) canvas()->drawTextFontFamily(text, x, y, c, family_id, size_px); }
362
- void Display::setPixel(int x, int y, uint16_t c) { if (framebuffer_target()) canvas()->fillRect(x, y, 1, 1, c); }
363
- void Display::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br, int bl, uint16_t c) { if (framebuffer_target()) canvas()->fillRoundedRect(x, y, w, h, tl, tr, br, bl, c); }
364
- void Display::fillRoundedRectBoxesRgb565(const int16_t *xs, const int16_t *ys, int count, int w, int h, int tl, int tr, int br, int bl, const uint16_t *colors)
361
+ void Display::drawTextFontFamily(const char *text, int x, int y, gea::framework::graphics::pixel::native_t c, int family_id, int size_px) { if (framebuffer_target()) canvas()->drawTextFontFamily(text, x, y, c, family_id, size_px); }
362
+ void Display::setPixel(int x, int y, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->fillRect(x, y, 1, 1, c); }
363
+ void Display::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br, int bl, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->fillRoundedRect(x, y, w, h, tl, tr, br, bl, c); }
364
+ void Display::fillRoundedRectBoxesRgb565(const int16_t *xs, const int16_t *ys, int count, int w, int h, int tl, int tr, int br, int bl, const gea::framework::graphics::pixel::native_t *colors)
365
365
  {
366
366
  if (framebuffer_target()) canvas()->fillRoundedRectBoxesRgb565(xs, ys, count, w, h, tl, tr, br, bl, colors);
367
367
  }
368
- void Display::strokeRoundedRect(int x, int y, int w, int h, int tl, int tr, int br, int bl, int lw, uint16_t c) { if (framebuffer_target()) canvas()->strokeRoundedRect(x, y, w, h, tl, tr, br, bl, lw, c); }
368
+ void Display::strokeRoundedRect(int x, int y, int w, int h, int tl, int tr, int br, int bl, int lw, gea::framework::graphics::pixel::native_t c) { if (framebuffer_target()) canvas()->strokeRoundedRect(x, y, w, h, tl, tr, br, bl, lw, c); }
369
369
  void Display::blitImage(const gea::framework::graphics::pixel::native_t *src, const uint8_t *alpha, int src_w, int src_h, int dx, int dy) { if (framebuffer_target()) canvas()->drawImage(src, alpha, src_w, src_h, dx, dy); }
370
370
  void Display::blitImageScaled(const gea::framework::graphics::pixel::native_t *src, const uint8_t *alpha, int src_w, int src_h, int dx, int dy, int dst_w, int dst_h) { if (framebuffer_target()) canvas()->drawImage(src, alpha, src_w, src_h, dx, dy, dst_w, dst_h); }
371
371
  void Display::setWorldOverlay(const uint16_t *, int, int, int, int) {}
@@ -387,7 +387,7 @@ bool Display::copySnapshotRgb565(uint16_t *dst, int pixel_capacity, int *width,
387
387
  if (width) *width = g_canvas_w;
388
388
  if (height) *height = g_canvas_h;
389
389
  if (!dst || pixel_capacity < pixels) return false;
390
- std::memcpy(dst, g_framebuffer, static_cast<std::size_t>(pixels) * sizeof(std::uint16_t));
390
+ for (int i = 0; i < pixels; ++i) dst[i] = gea::framework::graphics::pixel::toRgb565(g_framebuffer[i]);
391
391
  return true;
392
392
  }
393
393
  int Display::countNonBlackPixels(bool)
@@ -395,7 +395,9 @@ int Display::countNonBlackPixels(bool)
395
395
  ensure_canvas();
396
396
  int count = 0;
397
397
  for (int i = 0; i < g_canvas_w * g_canvas_h; i++) {
398
- if (g_framebuffer[i] != 0) count++;
398
+ int r, g, b, a;
399
+ gea::framework::graphics::pixel::unpackNative8(g_framebuffer[i], &r, &g, &b, &a);
400
+ if (r || g || b) count++;
399
401
  }
400
402
  return count;
401
403
  }
@@ -469,7 +471,7 @@ void applyOrientation(gea::framework::display::DisplayOrientation)
469
471
  g_canvas_w = w;
470
472
  g_canvas_h = h;
471
473
  std::free(g_framebuffer);
472
- g_framebuffer = static_cast<std::uint16_t *>(std::calloc((size_t)w * (size_t)h, sizeof(std::uint16_t)));
474
+ g_framebuffer = static_cast<gea::framework::graphics::pixel::native_t *>(std::calloc((size_t)w * (size_t)h, sizeof(gea::framework::graphics::pixel::native_t)));
473
475
  g_canvas.bindPixels(g_framebuffer, w, h);
474
476
  // Orientation is set from the app's top level inside Application::init,
475
477
  // which runs on the main thread with the window already on screen.
@@ -0,0 +1,73 @@
1
+ #pragma once
2
+ #import <AppKit/AppKit.h>
3
+ #include "events.h"
4
+
5
+ // NSEvent.keyCode is a physical AppKit scan code, not a legacy keyCode.
6
+ // `key` follows the current layout; `code` names the physical position.
7
+ static const char *geaPhysicalKeyCode(unsigned short code) {
8
+ switch(code) {
9
+ case 0:return "KeyA";case 1:return "KeyS";case 2:return "KeyD";case 3:return "KeyF";
10
+ case 4:return "KeyH";case 5:return "KeyG";case 6:return "KeyZ";case 7:return "KeyX";
11
+ case 8:return "KeyC";case 9:return "KeyV";case 11:return "KeyB";case 12:return "KeyQ";
12
+ case 13:return "KeyW";case 14:return "KeyE";case 15:return "KeyR";case 16:return "KeyY";
13
+ case 17:return "KeyT";case 18:return "Digit1";case 19:return "Digit2";case 20:return "Digit3";
14
+ case 21:return "Digit4";case 22:return "Digit6";case 23:return "Digit5";case 24:return "Equal";
15
+ case 25:return "Digit9";case 26:return "Digit7";case 27:return "Minus";case 28:return "Digit8";
16
+ case 29:return "Digit0";case 30:return "BracketRight";case 31:return "KeyO";case 32:return "KeyU";
17
+ case 33:return "BracketLeft";case 34:return "KeyI";case 35:return "KeyP";case 36:return "Enter";
18
+ case 37:return "KeyL";case 38:return "KeyJ";case 39:return "Quote";case 40:return "KeyK";
19
+ case 41:return "Semicolon";case 42:return "Backslash";case 43:return "Comma";case 44:return "Slash";
20
+ case 45:return "KeyN";case 46:return "KeyM";case 47:return "Period";case 48:return "Tab";
21
+ case 49:return "Space";case 50:return "Backquote";case 51:return "Backspace";case 53:return "Escape";
22
+ case 54:return "MetaRight";case 55:return "MetaLeft";case 56:return "ShiftLeft";case 57:return "CapsLock";
23
+ case 58:return "AltLeft";case 59:return "ControlLeft";case 60:return "ShiftRight";
24
+ case 61:return "AltRight";case 62:return "ControlRight";case 63:return "Fn";
25
+ case 65:return "NumpadDecimal";case 67:return "NumpadMultiply";case 69:return "NumpadAdd";
26
+ case 71:return "NumLock";case 75:return "NumpadDivide";case 76:return "NumpadEnter";
27
+ case 78:return "NumpadSubtract";case 81:return "NumpadEqual";case 82:return "Numpad0";
28
+ case 83:return "Numpad1";case 84:return "Numpad2";case 85:return "Numpad3";case 86:return "Numpad4";
29
+ case 87:return "Numpad5";case 88:return "Numpad6";case 89:return "Numpad7";case 91:return "Numpad8";
30
+ case 92:return "Numpad9";case 96:return "F5";case 97:return "F6";case 98:return "F7";
31
+ case 99:return "F3";case 100:return "F8";case 101:return "F9";case 103:return "F11";
32
+ case 109:return "F10";case 111:return "F12";case 115:return "Home";case 116:return "PageUp";
33
+ case 117:return "Delete";case 118:return "F4";case 119:return "End";case 120:return "F2";
34
+ case 121:return "PageDown";case 122:return "F1";case 123:return "ArrowLeft";
35
+ case 124:return "ArrowRight";case 125:return "ArrowDown";case 126:return "ArrowUp";
36
+ default:return "Unidentified";
37
+ }
38
+ }
39
+ static gea::framework::events::PointerEvent geaNativeKey(NSEvent *native, bool composing, bool up) {
40
+ gea::framework::events::PointerEvent event;
41
+ event.type=up?gea::framework::events::PointerEventType::KeyUp:gea::framework::events::PointerEventType::KeyDown;
42
+ event.code=geaPhysicalKeyCode(native.keyCode);
43
+ NSString *characters=(native.modifierFlags & (NSEventModifierFlagCommand|NSEventModifierFlagControl))?
44
+ native.charactersIgnoringModifiers:native.characters;
45
+ event.key=characters.UTF8String?:"";
46
+ switch(native.keyCode) {
47
+ case 36:case 76:event.key="Enter";event.keyCode=13;break;
48
+ case 48:event.key="Tab";event.keyCode=9;break;
49
+ case 49:event.key=" ";event.keyCode=32;break;
50
+ case 51:event.key="Backspace";event.keyCode=8;break;
51
+ case 53:event.key="Escape";event.keyCode=27;break;
52
+ case 115:event.key="Home";event.keyCode=36;break;
53
+ case 116:event.key="PageUp";event.keyCode=33;break;
54
+ case 117:event.key="Delete";event.keyCode=46;break;
55
+ case 119:event.key="End";event.keyCode=35;break;
56
+ case 121:event.key="PageDown";event.keyCode=34;break;
57
+ case 123:event.key="ArrowLeft";event.keyCode=37;break;
58
+ case 124:event.key="ArrowRight";event.keyCode=39;break;
59
+ case 125:event.key="ArrowDown";event.keyCode=40;break;
60
+ case 126:event.key="ArrowUp";event.keyCode=38;break;
61
+ default: {
62
+ NSString *upper=[native.charactersIgnoringModifiers uppercaseString];
63
+ if(upper.length==1)event.keyCode=[upper characterAtIndex:0];
64
+ break;
65
+ }
66
+ }
67
+ event.altKey=(native.modifierFlags & NSEventModifierFlagOption)!=0;
68
+ event.ctrlKey=(native.modifierFlags & NSEventModifierFlagControl)!=0;
69
+ event.metaKey=(native.modifierFlags & NSEventModifierFlagCommand)!=0;
70
+ event.shiftKey=(native.modifierFlags & NSEventModifierFlagShift)!=0;
71
+ event.repeat=native.isARepeat;event.isComposing=composing;
72
+ return event;
73
+ }