4 Mac OS X OpenGL and input module, using Carbon and AGL
6 Copyright (C) 2005-2006 Mathieu Olivier
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <OpenGL/OpenGL.h>
28 #include <Carbon/Carbon.h>
29 #include <IOKit/hidsystem/IOHIDLib.h>
30 #include <IOKit/hidsystem/IOHIDParameter.h>
31 #include <IOKit/hidsystem/event_status_driver.h>
33 #include "vid_agl_mackeys.h" // this is SDL/src/video/maccommon/SDL_mackeys.h
35 #ifndef kCGLCEMPEngine
36 #define kCGLCEMPEngine 313
39 // Tell startup code that we have a client
40 int cl_available = true;
42 qboolean vid_supportrefreshrate = true;
45 AGLPixelFormat (*qaglChoosePixelFormat) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList);
46 AGLContext (*qaglCreateContext) (AGLPixelFormat pix, AGLContext share);
47 GLboolean (*qaglDestroyContext) (AGLContext ctx);
48 void (*qaglDestroyPixelFormat) (AGLPixelFormat pix);
49 const GLubyte* (*qaglErrorString) (GLenum code);
50 GLenum (*qaglGetError) (void);
51 GLboolean (*qaglSetCurrentContext) (AGLContext ctx);
52 GLboolean (*qaglSetDrawable) (AGLContext ctx, AGLDrawable draw);
53 GLboolean (*qaglSetFullScreen) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device);
54 GLboolean (*qaglSetInteger) (AGLContext ctx, GLenum pname, const GLint *params);
55 void (*qaglSwapBuffers) (AGLContext ctx);
56 CGLError (*qCGLEnable) (CGLContextObj ctx, CGLContextEnable pname);
57 CGLError (*qCGLDisable) (CGLContextObj ctx, CGLContextEnable pname);
58 CGLContextObj (*qCGLGetCurrentContext) (void);
60 static qboolean multithreadedgl;
61 static qboolean mouse_avail = true;
62 static qboolean vid_usingmouse = false;
63 static qboolean vid_usinghidecursor = false;
64 static qboolean vid_usingnoaccel = false;
66 static qboolean vid_isfullscreen = false;
67 static qboolean vid_usingvsync = false;
69 static qboolean sound_active = true;
71 static cvar_t apple_multithreadedgl = {CVAR_SAVE, "apple_multithreadedgl", "1", "makes use of a second thread for the OpenGL driver (if possible) rather than using the engine thread (note: this is done automatically on most other operating systems)"};
72 static cvar_t apple_mouse_noaccel = {CVAR_SAVE, "apple_mouse_noaccel", "1", "disables mouse acceleration while DarkPlaces is active"};
74 static AGLContext context;
75 static WindowRef window;
77 static double originalMouseSpeed = -1.0;
79 io_connect_t IN_GetIOHandle(void)
81 io_connect_t iohandle = MACH_PORT_NULL;
83 io_service_t iohidsystem = MACH_PORT_NULL;
84 mach_port_t masterport;
86 status = IOMasterPort(MACH_PORT_NULL, &masterport);
87 if(status != KERN_SUCCESS)
90 iohidsystem = IORegistryEntryFromPath(masterport, kIOServicePlane ":/IOResources/IOHIDSystem");
94 status = IOServiceOpen(iohidsystem, mach_task_self(), kIOHIDParamConnectType, &iohandle);
95 IOObjectRelease(iohidsystem);
100 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
102 if (!mouse_avail || !window)
103 relative = hidecursor = false;
107 if(vid_usingmouse && (vid_usingnoaccel != !!apple_mouse_noaccel.integer))
108 VID_SetMouse(false, false, false); // ungrab first!
114 SelectWindow(window);
116 // Put the mouse cursor at the center of the window
117 GetWindowBounds (window, kWindowContentRgn, &winBounds);
118 winCenter.x = (winBounds.left + winBounds.right) / 2;
119 winCenter.y = (winBounds.top + winBounds.bottom) / 2;
120 CGWarpMouseCursorPosition(winCenter);
122 // Lock the mouse pointer at its current position
123 CGAssociateMouseAndMouseCursorPosition(false);
125 // Save the status of mouse acceleration
126 originalMouseSpeed = -1.0; // in case of error
127 if(apple_mouse_noaccel.integer)
129 io_connect_t mouseDev = IN_GetIOHandle();
132 if(IOHIDGetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), &originalMouseSpeed) == kIOReturnSuccess)
134 Con_DPrintf("previous mouse acceleration: %f\n", originalMouseSpeed);
135 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), -1.0) != kIOReturnSuccess)
137 Con_Print("Could not disable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
138 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
143 Con_Print("Could not disable mouse acceleration (failed at IOHIDGetAccelerationWithKey).\n");
144 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
146 IOServiceClose(mouseDev);
150 Con_Print("Could not disable mouse acceleration (failed at IO_GetIOHandle).\n");
151 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
155 vid_usingmouse = true;
156 vid_usingnoaccel = !!apple_mouse_noaccel.integer;
163 if(originalMouseSpeed != -1.0)
165 io_connect_t mouseDev = IN_GetIOHandle();
168 Con_DPrintf("restoring mouse acceleration to: %f\n", originalMouseSpeed);
169 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), originalMouseSpeed) != kIOReturnSuccess)
170 Con_Print("Could not re-enable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
171 IOServiceClose(mouseDev);
174 Con_Print("Could not re-enable mouse acceleration (failed at IO_GetIOHandle).\n");
177 CGAssociateMouseAndMouseCursorPosition(true);
179 vid_usingmouse = false;
183 if (vid_usinghidecursor != hidecursor)
185 vid_usinghidecursor = hidecursor;
187 CGDisplayHideCursor(CGMainDisplayID());
189 CGDisplayShowCursor(CGMainDisplayID());
193 void VID_Finish (void)
195 qboolean vid_usevsync;
197 // handle changes of the vsync option
198 vid_usevsync = (vid_vsync.integer && !cls.timedemo);
199 if (vid_usingvsync != vid_usevsync)
201 GLint sync = (vid_usevsync ? 1 : 0);
203 if (qaglSetInteger(context, AGL_SWAP_INTERVAL, &sync) == GL_TRUE)
205 vid_usingvsync = vid_usevsync;
206 Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
209 Con_Printf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
214 if (r_speeds.integer == 2 || gl_finish.integer)
216 qaglSwapBuffers(context);
220 if (apple_multithreadedgl.integer)
222 if (!multithreadedgl)
224 if(qCGLGetCurrentContext && qCGLEnable && qCGLDisable)
226 CGLContextObj ctx = qCGLGetCurrentContext();
227 CGLError e = qCGLEnable(ctx, kCGLCEMPEngine);
229 multithreadedgl = true;
232 Con_Printf("WARNING: can't enable multithreaded GL, error %d\n", (int) e);
233 Cvar_SetValueQuick(&apple_multithreadedgl, 0);
238 Con_Printf("WARNING: can't enable multithreaded GL, CGL functions not present\n");
239 Cvar_SetValueQuick(&apple_multithreadedgl, 0);
247 if(qCGLGetCurrentContext && qCGLEnable && qCGLDisable)
249 CGLContextObj ctx = qCGLGetCurrentContext();
250 qCGLDisable(ctx, kCGLCEMPEngine);
251 multithreadedgl = false;
257 void signal_handler(int sig)
259 Sys_PrintfToTerminal("Received signal %d, exiting...\n", sig);
265 signal(SIGHUP, signal_handler);
266 signal(SIGINT, signal_handler);
267 signal(SIGQUIT, signal_handler);
268 signal(SIGILL, signal_handler);
269 signal(SIGTRAP, signal_handler);
270 signal(SIGIOT, signal_handler);
271 signal(SIGBUS, signal_handler);
272 signal(SIGFPE, signal_handler);
273 signal(SIGSEGV, signal_handler);
274 signal(SIGTERM, signal_handler);
279 InitSig(); // trap evil signals
280 Cvar_RegisterVariable(&apple_multithreadedgl);
281 Cvar_RegisterVariable(&apple_mouse_noaccel);
282 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
283 if (COM_CheckParm ("-nomouse"))
287 static void *prjobj = NULL;
288 static void *cglobj = NULL;
290 static void GL_CloseLibrary(void)
301 gl_platformextensions = "";
304 static int GL_OpenLibrary(void)
306 const char *name = "/System/Library/Frameworks/AGL.framework/AGL";
307 const char *name2 = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
309 Con_Printf("Loading OpenGL driver %s\n", name);
311 if (!(prjobj = dlopen(name, RTLD_LAZY)))
313 Con_Printf("Unable to open symbol list for %s\n", name);
316 strlcpy(gl_driver, name, sizeof(gl_driver));
318 Con_Printf("Loading OpenGL driver %s\n", name2);
319 if (!(cglobj = dlopen(name2, RTLD_LAZY)))
320 Con_Printf("Unable to open symbol list for %s; multithreaded GL disabled\n", name);
325 void *GL_GetProcAddress(const char *name)
327 return dlsym(prjobj, name);
330 static void *CGL_GetProcAddress(const char *name)
334 return dlsym(cglobj, name);
337 void VID_Shutdown(void)
339 if (context == NULL && window == NULL)
342 VID_EnableJoystick(false);
343 VID_SetMouse(false, false, false);
347 qaglDestroyContext(context);
351 if (vid_isfullscreen)
352 CGReleaseAllDisplays();
356 DisposeWindow(window);
361 vid_isfullscreen = false;
366 // Since the event handler can be called at any time, we store the events for later processing
367 static qboolean AsyncEvent_Quitting = false;
368 static qboolean AsyncEvent_Collapsed = false;
369 static OSStatus MainWindowEventHandler (EventHandlerCallRef nextHandler, EventRef event, void *userData)
371 OSStatus err = noErr;
373 switch (GetEventKind (event))
375 case kEventWindowClosed:
376 AsyncEvent_Quitting = true;
380 case kEventWindowCollapsing:
381 AsyncEvent_Collapsed = true;
384 // Undocked / restored (end)
385 case kEventWindowExpanded:
386 AsyncEvent_Collapsed = false;
390 err = eventNotHandledErr;
397 static void VID_AppFocusChanged(qboolean windowIsActive)
399 if (vid_activewindow != windowIsActive)
400 vid_activewindow = windowIsActive;
402 if (windowIsActive || !snd_mutewhenidle.integer)
415 sound_active = false;
420 static void VID_ProcessPendingAsyncEvents (void)
422 // Collapsed / expanded
423 if (AsyncEvent_Collapsed != vid_hidden)
425 vid_hidden = !vid_hidden;
426 VID_AppFocusChanged(!vid_hidden);
430 if (AsyncEvent_Quitting)
434 static void VID_BuildAGLAttrib(GLint *attrib, qboolean stencil, qboolean fullscreen, qboolean stereobuffer, int samples)
436 *attrib++ = AGL_RGBA;
437 *attrib++ = AGL_RED_SIZE;*attrib++ = stencil ? 8 : 5;
438 *attrib++ = AGL_GREEN_SIZE;*attrib++ = stencil ? 8 : 5;
439 *attrib++ = AGL_BLUE_SIZE;*attrib++ = stencil ? 8 : 5;
440 *attrib++ = AGL_DOUBLEBUFFER;
441 *attrib++ = AGL_DEPTH_SIZE;*attrib++ = stencil ? 24 : 16;
443 // if stencil is enabled, ask for alpha too
446 *attrib++ = AGL_STENCIL_SIZE;*attrib++ = 8;
447 *attrib++ = AGL_ALPHA_SIZE;*attrib++ = 8;
450 *attrib++ = AGL_FULLSCREEN;
452 *attrib++ = AGL_STEREO;
453 #ifdef AGL_SAMPLE_BUFFERS_ARB
454 #ifdef AGL_SAMPLES_ARB
457 *attrib++ = AGL_SAMPLE_BUFFERS_ARB;
459 *attrib++ = AGL_SAMPLES_ARB;
465 *attrib++ = AGL_NONE;
468 qboolean VID_InitMode(viddef_mode_t *mode)
470 const EventTypeSpec winEvents[] =
472 { kEventClassWindow, kEventWindowClosed },
473 { kEventClassWindow, kEventWindowCollapsing },
474 { kEventClassWindow, kEventWindowExpanded },
476 OSStatus carbonError;
478 CFStringRef windowTitle;
479 AGLPixelFormat pixelFormat;
480 GLint attributes [32];
483 if (!GL_OpenLibrary())
485 Con_Printf("Unable to load GL driver\n");
489 if ((qaglChoosePixelFormat = (AGLPixelFormat (*) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList))GL_GetProcAddress("aglChoosePixelFormat")) == NULL
490 || (qaglCreateContext = (AGLContext (*) (AGLPixelFormat pix, AGLContext share))GL_GetProcAddress("aglCreateContext")) == NULL
491 || (qaglDestroyContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglDestroyContext")) == NULL
492 || (qaglDestroyPixelFormat = (void (*) (AGLPixelFormat pix))GL_GetProcAddress("aglDestroyPixelFormat")) == NULL
493 || (qaglErrorString = (const GLubyte* (*) (GLenum code))GL_GetProcAddress("aglErrorString")) == NULL
494 || (qaglGetError = (GLenum (*) (void))GL_GetProcAddress("aglGetError")) == NULL
495 || (qaglSetCurrentContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglSetCurrentContext")) == NULL
496 || (qaglSetDrawable = (GLboolean (*) (AGLContext ctx, AGLDrawable draw))GL_GetProcAddress("aglSetDrawable")) == NULL
497 || (qaglSetFullScreen = (GLboolean (*) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device))GL_GetProcAddress("aglSetFullScreen")) == NULL
498 || (qaglSetInteger = (GLboolean (*) (AGLContext ctx, GLenum pname, const GLint *params))GL_GetProcAddress("aglSetInteger")) == NULL
499 || (qaglSwapBuffers = (void (*) (AGLContext ctx))GL_GetProcAddress("aglSwapBuffers")) == NULL
502 Con_Printf("AGL functions not found\n");
503 ReleaseWindow(window);
507 qCGLEnable = (CGLError (*) (CGLContextObj ctx, CGLContextEnable pname)) CGL_GetProcAddress("CGLEnable");
508 qCGLDisable = (CGLError (*) (CGLContextObj ctx, CGLContextEnable pname)) CGL_GetProcAddress("CGLDisable");
509 qCGLGetCurrentContext = (CGLContextObj (*) (void)) CGL_GetProcAddress("CGLGetCurrentContext");
510 if(!qCGLEnable || !qCGLDisable || !qCGLGetCurrentContext)
511 Con_Printf("CGL functions not found; disabling multithreaded OpenGL\n");
513 // Ignore the events from the previous window
514 AsyncEvent_Quitting = false;
515 AsyncEvent_Collapsed = false;
517 // Create the window, a bit towards the center of the screen
518 windowBounds.left = 100;
519 windowBounds.top = 100;
520 windowBounds.right = mode->width + 100;
521 windowBounds.bottom = mode->height + 100;
522 carbonError = CreateNewWindow(kDocumentWindowClass, kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute, &windowBounds, &window);
523 if (carbonError != noErr || window == NULL)
525 Con_Printf("Unable to create window (error %u)\n", (unsigned)carbonError);
529 // Set the window title
530 windowTitle = CFSTR("DarkPlaces AGL");
531 SetWindowTitleWithCFString(window, windowTitle);
533 // Install the callback function for the window events we can't get
534 // through ReceiveNextEvent (i.e. close, collapse, and expand)
535 InstallWindowEventHandler (window, NewEventHandlerUPP (MainWindowEventHandler),
536 GetEventTypeCount(winEvents), winEvents, window, NULL);
538 // Create the desired attribute list
539 VID_BuildAGLAttrib(attributes, mode->bitsperpixel == 32, mode->fullscreen, mode->stereobuffer, mode->samples);
541 if (!mode->fullscreen)
544 pixelFormat = qaglChoosePixelFormat(NULL, 0, attributes);
545 error = qaglGetError();
546 if (error != AGL_NO_ERROR)
548 Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
549 (char *)qaglErrorString(error));
550 ReleaseWindow(window);
554 else // Output is fullScreen
556 CGDirectDisplayID mainDisplay;
557 CFDictionaryRef refDisplayMode;
560 // Get the mainDisplay and set resolution to current
561 mainDisplay = CGMainDisplayID();
562 CGDisplayCapture(mainDisplay);
564 // TOCHECK: not sure whether or not it's necessary to change the resolution
565 // "by hand", or if aglSetFullscreen does the job anyway
566 refDisplayMode = CGDisplayBestModeForParametersAndRefreshRateWithProperty(mainDisplay, mode->bitsperpixel, mode->width, mode->height, mode->refreshrate, kCGDisplayModeIsSafeForHardware, NULL);
567 CGDisplaySwitchToMode(mainDisplay, refDisplayMode);
568 DMGetGDeviceByDisplayID((DisplayIDType)mainDisplay, &gdhDisplay, false);
570 // Set pixel format with built attribs
571 // Note: specifying a device is *required* for AGL_FullScreen
572 pixelFormat = qaglChoosePixelFormat(&gdhDisplay, 1, attributes);
573 error = qaglGetError();
574 if (error != AGL_NO_ERROR)
576 Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
577 (char *)qaglErrorString(error));
578 ReleaseWindow(window);
583 // Create a context using the pform
584 context = qaglCreateContext(pixelFormat, NULL);
585 error = qaglGetError();
586 if (error != AGL_NO_ERROR)
588 Con_Printf("qaglCreateContext FAILED: %s\n",
589 (char *)qaglErrorString(error));
592 // Make the context the current one ('enable' it)
593 qaglSetCurrentContext(context);
594 error = qaglGetError();
595 if (error != AGL_NO_ERROR)
597 Con_Printf("qaglSetCurrentContext FAILED: %s\n",
598 (char *)qaglErrorString(error));
599 ReleaseWindow(window);
604 qaglDestroyPixelFormat(pixelFormat);
606 // Attempt fullscreen if requested
607 if (mode->fullscreen)
609 qaglSetFullScreen (context, mode->width, mode->height, mode->refreshrate, 0);
610 error = qaglGetError();
611 if (error != AGL_NO_ERROR)
613 Con_Printf("qaglSetFullScreen FAILED: %s\n",
614 (char *)qaglErrorString(error));
620 // Set Window as Drawable
621 qaglSetDrawable(context, GetWindowPort(window));
622 error = qaglGetError();
623 if (error != AGL_NO_ERROR)
625 Con_Printf("qaglSetDrawable FAILED: %s\n",
626 (char *)qaglErrorString(error));
627 ReleaseWindow(window);
632 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
633 Sys_Error("glGetString not found in %s", gl_driver);
635 gl_platformextensions = "";
638 multithreadedgl = false;
639 vid_isfullscreen = mode->fullscreen;
640 vid_usingmouse = false;
641 vid_usinghidecursor = false;
643 vid_activewindow = true;
647 SelectWindow(window);
653 static void Handle_KeyMod(UInt32 keymod)
655 const struct keymod_to_event_s { UInt32 keybit; keynum_t event; } keymod_events [] =
658 { shiftKey, K_SHIFT },
659 { alphaLock, K_CAPSLOCK },
660 { optionKey, K_ALT },
661 { controlKey, K_CTRL },
662 { kEventKeyModifierNumLockMask, K_NUMLOCK },
663 { kEventKeyModifierFnMask, K_AUX2 }
665 static UInt32 prev_keymod = 0;
669 modChanges = prev_keymod ^ keymod;
673 for (i = 0; i < sizeof(keymod_events) / sizeof(keymod_events[0]); i++)
675 UInt32 keybit = keymod_events[i].keybit;
677 if ((modChanges & keybit) != 0)
678 Key_Event(keymod_events[i].event, '\0', (keymod & keybit) != 0);
681 prev_keymod = keymod;
684 static void Handle_Key(unsigned char charcode, UInt32 mackeycode, qboolean keypressed)
686 unsigned int keycode = 0;
731 keycode = K_SCROLLOCK;
737 keycode = K_BACKSPACE;
752 keycode = K_KP_EQUALS;
755 keycode = K_KP_DIVIDE;
758 keycode = K_KP_MULTIPLY;
782 keycode = K_KP_MINUS;
785 keycode = K_CAPSLOCK;
813 keycode = K_KP_ENTER;
819 keycode = K_KP_PERIOD;
824 case kUpArrowCharCode:
827 case kLeftArrowCharCode:
828 keycode = K_LEFTARROW;
830 case kDownArrowCharCode:
831 keycode = K_DOWNARROW;
833 case kRightArrowCharCode:
834 keycode = K_RIGHTARROW;
838 // characters 0 and 191 are sent by the mouse buttons (?!)
841 if ('A' <= charcode && charcode <= 'Z')
843 keycode = charcode + ('a' - 'A'); // lowercase it
846 else if (charcode >= 32)
852 Con_DPrintf(">> UNKNOWN char/keycode: %d/%u <<\n", charcode, (unsigned) mackeycode);
857 Key_Event(keycode, ascii, keypressed);
860 void Sys_SendKeyEvents(void)
863 EventTargetRef theTarget;
865 // Start by processing the asynchronous events we received since the previous frame
866 VID_ProcessPendingAsyncEvents();
868 theTarget = GetEventDispatcherTarget();
869 while (ReceiveNextEvent(0, NULL, kEventDurationNoWait, true, &theEvent) == noErr)
871 UInt32 eventClass = GetEventClass(theEvent);
872 UInt32 eventKind = GetEventKind(theEvent);
876 case kEventClassMouse:
878 EventMouseButton theButton;
883 case kEventMouseDown:
885 GetEventParameter(theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(theButton), NULL, &theButton);
889 case kEventMouseButtonPrimary:
892 case kEventMouseButtonSecondary:
895 case kEventMouseButtonTertiary:
899 Key_Event(key, '\0', eventKind == kEventMouseDown);
902 // Note: These two events are mutual exclusives
903 // Treat MouseDragged in the same statement, so we don't block MouseMoved while a mousebutton is held
904 case kEventMouseMoved:
905 case kEventMouseDragged:
910 GetEventParameter(theEvent, kEventParamMouseDelta, typeHIPoint, NULL, sizeof(deltaPos), NULL, &deltaPos);
911 GetEventParameter(theEvent, kEventParamWindowMouseLocation, typeHIPoint, NULL, sizeof(windowPos), NULL, &windowPos);
915 in_mouse_x += deltaPos.x;
916 in_mouse_y += deltaPos.y;
919 in_windowmouse_x = windowPos.x;
920 in_windowmouse_y = windowPos.y;
924 case kEventMouseWheelMoved:
927 unsigned int wheelEvent;
929 GetEventParameter(theEvent, kEventParamMouseWheelDelta, typeSInt32, NULL, sizeof(delta), NULL, &delta);
931 wheelEvent = (delta > 0) ? K_MWHEELUP : K_MWHEELDOWN;
932 Key_Event(wheelEvent, 0, true);
933 Key_Event(wheelEvent, 0, false);
938 Con_Printf (">> kEventClassMouse (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
943 case kEventClassKeyboard:
950 case kEventRawKeyDown:
951 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charcode), NULL, &charcode);
952 GetEventParameter(theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(keycode), NULL, &keycode);
953 Handle_Key(charcode, keycode, true);
956 case kEventRawKeyRepeat:
960 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charcode), NULL, &charcode);
961 GetEventParameter(theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(keycode), NULL, &keycode);
962 Handle_Key(charcode, keycode, false);
965 case kEventRawKeyModifiersChanged:
968 GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(keymod), NULL, &keymod);
969 Handle_KeyMod(keymod);
973 case kEventHotKeyPressed:
976 case kEventHotKeyReleased:
979 case kEventMouseWheelMoved:
983 Con_Printf (">> kEventClassKeyboard (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
989 case kEventClassTextInput:
990 Con_Printf(">> kEventClassTextInput (%d) <<\n", (int)eventKind);
993 case kEventClassApplication:
996 case kEventAppActivated :
997 VID_AppFocusChanged(true);
999 case kEventAppDeactivated:
1000 VID_AppFocusChanged(false);
1005 case kEventAppActiveWindowChanged:
1008 Con_Printf(">> kEventClassApplication (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1013 case kEventClassAppleEvent:
1016 case kEventAppleEvent :
1019 Con_Printf(">> kEventClassAppleEvent (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1024 case kEventClassWindow:
1027 case kEventWindowUpdate :
1030 Con_Printf(">> kEventClassWindow (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1035 case kEventClassControl:
1039 /*Con_Printf(">> UNKNOWN eventClass: %c%c%c%c, eventKind: %d <<\n",
1040 eventClass >> 24, (eventClass >> 16) & 0xFF,
1041 (eventClass >> 8) & 0xFF, eventClass & 0xFF,
1046 SendEventToEventTarget (theEvent, theTarget);
1047 ReleaseEvent(theEvent);
1051 void VID_BuildJoyState(vid_joystate_t *joystate)
1053 VID_Shared_BuildJoyState_Begin(joystate);
1054 VID_Shared_BuildJoyState_Finish(joystate);
1057 void VID_EnableJoystick(qboolean enable)
1059 int index = joy_enable.integer > 0 ? joy_index.integer : -1;
1060 qboolean success = false;
1061 int sharedcount = 0;
1062 sharedcount = VID_Shared_SetJoystick(index);
1063 if (index >= 0 && index < sharedcount)
1066 // update cvar containing count of XInput joysticks
1067 if (joy_detected.integer != sharedcount)
1068 Cvar_SetValueQuick(&joy_detected, sharedcount);
1070 Cvar_SetValueQuick(&joy_active, success ? 1 : 0);
1075 vid_joystate_t joystate;
1076 VID_EnableJoystick(true);
1077 VID_BuildJoyState(&joystate);
1078 VID_ApplyJoyState(&joystate);
1081 static bool GetDictionaryBoolean(CFDictionaryRef d, const void *key)
1083 CFBooleanRef ref = (CFBooleanRef) CFDictionaryGetValue(d, key);
1085 return CFBooleanGetValue(ref);
1089 long GetDictionaryLong(CFDictionaryRef d, const void *key)
1092 CFNumberRef ref = (CFNumberRef) CFDictionaryGetValue(d, key);
1094 CFNumberGetValue(ref, kCFNumberLongType, &value);
1098 vid_mode_t *VID_GetDesktopMode(void)
1100 return NULL; // FIXME add desktopfullscreen
1103 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1105 CGDirectDisplayID mainDisplay = CGMainDisplayID();
1106 CFArrayRef vidmodes = CGDisplayAvailableModes(mainDisplay);
1107 CFDictionaryRef thismode;
1108 unsigned int n = CFArrayGetCount(vidmodes);
1113 for(i = 0; i < n; ++i)
1115 thismode = (CFDictionaryRef) CFArrayGetValueAtIndex(vidmodes, i);
1116 if(!GetDictionaryBoolean(thismode, kCGDisplayModeIsSafeForHardware))
1121 modes[k].width = GetDictionaryLong(thismode, kCGDisplayWidth);
1122 modes[k].height = GetDictionaryLong(thismode, kCGDisplayHeight);
1123 modes[k].bpp = GetDictionaryLong(thismode, kCGDisplayBitsPerPixel);
1124 modes[k].refreshrate = GetDictionaryLong(thismode, kCGDisplayRefreshRate);
1125 modes[k].pixelheight_num = 1;
1126 modes[k].pixelheight_denom = 1; // OS X doesn't expose this either