/
SNIIS_Linux_Mouse.cpp
287 lines (261 loc) · 10.4 KB
/
SNIIS_Linux_Mouse.cpp
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
/// @file SNIIS_Linux_Mouse.cpp
/// Linux implementation of mice
#include "SNIIS_Linux.h"
#include "SNIIS_Intern.h"
#if SNIIS_SYSTEM_LINUX
using namespace SNIIS;
// --------------------------------------------------------------------------------------------------------------------
LinuxMouse::LinuxMouse( LinuxInput* pSystem, size_t pId, const XIDeviceInfo& pDeviceInfo)
: Mouse( pId), mSystem( pSystem), mDeviceId( pDeviceInfo.deviceid)
{
mState.buttons = mState.prevButtons = 0;
// enumerate all controls on that device
for( int a = 0; a < pDeviceInfo.num_classes; ++a )
{
auto cl = pDeviceInfo.classes[a];
switch( cl->type )
{
case XIButtonClass:
{
auto bcl = reinterpret_cast<const XIButtonClassInfo*> (cl);
for( int b = 0; b < bcl->num_buttons; ++b )
mButtons.emplace_back( Button{ bcl->labels[b] });
break;
}
case XIValuatorClass:
{
auto vcl = reinterpret_cast<const XIValuatorClassInfo*> (cl);
size_t num = size_t( vcl->number);
// reserve axis 2 for mouse wheel
if( num >= 2 ) ++num;
if( mAxes.size() <= num )
mAxes.resize( num+1);
mAxes[num] = Axis{ vcl->label, vcl->min, vcl->max, 0.0, 0.0, vcl->mode == XIModeAbsolute };
}
}
}
// insert dummy mouse wheel axis
if( mAxes.size() < 3 )
mAxes.emplace_back();
mAxes[2] = Axis{ 0, 0, 256, 0.0, 0.0f, false };
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::StartUpdate()
{
mState.prevButtons = mState.buttons;
for( auto& a : mAxes )
a.prevValue = a.value;
// wheel axis is relative, shows movements only. So zero out for each frame and start accumulating differences anew
mAxes[2].value = 0.0;
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::HandleEvent( const XIRawEvent& ev)
{
switch( ev.evtype )
{
case XI_RawMotion:
{
size_t numReportedAxes = std::min( mAxes.size(), size_t( ev.valuators.mask_len*8));
const double* values = ev.valuators.values;
static std::vector<double> diffs;
if( diffs.size() < numReportedAxes )
diffs.resize( numReportedAxes);
std::fill( diffs.begin(), diffs.end(), 0.0);
for( size_t a = 0; a < numReportedAxes; ++a )
{
if( XIMaskIsSet( ev.valuators.mask, a) )
{
if( !mIsFirstUpdate )
InputSystemHelper::SortThisMouseToFront( this);
double v = *values++;
if( mAxes[a].isAbsolute )
diffs[a] = v - mAxes[a].value;
else
diffs[a] = v;
}
}
DoMouseMove( diffs.data(), diffs.size());
break;
}
case XI_RawButtonPress:
case XI_RawButtonRelease:
{
if( !mIsFirstUpdate )
InputSystemHelper::SortThisMouseToFront( this);
size_t button = size_t( ev.detail);
bool isPressed = (ev.evtype == XI_RawButtonPress);
// Mouse wheel. There seem to be two of those, we treat them the same
if( button >= 4 && button <= 7 )
{
if( isPressed && !mIsFirstUpdate )
DoMouseWheel( (button&1) == 0 ? 1.0 : -1.0);
} else
{
--button;
// we do "Left, Right, Middle", they do "Left, Middle, Right" - remap that
if( button == 1 ) button = 2; else if( button == 2 ) button = 1;
// announce
if( button < mButtons.size() && !mIsFirstUpdate )
DoMouseButton( button, isPressed);
}
break;
}
}
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::EndUpdate()
{
if( !mIsFirstUpdate )
{
// send the mouse move if we're primary or separate
if( mSystem->IsInMultiDeviceMode() || GetCount() == 0 )
{
if( mAxes[0].prevValue != mAxes[0].value || mAxes[1].prevValue != mAxes[1].value )
InputSystemHelper::DoMouseMove( this, mAxes[0].value, mAxes[1].value, mAxes[0].value - mAxes[0].prevValue, mAxes[1].value - mAxes[1].prevValue);
// send the wheel
if( mAxes[2].prevValue != mAxes[2].value )
InputSystemHelper::DoMouseWheel( this, float( mAxes[2].value));
// send the other axes, if there are any
for( size_t a = 3; a < mAxes.size(); ++a )
if( mAxes[a].prevValue != mAxes[a].value )
InputSystemHelper::DoAnalogEvent( this, a, mAxes[a].value);
}
}
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::DoMouseMove( double* diffs, size_t diffcount)
{
// apply to our values. Necessary to make the difference calculation in HandleEvent() work correctly
for( size_t a = 0; a < std::min( diffcount, mAxes.size()); ++a )
mAxes[a].value += diffs[a];
// also reroute to primary mouse if we're in SingleDeviceMode
if( !mSystem->IsInMultiDeviceMode() && GetCount() != 0 )
dynamic_cast<LinuxMouse*> (mSystem->GetMouseByCount( 0))->DoMouseMove( diffs, diffcount);
// callbacks are triggered from EndUpdate()
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::DoMouseWheel( double wheel)
{
// reroute to primary mouse if we're in SingleDeviceMode
if( !mSystem->IsInMultiDeviceMode() && GetCount() != 0 )
return dynamic_cast<LinuxMouse*> (mSystem->GetMouseByCount( 0))->DoMouseWheel( wheel);
// store change
mAxes[2].value += wheel;
// callbacks are triggered from EndUpdate()
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::DoMouseButton( size_t btnIndex, bool isPressed)
{
// reroute to primary mouse if we're in SingleDeviceMode
if( !mSystem->IsInMultiDeviceMode() && GetCount() != 0 )
return dynamic_cast<LinuxMouse*> (mSystem->GetMouseByCount( 0))->DoMouseButton( btnIndex, isPressed);
// don't signal if it isn't an actual state change
if( !!(mState.buttons & (1u << btnIndex)) == isPressed )
return;
// store state change
uint32_t bitmask = (1 << btnIndex);
mState.buttons = (mState.buttons & ~bitmask) | (isPressed ? bitmask : 0);
// and notify everyone interested
InputSystemHelper::DoMouseButton( this, btnIndex, isPressed);
}
// --------------------------------------------------------------------------------------------------------------------
void LinuxMouse::SetFocus( bool pHasFocus)
{
if( pHasFocus )
{
// get current mouse position when in SingleMouseMode
if( !mSystem->IsInMultiDeviceMode() && GetCount() == 0 )
{
Window wa, wb;
int rootx, rooty, childx, childy;
unsigned int mask;
if( XQueryPointer( mSystem->GetDisplay(), DefaultRootWindow( mSystem->GetDisplay()), &wa, &wb, &rootx, &rooty, &childx, &childy, &mask) != 0 )
{
mAxes[0].prevValue = mAxes[0].value; mAxes[1].prevValue = mAxes[1].value;
mAxes[0].value = rootx; mAxes[1].value = rooty;
if( mAxes[0].value != mAxes[0].prevValue || mAxes[1].value != mAxes[1].prevValue )
InputSystemHelper::DoMouseMove( this, mAxes[0].value, mAxes[1].value, mAxes[0].value - mAxes[0].prevValue, mAxes[1].value - mAxes[1].prevValue);
}
}
}
else
{
for( size_t a = 0; a < MB_Count; ++a )
{
if( mState.buttons & (1 << a) )
{
DoMouseButton( a, false);
mState.prevButtons |= (1 << a);
}
}
}
}
// --------------------------------------------------------------------------------------------------------------------
size_t LinuxMouse::GetNumButtons() const
{
return std::max( size_t( MB_Count), mButtons.size());
}
// --------------------------------------------------------------------------------------------------------------------
std::string LinuxMouse::GetButtonText( size_t idx) const
{
return "";
}
// --------------------------------------------------------------------------------------------------------------------
size_t LinuxMouse::GetNumAxes() const
{
return mAxes.size();
}
// --------------------------------------------------------------------------------------------------------------------
std::string LinuxMouse::GetAxisText( size_t idx) const
{
return "";
}
// --------------------------------------------------------------------------------------------------------------------
bool LinuxMouse::IsButtonDown( size_t idx) const
{
if( idx < GetNumButtons() )
return (mState.buttons & (1 << idx)) != 0;
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool LinuxMouse::WasButtonPressed( size_t idx) const
{
if( idx < GetNumButtons() )
return IsButtonDown( idx) && (mState.prevButtons & (1 << idx)) == 0;
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
bool LinuxMouse::WasButtonReleased( size_t idx) const
{
if( idx < GetNumButtons() )
return !IsButtonDown( idx) && (mState.prevButtons & (1 << idx)) != 0;
else
return false;
}
// --------------------------------------------------------------------------------------------------------------------
float LinuxMouse::GetAxisAbsolute( size_t idx) const
{
if( idx < mAxes.size() )
return float( mAxes[idx].value);
else
return 0.0f;
}
// --------------------------------------------------------------------------------------------------------------------
float LinuxMouse::GetAxisDifference( size_t idx) const
{
if( idx < mAxes.size() )
return float( mAxes[idx].value - mAxes[idx].prevValue);
else
return 0.0f;
}
// --------------------------------------------------------------------------------------------------------------------
float LinuxMouse::GetMouseX() const { return GetAxisAbsolute( 0); }
// --------------------------------------------------------------------------------------------------------------------
float LinuxMouse::GetMouseY() const { return GetAxisAbsolute( 1); }
// --------------------------------------------------------------------------------------------------------------------
float LinuxMouse::GetRelMouseX() const { return GetAxisDifference( 0); }
// --------------------------------------------------------------------------------------------------------------------
float LinuxMouse::GetRelMouseY() const { return GetAxisDifference( 1); }
#endif // SNIIS_SYSTEM_LINUX