-
Notifications
You must be signed in to change notification settings - Fork 0
/
SNIIS_Linux.h
154 lines (130 loc) · 4.81 KB
/
SNIIS_Linux.h
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
/// @file SNIIS_Linux.h
/// Linux-specific classes
#pragma once
#include "SNIIS.h"
#if SNIIS_SYSTEM_LINUX
#include <cstdint>
#include <cassert>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
class LinuxMouse;
class LinuxKeyboard;
class LinuxJoystick;
/// -------------------------------------------------------------------------------------------------------------------
/// Linux Input System
class LinuxInput : public SNIIS::InputSystem
{
/// Window and Display
Window mWindow;
Display* mDisplay;
/// XInput2 extension opcode
int mXiOpcode;
/// Devices by DeviceID
std::map<int, LinuxMouse*> mMiceById;
std::map<int, LinuxKeyboard*> mKeyboardsById;
public:
/// Constructor
LinuxInput( Window wnd);
/// Destructor
~LinuxInput();
/// Updates the inputs, to be called before handling system messages
void Update() override;
/// Notifies the input system that the application has lost/gained focus.
void InternSetFocus( bool pHasFocus) override;
void InternSetMouseGrab( bool enabled) override;
Display* GetDisplay() const { return mDisplay; }
};
/// -------------------------------------------------------------------------------------------------------------------
/// Linux mouse, fed by XEvents passed in from the outside
class LinuxMouse : public SNIIS::Mouse
{
LinuxInput* mSystem;
int mDeviceId;
struct Button { Atom label; };
std::vector<Button> mButtons;
struct Axis { Atom label; double min, max; double value, prevValue; bool isAbsolute; };
std::vector<Axis> mAxes;
struct State
{
uint32_t buttons, prevButtons;
} mState;
public:
LinuxMouse( LinuxInput* pSystem, size_t pId, const XIDeviceInfo& pDeviceInfo);
void StartUpdate();
void HandleEvent( const XIRawEvent& ev);
void EndUpdate();
void SetFocus( bool pHasFocus);
size_t GetNumButtons() const override;
std::string GetButtonText( size_t idx) const override;
size_t GetNumAxes() const override;
std::string GetAxisText( size_t idx) const override;
bool IsButtonDown( size_t idx) const override;
bool WasButtonPressed( size_t idx) const override;
bool WasButtonReleased( size_t idx) const override;
float GetAxisAbsolute( size_t idx) const override;
float GetAxisDifference( size_t idx) const override;
float GetMouseX() const override;
float GetMouseY() const override;
float GetRelMouseX() const override;
float GetRelMouseY() const override;
private:
void DoMouseMove( double* diffs, size_t diffcount);
void DoMouseWheel( double wheel);
void DoMouseButton( size_t btnIndex, bool isPressed);
};
/// -------------------------------------------------------------------------------------------------------------------
/// Linux keyboard, aswell fed by XEvents
class LinuxKeyboard : public SNIIS::Keyboard
{
LinuxInput* mSystem;
int mDeviceId;
size_t mNumKeys;
std::vector<uint32_t> mExtraButtons;
std::vector<uint64_t> mState, mPrevState; ///< current and previous keystate
public:
LinuxKeyboard(LinuxInput* pSystem, size_t pId, const XIDeviceInfo& pDeviceInfo);
void StartUpdate();
void HandleEvent( const XIRawEvent& ev);
void SetFocus( bool pHasFocus);
size_t GetNumButtons() const override;
std::string GetButtonText( size_t idx) const override;
bool IsButtonDown( size_t idx) const override;
bool WasButtonPressed( size_t idx) const override;
bool WasButtonReleased( size_t idx) const override;
protected:
void DoKeyboardButton( SNIIS::KeyCode kc, size_t unicode, bool isPressed);
void Set( size_t kc, bool set);
bool IsSet( size_t kc) const;
bool WasSet( size_t kc) const;
size_t TranslateText( size_t kc);
};
/// -------------------------------------------------------------------------------------------------------------------
/// Linux joystick
class LinuxJoystick : public SNIIS::Joystick
{
LinuxInput* mSystem;
int mFileDesc;
struct Axis { size_t idx; bool isAbsolute; int32_t min, max, flat; };
std::vector<Axis> mAxes;
struct Button { size_t idx; };
std::vector<Button> mButtons;
struct State {
uint64_t buttons, prevButtons;
float axes[16], diffs[16];
} mState;
public:
LinuxJoystick( LinuxInput* pSystem, size_t pId, int pFileDesc);
void StartUpdate();
void SetFocus( bool pHasFocus);
size_t GetNumButtons() const override;
std::string GetButtonText( size_t idx) const override;
size_t GetNumAxes() const override;
std::string GetAxisText( size_t idx) const override;
bool IsButtonDown( size_t idx) const override;
bool WasButtonPressed( size_t idx) const override;
bool WasButtonReleased( size_t idx) const override;
float GetAxisAbsolute( size_t idx) const override;
float GetAxisDifference( size_t idx) const override;
};
#endif // SNIIS_SYSTEM_LINUX