ZeroGS: Move most of the X11 window code to one file, to make it easier to switch from using an X11 window to a Gtk window.

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2394 96395faa-99c1-11dd-bbfe-3dabce05a288
This commit is contained in:
arcum42 2009-12-24 05:53:29 +00:00
parent 2f38decdab
commit 4b0b270776
4 changed files with 279 additions and 198 deletions

View File

@ -0,0 +1,242 @@
/* ZeroGS KOSMOS
* Copyright (C) 2005-2006 zerofrog@gmail.com
*
* 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 of the License, 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "GS.h"
#include "zerogs.h"
#ifdef GL_X11_WINDOW
bool GLWindow::CreateWindow(void *pDisplay)
{
glDisplay = XOpenDisplay(0);
glScreen = DefaultScreen(glDisplay);
if ( pDisplay == NULL ) return false;
*(Display**)pDisplay = glDisplay;
return true;
}
bool GLWindow::DestroyWindow()
{
if (context)
{
if (!glXMakeCurrent(glDisplay, None, NULL))
{
ERROR_LOG("Could not release drawing context.\n");
}
glXDestroyContext(glDisplay, context);
context = NULL;
}
/* switch back to original desktop resolution if we were in fullScreen */
if ( glDisplay != NULL )
{
if (fullScreen)
{
XF86VidModeSwitchToMode(glDisplay, glScreen, &deskMode);
XF86VidModeSetViewPort(glDisplay, glScreen, 0, 0);
}
}
}
void GLWindow::CloseWindow()
{
if ( glDisplay != NULL )
{
XCloseDisplay(glDisplay);
glDisplay = NULL;
}
}
void GLWindow::DisplayWindow(int _width, int _height)
{
XVisualInfo *vi;
Colormap cmap;
int GLDisplayWidth, GLDisplayHeight;
int glxMajorVersion, glxMinorVersion;
int vidModeMajorVersion, vidModeMinorVersion;
Atom wmDelete;
Window winDummy;
unsigned int borderDummy;
// attributes for a single buffered visual in RGBA format with at least
// 8 bits per color and a 24 bit depth buffer
int attrListSgl[] = {GLX_RGBA, GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24,
None};
// attributes for a double buffered visual in RGBA format with at least
// 8 bits per color and a 24 bit depth buffer
int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24,
None };
fullScreen = !!(conf.options & GSOPTION_FULLSCREEN);
/* get an appropriate visual */
vi = glXChooseVisual(glDisplay, glScreen, attrListDbl);
if (vi == NULL) {
vi = glXChooseVisual(glDisplay, glScreen, attrListSgl);
doubleBuffered = false;
ERROR_LOG("Only Singlebuffered Visual!\n");
}
else {
doubleBuffered = true;
ERROR_LOG("Got Doublebuffered Visual!\n");
}
glXQueryVersion(glDisplay, &glxMajorVersion, &glxMinorVersion);
ERROR_LOG("glX-Version %d.%d\n", glxMajorVersion, glxMinorVersion);
/* create a GLX context */
context = glXCreateContext(glDisplay, vi, 0, GL_TRUE);
/* create a color map */
cmap = XCreateColormap(glDisplay, RootWindow(glDisplay, vi->screen),
vi->visual, AllocNone);
attr.colormap = cmap;
attr.border_pixel = 0;
// get a connection
XF86VidModeQueryVersion(glDisplay, &vidModeMajorVersion, &vidModeMinorVersion);
if (fullScreen) {
XF86VidModeModeInfo **modes = NULL;
int modeNum = 0;
int bestMode = 0;
// set best mode to current
bestMode = 0;
ERROR_LOG("XF86VidModeExtension-Version %d.%d\n", vidModeMajorVersion, vidModeMinorVersion);
XF86VidModeGetAllModeLines(glDisplay, glScreen, &modeNum, &modes);
if( modeNum > 0 && modes != NULL ) {
/* save desktop-resolution before switching modes */
GLWin.deskMode = *modes[0];
/* look for mode with requested resolution */
for (int i = 0; i < modeNum; i++) {
if ((modes[i]->hdisplay == _width) && (modes[i]->vdisplay == _height)) {
bestMode = i;
}
}
XF86VidModeSwitchToMode(glDisplay, glScreen, modes[bestMode]);
XF86VidModeSetViewPort(glDisplay, glScreen, 0, 0);
GLDisplayWidth = modes[bestMode]->hdisplay;
GLDisplayHeight = modes[bestMode]->vdisplay;
ERROR_LOG("Resolution %dx%d\n", GLDisplayWidth, GLDisplayHeight);
XFree(modes);
/* create a fullscreen window */
attr.override_redirect = True;
attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
StructureNotifyMask;
glWindow = XCreateWindow(glDisplay, RootWindow(glDisplay, vi->screen),
0, 0, GLDisplayWidth, GLDisplayHeight, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
&attr);
XWarpPointer(glDisplay, None, glWindow, 0, 0, 0, 0, 0, 0);
XMapRaised(glDisplay, glWindow);
XGrabKeyboard(glDisplay, glWindow, True, GrabModeAsync,
GrabModeAsync, CurrentTime);
XGrabPointer(glDisplay, glWindow, True, ButtonPressMask,
GrabModeAsync, GrabModeAsync, glWindow, None, CurrentTime);
}
else {
ERROR_LOG("Failed to start fullscreen. If you received the \n"
"\"XFree86-VidModeExtension\" extension is missing, add\n"
"Load \"extmod\"\n"
"to your X configuration file (under the Module Section)\n");
fullScreen = 0;
}
}
if( !fullScreen ) {
//XRootWindow(glDisplay,glScreen)
//int X = (rcdesktop.right-rcdesktop.left)/2 - (rc.right-rc.left)/2;
//int Y = (rcdesktop.bottom-rcdesktop.top)/2 - (rc.bottom-rc.top)/2;
// create a window in window mode
attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
StructureNotifyMask;
glWindow = XCreateWindow(glDisplay, RootWindow(glDisplay, vi->screen),
0, 0, _width, _height, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &attr);
// only set window title and handle wm_delete_events if in windowed mode
wmDelete = XInternAtom(glDisplay, "WM_DELETE_WINDOW", True);
XSetWMProtocols(glDisplay, glWindow, &wmDelete, 1);
XSetStandardProperties(glDisplay, glWindow, "ZeroGS",
"ZeroGS", None, NULL, 0, NULL);
XMapRaised(glDisplay, glWindow);
}
// connect the glx-context to the window
glXMakeCurrent(glDisplay, glWindow, context);
XGetGeometry(glDisplay, glWindow, &winDummy, &x, &y,
&width, &height, &borderDummy, &depth);
ERROR_LOG("Depth %d\n", depth);
if (glXIsDirect(glDisplay, context))
ERROR_LOG("you have Direct Rendering!\n");
else
ERROR_LOG("no Direct Rendering possible!\n");
// better for pad plugin key input (thc)
XSelectInput(glDisplay, glWindow, ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPressMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask |
FocusChangeMask );
}
void GLWindow::SwapBuffers()
{
glXSwapBuffers(glDisplay, glWindow);
}
void GLWindow::SetTitle(char *strtitle)
{
XTextProperty prop;
memset(&prop, 0, sizeof(prop));
char* ptitle = strtitle;
if( XStringListToTextProperty(&ptitle, 1, &prop) )
XSetWMName(glDisplay, glWindow, &prop);
XFree(prop.value);
}
void GLWindow::ResizeCheck()
{
XEvent event;
while(XCheckTypedEvent(glDisplay, ConfigureNotify, &event))
{
if ((event.xconfigure.width != width) || (event.xconfigure.height != height)) {
ZeroGS::ChangeWindowSize(event.xconfigure.width, event.xconfigure.height);
width = event.xconfigure.width;
height = event.xconfigure.height;
}
}
}
#endif

View File

@ -121,19 +121,32 @@ struct RECT
int right, bottom;
};
typedef struct {
Display *dpy;
int screen;
Window win;
GLXContext ctx;
XSetWindowAttributes attr;
Bool fs;
Bool doubleBuffered;
XF86VidModeModeInfo deskMode;
int x, y;
unsigned int width, height;
unsigned int depth;
} GLWindow;
#define GL_X11_WINDOW
class GLWindow
{
private:
#ifdef GL_X11_WINDOW
Display *glDisplay;
Window glWindow;
int glScreen;
GLXContext context;
XSetWindowAttributes attr;
XF86VidModeModeInfo deskMode;
#endif
bool fullScreen, doubleBuffered;
s32 x, y;
u32 width, height, depth;
public:
void SwapBuffers();
void SetTitle(char *strtitle);
bool CreateWindow(void *pDisplay);
bool DestroyWindow();
void CloseWindow();
void DisplayWindow(int _width, int _height);
void ResizeCheck();
};
extern GLWindow GLWin;

View File

@ -592,13 +592,9 @@ s32 CALLBACK GSopen(void *pDsp, char *Title, int multithread)
LoadConfig();
strcpy(GStitle, Title);
GLWin.dpy = XOpenDisplay(0);
GLWin.screen = DefaultScreen(GLWin.dpy);
if( pDsp != NULL )
*(Display**)pDsp = GLWin.dpy;
GLWin.CreateWindow(pDsp);
ERROR_LOG("creating zerogs\n");
//if (conf.record) recOpen();
if( !ZeroGS::Create(conf.width, conf.height) )
@ -630,16 +626,8 @@ s32 CALLBACK GSopen(void *pDsp, char *Title, int multithread)
void ProcessMessages()
{
XEvent event;
// check resizing
while(XCheckTypedEvent(GLWin.dpy, ConfigureNotify, &event)) {
if ((event.xconfigure.width != GLWin.width) || (event.xconfigure.height != GLWin.height)) {
ZeroGS::ChangeWindowSize(event.xconfigure.width, event.xconfigure.height);
GLWin.width = event.xconfigure.width;
GLWin.height = event.xconfigure.height;
}
}
GLWin.ResizeCheck();
if ( THR_KeyEvent ) { // This values was passed from GSKeyEvents witch could be in another thread
int my_KeyEvent = THR_KeyEvent;
@ -673,10 +661,7 @@ void CALLBACK GSclose() {
GShwnd = NULL;
}
#else
if( GLWin.dpy != NULL ) {
XCloseDisplay(GLWin.dpy);
GLWin.dpy = NULL;
}
GLWin.CloseWindow();
#endif
}
@ -790,12 +775,8 @@ void CALLBACK GSvsync(int interlace)
if( !(conf.options&GSOPTION_FULLSCREEN) )
SetWindowText(GShwnd, strtitle);
#else // linux
XTextProperty prop;
memset(&prop, 0, sizeof(prop));
char* ptitle = strtitle;
if( XStringListToTextProperty(&ptitle, 1, &prop) )
XSetWMName(GLWin.dpy, GLWin.win, &prop);
XFree(prop.value);
if (!(conf.options & GSOPTION_FULLSCREEN))
GLWin.SetTitle(strtitle);
#endif
if( fFPS < 16 ) UPDATE_FRAMES = 4;

View File

@ -1056,147 +1056,7 @@ bool ZeroGS::Create(int _width, int _height)
}
#else
XVisualInfo *vi;
Colormap cmap;
int dpyWidth, dpyHeight;
int glxMajorVersion, glxMinorVersion;
int vidModeMajorVersion, vidModeMinorVersion;
Atom wmDelete;
Window winDummy;
unsigned int borderDummy;
// attributes for a single buffered visual in RGBA format with at least
// 8 bits per color and a 24 bit depth buffer
int attrListSgl[] = {GLX_RGBA, GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24,
None};
// attributes for a double buffered visual in RGBA format with at least
// 8 bits per color and a 24 bit depth buffer
int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24,
None };
GLWin.fs = !!(conf.options & GSOPTION_FULLSCREEN);
/* get an appropriate visual */
vi = glXChooseVisual(GLWin.dpy, GLWin.screen, attrListDbl);
if (vi == NULL) {
vi = glXChooseVisual(GLWin.dpy, GLWin.screen, attrListSgl);
GLWin.doubleBuffered = False;
ERROR_LOG("Only Singlebuffered Visual!\n");
}
else {
GLWin.doubleBuffered = True;
ERROR_LOG("Got Doublebuffered Visual!\n");
}
glXQueryVersion(GLWin.dpy, &glxMajorVersion, &glxMinorVersion);
ERROR_LOG("glX-Version %d.%d\n", glxMajorVersion, glxMinorVersion);
/* create a GLX context */
GLWin.ctx = glXCreateContext(GLWin.dpy, vi, 0, GL_TRUE);
/* create a color map */
cmap = XCreateColormap(GLWin.dpy, RootWindow(GLWin.dpy, vi->screen),
vi->visual, AllocNone);
GLWin.attr.colormap = cmap;
GLWin.attr.border_pixel = 0;
// get a connection
XF86VidModeQueryVersion(GLWin.dpy, &vidModeMajorVersion, &vidModeMinorVersion);
if (GLWin.fs) {
XF86VidModeModeInfo **modes = NULL;
int modeNum = 0;
int bestMode = 0;
// set best mode to current
bestMode = 0;
ERROR_LOG("XF86VidModeExtension-Version %d.%d\n", vidModeMajorVersion, vidModeMinorVersion);
XF86VidModeGetAllModeLines(GLWin.dpy, GLWin.screen, &modeNum, &modes);
if( modeNum > 0 && modes != NULL ) {
/* save desktop-resolution before switching modes */
GLWin.deskMode = *modes[0];
/* look for mode with requested resolution */
for (i = 0; i < modeNum; i++) {
if ((modes[i]->hdisplay == _width) && (modes[i]->vdisplay == _height)) {
bestMode = i;
}
}
XF86VidModeSwitchToMode(GLWin.dpy, GLWin.screen, modes[bestMode]);
XF86VidModeSetViewPort(GLWin.dpy, GLWin.screen, 0, 0);
dpyWidth = modes[bestMode]->hdisplay;
dpyHeight = modes[bestMode]->vdisplay;
ERROR_LOG("Resolution %dx%d\n", dpyWidth, dpyHeight);
XFree(modes);
/* create a fullscreen window */
GLWin.attr.override_redirect = True;
GLWin.attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
StructureNotifyMask;
GLWin.win = XCreateWindow(GLWin.dpy, RootWindow(GLWin.dpy, vi->screen),
0, 0, dpyWidth, dpyHeight, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
&GLWin.attr);
XWarpPointer(GLWin.dpy, None, GLWin.win, 0, 0, 0, 0, 0, 0);
XMapRaised(GLWin.dpy, GLWin.win);
XGrabKeyboard(GLWin.dpy, GLWin.win, True, GrabModeAsync,
GrabModeAsync, CurrentTime);
XGrabPointer(GLWin.dpy, GLWin.win, True, ButtonPressMask,
GrabModeAsync, GrabModeAsync, GLWin.win, None, CurrentTime);
}
else {
ERROR_LOG("Failed to start fullscreen. If you received the \n"
"\"XFree86-VidModeExtension\" extension is missing, add\n"
"Load \"extmod\"\n"
"to your X configuration file (under the Module Section)\n");
GLWin.fs = 0;
}
}
if( !GLWin.fs ) {
//XRootWindow(dpy,screen)
//int X = (rcdesktop.right-rcdesktop.left)/2 - (rc.right-rc.left)/2;
//int Y = (rcdesktop.bottom-rcdesktop.top)/2 - (rc.bottom-rc.top)/2;
// create a window in window mode
GLWin.attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask |
StructureNotifyMask;
GLWin.win = XCreateWindow(GLWin.dpy, RootWindow(GLWin.dpy, vi->screen),
0, 0, _width, _height, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &GLWin.attr);
// only set window title and handle wm_delete_events if in windowed mode
wmDelete = XInternAtom(GLWin.dpy, "WM_DELETE_WINDOW", True);
XSetWMProtocols(GLWin.dpy, GLWin.win, &wmDelete, 1);
XSetStandardProperties(GLWin.dpy, GLWin.win, "ZeroGS",
"ZeroGS", None, NULL, 0, NULL);
XMapRaised(GLWin.dpy, GLWin.win);
}
// connect the glx-context to the window
glXMakeCurrent(GLWin.dpy, GLWin.win, GLWin.ctx);
XGetGeometry(GLWin.dpy, GLWin.win, &winDummy, &GLWin.x, &GLWin.y,
&GLWin.width, &GLWin.height, &borderDummy, &GLWin.depth);
ERROR_LOG("Depth %d\n", GLWin.depth);
if (glXIsDirect(GLWin.dpy, GLWin.ctx))
ERROR_LOG("you have Direct Rendering!\n");
else
ERROR_LOG("no Direct Rendering possible!\n");
// better for pad plugin key input (thc)
XSelectInput(GLWin.dpy, GLWin.win, ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPressMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask |
FocusChangeMask );
GLWin.DisplayWindow(_width, _height);
#endif
// fill the opengl extension map
@ -1770,22 +1630,7 @@ void ZeroGS::Destroy(BOOL bD3D)
hDC=NULL; // Set DC To NULL
}
#else // linux
if (GLWin.ctx)
{
if (!glXMakeCurrent(GLWin.dpy, None, NULL))
{
ERROR_LOG("Could not release drawing context.\n");
}
glXDestroyContext(GLWin.dpy, GLWin.ctx);
GLWin.ctx = NULL;
}
/* switch back to original desktop resolution if we were in fs */
if( GLWin.dpy != NULL ) {
if (GLWin.fs) {
XF86VidModeSwitchToMode(GLWin.dpy, GLWin.screen, &GLWin.deskMode);
XF86VidModeSetViewPort(GLWin.dpy, GLWin.screen, 0, 0);
}
}
GLWin.DestroyWindow();
#endif
mapGLExtensions.clear();
@ -3476,7 +3321,7 @@ void ZeroGS::RenderCustom(float fAlpha)
#ifdef _WIN32
SwapBuffers(hDC);
#else
glXSwapBuffers(GLWin.dpy, GLWin.win);
GLWin.SwapBuffers();
#endif
glEnable(GL_SCISSOR_TEST);
@ -3983,7 +3828,7 @@ void ZeroGS::RenderCRTC(int interlace)
lastswaptime = timeGetTime();
//}
#else
glXSwapBuffers(GLWin.dpy, GLWin.win);
GLWin.SwapBuffers();
#endif
// if( glGetError() != GL_NO_ERROR) {