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