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 #include "CIrrDeviceOSX.h"
16 #include "IEventReceiver.h"
19 #include "irrString.h"
22 #include <sys/utsname.h>
23 #include "COSOperator.h"
24 #include "CColorConverter.h"
31 #include "CNSOGLManager.h"
33 #if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
35 #include <IOKit/IOKitLib.h>
36 #include <IOKit/IOCFPlugIn.h>
37 #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
38 #include <IOKit/hid/IOHIDLib.h>
39 #include <IOKit/hid/IOHIDKeys.h>
41 struct JoystickComponent
43 IOHIDElementCookie cookie; // unique value which identifies element, will NOT change
44 long min; // reported min value possible
45 long max; // reported max value possible
47 long minRead; //min read value
48 long maxRead; //max read value
50 JoystickComponent() : min(0), minRead(0), max(0), maxRead(0)
57 irr::core::array <JoystickComponent> axisComp;
58 irr::core::array <JoystickComponent> buttonComp;
59 irr::core::array <JoystickComponent> hatComp;
64 int numActiveJoysticks;
66 irr::SEvent persistentData;
68 IOHIDDeviceInterface ** interface;
70 char joystickName[256];
71 long usage; // usage page from IOUSBHID Parser.h which defines general usage
72 long usagePage; // usage within above page from IOUSBHID Parser.h which defines specific usage
74 JoystickInfo() : hats(0), axes(0), buttons(0), interface(0), removed(false), usage(0), usagePage(0), numActiveJoysticks(0)
77 memset(joystickName, '\0', 256);
82 persistentData.EventType = irr::EET_JOYSTICK_INPUT_EVENT;
83 persistentData.JoystickEvent.POV = 65535;
84 persistentData.JoystickEvent.ButtonStates = 0;
87 irr::core::array<JoystickInfo> ActiveJoysticks;
89 //helper functions for init joystick
90 static IOReturn closeJoystickDevice (JoystickInfo* joyInfo)
92 IOReturn result = kIOReturnSuccess;
93 if (joyInfo && joyInfo->interface)
95 /* close the interface */
96 result = (*(joyInfo->interface))->close (joyInfo->interface);
97 if (kIOReturnNotOpen == result)
99 /* do nothing as device was not opened, thus can't be closed */
101 else if (kIOReturnSuccess != result)
102 irr::os::Printer::log("IOHIDDeviceInterface failed to close", irr::ELL_ERROR);
103 /* release the interface */
104 result = (*(joyInfo->interface))->Release (joyInfo->interface);
105 if (kIOReturnSuccess != result)
106 irr::os::Printer::log("IOHIDDeviceInterface failed to release", irr::ELL_ERROR);
107 joyInfo->interface = NULL;
112 static void addComponentInfo (CFTypeRef refElement, JoystickComponent *pComponent, int numActiveJoysticks)
117 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementCookieKey));
118 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
119 pComponent->cookie = (IOHIDElementCookie) number;
120 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMinKey));
121 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
122 pComponent->minRead = pComponent->min = number;
123 refType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementMaxKey));
124 if (refType && CFNumberGetValue ((CFNumberRef)refType, kCFNumberLongType, &number))
125 pComponent->maxRead = pComponent->max = number;
128 static void getJoystickComponentArrayHandler (const void * value, void * parameter);
130 static void addJoystickComponent (CFTypeRef refElement, JoystickInfo* joyInfo)
132 long elementType, usagePage, usage;
133 CFTypeRef refElementType = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementTypeKey));
134 CFTypeRef refUsagePage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsagePageKey));
135 CFTypeRef refUsage = CFDictionaryGetValue ((CFDictionaryRef)refElement, CFSTR(kIOHIDElementUsageKey));
137 if ((refElementType) && (CFNumberGetValue ((CFNumberRef)refElementType, kCFNumberLongType, &elementType)))
139 /* look at types of interest */
140 if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) ||
141 (elementType == kIOHIDElementTypeInput_Axis))
143 if (refUsagePage && CFNumberGetValue ((CFNumberRef)refUsagePage, kCFNumberLongType, &usagePage) &&
144 refUsage && CFNumberGetValue ((CFNumberRef)refUsage, kCFNumberLongType, &usage))
146 switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
148 case kHIDPage_GenericDesktop:
150 switch (usage) /* look at usage to determine function */
155 case kHIDUsage_GD_Rx:
156 case kHIDUsage_GD_Ry:
157 case kHIDUsage_GD_Rz:
158 case kHIDUsage_GD_Slider:
159 case kHIDUsage_GD_Dial:
160 case kHIDUsage_GD_Wheel:
163 JoystickComponent newComponent;
164 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
165 joyInfo->axisComp.push_back(newComponent);
168 case kHIDUsage_GD_Hatswitch:
171 JoystickComponent newComponent;
172 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
173 joyInfo->hatComp.push_back(newComponent);
179 case kHIDPage_Button:
182 JoystickComponent newComponent;
183 addComponentInfo(refElement, &newComponent, joyInfo->numActiveJoysticks);
184 joyInfo->buttonComp.push_back(newComponent);
192 else if (kIOHIDElementTypeCollection == elementType)
195 CFTypeRef refElementTop = CFDictionaryGetValue ((CFMutableDictionaryRef) refElement, CFSTR(kIOHIDElementKey));
198 CFTypeID type = CFGetTypeID (refElementTop);
199 if (type == CFArrayGetTypeID())
201 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
202 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, joyInfo);
209 static void getJoystickComponentArrayHandler (const void * value, void * parameter)
211 if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
212 addJoystickComponent ((CFTypeRef) value, (JoystickInfo *) parameter);
215 static void joystickTopLevelElementHandler (const void * value, void * parameter)
218 if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
220 refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsagePageKey));
221 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usagePage))
222 irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usagePage", irr::ELL_ERROR);
223 refCF = CFDictionaryGetValue ((CFDictionaryRef)value, CFSTR(kIOHIDElementUsageKey));
224 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &((JoystickInfo *) parameter)->usage))
225 irr::os::Printer::log("CFNumberGetValue error retrieving JoystickInfo->usage", irr::ELL_ERROR);
228 static void getJoystickDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, JoystickInfo *joyInfo)
230 CFMutableDictionaryRef usbProperties = 0;
231 io_registry_entry_t parent1, parent2;
233 /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
234 * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
236 if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
237 (KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
238 (KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
244 * try hid dictionary first, if fail then go to usb dictionary
247 /* get joystickName name */
248 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
250 refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
253 if (!CFStringGetCString ((CFStringRef)refCF, joyInfo->joystickName, 256, CFStringGetSystemEncoding ()))
254 irr::os::Printer::log("CFStringGetCString error getting joyInfo->joystickName", irr::ELL_ERROR);
257 /* get usage page and usage */
258 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
261 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usagePage))
262 irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usagePage", irr::ELL_ERROR);
263 refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
265 if (!CFNumberGetValue ((CFNumberRef)refCF, kCFNumberLongType, &joyInfo->usage))
266 irr::os::Printer::log("CFNumberGetValue error getting joyInfo->usage", irr::ELL_ERROR);
269 if (NULL == refCF) /* get top level element HID usage page or usage */
271 /* use top level element instead */
272 CFTypeRef refCFTopElement = 0;
273 refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
275 /* refCFTopElement points to an array of element dictionaries */
276 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refCFTopElement)};
277 CFArrayApplyFunction ((CFArrayRef)refCFTopElement, range, joystickTopLevelElementHandler, joyInfo);
281 CFRelease (usbProperties);
284 irr::os::Printer::log("IORegistryEntryCreateCFProperties failed to create usbProperties", irr::ELL_ERROR);
286 if (kIOReturnSuccess != IOObjectRelease (parent2))
287 irr::os::Printer::log("IOObjectRelease failed to release parent2", irr::ELL_ERROR);
288 if (kIOReturnSuccess != IOObjectRelease (parent1))
289 irr::os::Printer::log("IOObjectRelease failed to release parent1", irr::ELL_ERROR);
293 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
295 // Contents from Events.h from Carbon/HIToolbox but we need it with Cocoa too
296 // and for some reason no Cocoa equivalent of these constants seems provided.
297 // So I'm doing like everyone else and using copy-and-paste.
304 * These constants are the virtual keycodes defined originally in
305 * Inside Mac Volume V, pg. V-191. They identify physical keys on a
306 * keyboard. Those constants with "ANSI" in the name are labeled
307 * according to the key position on an ANSI-standard US keyboard.
308 * For example, kVK_ANSI_A indicates the virtual keycode for the key
309 * with the letter 'A' in the US keyboard layout. Other keyboard
310 * layouts may have the 'A' key label on a different physical key;
311 * in this case, pressing 'A' will generate a different virtual
338 kVK_ANSI_Equal = 0x18,
341 kVK_ANSI_Minus = 0x1B,
344 kVK_ANSI_RightBracket = 0x1E,
347 kVK_ANSI_LeftBracket = 0x21,
352 kVK_ANSI_Quote = 0x27,
354 kVK_ANSI_Semicolon = 0x29,
355 kVK_ANSI_Backslash = 0x2A,
356 kVK_ANSI_Comma = 0x2B,
357 kVK_ANSI_Slash = 0x2C,
360 kVK_ANSI_Period = 0x2F,
361 kVK_ANSI_Grave = 0x32,
362 kVK_ANSI_KeypadDecimal = 0x41,
363 kVK_ANSI_KeypadMultiply = 0x43,
364 kVK_ANSI_KeypadPlus = 0x45,
365 kVK_ANSI_KeypadClear = 0x47,
366 kVK_ANSI_KeypadDivide = 0x4B,
367 kVK_ANSI_KeypadEnter = 0x4C,
368 kVK_ANSI_KeypadMinus = 0x4E,
369 kVK_ANSI_KeypadEquals = 0x51,
370 kVK_ANSI_Keypad0 = 0x52,
371 kVK_ANSI_Keypad1 = 0x53,
372 kVK_ANSI_Keypad2 = 0x54,
373 kVK_ANSI_Keypad3 = 0x55,
374 kVK_ANSI_Keypad4 = 0x56,
375 kVK_ANSI_Keypad5 = 0x57,
376 kVK_ANSI_Keypad6 = 0x58,
377 kVK_ANSI_Keypad7 = 0x59,
378 kVK_ANSI_Keypad8 = 0x5B,
379 kVK_ANSI_Keypad9 = 0x5C
382 /* keycodes for keys that are independent of keyboard layout*/
394 kVK_RightShift = 0x3C,
395 kVK_RightOption = 0x3D,
396 kVK_RightControl = 0x3E,
400 kVK_VolumeDown = 0x49,
421 kVK_ForwardDelete = 0x75,
427 kVK_LeftArrow = 0x7B,
428 kVK_RightArrow = 0x7C,
429 kVK_DownArrow = 0x7D,
433 //------------------------------------------------------------------------------------------
434 Boolean GetDictionaryBoolean(CFDictionaryRef theDict, const void* key)
436 // get a boolean from the dictionary
437 Boolean value = false;
438 CFBooleanRef boolRef;
439 boolRef = (CFBooleanRef)CFDictionaryGetValue(theDict, key);
441 value = CFBooleanGetValue(boolRef);
444 //------------------------------------------------------------------------------------------
445 long GetDictionaryLong(CFDictionaryRef theDict, const void* key)
447 // get a long from the dictionary
450 numRef = (CFNumberRef)CFDictionaryGetValue(theDict, key);
452 CFNumberGetValue(numRef, kCFNumberLongType, &value);
460 IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& param, io::IFileSystem* io, IContextManager* contextManager);
462 } // end namespace irr
464 static bool firstLaunch = true;
466 @implementation CIrrDelegateOSX
468 irr::CIrrDeviceMacOSX* Device;
472 - (id)initWithDevice:(irr::CIrrDeviceMacOSX*)device
484 - (void)applicationDidFinishLaunching:(NSNotification*)notification
489 - (void)orderFrontStandardAboutPanel:(id)sender
491 [NSApp orderFrontStandardAboutPanel:sender];
494 - (void)unhideAllApplications:(id)sender
496 [NSApp unhideAllApplications:sender];
499 - (void)hide:(id)sender
504 - (void)hideOtherApplications:(id)sender
506 [NSApp hideOtherApplications:sender];
509 - (void)terminate:(id)sender
514 - (void)windowWillClose:(id)sender
516 Device->setWindow(nil);
520 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
522 if (Device->isResizable())
523 return proposedFrameSize;
525 return [window frame].size;
528 - (void)windowDidResize:(NSNotification *)aNotification
533 window = [aNotification object];
534 frame = [window frame];
535 Device->setResize((int)frame.size.width,(int)frame.size.height);
548 CIrrDeviceMacOSX::CIrrDeviceMacOSX(const SIrrlichtCreationParameters& param)
549 : CIrrDeviceStub(param), Window(NULL), Display(NULL),
550 DeviceWidth(0), DeviceHeight(0),
551 ScreenWidth(0), ScreenHeight(0), MouseButtonStates(0),
552 IsActive(true), IsFullscreen(false), IsShiftDown(false), IsControlDown(false), IsResizable(false)
557 setDebugName("CIrrDeviceMacOSX");
564 if (!CreationParams.WindowId)
566 [[NSAutoreleasePool alloc] init];
567 [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
568 [NSApp setDelegate:(id<NSApplicationDelegate>)[[[CIrrDelegateOSX alloc] initWithDevice:this] autorelease]];
572 NSString* bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
573 if (bundleName == nil)
574 bundleName = @"Irrlicht";
576 NSMenu* mainMenu = [[[NSMenu alloc] initWithTitle:@"MainMenu"] autorelease];
577 NSMenu* menu = [[[NSMenu alloc] initWithTitle:bundleName] autorelease];
578 NSMenuItem* menuItem = [mainMenu addItemWithTitle:bundleName action:nil keyEquivalent:@""];
579 [mainMenu setSubmenu:menu forItem:menuItem];
580 menuItem = [menu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
581 [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
583 [NSApp setMainMenu:mainMenu];
585 [NSApp finishLaunching];
588 NSString *path = [[NSBundle mainBundle] bundlePath];
590 path = [path stringByAppendingString:@"/Contents/Resources"];
591 chdir([path fileSystemRepresentation]);
597 Operator = new COSOperator(name.version);
598 os::Printer::log(name.version,ELL_INFORMATION);
604 if (CreationParams.DriverType != video::EDT_NULL)
605 success = createWindow();
607 // in case of failure, one can check VideoDriver for initialization
612 CursorControl = new CCursorControl(CreationParams.WindowSize, this);
618 CIrrDeviceMacOSX::~CIrrDeviceMacOSX()
620 [NSApp setPresentationOptions:(NSApplicationPresentationDefault)];
622 #if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
623 for (u32 joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
625 if (ActiveJoysticks[joystick].interface)
626 closeJoystickDevice(&ActiveJoysticks[joystick]);
631 void CIrrDeviceMacOSX::closeDevice()
635 [Window setIsVisible:FALSE];
636 [Window setReleasedWhenClosed:TRUE];
641 IsFullscreen = false;
645 bool CIrrDeviceMacOSX::createWindow()
649 Display = CGMainDisplayID();
652 CGDisplayModeRef displaymode, olddisplaymode;
654 ScreenWidth = (int)CGDisplayPixelsWide(Display);
655 ScreenHeight = (int)CGDisplayPixelsHigh(Display);
657 const NSBackingStoreType type = (CreationParams.DriverType == video::EDT_OPENGL) ? NSBackingStoreBuffered : NSBackingStoreNonretained;
660 //if (!CreationParams.Fullscreen)
662 if (!CreationParams.WindowId) //create another window when WindowId is null
664 int x = (CreationParams.WindowPosition.X > 0) ? CreationParams.WindowPosition.X : 0;
665 int y = (CreationParams.WindowPosition.Y > 0) ? CreationParams.WindowPosition.Y : 0;
667 if (CreationParams.WindowPosition.Y > -1)
669 int screenHeight = [[[NSScreen screens] objectAtIndex:0] frame].size.height;
670 y = screenHeight - y - CreationParams.WindowSize.Height;
673 Window = [[NSWindow alloc] initWithContentRect:NSMakeRect(x, y, CreationParams.WindowSize.Width,CreationParams.WindowSize.Height) styleMask:NSTitledWindowMask+NSClosableWindowMask+NSResizableWindowMask backing:type defer:FALSE];
675 if (CreationParams.WindowPosition.X == -1 && CreationParams.WindowPosition.Y == -1)
679 DeviceWidth = CreationParams.WindowSize.Width;
680 DeviceHeight = CreationParams.WindowSize.Height;
689 [Window setDelegate:(id<NSWindowDelegate>)[NSApp delegate]];
690 [Window setAcceptsMouseMovedEvents:TRUE];
691 [Window setIsVisible:TRUE];
692 [Window makeKeyAndOrderFront:nil];
699 void CIrrDeviceMacOSX::setResize(int width, int height)
701 // set new window size
703 DeviceHeight = height;
705 #if defined(_IRR_COMPILE_WITH_OPENGL_)
706 // update the size of the opengl rendering context
707 if (CreationParams.DriverType == video::EDT_OPENGL)
709 NSOpenGLContext* Context = (NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context;
716 // resize the driver to the inner pane size
719 NSRect driverFrame = [Window contentRectForFrameRect:[Window frame]];
720 getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)driverFrame.size.width, (s32)driverFrame.size.height));
721 DeviceWidth = (s32)driverFrame.size.width;
722 DeviceHeight = (s32)driverFrame.size.height;
725 getVideoDriver()->OnResize(core::dimension2d<u32>( (s32)width, (s32)height));
729 void CIrrDeviceMacOSX::createDriver()
731 switch (CreationParams.DriverType)
733 case video::EDT_OPENGL:
734 #ifdef _IRR_COMPILE_WITH_OPENGL_
736 video::SExposedVideoData data;
737 data.OpenGLOSX.Window = Window;
738 ContextManager = new video::CNSOGLManager();
739 ContextManager->initialize(CreationParams, data);
740 VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, ContextManager);
743 os::Printer::log("Could not create OpenGL driver.", ELL_ERROR);
748 [[Window contentView] setWantsBestResolutionOpenGLSurface:NO];
749 [(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context setView:[Window contentView]];
753 [(NSView*)CreationParams.WindowId setWantsBestResolutionOpenGLSurface:NO];
754 [(NSOpenGLContext*)ContextManager->getContext().OpenGLOSX.Context setView:(NSView*)CreationParams.WindowId];
758 os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
762 case video::DEPRECATED_EDT_DIRECT3D8_NO_LONGER_EXISTS:
763 case video::EDT_DIRECT3D9:
764 case video::EDT_OGLES1:
765 case video::EDT_OGLES2:
766 os::Printer::log("This driver is not available in OSX. Try OpenGL or Software renderer.", ELL_ERROR);
769 case video::EDT_NULL:
770 VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
774 os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
779 bool CIrrDeviceMacOSX::run()
781 NSAutoreleasePool* Pool = [[NSAutoreleasePool alloc] init];
787 storeMouseLocation();
789 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
792 bzero(&ievent,sizeof(ievent));
794 switch([(NSEvent *)event type])
797 postKeyEvent(event,ievent,true);
801 postKeyEvent(event,ievent,false);
805 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
806 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
807 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
809 if (IsShiftDown != ievent.KeyInput.Shift)
811 ievent.KeyInput.Char = irr::KEY_SHIFT;
812 ievent.KeyInput.Key = irr::KEY_SHIFT;
813 ievent.KeyInput.PressedDown = ievent.KeyInput.Shift;
815 IsShiftDown = ievent.KeyInput.Shift;
817 postEventFromUser(ievent);
820 if (IsControlDown != ievent.KeyInput.Control)
822 ievent.KeyInput.Char = irr::KEY_CONTROL;
823 ievent.KeyInput.Key = irr::KEY_CONTROL;
824 ievent.KeyInput.PressedDown = ievent.KeyInput.Control;
826 IsControlDown = ievent.KeyInput.Control;
828 postEventFromUser(ievent);
831 [NSApp sendEvent:event];
834 case NSLeftMouseDown:
835 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
836 ievent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
837 MouseButtonStates |= irr::EMBSM_LEFT;
838 ievent.MouseInput.ButtonStates = MouseButtonStates;
839 postMouseEvent(event,ievent);
843 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
844 MouseButtonStates &= !irr::EMBSM_LEFT;
845 ievent.MouseInput.ButtonStates = MouseButtonStates;
846 ievent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
847 postMouseEvent(event,ievent);
850 case NSOtherMouseDown:
851 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
852 ievent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
853 MouseButtonStates |= irr::EMBSM_MIDDLE;
854 ievent.MouseInput.ButtonStates = MouseButtonStates;
855 postMouseEvent(event,ievent);
859 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
860 MouseButtonStates &= !irr::EMBSM_MIDDLE;
861 ievent.MouseInput.ButtonStates = MouseButtonStates;
862 ievent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
863 postMouseEvent(event,ievent);
867 case NSLeftMouseDragged:
868 case NSRightMouseDragged:
869 case NSOtherMouseDragged:
870 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
871 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
872 ievent.MouseInput.ButtonStates = MouseButtonStates;
873 postMouseEvent(event,ievent);
876 case NSRightMouseDown:
877 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
878 ievent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
879 MouseButtonStates |= irr::EMBSM_RIGHT;
880 ievent.MouseInput.ButtonStates = MouseButtonStates;
881 postMouseEvent(event,ievent);
885 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
886 ievent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
887 MouseButtonStates &= !irr::EMBSM_RIGHT;
888 ievent.MouseInput.ButtonStates = MouseButtonStates;
889 postMouseEvent(event,ievent);
893 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
894 ievent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
895 ievent.MouseInput.Wheel = [(NSEvent *)event deltaY];
896 if (ievent.MouseInput.Wheel < 1.0f)
897 ievent.MouseInput.Wheel *= 10.0f;
899 ievent.MouseInput.Wheel *= 5.0f;
900 postMouseEvent(event,ievent);
904 [NSApp sendEvent:event];
913 return (![[NSApp delegate] isQuit] && IsActive);
917 //! Pause the current process for the minimum time allowed only to allow other processes to execute
918 void CIrrDeviceMacOSX::yield()
920 struct timespec ts = {0,0};
921 nanosleep(&ts, NULL);
925 //! Pause execution and let other processes to run for a specified amount of time.
926 void CIrrDeviceMacOSX::sleep(u32 timeMs, bool pauseTimer=false)
928 bool wasStopped = Timer ? Timer->isStopped() : true;
931 ts.tv_sec = (time_t) (timeMs / 1000);
932 ts.tv_nsec = (long) (timeMs % 1000) * 1000000;
934 if (pauseTimer && !wasStopped)
937 nanosleep(&ts, NULL);
939 if (pauseTimer && !wasStopped)
944 void CIrrDeviceMacOSX::setWindowCaption(const wchar_t* text)
950 size_t numBytes = wcslen(text) * sizeof(wchar_t);
952 #ifdef __BIG_ENDIAN__
953 NSStringEncoding encode = sizeof(wchar_t) == 4 ? NSUTF32BigEndianStringEncoding : NSUTF16BigEndianStringEncoding;
955 NSStringEncoding encode = sizeof(wchar_t) == 4 ? NSUTF32LittleEndianStringEncoding : NSUTF16LittleEndianStringEncoding;
957 NSString* name = [[NSString alloc] initWithBytes:text length:numBytes encoding:encode];
960 [Window setTitle:name];
966 [Window setTitle:@""];
972 bool CIrrDeviceMacOSX::isWindowActive() const
978 bool CIrrDeviceMacOSX::isWindowFocused() const
981 return [Window isKeyWindow];
986 bool CIrrDeviceMacOSX::isWindowMinimized() const
989 return [Window isMiniaturized];
994 void CIrrDeviceMacOSX::postKeyEvent(void *event,irr::SEvent &ievent,bool pressed)
997 std::map<int,int>::const_iterator iter;
998 unsigned int c,mkey,mchar;
999 const unsigned char *cStr;
1002 str = [(NSEvent *)event characters];
1003 if ((str != nil) && ([str length] > 0))
1006 skipCommand = false;
1007 c = [str characterAtIndex:0];
1010 iter = KeyCodes.find([(NSEvent *)event keyCode]);
1011 if (iter != KeyCodes.end())
1012 mkey = (*iter).second;
1013 else if ((iter = KeyCodes.find(c)) != KeyCodes.end())
1014 mkey = (*iter).second;
1017 // workaround for period character
1020 mkey = irr::KEY_PERIOD;
1025 cStr = (unsigned char *)[str cStringUsingEncoding:NSWindowsCP1252StringEncoding];
1026 if (cStr != NULL && strlen((char*)cStr) > 0)
1029 mkey = toupper(mchar);
1030 if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1032 if (mkey == 'C' || mkey == 'V' || mkey == 'X')
1042 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
1043 ievent.KeyInput.Key = (irr::EKEY_CODE)mkey;
1044 ievent.KeyInput.PressedDown = pressed;
1045 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1046 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1047 ievent.KeyInput.Char = mchar;
1050 ievent.KeyInput.Control = true;
1051 else if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1052 [NSApp sendEvent:(NSEvent *)event];
1054 postEventFromUser(ievent);
1059 void CIrrDeviceMacOSX::postMouseEvent(void *event,irr::SEvent &ievent)
1065 ievent.MouseInput.X = (int)[(NSEvent *)event locationInWindow].x;
1066 ievent.MouseInput.Y = DeviceHeight - (int)[(NSEvent *)event locationInWindow].y;
1068 if (ievent.MouseInput.Y < 0)
1073 CGEventRef ourEvent = CGEventCreate(NULL);
1074 CGPoint point = CGEventGetLocation(ourEvent);
1075 CFRelease(ourEvent);
1077 ievent.MouseInput.X = (int)point.x;
1078 ievent.MouseInput.Y = (int)point.y;
1080 if (ievent.MouseInput.Y < 0)
1086 ievent.MouseInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1087 ievent.MouseInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1089 postEventFromUser(ievent);
1092 [NSApp sendEvent:(NSEvent *)event];
1096 void CIrrDeviceMacOSX::storeMouseLocation()
1103 p = [NSEvent mouseLocation];
1104 p = [Window convertScreenToBase:p];
1106 y = DeviceHeight - (int)p.y;
1110 // Do we still need this?
1111 CGEventRef ourEvent = CGEventCreate(NULL);
1112 CGPoint point = CGEventGetLocation(ourEvent);
1113 CFRelease(ourEvent);
1118 const core::position2di& curr = ((CCursorControl *)CursorControl)->getPosition(true);
1119 if (curr.X != x || curr.Y != y)
1122 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1123 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1124 ievent.MouseInput.X = x;
1125 ievent.MouseInput.Y = y;
1126 postEventFromUser(ievent);
1130 ((CCursorControl *)CursorControl)->updateInternalCursorPosition(x,y);
1134 void CIrrDeviceMacOSX::setMouseLocation(int x,int y)
1141 // Irrlicht window exists
1143 p.y = (float) (DeviceHeight - y);
1144 p = [Window convertBaseToScreen:p];
1145 p.y = ScreenHeight - p.y;
1150 p.y = (float) y + (ScreenHeight - DeviceHeight);
1156 CGWarpMouseCursorPosition(c);
1157 CGAssociateMouseAndMouseCursorPosition(YES);
1161 void CIrrDeviceMacOSX::setCursorVisible(bool visible)
1164 CGDisplayShowCursor(CGMainDisplayID());
1166 CGDisplayHideCursor(CGMainDisplayID());
1170 void CIrrDeviceMacOSX::setWindow(NSWindow* window)
1176 void CIrrDeviceMacOSX::initKeycodes()
1178 KeyCodes[kVK_UpArrow] = irr::KEY_UP;
1179 KeyCodes[kVK_DownArrow] = irr::KEY_DOWN;
1180 KeyCodes[kVK_LeftArrow] = irr::KEY_LEFT;
1181 KeyCodes[kVK_RightArrow] = irr::KEY_RIGHT;
1182 KeyCodes[kVK_F1] = irr::KEY_F1;
1183 KeyCodes[kVK_F2] = irr::KEY_F2;
1184 KeyCodes[kVK_F3] = irr::KEY_F3;
1185 KeyCodes[kVK_F4] = irr::KEY_F4;
1186 KeyCodes[kVK_F5] = irr::KEY_F5;
1187 KeyCodes[kVK_F6] = irr::KEY_F6;
1188 KeyCodes[kVK_F7] = irr::KEY_F7;
1189 KeyCodes[kVK_F8] = irr::KEY_F8;
1190 KeyCodes[kVK_F9] = irr::KEY_F9;
1191 KeyCodes[kVK_F10] = irr::KEY_F10;
1192 KeyCodes[kVK_F11] = irr::KEY_F11;
1193 KeyCodes[kVK_F12] = irr::KEY_F12;
1194 KeyCodes[kVK_F13] = irr::KEY_F13;
1195 KeyCodes[kVK_F14] = irr::KEY_F14;
1196 KeyCodes[kVK_F15] = irr::KEY_F15;
1197 KeyCodes[kVK_F16] = irr::KEY_F16;
1198 KeyCodes[kVK_F17] = irr::KEY_F17;
1199 KeyCodes[kVK_F18] = irr::KEY_F18;
1200 KeyCodes[kVK_F19] = irr::KEY_F19;
1201 KeyCodes[kVK_F20] = irr::KEY_F20;
1202 KeyCodes[kVK_Home] = irr::KEY_HOME;
1203 KeyCodes[kVK_End] = irr::KEY_END;
1204 KeyCodes[NSInsertFunctionKey] = irr::KEY_INSERT;
1205 KeyCodes[kVK_ForwardDelete] = irr::KEY_DELETE;
1206 KeyCodes[kVK_Help] = irr::KEY_HELP;
1207 KeyCodes[NSSelectFunctionKey] = irr::KEY_SELECT;
1208 KeyCodes[NSPrintFunctionKey] = irr::KEY_PRINT;
1209 KeyCodes[NSExecuteFunctionKey] = irr::KEY_EXECUT;
1210 KeyCodes[NSPrintScreenFunctionKey] = irr::KEY_SNAPSHOT;
1211 KeyCodes[NSPauseFunctionKey] = irr::KEY_PAUSE;
1212 KeyCodes[NSScrollLockFunctionKey] = irr::KEY_SCROLL;
1213 KeyCodes[kVK_Delete] = irr::KEY_BACK;
1214 KeyCodes[kVK_Tab] = irr::KEY_TAB;
1215 KeyCodes[kVK_Return] = irr::KEY_RETURN;
1216 KeyCodes[kVK_Escape] = irr::KEY_ESCAPE;
1217 KeyCodes[kVK_Control] = irr::KEY_CONTROL;
1218 KeyCodes[kVK_RightControl] = irr::KEY_RCONTROL;
1219 KeyCodes[kVK_Command] = irr::KEY_MENU;
1220 KeyCodes[kVK_Shift] = irr::KEY_SHIFT;
1221 KeyCodes[kVK_RightShift] = irr::KEY_RSHIFT;
1222 KeyCodes[kVK_Space] = irr::KEY_SPACE;
1224 KeyCodes[kVK_ANSI_A] = irr::KEY_KEY_A;
1225 KeyCodes[kVK_ANSI_B] = irr::KEY_KEY_B;
1226 KeyCodes[kVK_ANSI_C] = irr::KEY_KEY_C;
1227 KeyCodes[kVK_ANSI_D] = irr::KEY_KEY_D;
1228 KeyCodes[kVK_ANSI_E] = irr::KEY_KEY_E;
1229 KeyCodes[kVK_ANSI_F] = irr::KEY_KEY_F;
1230 KeyCodes[kVK_ANSI_G] = irr::KEY_KEY_G;
1231 KeyCodes[kVK_ANSI_H] = irr::KEY_KEY_H;
1232 KeyCodes[kVK_ANSI_I] = irr::KEY_KEY_I;
1233 KeyCodes[kVK_ANSI_J] = irr::KEY_KEY_J;
1234 KeyCodes[kVK_ANSI_K] = irr::KEY_KEY_K;
1235 KeyCodes[kVK_ANSI_L] = irr::KEY_KEY_L;
1236 KeyCodes[kVK_ANSI_M] = irr::KEY_KEY_M;
1237 KeyCodes[kVK_ANSI_N] = irr::KEY_KEY_N;
1238 KeyCodes[kVK_ANSI_O] = irr::KEY_KEY_O;
1239 KeyCodes[kVK_ANSI_P] = irr::KEY_KEY_P;
1240 KeyCodes[kVK_ANSI_Q] = irr::KEY_KEY_Q;
1241 KeyCodes[kVK_ANSI_R] = irr::KEY_KEY_R;
1242 KeyCodes[kVK_ANSI_S] = irr::KEY_KEY_S;
1243 KeyCodes[kVK_ANSI_T] = irr::KEY_KEY_T;
1244 KeyCodes[kVK_ANSI_U] = irr::KEY_KEY_U;
1245 KeyCodes[kVK_ANSI_V] = irr::KEY_KEY_V;
1246 KeyCodes[kVK_ANSI_W] = irr::KEY_KEY_W;
1247 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1248 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1249 KeyCodes[kVK_ANSI_Y] = irr::KEY_KEY_Y;
1250 KeyCodes[kVK_ANSI_Z] = irr::KEY_KEY_Z;
1252 KeyCodes[kVK_ANSI_0] = irr::KEY_KEY_0;
1253 KeyCodes[kVK_ANSI_1] = irr::KEY_KEY_1;
1254 KeyCodes[kVK_ANSI_2] = irr::KEY_KEY_2;
1255 KeyCodes[kVK_ANSI_3] = irr::KEY_KEY_3;
1256 KeyCodes[kVK_ANSI_4] = irr::KEY_KEY_4;
1257 KeyCodes[kVK_ANSI_5] = irr::KEY_KEY_5;
1258 KeyCodes[kVK_ANSI_6] = irr::KEY_KEY_6;
1259 KeyCodes[kVK_ANSI_7] = irr::KEY_KEY_7;
1260 KeyCodes[kVK_ANSI_8] = irr::KEY_KEY_8;
1261 KeyCodes[kVK_ANSI_9] = irr::KEY_KEY_9;
1263 KeyCodes[kVK_ANSI_Slash] = irr::KEY_DIVIDE;
1264 KeyCodes[kVK_ANSI_Comma] = irr::KEY_COMMA;
1265 KeyCodes[kVK_ANSI_Period] = irr::KEY_PERIOD;
1266 KeyCodes[kVK_PageUp] = irr::KEY_PRIOR;
1267 KeyCodes[kVK_PageDown] = irr::KEY_NEXT;
1269 KeyCodes[kVK_ANSI_Keypad0] = irr::KEY_NUMPAD0;
1270 KeyCodes[kVK_ANSI_Keypad1] = irr::KEY_NUMPAD1;
1271 KeyCodes[kVK_ANSI_Keypad2] = irr::KEY_NUMPAD2;
1272 KeyCodes[kVK_ANSI_Keypad3] = irr::KEY_NUMPAD3;
1273 KeyCodes[kVK_ANSI_Keypad4] = irr::KEY_NUMPAD4;
1274 KeyCodes[kVK_ANSI_Keypad5] = irr::KEY_NUMPAD5;
1275 KeyCodes[kVK_ANSI_Keypad6] = irr::KEY_NUMPAD6;
1276 KeyCodes[kVK_ANSI_Keypad7] = irr::KEY_NUMPAD7;
1277 KeyCodes[kVK_ANSI_Keypad8] = irr::KEY_NUMPAD8;
1278 KeyCodes[kVK_ANSI_Keypad9] = irr::KEY_NUMPAD9;
1280 KeyCodes[kVK_ANSI_KeypadDecimal] = irr::KEY_DECIMAL;
1281 KeyCodes[kVK_ANSI_KeypadMultiply] = irr::KEY_MULTIPLY;
1282 KeyCodes[kVK_ANSI_KeypadPlus] = irr::KEY_PLUS;
1283 KeyCodes[kVK_ANSI_KeypadClear] = irr::KEY_OEM_CLEAR;
1284 KeyCodes[kVK_ANSI_KeypadDivide] = irr::KEY_DIVIDE;
1285 KeyCodes[kVK_ANSI_KeypadEnter] = irr::KEY_RETURN;
1286 KeyCodes[kVK_ANSI_KeypadMinus] = irr::KEY_SUBTRACT;
1288 KeyCodes[kVK_ANSI_LeftBracket] = irr::KEY_OEM_4;
1289 KeyCodes[kVK_ANSI_Backslash] = irr::KEY_OEM_5;
1290 KeyCodes[kVK_ANSI_RightBracket] = irr::KEY_OEM_6;
1294 //! Sets if the window should be resizable in windowed mode.
1295 void CIrrDeviceMacOSX::setResizable(bool resize)
1297 IsResizable = resize;
1300 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask];
1302 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask];
1307 bool CIrrDeviceMacOSX::isResizable() const
1313 void CIrrDeviceMacOSX::minimizeWindow()
1316 [Window miniaturize:[NSApp self]];
1320 //! Maximizes the window if possible.
1321 void CIrrDeviceMacOSX::maximizeWindow()
1327 //! get the window to normal size if possible.
1328 void CIrrDeviceMacOSX::restoreWindow()
1330 [Window deminiaturize:[NSApp self]];
1333 //! Get the position of this window on screen
1334 core::position2di CIrrDeviceMacOSX::getWindowPosition()
1336 NSRect rect = [Window frame];
1337 int screenHeight = [[[NSScreen screens] objectAtIndex:0] frame].size.height;
1338 return core::position2di(rect.origin.x, screenHeight - rect.origin.y - rect.size.height);
1342 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1343 static void joystickRemovalCallback(void * target,
1344 IOReturn result, void * refcon, void * sender)
1346 JoystickInfo *joy = (JoystickInfo *) refcon;
1349 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1352 bool CIrrDeviceMacOSX::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
1354 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1355 ActiveJoysticks.clear();
1356 joystickInfo.clear();
1358 io_object_t hidObject = 0;
1359 io_iterator_t hidIterator = 0;
1360 IOReturn result = kIOReturnSuccess;
1361 mach_port_t masterPort = 0;
1362 CFMutableDictionaryRef hidDictionaryRef = NULL;
1364 result = IOMasterPort (bootstrap_port, &masterPort);
1365 if (kIOReturnSuccess != result)
1367 os::Printer::log("initialiseJoysticks IOMasterPort failed", ELL_ERROR);
1371 hidDictionaryRef = IOServiceMatching (kIOHIDDeviceKey);
1372 if (!hidDictionaryRef)
1374 os::Printer::log("initialiseJoysticks IOServiceMatching failed", ELL_ERROR);
1377 result = IOServiceGetMatchingServices (masterPort, hidDictionaryRef, &hidIterator);
1379 if (kIOReturnSuccess != result)
1381 os::Printer::log("initialiseJoysticks IOServiceGetMatchingServices failed", ELL_ERROR);
1385 //no joysticks just return
1390 while ((hidObject = IOIteratorNext (hidIterator)))
1394 // get dictionary for HID properties
1395 CFMutableDictionaryRef hidProperties = 0;
1397 kern_return_t kern_result = IORegistryEntryCreateCFProperties (hidObject, &hidProperties, kCFAllocatorDefault, kNilOptions);
1398 if ((kern_result == KERN_SUCCESS) && hidProperties)
1400 HRESULT plugInResult = S_OK;
1402 IOCFPlugInInterface ** ppPlugInInterface = NULL;
1403 result = IOCreatePlugInInterfaceForService (hidObject, kIOHIDDeviceUserClientTypeID,
1404 kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
1405 if (kIOReturnSuccess == result)
1407 plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
1408 CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void **) &(info.interface));
1409 if (plugInResult != S_OK)
1410 os::Printer::log("initialiseJoysticks query HID class device interface failed", ELL_ERROR);
1411 (*ppPlugInInterface)->Release(ppPlugInInterface);
1416 if (info.interface != NULL)
1418 result = (*(info.interface))->open (info.interface, 0);
1419 if (result == kIOReturnSuccess)
1421 (*(info.interface))->setRemovalCallback (info.interface, joystickRemovalCallback, &info, &info);
1422 getJoystickDeviceInfo(hidObject, hidProperties, &info);
1425 CFTypeRef refElementTop = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
1428 CFTypeID type = CFGetTypeID (refElementTop);
1429 if (type == CFArrayGetTypeID())
1431 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
1432 info.numActiveJoysticks = ActiveJoysticks.size();
1433 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, &info);
1439 CFRelease (hidProperties);
1440 os::Printer::log("initialiseJoysticks Open interface failed", ELL_ERROR);
1444 CFRelease (hidProperties);
1446 result = IOObjectRelease (hidObject);
1448 if ( (info.usagePage != kHIDPage_GenericDesktop) ||
1449 ((info.usage != kHIDUsage_GD_Joystick &&
1450 info.usage != kHIDUsage_GD_GamePad &&
1451 info.usage != kHIDUsage_GD_MultiAxisController)) )
1453 closeJoystickDevice (&info);
1457 for (u32 i = 0; i < 6; ++i)
1458 info.persistentData.JoystickEvent.Axis[i] = 0;
1460 ActiveJoysticks.push_back(info);
1462 SJoystickInfo returnInfo;
1463 returnInfo.Joystick = jindex;
1464 returnInfo.Axes = info.axes;
1465 //returnInfo.Hats = info.hats;
1466 returnInfo.Buttons = info.buttons;
1467 returnInfo.Name = info.joystickName;
1468 returnInfo.PovHat = SJoystickInfo::POV_HAT_UNKNOWN;
1471 //if (info.hatComp.size())
1472 // returnInfo.PovHat = SJoystickInfo::POV_HAT_PRESENT;
1474 // returnInfo.PovHat = SJoystickInfo::POV_HAT_ABSENT;
1476 joystickInfo.push_back(returnInfo);
1485 result = IOObjectRelease (hidIterator);
1488 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1493 void CIrrDeviceMacOSX::pollJoysticks()
1495 #if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1496 if(0 == ActiveJoysticks.size())
1500 for (joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
1502 if (ActiveJoysticks[joystick].removed)
1506 ActiveJoysticks[joystick].persistentData.JoystickEvent.Joystick = joystick;
1508 if (ActiveJoysticks[joystick].interface)
1510 for (u32 n = 0; n < ActiveJoysticks[joystick].axisComp.size(); n++)
1512 IOReturn result = kIOReturnSuccess;
1513 IOHIDEventStruct hidEvent;
1515 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].axisComp[n].cookie, &hidEvent);
1516 if (kIOReturnSuccess == result)
1518 const f32 min = -32768.0f;
1519 const f32 max = 32767.0f;
1520 const f32 deviceScale = max - min;
1521 const f32 readScale = (f32)ActiveJoysticks[joystick].axisComp[n].maxRead - (f32)ActiveJoysticks[joystick].axisComp[n].minRead;
1523 if (hidEvent.value < ActiveJoysticks[joystick].axisComp[n].minRead)
1524 ActiveJoysticks[joystick].axisComp[n].minRead = hidEvent.value;
1525 if (hidEvent.value > ActiveJoysticks[joystick].axisComp[n].maxRead)
1526 ActiveJoysticks[joystick].axisComp[n].maxRead = hidEvent.value;
1528 if (readScale != 0.0f)
1529 hidEvent.value = (int)(((f32)((f32)hidEvent.value - (f32)ActiveJoysticks[joystick].axisComp[n].minRead) * deviceScale / readScale) + min);
1531 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] != (s16)hidEvent.value)
1533 ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] = (s16)hidEvent.value;
1537 for (u32 n = 0; n < ActiveJoysticks[joystick].buttonComp.size(); n++)
1539 IOReturn result = kIOReturnSuccess;
1540 IOHIDEventStruct hidEvent;
1542 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].buttonComp[n].cookie, &hidEvent);
1543 if (kIOReturnSuccess == result)
1545 if (hidEvent.value && !((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false) )
1547 else if (!hidEvent.value && ((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false))
1551 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates |= (1 << n);
1553 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates &= ~(1 << n);
1556 //still ToDo..will be done soon :)
1558 for (u32 n = 0; n < ActiveJoysticks[joystick].hatComp.size(); n++)
1560 IOReturn result = kIOReturnSuccess;
1561 IOHIDEventStruct hidEvent;
1563 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].hatComp[n].cookie, &hidEvent);
1564 if (kIOReturnSuccess == result)
1566 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.POV != hidEvent.value)
1568 ActiveJoysticks[joystick].persistentData.JoystickEvent.POV = hidEvent.value;
1575 postEventFromUser(ActiveJoysticks[joystick].persistentData);
1577 #endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1582 #endif // _IRR_COMPILE_WITH_OSX_DEVICE_