summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/Src/ControllerInterface/Xlib/Xlib.cpp
blob: 1c9e9997ad9b541a633e83fc38b84864ad23920c (plain)
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
#include "Xlib.h"

namespace ciface
{
namespace Xlib
{

void Init(std::vector<ControllerInterface::Device*>& devices, void* const hwnd)
{
	// mouse will be added to this, Keyboard class will be turned into KeyboardMouse
	// single device for combined keyboard/mouse, this will allow combinations like shift+click more easily
	devices.push_back(new Keyboard((Display*)hwnd));
}

Keyboard::Keyboard(Display* display) : m_display(display)
{
	memset(&m_state, 0, sizeof(m_state));

	m_window = DefaultRootWindow(m_display);

	int min_keycode, max_keycode;
	XDisplayKeycodes(m_display, &min_keycode, &max_keycode);
	
	// Keyboard Keys
	for (int i = min_keycode; i <= max_keycode; ++i)
	{
		Key *temp_key = new Key(m_display, i);
		if (temp_key->m_keyname.length())
			inputs.push_back(temp_key);
		else
			delete temp_key;
	}

	// Mouse Buttons
	inputs.push_back(new Button(Button1Mask));
	inputs.push_back(new Button(Button2Mask));
	inputs.push_back(new Button(Button3Mask));
	inputs.push_back(new Button(Button4Mask));
	inputs.push_back(new Button(Button5Mask));
}

Keyboard::~Keyboard()
{
}


ControlState Keyboard::GetInputState(const ControllerInterface::Device::Input* const input)
{
	return ((Input*)input)->GetState(&m_state);
}

void Keyboard::SetOutputState(const ControllerInterface::Device::Output* const output, const ControlState state)
{

}

bool Keyboard::UpdateInput()
{
	XQueryKeymap(m_display, m_state.keyboard);

	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);

	return true;
}

bool Keyboard::UpdateOutput()
{
	return true;
}


std::string Keyboard::GetName() const
{
	return "Keyboard";
	//return "Keyboard Mouse";	// change to this later
}

std::string Keyboard::GetSource() const
{
	return "Xlib";
}

int Keyboard::GetId() const
{
	return 0;
}


Keyboard::Key::Key(Display* const display, KeyCode keycode)
  : m_display(display), m_keycode(keycode)
{
	int i = 0;
	KeySym keysym = 0;
	do
	{
		keysym = XKeycodeToKeysym(m_display, keycode, i);
		i++;
	}
	while (keysym == NoSymbol && i < 8);

	// 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)
		m_keyname = std::string();
	else
		m_keyname = std::string(XKeysymToString(keysym));
}

ControlState Keyboard::Key::GetState(const State* const state)
{
	return (state->keyboard[m_keycode/8] & (1 << (m_keycode%8))) != 0;
}

ControlState Keyboard::Button::GetState(const State* const state)
{
	return ((state->buttons & m_index) > 0);
}

std::string Keyboard::Key::GetName() const
{
	return m_keyname;
}

std::string Keyboard::Button::GetName() const
{
	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;
	}

	return std::string("Button ") + button;
}

}
}