| 
									
										
										
										
											2012-12-10 00:40:28 -06:00
										 |  |  | #include <X11/XKBlib.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-19 02:17:31 +01:00
										 |  |  | #include "InputCommon/ControllerInterface/Xlib/Xlib.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | namespace ciface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | namespace Xlib | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | void Init(std::vector<Core::Device*>& devices, void* const hwnd) | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-16 14:14:57 +00:00
										 |  |  | 	devices.push_back(new KeyboardMouse((Window)hwnd)); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | KeyboardMouse::KeyboardMouse(Window window) : m_window(window) | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 	memset(&m_state, 0, sizeof(m_state)); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	m_display = XOpenDisplay(nullptr); | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 	int min_keycode, max_keycode; | 
					
						
							|  |  |  | 	XDisplayKeycodes(m_display, &min_keycode, &max_keycode); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | 	// Keyboard Keys
 | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 	for (int i = min_keycode; i <= max_keycode; ++i) | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 		Key *temp_key = new Key(m_display, i, m_state.keyboard); | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 		if (temp_key->m_keyname.length()) | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 			AddInput(temp_key); | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 		else | 
					
						
							|  |  |  | 			delete temp_key; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Mouse Buttons
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 	AddInput(new Button(Button1Mask, m_state.buttons)); | 
					
						
							|  |  |  | 	AddInput(new Button(Button2Mask, m_state.buttons)); | 
					
						
							|  |  |  | 	AddInput(new Button(Button3Mask, m_state.buttons)); | 
					
						
							|  |  |  | 	AddInput(new Button(Button4Mask, m_state.buttons)); | 
					
						
							|  |  |  | 	AddInput(new Button(Button5Mask, m_state.buttons)); | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Mouse Cursor, X-/+ and Y-/+
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 	for (int i = 0; i != 4; ++i) | 
					
						
							|  |  |  | 		AddInput(new Cursor(!!(i & 2), !!(i & 1), (&m_state.cursor.x)[!!(i & 2)])); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | KeyboardMouse::~KeyboardMouse() | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | 	XCloseDisplay(m_display); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | bool KeyboardMouse::UpdateInput() | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 	XQueryKeymap(m_display, m_state.keyboard); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | 	int root_x, root_y, win_x, win_y; | 
					
						
							|  |  |  | 	Window root, child; | 
					
						
							|  |  |  | 	XQueryPointer(m_display, m_window, &root, &child, &root_x, &root_y, &win_x, &win_y, &m_state.buttons); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | 	// update mouse cursor
 | 
					
						
							|  |  |  | 	XWindowAttributes win_attribs; | 
					
						
							|  |  |  | 	XGetWindowAttributes(m_display, m_window, &win_attribs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// the mouse position as a range from -1 to 1
 | 
					
						
							|  |  |  | 	m_state.cursor.x = (float)win_x / (float)win_attribs.width * 2 - 1; | 
					
						
							|  |  |  | 	m_state.cursor.y = (float)win_y / (float)win_attribs.height * 2 - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | bool KeyboardMouse::UpdateOutput() | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | std::string KeyboardMouse::GetName() const | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | 	return "Keyboard Mouse"; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | std::string KeyboardMouse::GetSource() const | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return "Xlib"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | int KeyboardMouse::GetId() const | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | KeyboardMouse::Key::Key(Display* const display, KeyCode keycode, const char* keyboard) | 
					
						
							|  |  |  | 	: m_display(display), m_keyboard(keyboard), m_keycode(keycode) | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 	KeySym keysym = 0; | 
					
						
							|  |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2012-12-10 00:40:28 -06:00
										 |  |  | 		keysym = XkbKeycodeToKeysym(m_display, keycode, i, 0); | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 		i++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	while (keysym == NoSymbol && i < 8); | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-12 14:05:12 +00:00
										 |  |  | 	// Convert to upper case for the keyname
 | 
					
						
							|  |  |  | 	if (keysym >= 97 && keysym <= 122) | 
					
						
							| 
									
										
										
										
											2010-06-12 20:33:29 +00:00
										 |  |  | 		keysym -= 32; | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 07:55:35 +00:00
										 |  |  | 	// 0x0110ffff is the top of the unicode character range according
 | 
					
						
							|  |  |  | 	// to keysymdef.h although it is probably more than we need.
 | 
					
						
							|  |  |  | 	if (keysym == NoSymbol || keysym > 0x0110ffff || | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		XKeysymToString(keysym) == nullptr) | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 		m_keyname = std::string(); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		m_keyname = std::string(XKeysymToString(keysym)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | ControlState KeyboardMouse::Key::GetState() const | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-09 20:26:11 -06:00
										 |  |  | 	return (m_keyboard[m_keycode / 8] & (1 << (m_keycode % 8))) != 0; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | ControlState KeyboardMouse::Button::GetState() const | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 	return ((m_buttons & m_index) != 0); | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | ControlState KeyboardMouse::Cursor::GetState() const | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 	return std::max(0.0f, m_cursor / (m_positive ? 1.0f : -1.0f)); | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::Key::GetName() const | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | 	return m_keyname; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | std::string KeyboardMouse::Cursor::GetName() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static char tmpstr[] = "Cursor .."; | 
					
						
							|  |  |  | 	tmpstr[7] = (char)('X' + m_index); | 
					
						
							|  |  |  | 	tmpstr[8] = (m_positive ? '+' : '-'); | 
					
						
							|  |  |  | 	return tmpstr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string KeyboardMouse::Button::GetName() const | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char button = '0'; | 
					
						
							|  |  |  | 	switch (m_index) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		case Button1Mask: button = '1'; break; | 
					
						
							|  |  |  | 		case Button2Mask: button = '2'; break; | 
					
						
							|  |  |  | 		case Button3Mask: button = '3'; break; | 
					
						
							|  |  |  | 		case Button4Mask: button = '4'; break; | 
					
						
							|  |  |  | 		case Button5Mask: button = '5'; break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-19 23:11:22 +00:00
										 |  |  | 	static char tmpstr[] = "Click ."; | 
					
						
							| 
									
										
										
										
											2010-11-16 04:34:52 +00:00
										 |  |  | 	tmpstr[6] = button; | 
					
						
							| 
									
										
										
										
											2010-07-16 03:43:11 +00:00
										 |  |  | 	return tmpstr; | 
					
						
							| 
									
										
										
										
											2010-04-23 04:46:42 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | } |