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 float mouse_x, mouse_y;
65 static qboolean vid_isfullscreen = false;
66 static qboolean vid_usingvsync = false;
68 static qboolean sound_active = true;
70 static int scr_width, scr_height;
72 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)"};
73 static cvar_t apple_mouse_noaccel = {CVAR_SAVE, "apple_mouse_noaccel", "1", "disables mouse acceleration while DarkPlaces is active"};
75 static AGLContext context;
76 static WindowRef window;
78 static double originalMouseSpeed = -1.0;
80 io_connect_t IN_GetIOHandle()
82 io_connect_t iohandle = MACH_PORT_NULL;
84 io_service_t iohidsystem = MACH_PORT_NULL;
85 mach_port_t masterport;
87 status = IOMasterPort(MACH_PORT_NULL, &masterport);
88 if(status != KERN_SUCCESS)
91 iohidsystem = IORegistryEntryFromPath(masterport, kIOServicePlane ":/IOResources/IOHIDSystem");
95 status = IOServiceOpen(iohidsystem, mach_task_self(), kIOHIDParamConnectType, &iohandle);
96 IOObjectRelease(iohidsystem);
101 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
105 *height = scr_height;
108 static void IN_Activate( qboolean grab )
112 if (!vid_usingmouse && mouse_avail && window)
117 SelectWindow(window);
118 CGDisplayHideCursor(CGMainDisplayID());
120 // Put the mouse cursor at the center of the window
121 GetWindowBounds (window, kWindowContentRgn, &winBounds);
122 winCenter.x = (winBounds.left + winBounds.right) / 2;
123 winCenter.y = (winBounds.top + winBounds.bottom) / 2;
124 CGWarpMouseCursorPosition(winCenter);
126 // Lock the mouse pointer at its current position
127 CGAssociateMouseAndMouseCursorPosition(false);
129 // Save the status of mouse acceleration
130 originalMouseSpeed = -1.0; // in case of error
131 if(apple_mouse_noaccel.integer)
133 io_connect_t mouseDev = IN_GetIOHandle();
136 if(IOHIDGetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), &originalMouseSpeed) == kIOReturnSuccess)
138 Con_DPrintf("previous mouse acceleration: %f\n", originalMouseSpeed);
139 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), -1.0) != kIOReturnSuccess)
141 Con_Print("Could not disable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
142 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
147 Con_Print("Could not disable mouse acceleration (failed at IOHIDGetAccelerationWithKey).\n");
148 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
150 IOServiceClose(mouseDev);
154 Con_Print("Could not disable mouse acceleration (failed at IO_GetIOHandle).\n");
155 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
159 mouse_x = mouse_y = 0;
160 vid_usingmouse = true;
167 if(originalMouseSpeed != -1.0)
169 io_connect_t mouseDev = IN_GetIOHandle();
172 Con_DPrintf("restoring mouse acceleration to: %f\n", originalMouseSpeed);
173 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), originalMouseSpeed) != kIOReturnSuccess)
174 Con_Print("Could not re-enable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
175 IOServiceClose(mouseDev);
178 Con_Print("Could not re-enable mouse acceleration (failed at IO_GetIOHandle).\n");
181 CGAssociateMouseAndMouseCursorPosition(true);
182 CGDisplayShowCursor(CGMainDisplayID());
184 vid_usingmouse = false;
189 #define GAMMA_TABLE_SIZE 256
190 void VID_Finish (qboolean allowmousegrab)
192 qboolean vid_usemouse;
193 qboolean vid_usevsync;
195 // handle the mouse state when windowed if that's changed
196 vid_usemouse = false;
197 if (allowmousegrab && vid_mouse.integer && !key_consoleactive && (key_dest != key_game || !cls.demoplayback))
199 if (!vid_activewindow)
200 vid_usemouse = false;
201 if (vid_isfullscreen)
203 IN_Activate(vid_usemouse);
205 // handle changes of the vsync option
206 vid_usevsync = (vid_vsync.integer && !cls.timedemo);
207 if (vid_usingvsync != vid_usevsync)
209 GLint sync = (vid_usevsync ? 1 : 0);
211 if (qaglSetInteger(context, AGL_SWAP_INTERVAL, &sync) == GL_TRUE)
213 vid_usingvsync = vid_usevsync;
214 Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
217 Con_Printf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
220 if (r_render.integer)
223 if (r_speeds.integer || gl_finish.integer)
225 qglFinish();CHECKGLERROR
227 qaglSwapBuffers(context);
229 VID_UpdateGamma(false, GAMMA_TABLE_SIZE);
231 if (apple_multithreadedgl.integer)
233 if (!multithreadedgl)
235 if(qCGLGetCurrentContext && qCGLEnable && qCGLDisable)
237 CGLContextObj ctx = qCGLGetCurrentContext();
238 CGLError e = qCGLEnable(ctx, kCGLCEMPEngine);
240 multithreadedgl = true;
243 Con_Printf("WARNING: can't enable multithreaded GL, error %d\n", (int) e);
244 Cvar_SetValueQuick(&apple_multithreadedgl, 0);
249 Con_Printf("WARNING: can't enable multithreaded GL, CGL functions not present\n");
250 Cvar_SetValueQuick(&apple_multithreadedgl, 0);
258 if(qCGLGetCurrentContext && qCGLEnable && qCGLDisable)
260 CGLContextObj ctx = qCGLGetCurrentContext();
261 qCGLDisable(ctx, kCGLCEMPEngine);
262 multithreadedgl = false;
268 int VID_SetGamma(unsigned short *ramps, int rampsize)
270 CGGammaValue table_red [GAMMA_TABLE_SIZE];
271 CGGammaValue table_green [GAMMA_TABLE_SIZE];
272 CGGammaValue table_blue [GAMMA_TABLE_SIZE];
275 // Convert the unsigned short table into 3 float tables
276 for (i = 0; i < rampsize; i++)
277 table_red[i] = (float)ramps[i] / 65535.0f;
278 for (i = 0; i < rampsize; i++)
279 table_green[i] = (float)ramps[i + rampsize] / 65535.0f;
280 for (i = 0; i < rampsize; i++)
281 table_blue[i] = (float)ramps[i + 2 * rampsize] / 65535.0f;
283 if (CGSetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue) != CGDisplayNoErr)
285 Con_Print("VID_SetGamma: ERROR: CGSetDisplayTransferByTable failed!\n");
292 int VID_GetGamma(unsigned short *ramps, int rampsize)
294 CGGammaValue table_red [GAMMA_TABLE_SIZE];
295 CGGammaValue table_green [GAMMA_TABLE_SIZE];
296 CGGammaValue table_blue [GAMMA_TABLE_SIZE];
297 CGTableCount actualsize = 0;
300 // Get the gamma ramps from the system
301 if (CGGetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue, &actualsize) != CGDisplayNoErr)
303 Con_Print("VID_GetGamma: ERROR: CGGetDisplayTransferByTable failed!\n");
306 if (actualsize != (unsigned int)rampsize)
308 Con_Printf("VID_GetGamma: ERROR: invalid gamma table size (%u != %u)\n", actualsize, rampsize);
312 // Convert the 3 float tables into 1 unsigned short table
313 for (i = 0; i < rampsize; i++)
314 ramps[i] = table_red[i] * 65535.0f;
315 for (i = 0; i < rampsize; i++)
316 ramps[i + rampsize] = table_green[i] * 65535.0f;
317 for (i = 0; i < rampsize; i++)
318 ramps[i + 2 * rampsize] = table_blue[i] * 65535.0f;
323 void signal_handler(int sig)
325 printf("Received signal %d, exiting...\n", sig);
326 VID_RestoreSystemGamma();
332 signal(SIGHUP, signal_handler);
333 signal(SIGINT, signal_handler);
334 signal(SIGQUIT, signal_handler);
335 signal(SIGILL, signal_handler);
336 signal(SIGTRAP, signal_handler);
337 signal(SIGIOT, signal_handler);
338 signal(SIGBUS, signal_handler);
339 signal(SIGFPE, signal_handler);
340 signal(SIGSEGV, signal_handler);
341 signal(SIGTERM, signal_handler);
346 InitSig(); // trap evil signals
347 Cvar_RegisterVariable(&apple_multithreadedgl);
348 Cvar_RegisterVariable(&apple_mouse_noaccel);
349 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
350 if (COM_CheckParm ("-nomouse"))
354 static void *prjobj = NULL;
355 static void *cglobj = NULL;
357 static void GL_CloseLibrary(void)
368 gl_platformextensions = "";
371 static int GL_OpenLibrary(void)
373 const char *name = "/System/Library/Frameworks/AGL.framework/AGL";
374 const char *name2 = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
376 Con_Printf("Loading OpenGL driver %s\n", name);
378 if (!(prjobj = dlopen(name, RTLD_LAZY)))
380 Con_Printf("Unable to open symbol list for %s\n", name);
383 strlcpy(gl_driver, name, sizeof(gl_driver));
385 Con_Printf("Loading OpenGL driver %s\n", name2);
386 if (!(cglobj = dlopen(name2, RTLD_LAZY)))
387 Con_Printf("Unable to open symbol list for %s; multithreaded GL disabled\n", name);
392 void *GL_GetProcAddress(const char *name)
394 return dlsym(prjobj, name);
397 static void *CGL_GetProcAddress(const char *name)
401 return dlsym(cglobj, name);
404 void VID_Shutdown(void)
406 if (context == NULL && window == NULL)
410 VID_RestoreSystemGamma();
414 qaglDestroyContext(context);
418 if (vid_isfullscreen)
419 CGReleaseAllDisplays();
423 DisposeWindow(window);
428 vid_isfullscreen = false;
434 // Since the event handler can be called at any time, we store the events for later processing
435 static qboolean AsyncEvent_Quitting = false;
436 static qboolean AsyncEvent_Collapsed = false;
437 static OSStatus MainWindowEventHandler (EventHandlerCallRef nextHandler, EventRef event, void *userData)
439 OSStatus err = noErr;
441 switch (GetEventKind (event))
443 case kEventWindowClosed:
444 AsyncEvent_Quitting = true;
448 case kEventWindowCollapsing:
449 AsyncEvent_Collapsed = true;
452 // Undocked / restored (end)
453 case kEventWindowExpanded:
454 AsyncEvent_Collapsed = false;
458 err = eventNotHandledErr;
465 static void VID_AppFocusChanged(qboolean windowIsActive)
467 if (vid_activewindow != windowIsActive)
469 vid_activewindow = windowIsActive;
470 if (!vid_activewindow)
471 VID_RestoreSystemGamma();
474 if (sound_active != windowIsActive)
476 sound_active = windowIsActive;
484 static void VID_ProcessPendingAsyncEvents (void)
486 // Collapsed / expanded
487 if (AsyncEvent_Collapsed != vid_hidden)
489 vid_hidden = !vid_hidden;
490 VID_AppFocusChanged(!vid_hidden);
494 if (AsyncEvent_Quitting)
498 static void VID_BuildAGLAttrib(GLint *attrib, qboolean stencil, qboolean fullscreen, qboolean stereobuffer)
500 *attrib++ = AGL_RGBA;
501 *attrib++ = AGL_RED_SIZE;*attrib++ = 1;
502 *attrib++ = AGL_GREEN_SIZE;*attrib++ = 1;
503 *attrib++ = AGL_BLUE_SIZE;*attrib++ = 1;
504 *attrib++ = AGL_DOUBLEBUFFER;
505 *attrib++ = AGL_DEPTH_SIZE;*attrib++ = 1;
507 // if stencil is enabled, ask for alpha too
510 *attrib++ = AGL_STENCIL_SIZE;*attrib++ = 8;
511 *attrib++ = AGL_ALPHA_SIZE;*attrib++ = 1;
514 *attrib++ = AGL_FULLSCREEN;
516 *attrib++ = AGL_STEREO;
517 *attrib++ = AGL_NONE;
520 int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate, int stereobuffer)
522 const EventTypeSpec winEvents[] =
524 { kEventClassWindow, kEventWindowClosed },
525 { kEventClassWindow, kEventWindowCollapsing },
526 { kEventClassWindow, kEventWindowExpanded },
528 OSStatus carbonError;
530 CFStringRef windowTitle;
531 AGLPixelFormat pixelFormat;
532 GLint attributes [32];
535 if (!GL_OpenLibrary())
537 Con_Printf("Unable to load GL driver\n");
541 if ((qaglChoosePixelFormat = (AGLPixelFormat (*) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList))GL_GetProcAddress("aglChoosePixelFormat")) == NULL
542 || (qaglCreateContext = (AGLContext (*) (AGLPixelFormat pix, AGLContext share))GL_GetProcAddress("aglCreateContext")) == NULL
543 || (qaglDestroyContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglDestroyContext")) == NULL
544 || (qaglDestroyPixelFormat = (void (*) (AGLPixelFormat pix))GL_GetProcAddress("aglDestroyPixelFormat")) == NULL
545 || (qaglErrorString = (const GLubyte* (*) (GLenum code))GL_GetProcAddress("aglErrorString")) == NULL
546 || (qaglGetError = (GLenum (*) (void))GL_GetProcAddress("aglGetError")) == NULL
547 || (qaglSetCurrentContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglSetCurrentContext")) == NULL
548 || (qaglSetDrawable = (GLboolean (*) (AGLContext ctx, AGLDrawable draw))GL_GetProcAddress("aglSetDrawable")) == NULL
549 || (qaglSetFullScreen = (GLboolean (*) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device))GL_GetProcAddress("aglSetFullScreen")) == NULL
550 || (qaglSetInteger = (GLboolean (*) (AGLContext ctx, GLenum pname, const GLint *params))GL_GetProcAddress("aglSetInteger")) == NULL
551 || (qaglSwapBuffers = (void (*) (AGLContext ctx))GL_GetProcAddress("aglSwapBuffers")) == NULL
554 Con_Printf("AGL functions not found\n");
555 ReleaseWindow(window);
559 qCGLEnable = (CGLError (*) (CGLContextObj ctx, CGLContextEnable pname)) CGL_GetProcAddress("CGLEnable");
560 qCGLDisable = (CGLError (*) (CGLContextObj ctx, CGLContextEnable pname)) CGL_GetProcAddress("CGLDisable");
561 qCGLGetCurrentContext = (CGLContextObj (*) (void)) CGL_GetProcAddress("CGLGetCurrentContext");
562 if(!qCGLEnable || !qCGLDisable || !qCGLGetCurrentContext)
563 Con_Printf("CGL functions not found; disabling multithreaded OpenGL\n");
565 // Ignore the events from the previous window
566 AsyncEvent_Quitting = false;
567 AsyncEvent_Collapsed = false;
569 // Create the window, a bit towards the center of the screen
570 windowBounds.left = 100;
571 windowBounds.top = 100;
572 windowBounds.right = width + 100;
573 windowBounds.bottom = height + 100;
574 carbonError = CreateNewWindow(kDocumentWindowClass, kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute, &windowBounds, &window);
575 if (carbonError != noErr || window == NULL)
577 Con_Printf("Unable to create window (error %u)\n", (unsigned)carbonError);
581 // Set the window title
582 windowTitle = CFSTR("DarkPlaces AGL");
583 SetWindowTitleWithCFString(window, windowTitle);
585 // Install the callback function for the window events we can't get
586 // through ReceiveNextEvent (i.e. close, collapse, and expand)
587 InstallWindowEventHandler (window, NewEventHandlerUPP (MainWindowEventHandler),
588 GetEventTypeCount(winEvents), winEvents, window, NULL);
590 // Create the desired attribute list
591 VID_BuildAGLAttrib(attributes, bpp == 32, fullscreen, stereobuffer);
596 pixelFormat = qaglChoosePixelFormat(NULL, 0, attributes);
597 error = qaglGetError();
598 if (error != AGL_NO_ERROR)
600 Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
601 (char *)qaglErrorString(error));
602 ReleaseWindow(window);
606 else // Output is fullScreen
608 CGDirectDisplayID mainDisplay;
609 CFDictionaryRef refDisplayMode;
612 // Get the mainDisplay and set resolution to current
613 mainDisplay = CGMainDisplayID();
614 CGDisplayCapture(mainDisplay);
616 // TOCHECK: not sure whether or not it's necessary to change the resolution
617 // "by hand", or if aglSetFullscreen does the job anyway
618 refDisplayMode = CGDisplayBestModeForParametersAndRefreshRate(mainDisplay, bpp, width, height, refreshrate, NULL);
619 CGDisplaySwitchToMode(mainDisplay, refDisplayMode);
620 DMGetGDeviceByDisplayID((DisplayIDType)mainDisplay, &gdhDisplay, false);
622 // Set pixel format with built attribs
623 // Note: specifying a device is *required* for AGL_FullScreen
624 pixelFormat = qaglChoosePixelFormat(&gdhDisplay, 1, attributes);
625 error = qaglGetError();
626 if (error != AGL_NO_ERROR)
628 Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
629 (char *)qaglErrorString(error));
630 ReleaseWindow(window);
635 // Create a context using the pform
636 context = qaglCreateContext(pixelFormat, NULL);
637 error = qaglGetError();
638 if (error != AGL_NO_ERROR)
640 Con_Printf("qaglCreateContext FAILED: %s\n",
641 (char *)qaglErrorString(error));
644 // Make the context the current one ('enable' it)
645 qaglSetCurrentContext(context);
646 error = qaglGetError();
647 if (error != AGL_NO_ERROR)
649 Con_Printf("qaglSetCurrentContext FAILED: %s\n",
650 (char *)qaglErrorString(error));
651 ReleaseWindow(window);
656 qaglDestroyPixelFormat(pixelFormat);
658 // Attempt fullscreen if requested
661 qaglSetFullScreen (context, width, height, refreshrate, 0);
662 error = qaglGetError();
663 if (error != AGL_NO_ERROR)
665 Con_Printf("qaglSetFullScreen FAILED: %s\n",
666 (char *)qaglErrorString(error));
672 // Set Window as Drawable
673 qaglSetDrawable(context, GetWindowPort(window));
674 error = qaglGetError();
675 if (error != AGL_NO_ERROR)
677 Con_Printf("qaglSetDrawable FAILED: %s\n",
678 (char *)qaglErrorString(error));
679 ReleaseWindow(window);
687 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
688 Sys_Error("glGetString not found in %s", gl_driver);
690 gl_renderer = (const char *)qglGetString(GL_RENDERER);
691 gl_vendor = (const char *)qglGetString(GL_VENDOR);
692 gl_version = (const char *)qglGetString(GL_VERSION);
693 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
695 gl_videosyncavailable = true;
697 multithreadedgl = false;
698 vid_isfullscreen = fullscreen;
699 vid_usingmouse = false;
701 vid_activewindow = true;
705 SelectWindow(window);
711 static void Handle_KeyMod(UInt32 keymod)
713 const struct keymod_to_event_s { UInt32 keybit; keynum_t event; } keymod_events [] =
716 { shiftKey, K_SHIFT },
717 { alphaLock, K_CAPSLOCK },
718 { optionKey, K_ALT },
719 { controlKey, K_CTRL },
720 { kEventKeyModifierNumLockMask, K_NUMLOCK },
721 { kEventKeyModifierFnMask, K_AUX2 }
723 static UInt32 prev_keymod = 0;
727 modChanges = prev_keymod ^ keymod;
731 for (i = 0; i < sizeof(keymod_events) / sizeof(keymod_events[0]); i++)
733 UInt32 keybit = keymod_events[i].keybit;
735 if ((modChanges & keybit) != 0)
736 Key_Event(keymod_events[i].event, '\0', (keymod & keybit) != 0);
739 prev_keymod = keymod;
742 static void Handle_Key(unsigned char charcode, UInt32 mackeycode, qboolean keypressed)
744 unsigned int keycode = 0;
789 keycode = K_SCROLLOCK;
795 keycode = K_BACKSPACE;
810 keycode = K_KP_EQUALS;
813 keycode = K_KP_DIVIDE;
816 keycode = K_KP_MULTIPLY;
840 keycode = K_KP_MINUS;
843 keycode = K_CAPSLOCK;
871 keycode = K_KP_ENTER;
877 keycode = K_KP_PERIOD;
882 case kUpArrowCharCode:
885 case kLeftArrowCharCode:
886 keycode = K_LEFTARROW;
888 case kDownArrowCharCode:
889 keycode = K_DOWNARROW;
891 case kRightArrowCharCode:
892 keycode = K_RIGHTARROW;
896 // characters 0 and 191 are sent by the mouse buttons (?!)
899 if ('A' <= charcode && charcode <= 'Z')
901 keycode = charcode + ('a' - 'A'); // lowercase it
904 else if (charcode >= 32)
910 Con_DPrintf(">> UNKNOWN char/keycode: %d/%u <<\n", charcode, (unsigned) mackeycode);
915 Key_Event(keycode, ascii, keypressed);
918 void Sys_SendKeyEvents(void)
921 EventTargetRef theTarget;
923 // Start by processing the asynchronous events we received since the previous frame
924 VID_ProcessPendingAsyncEvents();
926 theTarget = GetEventDispatcherTarget();
927 while (ReceiveNextEvent(0, NULL, kEventDurationNoWait, true, &theEvent) == noErr)
929 UInt32 eventClass = GetEventClass(theEvent);
930 UInt32 eventKind = GetEventKind(theEvent);
934 case kEventClassMouse:
936 EventMouseButton theButton;
941 case kEventMouseDown:
943 GetEventParameter(theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(theButton), NULL, &theButton);
947 case kEventMouseButtonPrimary:
950 case kEventMouseButtonSecondary:
953 case kEventMouseButtonTertiary:
957 Key_Event(key, '\0', eventKind == kEventMouseDown);
960 // Note: These two events are mutual exclusives
961 // Treat MouseDragged in the same statement, so we don't block MouseMoved while a mousebutton is held
962 case kEventMouseMoved:
963 case kEventMouseDragged:
967 GetEventParameter(theEvent, kEventParamMouseDelta, typeHIPoint, NULL, sizeof(deltaPos), NULL, &deltaPos);
969 mouse_x += deltaPos.x;
970 mouse_y += deltaPos.y;
974 case kEventMouseWheelMoved:
977 unsigned int wheelEvent;
979 GetEventParameter(theEvent, kEventParamMouseWheelDelta, typeSInt32, NULL, sizeof(delta), NULL, &delta);
981 wheelEvent = (delta > 0) ? K_MWHEELUP : K_MWHEELDOWN;
982 Key_Event(wheelEvent, 0, true);
983 Key_Event(wheelEvent, 0, false);
988 Con_Printf (">> kEventClassMouse (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
993 case kEventClassKeyboard:
1000 case kEventRawKeyDown:
1001 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charcode), NULL, &charcode);
1002 GetEventParameter(theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(keycode), NULL, &keycode);
1003 Handle_Key(charcode, keycode, true);
1006 case kEventRawKeyRepeat:
1009 case kEventRawKeyUp:
1010 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charcode), NULL, &charcode);
1011 GetEventParameter(theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(keycode), NULL, &keycode);
1012 Handle_Key(charcode, keycode, false);
1015 case kEventRawKeyModifiersChanged:
1018 GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(keymod), NULL, &keymod);
1019 Handle_KeyMod(keymod);
1023 case kEventHotKeyPressed:
1026 case kEventHotKeyReleased:
1029 case kEventMouseWheelMoved:
1033 Con_Printf (">> kEventClassKeyboard (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1039 case kEventClassTextInput:
1040 Con_Printf(">> kEventClassTextInput (%d) <<\n", (int)eventKind);
1043 case kEventClassApplication:
1046 case kEventAppActivated :
1047 VID_AppFocusChanged(true);
1049 case kEventAppDeactivated:
1050 VID_AppFocusChanged(false);
1055 case kEventAppActiveWindowChanged:
1058 Con_Printf(">> kEventClassApplication (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1063 case kEventClassAppleEvent:
1066 case kEventAppleEvent :
1069 Con_Printf(">> kEventClassAppleEvent (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1074 case kEventClassWindow:
1077 case kEventWindowUpdate :
1080 Con_Printf(">> kEventClassWindow (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1085 case kEventClassControl:
1089 /*Con_Printf(">> UNKNOWN eventClass: %c%c%c%c, eventKind: %d <<\n",
1090 eventClass >> 24, (eventClass >> 16) & 0xFF,
1091 (eventClass >> 8) & 0xFF, eventClass & 0xFF,
1096 SendEventToEventTarget (theEvent, theTarget);
1097 ReleaseEvent(theEvent);
1105 in_mouse_x = mouse_x;
1106 in_mouse_y = mouse_y;