glfw/src/x11/x11_init.c

207 lines
5.9 KiB
C
Raw Normal View History

2010-09-07 17:34:51 +02:00
//========================================================================
// GLFW - An OpenGL framework
// Platform: X11/GLX
2010-09-07 17:41:26 +02:00
// API version: 3.0
2010-09-07 17:34:51 +02:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
#include <stdio.h>
2010-09-07 17:34:51 +02:00
//************************************************************************
//**** GLFW internal functions ****
//************************************************************************
//========================================================================
// Dynamically load libraries
//========================================================================
2010-09-08 15:51:25 +02:00
static void initLibraries(void)
2010-09-07 17:34:51 +02:00
{
#ifdef _GLFW_DLOPEN_LIBGL
int i;
2010-09-08 15:58:43 +02:00
char* libGL_names[ ] =
2010-09-07 17:34:51 +02:00
{
"libGL.so",
"libGL.so.1",
"/usr/lib/libGL.so",
"/usr/lib/libGL.so.1",
NULL
};
2010-09-09 20:25:33 +02:00
_glfwLibrary.X11.libGL = NULL;
2010-09-08 15:51:25 +02:00
for (i = 0; libGL_names[i] != NULL; i++)
2010-09-07 17:34:51 +02:00
{
2010-09-09 20:25:33 +02:00
_glfwLibrary.X11.libGL = dlopen(libGL_names[i], RTLD_LAZY | RTLD_GLOBAL);
if (_glfwLibrary.X11.libGL)
2010-09-07 17:34:51 +02:00
break;
}
#endif
}
//========================================================================
// Terminate GLFW when exiting application
//========================================================================
2010-09-08 15:51:25 +02:00
static void glfw_atexit(void)
2010-09-07 17:34:51 +02:00
{
glfwTerminate();
}
//========================================================================
// Initialize X11 display
//========================================================================
static GLboolean initDisplay(void)
2010-09-07 17:34:51 +02:00
{
// Open display
2010-09-09 18:15:32 +02:00
_glfwLibrary.X11.display = XOpenDisplay(0);
if (!_glfwLibrary.X11.display)
2010-09-07 17:34:51 +02:00
{
fprintf(stderr, "Failed to open X display\n");
_glfwSetError(GLFW_OPENGL_NOT_SUPPORTED);
2010-09-07 17:34:51 +02:00
return GL_FALSE;
}
// Check for XF86VidMode extension
#ifdef _GLFW_HAS_XF86VIDMODE
2010-09-09 18:15:32 +02:00
_glfwLibrary.X11.XF86VidMode.available =
XF86VidModeQueryExtension(_glfwLibrary.X11.display,
&_glfwLibrary.X11.XF86VidMode.eventBase,
&_glfwLibrary.X11.XF86VidMode.errorBase);
2010-09-07 17:34:51 +02:00
#else
2010-09-09 18:15:32 +02:00
_glfwLibrary.X11.XF86VidMode.available = 0;
2010-09-07 17:34:51 +02:00
#endif
// Check for XRandR extension
#ifdef _GLFW_HAS_XRANDR
2010-09-09 18:15:32 +02:00
_glfwLibrary.X11.XRandR.available =
XRRQueryExtension(_glfwLibrary.X11.display,
&_glfwLibrary.X11.XRandR.eventBase,
&_glfwLibrary.X11.XRandR.errorBase);
2010-09-07 17:34:51 +02:00
#else
2010-09-09 18:15:32 +02:00
_glfwLibrary.X11.XRandR.available = 0;
2010-09-07 17:34:51 +02:00
#endif
// Fullscreen & screen saver settings
// Check if GLX is supported on this display
2010-09-09 18:15:32 +02:00
if (!glXQueryExtension(_glfwLibrary.X11.display, NULL, NULL))
2010-09-07 17:34:51 +02:00
{
fprintf(stderr, "GLX not supported\n");
_glfwSetError(GLFW_OPENGL_NOT_SUPPORTED);
2010-09-07 17:34:51 +02:00
return GL_FALSE;
}
// Retrieve GLX version
2010-09-09 18:15:32 +02:00
if (!glXQueryVersion(_glfwLibrary.X11.display,
&_glfwLibrary.X11.glxMajor,
&_glfwLibrary.X11.glxMinor))
2010-09-07 17:34:51 +02:00
{
fprintf(stderr, "Unable to query GLX version\n");
_glfwSetError(GLFW_OPENGL_NOT_SUPPORTED);
2010-09-07 17:34:51 +02:00
return GL_FALSE;
}
return GL_TRUE;
}
//========================================================================
// Terminate X11 display
//========================================================================
2010-09-08 15:51:25 +02:00
static void terminateDisplay(void)
2010-09-07 17:34:51 +02:00
{
// Open display
2010-09-09 18:15:32 +02:00
if (_glfwLibrary.X11.display)
2010-09-07 17:34:51 +02:00
{
2010-09-09 18:15:32 +02:00
XCloseDisplay(_glfwLibrary.X11.display);
_glfwLibrary.X11.display = NULL;
2010-09-07 17:34:51 +02:00
}
}
//************************************************************************
//**** Platform implementation functions ****
//************************************************************************
//========================================================================
// Initialize various GLFW state
//========================================================================
2010-09-08 15:51:25 +02:00
int _glfwPlatformInit(void)
2010-09-07 17:34:51 +02:00
{
2010-09-08 15:51:25 +02:00
if (!initDisplay())
2010-09-07 17:34:51 +02:00
return GL_FALSE;
// Try to load libGL.so if necessary
initLibraries();
// Install atexit() routine
2010-09-08 15:51:25 +02:00
atexit(glfw_atexit);
2010-09-07 17:34:51 +02:00
// Initialize joysticks
_glfwInitJoysticks();
// Start the timer
_glfwInitTimer();
return GL_TRUE;
}
//========================================================================
// Close window and shut down library
//========================================================================
2010-09-08 15:51:25 +02:00
int _glfwPlatformTerminate(void)
2010-09-07 17:34:51 +02:00
{
2010-09-09 18:15:32 +02:00
if (_glfwLibrary.window)
glfwCloseWindow(_glfwLibrary.window);
2010-09-07 17:34:51 +02:00
// Terminate display
terminateDisplay();
// Terminate joysticks
_glfwTerminateJoysticks();
// Unload libGL.so if necessary
#ifdef _GLFW_DLOPEN_LIBGL
2010-09-09 20:25:33 +02:00
if (_glfwLibrary.X11.libGL != NULL)
2010-09-07 17:34:51 +02:00
{
2010-09-09 20:25:33 +02:00
dlclose(_glfwLibrary.X11.libGL);
_glfwLibrary.X11.libGL = NULL;
2010-09-07 17:34:51 +02:00
}
#endif
return GL_TRUE;
}