bool InputManager::configureInputManager(jccl::ConfigElementPtr element)
{
   bool have_bad_elt = (element->getID() != std::string("input_manager"));
   vprASSERT(!have_bad_elt);
   if(have_bad_elt)
   {
      return false;
   }

   bool ret_val = false;

   vpr::DebugOutputGuard dbg_output(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL,
                                    std::string("Handling input_manager element:\n"),
                                    std::string("-- end state -- \n"));

   // Keep this up to date with the version of the element definition we're
   // expecting to handle.
   const unsigned int cur_version(2);

   // If the element version is less than cur_version, we will not try to
   // proceed.  Instead, we'll print an error message and return false so
   // that the Config Manager knows this element wasn't consumed.
   if ( element->getVersion() < cur_version )
   {
      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
         << clrOutBOLD(clrRED, "ERROR")
         << ": [gadget::InputManager::configureInputManager()] Element named '"
         << element->getName() << "'" << std::endl << vprDEBUG_FLUSH;
      vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
         << "is version " << element->getVersion()
         << ", but we require at least version " << cur_version
         << std::endl << vprDEBUG_FLUSH;
      vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
         << "Ignoring this element and moving on." << std::endl
         << vprDEBUG_FLUSH;
      ret_val = false;
   }
   // We got the right version of the config element and can proceed.
   else
   {
      const std::string driver_path_prop_name("driver_path");
      const int path_count(element->getNum(driver_path_prop_name));
      std::vector<fs::path> search_path(path_count);

      for ( unsigned int i = 0; i < search_path.size(); ++i )
      {
         std::string temp_str =
            vpr::replaceEnvVars(element->getProperty<std::string>(driver_path_prop_name, i));

         try
         {
            search_path[i] = fs::path(temp_str, fs::native);
         }
         catch(fs::filesystem_error& fsEx)
         {
            vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
               << clrOutNORM(clrRED, "ERROR")
               << ": [gadget::InputManager::configureInputManager()] File "
               << "system exception caught while converting\n"
               << vprDEBUG_FLUSH;
            vprDEBUG_NEXT(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
               << "'" << temp_str << "'\n" << vprDEBUG_FLUSH;
            vprDEBUG_NEXT(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
               << "to a Boost.Filesystem path.\n" << vprDEBUG_FLUSH;
            vprDEBUG_NEXT(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
               << fsEx.what() << std::endl << vprDEBUG_FLUSH;
         }
      }

      // Append a default driver search path to search_path.
      const fs::path default_search_dir =
         gadget::getDefaultPluginRoot() / std::string("drivers");

      vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_VERB_LVL)
         << "[gadget::InputManager::configureInputManager()] Appending "
         << "default search path '"
         << default_search_dir.native_directory_string() << "'\n"
         << vprDEBUG_FLUSH;

#if defined(GADGET_DEBUG)
      // For a debug build, search in the debug subdirectory of
      // default_search_dir before looking in default_search_dir.
      search_path.push_back(default_search_dir / std::string("debug"));
#endif

      search_path.push_back(default_search_dir);

      // --- Load device driver dsos -- //
      // - Load individual drivers
      const std::string driver_prop_name("driver");
      const std::string get_version_func("getGadgeteerVersion");
      const std::string driver_init_func("initDevice");

      int driver_count = element->getNum(driver_prop_name);
      std::string driver_dso_name;

      for ( int i = 0; i < driver_count; ++i )
      {
         driver_dso_name =
            element->getProperty<std::string>(driver_prop_name, i);

         if ( ! driver_dso_name.empty() )
         {
            vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
               << "[gadget::InputManager::configureInputManager()] Loading "
               << "driver DSO '" << driver_dso_name << "'\n" << vprDEBUG_FLUSH;

            vpr::LibraryPtr dso = vpr::LibraryLoader::findDSO(driver_dso_name,
                                                              search_path);

            if ( dso.get() != NULL )
            {
               try
               {
                  VersionCheckCallable version_functor;
                  vpr::LibraryLoader::callEntryPoint(dso, get_version_func,
                                                     version_functor);

                  DriverInitCallable init_functor(this);
                  vpr::LibraryLoader::callEntryPoint(dso, driver_init_func,
                                                     init_functor);

                  mLoadedDrivers.push_back(dso);
               }
               catch (gadget::PluginVersionException& ex)
               {
                  vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                     << clrOutBOLD(clrRED, "ERROR")
                     << ": Version mismatch while loading driver DSO '"
                     << driver_dso_name << "'\n" << vprDEBUG_FLUSH;
                  vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                     << "This driver will not be usable.\n"
                     << vprDEBUG_FLUSH;
                  vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                     << ex.getExtendedDescription() << std::endl
                     << vprDEBUG_FLUSH;
               }
               catch (vpr::Exception& ex)
               {
                  vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                     << clrOutBOLD(clrRED, "ERROR")
                     << ": Failed to load driver DSO '"
                     << driver_dso_name << "'\n" << vprDEBUG_FLUSH;
                  vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                     << "This driver will not be usable.\n" << vprDEBUG_FLUSH;
                  vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                     << ex.what() << std::endl << vprDEBUG_FLUSH;
               }
            }
            else
            {
               vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                  << clrOutBOLD(clrRED, "ERROR")
                  << ": Failed to find driver DSO '" << driver_dso_name
                  << "'\n" << vprDEBUG_FLUSH;
            }
         }
      }

      // - Load driver directory
      const std::string dir_prop_name("driver_scan_path");
      int dir_count = element->getNum(dir_prop_name);
      std::string driver_dir;

#if defined(VPR_OS_Windows)
      const std::string driver_ext("dll");
#elif defined(VPR_OS_Darwin)
      const std::string driver_ext("dylib");
#else
      const std::string driver_ext("so");
#endif

      for ( int i = 0; i < dir_count; ++i )
      {
         driver_dir = vpr::replaceEnvVars(element->getProperty<std::string>(dir_prop_name, i));

         // The vpr::LibraryFinder will throw an exception if driver_dir is
         // (somehow) an invalid path.
         try
         {
            fs::path drv_path(driver_dir, fs::native);

            if ( fs::exists(drv_path) )
            {
               vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                  << "[gadget::InputManager::configureInputManager()] "
                  << "Searching for driver DSOs in '" << driver_dir << "'\n"
                  << vprDEBUG_FLUSH;

               vpr::LibraryFinder finder(driver_dir, driver_ext);
               vpr::LibraryFinder::LibraryList libs = finder.getLibraries();
               VersionCheckCallable version_functor;
               DriverInitCallable init_functor(this);

               for ( vpr::LibraryFinder::LibraryList::iterator lib = libs.begin();
                     lib != libs.end();
                     ++lib )
               {
                  try
                  {
                     vpr::LibraryLoader::callEntryPoint(*lib, get_version_func,
                                                        version_functor);
                     vpr::LibraryLoader::callEntryPoint(*lib, driver_init_func,
                                                        init_functor);
                     mLoadedDrivers.push_back(*lib);
                  }
                  catch (vpr::Exception& ex)
                  {
                     vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                        << clrOutBOLD(clrRED, "ERROR")
                        << ": Failed to load driver DSO '"
                        << (*lib)->getName() << "'\n" << vprDEBUG_FLUSH;
                     vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                        << "This driver will not be usable.\n"
                        << vprDEBUG_FLUSH;
                     vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                        << ex.what() << std::endl << vprDEBUG_FLUSH;
                  }
               }
            }
            else
            {
               vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
                  << clrOutBOLD(clrYELLOW, "WARNING")
                  << ": [gadget::InputManager::configureInputManager()] "
                  << "Invalid directory for driver DSOs: " << driver_dir
                  << std::endl << vprDEBUG_FLUSH;
            }
         }
         catch(fs::filesystem_error& fsEx)
         {
            vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
               << clrOutNORM(clrRED, "ERROR")
               << ": [gadget::InputManager::configureInputManager()] File "
               << "system exception caught!\n" << vprDEBUG_FLUSH;
            vprDEBUG_NEXT(gadgetDBG_INPUT_MGR, vprDBG_CRITICAL_LVL)
               << fsEx.what() << std::endl << vprDEBUG_FLUSH;
         }
      }

      ret_val = true;
   }

   return ret_val;
}
Esempio n. 2
0
void Organism::init() {
	detail::Trees::iterator itr = trees.begin();
	for(; itr != trees.end(); itr++)
		trees.modify(itr, init_functor());
}