From 97e8fddd7ed600b46ca58ea924420bb62517d7b9 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Tue, 5 Feb 2013 11:11:25 +0000 Subject: [PATCH] Added --wii command line option, which shows the connect-wiimote dialog at startup. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@12456 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/input/wiimote_manager.cpp | 93 ++++++++++++++----- src/input/wiimote_manager.hpp | 38 +++++++- src/main.cpp | 36 ++++--- .../dialogs/add_device_dialog.cpp | 36 +------ 4 files changed, 135 insertions(+), 68 deletions(-) diff --git a/src/input/wiimote_manager.cpp b/src/input/wiimote_manager.cpp index dbfa83129..eed562907 100644 --- a/src/input/wiimote_manager.cpp +++ b/src/input/wiimote_manager.cpp @@ -19,20 +19,25 @@ #ifdef ENABLE_WIIUSE #include "input/wiimote_manager.hpp" -#include "wiiuse.h" + #include "graphics/irr_driver.hpp" +#include "guiengine/modaldialog.hpp" #include "input/input_manager.hpp" #include "input/device_manager.hpp" #include "utils/string_utils.hpp" +#include "utils/translation.hpp" + +#include "wiiuse.h" WiimoteManager* wiimote_manager; const int WIIMOTE_AXES = 1; // only use one axis, for turning const int WIIMOTE_BUTTONS = 12; // A, B, left, right, top, bottom, 1, 2, (+), (-), home -/** Irrlicht device IDs for the wiimotes start at this value */ -static const int WIIMOTE_START_IRR_ID = 32; +bool WiimoteManager::m_enabled = false; +/** Irrlicht device IDs for the wiimotes start at this value */ +static const int WIIMOTE_START_IRR_ID = 32; static const float JOYSTICK_ABS_MAX_ANGLE = 32767.0f; // ============================ Helper functions ============================ @@ -40,7 +45,7 @@ static const float JOYSTICK_ABS_MAX_ANGLE = 32767.0f; static int wiimoteIdToIrrId(int wiimote_id) { return wiimote_id + WIIMOTE_START_IRR_ID; -} +} // wiimoteIdToIrrId // ----------------------------------------------------------------------------- static void resetIrrEvent(irr::SEvent* event, int irr_id) @@ -51,7 +56,7 @@ static void resetIrrEvent(irr::SEvent* event, int irr_id) event->JoystickEvent.Joystick = irr_id; event->JoystickEvent.POV = 65535; event->JoystickEvent.ButtonStates = 0; -} +} // resetIrrEvent // ----------------------------------------------------------------------------- struct WiimoteAction @@ -59,7 +64,7 @@ struct WiimoteAction int button_id; int wiimote_action_id; const char* wiimote_action_name; -}; +}; // struct WiimoteAction static WiimoteAction wiimote_actions[] = { {0, WIIMOTE_BUTTON_LEFT, "WIIMOTE_BUTTON_LEFT"}, @@ -73,7 +78,7 @@ static WiimoteAction wiimote_actions[] = { {8, WIIMOTE_BUTTON_ONE, "WIIMOTE_BUTTON_ONE"}, {9, WIIMOTE_BUTTON_TWO, "WIIMOTE_BUTTON_TWO"}, {10, WIIMOTE_BUTTON_HOME, "WIIMOTE_BUTTON_HOME"}, -}; +}; // wiimote_actions static int getButtonId(int wiimote_action_id) { @@ -82,7 +87,7 @@ static int getButtonId(int wiimote_action_id) return wiimote_actions[i].button_id; assert(false && "shouldn't happen"); return -1; -} +} // getButtonId // ----------------------------------------------------------------------------- static void setWiimoteBindings(GamepadConfig* gamepad_config) @@ -104,7 +109,7 @@ static void setWiimoteBindings(GamepadConfig* gamepad_config) gamepad_config->setBinding(PA_MENU_RIGHT, Input::IT_STICKBUTTON, getButtonId(WIIMOTE_BUTTON_DOWN)); gamepad_config->setBinding(PA_MENU_SELECT, Input::IT_STICKBUTTON, getButtonId(WIIMOTE_BUTTON_TWO)); gamepad_config->setBinding(PA_MENU_CANCEL, Input::IT_STICKBUTTON, getButtonId(WIIMOTE_BUTTON_ONE)); -} +} // setWiimoteBindings // ============================ Wiimote device implementation ============================ Wiimote::Wiimote() @@ -116,12 +121,13 @@ Wiimote::Wiimote() m_connected = false; pthread_mutex_init(&m_event_mutex, NULL); -} +} // Wiimote +// ---------------------------------------------------------------------------- Wiimote::~Wiimote() { pthread_mutex_destroy(&m_event_mutex); -} +} // ~Wiimote // ----------------------------------------------------------------------------- /** Resets internal state and creates the corresponding gamepad device */ @@ -146,7 +152,7 @@ void Wiimote::init(wiimote_t* wiimote_handle, int wiimote_id, GamepadConfig* gam WIIMOTE_BUTTONS, gamepad_config ); device_manager->addGamepad(m_gamepad_device); -} +} // init // ----------------------------------------------------------------------------- /** Called from the update thread: updates the Irrlicht event from the wiimote state */ @@ -218,8 +224,9 @@ void Wiimote::updateIrrEvent() } */ pthread_mutex_unlock(&m_event_mutex); -} +} // updateIrrEvent +// ---------------------------------------------------------------------------- irr::SEvent Wiimote::getIrrEvent() { irr::SEvent event; @@ -229,7 +236,7 @@ irr::SEvent Wiimote::getIrrEvent() pthread_mutex_unlock(&m_event_mutex); return event; -} +} // getIrrEvent // ============================ Wiimote manager implementation ============================ WiimoteManager::WiimoteManager() @@ -238,13 +245,13 @@ WiimoteManager::WiimoteManager() m_nb_wiimotes = 0; m_shut = false; -} +} // WiimoteManager // ----------------------------------------------------------------------------- WiimoteManager::~WiimoteManager() { cleanup(); -} +} // ~WiimoteManager // ----------------------------------------------------------------------------- /** @@ -309,7 +316,7 @@ void WiimoteManager::launchDetection(int timeout) m_shut = false; pthread_create(&m_thread, NULL, &threadFuncWrapper, this); -} +} // launchDetection // ----------------------------------------------------------------------------- void WiimoteManager::cleanup() @@ -339,7 +346,7 @@ void WiimoteManager::cleanup() m_all_wiimote_handles = NULL; m_nb_wiimotes = 0; m_shut = false; -} +} // cleanup // ----------------------------------------------------------------------------- void WiimoteManager::update() @@ -352,7 +359,7 @@ void WiimoteManager::update() input_manager->input(event); } } -} +} // update // ----------------------------------------------------------------------------- /** Thread update method - wiimotes state is updated in another thread to avoid latency problems */ @@ -420,12 +427,56 @@ void WiimoteManager::threadFunc() irr_driver->getDevice()->sleep(1); // 'cause come on, the whole CPU is not ours :) } // end while -} +} // threadFunc +// ---------------------------------------------------------------------------- +/** This is the start function of a separate thread used to poll the wiimotes. + * It receives the wiimote manager as parameter when the thread is created. + * \param data Pointer to the wiimote manager. + */ void* WiimoteManager::threadFuncWrapper(void *data) { ((WiimoteManager*)data)->threadFunc(); return NULL; -} +} // threadFuncWrapper +// ---------------------------------------------------------------------------- +/** Shows a simple popup menu asking the user to connect all wiimotes. + */ +int WiimoteManager::askUserToConnectWiimotes() +{ + new MessageDialog( + _("Press the buttons 1+2 simultaneously on your wiimote to put " + "it in discovery mode, then click on OK."), + MessageDialog::MESSAGE_DIALOG_CONFIRM, + new WiimoteDialogListener(), true); + + return getNbWiimotes(); +} // askUserToConnectWiimotes + +// ============================================================================ +/** Calles when the user clicks on OK, i.e. all wiimotes are in discovery + * mode. + */ +void WiimoteManager::WiimoteDialogListener::onConfirm() OVERRIDE +{ + GUIEngine::ModalDialog::dismiss(); + + wiimote_manager->launchDetection(5); + + int nb_wiimotes = wiimote_manager->getNbWiimotes(); + if(nb_wiimotes > 0) + { + core::stringw msg = StringUtils::insertValues( + _("Found %d wiimote(s)"), + core::stringw(nb_wiimotes)); + + new MessageDialog( msg ); + + } + else + { + new MessageDialog( _("Could not detect any wiimote :/") ); + } +} // WiimoteDialogListeneronConfirm #endif // ENABLE_WIIUSE diff --git a/src/input/wiimote_manager.hpp b/src/input/wiimote_manager.hpp index a90a22b7b..5c1b3ff3a 100644 --- a/src/input/wiimote_manager.hpp +++ b/src/input/wiimote_manager.hpp @@ -21,17 +21,23 @@ #ifdef ENABLE_WIIUSE -#include +#include "states_screens/dialogs/message_dialog.hpp" +#include "utils/cpp2011.h" #include "IEventReceiver.h" +#include + extern const int WIIMOTE_AXES; extern const int WIIMOTE_BUTTONS; #define MAX_WIIMOTES 4 struct wiimote_t; -class GamePadDevice; class GamepadConfig; +class GamePadDevice; +class WiimoteManager; + +extern WiimoteManager* wiimote_manager; /** Wiimote device class */ class Wiimote @@ -58,7 +64,7 @@ private: public: Wiimote(); ~Wiimote(); - + /** Resets internal state and creates the corresponding gamepad device */ void init(wiimote_t* wiimote_handle, int wiimote_id, GamepadConfig* gamepad_config); @@ -95,10 +101,19 @@ private: /** Shut the update thread? */ bool m_shut; + /** True if wii is enabled via command line option. */ + static bool m_enabled; + public: WiimoteManager(); ~WiimoteManager(); + /** Sets the wiimote to be enabled. */ + static void enable() { m_enabled = true; } + + /** Returns if the wii was enabled on the command line. */ + static bool isEnabled() { return m_enabled; } + void launchDetection(int timeout); void update(); void cleanup(); @@ -109,9 +124,24 @@ private: /** Wiimotes update thread */ void threadFunc(); static void* threadFuncWrapper(void* data); + +public: + /** A simple listener to allow the user to connect wiimotes. It + * will display a feedback windows (# wiimotes connected or 'no wiimotes + * found'). + */ + class WiimoteDialogListener : public MessageDialog::IConfirmDialogListener + { + public: + virtual void onConfirm() OVERRIDE; + }; // class WiimoteDialoListener + + /** Shows a dialog allowing the user to connect wiimotes. + * \return Number of wiimotes connected. + */ + int askUserToConnectWiimotes(); }; -extern WiimoteManager* wiimote_manager; #endif diff --git a/src/main.cpp b/src/main.cpp index 64581a6cc..27cf79142 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -567,8 +567,8 @@ int handleCmdLinePreliminary(int argc, char **argv) } else { - Log::fatal("main", "Error: --screensize argument must be " - "given as WIDTHxHEIGHT\n"); + Log::fatal("main", "Error: --screensize argument must be " + "given as WIDTHxHEIGHT\n"); exit(EXIT_FAILURE); } } @@ -648,7 +648,7 @@ int handleCmdLine(int argc, char **argv) const KartProperties *km = kart_properties_manager->getKartById(i); Log::info("main", "%s:\t%swidth: %f length: %f height: %f " - "mesh-buffer count %d\n", + "mesh-buffer count %d\n", km->getIdent().c_str(), (km->getIdent().size()<7) ? "\t" : "", km->getMasterKartModel().getWidth(), @@ -804,7 +804,7 @@ int handleCmdLine(int argc, char **argv) break; default: Log::warn("main", "Invalid race type '%d' - ignored.\n", - atoi(argv[i+1])); + atoi(argv[i+1])); } i++; } @@ -828,7 +828,7 @@ int handleCmdLine(int argc, char **argv) if (t == NULL) { Log::warn("main", "Can't find track named <%s>\n", - argv[i+1]); + argv[i+1]); } else if (t->isArena()) { @@ -870,7 +870,7 @@ int handleCmdLine(int argc, char **argv) if(UserConfigParams::m_num_karts > stk_config->m_max_karts) { Log::warn("main", - "Number of karts reset to maximum number %d\n", + "Number of karts reset to maximum number %d\n", stk_config->m_max_karts); UserConfigParams::m_num_karts = stk_config->m_max_karts; } @@ -1009,6 +1009,12 @@ int handleCmdLine(int argc, char **argv) ',')); i++; } +#ifdef ENABLE_WIIUSE + else if( !strcmp(argv[i], "--wii")) + { + WiimoteManager::enable(); + } +#endif // these commands are already processed in handleCmdLinePreliminary, // but repeat this just so that we don't get error messages about // unknown commands @@ -1245,8 +1251,8 @@ int main(int argc, char *argv[] ) std::string logoutfile = file_manager->getLogFile("stdout.log"); std::string logerrfile = file_manager->getLogFile("stderr.log"); Log::verbose("main", "Error messages and other text output will " - "be logged to %s and %s\n", logoutfile.c_str(), - logerrfile.c_str()); + "be logged to %s and %s\n", logoutfile.c_str(), + logerrfile.c_str()); if(freopen (logoutfile.c_str(),"w",stdout)!=stdout) { Log::error("main", "Can not open log file '%s'. Writing to " @@ -1255,7 +1261,7 @@ int main(int argc, char *argv[] ) if(freopen (logerrfile.c_str(),"w",stderr)!=stderr) { Log::error("main", "Can not open log file '%s'. Writing to " - "stderr instead.\n", logerrfile.c_str()); + "stderr instead.\n", logerrfile.c_str()); } } @@ -1322,10 +1328,16 @@ int main(int argc, char *argv[] ) } } - if(!UserConfigParams::m_no_start_screen) { StateManager::get()->pushScreen(StoryModeLobbyScreen::getInstance()); +#ifdef ENABLE_WIIUSE + // Show a dialog to allow connection of wiimotes. */ + if(WiimoteManager::isEnabled()) + { + wiimote_manager->askUserToConnectWiimotes(); + } +#endif if(UserConfigParams::m_internet_status == INetworkHttp::IPERM_NOT_ASKED) { @@ -1383,8 +1395,8 @@ int main(int argc, char *argv[] ) if (kart_properties_manager->getKart(UserConfigParams::m_default_kart) == NULL) { Log::warn("main", "Kart '%s' is unknown so will use the " - "default kart.\n", - UserConfigParams::m_default_kart.c_str()); + "default kart.\n", + UserConfigParams::m_default_kart.c_str()); race_manager->setLocalKartInfo(0, UserConfigParams::m_default_kart.getDefaultValue()); } else diff --git a/src/states_screens/dialogs/add_device_dialog.cpp b/src/states_screens/dialogs/add_device_dialog.cpp index 5f3b9c824..b1753c81c 100644 --- a/src/states_screens/dialogs/add_device_dialog.cpp +++ b/src/states_screens/dialogs/add_device_dialog.cpp @@ -152,34 +152,6 @@ void AddDeviceDialog::onEnterPressedInternal() } // onEnterPressedInternal // ---------------------------------------------------------------------------- -#ifdef ENABLE_WIIUSE -class WiimoteDialogListener : public MessageDialog::IConfirmDialogListener -{ -public: - virtual void onConfirm() OVERRIDE - { - ModalDialog::dismiss(); - - wiimote_manager->launchDetection(5); - - int nb_wiimotes = wiimote_manager->getNbWiimotes(); - if(nb_wiimotes > 0) - { - core::stringw msg = StringUtils::insertValues( - _("Found %d wiimote(s)"), - core::stringw(nb_wiimotes)); - - new MessageDialog( msg ); - - ((OptionsScreenInput*)GUIEngine::getCurrentScreen())->rebuildDeviceList(); - } - else - { - new MessageDialog( _("Could not detect any wiimote :/") ); - } - } -}; -#endif // ---------------------------------------------------------------------------- GUIEngine::EventPropagation AddDeviceDialog::processEvent @@ -204,9 +176,11 @@ GUIEngine::EventPropagation AddDeviceDialog::processEvent #ifdef ENABLE_WIIUSE else if (eventSource == "addwiimote") { - new MessageDialog( _("Press the buttons 1+2 simultaneously on your wiimote to put " - "it in discovery mode, then click on OK."), - MessageDialog::MESSAGE_DIALOG_CONFIRM, new WiimoteDialogListener(), true); + // Remove the previous modal dialog to avoid a warning + GUIEngine::ModalDialog::dismiss(); + if(wiimote_manager->askUserToConnectWiimotes() > 0) + ((OptionsScreenInput*)GUIEngine::getCurrentScreen())->rebuildDeviceList(); + return GUIEngine::EVENT_BLOCK; } #endif