1 // Copyright (C) 2005-2006 Etienne Petitjean
2 // Copyright (C) 2007-2012 Christian Stehno
3 // Copyright (C) 2013-2015 Patryk Nadrowski
4 // This file is part of the "Irrlicht Engine".
5 // For conditions of distribution and use, see copyright notice in Irrlicht.h
7 #include "IrrCompileConfig.h"
9 #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
11 #import <Cocoa/Cocoa.h>
14 #import <Carbon/Carbon.h>
17 #include "CIrrDeviceOSX.h"
19 #include "IEventReceiver.h"
23 #include "irrString.h"
26 #include <sys/utsname.h>
27 #include "COSOperator.h"
28 #include "CColorConverter.h"
35 #include "CNSOGLManager.h"
37 #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
39 #include <IOKit/IOKitLib.h>
40 #include <IOKit/IOCFPlugIn.h>
42 #include <IOKit/hidsystem/IOHIDUsageTables.h>
44 /* The header was moved here in Mac OS X 10.1 */
45 #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
47 #include <IOKit/hid/IOHIDLib.h>
48 #include <IOKit/hid/IOHIDKeys.h>
50 struct JoystickComponent
52 IOHIDElementCookie cookie; // unique value which identifies element, will NOT change
53 long min; // reported min value possible
54 long max; // reported max value possible
56 long minRead; //min read value
57 long maxRead; //max read value
59 JoystickComponent() : min(0), minRead(0), max(0), maxRead(0)
66 irr::core::array <JoystickComponent> axisComp;
67 irr::core::array <JoystickComponent> buttonComp;
68 irr::core::array <JoystickComponent> hatComp;
73 int numActiveJoysticks;
75 irr::SEvent persistentData;
77 IOHIDDeviceInterface ** interface;
79 char joystickName[256];
80 long usage; // usage page from IOUSBHID Parser.h which defines general usage
81 long usagePage; // usage within above page from IOUSBHID Parser.h which defines specific usage
83 JoystickInfo() : hats(0), axes(0), buttons(0), interface(0), removed(false), usage(0), usagePage(0), numActiveJoysticks(0)
86 memset(joystickName, '\0', 256);
91 persistentData.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
92 persistentData.JoystickEvent.POV = 65535;
93 persistentData.JoystickEvent.ButtonStates = 0;
96 irr::core::array<JoystickInfo> ActiveJoysticks;
98 //helper functions for init joystick
99 static IOReturn closeJoystickDevice (JoystickInfo* joyInfo)
101 IOReturn result = kIOReturnSuccess;
102 if (joyInfo && joyInfo->interface)
104 /* close the interface */
105 result = (*(joyInfo->interface))->close (joyInfo->interface);
106 if (kIOReturnNotOpen == result)
108 /* do nothing as device was not opened, thus can't be closed */
110 else if (kIOReturnSuccess != result)
111 irr::os::Printer::log("IOHIDDeviceInterface failed to close", irr::ELL_ERROR);
112 /* release the interface */
113 result = (*(joyInfo->interface))->Release (joyInfo->interface);
114 if (kIOReturnSuccess != result)
115 irr::os::Printer::log("IOHIDDeviceInterface failed to release", irr::ELL_ERROR);
116 joyInfo->interface = NULL;
121 static void addComponentInfo (CFTypeRef refElement, JoystickComponent *pComponent, int numActiveJoysticks)
126 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementCookieKey));
127 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
128 pComponent->cookie = (IOHIDElementCookie) number;
129 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMinKey));
130 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
131 pComponent->minRead = pComponent->min = number;
132 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMaxKey));
133 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
134 pComponent->maxRead = pComponent->max = number;
137 static void getJoystickComponentArrayHandler (const void * value, void * parameter);
139 static void addJoystickComponent (CFTypeRef refElement, JoystickInfo* joyInfo)
141 long elementType, usagePage, usage;
142 CFTypeRef refElementType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementTypeKey));
143 CFTypeRef refUsagePage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsagePageKey));
144 CFTypeRef refUsage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsageKey));
146 if ((refElementType) && (CFNumberGetValue ((CFNumberRef)refElementType, kCFNumberLongType, &elementType)))
148 /* look at types of interest */
149 if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) ||
150 (elementType == kIOHIDElementTypeInput_Axis))
152 if (refUsagePage && CFNumberGetValue ((CFNumberRef)refUsagePage, kCFNumberLongType, &usagePage) &&
153 refUsage && CFNumberGetValue ((CFNumberRef)refUsage, kCFNumberLongType, &usage))
155 switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
157 case kHIDPage_GenericDesktop:
159 switch (usage) /* look at usage to determine function */
164 case kHIDUsage_GD_Rx:
165 case kHIDUsage_GD_Ry:
166 case kHIDUsage_GD_Rz:
167 case kHIDUsage_GD_Slider:
168 case kHIDUsage_GD_Dial:
169 case kHIDUsage_GD_Wheel:
172 JoystickComponent newComponent;
173 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
174 joyInfo->axisComp.push_back(newComponent);
177 case kHIDUsage_GD_Hatswitch:
180 JoystickComponent newComponent;
181 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
182 joyInfo->hatComp.push_back(newComponent);
188 case kHIDPage_Button:
191 JoystickComponent newComponent;
192 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
193 joyInfo->buttonComp.push_back(newComponent);
201 else if (kIOHIDElementTypeCollection == elementType)
204 CFTypeRef refElementTop = CFDictionaryGetValue ((CFMutableDictionaryRef) refElement, CFSTR(kIOHIDElementKey));
207 CFTypeID type = CFGetTypeID (refElementTop);
208 if (type == CFArrayGetTypeID())
210 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
211 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, joyInfo);
218 static void getJoystickComponentArrayHandler (const void * value, void * parameter)
220 if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
221 addJoystickComponent ((CFTypeRef) value, (JoystickInfo *) parameter);
224 static void joystickTopLevelElementHandler (const void * value, void * parameter)
227 if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
229 refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsagePageKey));
230 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usagePage))
231 irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usagePage", irr::ELL_ERROR);
232 refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsageKey));
233 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usage))
234 irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usage", irr::ELL_ERROR);
237 static void getJoystickDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, JoystickInfo *joyInfo)
239 CFMutableDictionaryRef usbProperties = 0;
240 io_registry_entry_t parent1, parent2;
242 /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
243 * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
245 if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
246 (KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
247 (KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
253 * try hid dictionary first, if fail then go to usb dictionary
256 /* get joystickName name */
257 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
259 refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
262 if (!CFStringGetCString ((CFStringRef)refCF, joyInfo->joystickName, 256, CFStringGetSystemEncoding ()))
263 irr::os::Printer::log("CFStringGetCString error getting joyInfo->joystickName", irr::ELL_ERROR);
266 /* get usage page and usage */
267 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
270 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usagePage))
271 irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usagePage", irr::ELL_ERROR);
272 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
274 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usage))
275 irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usage", irr::ELL_ERROR);
278 if (NULL == refCF) /* get top level element HID usage page or usage */
280 /* use top level element instead */
281 CFTypeRef refCFTopElement = 0;
282 refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
284 /* refCFTopElement points to an array of element dictionaries */
285 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refCFTopElement)};
286 CFArrayApplyFunction ((CFArrayRef)refCFTopElement, range, joystickTopLevelElementHandler, joyInfo);
290 CFRelease (usbProperties);
293 irr::os::Printer::log("IORegistryEntryCreateCFProperties failed to create usbProperties", irr::ELL_ERROR);
295 if (kIOReturnSuccess != IOObjectRelease (parent2))
296 irr::os::Printer::log("IOObjectRelease failed to release parent2", irr::ELL_ERROR);
297 if (kIOReturnSuccess != IOObjectRelease (parent1))
298 irr::os::Printer::log("IOObjectRelease failed to release parent1", irr::ELL_ERROR);
302 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
304 // only OSX 10.5 seems to not need these defines...
305 #if !defined(__MAC_10_5) || defined(__MAC_10_6)
306 // Contents from Events.h from Carbon/HIToolbox but we need it with Cocoa too
307 // and for some reason no Cocoa equivalent of these constants seems provided.
308 // So I'm doing like everyone else and using copy-and-paste.
315 * These constants are the virtual keycodes defined originally in
316 * Inside Mac Volume V, pg. V-191. They identify physical keys on a
317 * keyboard. Those constants with "ANSI" in the name are labeled
318 * according to the key position on an ANSI-standard US keyboard.
319 * For example, kVK_ANSI_A indicates the virtual keycode for the key
320 * with the letter 'A' in the US keyboard layout. Other keyboard
321 * layouts may have the 'A' key label on a different physical key;
322 * in this case, pressing 'A' will generate a different virtual
349 kVK_ANSI_Equal = 0x18,
352 kVK_ANSI_Minus = 0x1B,
355 kVK_ANSI_RightBracket = 0x1E,
358 kVK_ANSI_LeftBracket = 0x21,
363 kVK_ANSI_Quote = 0x27,
365 kVK_ANSI_Semicolon = 0x29,
366 kVK_ANSI_Backslash = 0x2A,
367 kVK_ANSI_Comma = 0x2B,
368 kVK_ANSI_Slash = 0x2C,
371 kVK_ANSI_Period = 0x2F,
372 kVK_ANSI_Grave = 0x32,
373 kVK_ANSI_KeypadDecimal = 0x41,
374 kVK_ANSI_KeypadMultiply = 0x43,
375 kVK_ANSI_KeypadPlus = 0x45,
376 kVK_ANSI_KeypadClear = 0x47,
377 kVK_ANSI_KeypadDivide = 0x4B,
378 kVK_ANSI_KeypadEnter = 0x4C,
379 kVK_ANSI_KeypadMinus = 0x4E,
380 kVK_ANSI_KeypadEquals = 0x51,
381 kVK_ANSI_Keypad0 = 0x52,
382 kVK_ANSI_Keypad1 = 0x53,
383 kVK_ANSI_Keypad2 = 0x54,
384 kVK_ANSI_Keypad3 = 0x55,
385 kVK_ANSI_Keypad4 = 0x56,
386 kVK_ANSI_Keypad5 = 0x57,
387 kVK_ANSI_Keypad6 = 0x58,
388 kVK_ANSI_Keypad7 = 0x59,
389 kVK_ANSI_Keypad8 = 0x5B,
390 kVK_ANSI_Keypad9 = 0x5C
393 /* keycodes for keys that are independent of keyboard layout*/
405 kVK_RightShift = 0x3C,
406 kVK_RightOption = 0x3D,
407 kVK_RightControl = 0x3E,
411 kVK_VolumeDown = 0x49,
432 kVK_ForwardDelete = 0x75,
438 kVK_LeftArrow = 0x7B,
439 kVK_RightArrow = 0x7C,
440 kVK_DownArrow = 0x7D,
445 //------------------------------------------------------------------------------------------
446 Boolean GetDictionaryBoolean(CFDictionaryRef theDict, const void* key)
448 // get a boolean from the dictionary
449 Boolean value = false;
450 CFBooleanRef boolRef;
451 boolRef = (CFBooleanRef)CFDictionaryGetValue(theDict, key);
453 value = CFBooleanGetValue(boolRef);
456 //------------------------------------------------------------------------------------------
457 long GetDictionaryLong(CFDictionaryRef theDict, const void* key)
459 // get a long from the dictionary
462 numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
464 CFNumberGetValue(numRef, kCFNumberLongType, &value);
472 IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& param, io::IFileSystem* io, IContextManager* contextManager);
474 } // end namespace irr
476 static bool firstLaunch = true;
478 @implementation CIrrDelegateOSX
480 irr::CIrrDeviceMacOSX* Device;
484 - (id)initWithDevice:(irr::CIrrDeviceMacOSX*)device
496 - (void)applicationDidFinishLaunching:(NSNotification*)notification
501 - (void)orderFrontStandardAboutPanel:(id)sender
503 [NSApp orderFrontStandardAboutPanel:sender];
506 - (void)unhideAllApplications:(id)sender
508 [NSApp unhideAllApplications:sender];
511 - (void)hide:(id)sender
516 - (void)hideOtherApplications:(id)sender
518 [NSApp hideOtherApplications:sender];
521 - (void)terminate:(id)sender
526 - (void)windowWillClose:(id)sender
528 Device->setWindow(nil);
532 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
534 if (Device->isResizable())
535 return proposedFrameSize;
537 return [window frame].size;
540 - (void)windowDidResize:(NSNotification *)aNotification
545 window = [aNotification object];
546 frame = [window frame];
547 Device->setResize((int)frame.size.width,(int)frame.size.height);
560 CIrrDeviceMacOSX::CIrrDeviceMacOSX(const SIrrlichtCreationParameters& param)
561 : CIrrDeviceStub(param), Window(NULL), Display(NULL),
562 SoftwareDriverTarget(0), DeviceWidth(0), DeviceHeight(0),
563 ScreenWidth(0), ScreenHeight(0), MouseButtonStates(0), SoftwareRendererType(0),
564 IsActive(true), IsFullscreen(false), IsShiftDown(false), IsControlDown(false), IsResizable(false)
570 setDebugName("CIrrDeviceMacOSX");
577 if (!CreationParams.WindowId)
579 [[NSAutoreleasePool alloc] init];
580 [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
581 [NSApp setDelegate:(id<NSApplicationDelegate>)[[[CIrrDelegateOSX alloc] initWithDevice:this] autorelease]];
585 NSString* bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
587 NSMenu* mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease];
588 NSMenu* menu = [[[NSMenu alloc] initWithTitle:bundleName] autorelease];
589 NSMenuItem* menuItem = [mainMenu addItemWithTitle:bundleName action:nil keyEquivalent:@""];
590 [mainMenu setSubmenu:menu forItem:menuItem];
591 menuItem = [menu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
592 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
594 [NSApp setMainMenu:mainMenu];
596 [NSApp finishLaunching];
599 path = [[NSBundle mainBundle] bundlePath];
600 path = [path stringByAppendingString:@"/Contents/Resources"];
601 chdir([path fileSystemRepresentation]);
606 Operator = new COSOperator(name.version);
607 os::Printer::log(name.version,ELL_INFORMATION);
611 VideoModeList->setDesktop(CreationParams.Bits, core::dimension2d<u32>([[NSScreen mainScreen] frame].size.width, [[NSScreen mainScreen] frame].size.height));
615 if (CreationParams.DriverType != video::EDT_NULL)
616 success = createWindow();
618 // in case of failure, one can check VideoDriver for initialization
623 CursorControl = new CCursorControl(CreationParams.WindowSize, this);
629 CIrrDeviceMacOSX::~CIrrDeviceMacOSX()
631 [SoftwareDriverTarget release];
633 [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
635 SetSystemUIMode(kUIModeNormal, kUIOptionAutoShowMenuBar);
638 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
639 for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
641 if (ActiveJoysticks[joystick].interface)
642 closeJoystickDevice(&ActiveJoysticks[joystick]);
647 void CIrrDeviceMacOSX::closeDevice()
651 [Window setIsVisible:FALSE];
652 [Window setReleasedWhenClosed:TRUE];
658 CGReleaseAllDisplays();
660 IsFullscreen = false;
664 bool CIrrDeviceMacOSX::createWindow()
668 Display = CGMainDisplayID();
672 CGDisplayModeRef displaymode, olddisplaymode;
674 CFDictionaryRef displaymode, olddisplaymode;
677 ScreenWidth = (int)CGDisplayPixelsWide(Display);
678 ScreenHeight = (int)CGDisplayPixelsHigh(Display);
680 const NSBackingStoreType type = (CreationParams.DriverType == video::EDT_OPENGL) ? NSBackingStoreBuffered : NSBackingStoreNonretained;
682 if (!CreationParams.Fullscreen)
684 if (!CreationParams.WindowId) //create another window when WindowId is null
686 int x = (CreationParams.WindowPosition.X > 0) ? CreationParams.WindowPosition.X : 0;
687 int y = (CreationParams.WindowPosition.Y > 0) ? CreationParams.WindowPosition.Y : 0;
689 if (CreationParams.WindowPosition.Y > -1)
691 int screenHeight = [[[NSScreen screens] objectAtIndex:0] frame].size.height;
692 y = screenHeight - y - CreationParams.WindowSize.Height;
695 Window = [[NSWindow alloc] initWithContentRect:NSMakeRect(x, y, CreationParams.WindowSize.Width,CreationParams.WindowSize.Height) styleMask:NSTitledWindowMask+NSClosableWindowMask+NSResizableWindowMask backing:type defer:FALSE];
697 if (CreationParams.WindowPosition.X == -1 && CreationParams.WindowPosition.Y == -1)
701 DeviceWidth = CreationParams.WindowSize.Width;
702 DeviceHeight = CreationParams.WindowSize.Height;
711 displaymode = CGDisplayCopyDisplayMode(Display);
713 CFArrayRef Modes = CGDisplayCopyAllDisplayModes(Display, NULL);
715 for(int i = 0; i < CFArrayGetCount(Modes); ++i)
717 CGDisplayModeRef CurrentMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(Modes, i);
721 CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(CurrentMode);
723 if (CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
725 else if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
727 else if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
730 if(Depth == CreationParams.Bits)
731 if((CGDisplayModeGetWidth(CurrentMode) == CreationParams.WindowSize.Width) && (CGDisplayModeGetHeight(CurrentMode) == CreationParams.WindowSize.Height))
733 displaymode = CurrentMode;
738 displaymode = CGDisplayBestModeForParameters(Display,CreationParams.Bits,CreationParams.WindowSize.Width,CreationParams.WindowSize.Height,NULL);
741 if (displaymode != NULL)
744 olddisplaymode = CGDisplayCopyDisplayMode(Display);
746 olddisplaymode = CGDisplayCurrentMode(Display);
749 error = CGCaptureAllDisplays();
750 if (error == CGDisplayNoErr)
753 error = CGDisplaySetDisplayMode(Display, displaymode, NULL);
755 error = CGDisplaySwitchToMode(Display, displaymode);
758 if (error == CGDisplayNoErr)
760 Window = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:type defer:FALSE screen:[NSScreen mainScreen]];
762 [Window setLevel: CGShieldingWindowLevel()];
763 [Window setBackgroundColor:[NSColor blackColor]];
765 displayRect = CGDisplayBounds(Display);
766 ScreenWidth = DeviceWidth = (int)displayRect.size.width;
767 ScreenHeight = DeviceHeight = (int)displayRect.size.height;
768 CreationParams.WindowSize.set(ScreenWidth, ScreenHeight);
774 CGReleaseAllDisplays();
783 [Window setDelegate:(id<NSWindowDelegate>)[NSApp delegate]];
784 [Window setAcceptsMouseMovedEvents:TRUE];
785 [Window setIsVisible:TRUE];
786 [Window makeKeyAndOrderFront:nil];
789 if (IsFullscreen) //hide menus in fullscreen mode only
792 [NSApp setPresentationOptions:(NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
794 SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar);
802 void CIrrDeviceMacOSX::setResize(int width, int height)
804 // set new window size
806 DeviceHeight = height;
808 #if defined(_IRR_COMPILE_WITH_OPENGL_)
809 // update the size of the opengl rendering context
810 if (CreationParams.DriverType == video::EDT_OPENGL)
812 NSOpenGLContext* Context = (NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context;
819 // resize the driver to the inner pane size
822 NSRect driverFrame = [Window contentRectForFrameRect:[Window frame]];
823 getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)driverFrame.size.width, (s32)driverFrame.size.height));
826 getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)width, (s32)height));
830 void CIrrDeviceMacOSX::createDriver()
832 switch (CreationParams.DriverType)
834 case video::EDT_SOFTWARE:
835 #ifdef _IRR_COMPILE_WITH_SOFTWARE_
836 VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
837 SoftwareRendererType = 2;
839 os::Printer::log("No Software driver support compiled in.", ELL_ERROR);
843 case video::EDT_BURNINGSVIDEO:
844 #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
845 VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);
846 SoftwareRendererType = 1;
848 os::Printer::log("Burning's video driver was not compiled in.", ELL_ERROR);
852 case video::EDT_OPENGL:
853 #ifdef _IRR_COMPILE_WITH_OPENGL_
855 video::SExposedVideoData data;
856 data.OpenGLOSX.Window = Window;
857 ContextManager = new video::CNSOGLManager();
858 ContextManager->initialize(CreationParams, data);
859 VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, ContextManager);
862 os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);
867 [[Window contentView] setWantsBestResolutionOpenGLSurface:NO];
868 [(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context setView:[Window contentView]];
872 [(NSView*)CreationParams.WindowId setWantsBestResolutionOpenGLSurface:NO];
873 [(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context setView:(NSView*)CreationParams.WindowId];
877 CGLContextObj CGLContext = (CGLContextObj)[(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context CGLContextObj];
878 CGLSetFullScreen(CGLContext);
882 os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
886 case video::DEPRECATED_EDT_DIRECT3D8_NO_LONGER_EXISTS:
887 case video::EDT_DIRECT3D9:
888 case video::EDT_OGLES1:
889 case video::EDT_OGLES2:
890 os::Printer::log("This driver is not available in OSX. Try OpenGL or Software renderer.", ELL_ERROR);
893 case video::EDT_NULL:
894 VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
898 os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
903 bool CIrrDeviceMacOSX::run()
905 NSAutoreleasePool* Pool = [[NSAutoreleasePool alloc] init];
911 storeMouseLocation();
913 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
916 bzero(&ievent,sizeof(ievent));
918 switch([(NSEvent *)event type])
921 postKeyEvent(event,ievent,true);
925 postKeyEvent(event,ievent,false);
929 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
930 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
931 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
933 if (IsShiftDown != ievent.KeyInput.Shift)
935 ievent.KeyInput.Char = irr::KEY_SHIFT;
936 ievent.KeyInput.Key = irr::KEY_SHIFT;
937 ievent.KeyInput.PressedDown = ievent.KeyInput.Shift;
939 IsShiftDown = ievent.KeyInput.Shift;
941 postEventFromUser(ievent);
944 if (IsControlDown != ievent.KeyInput.Control)
946 ievent.KeyInput.Char = irr::KEY_CONTROL;
947 ievent.KeyInput.Key = irr::KEY_CONTROL;
948 ievent.KeyInput.PressedDown = ievent.KeyInput.Control;
950 IsControlDown = ievent.KeyInput.Control;
952 postEventFromUser(ievent);
955 [NSApp sendEvent:event];
958 case NSLeftMouseDown:
959 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
960 ievent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
961 MouseButtonStates |= irr::EMBSM_LEFT;
962 ievent.MouseInput.ButtonStates = MouseButtonStates;
963 postMouseEvent(event,ievent);
967 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
968 MouseButtonStates &= !irr::EMBSM_LEFT;
969 ievent.MouseInput.ButtonStates = MouseButtonStates;
970 ievent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
971 postMouseEvent(event,ievent);
974 case NSOtherMouseDown:
975 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
976 ievent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
977 MouseButtonStates |= irr::EMBSM_MIDDLE;
978 ievent.MouseInput.ButtonStates = MouseButtonStates;
979 postMouseEvent(event,ievent);
983 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
984 MouseButtonStates &= !irr::EMBSM_MIDDLE;
985 ievent.MouseInput.ButtonStates = MouseButtonStates;
986 ievent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
987 postMouseEvent(event,ievent);
991 case NSLeftMouseDragged:
992 case NSRightMouseDragged:
993 case NSOtherMouseDragged:
994 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
995 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
996 ievent.MouseInput.ButtonStates = MouseButtonStates;
997 postMouseEvent(event,ievent);
1000 case NSRightMouseDown:
1001 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1002 ievent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
1003 MouseButtonStates |= irr::EMBSM_RIGHT;
1004 ievent.MouseInput.ButtonStates = MouseButtonStates;
1005 postMouseEvent(event,ievent);
1008 case NSRightMouseUp:
1009 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1010 ievent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
1011 MouseButtonStates &= !irr::EMBSM_RIGHT;
1012 ievent.MouseInput.ButtonStates = MouseButtonStates;
1013 postMouseEvent(event,ievent);
1017 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1018 ievent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
1019 ievent.MouseInput.Wheel = [(NSEvent *)event deltaY];
1020 if (ievent.MouseInput.Wheel < 1.0f)
1021 ievent.MouseInput.Wheel *= 10.0f;
1023 ievent.MouseInput.Wheel *= 5.0f;
1024 postMouseEvent(event,ievent);
1028 [NSApp sendEvent:event];
1037 return (![[NSApp delegate] isQuit] && IsActive);
1041 //! Pause the current process for the minimum time allowed only to allow other processes to execute
1042 void CIrrDeviceMacOSX::yield()
1044 struct timespec ts = {0,0};
1045 nanosleep(&ts, NULL);
1049 //! Pause execution and let other processes to run for a specified amount of time.
1050 void CIrrDeviceMacOSX::sleep(u32 timeMs, bool pauseTimer=false)
1052 bool wasStopped = Timer ? Timer->isStopped() : true;
1055 ts.tv_sec = (time_t) (timeMs / 1000);
1056 ts.tv_nsec = (long) (timeMs % 1000) * 1000000;
1058 if (pauseTimer && !wasStopped)
1061 nanosleep(&ts, NULL);
1063 if (pauseTimer && !wasStopped)
1068 void CIrrDeviceMacOSX::setWindowCaption(const wchar_t* text)
1074 size_t numBytes = wcslen(text) * sizeof(wchar_t);
1076 #ifdef __BIG_ENDIAN__
1077 NSStringEncoding encode = sizeof(wchar_t) == 4 ? NSUTF32BigEndianStringEncoding : NSUTF16BigEndianStringEncoding;
1079 NSStringEncoding encode = sizeof(wchar_t) == 4 ? NSUTF32LittleEndianStringEncoding : NSUTF16LittleEndianStringEncoding;
1081 NSString* name = [[NSString alloc] initWithBytes:text length:numBytes encoding:encode];
1084 [Window setTitle:name];
1090 [Window setTitle:@""];
1096 bool CIrrDeviceMacOSX::isWindowActive() const
1102 bool CIrrDeviceMacOSX::isWindowFocused() const
1105 return [Window isKeyWindow];
1110 bool CIrrDeviceMacOSX::isWindowMinimized() const
1113 return [Window isMiniaturized];
1118 void CIrrDeviceMacOSX::postKeyEvent(void *event,irr::SEvent &ievent,bool pressed)
1121 std::map<int,int>::const_iterator iter;
1122 unsigned int c,mkey,mchar;
1123 const unsigned char *cStr;
1126 str = [(NSEvent *)event characters];
1127 if ((str != nil) && ([str length] > 0))
1130 skipCommand = false;
1131 c = [str characterAtIndex:0];
1134 iter = KeyCodes.find([(NSEvent *)event keyCode]);
1135 if (iter != KeyCodes.end())
1136 mkey = (*iter).second;
1137 else if ((iter = KeyCodes.find(c)) != KeyCodes.end())
1138 mkey = (*iter).second;
1141 // workaround for period character
1144 mkey = irr::KEY_PERIOD;
1149 cStr = (unsigned char *)[str cStringUsingEncoding:NSWindowsCP1252StringEncoding];
1150 if (cStr != NULL && strlen((char*)cStr) > 0)
1153 mkey = toupper(mchar);
1154 if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1156 if (mkey == 'C' || mkey == 'V' || mkey == 'X')
1166 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
1167 ievent.KeyInput.Key = (irr::EKEY_CODE)mkey;
1168 ievent.KeyInput.PressedDown = pressed;
1169 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1170 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1171 ievent.KeyInput.Char = mchar;
1174 ievent.KeyInput.Control = true;
1175 else if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1176 [NSApp sendEvent:(NSEvent *)event];
1178 postEventFromUser(ievent);
1183 void CIrrDeviceMacOSX::postMouseEvent(void *event,irr::SEvent &ievent)
1189 ievent.MouseInput.X = (int)[(NSEvent *)event locationInWindow].x;
1190 ievent.MouseInput.Y = DeviceHeight - (int)[(NSEvent *)event locationInWindow].y;
1192 if (ievent.MouseInput.Y < 0)
1197 CGEventRef ourEvent = CGEventCreate(NULL);
1198 CGPoint point = CGEventGetLocation(ourEvent);
1199 CFRelease(ourEvent);
1201 ievent.MouseInput.X = (int)point.x;
1202 ievent.MouseInput.Y = (int)point.y;
1204 if (ievent.MouseInput.Y < 0)
1210 ievent.MouseInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1211 ievent.MouseInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1213 postEventFromUser(ievent);
1216 [NSApp sendEvent:(NSEvent *)event];
1220 void CIrrDeviceMacOSX::storeMouseLocation()
1227 p = [NSEvent mouseLocation];
1228 p = [Window convertScreenToBase:p];
1230 y = DeviceHeight - (int)p.y;
1234 CGEventRef ourEvent = CGEventCreate(NULL);
1235 CGPoint point = CGEventGetLocation(ourEvent);
1236 CFRelease(ourEvent);
1241 const core::position2di& curr = ((CCursorControl *)CursorControl)->getPosition(true);
1242 if (curr.X != x || curr.Y != y)
1244 // In fullscreen mode, events are not sent regularly so rely on polling
1246 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1247 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1248 ievent.MouseInput.X = x;
1249 ievent.MouseInput.Y = y;
1250 postEventFromUser(ievent);
1254 ((CCursorControl *)CursorControl)->updateInternalCursorPosition(x,y);
1258 void CIrrDeviceMacOSX::setMouseLocation(int x,int y)
1265 // Irrlicht window exists
1267 p.y = (float) (DeviceHeight - y);
1268 p = [Window convertBaseToScreen:p];
1269 p.y = ScreenHeight - p.y;
1274 p.y = (float) y + (ScreenHeight - DeviceHeight);
1281 CGEventRef ev = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, c, kCGMouseButtonLeft);
1282 CGEventPost(kCGHIDEventTap, ev);
1285 CGSetLocalEventsSuppressionInterval(0);
1286 CGWarpMouseCursorPosition(c);
1291 void CIrrDeviceMacOSX::setCursorVisible(bool visible)
1294 CGDisplayShowCursor(CGMainDisplayID());
1296 CGDisplayHideCursor(CGMainDisplayID());
1300 void CIrrDeviceMacOSX::setWindow(NSWindow* window)
1306 void CIrrDeviceMacOSX::initKeycodes()
1308 KeyCodes[kVK_UpArrow] = irr::KEY_UP;
1309 KeyCodes[kVK_DownArrow] = irr::KEY_DOWN;
1310 KeyCodes[kVK_LeftArrow] = irr::KEY_LEFT;
1311 KeyCodes[kVK_RightArrow] = irr::KEY_RIGHT;
1312 KeyCodes[kVK_F1] = irr::KEY_F1;
1313 KeyCodes[kVK_F2] = irr::KEY_F2;
1314 KeyCodes[kVK_F3] = irr::KEY_F3;
1315 KeyCodes[kVK_F4] = irr::KEY_F4;
1316 KeyCodes[kVK_F5] = irr::KEY_F5;
1317 KeyCodes[kVK_F6] = irr::KEY_F6;
1318 KeyCodes[kVK_F7] = irr::KEY_F7;
1319 KeyCodes[kVK_F8] = irr::KEY_F8;
1320 KeyCodes[kVK_F9] = irr::KEY_F9;
1321 KeyCodes[kVK_F10] = irr::KEY_F10;
1322 KeyCodes[kVK_F11] = irr::KEY_F11;
1323 KeyCodes[kVK_F12] = irr::KEY_F12;
1324 KeyCodes[kVK_F13] = irr::KEY_F13;
1325 KeyCodes[kVK_F14] = irr::KEY_F14;
1326 KeyCodes[kVK_F15] = irr::KEY_F15;
1327 KeyCodes[kVK_F16] = irr::KEY_F16;
1328 KeyCodes[kVK_F17] = irr::KEY_F17;
1329 KeyCodes[kVK_F18] = irr::KEY_F18;
1330 KeyCodes[kVK_F19] = irr::KEY_F19;
1331 KeyCodes[kVK_F20] = irr::KEY_F20;
1332 KeyCodes[kVK_Home] = irr::KEY_HOME;
1333 KeyCodes[kVK_End] = irr::KEY_END;
1334 KeyCodes[NSInsertFunctionKey] = irr::KEY_INSERT;
1335 KeyCodes[kVK_ForwardDelete] = irr::KEY_DELETE;
1336 KeyCodes[kVK_Help] = irr::KEY_HELP;
1337 KeyCodes[NSSelectFunctionKey] = irr::KEY_SELECT;
1338 KeyCodes[NSPrintFunctionKey] = irr::KEY_PRINT;
1339 KeyCodes[NSExecuteFunctionKey] = irr::KEY_EXECUT;
1340 KeyCodes[NSPrintScreenFunctionKey] = irr::KEY_SNAPSHOT;
1341 KeyCodes[NSPauseFunctionKey] = irr::KEY_PAUSE;
1342 KeyCodes[NSScrollLockFunctionKey] = irr::KEY_SCROLL;
1343 KeyCodes[kVK_Delete] = irr::KEY_BACK;
1344 KeyCodes[kVK_Tab] = irr::KEY_TAB;
1345 KeyCodes[kVK_Return] = irr::KEY_RETURN;
1346 KeyCodes[kVK_Escape] = irr::KEY_ESCAPE;
1347 KeyCodes[kVK_Control] = irr::KEY_CONTROL;
1348 KeyCodes[kVK_RightControl] = irr::KEY_RCONTROL;
1349 KeyCodes[kVK_Command] = irr::KEY_MENU;
1350 KeyCodes[kVK_Shift] = irr::KEY_SHIFT;
1351 KeyCodes[kVK_RightShift] = irr::KEY_RSHIFT;
1352 KeyCodes[kVK_Space] = irr::KEY_SPACE;
1354 KeyCodes[kVK_ANSI_A] = irr::KEY_KEY_A;
1355 KeyCodes[kVK_ANSI_B] = irr::KEY_KEY_B;
1356 KeyCodes[kVK_ANSI_C] = irr::KEY_KEY_C;
1357 KeyCodes[kVK_ANSI_D] = irr::KEY_KEY_D;
1358 KeyCodes[kVK_ANSI_E] = irr::KEY_KEY_E;
1359 KeyCodes[kVK_ANSI_F] = irr::KEY_KEY_F;
1360 KeyCodes[kVK_ANSI_G] = irr::KEY_KEY_G;
1361 KeyCodes[kVK_ANSI_H] = irr::KEY_KEY_H;
1362 KeyCodes[kVK_ANSI_I] = irr::KEY_KEY_I;
1363 KeyCodes[kVK_ANSI_J] = irr::KEY_KEY_J;
1364 KeyCodes[kVK_ANSI_K] = irr::KEY_KEY_K;
1365 KeyCodes[kVK_ANSI_L] = irr::KEY_KEY_L;
1366 KeyCodes[kVK_ANSI_M] = irr::KEY_KEY_M;
1367 KeyCodes[kVK_ANSI_N] = irr::KEY_KEY_N;
1368 KeyCodes[kVK_ANSI_O] = irr::KEY_KEY_O;
1369 KeyCodes[kVK_ANSI_P] = irr::KEY_KEY_P;
1370 KeyCodes[kVK_ANSI_Q] = irr::KEY_KEY_Q;
1371 KeyCodes[kVK_ANSI_R] = irr::KEY_KEY_R;
1372 KeyCodes[kVK_ANSI_S] = irr::KEY_KEY_S;
1373 KeyCodes[kVK_ANSI_T] = irr::KEY_KEY_T;
1374 KeyCodes[kVK_ANSI_U] = irr::KEY_KEY_U;
1375 KeyCodes[kVK_ANSI_V] = irr::KEY_KEY_V;
1376 KeyCodes[kVK_ANSI_W] = irr::KEY_KEY_W;
1377 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1378 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1379 KeyCodes[kVK_ANSI_Y] = irr::KEY_KEY_Y;
1380 KeyCodes[kVK_ANSI_Z] = irr::KEY_KEY_Z;
1382 KeyCodes[kVK_ANSI_0] = irr::KEY_KEY_0;
1383 KeyCodes[kVK_ANSI_1] = irr::KEY_KEY_1;
1384 KeyCodes[kVK_ANSI_2] = irr::KEY_KEY_2;
1385 KeyCodes[kVK_ANSI_3] = irr::KEY_KEY_3;
1386 KeyCodes[kVK_ANSI_4] = irr::KEY_KEY_4;
1387 KeyCodes[kVK_ANSI_5] = irr::KEY_KEY_5;
1388 KeyCodes[kVK_ANSI_6] = irr::KEY_KEY_6;
1389 KeyCodes[kVK_ANSI_7] = irr::KEY_KEY_7;
1390 KeyCodes[kVK_ANSI_8] = irr::KEY_KEY_8;
1391 KeyCodes[kVK_ANSI_9] = irr::KEY_KEY_9;
1393 KeyCodes[kVK_ANSI_Slash] = irr::KEY_DIVIDE;
1394 KeyCodes[kVK_ANSI_Comma] = irr::KEY_COMMA;
1395 KeyCodes[kVK_ANSI_Period] = irr::KEY_PERIOD;
1396 KeyCodes[kVK_PageUp] = irr::KEY_PRIOR;
1397 KeyCodes[kVK_PageDown] = irr::KEY_NEXT;
1399 KeyCodes[kVK_ANSI_Keypad0] = irr::KEY_NUMPAD0;
1400 KeyCodes[kVK_ANSI_Keypad1] = irr::KEY_NUMPAD1;
1401 KeyCodes[kVK_ANSI_Keypad2] = irr::KEY_NUMPAD2;
1402 KeyCodes[kVK_ANSI_Keypad3] = irr::KEY_NUMPAD3;
1403 KeyCodes[kVK_ANSI_Keypad4] = irr::KEY_NUMPAD4;
1404 KeyCodes[kVK_ANSI_Keypad5] = irr::KEY_NUMPAD5;
1405 KeyCodes[kVK_ANSI_Keypad6] = irr::KEY_NUMPAD6;
1406 KeyCodes[kVK_ANSI_Keypad7] = irr::KEY_NUMPAD7;
1407 KeyCodes[kVK_ANSI_Keypad8] = irr::KEY_NUMPAD8;
1408 KeyCodes[kVK_ANSI_Keypad9] = irr::KEY_NUMPAD9;
1410 KeyCodes[kVK_ANSI_KeypadDecimal] = irr::KEY_DECIMAL;
1411 KeyCodes[kVK_ANSI_KeypadMultiply] = irr::KEY_MULTIPLY;
1412 KeyCodes[kVK_ANSI_KeypadPlus] = irr::KEY_PLUS;
1413 KeyCodes[kVK_ANSI_KeypadClear] = irr::KEY_OEM_CLEAR;
1414 KeyCodes[kVK_ANSI_KeypadDivide] = irr::KEY_DIVIDE;
1415 KeyCodes[kVK_ANSI_KeypadEnter] = irr::KEY_RETURN;
1416 KeyCodes[kVK_ANSI_KeypadMinus] = irr::KEY_SUBTRACT;
1418 KeyCodes[kVK_ANSI_LeftBracket] = irr::KEY_OEM_4;
1419 KeyCodes[kVK_ANSI_Backslash] = irr::KEY_OEM_5;
1420 KeyCodes[kVK_ANSI_RightBracket] = irr::KEY_OEM_6;
1424 //! Sets if the window should be resizable in windowed mode.
1425 void CIrrDeviceMacOSX::setResizable(bool resize)
1427 IsResizable = resize;
1430 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask];
1432 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask];
1437 bool CIrrDeviceMacOSX::isResizable() const
1443 void CIrrDeviceMacOSX::minimizeWindow()
1446 [Window miniaturize:[NSApp self]];
1450 //! Maximizes the window if possible.
1451 void CIrrDeviceMacOSX::maximizeWindow()
1457 //! get the window to normal size if possible.
1458 void CIrrDeviceMacOSX::restoreWindow()
1460 [Window deminiaturize:[NSApp self]];
1463 //! Get the position of this window on screen
1464 core::position2di CIrrDeviceMacOSX::getWindowPosition()
1466 NSRect rect = [Window frame];
1467 int screenHeight = [[[NSScreen screens] objectAtIndex:0] frame].size.height;
1468 return core::position2di(rect.origin.x, screenHeight - rect.origin.y - rect.size.height);
1473 bool CIrrDeviceMacOSX::present(video::IImage* surface, void* windowId, core::rect<s32>* src )
1475 // todo: implement window ID and src rectangle
1480 if (SoftwareRendererType > 0)
1482 const u32 colorSamples=3;
1483 // do we need to change the size?
1484 const bool updateSize = !SoftwareDriverTarget ||
1485 s32([SoftwareDriverTarget size].width) != surface->getDimension().Width ||
1486 s32([SoftwareDriverTarget size].height) != surface->getDimension().Height;
1488 NSRect areaRect = NSMakeRect(0.0, 0.0, surface->getDimension().Width, surface->getDimension().Height);
1489 const u32 destPitch = (colorSamples * areaRect.size.width);
1491 // create / update the target
1494 [SoftwareDriverTarget release];
1495 // allocate target for IImage
1496 SoftwareDriverTarget = [[NSBitmapImageRep alloc]
1497 initWithBitmapDataPlanes: nil
1498 pixelsWide: areaRect.size.width
1499 pixelsHigh: areaRect.size.height
1501 samplesPerPixel: colorSamples
1504 colorSpaceName: NSCalibratedRGBColorSpace
1505 bytesPerRow: destPitch
1506 bitsPerPixel: 8*colorSamples];
1509 if (SoftwareDriverTarget==nil)
1512 // get pointer to image data
1513 unsigned char* imgData = (unsigned char*)surface->getData();
1515 u8* srcdata = reinterpret_cast<u8*>(imgData);
1516 u8* destData = reinterpret_cast<u8*>([SoftwareDriverTarget bitmapData]);
1517 const u32 srcheight = core::min_(surface->getDimension().Height, (u32)areaRect.size.height);
1518 const u32 srcPitch = surface->getPitch();
1519 const u32 minWidth = core::min_(surface->getDimension().Width, (u32)areaRect.size.width);
1520 for (u32 y=0; y!=srcheight; ++y)
1522 if(SoftwareRendererType == 2)
1524 if (surface->getColorFormat() == video::ECF_A8R8G8B8)
1525 video::CColorConverter::convert_A8R8G8B8toB8G8R8(srcdata, minWidth, destData);
1526 else if (surface->getColorFormat() == video::ECF_A1R5G5B5)
1527 video::CColorConverter::convert_A1R5G5B5toB8G8R8(srcdata, minWidth, destData);
1529 video::CColorConverter::convert_viaFormat(srcdata, surface->getColorFormat(), minWidth, destData, video::ECF_R8G8B8);
1533 if (surface->getColorFormat() == video::ECF_A8R8G8B8)
1534 video::CColorConverter::convert_A8R8G8B8toR8G8B8(srcdata, minWidth, destData);
1535 else if (surface->getColorFormat() == video::ECF_A1R5G5B5)
1536 video::CColorConverter::convert_A1R5G5B5toR8G8B8(srcdata, minWidth, destData);
1538 video::CColorConverter::convert_viaFormat(srcdata, surface->getColorFormat(), minWidth, destData, video::ECF_R8G8B8);
1541 srcdata += srcPitch;
1542 destData += destPitch;
1545 // todo: draw properly into a sub-view
1546 [SoftwareDriverTarget draw];
1553 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1554 static void joystickRemovalCallback(void * target,
1555 IOReturn result, void * refcon, void * sender)
1557 JoystickInfo *joy = (JoystickInfo *) refcon;
1560 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1563 bool CIrrDeviceMacOSX::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
1565 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1566 ActiveJoysticks.clear();
1567 joystickInfo.clear();
1569 io_object_t hidObject = 0;
1570 io_iterator_t hidIterator = 0;
1571 IOReturn result = kIOReturnSuccess;
1572 mach_port_t masterPort = 0;
1573 CFMutableDictionaryRef hidDictionaryRef = NULL;
1575 result = IOMasterPort (bootstrap_port, &masterPort);
1576 if (kIOReturnSuccess != result)
1578 os::Printer::log("initialiseJoysticks IOMasterPort failed", ELL_ERROR);
1582 hidDictionaryRef = IOServiceMatching (kIOHIDDeviceKey);
1583 if (!hidDictionaryRef)
1585 os::Printer::log("initialiseJoysticks IOServiceMatching failed", ELL_ERROR);
1588 result = IOServiceGetMatchingServices (masterPort, hidDictionaryRef, &hidIterator);
1590 if (kIOReturnSuccess != result)
1592 os::Printer::log("initialiseJoysticks IOServiceGetMatchingServices failed", ELL_ERROR);
1596 //no joysticks just return
1601 while ((hidObject = IOIteratorNext (hidIterator)))
1605 // get dictionary for HID properties
1606 CFMutableDictionaryRef hidProperties = 0;
1608 kern_return_t kern_result = IORegistryEntryCreateCFProperties (hidObject, &hidProperties, kCFAllocatorDefault, kNilOptions);
1609 if ((kern_result == KERN_SUCCESS) && hidProperties)
1611 HRESULT plugInResult = S_OK;
1613 IOCFPlugInInterface ** ppPlugInInterface = NULL;
1614 result = IOCreatePlugInInterfaceForService (hidObject, kIOHIDDeviceUserClientTypeID,
1615 kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
1616 if (kIOReturnSuccess == result)
1618 plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
1619 CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void **) &(info.interface));
1620 if (plugInResult != S_OK)
1621 os::Printer::log("initialiseJoysticks query HID class device interface failed", ELL_ERROR);
1622 (*ppPlugInInterface)->Release(ppPlugInInterface);
1627 if (info.interface != NULL)
1629 result = (*(info.interface))->open (info.interface, 0);
1630 if (result == kIOReturnSuccess)
1632 (*(info.interface))->setRemovalCallback (info.interface, joystickRemovalCallback, &info, &info);
1633 getJoystickDeviceInfo(hidObject, hidProperties, &info);
1636 CFTypeRef refElementTop = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
1639 CFTypeID type = CFGetTypeID (refElementTop);
1640 if (type == CFArrayGetTypeID())
1642 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
1643 info.numActiveJoysticks = ActiveJoysticks.size();
1644 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, &info);
1650 CFRelease (hidProperties);
1651 os::Printer::log("initialiseJoysticks Open interface failed", ELL_ERROR);
1655 CFRelease (hidProperties);
1657 result = IOObjectRelease (hidObject);
1659 if ( (info.usagePage != kHIDPage_GenericDesktop) ||
1660 ((info.usage != kHIDUsage_GD_Joystick &&
1661 info.usage != kHIDUsage_GD_GamePad &&
1662 info.usage != kHIDUsage_GD_MultiAxisController)) )
1664 closeJoystickDevice (&info);
1668 for (u32 i = 0; i < 6; ++i)
1669 info.persistentData.JoystickEvent.Axis[i] = 0;
1671 ActiveJoysticks.push_back(info);
1673 SJoystickInfo returnInfo;
1674 returnInfo.Joystick = jindex;
1675 returnInfo.Axes = info.axes;
1676 //returnInfo.Hats = info.hats;
1677 returnInfo.Buttons = info.buttons;
1678 returnInfo.Name = info.joystickName;
1679 returnInfo.PovHat = SJoystickInfo::POV_HAT_UNKNOWN;
1682 //if (info.hatComp.size())
1683 // returnInfo.PovHat = SJoystickInfo::POV_HAT_PRESENT;
1685 // returnInfo.PovHat = SJoystickInfo::POV_HAT_ABSENT;
1687 joystickInfo.push_back(returnInfo);
1696 result = IOObjectRelease (hidIterator);
1699 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1704 void CIrrDeviceMacOSX::pollJoysticks()
1706 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1707 if(0 == ActiveJoysticks.size())
1711 for (joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
1713 if (ActiveJoysticks[joystick].removed)
1717 ActiveJoysticks[joystick].persistentData.JoystickEvent.Joystick = joystick;
1719 if (ActiveJoysticks[joystick].interface)
1721 for (u32 n = 0; n < ActiveJoysticks[joystick].axisComp.size(); n++)
1723 IOReturn result = kIOReturnSuccess;
1724 IOHIDEventStruct hidEvent;
1726 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].axisComp[n].cookie, &hidEvent);
1727 if (kIOReturnSuccess == result)
1729 const f32 min = -32768.0f;
1730 const f32 max = 32767.0f;
1731 const f32 deviceScale = max - min;
1732 const f32 readScale = (f32)ActiveJoysticks[joystick].axisComp[n].maxRead - (f32)ActiveJoysticks[joystick].axisComp[n].minRead;
1734 if (hidEvent.value < ActiveJoysticks[joystick].axisComp[n].minRead)
1735 ActiveJoysticks[joystick].axisComp[n].minRead = hidEvent.value;
1736 if (hidEvent.value > ActiveJoysticks[joystick].axisComp[n].maxRead)
1737 ActiveJoysticks[joystick].axisComp[n].maxRead = hidEvent.value;
1739 if (readScale != 0.0f)
1740 hidEvent.value = (int)(((f32)((f32)hidEvent.value - (f32)ActiveJoysticks[joystick].axisComp[n].minRead) * deviceScale / readScale) + min);
1742 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] != (s16)hidEvent.value)
1744 ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] = (s16)hidEvent.value;
1748 for (u32 n = 0; n < ActiveJoysticks[joystick].buttonComp.size(); n++)
1750 IOReturn result = kIOReturnSuccess;
1751 IOHIDEventStruct hidEvent;
1753 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].buttonComp[n].cookie, &hidEvent);
1754 if (kIOReturnSuccess == result)
1756 if (hidEvent.value && !((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false) )
1758 else if (!hidEvent.value && ((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false))
1762 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates |= (1 << n);
1764 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates &= ~(1 << n);
1767 //still ToDo..will be done soon :)
1769 for (u32 n = 0; n < ActiveJoysticks[joystick].hatComp.size(); n++)
1771 IOReturn result = kIOReturnSuccess;
1772 IOHIDEventStruct hidEvent;
1774 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].hatComp[n].cookie, &hidEvent);
1775 if (kIOReturnSuccess == result)
1777 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.POV != hidEvent.value)
1779 ActiveJoysticks[joystick].persistentData.JoystickEvent.POV = hidEvent.value;
1786 postEventFromUser(ActiveJoysticks[joystick].persistentData);
1788 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1791 video::IVideoModeList* CIrrDeviceMacOSX::getVideoModeList()
1793 if (!VideoModeList->getVideoModeCount())
1795 CGDirectDisplayID display;
1796 display = CGMainDisplayID();
1799 CFArrayRef Modes = CGDisplayCopyAllDisplayModes(display, NULL);
1801 for(int i = 0; i < CFArrayGetCount(Modes); ++i)
1803 CGDisplayModeRef CurrentMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(Modes, i);
1807 CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(CurrentMode);
1809 if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1812 if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1815 if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1820 unsigned int Width = CGDisplayModeGetWidth(CurrentMode);
1821 unsigned int Height = CGDisplayModeGetHeight(CurrentMode);
1823 VideoModeList->addMode(core::dimension2d<u32>(Width, Height), Depth);
1827 CFArrayRef availableModes = CGDisplayAvailableModes(display);
1828 unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes);
1829 for (u32 i= 0; i<numberOfAvailableModes; ++i)
1831 // look at each mode in the available list
1832 CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i);
1833 long bitsPerPixel = GetDictionaryLong(mode, kCGDisplayBitsPerPixel);
1834 Boolean safeForHardware = GetDictionaryBoolean(mode, kCGDisplayModeIsSafeForHardware);
1835 Boolean stretched = GetDictionaryBoolean(mode, kCGDisplayModeIsStretched);
1837 if (!safeForHardware)
1840 long width = GetDictionaryLong(mode, kCGDisplayWidth);
1841 long height = GetDictionaryLong(mode, kCGDisplayHeight);
1842 // long refresh = GetDictionaryLong((mode), kCGDisplayRefreshRate);
1843 VideoModeList.addMode(core::dimension2d<u32>(width, height),
1848 return VideoModeList;
1853 #endif // _IRR_COMPILE_WITH_OSX_DEVICE_