2013-01-04 11:41:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2012 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GSWndOGL.h"
|
|
|
|
|
|
|
|
#ifdef _LINUX
|
|
|
|
GSWndOGL::GSWndOGL()
|
2013-01-04 21:33:24 +00:00
|
|
|
: m_NativeWindow(0), m_NativeDisplay(NULL), m_swapinterval(NULL)
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSWndOGL::CreateContext(int major, int minor)
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
if ( !m_NativeDisplay || !m_NativeWindow )
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Wrong X11 display/window\n" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get visual information
|
|
|
|
static int attrListDbl[] =
|
|
|
|
{
|
|
|
|
// GLX_X_RENDERABLE: If True is specified, then only frame buffer configurations that have associated X
|
|
|
|
// visuals (and can be used to render to Windows and/or GLX pixmaps) will be considered. The default value is GLX_DONT_CARE.
|
|
|
|
GLX_X_RENDERABLE , True,
|
|
|
|
GLX_RED_SIZE , 8,
|
|
|
|
GLX_GREEN_SIZE , 8,
|
|
|
|
GLX_BLUE_SIZE , 8,
|
|
|
|
GLX_DEPTH_SIZE , 24,
|
|
|
|
GLX_DOUBLEBUFFER , True,
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
PFNGLXCHOOSEFBCONFIGPROC glXChooseFBConfig = (PFNGLXCHOOSEFBCONFIGPROC) glXGetProcAddress((GLubyte *) "glXChooseFBConfig");
|
|
|
|
int fbcount = 0;
|
2013-01-04 21:33:24 +00:00
|
|
|
GLXFBConfig *fbc = glXChooseFBConfig(m_NativeDisplay, DefaultScreen(m_NativeDisplay), attrListDbl, &fbcount);
|
2013-01-04 11:41:51 +00:00
|
|
|
if (!fbc || fbcount < 1) return false;
|
|
|
|
|
|
|
|
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte*) "glXCreateContextAttribsARB");
|
|
|
|
if (!glXCreateContextAttribsARB) return false;
|
|
|
|
|
|
|
|
// Create a context
|
|
|
|
int context_attribs[] =
|
|
|
|
{
|
|
|
|
GLX_CONTEXT_MAJOR_VERSION_ARB, major,
|
|
|
|
GLX_CONTEXT_MINOR_VERSION_ARB, minor,
|
|
|
|
// FIXME : Request a debug context to ease opengl development
|
|
|
|
// Note: don't support deprecated feature (pre openg 3.1)
|
|
|
|
//GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB | GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
|
|
|
|
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
m_context = glXCreateContextAttribsARB(m_NativeDisplay, fbc[0], 0, true, context_attribs);
|
2013-01-04 11:41:51 +00:00
|
|
|
if (!m_context) return false;
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
XSync( m_NativeDisplay, false);
|
2013-01-04 11:41:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::AttachContext()
|
|
|
|
{
|
|
|
|
if (!IsContextAttached()) {
|
|
|
|
//fprintf(stderr, "Attach the context\n");
|
2013-01-04 21:33:24 +00:00
|
|
|
glXMakeCurrent(m_NativeDisplay, m_NativeWindow, m_context);
|
2013-01-04 11:41:51 +00:00
|
|
|
m_ctx_attached = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::DetachContext()
|
|
|
|
{
|
|
|
|
if (IsContextAttached()) {
|
|
|
|
//fprintf(stderr, "Detach the context\n");
|
2013-01-04 21:33:24 +00:00
|
|
|
glXMakeCurrent(m_NativeDisplay, None, NULL);
|
2013-01-04 11:41:51 +00:00
|
|
|
m_ctx_attached = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::CheckContext()
|
|
|
|
{
|
|
|
|
int glxMajorVersion, glxMinorVersion;
|
2013-01-04 21:33:24 +00:00
|
|
|
glXQueryVersion(m_NativeDisplay, &glxMajorVersion, &glxMinorVersion);
|
|
|
|
if (glXIsDirect(m_NativeDisplay, m_context))
|
2013-01-04 11:41:51 +00:00
|
|
|
fprintf(stderr, "glX-Version %d.%d with Direct Rendering\n", glxMajorVersion, glxMinorVersion);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "glX-Version %d.%d with Indirect Rendering !!! It won't support properly opengl\n", glxMajorVersion, glxMinorVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSWndOGL::Attach(void* handle, bool managed)
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeWindow = *(Window*)handle;
|
2013-01-04 11:41:51 +00:00
|
|
|
m_managed = managed;
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeDisplay = XOpenDisplay(NULL);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
// Note: 4.2 crash on latest nvidia drivers!
|
|
|
|
if (!CreateContext(3, 3)) return false;
|
|
|
|
|
|
|
|
AttachContext();
|
|
|
|
|
|
|
|
CheckContext();
|
|
|
|
|
|
|
|
m_swapinterval = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*) "glXSwapIntervalMESA");
|
|
|
|
//PFNGLXSWAPINTERVALMESAPROC m_swapinterval = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*) "glXSwapInterval");
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Detach()
|
|
|
|
{
|
|
|
|
// Actually the destructor is not called when there is only a GSclose/GSshutdown
|
|
|
|
// The window still need to be closed
|
|
|
|
DetachContext();
|
2013-01-04 21:33:24 +00:00
|
|
|
if (m_context) glXDestroyContext(m_NativeDisplay, m_context);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
if (m_NativeDisplay) {
|
|
|
|
XCloseDisplay(m_NativeDisplay);
|
|
|
|
m_NativeDisplay = NULL;
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSWndOGL::Create(const string& title, int w, int h)
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
if(m_NativeWindow) return false;
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
if(w <= 0 || h <= 0) {
|
|
|
|
w = theApp.GetConfig("ModeWidth", 640);
|
|
|
|
h = theApp.GetConfig("ModeHeight", 480);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_managed = true;
|
|
|
|
|
|
|
|
// note this part must be only executed when replaying .gs debug file
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeDisplay = XOpenDisplay(NULL);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
|
|
|
|
GLX_RED_SIZE, 8,
|
|
|
|
GLX_GREEN_SIZE, 8,
|
|
|
|
GLX_BLUE_SIZE, 8,
|
|
|
|
GLX_DEPTH_SIZE, 24,
|
|
|
|
None
|
|
|
|
};
|
2013-01-04 21:33:24 +00:00
|
|
|
XVisualInfo* vi = glXChooseVisual(m_NativeDisplay, DefaultScreen(m_NativeDisplay), attrListDbl);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
/* create a color map */
|
|
|
|
XSetWindowAttributes attr;
|
2013-01-04 21:33:24 +00:00
|
|
|
attr.colormap = XCreateColormap(m_NativeDisplay, RootWindow(m_NativeDisplay, vi->screen),
|
2013-01-04 11:41:51 +00:00
|
|
|
vi->visual, AllocNone);
|
|
|
|
attr.border_pixel = 0;
|
|
|
|
attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask |
|
|
|
|
StructureNotifyMask | SubstructureRedirectMask | SubstructureNotifyMask |
|
|
|
|
EnterWindowMask | LeaveWindowMask | FocusChangeMask ;
|
|
|
|
|
|
|
|
// Create a window at the last position/size
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeWindow = XCreateWindow(m_NativeDisplay, RootWindow(m_NativeDisplay, vi->screen),
|
2013-01-04 11:41:51 +00:00
|
|
|
0 , 0 , w, h, 0, vi->depth, InputOutput, vi->visual,
|
|
|
|
CWBorderPixel | CWColormap | CWEventMask, &attr);
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
XMapWindow (m_NativeDisplay, m_NativeWindow);
|
2013-01-04 11:41:51 +00:00
|
|
|
XFree(vi);
|
|
|
|
|
|
|
|
if (!CreateContext(3, 3)) return false;
|
|
|
|
|
|
|
|
AttachContext();
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
return (m_NativeWindow != 0);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* GSWndOGL::GetDisplay()
|
|
|
|
{
|
|
|
|
// note this part must be only executed when replaying .gs debug file
|
2013-01-04 21:33:24 +00:00
|
|
|
return (void*)m_NativeDisplay;
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSVector4i GSWndOGL::GetClientRect()
|
|
|
|
{
|
|
|
|
unsigned int h = 480;
|
|
|
|
unsigned int w = 640;
|
|
|
|
|
|
|
|
unsigned int borderDummy;
|
|
|
|
unsigned int depthDummy;
|
|
|
|
Window winDummy;
|
|
|
|
int xDummy;
|
|
|
|
int yDummy;
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
if (!m_NativeDisplay) m_NativeDisplay = XOpenDisplay(NULL);
|
|
|
|
XGetGeometry(m_NativeDisplay, m_NativeWindow, &winDummy, &xDummy, &yDummy, &w, &h, &borderDummy, &depthDummy);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
return GSVector4i(0, 0, (int)w, (int)h);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns FALSE if the window has no title, or if th window title is under the strict
|
|
|
|
// management of the emulator.
|
|
|
|
|
|
|
|
bool GSWndOGL::SetWindowText(const char* title)
|
|
|
|
{
|
|
|
|
if (!m_managed) return true;
|
|
|
|
|
|
|
|
XTextProperty prop;
|
|
|
|
|
|
|
|
memset(&prop, 0, sizeof(prop));
|
|
|
|
|
|
|
|
char* ptitle = (char*)title;
|
|
|
|
if (XStringListToTextProperty(&ptitle, 1, &prop)) {
|
2013-01-04 21:33:24 +00:00
|
|
|
XSetWMName(m_NativeDisplay, m_NativeWindow, &prop);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XFree(prop.value);
|
2013-01-04 21:33:24 +00:00
|
|
|
XFlush(m_NativeDisplay);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::SetVSync(bool enable)
|
|
|
|
{
|
|
|
|
// m_swapinterval uses an integer as parameter
|
|
|
|
// 0 -> disable vsync
|
|
|
|
// n -> wait n frame
|
|
|
|
if (m_swapinterval) m_swapinterval((int)enable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Flip()
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
glXSwapBuffers(m_NativeDisplay, m_NativeWindow);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Show()
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
XMapRaised(m_NativeDisplay, m_NativeWindow);
|
|
|
|
XFlush(m_NativeDisplay);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Hide()
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
XUnmapWindow(m_NativeDisplay, m_NativeWindow);
|
|
|
|
XFlush(m_NativeDisplay);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::HideFrame()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
2013-01-04 21:33:24 +00:00
|
|
|
|
2013-01-04 11:41:51 +00:00
|
|
|
#endif
|