PixelLightAPI  .
InputManager.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: InputManager.h                                 *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLINPUT_INPUTMANAGER_H__
00024 #define __PLINPUT_INPUTMANAGER_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include <PLCore/Core/Singleton.h>
00032 #include <PLCore/Base/Event/Event.h>
00033 #include <PLCore/Container/List.h>
00034 #include <PLCore/Container/HashMap.h>
00035 #include "PLInput/PLInput.h"
00036 
00037 
00038 //[-------------------------------------------------------]
00039 //[ Forward declarations                                  ]
00040 //[-------------------------------------------------------]
00041 namespace PLCore {
00042     class Thread;
00043     class CriticalSection;
00044 }
00045 namespace PLInput {
00046     class Provider;
00047     class Device;
00048     class Keyboard;
00049     class Mouse;
00050     class Control;
00051     class Controller;
00052 }
00053 
00054 
00055 //[-------------------------------------------------------]
00056 //[ Namespace                                             ]
00057 //[-------------------------------------------------------]
00058 namespace PLInput {
00059 
00060 
00061 //[-------------------------------------------------------]
00062 //[ Classes                                               ]
00063 //[-------------------------------------------------------]
00064 /**
00065 *  @brief
00066 *    Input manager
00067 *
00068 *  @remarks
00069 *    The input manager stores all available devices that are present on the computer and
00070 *    controls the update of input messages.
00071 */
00072 class InputManager : public PLCore::Singleton<InputManager> {
00073 
00074 
00075     //[-------------------------------------------------------]
00076     //[ Friends                                               ]
00077     //[-------------------------------------------------------]
00078     friend class PLCore::Singleton<InputManager>;
00079     friend class Provider;
00080     friend class Control;
00081 
00082 
00083     //[-------------------------------------------------------]
00084     //[ Public events                                         ]
00085     //[-------------------------------------------------------]
00086     public:
00087         PLCore::Event<bool>                         EventOnDetectDevices;       /**< Called when device detection has started or stopped */
00088         PLCore::Event<Controller*, PLCore::String>  EventInputControllerFound;  /**< An input controller has been found. Use this event to for instance connect the input controller to real input devices. Found input controller as first parameter, input semantic as second parameter. */
00089 
00090 
00091     //[-------------------------------------------------------]
00092     //[ Public static PLCore::Singleton functions             ]
00093     //[-------------------------------------------------------]
00094     // This solution enhances the compatibility with legacy compilers like GCC 4.2.1 used on Mac OS X 10.6
00095     // -> The C++11 feature "extern template" (C++11, see e.g. http://www2.research.att.com/~bs/C++0xFAQ.html#extern-templates) can only be used on modern compilers like GCC 4.6
00096     // -> We can't break legacy compiler support, especially when only the singletons are responsible for the break
00097     // -> See PLCore::Singleton for more details about singletons
00098     public:
00099         PLINPUT_API static InputManager *GetInstance();
00100         PLINPUT_API static bool HasInstance();
00101 
00102 
00103     //[-------------------------------------------------------]
00104     //[ Public functions                                      ]
00105     //[-------------------------------------------------------]
00106     public:
00107         /**
00108         *  @brief
00109         *    Update input manager once per frame
00110         *
00111         *  @remarks
00112         *    This function must be called once per frame to allow devices to update their status
00113         *    and to process input messages read from these devices. This is also done to make sure
00114         *    that input messages are processed synchronously in the main thread, rather than sending
00115         *    messages from other threads asynchronously.
00116         */
00117         PLINPUT_API void Update();
00118 
00119         /**
00120         *  @brief
00121         *    Detect devices
00122         *
00123         *  @param[in] bReset
00124         *    If 'true', delete all input devices and re-detect them all. Otherwise,
00125         *    only new and removed input devices will be detected.
00126         *
00127         *  @remarks
00128         *    bReset = true should only be used if really necessary, because existing
00129         *    input handlers will most certainly lose their connection to the device.
00130         */
00131         PLINPUT_API void DetectDevices(bool bReset = false);
00132 
00133         /**
00134         *  @brief
00135         *    Get list of input providers
00136         *
00137         *  @return
00138         *    Provider list, do not destroy the returned instances!
00139         */
00140         PLINPUT_API const PLCore::List<Provider*> &GetProviders() const;
00141 
00142         /**
00143         *  @brief
00144         *    Get list of detected input providers
00145         *
00146         *  @return
00147         *    Provider list, do not destroy the returned instances!
00148         */
00149         PLINPUT_API PLCore::List<Provider*> &GetProviders();
00150 
00151         /**
00152         *  @brief
00153         *    Get a specific input provider
00154         *
00155         *  @param[in] sProvider
00156         *    Name of provider
00157         *
00158         *  @return
00159         *    Provider, or a null pointer if it doesn't exist, do not destroy the returned instance!
00160         */
00161         PLINPUT_API Provider *GetProvider(const PLCore::String &sProvider);
00162 
00163         /**
00164         *  @brief
00165         *    Get list of devices
00166         *
00167         *  @return
00168         *    Device list, do not destroy the returned instances!
00169         */
00170         PLINPUT_API PLCore::List<Device*> &GetDevices();
00171 
00172         /**
00173         *  @brief
00174         *    Get a specific device
00175         *
00176         *  @param[in] sDevice
00177         *    Name of device
00178         *
00179         *  @return
00180         *    Device, or a null pointer if it doesn't exist, do not destroy the returned instance!
00181         */
00182         PLINPUT_API Device *GetDevice(const PLCore::String &sDevice) const;
00183 
00184         /**
00185         *  @brief
00186         *    Get default keyboard device
00187         *
00188         *  @return
00189         *    Default keyboard, can be a null pointer, do not destroy the returned instance!
00190         */
00191         PLINPUT_API Keyboard *GetKeyboard() const;
00192 
00193         /**
00194         *  @brief
00195         *    Get default mouse device
00196         *
00197         *  @return
00198         *    Default mouse, can be a null pointer, do not destroy the returned instance!
00199         */
00200         PLINPUT_API Mouse *GetMouse() const;
00201 
00202 
00203     //[-------------------------------------------------------]
00204     //[ Private functions                                     ]
00205     //[-------------------------------------------------------]
00206     private:
00207         /**
00208         *  @brief
00209         *    Constructor
00210         */
00211         InputManager();
00212 
00213         /**
00214         *  @brief
00215         *    Copy constructor
00216         *
00217         *  @param[in] cSource
00218         *    Source to copy from
00219         */
00220         InputManager(const InputManager &cSource);
00221 
00222         /**
00223         *  @brief
00224         *    Destructor
00225         */
00226         virtual ~InputManager();
00227 
00228         /**
00229         *  @brief
00230         *    Destroy all input providers and devices
00231         */
00232         void Clear();
00233 
00234         /**
00235         *  @brief
00236         *    Detect devices from a specific provider
00237         *
00238         *  @param[in] sProvider
00239         *    Name of provider
00240         *  @param[in] bReset
00241         *    If 'true', delete all input devices and re-detect them all. Otherwise,
00242         *    only new and removed input devices will be detected.
00243         *
00244         *  @remarks
00245         *    If the provider is already present, it's Detect()-method will be called. Otherwise,
00246         *    a new instance of the provider will be created, then Detect() will be called as well.
00247         */
00248         void DetectProvider(const PLCore::String &sProvider, bool bReset);
00249 
00250         /**
00251         *  @brief
00252         *    Add a new input device
00253         *
00254         *  @param[in] pDevice
00255         *    Input device, shouldn't be a null pointer (but a null pointer is caught internally)
00256         *
00257         *  @return
00258         *    'true' if all went fine, else 'false'
00259         */
00260         bool AddDevice(Device *pDevice);
00261 
00262         /**
00263         *  @brief
00264         *    Remove device
00265         *
00266         *  @param[in] pDevice
00267         *    Input device, shouldn't be a null pointer (but a null pointer is caught internally)
00268         *
00269         *  @return
00270         *    'true' if all went fine, else 'false'
00271         */
00272         bool RemoveDevice(Device *pDevice);
00273 
00274         /**
00275         *  @brief
00276         *    Remove control
00277         *
00278         *  @param[in] pControl
00279         *    Input control to remove, shouldn't be a null pointer (but a null pointer is caught internally)
00280         */
00281         void RemoveControl(Control *pControl);
00282 
00283         /**
00284         *  @brief
00285         *    Update control
00286         *
00287         *  @param[in] pControl
00288         *    Input control, shouldn't be a null pointer (but a null pointer is caught internally)
00289         *
00290         *  @remarks
00291         *    This marks the control as being updated recently, which will fire a message
00292         *    in the next Update()-call.
00293         */
00294         void UpdateControl(Control *pControl);
00295 
00296 
00297     //[-------------------------------------------------------]
00298     //[ Private data                                          ]
00299     //[-------------------------------------------------------]
00300     private:
00301         // Providers and devices
00302         PLCore::List<Provider*>                      m_lstProviders;        /**< List of providers */
00303         PLCore::HashMap<PLCore::String, Provider*>   m_mapProviders;        /**< Hash map of providers */
00304         PLCore::List<Device*>                        m_lstDevices;          /**< List of devices */
00305         PLCore::HashMap<PLCore::String, Device*>     m_mapDevices;          /**< Hash map of devices */
00306         PLCore::CriticalSection                     *m_pCriticalSection;    /**< Critical section for reading/writing input messages, always valid! */
00307         PLCore::List<Control*>                       m_lstUpdatedControls;  /**< List of controls that have been updated (message list) */
00308 
00309 
00310 };
00311 
00312 
00313 //[-------------------------------------------------------]
00314 //[ Namespace                                             ]
00315 //[-------------------------------------------------------]
00316 } // PLInput
00317 
00318 
00319 #endif // __PLINPUT_INPUTMANAGER_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:56
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported