File: mouse_x11.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (412 lines) | stat: -rw-r--r-- 7,980 bytes parent folder | download
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
175
176
177
178
179
180
181
182
183
184
185
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
	$Id: mouse_x11.cpp,v 1.7 2001/12/22 17:03:35 sphair Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#include "Core/precomp.h"

#ifdef USE_X11

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "API/Core/System/cl_assert.h"

#include "mouse_x11.h"

#include <API/Display/Input/input.h>
#include <API/Display/Input/mouse.h>
#include <API/Display/Input/inputaxis.h>
#include <API/Display/Input/inputbutton.h>
#include <API/Display/Input/inputcursor.h>
#include <API/Display/Input/key.h>
#include <API/Display/Input/inputhat.h>
#include <Display/Input/X11/mouse_x11.h>
#include <Display/Display/X11/display_xwindow.h>

#define NUM_MOUSE_BUTTONS 3

/*************************************
  CL_Mouse_XWin
*************************************/

CL_Mouse_XWin::CL_Mouse_XWin(CL_XWindow_CompatibleCard *_card)
{
	card = _card;

	cursor = new CL_InputCursor_Mouse_XWin(card);
	axes = new CL_InputAxis_Mouse_XWin[2]; 

	buttons = new CL_InputButton_Mouse_XWin*[NUM_MOUSE_BUTTONS];
	for (int i=0; i<NUM_MOUSE_BUTTONS; i++)
		buttons[i] = new CL_InputButton_Mouse_XWin();
	have_focus = false;
}

CL_Mouse_XWin::~CL_Mouse_XWin()
{
	delete cursor;

	for (int i=0; i<NUM_MOUSE_BUTTONS; i++) delete buttons[i];
	delete[] buttons;
}

CL_InputDevice::InputDeviceType CL_Mouse_XWin::get_type() const
{
	return CL_InputDevice::type_mouse;
}

int CL_Mouse_XWin::get_num_buttons() const
{
	return 3;
}

CL_InputButton *CL_Mouse_XWin::get_button(int button_num)
{
	if (button_num < 0 || button_num >= NUM_MOUSE_BUTTONS) return NULL;

	return buttons[button_num];
}

int CL_Mouse_XWin::get_num_axes() const
{
	return 2;
}

CL_InputAxis *CL_Mouse_XWin::get_axis(int axis_num)
{
	cl_assert(((unsigned int) axis_num)<2);
	return &axes[axis_num];
}

int CL_Mouse_XWin::get_num_hats() const
{
	return 0;
}

CL_InputHat *CL_Mouse_XWin::get_hat(int /*hat_num*/)
{
	return NULL;
}

int CL_Mouse_XWin::get_num_cursors() const
{
	return 1;
}

void CL_Mouse_XWin::keep_alive()
{
	if (card->is_initialized() == false) return;

	CL_Key key;
	Window child;
	Window root;
	int root_x;
	int root_y;
	int win_x;
	int win_y;
	unsigned int mask;

	axes[0].center = (card->get_width())/2.0;
	axes[1].center = (card->get_height())/2.0;

	// Check if window has focus right now:
	Window focus_win;
	int focus_state;
	XGetInputFocus(
		card->get_display(),
		&focus_win,
		&focus_state);

	XQueryPointer(card->get_display(),
		      card->get_window(),
		      &root,
		      &child,
		      &root_x,
		      &root_y,
		      &win_x,
		      &win_y,
		      &mask);

	// Cutting the mouse position to the effective window size
	if (win_x < 0) win_x = 0;
	if (win_y < 0) win_y = 0;
	
	if (win_x >= cursor->get_max_x()) win_x = (int)cursor->get_max_x() - 1;
	if (win_y >= cursor->get_max_y()) win_y = (int)cursor->get_max_y() - 1;

	// get_window() returns NULL if in DGA mode.	
	if (card->get_window() == 0 || card->get_window() == focus_win)
	{
		axes[0].pos = cursor->x;
		axes[1].pos = cursor->y;
		
		if (cursor->x != win_x || cursor->y != win_y) 
		{
			//the mouse is on our display:
			cursor->x = win_x;
			cursor->y = win_y;
			
			CL_Input::sig_mouse_move(this, (int)cursor->x, (int)cursor->y);
			CL_Mouse::sig_move((int)cursor->x, (int)cursor->y);
		}
		
		// Handle the key events
		if (mask & 0x100)
		{
			if (buttons[0]->button_state == false)
			{
				buttons[0]->button_state = true;
				CL_Input::sig_button_press(
					this,
					CL_Key(
						CL_MOUSE_LEFTBUTTON,
						CL_Key::Pressed,
						-1,
						cursor->x,
						cursor->y));
			}
		}
		else
		{
			if (buttons[0]->button_state == true)
			{
				buttons[0]->button_state = false;
				CL_Input::sig_button_release(
					this,
					CL_Key(
						CL_MOUSE_LEFTBUTTON,
						CL_Key::Released,
						-1,
						cursor->x,
						cursor->y));
			}
		}
		
		if (mask & 0x200)
		{
			if (buttons[1]->button_state == false)
			{
				buttons[1]->button_state = true;
				CL_Input::sig_button_press(
					this,
					CL_Key(
						CL_MOUSE_MIDDLEBUTTON,
						CL_Key::Pressed,
						-1,
						cursor->x,
						cursor->y));
			}
		}
		else
		{
			if (buttons[1]->button_state == true)
			{
				buttons[1]->button_state = false;
				CL_Input::sig_button_release(
					this,
					CL_Key(
						CL_MOUSE_MIDDLEBUTTON,
						CL_Key::Released,
						-1,
						cursor->x,
						cursor->y));
			}
		}

		if(mask & 0x400) 
		{
			if (buttons[2]->button_state == false)
			{
				buttons[2]->button_state = true;
				CL_Input::sig_button_press(
					this,
					CL_Key(
						CL_MOUSE_RIGHTBUTTON,
						CL_Key::Pressed,
						-1,
						cursor->x,
						cursor->y));
			}
		}
		else
		{
			if (buttons[2]->button_state == true)
			{
				buttons[2]->button_state = false;
				CL_Input::sig_button_release(
					this,
					CL_Key(
						CL_MOUSE_RIGHTBUTTON,
						CL_Key::Released,
						-1,
						cursor->x,
						cursor->y));
			}
		}

		if (!have_focus)
		{
			have_focus = true;
//			CL_Input::sig_mouse_enter(this);
		}
	}
	else // we don't have window focus. keep last coordinates and release buttons
	{
		XQueryPointer(card->get_display(),
			      card->get_window(),
			      &root,
			      &child,
			      &root_x,
			      &root_y,
			      &win_x,
			      &win_y,
			      &mask);

		cursor->x = win_x;
		cursor->y = win_y;
	
		if (have_focus)
		{
			have_focus = false;
//			CL_Input::sig_mouse_exit(this);
		}
		
		// Handle the key events
		key.state = CL_Key::Released;     

		if (buttons[0]->button_state == true) 
		{
			key.id = CL_MOUSE_LEFTBUTTON;
			buttons[0]->button_state = false;
			CL_Input::sig_button_release(this, key);
		}
	  
		if (buttons[1]->button_state == true)
		{
			key.id = CL_MOUSE_MIDDLEBUTTON;
			buttons[1]->button_state = false;
			CL_Input::sig_button_release(this, key);
		}

		if (buttons[2]->button_state == true)
		{
			key.id = CL_MOUSE_RIGHTBUTTON;
			buttons[2]->button_state = false;
			CL_Input::sig_button_release(this, key);
		}	  
	}
	
	if (card->is_fullscreen())
	{
		int w = card->get_win_width();
		int h = card->get_win_height();

		cursor->x -= (w - card->get_width())/2;
		cursor->y -= (h - card->get_height())/2;

		if (cursor->x < 0)
			cursor->x = 0;
		if (cursor->x > card->get_width())
			cursor->x = card->get_width();
		if (cursor->y < 0)
			cursor->y = 0;
		if (cursor->y > card->get_height())
			cursor->y = card->get_height();
	}
}

CL_InputCursor *CL_Mouse_XWin::get_cursor(int /*cursor_num*/)
{
	return cursor;
}

/*******************************
  CL_InputButton_Mouse_XWin
*******************************/

CL_InputButton_Mouse_XWin::CL_InputButton_Mouse_XWin()
{
	button_state = false;
}

CL_InputButton_Mouse_XWin::~CL_InputButton_Mouse_XWin()
{
}

bool CL_InputButton_Mouse_XWin::is_pressed()
{
	return button_state;
}

/*******************************
  CL_InputCursor_Mouse_XWin
*******************************/

CL_InputCursor_Mouse_XWin::CL_InputCursor_Mouse_XWin(
	CL_XWindow_CompatibleCard *_card)
{
	card = _card;
	x = 0;
	y = 0;
}

CL_InputCursor_Mouse_XWin::~CL_InputCursor_Mouse_XWin()
{
}


float CL_InputCursor_Mouse_XWin::get_x()
{
	return x;
}

float CL_InputCursor_Mouse_XWin::get_y()
{
	return y;
}

float CL_InputCursor_Mouse_XWin::get_max_x()
{
	return card->get_width();
}

float CL_InputCursor_Mouse_XWin::get_max_y()
{
	return card->get_height();
}

/*******************************
  CL_InputAxis_Mouse_XWin
*******************************/

CL_InputAxis_Mouse_XWin::CL_InputAxis_Mouse_XWin()
{
	center = 1;
	pos = 0;
}

CL_InputAxis_Mouse_XWin::~CL_InputAxis_Mouse_XWin()
{
}


float CL_InputAxis_Mouse_XWin::get_pos()
{
	return (pos - center) / center;
}

#endif