summaryrefslogtreecommitdiff
path: root/src/SFML/Window/Cocoa/WindowImplCocoa.mm
blob: 460c7652972fe68cc7d1b06a54a5fc7c0dd6f2ba (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2008 Lucas Soltic (elmerod@gmail.com) and Laurent Gomila (laurent.gom@gmail.com)
//
// 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.
//
////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#import <SFML/Window/Cocoa/WindowImplCocoa.hpp>
#import <SFML/Window/Cocoa/WindowController.h>
#import <SFML/Window/Cocoa/AppController.h>
#import <SFML/Window/WindowStyle.hpp>
#import <SFML/System.hpp>
#import <OpenGL/OpenGL.h>
#import <OpenGL/gl.h>
#import <Cocoa/Cocoa.h>
#import <iostream>


#pragma mark Notes

// ENABLE_ANTIALIASING macro :
//
// I use this to prevent the use of antialiasing
// as OpenGL context sharing does not allow only one of the
// shared OpenGL context to be shared. That means
// antialiasing could not be used without re-compiling
// the whole sfml-window library.
//
// I've no way to fix this for now.

// ENABLE_WINDOWIMPORT macro :
//
// I use this to disable the import of Cocoa windows
// as it does not work fine for now and is not
// a high priority.





namespace sf
{
namespace priv
{

// Do something only once (useful in loops)
#define ONCE(make) \
{ static int __done = 0;\
if (!__done) {\
make;\
__done = 1;\
} }

// Has the event been used or not ? If not, it must be sent to NSApp
enum {
	UsedEvent,
	UnusedEvent
};


////////////////////////////////////////////////////////////
/// Structure containing all the members I can't directly put in the class definition
/// because I would have to hide them in a #ifdef __OBJC__ block and the object
/// allocator would allocate space for it as it would be called from a C++ code
/// that wouldn't see these members
////////////////////////////////////////////////////////////
struct objc_members {
	WindowController *controller;
	SFWindow *window;
	NSOpenGLContext *context;
	NSOpenGLView *view;
};

////////////////////////////////////////////////////////////
/// Pointer to the shared OpenGL context
////////////////////////////////////////////////////////////
static NSOpenGLContext *sharedContext = nil;


////////////////////////////////////////////////////////////
/// Private function declarations
////////////////////////////////////////////////////////////
static SFWindow *		MakeWindow(WindowSettings& params, unsigned long style, VideoMode& mode, NSString *title);
static NSOpenGLContext *MakeOpenGLContext(WindowSettings& params);
static NSOpenGLView	*	MakeOpenGLView(SFWindow *window, NSOpenGLContext *context, WindowSettings& params);
static void				ConfigureWindow(SFWindow *window, NSOpenGLView *view, WindowController *controller);
static Key::Code		KeyForVirtualCode(unsigned short vCode);
static Key::Code		KeyForUnicode(unsigned short uniCode);
static bool				IsTextEvent(NSEvent *event);
static bool				MouseInside(SFWindow *window, NSView *view);
static NSPoint			MouseLocation(SFWindow *window);


////////////////////////////////////////////////////////////
/// Default constructor
/// (creates a dummy window to provide a valid OpenGL context)
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa() :
members(NULL),
useKeyRepeat(false),
mouseIn(false),
wheelStatus(0.0f),
fullscreen(false),
fullscreenMode(0, 0, 0),
desktopMode(0, 0, 0)
{
	Initialize();
	
    // We just want to have a valid support for an OpenGL context
	
	// So we create the OpenGL context
	WindowSettings params(0, 0, 0);
	members->context = MakeOpenGLContext(params);
	
	if (members->context != nil) {
		// Increase the reference counter for the shared OpenGL context
		sharedContext = [members->context retain];
		
		// Then we make it the current active OpenGL context
		SetActive();
	} else {
		std::cerr << "*** SFML: Unable to make the main shared OpenGL context" << std::endl;
	}
}


////////////////////////////////////////////////////////////
/// Create the window implementation from an existing control
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa(WindowHandle Handle, WindowSettings& params) :
members(NULL),
useKeyRepeat(false),
mouseIn(false),
wheelStatus(0.0f),
fullscreen(false),
fullscreenMode(0, 0, 0),
desktopMode(0, 0, 0)
{
	Initialize();
	
#if ENABLE_WINDOWIMPORT
	// Register ourselves for event handling
	[[AppController sharedController] registerWindow:this];
	
	// Make a WindowController to handle notifications
	members->controller = [[WindowController controllerWithWindow:this] retain];
	
	// Use existing window
	members->window = [static_cast <SFWindow *> (Handle) retain];
	
	if (members->window != nil) {		
		// We make the OpenGL context, associate it to the OpenGL view
		// and add the view to our window
		members->context = MakeOpenGLContext(params);
		
		if (members->context != nil) {
			members->view = MakeOpenGLView(members->window, members->context, params);
			
			if (members->view != nil) {
				// initial mouse state
				mouseIn = MouseInside(members->window, members->view);
				
				// Initialize myWidth and myHeight members from base class with the window size
				myWidth = (unsigned) [members->window frame].size.width;
				myHeight = (unsigned) [members->window frame].size.height;
			} else {
				//error(__FILE__, __LINE__, "failed to make the OpenGL view for the public window");
			}
		} else {
			//error(__FILE__, __LINE__, "failed to make the OpenGL context for the public window");
		}
	} else {
		//error(__FILE__, __LINE__, "invalid imported window");
	}
#else
	std::cerr << "*** SFML: making a sf::Window from a Cocoa one is not available in this version of the SFML" << std::endl;
#endif
}


////////////////////////////////////////////////////////////
/// Create the window implementation
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa(VideoMode Mode, const std::string& Title, unsigned long WindowStyle, WindowSettings& params) :
members(NULL),
useKeyRepeat(false),
mouseIn(false),
wheelStatus(0.0f),
fullscreen(WindowStyle & Style::Fullscreen),
fullscreenMode(0, 0, 0),
desktopMode(0, 0, 0)
{
	Initialize();
	
	// Make a WindowController to handle notifications
	members->controller = [[WindowController controllerWithWindow:this] retain];
	
    // Create a new window with given size, title and style
	// First we define some objects used for our window
	NSString *title = massert([NSString stringWithUTF8String:Title.c_str()]);
	
	// We create the window
	members->window = MakeWindow(params, WindowStyle, Mode, title);
	
	
	if (members->window != nil) {
		members->context = MakeOpenGLContext(params);
		
		if (members->context != nil) {
			// We make the OpenGL context, associate it to the OpenGL view
			// and add the view to our window
			members->view = MakeOpenGLView(members->window, members->context, params);
			
			if (members->view != nil) {
				// Set observers and some window settings
				ConfigureWindow(members->window, members->view, members->controller);
				
				// initial mouse state
				mouseIn = MouseInside(members->window, members->view);
				
				// We set the myWidth and myHeight members to the correct values
				myWidth = Mode.Width;
				myHeight = Mode.Height;
				
				if (WindowStyle & Style::Fullscreen) {
					fullscreenMode = Mode;
					
					// Using this because full screen window was not always
					// in front of the other application windows when unhiding app
					[members->window setLevel:NSFloatingWindowLevel];
				}
			} else {
				std::cerr << "*** SFML: failed to make the OpenGL view for the public window" << std::endl;
				[members->context release], members->context = nil;
				[sharedContext release];
				[members->window release], members->window = nil;
			}
		} else {
			std::cerr << "*** SFML: failed to make the OpenGL context for the public window" << std::endl;
			[members->window release], members->window = nil;
		}
	} else {
		std::cerr << "*** SFML: failed to make the public window" << std::endl;
	}
}


////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
WindowImplCocoa::~WindowImplCocoa()
{
    // Destroy the OpenGL context, the window and every resource allocated by this class
	Show(false);
	
	if (members) {
		if (members->window)
			[[NSNotificationCenter defaultCenter] removeObserver:members->window];
		if (members->view)
			[[NSNotificationCenter defaultCenter] removeObserver:members->view];
		[members->controller release];
	}
	
	[sharedContext release];
	if (members) {
		[members->context release];
		[members->view release];
		[members->window release];
	}
	
	[[AppController sharedController] unregisterWindow:this];
	free (members);
}


////////////////////////////////////////////////////////////
/// Check if there's an active context on the current thread
////////////////////////////////////////////////////////////
bool WindowImplCocoa::IsContextActive()
{
	return ([NSOpenGLContext currentContext] != NULL);
}


////////////////////////////////////////////////////////////
/// Handle a Cocoa NSEvent
////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleEvent(void *eventRef)
{
	if (eventRef == nil) {
		std::cerr << "*** SFML: cannot handle a NULL event. Returning." << std::endl;
		return;
	}
	
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	int eventStatus = UnusedEvent;
	
	switch ([event type]) {
		case NSKeyDown:
			eventStatus = HandleKeyDown(eventRef);
			break;
			
		case NSKeyUp:
			eventStatus = HandleKeyUp(eventRef);
			break;
			
		case NSFlagsChanged:
			eventStatus = HandleModifierKey(eventRef);
			break;
			
		case NSScrollWheel:
			eventStatus = HandleMouseWheel(eventRef);
			break;
		
		case NSLeftMouseDown:
		case NSRightMouseDown:
			eventStatus = HandleMouseDown(eventRef);
			break;
			
		case NSLeftMouseUp:
		case NSRightMouseUp:
			eventStatus = HandleMouseUp(eventRef);
			break;
			
		case NSMouseMoved:
		case NSLeftMouseDragged:
		case NSRightMouseDragged:
		case NSOtherMouseDragged:
			eventStatus = HandleMouseMove(eventRef);
			break;
			
		default:
			break;
	}
	
	if (eventStatus == UnusedEvent) {
		[NSApp sendEvent:event];
	}
}


////////////////////////////////////////////////////////////
/// Handle event sent by the default NSNotificationCenter
////////////////////////////////////////////////////////////
void WindowImplCocoa::HandleNotifiedEvent(Event& event)
{
	// Set myWidth and myHeight to correct value if
	// window size changed
	switch (event.Type) {
		case Event::Resized:
			myWidth = event.Size.Width;
			myHeight = event.Size.Height;
			break;
			
		default:
			break;
	}
	
	// And send the event
	SendEvent(event);
}


////////////////////////////////////////////////////////////
/// Handle a key down event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleKeyDown(void *eventRef)
{
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	
	Event sfEvent;
	unichar chr = 0, rawchr = 0;
	unsigned long length = [[event characters] length];
	unsigned mods = [event modifierFlags];
	
	if (length) {
		chr = [[event characters] characterAtIndex:0];
		
		// Note : I got a crash (out of bounds exception) while typing so now I test...
		if ([[event charactersIgnoringModifiers] length])
			rawchr = [[event charactersIgnoringModifiers] characterAtIndex:0];
		
	}
	
	if (mods & NSCommandKeyMask) {
		// Application commands
		[NSApp sendEvent:event];
	}
	
	// User events
	
	if (!useKeyRepeat && [event isARepeat]) {
		return UsedEvent;
	}
	
#if 1
	// Is it also a text event ?
	if (IsTextEvent(event)) {
		// tampon d'accueil des caracteres UTF-16 recuperes a partir de l'evenement clavier
		unichar *utf16Characters = new unichar[length];
		
		// recuperation des caracteres
		[[event characters] getCharacters:utf16Characters];
		
		// tampon d'accueil des caracteres convertis en UTF-32
		Uint32 utf32Characters[2];
		
		// conversion des caracteres
		const Uint32 *addr = Unicode::UTF16ToUTF32(utf16Characters,
												   utf16Characters + length,
												   utf32Characters);
		
		// si il y a eu des caracteres convertis ?
		if (addr > utf32Characters) {
			sfEvent.Type = Event::TextEntered;
			sfEvent.Text.Unicode = utf32Characters[0];
			
			SendEvent(sfEvent);
		}
		
		delete[] utf16Characters;
	}
#else
	// Is it also a text event ?
	if (IsTextEvent(event)) {
		static NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:1];
		
		sfEvent.Type = Event::TextEntered;
		sfEvent.Text.Unicode = chr;
		
		NSText *field = [members->window fieldEditor:YES forObject:nil];
		[arr addObject:event];
		[field interpretKeyEvents:arr];
		
		if ([[field string] length]) {
			unichar unichr = [[field string] characterAtIndex:0];
			sfEvent.Text.Unicode = unichr;
			SendEvent(sfEvent);
			
			unichar str[2] = {unichr, 0};
			NSLog(@"Char::%@", [NSString stringWithCharacters:str length:2]);
			
			[field setString:@""];
			[arr removeAllObjects];
		}
		
		
	}
#endif
	
	// Anyway it's also a KeyPressed event
	sfEvent.Type = Event::KeyPressed;
	
	// Get the keys
	if (Key::Code(0) == (sfEvent.Key.Code = KeyForUnicode(rawchr))) {
		sfEvent.Key.Code = KeyForVirtualCode([event keyCode]);
	}
	
	// Get the modifiers
	sfEvent.Key.Alt = mods & NSAlternateKeyMask;
	sfEvent.Key.Control = mods & NSControlKeyMask;
	sfEvent.Key.Shift = mods & NSShiftKeyMask;
	
	// Send the event
	SendEvent(sfEvent);
	
	return UsedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a key up event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleKeyUp(void *eventRef)
{
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	
	Event sfEvent;
	unsigned mods = [event modifierFlags];
	unichar chr = 0, rawchr = 0;
	
	if ([[event characters] length]) {
		chr = [[event characters] characterAtIndex:0];
		
		if ([[event charactersIgnoringModifiers] length])
			rawchr = [[event charactersIgnoringModifiers] characterAtIndex:0];
		
	}
	
	if (mods & NSCommandKeyMask) {
		[NSApp sendEvent:event];
	}
	
	sfEvent.Type = Event::KeyReleased;
	
	// Get the code
	if (Key::Code(0) == (sfEvent.Key.Code = KeyForUnicode(rawchr))) {
		sfEvent.Key.Code = KeyForVirtualCode([event keyCode]);
	}
	
	// Get the modifiers
	sfEvent.Key.Alt = mods & NSAlternateKeyMask;
	sfEvent.Key.Control = mods & NSControlKeyMask;
	sfEvent.Key.Shift = mods & NSShiftKeyMask;
	
	SendEvent(sfEvent);
	
	return UsedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a key modifier event [Command, Option, Control, Shift]
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleModifierKey(void *eventRef)
{
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	Event sfEvent;
	unsigned mods = [event modifierFlags];
	
	sfEvent.Type = Event::KeyPressed;
	sfEvent.Key.Code = KeyForVirtualCode([event keyCode]);
	
	sfEvent.Key.Alt = mods & NSAlternateKeyMask;
	sfEvent.Key.Control = mods & NSControlKeyMask;
	sfEvent.Key.Shift = mods & NSShiftKeyMask;
	
	if (!(mods & NSAlternateKeyMask) &&
		(sfEvent.Key.Code == Key::LAlt || sfEvent.Key.Code == Key::RAlt)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	if (!(mods & NSControlKeyMask) &&
		(sfEvent.Key.Code == Key::LControl || sfEvent.Key.Code == Key::RControl)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	if (!(mods & NSShiftKeyMask) &&
		(sfEvent.Key.Code == Key::LShift || sfEvent.Key.Code == Key::RShift)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	if (!(mods & NSCommandKeyMask) &&
		(sfEvent.Key.Code == Key::LSystem || sfEvent.Key.Code == Key::RSystem)) {
		sfEvent.Type = Event::KeyReleased;
	}
	
	SendEvent(sfEvent);
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a mouse down event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseDown(void *eventRef)
{
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	Event sfEvent;
	NSPoint loc = {0, 0};
	unsigned mods = [event modifierFlags];
	
	switch ([event type]) {
		case NSLeftMouseDown:
			sfEvent.Type = Event::MouseButtonPressed;
			
			if (mods & NSControlKeyMask) {
				sfEvent.MouseButton.Button = Mouse::Right;
			} else {
				sfEvent.MouseButton.Button = Mouse::Left;
			}
			
			// Get mouse position
			loc = MouseLocation(members->window);
			
			sfEvent.MouseButton.X = (int) loc.x;
			sfEvent.MouseButton.Y = (int) loc.y;
			
			SendEvent(sfEvent);
			break;
			
		case NSRightMouseDown:
			sfEvent.Type = Event::MouseButtonPressed;
			sfEvent.MouseButton.Button = Mouse::Right;
			
			// Get mouse position
			loc = MouseLocation(members->window);
			
			sfEvent.MouseButton.X = (int) loc.x;
			sfEvent.MouseButton.Y = (int) loc.y;
			
			SendEvent(sfEvent);
			break;
			
		default:
			break;
	}
	
	return UnusedEvent;
	
}


////////////////////////////////////////////////////////////
/// Handle a mouse up event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseUp(void *eventRef)
{
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	Event sfEvent;
	NSPoint loc = {0, 0};
	unsigned mods = [event modifierFlags];
	
	switch ([event type]) {
		case NSLeftMouseUp:
			sfEvent.Type = Event::MouseButtonReleased;
			
			if (mods & NSControlKeyMask) {
				sfEvent.MouseButton.Button = Mouse::Right;
			} else {
				sfEvent.MouseButton.Button = Mouse::Left;
			}
			
			// Get mouse position
			loc = MouseLocation(members->window);
			
			sfEvent.MouseButton.X = (int) loc.x;
			sfEvent.MouseButton.Y = (int) loc.y;
			
			SendEvent(sfEvent);
			break;
			
		case NSRightMouseUp:
			sfEvent.Type = Event::MouseButtonReleased;
			sfEvent.MouseButton.Button = Mouse::Right;
			
			// Get mouse position
			loc = MouseLocation(members->window);
			
			sfEvent.MouseButton.X = (int) loc.x;
			sfEvent.MouseButton.Y = (int) loc.y;
			
			SendEvent(sfEvent);
			break;
			
		default:
			break;
	}
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a mouse move event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseMove(void *eventRef)
{
	Event sfEvent;
	NSPoint loc = {0, 0};
	
	loc = MouseLocation(members->window);
	sfEvent.Type = Event::MouseMoved;
	
	sfEvent.MouseMove.X = (int) loc.x;
	sfEvent.MouseMove.Y = (int) loc.y;
	
	SendEvent(sfEvent);
	
	// MouseEntered and MouseLeft events
	if (MouseInside(members->window, members->view) && !mouseIn) {
		sfEvent.Type = Event::MouseEntered;
		mouseIn = true;
		SendEvent(sfEvent);
	} else if (!MouseInside(members->window, members->view) && mouseIn) {
		sfEvent.Type = Event::MouseLeft;
		mouseIn = false;
		SendEvent(sfEvent);
	}
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Handle a mouse wheel event (NSEvent)
////////////////////////////////////////////////////////////
int WindowImplCocoa::HandleMouseWheel(void *eventRef)
{
	NSEvent *event = static_cast <NSEvent *> (eventRef);
	
	wheelStatus += [event deltaY];
	
	if (fabs(wheelStatus) > 1.0f) {
		Event sfEvent;
		sfEvent.Type = Event::MouseWheelMoved;
		sfEvent.MouseWheel.Delta = (int) wheelStatus;
		SendEvent(sfEvent);
		
		wheelStatus -= (int) wheelStatus;
	}
	
	return UnusedEvent;
}


////////////////////////////////////////////////////////////
/// Return a pointer to the SFWindow object
////////////////////////////////////////////////////////////
void *WindowImplCocoa::CocoaWindow(void)
{
	return static_cast <void *> (members->window);
}

////////////////////////////////////////////////////////////
/// Return whether the window is in full screen mode
////////////////////////////////////////////////////////////
bool WindowImplCocoa::IsFullscreen(void)
{
	return fullscreen;
}

////////////////////////////////////////////////////////////
/// /see sfWindowImpl::Display
////////////////////////////////////////////////////////////
void WindowImplCocoa::Display()
{
	[members->context flushBuffer];
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::ProcessEvents
////////////////////////////////////////////////////////////
void WindowImplCocoa::ProcessEvents()
{
	if (![NSApp isRunning])
		return;
	
	[[AppController sharedController] processEvents];
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::MakeActive
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetActive(bool Active) const
{
	if (Active) {
		if ([NSOpenGLContext currentContext] != members->context)
			[members->context makeCurrentContext];
	} else {
		if ([NSOpenGLContext currentContext] == members->context)
			[NSOpenGLContext clearCurrentContext];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::UseVerticalSync
////////////////////////////////////////////////////////////
void WindowImplCocoa::UseVerticalSync(bool Enabled)
{
	GLint enable = (Enabled) ? 1 : 0;
	[members->context setValues:&enable forParameter:NSOpenGLCPSwapInterval];
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::ShowMouseCursor
////////////////////////////////////////////////////////////
void WindowImplCocoa::ShowMouseCursor(bool flag)
{
	if (flag) {
		[NSCursor unhide];
	} else {
		[NSCursor hide];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::SetCursorPosition
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetCursorPosition(unsigned int Left, unsigned int Top)
{
	NSPoint pos = NSMakePoint ((float) Left, (float) Top);
	
	if (members->window) {
		// Flip for SFML window coordinate system
		pos.y = [members->window frame].size.height - pos.y;
		
		// Adjust for view reference instead of window
		pos.y -= [members->window frame].size.height - [members->view frame].size.height;
		
		// Convert to screen coordinates
		NSPoint absolute = [members->window convertBaseToScreen:pos];
		
		// Flip screen coodinates
		absolute.y = [[NSScreen mainScreen] frame].size.height - absolute.y;
		
		// Move cursor
		CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, CGPointMake(absolute.x, absolute.y));
	} else {
		std::cerr << "*** SFML: uninitialized 'members->window' member (objc_members) in "
			 << this << " (sf::priv::WindowImplCocoa)" << std::endl;
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::SetPosition
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetPosition(int Left, int Top)
{
	if (members->window) {
		if (!fullscreen) {
			// Change the window position
			Top = (int) [[members->window screen] frame].size.height - Top;
			[members->window setFrameTopLeftPoint:NSMakePoint(Left, Top)];
		}
	} else {
		std::cerr << "*** SFML: uninitialized 'members->window' member (objc_members) in "
			 << this << " (sf::priv::WindowImplCocoa)";
	}
}


////////////////////////////////////////////////////////////
/// /see WindowImpl::SetSize
///
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetSize(unsigned int Width, unsigned int Height)
{
	if (members->window) {
		if (!fullscreen) {
			[members->window setFrame:NSMakeRect([members->window frame].origin.x,
												 [members->window frame].origin.y,
												 (float) Width, (float) Height)
			 display:YES];
		}
	} else {
		std::cerr << "*** SFML: uninitialized 'members->window' member (objc_members) in "
			 << this << " (sf::priv::WindowImplCocoa)" << std::endl;
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::Show
////////////////////////////////////////////////////////////
void WindowImplCocoa::Show(bool State)
{
	if (State && ![members->window isVisible]) {
		// Wanna open the closed window
		
		// Register ourselves for event handling
		[[AppController sharedController] registerWindow:this];
		
		if (fullscreen) {
			desktopMode = VideoMode::GetDesktopMode();
			
			CFDictionaryRef displayMode = CGDisplayBestModeForParameters (kCGDirectMainDisplay,
																		  fullscreenMode.BitsPerPixel,
																		  fullscreenMode.Width,
																		  fullscreenMode.Height,
																		  NULL);
			
			CGDisplayFadeReservationToken token = kCGDisplayFadeReservationInvalidToken;
			
			// Fade to a black screen
			[SharedAppController doFadeOperation:FillScreen time:0.5f sync:true token:&token];
			[NSMenu setMenuBarVisible:NO];
			
			// Switch to the wished display mode
			CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode);
			
			// Open the window
			[members->window makeKeyAndOrderFront:nil];
			[members->window center];
			
			// Fade to normal screen
			[SharedAppController doFadeOperation:CleanScreen time:0.2f sync:false token:&token];
			
		} else {
			// Show the window
			// Note: using these two lines instead of -[NSWindow makeKeyAndOrderFront:]
			// in order to prevent the standard window buttons from not displaying
			// the "mouse over" icons
			[members->window orderFront:nil];
			[members->window makeKeyWindow];
			
		}
	} else if (!State && [members->window isVisible]) {
		// Wanna close the opened window
		
		if (fullscreen) {
			CFDictionaryRef displayMode = CGDisplayBestModeForParameters (kCGDirectMainDisplay,
																		  desktopMode.BitsPerPixel,
																		  desktopMode.Width,
																		  desktopMode.Height,
																		  NULL);
			
			CGDisplayFadeReservationToken token = kCGDisplayFadeReservationInvalidToken;
			
			// Fade to black screen
			[SharedAppController doFadeOperation:FillScreen time:0.2f sync:true token:&token];
			
			// Switch to the wished display mode
			CGDisplaySwitchToMode(kCGDirectMainDisplay, displayMode);
			
			// Close the window
			[members->window close];
			[NSMenu setMenuBarVisible:YES];
			
			// Fade to normal screen
			[SharedAppController doFadeOperation:CleanScreen time:0.5f sync:false token:&token];
			
			// Do not sync but sleep so that the Dock and the Finder desktop go back to normal
			// state before the end of the fade operation
			sf::Sleep(0.5f);
		} else {
			// Close the window
			[members->window close];
			
		}
		
		// Unregister ourselves from the event handler
		[[AppController sharedController] unregisterWindow:this];
	}
}


////////////////////////////////////////////////////////////
/// /see sfWindowImpl::EnableKeyRepeat
////////////////////////////////////////////////////////////
void WindowImplCocoa::EnableKeyRepeat(bool Enabled)
{
	useKeyRepeat = Enabled;
}


////////////////////////////////////////////////////////////
/// see WindowImpl::SetIcon
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetIcon(unsigned int Width, unsigned int Height, const Uint8* Pixels)
{
	// Nothing to do
}


////////////////////////////////////////////////////////////
/// Make some allocations and initializations
////////////////////////////////////////////////////////////
void WindowImplCocoa::Initialize(void)
{
	// Allocate mem for the private objc members
	members = new objc_members;
	bzero(members, sizeof(*members));
	
	// Needed to always have an autorelease pool as soon as application is launched
	ONCE([SharedAppController resetPool]);
	
	// Register application if needed and launch it
	ONCE([SharedAppController runApplication]);
}


////////////////////////////////////////////////////////////
/// Make the window
////////////////////////////////////////////////////////////
static SFWindow *MakeWindow(WindowSettings& params, unsigned long style, VideoMode& mode, NSString *title)
{
	SFWindow *window = nil;
	
	NSRect frame = NSMakeRect (0.0f, 0.0f, (float) mode.Width, (float) mode.Height);
	unsigned int mask = 0;
	
	// We grab options from WindowStyle and add them to our window mask
	if (style & Style::None || style & Style::Fullscreen) {
		mask |= NSBorderlessWindowMask;
		
		if (style & style & Style::Fullscreen) {
			// Check display mode and put new values in 'mode' if needed
			boolean_t exact = true;
			CFDictionaryRef properties = CGDisplayBestModeForParameters(kCGDirectMainDisplay, mode.BitsPerPixel,
																		mode.Width, mode.Height, &exact);
			
			if (!properties) {
				std::cerr << "*** SFML: unable to get a display mode with the given parameters" << std::endl; 
				return nil;
			}
			
			if (exact == false) {
				CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayWidth),
								 kCFNumberIntType, &mode.Width);
				
				CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayHeight),
								 kCFNumberIntType, &mode.Height);
				
				CFNumberGetValue((CFNumberRef) CFDictionaryGetValue(properties, kCGDisplayBitsPerPixel),
								 kCFNumberIntType, &mode.BitsPerPixel);
			}
		}
		
	} else {
		if (style & Style::Titlebar) {
			mask |= NSTitledWindowMask;
			mask |= NSMiniaturizableWindowMask;
		}
		
		if (style & Style::Resize) {
			mask |= NSTitledWindowMask;
			mask |= NSMiniaturizableWindowMask;
			mask |= NSResizableWindowMask;
		}
		
		if (style & Style::Close) {
			mask |= NSTitledWindowMask;
			mask |= NSClosableWindowMask;
			mask |= NSMiniaturizableWindowMask;
		}
	}
	
	// Now we make the window with the values we got
	// Note: defer flag set to NO to be able to use OpenGL in our window
	window =[massert([SFWindow alloc]) initWithContentRect:frame
												  styleMask:mask
													backing:NSBackingStoreBuffered
													  defer:NO];
	
	if (window != nil) {
		// We set title and window position
		[window setTitle:title];
		[window center];
	}
	
	return window;
}


////////////////////////////////////////////////////////////
/// Make the OpenGL pixel format from the given attributes
////////////////////////////////////////////////////////////
static NSOpenGLContext *MakeOpenGLContext(WindowSettings& params)
{
	NSOpenGLPixelFormat *pixFormat = nil;
	NSOpenGLContext *context = nil;
	unsigned idx = 0;
#if ENABLE_ANTIALIASING
	unsigned samplesIdx = 0;
#endif
	
	// Attributes list
	NSOpenGLPixelFormatAttribute attribs[15] = {(NSOpenGLPixelFormatAttribute) 0};
	
	// Accelerated, double buffered
	attribs[idx++] = NSOpenGLPFAClosestPolicy;
	attribs[idx++] = NSOpenGLPFADoubleBuffer;
	attribs[idx++] = NSOpenGLPFAAccelerated;
	
	// windowed context
	attribs[idx++] = NSOpenGLPFAWindow;
	
	// Color size ; usually 32 bits per pixel
	attribs[idx++] = NSOpenGLPFAColorSize;
	attribs[idx++] = (NSOpenGLPixelFormatAttribute) VideoMode::GetDesktopMode().BitsPerPixel;
	
	// Z-buffer size
	attribs[idx++] = NSOpenGLPFADepthSize;
	attribs[idx++] = (NSOpenGLPixelFormatAttribute) params.DepthBits;
	
	// Stencil bits (I don't really know what's that...)
	attribs[idx++] = NSOpenGLPFAStencilSize;
	attribs[idx++] = (NSOpenGLPixelFormatAttribute) params.StencilBits;
	
#if ENABLE_ANTIALIASING
	// Antialiasing settings
	if (params.AntialiasingLevel) {
		samplesIdx = idx;
		
		attribs[idx++] = NSOpenGLPFASamples;
		attribs[idx++] = (NSOpenGLPixelFormatAttribute) params.AntialiasingLevel;
		
		attribs[idx++] = NSOpenGLPFASampleBuffers;
		attribs[idx++] = (NSOpenGLPixelFormatAttribute) GL_TRUE;
	}
#endif
	
	pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	
#if ENABLE_ANTIALIASING
	// If pixel format creation fails and antialiasing level is
	// greater than 2, we set it to 2.
	if (pixFormat == nil && params.AntialiasingLevel > 2) {
		std::cerr << "Failed to find a pixel format supporting " << params.AntialiasingLevel << " antialiasing levels ; trying with 2 levels" << std::endl;
		params.AntialiasingLevel = attribs[samplesIdx + 1] = (NSOpenGLPixelFormatAttribute) 2;
		
		pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	}
	
	// If pixel format creation fails and antialiasing is enabled,
	// we disable it.
	if (pixFormat == nil && params.AntialiasingLevel > 0) {
		std::cerr << "Failed to find a pixel format supporting antialiasing ; antialiasing will be disabled" << std::endl;
		attribs[samplesIdx] = (NSOpenGLPixelFormatAttribute) nil;
		
		pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
	}
#endif
	
	if (pixFormat) {
		context = [[NSOpenGLContext alloc] initWithFormat:pixFormat
											 shareContext:[sharedContext retain]];
		
		// Get the effective properties from our OpenGL context
		GLint tmpDepthSize = 0, tmpStencilBits = 0, tmpAntialiasingLevel = 0;
		
		if (context) {
			[pixFormat getValues:&tmpDepthSize
					forAttribute:NSOpenGLPFADepthSize
				forVirtualScreen:[context currentVirtualScreen]];
			
			[pixFormat getValues:&tmpStencilBits
					forAttribute:NSOpenGLPFAStencilSize
				forVirtualScreen:[context currentVirtualScreen]];
			
			[pixFormat getValues:&tmpAntialiasingLevel
					forAttribute:NSOpenGLPFASamples
				forVirtualScreen:[context currentVirtualScreen]];
		}
		
		
		params.DepthBits = (unsigned) tmpDepthSize;
		params.StencilBits = (unsigned) tmpStencilBits;
		params.AntialiasingLevel = (unsigned) tmpAntialiasingLevel;
		
		[pixFormat release];
	}
	
	return context;
}


static NSOpenGLView	* MakeOpenGLView(SFWindow *window, NSOpenGLContext *context, WindowSettings& params)
{
	assert(window != nil);
	assert(context != nil);
	
	NSOpenGLView *view = nil;
	
	
	// We make the NSOpenGLView
	view = [[NSOpenGLView alloc] initWithFrame:[[window contentView] bounds]
								   pixelFormat:nil];
	
	if (view) {
		// We add the NSOpenGLView to the window
		[[window contentView] addSubview:view];
		
		[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
		[view clearGLContext];
		[view setOpenGLContext:context];
		[context setView:view];
	}
	
	return view;
}


static void ConfigureWindow(SFWindow *window, NSOpenGLView *view, WindowController *controller)
{
	assert(window != nil);
	assert(view != nil);
	assert(controller != nil);
	
	// We need to update the OpenGL view when it changes
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc addObserver:controller
		   selector:@selector(viewFrameDidChange:)
			   name:NSViewFrameDidChangeNotification
			 object:view];
	
	// We want to know when our window got the focus
	[nc addObserver:controller
		   selector:@selector(windowDidBecomeMain:)
			   name:NSWindowDidBecomeMainNotification
			 object:window];
	
	// We want to know when our window lost the focus
	[nc addObserver:controller
		   selector:@selector(windowDidResignMain:)
			   name:NSWindowDidResignMainNotification
			 object:window];
	
	// We want to know when the user closes the window
	[nc addObserver:controller
		   selector:@selector(windowWillClose:)
			   name:NSWindowWillCloseNotification
			 object:window];
	
	// I want to re-center the window if it's a full screen one and moved by Spaces
	[nc addObserver:controller
		   selector:@selector(windowDidMove:)
			   name:NSWindowDidMoveNotification
			 object:window];
	
	
	// Needed not to make application crash when releasing the window in our destructor
	// (I prefer to take control of everything :P)
	[window setReleasedWhenClosed:NO];
	[window setAcceptsMouseMovedEvents:YES];
}


////////////////////////////////////////////////////////////
/// Return the SFML key corresponding to a key code
////////////////////////////////////////////////////////////
static Key::Code KeyForVirtualCode(unsigned short vCode)
{
	static struct {
		unsigned short code;
		Key::Code sfKey;
	} virtualTable[] =
	{
		{0x35, Key::Escape},
		{0x31, Key::Space},
		{0x24, Key::Return},	// main Return key
		{0x4C, Key::Return},	// pav Return key
		{0x33, Key::Back},
		{0x30, Key::Tab},
		{0x74, Key::PageUp},
		{0x79, Key::PageDown},
		{0x77, Key::End},
		{0x73, Key::Home},
		{0x72, Key::Insert},
		{0x75, Key::Delete},
		{0x45, Key::Add},
		{0x4E, Key::Subtract},
		{0x43, Key::Multiply},
		{0x4B, Key::Divide},
		
		{0x7A, Key::F1}, {0x78, Key::F2}, {0x63, Key::F3},
		{0x76, Key::F4}, {0x60, Key::F5}, {0x61, Key::F6},
		{0x62, Key::F7}, {0x64, Key::F8}, {0x65, Key::F9},
		{0x6D, Key::F10}, {0x67, Key::F11}, {0x6F, Key::F12},
		{0x69, Key::F13}, {0x6B, Key::F14}, {0x71, Key::F15},
		
		{0x7B, Key::Left},
		{0x7C, Key::Right},
		{0x7E, Key::Up},
		{0x7D, Key::Down},
		
		{0x52, Key::Numpad0}, {0x53, Key::Numpad1}, {0x54, Key::Numpad2},
		{0x55, Key::Numpad3}, {0x56, Key::Numpad4}, {0x57, Key::Numpad5},
		{0x58, Key::Numpad6}, {0x59, Key::Numpad7}, {0x5B, Key::Numpad8},
		{0x5C, Key::Numpad9},
		
		{0x1D, Key::Num0}, {0x12, Key::Num1}, {0x13, Key::Num2},
		{0x14, Key::Num3}, {0x15, Key::Num4}, {0x17, Key::Num5},
		{0x16, Key::Num6}, {0x1A, Key::Num7}, {0x1C, Key::Num8},
		{0x19, Key::Num9},
		
		{0x3B, Key::LControl},	//< Left Ctrl
		{0x3A, Key::LAlt},		//< Left Option/Alt
		{0x37, Key::LSystem},	//< Left Command
		{0x38, Key::LShift},	//< Left Shift
		{0x3E, Key::RControl},	//< Right Ctrl
		{0x3D, Key::RAlt},		//< Right Option/Alt
		{0x36, Key::RSystem},	//< Right Command
		{0x3C, Key::RShift},	//< Right Shift
		
		{0x39, Key::Code(0)}	//< Caps Lock
	};
	
	Key::Code result = Key::Code(0);
	
	for (unsigned i = 0;virtualTable[i].code;i++) {
		if (virtualTable[i].code == vCode) {
			result = virtualTable[i].sfKey;
			break;
		}
	}
	
	return result;
}


////////////////////////////////////////////////////////////
/// Return the SFML key corresponding to a unicode code
////////////////////////////////////////////////////////////
static Key::Code KeyForUnicode(unsigned short uniCode)
{
	static struct {
		unsigned short character;
		Key::Code sfKey;
	} unicodeTable[] =
	{
		{'!', Key::Code(0)}, //< No Key for this code
		{'"', Key::Code(0)}, //< No Key for this code
		{'#', Key::Code(0)}, //< No Key for this code
		{'$', Key::Code(0)}, //< No Key for this code
		{'%', Key::Code(0)}, //< No Key for this code
		{'&', Key::Code(0)}, //< No Key for this code
		{'\'', Key::Quote},
		{'(', Key::Code(0)}, //< No Key for this code
		{')', Key::Code(0)}, //< No Key for this code
		{'*', Key::Multiply},
		{'+', Key::Add},
		{',', Key::Comma},
		{'-', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'.', Key::Period},
		{'/', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'0', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'1', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'2', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'3', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'4', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'5', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'6', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'7', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'8', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{'9', Key::Code(0)}, //< Handled by KeyForVirtualCode()
		{':', Key::Code(0)}, //< No Key for this code
		{';', Key::SemiColon},
		{'<', Key::Code(0)}, //< No Key for this code
		{'=', Key::Equal},
		{'>', Key::Code(0)}, //< No Key for this code
		{'?', Key::Code(0)}, //< No Key for this code
		{'@', Key::Code(0)}, //< No Key for this code
		{'A', Key::A}, {'B', Key::B}, {'C', Key::C},
		{'D', Key::D}, {'E', Key::E}, {'F', Key::F},
		{'G', Key::G}, {'H', Key::H}, {'I', Key::I},
		{'J', Key::J}, {'K', Key::K}, {'L', Key::L},
		{'M', Key::M}, {'N', Key::N}, {'O', Key::O},
		{'P', Key::P}, {'Q', Key::Q}, {'R', Key::R},
		{'S', Key::S}, {'T', Key::T}, {'U', Key::U},
		{'V', Key::V}, {'W', Key::W}, {'X', Key::X},
		{'Y', Key::Y}, {'Z', Key::Z},
		{'[', Key::LBracket},
		{'\\', Key::BackSlash},
		{']', Key::RBracket},
		{'^', Key::Code(0)}, //< No Key for this code
		{'_', Key::Code(0)}, //< No Key for this code
		{'`', Key::Code(0)}, //< No Key for this code
		{'a', Key::A}, {'b', Key::B}, {'c', Key::C},
		{'d', Key::D}, {'e', Key::E}, {'f', Key::F},
		{'g', Key::G}, {'h', Key::H}, {'i', Key::I},
		{'j', Key::J}, {'k', Key::K}, {'l', Key::L},
		{'m', Key::M}, {'n', Key::N}, {'o', Key::O},
		{'p', Key::P}, {'q', Key::Q}, {'r', Key::R},
		{'s', Key::S}, {'t', Key::T}, {'u', Key::U},
		{'v', Key::V}, {'w', Key::W}, {'x', Key::X},
		{'y', Key::Y}, {'z', Key::Z},
		{'{', Key::Code(0)}, //< No Key for this code
		{'|', Key::Code(0)}, //< No Key for this code
		{'}', Key::Code(0)}, //< No Key for this code
		{'~', Key::Tilde},
		{0, Key::Code(0)}
	};
	
	Key::Code result = Key::Code(0);
	
	for (unsigned i = 0;unicodeTable[i].character;i++) {
		if (unicodeTable[i].character == uniCode) {
			result = unicodeTable[i].sfKey;
			break;
		}
	}
	
	return result;
}


////////////////////////////////////////////////////////////
/// Return whether 'ev' must be considered as a TextEntered event
////////////////////////////////////////////////////////////
static bool IsTextEvent(NSEvent *event)
{
	bool res = false;
	
	if (event && [event type] == NSKeyDown && [[event characters] length]) {
		unichar code = [[event characters] characterAtIndex:0];
		
		// Codes from 0xF700 to 0xF8FF are non text keys (see NSEvent.h)
		if (code < 0xF700 || code > 0xF8FF)
			res = true;
	}
	
	return res;
}

	
////////////////////////////////////////////////////////////
/// Return whether the mouse is on our OpenGL view
////////////////////////////////////////////////////////////
static bool MouseInside(SFWindow *window, NSView *view)
{
	bool res = false;
	
	if (window && view && [window isVisible]) {
		NSPoint relativeToWindow = [window mouseLocationOutsideOfEventStream];
		NSPoint relativeToView = [view convertPoint:relativeToWindow fromView:nil];
		
		if (NSPointInRect (relativeToView, [view bounds]))
			res = true;
	}
	
	return res;
}


////////////////////////////////////////////////////////////
/// Return the mouse location in the SFML coordinates according to 'window'
////////////////////////////////////////////////////////////
static NSPoint MouseLocation(SFWindow *window)
{
	NSPoint location = [NSEvent mouseLocation];
	NSPoint	relativeLocation = {0, 0};
	
	if (window) {
		[window convertScreenToBase:location];
		relativeLocation.y = [[window contentView] frame].size.height - relativeLocation.y;
	} else {
		std::cerr << "*** SFML: tried to get mouse location from no window" << std::endl;
	}
	
	return relativeLocation;
}

} // namespace priv

} // namespace sf