forked from kovidgoyal/kitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcocoa_window.m
2703 lines (2371 loc) · 92.5 KB
/
cocoa_window.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========================================================================
// GLFW 3.4 macOS - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2009-2019 Camilla Löwy <[email protected]>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
// It is fine to use C99 in this file because it will not be built with VS
//========================================================================
#include "internal.h"
#include "../kitty/monotonic.h"
#include <float.h>
#include <string.h>
static const char*
polymorphic_string_as_utf8(id string) {
if (string == nil) return "(nil)";
NSString* characters;
if ([string isKindOfClass:[NSAttributedString class]])
characters = [string string];
else
characters = (NSString*) string;
return [characters UTF8String];
}
static uint32_t
vk_code_to_functional_key_code(uint8_t key_code) { // {{{
switch(key_code) {
/* start vk to functional (auto generated by gen-key-constants.py do not edit) */
case 0x35: return GLFW_FKEY_ESCAPE;
case 0x24: return GLFW_FKEY_ENTER;
case 0x30: return GLFW_FKEY_TAB;
case 0x33: return GLFW_FKEY_BACKSPACE;
case 0x72: return GLFW_FKEY_INSERT;
case 0x75: return GLFW_FKEY_DELETE;
case 0x7b: return GLFW_FKEY_LEFT;
case 0x7c: return GLFW_FKEY_RIGHT;
case 0x7e: return GLFW_FKEY_UP;
case 0x7d: return GLFW_FKEY_DOWN;
case 0x74: return GLFW_FKEY_PAGE_UP;
case 0x79: return GLFW_FKEY_PAGE_DOWN;
case 0x73: return GLFW_FKEY_HOME;
case 0x77: return GLFW_FKEY_END;
case 0x39: return GLFW_FKEY_CAPS_LOCK;
case 0x47: return GLFW_FKEY_NUM_LOCK;
case 0x6e: return GLFW_FKEY_MENU;
case 0x7a: return GLFW_FKEY_F1;
case 0x78: return GLFW_FKEY_F2;
case 0x63: return GLFW_FKEY_F3;
case 0x76: return GLFW_FKEY_F4;
case 0x60: return GLFW_FKEY_F5;
case 0x61: return GLFW_FKEY_F6;
case 0x62: return GLFW_FKEY_F7;
case 0x64: return GLFW_FKEY_F8;
case 0x65: return GLFW_FKEY_F9;
case 0x6d: return GLFW_FKEY_F10;
case 0x67: return GLFW_FKEY_F11;
case 0x6f: return GLFW_FKEY_F12;
case 0x69: return GLFW_FKEY_F13;
case 0x6b: return GLFW_FKEY_F14;
case 0x71: return GLFW_FKEY_F15;
case 0x6a: return GLFW_FKEY_F16;
case 0x40: return GLFW_FKEY_F17;
case 0x4f: return GLFW_FKEY_F18;
case 0x50: return GLFW_FKEY_F19;
case 0x5a: return GLFW_FKEY_F20;
case 0x52: return GLFW_FKEY_KP_0;
case 0x53: return GLFW_FKEY_KP_1;
case 0x54: return GLFW_FKEY_KP_2;
case 0x55: return GLFW_FKEY_KP_3;
case 0x56: return GLFW_FKEY_KP_4;
case 0x57: return GLFW_FKEY_KP_5;
case 0x58: return GLFW_FKEY_KP_6;
case 0x59: return GLFW_FKEY_KP_7;
case 0x5b: return GLFW_FKEY_KP_8;
case 0x5c: return GLFW_FKEY_KP_9;
case 0x41: return GLFW_FKEY_KP_DECIMAL;
case 0x4b: return GLFW_FKEY_KP_DIVIDE;
case 0x43: return GLFW_FKEY_KP_MULTIPLY;
case 0x4e: return GLFW_FKEY_KP_SUBTRACT;
case 0x45: return GLFW_FKEY_KP_ADD;
case 0x4c: return GLFW_FKEY_KP_ENTER;
case 0x51: return GLFW_FKEY_KP_EQUAL;
case 0x38: return GLFW_FKEY_LEFT_SHIFT;
case 0x3b: return GLFW_FKEY_LEFT_CONTROL;
case 0x3a: return GLFW_FKEY_LEFT_ALT;
case 0x37: return GLFW_FKEY_LEFT_SUPER;
case 0x3c: return GLFW_FKEY_RIGHT_SHIFT;
case 0x3e: return GLFW_FKEY_RIGHT_CONTROL;
case 0x3d: return GLFW_FKEY_RIGHT_ALT;
case 0x36: return GLFW_FKEY_RIGHT_SUPER;
/* end vk to functional */
default:
return 0;
}
} // }}}
static uint32_t
vk_code_to_unicode(uint8_t key_code) { // {{{
switch(key_code) {
/* start vk to unicode (auto generated by gen-key-constants.py do not edit) */
case 0x0: return 0x61;
case 0x1: return 0x73;
case 0x2: return 0x64;
case 0x3: return 0x66;
case 0x4: return 0x68;
case 0x5: return 0x67;
case 0x6: return 0x7a;
case 0x7: return 0x78;
case 0x8: return 0x63;
case 0x9: return 0x76;
case 0xb: return 0x62;
case 0xc: return 0x71;
case 0xd: return 0x77;
case 0xe: return 0x65;
case 0xf: return 0x72;
case 0x10: return 0x79;
case 0x11: return 0x74;
case 0x12: return 0x31;
case 0x13: return 0x32;
case 0x14: return 0x33;
case 0x15: return 0x34;
case 0x16: return 0x36;
case 0x17: return 0x35;
case 0x18: return 0x3d;
case 0x19: return 0x39;
case 0x1a: return 0x37;
case 0x1b: return 0x2d;
case 0x1c: return 0x38;
case 0x1d: return 0x30;
case 0x1e: return 0x5d;
case 0x1f: return 0x6f;
case 0x20: return 0x75;
case 0x21: return 0x5b;
case 0x22: return 0x69;
case 0x23: return 0x70;
case 0x25: return 0x6c;
case 0x26: return 0x6a;
case 0x27: return 0x27;
case 0x28: return 0x6b;
case 0x29: return 0x3b;
case 0x2a: return 0x5c;
case 0x2b: return 0x2c;
case 0x2c: return 0x2f;
case 0x2d: return 0x6e;
case 0x2e: return 0x6d;
case 0x2f: return 0x2e;
case 0x31: return 0x20;
case 0x32: return 0x60;
/* end vk to unicode */
default:
return 0;
}
} // }}}
static uint32_t
mac_ucode_to_functional(uint32_t key_code) { // {{{
switch(key_code) {
/* start macu to functional (auto generated by gen-key-constants.py do not edit) */
case NSCarriageReturnCharacter: return GLFW_FKEY_ENTER;
case NSTabCharacter: return GLFW_FKEY_TAB;
case NSBackspaceCharacter: return GLFW_FKEY_BACKSPACE;
case NSInsertFunctionKey: return GLFW_FKEY_INSERT;
case NSDeleteFunctionKey: return GLFW_FKEY_DELETE;
case NSLeftArrowFunctionKey: return GLFW_FKEY_LEFT;
case NSRightArrowFunctionKey: return GLFW_FKEY_RIGHT;
case NSUpArrowFunctionKey: return GLFW_FKEY_UP;
case NSDownArrowFunctionKey: return GLFW_FKEY_DOWN;
case NSPageUpFunctionKey: return GLFW_FKEY_PAGE_UP;
case NSPageDownFunctionKey: return GLFW_FKEY_PAGE_DOWN;
case NSHomeFunctionKey: return GLFW_FKEY_HOME;
case NSEndFunctionKey: return GLFW_FKEY_END;
case NSScrollLockFunctionKey: return GLFW_FKEY_SCROLL_LOCK;
case NSClearLineFunctionKey: return GLFW_FKEY_NUM_LOCK;
case NSPrintScreenFunctionKey: return GLFW_FKEY_PRINT_SCREEN;
case NSPauseFunctionKey: return GLFW_FKEY_PAUSE;
case NSMenuFunctionKey: return GLFW_FKEY_MENU;
case NSF1FunctionKey: return GLFW_FKEY_F1;
case NSF2FunctionKey: return GLFW_FKEY_F2;
case NSF3FunctionKey: return GLFW_FKEY_F3;
case NSF4FunctionKey: return GLFW_FKEY_F4;
case NSF5FunctionKey: return GLFW_FKEY_F5;
case NSF6FunctionKey: return GLFW_FKEY_F6;
case NSF7FunctionKey: return GLFW_FKEY_F7;
case NSF8FunctionKey: return GLFW_FKEY_F8;
case NSF9FunctionKey: return GLFW_FKEY_F9;
case NSF10FunctionKey: return GLFW_FKEY_F10;
case NSF11FunctionKey: return GLFW_FKEY_F11;
case NSF12FunctionKey: return GLFW_FKEY_F12;
case NSF13FunctionKey: return GLFW_FKEY_F13;
case NSF14FunctionKey: return GLFW_FKEY_F14;
case NSF15FunctionKey: return GLFW_FKEY_F15;
case NSF16FunctionKey: return GLFW_FKEY_F16;
case NSF17FunctionKey: return GLFW_FKEY_F17;
case NSF18FunctionKey: return GLFW_FKEY_F18;
case NSF19FunctionKey: return GLFW_FKEY_F19;
case NSF20FunctionKey: return GLFW_FKEY_F20;
case NSF21FunctionKey: return GLFW_FKEY_F21;
case NSF22FunctionKey: return GLFW_FKEY_F22;
case NSF23FunctionKey: return GLFW_FKEY_F23;
case NSF24FunctionKey: return GLFW_FKEY_F24;
case NSF25FunctionKey: return GLFW_FKEY_F25;
case NSF26FunctionKey: return GLFW_FKEY_F26;
case NSF27FunctionKey: return GLFW_FKEY_F27;
case NSF28FunctionKey: return GLFW_FKEY_F28;
case NSF29FunctionKey: return GLFW_FKEY_F29;
case NSF30FunctionKey: return GLFW_FKEY_F30;
case NSF31FunctionKey: return GLFW_FKEY_F31;
case NSF32FunctionKey: return GLFW_FKEY_F32;
case NSF33FunctionKey: return GLFW_FKEY_F33;
case NSF34FunctionKey: return GLFW_FKEY_F34;
case NSF35FunctionKey: return GLFW_FKEY_F35;
case NSEnterCharacter: return GLFW_FKEY_KP_ENTER;
/* end macu to functional */
default:
return 0;
}
} // }}}
static bool
is_surrogate(UniChar uc) { return (uc - 0xd800u) < 2048u; }
static uint32_t
get_first_codepoint(UniChar *utf16, UniCharCount num) {
if (!num) return 0;
if (!is_surrogate(*utf16)) return *utf16;
if (CFStringIsSurrogateHighCharacter(*utf16) && num > 1 && CFStringIsSurrogateLowCharacter(utf16[1])) return CFStringGetLongCharacterForSurrogatePair(utf16[0], utf16[1]);
return 0;
}
static bool
is_pua_char(uint32_t ch) {
return (0xE000 <= ch && ch <= 0xF8FF) || (0xF0000 <= ch && ch <= 0xFFFFF) || (0x100000 <= ch && ch <= 0x10FFFF);
}
uint32_t
vk_to_unicode_key_with_current_layout(uint16_t keycode)
{
UInt32 dead_key_state = 0;
UniChar characters[256];
UniCharCount character_count = 0;
uint32_t ans = vk_code_to_functional_key_code(keycode);
if (ans) return ans;
if (UCKeyTranslate([(NSData*) _glfw.ns.unicodeData bytes],
keycode,
kUCKeyActionDisplay,
0,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&dead_key_state,
arraysz(characters),
&character_count,
characters) == noErr) {
uint32_t cp = get_first_codepoint(characters, character_count);
if (cp) {
if (cp < 32 || (0xF700 <= cp && cp <= 0xF8FF)) return mac_ucode_to_functional(cp);
if (cp >= 32 && !is_pua_char(cp)) return cp;
}
}
return vk_code_to_unicode(keycode);
}
// Returns the style mask corresponding to the window settings
//
static NSUInteger getStyleMask(_GLFWwindow* window)
{
NSUInteger styleMask = NSWindowStyleMaskMiniaturizable;
if (window->monitor || !window->decorated)
styleMask |= NSWindowStyleMaskBorderless;
else
{
styleMask |= NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable;
if (window->resizable)
styleMask |= NSWindowStyleMaskResizable;
}
return styleMask;
}
CGDirectDisplayID displayIDForWindow(_GLFWwindow *w) {
NSWindow *nw = w->ns.object;
NSDictionary *dict = [nw.screen deviceDescription];
NSNumber *displayIDns = dict[@"NSScreenNumber"];
if (displayIDns) return [displayIDns unsignedIntValue];
return (CGDirectDisplayID)-1;
}
static unsigned long long display_link_shutdown_timer = 0;
#define DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL s_to_monotonic_t(30ll)
void
_glfwShutdownCVDisplayLink(unsigned long long timer_id UNUSED, void *user_data UNUSED) {
display_link_shutdown_timer = 0;
for (size_t i = 0; i < _glfw.ns.displayLinks.count; i++) {
_GLFWDisplayLinkNS *dl = &_glfw.ns.displayLinks.entries[i];
if (dl->displayLink) CVDisplayLinkStop(dl->displayLink);
dl->lastRenderFrameRequestedAt = 0;
dl->first_unserviced_render_frame_request_at = 0;
}
}
static void
requestRenderFrame(_GLFWwindow *w, GLFWcocoarenderframefun callback) {
if (!callback) {
w->ns.renderFrameRequested = false;
w->ns.renderFrameCallback = NULL;
return;
}
w->ns.renderFrameCallback = callback;
w->ns.renderFrameRequested = true;
CGDirectDisplayID displayID = displayIDForWindow(w);
if (display_link_shutdown_timer) {
_glfwPlatformUpdateTimer(display_link_shutdown_timer, DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, true);
} else {
display_link_shutdown_timer = _glfwPlatformAddTimer(DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL, false, _glfwShutdownCVDisplayLink, NULL, NULL);
}
monotonic_t now = glfwGetTime();
bool found_display_link = false;
_GLFWDisplayLinkNS *dl = NULL;
for (size_t i = 0; i < _glfw.ns.displayLinks.count; i++) {
dl = &_glfw.ns.displayLinks.entries[i];
if (dl->displayID == displayID) {
found_display_link = true;
dl->lastRenderFrameRequestedAt = now;
if (!dl->first_unserviced_render_frame_request_at) dl->first_unserviced_render_frame_request_at = now;
if (!CVDisplayLinkIsRunning(dl->displayLink)) CVDisplayLinkStart(dl->displayLink);
else if (now - dl->first_unserviced_render_frame_request_at > s_to_monotonic_t(1ll)) {
// display link is stuck need to recreate it because Apple cant even
// get a simple timer right
CVDisplayLinkRelease(dl->displayLink); dl->displayLink = nil;
dl->first_unserviced_render_frame_request_at = now;
_glfw_create_cv_display_link(dl);
_glfwInputError(GLFW_PLATFORM_ERROR,
"CVDisplayLink stuck possibly because of sleep/screensaver + Apple's incompetence, recreating.");
if (!CVDisplayLinkIsRunning(dl->displayLink)) CVDisplayLinkStart(dl->displayLink);
}
} else if (dl->displayLink && dl->lastRenderFrameRequestedAt && now - dl->lastRenderFrameRequestedAt >= DISPLAY_LINK_SHUTDOWN_CHECK_INTERVAL) {
CVDisplayLinkStop(dl->displayLink);
dl->lastRenderFrameRequestedAt = 0;
dl->first_unserviced_render_frame_request_at = 0;
}
}
if (!found_display_link) {
dl = _glfw_create_display_link(displayID);
if (dl) {
dl->lastRenderFrameRequestedAt = now;
dl->first_unserviced_render_frame_request_at = now;
if (!CVDisplayLinkIsRunning(dl->displayLink)) CVDisplayLinkStart(dl->displayLink);
}
}
}
void
_glfwRestartDisplayLinks(void) {
_GLFWwindow* window;
for (window = _glfw.windowListHead; window; window = window->next) {
if (window->ns.renderFrameRequested && window->ns.renderFrameCallback) {
requestRenderFrame(window, window->ns.renderFrameCallback);
}
}
}
// Returns whether the cursor is in the content area of the specified window
//
static bool cursorInContentArea(_GLFWwindow* window)
{
const NSPoint pos = [window->ns.object mouseLocationOutsideOfEventStream];
return [window->ns.view mouse:pos inRect:[window->ns.view frame]];
}
// Hides the cursor if not already hidden
//
static void hideCursor(_GLFWwindow* window UNUSED)
{
if (!_glfw.ns.cursorHidden)
{
[NSCursor hide];
_glfw.ns.cursorHidden = true;
}
}
// Shows the cursor if not already shown
//
static void showCursor(_GLFWwindow* window UNUSED)
{
if (_glfw.ns.cursorHidden)
{
[NSCursor unhide];
_glfw.ns.cursorHidden = false;
}
}
// Updates the cursor image according to its cursor mode
//
static void updateCursorImage(_GLFWwindow* window)
{
if (window->cursorMode == GLFW_CURSOR_NORMAL)
{
showCursor(window);
if (window->cursor)
[(NSCursor*) window->cursor->ns.object set];
else
[[NSCursor arrowCursor] set];
}
else
hideCursor(window);
}
// Apply chosen cursor mode to a focused window
//
static void updateCursorMode(_GLFWwindow* window)
{
if (window->cursorMode == GLFW_CURSOR_DISABLED)
{
_glfw.ns.disabledCursorWindow = window;
_glfwPlatformGetCursorPos(window,
&_glfw.ns.restoreCursorPosX,
&_glfw.ns.restoreCursorPosY);
_glfwCenterCursorInContentArea(window);
CGAssociateMouseAndMouseCursorPosition(false);
}
else if (_glfw.ns.disabledCursorWindow == window)
{
_glfw.ns.disabledCursorWindow = NULL;
CGAssociateMouseAndMouseCursorPosition(true);
_glfwPlatformSetCursorPos(window,
_glfw.ns.restoreCursorPosX,
_glfw.ns.restoreCursorPosY);
}
if (cursorInContentArea(window))
updateCursorImage(window);
}
// Make the specified window and its video mode active on its monitor
//
static void acquireMonitor(_GLFWwindow* window)
{
_glfwSetVideoModeNS(window->monitor, &window->videoMode);
const CGRect bounds = CGDisplayBounds(window->monitor->ns.displayID);
const NSRect frame = NSMakeRect(bounds.origin.x,
_glfwTransformYNS(bounds.origin.y + bounds.size.height - 1),
bounds.size.width,
bounds.size.height);
[window->ns.object setFrame:frame display:YES];
_glfwInputMonitorWindow(window->monitor, window);
}
// Remove the window and restore the original video mode
//
static void releaseMonitor(_GLFWwindow* window)
{
if (window->monitor->window != window)
return;
_glfwInputMonitorWindow(window->monitor, NULL);
_glfwRestoreVideoModeNS(window->monitor);
}
// Translates macOS key modifiers into GLFW ones
//
static int
translateFlags(NSUInteger flags)
{
int mods = 0;
if (flags & NSEventModifierFlagShift)
mods |= GLFW_MOD_SHIFT;
if (flags & NSEventModifierFlagControl)
mods |= GLFW_MOD_CONTROL;
if (flags & NSEventModifierFlagOption)
mods |= GLFW_MOD_ALT;
if (flags & NSEventModifierFlagCommand)
mods |= GLFW_MOD_SUPER;
if (flags & NSEventModifierFlagCapsLock)
mods |= GLFW_MOD_CAPS_LOCK;
return mods;
}
static const char*
format_mods(int mods) {
static char buf[128];
char *p = buf, *s;
#define pr(x) p += snprintf(p, sizeof(buf) - (p - buf) - 1, x)
pr("mods: ");
s = p;
if (mods & GLFW_MOD_CONTROL) pr("ctrl+");
if (mods & GLFW_MOD_ALT) pr("alt+");
if (mods & GLFW_MOD_SHIFT) pr("shift+");
if (mods & GLFW_MOD_SUPER) pr("super+");
if (mods & GLFW_MOD_CAPS_LOCK) pr("capslock+");
if (mods & GLFW_MOD_NUM_LOCK) pr("numlock+");
if (p == s) pr("none");
else p--;
pr(" ");
#undef pr
return buf;
}
static const char*
format_text(const char *src) {
static char buf[256];
char *p = buf;
const char *last_char = buf + sizeof(buf) - 1;
if (!src[0]) return "<none>";
while (*src) {
int num = snprintf(p, sizeof(buf) - (p - buf), "0x%x ", (unsigned char)*(src++));
if (num < 0) return "<error>";
if (p + num >= last_char) break;
p += num;
}
if (p != buf) *(--p) = 0;
return buf;
}
static const char*
safe_name_for_keycode(unsigned int keycode) {
const char *ans = _glfwPlatformGetNativeKeyName(keycode);
if (!ans) return "<noname>";
if ((1 <= ans[0] && ans[0] <= 31) || ans[0] == 127) ans = "<cc>";
return ans;
}
// Translates a macOS keycode to a GLFW keycode
//
static uint32_t
translateKey(uint16_t vk_key, bool apply_keymap)
{
if (apply_keymap) return vk_to_unicode_key_with_current_layout(vk_key);
uint32_t ans = vk_code_to_functional_key_code(vk_key);
if (!ans) ans = vk_code_to_unicode(vk_key);
return ans;
}
static NSRect
get_window_size_without_border_in_logical_pixels(_GLFWwindow *window) {
return [window->ns.object contentRectForFrameRect:[window->ns.object frame]];
}
// Defines a constant for empty ranges in NSTextInputClient
//
static const NSRange kEmptyRange = { NSNotFound, 0 };
// Delegate for window related notifications {{{
@interface GLFWWindowDelegate : NSObject
{
_GLFWwindow* window;
}
- (instancetype)initWithGlfwWindow:(_GLFWwindow *)initWindow;
@end
@implementation GLFWWindowDelegate
- (instancetype)initWithGlfwWindow:(_GLFWwindow *)initWindow
{
self = [super init];
if (self != nil)
window = initWindow;
return self;
}
- (BOOL)windowShouldClose:(id)sender
{
(void)sender;
_glfwInputWindowCloseRequest(window);
return NO;
}
- (void)windowDidResize:(NSNotification *)notification
{
(void)notification;
if (window->context.client != GLFW_NO_API)
[window->context.nsgl.object update];
if (_glfw.ns.disabledCursorWindow == window)
_glfwCenterCursorInContentArea(window);
const int maximized = [window->ns.object isZoomed];
if (window->ns.maximized != maximized)
{
window->ns.maximized = maximized;
_glfwInputWindowMaximize(window, maximized);
}
const NSRect contentRect = get_window_size_without_border_in_logical_pixels(window);
const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect];
if (fbRect.size.width != window->ns.fbWidth ||
fbRect.size.height != window->ns.fbHeight)
{
window->ns.fbWidth = (int)fbRect.size.width;
window->ns.fbHeight = (int)fbRect.size.height;
_glfwInputFramebufferSize(window, (int)fbRect.size.width, (int)fbRect.size.height);
}
if (contentRect.size.width != window->ns.width ||
contentRect.size.height != window->ns.height)
{
window->ns.width = (int)contentRect.size.width;
window->ns.height = (int)contentRect.size.height;
_glfwInputWindowSize(window, (int)contentRect.size.width, (int)contentRect.size.height);
}
}
- (void)windowDidMove:(NSNotification *)notification
{
(void)notification;
if (window->context.client != GLFW_NO_API)
[window->context.nsgl.object update];
if (_glfw.ns.disabledCursorWindow == window)
_glfwCenterCursorInContentArea(window);
int x, y;
_glfwPlatformGetWindowPos(window, &x, &y);
_glfwInputWindowPos(window, x, y);
}
- (void)windowDidChangeOcclusionState:(NSNotification *)notification
{
(void)notification;
_glfwInputWindowOcclusion(window, !([window->ns.object occlusionState] & NSWindowOcclusionStateVisible));
}
- (void)windowDidMiniaturize:(NSNotification *)notification
{
(void)notification;
if (window->monitor)
releaseMonitor(window);
_glfwInputWindowIconify(window, true);
}
- (void)windowDidDeminiaturize:(NSNotification *)notification
{
(void)notification;
if (window->monitor)
acquireMonitor(window);
_glfwInputWindowIconify(window, false);
}
- (void)windowDidBecomeKey:(NSNotification *)notification
{
(void)notification;
if (_glfw.ns.disabledCursorWindow == window)
_glfwCenterCursorInContentArea(window);
_glfwInputWindowFocus(window, true);
updateCursorMode(window);
if (window->cursorMode == GLFW_CURSOR_HIDDEN) hideCursor(window);
if (_glfw.ns.disabledCursorWindow != window && cursorInContentArea(window))
{
double x = 0, y = 0;
_glfwPlatformGetCursorPos(window, &x, &y);
_glfwInputCursorPos(window, x, y);
}
}
- (void)windowDidResignKey:(NSNotification *)notification
{
(void)notification;
if (window->monitor && window->autoIconify)
_glfwPlatformIconifyWindow(window);
showCursor(window);
_glfwInputWindowFocus(window, false);
// IME is cancelled when losing the focus
if ([window->ns.view hasMarkedText]) {
[[window->ns.view inputContext] discardMarkedText];
[window->ns.view unmarkText];
GLFWkeyevent dummy = {.action = GLFW_RELEASE, .ime_state = GLFW_IME_PREEDIT_CHANGED};
_glfwInputKeyboard(window, &dummy);
_glfw.ns.text[0] = 0;
}
}
- (void)windowDidChangeScreen:(NSNotification *)notification
{
(void)notification;
if (window->ns.renderFrameRequested && window->ns.renderFrameCallback) {
// Ensure that if the window changed its monitor, CVDisplayLink
// is running for the new monitor
requestRenderFrame(window, window->ns.renderFrameCallback);
}
}
@end // }}}
// Text input context class for the GLFW content view {{{
@interface GLFWTextInputContext : NSTextInputContext
@end
@implementation GLFWTextInputContext
- (void)doCommandBySelector:(SEL)selector
{
// interpretKeyEvents: May call insertText: or doCommandBySelector:.
// With the default macOS keybindings, pressing certain key combinations
// (e.g. Ctrl+/, Ctrl+Cmd+Down/Left/Right) will produce a beep sound.
debug_key("\n\tTextInputCtx: doCommandBySelector: (%s)\n", [NSStringFromSelector(selector) UTF8String]);
}
@end // }}}
// Content view class for the GLFW window {{{
@interface GLFWContentView : NSView <NSTextInputClient>
{
_GLFWwindow* window;
NSTrackingArea* trackingArea;
GLFWTextInputContext* input_context;
NSMutableAttributedString* markedText;
NSRect markedRect;
bool marked_text_cleared_by_insert;
int in_key_handler;
NSString *input_source_at_last_key_event;
}
- (void) removeGLFWWindow;
- (instancetype)initWithGlfwWindow:(_GLFWwindow *)initWindow;
@end
@implementation GLFWContentView
- (instancetype)initWithGlfwWindow:(_GLFWwindow *)initWindow
{
self = [super init];
if (self != nil)
{
window = initWindow;
trackingArea = nil;
input_context = [[GLFWTextInputContext alloc] initWithClient:self];
markedText = [[NSMutableAttributedString alloc] init];
markedRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
input_source_at_last_key_event = nil;
in_key_handler = 0;
[self updateTrackingAreas];
[self registerForDraggedTypes:@[NSPasteboardTypeFileURL, NSPasteboardTypeString]];
}
return self;
}
- (void)dealloc
{
[trackingArea release];
[markedText release];
if (input_source_at_last_key_event) [input_source_at_last_key_event release];
[input_context release];
[super dealloc];
}
- (void) removeGLFWWindow
{
window = NULL;
}
- (_GLFWwindow*)glfwWindow {
return window;
}
- (BOOL)isOpaque
{
return window && [window->ns.object isOpaque];
}
- (BOOL)canBecomeKeyView
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (void) viewWillStartLiveResize
{
if (!window) return;
_glfwInputLiveResize(window, true);
}
- (void)viewDidEndLiveResize
{
if (!window) return;
_glfwInputLiveResize(window, false);
}
- (BOOL)wantsUpdateLayer
{
return YES;
}
- (void)updateLayer
{
if (!window) return;
if (window->context.client != GLFW_NO_API) {
@try {
[window->context.nsgl.object update];
} @catch (NSException *e) {
_glfwInputError(GLFW_PLATFORM_ERROR,
"Failed to update NSGL Context object with error: %s (%s)",
[[e name] UTF8String], [[e reason] UTF8String]);
}
}
_glfwInputWindowDamage(window);
}
- (void)cursorUpdate:(NSEvent *)event
{
(void)event;
if (window) updateCursorImage(window);
}
- (BOOL)acceptsFirstMouse:(NSEvent *)event
{
(void)event;
return NO; // changed by Kovid, to follow cocoa platform conventions
}
- (void)mouseDown:(NSEvent *)event
{
if (!window) return;
_glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_LEFT,
GLFW_PRESS,
translateFlags([event modifierFlags]));
}
- (void)mouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)mouseUp:(NSEvent *)event
{
if (!window) return;
_glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_LEFT,
GLFW_RELEASE,
translateFlags([event modifierFlags]));
}
- (void)mouseMoved:(NSEvent *)event
{
if (!window) return;
if (window->cursorMode == GLFW_CURSOR_DISABLED)
{
const double dx = [event deltaX] - window->ns.cursorWarpDeltaX;
const double dy = [event deltaY] - window->ns.cursorWarpDeltaY;
_glfwInputCursorPos(window,
window->virtualCursorPosX + dx,
window->virtualCursorPosY + dy);
}
else
{
const NSRect contentRect = [window->ns.view frame];
// NOTE: The returned location uses base 0,1 not 0,0
const NSPoint pos = [event locationInWindow];
_glfwInputCursorPos(window, pos.x, contentRect.size.height - pos.y);
}
window->ns.cursorWarpDeltaX = 0;
window->ns.cursorWarpDeltaY = 0;
}
- (void)rightMouseDown:(NSEvent *)event
{
if (!window) return;
_glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_RIGHT,
GLFW_PRESS,
translateFlags([event modifierFlags]));
}
- (void)rightMouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)rightMouseUp:(NSEvent *)event
{
if (!window) return;
_glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_RIGHT,
GLFW_RELEASE,
translateFlags([event modifierFlags]));
}
- (void)otherMouseDown:(NSEvent *)event
{
if (!window) return;
_glfwInputMouseClick(window,
(int) [event buttonNumber],
GLFW_PRESS,
translateFlags([event modifierFlags]));
}
- (void)otherMouseDragged:(NSEvent *)event
{
[self mouseMoved:event];
}
- (void)otherMouseUp:(NSEvent *)event
{
if (!window) return;
_glfwInputMouseClick(window,
(int) [event buttonNumber],
GLFW_RELEASE,
translateFlags([event modifierFlags]));
}
- (void)mouseExited:(NSEvent *)event
{
(void)event;
if (!window) return;
_glfwInputCursorEnter(window, false);
}
- (void)mouseEntered:(NSEvent *)event
{
(void)event;
if (!window) return;
_glfwInputCursorEnter(window, true);
}
- (void)viewDidChangeBackingProperties
{
if (!window) return;
const NSRect contentRect = get_window_size_without_border_in_logical_pixels(window);
const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect];
if (fbRect.size.width != window->ns.fbWidth ||
fbRect.size.height != window->ns.fbHeight)
{
window->ns.fbWidth = (int)fbRect.size.width;
window->ns.fbHeight = (int)fbRect.size.height;
_glfwInputFramebufferSize(window, (int)fbRect.size.width, (int)fbRect.size.height);
}
const float xscale = fbRect.size.width / contentRect.size.width;
const float yscale = fbRect.size.height / contentRect.size.height;
if (xscale != window->ns.xscale || yscale != window->ns.yscale)
{
window->ns.xscale = xscale;
window->ns.yscale = yscale;
_glfwInputWindowContentScale(window, xscale, yscale);
if (window->ns.retina && window->ns.layer)
[window->ns.layer setContentsScale:[window->ns.object backingScaleFactor]];
}
}
- (void)drawRect:(NSRect)rect
{
(void)rect;
if (!window) return;