manager.h

Go to the documentation of this file.
00001 /*
00002  * manager.h
00003  *
00004  * Copyright (C) 2008-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 manager
00032  */
00033 
00034 #ifndef GAMEPAD_MANAGER_H__
00035 #define GAMEPAD_MANAGER_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "gamepad.h"
00039 
00040 #include "nstream/nstream.h"    // named streams
00041 
00042 
00043 namespace gamepad {
00044 
00045 
00046 /// \ingroup gamepadlib
00047 /*@{*/
00048 
00049 
00050 
00051 /// you'll want one of these to get at source devices (gamepad::SourceDevice)
00052 ///   and create gamepad objects (gamepad::Gamepad).
00053 ///
00054 /// Here is how you use a gamepad::Manager object:
00055 ///  - iterate over all exposed SourceDevice objects
00056 ///  - once you have one (or the player selects one), pick a mapping for
00057 ///     the device.  A mapping (gamepad::Map) describes how to extract
00058 ///     logical gamepad events from the raw source device events.
00059 ///  - with a particular source device and mapping, you can create a
00060 ///     gamepad object (gamepad::Gamepad).
00061 ///
00062 /// \b WARNING: this class is not threadsafe.  You'll need to synchronize
00063 ///     access at a higher layer if necessary.
00064 class Manager {
00065 public:
00066         // public enums --------------------------------------------------------
00067         enum eResult {
00068                 eResult_Success         = 0,    ///< success!
00069                 eResult_SourceInUse     = 1,    ///< source device already bound
00070                 eResult_BadSource       = 2,    ///< source device is gone/bad
00071                 eResult_BadMap          = 3,    ///< specified map won't work
00072 
00073                 // keep this last!
00074                 eResult_Invalid         = -1
00075         };
00076 
00077         // virtual destructor --------------------------------------------------
00078         virtual ~Manager(void) throw();
00079 
00080         // gamepad::Manager class interface methods ----------------------------
00081 
00082         /// retrieve the data directory used by this gamepad::Manager
00083         virtual nstream::Folder * getDataDirectory(void) = 0;
00084 
00085         /// this can be an expensive call!  This will re-poll all devices as
00086         ///     needed.  Returns true if anything changed (devices
00087         ///     added/removed)
00088         virtual bool rediscover(void) = 0;
00089 
00090         /// the number of currently discovered source devices
00091         virtual int getSourceDeviceCount(void) = 0;
00092 
00093         /// given an index (0 <= index < SourceDeviceCount), returns the
00094         ///     specified source device.  Will return NULL on bad index, or if
00095         ///      a source device no longer exists.
00096         virtual smart_ptr<SourceDevice> getSourceDevice(IN int index) = 0;
00097 
00098         /// returns source device with the specified unique ID.  Can return
00099         ///     null for a variety of reasons (bad ID, source device was just
00100         ///     unplugged, etc.)
00101         virtual smart_ptr<SourceDevice> getSourceDeviceById(IN const char * id) = 0;
00102 
00103         /// get the count of known gamepade types.  To start with, this consists
00104         ///     of built-in types only, but the client can add more.
00105         virtual int getTypeCount(void) = 0;
00106 
00107         /// get the specified type.  Returns NULL if the input index is invalid.
00108         virtual smart_ptr<Type> getType(IN int index) = 0;
00109 
00110         /// get the specified type (specified by its unique ID)
00111         virtual smart_ptr<Type> getType(IN const char * id) = 0;
00112 
00113         /// add a type to the set of known gamepad types.
00114         virtual void addType(IN smart_ptr<Type>& type) = 0;
00115 
00116         /// get the count of known mappings.  To start with, this consists of
00117         ///     built-in mappings only, but the client can add more.
00118         virtual int getMappingCount(void) = 0;
00119 
00120         /// get the specified mapping.  Returns NULL if the input index is
00121         ///     invalid.
00122         virtual smart_ptr<Map> getMapping(IN int index) = 0;
00123 
00124         /// get the specified mapping (specified by its unique ID)
00125         virtual smart_ptr<Map> getMapping(IN const char * id) = 0;
00126 
00127         /// add a mapping to the set of known mappings
00128         virtual void addMapping(IN smart_ptr<Map>& map) = 0;
00129 
00130         /// once you have a source device and a mapping, you can create a
00131         ///     logical gamepad object.
00132         virtual eResult createGamepad(IN smart_ptr<SourceDevice>& device,
00133                                 IN smart_ptr<Map>& map,
00134                                 OUT smart_ptr<Gamepad>& gamepad) = 0;
00135 
00136         /// get the count of known good gamepad devices
00137         virtual int getGamepadCount(void) = 0;
00138 
00139         /// get the specified gamepad device
00140         virtual smart_ptr<Gamepad> getGamepad(IN int index) = 0;
00141 
00142         /// get the specified gamepad by its ID
00143         virtual smart_ptr<Gamepad> getGamepad(IN const char * id) = 0;
00144 
00145         /// delete (un-configure) the specified gamepad.  Returns false if
00146         ///     the gamepad is not recognized.
00147         virtual bool deleteGamepad(IN const char * gamepadId) = 0;
00148 
00149         /// this refreshes all state for devices.  You can call this once on
00150         ///     the manager, and then all gamepad objects are automatically
00151         ///     updated for you.
00152         /// This routine just iterates through all known source devices and
00153         ///     calls poll() on them.  If you need more control over updates,
00154         ///     you can avoid calling this and call poll() on individual
00155         ///     source devices as needed.
00156         virtual void update(void) = 0;
00157 
00158         // static class methods (factory methods) ------------------------------
00159 
00160         /// this is the static method used to create gamepad::Manager objects.
00161         ///     The data directory should contain all gamepad type files, as
00162         ///     well as localized text files.  See the data directory in this
00163         ///     svn repository for an example (and reference sample).
00164         ///     For now only a single data directory per Manager is supported.
00165         static smart_ptr<Manager> create(IN smart_ptr<nstream::Folder>& dataDirectory);
00166 };
00167 
00168 
00169 
00170 /// helper method to return a list of recommended mappings for a given
00171 ///     source device.  Note that there could be zero to many recommended
00172 ///     mappings!
00173 ///
00174 /// \b NOTE: this helper is very simple and inefficient.  It is intended
00175 ///     to be called very infrequently (such as when a user plugs in a new
00176 ///     device).
00177 void getRecommendedMappings(IN Manager * manager,
00178                                 IN SourceDevice * device,
00179                                 OUT VecString& mapIds);
00180 
00181 
00182 /// helper method to autoconfigure any gamepads possible based on
00183 ///     recommended mappings.
00184 ///
00185 /// \b NOTE: if you use this, make sure you give the user a chance to
00186 ///     correct or remove any autoconfigured gamepads!  Some manufacturers
00187 ///     may change how their devices, breaking older versions of gamepad
00188 ///     libraries etc.
00189 ///
00190 /// This is also inefficient.  Only call when new source devices are
00191 ///     discovered.
00192 void autoconfigureGamepads(IN Manager * manager);
00193 
00194 
00195 };      // gamepad namespace
00196 
00197 #endif  // GAMEPAD_MANAGER_H__
00198