| 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 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/lib/Controller.h $ |
| 21 | * $Revision: 21 $ |
| 22 | * $Date: 3/20/00 12:25p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Universal controller header |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/lib/Controller.h $ |
| 28 | * |
| 29 | * 21 3/20/00 12:25p Matt |
| 30 | * Merge of Duane's post-1.3 changes. |
| 31 | * Added declaration. |
| 32 | * |
| 33 | * 20 8/11/99 6:06p Samir |
| 34 | * added flag to determine whether packet contained mouse info. |
| 35 | * |
| 36 | * 19 7/20/99 4:53p Samir |
| 37 | * added ability to manually set the deadzone for a controller. |
| 38 | * |
| 39 | * 18 7/16/99 11:14a Samir |
| 40 | * multiple hat support and improved direct input support. |
| 41 | * |
| 42 | * 17 4/29/99 2:23a Samir |
| 43 | * moved binding text functions to wincontroller.cpp and new text for |
| 44 | * multiple joysticks. |
| 45 | * |
| 46 | * 16 4/16/99 7:34p Jeff |
| 47 | * added linux controller |
| 48 | * |
| 49 | * 15 2/16/99 11:59a Samir |
| 50 | * added proper constants for controller and binding null values. |
| 51 | * |
| 52 | * 14 12/18/98 6:00p Samir |
| 53 | * added enable_function. |
| 54 | * |
| 55 | * 13 10/24/98 2:18p Samir |
| 56 | * added mouse and joytick raw value retrieval functions. |
| 57 | * |
| 58 | * 12 10/21/98 10:36a Samir |
| 59 | * added code to turn on or off joystick or mouse. |
| 60 | * |
| 61 | * 11 10/18/98 7:29p Samir |
| 62 | * made assign_function public. |
| 63 | * |
| 64 | * 10 10/17/98 7:31p Samir |
| 65 | * added invertible axes |
| 66 | * |
| 67 | * 9 9/10/98 12:39p Samir |
| 68 | * added senstivity issures for controller. |
| 69 | * |
| 70 | * 8 6/18/98 4:48p Samir |
| 71 | * added changes for multiple configs for joystick controls. |
| 72 | * |
| 73 | * 7 5/15/98 3:13p Samir |
| 74 | * added 2 key support for controller functions. |
| 75 | * |
| 76 | * 6 2/24/98 11:03a Samir |
| 77 | * Added flush function to controller system. |
| 78 | * |
| 79 | * 5 2/16/98 3:04p Samir |
| 80 | * ctAxis instead of ctXAxis, ctYAxis, etc. |
| 81 | * |
| 82 | * 4 2/13/98 6:38p Samir |
| 83 | * Added get and set controller function. |
| 84 | * |
| 85 | * 3 12/03/97 7:35p Samir |
| 86 | * Newer joystick library support and some POV. |
| 87 | * |
| 88 | * 2 10/29/97 4:44p Samir |
| 89 | * Added ctDownCount format. |
| 90 | * |
| 91 | * 8 5/21/97 3:51p Samir |
| 92 | * changed ct_need to ct_function |
| 93 | * |
| 94 | * 7 5/12/97 4:39 PM Jeremy |
| 95 | * #include of macController.h on Macintosh |
| 96 | * |
| 97 | * 6 5/12/97 1:21p Samir |
| 98 | * Added suspend and resume functions. |
| 99 | * |
| 100 | * 5 4/23/97 1:07p Samir |
| 101 | * Now we can poll for either positonal or evaluator data. |
| 102 | * |
| 103 | * 4 4/16/97 1:04p Samir |
| 104 | * For get packet, allow one to return an alternate format value if that |
| 105 | * function supports it. |
| 106 | * |
| 107 | * 3 4/16/97 12:27p Samir |
| 108 | * Added mouse support. |
| 109 | * |
| 110 | * 2 4/11/97 2:13p Samir |
| 111 | * Universal header for control system. first version |
| 112 | * |
| 113 | * $NoKeywords: $ |
| 114 | */ |
| 115 | |
| 116 | #ifndef CONTROLLER_H |
| 117 | #define CONTROLLER_H |
| 118 | |
| 119 | #include <cstdint> |
| 120 | |
| 121 | #include "pstypes.h" |
| 122 | #include "Macros.h" |
| 123 | |
| 124 | enum ct_format { |
| 125 | ctNoFormat, |
| 126 | ctAnalog, // analog information (-1.0 to 1.0) |
| 127 | ctDigital, // digital information (0 or 1) |
| 128 | ctDownCount, // special information for key presses and button/mouse presses. |
| 129 | ctTime // time in seconds format |
| 130 | }; |
| 131 | |
| 132 | enum ct_type { |
| 133 | ctNone, |
| 134 | ctAxis, // axis element of controller. |
| 135 | ctPOV, // function value (hiword = +axis, loword = -axis) |
| 136 | ctButton, // fn value: controller button # |
| 137 | ctKey, // fn value: lobyte(key constant), hibyte(alternate key constant) |
| 138 | ctMouseAxis, |
| 139 | ctMouseButton, |
| 140 | ctPOV2, |
| 141 | ctPOV3, |
| 142 | ctPOV4 // auxillary POV values. |
| 143 | }; |
| 144 | |
| 145 | struct ct_function { |
| 146 | int id; // identifier for the function (like forward thrust) |
| 147 | ct_format format; // what format should the return value be for this function |
| 148 | ct_type ctype[2]; // type of controller requested for this id. (1 for each value packed.) |
| 149 | uint8_t value[2]; // corresponding value to ctype |
| 150 | uint8_t flags[2]; // flags. |
| 151 | }; |
| 152 | |
| 153 | struct ct_packet { |
| 154 | ct_format format; // format of value. |
| 155 | float value; // time value for buttons, absolute value for axis values |
| 156 | unsigned flags; // additional information (see below) |
| 157 | }; |
| 158 | |
| 159 | typedef unsigned ct_config_data; // passed by controller system to the outside, and back to controller system |
| 160 | |
| 161 | // values for ct_packet.flags |
| 162 | #define CTPK_ELEMENTACTIVE 0x1 // indicates element was activated but no time/analog information is available. |
| 163 | #define CTPK_MOUSE 0x2 // this is coming from a mouse device. default is joystick/keyboard. |
| 164 | |
| 165 | // element values |
| 166 | const uint8_t CT_X_AXIS = 1, // AXIS constants for ctAxis |
| 167 | CT_Y_AXIS = 2, CT_Z_AXIS = 3, CT_R_AXIS = 4, CT_U_AXIS = 5, CT_V_AXIS = 6, |
| 168 | CT_NUM_AXES = 6; // number of axes |
| 169 | |
| 170 | #define makeword(_h, _l) (((_h) << 16) + ((_l) & 0xffff)) |
| 171 | #define hiword(_v) ((_v) >> 16) |
| 172 | #define loword(_v) ((_v) & 0x0000ffff) |
| 173 | #define makeshort(_h, _l) (((_h) << 8) + ((_l) & 0x00ff)) |
| 174 | #define hibyte(_w) ((_w) >> 8) |
| 175 | #define lobyte(_w) ((_w) & 0x00ff) |
| 176 | |
| 177 | // ct_function flags |
| 178 | #define CTFNF_INVERT 0x1 // invert values returned via get_packet. |
| 179 | |
| 180 | #define NULL_BINDING 0x00 |
| 181 | #define NULL_CONTROLLER 0xff |
| 182 | #define INVALID_CONTROLLER_INFO 0xffff |
| 183 | |
| 184 | #define CONTROLLER_KEY1_VALUE(_b) lobyte(_b) |
| 185 | #define CONTROLLER_KEY2_VALUE(_b) hibyte(_b) |
| 186 | #define CONTROLLER_KEY_VALUE(_k1, _k2) makeshort(_k2, _k1) |
| 187 | |
| 188 | #define CONTROLLER_CTL1_VALUE(_b) CONTROLLER_KEY1_VALUE(_b) |
| 189 | #define CONTROLLER_CTL2_VALUE(_b) CONTROLLER_KEY2_VALUE(_b) |
| 190 | #define CONTROLLER_CTL_VALUE(_l, _h) makeshort(_h, _l) |
| 191 | |
| 192 | #define CONTROLLER_CTL1_INFO(_b) ((int8_t)lobyte(_b)) |
| 193 | #define CONTROLLER_CTL2_INFO(_b) ((int8_t)hibyte(_b)) |
| 194 | #define CONTROLLER_CTL_INFO(_l, _h) makeshort(_h, _l) |
| 195 | |
| 196 | #define CONTROLLER_VALUE(_l) ((uint16_t)loword(_l)) |
| 197 | #define CONTROLLER_INFO(_l) ((int16_t)hiword(_l)) |
| 198 | #define MAKE_CONFIG_DATA(_c, _v) makeword(_c, _v) |
| 199 | |
| 200 | #define CTLBINDS_PER_FUNC 2 |
| 201 | |
| 202 | class gameController { |
| 203 | public: |
| 204 | gameController(int num_funcs, ct_function *funcs){}; |
| 205 | virtual ~gameController(){}; |
| 206 | |
| 207 | // these functions suspend or resume any controller reading. this is really only useful for |
| 208 | // preemptive controller polling, but they should be used to activate and deactivate controller |
| 209 | // reading. |
| 210 | virtual void suspend(){}; |
| 211 | virtual void resume(){}; |
| 212 | |
| 213 | // this functions polls the controllers if needed. some systems may not need to implement |
| 214 | // this function. |
| 215 | virtual void poll(){}; |
| 216 | |
| 217 | // flushes all controller information |
| 218 | virtual void flush() = 0; |
| 219 | |
| 220 | // returns the value of a requested controller type. make sure you flush the controller before polling. |
| 221 | virtual ct_config_data get_controller_value(ct_type type_req) = 0; |
| 222 | |
| 223 | // sets the configuration of a function (type must be of an array == CTLBINDS_PER_FUNC) |
| 224 | virtual void set_controller_function(int id, const ct_type *type, ct_config_data value, const uint8_t *flags) = 0; |
| 225 | |
| 226 | // returns information about a requested function (type must be of an array == CTLBINDS_PER_FUNC) |
| 227 | virtual void get_controller_function(int id, ct_type *type, ct_config_data *value, uint8_t *flags) = 0; |
| 228 | |
| 229 | // temporarily enables or disables a function |
| 230 | virtual void enable_function(int id, bool enable) = 0; |
| 231 | |
| 232 | // all systems need to implement this function. this returns information about the controller |
| 233 | virtual bool get_packet(int id, ct_packet *packet, ct_format alt_format = ctNoFormat) = 0; |
| 234 | |
| 235 | // gets sensitivity of axis item |
| 236 | virtual float get_axis_sensitivity(ct_type axis_type, uint8_t axis) = 0; |
| 237 | |
| 238 | // sets sensitivity of axis item |
| 239 | virtual void set_axis_sensitivity(ct_type axis_type, uint8_t axis, float val) = 0; |
| 240 | |
| 241 | // assigns an individual function |
| 242 | virtual int assign_function(ct_function *fn) = 0; |
| 243 | |
| 244 | // activates or deactivates mouse and or controller |
| 245 | virtual void mask_controllers(bool joystick, bool mouse) = 0; |
| 246 | |
| 247 | // retrieves binding text for desired function, binding, etc. |
| 248 | virtual const char *get_binding_text(ct_type type, uint8_t ctrl, uint8_t bind) = 0; |
| 249 | |
| 250 | // get raw values for the controllers |
| 251 | virtual int get_mouse_raw_values(int *x, int *y) = 0; |
| 252 | virtual unsigned get_joy_raw_values(int *x, int *y) = 0; |
| 253 | |
| 254 | // toggles use of deadzone for controllers. ctl can be 0 to ??? |
| 255 | // dead zone is from 0.0 to 0.5 |
| 256 | virtual void set_controller_deadzone(int ctl, float deadzone){}; |
| 257 | virtual float get_controller_deadzone(int ctl) { return 0; }; |
| 258 | |
| 259 | // toggles use of axis on controllers. ctl can be 0 to ??? |
| 260 | // axis is a CT_?_AXIS value |
| 261 | void toggle_controller_axis(int ctl, int axis, bool toggle){}; |
| 262 | }; |
| 263 | |
| 264 | gameController *CreateController(int num_funcs, ct_function *funcs, char *remote_ip); |
| 265 | void DestroyController(gameController *ctl); |
| 266 | |
| 267 | #if defined(DX_APP) |
| 268 | #include "win\WinController.h" |
| 269 | #else |
| 270 | #include "lnxcontroller.h" |
| 271 | #endif |
| 272 | |
| 273 | #endif |