Closed exellian closed 2 years ago
For Problem 1. I found a way to achieve polling on macos by trying out several things:
So the normal way of running a main loop is to call the run method on the NSApp instance:
[NSApp run]
This approach is an internal implementation and follows a waiting based approach. The way I found to create custom polling event loop is the following:
while (running) {
NSEvent* event = [
NSApp
nextEventMatchingMask:NSEventMaskAny
untilDate:nil // that was the trick
inMode:NSDefaultRunLoopMode
dequeue:YES
];
if (event != NULL) {
[NSApp sendEvent:event];
}
[NSApp updateWindows];
}
The main trick is to pass nil to the untilDate parameter of the call to nextEventMatchingMask method. This has the effect that the nextEventMatchingMask also returns when no event is in the queue and it returns NULL in this case. I tested the code example above and got 100% CPU usage and working window event handling which indicates that polling works. I can't verify that this is the way you should do it because apple lacks documentation and also doesn't provide open source of cocoa.
For a full version of the event loop we should look at the cocotron implementation of NSApplication and at the blog article demystifying-nsapplication
Choosing the right application lifecycle is somehow hard:
Situation Description:
Problem Description:
Problem resolution