Event queue refactor

This commit is contained in:
elasota
2019-12-31 03:55:17 -05:00
parent 84e4f9fb0b
commit 52338a3983
25 changed files with 244 additions and 297 deletions

View File

@@ -8,6 +8,7 @@
#include "PLAppleEvents.h"
#include "PLKeyEncoding.h"
#include "PLTimeTaggedVOSEvent.h"
#include "PLToolUtils.h"
#include "PLQDraw.h"
#include "Externs.h"
@@ -17,8 +18,8 @@
short BitchAboutColorDepth (void);
void HandleMouseEvent (EventRecord *);
void HandleKeyEvent (EventRecord *);
void HandleMouseEvent (const GpMouseInputEvent &, uint32_t);
void HandleKeyEvent (const KeyDownStates &keyStates, const GpKeyboardInputEvent &);
void HandleUpdateEvent (EventRecord *);
void HandleOSEvent (EventRecord *);
void HandleHighLevelEvent (EventRecord *);
@@ -59,24 +60,25 @@ short BitchAboutColorDepth (void)
//-------------------------------------------------------------- HandleMouseEvent
// Handle a mouse click event.
void HandleMouseEvent (EventRecord *theEvent)
void HandleMouseEvent (const GpMouseInputEvent &theEvent, uint32_t tick)
{
WindowPtr whichWindow;
long menuChoice, newSize;
short thePart, hDelta, vDelta;
Boolean isDoubleClick;
Point evtPoint = Point::Create(theEvent.m_x, theEvent.m_y);
thePart = FindWindow(theEvent->where, &whichWindow);
thePart = FindWindow(evtPoint, &whichWindow);
switch (thePart)
{
case inMenuBar:
menuChoice = MenuSelect(theEvent->where);
menuChoice = MenuSelect(evtPoint);
DoMenuChoice(menuChoice);
break;
case inDrag:
DragWindow(whichWindow, theEvent->where, &thisMac.screen);
DragWindow(whichWindow, evtPoint, &thisMac.screen);
if (whichWindow == mainWindow)
{
SendBehind(mainWindow, (WindowPtr)0L);
@@ -94,7 +96,7 @@ void HandleMouseEvent (EventRecord *theEvent)
break;
case inGoAway:
if (TrackGoAway(whichWindow,theEvent->where))
if (TrackGoAway(whichWindow, evtPoint))
{
if (whichWindow == mapWindow)
ToggleMapWindow();
@@ -110,43 +112,43 @@ void HandleMouseEvent (EventRecord *theEvent)
case inGrow:
if (whichWindow == mapWindow)
{
newSize = GrowWindow(mapWindow, theEvent->where, &thisMac.gray);
newSize = GrowWindow(mapWindow, evtPoint, &thisMac.gray);
ResizeMapWindow(LoWord(newSize), HiWord(newSize));
}
break;
case inZoomIn:
case inZoomOut:
if (TrackBox(whichWindow, theEvent->where, thePart))
if (TrackBox(whichWindow, evtPoint, thePart))
ZoomWindow(whichWindow, thePart, true);
break;
case inContent:
if (whichWindow == mainWindow)
{
hDelta = theEvent->where.h - lastWhere.h;
hDelta = evtPoint.h - lastWhere.h;
if (hDelta < 0)
hDelta = -hDelta;
vDelta = theEvent->where.v - lastWhere.v;
vDelta = evtPoint.v - lastWhere.v;
if (vDelta < 0)
vDelta = -vDelta;
if (((theEvent->when - lastUp) < doubleTime) && (hDelta < 5) &&
if (((tick - lastUp) < doubleTime) && (hDelta < 5) &&
(vDelta < 5))
isDoubleClick = true;
else
{
isDoubleClick = false;
lastUp = theEvent->when;
lastWhere = theEvent->where;
lastUp = tick;
lastWhere = evtPoint;
}
HandleMainClick(theEvent->where, isDoubleClick);
HandleMainClick(evtPoint, isDoubleClick);
}
else if (whichWindow == mapWindow)
HandleMapClick(theEvent);
else if (whichWindow == toolsWindow)
HandleToolsClick(theEvent->where);
HandleToolsClick(evtPoint);
else if (whichWindow == linkWindow)
HandleLinkClick(theEvent->where);
HandleLinkClick(evtPoint);
break;
default:
@@ -157,15 +159,12 @@ void HandleMouseEvent (EventRecord *theEvent)
//-------------------------------------------------------------- HandleKeyEvent
// Handle a key-down event.
void HandleKeyEvent (EventRecord *theEvent)
void HandleKeyEvent (const KeyDownStates &keyStates, const GpKeyboardInputEvent &theEvent)
{
intptr_t theChar;
Boolean shiftDown, commandDown, optionDown;
theChar = theEvent->message;
shiftDown = ((theEvent->modifiers & shiftKey) != 0);
commandDown = ((theEvent->modifiers & cmdKey) != 0);
optionDown = ((theEvent->modifiers & optionKey) != 0);
const intptr_t theChar = PackVOSKeyCode(theEvent);
const bool shiftDown = BitTst(keyStates, PL_KEY_EITHER_SPECIAL(kShift));
const bool commandDown = BitTst(keyStates, PL_KEY_EITHER_SPECIAL(kControl));
const bool optionDown = BitTst(keyStates, PL_KEY_EITHER_SPECIAL(kAlt));
if ((commandDown) && (!optionDown))
DoMenuChoice(MenuKey(static_cast<int>(theChar)));
@@ -324,6 +323,7 @@ void HandleKeyEvent (EventRecord *theEvent)
//-------------------------------------------------------------- HandleUpdateEvent
// Handle an update event.
#if 0
void HandleUpdateEvent (EventRecord *theEvent)
{
if ((WindowPtr)theEvent->message == mainWindow)
@@ -431,6 +431,7 @@ void HandleHighLevelEvent (EventRecord *theEvent)
if ((theErr != PLErrors::kNone) && (theErr != errAEEventNotHandled))
YellowAlert(kYellowAppleEventErr, theErr);
}
#endif
//-------------------------------------------------------------- HandleIdleTask
// Handle some processing during event lulls.
@@ -457,10 +458,10 @@ void HandleIdleTask (void)
void HandleEvent (void)
{
KeyMap eventKeys;
EventRecord theEvent;
long sleep = 2;
Boolean itHappened = true;
KeyDownStates eventKeys;
TimeTaggedVOSEvent theEvent;
uint32_t sleep = 2;
bool itHappened = true;
GetKeys(eventKeys);
if ((BitTst(eventKeys, PL_KEY_EITHER_SPECIAL(kControl))) &&
@@ -475,39 +476,32 @@ void HandleEvent (void)
SelectTool(kSelectTool);
}
// GP: Use WaitNextEvent to yield to the host
//if (thisMac.hasWNE)
itHappened = WaitNextEvent(everyEvent, &theEvent, sleep, nil);
//else
//{
// // SystemTask();
// itHappened = GetNextEvent(everyEvent, &theEvent);
//}
itHappened = WaitForEvent(&theEvent, sleep);
if (itHappened)
{
switch (theEvent.what)
if (theEvent.m_vosEvent.m_eventType == GpVOSEventTypes::kMouseInput)
{
case mouseDown:
HandleMouseEvent(&theEvent);
break;
case keyDown:
case autoKey:
HandleKeyEvent(&theEvent);
break;
case updateEvt:
HandleUpdateEvent(&theEvent);
break;
case osEvt:
HandleOSEvent(&theEvent);
break;
case kHighLevelEvent:
HandleHighLevelEvent(&theEvent);
break;
switch (theEvent.m_vosEvent.m_event.m_mouseInputEvent.m_eventType)
{
case GpMouseEventTypes::kDown:
HandleMouseEvent(theEvent.m_vosEvent.m_event.m_mouseInputEvent, theEvent.m_timestamp);
break;
default:
break;
}
}
else if (theEvent.m_vosEvent.m_eventType == GpVOSEventTypes::kKeyboardInput)
{
switch (theEvent.m_vosEvent.m_event.m_keyboardInputEvent.m_eventType)
{
case GpKeyboardInputEventTypes::kDown:
case GpKeyboardInputEventTypes::kAuto:
HandleKeyEvent(eventKeys, theEvent.m_vosEvent.m_event.m_keyboardInputEvent);
break;
default:
break;
}
}
}
else

View File

@@ -7,7 +7,9 @@
#include "PLToolUtils.h"
#include "PLPasStr.h"
#include "PLEventQueue.h"
#include "PLKeyEncoding.h"
#include "PLTimeTaggedVOSEvent.h"
#include "Externs.h"
#include "Environ.h"
#include "FontManager.h"
@@ -138,8 +140,8 @@ void SetUpFinalScreen (void)
void DoGameOverStarAnimation (void)
{
#define kStarFalls 8
EventRecord theEvent;
KeyMap theKeys;
TimeTaggedVOSEvent theEvent;
KeyDownStates theKeys;
Rect angelDest;
long nextLoop;
short which, i, count, pass;
@@ -213,9 +215,12 @@ void DoGameOverStarAnimation (void)
GetKeys(theKeys);
if ((BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kControl))) || (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kAlt))) || (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kShift))))
noInteruption = false;
if (GetNextEvent(everyEvent, &theEvent))
if ((theEvent.what == mouseDown) || (theEvent.what == keyDown))
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(&theEvent))
{
if (theEvent.IsLMouseDownEvent() || theEvent.IsKeyDownEvent())
noInteruption = false;
}
Delay(1, nullptr);
}
@@ -436,10 +441,10 @@ void DrawPages (void)
void DoDiedGameOver (void)
{
EventRecord theEvent;
KeyMap theKeys;
long nextLoop;
Boolean userAborted;
TimeTaggedVOSEvent theEvent;
KeyDownStates theKeys;
long nextLoop;
Boolean userAborted;
userAborted = false;
InitDiedGameOver();
@@ -460,12 +465,15 @@ void DoDiedGameOver (void)
pagesStuck = 8;
userAborted = true;
}
if (GetNextEvent(everyEvent, &theEvent))
if ((theEvent.what == mouseDown) || (theEvent.what == keyDown))
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(&theEvent))
{
if (theEvent.IsLMouseDownEvent() || theEvent.IsKeyDownEvent())
{
pagesStuck = 8;
userAborted = true;
}
}
Delay(1, nullptr);
}

View File

@@ -5,6 +5,7 @@
//----------------------------------------------------------------------------
//============================================================================
struct GpMouseInputEvent;
//-------------------------------------------------------------- Prototypes
@@ -163,7 +164,7 @@ void ResizeMapWindow (SInt16, SInt16);
void OpenMapWindow (void);
void CloseMapWindow (void);
void ToggleMapWindow (void);
void HandleMapClick (EventRecord *);
void HandleMapClick (const GpMouseInputEvent &);
void MoveRoom (Point);
void DoMarquee (void); // --- Marquee.c

View File

@@ -31,7 +31,7 @@ Boolean QuerySaveGame (void);
demoPtr demoData;
KeyMap theKeys;
KeyDownStates theKeys;
Dialog *saveDial;
short demoIndex, batteryFrame;
Boolean isEscPauseKey, paused, batteryWasEngaged;

View File

@@ -384,6 +384,8 @@ int gpAppMain()
// numSMWarnings++;
// BitchAboutSM3();
// }
UpdateMainWindow();
while (!quitting) // this is the main loop
HandleEvent();

View File

@@ -159,6 +159,8 @@ void UpdateMainWindow (void)
DrawOnSplash(mainWindow->GetDrawSurface());
}
mainWindow->m_graf.m_port.SetDirty(PortabilityLayer::QDPortDirtyFlag_Contents);
splashDrawn = true;
}
@@ -370,7 +372,7 @@ void UpdateEditWindowTitle (void)
void HandleMainClick (Point wherePt, Boolean isDoubleClick)
{
KeyMap theseKeys;
KeyDownStates theseKeys;
if ((theMode != kEditMode) || (mainWindow == nil) ||
(!houseUnlocked))

View File

@@ -554,7 +554,7 @@ void LiveVScrollAction (ControlHandle theControl, short thePart)
//-------------------------------------------------------------- HandleMapClick
void HandleMapClick (EventRecord *theEvent)
void HandleMapClick (const GpMouseInputEvent &theEvent)
{
#ifndef COMPILEDEMO
Rect aRoom;
@@ -565,7 +565,7 @@ void HandleMapClick (EventRecord *theEvent)
short roomH, roomV, itsNumber;
ControlActionUPP scrollHActionUPP, scrollVActionUPP;
wherePt = theEvent->where;
wherePt = Point::Create(theEvent.m_x, theEvent.m_y);
scrollHActionUPP = NewControlActionUPP(LiveHScrollAction);
scrollVActionUPP = NewControlActionUPP(LiveVScrollAction);

View File

@@ -48,7 +48,7 @@ short wasFlower;
Boolean AddNewObject (Point where, short what, Boolean showItNow)
{
KeyMap theseKeys;
KeyDownStates theseKeys;
Rect srcRect, newRect;
short direction, dist;
Boolean handled, drawWholeRoom;

View File

@@ -2683,7 +2683,7 @@ void DrawThisRoomsObjects (void)
void HiliteAllObjects (void)
{
#ifndef COMPILEDEMO
KeyMap theseKeys;
KeyDownStates theseKeys;
short i;
Pattern dummyPattern;

View File

@@ -35,7 +35,6 @@ typedef struct
void InitGlider (gliderPtr, short);
void SetHouseToFirstRoom (void);
void SetHouseToSavedRoom (void);
void HandlePlayEvent (void);
void PlayGame (void);
void HandleRoomVisitation (void);
void SetObjectsToDefaults (void);
@@ -364,48 +363,6 @@ void SetHouseToSavedRoom (void)
ForceThisRoom(smallGame.roomNumber);
}
//-------------------------------------------------------------- HandlePlayEvent
void HandlePlayEvent (void)
{
EventRecord theEvent;
GrafPtr wasPort;
long sleep = 2;
if (WaitNextEvent(everyEvent, &theEvent, sleep, nil))
{
if ((theEvent.what == updateEvt) &&
((WindowPtr)theEvent.message == mainWindow))
{
GetPort(&wasPort);
SetPortWindowPort(mainWindow);
CopyBits((BitMap *)*GetGWorldPixMap(workSrcMap),
GetPortBitMapForCopyBits(GetWindowPort(mainWindow)),
&justRoomsRect, &justRoomsRect, srcCopy);
RefreshScoreboard(kNormalTitleMode);
EndUpdate(mainWindow);
SetPort(wasPort);
}
else if ((theEvent.what == osEvt) && (theEvent.message & 0x01000000))
{
if (theEvent.message & 0x00000001) // resume event
{
switchedOut = false;
ToggleMusicWhilePlaying();
HideCursor();
// HideMenuBarOld(); // TEMP
}
else // suspend event
{
InitCursor();
switchedOut = true;
ToggleMusicWhilePlaying();
// ShowMenuBarOld(); // TEMP replace with Carbon calls
}
}
}
}
//-------------------------------------------------------------- PlayGame
void PlayGame (void)
@@ -417,10 +374,7 @@ void PlayGame (void)
if (doBackground)
{
do
{
HandlePlayEvent();
} while (switchedOut);
Delay(2, nil);
}
HandleTelephone();

View File

@@ -160,7 +160,7 @@ void SetInitialTiles (short background, Boolean doRoom)
#ifndef COMPILEDEMO
Boolean CreateNewRoom (short h, short v)
{
KeyMap theKeys;
KeyDownStates theKeys;
long howMuch;
PLError_t theErr;
short i, availableRoom;

View File

@@ -4,11 +4,13 @@
//----------------------------------------------------------------------------
//============================================================================
#include "PLEventQueue.h"
#include "PLKeyEncoding.h"
#include "PLQDraw.h"
#include "PLPasStr.h"
#include "PLResources.h"
#include "PLSound.h"
#include "PLTimeTaggedVOSEvent.h"
#include "Externs.h"
#include "Utilities.h"
@@ -372,10 +374,10 @@ long LongSquareRoot (long theNumber)
Boolean WaitForInputEvent (short seconds)
{
EventRecord theEvent;
KeyMap theKeys;
long timeToBail;
Boolean waiting, didResume;
TimeTaggedVOSEvent theEvent;
KeyDownStates theKeys;
long timeToBail;
Boolean waiting, didResume;
timeToBail = TickCount() + 60L * (long)seconds;
FlushEvents(everyEvent, 0);
@@ -387,22 +389,11 @@ Boolean WaitForInputEvent (short seconds)
GetKeys(theKeys);
if (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kControl)) || BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kAlt)) || BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kShift)))
waiting = false;
if (GetNextEvent(everyEvent, &theEvent))
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(&theEvent))
{
if ((theEvent.what == mouseDown) || (theEvent.what == keyDown))
if (theEvent.IsLMouseDownEvent() || theEvent.IsKeyDownEvent())
waiting = false;
else if ((theEvent.what == osEvt) && (theEvent.message & 0x01000000))
{
if (theEvent.message & 0x00000001) // resuming
{
didResume = true;
waiting = false;
}
else // suspending
{
InitCursor();
}
}
}
if ((seconds != -1) && (TickCount() >= timeToBail))
waiting = false;
@@ -419,7 +410,7 @@ Boolean WaitForInputEvent (short seconds)
void WaitCommandQReleased (void)
{
KeyMap theKeys;
KeyDownStates theKeys;
Boolean waiting;
waiting = true;
@@ -469,7 +460,7 @@ void GetKeyName (intptr_t message, StringPtr theName)
Boolean OptionKeyDown (void)
{
KeyMap theKeys;
KeyDownStates theKeys;
GetKeys(theKeys);
if (BitTst(theKeys, PL_KEY_EITHER_SPECIAL(kAlt)))

View File

@@ -12,7 +12,7 @@ namespace PortabilityLayer
public:
InputManagerImpl();
void GetKeys(KeyMap &keyMap) const override;
void GetKeys(KeyDownStates &keyMap) const override;
void ApplyKeyboardEvent(const GpKeyboardInputEvent &vosEvent) override;
void ApplyGamepadEvent(const GpGamepadInputEvent &vosEvent) override;
int16_t GetGamepadAxis(unsigned int playerNum, GpGamepadAxis_t gamepadAxis) override;
@@ -23,13 +23,13 @@ namespace PortabilityLayer
void ApplyEventAsKey(const GpKeyboardInputEvent &vosEvent, bool bit);
void ApplyAnalogAxisEvent(const GpGamepadAnalogAxisEvent &axisEvent);
KeyMap m_keyMap;
KeyDownStates m_keyMap;
int16_t m_axisStates[PL_INPUT_MAX_PLAYERS][GpGamepadAxes::kCount];
static InputManagerImpl ms_instance;
};
void InputManagerImpl::GetKeys(KeyMap &keyMap) const
void InputManagerImpl::GetKeys(KeyDownStates &keyMap) const
{
keyMap = m_keyMap;
}

View File

@@ -4,14 +4,14 @@
struct GpKeyboardInputEvent;
struct GpGamepadInputEvent;
struct KeyMap;
struct KeyDownStates;
namespace PortabilityLayer
{
class InputManager
{
public:
virtual void GetKeys(KeyMap &keys16) const = 0;
virtual void GetKeys(KeyDownStates &keys16) const = 0;
virtual void ApplyKeyboardEvent(const GpKeyboardInputEvent &vosEvent) = 0;
virtual void ApplyGamepadEvent(const GpGamepadInputEvent &vosEvent) = 0;
virtual int16_t GetGamepadAxis(unsigned int playerNum, GpGamepadAxis_t gamepadAxis) = 0;

View File

@@ -12,6 +12,7 @@
#include "PLCore.h"
#include "PLPasStr.h"
#include "PLResources.h"
#include "PLTimeTaggedVOSEvent.h"
#include "PLQDOffscreen.h"
#include "RenderedFont.h"
#include "QDGraf.h"
@@ -463,23 +464,28 @@ namespace PortabilityLayer
return;
}
EventRecord evt;
TimeTaggedVOSEvent evt;
bool canDismiss = false;
while (!canDismiss)
{
if (WaitNextEvent(everyEvent, &evt, 1, nullptr))
if (WaitForEvent(&evt, 1))
{
const EventCode eventCode = static_cast<EventCode>(evt.what);
switch (eventCode)
if (evt.m_vosEvent.m_eventType == GpVOSEventTypes::kMouseInput)
{
case mouseMove:
case mouseDown: // It's possible to get a mouse down event again if the mouse leaves the window and is downed again inside
ProcessMouseMoveTo(PortabilityLayer::Vec2i(evt.where.h, evt.where.v));
break;
case mouseUp:
canDismiss = true;
break;
switch (evt.m_vosEvent.m_event.m_mouseInputEvent.m_eventType)
{
case GpMouseEventTypes::kMove:
ProcessMouseMoveTo(PortabilityLayer::Vec2i(evt.m_vosEvent.m_event.m_mouseInputEvent.m_x, evt.m_vosEvent.m_event.m_mouseInputEvent.m_y));
break;
case GpMouseEventTypes::kDown:
if (evt.m_vosEvent.m_event.m_mouseInputEvent.m_button == GpMouseButtons::kLeft)
ProcessMouseMoveTo(PortabilityLayer::Vec2i(evt.m_vosEvent.m_event.m_mouseInputEvent.m_x, evt.m_vosEvent.m_event.m_mouseInputEvent.m_y));
break;
case GpMouseEventTypes::kUp:
if (evt.m_vosEvent.m_event.m_mouseInputEvent.m_button == GpMouseButtons::kLeft)
canDismiss = true;
break;
}
}
}
}

View File

@@ -33,6 +33,7 @@
#include "PLBigEndian.h"
#include "PLEventQueue.h"
#include "PLKeyEncoding.h"
#include "PLTimeTaggedVOSEvent.h"
#include "QDManager.h"
#include "Vec2i.h"
#include "WindowDef.h"
@@ -64,37 +65,25 @@ static bool ConvertFilenameToSafePStr(const char *str, uint8_t *pstr)
return true;
}
static void TranslateMouseInputEvent(const GpMouseInputEvent &vosEvent, PortabilityLayer::EventQueue *queue)
static void TranslateMouseInputEvent(const GpVOSEvent &vosEventBase, uint32_t timestamp, PortabilityLayer::EventQueue *queue)
{
const GpMouseInputEvent &vosEvent = vosEventBase.m_event.m_mouseInputEvent;
bool requeue = false;
if (vosEvent.m_button == GpMouseButtons::kLeft)
{
if (vosEvent.m_eventType == GpMouseEventTypes::kDown)
{
if (EventRecord *evt = queue->Enqueue())
{
evt->what = mouseDown;
evt->where.h = std::min<int32_t>(INT16_MAX, std::max<int32_t>(INT16_MIN, vosEvent.m_x));
evt->where.v = std::min<int32_t>(INT16_MAX, std::max<int32_t>(INT16_MIN, vosEvent.m_y));
}
}
requeue = true;
else if (vosEvent.m_eventType == GpMouseEventTypes::kUp)
{
if (EventRecord *evt = queue->Enqueue())
{
evt->what = mouseUp;
evt->where.h = std::min<int32_t>(INT16_MAX, std::max<int32_t>(INT16_MIN, vosEvent.m_x));
evt->where.v = std::min<int32_t>(INT16_MAX, std::max<int32_t>(INT16_MIN, vosEvent.m_y));
}
}
requeue = true;
}
else if (vosEvent.m_eventType == GpMouseEventTypes::kMove)
requeue = true;
if (requeue)
{
if (EventRecord *evt = queue->Enqueue())
{
evt->what = mouseMove;
evt->where.h = std::min<int32_t>(INT16_MAX, std::max<int32_t>(INT16_MIN, vosEvent.m_x));
evt->where.v = std::min<int32_t>(INT16_MAX, std::max<int32_t>(INT16_MIN, vosEvent.m_y));
}
if (TimeTaggedVOSEvent *evt = queue->Enqueue())
*evt = TimeTaggedVOSEvent::Create(vosEventBase, timestamp);
}
}
@@ -107,8 +96,10 @@ static void TranslateGamepadInputEvent(const GpGamepadInputEvent &vosEvent, Port
PL_DEAD(queue);
}
static void TranslateKeyboardInputEvent(const GpKeyboardInputEvent &vosEvent, PortabilityLayer::EventQueue *queue)
static void TranslateKeyboardInputEvent(const GpVOSEvent &vosEventBase, uint32_t timestamp, PortabilityLayer::EventQueue *queue)
{
const GpKeyboardInputEvent &vosEvent = vosEventBase.m_event.m_keyboardInputEvent;
PL_STATIC_ASSERT((1 << PL_INPUT_PLAYER_INDEX_BITS) >= PL_INPUT_MAX_PLAYERS);
PL_STATIC_ASSERT((1 << PL_INPUT_TYPE_CODE_BITS) >= KeyEventType_Count);
@@ -117,74 +108,51 @@ static void TranslateKeyboardInputEvent(const GpKeyboardInputEvent &vosEvent, Po
if (vosEvent.m_eventType == GpKeyboardInputEventTypes::kUp || vosEvent.m_eventType == GpKeyboardInputEventTypes::kDown)
inputManager->ApplyKeyboardEvent(vosEvent);
intptr_t msg = 0;
if (TimeTaggedVOSEvent *evt = queue->Enqueue())
*evt = TimeTaggedVOSEvent::Create(vosEventBase, timestamp);
}
intptr_t PackVOSKeyCode(const GpKeyboardInputEvent &vosEvent)
{
switch (vosEvent.m_keyIDSubset)
{
case GpKeyIDSubsets::kASCII:
msg = PL_KEY_ASCII(vosEvent.m_key.m_asciiChar);
break;
return PL_KEY_ASCII(vosEvent.m_key.m_asciiChar);
case GpKeyIDSubsets::kFKey:
msg = PL_KEY_FKEY(vosEvent.m_key.m_fKey);
break;
return PL_KEY_FKEY(vosEvent.m_key.m_fKey);
case GpKeyIDSubsets::kNumPadNumber:
msg = PL_KEY_NUMPAD_NUMBER(vosEvent.m_key.m_numPadNumber);
break;
return PL_KEY_NUMPAD_NUMBER(vosEvent.m_key.m_numPadNumber);
case GpKeyIDSubsets::kSpecial:
msg = PL_KEY_SPECIAL_ENCODE(vosEvent.m_key.m_specialKey);
return PL_KEY_SPECIAL_ENCODE(vosEvent.m_key.m_specialKey);
break;
case GpKeyIDSubsets::kNumPadSpecial:
msg = PL_KEY_NUMPAD_SPECIAL_ENCODE(vosEvent.m_key.m_numPadSpecialKey);
return PL_KEY_NUMPAD_SPECIAL_ENCODE(vosEvent.m_key.m_numPadSpecialKey);
break;
case GpKeyIDSubsets::kUnicode:
for (int i = 128; i < 256; i++)
{
if (PortabilityLayer::MacRoman::g_toUnicode[i] == vosEvent.m_key.m_unicodeChar)
{
msg = PL_KEY_MACROMAN(i);
break;
}
return PL_KEY_MACROMAN(i);
}
break;
case GpKeyIDSubsets::kGamepadButton:
msg = PL_KEY_GAMEPAD_BUTTON_ENCODE(vosEvent.m_key.m_gamepadKey.m_button, vosEvent.m_key.m_gamepadKey.m_player);
break;
return PL_KEY_GAMEPAD_BUTTON_ENCODE(vosEvent.m_key.m_gamepadKey.m_button, vosEvent.m_key.m_gamepadKey.m_player);
default:
PL_NotYetImplemented();
return 0;
}
if (msg == 0)
return;
EventRecord *evt = queue->Enqueue();
switch (vosEvent.m_eventType)
{
case GpKeyboardInputEventTypes::kUp:
evt->what = keyUp;
break;
case GpKeyboardInputEventTypes::kDown:
evt->what = keyUp;
break;
case GpKeyboardInputEventTypes::kAuto:
evt->what = autoKey;
break;
};
evt->message = msg;
PL_NotYetImplemented_TODO("Modifiers");
return 0;
}
static void TranslateVOSEvent(const GpVOSEvent *vosEvent, PortabilityLayer::EventQueue *queue)
static void TranslateVOSEvent(const GpVOSEvent *vosEvent, uint32_t timestamp, PortabilityLayer::EventQueue *queue)
{
switch (vosEvent->m_eventType)
{
case GpVOSEventTypes::kMouseInput:
TranslateMouseInputEvent(vosEvent->m_event.m_mouseInputEvent, queue);
TranslateMouseInputEvent(*vosEvent, timestamp, queue);
break;
case GpVOSEventTypes::kKeyboardInput:
TranslateKeyboardInputEvent(vosEvent->m_event.m_keyboardInputEvent, queue);
TranslateKeyboardInputEvent(*vosEvent, timestamp, queue);
break;
case GpVOSEventTypes::kGamepadInput:
TranslateGamepadInputEvent(vosEvent->m_event.m_gamepadInputEvent, queue);
@@ -192,14 +160,14 @@ static void TranslateVOSEvent(const GpVOSEvent *vosEvent, PortabilityLayer::Even
}
}
static void ImportVOSEvents()
static void ImportVOSEvents(uint32_t timestamp)
{
PortabilityLayer::EventQueue *plQueue = PortabilityLayer::EventQueue::GetInstance();
PortabilityLayer::HostVOSEventQueue *evtQueue = PortabilityLayer::HostVOSEventQueue::GetInstance();
while (const GpVOSEvent *evt = evtQueue->GetNext())
{
TranslateVOSEvent(evt, plQueue);
TranslateVOSEvent(evt, timestamp, plQueue);
evtQueue->DischargeOne();
}
}
@@ -283,7 +251,7 @@ void Delay(int ticks, UInt32 *endTickCount)
PortabilityLayer::SuspendApplication(PortabilityLayer::HostSuspendCallID_Delay, args, nullptr);
ImportVOSEvents();
ImportVOSEvents(PortabilityLayer::DisplayDeviceManager::GetInstance()->GetTickCount());
}
if (endTickCount)
@@ -436,30 +404,6 @@ void SetWTitle(WindowPtr window, const PLPasStr &title)
PL_NotYetImplemented();
}
bool PeekNextEvent(int32_t eventMask, EventRecord *event)
{
assert(eventMask == everyEvent); // We don't support other use cases
PortabilityLayer::EventQueue *queue = PortabilityLayer::EventQueue::GetInstance();
const EventRecord *record = queue->Peek();
if (record)
{
*event = *record;
return PL_TRUE;
}
else
return PL_FALSE;
}
bool GetNextEvent(int32_t eventMask, EventRecord *event)
{
assert(eventMask == everyEvent); // We don't support other use cases
PortabilityLayer::EventQueue *queue = PortabilityLayer::EventQueue::GetInstance();
return queue->Dequeue(event) ? PL_TRUE : PL_FALSE;
}
long MenuSelect(Point point)
{
int16_t menuID = 0;
@@ -481,7 +425,7 @@ long TickCount()
return PortabilityLayer::DisplayDeviceManager::GetInstance()->GetTickCount();
}
void GetKeys(KeyMap &keyMap)
void GetKeys(KeyDownStates &keyMap)
{
PortabilityLayer::InputManager::GetInstance()->GetKeys(keyMap);
}
@@ -496,7 +440,7 @@ short HiWord(Int32 v)
return (((v >> 16) ^ 0x8000) & 0xffff) - 0x8000;
}
static bool BitTestEitherSpecial(const KeyMap &keyMap, int eitherSpecial)
static bool BitTestEitherSpecial(const KeyDownStates &keyMap, int eitherSpecial)
{
switch (eitherSpecial)
{
@@ -512,7 +456,7 @@ static bool BitTestEitherSpecial(const KeyMap &keyMap, int eitherSpecial)
}
}
bool BitTst(const KeyMap &keyMap, int encodedKey)
bool BitTst(const KeyDownStates &keyMap, int encodedKey)
{
const KeyEventType evtType = PL_KEY_GET_EVENT_TYPE(encodedKey);
const int evtValue = PL_KEY_GET_VALUE(encodedKey);
@@ -947,23 +891,22 @@ void BlockMove(const void *src, void *dest, Size size)
memcpy(dest, src, size);
}
Boolean WaitNextEvent(int eventMask, EventRecord *eventOut, long sleep, void *unknown)
bool WaitForEvent(TimeTaggedVOSEvent *eventOut, uint32_t ticks)
{
for (;;)
{
Boolean hasEvent = GetNextEvent(eventMask, eventOut);
if (hasEvent)
return hasEvent;
if (PortabilityLayer::EventQueue::GetInstance()->Dequeue(eventOut))
return true;
Delay(1, nullptr);
if (sleep == 0)
if (ticks == 0)
break;
sleep--;
ticks--;
}
return PL_FALSE;
return false;
}
void DrawControls(WindowPtr window)

View File

@@ -14,6 +14,8 @@
#endif
struct IGpColorCursor;
struct GpVOSEvent;
struct TimeTaggedVOSEvent;
namespace PortabilityLayer
{
@@ -177,7 +179,7 @@ typedef THandle<VersionRecord> VersRecHndl;
typedef WindowPtr WindowRef; // wtf?
struct KeyMap;
struct KeyDownStates;
enum RegionID
{
@@ -277,18 +279,15 @@ void MoveWindow(WindowPtr window, int x, int y, Boolean moveToFront);
void ShowWindow(WindowPtr window);
void SetWTitle(WindowPtr window, const PLPasStr &title);
bool PeekNextEvent(int32_t eventMask, EventRecord *event);
bool GetNextEvent(int32_t eventMask, EventRecord *event);
long MenuSelect(Point point); // Breaks into menu select routine (in practice we'll just forward one from the queue?)
long MenuKey(int charCode);
long TickCount();
void GetKeys(KeyMap &keyMap);
void GetKeys(KeyDownStates &keyMap);
short LoWord(Int32 v);
short HiWord(Int32 v);
bool BitTst(const KeyMap &keyMap, int bit);
bool BitTst(const KeyDownStates &keyMap, int bit);
void NumToString(long number, unsigned char *str);
void ParamText(const PLPasStr &title, const PLPasStr &a, const PLPasStr &b, const PLPasStr &c);
@@ -338,11 +337,9 @@ void *NewPtr(Size size);
void *NewPtrClear(Size size);
void DisposePtr(void *ptr);
void PurgeSpace(long *totalFree, long *contiguousFree);
void BlockMove(const void *src, void *dest, Size size);
Boolean WaitNextEvent(int eventMask, EventRecord *eventOut, long sleep, void *unknown);
bool WaitForEvent(TimeTaggedVOSEvent *evt, uint32_t ticks);
void DrawControls(WindowPtr window);
void DrawGrowIcon(WindowPtr window);

View File

@@ -1,4 +1,5 @@
#include "PLEventQueue.h"
#include "PLTimeTaggedVOSEvent.h"
#include <stdint.h>
@@ -10,16 +11,16 @@ namespace PortabilityLayer
EventQueueImpl();
~EventQueueImpl();
bool Dequeue(EventRecord *evt) override;
const EventRecord *Peek() const override;
EventRecord *Enqueue() override;
bool Dequeue(TimeTaggedVOSEvent *evt) override;
const TimeTaggedVOSEvent *Peek() const override;
TimeTaggedVOSEvent *Enqueue() override;
static EventQueueImpl *GetInstance();
private:
static const size_t kMaxEvents = 10000;
EventRecord m_events[kMaxEvents];
TimeTaggedVOSEvent m_events[kMaxEvents];
size_t m_firstEvent;
size_t m_numQueuedEvents;
@@ -36,7 +37,7 @@ namespace PortabilityLayer
{
}
bool EventQueueImpl::Dequeue(EventRecord *evt)
bool EventQueueImpl::Dequeue(TimeTaggedVOSEvent *evt)
{
if (m_numQueuedEvents == 0)
return false;
@@ -53,7 +54,7 @@ namespace PortabilityLayer
return true;
}
const EventRecord *EventQueueImpl::Peek() const
const TimeTaggedVOSEvent *EventQueueImpl::Peek() const
{
if (m_numQueuedEvents == 0)
return nullptr;
@@ -62,7 +63,7 @@ namespace PortabilityLayer
}
EventRecord *EventQueueImpl::Enqueue()
TimeTaggedVOSEvent *EventQueueImpl::Enqueue()
{
if (m_numQueuedEvents == kMaxEvents)
return nullptr;
@@ -73,8 +74,8 @@ namespace PortabilityLayer
m_numQueuedEvents++;
EventRecord *evt = m_events + nextEvent;
memset(evt, 0, sizeof(EventRecord));
TimeTaggedVOSEvent *evt = m_events + nextEvent;
memset(evt, 0, sizeof(TimeTaggedVOSEvent));
return evt;
}

View File

@@ -4,14 +4,16 @@
#include <stdint.h>
struct TimeTaggedVOSEvent;
namespace PortabilityLayer
{
class EventQueue
{
public:
virtual bool Dequeue(EventRecord *evt) = 0;
virtual const EventRecord *Peek() const = 0;
virtual EventRecord *Enqueue() = 0;
virtual bool Dequeue(TimeTaggedVOSEvent *evt) = 0;
virtual const TimeTaggedVOSEvent *Peek() const = 0;
virtual TimeTaggedVOSEvent *Enqueue() = 0;
static EventQueue *GetInstance();
};

View File

@@ -3,6 +3,8 @@
#include "GpVOSEvent.h"
#include "GpBitfield.h"
struct GpKeyboardInputEvent;
enum KeyEventType
{
KeyEventType_Special,
@@ -44,10 +46,12 @@ namespace KeyEventEitherSpecialCategories
#define PL_KEY_GAMEPAD_BUTTON(k, pl) ((KeyEventType_GamepadButton) | (pl << PL_INPUT_TYPE_CODE_BITS) | ((GpGamepadButtons::k) << (PL_INPUT_TYPE_CODE_BITS + PL_INPUT_PLAYER_INDEX_BITS)))
#define PL_KEY_GAMEPAD_BUTTON_ENCODE(k, pl) ((KeyEventType_GamepadButton) | (pl << PL_INPUT_TYPE_CODE_BITS) | ((k) << (PL_INPUT_TYPE_CODE_BITS + PL_INPUT_PLAYER_INDEX_BITS)))
#define PL_KEY_GET_EVENT_TYPE(k) (static_cast<KeyEventType>(k & ((1 << PL_INPUT_TYPE_CODE_BITS) - 1)))
#define PL_KEY_GET_VALUE(k) ((k) >> PL_INPUT_TYPE_CODE_BITS)
#define PL_KEY_GET_EVENT_TYPE(k) (static_cast<KeyEventType>(k & ((1 << PL_INPUT_TYPE_CODE_BITS) - 1)))
#define PL_KEY_GET_VALUE(k) ((k) >> PL_INPUT_TYPE_CODE_BITS)
struct KeyMap
intptr_t PackVOSKeyCode(const GpKeyboardInputEvent &evt);
struct KeyDownStates
{
GpBitfield<GpKeySpecials::kCount> m_special;
GpBitfield<128> m_ascii;

View File

@@ -0,0 +1,22 @@
#include "PLTimeTaggedVOSEvent.h"
// Helpers for common cases
bool TimeTaggedVOSEvent::IsKeyDownEvent() const
{
if (m_vosEvent.m_eventType != GpVOSEventTypes::kKeyboardInput)
return false;
const GpKeyboardInputEvent &keyboardEvent = m_vosEvent.m_event.m_keyboardInputEvent;
return keyboardEvent.m_eventType == GpKeyboardInputEventTypes::kDown;
}
bool TimeTaggedVOSEvent::IsLMouseDownEvent() const
{
if (m_vosEvent.m_eventType != GpVOSEventTypes::kMouseInput)
return false;
const GpMouseInputEvent &mouseEvent = m_vosEvent.m_event.m_mouseInputEvent;
return mouseEvent.m_eventType == GpMouseEventTypes::kDown && mouseEvent.m_button == GpMouseButtons::kLeft;
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "GpVOSEvent.h"
struct TimeTaggedVOSEvent
{
GpVOSEvent m_vosEvent;
uint32_t m_timestamp;
static TimeTaggedVOSEvent Create(const GpVOSEvent &vosEvent, uint32_t timestamp);
// Helpers for common cases
bool IsKeyDownEvent() const;
bool IsLMouseDownEvent() const;
};
inline TimeTaggedVOSEvent TimeTaggedVOSEvent::Create(const GpVOSEvent &vosEvent, uint32_t timestamp)
{
TimeTaggedVOSEvent result;
result.m_vosEvent = vosEvent;
result.m_timestamp = timestamp;
return result;
}

View File

@@ -203,6 +203,7 @@
<ClInclude Include="MenuManager.h" />
<ClInclude Include="PlotDirection.h" />
<ClInclude Include="PLStandardColors.h" />
<ClInclude Include="PLTimeTaggedVOSEvent.h" />
<ClInclude Include="ResolvedColor.h" />
<ClInclude Include="ScanlineMaskBuilder.h" />
<ClInclude Include="ScanlineMaskConverter.h" />
@@ -308,6 +309,7 @@
<ClCompile Include="PLSound.cpp" />
<ClCompile Include="PLStandardColors.cpp" />
<ClCompile Include="PLStringCompare.cpp" />
<ClCompile Include="PLTimeTaggedVOSEvent.cpp" />
<ClCompile Include="ScanlineMask.cpp" />
<ClCompile Include="ScanlineMaskBuilder.cpp" />
<ClCompile Include="ScanlineMaskConverter.cpp" />

View File

@@ -408,6 +408,9 @@
<ClInclude Include="PLArrayViewIterator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PLTimeTaggedVOSEvent.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CFileStream.cpp">
@@ -617,5 +620,8 @@
<ClCompile Include="PLStandardColors.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="PLTimeTaggedVOSEvent.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -168,12 +168,6 @@ namespace PortabilityLayer
return nullptr;
}
if (EventRecord *evt = PortabilityLayer::EventQueue::GetInstance()->Enqueue())
{
evt->what = updateEvt;
evt->message = reinterpret_cast<intptr_t>(static_cast<Window*>(window));
}
return window;
}
@@ -312,12 +306,6 @@ namespace PortabilityLayer
void WindowManagerImpl::ResizeWindow(Window *window, int width, int height)
{
static_cast<WindowImpl*>(window)->Resize(width, height);
if (EventRecord *evt = PortabilityLayer::EventQueue::GetInstance()->Enqueue())
{
evt->what = updateEvt;
evt->message = reinterpret_cast<intptr_t>(window);
}
}
void WindowManagerImpl::MoveWindow(Window *window, int x, int y)