master ef448d2ca99: NS port freezes without beach ball

Alan Third <[email protected]>
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit ef448d2ca996410f1f87b19740c1d4c2c6f37f7f
Author: [email protected] <[email protected]>
Commit: Alan Third <[email protected]>

    NS port freezes without beach ball
    
    * src/nsterm.m (ns_send_appdefined): Address the app-defined event to a
    window that exists.  (Bug#72496)
    
    Copyright-paperwork-exempt: yes
---
 src/nsterm.m | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index 20967b7581f..5cca003f18d 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -5029,6 +5029,7 @@ ns_send_appdefined (int value)
   if (send_appdefined)
     {
       NSEvent *nxev;
+      NSWindow *dest;
 
       /* We only need one NX_APPDEFINED event to stop NXApp from running.  */
       send_appdefined = NO;
@@ -5041,11 +5042,28 @@ ns_send_appdefined (int value)
           timed_entry = nil;
         }
 
+      /* Address the event to a window that actually exists.  With no main
+         window -- miniaturized, or mid handover of key/main status -- the
+         window number would be 0 and AppKit would silently discard the
+         event.  That is fatal here: send_appdefined has just been cleared
+         and timed_entry invalidated, so nothing would ever end [NSApp run]
+         again, and Emacs would hang forever with its UI unresponsive.  */
+      dest = [NSApp mainWindow];
+      if (dest == nil)
+        dest = [NSApp keyWindow];
+      if (dest == nil)
+        for (NSWindow *cand in [NSApp windows])
+          if ([cand windowNumber] > 0)
+            {
+              dest = cand;
+              break;
+            }
+
       nxev = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
                                 location: NSMakePoint (0, 0)
                            modifierFlags: 0
                                timestamp: 0
-                            windowNumber: [[NSApp mainWindow] windowNumber]
+                            windowNumber: [dest windowNumber]
                                  context: [NSApp context]
                                  subtype: 0
                                    data1: value
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.