From 34e281d1b72a6abeecde2afee61159e2d2ada3d7 Mon Sep 17 00:00:00 2001 From: stephena Date: Mon, 9 Jan 2006 16:50:01 +0000 Subject: [PATCH] First pass at adding SDL joystick hat support to the EventHandler and associated classes. git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@945 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba --- stella/src/emucore/EventHandler.cxx | 141 ++++++++++++++++++++++++++-- stella/src/emucore/EventHandler.hxx | 46 ++++++++- stella/src/emucore/OSystem.cxx | 7 +- stella/src/emucore/OSystem.hxx | 10 +- stella/src/emucore/Settings.cxx | 3 +- stella/src/gui/Dialog.cxx | 10 +- stella/src/gui/Dialog.hxx | 5 +- stella/src/gui/DialogContainer.cxx | 9 +- stella/src/gui/DialogContainer.hxx | 13 ++- stella/src/gui/Widget.hxx | 5 +- 10 files changed, 225 insertions(+), 24 deletions(-) diff --git a/stella/src/emucore/EventHandler.cxx b/stella/src/emucore/EventHandler.cxx index 17ecb2d39..c8569fff8 100644 --- a/stella/src/emucore/EventHandler.cxx +++ b/stella/src/emucore/EventHandler.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: EventHandler.cxx,v 1.143 2006-01-09 01:13:25 stephena Exp $ +// $Id: EventHandler.cxx,v 1.144 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #include @@ -71,7 +71,7 @@ EventHandler::EventHandler(OSystem* osystem) myUseLauncherFlag(false), myPaddleMode(0) { - int i, j; + int i, j, k; // Add this eventhandler object to the OSystem myOSystem->attach(this); @@ -99,6 +99,12 @@ EventHandler::EventHandler(OSystem* osystem) for(j = 0; j < kNumJoyAxis; ++j) myJoyAxisTable[i][j][0] = myJoyAxisTable[i][j][1] = Event::NoType; + // Erase the joystick hat mapping array + for(i = 0; i < kNumJoysticks; ++i) + for(j = 0; j < kNumJoyHats; ++j) + for(k = 0; k < 4; ++k) + myJoyHatTable[i][j][k] = Event::NoType; + // Erase the Message array for(i = 0; i < Event::LastType; ++i) ourMessageTable[i] = ""; @@ -117,6 +123,7 @@ EventHandler::EventHandler(OSystem* osystem) setKeymap(); setJoymap(); setJoyAxisMap(); + setJoyHatMap(); setActionMappings(); myGrabMouseFlag = myOSystem->settings().getBool("grabmouse"); @@ -263,9 +270,12 @@ void EventHandler::setupJoysticks() ourJoysticks[i].name = SDL_JoystickName(i); if(showinfo) - cout << " " << i << ": " << ourJoysticks[i].name - << " with " << SDL_JoystickNumButtons(ourJoysticks[i].stick) - << " buttons" << endl; + cout << " " << i << ": " << ourJoysticks[i].name << " with " + << SDL_JoystickNumAxes(ourJoysticks[i].stick) << " axes, " + << SDL_JoystickNumHats(ourJoysticks[i].stick) << " hats, " + << SDL_JoystickNumBalls(ourJoysticks[i].stick) << " balls, " + << SDL_JoystickNumButtons(ourJoysticks[i].stick) << " buttons" + << endl; } } if(showinfo) @@ -631,7 +641,7 @@ void EventHandler::poll(uInt32 time) { case JT_REGULAR: { - if(event.jbutton.button >= kNumJoyButtons-4) + if(event.jbutton.button >= kNumJoyButtons) return; int stick = event.jbutton.which; @@ -712,7 +722,6 @@ void EventHandler::poll(uInt32 time) int axis = event.jaxis.axis; int value = event.jaxis.value; - // Handle emulation of mouse using the joystick if(myState == S_EMULATE) handleJoyAxisEvent(stick, axis, value); else if(myOverlay != NULL) @@ -764,6 +773,22 @@ void EventHandler::poll(uInt32 time) } break; // SDL_JOYAXISMOTION } + + case SDL_JOYHATMOTION: + { + int stick = event.jhat.which; + int hat = event.jhat.hat; + int value = event.jhat.value; + + if(stick >= kNumJoysticks || hat >= kNumJoyHats) + break; + + if(myState == S_EMULATE) + handleJoyHatEvent(stick, hat, value); + else if(myOverlay != NULL) + myOverlay->handleJoyHatEvent(stick, hat, value); + break; // SDL_JOYHATMOTION + } #endif // JOYSTICK_SUPPORT } } @@ -922,6 +947,12 @@ void EventHandler::handleJoyAxisEvent(int stick, int axis, int value) } } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void EventHandler::handleJoyHatEvent(int stick, int hat, int value) +{ +cerr << "stick = " << stick << ", hat = " << hat << ", value = " << value << endl; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void EventHandler::handleEvent(Event::Type event, int state) { @@ -1275,6 +1306,8 @@ void EventHandler::setActionMappings() } } +// FIXME - add joy hat labeling + // There are some keys which are hardcoded. These should be represented too. string prepend = ""; if(event == Event::Quit) @@ -1348,6 +1381,25 @@ void EventHandler::setJoyAxisMap() setDefaultJoyAxisMap(); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void EventHandler::setJoyHatMap() +{ + string list = myOSystem->settings().getString("joyhatmap"); + IntArray map; + + if(isValidList(list, map, kNumJoysticks*kNumJoyHats*4)) + { + // Fill the joyhatmap table with events + int idx = 0; + for(int i = 0; i < kNumJoysticks; ++i) + for(int j = 0; j < kNumJoyHats; ++j) + for(int k = 0; k < 4; ++k) + myJoyHatTable[i][j][k] = (Event::Type) map[idx++]; + } + else + setDefaultJoyHatMap(); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void EventHandler::addKeyMapping(Event::Type event, int key) { @@ -1414,6 +1466,44 @@ void EventHandler::addJoyAxisMapping(Event::Type event, int stick, int axis, setActionMappings(); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void EventHandler::setDefaultJoyHatMapping(Event::Type event, int stick, + int hat, int value) +{ + if(stick >= 0 && stick < kNumJoysticks && + hat >= 0 && hat < kNumJoyHats && + event >= 0 && event < Event::LastType) + { +cerr << "add mapping for stick = " << stick << ", hat = " << hat << ", value = " << value << endl; +/* + // This confusing code is because each axis has two associated values, + // but analog events only affect one of the axis. + if(eventIsAnalog(event)) + myJoyAxisTable[stick][axis][0] = myJoyAxisTable[stick][axis][1] = event; + else + { + // Otherwise, turn off the analog event(s) for this axis + if(eventIsAnalog(myJoyAxisTable[stick][axis][0])) + myJoyAxisTable[stick][axis][0] = Event::NoType; + if(eventIsAnalog(myJoyAxisTable[stick][axis][1])) + myJoyAxisTable[stick][axis][1] = Event::NoType; + + myJoyAxisTable[stick][axis][(value > 0)] = event; + } +*/ + } +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void EventHandler::addJoyHatMapping(Event::Type event, int stick, int hat, + int value) +{ + setDefaultJoyHatMapping(event, stick, hat, value); + + saveJoyHatMapping(); + setActionMappings(); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void EventHandler::eraseMapping(Event::Type event) { @@ -1440,6 +1530,14 @@ void EventHandler::eraseMapping(Event::Type event) myJoyAxisTable[i][j][k] = Event::NoType; saveJoyAxisMapping(); + // Erase the JoyHatEvent array + for(i = 0; i < kNumJoysticks; ++i) + for(j = 0; j < kNumJoyHats; ++j) + for(k = 0; k < 4; ++k) + if(myJoyHatTable[i][j][k] == event) + myJoyHatTable[i][j][k] = Event::NoType; + saveJoyHatMapping(); + setActionMappings(); } @@ -1449,6 +1547,7 @@ void EventHandler::setDefaultMapping() setDefaultKeymap(); setDefaultJoymap(); setDefaultJoyAxisMap(); + setDefaultJoyHatMap(); setActionMappings(); } @@ -1558,6 +1657,19 @@ void EventHandler::setDefaultJoyAxisMap() saveJoyAxisMapping(); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void EventHandler::setDefaultJoyHatMap() +{ + // Erase all mappings + for(int i = 0; i < kNumJoysticks; ++i) + for(int j = 0; j < kNumJoyHats; ++j) + for(int k = 0; k < 4; ++k) + myJoyHatTable[i][j][k] = Event::NoType; + + myOSystem->setDefaultJoyHatMap(); + saveJoyHatMapping(); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void EventHandler::saveKeyMapping() { @@ -1600,6 +1712,21 @@ void EventHandler::saveJoyAxisMapping() myOSystem->settings().setString("joyaxismap", buf.str()); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void EventHandler::saveJoyHatMapping() +{ + // Iterate through the joyhatmap table and create a colon-separated list + // Prepend the event count, so we can check it on next load + ostringstream buf; + buf << Event::LastType << ":"; + for(int i = 0; i < kNumJoysticks; ++i) + for(int j = 0; j < kNumJoyHats; ++j) + for(int k = 0; k < 4; ++k) + buf << myJoyHatTable[i][j][k] << ":"; + + myOSystem->settings().setString("joyhatmap", buf.str()); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool EventHandler::isValidList(string& list, IntArray& map, uInt32 length) { diff --git a/stella/src/emucore/EventHandler.hxx b/stella/src/emucore/EventHandler.hxx index ee7282f1b..0ec6d3614 100644 --- a/stella/src/emucore/EventHandler.hxx +++ b/stella/src/emucore/EventHandler.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: EventHandler.hxx,v 1.73 2006-01-09 00:42:12 stephena Exp $ +// $Id: EventHandler.hxx,v 1.74 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #ifndef EVENTHANDLER_HXX @@ -84,7 +84,8 @@ enum { enum { kNumJoysticks = 8, kNumJoyButtons = 24, - kNumJoyAxis = 16 + kNumJoyAxis = 16, + kNumJoyHats = 16 }; enum JoyType { @@ -122,7 +123,7 @@ struct JoyMouse { mapping can take place. @author Stephen Anthony - @version $Id: EventHandler.hxx,v 1.73 2006-01-09 00:42:12 stephena Exp $ + @version $Id: EventHandler.hxx,v 1.74 2006-01-09 16:50:01 stephena Exp $ */ class EventHandler { @@ -189,8 +190,17 @@ class EventHandler @param axis The joystick axis @param value The value on the given axis */ - void setDefaultJoyAxisMapping(Event::Type event, int stick, int axis, - int value); + void setDefaultJoyAxisMapping(Event::Type event, int stick, int axis, int value); + + /** + Set the default for a joystick hat to the given event + + @param event The event we are assigning + @param stick The joystick number + @param axis The joystick axis + @param value The value on the given axis + */ + void setDefaultJoyHatMapping(Event::Type event, int stick, int hat, int value); /** Returns the current state of the EventHandler @@ -345,6 +355,17 @@ class EventHandler */ void addJoyAxisMapping(Event::Type event, int stick, int axis, int value); + /** + Bind a joystick hat direction to an event/action and regenerate + the mapping array(s) + + @param event The event we are remapping + @param stick The joystick number + @param axis The joystick hat + @param value The value on the given hat + */ + void addJoyHatMapping(Event::Type event, int stick, int hat, int value); + /** Erase the specified mapping @@ -389,6 +410,15 @@ class EventHandler */ void handleJoyAxisEvent(int stick, int axis, int value); + /** + Send a joystick hat event to the handler + + @param stick The joystick number + @param axis The joystick hat + @param value The value on the given hat + */ + void handleJoyHatEvent(int stick, int hat, int value); + /** Detects and changes the eventhandler state @@ -405,12 +435,15 @@ class EventHandler void setKeymap(); void setJoymap(); void setJoyAxisMap(); + void setJoyHatMap(); void setDefaultKeymap(); void setDefaultJoymap(); void setDefaultJoyAxisMap(); + void setDefaultJoyHatMap(); void saveKeyMapping(); void saveJoyMapping(); void saveJoyAxisMapping(); + void saveJoyHatMapping(); /** Tests if a mapping list is valid, both by length and by event count. @@ -459,6 +492,9 @@ class EventHandler // Array of joystick axis events Event::Type myJoyAxisTable[kNumJoysticks][kNumJoyAxis][2]; + // Array of joystick hat events (we don't record diagonals) + Event::Type myJoyHatTable[kNumJoysticks][kNumJoyHats][4]; + // Array of messages for each Event string ourMessageTable[Event::LastType]; diff --git a/stella/src/emucore/OSystem.cxx b/stella/src/emucore/OSystem.cxx index 4558f85d4..69781ff80 100644 --- a/stella/src/emucore/OSystem.cxx +++ b/stella/src/emucore/OSystem.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: OSystem.cxx,v 1.55 2006-01-08 20:55:53 stephena Exp $ +// $Id: OSystem.cxx,v 1.56 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #include @@ -482,6 +482,11 @@ void OSystem::setDefaultJoyAxisMap() myEventHandler->setDefaultJoyAxisMapping(Event::JoystickOneDown, 1, 1, 1); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void OSystem::setDefaultJoyHatMap() +{ +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OSystem::OSystem(const OSystem& osystem) { diff --git a/stella/src/emucore/OSystem.hxx b/stella/src/emucore/OSystem.hxx index f1b7b0740..613581af5 100644 --- a/stella/src/emucore/OSystem.hxx +++ b/stella/src/emucore/OSystem.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: OSystem.hxx,v 1.35 2006-01-08 13:55:03 stephena Exp $ +// $Id: OSystem.hxx,v 1.36 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #ifndef OSYSTEM_HXX @@ -44,7 +44,7 @@ class CheatManager; other objects belong. @author Stephen Anthony - @version $Id: OSystem.hxx,v 1.35 2006-01-08 13:55:03 stephena Exp $ + @version $Id: OSystem.hxx,v 1.36 2006-01-09 16:50:01 stephena Exp $ */ class OSystem { @@ -339,6 +339,12 @@ class OSystem */ virtual void setDefaultJoyAxisMap(); + /** + This method determines the default mapping of joystick hats to + Stella events for a specific system/platform. + */ + virtual void setDefaultJoyHatMap(); + protected: /** Set the base directory for all Stella files diff --git a/stella/src/emucore/Settings.cxx b/stella/src/emucore/Settings.cxx index af6f74968..0c9035164 100644 --- a/stella/src/emucore/Settings.cxx +++ b/stella/src/emucore/Settings.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Settings.cxx,v 1.72 2006-01-08 13:55:03 stephena Exp $ +// $Id: Settings.cxx,v 1.73 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #include @@ -62,6 +62,7 @@ Settings::Settings(OSystem* osystem) set("keymap", ""); set("joymap", ""); set("joyaxismap", ""); + set("joyhatmap", ""); set("paddle", "0"); set("sa1", "left"); set("sa2", "right"); diff --git a/stella/src/gui/Dialog.cxx b/stella/src/gui/Dialog.cxx index 9b217cf3f..01d265694 100644 --- a/stella/src/gui/Dialog.cxx +++ b/stella/src/gui/Dialog.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Dialog.cxx,v 1.40 2006-01-08 02:28:03 stephena Exp $ +// $Id: Dialog.cxx,v 1.41 2006-01-09 16:50:01 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -415,6 +415,14 @@ void Dialog::handleJoyAxis(int stick, int axis, int value) _focusedWidget->handleJoyAxis(stick, axis, value); } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void Dialog::handleJoyHat(int stick, int hat, int value) +{ + // Focused widget receives joystick events + if(_focusedWidget) + _focusedWidget->handleJoyHat(stick, hat, value); +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Dialog::handleCommand(CommandSender* sender, int cmd, int data, int id) { diff --git a/stella/src/gui/Dialog.hxx b/stella/src/gui/Dialog.hxx index f18d08be1..1de05b21c 100644 --- a/stella/src/gui/Dialog.hxx +++ b/stella/src/gui/Dialog.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Dialog.hxx,v 1.25 2006-01-08 02:28:03 stephena Exp $ +// $Id: Dialog.hxx,v 1.26 2006-01-09 16:50:01 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -36,7 +36,7 @@ class TabWidget; This is the base class for all dialog boxes. @author Stephen Anthony - @version $Id: Dialog.hxx,v 1.25 2006-01-08 02:28:03 stephena Exp $ + @version $Id: Dialog.hxx,v 1.26 2006-01-09 16:50:01 stephena Exp $ */ class Dialog : public GuiObject { @@ -82,6 +82,7 @@ class Dialog : public GuiObject virtual void handleJoyDown(int stick, int button); virtual void handleJoyUp(int stick, int button); virtual void handleJoyAxis(int stick, int axis, int value); + virtual void handleJoyHat(int stick, int hat, int value); virtual void handleCommand(CommandSender* sender, int cmd, int data, int id); virtual void handleScreenChanged() {} diff --git a/stella/src/gui/DialogContainer.cxx b/stella/src/gui/DialogContainer.cxx index f95aa4c82..b7a543405 100644 --- a/stella/src/gui/DialogContainer.cxx +++ b/stella/src/gui/DialogContainer.cxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: DialogContainer.cxx,v 1.28 2006-01-08 20:55:53 stephena Exp $ +// $Id: DialogContainer.cxx,v 1.29 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #include "OSystem.hxx" @@ -386,6 +386,13 @@ void DialogContainer::handleJoyAxisEvent(int stick, int axis, int value) } } +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +void DialogContainer::handleJoyHatEvent(int stick, int hat, int value) +{ +cerr << "received hat event in dialogcontainer:" << endl + << "stick = " << stick << ", hat = " << hat << ", value = " << value << endl; +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void DialogContainer::handleJoyMouse(uInt32 time) { diff --git a/stella/src/gui/DialogContainer.hxx b/stella/src/gui/DialogContainer.hxx index 754b97972..3a1418cbe 100644 --- a/stella/src/gui/DialogContainer.hxx +++ b/stella/src/gui/DialogContainer.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: DialogContainer.hxx,v 1.15 2006-01-08 20:55:54 stephena Exp $ +// $Id: DialogContainer.hxx,v 1.16 2006-01-09 16:50:01 stephena Exp $ //============================================================================ #ifndef DIALOG_CONTAINER_HXX @@ -37,7 +37,7 @@ typedef FixedStack DialogStack; a stack, and handles their events. @author Stephen Anthony - @version $Id: DialogContainer.hxx,v 1.15 2006-01-08 20:55:54 stephena Exp $ + @version $Id: DialogContainer.hxx,v 1.16 2006-01-09 16:50:01 stephena Exp $ */ class DialogContainer { @@ -110,6 +110,15 @@ class DialogContainer */ void handleJoyAxisEvent(int stick, int axis, int value); + /** + Handle a joystick hat event. + + @param stick The joystick number + @param axis The joystick hat + @param value Value associated with given hat + */ + void handleJoyHatEvent(int stick, int hat, int value); + /** Draw the stack of menus. */ diff --git a/stella/src/gui/Widget.hxx b/stella/src/gui/Widget.hxx index fc0b856c7..f85a0c2ba 100644 --- a/stella/src/gui/Widget.hxx +++ b/stella/src/gui/Widget.hxx @@ -13,7 +13,7 @@ // See the file "license" for information on usage and redistribution of // this file, and for a DISCLAIMER OF ALL WARRANTIES. // -// $Id: Widget.hxx,v 1.45 2006-01-08 02:28:03 stephena Exp $ +// $Id: Widget.hxx,v 1.46 2006-01-09 16:50:01 stephena Exp $ // // Based on code from ScummVM - Scumm Interpreter // Copyright (C) 2002-2004 The ScummVM project @@ -75,7 +75,7 @@ enum { This is the base class for all widgets. @author Stephen Anthony - @version $Id: Widget.hxx,v 1.45 2006-01-08 02:28:03 stephena Exp $ + @version $Id: Widget.hxx,v 1.46 2006-01-09 16:50:01 stephena Exp $ */ class Widget : public GuiObject { @@ -99,6 +99,7 @@ class Widget : public GuiObject virtual void handleJoyDown(int stick, int button) {} virtual void handleJoyUp(int stick, int button) {} virtual void handleJoyAxis(int stick, int axis, int value) {} + virtual void handleJoyHat(int stick, int hat, int value) {} void draw(); void receivedFocus();