"Fossies" - the Fresh Open Source Software Archive

Member "stella-6.0.2/src/emucore/Event.hxx" (11 Oct 2019, 5651 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 "Event.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 EVENT_HXX
   19 #define EVENT_HXX
   20 
   21 #include <mutex>
   22 
   23 #include "bspf.hxx"
   24 #include "StellaKeys.hxx"
   25 
   26 /**
   27   @author  Stephen Anthony, Christian Speckner
   28 */
   29 class Event
   30 {
   31   public:
   32     /**
   33       Enumeration of all possible events in Stella, including both
   34       console and controller event types as well as events that aren't
   35       technically part of the emulation core
   36     */
   37     enum Type
   38     {
   39       NoType,
   40       ConsoleOn, ConsoleOff, ConsoleColor, ConsoleBlackWhite,
   41       ConsoleLeftDiffA, ConsoleLeftDiffB,
   42       ConsoleRightDiffA, ConsoleRightDiffB,
   43       ConsoleSelect, ConsoleReset,
   44       ConsoleLeftDiffToggle, ConsoleRightDiffToggle, ConsoleColorToggle, Console7800Pause,
   45 
   46       JoystickZeroUp, JoystickZeroDown, JoystickZeroLeft, JoystickZeroRight,
   47         JoystickZeroFire, JoystickZeroFire5, JoystickZeroFire9,
   48       JoystickOneUp, JoystickOneDown, JoystickOneLeft, JoystickOneRight,
   49         JoystickOneFire, JoystickOneFire5, JoystickOneFire9,
   50 
   51       PaddleZeroDecrease, PaddleZeroIncrease, PaddleZeroAnalog, PaddleZeroFire,
   52       PaddleOneDecrease, PaddleOneIncrease, PaddleOneAnalog, PaddleOneFire,
   53       PaddleTwoDecrease, PaddleTwoIncrease, PaddleTwoAnalog, PaddleTwoFire,
   54       PaddleThreeDecrease, PaddleThreeIncrease, PaddleThreeAnalog, PaddleThreeFire,
   55 
   56       KeyboardZero1, KeyboardZero2, KeyboardZero3,
   57       KeyboardZero4, KeyboardZero5, KeyboardZero6,
   58       KeyboardZero7, KeyboardZero8, KeyboardZero9,
   59       KeyboardZeroStar, KeyboardZero0, KeyboardZeroPound,
   60 
   61       KeyboardOne1, KeyboardOne2, KeyboardOne3,
   62       KeyboardOne4, KeyboardOne5, KeyboardOne6,
   63       KeyboardOne7, KeyboardOne8, KeyboardOne9,
   64       KeyboardOneStar, KeyboardOne0, KeyboardOnePound,
   65 
   66       Combo1, Combo2, Combo3, Combo4, Combo5, Combo6, Combo7, Combo8,
   67       Combo9, Combo10, Combo11, Combo12, Combo13, Combo14, Combo15, Combo16,
   68 
   69       SALeftAxis0Value, SALeftAxis1Value,
   70       SARightAxis0Value, SARightAxis1Value,
   71 
   72       MouseAxisXValue, MouseAxisYValue,
   73       MouseButtonLeftValue, MouseButtonRightValue,
   74 
   75       ChangeState, LoadState, SaveState, TakeSnapshot, Quit,
   76       PauseMode, OptionsMenuMode, CmdMenuMode, TimeMachineMode, DebuggerMode, LauncherMode,
   77       Fry, VolumeDecrease, VolumeIncrease,
   78 
   79       UIUp, UIDown, UILeft, UIRight, UIHome, UIEnd, UIPgUp, UIPgDown,
   80       UISelect, UINavPrev, UINavNext, UIOK, UICancel, UIPrevDir,
   81 
   82       LastType
   83     };
   84 
   85     class KeyTable {
   86       public:
   87 
   88         KeyTable(const bool* keyTable, std::mutex& mutex)
   89           : myKeyTable(keyTable),
   90             myMutex(mutex),
   91             myIsEnabled(true)
   92         {
   93         }
   94 
   95         bool operator[](int type) const {
   96           if (!myIsEnabled) return false;
   97 
   98           std::lock_guard<std::mutex> lock(myMutex);
   99 
  100           return myKeyTable[type];
  101         }
  102 
  103         void enable(bool isEnabled) {
  104           myIsEnabled = isEnabled;
  105         }
  106 
  107       private:
  108 
  109         const bool *myKeyTable;
  110         std::mutex& myMutex;
  111 
  112         bool myIsEnabled;
  113 
  114     };
  115 
  116   public:
  117     /**
  118       Create a new event object.
  119     */
  120     Event() { clear(); }
  121 
  122   public:
  123     /**
  124       Get the value associated with the event of the specified type.
  125     */
  126     Int32 get(Type type) const {
  127       std::lock_guard<std::mutex> lock(myMutex);
  128 
  129       return myValues[type];
  130     }
  131 
  132     /**
  133       Set the value associated with the event of the specified type.
  134     */
  135     void set(Type type, Int32 value) {
  136       std::lock_guard<std::mutex> lock(myMutex);
  137 
  138       myValues[type] = value;
  139     }
  140 
  141     /**
  142       Clears the event array (resets to initial state).
  143     */
  144     void clear()
  145     {
  146       std::lock_guard<std::mutex> lock(myMutex);
  147 
  148       for(uInt32 i = 0; i < LastType; ++i)
  149         myValues[i] = Event::NoType;
  150 
  151       for(uInt32 i = 0; i < KBDK_LAST; ++i)
  152         myKeyTable[i] = false;
  153     }
  154 
  155     /**
  156       Get the keytable associated with this event.
  157     */
  158     KeyTable getKeys() const { return KeyTable(myKeyTable, myMutex); }
  159 
  160     /**
  161       Set the value associated with the event of the specified type.
  162     */
  163     void setKey(StellaKey key, bool state) {
  164       std::lock_guard<std::mutex> lock(myMutex);
  165 
  166       myKeyTable[key] = state;
  167     }
  168 
  169     /**
  170       Tests if a given event represents continuous or analog values.
  171     */
  172     static bool isAnalog(Type type)
  173     {
  174       switch(type)
  175       {
  176         case Event::PaddleZeroAnalog:
  177         case Event::PaddleOneAnalog:
  178         case Event::PaddleTwoAnalog:
  179         case Event::PaddleThreeAnalog:
  180           return true;
  181         default:
  182           return false;
  183       }
  184     }
  185 
  186   private:
  187     // Array of values associated with each event type
  188     Int32 myValues[LastType];
  189 
  190     // Array of keyboard key states
  191     bool myKeyTable[KBDK_LAST];
  192 
  193     mutable std::mutex myMutex;
  194 
  195   private:
  196     // Following constructors and assignment operators not supported
  197     Event(const Event&) = delete;
  198     Event(Event&&) = delete;
  199     Event& operator=(const Event&) = delete;
  200     Event& operator=(Event&&) = delete;
  201 };
  202 
  203 #endif