Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2e5bfe1

Browse filesBrowse files
committed
FIX: Add PyOS_InputHook back to macos backend
The PyOS_InputHook is used to run our event loop while waiting on standard input from the user. This means we should flush our events while waiting for any input from the user so the figures are responsive.
1 parent 2cb8c6a commit 2e5bfe1
Copy full SHA for 2e5bfe1

File tree

Expand file treeCollapse file tree

1 file changed

+63
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+63
-0
lines changed

‎src/_macosx.m

Copy file name to clipboardExpand all lines: src/_macosx.m
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,66 @@
4141
static bool keyChangeCapsLock = false;
4242
/* Keep track of the current mouse up/down state for open/closed cursor hand */
4343
static bool leftMouseGrabbing = false;
44+
/* Keep track of whether stdin has been received */
45+
bool stdin_received = false;
46+
bool stdin_sigint = false;
47+
48+
// Signal handler for SIGINT
49+
void handleSigint(int signal) {
50+
stdin_sigint = true;
51+
}
52+
53+
int wait_for_stdin() {
54+
@autoreleasepool {
55+
stdin_received = false;
56+
stdin_sigint = false;
57+
58+
// Set up a SIGINT handler to interrupt the event loop if ctrl+c comes in too
59+
struct sigaction action = {0};
60+
action.sa_handler = handleSigint;
61+
sigaction(SIGINT, &action, NULL);
62+
63+
// Create an NSFileHandle for standard input
64+
NSFileHandle *stdinHandle = [NSFileHandle fileHandleWithStandardInput];
65+
66+
// Register for data available notifications on standard input
67+
[[NSNotificationCenter defaultCenter] addObserverForName: NSFileHandleDataAvailableNotification
68+
object: stdinHandle
69+
queue: [NSOperationQueue mainQueue] // Use the main queue
70+
usingBlock: ^(NSNotification *notification) {
71+
// Mark that input has been received
72+
stdin_received = true;
73+
}
74+
];
75+
76+
// Wait in the background for anything that happens to stdin
77+
[stdinHandle waitForDataInBackgroundAndNotify];
78+
79+
// continuously run an event loop until the stdin_received flag is set to exit
80+
while (!stdin_received && !stdin_sigint) {
81+
while (true) {
82+
NSEvent *event = [NSApp nextEventMatchingMask: NSEventMaskAny
83+
untilDate: [NSDate distantPast]
84+
inMode: NSDefaultRunLoopMode
85+
dequeue: YES];
86+
if (!event) { break; }
87+
[NSApp sendEvent: event];
88+
}
89+
// We need to run the run loop for a short time to allow the
90+
// events to be processed and keep flushing them while we wait for stdin
91+
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
92+
}
93+
94+
// reset the signal handler to the default behavior
95+
signal(SIGINT, SIG_DFL);
96+
if (stdin_sigint) {
97+
raise(SIGINT);
98+
}
99+
// Remove the input handler as an observer
100+
[[NSNotificationCenter defaultCenter] removeObserver: stdinHandle];
101+
return 1;
102+
}
103+
}
44104

45105
/* ---------------------------- Cocoa classes ---------------------------- */
46106

@@ -139,6 +199,9 @@ static void lazy_init(void) {
139199
NSApp = [NSApplication sharedApplication];
140200
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
141201

202+
// Run our own event loop while waiting for stdin on the Python side
203+
// this is needed to keep the application responsive while waiting for input
204+
PyOS_InputHook = wait_for_stdin;
142205
}
143206

144207
static PyObject*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.