diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-13 15:41:39 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-13 15:41:39 +0300 |
| commit | 537b2d768b31deb0f95c5122e0d562e4406ae9a0 (patch) | |
| tree | cc3e85cb2990672c11b4e35c6dbcfaac733d09f5 /src/platform/cocoa | |
| parent | 98f0d51157fa2a2b8ccaae54ed4850a88d223d01 (diff) | |
Unify core and runner events into struct yasl_event
The core input queue and the yasl_run() runner each had their own event
type, and the runner translated one into the other. Both now share
struct yasl_event in <yasl/yasl.h>: yasl_surface_poll_event() returns the
same events the app event callback receives, so the runner forwards them
unchanged.
Pointer input becomes POINTER_DOWN/UP/MOTION/SCROLL/LEAVE with a named
yasl_pointer_button and a motion delta computed in the input queue. TEXT,
PASTE and DROP share one text payload, scale changes fold into
WINDOW_RESIZE, and every event carries a monotonic time_ns.
yasl_surface_set_mouse_mode() is replaced by
yasl_surface_set_pointer_mode(); no backend implements relative mode yet.
The export check now only counts YASL_API declarations, so function
pointer fields returning enum yasl_app_result are not mistaken for
exports.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/platform/cocoa')
| -rw-r--r-- | src/platform/cocoa/api.h | 2 | ||||
| -rw-r--r-- | src/platform/cocoa/window.m | 113 |
2 files changed, 59 insertions, 56 deletions
diff --git a/src/platform/cocoa/api.h b/src/platform/cocoa/api.h index 0f705dd..eaacc01 100644 --- a/src/platform/cocoa/api.h +++ b/src/platform/cocoa/api.h @@ -24,7 +24,7 @@ void cocoa_surface_set_callbacks(struct cocoa_surface *s, const struct yasl_wind void cocoa_surface_set_title(struct cocoa_surface *s, const char *title); void cocoa_surface_schedule_redraw(struct cocoa_surface *s); bool cocoa_surface_present(struct cocoa_surface *s, const struct yasl_framebuffer *fb, const struct yasl_damage_list *damage); -bool cocoa_surface_input_poll(struct cocoa_surface *s, struct yasl_input_event *ev); +bool cocoa_surface_poll_event(struct cocoa_surface *s, struct yasl_event *ev); struct yasl_geometry cocoa_surface_geometry(const struct cocoa_surface *s); struct yasl_timing cocoa_surface_timing(const struct cocoa_surface *s); void cocoa_surface_set_cursor(struct cocoa_surface *s, enum yasl_cursor cursor); diff --git a/src/platform/cocoa/window.m b/src/platform/cocoa/window.m index aa9fa48..f1f0f03 100644 --- a/src/platform/cocoa/window.m +++ b/src/platform/cocoa/window.m @@ -194,8 +194,8 @@ static void check_pointer_reset(struct cocoa_surface *w) { if (!w || w->closed) return; if (w->buttons != 0 || w->tablet_down) { - struct yasl_input_event ev = { - .kind = YASL_INPUT_RESET, + struct yasl_event ev = { + .type = YASL_EVENT_INPUT_RESET, .surface = &w->base, }; input_queue_push(&w->input, &ev); @@ -210,8 +210,8 @@ static void push_focus(struct cocoa_surface *w, bool focused) return; if (!focused) check_pointer_reset(w); - struct yasl_input_event ev = { - .kind = YASL_INPUT_FOCUS, + struct yasl_event ev = { + .type = YASL_EVENT_FOCUS, .surface = &w->base, .focus = { .focused = focused }, }; @@ -303,8 +303,8 @@ static void on_key_event(struct cocoa_surface *w, NSEvent *ev, enum yasl_key_act { w->mods = cocoa_mods(ev.modifierFlags); struct yasl_key_event k = cocoa_key_event(ev, action); - struct yasl_input_event input_ev = { - .kind = YASL_INPUT_KEY, + struct yasl_event input_ev = { + .type = YASL_EVENT_KEY, .surface = &w->base, .key = k, }; @@ -314,10 +314,10 @@ static void on_key_event(struct cocoa_surface *w, NSEvent *ev, enum yasl_key_act static void on_text_input(struct cocoa_surface *w, const char *text, size_t len) { if (len) { - struct yasl_input_event ev = { - .kind = YASL_INPUT_TEXT, + struct yasl_event ev = { + .type = YASL_EVENT_TEXT, .surface = &w->base, - .text = { .text = text, .len = len }, + .text = { .data = text, .len = len }, }; input_queue_push(&w->input, &ev); } @@ -343,9 +343,11 @@ static bool modifier_down(NSEvent *ev) static int cocoa_button(NSInteger number) { switch (number) { - case 0: return 0; - case 1: return 2; - case 2: return 1; + case 0: return YASL_BUTTON_PRIMARY; + case 1: return YASL_BUTTON_SECONDARY; + case 2: return YASL_BUTTON_MIDDLE; + case 3: return YASL_BUTTON_BACK; + case 4: return YASL_BUTTON_FORWARD; default: return -1; } } @@ -432,10 +434,13 @@ static void update_drag_hover(struct cocoa_display *d, NSPoint screenPoint) } } -static void push_pointer(struct cocoa_surface *w, NSEvent *ev, enum yasl_pointer_kind kind, +static void push_pointer(struct cocoa_surface *w, NSEvent *ev, enum yasl_event_type type, int button) { - if (!w || w->closed || button < 0) + if (!w || w->closed) + return; + if ((type == YASL_EVENT_POINTER_DOWN || type == YASL_EVENT_POINTER_UP) && + (button < 0 || button > 4)) return; bool proximity = ev.type == NSEventTypeTabletProximity || ev.subtype == NSEventSubtypeTabletProximity; @@ -446,17 +451,16 @@ static void push_pointer(struct cocoa_surface *w, NSEvent *ev, enum yasl_pointer YASL_PTR_DEV_PEN : YASL_PTR_DEV_MOUSE; return; } - kind = YASL_PTR_LEAVE; - button = 3; + type = YASL_EVENT_POINTER_LEAVE; } w->mods = cocoa_mods(ev.modifierFlags); - if (kind != YASL_PTR_LEAVE) + if (type != YASL_EVENT_POINTER_LEAVE) w->pointer = event_pos(w, ev); enum yasl_pointer_device device = YASL_PTR_DEV_MOUSE; struct yasl_pointer_pen pen = {0}; - if (kind == YASL_PTR_LEAVE && w->tablet_device != YASL_PTR_DEV_MOUSE) { + if (type == YASL_EVENT_POINTER_LEAVE && w->tablet_device != YASL_PTR_DEV_MOUSE) { device = w->tablet_device; w->tablet_down = false; } else if (ev.type == NSEventTypeTabletPoint || ev.subtype == NSEventSubtypeTabletPoint) { @@ -471,32 +475,32 @@ static void push_pointer(struct cocoa_surface *w, NSEvent *ev, enum yasl_pointer NSPoint tilt = ev.tilt; pen.tilt_x = (int8_t)lround(fmax(-1, fmin(1, tilt.x)) * 90.0); pen.tilt_y = (int8_t)lround(fmax(-1, fmin(1, -tilt.y)) * 90.0); - if (button == 0 && kind == YASL_PTR_PRESS) w->tablet_down = true; - if (button == 0 && kind == YASL_PTR_RELEASE) { + if (button == 0 && type == YASL_EVENT_POINTER_DOWN) w->tablet_down = true; + if (button == 0 && type == YASL_EVENT_POINTER_UP) { w->tablet_down = false; pen.pressure = 0; } } - if (kind == YASL_PTR_PRESS && button >= 0 && button < 32) + if (type == YASL_EVENT_POINTER_DOWN && button >= 0 && button < 32) w->buttons |= (1u << button); - else if (kind == YASL_PTR_RELEASE && button >= 0 && button < 32) + else if (type == YASL_EVENT_POINTER_UP && button >= 0 && button < 32) w->buttons &= ~(1u << button); - struct yasl_input_event input_ev = { + struct yasl_event input_ev = { + .type = type, .surface = &w->base, - .kind = YASL_INPUT_MOUSE, - .mouse = { + .pointer = { .pos = w->pointer, - .button = (int16_t)button, - .kind = kind, + .button = (type == YASL_EVENT_POINTER_DOWN || type == YASL_EVENT_POINTER_UP) ? + (enum yasl_pointer_button)button : YASL_BUTTON_PRIMARY, .device = device, .mods = w->mods, .pen = pen, }, }; input_queue_push(&w->input, &input_ev); - if (kind == YASL_PTR_MOTION && darwin_ctx.drag.active) { + if (type == YASL_EVENT_POINTER_MOTION && darwin_ctx.drag.active) { if (darwin_ctx.drag.carried) { cocoa_surface_place_under_pointer(darwin_ctx.drag.carried, darwin_ctx.drag.carry_offset); } else if (darwin_ctx.drag.image_window) { @@ -510,7 +514,7 @@ static void push_pointer(struct cocoa_surface *w, NSEvent *ev, enum yasl_pointer mouse.y - pt_h + darwin_ctx.drag.image_hotspot.y / scale)]; } update_drag_hover(&darwin_ctx, [NSEvent mouseLocation]); - } else if (kind == YASL_PTR_RELEASE && darwin_ctx.drag.active) { + } else if (type == YASL_EVENT_POINTER_UP && darwin_ctx.drag.active) { update_drag_hover(&darwin_ctx, [NSEvent mouseLocation]); drag_end(&darwin_ctx, YASL_DRAG_DROPPED); } @@ -639,10 +643,10 @@ static struct yasl_vector2i dragging_pos(YaslView *view, id<NSDraggingInfo> send } const char *utf8 = result.UTF8String; if (utf8 && strlen(utf8)) { - struct yasl_input_event ev = { - .kind = YASL_INPUT_DROP_FILES, + struct yasl_event ev = { + .type = YASL_EVENT_DROP, .surface = &w->base, - .drop_files = { .files = utf8, .len = strlen(utf8) }, + .text = { .data = utf8, .len = strlen(utf8) }, }; input_queue_push(&w->input, &ev); cocoa_surface_schedule_redraw(w); @@ -772,44 +776,44 @@ static struct yasl_vector2i dragging_pos(YaslView *view, id<NSDraggingInfo> send - (void)mouseDown:(NSEvent *)event { [self.window makeFirstResponder:self]; - push_pointer(self.surface, event, YASL_PTR_PRESS, cocoa_button(event.buttonNumber)); + push_pointer(self.surface, event, YASL_EVENT_POINTER_DOWN, cocoa_button(event.buttonNumber)); } - (void)rightMouseDown:(NSEvent *)event { [self mouseDown:event]; } - (void)otherMouseDown:(NSEvent *)event { [self mouseDown:event]; } - (void)mouseUp:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_RELEASE, cocoa_button(event.buttonNumber)); + push_pointer(self.surface, event, YASL_EVENT_POINTER_UP, cocoa_button(event.buttonNumber)); } - (void)rightMouseUp:(NSEvent *)event { [self mouseUp:event]; } - (void)otherMouseUp:(NSEvent *)event { [self mouseUp:event]; } - (void)mouseMoved:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_MOTION, 3); + push_pointer(self.surface, event, YASL_EVENT_POINTER_MOTION, 0); } - (void)mouseDragged:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_MOTION, 0); + push_pointer(self.surface, event, YASL_EVENT_POINTER_MOTION, 0); } - (void)rightMouseDragged:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_MOTION, 2); + push_pointer(self.surface, event, YASL_EVENT_POINTER_MOTION, 0); } - (void)otherMouseDragged:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_MOTION, cocoa_button(event.buttonNumber)); + push_pointer(self.surface, event, YASL_EVENT_POINTER_MOTION, 0); } - (void)mouseExited:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_LEAVE, 3); + push_pointer(self.surface, event, YASL_EVENT_POINTER_LEAVE, 0); } - (void)tabletProximity:(NSEvent *)event { - push_pointer(self.surface, event, YASL_PTR_MOTION, 3); + push_pointer(self.surface, event, YASL_EVENT_POINTER_MOTION, 0); } - (void)tabletPoint:(NSEvent *)event @@ -817,9 +821,9 @@ static struct yasl_vector2i dragging_pos(YaslView *view, id<NSDraggingInfo> send struct cocoa_surface *w = self.surface; if (!w) return; bool down = (event.buttonMask & NSEventButtonMaskPenTip) != 0; - enum yasl_pointer_kind kind = down == w->tablet_down ? YASL_PTR_MOTION : - down ? YASL_PTR_PRESS : YASL_PTR_RELEASE; - push_pointer(w, event, kind, 0); + enum yasl_event_type type = down == w->tablet_down ? YASL_EVENT_POINTER_MOTION : + down ? YASL_EVENT_POINTER_DOWN : YASL_EVENT_POINTER_UP; + push_pointer(w, event, type, 0); } - (void)scrollWheel:(NSEvent *)event @@ -834,12 +838,11 @@ static struct yasl_vector2i dragging_pos(YaslView *view, id<NSDraggingInfo> send if (!isfinite(dx) || !isfinite(dy) || (dx == 0 && dy == 0)) return; w->mods = cocoa_mods(event.modifierFlags); w->pointer = event_pos(w, event); - struct yasl_input_event input_ev = { - .kind = YASL_INPUT_MOUSE, + struct yasl_event input_ev = { + .type = YASL_EVENT_POINTER_SCROLL, .surface = &w->base, - .mouse = { + .pointer = { .pos = w->pointer, - .kind = YASL_PTR_SCROLL, .mods = w->mods, .scroll = {dx, dy}, }, @@ -1574,10 +1577,10 @@ void cocoa_display_paste(struct cocoa_display *d, enum yasl_selection sel, struc NSString *s = [pb stringForType:NSPasteboardTypeString]; const char *utf8 = s ? s.UTF8String : NULL; if (utf8) { - struct yasl_input_event ev = { - .kind = YASL_INPUT_PASTE, + struct yasl_event ev = { + .type = YASL_EVENT_PASTE, .surface = &target->base, - .paste = { .text = utf8, .len = strlen(utf8) }, + .text = { .data = utf8, .len = strlen(utf8) }, }; input_queue_push(&target->input, &ev); } @@ -1903,11 +1906,11 @@ void cocoa_surface_set_callbacks(struct cocoa_surface *w, w->ud = ud; } -bool cocoa_surface_input_poll(struct cocoa_surface *w, struct yasl_input_event *ev) +bool cocoa_surface_poll_event(struct cocoa_surface *w, struct yasl_event *ev) { if (!w || !ev || !input_queue_pop(&w->input, ev)) return false; - if (ev->kind == YASL_INPUT_RESET) { + if (ev->type == YASL_EVENT_INPUT_RESET) { w->buttons = 0; w->tablet_down = false; w->mods = 0; @@ -2032,9 +2035,9 @@ static bool impl_surface_present(struct yasl_surface *s, const struct yasl_frame return cocoa_surface_present((struct cocoa_surface *)s, fb, damage); } -static bool impl_surface_input_poll(struct yasl_surface *s, struct yasl_input_event *ev) +static bool impl_surface_poll_event(struct yasl_surface *s, struct yasl_event *ev) { - return cocoa_surface_input_poll((struct cocoa_surface *)s, ev); + return cocoa_surface_poll_event((struct cocoa_surface *)s, ev); } static struct yasl_geometry impl_surface_geometry(const struct yasl_surface *s) @@ -2148,7 +2151,7 @@ const struct yasl_backend_ops yasl_cocoa_backend = { .surface_set_title = impl_surface_set_title, .surface_schedule_redraw = impl_surface_schedule_redraw, .surface_present = impl_surface_present, - .surface_input_poll = impl_surface_input_poll, + .surface_poll_event = impl_surface_poll_event, .surface_geometry = impl_surface_geometry, .surface_timing = impl_surface_timing, .surface_frame_time_ns = impl_surface_frame_time_ns, |
