Debian Codegraph @

linux/lnxcontroller.h

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef __LNXCONTROLLER_H_
20 #define __LNXCONTROLLER_H_
21
22 #include "Controller.h"
23 #include "joystick.h"
24
25 #define NULL_LNXCONTROLLER ((int8_t)NULL_CONTROLLER)
26
27 const int CTF_POV = 64, // POV control
28 CTF_POV2 = 128, // POV 2
29 CTF_POV3 = 256, // POV 3
30 CTF_POV4 = 512; // POV 4
31
32 const unsigned CTF_X_AXIS = (1 << (CT_X_AXIS - 1)), // AXIS constants for ctAxis
33 CTF_Y_AXIS = (1 << (CT_Y_AXIS - 1)), CTF_Z_AXIS = (1 << (CT_Z_AXIS - 1)), CTF_R_AXIS = (1 << (CT_R_AXIS - 1)),
34 CTF_U_AXIS = (1 << (CT_U_AXIS - 1)), CTF_V_AXIS = (1 << (CT_V_AXIS - 1));
35
36 const unsigned CT_MAX_CONTROLLERS = 32, CT_MAX_ELEMENTS = 255, CT_MAX_EXTCTLS = 16, CT_MAX_BUTTONS = 32;
37
38 // rules for adding controllers
39 // any nonstandard special controllers should be added to the below list starting at
40 // CTID_MOUSE-1 (meaning a value of -3, -4 and so on)
41
42 const int CTID_KEYBOARD = -1, // always -1 for keyboards
43 CTID_MOUSE = -2, // always -2 for mice
44 CTID_INVALID = -3; // invalid controller
45
46 // External controls
47 // these are standard controllers handled through DDIO interface
48 // like joysticks, etc.
49
50 const int CTID_EXTCONTROL0 = 0;
51
52 class lnxgameController : public gameController {
53 public:
54 lnxgameController(int num_funcs, ct_function *funcs);
55 ~lnxgameController();
56
57 // these functions suspend or resume any controller reading. this is really only useful for
58 // preemptive controller polling, but they should be used to activate and deactivate controller
59 // reading.
60 virtual void suspend();
61 virtual void resume();
62
63 // this functions polls the controllers if needed. some systems may not need to implement
64 // this function.
65 virtual void poll();
66
67 // flushes all controller information
68 virtual void flush();
69
70 // returns the value of a requested controller type. make sure you flush the controller before polling.
71 virtual ct_config_data get_controller_value(ct_type type_req);
72
73 // sets the configuration of a function (type must be of an array == CTLBINDS_PER_FUNC)
74 virtual void set_controller_function(int id, const ct_type *type, ct_config_data value, const uint8_t *flags);
75
76 // returns information about a requested function (type must be of an array == CTLBINDS_PER_FUNC)
77 virtual void get_controller_function(int id, ct_type *type, ct_config_data *value, uint8_t *flags);
78
79 // temporarily enables or disables a function
80 virtual void enable_function(int id, bool enable);
81
82 // all systems need to implement this function. this returns information about the controller
83 virtual bool get_packet(int id, ct_packet *packet, ct_format alt_format = ctNoFormat);
84
85 // gets sensitivity of axis item
86 virtual float get_axis_sensitivity(ct_type axis_type, uint8_t axis);
87
88 // sets sensitivity of axis item
89 virtual void set_axis_sensitivity(ct_type axis_type, uint8_t axis, float val);
90
91 // assigns an individual function
92 virtual int assign_function(ct_function *fn);
93
94 // activates or deactivates mouse and or controller
95 virtual void mask_controllers(bool joystick, bool mouse);
96
97 // get raw values for the controllers
98 virtual int get_mouse_raw_values(int *x, int *y);
99 virtual unsigned get_joy_raw_values(int *x, int *y);
100
101 // retrieves binding text for desired function, binding, etc.
102 virtual const char *get_binding_text(ct_type type, uint8_t ctrl, uint8_t bind);
103
104 // toggles use of deadzone for controllers
105 void set_controller_deadzone(int ctl, float deadzone);
106
107 private:
108 int m_NumControls; // number of controllers available
109 int m_Suspended; // is controller polling suspended?
110 bool m_JoyActive, m_MouseActive; // enables or disables mouse, joystick control
111
112 struct t_controller {
113 int id;
114 uint16_t flags;
115 uint16_t buttons;
116 unsigned btnmask;
117 float normalizer[CT_NUM_AXES];
118 float sens[CT_NUM_AXES];
119 float sensmod[CT_NUM_AXES];
120 float deadzone;
121 } m_ControlList[CT_MAX_CONTROLLERS]; // the control list.
122
123 struct ct_element {
124 ct_format format;
125 int8_t ctl[CTLBINDS_PER_FUNC];
126 uint8_t value[CTLBINDS_PER_FUNC];
127 ct_type ctype[CTLBINDS_PER_FUNC];
128 uint8_t flags[2];
129 bool enabled;
130 } m_ElementList[CT_MAX_ELEMENTS];
131
132 bool enum_controllers();
133
134 // sets up an elements information structure
135 void assign_element(int id, ct_element *elem);
136
137 // this returns an index into the control list.
138 int8_t get_axis_controller(uint8_t axis);
139
140 // returns controller with specified button
141 int8_t get_button_controller(uint8_t btn);
142
143 // returns the controller with a pov hat
144 int8_t get_pov_controller(uint8_t pov);
145
146 // note controller is index into ControlList.
147 float get_axis_value(int8_t controller, uint8_t axis, ct_format format, bool invert = false);
148
149 // get value of button in seconds, presses, etc.
150 float get_button_value(int8_t controller, ct_format format, uint8_t button);
151
152 // get value of pov (using JOYPOV values)
153 float get_pov_value(int8_t controller, ct_format format, uint8_t pov_number, uint8_t pov);
154
155 // get keyboard info
156 float get_key_value(int key, ct_format format);
157
158 void parse_ctl_file(int devnum, const char *ctlname);
159
160 private:
161 struct t_msestate {
162 int x, y, z;
163 int mx, my;
164 unsigned btnmask;
165 } m_MseState;
166
167 struct t_extctlstate {
168 int x, y, z, r, u, v;
169 int pov[JOYPOV_NUM];
170 int last_pov[JOYPOV_NUM];
171 float povstarts[JOYPOV_NUM][JOYPOV_DIR];
172 float povtimes[JOYPOV_NUM][JOYPOV_DIR];
173 uint8_t povpresses[JOYPOV_NUM][JOYPOV_DIR];
174 unsigned buttons;
175 uint8_t btnpresses[CT_MAX_BUTTONS];
176 float btnstarts[CT_MAX_BUTTONS];
177 float btntimes[CT_MAX_BUTTONS];
178 } m_ExtCtlStates[CT_MAX_EXTCTLS];
179
180 // thread info.
181 int64_t m_frame_timer_ms;
182 float m_frame_time;
183
184 // note id is id value from controller in control list.
185 void extctl_getpos(int id);
186
187 // this gets timings for mouse buttons
188 void mouse_geteval();
189 };
190
191 #endif