00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <gadget/gadgetConfig.h>
00034
00035 #include <iomanip>
00036 #include <typeinfo>
00037
00038 #include <vpr/vpr.h>
00039 #include <jccl/Config/ConfigElement.h>
00040 #include <gadget/Type/DeviceFactory.h>
00041 #include <gadget/Type/DeviceConstructor.h>
00042
00043
00044 #include <gadget/Devices/Sim/SimAnalog.h>
00045 #include <gadget/Devices/Sim/SimDigital.h>
00046 #include <gadget/Devices/Sim/SimPosition.h>
00047 #include <gadget/Devices/Sim/SimGloveGesture.h>
00048
00049 #include <gadget/Devices/Sim/SimRelativePosition.h>
00050 #include <gadget/Devices/Sim/SimSetablePosition.h>
00051 #include <gadget/Devices/Sim/SimDigitalGlove.h>
00052 #include <gadget/Devices/KeyboardMouseDevice/KeyboardMouseDevice.h>
00053
00054 #if defined(VPR_OS_Windows)
00055 # include <gadget/Devices/KeyboardMouseDevice/InputWindowWin32.h>
00056 #elif defined(VPR_OS_Darwin) && ! defined(GADGET_USE_X11)
00057 # include <gadget/Devices/KeyboardMouseDevice/InputWindowOSX.h>
00058 #else
00059
00060 # include <gadget/Devices/KeyboardMouseDevice/InputWindowXWin.h>
00061 #endif
00062
00063
00064 #ifdef STATIC_DRIVERS
00065
00066
00067 #ifdef GADGET_HAVE_DTK
00068 # include <gadget/Devices/Open/DTK/DTK.h>
00069 #endif
00070
00071 #endif
00072
00073 #include <gadget/Util/Debug.h>
00074
00075 namespace gadget
00076 {
00077
00078
00079 vprSingletonImpWithInitFunc(DeviceFactory, loadKnownDevices);
00080
00084 void DeviceFactory::loadKnownDevices()
00085 {
00086 gadget::InputManager* input_mgr = gadget::InputManager::instance();
00087
00088 #ifdef STATIC_DRIVERS
00089
00090 #ifdef GADGET_HAVE_DTK
00091 DeviceConstructor<DTK>* dtk_wrapper = new DeviceConstructor<DTK>;
00092 if( (NULL == dtk_wrapper))
00093 {
00094 vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL) << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n" << vprDEBUG_FLUSH;
00095 }
00096 #endif
00097
00098 #endif
00099
00100
00101 DeviceConstructor<SimAnalog>* sim_analog = new DeviceConstructor<SimAnalog>(input_mgr);
00102 DeviceConstructor<SimDigital>* sim_digital = new DeviceConstructor<SimDigital>(input_mgr);
00103 DeviceConstructor<SimPosition>* sim_position = new DeviceConstructor<SimPosition>(input_mgr);
00104
00105 DeviceConstructor<SimSetablePosition>* sim_setable = new DeviceConstructor<SimSetablePosition>(input_mgr);
00106 DeviceConstructor<SimRelativePosition>* sim_relative = new DeviceConstructor<SimRelativePosition>(input_mgr);
00107
00108 DeviceConstructor<SimGloveGesture>* sim_glove = new DeviceConstructor<SimGloveGesture>(input_mgr);
00109 DeviceConstructor<SimDigitalGlove>* simpinch_glove = new DeviceConstructor<SimDigitalGlove>(input_mgr);
00110
00111 if( (NULL == sim_analog) ||
00112 (NULL == sim_digital) ||
00113 (NULL == sim_position) ||
00114 (NULL == sim_glove) ||
00115 (NULL == simpinch_glove) ||
00116 (NULL == sim_setable) ||
00117 (NULL == sim_relative) )
00118 {
00119 vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL) << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n" << vprDEBUG_FLUSH;
00120 }
00121
00122 DeviceConstructor<KeyboardMouseDevice>* keyboard_device =
00123 new DeviceConstructor<KeyboardMouseDevice>(input_mgr);
00124 if( (NULL == keyboard_device))
00125 {
00126 vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00127 << clrOutBOLD(clrRED,"ERROR:") << "Failed to load the known device KeyboardMouseDevice."
00128 << std::endl << vprDEBUG_FLUSH;
00129 }
00130 #if defined(VPR_OS_Windows)
00131 DeviceConstructor<InputWindowWin32>* key_win32 =
00132 new DeviceConstructor<InputWindowWin32>(input_mgr);
00133 if( (NULL == key_win32))
00134 {
00135 vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00136 << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n"
00137 << vprDEBUG_FLUSH;
00138 }
00139 #elif defined(VPR_OS_Darwin) && ! defined(GADGET_USE_X11)
00140 DeviceConstructor<InputWindowOSX>* osx_keyboard =
00141 new DeviceConstructor<InputWindowOSX>(input_mgr);
00142 if( (NULL == osx_keyboard) )
00143 {
00144 vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00145 << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n"
00146 << vprDEBUG_FLUSH;
00147 }
00148 #else
00149 DeviceConstructor<InputWindowXWin>* xwin_key =
00150 new DeviceConstructor<InputWindowXWin>(input_mgr);
00151 if( (NULL == xwin_key) )
00152 {
00153 vprDEBUG(vprDBG_ALL,vprDBG_CRITICAL_LVL)
00154 << clrOutBOLD(clrRED,"ERROR:") << "Failed to load a known device\n"
00155 << vprDEBUG_FLUSH;
00156 }
00157 #endif
00158
00159 }
00160
00161 void DeviceFactory::registerDevice(DeviceConstructorBase* constructor)
00162 {
00163 vprASSERT(constructor != NULL);
00164 mConstructors.push_back(constructor);
00165 vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
00166 << "gadget::DeviceFactory: Registered: "
00167 << std::setiosflags(std::ios::right) << std::setw(25)
00168 << std::setfill(' ') << constructor->getElementType()
00169 << std::setiosflags(std::ios::right)
00170
00171 << " type: " << typeid(*constructor).name() << std::endl
00172 << vprDEBUG_FLUSH;
00173 }
00174
00175
00176
00177 bool DeviceFactory::recognizeDevice(jccl::ConfigElementPtr element)
00178 {
00179 return ! (findConstructor(element) == -1);
00180 }
00181
00185 Input* DeviceFactory::loadDevice(jccl::ConfigElementPtr element)
00186 {
00187 vprASSERT(recognizeDevice(element));
00188
00189 int index(findConstructor(element));
00190
00191 Input* new_dev;
00192 DeviceConstructorBase* constructor = mConstructors[index];
00193
00194 vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
00195 << "gadget::DeviceFactory::loadDevice: Loading device: "
00196 << element->getID() << " with: " << typeid(*constructor).name()
00197 << std::endl << vprDEBUG_FLUSH;
00198
00199 new_dev = constructor->createDevice(element);
00200 return new_dev;
00201 }
00202
00203 int DeviceFactory::findConstructor(jccl::ConfigElementPtr element)
00204 {
00205 const std::string element_type(element->getID());
00206
00207 for ( unsigned int i = 0; i < mConstructors.size(); ++i )
00208 {
00209
00210 DeviceConstructorBase* construct = mConstructors[i];
00211 vprASSERT(construct != NULL);
00212
00213 if(construct->getElementType() == element_type)
00214 {
00215 return i;
00216 }
00217 }
00218
00219 return -1;
00220 }
00221
00222
00223 void DeviceFactory::debugDump()
00224 {
00225 vprDEBUG_OutputGuard(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL,
00226 std::string("gadget::DeviceFactory::debugDump\n"),
00227 std::string("------ END DUMP ------\n"));
00228 vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL) << "num constructors:"
00229 << mConstructors.size() << "\n"
00230 << vprDEBUG_FLUSH;
00231
00232 for(unsigned int cNum=0;cNum<mConstructors.size();cNum++)
00233 {
00234 DeviceConstructorBase* dev_constr = mConstructors[cNum];
00235 vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL)
00236 << cNum << ": Constructor:" << (void*)dev_constr
00237 << " type:" << typeid(*dev_constr).name() << "\n" << vprDEBUG_FLUSH;
00238 vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL)
00239 << " recog:" << dev_constr->getElementType() << "\n"
00240 << vprDEBUG_FLUSH;
00241 }
00242 }
00243
00244 }