1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /**
34  * $Id: org_lwjgl_input_Keyboard.c 2399 2006-06-30 19:28:00Z elias_naur $
35  *
36  * Linux mouse handling.
37  *
38  * @author elias_naur <elias_naur@users.sourceforge.net>
39  * @version $Revision: 2399 $
40  */
41 
42 #include <X11/X.h>
43 #include <X11/Xlib.h>
44 #include <X11/Xutil.h>
45 #include "common_tools.h"
46 #include "org_lwjgl_opengl_LinuxMouse.h"
47 
getWindowAttributes(jlong display_ptr,jlong window_ptr,XWindowAttributes * attr)48 static void getWindowAttributes(jlong display_ptr, jlong window_ptr, XWindowAttributes *attr) {
49 	Display *disp = (Display *)(intptr_t)display_ptr;
50 	Window win = (Window)window_ptr;
51 	XGetWindowAttributes(disp, win, attr);
52 }
53 
Java_org_lwjgl_opengl_LinuxMouse_nGetWindowHeight(JNIEnv * env,jclass unused,jlong display_ptr,jlong window_ptr)54 JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxMouse_nGetWindowHeight(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr) {
55 	XWindowAttributes window_attributes;
56 	getWindowAttributes(display_ptr, window_ptr, &window_attributes);
57 	return window_attributes.height;
58 }
59 
Java_org_lwjgl_opengl_LinuxMouse_nGetWindowWidth(JNIEnv * env,jclass unused,jlong display_ptr,jlong window_ptr)60 JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxMouse_nGetWindowWidth(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr) {
61 	XWindowAttributes window_attributes;
62 	getWindowAttributes(display_ptr, window_ptr, &window_attributes);
63 	return window_attributes.width;
64 }
65 
Java_org_lwjgl_opengl_LinuxMouse_nWarpCursor(JNIEnv * env,jclass unused,jlong display_ptr,jlong window_ptr,jint x,jint y)66 JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxMouse_nWarpCursor(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr, jint x, jint y) {
67 	Display *disp = (Display *)(intptr_t)display_ptr;
68 	Window win = (Window)window_ptr;
69 	XWarpPointer(disp, None, win, 0, 0, 0, 0, x, y);
70 }
71 
Java_org_lwjgl_opengl_LinuxMouse_nQueryPointer(JNIEnv * env,jclass unused,jlong display_ptr,jlong window_ptr,jobject result_buffer)72 JNIEXPORT jlong JNICALL Java_org_lwjgl_opengl_LinuxMouse_nQueryPointer(JNIEnv *env, jclass unused, jlong display_ptr, jlong window_ptr, jobject result_buffer) {
73 	Display *disp = (Display *)(intptr_t)display_ptr;
74 	Window win = (Window)window_ptr;
75 	Window root_return, child_return;
76 	int root_x, root_y, win_x, win_y;
77 	unsigned int mask_return;
78 	jint *result = (jint *)(*env)->GetDirectBufferAddress(env, result_buffer);
79 	int result_size = (*env)->GetDirectBufferCapacity(env, result_buffer);
80 	if (result_size < 4) {
81 		throwFormattedException(env, "Not enough space in result buffer (%d)", result_size);
82 		return (intptr_t)NULL;
83 	}
84 
85 	XQueryPointer(disp, win, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return);
86 	result[0] = root_x;
87 	result[1] = root_y;
88 	result[2] = win_x;
89 	result[3] = win_y;
90 	return root_return;
91 }
92 
Java_org_lwjgl_opengl_LinuxMouse_nSendWarpEvent(JNIEnv * env,jclass unusued,jlong display_ptr,jlong window_ptr,jlong warp_atom_ptr,jint x,jint y)93 JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxMouse_nSendWarpEvent(JNIEnv *env, jclass unusued, jlong display_ptr, jlong window_ptr, jlong warp_atom_ptr, jint x, jint y) {
94 	Atom warp_atom = (Atom)warp_atom_ptr;
95 	Display *disp = (Display *)(intptr_t)display_ptr;
96 	Window win = (Window)window_ptr;
97 	XEvent warp_event;
98 	warp_event.type = ClientMessage;
99 	warp_event.xclient.window = win;
100 	warp_event.xclient.message_type = warp_atom;
101 	warp_event.xclient.format = 32;
102 	warp_event.xclient.data.l[0] = x;
103 	warp_event.xclient.data.l[1] = y;
104 	XSendEvent(disp, win, False, 0, &warp_event);
105 }
106 
Java_org_lwjgl_opengl_LinuxMouse_nGetButtonCount(JNIEnv * env,jclass unused,jlong display_ptr)107 JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxMouse_nGetButtonCount(JNIEnv *env, jclass unused, jlong display_ptr) {
108 	Display *disp = (Display *)(intptr_t)display_ptr;
109 
110 	int count = 256;
111 
112 	unsigned char * pointer_map = malloc(sizeof(unsigned char) * count);
113 	count = XGetPointerMapping(disp, pointer_map, count);
114 
115 	free(pointer_map);
116 
117 	return count;
118 }
119