Added closeRequested support to linux

This commit is contained in:
Elias Naur 2003-02-09 17:01:01 +00:00
parent 36d8f34e86
commit c022100cab
4 changed files with 99 additions and 78 deletions

View File

@ -55,6 +55,7 @@ Display * disp;
int screen; int screen;
Window win; Window win;
static jfieldID fid_close;
static bool current_fullscreen; static bool current_fullscreen;
static bool current_focused; static bool current_focused;
static bool current_minimized; static bool current_minimized;
@ -62,7 +63,15 @@ static int win_width;
static int win_height; static int win_height;
static XF86VidModeModeInfo **avail_modes; static XF86VidModeModeInfo **avail_modes;
static XVisualInfo * vis_info; static XVisualInfo * vis_info;
static Atom delete_atom;
static bool gl_loaded = false; static bool gl_loaded = false;
static JNIEnv *saved_env;
static jclass saved_clazz;
extern void handlePointerMotion(XMotionEvent *);
extern void handleButtonPress(XButtonEvent *);
extern void handleButtonRelease(XButtonEvent *);
extern void handleKeyEvent(XKeyEvent *);
struct pixelformat { struct pixelformat {
int bpp; int bpp;
@ -192,8 +201,13 @@ void waitMapped(Display *disp, Window win) {
void handleMessages(void) { void handleMessages(void) {
XEvent event; XEvent event;
while (XCheckMaskEvent(disp, EnterWindowMask | LeaveWindowMask | StructureNotifyMask, &event)) { while (XPending(disp) > 0) {
XNextEvent(disp, &event);
switch (event.type) { switch (event.type) {
case ClientMessage:
if ((event.xclient.format == 32) && (event.xclient.data.l[0] == delete_atom))
saved_env->SetStaticBooleanField(saved_clazz, fid_close, JNI_TRUE);
break;
case EnterNotify: case EnterNotify:
current_focused = true; current_focused = true;
break; break;
@ -206,6 +220,19 @@ void handleMessages(void) {
case UnmapNotify: case UnmapNotify:
current_minimized = true; current_minimized = true;
break; break;
case ButtonPress:
handleButtonPress(&(event.xbutton));
break;
case ButtonRelease:
handleButtonRelease(&(event.xbutton));
break;
case MotionNotify:
handlePointerMotion(&(event.xmotion));
break;
case KeyPress:
case KeyRelease:
handleKeyEvent(&(event.xkey));
break;
} }
} }
} }
@ -249,12 +276,12 @@ bool isFullscreen(void) {
return current_fullscreen; return current_fullscreen;
} }
bool isFocused(void) { bool isFocused() {
handleMessages(); handleMessages();
return current_focused; return current_focused;
} }
bool isMinimized(void) { bool isMinimized() {
handleMessages(); handleMessages();
return current_minimized; return current_minimized;
} }
@ -281,6 +308,10 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_isMinimized(JNIEnv *env, jclas
} }
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jint alpha_bits, jint depth_bits, jint stencil_bits, jboolean fullscreen, jstring title) { JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jint alpha_bits, jint depth_bits, jint stencil_bits, jboolean fullscreen, jstring title) {
saved_env = env;
saved_clazz = clazz;
fid_close = env->GetStaticFieldID(clazz, "closeRequested", "Z");
Window root_win; Window root_win;
XSetWindowAttributes attribs; XSetWindowAttributes attribs;
Colormap cmap; Colormap cmap;
@ -350,6 +381,8 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate(JNIEnv * env, jclass c
size_hints->max_height = height; size_hints->max_height = height;
XSetWMNormalHints(disp, win, size_hints); XSetWMNormalHints(disp, win, size_hints);
XFree(size_hints); XFree(size_hints);
delete_atom = XInternAtom(disp, "WM_DELETE_WINDOW", False);
XSetWMProtocols(disp, win, &delete_atom, 1);
XMapRaised(disp, win); XMapRaised(disp, win);
waitMapped(disp, win); waitMapped(disp, win);
if (fullscreen) { if (fullscreen) {

View File

@ -48,7 +48,7 @@
#define KEYBOARD_BUFFER_SIZE 50 #define KEYBOARD_BUFFER_SIZE 50
#define KEYBOARD_SIZE 256 #define KEYBOARD_SIZE 256
#define KEY_EVENT_BACKLOG 20 #define KEY_EVENT_BACKLOG 40
static unsigned char readBuffer[KEYBOARD_BUFFER_SIZE * 2]; static unsigned char readBuffer[KEYBOARD_BUFFER_SIZE * 2];
static jfieldID fid_readBuffer; static jfieldID fid_readBuffer;
@ -69,6 +69,7 @@ extern Window win;
extern bool isFullscreen(void); extern bool isFullscreen(void);
extern bool isFocused(void); extern bool isFocused(void);
extern void handleMessages(void);
/* /*
* Class: org_lwjgl_input_Keyboard * Class: org_lwjgl_input_Keyboard
@ -100,7 +101,7 @@ void ungrabKeyboard(void) {
XUngrabKeyboard(disp, CurrentTime); XUngrabKeyboard(disp, CurrentTime);
} }
int updateKeyboardGrab(void) { int updateKeyboardGrab() {
if (isFullscreen()) { if (isFullscreen()) {
if (!keyboard_grabbed) if (!keyboard_grabbed)
return grabKeyboard(); return grabKeyboard();
@ -233,6 +234,14 @@ unsigned char eventState(XKeyEvent *event) {
assert(0); assert(0);
} }
void handleKeyEvent(XKeyEvent *event) {
unsigned char keycode = getKeycode(event);
unsigned char state = eventState(event);
key_buf[keycode] = state;
if (buffer_enabled)
putEventElement(event);
}
/* /*
* Class: org_lwjgl_input_Keyboard * Class: org_lwjgl_input_Keyboard
* Method: nPoll * Method: nPoll
@ -245,14 +254,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nPoll
unsigned char state; unsigned char state;
updateKeyboardGrab(); updateKeyboardGrab();
handleMessages();
while (XCheckMaskEvent(disp, KeyPressMask | KeyReleaseMask, &event)) {
unsigned char keycode = getKeycode(&(event.xkey));
state = eventState(&(event.xkey));
key_buf[keycode] = state;
if (buffer_enabled)
putEventElement(&(event.xkey));
}
memcpy((unsigned char*)buf, key_buf, KEYBOARD_SIZE*sizeof(unsigned char)); memcpy((unsigned char*)buf, key_buf, KEYBOARD_SIZE*sizeof(unsigned char));
} }
@ -271,6 +274,7 @@ JNIEXPORT int JNICALL Java_org_lwjgl_input_Keyboard_nRead
int num_events = 0; int num_events = 0;
updateKeyboardGrab(); updateKeyboardGrab();
handleMessages();
while (buf_count < KEYBOARD_BUFFER_SIZE * 2 && (key_event = nextEventElement()) != NULL) { while (buf_count < KEYBOARD_BUFFER_SIZE * 2 && (key_event = nextEventElement()) != NULL) {
num_events++; num_events++;
@ -283,17 +287,6 @@ JNIEXPORT int JNICALL Java_org_lwjgl_input_Keyboard_nRead
num_events += translateEvent(&buf_count, key_event); num_events += translateEvent(&buf_count, key_event);
} }
while (buf_count < KEYBOARD_BUFFER_SIZE * 2 && XCheckMaskEvent(disp, KeyPressMask | KeyReleaseMask, &event)) {
num_events++;
unsigned char keycode = getKeycode(&(event.xkey));
unsigned char state = eventState(&(event.xkey));
// printf("Reading a key %d %d count %d\n", (int)keycode, (int)state, num_events);
key_buf[keycode] = state;
readBuffer[buf_count++] = keycode;
readBuffer[buf_count++] = state;
if (translation_enabled)
num_events += translateEvent(&buf_count, &(event.xkey));
}
return num_events; return num_events;
} }

View File

@ -59,6 +59,7 @@ extern bool isFullscreen(void);
extern bool isFocused(void); extern bool isFocused(void);
extern int getWindowWidth(void); extern int getWindowWidth(void);
extern int getWindowHeight(void); extern int getWindowHeight(void);
extern void handleMessages(void);
static bool pointer_grabbed; static bool pointer_grabbed;
@ -140,7 +141,7 @@ void ungrabPointer(void) {
XUngrabPointer(disp, CurrentTime); XUngrabPointer(disp, CurrentTime);
} }
int updatePointerGrab(void) { int updatePointerGrab(JNIEnv *env, jclass clazz) {
if (isFullscreen()) { if (isFullscreen()) {
if (!pointer_grabbed) if (!pointer_grabbed)
return grabPointer(); return grabPointer();
@ -179,7 +180,7 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Mouse_nCreate
#endif #endif
return JNI_FALSE; return JNI_FALSE;
} }
if (updatePointerGrab() != GrabSuccess) { if (updatePointerGrab(env, clazz) != GrabSuccess) {
#ifdef _DEBUG #ifdef _DEBUG
printf("Could not grab pointer\n"); printf("Could not grab pointer\n");
#endif #endif
@ -201,15 +202,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nDestroy
XFreeCursor(disp, blank_cursor); XFreeCursor(disp, blank_cursor);
} }
int checkPointer() { void handleButtonPress(XButtonEvent *event) {
XEvent event; switch (event->button) {
int count = 0;
updatePointerGrab();
while (XCheckMaskEvent(disp, ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &event)) {
count++;
switch (event.type) {
case ButtonPress:
switch (event.xbutton.button) {
case Button1: case Button1:
buttons[0] = JNI_TRUE; buttons[0] = JNI_TRUE;
break; break;
@ -227,9 +221,10 @@ int checkPointer() {
break; break;
default: assert(0); default: assert(0);
} }
break; }
case ButtonRelease:
switch (event.xbutton.button) { void handleButtonRelease(XButtonEvent *event) {
switch (event->button) {
case Button1: case Button1:
buttons[0] = JNI_FALSE; buttons[0] = JNI_FALSE;
break; break;
@ -244,15 +239,11 @@ int checkPointer() {
break; break;
default: assert(0); default: assert(0);
} }
break;
case MotionNotify:
current_x = event.xmotion.x;
current_y = event.xmotion.y;
break;
default: assert(0);
} }
}
return count; void handlePointerMotion(XMotionEvent *event) {
current_x = event->x;
current_y = event->y;
} }
void warpPointer(void) { void warpPointer(void) {
@ -291,7 +282,8 @@ void warpPointer(void) {
JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll JNIEXPORT void JNICALL Java_org_lwjgl_input_Mouse_nPoll
(JNIEnv * env, jclass clazz) (JNIEnv * env, jclass clazz)
{ {
checkPointer(); updatePointerGrab(env, clazz);
handleMessages();
int moved_x = current_x - last_x; int moved_x = current_x - last_x;
int moved_y = current_y - last_y; int moved_y = current_y - last_y;
int moved_z = current_z - last_z; int moved_z = current_z - last_z;

View File

@ -42,11 +42,13 @@
#include "extgl.h" #include "extgl.h"
#include "org_lwjgl_opengl_BaseGL.h" #include "org_lwjgl_opengl_BaseGL.h"
GLXContext context = NULL; // OpenGL rendering context static GLXContext context = NULL; // OpenGL rendering context
extern XVisualInfo * getVisualInfo(void); extern XVisualInfo * getVisualInfo(void);
extern Window win; extern Window win;
extern Display * disp; extern Display * disp;
extern void handleMessages();
void makeCurrent(void) { void makeCurrent(void) {
glXMakeCurrent(disp, win, context); glXMakeCurrent(disp, win, context);
} }
@ -113,6 +115,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_nDestroy
*/ */
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_swapBuffers(JNIEnv * env, jobject obj) JNIEXPORT void JNICALL Java_org_lwjgl_opengl_BaseGL_swapBuffers(JNIEnv * env, jobject obj)
{ {
handleMessages();
glXSwapBuffers(disp, win); glXSwapBuffers(disp, win);
} }