gamepad-type.h

Go to the documentation of this file.
00001 /*
00002  * gamepad-type.h
00003  *
00004  * Copyright (C) 2009,2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Gamepad Type object.  This is a completely data-driven concept.
00032  */
00033 
00034 #ifndef GAMEPAD_GAMEPAD_TYPE_H__
00035 #define GAMEPAD_GAMEPAD_TYPE_H__
00036 
00037 
00038 // includes --------------------------------------------------------------------
00039 #include "datahash/datahash.h"
00040 #include "nstream/nstream.h"
00041 
00042 
00043 namespace gamepad {
00044 
00045 
00046 /// \ingroup gamepadlib
00047 /*@{*/
00048 
00049 /// A type of input object (button, joystick, potentiometer, directional pad...)
00050 enum eInputType {
00051         eInput_Button   = 0x01,  ///< simple on/off button or switch
00052         eInput_Joystick = 0x02,  ///< 2-axis joystick
00053         eInput_Pot      = 0x04,  ///< potentiometer (1-dimensional variable)
00054         eInput_Dpad     = 0x08,  ///< directional pad
00055 
00056         // keep this last!
00057         eInput_Invalid  = 0x00
00058 };
00059 
00060 
00061 
00062 /// a type of gamepad (Dualshock, Xbox, N64, etc)
00063 class Type {
00064 public:
00065         // virtual destructor --------------------------------------------------
00066         virtual ~Type(void) throw();
00067 
00068         // gamepad::Type class interface methods -------------------------------
00069 
00070         /// returns the ID of this type.  This should be a UUID
00071         virtual const char * getId(void) const throw() = 0;
00072 
00073         /// returns the name of this type of gamepad.  This is usually a string
00074         ///     such as "XBox 360" or "Playstation 3" or "Nintendo 64", etc.
00075         ///     The string is intended for internal use only (so it is stable
00076         ///     across locales, but therefore not appropriate for display).
00077         virtual const char * getName(void) const throw() = 0;
00078 
00079         /// given a type of input (button, joystick, etc.) this returns the
00080         ///     number of such inputs available on this type of gamepad. 
00081         virtual int getInputCount(IN eInputType type) const throw() = 0;
00082 
00083         /// given the index of a particular input, returns the logical name.
00084         ///     Returns NULL in some cases (no such type, etc.).  Program will
00085         ///     crash (with error) if you pass in a bad index, so call
00086         ///     Type::getInputCount() first!
00087         virtual const char * getLogicalName(IN eInputType type,
00088                                 IN int idx) const throw() = 0;
00089 
00090         /// returns all known logical inputs on the gamepad, regardless of type
00091         virtual void getAllInputs(OUT VecString& logicalNames) const = 0;
00092 
00093         /// given a logical name for an input object on the gamepad, returns
00094         ///     all of the types (eInputType values OR'd together).  Returns
00095         ///     eInput_Invalid if name is unrecognized.
00096         virtual eInputType getInputTypes(IN const char * logicalName) const throw() = 0;
00097 
00098         /// given a logical name and input type, return index for that type.
00099         ///     returns -1 if logical name or type are incorrect
00100         virtual int getLogicalIndex(IN const char * logicalName,
00101                                 IN eInputType type) throw() = 0;
00102 
00103         /// get the logical name for the most commonly used button
00104         virtual const char * getMainButton(void) const throw() = 0;
00105 
00106         /// get the vector graphics (vgfx) request that describes this
00107         ///     controller.  Returns NULL if no vector graphics request was
00108         ///     provided.  See vgfx::processRequest() for details about the
00109         ///     vgfx request stream.
00110         virtual smart_ptr<nstream::Stream> getVgfx(void) = 0;
00111 
00112         /// debugging only: dump type information to stdout
00113         virtual void dump(void) const throw() = 0;
00114 };
00115 
00116 
00117 /// maps from name --> gamepad type object
00118 typedef std::map<std::string, smart_ptr<Type> > gamepad_type_map_t;
00119 
00120 
00121 
00122 ////////////////////////////////////////////////////////////////////////////////
00123 //
00124 //      Gamepad API
00125 //
00126 ////////////////////////////////////////////////////////////////////////////////
00127 
00128 /// read a gamepad type object from the given datahash
00129 smart_ptr<Type> readType(IN nstream::Stream * stream);
00130 
00131 
00132 /// load all gamepad objects in the specified folder, and add them to the
00133 ///     provided map (keyed by name, see gamepad::Type::getName() ).
00134 /// This method is NOT recursive (it does not parse subfolders if any).
00135 void addGamepadTypesInFolderToMap(IO nstream::Folder * folder,
00136                                 IO gamepad_type_map_t& map);
00137 
00138 
00139 
00140 };      // gamepad namespace
00141 
00142 #endif  // GAMEPAD_GAMEPAD_TYPE_H__
00143