2010-07-10 08:20:50 +00:00
|
|
|
/* ZZ Open GL graphics plugin
|
|
|
|
* Copyright (c)2009-2010 zeydlitz@gmail.com, arcum42@gmail.com
|
|
|
|
* Based on Zerofrog's ZeroGS KOSMOS (c)2005-2008
|
2010-03-19 02:04:55 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-07-04 22:49:00 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
2010-03-19 02:04:55 +00:00
|
|
|
*/
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
#include "GS.h"
|
|
|
|
#include "zerogs.h"
|
|
|
|
|
2010-06-11 11:48:07 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
#ifdef GL_X11_WINDOW
|
|
|
|
|
2010-06-11 11:48:07 +00:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
bool GLWindow::CreateWindow(void *pDisplay)
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2010-05-01 20:33:53 +00:00
|
|
|
glDisplay = XOpenDisplay(0);
|
|
|
|
glScreen = DefaultScreen(glDisplay);
|
2010-03-19 02:04:55 +00:00
|
|
|
|
2010-05-01 20:33:53 +00:00
|
|
|
if (pDisplay == NULL) return false;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-05-01 20:33:53 +00:00
|
|
|
*(Display**)pDisplay = glDisplay;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-05-01 20:33:53 +00:00
|
|
|
return true;
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 05:24:36 +00:00
|
|
|
bool GLWindow::ReleaseWindow()
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2010-03-19 06:38:44 +00:00
|
|
|
if (context)
|
|
|
|
{
|
|
|
|
if (!glXMakeCurrent(glDisplay, None, NULL))
|
|
|
|
{
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Could not release drawing context.");
|
2010-03-19 06:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glXDestroyContext(glDisplay, context);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
context = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* switch back to original desktop resolution if we were in fullscreen */
|
2010-05-01 20:33:53 +00:00
|
|
|
if (glDisplay != NULL)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
if (fullScreen)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
|
|
|
XF86VidModeSwitchToMode(glDisplay, glScreen, &deskMode);
|
|
|
|
XF86VidModeSetViewPort(glDisplay, glScreen, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
return true;
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindow::CloseWindow()
|
2010-04-25 00:31:27 +00:00
|
|
|
{
|
2010-04-28 02:56:40 +00:00
|
|
|
conf.x = x;
|
|
|
|
conf.y = y;
|
|
|
|
SaveConfig();
|
2010-05-01 20:33:53 +00:00
|
|
|
|
|
|
|
if (glDisplay != NULL)
|
|
|
|
{
|
2010-03-26 09:19:41 +00:00
|
|
|
XCloseDisplay(glDisplay);
|
|
|
|
glDisplay = NULL;
|
|
|
|
}
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 05:24:36 +00:00
|
|
|
bool GLWindow::DisplayWindow(int _width, int _height)
|
2010-03-19 02:04:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
XVisualInfo *vi;
|
|
|
|
Colormap cmap;
|
|
|
|
int dpyWidth, dpyHeight;
|
|
|
|
int glxMajorVersion, glxMinorVersion;
|
|
|
|
int vidModeMajorVersion, vidModeMinorVersion;
|
|
|
|
Atom wmDelete;
|
|
|
|
Window winDummy;
|
|
|
|
unsigned int borderDummy;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-04-28 02:56:40 +00:00
|
|
|
x = conf.x;
|
|
|
|
y = conf.y;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
// attributes for a single buffered visual in RGBA format with at least
|
|
|
|
// 8 bits per color and a 24 bit depth buffer
|
2010-04-25 00:31:27 +00:00
|
|
|
int attrListSgl[] = {GLX_RGBA, GLX_RED_SIZE, 8,
|
|
|
|
GLX_GREEN_SIZE, 8,
|
|
|
|
GLX_BLUE_SIZE, 8,
|
2010-03-19 02:04:55 +00:00
|
|
|
GLX_DEPTH_SIZE, 24,
|
2010-05-01 20:33:53 +00:00
|
|
|
None
|
|
|
|
};
|
2010-03-19 02:04:55 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
// attributes for a double buffered visual in RGBA format with at least
|
2010-03-19 02:04:55 +00:00
|
|
|
// 8 bits per color and a 24 bit depth buffer
|
2010-04-25 00:31:27 +00:00
|
|
|
int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER,
|
|
|
|
GLX_RED_SIZE, 8,
|
|
|
|
GLX_GREEN_SIZE, 8,
|
|
|
|
GLX_BLUE_SIZE, 8,
|
2010-03-19 02:04:55 +00:00
|
|
|
GLX_DEPTH_SIZE, 24,
|
2010-05-01 20:33:53 +00:00
|
|
|
None
|
|
|
|
};
|
2010-03-19 02:04:55 +00:00
|
|
|
|
2010-06-19 06:23:40 +00:00
|
|
|
GLWin.fullScreen = (conf.fullscreen());
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
/* get an appropriate visual */
|
2010-03-19 06:38:44 +00:00
|
|
|
vi = glXChooseVisual(glDisplay, glScreen, attrListDbl);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
if (vi == NULL)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
|
|
|
vi = glXChooseVisual(glDisplay, glScreen, attrListSgl);
|
|
|
|
doubleBuffered = false;
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Only Singlebuffered Visual!");
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
else
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
|
|
|
doubleBuffered = true;
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Got Doublebuffered Visual!");
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
if (vi == NULL)
|
2010-04-08 21:58:14 +00:00
|
|
|
{
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Failed to get buffered Visual!");
|
2010-04-08 21:58:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-19 02:04:55 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
glXQueryVersion(glDisplay, &glxMajorVersion, &glxMinorVersion);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("glX-Version %d.%d", glxMajorVersion, glxMinorVersion);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
/* create a GLX context */
|
2010-04-08 21:58:14 +00:00
|
|
|
context = glXCreateContext(glDisplay, vi, NULL, GL_TRUE);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
/* create a color map */
|
2010-03-19 06:38:44 +00:00
|
|
|
cmap = XCreateColormap(glDisplay, RootWindow(glDisplay, vi->screen),
|
2010-03-19 02:04:55 +00:00
|
|
|
vi->visual, AllocNone);
|
2010-03-19 06:38:44 +00:00
|
|
|
attr.colormap = cmap;
|
|
|
|
attr.border_pixel = 0;
|
2010-03-19 02:04:55 +00:00
|
|
|
|
|
|
|
// get a connection
|
2010-03-19 06:38:44 +00:00
|
|
|
XF86VidModeQueryVersion(glDisplay, &vidModeMajorVersion, &vidModeMinorVersion);
|
2010-03-19 02:04:55 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
if (fullScreen)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
2010-03-19 02:04:55 +00:00
|
|
|
XF86VidModeModeInfo **modes = NULL;
|
|
|
|
int modeNum = 0;
|
|
|
|
int bestMode = 0;
|
|
|
|
|
|
|
|
// set best mode to current
|
|
|
|
bestMode = 0;
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("XF86VidModeExtension-Version %d.%d.", vidModeMajorVersion, vidModeMinorVersion);
|
2010-03-19 06:38:44 +00:00
|
|
|
XF86VidModeGetAllModeLines(glDisplay, glScreen, &modeNum, &modes);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-05-01 20:33:53 +00:00
|
|
|
if (modeNum > 0 && modes != NULL)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
2010-03-19 02:04:55 +00:00
|
|
|
/* save desktop-resolution before switching modes */
|
2010-03-19 06:38:44 +00:00
|
|
|
deskMode = *modes[0];
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
/* look for mode with requested resolution */
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
for (i = 0; i < modeNum; i++)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
|
|
|
if ((modes[i]->hdisplay == _width) && (modes[i]->vdisplay == _height))
|
|
|
|
{
|
2010-03-19 02:04:55 +00:00
|
|
|
bestMode = i;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
2010-03-19 02:04:55 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
XF86VidModeSwitchToMode(glDisplay, glScreen, modes[bestMode]);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
XF86VidModeSetViewPort(glDisplay, glScreen, 0, 0);
|
2010-03-19 02:04:55 +00:00
|
|
|
dpyWidth = modes[bestMode]->hdisplay;
|
|
|
|
dpyHeight = modes[bestMode]->vdisplay;
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Resolution %dx%d.", dpyWidth, dpyHeight);
|
2010-03-19 02:04:55 +00:00
|
|
|
XFree(modes);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
/* create a fullscreen window */
|
2010-03-19 06:38:44 +00:00
|
|
|
attr.override_redirect = True;
|
|
|
|
attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
|
|
|
|
glWindow = XCreateWindow(glDisplay, RootWindow(glDisplay, vi->screen),
|
2010-05-01 20:33:53 +00:00
|
|
|
0, 0, dpyWidth, dpyHeight, 0, vi->depth, InputOutput, vi->visual,
|
|
|
|
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect,
|
|
|
|
&attr);
|
2010-03-19 06:38:44 +00:00
|
|
|
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);
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
else
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Failed to start fullscreen. If you received the \n"
|
2010-05-01 20:33:53 +00:00
|
|
|
"\"XFree86-VidModeExtension\" extension is missing, add\n"
|
|
|
|
"Load \"extmod\"\n"
|
|
|
|
"to your X configuration file (under the Module Section)");
|
2010-03-19 06:38:44 +00:00
|
|
|
fullScreen = false;
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
|
|
|
if (!fullScreen)
|
2010-03-19 06:38:44 +00:00
|
|
|
{
|
2010-03-19 02:04:55 +00:00
|
|
|
// create a window in window mode
|
2010-03-19 06:38:44 +00:00
|
|
|
attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
|
|
|
|
glWindow = XCreateWindow(glDisplay, RootWindow(glDisplay, vi->screen),
|
2010-05-01 20:33:53 +00:00
|
|
|
0, 0, _width, _height, 0, vi->depth, InputOutput, vi->visual,
|
|
|
|
CWBorderPixel | CWColormap | CWEventMask, &attr);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
// only set window title and handle wm_delete_events if in windowed mode
|
2010-03-19 06:38:44 +00:00
|
|
|
wmDelete = XInternAtom(glDisplay, "WM_DELETE_WINDOW", True);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
XSetWMProtocols(glDisplay, glWindow, &wmDelete, 1);
|
|
|
|
XSetStandardProperties(glDisplay, glWindow, "ZZOgl-PG", "ZZOgl-PG", None, NULL, 0, NULL);
|
|
|
|
XMapRaised(glDisplay, glWindow);
|
2010-04-28 02:56:40 +00:00
|
|
|
XMoveWindow(glDisplay, glWindow, x, y);
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|
2010-03-19 02:04:55 +00:00
|
|
|
|
|
|
|
// connect the glx-context to the window
|
2010-03-19 06:38:44 +00:00
|
|
|
glXMakeCurrent(glDisplay, glWindow, context);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 06:38:44 +00:00
|
|
|
XGetGeometry(glDisplay, glWindow, &winDummy, &x, &y, &width, &height, &borderDummy, &depth);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("Depth %d", depth);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
if (glXIsDirect(glDisplay, context))
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("You have Direct Rendering!");
|
2010-03-19 02:04:55 +00:00
|
|
|
else
|
2010-04-25 08:33:05 +00:00
|
|
|
ZZLog::Error_Log("No Direct Rendering possible!");
|
2010-03-19 02:04:55 +00:00
|
|
|
|
|
|
|
// better for pad plugin key input (thc)
|
2010-04-25 00:31:27 +00:00
|
|
|
XSelectInput(glDisplay, glWindow, ExposureMask | KeyPressMask | KeyReleaseMask |
|
2010-03-19 02:04:55 +00:00
|
|
|
ButtonPressMask | StructureNotifyMask | EnterWindowMask | LeaveWindowMask |
|
2010-05-01 20:33:53 +00:00
|
|
|
FocusChangeMask);
|
2010-03-19 05:24:36 +00:00
|
|
|
|
|
|
|
return true;
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 05:24:36 +00:00
|
|
|
void GLWindow::SwapGLBuffers()
|
2010-03-19 02:04:55 +00:00
|
|
|
{
|
2010-05-01 20:33:53 +00:00
|
|
|
glXSwapBuffers(glDisplay, glWindow);
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 02:04:55 +00:00
|
|
|
void GLWindow::SetTitle(char *strtitle)
|
|
|
|
{
|
2010-03-19 05:24:36 +00:00
|
|
|
XTextProperty prop;
|
|
|
|
memset(&prop, 0, sizeof(prop));
|
|
|
|
char* ptitle = strtitle;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
|
|
|
if (XStringListToTextProperty(&ptitle, 1, &prop))
|
2010-03-19 05:24:36 +00:00
|
|
|
XSetWMName(glDisplay, glWindow, &prop);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 05:24:36 +00:00
|
|
|
XFree(prop.value);
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLWindow::ResizeCheck()
|
|
|
|
{
|
2010-05-01 20:33:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((event.xconfigure.x != x) || (event.xconfigure.y != y))
|
|
|
|
{
|
|
|
|
x = event.xconfigure.x;
|
|
|
|
y = event.xconfigure.y;
|
|
|
|
}
|
|
|
|
}
|
2010-03-19 02:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|