summaryrefslogtreecommitdiff
path: root/src/cocoa/window.m
diff options
context:
space:
mode:
Diffstat (limited to 'src/cocoa/window.m')
-rw-r--r--src/cocoa/window.m73
1 files changed, 38 insertions, 35 deletions
diff --git a/src/cocoa/window.m b/src/cocoa/window.m
index 645ae07..028ba64 100644
--- a/src/cocoa/window.m
+++ b/src/cocoa/window.m
@@ -7,6 +7,7 @@
#include "common/timing.h"
#include "common/loop.h"
#include "cocoa/keycodes.h"
+#include "common/keyboard.h"
#include <assert.h>
#include <math.h>
@@ -67,10 +68,6 @@ struct cocoa_surface {
char *preedit;
struct yasl_rect2i ime_caret;
- char *key_text;
- size_t key_text_capacity;
- size_t key_text_len;
- bool in_key_down;
struct yasl_vector2i size;
uint32_t scale_120;
@@ -269,23 +266,35 @@ static unsigned cocoa_mods(NSEventModifierFlags m)
return mods;
}
+/* Decode a single Unicode scalar, never a lone UTF-16 surrogate. Layout labels
+ * are optional: multi-scalar mappings and native function-key characters are 0. */
+static uint32_t cocoa_key_label(NSString *characters)
+{
+ if (!characters.length) return 0;
+ unichar first = [characters characterAtIndex:0];
+ if (characters.length == 1)
+ return yasl_key_label(first);
+ if (characters.length == 2 && first >= 0xd800 && first <= 0xdbff) {
+ unichar second = [characters characterAtIndex:1];
+ if (second >= 0xdc00 && second <= 0xdfff)
+ return yasl_key_label(0x10000 + ((first - 0xd800) << 10) + second - 0xdc00);
+ }
+ return 0;
+}
+
static struct yasl_key_event cocoa_key_event(NSEvent *ev, enum yasl_key_action action)
{
- unsigned short vk = ev.keyCode;
struct yasl_key_event k = {
- .key = vk,
- .keypad = (vk >= kVK_ANSI_Keypad0 && vk <= kVK_ANSI_Keypad9) ||
- vk == kVK_ANSI_KeypadDecimal || vk == kVK_ANSI_KeypadMultiply ||
- vk == kVK_ANSI_KeypadPlus || vk == kVK_ANSI_KeypadClear ||
- vk == kVK_ANSI_KeypadDivide || vk == kVK_ANSI_KeypadEnter ||
- vk == kVK_ANSI_KeypadMinus || vk == kVK_ANSI_KeypadEquals,
+ .key = yasl_key_from_cocoa(ev.keyCode),
.action = action,
.mods = cocoa_mods(ev.modifierFlags),
- .base = vk,
+ .native_code = ev.keyCode,
};
- NSString *chars = ev.characters;
- if (chars && chars.length > 0)
- k.text = [chars characterAtIndex:0];
+ /* characters properties are invalid on flagsChanged events. */
+ if (ev.type == NSEventTypeKeyDown || ev.type == NSEventTypeKeyUp) {
+ k.codepoint = cocoa_key_label([ev charactersByApplyingModifiers:0]);
+ k.shifted = cocoa_key_label([ev charactersByApplyingModifiers:NSEventModifierFlagShift]);
+ }
return k;
}
@@ -741,9 +750,7 @@ static struct yasl_vector2i dragging_pos(YaslView *view, id<NSDraggingInfo> send
struct cocoa_surface *w = self.surface;
if (!w) return;
on_key_event(w, event, event.isARepeat ? YASL_KEY_REPEAT : YASL_KEY_PRESS);
- NSString *chars = event.characters;
- if (chars && chars.length > 0)
- on_text_input(w, chars.UTF8String, strlen(chars.UTF8String));
+ [self interpretKeyEvents:@[event]];
}
- (void)keyUp:(NSEvent *)event
@@ -818,28 +825,25 @@ static struct yasl_vector2i dragging_pos(YaslView *view, id<NSDraggingInfo> send
{
struct cocoa_surface *w = self.surface;
if (!w) return;
- double dx = event.scrollingDeltaX, dy = event.scrollingDeltaY;
+ double dx = -event.scrollingDeltaX, dy = -event.scrollingDeltaY;
if (event.hasPreciseScrollingDeltas) {
dx /= SCROLL_POINTS_PER_LINE;
dy /= SCROLL_POINTS_PER_LINE;
}
+ if (!isfinite(dx) || !isfinite(dy) || (dx == 0 && dy == 0)) return;
w->mods = cocoa_mods(event.modifierFlags);
w->pointer = event_pos(w, event);
- if (dy != 0.0) {
- struct yasl_input_event input_ev = {
- .kind = YASL_INPUT_MOUSE,
- .surface = &w->base,
- .mouse = {
- .pos = w->pointer,
- .button = dy > 0.0 ? 4 : 5,
- .kind = YASL_PTR_SCROLL,
- .mods = w->mods,
- .scroll_delta = (float)dy,
- .scroll_lines = (int)dy,
- },
- };
- input_queue_push(&w->input, &input_ev);
- }
+ struct yasl_input_event input_ev = {
+ .kind = YASL_INPUT_MOUSE,
+ .surface = &w->base,
+ .mouse = {
+ .pos = w->pointer,
+ .kind = YASL_PTR_SCROLL,
+ .mods = w->mods,
+ .scroll = {dx, dy},
+ },
+ };
+ input_queue_push(&w->input, &input_ev);
}
static NSString *plain_string(id string)
@@ -1409,7 +1413,6 @@ static void surface_free(struct cocoa_surface *w)
[w->view release];
[w->controller release];
[w->window release];
- free(w->key_text);
free(w->preedit);
free(w->pixels);
free(w);