"Fossies" - the Fresh Open Source Software Archive

Member "stella-6.0.2/src/common/PJoystickHandler.hxx" (11 Oct 2019, 4786 Bytes) of package /linux/privat/stella-6.0.2-src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "PJoystickHandler.hxx" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 6.0.1_vs_6.0.2.

    1 //============================================================================
    2 //
    3 //   SSSS    tt          lll  lll
    4 //  SS  SS   tt           ll   ll
    5 //  SS     tttttt  eeee   ll   ll   aaaa
    6 //   SSSS    tt   ee  ee  ll   ll      aa
    7 //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
    8 //  SS  SS   tt   ee      ll   ll  aa  aa
    9 //   SSSS     ttt  eeeee llll llll  aaaaa
   10 //
   11 // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
   12 // and the Stella Team
   13 //
   14 // See the file "License.txt" for information on usage and redistribution of
   15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
   16 //============================================================================
   17 
   18 #ifndef PHYSICAL_JOYSTICK_HANDLER_HXX
   19 #define PHYSICAL_JOYSTICK_HANDLER_HXX
   20 
   21 #include <map>
   22 
   23 class OSystem;
   24 class EventHandler;
   25 class Event;
   26 
   27 #include "bspf.hxx"
   28 #include "EventHandlerConstants.hxx"
   29 #include "PhysicalJoystick.hxx"
   30 #include "Variant.hxx"
   31 
   32 using PhysicalJoystickPtr = shared_ptr<PhysicalJoystick>;
   33 
   34 /**
   35   This class handles all physical joystick-related operations in Stella.
   36 
   37   It is responsible for adding/accessing/removing PhysicalJoystick objects,
   38   and getting/setting events associated with joystick actions (button presses,
   39   axis/hat actions, etc).
   40 
   41   Essentially, this class is an extension of the EventHandler class, but
   42   handling only joystick-specific functionality.
   43 
   44   @author  Stephen Anthony
   45 */
   46 class PhysicalJoystickHandler
   47 {
   48   private:
   49     struct StickInfo
   50     {
   51       StickInfo(const string& map = EmptyString, PhysicalJoystickPtr stick = nullptr)
   52         : mapping(map), joy(stick) {}
   53 
   54       string mapping;
   55       PhysicalJoystickPtr joy;
   56 
   57       friend ostream& operator<<(ostream& os, const StickInfo& si) {
   58         os << "  joy: " << si.joy << endl << "  map: " << si.mapping;
   59         return os;
   60       }
   61     };
   62 
   63   public:
   64     PhysicalJoystickHandler(OSystem& system, EventHandler& handler, Event& event);
   65 
   66     /** Return stick ID on success, -1 on failure. */
   67     int add(PhysicalJoystickPtr stick);
   68     bool remove(int id);
   69     bool remove(const string& name);
   70     void mapStelladaptors(const string& saport);
   71     void setDefaultMapping(Event::Type type, EventMode mode);
   72     void eraseMapping(Event::Type event, EventMode mode);
   73     void saveMapping();
   74     string getMappingDesc(Event::Type, EventMode mode) const;
   75 
   76     /** Bind a physical joystick event to a virtual event/action. */
   77     bool addAxisMapping(Event::Type event, EventMode mode, int stick, int axis, int value);
   78     bool addBtnMapping(Event::Type event, EventMode mode, int stick, int button);
   79     bool addHatMapping(Event::Type event, EventMode mode, int stick, int hat, JoyHat value);
   80 
   81     /** Handle a physical joystick event. */
   82     void handleAxisEvent(int stick, int axis, int value);
   83     void handleBtnEvent(int stick, int button, uInt8 state);
   84     void handleHatEvent(int stick, int hat, int value);
   85 
   86     Event::Type eventForAxis(int stick, int axis, int value, EventMode mode) const {
   87       const PhysicalJoystickPtr j = joy(stick);
   88       return j ? j->axisTable[axis][(value > 0)][mode] : Event::NoType;
   89     }
   90     Event::Type eventForButton(int stick, int button, EventMode mode) const {
   91       const PhysicalJoystickPtr j = joy(stick);
   92       return j ? j->btnTable[button][mode] : Event::NoType;
   93     }
   94     Event::Type eventForHat(int stick, int hat, JoyHat value, EventMode mode) const {
   95       const PhysicalJoystickPtr j = joy(stick);
   96       return j ? j->hatTable[hat][int(value)][mode] : Event::NoType;
   97     }
   98 
   99     /** Returns a list of pairs consisting of joystick name and associated ID. */
  100     VariantList database() const;
  101 
  102   private:
  103     using StickDatabase = std::map<string,StickInfo>;
  104     using StickList = std::map<int, PhysicalJoystickPtr>;
  105 
  106     OSystem& myOSystem;
  107     EventHandler& myHandler;
  108     Event& myEvent;
  109 
  110     // Contains all joysticks that Stella knows about, indexed by name
  111     StickDatabase myDatabase;
  112 
  113     // Contains only joysticks that are currently available, indexed by id
  114     StickList mySticks;
  115 
  116     // Get joystick corresponding to given id (or nullptr if it doesn't exist)
  117     // Make this inline so it's as fast as possible
  118     const PhysicalJoystickPtr joy(int id) const {
  119       const auto& i = mySticks.find(id);
  120       return i != mySticks.cend() ? i->second : nullptr;
  121     }
  122 
  123     // Set default mapping for given joystick when no mappings already exist
  124     void setStickDefaultMapping(int stick, Event::Type type, EventMode mode);
  125 
  126     friend ostream& operator<<(ostream& os, const PhysicalJoystickHandler& jh);
  127 
  128     // Static lookup tables for Stelladaptor/2600-daptor axis/button support
  129     static const Event::Type SA_Axis[2][2];
  130     static const Event::Type SA_Button[2][4];
  131     static const Event::Type SA_Key[2][12];
  132 };
  133 
  134 #endif