-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTWindow.extern.c
286 lines (243 loc) · 10.7 KB
/
CTWindow.extern.c
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
#include "CTWindow.extern.h"
#import <Cocoa/Cocoa.h>
#import "CTWindow.extern.m"
Bool compareKeys_CTWindow(CocoaKey_CocoaDef aKey, CocoaKey_CocoaDef anotherKey) {
return ((int)aKey == (int)anotherKey);
}
@interface NSEvent (DeviceDelta)
- (float)deviceDeltaX;
- (float)deviceDeltaY;
@end
bool dispatchEventToTimber(NSEvent* event) {
InputEvent_CocoaDef receivedEvent; //should be global if has to be a root for GC
if ([event type] == NSLeftMouseDown) {
Position_CocoaDef pos;
NEW (Position_CocoaDef, pos, WORDS(sizeof(struct Position_CocoaDef)));
pos->GCINFO = __GC__Position_CocoaDef;
NSPoint p = [event locationInWindow];
pos->x_CocoaDef = p.x;
pos->y_CocoaDef = p.y;
DEBUG("C detects mouse at %f , %f", p.x, p.y);
_MouseClicked_CocoaDef clicked;
NEW (_MouseClicked_CocoaDef, clicked, WORDS(sizeof(struct _MouseClicked_CocoaDef)));
clicked->GCINFO = __GC___MouseClicked_CocoaDef;
clicked->Tag = 3;
clicked->a = pos;
NEW (InputEvent_CocoaDef, receivedEvent, WORDS(sizeof(struct _MouseEvent_CocoaDef)));
((_MouseEvent_CocoaDef)receivedEvent)->GCINFO = __GC___MouseEvent_CocoaDef;
((_MouseEvent_CocoaDef)receivedEvent)->Tag = 1;
((_MouseEvent_CocoaDef)receivedEvent)->a = (MouseEventType_CocoaDef)clicked;
DEBUG("Event is to be sent for window nr %d", [event windowNumber]);
} else if ([event type] == NSKeyDown || [event type] == NSFlagsChanged) {
unsigned short code = [event keyCode];
if (code > 126)
return false;
_KeyPressed_CocoaDef pressed;
NEW (_KeyPressed_CocoaDef, pressed, WORDS(sizeof(struct _KeyPressed_CocoaDef)));
pressed->GCINFO = __GC___KeyPressed_CocoaDef;
pressed->Tag = 1;
pressed->a = (CocoaKey_CocoaDef)(POLY)(int)code;
DEBUG("KEY CODE %d\n", [event keyCode]);
NEW (InputEvent_CocoaDef, receivedEvent, WORDS(sizeof(struct _KeyEvent_CocoaDef)));
((_KeyEvent_CocoaDef)receivedEvent)->GCINFO = __GC___KeyEvent_CocoaDef;
((_KeyEvent_CocoaDef)receivedEvent)->Tag = 0;
((_KeyEvent_CocoaDef)receivedEvent)->a = (KeyEventType_CocoaDef)pressed;
} else if ([event type] == NSKeyUp) {
unsigned short code = [event keyCode];
if (code > 126)
return false;
_KeyReleased_CocoaDef released;
NEW (_KeyReleased_CocoaDef, released, WORDS(sizeof(struct _KeyReleased_CocoaDef)));
released->GCINFO = __GC___KeyReleased_CocoaDef;
released->Tag = 0;
released->a = (CocoaKey_CocoaDef)(POLY)(int)code;
DEBUG("KEY CODE %d\n", [event keyCode]);
NEW (InputEvent_CocoaDef, receivedEvent, WORDS(sizeof(struct _KeyEvent_CocoaDef)));
((_KeyEvent_CocoaDef)receivedEvent)->GCINFO = __GC___KeyEvent_CocoaDef;
((_KeyEvent_CocoaDef)receivedEvent)->Tag = 0;
((_KeyEvent_CocoaDef)receivedEvent)->a = (KeyEventType_CocoaDef)released;
} else if ([event type] == NSMouseMoved) {
Position_CocoaDef pos;
NEW (Position_CocoaDef, pos, WORDS(sizeof(struct Position_CocoaDef)));
pos->GCINFO = __GC__Position_CocoaDef;
NSPoint p = [event locationInWindow];
pos->x_CocoaDef = p.x;
pos->y_CocoaDef = p.y;
_MouseMoved_CocoaDef clicked;
NEW (_MouseMoved_CocoaDef, clicked, WORDS(sizeof(struct _MouseMoved_CocoaDef)));
clicked->GCINFO = __GC___MouseMoved_CocoaDef;
clicked->Tag = 0;
clicked->a = pos;
NEW (InputEvent_CocoaDef, receivedEvent, WORDS(sizeof(struct _MouseEvent_CocoaDef)));
((_MouseEvent_CocoaDef)receivedEvent)->GCINFO = __GC___MouseEvent_CocoaDef;
((_MouseEvent_CocoaDef)receivedEvent)->Tag = 1;
((_MouseEvent_CocoaDef)receivedEvent)->a = (MouseEventType_CocoaDef)clicked;
} else if ([event type] == NSScrollWheel) {
DEBUG("Scrolling event! \n");
NSPoint p = [event locationInWindow];
Position_CocoaDef position;
NEW(Position_CocoaDef, position, WORDS(sizeof(struct Position_CocoaDef)));
position->GCINFO = __GC__Position_CocoaDef;
position->x_CocoaDef = p.x;
position->y_CocoaDef = p.y;
_MouseWheelScroll_CocoaDef scroll;
NEW (_MouseWheelScroll_CocoaDef, scroll, WORDS(sizeof(struct _MouseWheelScroll_CocoaDef)));
scroll->GCINFO = __GC___MouseWheelScroll_CocoaDef;
scroll->Tag = 4;
scroll->a = position;
float deltaX;
float deltaY;
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1070
if ([event hasPreciseScrollingDeltas]) {
deltaX = [event scrollingDeltaX];
deltaY = [event scrollingDeltaY];
}
if ([event isDirectionInvertedFromDevice]) {
deltaX *= -1;
deltaY *= -1;
}
#else
deltaX = -1 * [event deviceDeltaX];
deltaY = -1 * [event deviceDeltaY];
#endif
scroll->b = deltaX;
scroll->c = deltaY;
NEW (InputEvent_CocoaDef, receivedEvent, WORDS(sizeof(struct _MouseEvent_CocoaDef)));
((_MouseEvent_CocoaDef)receivedEvent)->GCINFO = __GC___MouseEvent_CocoaDef;
((_MouseEvent_CocoaDef)receivedEvent)->Tag = 1;
((_MouseEvent_CocoaDef)receivedEvent)->a = (MouseEventType_CocoaDef)scroll;
} else {
// printf("Event of type %d was discarded\n", [event type]);
return false;
}
App_CocoaDef app = getApp();
return app->l_App_CocoaDef_AppImpl_CocoaDef_CocoaDef->sendInputEvent_CocoaDef(
app->l_App_CocoaDef_AppImpl_CocoaDef_CocoaDef, (InputEvent_CocoaDef)receivedEvent,
[event windowNumber], 0);
}
// --------- Window ----------------------------------------------
static WindowDelegate *delegate;
TUP2 initCocoaWindow_CTWindow(World w, Int dummy) {
DEBUG("Initializing window...");
__block CocoaWindow *window;
// Keep this as sync to ensure window is completely created before use.
// Also, with async there would be a risk of GC moving wnd before it is accessed.
dispatch_sync(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRect frameRect = NSMakeRect(0, [[[NSScreen screens] objectAtIndex: 0] frame].size.height, 1, 1);
NSUInteger styleMask = (NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask);
window = [[CocoaWindow alloc] initWithContentRect:frameRect styleMask:styleMask
backing:NSBackingStoreBuffered defer:NO];
[window setTitle:@"CocoaWindow"];
[window setEventDispatcher:dispatchEventToTimber];
[window setAcceptsMouseMovedEvents:YES];
if (!delegate)
delegate = [[WindowDelegate alloc] init];
[window setDelegate:delegate];
[window makeKeyAndOrderFront:window];
[pool drain];
});
TUP2 initResult;
NEW (TUP2, initResult, WORDS(sizeof(struct TUP2)));
initResult->GCINFO = __GC__TUP2+((5 * (((BITS32)1 | (BITS32)2))));
initResult->a = (POLY)window;
initResult->b = (POLY)([window windowNumber]);
return initResult;
}
TUP0 destroyCocoaWindow_CTWindow(Int cocoaRef, Int dummy) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
[thisWindow close];
[pool drain];
});
}
TUP0 windowSetContentView_CTWindow(Int windowRef, Int cmpRef, Int dummy) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *wnd = (CocoaWindow*) windowRef;
NSView *view = (NSView*) cmpRef;
[wnd setContentView: view];
[pool release];
});
}
TUP0 windowSetVisible_CTWindow(Int cocoaRef, Int dummy) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
[thisWindow orderFront: NULL];
[pool drain];
});
}
TUP0 windowSetHidden_CTWindow(Int cocoaRef, Int dummy) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
[thisWindow orderOut: NULL];
[pool drain];
});
}
TUP0 windowSetFocus_CTWindow(Int cocoaRef, Int cmpRef, Int dummy) {
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
NSView *responder = (NSView*) cmpRef;
[thisWindow makeFirstResponder: responder];
[pool drain];
});
}
TUP0 windowSetSize_CTWindow (Int cocoaRef, Size_CocoaDef size, Int dummy) {
int width = size->width_CocoaDef;
int height = size->height_CocoaDef+22;
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
NSRect frame = [thisWindow frame];
frame.origin.y -= (height - frame.size.height);
frame.size.height = height;
frame.size.width = width;
[thisWindow setFrame: frame display: YES];
[pool drain];
});
}
TUP0 windowSetPosition_CTWindow (Int cocoaRef, Position_CocoaDef pos, Int dummy) {
int y = pos->y_CocoaDef;
int x = pos->x_CocoaDef;
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
int screenHeight = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
int windowHeight = [thisWindow frame].size.height;
int newY = screenHeight - y - windowHeight;
NSPoint p = NSMakePoint(x,newY);
[thisWindow setFrameOrigin:p];
[pool drain];
});
}
TUP0 windowSetResizable_CTWindow (Int cocoaRef, Bool resizable, Int dummy) {
NSUInteger styleMask;
if (resizable) {
styleMask = (NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask);
} else {
styleMask = (NSTitledWindowMask | NSClosableWindowMask);
}
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
[thisWindow setStyleMask: styleMask];
[pool drain];
});
}
TUP0 windowSetTitle_CTWindow (Int cocoaRef, LIST title, Int dummy) {
char *buf = listToChars(title);
dispatch_async(dispatch_get_main_queue(), ^{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CocoaWindow *thisWindow = (CocoaWindow*) cocoaRef;
[thisWindow setTitle: [NSString stringWithFormat:@"%s", buf]];
[pool drain];
free(buf);
});
}
void _init_external_CTWindow(void) {
// Do nothing
}