X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=vid_agl.c;h=977d8b2fe2ed00507ff76702f8cfac805003c9d7;hp=f5db48057836272958726ec031ee0e76e10a6176;hb=15368629f4f2f207e22af38b6fa29fdcbbe50982;hpb=6824d8ddc8a43cae0609be5bbe8bee01fa1a4225 diff --git a/vid_agl.c b/vid_agl.c index f5db4805..977d8b2f 100644 --- a/vid_agl.c +++ b/vid_agl.c @@ -3,7 +3,7 @@ Mac OS X OpenGL and input module, using Carbon and AGL - Copyright (C) 2005 Mathieu Olivier + Copyright (C) 2005-2006 Mathieu Olivier This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,6 +31,8 @@ // Tell startup code that we have a client int cl_available = true; +qboolean vid_supportrefreshrate = true; + // AGL prototypes AGLPixelFormat (*qaglChoosePixelFormat) (const AGLDevice *gdevs, GLint ndev, const GLint *attribList); AGLContext (*qaglCreateContext) (AGLPixelFormat pix, AGLContext share); @@ -39,6 +41,7 @@ void (*qaglDestroyPixelFormat) (AGLPixelFormat pix); GLboolean (*qaglSetCurrentContext) (AGLContext ctx); GLboolean (*qaglSetDrawable) (AGLContext ctx, AGLDrawable draw); GLboolean (*qaglSetFullScreen) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device); +GLboolean (*qaglSetInteger) (AGLContext ctx, GLenum pname, const GLint *params); void (*qaglSwapBuffers) (AGLContext ctx); static qboolean mouse_avail = true; @@ -46,11 +49,12 @@ static qboolean vid_usingmouse = false; static float mouse_x, mouse_y; static qboolean vid_isfullscreen = false; +static qboolean vid_usingvsync = false; static int scr_width, scr_height; static AGLContext context; -static WindowRef window; +static WindowRef window; void VID_GetWindowSize (int *x, int *y, int *width, int *height) @@ -70,7 +74,7 @@ static void IN_Activate( qboolean grab ) CGPoint winCenter; SelectWindow(window); - HideCursor(); + CGDisplayHideCursor(CGMainDisplayID()); // Put the mouse cursor at the center of the window GetWindowBounds (window, kWindowContentRgn, &winBounds); @@ -90,20 +94,22 @@ static void IN_Activate( qboolean grab ) if (vid_usingmouse) { CGAssociateMouseAndMouseCursorPosition(true); - ShowCursor(); + CGDisplayShowCursor(CGMainDisplayID()); vid_usingmouse = false; } } } -void VID_Finish (void) +#define GAMMA_TABLE_SIZE 256 +void VID_Finish (qboolean allowmousegrab) { qboolean vid_usemouse; + qboolean vid_usevsync; // handle the mouse state when windowed if that's changed vid_usemouse = false; - if (vid_mouse.integer && !key_consoleactive && !cls.demoplayback) + if (allowmousegrab && vid_mouse.integer && !key_consoleactive && !cls.demoplayback) vid_usemouse = true; if (!vid_activewindow) vid_usemouse = false; @@ -111,24 +117,83 @@ void VID_Finish (void) vid_usemouse = true; IN_Activate(vid_usemouse); + // handle changes of the vsync option + vid_usevsync = (vid_vsync.integer && !cls.timedemo); + if (vid_usingvsync != vid_usevsync) + { + GLint sync = (vid_usevsync ? 1 : 0); + + if (qaglSetInteger(context, AGL_SWAP_INTERVAL, &sync) == GL_TRUE) + { + vid_usingvsync = vid_usevsync; + Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated"); + } + else + Con_Printf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate"); + } + if (r_render.integer) { if (r_speeds.integer || gl_finish.integer) qglFinish(); qaglSwapBuffers(context); } + VID_UpdateGamma(false, GAMMA_TABLE_SIZE); } -int VID_SetGamma(unsigned short *ramps) +int VID_SetGamma(unsigned short *ramps, int rampsize) { - // TODO - return false; + CGGammaValue table_red [GAMMA_TABLE_SIZE]; + CGGammaValue table_green [GAMMA_TABLE_SIZE]; + CGGammaValue table_blue [GAMMA_TABLE_SIZE]; + int i; + + // Convert the unsigned short table into 3 float tables + for (i = 0; i < rampsize; i++) + table_red[i] = (float)ramps[i] / 65535.0f; + for (i = 0; i < rampsize; i++) + table_green[i] = (float)ramps[i + rampsize] / 65535.0f; + for (i = 0; i < rampsize; i++) + table_blue[i] = (float)ramps[i + 2 * rampsize] / 65535.0f; + + if (CGSetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue) != CGDisplayNoErr) + { + Con_Print("VID_SetGamma: ERROR: CGSetDisplayTransferByTable failed!\n"); + return false; + } + + return true; } -int VID_GetGamma(unsigned short *ramps) +int VID_GetGamma(unsigned short *ramps, int rampsize) { - // TODO - return false; + CGGammaValue table_red [GAMMA_TABLE_SIZE]; + CGGammaValue table_green [GAMMA_TABLE_SIZE]; + CGGammaValue table_blue [GAMMA_TABLE_SIZE]; + CGTableCount actualsize = 0; + int i; + + // Get the gamma ramps from the system + if (CGGetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue, &actualsize) != CGDisplayNoErr) + { + Con_Print("VID_GetGamma: ERROR: CGGetDisplayTransferByTable failed!\n"); + return false; + } + if (actualsize != (unsigned int)rampsize) + { + Con_Printf("VID_GetGamma: ERROR: invalid gamma table size (%u != %u)\n", actualsize, rampsize); + return false; + } + + // Convert the 3 float tables into 1 unsigned short table + for (i = 0; i < rampsize; i++) + ramps[i] = table_red[i] * 65535.0f; + for (i = 0; i < rampsize; i++) + ramps[i + rampsize] = table_green[i] * 65535.0f; + for (i = 0; i < rampsize; i++) + ramps[i + 2 * rampsize] = table_blue[i] * 65535.0f; + + return true; } void signal_handler(int sig) @@ -231,19 +296,16 @@ static OSStatus MainWindowEventHandler (EventHandlerCallRef nextHandler, EventRe switch (GetEventKind (event)) { case kEventWindowClosed: - //Con_Printf(">> kEventWindowClosed (received) <<\n"); AsyncEvent_Quitting = true; break; // Docked (start) case kEventWindowCollapsing: - //Con_Printf(">> kEventWindowCollapsing (received) <<\n"); AsyncEvent_Collapsed = true; break; // Undocked / restored (end) case kEventWindowExpanded: - //Con_Printf(">> kEventWindowExpanded (received) <<\n"); AsyncEvent_Collapsed = false; break; @@ -260,13 +322,6 @@ static void VID_ProcessPendingAsyncEvents (void) // Collapsed / expanded if (AsyncEvent_Collapsed != vid_hidden) { - /* - if (vid_hidden) - Con_Printf(">> kEventWindowExpanded (processed) <<\n"); - else - Con_Printf(">> kEventWindowCollapsing (processed) <<\n"); - */ - vid_hidden = !vid_hidden; vid_activewindow = false; VID_RestoreSystemGamma(); @@ -275,12 +330,11 @@ static void VID_ProcessPendingAsyncEvents (void) // Closed if (AsyncEvent_Quitting) { - //Con_Printf(">> kEventWindowClosed (processed) <<\n"); Sys_Quit(); } } -static void VID_BuildAGLAttrib(GLint *attrib, int stencil, qboolean fullscreen) +static void VID_BuildAGLAttrib(GLint *attrib, GLint stencil, qboolean fullscreen) { *attrib++ = AGL_RGBA; *attrib++ = AGL_RED_SIZE;*attrib++ = 1; @@ -288,6 +342,7 @@ static void VID_BuildAGLAttrib(GLint *attrib, int stencil, qboolean fullscreen) *attrib++ = AGL_BLUE_SIZE;*attrib++ = 1; *attrib++ = AGL_DOUBLEBUFFER; *attrib++ = AGL_DEPTH_SIZE;*attrib++ = 1; + // if stencil is enabled, ask for alpha too if (stencil) { @@ -299,7 +354,7 @@ static void VID_BuildAGLAttrib(GLint *attrib, int stencil, qboolean fullscreen) *attrib++ = AGL_NONE; } -int VID_InitMode(int fullscreen, int width, int height, int bpp) +int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate) { const EventTypeSpec winEvents[] = { @@ -309,9 +364,13 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp) }; OSStatus carbonError; Rect windowBounds; - GDHandle screen; AGLPixelFormat pixelFormat; GLint attributes [32]; + GLenum error; + + // FullScreen Display stuff + GDHandle gdhDisplay; // = GetMainDevice(); /*deprecated*/ + CGDirectDisplayID mainDisplay = CGMainDisplayID(); if (!GL_OpenLibrary()) { @@ -326,6 +385,7 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp) || (qaglSetCurrentContext = (GLboolean (*) (AGLContext ctx))GL_GetProcAddress("aglSetCurrentContext")) == NULL || (qaglSetDrawable = (GLboolean (*) (AGLContext ctx, AGLDrawable draw))GL_GetProcAddress("aglSetDrawable")) == NULL || (qaglSetFullScreen = (GLboolean (*) (AGLContext ctx, GLsizei width, GLsizei height, GLsizei freq, GLint device))GL_GetProcAddress("aglSetFullScreen")) == NULL + || (qaglSetInteger = (GLboolean (*) (AGLContext ctx, GLenum pname, const GLint *params))GL_GetProcAddress("aglSetInteger")) == NULL || (qaglSwapBuffers = (void (*) (AGLContext ctx))GL_GetProcAddress("aglSwapBuffers")) == NULL ) { @@ -338,9 +398,11 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp) AsyncEvent_Quitting = false; AsyncEvent_Collapsed = false; - // Create the window - SetRect(&windowBounds, 0, 0, width, height); - OffsetRect(&windowBounds, 100, 100); // move it a bit towards the center of the screen + // Create the window, a bit towards the center of the screen + windowBounds.left = 100; + windowBounds.top = 100; + windowBounds.right = width + 100; + windowBounds.bottom = height + 100; carbonError = CreateNewWindow(kDocumentWindowClass, kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute, &windowBounds, &window); if (carbonError != noErr || window == NULL) { @@ -358,33 +420,105 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp) InstallWindowEventHandler (window, NewEventHandlerUPP (MainWindowEventHandler), GetEventTypeCount(winEvents), winEvents, window, NULL); - screen = GetGWorldDevice(GetWindowPort(window)); - if (screen == NULL) + // Create the desired attribute list + VID_BuildAGLAttrib(attributes, bpp == 32, fullscreen); + + if (!fullscreen) { - Con_Printf("Unable to get GDevice for window\n"); - ReleaseWindow(window); - return false; + // Output to Window + // Set pixel format with built attribs + // Note: use NULL then must use 0 + pixelFormat = qaglChoosePixelFormat(NULL, 0, attributes); + error = aglGetError(); + if (error != AGL_NO_ERROR) + { + Con_Printf("qaglChoosePixelFormat FAILED: %s\n", + (char *)aglErrorString(error)); + ReleaseWindow(window); + return false; + } } - // Create and set pixel format - VID_BuildAGLAttrib(attributes, bpp == 32, fullscreen); - pixelFormat = qaglChoosePixelFormat(&screen, 1, attributes); - if (pixelFormat == NULL) + if (fullscreen) { - Con_Printf("Unable to create pixel format\n"); - ReleaseWindow(window); - return false; + // Output is FullScreen + // Get the mainDisplay and set resolution to current + CGDisplayCapture (mainDisplay); + CFDictionaryRef refDisplayMode = CGDisplayBestModeForParameters (mainDisplay, bpp, width, height, NULL); + CGDisplaySwitchToMode (mainDisplay, refDisplayMode); + DMGetGDeviceByDisplayID ((DisplayIDType)mainDisplay, &gdhDisplay, false); + + // Set pixel format with built attribs + // Note: specifying a device is *required* for AGL_FullScreen + pixelFormat = qaglChoosePixelFormat(&gdhDisplay, 1, attributes); + error = aglGetError(); + if (error != AGL_NO_ERROR) + { + Con_Printf("qaglChoosePixelFormat FAILED: %s\n", + (char *)aglErrorString(error)); + ReleaseWindow(window); + return false; + } } - // Set context and show the window + // Create a context using the pform context = qaglCreateContext(pixelFormat, NULL); + error = aglGetError(); + if (error != AGL_NO_ERROR) + { + Con_Printf("qaglCreateContext FAILED: %s\n", + (char *)aglErrorString(error)); + } + + // Make the context the current one ('enable' it) + qaglSetCurrentContext(context); + error = aglGetError(); + if (error != AGL_NO_ERROR) + { + Con_Printf("qaglSetCurrentContext FAILED: %s\n", + (char *)aglErrorString(error)); + ReleaseWindow(window); + return false; + } + + // Discard pform qaglDestroyPixelFormat(pixelFormat); - if (context == NULL) - Sys_Error ("aglCreateContext failed\n"); - if (!qaglSetDrawable(context, GetWindowPort(window))) - Sys_Error ("aglSetDrawable failed\n"); - if (!qaglSetCurrentContext(context)) - Sys_Error ("aglSetCurrentContext failed\n"); + + // Attempt fullscreen if requested + if (fullscreen) + { + qaglSetFullScreen (context, width, height, refreshrate, 0); + error = aglGetError(); + if (error != AGL_NO_ERROR) + { + Con_Printf("qaglSetFullScreen FAILED: %s\n", + (char *)aglErrorString(error)); + return false; + // FullScreen Failed + vid_isfullscreen = false; + } + else + { + // FullScreen Success + vid_isfullscreen = true; + } + } + + if (!fullscreen) + { + // Note: this _is_ a Window != FullScreen + // Set Window as Drawable + qaglSetDrawable(context, GetWindowPort(window)); + error = aglGetError(); + if (error != AGL_NO_ERROR) + { + Con_Printf("qaglSetDrawable FAILED: %s\n", + (char *)aglErrorString(error)); + ReleaseWindow(window); + return false; + } + + } scr_width = width; scr_height = height; @@ -392,19 +526,12 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp) if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL) Sys_Error("glGetString not found in %s", gl_driver); - if (fullscreen) - { - if (!qaglSetFullScreen (context, width, height, 0, 0)) - Sys_Error("aglSetFullScreen failed\n"); - vid_isfullscreen = true; - } - gl_renderer = (const char *)qglGetString(GL_RENDERER); gl_vendor = (const char *)qglGetString(GL_VENDOR); gl_version = (const char *)qglGetString(GL_VERSION); gl_extensions = (const char *)qglGetString(GL_EXTENSIONS); gl_platform = "AGL"; - gl_videosyncavailable = false; + gl_videosyncavailable = true; vid_usingmouse = false; vid_hidden = false; @@ -503,8 +630,9 @@ static void Handle_Key(unsigned char charcode, qboolean keypressed) case kDeleteCharCode: keycode = K_DEL; break; + case 0: case 191: - // char 191 is sent by the mouse buttons (?!) + // characters 0 and 191 are sent by the mouse buttons (?!) break; default: if ('A' <= charcode && charcode <= 'Z') @@ -512,7 +640,7 @@ static void Handle_Key(unsigned char charcode, qboolean keypressed) keycode = charcode + ('a' - 'A'); // lowercase it ascii = charcode; } - else if (32 <= charcode && charcode <= 126) + else if (charcode >= 32) { keycode = charcode; ascii = charcode; @@ -556,26 +684,25 @@ void Sys_SendKeyEvents(void) default: case kEventMouseButtonPrimary: key = K_MOUSE1; - //Con_Printf(">> kEventMouseButtonPrimary <<\n"); break; case kEventMouseButtonSecondary: key = K_MOUSE2; - //Con_Printf(">> kEventMouseButtonSecondary <<\n"); break; case kEventMouseButtonTertiary: key = K_MOUSE3; - //Con_Printf(">> kEventMouseButtonTertiary <<\n"); break; } Key_Event(key, '\0', eventKind == kEventMouseDown); break; + // Note: These two events are mutual exclusives + // Treat MouseDragged in the same statement, so we don't block MouseMoved while a mousebutton is held case kEventMouseMoved: + case kEventMouseDragged: { HIPoint deltaPos; GetEventParameter(theEvent, kEventParamMouseDelta, typeHIPoint, NULL, sizeof(deltaPos), NULL, &deltaPos); - //Con_Printf(">> kEventMouseMoved (%f, %f) <<\n", deltaPos.x, deltaPos.y); mouse_x += deltaPos.x; mouse_y += deltaPos.y; @@ -588,7 +715,6 @@ void Sys_SendKeyEvents(void) unsigned int wheelEvent; GetEventParameter(theEvent, kEventParamMouseWheelDelta, typeSInt32, NULL, sizeof(delta), NULL, &delta); - //Con_Printf(">> kEventMouseWheelMoved (delta: %d) <<\n", delta); wheelEvent = (delta > 0) ? K_MWHEELUP : K_MWHEELDOWN; Key_Event(wheelEvent, 0, true); @@ -596,10 +722,6 @@ void Sys_SendKeyEvents(void) break; } - case kEventMouseDragged: - //Con_Printf(">> kEventMouseDragged <<\n"); - break; - default: Con_Printf (">> kEventClassMouse (UNKNOWN eventKind: %d) <<\n", eventKind); break; @@ -615,17 +737,14 @@ void Sys_SendKeyEvents(void) case kEventRawKeyDown: GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(keycode), NULL, &keycode); Handle_Key(keycode, true); - //Con_Printf(">> kEventRawKeyDown (%d) <<\n", keycode); break; case kEventRawKeyRepeat: - //Con_Printf(">> kEventRawKeyRepeat <<\n"); break; case kEventRawKeyUp: GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(keycode), NULL, &keycode); Handle_Key(keycode, false); - //Con_Printf(">> kEventRawKeyUp (%d) <<\n", keycode); break; case kEventRawKeyModifiersChanged: @@ -633,20 +752,16 @@ void Sys_SendKeyEvents(void) UInt32 keymod = 0; GetEventParameter(theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(keymod), NULL, &keymod); Handle_KeyMod(keymod); - //Con_Printf(">> kEventRawKeyModifiersChanged (0x%08X) <<\n", keymod); break; } case kEventHotKeyPressed: - //Con_Printf(">> kEventHotKeyPressed <<\n"); break; case kEventHotKeyReleased: - //Con_Printf(">> kEventHotKeyReleased <<\n"); break; case kEventMouseWheelMoved: - //Con_Printf(">> kEventMouseWheelMoved - via a keyboard event (!?) <<\n"); break; default: @@ -664,20 +779,16 @@ void Sys_SendKeyEvents(void) switch (eventKind) { case kEventAppActivated : - //Con_Printf(">> kEventAppActivated <<\n"); vid_activewindow = true; break; case kEventAppDeactivated: - //Con_Printf(">> kEventAppDeactivated <<\n"); vid_activewindow = false; VID_RestoreSystemGamma(); break; case kEventAppQuit: - //Con_Printf(">> kEventAppQuit <<\n"); Sys_Quit(); break; case kEventAppActiveWindowChanged: - //Con_Printf(">> kEventAppActiveWindowChanged <<\n"); break; default: Con_Printf(">> kEventClassApplication (UNKNOWN eventKind: %d) <<\n", eventKind); @@ -689,7 +800,6 @@ void Sys_SendKeyEvents(void) switch (eventKind) { case kEventAppleEvent : - //Con_Printf(">> kEventAppleEvent <<\n"); break; default: Con_Printf(">> kEventClassAppleEvent (UNKNOWN eventKind: %d) <<\n", eventKind); @@ -701,7 +811,6 @@ void Sys_SendKeyEvents(void) switch (eventKind) { case kEventWindowUpdate : - //Con_Printf(">> kEventWindowUpdate <<\n"); break; default: Con_Printf(">> kEventClassWindow (UNKNOWN eventKind: %d) <<\n", eventKind); @@ -710,7 +819,6 @@ void Sys_SendKeyEvents(void) break; case kEventClassControl: - //Con_Printf(">> kEventClassControl (%d) <<\n", eventKind); break; default: