unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Alan Third <alan@idiocy.org>
To: Bob Halley <halley@play-bow.org>
Cc: emacs-devel@gnu.org
Subject: Re: UI input unresponsive on macOS
Date: Tue, 3 Jan 2017 14:36:21 +0000	[thread overview]
Message-ID: <20170103143621.GA41725@breton.holly.idiocy.org> (raw)
In-Reply-To: <72DF4592-73FE-452D-9CF3-C892E73BD36E@play-bow.org>

[-- Attachment #1: Type: text/plain, Size: 1022 bytes --]

On Tue, Jan 03, 2017 at 05:36:01AM -0800, Bob Halley wrote:
> I was running from the master branch with a build containing the
> recent macOS event changes,
> e0e5b0f4a4ce1d19ee0240c514dedd873d4165dc, and the UI became
> unresponsive to the mouse and keyboard. I had to kill Emacs by hand.
> 
> I was in the middle of something so I rolled back to my prior Emacs
> build. I also am not sure how to debug this. If someone tells me how
> to gather info that’s useful should this happen again, I’m willing
> to reinstate the build and report if it fails.

Hi Bob, I’m working on coming up with a better solution.

Can you try the attached patch? So far the only problem I’ve seen is
that emacsclient doesn’t return to the shell when it exits. I suspect
it’s because I’m not monitoring write or exeption fds correctly.

If you spot any other problems, either reply here or to bug 25265.

Additionally, if anyone has any better ideas of how to deal with
ns_select, please feel free to let me know.
-- 
Alan Third

[-- Attachment #2: 0001-Improve-NS-event-handling-bug-25265.patch --]
[-- Type: text/plain, Size: 4546 bytes --]

From 9663d300d231242fc77f93a35219d56ba9c90601 Mon Sep 17 00:00:00 2001
From: Alan Third <alan@idiocy.org>
Date: Tue, 3 Jan 2017 14:16:37 +0000
Subject: [PATCH] Improve NS event handling (bug#25265)

* src/nsterm.h: Remove reference to timeout_handler.
* src/nsterm.m (ns_select): Monitor fd changes using NSFileHandler.
---
 src/nsterm.h |  1 -
 src/nsterm.m | 62 +++++++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/src/nsterm.h b/src/nsterm.h
index dc222a7..8b5b691 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -392,7 +392,6 @@ char const * nstrace_fullscreen_type_name (int);
 - (void)sendEvent: (NSEvent *)theEvent;
 - (void)showPreferencesWindow: (id)sender;
 - (BOOL) openFile: (NSString *)fileName;
-- (void)timeout_handler: (NSTimer *)timedEntry;
 - (BOOL)fulfillService: (NSString *)name withArg: (NSString *)arg;
 #ifdef NS_IMPL_GNUSTEP
 - (void)sendFromMainThread:(id)unused;
diff --git a/src/nsterm.m b/src/nsterm.m
index 98fd8ab..51cccd0 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -4098,28 +4098,55 @@ overwriting cursor (usually when cursor on a tab) */
     return pselect(nfds, readfds, writefds,
                    exceptfds, timeout, sigmask);
 
-  result = pselect(nfds, readfds, writefds, exceptfds,
-                   &(struct timespec){.tv_sec = 0, .tv_nsec = 100},
-                   sigmask);
-
   [outerpool release];
   outerpool = [[NSAutoreleasePool alloc] init];
 
+  /* Set up one NSFileHandle for each fd we want to monitor. */
+  for (int i = 0 ; i < nfds ; i++)
+    {
+      if ((readfds != NULL && FD_ISSET(i, readfds))
+          || (writefds != NULL && FD_ISSET(i, writefds))
+          || (exceptfds != NULL &&FD_ISSET(i, exceptfds)))
+        {
+          NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor: i
+                                                           closeOnDealloc: NO];
+          /* Send a notification to EmacsApp:fileHandleDataAvailable,
+             which will raise an appDefined event. */
+          /* NOTE: I don't think this will pick up writes and
+             exceptions, so ns_select may not always behave correctly.
+             I've yet to see a problem, though. */
+          [fh waitForDataInBackgroundAndNotify];
+          [fh autorelease];
+        }
+    }
+
   if (timeout)
     {
       double time = timespectod (*timeout);
-      timeout_date = [NSDate dateWithTimeIntervalSinceNow:time];
+      timeout_date = [NSDate dateWithTimeIntervalSinceNow: time];
     }
 
   /* Listen for a new NSEvent. */
-  ns_event = [NSApp nextEventMatchingMask:NSEventMaskAny
-                                untilDate:timeout_date
-                                   inMode:NSDefaultRunLoopMode
-                                  dequeue:NO];
+  ns_event = [NSApp nextEventMatchingMask: NSEventMaskAny
+                                untilDate: timeout_date
+                                   inMode: NSDefaultRunLoopMode
+                                  dequeue: NO];
 
   if (ns_event != nil)
     {
-      raise (SIGIO);
+      if ([ns_event type] == NSEventTypeApplicationDefined
+          && [ns_event data1] == -3)
+        {
+          result = pselect(nfds, readfds, writefds, exceptfds,
+                           &(struct timespec){.tv_sec = 0, .tv_nsec = 0},
+                           sigmask);
+        }
+      else
+        {
+          raise (SIGIO);
+          errno = EINTR;
+          return -1;
+        }
     }
 
   return result;
@@ -5132,6 +5159,12 @@ - (void)applicationDidFinishLaunching: (NSNotification *)notification
 	 object:nil];
 #endif
 
+  [[NSNotificationCenter defaultCenter]
+      addObserver: self
+         selector: @selector (fileHandleDataAvailable:)
+             name: NSFileHandleDataAvailableNotification
+           object: nil];
+
   ns_send_appdefined (-2);
 }
 
@@ -5315,14 +5348,9 @@ - (void)applicationDidResignActive: (NSNotification *)notification
 
    ========================================================================== */
 
-
-- (void)timeout_handler: (NSTimer *)timedEntry
-/* --------------------------------------------------------------------------
-     The timeout specified to ns_select has passed.
-   -------------------------------------------------------------------------- */
+- (void)fileHandleDataAvailable: (NSNotification *)notification
 {
-  /*NSTRACE ("timeout_handler"); */
-  ns_send_appdefined (-2);
+  ns_send_appdefined (-3);
 }
 
 - (void)sendFromMainThread:(id)unused
-- 
2.10.2


  reply	other threads:[~2017-01-03 14:36 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-03 13:36 UI input unresponsive on macOS Bob Halley
2017-01-03 14:36 ` Alan Third [this message]
2017-01-03 15:39   ` Bob Halley
2017-01-03 16:59   ` Bob Halley
2017-01-03 17:49     ` Alan Third
2017-01-04  3:52       ` Gong-Yi Liao 廖宮毅
2017-01-04 15:38         ` Eli Zaretskii
2017-01-05  4:34           ` Gong-Yi Liao 廖宮毅
2017-01-05 15:37             ` Eli Zaretskii
2017-01-05 17:00               ` Gong-Yi Liao 廖宮毅
2017-10-05  3:40                 ` YAMAMOTO Mitsuharu
2017-11-16 19:33                   ` Alan Third
2017-11-17  1:05                     ` YAMAMOTO Mitsuharu
2017-11-17 13:53                     ` Eli Zaretskii
2017-11-17 17:42                       ` Alan Third
2017-11-18  0:24                         ` YAMAMOTO Mitsuharu
2017-12-02 16:51                           ` Alan Third
2017-01-04 21:09   ` Alan Third

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170103143621.GA41725@breton.holly.idiocy.org \
    --to=alan@idiocy.org \
    --cc=emacs-devel@gnu.org \
    --cc=halley@play-bow.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).