Reduce high CPU when sitting idle at the console. (#288)

* Reduce high CPU when sitting idle at the console.

* Update ConsoleWindow.cc

Add include for Patches.h. This is where OSTrap and ToolTrap are defined.

* Update ConsoleWindow.cc

Define OSTrap and ToolTrap instead of relying on a header file to do it.

* Update ConsoleWindow.cc

Added two different routineAvailable() functions.

* Update ConsoleWindow.cc

Implement changes suggested by the maintainer.
- Declare new functions as static.
This commit is contained in:
programmingkidx 2025-03-31 04:21:01 -04:00 committed by GitHub
parent 3672e5e663
commit 017f80ad50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -26,13 +26,19 @@
#include <unordered_map>
#include <cstring>
#include <TextUtils.h>
#include <functional>
#include <OSUtils.h>
#include <Traps.h>
using namespace std;
using namespace retro;
namespace
{
std::unordered_map<WindowPtr, ConsoleWindow*> *windows = NULL;
function<bool(EventRecord *)> getEvent;
}
static void setupEventFunction();
ConsoleWindow::ConsoleWindow(Rect r, ConstStr255Param title)
{
@ -56,6 +62,7 @@ ConsoleWindow::ConsoleWindow(Rect r, ConstStr255Param title)
(*windows)[win] = this;
Init(port, portRect);
setupEventFunction();
}
ConsoleWindow::~ConsoleWindow()
@ -101,7 +108,7 @@ char ConsoleWindow::WaitNextChar()
#endif
SystemTask();
Idle();
while(!GetNextEvent(everyEvent, &event))
while(!getEvent(&event))
{
SystemTask();
Idle();
@ -154,3 +161,62 @@ char ConsoleWindow::WaitNextChar()
return event.message & charCodeMask;
}
// Wrapper for the WaitNextEvent() function
static bool waitNextEventWrapper(EventRecord *event)
{
const int sleepValue = 5;
const RgnHandle mouseRegion = nil;
return WaitNextEvent(everyEvent, event, sleepValue, mouseRegion);
}
#if !(TARGET_API_MAC_CARBON)
// Wrapper for the GetNextEvent() function
static bool getNextEventWrapper(EventRecord *event)
{
return GetNextEvent(everyEvent, event);
}
// Determines if a Toolbox routine is available
static bool routineAvailable(int trapWord) {
TrapType trType;
int OSTrap = 0;
int ToolTrap = 1;
// Determine whether it is an Operating System or Toolbox routine
if ((trapWord & 0x0800) == 0) {
trType = OSTrap;
}
else {
trType = ToolTrap;
}
// Filter cases where older systems mask with 0x1FF rather than 0x3FF
if ((trType == ToolTrap) &&
((trapWord & 0x03FF) >= 0x200) &&
(GetToolboxTrapAddress(0xA86E) == GetToolboxTrapAddress(0xAA6E))) {
return false;
}
else {
return (NGetTrapAddress(trapWord, trType) != GetToolboxTrapAddress(_Unimplemented));
}
}
#endif /* TARGET_API_MAC_CARBON */
// Decides which event retrieving function to use
static void setupEventFunction()
{
#if TARGET_API_MAC_CARBON
getEvent = waitNextEventWrapper;
#else
if (routineAvailable(_WaitNextEvent) == true) {
getEvent = waitNextEventWrapper;
}
else {
getEvent = getNextEventWrapper;
}
#endif
}