aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/MacOSX/CIrrDeviceMacOSX.mm
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/MacOSX/CIrrDeviceMacOSX.mm1892
1 files changed, 1892 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/MacOSX/CIrrDeviceMacOSX.mm b/libraries/irrlicht-1.8/source/Irrlicht/MacOSX/CIrrDeviceMacOSX.mm
new file mode 100644
index 0000000..43453f4
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/MacOSX/CIrrDeviceMacOSX.mm
@@ -0,0 +1,1892 @@
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
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 {
962 glFinish();
963 CGLFlushDrawable(CGLContext);
964 }
965}
966
967bool CIrrDeviceMacOSX::run()
968{
969 NSAutoreleasePool* Pool = [[NSAutoreleasePool alloc] init];
970
971 NSEvent *event;
972 irr::SEvent ievent;
973
974 os::Timer::tick();
975 storeMouseLocation();
976
977 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
978 if (event != nil)
979 {
980 bzero(&ievent,sizeof(ievent));
981
982 switch([(NSEvent *)event type])
983 {
984 case NSKeyDown:
985 postKeyEvent(event,ievent,true);
986 break;
987
988 case NSKeyUp:
989 postKeyEvent(event,ievent,false);
990 break;
991
992 case NSFlagsChanged:
993 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
994 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
995 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
996
997 if (IsShiftDown != ievent.KeyInput.Shift)
998 {
999 ievent.KeyInput.Char = irr::KEY_SHIFT;
1000 ievent.KeyInput.Key = irr::KEY_SHIFT;
1001 ievent.KeyInput.PressedDown = ievent.KeyInput.Shift;
1002
1003 IsShiftDown = ievent.KeyInput.Shift;
1004
1005 postEventFromUser(ievent);
1006 }
1007
1008 if (IsControlDown != ievent.KeyInput.Control)
1009 {
1010 ievent.KeyInput.Char = irr::KEY_CONTROL;
1011 ievent.KeyInput.Key = irr::KEY_CONTROL;
1012 ievent.KeyInput.PressedDown = ievent.KeyInput.Control;
1013
1014 IsControlDown = ievent.KeyInput.Control;
1015
1016 postEventFromUser(ievent);
1017 }
1018
1019 [NSApp sendEvent:event];
1020 break;
1021
1022 case NSLeftMouseDown:
1023 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1024 ievent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
1025 MouseButtonStates |= irr::EMBSM_LEFT;
1026 ievent.MouseInput.ButtonStates = MouseButtonStates;
1027 postMouseEvent(event,ievent);
1028 break;
1029
1030 case NSLeftMouseUp:
1031 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1032 MouseButtonStates &= !irr::EMBSM_LEFT;
1033 ievent.MouseInput.ButtonStates = MouseButtonStates;
1034 ievent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
1035 postMouseEvent(event,ievent);
1036 break;
1037
1038 case NSOtherMouseDown:
1039 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1040 ievent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
1041 MouseButtonStates |= irr::EMBSM_MIDDLE;
1042 ievent.MouseInput.ButtonStates = MouseButtonStates;
1043 postMouseEvent(event,ievent);
1044 break;
1045
1046 case NSOtherMouseUp:
1047 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1048 MouseButtonStates &= !irr::EMBSM_MIDDLE;
1049 ievent.MouseInput.ButtonStates = MouseButtonStates;
1050 ievent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
1051 postMouseEvent(event,ievent);
1052 break;
1053
1054 case NSMouseMoved:
1055 case NSLeftMouseDragged:
1056 case NSRightMouseDragged:
1057 case NSOtherMouseDragged:
1058 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1059 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1060 ievent.MouseInput.ButtonStates = MouseButtonStates;
1061 postMouseEvent(event,ievent);
1062 break;
1063
1064 case NSRightMouseDown:
1065 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1066 ievent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
1067 MouseButtonStates |= irr::EMBSM_RIGHT;
1068 ievent.MouseInput.ButtonStates = MouseButtonStates;
1069 postMouseEvent(event,ievent);
1070 break;
1071
1072 case NSRightMouseUp:
1073 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1074 ievent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
1075 MouseButtonStates &= !irr::EMBSM_RIGHT;
1076 ievent.MouseInput.ButtonStates = MouseButtonStates;
1077 postMouseEvent(event,ievent);
1078 break;
1079
1080 case NSScrollWheel:
1081 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1082 ievent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
1083 ievent.MouseInput.Wheel = [(NSEvent *)event deltaY];
1084 if (ievent.MouseInput.Wheel < 1.0f)
1085 ievent.MouseInput.Wheel *= 10.0f;
1086 else
1087 ievent.MouseInput.Wheel *= 5.0f;
1088 postMouseEvent(event,ievent);
1089 break;
1090
1091 default:
1092 [NSApp sendEvent:event];
1093 break;
1094 }
1095 }
1096
1097 pollJoysticks();
1098
1099 [Pool release];
1100
1101 return (![[NSApp delegate] isQuit] && IsActive);
1102}
1103
1104
1105//! Pause the current process for the minimum time allowed only to allow other processes to execute
1106void CIrrDeviceMacOSX::yield()
1107{
1108 struct timespec ts = {0,0};
1109 nanosleep(&ts, NULL);
1110}
1111
1112
1113//! Pause execution and let other processes to run for a specified amount of time.
1114void CIrrDeviceMacOSX::sleep(u32 timeMs, bool pauseTimer=false)
1115{
1116 bool wasStopped = Timer ? Timer->isStopped() : true;
1117
1118 struct timespec ts;
1119 ts.tv_sec = (time_t) (timeMs / 1000);
1120 ts.tv_nsec = (long) (timeMs % 1000) * 1000000;
1121
1122 if (pauseTimer && !wasStopped)
1123 Timer->stop();
1124
1125 nanosleep(&ts, NULL);
1126
1127 if (pauseTimer && !wasStopped)
1128 Timer->start();
1129}
1130
1131
1132void CIrrDeviceMacOSX::setWindowCaption(const wchar_t* text)
1133{
1134 size_t size;
1135 char title[1024];
1136
1137 if (Window != NULL)
1138 {
1139 size = wcstombs(title,text,1024);
1140 title[1023] = 0;
1141#ifdef __MAC_10_6
1142 NSString* name = [NSString stringWithCString:title encoding:NSUTF8StringEncoding];
1143#else
1144 NSString* name = [NSString stringWithCString:title length:size];
1145#endif
1146 [Window setTitle:name];
1147 [name release];
1148 }
1149}
1150
1151
1152bool CIrrDeviceMacOSX::isWindowActive() const
1153{
1154 return (IsActive);
1155}
1156
1157
1158bool CIrrDeviceMacOSX::isWindowFocused() const
1159{
1160 if (Window != NULL)
1161 return [Window isKeyWindow];
1162 return false;
1163}
1164
1165
1166bool CIrrDeviceMacOSX::isWindowMinimized() const
1167{
1168 if (Window != NULL)
1169 return [Window isMiniaturized];
1170 return false;
1171}
1172
1173
1174void CIrrDeviceMacOSX::postKeyEvent(void *event,irr::SEvent &ievent,bool pressed)
1175{
1176 NSString *str;
1177 std::map<int,int>::const_iterator iter;
1178 unsigned int result,c,mkey,mchar;
1179 const unsigned char *cStr;
1180 BOOL skipCommand;
1181
1182 str = [(NSEvent *)event characters];
1183 if ((str != nil) && ([str length] > 0))
1184 {
1185 mkey = mchar = 0;
1186 skipCommand = false;
1187 c = [str characterAtIndex:0];
1188 mchar = c;
1189
1190 iter = KeyCodes.find([(NSEvent *)event keyCode]);
1191 if (iter != KeyCodes.end())
1192 mkey = (*iter).second;
1193 else if ((iter = KeyCodes.find(c)) != KeyCodes.end())
1194 mkey = (*iter).second;
1195 else
1196 {
1197 // workaround for period character
1198 if (c == 0x2E)
1199 {
1200 mkey = irr::KEY_PERIOD;
1201 mchar = '.';
1202 }
1203 else
1204 {
1205 cStr = (unsigned char *)[str cStringUsingEncoding:NSWindowsCP1252StringEncoding];
1206 if (cStr != NULL && strlen((char*)cStr) > 0)
1207 {
1208 mchar = cStr[0];
1209 mkey = toupper(mchar);
1210 if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1211 {
1212 if (mkey == 'C' || mkey == 'V' || mkey == 'X')
1213 {
1214 mchar = 0;
1215 skipCommand = true;
1216 }
1217 }
1218 }
1219 }
1220 }
1221
1222 ievent.EventType = irr::EET_KEY_INPUT_EVENT;
1223 ievent.KeyInput.Key = (irr::EKEY_CODE)mkey;
1224 ievent.KeyInput.PressedDown = pressed;
1225 ievent.KeyInput.Shift = ([(NSEvent *)event modifierFlags] & NSShiftKeyMask) != 0;
1226 ievent.KeyInput.Control = ([(NSEvent *)event modifierFlags] & NSControlKeyMask) != 0;
1227 ievent.KeyInput.Char = mchar;
1228
1229 if (skipCommand)
1230 ievent.KeyInput.Control = true;
1231 else if ([(NSEvent *)event modifierFlags] & NSCommandKeyMask)
1232 [NSApp sendEvent:(NSEvent *)event];
1233
1234 postEventFromUser(ievent);
1235 }
1236}
1237
1238
1239void CIrrDeviceMacOSX::postMouseEvent(void *event,irr::SEvent &ievent)
1240{
1241 bool post = true;
1242
1243 if (Window != NULL)
1244 {
1245 ievent.MouseInput.X = (int)[(NSEvent *)event locationInWindow].x;
1246 ievent.MouseInput.Y = DeviceHeight - (int)[(NSEvent *)event locationInWindow].y;
1247
1248 if (ievent.MouseInput.Y < 0)
1249 post = false;
1250 }
1251 else
1252 {
1253 CGEventRef ourEvent = CGEventCreate(NULL);
1254 CGPoint point = CGEventGetLocation(ourEvent);
1255 CFRelease(ourEvent);
1256
1257 ievent.MouseInput.X = (int)point.x;
1258 ievent.MouseInput.Y = (int)point.y;
1259
1260 if (ievent.MouseInput.Y < 0)
1261 post = false;
1262 }
1263
1264 if (post)
1265 postEventFromUser(ievent);
1266
1267 [NSApp sendEvent:(NSEvent *)event];
1268}
1269
1270
1271void CIrrDeviceMacOSX::storeMouseLocation()
1272{
1273 int x,y;
1274
1275 if (Window != NULL)
1276 {
1277 NSPoint p;
1278 p = [NSEvent mouseLocation];
1279 p = [Window convertScreenToBase:p];
1280 x = (int)p.x;
1281 y = DeviceHeight - (int)p.y;
1282 }
1283 else
1284 {
1285 CGEventRef ourEvent = CGEventCreate(NULL);
1286 CGPoint point = CGEventGetLocation(ourEvent);
1287 CFRelease(ourEvent);
1288
1289 x = (int)point.x;
1290 y = (int)point.y;
1291
1292 const core::position2di& curr = ((CCursorControl *)CursorControl)->getPosition();
1293 if (curr.X != x || curr.Y != y)
1294 {
1295 // In fullscreen mode, events are not sent regularly so rely on polling
1296 irr::SEvent ievent;
1297 ievent.EventType = irr::EET_MOUSE_INPUT_EVENT;
1298 ievent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
1299 ievent.MouseInput.X = x;
1300 ievent.MouseInput.Y = y;
1301 postEventFromUser(ievent);
1302 }
1303 }
1304
1305 ((CCursorControl *)CursorControl)->updateInternalCursorPosition(x,y);
1306}
1307
1308
1309void CIrrDeviceMacOSX::setMouseLocation(int x,int y)
1310{
1311 NSPoint p;
1312 CGPoint c;
1313
1314 if (Window != NULL)
1315 {
1316 // Irrlicht window exists
1317 p.x = (float) x;
1318 p.y = (float) (DeviceHeight - y);
1319 p = [Window convertBaseToScreen:p];
1320 p.y = ScreenHeight - p.y;
1321 }
1322 else
1323 {
1324 p.x = (float) x;
1325 p.y = (float) y + (ScreenHeight - DeviceHeight);
1326 }
1327
1328 c.x = p.x;
1329 c.y = p.y;
1330
1331#ifdef __MAC_10_6
1332 /*CGEventSourceRef SourceRef = CGEventSourceCreate(0);
1333 CGEventSourceSetLocalEventsSuppressionInterval(SourceRef, 0);
1334 CFRelease(SourceRef);*/
1335 CGSetLocalEventsSuppressionInterval(0);
1336#else
1337 CGSetLocalEventsSuppressionInterval(0);
1338#endif
1339 CGWarpMouseCursorPosition(c);
1340}
1341
1342
1343void CIrrDeviceMacOSX::setCursorVisible(bool visible)
1344{
1345 if (visible)
1346 CGDisplayShowCursor(CGMainDisplayID());
1347 else
1348 CGDisplayHideCursor(CGMainDisplayID());
1349}
1350
1351
1352void CIrrDeviceMacOSX::initKeycodes()
1353{
1354 KeyCodes[kVK_UpArrow] = irr::KEY_UP;
1355 KeyCodes[kVK_DownArrow] = irr::KEY_DOWN;
1356 KeyCodes[kVK_LeftArrow] = irr::KEY_LEFT;
1357 KeyCodes[kVK_RightArrow] = irr::KEY_RIGHT;
1358 KeyCodes[kVK_F1] = irr::KEY_F1;
1359 KeyCodes[kVK_F2] = irr::KEY_F2;
1360 KeyCodes[kVK_F3] = irr::KEY_F3;
1361 KeyCodes[kVK_F4] = irr::KEY_F4;
1362 KeyCodes[kVK_F5] = irr::KEY_F5;
1363 KeyCodes[kVK_F6] = irr::KEY_F6;
1364 KeyCodes[kVK_F7] = irr::KEY_F7;
1365 KeyCodes[kVK_F8] = irr::KEY_F8;
1366 KeyCodes[kVK_F9] = irr::KEY_F9;
1367 KeyCodes[kVK_F10] = irr::KEY_F10;
1368 KeyCodes[kVK_F11] = irr::KEY_F11;
1369 KeyCodes[kVK_F12] = irr::KEY_F12;
1370 KeyCodes[kVK_F13] = irr::KEY_F13;
1371 KeyCodes[kVK_F14] = irr::KEY_F14;
1372 KeyCodes[kVK_F15] = irr::KEY_F15;
1373 KeyCodes[kVK_F16] = irr::KEY_F16;
1374 KeyCodes[kVK_F17] = irr::KEY_F17;
1375 KeyCodes[kVK_F18] = irr::KEY_F18;
1376 KeyCodes[kVK_F19] = irr::KEY_F19;
1377 KeyCodes[kVK_F20] = irr::KEY_F20;
1378 KeyCodes[kVK_Home] = irr::KEY_HOME;
1379 KeyCodes[kVK_End] = irr::KEY_END;
1380 KeyCodes[NSInsertFunctionKey] = irr::KEY_INSERT;
1381 KeyCodes[kVK_ForwardDelete] = irr::KEY_DELETE;
1382 KeyCodes[kVK_Help] = irr::KEY_HELP;
1383 KeyCodes[NSSelectFunctionKey] = irr::KEY_SELECT;
1384 KeyCodes[NSPrintFunctionKey] = irr::KEY_PRINT;
1385 KeyCodes[NSExecuteFunctionKey] = irr::KEY_EXECUT;
1386 KeyCodes[NSPrintScreenFunctionKey] = irr::KEY_SNAPSHOT;
1387 KeyCodes[NSPauseFunctionKey] = irr::KEY_PAUSE;
1388 KeyCodes[NSScrollLockFunctionKey] = irr::KEY_SCROLL;
1389 KeyCodes[kVK_Delete] = irr::KEY_BACK;
1390 KeyCodes[kVK_Tab] = irr::KEY_TAB;
1391 KeyCodes[kVK_Return] = irr::KEY_RETURN;
1392 KeyCodes[kVK_Escape] = irr::KEY_ESCAPE;
1393 KeyCodes[kVK_Control] = irr::KEY_CONTROL;
1394 KeyCodes[kVK_RightControl] = irr::KEY_RCONTROL;
1395 KeyCodes[kVK_Command] = irr::KEY_MENU;
1396 KeyCodes[kVK_Shift] = irr::KEY_SHIFT;
1397 KeyCodes[kVK_RightShift] = irr::KEY_RSHIFT;
1398 KeyCodes[kVK_Space] = irr::KEY_SPACE;
1399
1400 KeyCodes[kVK_ANSI_A] = irr::KEY_KEY_A;
1401 KeyCodes[kVK_ANSI_B] = irr::KEY_KEY_B;
1402 KeyCodes[kVK_ANSI_C] = irr::KEY_KEY_C;
1403 KeyCodes[kVK_ANSI_D] = irr::KEY_KEY_D;
1404 KeyCodes[kVK_ANSI_E] = irr::KEY_KEY_E;
1405 KeyCodes[kVK_ANSI_F] = irr::KEY_KEY_F;
1406 KeyCodes[kVK_ANSI_G] = irr::KEY_KEY_G;
1407 KeyCodes[kVK_ANSI_H] = irr::KEY_KEY_H;
1408 KeyCodes[kVK_ANSI_I] = irr::KEY_KEY_I;
1409 KeyCodes[kVK_ANSI_J] = irr::KEY_KEY_J;
1410 KeyCodes[kVK_ANSI_K] = irr::KEY_KEY_K;
1411 KeyCodes[kVK_ANSI_L] = irr::KEY_KEY_L;
1412 KeyCodes[kVK_ANSI_M] = irr::KEY_KEY_M;
1413 KeyCodes[kVK_ANSI_N] = irr::KEY_KEY_N;
1414 KeyCodes[kVK_ANSI_O] = irr::KEY_KEY_O;
1415 KeyCodes[kVK_ANSI_P] = irr::KEY_KEY_P;
1416 KeyCodes[kVK_ANSI_Q] = irr::KEY_KEY_Q;
1417 KeyCodes[kVK_ANSI_R] = irr::KEY_KEY_R;
1418 KeyCodes[kVK_ANSI_S] = irr::KEY_KEY_S;
1419 KeyCodes[kVK_ANSI_T] = irr::KEY_KEY_T;
1420 KeyCodes[kVK_ANSI_U] = irr::KEY_KEY_U;
1421 KeyCodes[kVK_ANSI_V] = irr::KEY_KEY_V;
1422 KeyCodes[kVK_ANSI_W] = irr::KEY_KEY_W;
1423 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1424 KeyCodes[kVK_ANSI_X] = irr::KEY_KEY_X;
1425 KeyCodes[kVK_ANSI_Y] = irr::KEY_KEY_Y;
1426 KeyCodes[kVK_ANSI_Z] = irr::KEY_KEY_Z;
1427
1428 KeyCodes[kVK_ANSI_0] = irr::KEY_KEY_0;
1429 KeyCodes[kVK_ANSI_1] = irr::KEY_KEY_1;
1430 KeyCodes[kVK_ANSI_2] = irr::KEY_KEY_2;
1431 KeyCodes[kVK_ANSI_3] = irr::KEY_KEY_3;
1432 KeyCodes[kVK_ANSI_4] = irr::KEY_KEY_4;
1433 KeyCodes[kVK_ANSI_5] = irr::KEY_KEY_5;
1434 KeyCodes[kVK_ANSI_6] = irr::KEY_KEY_6;
1435 KeyCodes[kVK_ANSI_7] = irr::KEY_KEY_7;
1436 KeyCodes[kVK_ANSI_8] = irr::KEY_KEY_8;
1437 KeyCodes[kVK_ANSI_9] = irr::KEY_KEY_9;
1438
1439 KeyCodes[kVK_ANSI_Slash] = irr::KEY_DIVIDE;
1440 KeyCodes[kVK_ANSI_Comma] = irr::KEY_COMMA;
1441 KeyCodes[kVK_ANSI_Period] = irr::KEY_PERIOD;
1442 KeyCodes[kVK_PageUp] = irr::KEY_PRIOR;
1443 KeyCodes[kVK_PageDown] = irr::KEY_NEXT;
1444
1445 KeyCodes[kVK_ANSI_Keypad0] = irr::KEY_NUMPAD0;
1446 KeyCodes[kVK_ANSI_Keypad1] = irr::KEY_NUMPAD1;
1447 KeyCodes[kVK_ANSI_Keypad2] = irr::KEY_NUMPAD2;
1448 KeyCodes[kVK_ANSI_Keypad3] = irr::KEY_NUMPAD3;
1449 KeyCodes[kVK_ANSI_Keypad4] = irr::KEY_NUMPAD4;
1450 KeyCodes[kVK_ANSI_Keypad5] = irr::KEY_NUMPAD5;
1451 KeyCodes[kVK_ANSI_Keypad6] = irr::KEY_NUMPAD6;
1452 KeyCodes[kVK_ANSI_Keypad7] = irr::KEY_NUMPAD7;
1453 KeyCodes[kVK_ANSI_Keypad8] = irr::KEY_NUMPAD8;
1454 KeyCodes[kVK_ANSI_Keypad9] = irr::KEY_NUMPAD9;
1455
1456 KeyCodes[kVK_ANSI_KeypadDecimal] = irr::KEY_DECIMAL;
1457 KeyCodes[kVK_ANSI_KeypadMultiply] = irr::KEY_MULTIPLY;
1458 KeyCodes[kVK_ANSI_KeypadPlus] = irr::KEY_PLUS;
1459 KeyCodes[kVK_ANSI_KeypadClear] = irr::KEY_OEM_CLEAR;
1460 KeyCodes[kVK_ANSI_KeypadDivide] = irr::KEY_DIVIDE;
1461 KeyCodes[kVK_ANSI_KeypadEnter] = irr::KEY_RETURN;
1462 KeyCodes[kVK_ANSI_KeypadMinus] = irr::KEY_SUBTRACT;
1463}
1464
1465
1466//! Sets if the window should be resizable in windowed mode.
1467void CIrrDeviceMacOSX::setResizable(bool resize)
1468{
1469 IsResizable = resize;
1470#if 0
1471 if (resize)
1472 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask];
1473 else
1474 [Window setStyleMask:NSTitledWindowMask|NSClosableWindowMask];
1475#endif
1476}
1477
1478
1479bool CIrrDeviceMacOSX::isResizable() const
1480{
1481 return IsResizable;
1482}
1483
1484
1485void CIrrDeviceMacOSX::minimizeWindow()
1486{
1487 if (Window != NULL)
1488 [Window miniaturize:[NSApp self]];
1489}
1490
1491
1492//! Maximizes the window if possible.
1493void CIrrDeviceMacOSX::maximizeWindow()
1494{
1495 // todo: implement
1496}
1497
1498
1499//! Restore the window to normal size if possible.
1500void CIrrDeviceMacOSX::restoreWindow()
1501{
1502 [Window deminiaturize:[NSApp self]];
1503}
1504
1505
1506bool CIrrDeviceMacOSX::present(video::IImage* surface, void* windowId, core::rect<s32>* src )
1507{
1508 // todo: implement window ID and src rectangle
1509
1510 if (!surface)
1511 return false;
1512
1513 if (SoftwareRendererType > 0)
1514 {
1515 const u32 colorSamples=3;
1516 // do we need to change the size?
1517 const bool updateSize = !SoftwareDriverTarget ||
1518 s32([SoftwareDriverTarget size].width) != surface->getDimension().Width ||
1519 s32([SoftwareDriverTarget size].height) != surface->getDimension().Height;
1520
1521 NSRect areaRect = NSMakeRect(0.0, 0.0, surface->getDimension().Width, surface->getDimension().Height);
1522 const u32 destPitch = (colorSamples * areaRect.size.width);
1523
1524 // create / update the target
1525 if (updateSize)
1526 {
1527 [SoftwareDriverTarget release];
1528 // allocate target for IImage
1529 SoftwareDriverTarget = [[NSBitmapImageRep alloc]
1530 initWithBitmapDataPlanes: nil
1531 pixelsWide: areaRect.size.width
1532 pixelsHigh: areaRect.size.height
1533 bitsPerSample: 8
1534 samplesPerPixel: colorSamples
1535 hasAlpha: NO
1536 isPlanar: NO
1537 colorSpaceName: NSCalibratedRGBColorSpace
1538 bytesPerRow: destPitch
1539 bitsPerPixel: 8*colorSamples];
1540 }
1541
1542 if (SoftwareDriverTarget==nil)
1543 return false;
1544
1545 // get pointer to image data
1546 unsigned char* imgData = (unsigned char*)surface->lock();
1547
1548 u8* srcdata = reinterpret_cast<u8*>(imgData);
1549 u8* destData = reinterpret_cast<u8*>([SoftwareDriverTarget bitmapData]);
1550 const u32 srcheight = core::min_(surface->getDimension().Height, (u32)areaRect.size.height);
1551 const u32 srcPitch = surface->getPitch();
1552 const u32 minWidth = core::min_(surface->getDimension().Width, (u32)areaRect.size.width);
1553 for (u32 y=0; y!=srcheight; ++y)
1554 {
1555 if(SoftwareRendererType == 2)
1556 {
1557 if (surface->getColorFormat() == video::ECF_A8R8G8B8)
1558 video::CColorConverter::convert_A8R8G8B8toB8G8R8(srcdata, minWidth, destData);
1559 else if (surface->getColorFormat() == video::ECF_A1R5G5B5)
1560 video::CColorConverter::convert_A1R5G5B5toB8G8R8(srcdata, minWidth, destData);
1561 else
1562 video::CColorConverter::convert_viaFormat(srcdata, surface->getColorFormat(), minWidth, destData, video::ECF_R8G8B8);
1563 }
1564 else
1565 {
1566 if (surface->getColorFormat() == video::ECF_A8R8G8B8)
1567 video::CColorConverter::convert_A8R8G8B8toR8G8B8(srcdata, minWidth, destData);
1568 else if (surface->getColorFormat() == video::ECF_A1R5G5B5)
1569 video::CColorConverter::convert_A1R5G5B5toR8G8B8(srcdata, minWidth, destData);
1570 else
1571 video::CColorConverter::convert_viaFormat(srcdata, surface->getColorFormat(), minWidth, destData, video::ECF_R8G8B8);
1572 }
1573
1574 srcdata += srcPitch;
1575 destData += destPitch;
1576 }
1577
1578 // unlock the data
1579 surface->unlock();
1580
1581 // todo: draw properly into a sub-view
1582 [SoftwareDriverTarget draw];
1583 }
1584
1585 return false;
1586}
1587
1588
1589#if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1590static void joystickRemovalCallback(void * target,
1591 IOReturn result, void * refcon, void * sender)
1592{
1593 JoystickInfo *joy = (JoystickInfo *) refcon;
1594 joy->removed = 1;
1595}
1596#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1597
1598
1599bool CIrrDeviceMacOSX::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
1600{
1601#if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1602 ActiveJoysticks.clear();
1603 joystickInfo.clear();
1604
1605 io_object_t hidObject = 0;
1606 io_iterator_t hidIterator = 0;
1607 IOReturn result = kIOReturnSuccess;
1608 mach_port_t masterPort = 0;
1609 CFMutableDictionaryRef hidDictionaryRef = NULL;
1610
1611 result = IOMasterPort (bootstrap_port, &masterPort);
1612 if (kIOReturnSuccess != result)
1613 {
1614 os::Printer::log("initialiseJoysticks IOMasterPort failed", ELL_ERROR);
1615 return false;
1616 }
1617
1618 hidDictionaryRef = IOServiceMatching (kIOHIDDeviceKey);
1619 if (!hidDictionaryRef)
1620 {
1621 os::Printer::log("initialiseJoysticks IOServiceMatching failed", ELL_ERROR);
1622 return false;
1623 }
1624 result = IOServiceGetMatchingServices (masterPort, hidDictionaryRef, &hidIterator);
1625
1626 if (kIOReturnSuccess != result)
1627 {
1628 os::Printer::log("initialiseJoysticks IOServiceGetMatchingServices failed", ELL_ERROR);
1629 return false;
1630 }
1631
1632 //no joysticks just return
1633 if (!hidIterator)
1634 return false;
1635
1636 u32 jindex = 0u;
1637 while ((hidObject = IOIteratorNext (hidIterator)))
1638 {
1639 JoystickInfo info;
1640
1641 // get dictionary for HID properties
1642 CFMutableDictionaryRef hidProperties = 0;
1643
1644 kern_return_t kern_result = IORegistryEntryCreateCFProperties (hidObject, &hidProperties, kCFAllocatorDefault, kNilOptions);
1645 if ((kern_result == KERN_SUCCESS) && hidProperties)
1646 {
1647 HRESULT plugInResult = S_OK;
1648 SInt32 score = 0;
1649 IOCFPlugInInterface ** ppPlugInInterface = NULL;
1650 result = IOCreatePlugInInterfaceForService (hidObject, kIOHIDDeviceUserClientTypeID,
1651 kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
1652 if (kIOReturnSuccess == result)
1653 {
1654 plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
1655 CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void **) &(info.interface));
1656 if (plugInResult != S_OK)
1657 os::Printer::log("initialiseJoysticks query HID class device interface failed", ELL_ERROR);
1658 (*ppPlugInInterface)->Release(ppPlugInInterface);
1659 }
1660 else
1661 continue;
1662
1663 if (info.interface != NULL)
1664 {
1665 result = (*(info.interface))->open (info.interface, 0);
1666 if (result == kIOReturnSuccess)
1667 {
1668 (*(info.interface))->setRemovalCallback (info.interface, joystickRemovalCallback, &info, &info);
1669 getJoystickDeviceInfo(hidObject, hidProperties, &info);
1670
1671 // get elements
1672 CFTypeRef refElementTop = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
1673 if (refElementTop)
1674 {
1675 CFTypeID type = CFGetTypeID (refElementTop);
1676 if (type == CFArrayGetTypeID())
1677 {
1678 CFRange range = {0, CFArrayGetCount ((CFArrayRef)refElementTop)};
1679 info.numActiveJoysticks = ActiveJoysticks.size();
1680 CFArrayApplyFunction ((CFArrayRef)refElementTop, range, getJoystickComponentArrayHandler, &info);
1681 }
1682 }
1683 }
1684 else
1685 {
1686 CFRelease (hidProperties);
1687 os::Printer::log("initialiseJoysticks Open interface failed", ELL_ERROR);
1688 continue;
1689 }
1690
1691 CFRelease (hidProperties);
1692
1693 result = IOObjectRelease (hidObject);
1694
1695 if ( (info.usagePage != kHIDPage_GenericDesktop) ||
1696 ((info.usage != kHIDUsage_GD_Joystick &&
1697 info.usage != kHIDUsage_GD_GamePad &&
1698 info.usage != kHIDUsage_GD_MultiAxisController)) )
1699 {
1700 closeJoystickDevice (&info);
1701 continue;
1702 }
1703
1704 for (u32 i = 0; i < 6; ++i)
1705 info.persistentData.JoystickEvent.Axis[i] = 0;
1706
1707 ActiveJoysticks.push_back(info);
1708
1709 SJoystickInfo returnInfo;
1710 returnInfo.Joystick = jindex;
1711 returnInfo.Axes = info.axes;
1712 //returnInfo.Hats = info.hats;
1713 returnInfo.Buttons = info.buttons;
1714 returnInfo.Name = info.joystickName;
1715 returnInfo.PovHat = SJoystickInfo::POV_HAT_UNKNOWN;
1716 ++ jindex;
1717
1718 //if (info.hatComp.size())
1719 // returnInfo.PovHat = SJoystickInfo::POV_HAT_PRESENT;
1720 //else
1721 // returnInfo.PovHat = SJoystickInfo::POV_HAT_ABSENT;
1722
1723 joystickInfo.push_back(returnInfo);
1724 }
1725
1726 }
1727 else
1728 {
1729 continue;
1730 }
1731 }
1732 result = IOObjectRelease (hidIterator);
1733
1734 return true;
1735#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1736
1737 return false;
1738}
1739
1740void CIrrDeviceMacOSX::pollJoysticks()
1741{
1742#if defined (_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
1743 if(0 == ActiveJoysticks.size())
1744 return;
1745
1746 u32 joystick;
1747 for (joystick = 0; joystick < ActiveJoysticks.size(); ++joystick)
1748 {
1749 if (ActiveJoysticks[joystick].removed)
1750 continue;
1751
1752 bool found = false;
1753 ActiveJoysticks[joystick].persistentData.JoystickEvent.Joystick = joystick;
1754
1755 if (ActiveJoysticks[joystick].interface)
1756 {
1757 for (u32 n = 0; n < ActiveJoysticks[joystick].axisComp.size(); n++)
1758 {
1759 IOReturn result = kIOReturnSuccess;
1760 IOHIDEventStruct hidEvent;
1761 hidEvent.value = 0;
1762 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].axisComp[n].cookie, &hidEvent);
1763 if (kIOReturnSuccess == result)
1764 {
1765 const f32 min = -32768.0f;
1766 const f32 max = 32767.0f;
1767 const f32 deviceScale = max - min;
1768 const f32 readScale = (f32)ActiveJoysticks[joystick].axisComp[n].maxRead - (f32)ActiveJoysticks[joystick].axisComp[n].minRead;
1769
1770 if (hidEvent.value < ActiveJoysticks[joystick].axisComp[n].minRead)
1771 ActiveJoysticks[joystick].axisComp[n].minRead = hidEvent.value;
1772 if (hidEvent.value > ActiveJoysticks[joystick].axisComp[n].maxRead)
1773 ActiveJoysticks[joystick].axisComp[n].maxRead = hidEvent.value;
1774
1775 if (readScale != 0.0f)
1776 hidEvent.value = (int)(((f32)((f32)hidEvent.value - (f32)ActiveJoysticks[joystick].axisComp[n].minRead) * deviceScale / readScale) + min);
1777
1778 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] != (s16)hidEvent.value)
1779 found = true;
1780 ActiveJoysticks[joystick].persistentData.JoystickEvent.Axis[n] = (s16)hidEvent.value;
1781 }
1782 }//axis check
1783
1784 for (u32 n = 0; n < ActiveJoysticks[joystick].buttonComp.size(); n++)
1785 {
1786 IOReturn result = kIOReturnSuccess;
1787 IOHIDEventStruct hidEvent;
1788 hidEvent.value = 0;
1789 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].buttonComp[n].cookie, &hidEvent);
1790 if (kIOReturnSuccess == result)
1791 {
1792 u32 ButtonStates = 0;
1793
1794 if (hidEvent.value && !((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false) )
1795 found = true;
1796 else if (!hidEvent.value && ((ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates & (1 << n)) ? true : false))
1797 found = true;
1798
1799 if (hidEvent.value)
1800 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates |= (1 << n);
1801 else
1802 ActiveJoysticks[joystick].persistentData.JoystickEvent.ButtonStates &= ~(1 << n);
1803 }
1804 }//button check
1805 //still ToDo..will be done soon :)
1806/*
1807 for (u32 n = 0; n < ActiveJoysticks[joystick].hatComp.size(); n++)
1808 {
1809 IOReturn result = kIOReturnSuccess;
1810 IOHIDEventStruct hidEvent;
1811 hidEvent.value = 0;
1812 result = (*(ActiveJoysticks[joystick].interface))->getElementValue(ActiveJoysticks[joystick].interface, ActiveJoysticks[joystick].hatComp[n].cookie, &hidEvent);
1813 if (kIOReturnSuccess == result)
1814 {
1815 if (ActiveJoysticks[joystick].persistentData.JoystickEvent.POV != hidEvent.value)
1816 found = true;
1817 ActiveJoysticks[joystick].persistentData.JoystickEvent.POV = hidEvent.value;
1818 }
1819 }//hat check
1820*/
1821 }
1822
1823 if (found)
1824 postEventFromUser(ActiveJoysticks[joystick].persistentData);
1825 }
1826#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
1827}
1828
1829video::IVideoModeList* CIrrDeviceMacOSX::getVideoModeList()
1830{
1831 if (!VideoModeList.getVideoModeCount())
1832 {
1833 CGDirectDisplayID display;
1834 display = CGMainDisplayID();
1835
1836#ifdef __MAC_10_6
1837 CFArrayRef Modes = CGDisplayCopyAllDisplayModes(display, NULL);
1838
1839 for(int i = 0; i < CFArrayGetCount(Modes); ++i)
1840 {
1841 CGDisplayModeRef CurrentMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(Modes, i);
1842
1843 u8 Depth = 0;
1844
1845 CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(CurrentMode);
1846
1847 if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1848 Depth = 32;
1849 else
1850 if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1851 Depth = 16;
1852 else
1853 if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
1854 Depth = 8;
1855
1856 if(Depth)
1857 {
1858 unsigned int Width = CGDisplayModeGetWidth(CurrentMode);
1859 unsigned int Height = CGDisplayModeGetHeight(CurrentMode);
1860
1861 VideoModeList.addMode(core::dimension2d<u32>(Width, Height), Depth);
1862 }
1863 }
1864#else
1865 CFArrayRef availableModes = CGDisplayAvailableModes(display);
1866 unsigned int numberOfAvailableModes = CFArrayGetCount(availableModes);
1867 for (u32 i= 0; i<numberOfAvailableModes; ++i)
1868 {
1869 // look at each mode in the available list
1870 CFDictionaryRef mode = (CFDictionaryRef)CFArrayGetValueAtIndex(availableModes, i);
1871 long bitsPerPixel = GetDictionaryLong(mode, kCGDisplayBitsPerPixel);
1872 Boolean safeForHardware = GetDictionaryBoolean(mode, kCGDisplayModeIsSafeForHardware);
1873 Boolean stretched = GetDictionaryBoolean(mode, kCGDisplayModeIsStretched);
1874
1875 if (!safeForHardware)
1876 continue;
1877
1878 long width = GetDictionaryLong(mode, kCGDisplayWidth);
1879 long height = GetDictionaryLong(mode, kCGDisplayHeight);
1880 // long refresh = GetDictionaryLong((mode), kCGDisplayRefreshRate);
1881 VideoModeList.addMode(core::dimension2d<u32>(width, height),
1882 bitsPerPixel);
1883 }
1884#endif
1885 }
1886 return &VideoModeList;
1887}
1888
1889} // end namespace
1890
1891#endif // _IRR_COMPILE_WITH_OSX_DEVICE_
1892