]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_agl.c
fix a crash that could occur if resizing the resize buffer twice in one upload call...
[divverent/darkplaces.git] / vid_agl.c
1 /*
2         vid_agl.c
3
4         Mac OS X OpenGL and input module, using Carbon and AGL
5
6         Copyright (C) 2005-2006  Mathieu Olivier
7
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.
12
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.
17
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
21 */
22
23
24 #include <dlfcn.h>
25 #include <signal.h>
26 #include <AGL/agl.h>
27 #include <Carbon/Carbon.h>
28 #include "quakedef.h"
29
30
31 // Tell startup code that we have a client
32 int cl_available = true;
33
34 qboolean vid_supportrefreshrate = true;
35
36 // AGL prototypes
37 AGLPixelFormat (*qaglChoosePixelFormat) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList);
38 AGLContext (*qaglCreateContext) (AGLPixelFormat pix, AGLContext share);
39 GLboolean (*qaglDestroyContext) (AGLContext ctx);
40 void (*qaglDestroyPixelFormat) (AGLPixelFormat pix);
41 GLboolean (*qaglSetCurrentContext) (AGLContext ctx);
42 GLboolean (*qaglSetDrawable) (AGLContext ctx, AGLDrawable draw);
43 GLboolean (*qaglSetFullScreen) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device);
44 GLboolean (*qaglSetInteger) (AGLContext ctx, GLenum pname, const GLint *params);
45 void (*qaglSwapBuffers) (AGLContext ctx);
46
47 static qboolean mouse_avail = true;
48 static qboolean vid_usingmouse = false;
49 static float mouse_x, mouse_y;
50
51 static qboolean vid_isfullscreen = false;
52 static qboolean vid_usingvsync = false;
53
54 static int scr_width, scr_height;
55
56 static AGLContext context;
57 static WindowRef window;
58
59
60 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
61 {
62         *x = *y = 0;
63         *width = scr_width;
64         *height = scr_height;
65 }
66
67 static void IN_Activate( qboolean grab )
68 {
69         if (grab)
70         {
71                 if (!vid_usingmouse && mouse_avail && window)
72                 {
73                         Rect winBounds;
74                         CGPoint winCenter;
75
76                         SelectWindow(window);
77                         CGDisplayHideCursor(CGMainDisplayID());
78
79                         // Put the mouse cursor at the center of the window
80                         GetWindowBounds (window, kWindowContentRgn, &winBounds);
81                         winCenter.x = (winBounds.left + winBounds.right) / 2;
82                         winCenter.y = (winBounds.top + winBounds.bottom) / 2;
83                         CGWarpMouseCursorPosition(winCenter);
84
85                         // Lock the mouse pointer at its current position
86                         CGAssociateMouseAndMouseCursorPosition(false);
87
88                         mouse_x = mouse_y = 0;
89                         vid_usingmouse = true;
90                 }
91         }
92         else
93         {
94                 if (vid_usingmouse)
95                 {
96                         CGAssociateMouseAndMouseCursorPosition(true);
97                         CGDisplayShowCursor(CGMainDisplayID());
98
99                         vid_usingmouse = false;
100                 }
101         }
102 }
103
104 void VID_Finish (qboolean allowmousegrab)
105 {
106         qboolean vid_usemouse;
107         qboolean vid_usevsync;
108
109         // handle the mouse state when windowed if that's changed
110         vid_usemouse = false;
111         if (allowmousegrab && vid_mouse.integer && !key_consoleactive && !cls.demoplayback)
112                 vid_usemouse = true;
113         if (!vid_activewindow)
114                 vid_usemouse = false;
115         if (vid_isfullscreen)
116                 vid_usemouse = true;
117         IN_Activate(vid_usemouse);
118
119         // handle changes of the vsync option
120         vid_usevsync = (vid_vsync.integer && !cls.timedemo);
121         if (vid_usingvsync != vid_usevsync)
122         {
123                 GLint sync = (vid_usevsync ? 1 : 0);
124
125                 if (qaglSetInteger(context, AGL_SWAP_INTERVAL, &sync) == GL_TRUE)
126                 {
127                         vid_usingvsync = vid_usevsync;
128                         Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
129                 }
130                 else
131                         Con_Printf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
132         }
133
134         if (r_render.integer)
135         {
136                 if (r_speeds.integer || gl_finish.integer)
137                         qglFinish();
138                 qaglSwapBuffers(context);
139         }
140 }
141
142 #define GAMMA_TABLE_SIZE 256
143 int VID_SetGamma(unsigned short *ramps)
144 {
145         CGGammaValue table_red [GAMMA_TABLE_SIZE];
146         CGGammaValue table_green [GAMMA_TABLE_SIZE];
147         CGGammaValue table_blue [GAMMA_TABLE_SIZE];
148         unsigned int i;
149
150         // Convert the unsigned short table into 3 float tables
151         for (i = 0; i < GAMMA_TABLE_SIZE; i++)
152                 table_red[i] = (float)ramps[i] / 65535.0f;
153         for (i = 0; i < GAMMA_TABLE_SIZE; i++)
154                 table_green[i] = (float)ramps[i + GAMMA_TABLE_SIZE] / 65535.0f;
155         for (i = 0; i < GAMMA_TABLE_SIZE; i++)
156                 table_blue[i] = (float)ramps[i + 2 * GAMMA_TABLE_SIZE] / 65535.0f;
157
158         if (CGSetDisplayTransferByTable(CGMainDisplayID(), GAMMA_TABLE_SIZE, table_red, table_green, table_blue) != CGDisplayNoErr)
159         {
160                 Con_Print("VID_SetGamma: ERROR: CGSetDisplayTransferByTable failed!\n");
161                 return false;
162         }
163
164         return true;
165 }
166
167 int VID_GetGamma(unsigned short *ramps)
168 {
169         CGGammaValue table_red [GAMMA_TABLE_SIZE];
170         CGGammaValue table_green [GAMMA_TABLE_SIZE];
171         CGGammaValue table_blue [GAMMA_TABLE_SIZE];
172         CGTableCount actualsize = 0;
173         unsigned int i;
174
175         // Get the gamma ramps from the system
176         if (CGGetDisplayTransferByTable(CGMainDisplayID(), GAMMA_TABLE_SIZE, table_red, table_green, table_blue, &actualsize) != CGDisplayNoErr)
177         {
178                 Con_Print("VID_GetGamma: ERROR: CGGetDisplayTransferByTable failed!\n");
179                 return false;
180         }
181         if (actualsize != GAMMA_TABLE_SIZE)
182         {
183                 Con_Printf("VID_GetGamma: ERROR: invalid gamma table size (%u != %u)\n", actualsize, GAMMA_TABLE_SIZE);
184                 return false;
185         }
186
187         // Convert the 3 float tables into 1 unsigned short table
188         for (i = 0; i < GAMMA_TABLE_SIZE; i++)
189                 ramps[i] = table_red[i] * 65535.0f;
190         for (i = 0; i < GAMMA_TABLE_SIZE; i++)
191                 ramps[i + GAMMA_TABLE_SIZE] = table_green[i] * 65535.0f;
192         for (i = 0; i < GAMMA_TABLE_SIZE; i++)
193                 ramps[i + 2 * GAMMA_TABLE_SIZE] = table_blue[i] * 65535.0f;
194
195         return true;
196 }
197
198 void signal_handler(int sig)
199 {
200         printf("Received signal %d, exiting...\n", sig);
201         VID_RestoreSystemGamma();
202         Sys_Quit();
203         exit(0);
204 }
205
206 void InitSig(void)
207 {
208         signal(SIGHUP, signal_handler);
209         signal(SIGINT, signal_handler);
210         signal(SIGQUIT, signal_handler);
211         signal(SIGILL, signal_handler);
212         signal(SIGTRAP, signal_handler);
213         signal(SIGIOT, signal_handler);
214         signal(SIGBUS, signal_handler);
215         signal(SIGFPE, signal_handler);
216         signal(SIGSEGV, signal_handler);
217         signal(SIGTERM, signal_handler);
218 }
219
220 void VID_Init(void)
221 {
222         InitSig(); // trap evil signals
223 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
224         if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
225                 mouse_avail = false;
226 }
227
228 static void *prjobj = NULL;
229
230 static void GL_CloseLibrary(void)
231 {
232         if (prjobj)
233                 dlclose(prjobj);
234         prjobj = NULL;
235         gl_driver[0] = 0;
236         gl_extensions = "";
237         gl_platform = "";
238         gl_platformextensions = "";
239 }
240
241 static int GL_OpenLibrary(void)
242 {
243         const char *name = "/System/Library/Frameworks/AGL.framework/AGL";
244
245         Con_Printf("Loading OpenGL driver %s\n", name);
246         GL_CloseLibrary();
247         if (!(prjobj = dlopen(name, RTLD_LAZY)))
248         {
249                 Con_Printf("Unable to open symbol list for %s\n", name);
250                 return false;
251         }
252         strcpy(gl_driver, name);
253         return true;
254 }
255
256 void *GL_GetProcAddress(const char *name)
257 {
258         return dlsym(prjobj, name);
259 }
260
261 void VID_Shutdown(void)
262 {
263         if (context == NULL || window == NULL)
264                 return;
265
266         IN_Activate(false);
267         VID_RestoreSystemGamma();
268
269         if (context != NULL)
270         {
271                 qaglDestroyContext(context);
272                 context = NULL;
273         }
274
275         if (window != NULL)
276         {
277                 DisposeWindow(window);
278                 window = NULL;
279         }
280
281         vid_hidden = true;
282         vid_isfullscreen = false;
283
284         GL_CloseLibrary();
285         Key_ClearStates ();
286 }
287
288 // Since the event handler can be called at any time, we store the events for later processing
289 static qboolean AsyncEvent_Quitting = false;
290 static qboolean AsyncEvent_Collapsed = false;
291 static OSStatus MainWindowEventHandler (EventHandlerCallRef nextHandler, EventRef event, void *userData)
292 {
293         OSStatus err = noErr;
294
295         switch (GetEventKind (event))
296         {
297                 case kEventWindowClosed:
298                         AsyncEvent_Quitting = true;
299                         break;
300
301                 // Docked (start)
302                 case kEventWindowCollapsing:
303                         AsyncEvent_Collapsed = true;
304                         break;
305
306                 // Undocked / restored (end)
307                 case kEventWindowExpanded:
308                         AsyncEvent_Collapsed = false;
309                         break;
310
311                 default:
312                         err = eventNotHandledErr;
313                         break;
314         }
315
316         return err;
317 }
318
319 static void VID_ProcessPendingAsyncEvents (void)
320 {
321         // Collapsed / expanded
322         if (AsyncEvent_Collapsed != vid_hidden)
323         {
324                 vid_hidden = !vid_hidden;
325                 vid_activewindow = false;
326                 VID_RestoreSystemGamma();
327         }
328
329         // Closed
330         if (AsyncEvent_Quitting)
331         {
332                 Sys_Quit();
333         }
334 }
335
336 static void VID_BuildAGLAttrib(GLint *attrib, int stencil, qboolean fullscreen)
337 {
338         *attrib++ = AGL_RGBA;
339         *attrib++ = AGL_RED_SIZE;*attrib++ = 1;
340         *attrib++ = AGL_GREEN_SIZE;*attrib++ = 1;
341         *attrib++ = AGL_BLUE_SIZE;*attrib++ = 1;
342         *attrib++ = AGL_DOUBLEBUFFER;
343         *attrib++ = AGL_DEPTH_SIZE;*attrib++ = 1;
344
345         // if stencil is enabled, ask for alpha too
346         if (stencil)
347         {
348                 *attrib++ = AGL_STENCIL_SIZE;*attrib++ = 8;
349                 *attrib++ = AGL_ALPHA_SIZE;*attrib++ = 1;
350         }
351         if (fullscreen)
352                 *attrib++ = AGL_FULLSCREEN;
353         *attrib++ = AGL_NONE;
354 }
355
356 int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate)
357 {
358     const EventTypeSpec winEvents[] =
359         {
360                 { kEventClassWindow, kEventWindowClosed },
361                 { kEventClassWindow, kEventWindowCollapsing },
362                 { kEventClassWindow, kEventWindowExpanded },
363         };
364         OSStatus carbonError;
365         Rect windowBounds;
366         AGLPixelFormat pixelFormat;
367         GLint attributes [32];
368
369         if (!GL_OpenLibrary())
370         {
371                 Con_Printf("Unable to load GL driver\n");
372                 return false;
373         }
374
375         if ((qaglChoosePixelFormat = (AGLPixelFormat (*) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList))GL_GetProcAddress("aglChoosePixelFormat")) == NULL
376          || (qaglCreateContext = (AGLContext (*) (AGLPixelFormat pix, AGLContext share))GL_GetProcAddress("aglCreateContext")) == NULL
377          || (qaglDestroyContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglDestroyContext")) == NULL
378          || (qaglDestroyPixelFormat = (void (*) (AGLPixelFormat pix))GL_GetProcAddress("aglDestroyPixelFormat")) == NULL
379          || (qaglSetCurrentContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglSetCurrentContext")) == NULL
380          || (qaglSetDrawable = (GLboolean (*) (AGLContext ctx, AGLDrawable draw))GL_GetProcAddress("aglSetDrawable")) == NULL
381          || (qaglSetFullScreen = (GLboolean (*) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device))GL_GetProcAddress("aglSetFullScreen")) == NULL
382          || (qaglSetInteger = (GLboolean (*) (AGLContext ctx, GLenum pname, const GLint *params))GL_GetProcAddress("aglSetInteger")) == NULL
383          || (qaglSwapBuffers = (void (*) (AGLContext ctx))GL_GetProcAddress("aglSwapBuffers")) == NULL
384         )
385         {
386                 Con_Printf("AGL functions not found\n");
387                 ReleaseWindow(window);
388                 return false;
389         }
390
391         // Ignore the events from the previous window
392         AsyncEvent_Quitting = false;
393         AsyncEvent_Collapsed = false;
394
395         // Create the window, a bit towards the center of the screen
396         windowBounds.left = 100;
397         windowBounds.top = 100;
398         windowBounds.right = width + 100;
399         windowBounds.bottom = height + 100;
400         carbonError = CreateNewWindow(kDocumentWindowClass, kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute, &windowBounds, &window);
401         if (carbonError != noErr || window == NULL)
402         {
403                 Con_Printf("Unable to create window (error %d)\n", carbonError);
404                 return false;
405         }
406
407         // Set the window title
408         CFStringRef windowTitle = CFSTR("DarkPlaces AGL");
409         SetWindowTitleWithCFString(window, windowTitle);
410         CFRelease(windowTitle);
411
412         // Install the callback function for the window events we can't get
413         // through ReceiveNextEvent (i.e. close, collapse, and expand)
414         InstallWindowEventHandler (window, NewEventHandlerUPP (MainWindowEventHandler),
415                                                            GetEventTypeCount(winEvents), winEvents, window, NULL);
416
417         // Create and set pixel format
418         VID_BuildAGLAttrib(attributes, bpp == 32, fullscreen);
419         pixelFormat = qaglChoosePixelFormat(NULL, 1, attributes);
420         if (pixelFormat == NULL)
421         {
422                 Con_Printf("Unable to create pixel format\n");
423                 ReleaseWindow(window);
424                 return false;
425         }
426
427         // Set context and show the window
428         context = qaglCreateContext(pixelFormat, NULL);
429         if (context == NULL)
430                 Sys_Error ("aglCreateContext failed");
431         if (fullscreen)
432         {
433                 if (!qaglSetFullScreen (context, width, height, refreshrate, 0))
434                         Sys_Error("aglSetFullScreen failed");
435                 vid_isfullscreen = true;
436         }
437         else
438         {
439                 if (!qaglSetDrawable(context, GetWindowPort(window)))
440                         Sys_Error ("aglSetDrawable failed");
441         }
442         if (!qaglSetCurrentContext(context))
443                 Sys_Error ("aglSetCurrentContext failed");
444         qaglDestroyPixelFormat(pixelFormat);
445
446         scr_width = width;
447         scr_height = height;
448
449         if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
450                 Sys_Error("glGetString not found in %s", gl_driver);
451
452         gl_renderer = (const char *)qglGetString(GL_RENDERER);
453         gl_vendor = (const char *)qglGetString(GL_VENDOR);
454         gl_version = (const char *)qglGetString(GL_VERSION);
455         gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
456         gl_platform = "AGL";
457         gl_videosyncavailable = true;
458
459         vid_usingmouse = false;
460         vid_hidden = false;
461         vid_activewindow = true;
462         GL_Init();
463
464         SelectWindow(window);
465         ShowWindow(window);
466
467         return true;
468 }
469
470 static void Handle_KeyMod(UInt32 keymod)
471 {
472         static UInt32 prev_keymod = 0;
473         UInt32 modChanges = prev_keymod ^ keymod;
474
475         if ((modChanges & cmdKey) != 0)
476         {
477                 Key_Event(K_AUX1, '\0', (keymod & cmdKey) != 0);
478         }
479         if ((modChanges & shiftKey) != 0 || (modChanges & rightShiftKey) != 0)
480         {
481                 Key_Event(K_SHIFT, '\0', (keymod & shiftKey) != 0);
482         }
483         if ((modChanges & alphaLock) != 0)
484         {
485                 Key_Event(K_CAPSLOCK, '\0', (keymod & alphaLock) != 0);
486         }
487         if ((modChanges & optionKey) != 0 || (modChanges & rightOptionKey) != 0)
488         {
489                 Key_Event(K_ALT, '\0', (keymod & optionKey) != 0);
490         }
491         if ((modChanges & controlKey) != 0 || (modChanges & rightControlKey) != 0)
492         {
493                 Key_Event(K_CTRL, '\0', (keymod & controlKey) != 0);
494         }
495         if ((modChanges & kEventKeyModifierNumLockMask) != 0)
496         {
497                 Key_Event(K_NUMLOCK, '\0', (keymod & kEventKeyModifierNumLockMask) != 0);
498         }
499         if ((modChanges & kEventKeyModifierFnMask) != 0)
500         {
501                 Key_Event(K_AUX2, '\0', (keymod & kEventKeyModifierFnMask) != 0);
502         }
503
504         prev_keymod = keymod;
505 }
506
507 static void Handle_Key(unsigned char charcode, qboolean keypressed)
508 {
509         unsigned int keycode = 0;
510         char ascii = '\0';
511
512         switch (charcode)
513         {
514                 case kHomeCharCode:
515                         keycode = K_HOME;
516                         break;
517                 case kEnterCharCode:
518                         keycode = K_KP_ENTER;
519                         break;
520                 case kEndCharCode:
521                         keycode = K_END;
522                         break;
523                 case kBackspaceCharCode:
524                         keycode = K_BACKSPACE;
525                         break;
526                 case kTabCharCode:
527                         keycode = K_TAB;
528                         break;
529                 case kPageUpCharCode:
530                         keycode = K_PGUP;
531                         break;
532                 case kPageDownCharCode:
533                         keycode = K_PGDN;
534                         break;
535                 case kReturnCharCode:
536                         keycode = K_ENTER;
537                         break;
538                 case kEscapeCharCode:
539                         keycode = K_ESCAPE;
540                         break;
541                 case kLeftArrowCharCode:
542                         keycode = K_LEFTARROW;
543                         break;
544                 case kRightArrowCharCode:
545                         keycode = K_RIGHTARROW;
546                         break;
547                 case kUpArrowCharCode:
548                         keycode = K_UPARROW;
549                         break;
550                 case kDownArrowCharCode :
551                         keycode = K_DOWNARROW;
552                         break;
553                 case kDeleteCharCode:
554                         keycode = K_DEL;
555                         break;
556                 case 0:
557                 case 191:
558                         // characters 0 and 191 are sent by the mouse buttons (?!)
559                         break;
560                 default:
561                         if ('A' <= charcode && charcode <= 'Z')
562                         {
563                                 keycode = charcode + ('a' - 'A');  // lowercase it
564                                 ascii = charcode;
565                         }
566                         else if (charcode >= 32)
567                         {
568                                 keycode = charcode;
569                                 ascii = charcode;
570                         }
571                         else
572                                 Con_Printf(">> UNKNOWN charcode: %d <<\n", charcode);
573         }
574
575         if (keycode != 0)
576                 Key_Event(keycode, ascii, keypressed);
577 }
578
579 void Sys_SendKeyEvents(void)
580 {
581         EventRef theEvent;
582         EventTargetRef theTarget;
583
584         // Start by processing the asynchronous events we received since the previous frame
585         VID_ProcessPendingAsyncEvents();
586
587         theTarget = GetEventDispatcherTarget();
588         while (ReceiveNextEvent(0, NULL, kEventDurationNoWait, true, &theEvent) == noErr)
589         {
590                 UInt32 eventClass = GetEventClass(theEvent);
591                 UInt32 eventKind = GetEventKind(theEvent);
592
593                 switch (eventClass)
594                 {
595                         case kEventClassMouse:
596                         {
597                                 EventMouseButton theButton;
598                                 int key;
599
600                                 switch (eventKind)
601                                 {
602                                         case kEventMouseDown:
603                                         case kEventMouseUp:
604                                                 GetEventParameter(theEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof(theButton), NULL, &theButton);
605                                                 switch (theButton)
606                                                 {
607                                                         default:
608                                                         case kEventMouseButtonPrimary:
609                                                                 key = K_MOUSE1;
610                                                                 break;
611                                                         case kEventMouseButtonSecondary:
612                                                                 key = K_MOUSE2;
613                                                                 break;
614                                                         case kEventMouseButtonTertiary:
615                                                                 key = K_MOUSE3;
616                                                                 break;
617                                                 }
618                                                 Key_Event(key, '\0', eventKind == kEventMouseDown);
619                                                 break;
620
621                                         case kEventMouseMoved:
622                                         {
623                                                 HIPoint deltaPos;
624
625                                                 GetEventParameter(theEvent, kEventParamMouseDelta, typeHIPoint, NULL, sizeof(deltaPos), NULL, &deltaPos);
626
627                                                 mouse_x += deltaPos.x;
628                                                 mouse_y += deltaPos.y;
629                                                 break;
630                                         }
631
632                                         case kEventMouseWheelMoved:
633                                         {
634                                                 SInt32 delta;
635                                                 unsigned int wheelEvent;
636
637                                                 GetEventParameter(theEvent, kEventParamMouseWheelDelta, typeSInt32, NULL, sizeof(delta), NULL, &delta);
638
639                                                 wheelEvent = (delta > 0) ? K_MWHEELUP : K_MWHEELDOWN;
640                                                 Key_Event(wheelEvent, 0, true);
641                                                 Key_Event(wheelEvent, 0, false);
642                                                 break;
643                                         }
644
645                                         case kEventMouseDragged:
646                                                 break;
647
648                                         default:
649                                                 Con_Printf (">> kEventClassMouse (UNKNOWN eventKind: %d) <<\n", eventKind);
650                                                 break;
651                                 }
652                         }
653
654                         case kEventClassKeyboard:
655                         {
656                                 char keycode;
657
658                                 switch (eventKind)
659                                 {
660                                         case kEventRawKeyDown:
661                                                 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(keycode), NULL, &keycode);
662                                                 Handle_Key(keycode, true);
663                                                 break;
664
665                                         case kEventRawKeyRepeat:
666                                                 break;
667
668                                         case kEventRawKeyUp:
669                                                 GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(keycode), NULL, &keycode);
670                                                 Handle_Key(keycode, false);
671                                                 break;
672
673                                         case kEventRawKeyModifiersChanged:
674                                         {
675                                                 UInt32 keymod = 0;
676                                                 GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(keymod), NULL, &keymod);
677                                                 Handle_KeyMod(keymod);
678                                                 break;
679                                         }
680
681                                         case kEventHotKeyPressed:
682                                                 break;
683
684                                         case kEventHotKeyReleased:
685                                                 break;
686
687                                         case kEventMouseWheelMoved:
688                                                 break;
689
690                                         default:
691                                                 Con_Printf (">> kEventClassKeyboard (UNKNOWN eventKind: %d) <<\n", eventKind);
692                                                 break;
693                                 }
694                                 break;
695                         }
696
697                         case kEventClassTextInput:
698                                 Con_Printf(">> kEventClassTextInput (%d) <<\n", eventKind);
699                                 break;
700
701                         case kEventClassApplication:
702                                 switch (eventKind)
703                                 {
704                                         case kEventAppActivated :
705                                                 vid_activewindow = true;
706                                                 break;
707                                         case kEventAppDeactivated:
708                                                 vid_activewindow = false;
709                                                 VID_RestoreSystemGamma();
710                                                 break;
711                                         case kEventAppQuit:
712                                                 Sys_Quit();
713                                                 break;
714                                         case kEventAppActiveWindowChanged:
715                                                 break;
716                                         default:
717                                                 Con_Printf(">> kEventClassApplication (UNKNOWN eventKind: %d) <<\n", eventKind);
718                                                 break;
719                                 }
720                                 break;
721
722                         case kEventClassAppleEvent:
723                                 switch (eventKind)
724                                 {
725                                         case kEventAppleEvent :
726                                                 break;
727                                         default:
728                                                 Con_Printf(">> kEventClassAppleEvent (UNKNOWN eventKind: %d) <<\n", eventKind);
729                                                 break;
730                                 }
731                                 break;
732
733                         case kEventClassWindow:
734                                 switch (eventKind)
735                                 {
736                                         case kEventWindowUpdate :
737                                                 break;
738                                         default:
739                                                 Con_Printf(">> kEventClassWindow (UNKNOWN eventKind: %d) <<\n", eventKind);
740                                                 break;
741                                 }
742                                 break;
743
744                         case kEventClassControl:
745                                 break;
746
747                         default:
748                                 /*Con_Printf(">> UNKNOWN eventClass: %c%c%c%c, eventKind: %d <<\n",
749                                                         eventClass >> 24, (eventClass >> 16) & 0xFF,
750                                                         (eventClass >> 8) & 0xFF, eventClass & 0xFF,
751                                                         eventKind);*/
752                                 break;
753                 }
754
755                 SendEventToEventTarget (theEvent, theTarget);
756                 ReleaseEvent(theEvent);
757         }
758 }
759
760 void IN_Move (void)
761 {
762         if (mouse_avail)
763         {
764                 in_mouse_x = mouse_x;
765                 in_mouse_y = mouse_y;
766         }
767         mouse_x = 0;
768         mouse_y = 0;
769 }