LinuxMouse.cpp 7.53 KB
Newer Older
Phillip Castaneda's avatar
Phillip Castaneda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
The zlib/libpng License

Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)

This software is provided 'as-is', without any express or implied warranty. In no event will
the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial 
applications, and to alter it and redistribute it freely, subject to the following
restrictions:

    1. The origin of this software must not be misrepresented; you must not claim that 
		you wrote the original software. If you use this software in a product, 
		an acknowledgment in the product documentation would be appreciated but is 
		not required.

    2. Altered source versions must be plainly marked as such, and must not be 
		misrepresented as being the original software.

    3. This notice may not be removed or altered friosom any source distribution.
*/
#include "linux/LinuxMouse.h"
#include "linux/LinuxInputManager.h"
#include "OISException.h"
#include "OISEvents.h"

using namespace OIS;

//-------------------------------------------------------------------//
LinuxMouse::LinuxMouse(InputManager* creator, bool buffered, bool grab, bool hide)
	: Mouse(creator->inputSystemName(), buffered, 0, creator)
{
	display = 0;
	window = 0;
	cursor = 0;

	grabMouse = grab;
	hideMouse = hide;

	static_cast<LinuxInputManager*>(mCreator)->_setMouseUsed(true);
}

//-------------------------------------------------------------------//
void LinuxMouse::_initialize()
{
	//Clear old state
	mState.clear();
	mMoved  = false;
	mWarped = false;

	//6 is just some random value... hardly ever would anyone have a window smaller than 6
	oldXMouseX = oldXMouseY = 6;
	oldXMouseZ = 0;

	if( display ) XCloseDisplay(display);
	display = 0;
	window = static_cast<LinuxInputManager*>(mCreator)->_getWindow();

	//Create our local X mListener connection
	if( !(display = XOpenDisplay(0)) )
		OIS_EXCEPT(E_General, "LinuxMouse::_initialize >> Error opening X!");

	//Set it to recieve Mouse Input events
	if( XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask) == BadWindow )
		OIS_EXCEPT(E_General, "LinuxMouse::_initialize >> X error!");

	//Warp mouse inside window
	XWarpPointer(display,None,window,0,0,0,0, 6,6);

	//Create a blank cursor:
	Pixmap bm_no;
	XColor black, dummy;
	Colormap colormap;
	static char no_data[] = { 0,0,0,0,0,0,0,0 };

	colormap = DefaultColormap( display, DefaultScreen(display) );
	XAllocNamedColor( display, colormap, "black", &black, &dummy );
	bm_no = XCreateBitmapFromData( display, window, no_data, 8, 8 );
	cursor = XCreatePixmapCursor( display, bm_no, bm_no, &black, &black, 0, 0 );

	grab( grabMouse );
	hide( hideMouse );

	mouseFocusLost = false;
}

//-------------------------------------------------------------------//
LinuxMouse::~LinuxMouse()
{
	if( display )
	{
		grab(false);
		hide(false);
		XFreeCursor(display, cursor);
		XCloseDisplay(display);
	}

	static_cast<LinuxInputManager*>(mCreator)->_setMouseUsed(false);
}

//-------------------------------------------------------------------//
void LinuxMouse::setBuffered(bool buffered)
{
	mBuffered = buffered;
}

//-------------------------------------------------------------------//
void LinuxMouse::capture()
{
	//Clear out last frames values
	mState.X.rel = 0;
	mState.Y.rel = 0;
	mState.Z.rel = 0;

	_processXEvents();

	mWarped = false;

	if( mMoved == true )
	{
		if( mBuffered && mListener )
			mListener->mouseMoved( MouseEvent( this, mState ) );

		mMoved = false;
	}

	//Check for losing/gaining mouse grab focus (alt-tab, etc)
	if( grabMouse )
	{
		if( static_cast<LinuxInputManager*>(mCreator)->_getGrabState() )
		{
			if( mouseFocusLost )	//We just regained mouse grab focus
			{
				grab( true );
				hide( hideMouse );
				mouseFocusLost = false;
			}
		}
		else
		{
			if( mouseFocusLost == false )	//We just lost mouse grab focus
			{
				grab( false );
				hide( false );
				mouseFocusLost = true;
			}
		}
	}
}

//-------------------------------------------------------------------//
void LinuxMouse::_processXEvents()
{
	//X11 Button Events: 1=left 2=middle 3=right; Our Bit Postion: 1=Left 2=Right 3=Middle
	char mask[4] = {0,1,4,2};
	XEvent event;

	//Poll x11 for events mouse events
	while( XPending(display) > 0 ) 
	{
		XNextEvent(display, &event);

		if( event.type == MotionNotify )
		{	//Mouse moved
			//Ignore out of bounds mouse if we just warped
			if( mWarped )
			{
				if(event.xmotion.x < 5 || event.xmotion.x > mState.width - 5 ||
				   event.xmotion.y < 5 || event.xmotion.y > mState.height - 5)
					continue;
			}

			//Compute this frames Relative X & Y motion
Phillip Castaneda's avatar
Phillip Castaneda committed
175
176
			int dx = event.xmotion.x - oldXMouseX;
			int dy = event.xmotion.y - oldXMouseY;
Phillip Castaneda's avatar
Phillip Castaneda committed
177
178
179
180
181
		
			//Store old values for next time to compute relative motion
			oldXMouseX = event.xmotion.x;
			oldXMouseY = event.xmotion.y;

Phillip Castaneda's avatar
Phillip Castaneda committed
182
183
184
185
			mState.X.abs += dx;
			mState.Y.abs += dy;
			mState.X.rel += dx;
			mState.Y.rel += dy;
Phillip Castaneda's avatar
Phillip Castaneda committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

			//Check to see if we are grabbing the mouse to the window (requires clipping and warping)
			if( grabMouse )
			{
				if( mState.X.abs < 0 )
					mState.X.abs = 0;
				else if( mState.X.abs > mState.width )
					mState.X.abs = mState.width;

				if( mState.Y.abs < 0 )
					mState.Y.abs = 0;
				else if( mState.Y.abs > mState.height )
					mState.Y.abs = mState.height;

				if( mouseFocusLost == false )
				{
					//Keep mouse in window (fudge factor)
					if(event.xmotion.x < 5 || event.xmotion.x > mState.width - 5 ||
					   event.xmotion.y < 5 || event.xmotion.y > mState.height - 5 )
					{
						oldXMouseX = mState.width >> 1;  //center x
						oldXMouseY = mState.height >> 1; //center y
						XWarpPointer(display, None, window, 0, 0, 0, 0, oldXMouseX, oldXMouseY);
						mWarped = true;
					}
				}
			}
			mMoved = true;
		}
		else if( event.type == ButtonPress )
		{	//Button down
			static_cast<LinuxInputManager*>(mCreator)->_setGrabState(true);

			if( event.xbutton.button < 4 )
			{
				mState.buttons |= mask[event.xbutton.button];
				if( mBuffered && mListener )
					if( mListener->mousePressed( MouseEvent( this, mState ),
						(MouseButtonID)(mask[event.xbutton.button] >> 1)) == false )
						return;
			}
		}
		else if( event.type == ButtonRelease )
		{	//Button up
			if( event.xbutton.button < 4 )
			{
				mState.buttons &= ~mask[event.xbutton.button];
				if( mBuffered && mListener )
					if( mListener->mouseReleased( MouseEvent( this, mState ),
						(MouseButtonID)(mask[event.xbutton.button] >> 1)) == false )
						return;
			}
			//The Z axis gets pushed/released pair message (this is up)
			else if( event.xbutton.button == 4 )
			{
				mState.Z.rel += 120;
				mState.Z.abs += 120;
				mMoved = true;
			}
			//The Z axis gets pushed/released pair message (this is down)
			else if( event.xbutton.button == 5 )
			{
				mState.Z.rel -= 120;
				mState.Z.abs -= 120;
				mMoved = true;
			}
		}
	}
}

//-------------------------------------------------------------------//
void LinuxMouse::grab(bool grab)
{
	if( grab )
		XGrabPointer(display, window, True, 0, GrabModeAsync, GrabModeAsync, window, None, CurrentTime);
	else
		XUngrabPointer(display, CurrentTime);
}

//-------------------------------------------------------------------//
void LinuxMouse::hide(bool hide)
{
	if( hide )
		XDefineCursor(display, window, cursor);
	else
		XUndefineCursor(display, window);
}