1// Copyright (C) 2005-2006 Etienne Petitjean
2// Copyright (C) 2007-2012 Christian Stehno
3// This file is part of the "Irrlicht Engine".
4// For conditions of distribution and use, see copyright notice in Irrlicht.h
5
6#include "IrrCompileConfig.h"
7
8#ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
9
10#import <Cocoa/Cocoa.h>
11#import <OpenGL/gl.h>
12#ifndef __MAC_10_6
13#import <Carbon/Carbon.h>
14#endif
15
16#include "CIrrDeviceMacOSX.h"
17#include "IEventReceiver.h"
18#include "irrList.h"
19#include "os.h"
20#include "CTimer.h"
21#include "irrString.h"
22#include "Keycodes.h"
23#include <stdio.h>
24#include <sys/utsname.h>
25#include "COSOperator.h"
26#include "CColorConverter.h"
27#include "irrlicht.h"
28
29
30#import <wchar.h>
31#import <time.h>
32#import "AppDelegate.h"
33
34#if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
35
36#include <IOKit/IOKitLib.h>
37#include <IOKit/IOCFPlugIn.h>
38#ifdef MACOS_10_0_4
39#include <IOKit/hidsystem/IOHIDUsageTables.h>
40#else
41/* The header was moved here in Mac OS X 10.1 */
42#include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
43#endif
44#include <IOKit/hid/IOHIDLib.h>
45#include <IOKit/hid/IOHIDKeys.h>
46
47// only OSX 10.5 seems to not need these defines...
48#if !defined(__MAC_10_5) || defined(__MAC_10_6)
49// Contents from Events.h from Carbon/HIToolbox but we need it with Cocoa too
50// and for some reason no Cocoa equivalent of these constants seems provided.
51// So I'm doing like everyone else and using copy-and-paste.
52
53/*
54 *  Summary:
55 *	Virtual keycodes
56 *
57 *  Discussion:
58 *	These constants are the virtual keycodes defined originally in
59 *	Inside Mac Volume V, pg. V-191. They identify physical keys on a
60 *	keyboard. Those constants with "ANSI" in the name are labeled
61 *	according to the key position on an ANSI-standard US keyboard.
62 *	For example, kVK_ANSI_A indicates the virtual keycode for the key
63 *	with the letter 'A' in the US keyboard layout. Other keyboard
64 *	layouts may have the 'A' key label on a different physical key;
65 *	in this case, pressing 'A' will generate a different virtual
66 *	keycode.
67 */
68enum {
69	kVK_ANSI_A		= 0x00,
70	kVK_ANSI_S		= 0x01,
71	kVK_ANSI_D		= 0x02,
72	kVK_ANSI_F		= 0x03,
73	kVK_ANSI_H		= 0x04,
74	kVK_ANSI_G		= 0x05,
75	kVK_ANSI_Z		= 0x06,
76	kVK_ANSI_X		= 0x07,
77	kVK_ANSI_C		= 0x08,
78	kVK_ANSI_V		= 0x09,
79	kVK_ANSI_B		= 0x0B,
80	kVK_ANSI_Q		= 0x0C,
81	kVK_ANSI_W		= 0x0D,
82	kVK_ANSI_E		= 0x0E,
83	kVK_ANSI_R		= 0x0F,
84	kVK_ANSI_Y		= 0x10,
85	kVK_ANSI_T		= 0x11,
86	kVK_ANSI_1		= 0x12,
87	kVK_ANSI_2		= 0x13,
88	kVK_ANSI_3		= 0x14,
89	kVK_ANSI_4		= 0x15,
90	kVK_ANSI_6		= 0x16,
91	kVK_ANSI_5		= 0x17,
92	kVK_ANSI_Equal		= 0x18,
93	kVK_ANSI_9		= 0x19,
94	kVK_ANSI_7		= 0x1A,
95	kVK_ANSI_Minus		= 0x1B,
96	kVK_ANSI_8		= 0x1C,
97	kVK_ANSI_0		= 0x1D,
98	kVK_ANSI_RightBracket   = 0x1E,
99	kVK_ANSI_O		= 0x1F,
100	kVK_ANSI_U		= 0x20,
101	kVK_ANSI_LeftBracket	= 0x21,
102	kVK_ANSI_I		= 0x22,
103	kVK_ANSI_P		= 0x23,
104	kVK_ANSI_L		= 0x25,
105	kVK_ANSI_J		= 0x26,
106	kVK_ANSI_Quote		= 0x27,
107	kVK_ANSI_K		= 0x28,
108	kVK_ANSI_Semicolon	= 0x29,
109	kVK_ANSI_Backslash	= 0x2A,
110	kVK_ANSI_Comma		= 0x2B,
111	kVK_ANSI_Slash		= 0x2C,
112	kVK_ANSI_N		= 0x2D,
113	kVK_ANSI_M		= 0x2E,
114	kVK_ANSI_Period		= 0x2F,
115	kVK_ANSI_Grave		= 0x32,
116	kVK_ANSI_KeypadDecimal  = 0x41,
117	kVK_ANSI_KeypadMultiply = 0x43,
118	kVK_ANSI_KeypadPlus	= 0x45,
119	kVK_ANSI_KeypadClear	= 0x47,
120	kVK_ANSI_KeypadDivide   = 0x4B,
121	kVK_ANSI_KeypadEnter	= 0x4C,
122	kVK_ANSI_KeypadMinus	= 0x4E,
123	kVK_ANSI_KeypadEquals   = 0x51,
124	kVK_ANSI_Keypad0	= 0x52,
125	kVK_ANSI_Keypad1	= 0x53,
126	kVK_ANSI_Keypad2	= 0x54,
127	kVK_ANSI_Keypad3	= 0x55,
128	kVK_ANSI_Keypad4	= 0x56,
129	kVK_ANSI_Keypad5	= 0x57,
130	kVK_ANSI_Keypad6	= 0x58,
131	kVK_ANSI_Keypad7	= 0x59,
132	kVK_ANSI_Keypad8	= 0x5B,
133	kVK_ANSI_Keypad9	= 0x5C
134};
135
136/* keycodes for keys that are independent of keyboard layout*/
137enum {
138	kVK_Return		= 0x24,
139	kVK_Tab			= 0x30,
140	kVK_Space		= 0x31,
141	kVK_Delete		= 0x33,
142	kVK_Escape		= 0x35,
143	kVK_Command		= 0x37,
144	kVK_Shift		= 0x38,
145	kVK_CapsLock		= 0x39,
146	kVK_Option		= 0x3A,
147	kVK_Control		= 0x3B,
148	kVK_RightShift		= 0x3C,
149	kVK_RightOption		= 0x3D,
150	kVK_RightControl	= 0x3E,
151	kVK_Function		= 0x3F,
152	kVK_F17			= 0x40,
153	kVK_VolumeUp		= 0x48,
154	kVK_VolumeDown		= 0x49,
155	kVK_Mute		= 0x4A,
156	kVK_F18			= 0x4F,
157	kVK_F19			= 0x50,
158	kVK_F20			= 0x5A,
159	kVK_F5			= 0x60,
160	kVK_F6			= 0x61,
161	kVK_F7			= 0x62,
162	kVK_F3			= 0x63,
163	kVK_F8			= 0x64,
164	kVK_F9			= 0x65,
165	kVK_F11			= 0x67,
166	kVK_F13			= 0x69,
167	kVK_F16			= 0x6A,
168	kVK_F14			= 0x6B,
169	kVK_F10			= 0x6D,
170	kVK_F12			= 0x6F,
171	kVK_F15			= 0x71,
172	kVK_Help		= 0x72,
173	kVK_Home		= 0x73,
174	kVK_PageUp		= 0x74,
175	kVK_ForwardDelete	= 0x75,
176	kVK_F4			= 0x76,
177	kVK_End			= 0x77,
178	kVK_F2			= 0x78,
179	kVK_PageDown		= 0x79,
180	kVK_F1			= 0x7A,
181	kVK_LeftArrow		= 0x7B,
182	kVK_RightArrow		= 0x7C,
183	kVK_DownArrow		= 0x7D,
184	kVK_UpArrow		= 0x7E
185};
186#endif
187
188struct JoystickComponent
189{
190	IOHIDElementCookie cookie; // unique value which identifies element, will NOT change
191	long min; // reported min value possible
192	long max; // reported max value possible
193
194	long minRead; //min read value
195	long maxRead; //max read value
196
197	JoystickComponent() : min(0), minRead(0), max(0), maxRead(0)
198	{
199	}
200};
201
202struct JoystickInfo
203{
204	irr::core::array <JoystickComponent> axisComp;
205	irr::core::array <JoystickComponent> buttonComp;
206	irr::core::array <JoystickComponent> hatComp;
207
208	int hats;
209	int axes;
210	int buttons;
211	int numActiveJoysticks;
212
213	irr::SEvent persistentData;
214
215	IOHIDDeviceInterface ** interface;
216	bool removed;
217	char joystickName[256];
218	long usage; // usage page from IOUSBHID Parser.h which defines general usage
219	long usagePage; // usage within above page from IOUSBHID Parser.h which defines specific usage
220
221	JoystickInfo() : hats(0), axes(0), buttons(0), interface(0), removed(false), usage(0), usagePage(0), numActiveJoysticks(0)
222	{
223		interface = NULL;
224		memset(joystickName, '\0', 256);
225		axisComp.clear();
226		buttonComp.clear();
227		hatComp.clear();
228
229		persistentData.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
230		persistentData.JoystickEvent.POV = 65535;
231		persistentData.JoystickEvent.ButtonStates = 0;
232	}
233};
234irr::core::array<JoystickInfo> ActiveJoysticks;
235
236//helper functions for init joystick
237static IOReturn closeJoystickDevice (JoystickInfo* joyInfo)
238{
239	IOReturn result = kIOReturnSuccess;
240	if (joyInfo && joyInfo->interface)
241	{
242		/* close the interface */
243		result = (*(joyInfo->interface))->close (joyInfo->interface);
244		if (kIOReturnNotOpen == result)
245		{
246			/* do nothing as device was not opened, thus can't be closed */
247		}
248		else if (kIOReturnSuccess != result)
249			irr::os::Printer::log("IOHIDDeviceInterface failed to close", irr::ELL_ERROR);
250		/* release the interface */
251		result = (*(joyInfo->interface))->Release (joyInfo->interface);
252		if (kIOReturnSuccess != result)
253			irr::os::Printer::log("IOHIDDeviceInterface failed to release", irr::ELL_ERROR);
254		joyInfo->interface = NULL;
255	}
256	return result;
257}
258
259static void addComponentInfo (CFTypeRef refElement, JoystickComponent *pComponent, int numActiveJoysticks)
260{
261	long number;
262	CFTypeRef refType;
263
264	refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementCookieKey));
265	if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
266		pComponent->cookie = (IOHIDElementCookie) number;
267	refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMinKey));
268	if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
269		pComponent->minRead = pComponent->min = number;
270	refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMaxKey));
271	if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
272		pComponent->maxRead = pComponent->max = number;
273}
274
275static void getJoystickComponentArrayHandler (const void * value, void * parameter);
276
277static void addJoystickComponent (CFTypeRef refElement, JoystickInfo* joyInfo)
278{
279	long elementType, usagePage, usage;
280	CFTypeRef refElementType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementTypeKey));
281	CFTypeRef refUsagePage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsagePageKey));
282	CFTypeRef refUsage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsageKey));
283
284	if ((refElementType) && (CFNumberGetValue ((CFNumberRef)refElementType, kCFNumberLongType, &elementType)))
285	{
286		/* look at types of interest */
287		if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) ||
288			(elementType == kIOHIDElementTypeInput_Axis))
289		{
290			if (refUsagePage && CFNumberGetValue ((CFNumberRef)refUsagePage, kCFNumberLongType, &usagePage) &&
291				refUsage && CFNumberGetValue ((CFNumberRef)refUsage, kCFNumberLongType, &usage))
292			{
293				switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
294				{
295					case kHIDPage_GenericDesktop:
296					{
297						switch (usage) /* look at usage to determine function */
298						{
299							case kHIDUsage_GD_X:
300							case kHIDUsage_GD_Y:
301							case kHIDUsage_GD_Z:
302							case kHIDUsage_GD_Rx:
303							case kHIDUsage_GD_Ry:
304							case kHIDUsage_GD_Rz:
305							case kHIDUsage_GD_Slider:
306							case kHIDUsage_GD_Dial:
307							case kHIDUsage_GD_Wheel:
308							{
309								joyInfo->axes++;
310								JoystickComponent newComponent;
311								addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
312								joyInfo->axisComp.push_back(newComponent);
313							}
314							break;
315							case kHIDUsage_GD_Hatswitch:
316							{
317								joyInfo->hats++;
318								JoystickComponent newComponent;
319								addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
320								joyInfo->hatComp.push_back(newComponent);
321							}
322							break;
323						}
324					}
325						break;
326					case kHIDPage_Button:
327					{
328						joyInfo->buttons++;
329						JoystickComponent newComponent;
330						addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
331						joyInfo->buttonComp.push_back(newComponent);
332					}
333						break;
334					default:
335						break;
336				}
337			}
338		}
339		else if (kIOHIDElementTypeCollection == elementType)
340		{
341			//get elements
342			CFTypeRef refElementTop = CFDictionaryGetValue ((CFMutableDictionaryRef) refElement, CFSTR(kIOHIDElementKey));
343			if (refElementTop)
344			{
345				CFTypeID type = CFGetTypeID (refElementTop);
346				if (type == CFArrayGetTypeID())
347				{
348					CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
349					CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, joyInfo);
350				}
351			}
352		}
353	}
354}
355
356static void getJoystickComponentArrayHandler (const void * value, void * parameter)
357{
358	if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
359		addJoystickComponent ((CFTypeRef) value, (JoystickInfo *) parameter);
360}
361
362static void joystickTopLevelElementHandler (const void * value, void * parameter)
363{
364	CFTypeRef refCF = 0;
365	if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
366		return;
367	refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsagePageKey));
368	if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usagePage))
369		irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usagePage", irr::ELL_ERROR);
370	refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsageKey));
371	if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usage))
372		irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usage", irr::ELL_ERROR);
373}
374
375static void getJoystickDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, JoystickInfo *joyInfo)
376{
377	CFMutableDictionaryRef usbProperties = 0;
378	io_registry_entry_t parent1, parent2;
379
380	/* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
381	* get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
382	*/
383	if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
384		(KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
385		(KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
386	{
387		if (usbProperties)
388		{
389			CFTypeRef refCF = 0;
390			/* get device info
391			* try hid dictionary first, if fail then go to usb dictionary
392			*/
393
394			/* get joystickName name */
395			refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
396			if (!refCF)
397				refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
398			if (refCF)
399			{
400				if (!CFStringGetCString ((CFStringRef)refCF, joyInfo->joystickName, 256, CFStringGetSystemEncoding ()))
401					irr::os::Printer::log("CFStringGetCString error getting joyInfo->joystickName", irr::ELL_ERROR);
402			}
403
404			/* get usage page and usage */
405			refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
406			if (refCF)
407			{
408				if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usagePage))
409					irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usagePage", irr::ELL_ERROR);
410				refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
411				if (refCF)
412					if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usage))
413						irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usage", irr::ELL_ERROR);
414			}
415
416			if (NULL == refCF) /* get top level element HID usage page or usage */
417			{
418				/* use top level element instead */
419				CFTypeRef refCFTopElement = 0;
420				refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
421				{
422					/* refCFTopElement points to an array of element dictionaries */
423					CFRange range = {0, CFArrayGetCount ((CFArrayRef)refCFTopElement)};
424					CFArrayApplyFunction ((CFArrayRef)refCFTopElement, range, joystickTopLevelElementHandler, joyInfo);
425				}
426			}
427
428			CFRelease (usbProperties);
429		}
430		else
431			irr::os::Printer::log("IORegistryEntryCreateCFProperties failed to create usbProperties", irr::ELL_ERROR);
432
433		if (kIOReturnSuccess != IOObjectRelease (parent2))
434			irr::os::Printer::log("IOObjectRelease failed to release parent2", irr::ELL_ERROR);
435		if (kIOReturnSuccess != IOObjectRelease (parent1))
436			irr::os::Printer::log("IOObjectRelease failed to release parent1", irr::ELL_ERROR);
437	}
438}
439
440#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
441
442//------------------------------------------------------------------------------------------
443Boolean GetDictionaryBoolean(CFDictionaryRef theDict, const void* key)
444{
445	// get a boolean from the dictionary
446	Boolean value = false;
447	CFBooleanRef boolRef;
448	boolRef = (CFBooleanRef)CFDictionaryGetValue(theDict, key);
449	if (boolRef != NULL)
450		value = CFBooleanGetValue(boolRef);
451	return value;
452}
453//------------------------------------------------------------------------------------------
454long GetDictionaryLong(CFDictionaryRef theDict, const void* key)
455{
456	// get a long from the dictionary
457	long value = 0;
458	CFNumberRef numRef;
459	numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
460	if (numRef != NULL)
461		CFNumberGetValue(numRef, kCFNumberLongType, &value);
462	return value;
463}
464
465namespace irr
466{
467	namespace video
468	{
469		IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& param, io::IFileSystem* io, CIrrDeviceMacOSX *device);
470	}
471} // end namespace irr
472
473static bool firstLaunch = true;
474
475namespace irr
476{
477//! constructor
478CIrrDeviceMacOSX::CIrrDeviceMacOSX(const SIrrlichtCreationParameters& param)
479	: CIrrDeviceStub(param), Window(NULL), CGLContext(NULL), OGLContext(NULL),
480	SoftwareDriverTarget(0), DeviceWidth(0), DeviceHeight(0),
481	ScreenWidth(0), ScreenHeight(0), MouseButtonStates(0), SoftwareRendererType(0),
482	IsActive(true), IsFullscreen(false), IsShiftDown(false), IsControlDown(false), IsResizable(false)
483{
484	struct utsname name;
485	NSString *path;
486
487	#ifdef _DEBUG
488	setDebugName("CIrrDeviceMacOSX");
489	#endif
490
491	if (firstLaunch)
492	{
493		firstLaunch = false;
494
495		if(!CreationParams.WindowId) //load menus if standalone application
496		{
497			[[NSAutoreleasePool alloc] init];
498			[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
499			[NSApp setDelegate:(id<NSApplicationDelegate>)[[[AppDelegate alloc] initWithDevice:this] autorelease]];
500			[NSBundle loadNibNamed:@"MainMenu" owner:[NSApp delegate]];
501			[NSApp finishLaunching];
502		}
503
504		path = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
505		chdir([path fileSystemRepresentation]);
506		[path release];
507	}
508    NSWindow* a;
509	uname(&name);
510	Operator = new COSOperator(name.version);
511	os::Printer::log(name.version,ELL_INFORMATION);
512
513	initKeycodes();
514
515	VideoModeList->setDesktop(CreationParams.Bits, core::dimension2d<u32>([[NSScreen mainScreen] frame].size.width, [[NSScreen mainScreen] frame].size.height));
516
517	bool success = true;
518	if (CreationParams.DriverType != video::EDT_NULL)
519		success = createWindow();
520
521	// in case of failure, one can check VideoDriver for initialization
522	if (!success)
523		return;
524
525	setResizable(false);
526	CursorControl = new CCursorControl(CreationParams.WindowSize, this);
527
528	createDriver();
529	createGUIAndScene();
530}
531
532CIrrDeviceMacOSX::~CIrrDeviceMacOSX()
533{
534	[SoftwareDriverTarget release];
535#ifdef __MAC_10_6
536	[NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
537#else
538	SetSystemUIMode(kUIModeNormal, kUIOptionAutoShowMenuBar);
539#endif
540	closeDevice();
541#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
542	for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
543	{
544		if (ActiveJoysticks[joystick].interface)
545			closeJoystickDevice(&ActiveJoysticks[joystick]);
546	}
547#endif
548}
549
550void CIrrDeviceMacOSX::closeDevice()
551{
552	if (Window != NULL)
553	{
554		[Window setIsVisible:FALSE];
555
556		if (OGLContext != NULL)
557		{
558			[OGLContext clearDrawable];
559			[OGLContext release];
560			OGLContext = NULL;
561		}
562
563		[Window setReleasedWhenClosed:TRUE];
564		[Window release];
565		Window = NULL;
566
567		if (IsFullscreen)
568			CGReleaseAllDisplays();
569	}
570	else
571	{
572		if (CGLContext != NULL)
573		{
574			if(CreationParams.WindowId)
575			{
576				[(NSOpenGLContext *)OGLContext clearDrawable];
577				[(NSOpenGLContext *)OGLContext release];
578				OGLContext = NULL;
579			}
580			else
581			{
582				CGLSetCurrentContext(NULL);
583				CGLClearDrawable(CGLContext);
584				CGLDestroyContext(CGLContext);
585				CGReleaseAllDisplays();
586			}
587		}
588	}
589
590	IsFullscreen = false;
591	IsActive = false;
592	CGLContext = NULL;
593}
594
595bool CIrrDeviceMacOSX::createWindow()
596{
597	CGDisplayErr error;
598	bool result=false;
599	CGDirectDisplayID display=CGMainDisplayID();
600	CGLPixelFormatObj pixelFormat;
601	CGRect displayRect;
602#ifdef __MAC_10_6
603	CGDisplayModeRef displaymode, olddisplaymode;
604#else
605	CFDictionaryRef displaymode, olddisplaymode;
606#endif
607	GLint numPixelFormats, newSwapInterval;
608
609	int alphaSize = CreationParams.WithAlphaChannel?4:0;
610	int depthSize = CreationParams.ZBufferBits;
611	if (CreationParams.WithAlphaChannel && (CreationParams.Bits == 32))
612		alphaSize = 8;
613
614	ScreenWidth = (int) CGDisplayPixelsWide(display);
615	ScreenHeight = (int) CGDisplayPixelsHigh(display);
616
617	// we need to check where the exceptions may happen and work at them
618	// for now we will just catch them to be able to avoid an app exit
619	@try
620	{
621		if (!CreationParams.Fullscreen)
622		{
623			if(!CreationParams.WindowId) //create another window when WindowId is null
624			{
625				NSBackingStoreType type = (CreationParams.DriverType == video::EDT_OPENGL) ? NSBackingStoreBuffered : NSBackingStoreNonretained;
626
627				Window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,CreationParams.WindowSize.Width,CreationParams.WindowSize.Height) styleMask:NSTitledWindowMask+NSClosableWindowMask+NSResizableWindowMask backing:type defer:FALSE];
628			}
629
630			if (Window != NULL || CreationParams.WindowId)
631			{
632				if (CreationParams.DriverType == video::EDT_OPENGL)
633				{
634					NSOpenGLPixelFormatAttribute windowattribs[] =
635					{
636						NSOpenGLPFANoRecovery,
637						NSOpenGLPFAAccelerated,
638						NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)depthSize,
639						NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)CreationParams.Bits,
640						NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)alphaSize,
641						NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)1,
642						NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute)CreationParams.AntiAlias,
643						NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)(CreationParams.Stencilbuffer?1:0),
644						NSOpenGLPFADoubleBuffer,
645						(NSOpenGLPixelFormatAttribute)nil
646					};
647
648					if (CreationParams.AntiAlias<2)
649					{
650						windowattribs[ 9] = (NSOpenGLPixelFormatAttribute)0;
651						windowattribs[11] = (NSOpenGLPixelFormatAttribute)0;
652					}
653
654					NSOpenGLPixelFormat *format;
655					for (int i=0; i<3; ++i)
656					{
657						if (1==i)
658						{
659							// Second try without stencilbuffer
660							if (CreationParams.Stencilbuffer)
661							{
662								windowattribs[13]=(NSOpenGLPixelFormatAttribute)0;
663							}
664							else
665								continue;
666						}
667						else if (2==i)
668						{
669							// Third try without Doublebuffer
670							os::Printer::log("No doublebuffering available.", ELL_WARNING);
671							windowattribs[14]=(NSOpenGLPixelFormatAttribute)nil;
672						}
673
674						format = [[NSOpenGLPixelFormat alloc] initWithAttributes:windowattribs];
675						if (format == NULL)
676						{
677							if (CreationParams.AntiAlias>1)
678							{
679								while (!format && windowattribs[12]>1)
680								{
681									windowattribs[12] = (NSOpenGLPixelFormatAttribute)((int)windowattribs[12]-1);
682									format = [[NSOpenGLPixelFormat alloc] initWithAttributes:windowattribs];
683								}
684
685								if (!format)
686								{
687									windowattribs[9] = (NSOpenGLPixelFormatAttribute)0;
688									windowattribs[11] = (NSOpenGLPixelFormatAttribute)0;
689									format = [[NSOpenGLPixelFormat alloc] initWithAttributes:windowattribs];
690									if (!format)
691									{
692										// reset values for next try
693										windowattribs[9] = (NSOpenGLPixelFormatAttribute)1;
694										windowattribs[11] = (NSOpenGLPixelFormatAttribute)CreationParams.AntiAlias;
695									}
696									else
697									{
698										os::Printer::log("No FSAA available.", ELL_WARNING);
699									}
700								}
701							}
702						}
703						else
704							break;
705					}
706					CreationParams.AntiAlias = windowattribs[11];
707					CreationParams.Stencilbuffer=(windowattribs[13]==1);
708
709					if (format != NULL)
710					{
711						OGLContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:NULL];
712						[format release];
713					}
714				}
715
716				if (OGLContext != NULL || CreationParams.DriverType != video::EDT_OPENGL)
717				{
718					if (!CreationParams.WindowId)
719					{
720						[Window center];
721						[Window setDelegate:(id<NSWindowDelegate>)[NSApp delegate]];
722
723						if(CreationParams.DriverType == video::EDT_OPENGL)
724							[OGLContext setView:[Window contentView]];
725
726						[Window setAcceptsMouseMovedEvents:TRUE];
727						[Window setIsVisible:TRUE];
728						[Window makeKeyAndOrderFront:nil];
729					}
730					else if(CreationParams.DriverType == video::EDT_OPENGL) //use another window for drawing
731						[OGLContext setView:(NSView*)CreationParams.WindowId];
732
733					if (CreationParams.DriverType == video::EDT_OPENGL)
734						CGLContext = (CGLContextObj) [OGLContext CGLContextObj];
735
736					DeviceWidth = CreationParams.WindowSize.Width;
737					DeviceHeight = CreationParams.WindowSize.Height;
738					result = true;
739				}
740			}
741		}
742		else
743		{
744			IsFullscreen = true;
745
746#ifdef __MAC_10_6
747			displaymode = CGDisplayCopyDisplayMode(display);
748
749			CFArrayRef Modes = CGDisplayCopyAllDisplayModes(display, NULL);
750
751			for(int i = 0; i < CFArrayGetCount(Modes); ++i)
752			{
753				CGDisplayModeRef CurrentMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(Modes, i);
754
755				u8 Depth = 0;
756
757				CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(CurrentMode);
758
759				if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
760					Depth = 32;
761				else
762					if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
763						Depth = 16;
764					else
765						if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
766							Depth = 8;
767
768				if(Depth == CreationParams.Bits)
769					if((CGDisplayModeGetWidth(CurrentMode) == CreationParams.WindowSize.Width) && (CGDisplayModeGetHeight(CurrentMode) == CreationParams.WindowSize.Height))
770					{
771						displaymode = CurrentMode;
772						break;
773					}
774			}
775#else
776			displaymode = CGDisplayBestModeForParameters(display,CreationParams.Bits,CreationParams.WindowSize.Width,CreationParams.WindowSize.Height,NULL);
777#endif
778
779			if (displaymode != NULL)
780			{
781#ifdef __MAC_10_6
782				olddisplaymode = CGDisplayCopyDisplayMode(display);
783#else
784				olddisplaymode = CGDisplayCurrentMode(display);
785#endif
786
787				error = CGCaptureAllDisplays();
788				if (error == CGDisplayNoErr)
789				{
790#ifdef __MAC_10_6
791					error = CGDisplaySetDisplayMode(display, displaymode, NULL);
792#else
793					error = CGDisplaySwitchToMode(display, displaymode);
794#endif
795
796					if (error == CGDisplayNoErr)
797					{
798						if (CreationParams.DriverType == video::EDT_OPENGL)
799						{
800							CGLPixelFormatAttribute	fullattribs[] =
801							{
802								kCGLPFAFullScreen,
803								kCGLPFADisplayMask, (CGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(display),
804								kCGLPFADoubleBuffer,
805								kCGLPFANoRecovery,
806								kCGLPFAAccelerated,
807								kCGLPFADepthSize, (CGLPixelFormatAttribute)depthSize,
808								kCGLPFAColorSize, (CGLPixelFormatAttribute)CreationParams.Bits,
809								kCGLPFAAlphaSize, (CGLPixelFormatAttribute)alphaSize,
810								kCGLPFASampleBuffers, (CGLPixelFormatAttribute)(CreationParams.AntiAlias?1:0),
811								kCGLPFASamples, (CGLPixelFormatAttribute)CreationParams.AntiAlias,
812								kCGLPFAStencilSize, (CGLPixelFormatAttribute)(CreationParams.Stencilbuffer?1:0),
813								(CGLPixelFormatAttribute)NULL
814							};
815
816							pixelFormat = NULL;
817							numPixelFormats = 0;
818							CGLChoosePixelFormat(fullattribs,&pixelFormat,&numPixelFormats);
819
820							if (pixelFormat != NULL)
821							{
822								CGLCreateContext(pixelFormat,NULL,&CGLContext);
823								CGLDestroyPixelFormat(pixelFormat);
824							}
825
826							if (CGLContext != NULL)
827							{
828#ifdef __MAC_10_6
829								CGLSetFullScreenOnDisplay(CGLContext, CGDisplayIDToOpenGLDisplayMask(display));
830#else
831								CGLSetFullScreen(CGLContext);
832#endif
833								displayRect = CGDisplayBounds(display);
834								ScreenWidth = DeviceWidth = (int)displayRect.size.width;
835								ScreenHeight = DeviceHeight = (int)displayRect.size.height;
836								CreationParams.WindowSize.set(ScreenWidth, ScreenHeight);
837								result = true;
838							}
839						}
840						else
841						{
842							Window = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained defer:NO screen:[NSScreen mainScreen]];
843
844							[Window setLevel: CGShieldingWindowLevel()];
845							[Window setAcceptsMouseMovedEvents:TRUE];
846							[Window setIsVisible:TRUE];
847							[Window makeKeyAndOrderFront:nil];
848
849							displayRect = CGDisplayBounds(display);
850							ScreenWidth = DeviceWidth = (int)displayRect.size.width;
851							ScreenHeight = DeviceHeight = (int)displayRect.size.height;
852							CreationParams.WindowSize.set(ScreenWidth, ScreenHeight);
853							result = true;
854						}
855					}
856					if (!result)
857						CGReleaseAllDisplays();
858				}
859			}
860		}
861	}
862	@catch (NSException *exception)
863	{
864		closeDevice();
865		result = false;
866	}
867
868	if (result)
869	{
870		// fullscreen?
871		if (Window == NULL && !CreationParams.WindowId) //hide menus in fullscreen mode only
872#ifdef __MAC_10_6
873			[NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
874#else
875			SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar);
876#endif
877
878		if(CreationParams.DriverType == video::EDT_OPENGL)
879		{
880			CGLSetCurrentContext(CGLContext);
881			newSwapInterval = (CreationParams.Vsync) ? 1 : 0;
882			CGLSetParameter(CGLContext,kCGLCPSwapInterval,&newSwapInterval);
883		}
884	}
885
886	return (result);
887}
888
889void CIrrDeviceMacOSX::setResize(int width, int height)
890{
891	// set new window size
892	DeviceWidth = width;
893	DeviceHeight = height;
894
895	// update the size of the opengl rendering context
896	if(OGLContext);
897		[OGLContext update];
898
899	// resize the driver to the inner pane size
900	if (Window)
901	{
902		NSRect driverFrame = [Window contentRectForFrameRect:[Window frame]];
903		getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)driverFrame.size.width, (s32)driverFrame.size.height));
904	}
905	else
906		getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)width, (s32)height));
907
908	if (CreationParams.WindowId && OGLContext)
909		[(NSOpenGLContext *)OGLContext update];
910}
911
912
913void CIrrDeviceMacOSX::createDriver()
914{
915	switch (CreationParams.DriverType)
916	{
917		case video::EDT_SOFTWARE:
918		#ifdef _IRR_COMPILE_WITH_SOFTWARE_
919			VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
920			SoftwareRendererType = 2;
921		#else
922			os::Printer::log("No Software driver support compiled in.", ELL_ERROR);
923		#endif
924			break;
925
926		case video::EDT_BURNINGSVIDEO:
927		#ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
928			VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);
929			SoftwareRendererType = 1;
930		#else
931			os::Printer::log("Burning's video driver was not compiled in.", ELL_ERROR);
932		#endif
933			break;
934
935		case video::EDT_OPENGL:
936		#ifdef _IRR_COMPILE_WITH_OPENGL_
937			VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
938		#else
939			os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
940		#endif
941			break;
942
943		case video::EDT_DIRECT3D8:
944		case video::EDT_DIRECT3D9:
945			os::Printer::log("This driver is not available in OSX. Try OpenGL or Software renderer.", ELL_ERROR);
946			break;
947
948		case video::EDT_NULL:
949			VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
950			break;
951
952		default:
953			os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
954			break;
955	}
956}
957
958void CIrrDeviceMacOSX::flush()
959{
960	if (CGLContext != NULL)
961		CGLFlushDrawable(CGLContext);
962}
963
964bool CIrrDeviceMacOSX::run()
965{
966	NSAutoreleasePool* Pool = [[NSAutoreleasePool alloc] init];
967
968	NSEvent *event;
969	irr::SEvent	ievent;
970
971	os::Timer::tick();
972	storeMouseLocation();
973
974	event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
975	if (event != nil)
976	{
977		bzero(&ievent,sizeof(ievent));
978
979		switch([(NSEvent *)event type])
980		{
981			case NSKeyDown:
982				postKeyEvent(event,ievent,true);
983				break;
984
985			case NSKeyUp:
986				postKeyEvent(event,ievent,false);
987				break;
988
989			case NSFlagsChanged:
990				ievent.EventType = irr::EET_KEY_INPUT_EVENT;
991				ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
992				ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
993
994				if (IsShiftDown != ievent.KeyInput.Shift)
995				{
996					ievent.KeyInput.Char = irr::KEY_SHIFT;
997					ievent.KeyInput.Key = irr::KEY_SHIFT;
998					ievent.KeyInput.PressedDown = ievent.KeyInput.Shift;
999
1000					IsShiftDown = ievent.KeyInput.Shift;
1001
1002					postEventFromUser(ievent);
1003				}
1004
1005				if (IsControlDown != ievent.KeyInput.Control)
1006				{
1007					ievent.KeyInput.Char = irr::KEY_CONTROL;
1008					ievent.KeyInput.Key = irr::KEY_CONTROL;
1009					ievent.KeyInput.PressedDown = ievent.KeyInput.Control;
1010
1011					IsControlDown = ievent.KeyInput.Control;
1012
1013					postEventFromUser(ievent);
1014				}
1015
1016				[NSApp sendEvent:event];
1017				break;
1018
1019			case NSLeftMouseDown:
1020				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1021				ievent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
1022				MouseButtonStates |= irr::EMBSM_LEFT;
1023				ievent.MouseInput.ButtonStates = MouseButtonStates;
1024				postMouseEvent(event,ievent);
1025				break;
1026
1027			case NSLeftMouseUp:
1028				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1029				MouseButtonStates &= !irr::EMBSM_LEFT;
1030				ievent.MouseInput.ButtonStates = MouseButtonStates;
1031				ievent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
1032				postMouseEvent(event,ievent);
1033				break;
1034
1035			case NSOtherMouseDown:
1036				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1037				ievent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
1038				MouseButtonStates |= irr::EMBSM_MIDDLE;
1039				ievent.MouseInput.ButtonStates = MouseButtonStates;
1040				postMouseEvent(event,ievent);
1041				break;
1042
1043			case NSOtherMouseUp:
1044				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1045				MouseButtonStates &= !irr::EMBSM_MIDDLE;
1046				ievent.MouseInput.ButtonStates = MouseButtonStates;
1047				ievent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
1048				postMouseEvent(event,ievent);
1049				break;
1050
1051			case NSMouseMoved:
1052			case NSLeftMouseDragged:
1053			case NSRightMouseDragged:
1054			case NSOtherMouseDragged:
1055				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1056				ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1057				ievent.MouseInput.ButtonStates = MouseButtonStates;
1058				postMouseEvent(event,ievent);
1059				break;
1060
1061			case NSRightMouseDown:
1062				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1063				ievent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
1064				MouseButtonStates |= irr::EMBSM_RIGHT;
1065				ievent.MouseInput.ButtonStates = MouseButtonStates;
1066				postMouseEvent(event,ievent);
1067				break;
1068
1069			case NSRightMouseUp:
1070				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1071				ievent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
1072				MouseButtonStates &= !irr::EMBSM_RIGHT;
1073				ievent.MouseInput.ButtonStates = MouseButtonStates;
1074				postMouseEvent(event,ievent);
1075				break;
1076
1077			case NSScrollWheel:
1078				ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1079				ievent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
1080				ievent.MouseInput.Wheel = [(NSEvent *)event deltaY];
1081				if (ievent.MouseInput.Wheel < 1.0f)
1082					ievent.MouseInput.Wheel *= 10.0f;
1083				else
1084					ievent.MouseInput.Wheel *= 5.0f;
1085				postMouseEvent(event,ievent);
1086				break;
1087
1088			default:
1089				[NSApp sendEvent:event];
1090				break;
1091		}
1092	}
1093
1094	pollJoysticks();
1095
1096	[Pool release];
1097
1098	return (![[NSApp delegate] isQuit] && IsActive);
1099}
1100
1101
1102//! Pause the current process for the minimum time allowed only to allow other processes to execute
1103void CIrrDeviceMacOSX::yield()
1104{
1105	struct timespec ts = {0,0};
1106	nanosleep(&ts, NULL);
1107}
1108
1109
1110//! Pause execution and let other processes to run for a specified amount of time.
1111void CIrrDeviceMacOSX::sleep(u32 timeMs, bool pauseTimer=false)
1112{
1113	bool wasStopped = Timer ? Timer->isStopped() : true;
1114
1115	struct timespec ts;
1116	ts.tv_sec = (time_t) (timeMs / 1000);
1117	ts.tv_nsec = (long) (timeMs % 1000) * 1000000;
1118
1119	if (pauseTimer && !wasStopped)
1120		Timer->stop();
1121
1122	nanosleep(&ts, NULL);
1123
1124	if (pauseTimer && !wasStopped)
1125		Timer->start();
1126}
1127
1128
1129void CIrrDeviceMacOSX::setWindowCaption(const wchar_t* text)
1130{
1131	size_t size;
1132	char title[1024];
1133
1134	if (Window != NULL)
1135	{
1136		size = wcstombs(title,text,1024);
1137		title[1023] = 0;
1138#ifdef __MAC_10_6
1139		NSString* name = [NSString stringWithCString:title encoding:NSUTF8StringEncoding];
1140#else
1141		NSString* name = [NSString stringWithCString:title length:size];
1142#endif
1143		[Window setTitle:name];
1144		[name release];
1145	}
1146}
1147
1148
1149bool CIrrDeviceMacOSX::isWindowActive() const
1150{
1151	return (IsActive);
1152}
1153
1154
1155bool CIrrDeviceMacOSX::isWindowFocused() const
1156{
1157	if (Window != NULL)
1158		return [Window isKeyWindow];
1159	return false;
1160}
1161
1162
1163bool CIrrDeviceMacOSX::isWindowMinimized() const
1164{
1165	if (Window != NULL)
1166		return [Window isMiniaturized];
1167	return false;
1168}
1169
1170
1171void CIrrDeviceMacOSX::postKeyEvent(void *event,irr::SEvent &ievent,bool pressed)
1172{
1173	NSString *str;
1174	std::map<int,int>::const_iterator iter;
1175	unsigned int result,c,mkey,mchar;
1176	const unsigned char *cStr;
1177	BOOL skipCommand;
1178
1179	str = [(NSEvent *)event characters];
1180	if ((str != nil) && ([str length] > 0))
1181	{
1182		mkey = mchar = 0;
1183		skipCommand = false;
1184		c = [str characterAtIndex:0];
1185		mchar = c;
1186
1187		iter = KeyCodes.find([(NSEvent *)event keyCode]);
1188		if (iter != KeyCodes.end())
1189			mkey = (*iter).second;
1190		else if ((iter = KeyCodes.find(c))  != KeyCodes.end())
1191			mkey = (*iter).second;
1192		else
1193		{
1194			// workaround for period character
1195			if (c == 0x2E)
1196			{
1197				mkey = irr::KEY_PERIOD;
1198				mchar = '.';
1199			}
1200			else
1201			{
1202				cStr = (unsigned char *)[str cStringUsingEncoding:NSWindowsCP1252StringEncoding];
1203				if (cStr != NULL && strlen((char*)cStr) > 0)
1204				{
1205					mchar = cStr[0];
1206					mkey = toupper(mchar);
1207					if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1208					{
1209						if (mkey == 'C' || mkey == 'V' || mkey == 'X')
1210						{
1211							mchar = 0;
1212							skipCommand = true;
1213						}
1214					}
1215				}
1216			}
1217		}
1218
1219		ievent.EventType = irr::EET_KEY_INPUT_EVENT;
1220		ievent.KeyInput.Key = (irr::EKEY_CODE)mkey;
1221		ievent.KeyInput.PressedDown = pressed;
1222		ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1223		ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1224		ievent.KeyInput.Char = mchar;
1225
1226		if (skipCommand)
1227			ievent.KeyInput.Control = true;
1228		else if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1229			[NSApp sendEvent:(NSEvent *)event];
1230
1231		postEventFromUser(ievent);
1232	}
1233}
1234
1235
1236void CIrrDeviceMacOSX::postMouseEvent(void *event,irr::SEvent &ievent)
1237{
1238	bool post = true;
1239
1240	if (Window != NULL)
1241	{
1242		ievent.MouseInput.X = (int)[(NSEvent *)event locationInWindow].x;
1243		ievent.MouseInput.Y = DeviceHeight - (int)[(NSEvent *)event locationInWindow].y;
1244
1245		if (ievent.MouseInput.Y < 0)
1246			post = false;
1247	}
1248	else
1249	{
1250		CGEventRef ourEvent = CGEventCreate(NULL);
1251		CGPoint point = CGEventGetLocation(ourEvent);
1252		CFRelease(ourEvent);
1253
1254		ievent.MouseInput.X = (int)point.x;
1255		ievent.MouseInput.Y = (int)point.y;
1256
1257		if (ievent.MouseInput.Y < 0)
1258			post = false;
1259	}
1260
1261	if (post)
1262	{
1263		ievent.MouseInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1264		ievent.MouseInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1265
1266		postEventFromUser(ievent);
1267	}
1268
1269	[NSApp sendEvent:(NSEvent *)event];
1270}
1271
1272
1273void CIrrDeviceMacOSX::storeMouseLocation()
1274{
1275	int x,y;
1276
1277	if (Window != NULL)
1278	{
1279		NSPoint	p;
1280		p = [NSEvent mouseLocation];
1281		p = [Window convertScreenToBase:p];
1282		x = (int)p.x;
1283		y = DeviceHeight - (int)p.y;
1284	}
1285	else
1286	{
1287		CGEventRef ourEvent = CGEventCreate(NULL);
1288		CGPoint point = CGEventGetLocation(ourEvent);
1289		CFRelease(ourEvent);
1290
1291		x = (int)point.x;
1292		y = (int)point.y;
1293
1294		const core::position2di& curr = ((CCursorControl *)CursorControl)->getPosition();
1295		if (curr.X != x || curr.Y != y)
1296		{
1297			// In fullscreen mode, events are not sent regularly so rely on polling
1298			irr::SEvent ievent;
1299			ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1300			ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1301			ievent.MouseInput.X = x;
1302			ievent.MouseInput.Y = y;
1303			postEventFromUser(ievent);
1304		}
1305	}
1306
1307	((CCursorControl *)CursorControl)->updateInternalCursorPosition(x,y);
1308}
1309
1310
1311void CIrrDeviceMacOSX::setMouseLocation(int x,int y)
1312{
1313	NSPoint	p;
1314	CGPoint	c;
1315
1316	if (Window != NULL)
1317	{
1318		// Irrlicht window exists
1319		p.x = (float) x;
1320		p.y = (float) (DeviceHeight - y);
1321		p = [Window convertBaseToScreen:p];
1322		p.y = ScreenHeight - p.y;
1323	}
1324	else
1325	{
1326		p.x = (float) x;
1327		p.y = (float) y + (ScreenHeight - DeviceHeight);
1328	}
1329
1330	c.x = p.x;
1331	c.y = p.y;
1332
1333#ifdef __MAC_10_6
1334	/*CGEventSourceRef SourceRef = CGEventSourceCreate(0);
1335	CGEventSourceSetLocalEventsSuppressionInterval(SourceRef, 0);
1336	CFRelease(SourceRef);*/
1337	CGSetLocalEventsSuppressionInterval(0);
1338#else
1339	CGSetLocalEventsSuppressionInterval(0);
1340#endif
1341	CGWarpMouseCursorPosition(c);
1342}
1343
1344
1345void CIrrDeviceMacOSX::setCursorVisible(bool visible)
1346{
1347	if (visible)
1348		CGDisplayShowCursor(CGMainDisplayID());
1349	else
1350		CGDisplayHideCursor(CGMainDisplayID());
1351}
1352
1353
1354void CIrrDeviceMacOSX::initKeycodes()
1355{
1356	KeyCodes[kVK_UpArrow] = irr::KEY_UP;
1357	KeyCodes[kVK_DownArrow] = irr::KEY_DOWN;
1358	KeyCodes[kVK_LeftArrow] = irr::KEY_LEFT;
1359	KeyCodes[kVK_RightArrow] = irr::KEY_RIGHT;
1360	KeyCodes[kVK_F1]	= irr::KEY_F1;
1361	KeyCodes[kVK_F2]	= irr::KEY_F2;
1362	KeyCodes[kVK_F3]	= irr::KEY_F3;
1363	KeyCodes[kVK_F4]	= irr::KEY_F4;
1364	KeyCodes[kVK_F5]	= irr::KEY_F5;
1365	KeyCodes[kVK_F6]	= irr::KEY_F6;
1366	KeyCodes[kVK_F7]	= irr::KEY_F7;
1367	KeyCodes[kVK_F8]	= irr::KEY_F8;
1368	KeyCodes[kVK_F9]	= irr::KEY_F9;
1369	KeyCodes[kVK_F10]	= irr::KEY_F10;
1370	KeyCodes[kVK_F11]	= irr::KEY_F11;
1371	KeyCodes[kVK_F12]	= irr::KEY_F12;
1372	KeyCodes[kVK_F13]	= irr::KEY_F13;
1373	KeyCodes[kVK_F14]	= irr::KEY_F14;
1374	KeyCodes[kVK_F15]	= irr::KEY_F15;
1375	KeyCodes[kVK_F16]	= irr::KEY_F16;
1376	KeyCodes[kVK_F17]	= irr::KEY_F17;
1377	KeyCodes[kVK_F18]	= irr::KEY_F18;
1378	KeyCodes[kVK_F19]	= irr::KEY_F19;
1379	KeyCodes[kVK_F20]	= irr::KEY_F20;
1380	KeyCodes[kVK_Home]	= irr::KEY_HOME;
1381	KeyCodes[kVK_End]	= irr::KEY_END;
1382	KeyCodes[NSInsertFunctionKey] = irr::KEY_INSERT;
1383	KeyCodes[kVK_ForwardDelete] = irr::KEY_DELETE;
1384	KeyCodes[kVK_Help] = irr::KEY_HELP;
1385	KeyCodes[NSSelectFunctionKey] = irr::KEY_SELECT;
1386	KeyCodes[NSPrintFunctionKey] = irr::KEY_PRINT;
1387	KeyCodes[NSExecuteFunctionKey] = irr::KEY_EXECUT;
1388	KeyCodes[NSPrintScreenFunctionKey] = irr::KEY_SNAPSHOT;
1389	KeyCodes[NSPauseFunctionKey] = irr::KEY_PAUSE;
1390	KeyCodes[NSScrollLockFunctionKey] = irr::KEY_SCROLL;
1391	KeyCodes[kVK_Delete] = irr::KEY_BACK;
1392	KeyCodes[kVK_Tab] = irr::KEY_TAB;
1393	KeyCodes[kVK_Return] = irr::KEY_RETURN;
1394	KeyCodes[kVK_Escape] = irr::KEY_ESCAPE;
1395	KeyCodes[kVK_Control] = irr::KEY_CONTROL;
1396	KeyCodes[kVK_RightControl] = irr::KEY_RCONTROL;
1397	KeyCodes[kVK_Command] = irr::KEY_MENU;
1398	KeyCodes[kVK_Shift] = irr::KEY_SHIFT;
1399	KeyCodes[kVK_RightShift] = irr::KEY_RSHIFT;
1400	KeyCodes[kVK_Space] = irr::KEY_SPACE;
1401
1402	KeyCodes[kVK_ANSI_A] = irr::KEY_KEY_A;
1403	KeyCodes[kVK_ANSI_B] = irr::KEY_KEY_B;
1404	KeyCodes[kVK_ANSI_C] = irr::KEY_KEY_C;
1405	KeyCodes[kVK_ANSI_D] = irr::KEY_KEY_D;
1406	KeyCodes[kVK_ANSI_E] = irr::KEY_KEY_E;
1407	KeyCodes[kVK_ANSI_F] = irr::KEY_KEY_F;
1408	KeyCodes[kVK_ANSI_G] = irr::KEY_KEY_G;
1409	KeyCodes[kVK_ANSI_H] = irr::KEY_KEY_H;
1410	KeyCodes[kVK_ANSI_I] = irr::KEY_KEY_I;
1411	KeyCodes[kVK_ANSI_J] = irr::KEY_KEY_J;
1412	KeyCodes[kVK_ANSI_K] = irr::KEY_KEY_K;
1413	KeyCodes[kVK_ANSI_L] = irr::KEY_KEY_L;
1414	KeyCodes[kVK_ANSI_M] = irr::KEY_KEY_M;
1415	KeyCodes[kVK_ANSI_N] = irr::KEY_KEY_N;
1416	KeyCodes[kVK_ANSI_O] = irr::KEY_KEY_O;
1417	KeyCodes[kVK_ANSI_P] = irr::KEY_KEY_P;
1418	KeyCodes[kVK_ANSI_Q] = irr::KEY_KEY_Q;
1419	KeyCodes[kVK_ANSI_R] = irr::KEY_KEY_R;
1420	KeyCodes[kVK_ANSI_S] = irr::KEY_KEY_S;
1421	KeyCodes[kVK_ANSI_T] = irr::KEY_KEY_T;
1422	KeyCodes[kVK_ANSI_U] = irr::KEY_KEY_U;
1423	KeyCodes[kVK_ANSI_V] = irr::KEY_KEY_V;
1424	KeyCodes[kVK_ANSI_W] = irr::KEY_KEY_W;
1425	KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1426	KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1427	KeyCodes[kVK_ANSI_Y] = irr::KEY_KEY_Y;
1428	KeyCodes[kVK_ANSI_Z] = irr::KEY_KEY_Z;
1429
1430	KeyCodes[kVK_ANSI_0] = irr::KEY_KEY_0;
1431	KeyCodes[kVK_ANSI_1] = irr::KEY_KEY_1;
1432	KeyCodes[kVK_ANSI_2] = irr::KEY_KEY_2;
1433	KeyCodes[kVK_ANSI_3] = irr::KEY_KEY_3;
1434	KeyCodes[kVK_ANSI_4] = irr::KEY_KEY_4;
1435	KeyCodes[kVK_ANSI_5] = irr::KEY_KEY_5;
1436	KeyCodes[kVK_ANSI_6] = irr::KEY_KEY_6;
1437	KeyCodes[kVK_ANSI_7] = irr::KEY_KEY_7;
1438	KeyCodes[kVK_ANSI_8] = irr::KEY_KEY_8;
1439	KeyCodes[kVK_ANSI_9] = irr::KEY_KEY_9;
1440
1441	KeyCodes[kVK_ANSI_Slash] = irr::KEY_DIVIDE;
1442	KeyCodes[kVK_ANSI_Comma] = irr::KEY_COMMA;
1443	KeyCodes[kVK_ANSI_Period] = irr::KEY_PERIOD;
1444	KeyCodes[kVK_PageUp] = irr::KEY_PRIOR;
1445	KeyCodes[kVK_PageDown] = irr::KEY_NEXT;
1446
1447	KeyCodes[kVK_ANSI_Keypad0] = irr::KEY_NUMPAD0;
1448	KeyCodes[kVK_ANSI_Keypad1] = irr::KEY_NUMPAD1;
1449	KeyCodes[kVK_ANSI_Keypad2] = irr::KEY_NUMPAD2;
1450	KeyCodes[kVK_ANSI_Keypad3] = irr::KEY_NUMPAD3;
1451	KeyCodes[kVK_ANSI_Keypad4] = irr::KEY_NUMPAD4;
1452	KeyCodes[kVK_ANSI_Keypad5] = irr::KEY_NUMPAD5;
1453	KeyCodes[kVK_ANSI_Keypad6] = irr::KEY_NUMPAD6;
1454	KeyCodes[kVK_ANSI_Keypad7] = irr::KEY_NUMPAD7;
1455	KeyCodes[kVK_ANSI_Keypad8] = irr::KEY_NUMPAD8;
1456	KeyCodes[kVK_ANSI_Keypad9] = irr::KEY_NUMPAD9;
1457
1458	KeyCodes[kVK_ANSI_KeypadDecimal] = irr::KEY_DECIMAL;
1459	KeyCodes[kVK_ANSI_KeypadMultiply] = irr::KEY_MULTIPLY;
1460	KeyCodes[kVK_ANSI_KeypadPlus] = irr::KEY_PLUS;
1461	KeyCodes[kVK_ANSI_KeypadClear] = irr::KEY_OEM_CLEAR;
1462	KeyCodes[kVK_ANSI_KeypadDivide] = irr::KEY_DIVIDE;
1463	KeyCodes[kVK_ANSI_KeypadEnter] = irr::KEY_RETURN;
1464	KeyCodes[kVK_ANSI_KeypadMinus] = irr::KEY_SUBTRACT;
1465}
1466
1467
1468//! Sets if the window should be resizable in windowed mode.
1469void CIrrDeviceMacOSX::setResizable(bool resize)
1470{
1471	IsResizable = resize;
1472#if 0
1473	if (resize)
1474		[Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask];
1475	else
1476		[Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask];
1477#endif
1478}
1479
1480
1481bool CIrrDeviceMacOSX::isResizable() const
1482{
1483	return IsResizable;
1484}
1485
1486
1487void CIrrDeviceMacOSX::minimizeWindow()
1488{
1489	if (Window != NULL)
1490		[Window miniaturize:[NSApp self]];
1491}
1492
1493
1494//! Maximizes the window if possible.
1495void CIrrDeviceMacOSX::maximizeWindow()
1496{
1497	// todo: implement
1498}
1499
1500
1501//! Restore the window to normal size if possible.
1502void CIrrDeviceMacOSX::restoreWindow()
1503{
1504	[Window deminiaturize:[NSApp self]];
1505}
1506
1507
1508bool CIrrDeviceMacOSX::present(video::IImage* surface, void* windowId, core::rect<s32>* src )
1509{
1510	// todo: implement window ID and src rectangle
1511
1512	if (!surface)
1513		return false;
1514
1515	if (SoftwareRendererType > 0)
1516	{
1517		const u32 colorSamples=3;
1518		// do we need to change the size?
1519		const bool updateSize = !SoftwareDriverTarget ||
1520				s32([SoftwareDriverTarget size].width) != surface->getDimension().Width ||
1521				s32([SoftwareDriverTarget size].height) != surface->getDimension().Height;
1522
1523		NSRect areaRect = NSMakeRect(0.0, 0.0, surface->getDimension().Width, surface->getDimension().Height);
1524		const u32 destPitch = (colorSamples * areaRect.size.width);
1525
1526		// create / update the target
1527		if (updateSize)
1528		{
1529			[SoftwareDriverTarget release];
1530			// allocate target for IImage
1531			SoftwareDriverTarget = [[NSBitmapImageRep alloc]
1532					initWithBitmapDataPlanes: nil
1533					pixelsWide: areaRect.size.width
1534					pixelsHigh: areaRect.size.height
1535					bitsPerSample: 8
1536					samplesPerPixel: colorSamples
1537					hasAlpha: NO
1538					isPlanar: NO
1539					colorSpaceName: NSCalibratedRGBColorSpace
1540					bytesPerRow: destPitch
1541					bitsPerPixel: 8*colorSamples];
1542		}
1543
1544		if (SoftwareDriverTarget==nil)
1545			return false;
1546
1547		// get pointer to image data
1548		unsigned char* imgData = (unsigned char*)surface->lock();
1549
1550		u8* srcdata = reinterpret_cast<u8*>(imgData);
1551		u8* destData = reinterpret_cast<u8*>([SoftwareDriverTarget bitmapData]);
1552		const u32 srcheight = core::min_(surface->getDimension().Height, (u32)areaRect.size.height);
1553		const u32 srcPitch = surface->getPitch();
1554		const u32 minWidth = core::min_(surface->getDimension().Width, (u32)areaRect.size.width);
1555		for (u32 y=0; y!=srcheight; ++y)
1556		{
1557			if(SoftwareRendererType == 2)
1558			{
1559				if (surface->getColorFormat() == video::ECF_A8R8G8B8)
1560					video::CColorConverter::convert_A8R8G8B8toB8G8R8(srcdata, minWidth, destData);
1561				else if (surface->getColorFormat() == video::ECF_A1R5G5B5)
1562					video::CColorConverter::convert_A1R5G5B5toB8G8R8(srcdata, minWidth, destData);
1563				else
1564					video::CColorConverter::convert_viaFormat(srcdata, surface->getColorFormat(), minWidth, destData, video::ECF_R8G8B8);
1565			}
1566			else
1567			{
1568				if (surface->getColorFormat() == video::ECF_A8R8G8B8)
1569					video::CColorConverter::convert_A8R8G8B8toR8G8B8(srcdata, minWidth, destData);
1570				else if (surface->getColorFormat() == video::ECF_A1R5G5B5)
1571					video::CColorConverter::convert_A1R5G5B5toR8G8B8(srcdata, minWidth, destData);
1572				else
1573					video::CColorConverter::convert_viaFormat(srcdata, surface->getColorFormat(), minWidth, destData, video::ECF_R8G8B8);
1574			}
1575
1576			srcdata += srcPitch;
1577			destData += destPitch;
1578		}
1579
1580		// unlock the data
1581		surface->unlock();
1582
1583		// todo: draw properly into a sub-view
1584		[SoftwareDriverTarget draw];
1585	}
1586
1587	return false;
1588}
1589
1590
1591#if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1592static void joystickRemovalCallback(void * target,
1593		IOReturn result, void * refcon, void * sender)
1594{
1595	JoystickInfo *joy = (JoystickInfo *) refcon;
1596	joy->removed = 1;
1597}
1598#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1599
1600
1601bool CIrrDeviceMacOSX::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
1602{
1603#if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1604	ActiveJoysticks.clear();
1605	joystickInfo.clear();
1606
1607	io_object_t hidObject = 0;
1608	io_iterator_t hidIterator = 0;
1609	IOReturn result = kIOReturnSuccess;
1610	mach_port_t masterPort = 0;
1611	CFMutableDictionaryRef hidDictionaryRef = NULL;
1612
1613	result = IOMasterPort (bootstrap_port, &masterPort);
1614	if (kIOReturnSuccess != result)
1615	{
1616		os::Printer::log("initialiseJoysticks IOMasterPort failed", ELL_ERROR);
1617		return false;
1618	}
1619
1620	hidDictionaryRef = IOServiceMatching (kIOHIDDeviceKey);
1621	if (!hidDictionaryRef)
1622	{
1623		os::Printer::log("initialiseJoysticks IOServiceMatching failed", ELL_ERROR);
1624		return false;
1625	}
1626	result = IOServiceGetMatchingServices (masterPort, hidDictionaryRef, &hidIterator);
1627
1628	if (kIOReturnSuccess != result)
1629	{
1630		os::Printer::log("initialiseJoysticks IOServiceGetMatchingServices failed", ELL_ERROR);
1631		return false;
1632	}
1633
1634	//no joysticks just return
1635	if (!hidIterator)
1636		return false;
1637
1638	u32 jindex = 0u;
1639	while ((hidObject = IOIteratorNext (hidIterator)))
1640	{
1641		JoystickInfo info;
1642
1643		// get dictionary for HID properties
1644		CFMutableDictionaryRef hidProperties = 0;
1645
1646		kern_return_t kern_result = IORegistryEntryCreateCFProperties (hidObject, &hidProperties, kCFAllocatorDefault, kNilOptions);
1647		if ((kern_result == KERN_SUCCESS) && hidProperties)
1648		{
1649			HRESULT plugInResult = S_OK;
1650			SInt32 score = 0;
1651			IOCFPlugInInterface ** ppPlugInInterface = NULL;
1652			result = IOCreatePlugInInterfaceForService (hidObject, kIOHIDDeviceUserClientTypeID,
1653													kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
1654			if (kIOReturnSuccess == result)
1655			{
1656				plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
1657									CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void **) &(info.interface));
1658				if (plugInResult != S_OK)
1659					os::Printer::log("initialiseJoysticks query HID class device interface failed", ELL_ERROR);
1660				(*ppPlugInInterface)->Release(ppPlugInInterface);
1661			}
1662			else
1663				continue;
1664
1665			if (info.interface != NULL)
1666			{
1667				result = (*(info.interface))->open (info.interface, 0);
1668				if (result == kIOReturnSuccess)
1669				{
1670					(*(info.interface))->setRemovalCallback (info.interface, joystickRemovalCallback, &info, &info);
1671					getJoystickDeviceInfo(hidObject, hidProperties, &info);
1672
1673					// get elements
1674					CFTypeRef refElementTop = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
1675					if (refElementTop)
1676					{
1677						CFTypeID type = CFGetTypeID (refElementTop);
1678						if (type == CFArrayGetTypeID())
1679						{
1680							CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
1681							info.numActiveJoysticks = ActiveJoysticks.size();
1682							CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, &info);
1683						}
1684					}
1685				}
1686				else
1687				{
1688					CFRelease (hidProperties);
1689					os::Printer::log("initialiseJoysticks Open interface failed", ELL_ERROR);
1690					continue;
1691				}
1692
1693				CFRelease (hidProperties);
1694
1695				result = IOObjectRelease (hidObject);
1696
1697				if ( (info.usagePage != kHIDPage_GenericDesktop) ||
1698					((info.usage != kHIDUsage_GD_Joystick &&
1699					info.usage != kHIDUsage_GD_GamePad &&
1700					info.usage != kHIDUsage_GD_MultiAxisController)) )
1701				{
1702					closeJoystickDevice (&info);
1703					continue;
1704				}
1705
1706				for (u32 i = 0; i < 6; ++i)
1707					info.persistentData.JoystickEvent.Axis[i] = 0;
1708
1709				ActiveJoysticks.push_back(info);
1710
1711				SJoystickInfo returnInfo;
1712				returnInfo.Joystick = jindex;
1713				returnInfo.Axes = info.axes;
1714				//returnInfo.Hats = info.hats;
1715				returnInfo.Buttons = info.buttons;
1716				returnInfo.Name = info.joystickName;
1717				returnInfo.PovHat = SJoystickInfo::POV_HAT_UNKNOWN;
1718				++ jindex;
1719
1720				//if (info.hatComp.size())
1721				//	returnInfo.PovHat = SJoystickInfo::POV_HAT_PRESENT;
1722				//else
1723				//	returnInfo.PovHat = SJoystickInfo::POV_HAT_ABSENT;
1724
1725				joystickInfo.push_back(returnInfo);
1726			}
1727
1728		}
1729		else
1730		{
1731			continue;
1732		}
1733	}
1734	result = IOObjectRelease (hidIterator);
1735
1736	return true;
1737#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1738
1739	return false;
1740}
1741
1742void CIrrDeviceMacOSX::pollJoysticks()
1743{
1744#if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1745	if(0 == ActiveJoysticks.size())
1746		return;
1747
1748	u32 joystick;
1749	for (joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
1750	{
1751		if (ActiveJoysticks[joystick].removed)
1752			continue;
1753
1754		bool found = false;
1755		ActiveJoysticks[joystick].persistentData.JoystickEvent.Joystick = joystick;
1756
1757		if (ActiveJoysticks[joystick].interface)
1758		{
1759			for (u32 n = 0; n < ActiveJoysticks[joystick].axisComp.size(); n++)
1760			{
1761				IOReturn result = kIOReturnSuccess;
1762				IOHIDEventStruct hidEvent;
1763				hidEvent.value = 0;
1764				result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].axisComp[n].cookie, &hidEvent);
1765				if (kIOReturnSuccess == result)
1766				{
1767					const f32 min = -32768.0f;
1768					const f32 max = 32767.0f;
1769					const f32 deviceScale = max - min;
1770					const f32 readScale = (f32)ActiveJoysticks[joystick].axisComp[n].maxRead - (f32)ActiveJoysticks[joystick].axisComp[n].minRead;
1771
1772					if (hidEvent.value < ActiveJoysticks[joystick].axisComp[n].minRead)
1773						ActiveJoysticks[joystick].axisComp[n].minRead = hidEvent.value;
1774					if (hidEvent.value > ActiveJoysticks[joystick].axisComp[n].maxRead)
1775						ActiveJoysticks[joystick].axisComp[n].maxRead = hidEvent.value;
1776
1777					if (readScale != 0.0f)
1778						hidEvent.value = (int)(((f32)((f32)hidEvent.value - (f32)ActiveJoysticks[joystick].axisComp[n].minRead) * deviceScale / readScale) + min);
1779
1780					if (ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] != (s16)hidEvent.value)
1781						found = true;
1782					ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] = (s16)hidEvent.value;
1783				}
1784			}//axis check
1785
1786			for (u32 n = 0; n < ActiveJoysticks[joystick].buttonComp.size(); n++)
1787			{
1788				IOReturn result = kIOReturnSuccess;
1789				IOHIDEventStruct hidEvent;
1790				hidEvent.value = 0;
1791				result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].buttonComp[n].cookie, &hidEvent);
1792				if (kIOReturnSuccess == result)
1793				{
1794					u32 ButtonStates = 0;
1795
1796					if (hidEvent.value && !((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false) )
1797							found = true;
1798					else if (!hidEvent.value && ((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false))
1799							found = true;
1800
1801					if (hidEvent.value)
1802							ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates |= (1 << n);
1803					else
1804							ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates &= ~(1 << n);
1805				}
1806			}//button check
1807			//still ToDo..will be done soon :)
1808/*
1809			for (u32 n = 0; n < ActiveJoysticks[joystick].hatComp.size(); n++)
1810			{
1811				IOReturn result = kIOReturnSuccess;
1812				IOHIDEventStruct hidEvent;
1813				hidEvent.value = 0;
1814				result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].hatComp[n].cookie, &hidEvent);
1815				if (kIOReturnSuccess == result)
1816				{
1817					if (ActiveJoysticks[joystick].persistentData.JoystickEvent.POV != hidEvent.value)
1818						found = true;
1819					ActiveJoysticks[joystick].persistentData.JoystickEvent.POV = hidEvent.value;
1820				}
1821			}//hat check
1822*/
1823		}
1824
1825		if (found)
1826			postEventFromUser(ActiveJoysticks[joystick].persistentData);
1827	}
1828#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1829}
1830
1831video::IVideoModeList* CIrrDeviceMacOSX::getVideoModeList()
1832{
1833	if (!VideoModeList->getVideoModeCount())
1834	{
1835		CGDirectDisplayID display;
1836		display = CGMainDisplayID();
1837
1838#ifdef __MAC_10_6
1839		CFArrayRef Modes = CGDisplayCopyAllDisplayModes(display, NULL);
1840
1841		for(int i = 0; i < CFArrayGetCount(Modes); ++i)
1842		{
1843			CGDisplayModeRef CurrentMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(Modes, i);
1844
1845			u8 Depth = 0;
1846
1847			CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(CurrentMode);
1848
1849			if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1850				Depth = 32;
1851			else
1852			if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1853				Depth = 16;
1854			else
1855			if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1856				Depth = 8;
1857
1858			if(Depth)
1859			{
1860				unsigned int Width = CGDisplayModeGetWidth(CurrentMode);
1861				unsigned int Height = CGDisplayModeGetHeight(CurrentMode);
1862
1863				VideoModeList->addMode(core::dimension2d<u32>(Width, Height), Depth);
1864			}
1865		}
1866#else
1867		CFArrayRef availableModes = CGDisplayAvailableModes(display);
1868		unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes);
1869		for (u32 i= 0; i<numberOfAvailableModes; ++i)
1870		{
1871			// look at each mode in the available list
1872			CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i);
1873			long bitsPerPixel = GetDictionaryLong(mode, kCGDisplayBitsPerPixel);
1874			Boolean safeForHardware = GetDictionaryBoolean(mode, kCGDisplayModeIsSafeForHardware);
1875			Boolean stretched = GetDictionaryBoolean(mode, kCGDisplayModeIsStretched);
1876
1877			if (!safeForHardware)
1878				continue;
1879
1880			long width = GetDictionaryLong(mode, kCGDisplayWidth);
1881			long height = GetDictionaryLong(mode, kCGDisplayHeight);
1882			// long refresh = GetDictionaryLong((mode), kCGDisplayRefreshRate);
1883			VideoModeList.addMode(core::dimension2d<u32>(width, height),
1884				bitsPerPixel);
1885		}
1886#endif
1887	}
1888	return VideoModeList;
1889}
1890
1891} // end namespace
1892
1893#endif // _IRR_COMPILE_WITH_OSX_DEVICE_
1894
1895