From dc2f77557344cdeb714728c87c783bcf6f1b69ad Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 20 Apr 2026 17:54:59 +0300 Subject: [PATCH] ios module --- thirdparty/mbedtls/library/entropy_poll.c | 13 ++- vlib/gg/gg_darwin.m | 17 +++- vlib/ios/ios_darwin.c.v | 43 +++++++++ vlib/ios/uikit_bridge.h | 32 +++++++ vlib/macos/macos_darwin.c.v | 101 ++++++++++++++++++++++ 5 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 vlib/ios/ios_darwin.c.v create mode 100644 vlib/ios/uikit_bridge.h diff --git a/thirdparty/mbedtls/library/entropy_poll.c b/thirdparty/mbedtls/library/entropy_poll.c index f7437b384..14dfc1314 100644 --- a/thirdparty/mbedtls/library/entropy_poll.c +++ b/thirdparty/mbedtls/library/entropy_poll.c @@ -119,6 +119,11 @@ static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags) * * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7 */ +#if defined(__APPLE__) +#include +#define HAVE_ARC4RANDOM +#endif + #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM) #include #include @@ -171,7 +176,13 @@ int mbedtls_platform_entropy_poll(void *data, ((void) ret); #endif /* HAVE_GETRANDOM */ -#if defined(HAVE_SYSCTL_ARND) +#if defined(HAVE_ARC4RANDOM) + ((void) file); + ((void) read_len); + arc4random_buf(output, len); + *olen = len; + return 0; +#elif defined(HAVE_SYSCTL_ARND) ((void) file); ((void) read_len); if (sysctl_arnd_wrapper(output, len) == -1) { diff --git a/vlib/gg/gg_darwin.m b/vlib/gg/gg_darwin.m index a98c5d2eb..88e29c68a 100644 --- a/vlib/gg/gg_darwin.m +++ b/vlib/gg/gg_darwin.m @@ -114,6 +114,16 @@ void darwin_draw_rect(float x, float y, float width, float height, gg__Color c) NSRectFill(rect); } +static void mark_view_tree_needs_display(NSView *view) { + if (view == nil) { + return; + } + [view setNeedsDisplay:YES]; + for (NSView *subview in [view subviews]) { + mark_view_tree_needs_display(subview); + } +} + void darwin_window_refresh() { dispatch_async(dispatch_get_main_queue(), ^{ NSWindow *window = [NSApp mainWindow]; @@ -123,7 +133,12 @@ void darwin_window_refresh() { if (window == nil) { return; } - [[window contentView] setNeedsDisplay:YES]; + NSView *contentView = [window contentView]; + if (contentView == nil) { + return; + } + mark_view_tree_needs_display(contentView); + [window displayIfNeeded]; }); // puts("refresh"); diff --git a/vlib/ios/ios_darwin.c.v b/vlib/ios/ios_darwin.c.v new file mode 100644 index 000000000..27a2618d5 --- /dev/null +++ b/vlib/ios/ios_darwin.c.v @@ -0,0 +1,43 @@ +module ios + +import macos + +#flag -framework UIKit +#insert "@VEXEROOT/vlib/ios/uikit_bridge.h" + +fn C.ios_ui_application_main(argc int, argv &&char, principal voidptr, delegate voidptr) int +fn C.ios_color_from_hex(hex u32) voidptr +fn C.ios_color_from_hex_alpha(hex u32, alpha f64) voidptr +fn C.ios_index_path_row(index_path voidptr) i64 +fn C.ios_index_path_for_row(row i64, section i64) voidptr + +// Start the UIKit application run loop. +@[inline] +pub fn application_main(argc int, argv &&char, delegate_class_name string) int { + return C.ios_ui_application_main(argc, argv, unsafe { nil }, + macos.nsstring(delegate_class_name)) +} + +// Create a UIColor from a hex value (0xRRGGBB). +@[inline] +pub fn color(hex u32) macos.Id { + return C.ios_color_from_hex(hex) +} + +// Create a UIColor from a hex value with alpha. +@[inline] +pub fn color_alpha(hex u32, alpha f64) macos.Id { + return C.ios_color_from_hex_alpha(hex, alpha) +} + +// Get the row index from an NSIndexPath. +@[inline] +pub fn index_path_row(index_path macos.Id) int { + return int(C.ios_index_path_row(index_path)) +} + +// Create an NSIndexPath for a row in a section. +@[inline] +pub fn index_path(row int, section int) macos.Id { + return C.ios_index_path_for_row(i64(row), i64(section)) +} diff --git a/vlib/ios/uikit_bridge.h b/vlib/ios/uikit_bridge.h new file mode 100644 index 000000000..4ccbb819b --- /dev/null +++ b/vlib/ios/uikit_bridge.h @@ -0,0 +1,32 @@ +#ifndef IOS_UIKIT_BRIDGE_H +#define IOS_UIKIT_BRIDGE_H + +#import + +static inline int ios_ui_application_main(int argc, char** argv, void* principal, void* delegate) { + return UIApplicationMain(argc, argv, (__bridge NSString*)principal, (__bridge NSString*)delegate); +} + +static inline void* ios_color_from_hex(unsigned int hex) { + CGFloat r = ((hex >> 16) & 0xFF) / 255.0; + CGFloat g = ((hex >> 8) & 0xFF) / 255.0; + CGFloat b = (hex & 0xFF) / 255.0; + return (void*)[UIColor colorWithRed:r green:g blue:b alpha:1.0]; +} + +static inline void* ios_color_from_hex_alpha(unsigned int hex, double alpha) { + CGFloat r = ((hex >> 16) & 0xFF) / 255.0; + CGFloat g = ((hex >> 8) & 0xFF) / 255.0; + CGFloat b = (hex & 0xFF) / 255.0; + return (void*)[UIColor colorWithRed:r green:g blue:b alpha:alpha]; +} + +static inline long long ios_index_path_row(void* index_path) { + return [((NSIndexPath*)index_path) row]; +} + +static inline void* ios_index_path_for_row(long long row, long long section) { + return (void*)[NSIndexPath indexPathForRow:row inSection:section]; +} + +#endif diff --git a/vlib/macos/macos_darwin.c.v b/vlib/macos/macos_darwin.c.v index 3f4c635d2..1f12a20d5 100644 --- a/vlib/macos/macos_darwin.c.v +++ b/vlib/macos/macos_darwin.c.v @@ -127,6 +127,107 @@ pub fn msg_rect(obj Id, selector string) Rect { return C.macos_objc_msg_rect(obj, sel(selector)) } +// ── Multi-argument message sending ───────────────────────────────── + +@[inline] +pub fn msg_id1(obj Id, selector string, a0 Id) Id { + return C.macos_objc_msg_id1(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_id2(obj Id, selector string, a0 Id, a1 Id) Id { + return C.macos_objc_msg_id2(obj, sel(selector), a0, a1) +} + +@[inline] +pub fn msg_id3(obj Id, selector string, a0 Id, a1 Id, a2 Id) Id { + return C.macos_objc_msg_id3(obj, sel(selector), a0, a1, a2) +} + +@[inline] +pub fn msg_id4(obj Id, selector string, a0 Id, a1 Id, a2 Id, a3 Id) Id { + return C.macos_objc_msg_id4(obj, sel(selector), a0, a1, a2, a3) +} + +@[inline] +pub fn msg_id_rect(obj Id, selector string, r Rect) Id { + return C.macos_objc_msg_id_rect(obj, sel(selector), r) +} + +@[inline] +pub fn msg_id_f64(obj Id, selector string, a0 f64) Id { + return C.macos_objc_msg_id_f64(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_id_u64(obj Id, selector string, a0 u64) Id { + return C.macos_objc_msg_id_u64(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_void1(obj Id, selector string, a0 Id) { + C.macos_objc_msg_void1(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_void2(obj Id, selector string, a0 Id, a1 Id) { + C.macos_objc_msg_void2(obj, sel(selector), a0, a1) +} + +@[inline] +pub fn msg_void3(obj Id, selector string, a0 Id, a1 Id, a2 Id) { + C.macos_objc_msg_void3(obj, sel(selector), a0, a1, a2) +} + +@[inline] +pub fn msg_void_bool(obj Id, selector string, a0 bool) { + C.macos_objc_msg_void_bool(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_void_i64(obj Id, selector string, a0 i64) { + C.macos_objc_msg_void_i64(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_void_u64(obj Id, selector string, a0 u64) { + C.macos_objc_msg_void_u64(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_void_f64(obj Id, selector string, a0 f64) { + C.macos_objc_msg_void_f64(obj, sel(selector), a0) +} + +@[inline] +pub fn msg_void_rect(obj Id, selector string, r Rect) { + C.macos_objc_msg_void_rect(obj, sel(selector), r) +} + +// ── Runtime class helpers ────────────────────────────────────────── + +@[inline] +pub fn allocate_class_pair(superclass Id, name string) Id { + return C.macos_objc_allocate_class_pair(superclass, &char(name.str), 0) +} + +@[inline] +pub fn register_class_pair(cls Id) { + C.macos_objc_register_class_pair(cls) +} + +@[inline] +pub fn add_method(cls Id, sel_name string, imp voidptr, types string) bool { + return C.macos_class_add_method(cls, sel(sel_name), imp, &char(types.str)) +} + +@[inline] +pub fn add_protocol(cls Id, proto Protocol) bool { + return C.macos_class_add_protocol(cls, proto) +} + +// ── Allocation ───────────────────────────────────────────────────── + @[inline] pub fn alloc(class_name string) Id { return msg_id(get_class(class_name), 'alloc') -- 2.39.5