2002-11-15 05:58:57 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002 Light Weight Java Game Library Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of 'Light Weight Java Game Library' nor the names of
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2002-11-15 06:00:19 -05:00
|
|
|
* $Id$
|
2002-11-15 05:58:57 -05:00
|
|
|
*
|
2002-11-15 06:10:32 -05:00
|
|
|
* Linux specific library for display handling.
|
2002-11-15 05:58:57 -05:00
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
2002-11-15 06:00:19 -05:00
|
|
|
* @version $Revision$
|
2002-11-15 05:58:57 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/extensions/xf86vmode.h>
|
|
|
|
#include <GL/glx.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <jni.h>
|
|
|
|
#include "org_lwjgl_Display.h"
|
|
|
|
|
|
|
|
|
|
|
|
Display * disp;
|
|
|
|
int screen;
|
2002-11-16 15:10:42 -05:00
|
|
|
int current_fullscreen;
|
2002-11-20 05:33:37 -05:00
|
|
|
int current_focused;
|
2002-11-15 05:40:55 -05:00
|
|
|
Window win;
|
2002-11-28 16:49:11 -05:00
|
|
|
int win_width;
|
|
|
|
int win_height;
|
2002-11-15 05:40:55 -05:00
|
|
|
XF86VidModeModeInfo **avail_modes;
|
|
|
|
XVisualInfo * vis_info;
|
|
|
|
|
|
|
|
void waitMapped(Display *disp, Window win) {
|
|
|
|
XEvent event;
|
|
|
|
|
|
|
|
do {
|
|
|
|
XMaskEvent(disp, StructureNotifyMask, &event);
|
|
|
|
} while ((event.type != MapNotify) || (event.xmap.event != win));
|
|
|
|
}
|
|
|
|
|
2002-11-20 05:33:37 -05:00
|
|
|
int isFocused(void) {
|
|
|
|
XEvent event;
|
|
|
|
while (XCheckMaskEvent(disp, EnterWindowMask | LeaveWindowMask, &event)) {
|
|
|
|
if (event.type == EnterNotify)
|
|
|
|
current_focused = 1;
|
|
|
|
else if (event.type == LeaveNotify)
|
|
|
|
current_focused = 0;
|
|
|
|
}
|
|
|
|
return current_focused;
|
|
|
|
}
|
|
|
|
|
2002-11-15 05:40:55 -05:00
|
|
|
int getDisplayModes(Display *disp, int screen, int *num_modes, XF86VidModeModeInfo ***avail_modes) {
|
2002-11-19 03:50:57 -05:00
|
|
|
int event_base, error_base, xvid_ver, xvid_rev;
|
2002-11-15 05:40:55 -05:00
|
|
|
|
|
|
|
if (!XF86VidModeQueryExtension(disp, &event_base, &error_base)) {
|
|
|
|
#ifdef _DEBUG
|
2002-11-20 05:33:37 -05:00
|
|
|
printf("XF86VidMode extension not available\n");
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
XF86VidModeQueryVersion(disp, &xvid_ver, &xvid_rev);
|
|
|
|
#ifdef _DEBUG
|
2002-11-20 05:33:37 -05:00
|
|
|
printf("XF86VidMode extension version %i.%i\n", xvid_ver, xvid_rev);
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
|
|
|
XF86VidModeGetAllModeLines(disp, screen, num_modes, avail_modes);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jboolean fullscreen) {
|
|
|
|
Window root_win;
|
|
|
|
XSetWindowAttributes attribs;
|
|
|
|
Colormap cmap;
|
|
|
|
int attribmask;
|
|
|
|
int bpe = bpp/4;
|
2002-11-25 22:30:41 -05:00
|
|
|
int attriblist[] = { GLX_RGBA,
|
|
|
|
GLX_DOUBLEBUFFER,
|
|
|
|
GLX_DEPTH_SIZE, 24,
|
|
|
|
GLX_RED_SIZE, bpe,
|
|
|
|
GLX_GREEN_SIZE, bpe,
|
|
|
|
GLX_BLUE_SIZE, bpe,
|
|
|
|
GLX_ALPHA_SIZE, bpe,
|
|
|
|
None };
|
|
|
|
int attriblistna[] = { GLX_RGBA,
|
|
|
|
GLX_DOUBLEBUFFER,
|
|
|
|
GLX_DEPTH_SIZE, 24,
|
|
|
|
GLX_RED_SIZE, bpe,
|
|
|
|
GLX_GREEN_SIZE, bpe,
|
|
|
|
GLX_BLUE_SIZE, bpe,
|
|
|
|
None };
|
|
|
|
|
|
|
|
int num_modes, i;
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-28 16:49:11 -05:00
|
|
|
win_width = width;
|
|
|
|
win_height = height;
|
2002-11-16 15:10:42 -05:00
|
|
|
current_fullscreen = fullscreen;
|
2002-11-20 05:33:37 -05:00
|
|
|
current_focused = 0;
|
2002-11-15 05:40:55 -05:00
|
|
|
disp = XOpenDisplay(NULL);
|
|
|
|
if (disp == NULL) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not open X connection\n");
|
|
|
|
#endif
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
|
|
|
screen = DefaultScreen(disp);
|
2002-11-17 11:14:53 -05:00
|
|
|
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
|
|
|
|
XCloseDisplay(disp);
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not get display modes\n");
|
|
|
|
#endif
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
2002-11-15 05:40:55 -05:00
|
|
|
root_win = RootWindow(disp, screen);
|
|
|
|
|
|
|
|
vis_info = glXChooseVisual(disp, screen, attriblist);
|
2002-11-25 22:30:41 -05:00
|
|
|
|
|
|
|
/* might be a better way to handle not being able to set GLX_ALPHA_SIZE... */
|
|
|
|
if (vis_info == NULL) {
|
|
|
|
vis_info = glXChooseVisual(disp, screen, attriblistna);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vis_info == NULL) {
|
2002-11-17 11:14:53 -05:00
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not choose glx visual\n");
|
|
|
|
#endif
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmap = XCreateColormap(disp, root_win, vis_info->visual, AllocNone);
|
|
|
|
attribs.colormap = cmap;
|
2002-11-20 05:33:37 -05:00
|
|
|
attribs.event_mask = StructureNotifyMask | EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
|
2002-11-15 05:40:55 -05:00
|
|
|
attribs.background_pixel = 0xFF000000;
|
|
|
|
attribmask = CWColormap | CWBackPixel | CWEventMask;
|
|
|
|
if (fullscreen) {
|
|
|
|
attribmask |= CWOverrideRedirect;
|
|
|
|
attribs.override_redirect = True;
|
|
|
|
}
|
|
|
|
win = XCreateWindow(disp, root_win, 0, 0, width, height, 0, vis_info->depth, InputOutput, vis_info->visual, attribmask, &attribs);
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Created window\n");
|
|
|
|
#endif
|
2002-11-20 05:33:37 -05:00
|
|
|
XMapRaised(disp, win);
|
|
|
|
waitMapped(disp, win);
|
2002-11-15 05:40:55 -05:00
|
|
|
if (fullscreen) {
|
|
|
|
for ( i = 0; i < num_modes; ++i ) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Mode %d: %dx%d\n", i, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay);
|
|
|
|
#endif
|
|
|
|
if (avail_modes[i]->hdisplay == width && avail_modes[i]->vdisplay == height) {
|
|
|
|
if (!XF86VidModeSwitchToMode(disp, screen, avail_modes[i])) {
|
2002-11-17 11:14:53 -05:00
|
|
|
XFree(vis_info);
|
|
|
|
XFree(avail_modes);
|
|
|
|
XDestroyWindow(disp, win);
|
|
|
|
XCloseDisplay(disp);
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not switch mode\n");
|
|
|
|
#endif
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XF86VidModeSetViewPort(disp, screen, 0, 0);
|
|
|
|
}
|
|
|
|
XClearWindow(disp, win);
|
|
|
|
XSync(disp, True);
|
2002-11-20 05:33:37 -05:00
|
|
|
isFocused();
|
2002-11-15 05:40:55 -05:00
|
|
|
return JNI_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_Display_nDestroy(JNIEnv * env, jclass clazz) {
|
|
|
|
XDestroyWindow(disp, win);
|
2002-11-16 15:10:42 -05:00
|
|
|
if (current_fullscreen) {
|
|
|
|
if (!XF86VidModeSwitchToMode(disp, screen, avail_modes[0])) {
|
2002-11-15 05:40:55 -05:00
|
|
|
#ifdef _DEBUG
|
2002-11-16 15:10:42 -05:00
|
|
|
printf("Could not switch mode\n");
|
2002-11-15 05:40:55 -05:00
|
|
|
#endif
|
2002-11-16 15:10:42 -05:00
|
|
|
}
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
2002-11-17 11:14:53 -05:00
|
|
|
XFree(avail_modes);
|
2002-11-15 05:40:55 -05:00
|
|
|
XFree(vis_info);
|
|
|
|
XCloseDisplay(disp);
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Closed X connection\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Class: org_lwjgl_Display
|
|
|
|
* Method: getAvailableDisplayModes
|
|
|
|
* Signature: ()[Lorg/lwjgl/DisplayMode;
|
|
|
|
*/
|
|
|
|
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes
|
|
|
|
(JNIEnv * env, jclass clazz)
|
|
|
|
{
|
|
|
|
int num_modes, i;
|
|
|
|
|
|
|
|
Display *disp = XOpenDisplay(NULL);
|
|
|
|
int screen = DefaultScreen(disp);
|
|
|
|
XF86VidModeModeInfo **avail_modes;
|
|
|
|
|
|
|
|
int depth = DefaultDepth(disp, screen);
|
|
|
|
|
|
|
|
if (disp == NULL) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not open X connection\n");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!getDisplayModes(disp, screen, &num_modes, &avail_modes)) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
printf("Could not get display modes\n");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Allocate an array of DisplayModes big enough
|
2002-11-19 03:50:57 -05:00
|
|
|
jclass displayModeClass = env->FindClass("org/lwjgl/DisplayMode");
|
|
|
|
jobjectArray ret = env->NewObjectArray(num_modes, displayModeClass, NULL);
|
|
|
|
jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIII)V");
|
2002-11-15 05:40:55 -05:00
|
|
|
|
|
|
|
for (i = 0; i < num_modes; i++) {
|
2002-11-19 03:50:57 -05:00
|
|
|
jobject displayMode = env->NewObject(displayModeClass, displayModeConstructor, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay, depth, 0);
|
|
|
|
env->SetObjectArrayElement(ret, i, displayMode);
|
2002-11-15 05:40:55 -05:00
|
|
|
}
|
|
|
|
XFree(avail_modes);
|
|
|
|
XCloseDisplay(disp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|