gamepad.cpp

Go to the documentation of this file.
00001 /*
00002  * gamepad.cpp
00003  *
00004  * Copyright (C) 2008,2009  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  * Provides canonical gamepad input.
00032  * Basically a mapping layer on top of the SDL joystick API.
00033  */
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "gamepad.h"            // always include our own header first
00037 
00038 #include "util/file.h"
00039 
00040 
00041 namespace gamepad {
00042 
00043 
00044 ////////////////////////////////////////////////////////////////////////////////
00045 //
00046 //      static helper methods
00047 //
00048 ////////////////////////////////////////////////////////////////////////////////
00049 
00050 static smart_ptr<i18n::Manager>
00051 createLocaleMgr
00052 (
00053 IN const char * locale,
00054 IN nstream::Folder * dir,
00055 IN const char * fileroot,
00056 IN const char * importantId
00057 )
00058 {
00059         ASSERT(locale, "null");
00060         ASSERT(dir, "null");
00061         ASSERT(fileroot, "null");
00062         ASSERT(importantId, "null");
00063 
00064         // create locale manager (empty)
00065         smart_ptr<i18n::Manager> mgr = i18n::Manager::create(locale);
00066         ASSERT(mgr, "failed to create base locale manager");
00067 
00068         // get the stream manager
00069         smart_ptr<nstream::Manager> fsMgr = dir->getManager();
00070         ASSERT(fsMgr, "Failed to create filesystem named stream manager");
00071 
00072         // construct relative path to config file...
00073         std::string temp, relpath;
00074         appendPath("strings", locale, temp);
00075         appendPath(temp.c_str(), fileroot, relpath);
00076         relpath += ".txt";
00077         //DPRINTF("Relative path: %s", relpath.c_str());
00078 
00079         // open this file
00080         smart_ptr<nstream::Stream> stream;
00081         try {
00082                 stream = nstream::openNamedStream(fsMgr, relpath.c_str());
00083         } catch (std::exception& e) {
00084                 DPRINTF("Exception: %s", e.what());
00085         }
00086 
00087         if (!stream) {
00088                 DPRINTF("Unable to open stream at path: '%s'", relpath.c_str());
00089                 return NULL;    // file does not exist--not in locale?
00090         }
00091 
00092         // read locale information from stream
00093         mgr->parseStrings(stream);
00094 
00095         // verify important id exists
00096         if (!mgr->getString(importantId)) {
00097                 DPRINTF("Found file '%s', but does not contain id '%s'",
00098                     relpath.c_str(), importantId);
00099                 return NULL;    // incomplete or empty stream?
00100         }
00101 
00102         // okay...
00103         return mgr;
00104 }
00105 
00106 
00107 ////////////////////////////////////////////////////////////////////////////////
00108 //
00109 //      public API
00110 //
00111 ////////////////////////////////////////////////////////////////////////////////
00112 
00113 smart_ptr<i18n::Manager>
00114 getConfigLocaleMgr
00115 (
00116 IN const char * locale,
00117 IN const char * defaultLocale,
00118 IN nstream::Folder * dataDirectory
00119 )
00120 {
00121         ASSERT_THROW(locale || defaultLocale,
00122             "At least one locale (primary or backup) should be non-null!");
00123         ASSERT(dataDirectory, "null");
00124 
00125         // save some duplicate lookups
00126         if (!strcmp(locale, defaultLocale)) {
00127                 defaultLocale = NULL;
00128         }
00129 
00130         smart_ptr<i18n::Manager> mgr;
00131         if (locale) {
00132                 DPRINTF("Trying requested locale: '%s'", locale);
00133                 mgr = createLocaleMgr(locale, dataDirectory, "config",
00134                     "buttonPress");
00135         }
00136 
00137         if (!mgr && defaultLocale) {
00138                 DPRINTF("Falling back to default locale: '%s'", defaultLocale);
00139                 mgr = createLocaleMgr(defaultLocale, dataDirectory,
00140                     "config", "buttonPress");
00141         }
00142 
00143         ASSERT_THROW(mgr, "Failed to create locale manager for gamepad " <<
00144             "configuration.  Bad data directory?  " << dataDirectory);
00145 
00146         return mgr;
00147 }
00148 
00149 
00150 
00151 smart_ptr<i18n::Manager>
00152 getTypeLocaleMgr
00153 (
00154 IN const Type * type,
00155 IN const char * locale,
00156 IN const char * defaultLocale,
00157 IN nstream::Folder * dataDirectory
00158 )
00159 {
00160         ASSERT(type, "null");
00161         ASSERT_THROW(locale || defaultLocale,
00162             "Either the primary or backup locale should be non-null!");
00163         ASSERT(dataDirectory, "null");
00164 
00165         // save some duplicate lookups
00166         if (locale && defaultLocale && !strcmp(locale, defaultLocale)) {
00167                 defaultLocale = NULL;
00168         }
00169 
00170         // get the root
00171         const char * fileroot = type->getName();
00172         //DPRINTF("Looking up locale manager for gamepad type '%s'", fileroot);
00173 
00174         smart_ptr<i18n::Manager> mgr;
00175         if (locale) {
00176                 mgr = createLocaleMgr(locale, dataDirectory, fileroot,
00177                     "friendlyName");
00178         }
00179 
00180         if (!mgr && defaultLocale) {
00181                 mgr = createLocaleMgr(defaultLocale, dataDirectory, fileroot,
00182                     "friendlyName");
00183         }
00184 
00185         ASSERT_THROW(mgr, "Failed to create locale manager for gamepad '" <<
00186             fileroot << "'.  Bad data directory?  " << dataDirectory);
00187 
00188         return mgr;
00189 }
00190 
00191 
00192 
00193 };      // gamepad namespace
00194