From 4d660530beeb42d192e2da594621b81ff991df10 Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <michael@stapelberg.de>
Date: Fri, 19 Feb 2010 04:20:49 +0100
Subject: [PATCH] =?UTF-8?q?Bugfix:=20Don=E2=80=99t=20call=20Ncurses.getch=20when=20in=20shell=5Fout=20mode?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Previously, when using threads, Ncurses.getch was called while
the gpg pinentry was running (as an example of using the shell_out
method). Now, the Ncurses mutex will be used to wait until shell_out
mode is finished.
---
 bin/sup           |    2 +-
 lib/sup/buffer.rb |   11 +++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/bin/sup b/bin/sup
index 7824aca..c8eb5b9 100755
--- a/bin/sup
+++ b/bin/sup
@@ -243,7 +243,7 @@ begin
 
   until Redwood::exceptions.nonempty? || $die
     c = begin
-      Ncurses.nonblocking_getch
+      Ncurses.nonblocking_getch BufferManager
     rescue Interrupt
       raise if BufferManager.ask_yes_or_no "Die ungracefully now?"
       BufferManager.draw_screen
diff --git a/lib/sup/buffer.rb b/lib/sup/buffer.rb
index c82fca0..84b3d00 100644
--- a/lib/sup/buffer.rb
+++ b/lib/sup/buffer.rb
@@ -30,13 +30,19 @@ module Ncurses
   def mutex; @mutex ||= Mutex.new; end
   def sync &b; mutex.synchronize(&b); end
 
-  def nonblocking_getch
+  def nonblocking_getch bm=nil
     ## INSANTIY
     ## it is NECESSARY to wrap Ncurses.getch in a select() otherwise all
     ## background threads will be BLOCKED. (except in very modern versions
     ## of libncurses-ruby. the current one on ubuntu seems to work well.)
     if IO.select([$stdin], nil, nil, 0.5)
-      c = Ncurses.getch
+      if bm && bm.shelled?
+        # If we got input while we are shelled, we will drop the input
+        # but use Ncurses.sync to wait until the shell_out is done.
+        Ncurses.sync { nil }
+      else
+        Ncurses.getch
+      end
     end
   end
 
@@ -216,6 +222,7 @@ EOS
   def sigwinch_happened?; @sigwinch_mutex.synchronize { @sigwinch_happened } end
 
   def buffers; @name_map.to_a; end
+  def shelled?; @shelled; end
 
   def focus_on buf
     return unless @buffers.member? buf
-- 
1.6.5

