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