Skip to content

Commit 8653dad

Browse files
committed
First pass at MPLEventLoop for matplotlib#30419
1 parent 40f21d8 commit 8653dad

5 files changed

Lines changed: 269 additions & 111 deletions

File tree

‎src/macosx/MPLEventLoop.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#import <AppKit/AppKit.h>
2+
#import <Python.h>
3+
#import "MPLUtils.h"
4+
5+
NS_ASSUME_NONNULL_BEGIN
6+
7+
@interface MPLEventLoop : NSObject
8+
9+
+ (instancetype) sharedInstance;
10+
11+
// Calls to -spin... may be nested.
12+
- (void) spinUntilStandardInputActivity;
13+
- (void) spinUntilNoEvents;
14+
15+
// Calls to -run... may not be nested. Will call -[NSApp run]
16+
- (void) runUntilTimeout:(double)timeout; // 0 = forever
17+
18+
- (void) runUntilStopCondition:(BOOL (^)(void))stopCondition;
19+
- (void) checkStopCondition;
20+
21+
// Stops all active loops (both -spin... and -run...)
22+
- (void) stop;
23+
24+
@end
25+
26+
NS_ASSUME_NONNULL_END

‎src/macosx/MPLEventLoop.m‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
#import "MPLEventLoop.h"
2+
#import "MPLFigureManager.h"
3+
#import "MPLUtils.h"
4+
5+
6+
@implementation MPLEventLoop {
7+
// Keeps track of all active loops (both -spin... and -run...)
8+
// Needed so we can post enough events to cancel all loops
9+
NSInteger _loopCount;
10+
11+
// Each -spin... loop checks this to see if it should stop.
12+
// We can't use -[NSApplication isRunning] as spin loops can be used
13+
// while not in a running state (flush_events())
14+
BOOL _shouldStop;
15+
16+
// Makes sure that [NSApp run] was called at least once for proper initialization
17+
BOOL _wasRunCalledAtLeastOnce;
18+
19+
// Internal state for dealing with -runUntilNoFigureManagers
20+
BOOL (^_stopCondition)(void);
21+
}
22+
23+
24+
#pragma mark - Lifecycle
25+
26+
+ (instancetype) sharedInstance
27+
{
28+
static MPLEventLoop *sharedInstance;
29+
30+
static dispatch_once_t onceToken;
31+
dispatch_once(&onceToken, ^{
32+
sharedInstance = [[MPLEventLoop alloc] init];
33+
});
34+
35+
return sharedInstance;
36+
}
37+
38+
- (instancetype) init
39+
{
40+
if ((self = [super init])) {
41+
_wasRunCalledAtLeastOnce = [NSApp isRunning];
42+
}
43+
44+
return self;
45+
}
46+
47+
48+
#pragma mark - Private Methods
49+
50+
- (void) _postInternalEvent
51+
{
52+
// There exists a very small possibility that we are running as a framework
53+
// inside of another app which is using NSEventTypeApplicationDefined events.
54+
// Hence, set subtype to a value unlikely to conflict.
55+
NSEvent *event = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
56+
location: NSZeroPoint
57+
modifierFlags: 0
58+
timestamp: 0
59+
windowNumber: 0
60+
context: nil
61+
subtype: INT16_MAX
62+
data1: 0
63+
data2: 0];
64+
65+
// +[NSEvent otherEventWithType:...] is declared nullable but will not return
66+
// nil for these constant, valid arguments; guard defensively anyway.
67+
if (event) {
68+
[NSApp postEvent: event atStart: YES];
69+
}
70+
}
71+
72+
- (void) _ensureRunWasCalledAtLeastOnce
73+
{
74+
if (!_wasRunCalledAtLeastOnce) {
75+
dispatch_async(dispatch_get_main_queue(), ^{ [NSApp stop:self]; });
76+
[NSApp run];
77+
_wasRunCalledAtLeastOnce = YES;
78+
}
79+
}
80+
81+
82+
#pragma mark - Public Methods
83+
84+
- (void) spinUntilStandardInputActivity
85+
{
86+
[self _ensureRunWasCalledAtLeastOnce];
87+
88+
__block int inputDidOccur = 0;
89+
__weak id weakSelf = self;
90+
91+
dispatch_source_t source = dispatch_source_create(
92+
DISPATCH_SOURCE_TYPE_READ, STDIN_FILENO, 0,
93+
dispatch_get_main_queue()
94+
);
95+
96+
dispatch_source_set_event_handler(source, ^{
97+
inputDidOccur = 1;
98+
dispatch_source_cancel(source);
99+
[weakSelf _postInternalEvent];
100+
});
101+
102+
dispatch_resume(source);
103+
104+
_loopCount++;
105+
106+
while (!inputDidOccur) {
107+
@autoreleasepool {
108+
NSEvent *event = [NSApp nextEventMatchingMask: NSEventMaskAny
109+
untilDate: [NSDate distantFuture]
110+
inMode: NSDefaultRunLoopMode
111+
dequeue: YES];
112+
113+
[NSApp sendEvent:event];
114+
}
115+
}
116+
117+
_loopCount--;
118+
if (_loopCount == 0) _shouldStop = NO;
119+
}
120+
121+
- (void) spinUntilNoEvents
122+
{
123+
[self _ensureRunWasCalledAtLeastOnce];
124+
125+
_loopCount++;
126+
127+
while (!_shouldStop) {
128+
@autoreleasepool {
129+
NSEvent *event = [NSApp nextEventMatchingMask: NSEventMaskAny
130+
untilDate: [NSDate distantPast]
131+
inMode: NSDefaultRunLoopMode
132+
dequeue: YES];
133+
134+
if (!event) break;
135+
136+
[NSApp sendEvent:event];
137+
}
138+
}
139+
140+
_loopCount--;
141+
if (_loopCount == 0) _shouldStop = NO;
142+
}
143+
144+
- (void) runUntilTimeout:(double)timeout
145+
{
146+
if ([NSApp isRunning]) {
147+
PyErr_SetString(PyExc_RuntimeError, "An event loop is already running");
148+
return;
149+
}
150+
151+
_loopCount++;
152+
153+
if (timeout > 0.0) {
154+
[self performSelector:@selector(stop) withObject:nil afterDelay:timeout];
155+
}
156+
157+
_wasRunCalledAtLeastOnce = YES;
158+
[NSApp run];
159+
160+
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stop) object:nil];
161+
162+
_loopCount--;
163+
if (!_loopCount) _shouldStop = NO;
164+
}
165+
166+
- (void) runUntilStopCondition:(BOOL (^)(void))stopCondition
167+
{
168+
if ([NSApp isRunning]) {
169+
PyErr_SetString(PyExc_RuntimeError, "An event loop is already running");
170+
return;
171+
}
172+
173+
_loopCount++;
174+
175+
_stopCondition = stopCondition;
176+
_wasRunCalledAtLeastOnce = YES;
177+
[NSApp run];
178+
_stopCondition = nil;
179+
180+
_loopCount--;
181+
if (!_loopCount) _shouldStop = NO;
182+
}
183+
184+
- (void) checkStopCondition
185+
{
186+
if (_stopCondition && _stopCondition()) {
187+
[self stop];
188+
}
189+
}
190+
191+
- (void) stop
192+
{
193+
_shouldStop = YES;
194+
195+
[NSApp stop:self];
196+
197+
// Post X events, where X is the number of active loops
198+
for (NSInteger i = 0; i < _loopCount; i++) {
199+
[self _postInternalEvent];
200+
}
201+
}
202+
203+
204+
@end
205+

0 commit comments

Comments
 (0)