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
8 #ifdef _IRR_COMPILE_WITH_OSX_DEVICE_
10 #import <Cocoa/Cocoa.h>
13 #include "CIrrDeviceOSX.h"
15 #include "IEventReceiver.h"
18 #include "irrString.h"
21 #include <sys/utsname.h>
22 #include "COSOperator.h"
23 #include "CColorConverter.h"
30 #include "CNSOGLManager.h"
32 #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
34 #include <IOKit/IOKitLib.h>
35 #include <IOKit/IOCFPlugIn.h>
36 #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
37 #include <IOKit/hid/IOHIDLib.h>
38 #include <IOKit/hid/IOHIDKeys.h>
40 struct JoystickComponent
42 IOHIDElementCookie cookie; // unique value which identifies element, will NOT change
43 long min; // reported min value possible
44 long max; // reported max value possible
46 long minRead; //min read value
47 long maxRead; //max read value
49 JoystickComponent() : min(0), minRead(0), max(0), maxRead(0)
56 irr::core::array <JoystickComponent> axisComp;
57 irr::core::array <JoystickComponent> buttonComp;
58 irr::core::array <JoystickComponent> hatComp;
63 int numActiveJoysticks;
65 irr::SEvent persistentData;
67 IOHIDDeviceInterface ** interface;
69 char joystickName[256];
70 long usage; // usage page from IOUSBHID Parser.h which defines general usage
71 long usagePage; // usage within above page from IOUSBHID Parser.h which defines specific usage
73 JoystickInfo() : hats(0), axes(0), buttons(0), interface(0), removed(false), usage(0), usagePage(0), numActiveJoysticks(0)
76 memset(joystickName, '\0', 256);
81 persistentData.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
82 persistentData.JoystickEvent.POV = 65535;
83 persistentData.JoystickEvent.ButtonStates = 0;
86 irr::core::array<JoystickInfo> ActiveJoysticks;
88 //helper functions for init joystick
89 static IOReturn closeJoystickDevice (JoystickInfo* joyInfo)
91 IOReturn result = kIOReturnSuccess;
92 if (joyInfo && joyInfo->interface)
94 /* close the interface */
95 result = (*(joyInfo->interface))->close (joyInfo->interface);
96 if (kIOReturnNotOpen == result)
98 /* do nothing as device was not opened, thus can't be closed */
100 else if (kIOReturnSuccess != result)
101 irr::os::Printer::log("IOHIDDeviceInterface failed to close", irr::ELL_ERROR);
102 /* release the interface */
103 result = (*(joyInfo->interface))->Release (joyInfo->interface);
104 if (kIOReturnSuccess != result)
105 irr::os::Printer::log("IOHIDDeviceInterface failed to release", irr::ELL_ERROR);
106 joyInfo->interface = NULL;
111 static void addComponentInfo (CFTypeRef refElement, JoystickComponent *pComponent, int numActiveJoysticks)
116 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementCookieKey));
117 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
118 pComponent->cookie = (IOHIDElementCookie) number;
119 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMinKey));
120 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
121 pComponent->minRead = pComponent->min = number;
122 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMaxKey));
123 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
124 pComponent->maxRead = pComponent->max = number;
127 static void getJoystickComponentArrayHandler (const void * value, void * parameter);
129 static void addJoystickComponent (CFTypeRef refElement, JoystickInfo* joyInfo)
131 long elementType, usagePage, usage;
132 CFTypeRef refElementType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementTypeKey));
133 CFTypeRef refUsagePage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsagePageKey));
134 CFTypeRef refUsage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsageKey));
136 if ((refElementType) && (CFNumberGetValue ((CFNumberRef)refElementType, kCFNumberLongType, &elementType)))
138 /* look at types of interest */
139 if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) ||
140 (elementType == kIOHIDElementTypeInput_Axis))
142 if (refUsagePage && CFNumberGetValue ((CFNumberRef)refUsagePage, kCFNumberLongType, &usagePage) &&
143 refUsage && CFNumberGetValue ((CFNumberRef)refUsage, kCFNumberLongType, &usage))
145 switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
147 case kHIDPage_GenericDesktop:
149 switch (usage) /* look at usage to determine function */
154 case kHIDUsage_GD_Rx:
155 case kHIDUsage_GD_Ry:
156 case kHIDUsage_GD_Rz:
157 case kHIDUsage_GD_Slider:
158 case kHIDUsage_GD_Dial:
159 case kHIDUsage_GD_Wheel:
162 JoystickComponent newComponent;
163 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
164 joyInfo->axisComp.push_back(newComponent);
167 case kHIDUsage_GD_Hatswitch:
170 JoystickComponent newComponent;
171 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
172 joyInfo->hatComp.push_back(newComponent);
178 case kHIDPage_Button:
181 JoystickComponent newComponent;
182 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
183 joyInfo->buttonComp.push_back(newComponent);
191 else if (kIOHIDElementTypeCollection == elementType)
194 CFTypeRef refElementTop = CFDictionaryGetValue ((CFMutableDictionaryRef) refElement, CFSTR(kIOHIDElementKey));
197 CFTypeID type = CFGetTypeID (refElementTop);
198 if (type == CFArrayGetTypeID())
200 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
201 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, joyInfo);
208 static void getJoystickComponentArrayHandler (const void * value, void * parameter)
210 if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
211 addJoystickComponent ((CFTypeRef) value, (JoystickInfo *) parameter);
214 static void joystickTopLevelElementHandler (const void * value, void * parameter)
217 if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
219 refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsagePageKey));
220 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usagePage))
221 irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usagePage", irr::ELL_ERROR);
222 refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsageKey));
223 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usage))
224 irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usage", irr::ELL_ERROR);
227 static void getJoystickDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, JoystickInfo *joyInfo)
229 CFMutableDictionaryRef usbProperties = 0;
230 io_registry_entry_t parent1, parent2;
232 /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
233 * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
235 if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
236 (KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
237 (KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
243 * try hid dictionary first, if fail then go to usb dictionary
246 /* get joystickName name */
247 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
249 refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
252 if (!CFStringGetCString ((CFStringRef)refCF, joyInfo->joystickName, 256, CFStringGetSystemEncoding ()))
253 irr::os::Printer::log("CFStringGetCString error getting joyInfo->joystickName", irr::ELL_ERROR);
256 /* get usage page and usage */
257 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
260 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usagePage))
261 irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usagePage", irr::ELL_ERROR);
262 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
264 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usage))
265 irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usage", irr::ELL_ERROR);
268 if (NULL == refCF) /* get top level element HID usage page or usage */
270 /* use top level element instead */
271 CFTypeRef refCFTopElement = 0;
272 refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
274 /* refCFTopElement points to an array of element dictionaries */
275 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refCFTopElement)};
276 CFArrayApplyFunction ((CFArrayRef)refCFTopElement, range, joystickTopLevelElementHandler, joyInfo);
280 CFRelease (usbProperties);
283 irr::os::Printer::log("IORegistryEntryCreateCFProperties failed to create usbProperties", irr::ELL_ERROR);
285 if (kIOReturnSuccess != IOObjectRelease (parent2))
286 irr::os::Printer::log("IOObjectRelease failed to release parent2", irr::ELL_ERROR);
287 if (kIOReturnSuccess != IOObjectRelease (parent1))
288 irr::os::Printer::log("IOObjectRelease failed to release parent1", irr::ELL_ERROR);
292 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
294 // Contents from Events.h from Carbon/HIToolbox but we need it with Cocoa too
295 // and for some reason no Cocoa equivalent of these constants seems provided.
296 // So I'm doing like everyone else and using copy-and-paste.
303 * These constants are the virtual keycodes defined originally in
304 * Inside Mac Volume V, pg. V-191. They identify physical keys on a
305 * keyboard. Those constants with "ANSI" in the name are labeled
306 * according to the key position on an ANSI-standard US keyboard.
307 * For example, kVK_ANSI_A indicates the virtual keycode for the key
308 * with the letter 'A' in the US keyboard layout. Other keyboard
309 * layouts may have the 'A' key label on a different physical key;
310 * in this case, pressing 'A' will generate a different virtual
337 kVK_ANSI_Equal = 0x18,
340 kVK_ANSI_Minus = 0x1B,
343 kVK_ANSI_RightBracket = 0x1E,
346 kVK_ANSI_LeftBracket = 0x21,
351 kVK_ANSI_Quote = 0x27,
353 kVK_ANSI_Semicolon = 0x29,
354 kVK_ANSI_Backslash = 0x2A,
355 kVK_ANSI_Comma = 0x2B,
356 kVK_ANSI_Slash = 0x2C,
359 kVK_ANSI_Period = 0x2F,
360 kVK_ANSI_Grave = 0x32,
361 kVK_ANSI_KeypadDecimal = 0x41,
362 kVK_ANSI_KeypadMultiply = 0x43,
363 kVK_ANSI_KeypadPlus = 0x45,
364 kVK_ANSI_KeypadClear = 0x47,
365 kVK_ANSI_KeypadDivide = 0x4B,
366 kVK_ANSI_KeypadEnter = 0x4C,
367 kVK_ANSI_KeypadMinus = 0x4E,
368 kVK_ANSI_KeypadEquals = 0x51,
369 kVK_ANSI_Keypad0 = 0x52,
370 kVK_ANSI_Keypad1 = 0x53,
371 kVK_ANSI_Keypad2 = 0x54,
372 kVK_ANSI_Keypad3 = 0x55,
373 kVK_ANSI_Keypad4 = 0x56,
374 kVK_ANSI_Keypad5 = 0x57,
375 kVK_ANSI_Keypad6 = 0x58,
376 kVK_ANSI_Keypad7 = 0x59,
377 kVK_ANSI_Keypad8 = 0x5B,
378 kVK_ANSI_Keypad9 = 0x5C
381 /* keycodes for keys that are independent of keyboard layout*/
393 kVK_RightShift = 0x3C,
394 kVK_RightOption = 0x3D,
395 kVK_RightControl = 0x3E,
399 kVK_VolumeDown = 0x49,
420 kVK_ForwardDelete = 0x75,
426 kVK_LeftArrow = 0x7B,
427 kVK_RightArrow = 0x7C,
428 kVK_DownArrow = 0x7D,
432 //------------------------------------------------------------------------------------------
433 Boolean GetDictionaryBoolean(CFDictionaryRef theDict, const void* key)
435 // get a boolean from the dictionary
436 Boolean value = false;
437 CFBooleanRef boolRef;
438 boolRef = (CFBooleanRef)CFDictionaryGetValue(theDict, key);
440 value = CFBooleanGetValue(boolRef);
443 //------------------------------------------------------------------------------------------
444 long GetDictionaryLong(CFDictionaryRef theDict, const void* key)
446 // get a long from the dictionary
449 numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
451 CFNumberGetValue(numRef, kCFNumberLongType, &value);
459 IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& param, io::IFileSystem* io, IContextManager* contextManager);
461 } // end namespace irr
463 static bool firstLaunch = true;
465 @implementation CIrrDelegateOSX
467 irr::CIrrDeviceMacOSX* Device;
471 - (id)initWithDevice:(irr::CIrrDeviceMacOSX*)device
483 - (void)applicationDidFinishLaunching:(NSNotification*)notification
488 - (void)orderFrontStandardAboutPanel:(id)sender
490 [NSApp orderFrontStandardAboutPanel:sender];
493 - (void)unhideAllApplications:(id)sender
495 [NSApp unhideAllApplications:sender];
498 - (void)hide:(id)sender
503 - (void)hideOtherApplications:(id)sender
505 [NSApp hideOtherApplications:sender];
508 - (void)terminate:(id)sender
513 - (void)windowWillClose:(id)sender
515 Device->setWindow(nil);
519 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
521 if (Device->isResizable())
522 return proposedFrameSize;
524 return [window frame].size;
527 - (void)windowDidResize:(NSNotification *)aNotification
532 window = [aNotification object];
533 frame = [window frame];
534 Device->setResize((int)frame.size.width,(int)frame.size.height);
547 CIrrDeviceMacOSX::CIrrDeviceMacOSX(const SIrrlichtCreationParameters& param)
548 : CIrrDeviceStub(param), Window(NULL), Display(NULL),
549 DeviceWidth(0), DeviceHeight(0),
550 ScreenWidth(0), ScreenHeight(0), MouseButtonStates(0),
551 IsActive(true), IsFullscreen(false), IsShiftDown(false), IsControlDown(false), IsResizable(false)
556 setDebugName("CIrrDeviceMacOSX");
563 if (!CreationParams.WindowId)
565 [[NSAutoreleasePool alloc] init];
566 [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
567 [NSApp setDelegate:(id<NSApplicationDelegate>)[[[CIrrDelegateOSX alloc] initWithDevice:this] autorelease]];
571 NSString* bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
572 if (bundleName == nil)
573 bundleName = @"Irrlicht";
575 NSMenu* mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease];
576 NSMenu* menu = [[[NSMenu alloc] initWithTitle:bundleName] autorelease];
577 NSMenuItem* menuItem = [mainMenu addItemWithTitle:bundleName action:nil keyEquivalent:@""];
578 [mainMenu setSubmenu:menu forItem:menuItem];
579 menuItem = [menu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
580 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
582 [NSApp setMainMenu:mainMenu];
584 [NSApp finishLaunching];
587 NSString *path = [[NSBundle mainBundle] bundlePath];
589 path = [path stringByAppendingString:@"/Contents/Resources"];
590 chdir([path fileSystemRepresentation]);
596 Operator = new COSOperator(name.version);
597 os::Printer::log(name.version,ELL_INFORMATION);
603 if (CreationParams.DriverType != video::EDT_NULL)
604 success = createWindow();
606 // in case of failure, one can check VideoDriver for initialization
611 CursorControl = new CCursorControl(CreationParams.WindowSize, this);
617 CIrrDeviceMacOSX::~CIrrDeviceMacOSX()
619 [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
621 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
622 for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
624 if (ActiveJoysticks[joystick].interface)
625 closeJoystickDevice(&ActiveJoysticks[joystick]);
630 void CIrrDeviceMacOSX::closeDevice()
634 [Window setIsVisible:FALSE];
635 [Window setReleasedWhenClosed:TRUE];
640 IsFullscreen = false;
644 bool CIrrDeviceMacOSX::createWindow()
648 Display = CGMainDisplayID();
651 CGDisplayModeRef displaymode, olddisplaymode;
653 ScreenWidth = (int)CGDisplayPixelsWide(Display);
654 ScreenHeight = (int)CGDisplayPixelsHigh(Display);
656 const NSBackingStoreType type = (CreationParams.DriverType == video::EDT_OPENGL) ? NSBackingStoreBuffered : NSBackingStoreNonretained;
659 //if (!CreationParams.Fullscreen)
661 if (!CreationParams.WindowId) //create another window when WindowId is null
663 int x = (CreationParams.WindowPosition.X > 0) ? CreationParams.WindowPosition.X : 0;
664 int y = (CreationParams.WindowPosition.Y > 0) ? CreationParams.WindowPosition.Y : 0;
666 if (CreationParams.WindowPosition.Y > -1)
668 int screenHeight = [[[NSScreen screens] objectAtIndex:0] frame].size.height;
669 y = screenHeight - y - CreationParams.WindowSize.Height;
672 Window = [[NSWindow alloc] initWithContentRect:NSMakeRect(x, y, CreationParams.WindowSize.Width,CreationParams.WindowSize.Height) styleMask:NSTitledWindowMask+NSClosableWindowMask+NSResizableWindowMask backing:type defer:FALSE];
674 if (CreationParams.WindowPosition.X == -1 && CreationParams.WindowPosition.Y == -1)
678 DeviceWidth = CreationParams.WindowSize.Width;
679 DeviceHeight = CreationParams.WindowSize.Height;
688 [Window setDelegate:(id<NSWindowDelegate>)[NSApp delegate]];
689 [Window setAcceptsMouseMovedEvents:TRUE];
690 [Window setIsVisible:TRUE];
691 [Window makeKeyAndOrderFront:nil];
698 void CIrrDeviceMacOSX::setResize(int width, int height)
700 // set new window size
702 DeviceHeight = height;
704 #if defined(_IRR_COMPILE_WITH_OPENGL_)
705 // update the size of the opengl rendering context
706 if (CreationParams.DriverType == video::EDT_OPENGL)
708 NSOpenGLContext* Context = (NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context;
715 // resize the driver to the inner pane size
718 NSRect driverFrame = [Window contentRectForFrameRect:[Window frame]];
719 getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)driverFrame.size.width, (s32)driverFrame.size.height));
720 DeviceWidth = (s32)driverFrame.size.width;
721 DeviceHeight = (s32)driverFrame.size.height;
724 getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)width, (s32)height));
728 void CIrrDeviceMacOSX::createDriver()
730 switch (CreationParams.DriverType)
732 case video::EDT_OPENGL:
733 #ifdef _IRR_COMPILE_WITH_OPENGL_
735 video::SExposedVideoData data;
736 data.OpenGLOSX.Window = Window;
737 ContextManager = new video::CNSOGLManager();
738 ContextManager->initialize(CreationParams, data);
739 VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, ContextManager);
742 os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);
747 [[Window contentView] setWantsBestResolutionOpenGLSurface:NO];
748 [(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context setView:[Window contentView]];
752 [(NSView*)CreationParams.WindowId setWantsBestResolutionOpenGLSurface:NO];
753 [(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context setView:(NSView*)CreationParams.WindowId];
757 os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
761 case video::DEPRECATED_EDT_DIRECT3D8_NO_LONGER_EXISTS:
762 case video::EDT_DIRECT3D9:
763 case video::EDT_OGLES1:
764 case video::EDT_OGLES2:
765 os::Printer::log("This driver is not available in OSX. Try OpenGL or Software renderer.", ELL_ERROR);
768 case video::EDT_NULL:
769 VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
773 os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
778 bool CIrrDeviceMacOSX::run()
780 NSAutoreleasePool* Pool = [[NSAutoreleasePool alloc] init];
786 storeMouseLocation();
788 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
791 bzero(&ievent,sizeof(ievent));
793 switch([(NSEvent *)event type])
796 postKeyEvent(event,ievent,true);
800 postKeyEvent(event,ievent,false);
804 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
805 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
806 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
808 if (IsShiftDown != ievent.KeyInput.Shift)
810 ievent.KeyInput.Char = irr::KEY_SHIFT;
811 ievent.KeyInput.Key = irr::KEY_SHIFT;
812 ievent.KeyInput.PressedDown = ievent.KeyInput.Shift;
814 IsShiftDown = ievent.KeyInput.Shift;
816 postEventFromUser(ievent);
819 if (IsControlDown != ievent.KeyInput.Control)
821 ievent.KeyInput.Char = irr::KEY_CONTROL;
822 ievent.KeyInput.Key = irr::KEY_CONTROL;
823 ievent.KeyInput.PressedDown = ievent.KeyInput.Control;
825 IsControlDown = ievent.KeyInput.Control;
827 postEventFromUser(ievent);
830 [NSApp sendEvent:event];
833 case NSLeftMouseDown:
834 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
835 ievent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
836 MouseButtonStates |= irr::EMBSM_LEFT;
837 ievent.MouseInput.ButtonStates = MouseButtonStates;
838 postMouseEvent(event,ievent);
842 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
843 MouseButtonStates &= !irr::EMBSM_LEFT;
844 ievent.MouseInput.ButtonStates = MouseButtonStates;
845 ievent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
846 postMouseEvent(event,ievent);
849 case NSOtherMouseDown:
850 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
851 ievent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
852 MouseButtonStates |= irr::EMBSM_MIDDLE;
853 ievent.MouseInput.ButtonStates = MouseButtonStates;
854 postMouseEvent(event,ievent);
858 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
859 MouseButtonStates &= !irr::EMBSM_MIDDLE;
860 ievent.MouseInput.ButtonStates = MouseButtonStates;
861 ievent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
862 postMouseEvent(event,ievent);
866 case NSLeftMouseDragged:
867 case NSRightMouseDragged:
868 case NSOtherMouseDragged:
869 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
870 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
871 ievent.MouseInput.ButtonStates = MouseButtonStates;
872 postMouseEvent(event,ievent);
875 case NSRightMouseDown:
876 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
877 ievent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
878 MouseButtonStates |= irr::EMBSM_RIGHT;
879 ievent.MouseInput.ButtonStates = MouseButtonStates;
880 postMouseEvent(event,ievent);
884 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
885 ievent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
886 MouseButtonStates &= !irr::EMBSM_RIGHT;
887 ievent.MouseInput.ButtonStates = MouseButtonStates;
888 postMouseEvent(event,ievent);
892 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
893 ievent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
894 ievent.MouseInput.Wheel = [(NSEvent *)event deltaY];
895 if (ievent.MouseInput.Wheel < 1.0f)
896 ievent.MouseInput.Wheel *= 10.0f;
898 ievent.MouseInput.Wheel *= 5.0f;
899 postMouseEvent(event,ievent);
903 [NSApp sendEvent:event];
912 return (![[NSApp delegate] isQuit] && IsActive);
916 //! Pause the current process for the minimum time allowed only to allow other processes to execute
917 void CIrrDeviceMacOSX::yield()
919 struct timespec ts = {0,0};
920 nanosleep(&ts, NULL);
924 //! Pause execution and let other processes to run for a specified amount of time.
925 void CIrrDeviceMacOSX::sleep(u32 timeMs, bool pauseTimer=false)
927 bool wasStopped = Timer ? Timer->isStopped() : true;
930 ts.tv_sec = (time_t) (timeMs / 1000);
931 ts.tv_nsec = (long) (timeMs % 1000) * 1000000;
933 if (pauseTimer && !wasStopped)
936 nanosleep(&ts, NULL);
938 if (pauseTimer && !wasStopped)
943 void CIrrDeviceMacOSX::setWindowCaption(const wchar_t* text)
949 size_t numBytes = wcslen(text) * sizeof(wchar_t);
951 #ifdef __BIG_ENDIAN__
952 NSStringEncoding encode = sizeof(wchar_t) == 4 ? NSUTF32BigEndianStringEncoding : NSUTF16BigEndianStringEncoding;
954 NSStringEncoding encode = sizeof(wchar_t) == 4 ? NSUTF32LittleEndianStringEncoding : NSUTF16LittleEndianStringEncoding;
956 NSString* name = [[NSString alloc] initWithBytes:text length:numBytes encoding:encode];
959 [Window setTitle:name];
965 [Window setTitle:@""];
971 bool CIrrDeviceMacOSX::isWindowActive() const
977 bool CIrrDeviceMacOSX::isWindowFocused() const
980 return [Window isKeyWindow];
985 bool CIrrDeviceMacOSX::isWindowMinimized() const
988 return [Window isMiniaturized];
993 void CIrrDeviceMacOSX::postKeyEvent(void *event,irr::SEvent &ievent,bool pressed)
996 std::map<int,int>::const_iterator iter;
997 unsigned int c,mkey,mchar;
998 const unsigned char *cStr;
1001 str = [(NSEvent *)event characters];
1002 if ((str != nil) && ([str length] > 0))
1005 skipCommand = false;
1006 c = [str characterAtIndex:0];
1009 iter = KeyCodes.find([(NSEvent *)event keyCode]);
1010 if (iter != KeyCodes.end())
1011 mkey = (*iter).second;
1012 else if ((iter = KeyCodes.find(c)) != KeyCodes.end())
1013 mkey = (*iter).second;
1016 // workaround for period character
1019 mkey = irr::KEY_PERIOD;
1024 cStr = (unsigned char *)[str cStringUsingEncoding:NSWindowsCP1252StringEncoding];
1025 if (cStr != NULL && strlen((char*)cStr) > 0)
1028 mkey = toupper(mchar);
1029 if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1031 if (mkey == 'C' || mkey == 'V' || mkey == 'X')
1041 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
1042 ievent.KeyInput.Key = (irr::EKEY_CODE)mkey;
1043 ievent.KeyInput.PressedDown = pressed;
1044 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1045 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1046 ievent.KeyInput.Char = mchar;
1049 ievent.KeyInput.Control = true;
1050 else if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1051 [NSApp sendEvent:(NSEvent *)event];
1053 postEventFromUser(ievent);
1058 void CIrrDeviceMacOSX::postMouseEvent(void *event,irr::SEvent &ievent)
1064 ievent.MouseInput.X = (int)[(NSEvent *)event locationInWindow].x;
1065 ievent.MouseInput.Y = DeviceHeight - (int)[(NSEvent *)event locationInWindow].y;
1067 if (ievent.MouseInput.Y < 0)
1072 CGEventRef ourEvent = CGEventCreate(NULL);
1073 CGPoint point = CGEventGetLocation(ourEvent);
1074 CFRelease(ourEvent);
1076 ievent.MouseInput.X = (int)point.x;
1077 ievent.MouseInput.Y = (int)point.y;
1079 if (ievent.MouseInput.Y < 0)
1085 ievent.MouseInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1086 ievent.MouseInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1088 postEventFromUser(ievent);
1091 [NSApp sendEvent:(NSEvent *)event];
1095 void CIrrDeviceMacOSX::storeMouseLocation()
1102 p = [NSEvent mouseLocation];
1103 p = [Window convertScreenToBase:p];
1105 y = DeviceHeight - (int)p.y;
1109 // Do we still need this?
1110 CGEventRef ourEvent = CGEventCreate(NULL);
1111 CGPoint point = CGEventGetLocation(ourEvent);
1112 CFRelease(ourEvent);
1117 const core::position2di& curr = ((CCursorControl *)CursorControl)->getPosition(true);
1118 if (curr.X != x || curr.Y != y)
1121 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1122 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1123 ievent.MouseInput.X = x;
1124 ievent.MouseInput.Y = y;
1125 postEventFromUser(ievent);
1129 ((CCursorControl *)CursorControl)->updateInternalCursorPosition(x,y);
1133 void CIrrDeviceMacOSX::setMouseLocation(int x,int y)
1140 // Irrlicht window exists
1142 p.y = (float) (DeviceHeight - y);
1143 p = [Window convertBaseToScreen:p];
1144 p.y = ScreenHeight - p.y;
1149 p.y = (float) y + (ScreenHeight - DeviceHeight);
1155 CGWarpMouseCursorPosition(c);
1156 CGAssociateMouseAndMouseCursorPosition(YES);
1160 void CIrrDeviceMacOSX::setCursorVisible(bool visible)
1163 CGDisplayShowCursor(CGMainDisplayID());
1165 CGDisplayHideCursor(CGMainDisplayID());
1169 void CIrrDeviceMacOSX::setWindow(NSWindow* window)
1175 void CIrrDeviceMacOSX::initKeycodes()
1177 KeyCodes[kVK_UpArrow] = irr::KEY_UP;
1178 KeyCodes[kVK_DownArrow] = irr::KEY_DOWN;
1179 KeyCodes[kVK_LeftArrow] = irr::KEY_LEFT;
1180 KeyCodes[kVK_RightArrow] = irr::KEY_RIGHT;
1181 KeyCodes[kVK_F1] = irr::KEY_F1;
1182 KeyCodes[kVK_F2] = irr::KEY_F2;
1183 KeyCodes[kVK_F3] = irr::KEY_F3;
1184 KeyCodes[kVK_F4] = irr::KEY_F4;
1185 KeyCodes[kVK_F5] = irr::KEY_F5;
1186 KeyCodes[kVK_F6] = irr::KEY_F6;
1187 KeyCodes[kVK_F7] = irr::KEY_F7;
1188 KeyCodes[kVK_F8] = irr::KEY_F8;
1189 KeyCodes[kVK_F9] = irr::KEY_F9;
1190 KeyCodes[kVK_F10] = irr::KEY_F10;
1191 KeyCodes[kVK_F11] = irr::KEY_F11;
1192 KeyCodes[kVK_F12] = irr::KEY_F12;
1193 KeyCodes[kVK_F13] = irr::KEY_F13;
1194 KeyCodes[kVK_F14] = irr::KEY_F14;
1195 KeyCodes[kVK_F15] = irr::KEY_F15;
1196 KeyCodes[kVK_F16] = irr::KEY_F16;
1197 KeyCodes[kVK_F17] = irr::KEY_F17;
1198 KeyCodes[kVK_F18] = irr::KEY_F18;
1199 KeyCodes[kVK_F19] = irr::KEY_F19;
1200 KeyCodes[kVK_F20] = irr::KEY_F20;
1201 KeyCodes[kVK_Home] = irr::KEY_HOME;
1202 KeyCodes[kVK_End] = irr::KEY_END;
1203 KeyCodes[NSInsertFunctionKey] = irr::KEY_INSERT;
1204 KeyCodes[kVK_ForwardDelete] = irr::KEY_DELETE;
1205 KeyCodes[kVK_Help] = irr::KEY_HELP;
1206 KeyCodes[NSSelectFunctionKey] = irr::KEY_SELECT;
1207 KeyCodes[NSPrintFunctionKey] = irr::KEY_PRINT;
1208 KeyCodes[NSExecuteFunctionKey] = irr::KEY_EXECUT;
1209 KeyCodes[NSPrintScreenFunctionKey] = irr::KEY_SNAPSHOT;
1210 KeyCodes[NSPauseFunctionKey] = irr::KEY_PAUSE;
1211 KeyCodes[NSScrollLockFunctionKey] = irr::KEY_SCROLL;
1212 KeyCodes[kVK_Delete] = irr::KEY_BACK;
1213 KeyCodes[kVK_Tab] = irr::KEY_TAB;
1214 KeyCodes[kVK_Return] = irr::KEY_RETURN;
1215 KeyCodes[kVK_Escape] = irr::KEY_ESCAPE;
1216 KeyCodes[kVK_Control] = irr::KEY_CONTROL;
1217 KeyCodes[kVK_RightControl] = irr::KEY_RCONTROL;
1218 KeyCodes[kVK_Command] = irr::KEY_MENU;
1219 KeyCodes[kVK_Shift] = irr::KEY_SHIFT;
1220 KeyCodes[kVK_RightShift] = irr::KEY_RSHIFT;
1221 KeyCodes[kVK_Space] = irr::KEY_SPACE;
1223 KeyCodes[kVK_ANSI_A] = irr::KEY_KEY_A;
1224 KeyCodes[kVK_ANSI_B] = irr::KEY_KEY_B;
1225 KeyCodes[kVK_ANSI_C] = irr::KEY_KEY_C;
1226 KeyCodes[kVK_ANSI_D] = irr::KEY_KEY_D;
1227 KeyCodes[kVK_ANSI_E] = irr::KEY_KEY_E;
1228 KeyCodes[kVK_ANSI_F] = irr::KEY_KEY_F;
1229 KeyCodes[kVK_ANSI_G] = irr::KEY_KEY_G;
1230 KeyCodes[kVK_ANSI_H] = irr::KEY_KEY_H;
1231 KeyCodes[kVK_ANSI_I] = irr::KEY_KEY_I;
1232 KeyCodes[kVK_ANSI_J] = irr::KEY_KEY_J;
1233 KeyCodes[kVK_ANSI_K] = irr::KEY_KEY_K;
1234 KeyCodes[kVK_ANSI_L] = irr::KEY_KEY_L;
1235 KeyCodes[kVK_ANSI_M] = irr::KEY_KEY_M;
1236 KeyCodes[kVK_ANSI_N] = irr::KEY_KEY_N;
1237 KeyCodes[kVK_ANSI_O] = irr::KEY_KEY_O;
1238 KeyCodes[kVK_ANSI_P] = irr::KEY_KEY_P;
1239 KeyCodes[kVK_ANSI_Q] = irr::KEY_KEY_Q;
1240 KeyCodes[kVK_ANSI_R] = irr::KEY_KEY_R;
1241 KeyCodes[kVK_ANSI_S] = irr::KEY_KEY_S;
1242 KeyCodes[kVK_ANSI_T] = irr::KEY_KEY_T;
1243 KeyCodes[kVK_ANSI_U] = irr::KEY_KEY_U;
1244 KeyCodes[kVK_ANSI_V] = irr::KEY_KEY_V;
1245 KeyCodes[kVK_ANSI_W] = irr::KEY_KEY_W;
1246 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1247 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1248 KeyCodes[kVK_ANSI_Y] = irr::KEY_KEY_Y;
1249 KeyCodes[kVK_ANSI_Z] = irr::KEY_KEY_Z;
1251 KeyCodes[kVK_ANSI_0] = irr::KEY_KEY_0;
1252 KeyCodes[kVK_ANSI_1] = irr::KEY_KEY_1;
1253 KeyCodes[kVK_ANSI_2] = irr::KEY_KEY_2;
1254 KeyCodes[kVK_ANSI_3] = irr::KEY_KEY_3;
1255 KeyCodes[kVK_ANSI_4] = irr::KEY_KEY_4;
1256 KeyCodes[kVK_ANSI_5] = irr::KEY_KEY_5;
1257 KeyCodes[kVK_ANSI_6] = irr::KEY_KEY_6;
1258 KeyCodes[kVK_ANSI_7] = irr::KEY_KEY_7;
1259 KeyCodes[kVK_ANSI_8] = irr::KEY_KEY_8;
1260 KeyCodes[kVK_ANSI_9] = irr::KEY_KEY_9;
1262 KeyCodes[kVK_ANSI_Slash] = irr::KEY_DIVIDE;
1263 KeyCodes[kVK_ANSI_Comma] = irr::KEY_COMMA;
1264 KeyCodes[kVK_ANSI_Period] = irr::KEY_PERIOD;
1265 KeyCodes[kVK_PageUp] = irr::KEY_PRIOR;
1266 KeyCodes[kVK_PageDown] = irr::KEY_NEXT;
1268 KeyCodes[kVK_ANSI_Keypad0] = irr::KEY_NUMPAD0;
1269 KeyCodes[kVK_ANSI_Keypad1] = irr::KEY_NUMPAD1;
1270 KeyCodes[kVK_ANSI_Keypad2] = irr::KEY_NUMPAD2;
1271 KeyCodes[kVK_ANSI_Keypad3] = irr::KEY_NUMPAD3;
1272 KeyCodes[kVK_ANSI_Keypad4] = irr::KEY_NUMPAD4;
1273 KeyCodes[kVK_ANSI_Keypad5] = irr::KEY_NUMPAD5;
1274 KeyCodes[kVK_ANSI_Keypad6] = irr::KEY_NUMPAD6;
1275 KeyCodes[kVK_ANSI_Keypad7] = irr::KEY_NUMPAD7;
1276 KeyCodes[kVK_ANSI_Keypad8] = irr::KEY_NUMPAD8;
1277 KeyCodes[kVK_ANSI_Keypad9] = irr::KEY_NUMPAD9;
1279 KeyCodes[kVK_ANSI_KeypadDecimal] = irr::KEY_DECIMAL;
1280 KeyCodes[kVK_ANSI_KeypadMultiply] = irr::KEY_MULTIPLY;
1281 KeyCodes[kVK_ANSI_KeypadPlus] = irr::KEY_PLUS;
1282 KeyCodes[kVK_ANSI_KeypadClear] = irr::KEY_OEM_CLEAR;
1283 KeyCodes[kVK_ANSI_KeypadDivide] = irr::KEY_DIVIDE;
1284 KeyCodes[kVK_ANSI_KeypadEnter] = irr::KEY_RETURN;
1285 KeyCodes[kVK_ANSI_KeypadMinus] = irr::KEY_SUBTRACT;
1287 KeyCodes[kVK_ANSI_LeftBracket] = irr::KEY_OEM_4;
1288 KeyCodes[kVK_ANSI_Backslash] = irr::KEY_OEM_5;
1289 KeyCodes[kVK_ANSI_RightBracket] = irr::KEY_OEM_6;
1293 //! Sets if the window should be resizable in windowed mode.
1294 void CIrrDeviceMacOSX::setResizable(bool resize)
1296 IsResizable = resize;
1299 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask];
1301 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask];
1306 bool CIrrDeviceMacOSX::isResizable() const
1312 void CIrrDeviceMacOSX::minimizeWindow()
1315 [Window miniaturize:[NSApp self]];
1319 //! Maximizes the window if possible.
1320 void CIrrDeviceMacOSX::maximizeWindow()
1326 //! get the window to normal size if possible.
1327 void CIrrDeviceMacOSX::restoreWindow()
1329 [Window deminiaturize:[NSApp self]];
1332 //! Get the position of this window on screen
1333 core::position2di CIrrDeviceMacOSX::getWindowPosition()
1335 NSRect rect = [Window frame];
1336 int screenHeight = [[[NSScreen screens] objectAtIndex:0] frame].size.height;
1337 return core::position2di(rect.origin.x, screenHeight - rect.origin.y - rect.size.height);
1341 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1342 static void joystickRemovalCallback(void * target,
1343 IOReturn result, void * refcon, void * sender)
1345 JoystickInfo *joy = (JoystickInfo *) refcon;
1348 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1351 bool CIrrDeviceMacOSX::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
1353 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1354 ActiveJoysticks.clear();
1355 joystickInfo.clear();
1357 io_object_t hidObject = 0;
1358 io_iterator_t hidIterator = 0;
1359 IOReturn result = kIOReturnSuccess;
1360 mach_port_t masterPort = 0;
1361 CFMutableDictionaryRef hidDictionaryRef = NULL;
1363 result = IOMasterPort (bootstrap_port, &masterPort);
1364 if (kIOReturnSuccess != result)
1366 os::Printer::log("initialiseJoysticks IOMasterPort failed", ELL_ERROR);
1370 hidDictionaryRef = IOServiceMatching (kIOHIDDeviceKey);
1371 if (!hidDictionaryRef)
1373 os::Printer::log("initialiseJoysticks IOServiceMatching failed", ELL_ERROR);
1376 result = IOServiceGetMatchingServices (masterPort, hidDictionaryRef, &hidIterator);
1378 if (kIOReturnSuccess != result)
1380 os::Printer::log("initialiseJoysticks IOServiceGetMatchingServices failed", ELL_ERROR);
1384 //no joysticks just return
1389 while ((hidObject = IOIteratorNext (hidIterator)))
1393 // get dictionary for HID properties
1394 CFMutableDictionaryRef hidProperties = 0;
1396 kern_return_t kern_result = IORegistryEntryCreateCFProperties (hidObject, &hidProperties, kCFAllocatorDefault, kNilOptions);
1397 if ((kern_result == KERN_SUCCESS) && hidProperties)
1399 HRESULT plugInResult = S_OK;
1401 IOCFPlugInInterface ** ppPlugInInterface = NULL;
1402 result = IOCreatePlugInInterfaceForService (hidObject, kIOHIDDeviceUserClientTypeID,
1403 kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
1404 if (kIOReturnSuccess == result)
1406 plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
1407 CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void **) &(info.interface));
1408 if (plugInResult != S_OK)
1409 os::Printer::log("initialiseJoysticks query HID class device interface failed", ELL_ERROR);
1410 (*ppPlugInInterface)->Release(ppPlugInInterface);
1415 if (info.interface != NULL)
1417 result = (*(info.interface))->open (info.interface, 0);
1418 if (result == kIOReturnSuccess)
1420 (*(info.interface))->setRemovalCallback (info.interface, joystickRemovalCallback, &info, &info);
1421 getJoystickDeviceInfo(hidObject, hidProperties, &info);
1424 CFTypeRef refElementTop = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
1427 CFTypeID type = CFGetTypeID (refElementTop);
1428 if (type == CFArrayGetTypeID())
1430 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
1431 info.numActiveJoysticks = ActiveJoysticks.size();
1432 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, &info);
1438 CFRelease (hidProperties);
1439 os::Printer::log("initialiseJoysticks Open interface failed", ELL_ERROR);
1443 CFRelease (hidProperties);
1445 result = IOObjectRelease (hidObject);
1447 if ( (info.usagePage != kHIDPage_GenericDesktop) ||
1448 ((info.usage != kHIDUsage_GD_Joystick &&
1449 info.usage != kHIDUsage_GD_GamePad &&
1450 info.usage != kHIDUsage_GD_MultiAxisController)) )
1452 closeJoystickDevice (&info);
1456 for (u32 i = 0; i < 6; ++i)
1457 info.persistentData.JoystickEvent.Axis[i] = 0;
1459 ActiveJoysticks.push_back(info);
1461 SJoystickInfo returnInfo;
1462 returnInfo.Joystick = jindex;
1463 returnInfo.Axes = info.axes;
1464 //returnInfo.Hats = info.hats;
1465 returnInfo.Buttons = info.buttons;
1466 returnInfo.Name = info.joystickName;
1467 returnInfo.PovHat = SJoystickInfo::POV_HAT_UNKNOWN;
1470 //if (info.hatComp.size())
1471 // returnInfo.PovHat = SJoystickInfo::POV_HAT_PRESENT;
1473 // returnInfo.PovHat = SJoystickInfo::POV_HAT_ABSENT;
1475 joystickInfo.push_back(returnInfo);
1484 result = IOObjectRelease (hidIterator);
1487 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1492 void CIrrDeviceMacOSX::pollJoysticks()
1494 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1495 if(0 == ActiveJoysticks.size())
1499 for (joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
1501 if (ActiveJoysticks[joystick].removed)
1505 ActiveJoysticks[joystick].persistentData.JoystickEvent.Joystick = joystick;
1507 if (ActiveJoysticks[joystick].interface)
1509 for (u32 n = 0; n < ActiveJoysticks[joystick].axisComp.size(); n++)
1511 IOReturn result = kIOReturnSuccess;
1512 IOHIDEventStruct hidEvent;
1514 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].axisComp[n].cookie, &hidEvent);
1515 if (kIOReturnSuccess == result)
1517 const f32 min = -32768.0f;
1518 const f32 max = 32767.0f;
1519 const f32 deviceScale = max - min;
1520 const f32 readScale = (f32)ActiveJoysticks[joystick].axisComp[n].maxRead - (f32)ActiveJoysticks[joystick].axisComp[n].minRead;
1522 if (hidEvent.value < ActiveJoysticks[joystick].axisComp[n].minRead)
1523 ActiveJoysticks[joystick].axisComp[n].minRead = hidEvent.value;
1524 if (hidEvent.value > ActiveJoysticks[joystick].axisComp[n].maxRead)
1525 ActiveJoysticks[joystick].axisComp[n].maxRead = hidEvent.value;
1527 if (readScale != 0.0f)
1528 hidEvent.value = (int)(((f32)((f32)hidEvent.value - (f32)ActiveJoysticks[joystick].axisComp[n].minRead) * deviceScale / readScale) + min);
1530 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] != (s16)hidEvent.value)
1532 ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] = (s16)hidEvent.value;
1536 for (u32 n = 0; n < ActiveJoysticks[joystick].buttonComp.size(); n++)
1538 IOReturn result = kIOReturnSuccess;
1539 IOHIDEventStruct hidEvent;
1541 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].buttonComp[n].cookie, &hidEvent);
1542 if (kIOReturnSuccess == result)
1544 if (hidEvent.value && !((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false) )
1546 else if (!hidEvent.value && ((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false))
1550 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates |= (1 << n);
1552 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates &= ~(1 << n);
1555 //still ToDo..will be done soon :)
1557 for (u32 n = 0; n < ActiveJoysticks[joystick].hatComp.size(); n++)
1559 IOReturn result = kIOReturnSuccess;
1560 IOHIDEventStruct hidEvent;
1562 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].hatComp[n].cookie, &hidEvent);
1563 if (kIOReturnSuccess == result)
1565 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.POV != hidEvent.value)
1567 ActiveJoysticks[joystick].persistentData.JoystickEvent.POV = hidEvent.value;
1574 postEventFromUser(ActiveJoysticks[joystick].persistentData);
1576 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1581 #endif // _IRR_COMPILE_WITH_OSX_DEVICE_