#include <stdlib.h>
#if !defined(_WIN32) && !defined(__BEOS__)
#include <GL/glx.h>
#endif
#ifdef __sgi
#include <dlfcn.h>
#endif
#include "glutint.h"
#ifdef GLX_SYNC_FRAME_SGIX
#define GLX_SGIX_video_resize 1
#endif
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
static int canVideoResize = -1;
static int videoResizeChannel;
#else
static int canVideoResize = 0;
#endif
static int videoResizeInUse = 0;
static int dx = -1, dy = -1, dw = -1, dh = -1;
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
static volatile int errorCaught;
static
catchXSGIvcErrors(Display * dpy, XErrorEvent * event)
{
errorCaught = 1;
return 0;
}
#endif
int APIENTRY
glutVideoResizeGet(GLenum param)
{
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
if (canVideoResize < 0) {
canVideoResize = __glutIsSupportedByGLX("GLX_SGIX_video_resize");
if (canVideoResize) {
#if __sgi
void (*func) (void);
void *glxDso = dlopen("libGL.so", RTLD_LAZY);
func = (void (*)(void)) dlsym(glxDso, "glXQueryChannelDeltasSGIX");
if (!func) {
canVideoResize = 0;
} else
#endif
{
char *channelString;
int (*handler) (Display *, XErrorEvent *);
channelString = getenv("GLUT_VIDEO_RESIZE_CHANNEL");
videoResizeChannel = channelString ? atoi(channelString) : 0;
handler = XSetErrorHandler(catchXSGIvcErrors);
errorCaught = 0;
glXQueryChannelDeltasSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, &dx, &dy, &dw, &dh);
XSetErrorHandler(handler);
if (errorCaught ||
dx < 0 || dy < 0 || dw < 0 || dh < 0 ||
dx > 2048 || dy > 2048 || dw > 2048 || dh > 2048) {
canVideoResize = 0;
}
}
}
}
#endif
switch (param) {
case GLUT_VIDEO_RESIZE_POSSIBLE:
return canVideoResize;
case GLUT_VIDEO_RESIZE_IN_USE:
return videoResizeInUse;
case GLUT_VIDEO_RESIZE_X_DELTA:
return dx;
case GLUT_VIDEO_RESIZE_Y_DELTA:
return dy;
case GLUT_VIDEO_RESIZE_WIDTH_DELTA:
return dw;
case GLUT_VIDEO_RESIZE_HEIGHT_DELTA:
return dh;
case GLUT_VIDEO_RESIZE_X:
case GLUT_VIDEO_RESIZE_Y:
case GLUT_VIDEO_RESIZE_WIDTH:
case GLUT_VIDEO_RESIZE_HEIGHT:
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
if (videoResizeInUse) {
int x, y, width, height;
glXQueryChannelRectSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, &x, &y, &width, &height);
switch (param) {
case GLUT_VIDEO_RESIZE_X:
return x;
case GLUT_VIDEO_RESIZE_Y:
return y;
case GLUT_VIDEO_RESIZE_WIDTH:
return width;
case GLUT_VIDEO_RESIZE_HEIGHT:
return height;
}
}
#endif
return -1;
default:
__glutWarning("invalid glutVideoResizeGet parameter: %d", param);
return -1;
}
}
void APIENTRY
glutSetupVideoResizing(void)
{
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
if (glutVideoResizeGet(GLUT_VIDEO_RESIZE_POSSIBLE)) {
glXBindChannelToWindowSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, __glutCurrentWindow->win);
videoResizeInUse = 1;
} else
#endif
__glutFatalError("glutEstablishVideoResizing: video resizing not possible.\n");
}
void APIENTRY
glutStopVideoResizing(void)
{
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
if (glutVideoResizeGet(GLUT_VIDEO_RESIZE_POSSIBLE)) {
if (videoResizeInUse) {
glXBindChannelToWindowSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, None);
videoResizeInUse = 0;
}
}
#endif
}
void APIENTRY
glutVideoResize(int x, int y, int width, int height)
{
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
if (videoResizeInUse) {
#ifdef GLX_SYNC_SWAP_SGIX
glXChannelRectSyncSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, GLX_SYNC_SWAP_SGIX);
#endif
glXChannelRectSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, x, y, width, height);
}
#endif
}
void APIENTRY
glutVideoPan(int x, int y, int width, int height)
{
#if defined(GLX_VERSION_1_1) && defined(GLX_SGIX_video_resize)
if (videoResizeInUse) {
#ifdef GLX_SYNC_FRAME_SGIX
glXChannelRectSyncSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, GLX_SYNC_FRAME_SGIX);
#endif
glXChannelRectSGIX(__glutDisplay, __glutScreen,
videoResizeChannel, x, y, width, height);
}
#endif
}