//////////////////////////////////////////////////////////// // input.h // // Header file for input device classes for EDrive, a multiplatform // driving simulator. // // Copyright 2004 by Evan Alexander Weaver // // Despite the copyright, this is free software. See edrive.cpp for details. // // Date last modified: 31 May 2004 #ifndef _edrive_input_h_ #define _edrive_input_h_ #include "platform.h" #if ED_DIRECTX_9_INPUT //*************************************************** #include #include #endif //******************************************************************* #include // Keys we care about #define EDKEY_ESC 1 #define EDKEY_UP 2 #define EDKEY_DOWN 4 #define EDKEY_LEFT 8 #define EDKEY_RIGHT 0x10 #define EDKEY_C 0x20 #define EDKEY_ENTER 0x40 #define EDKEY_A 0x80 #define EDKEY_Z 0x0100 // The keyboard // class keyboard { #if ED_DIRECTX_9_INPUT //*********************************************** LPDIRECTINPUTDEVICE8 dev; #endif //*************************************************************** public: static unsigned int keys; // key states. There had better be only one // keyboard! (Done this way because of GLUT) keyboard(); keyboard(const keyboard &); // intentionally omitted... keyboard &operator=(const keyboard &); // ...to prevent copies ~keyboard(); void destroy(); // unattached object from hardware void restore(); // restores use of keyboard after we've lost it // attach the object to the actual keyboard #if ED_DIRECTX_9_INPUT //*********************************************** bool create(HINSTANCE, HWND); #else //**************************************************************** bool create(); #endif //*************************************************************** unsigned int read(); // return the current state of the keys }; // The ID for a joystick. In DirectInput, this is a GUID, but in Linux it // is a string (with the file name of the joystick device). We need to be // able to remember what joystick was chosen, so we made this class in order // to have the game configuration code not be system dependent. // class joystickid { #if ED_DIRECTX_9_INPUT //*********************************************** GUID id; #else //**************************************************************** char id[81]; #endif //*************************************************************** public: // default constuctor makes an "empty" ID joystickid(); #if ED_DIRECTX_9_INPUT //*********************************************** // construct an ID from a GUID joystickid(const GUID &); // cast an ID to a GUID operator GUID() const; #else //**************************************************************** // construct an ID from a device name string joystickid(const char s[]); // cast an ID to a string operator const char *() const; #endif //*************************************************************** // read ID from/write ID to a file bool fread(FILE *); bool fwrite(FILE *) const; // see if ID is empty bool empty() const; // see if this ID is the same as another bool operator==(const joystickid &) const; }; // A list of available joysticks // class joysticklist { #if ED_DIRECTX_9_INPUT //*********************************************** // In Windows, leave room for up to 15 // joystickid ids[15]; // GUIDs of active joysticks char names[15][MAX_PATH]; // friendly names of active joysticks #else //**************************************************************** // In Linux, there are just four devices // static char choices[4][20]; // actual device names char names[4][81]; // friendly names of active joystick int ids[4]; // index of active devices in "choices" #endif //*************************************************************** int joycnt; // number of available joysticks public: // default constructor makes an empty list joysticklist(); #if ED_DIRECTX_9_INPUT //*********************************************** // get a fresh list (in case user has attached a joystick) void refresh(HINSTANCE); // add a joy void joysticklist::add(joystickid, const char *); #else //**************************************************************** // get a fresh list (in case user has attached a joystick) void refresh(); #endif //*************************************************************** // return the ID for specified joystick joystickid getid(int) const; // return the number of active joysticks int count() const; // return the friendly name for specified joystick const char *getdesc(int); }; // A joystick. Currently force feedback is not supported, and only the // driving axes (and no buttons) are tracked // class joystick { #if ED_DIRECTX_9_INPUT //*********************************************** LPDIRECTINPUTDEVICE8 dev; // the DirectInput device for the joystick int firsttime; // some setup is required the first time GUID guid; // the GUID for the device DIJOYSTATE2 js; // data polled from joystick // In DirectInput, there are at most 6 axes [x y z rx ry rz] static DWORD axofs[6]; // offset of each exis in "js" bool active[6]; // are axes active? #else //**************************************************************** int fd; // file descriptor for open joystick // In Linux, leave room for up to 20 axes and 20 buttons int axis[20]; // state of each axis int button[20]; // state of each button #endif //*************************************************************** // poll the joystick, filling "js" or "axis/button" above bool readdata(); // IDs for each axis we want to track. This is // dir * (1 + index of axis in "axofs" or "axis" above) // where dir is 1 (normal direction) or -1 (reverse) // Note that accelaxis and brakeaxis can share the same axis as long // as they go in opposite directions (and so have opposite signs), // in which case the range will be split down the middle. // // a value of zero means that axis isn't used (keys will be used instead) // int steeraxis; // steering int accelaxis; // accelerating int brakeaxis; // braking public: // default constructor makes a non-functioning joystick joystick(); ~joystick(); // unattach from physical joystick // void destroy(); // reestablish connection with physical joystick in case it was lost void restore(); // is the joystick working? bool alive() const; // attach to the specified physical joystick #if ED_DIRECTX_9_INPUT //*********************************************** bool create(const joystickid &id, HWND hwnd, HINSTANCE hinst); #else //**************************************************************** bool create(const joystickid &id); #endif //*************************************************************** // returns ID of axis selected by moving the joystick, or zero if // the joystick hasn't been moved enough yet. If "reset" is true // then the starting position is reset to the current joystick position. // int pickaxis(bool reset = false); // return the values of the current driving axes bool read(int &steer, int &accel, int &brake); // set which axes are used for driving void setaxes(int stax, int acax, int brax); }; #endif