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-2021 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 #include <set>
23 
24 #include "bspf.hxx"
25 
26 /**
27   @author  Stephen Anthony, Christian Speckner, Thomas Jentzsch
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 = 0,
40       ConsoleColor, ConsoleBlackWhite, ConsoleColorToggle, Console7800Pause,
41       ConsoleLeftDiffA, ConsoleLeftDiffB, ConsoleLeftDiffToggle,
42       ConsoleRightDiffA, ConsoleRightDiffB, ConsoleRightDiffToggle,
43       ConsoleSelect, ConsoleReset,
44 
45       LeftJoystickUp, LeftJoystickDown, LeftJoystickLeft, LeftJoystickRight,
46       LeftJoystickFire, LeftJoystickFire5, LeftJoystickFire9,
47       RightJoystickUp, RightJoystickDown, RightJoystickLeft, RightJoystickRight,
48       RightJoystickFire, RightJoystickFire5, RightJoystickFire9,
49 
50       LeftPaddleADecrease, LeftPaddleAIncrease, LeftPaddleAAnalog, LeftPaddleAFire,
51       LeftPaddleBDecrease, LeftPaddleBIncrease, LeftPaddleBAnalog, LeftPaddleBFire,
52       RightPaddleADecrease, RightPaddleAIncrease, RightPaddleAAnalog, RightPaddleAFire,
53       RightPaddleBDecrease, RightPaddleBIncrease, RightPaddleBAnalog, RightPaddleBFire,
54 
55       LeftKeyboard1, LeftKeyboard2, LeftKeyboard3,
56       LeftKeyboard4, LeftKeyboard5, LeftKeyboard6,
57       LeftKeyboard7, LeftKeyboard8, LeftKeyboard9,
58       LeftKeyboardStar, LeftKeyboard0, LeftKeyboardPound,
59 
60       RightKeyboard1, RightKeyboard2, RightKeyboard3,
61       RightKeyboard4, RightKeyboard5, RightKeyboard6,
62       RightKeyboard7, RightKeyboard8, RightKeyboard9,
63       RightKeyboardStar, RightKeyboard0, RightKeyboardPound,
64 
65       CompuMateFunc, CompuMateShift,
66       CompuMate0, CompuMate1, CompuMate2, CompuMate3, CompuMate4,
67       CompuMate5, CompuMate6, CompuMate7, CompuMate8, CompuMate9,
68       CompuMateA, CompuMateB, CompuMateC, CompuMateD, CompuMateE,
69       CompuMateF, CompuMateG, CompuMateH, CompuMateI, CompuMateJ,
70       CompuMateK, CompuMateL, CompuMateM, CompuMateN, CompuMateO,
71       CompuMateP, CompuMateQ, CompuMateR, CompuMateS, CompuMateT,
72       CompuMateU, CompuMateV, CompuMateW, CompuMateX, CompuMateY,
73       CompuMateZ,
74       CompuMateComma, CompuMatePeriod, CompuMateEnter, CompuMateSpace,
75       CompuMateQuestion, CompuMateLeftBracket, CompuMateRightBracket, CompuMateMinus,
76       CompuMateQuote, CompuMateBackspace, CompuMateEquals, CompuMatePlus,
77       CompuMateSlash,
78 
79       Combo1, Combo2, Combo3, Combo4, Combo5, Combo6, Combo7, Combo8,
80       Combo9, Combo10, Combo11, Combo12, Combo13, Combo14, Combo15, Combo16,
81 
82       UIUp, UIDown, UILeft, UIRight, UIHome, UIEnd, UIPgUp, UIPgDown,
83       UISelect, UINavPrev, UINavNext, UIOK, UICancel, UIPrevDir,
84       UITabPrev, UITabNext,
85 
86       NextMouseControl, ToggleGrabMouse,
87       MouseAxisXMove, MouseAxisYMove, MouseAxisXValue, MouseAxisYValue,
88       MouseButtonLeftValue, MouseButtonRightValue,
89 
90       Quit, ReloadConsole, Fry,
91       TogglePauseMode, StartPauseMode,
92       OptionsMenuMode, CmdMenuMode, DebuggerMode, PlusRomsSetupMode, ExitMode,
93       TakeSnapshot, ToggleContSnapshots, ToggleContSnapshotsFrame,
94       ToggleTurbo,
95 
96       NextState, PreviousState, LoadState, SaveState,
97       SaveAllStates, LoadAllStates,
98       ToggleAutoSlot, ToggleTimeMachine, TimeMachineMode,
99       Rewind1Menu, Rewind10Menu, RewindAllMenu,
100       Unwind1Menu, Unwind10Menu, UnwindAllMenu,
101       RewindPause, UnwindPause,
102 
103       FormatDecrease, FormatIncrease, PaletteDecrease, PaletteIncrease, ToggleColorLoss,
104       PreviousPaletteAttribute, NextPaletteAttribute,
105       PaletteAttributeDecrease, PaletteAttributeIncrease,
106       ToggleFullScreen, VidmodeDecrease, VidmodeIncrease,
107       VCenterDecrease, VCenterIncrease, VSizeAdjustDecrease, VSizeAdjustIncrease,
108       OverscanDecrease, OverscanIncrease,
109 
110       VidmodeStd, VidmodeRGB, VidmodeSVideo, VidModeComposite, VidModeBad, VidModeCustom,
111       PreviousVideoMode, NextVideoMode,
112       PreviousAttribute, NextAttribute, DecreaseAttribute, IncreaseAttribute,
113       ScanlinesDecrease, ScanlinesIncrease,
114       PreviousScanlineMask, NextScanlineMask,
115       PhosphorDecrease, PhosphorIncrease, TogglePhosphor, ToggleInter,
116       ToggleDeveloperSet, JitterDecrease, JitterIncrease, ToggleJitter,
117 
118       VolumeDecrease, VolumeIncrease, SoundToggle,
119 
120       ToggleP0Collision, ToggleP0Bit, ToggleP1Collision, ToggleP1Bit,
121       ToggleM0Collision, ToggleM0Bit, ToggleM1Collision, ToggleM1Bit,
122       ToggleBLCollision, ToggleBLBit, TogglePFCollision, TogglePFBit,
123       ToggleCollisions, ToggleBits, ToggleFixedColors,
124 
125       ToggleFrameStats, ToggleSAPortOrder, ExitGame,
126       SettingDecrease, SettingIncrease, PreviousSetting, NextSetting,
127       ToggleAdaptRefresh, PreviousMultiCartRom,
128       // add new (after Version 4) events from here to avoid that user remapped events get overwritten
129       PreviousSettingGroup, NextSettingGroup,
130       TogglePlayBackMode,
131       DecreaseAutoFire, IncreaseAutoFire,
132       DecreaseSpeed, IncreaseSpeed,
133 
134       QTJoystickThreeUp, QTJoystickThreeDown, QTJoystickThreeLeft, QTJoystickThreeRight,
135       QTJoystickThreeFire,
136       QTJoystickFourUp, QTJoystickFourDown, QTJoystickFourLeft, QTJoystickFourRight,
137       QTJoystickFourFire,
138 
139       ToggleCorrectAspectRatio,
140 
141       MoveLeftChar, MoveRightChar, MoveLeftWord, MoveRightWord,
142       MoveHome, MoveEnd,
143       SelectLeftChar, SelectRightChar, SelectLeftWord, SelectRightWord,
144       SelectHome, SelectEnd, SelectAll,
145       Delete, DeleteLeftWord, DeleteRightWord, DeleteHome, DeleteEnd, Backspace,
146       Cut, Copy, Paste, Undo, Redo,
147       AbortEdit, EndEdit,
148 
149       HighScoresMenuMode,
150       // Input settings
151       DecreaseDeadzone, IncreaseDeadzone,
152       DecAnalogDeadzone, IncAnalogDeadzone,
153       DecAnalogSense, IncAnalogSense,
154       DecAnalogLinear, IncAnalogLinear,
155       DecDejtterAveraging, IncDejtterAveraging,
156       DecDejtterReaction, IncDejtterReaction,
157       DecDigitalSense, IncDigitalSense,
158       ToggleFourDirections, ToggleKeyCombos,
159       PrevMouseAsController, NextMouseAsController,
160       DecMousePaddleSense, IncMousePaddleSense,
161       DecMouseTrackballSense, IncMouseTrackballSense,
162       DecreaseDrivingSense, IncreaseDrivingSense,
163       PreviousCursorVisbility, NextCursorVisbility,
164       // GameInfoDialog/Controllers
165       PreviousLeftPort, NextLeftPort,
166       PreviousRightPort, NextRightPort,
167       ToggleSwapPorts, ToggleSwapPaddles,
168       DecreasePaddleCenterX, IncreasePaddleCenterX,
169       DecreasePaddleCenterY, IncreasePaddleCenterY,
170       PreviousMouseControl,
171       DecreaseMouseAxesRange, IncreaseMouseAxesRange,
172 
173       SALeftAxis0Value, SALeftAxis1Value, SARightAxis0Value, SARightAxis1Value,
174       QTPaddle3AFire, QTPaddle3BFire, QTPaddle4AFire, QTPaddle4BFire,
175       UIHelp,
176       LastType
177     };
178 
179     // Event categorizing groups
180     enum Group
181     {
182       Menu, Emulation,
183       Misc, AudioVideo, States, Console, Joystick, Paddles, Keyboard,
184       Devices,
185       Debug, Combo,
186       LastGroup
187     };
188 
189     // Event list version, update only if the id of existing(!) event types changed
190     static constexpr Int32 VERSION = 5;
191 
192     using EventSet = std::set<Event::Type>;
193 
194   public:
195     /**
196       Create a new event object.
197     */
Event()198     Event() { clear(); }
199 
200   public:
201     /**
202       Get the value associated with the event of the specified type.
203     */
get(Type type) const204     Int32 get(Type type) const {
205       std::lock_guard<std::mutex> lock(myMutex);
206 
207       return myValues[type];
208     }
209 
210     /**
211       Set the value associated with the event of the specified type.
212     */
set(Type type,Int32 value)213     void set(Type type, Int32 value) {
214       std::lock_guard<std::mutex> lock(myMutex);
215 
216       myValues[type] = value;
217     }
218 
219     /**
220       Clears the event array (resets to initial state).
221     */
clear()222     void clear()
223     {
224       std::lock_guard<std::mutex> lock(myMutex);
225 
226       myValues.fill(Event::NoType);
227     }
228 
229     /**
230       Tests if a given event represents continuous or analog values.
231     */
isAnalog(Type type)232     static bool isAnalog(Type type)
233     {
234       switch(type)
235       {
236         case Event::LeftPaddleAAnalog:
237         case Event::LeftPaddleBAnalog:
238         case Event::RightPaddleAAnalog:
239         case Event::RightPaddleBAnalog:
240           return true;
241         default:
242           return false;
243       }
244     }
245 
246   private:
247     // Array of values associated with each event type
248     std::array<Int32, LastType> myValues;
249 
250     mutable std::mutex myMutex;
251 
252   private:
253     // Following constructors and assignment operators not supported
254     Event(const Event&) = delete;
255     Event(Event&&) = delete;
256     Event& operator=(const Event&) = delete;
257     Event& operator=(Event&&) = delete;
258 };
259 
260 // Hold controller related events
261 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
262 static const Event::EventSet LeftJoystickEvents = {
263   Event::LeftJoystickUp, Event::LeftJoystickDown, Event::LeftJoystickLeft, Event::LeftJoystickRight,
264   Event::LeftJoystickFire, Event::LeftJoystickFire5, Event::LeftJoystickFire9,
265 };
266 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
267 static const Event::EventSet QTJoystick3Events = {
268   Event::QTJoystickThreeUp, Event::QTJoystickThreeDown, Event::QTJoystickThreeLeft, Event::QTJoystickThreeRight,
269   Event::QTJoystickThreeFire
270 };
271 
272 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
273 static const Event::EventSet RightJoystickEvents = {
274   Event::RightJoystickUp, Event::RightJoystickDown, Event::RightJoystickLeft, Event::RightJoystickRight,
275   Event::RightJoystickFire, Event::RightJoystickFire5, Event::RightJoystickFire9,
276 };
277 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
278 static const Event::EventSet QTJoystick4Events = {
279   Event::QTJoystickFourUp, Event::QTJoystickFourDown, Event::QTJoystickFourLeft, Event::QTJoystickFourRight,
280   Event::QTJoystickFourFire
281 };
282 
283 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
284 static const Event::EventSet LeftPaddlesEvents = {
285   Event::LeftPaddleADecrease, Event::LeftPaddleAIncrease, Event::LeftPaddleAAnalog, Event::LeftPaddleAFire,
286   Event::LeftPaddleBDecrease, Event::LeftPaddleBIncrease, Event::LeftPaddleBAnalog, Event::LeftPaddleBFire,
287 };
288 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
289 static const Event::EventSet QTPaddles3Events = {
290   // Only fire buttons supported by QuadTari
291   Event::QTPaddle3AFire, Event::QTPaddle3BFire
292 };
293 
294 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
295 static const Event::EventSet RightPaddlesEvents = {
296   Event::RightPaddleADecrease, Event::RightPaddleAIncrease, Event::RightPaddleAAnalog, Event::RightPaddleAFire,
297   Event::RightPaddleBDecrease, Event::RightPaddleBIncrease, Event::RightPaddleBAnalog, Event::RightPaddleBFire,
298 };
299 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
300 static const Event::EventSet QTPaddles4Events = {
301   // Only fire buttons supported by QuadTari
302   Event::QTPaddle4AFire, Event::QTPaddle4BFire
303 };
304 
305 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
306 static const Event::EventSet LeftKeyboardEvents = {
307   Event::LeftKeyboard1, Event::LeftKeyboard2, Event::LeftKeyboard3,
308   Event::LeftKeyboard4, Event::LeftKeyboard5, Event::LeftKeyboard6,
309   Event::LeftKeyboard7, Event::LeftKeyboard8, Event::LeftKeyboard9,
310   Event::LeftKeyboardStar, Event::LeftKeyboard0, Event::LeftKeyboardPound,
311 };
312 
313 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
314 static const Event::EventSet RightKeyboardEvents = {
315   Event::RightKeyboard1, Event::RightKeyboard2, Event::RightKeyboard3,
316   Event::RightKeyboard4, Event::RightKeyboard5, Event::RightKeyboard6,
317   Event::RightKeyboard7, Event::RightKeyboard8, Event::RightKeyboard9,
318   Event::RightKeyboardStar, Event::RightKeyboard0, Event::RightKeyboardPound,
319 };
320 
321 #endif
322