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
29 #include <OpenGL/OpenGL.h>
30 #include <Carbon/Carbon.h>
31 #include <IOKit/hidsystem/IOHIDLib.h>
32 #include <IOKit/hidsystem/IOHIDParameter.h>
33 #include <IOKit/hidsystem/event_status_driver.h>
34 #include "vid_agl_mackeys.h" // this is SDL/src/video/maccommon/SDL_mackeys.h
36 #ifndef kCGLCEMPEngine
37 #define kCGLCEMPEngine 313
40 // Tell startup code that we have a client
41 int cl_available = true;
43 qboolean vid_supportrefreshrate = true;
46 AGLPixelFormat (*qaglChoosePixelFormat) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList);
47 AGLContext (*qaglCreateContext) (AGLPixelFormat pix, AGLContext share);
48 GLboolean (*qaglDestroyContext) (AGLContext ctx);
49 void (*qaglDestroyPixelFormat) (AGLPixelFormat pix);
50 const GLubyte* (*qaglErrorString) (GLenum code);
51 GLenum (*qaglGetError) (void);
52 GLboolean (*qaglSetCurrentContext) (AGLContext ctx);
53 GLboolean (*qaglSetDrawable) (AGLContext ctx, AGLDrawable draw);
54 GLboolean (*qaglSetFullScreen) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device);
55 GLboolean (*qaglSetInteger) (AGLContext ctx, GLenum pname, const GLint *params);
56 void (*qaglSwapBuffers) (AGLContext ctx);
57 CGLError (*qCGLEnable) (CGLContextObj ctx, CGLContextEnable pname);
58 CGLError (*qCGLDisable) (CGLContextObj ctx, CGLContextEnable pname);
59 CGLContextObj (*qCGLGetCurrentContext) (void);
61 static qboolean multithreadedgl;
62 static qboolean mouse_avail = true;
63 static qboolean vid_usingmouse = false;
64 static float mouse_x, mouse_y;
66 static qboolean vid_isfullscreen = false;
67 static qboolean vid_usingvsync = false;
69 static qboolean sound_active = true;
71 static int scr_width, scr_height;
73 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)"};
74 static cvar_t apple_mouse_noaccel = {CVAR_SAVE, "apple_mouse_noaccel", "1", "disables mouse acceleration while DarkPlaces is active"};
76 static AGLContext context;
77 static WindowRef window;
79 static double originalMouseSpeed = -1.0;
81 io_connect_t IN_GetIOHandle()
83 io_connect_t iohandle = MACH_PORT_NULL;
85 io_service_t iohidsystem = MACH_PORT_NULL;
86 mach_port_t masterport;
88 status = IOMasterPort(MACH_PORT_NULL, &masterport);
89 if(status != KERN_SUCCESS)
92 iohidsystem = IORegistryEntryFromPath(masterport, kIOServicePlane ":/IOResources/IOHIDSystem");
96 status = IOServiceOpen(iohidsystem, mach_task_self(), kIOHIDParamConnectType, &iohandle);
97 IOObjectRelease(iohidsystem);
102 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
106 *height = scr_height;
109 static void IN_Activate( qboolean grab )
113 if (!vid_usingmouse && mouse_avail && window)
118 SelectWindow(window);
119 CGDisplayHideCursor(CGMainDisplayID());
121 // Put the mouse cursor at the center of the window
122 GetWindowBounds (window, kWindowContentRgn, &winBounds);
123 winCenter.x = (winBounds.left + winBounds.right) / 2;
124 winCenter.y = (winBounds.top + winBounds.bottom) / 2;
125 CGWarpMouseCursorPosition(winCenter);
127 // Lock the mouse pointer at its current position
128 CGAssociateMouseAndMouseCursorPosition(false);
130 // Save the status of mouse acceleration
131 originalMouseSpeed = -1.0; // in case of error
132 if(apple_mouse_noaccel.integer)
134 io_connect_t mouseDev = IN_GetIOHandle();
137 if(IOHIDGetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), &originalMouseSpeed) == kIOReturnSuccess)
139 Con_DPrintf("previous mouse acceleration: %f\n", originalMouseSpeed);
140 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), -1.0) != kIOReturnSuccess)
142 Con_Print("Could not disable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
143 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
148 Con_Print("Could not disable mouse acceleration (failed at IOHIDGetAccelerationWithKey).\n");
149 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
151 IOServiceClose(mouseDev);
155 Con_Print("Could not disable mouse acceleration (failed at IO_GetIOHandle).\n");
156 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
160 mouse_x = mouse_y = 0;
161 vid_usingmouse = true;
168 if(originalMouseSpeed != -1.0)
170 io_connect_t mouseDev = IN_GetIOHandle();
173 Con_DPrintf("restoring mouse acceleration to: %f\n", originalMouseSpeed);
174 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), originalMouseSpeed) != kIOReturnSuccess)
175 Con_Print("Could not re-enable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
176 IOServiceClose(mouseDev);
179 Con_Print("Could not re-enable mouse acceleration (failed at IO_GetIOHandle).\n");
182 CGAssociateMouseAndMouseCursorPosition(true);
183 CGDisplayShowCursor(CGMainDisplayID());
185 vid_usingmouse = false;
190 #define GAMMA_TABLE_SIZE 256
191 void VID_Finish (qboolean allowmousegrab)
193 qboolean vid_usemouse;
194 qboolean vid_usevsync;
196 // handle the mouse state when windowed if that's changed
197 vid_usemouse = false;
198 if (allowmousegrab && vid_mouse.integer && !key_consoleactive && (key_dest != key_game || !cls.demoplayback))
200 if (!vid_activewindow)
201 vid_usemouse = false;
202 if (vid_isfullscreen)
204 IN_Activate(vid_usemouse);
206 // handle changes of the vsync option
207 vid_usevsync = (vid_vsync.integer && !cls.timedemo);
208 if (vid_usingvsync != vid_usevsync)
210 GLint sync = (vid_usevsync ? 1 : 0);
212 if (qaglSetInteger(context, AGL_SWAP_INTERVAL, &sync) == GL_TRUE)
214 vid_usingvsync = vid_usevsync;
215 Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
218 Con_Printf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
221 if (r_render.integer)
224 if (r_speeds.integer || gl_finish.integer)
226 qglFinish();CHECKGLERROR
228 qaglSwapBuffers(context);
230 VID_UpdateGamma(false, GAMMA_TABLE_SIZE);
232 if (apple_multithreadedgl.integer)
234 if (!multithreadedgl)
236 if(qCGLGetCurrentContext && qCGLEnable && qCGLDisable)
238 CGLContextObj ctx = qCGLGetCurrentContext();
239 CGLError e = qCGLEnable(ctx, kCGLCEMPEngine);
241 multithreadedgl = true;
244 Con_Printf("WARNING: can't enable multithreaded GL, error %d\n", (int) e);
245 Cvar_SetValueQuick(&apple_multithreadedgl, 0);
250 Con_Printf("WARNING: can't enable multithreaded GL, CGL functions not present\n");
251 Cvar_SetValueQuick(&apple_multithreadedgl, 0);
259 if(qCGLGetCurrentContext && qCGLEnable && qCGLDisable)
261 CGLContextObj ctx = qCGLGetCurrentContext();
262 qCGLDisable(ctx, kCGLCEMPEngine);
263 multithreadedgl = false;
269 int VID_SetGamma(unsigned short *ramps, int rampsize)
271 CGGammaValue table_red [GAMMA_TABLE_SIZE];
272 CGGammaValue table_green [GAMMA_TABLE_SIZE];
273 CGGammaValue table_blue [GAMMA_TABLE_SIZE];
276 // Convert the unsigned short table into 3 float tables
277 for (i = 0; i < rampsize; i++)
278 table_red[i] = (float)ramps[i] / 65535.0f;
279 for (i = 0; i < rampsize; i++)
280 table_green[i] = (float)ramps[i + rampsize] / 65535.0f;
281 for (i = 0; i < rampsize; i++)
282 table_blue[i] = (float)ramps[i + 2 * rampsize] / 65535.0f;
284 if (CGSetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue) != CGDisplayNoErr)
286 Con_Print("VID_SetGamma: ERROR: CGSetDisplayTransferByTable failed!\n");
293 int VID_GetGamma(unsigned short *ramps, int rampsize)
295 CGGammaValue table_red [GAMMA_TABLE_SIZE];
296 CGGammaValue table_green [GAMMA_TABLE_SIZE];
297 CGGammaValue table_blue [GAMMA_TABLE_SIZE];
298 CGTableCount actualsize = 0;
301 // Get the gamma ramps from the system
302 if (CGGetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue, &actualsize) != CGDisplayNoErr)
304 Con_Print("VID_GetGamma: ERROR: CGGetDisplayTransferByTable failed!\n");
307 if (actualsize != (unsigned int)rampsize)
309 Con_Printf("VID_GetGamma: ERROR: invalid gamma table size (%u != %u)\n", actualsize, rampsize);
313 // Convert the 3 float tables into 1 unsigned short table
314 for (i = 0; i < rampsize; i++)
315 ramps[i] = table_red[i] * 65535.0f;
316 for (i = 0; i < rampsize; i++)
317 ramps[i + rampsize] = table_green[i] * 65535.0f;
318 for (i = 0; i < rampsize; i++)
319 ramps[i + 2 * rampsize] = table_blue[i] * 65535.0f;
324 void signal_handler(int sig)
326 printf("Received signal %d, exiting...\n", sig);
327 VID_RestoreSystemGamma();
333 signal(SIGHUP, signal_handler);
334 signal(SIGINT, signal_handler);
335 signal(SIGQUIT, signal_handler);
336 signal(SIGILL, signal_handler);
337 signal(SIGTRAP, signal_handler);
338 signal(SIGIOT, signal_handler);
339 signal(SIGBUS, signal_handler);
340 signal(SIGFPE, signal_handler);
341 signal(SIGSEGV, signal_handler);
342 signal(SIGTERM, signal_handler);
347 InitSig(); // trap evil signals
348 Cvar_RegisterVariable(&apple_multithreadedgl);
349 Cvar_RegisterVariable(&apple_mouse_noaccel);
350 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
351 if (COM_CheckParm ("-nomouse"))
355 static void *prjobj = NULL;
356 static void *cglobj = NULL;
358 static void GL_CloseLibrary(void)
369 gl_platformextensions = "";
372 static int GL_OpenLibrary(void)
374 const char *name = "/System/Library/Frameworks/AGL.framework/AGL";
375 const char *name2 = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
377 Con_Printf("Loading OpenGL driver %s\n", name);
379 if (!(prjobj = dlopen(name, RTLD_LAZY)))
381 Con_Printf("Unable to open symbol list for %s\n", name);
384 strlcpy(gl_driver, name, sizeof(gl_driver));
386 Con_Printf("Loading OpenGL driver %s\n", name2);
387 if (!(cglobj = dlopen(name2, RTLD_LAZY)))
388 Con_Printf("Unable to open symbol list for %s; multithreaded GL disabled\n", name);
393 void *GL_GetProcAddress(const char *name)
395 return dlsym(prjobj, name);
398 static void *CGL_GetProcAddress(const char *name)
402 return dlsym(cglobj, name);
405 void VID_Shutdown(void)
407 if (context == NULL && window == NULL)
411 VID_RestoreSystemGamma();
415 qaglDestroyContext(context);
419 if (vid_isfullscreen)
420 CGReleaseAllDisplays();
424 DisposeWindow(window);
429 vid_isfullscreen = false;
435 // Since the event handler can be called at any time, we store the events for later processing
436 static qboolean AsyncEvent_Quitting = false;
437 static qboolean AsyncEvent_Collapsed = false;
438 static OSStatus MainWindowEventHandler (EventHandlerCallRef nextHandler, EventRef event, void *userData)
440 OSStatus err = noErr;
442 switch (GetEventKind (event))
444 case kEventWindowClosed:
445 AsyncEvent_Quitting = true;
449 case kEventWindowCollapsing:
450 AsyncEvent_Collapsed = true;
453 // Undocked / restored (end)
454 case kEventWindowExpanded:
455 AsyncEvent_Collapsed = false;
459 err = eventNotHandledErr;
466 static void VID_AppFocusChanged(qboolean windowIsActive)
468 if (vid_activewindow != windowIsActive)
470 vid_activewindow = windowIsActive;
471 if (!vid_activewindow)
472 VID_RestoreSystemGamma();
475 if (sound_active != windowIsActive)
477 sound_active = windowIsActive;
485 static void VID_ProcessPendingAsyncEvents (void)
487 // Collapsed / expanded
488 if (AsyncEvent_Collapsed != vid_hidden)
490 vid_hidden = !vid_hidden;
491 VID_AppFocusChanged(!vid_hidden);
495 if (AsyncEvent_Quitting)
499 static void VID_BuildAGLAttrib(GLint *attrib, qboolean stencil, qboolean fullscreen, qboolean stereobuffer)
501 *attrib++ = AGL_RGBA;
502 *attrib++ = AGL_RED_SIZE;*attrib++ = 1;
503 *attrib++ = AGL_GREEN_SIZE;*attrib++ = 1;
504 *attrib++ = AGL_BLUE_SIZE;*attrib++ = 1;
505 *attrib++ = AGL_DOUBLEBUFFER;
506 *attrib++ = AGL_DEPTH_SIZE;*attrib++ = 1;
508 // if stencil is enabled, ask for alpha too
511 *attrib++ = AGL_STENCIL_SIZE;*attrib++ = 8;
512 *attrib++ = AGL_ALPHA_SIZE;*attrib++ = 1;
515 *attrib++ = AGL_FULLSCREEN;
517 *attrib++ = AGL_STEREO;
518 *attrib++ = AGL_NONE;
521 int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate, int stereobuffer)
523 const EventTypeSpec winEvents[] =
525 { kEventClassWindow, kEventWindowClosed },
526 { kEventClassWindow, kEventWindowCollapsing },
527 { kEventClassWindow, kEventWindowExpanded },
529 OSStatus carbonError;
531 CFStringRef windowTitle;
532 AGLPixelFormat pixelFormat;
533 GLint attributes [32];
536 if (!GL_OpenLibrary())
538 Con_Printf("Unable to load GL driver\n");
542 if ((qaglChoosePixelFormat = (AGLPixelFormat (*) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList))GL_GetProcAddress("aglChoosePixelFormat")) == NULL
543 || (qaglCreateContext = (AGLContext (*) (AGLPixelFormat pix, AGLContext share))GL_GetProcAddress("aglCreateContext")) == NULL
544 || (qaglDestroyContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglDestroyContext")) == NULL
545 || (qaglDestroyPixelFormat = (void (*) (AGLPixelFormat pix))GL_GetProcAddress("aglDestroyPixelFormat")) == NULL
546 || (qaglErrorString = (const GLubyte* (*) (GLenum code))GL_GetProcAddress("aglErrorString")) == NULL
547 || (qaglGetError = (GLenum (*) (void))GL_GetProcAddress("aglGetError")) == NULL
548 || (qaglSetCurrentContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglSetCurrentContext")) == NULL
549 || (qaglSetDrawable = (GLboolean (*) (AGLContext ctx, AGLDrawable draw))GL_GetProcAddress("aglSetDrawable")) == NULL
550 || (qaglSetFullScreen = (GLboolean (*) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device))GL_GetProcAddress("aglSetFullScreen")) == NULL
551 || (qaglSetInteger = (GLboolean (*) (AGLContext ctx, GLenum pname, const GLint *params))GL_GetProcAddress("aglSetInteger")) == NULL
552 || (qaglSwapBuffers = (void (*) (AGLContext ctx))GL_GetProcAddress("aglSwapBuffers")) == NULL
555 Con_Printf("AGL functions not found\n");
556 ReleaseWindow(window);
560 qCGLEnable = (CGLError (*) (CGLContextObj ctx, CGLContextEnable pname)) CGL_GetProcAddress("CGLEnable");
561 qCGLDisable = (CGLError (*) (CGLContextObj ctx, CGLContextEnable pname)) CGL_GetProcAddress("CGLDisable");
562 qCGLGetCurrentContext = (CGLContextObj (*) (void)) CGL_GetProcAddress("CGLGetCurrentContext");
563 if(!qCGLEnable || !qCGLDisable || !qCGLGetCurrentContext)
564 Con_Printf("CGL functions not found; disabling multithreaded OpenGL\n");
566 // Ignore the events from the previous window
567 AsyncEvent_Quitting = false;
568 AsyncEvent_Collapsed = false;
570 // Create the window, a bit towards the center of the screen
571 windowBounds.left = 100;
572 windowBounds.top = 100;
573 windowBounds.right = width + 100;
574 windowBounds.bottom = height + 100;
575 carbonError = CreateNewWindow(kDocumentWindowClass, kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute, &windowBounds, &window);
576 if (carbonError != noErr || window == NULL)
578 Con_Printf("Unable to create window (error %u)\n", (unsigned)carbonError);
582 // Set the window title
583 windowTitle = CFSTR("DarkPlaces AGL");
584 SetWindowTitleWithCFString(window, windowTitle);
586 // Install the callback function for the window events we can't get
587 // through ReceiveNextEvent (i.e. close, collapse, and expand)
588 InstallWindowEventHandler (window, NewEventHandlerUPP (MainWindowEventHandler),
589 GetEventTypeCount(winEvents), winEvents, window, NULL);
591 // Create the desired attribute list
592 VID_BuildAGLAttrib(attributes, bpp == 32, fullscreen, stereobuffer);
597 pixelFormat = qaglChoosePixelFormat(NULL, 0, attributes);
598 error = qaglGetError();
599 if (error != AGL_NO_ERROR)
601 Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
602 (char *)qaglErrorString(error));
603 ReleaseWindow(window);
607 else // Output is fullScreen
609 CGDirectDisplayID mainDisplay;
610 CFDictionaryRef refDisplayMode;
613 // Get the mainDisplay and set resolution to current
614 mainDisplay = CGMainDisplayID();
615 CGDisplayCapture(mainDisplay);
617 // TOCHECK: not sure whether or not it's necessary to change the resolution
618 // "by hand", or if aglSetFullscreen does the job anyway
619 refDisplayMode = CGDisplayBestModeForParametersAndRefreshRate(mainDisplay, bpp, width, height, refreshrate, NULL);
620 CGDisplaySwitchToMode(mainDisplay, refDisplayMode);
621 DMGetGDeviceByDisplayID((DisplayIDType)mainDisplay, &gdhDisplay, false);
623 // Set pixel format with built attribs
624 // Note: specifying a device is *required* for AGL_FullScreen
625 pixelFormat = qaglChoosePixelFormat(&gdhDisplay, 1, attributes);
626 error = qaglGetError();
627 if (error != AGL_NO_ERROR)
629 Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
630 (char *)qaglErrorString(error));
631 ReleaseWindow(window);
636 // Create a context using the pform
637 context = qaglCreateContext(pixelFormat, NULL);
638 error = qaglGetError();
639 if (error != AGL_NO_ERROR)
641 Con_Printf("qaglCreateContext FAILED: %s\n",
642 (char *)qaglErrorString(error));
645 // Make the context the current one ('enable' it)
646 qaglSetCurrentContext(context);
647 error = qaglGetError();
648 if (error != AGL_NO_ERROR)
650 Con_Printf("qaglSetCurrentContext FAILED: %s\n",
651 (char *)qaglErrorString(error));
652 ReleaseWindow(window);
657 qaglDestroyPixelFormat(pixelFormat);
659 // Attempt fullscreen if requested
662 qaglSetFullScreen (context, width, height, refreshrate, 0);
663 error = qaglGetError();
664 if (error != AGL_NO_ERROR)
666 Con_Printf("qaglSetFullScreen FAILED: %s\n",
667 (char *)qaglErrorString(error));
673 // Set Window as Drawable
674 qaglSetDrawable(context, GetWindowPort(window));
675 error = qaglGetError();
676 if (error != AGL_NO_ERROR)
678 Con_Printf("qaglSetDrawable FAILED: %s\n",
679 (char *)qaglErrorString(error));
680 ReleaseWindow(window);
688 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
689 Sys_Error("glGetString not found in %s", gl_driver);
691 gl_renderer = (const char *)qglGetString(GL_RENDERER);
692 gl_vendor = (const char *)qglGetString(GL_VENDOR);
693 gl_version = (const char *)qglGetString(GL_VERSION);
694 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
696 gl_videosyncavailable = true;
698 multithreadedgl = false;
699 vid_isfullscreen = fullscreen;
700 vid_usingmouse = false;
702 vid_activewindow = true;
706 SelectWindow(window);
712 static void Handle_KeyMod(UInt32 keymod)
714 const struct keymod_to_event_s { UInt32 keybit; keynum_t event; } keymod_events [] =
717 { shiftKey, K_SHIFT },
718 { alphaLock, K_CAPSLOCK },
719 { optionKey, K_ALT },
720 { controlKey, K_CTRL },
721 { kEventKeyModifierNumLockMask, K_NUMLOCK },
722 { kEventKeyModifierFnMask, K_AUX2 }
724 static UInt32 prev_keymod = 0;
728 modChanges = prev_keymod ^ keymod;
732 for (i = 0; i < sizeof(keymod_events) / sizeof(keymod_events[0]); i++)
734 UInt32 keybit = keymod_events[i].keybit;
736 if ((modChanges & keybit) != 0)
737 Key_Event(keymod_events[i].event, '\0', (keymod & keybit) != 0);
740 prev_keymod = keymod;
743 static void Handle_Key(unsigned char charcode, UInt32 mackeycode, qboolean keypressed)
745 unsigned int keycode = 0;
790 keycode = K_SCROLLOCK;
796 keycode = K_BACKSPACE;
811 keycode = K_KP_EQUALS;
814 keycode = K_KP_DIVIDE;
817 keycode = K_KP_MULTIPLY;
841 keycode = K_KP_MINUS;
844 keycode = K_CAPSLOCK;
872 keycode = K_KP_ENTER;
878 keycode = K_KP_PERIOD;
883 case kUpArrowCharCode:
886 case kLeftArrowCharCode:
887 keycode = K_LEFTARROW;
889 case kDownArrowCharCode:
890 keycode = K_DOWNARROW;
892 case kRightArrowCharCode:
893 keycode = K_RIGHTARROW;
897 // characters 0 and 191 are sent by the mouse buttons (?!)
900 if ('A' <= charcode && charcode <= 'Z')
902 keycode = charcode + ('a' - 'A'); // lowercase it
905 else if (charcode >= 32)
911 Con_DPrintf(">> UNKNOWN char/keycode: %d/%u <<\n", charcode, (unsigned) mackeycode);
916 Key_Event(keycode, ascii, keypressed);
919 void Sys_SendKeyEvents(void)
922 EventTargetRef theTarget;
924 // Start by processing the asynchronous events we received since the previous frame
925 VID_ProcessPendingAsyncEvents();
927 theTarget = GetEventDispatcherTarget();
928 while (ReceiveNextEvent(0, NULL, kEventDurationNoWait, true, &theEvent) == noErr)
930 UInt32 eventClass = GetEventClass(theEvent);
931 UInt32 eventKind = GetEventKind(theEvent);
935 case kEventClassMouse:
937 EventMouseButton theButton;
942 case kEventMouseDown:
944 GetEventParameter(theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(theButton), NULL, &theButton);
948 case kEventMouseButtonPrimary:
951 case kEventMouseButtonSecondary:
954 case kEventMouseButtonTertiary:
958 Key_Event(key, '\0', eventKind == kEventMouseDown);
961 // Note: These two events are mutual exclusives
962 // Treat MouseDragged in the same statement, so we don't block MouseMoved while a mousebutton is held
963 case kEventMouseMoved:
964 case kEventMouseDragged:
968 GetEventParameter(theEvent, kEventParamMouseDelta, typeHIPoint, NULL, sizeof(deltaPos), NULL, &deltaPos);
970 mouse_x += deltaPos.x;
971 mouse_y += deltaPos.y;
975 case kEventMouseWheelMoved:
978 unsigned int wheelEvent;
980 GetEventParameter(theEvent, kEventParamMouseWheelDelta, typeSInt32, NULL, sizeof(delta), NULL, &delta);
982 wheelEvent = (delta > 0) ? K_MWHEELUP : K_MWHEELDOWN;
983 Key_Event(wheelEvent, 0, true);
984 Key_Event(wheelEvent, 0, false);
989 Con_Printf (">> kEventClassMouse (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
994 case kEventClassKeyboard:
1001 case kEventRawKeyDown:
1002 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charcode), NULL, &charcode);
1003 GetEventParameter(theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(keycode), NULL, &keycode);
1004 Handle_Key(charcode, keycode, true);
1007 case kEventRawKeyRepeat:
1010 case kEventRawKeyUp:
1011 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(charcode), NULL, &charcode);
1012 GetEventParameter(theEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(keycode), NULL, &keycode);
1013 Handle_Key(charcode, keycode, false);
1016 case kEventRawKeyModifiersChanged:
1019 GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(keymod), NULL, &keymod);
1020 Handle_KeyMod(keymod);
1024 case kEventHotKeyPressed:
1027 case kEventHotKeyReleased:
1030 case kEventMouseWheelMoved:
1034 Con_Printf (">> kEventClassKeyboard (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1040 case kEventClassTextInput:
1041 Con_Printf(">> kEventClassTextInput (%d) <<\n", (int)eventKind);
1044 case kEventClassApplication:
1047 case kEventAppActivated :
1048 VID_AppFocusChanged(true);
1050 case kEventAppDeactivated:
1051 VID_AppFocusChanged(false);
1056 case kEventAppActiveWindowChanged:
1059 Con_Printf(">> kEventClassApplication (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1064 case kEventClassAppleEvent:
1067 case kEventAppleEvent :
1070 Con_Printf(">> kEventClassAppleEvent (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1075 case kEventClassWindow:
1078 case kEventWindowUpdate :
1081 Con_Printf(">> kEventClassWindow (UNKNOWN eventKind: %u) <<\n", (unsigned)eventKind);
1086 case kEventClassControl:
1090 /*Con_Printf(">> UNKNOWN eventClass: %c%c%c%c, eventKind: %d <<\n",
1091 eventClass >> 24, (eventClass >> 16) & 0xFF,
1092 (eventClass >> 8) & 0xFF, eventClass & 0xFF,
1097 SendEventToEventTarget (theEvent, theTarget);
1098 ReleaseEvent(theEvent);
1106 in_mouse_x = mouse_x;
1107 in_mouse_y = mouse_y;