From 26da27a5f69ac8de7f2ac4a786485e8e4b5d1f48 Mon Sep 17 00:00:00 2001
From: Corinna Vinschen <corinna@vinschen.de>
Date: Fri, 22 Jun 2001 20:08:06 +0000
Subject: [PATCH]         * fhandler_console.cc (fhandler_console::read):
 Detect AltGr more         robustly on WinNT.

---
 winsup/cygwin/ChangeLog           |  5 +++++
 winsup/cygwin/fhandler_console.cc | 22 ++++++++++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 4c2a264d6..9f13991f8 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,8 @@
+Thu Jun 21 22:01:39 2001  Marius Gedminas <mgedmin@delfi.lt>
+
+	* fhandler_console.cc (fhandler_console::read): Detect AltGr more
+	robustly on WinNT.
+
 2001-06-22  Robert COllins  <rbbtcollins@hotmail.com>
 
 	* thread.cc (__pthread_cond_timedwait): Lock the waiting mutex before 
diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
index b09eeff69..f90f37069 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -289,6 +289,8 @@ fhandler_console::read (void *pv, size_t buflen)
 
 #define ich (input_rec.Event.KeyEvent.uChar.AsciiChar)
 #define wch (input_rec.Event.KeyEvent.uChar.UnicodeChar)
+#define ALT_PRESSED (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)
+#define CTRL_PRESSED (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)
 
 	  if (wch == 0 ||
 	      /* arrow/function keys */
@@ -306,8 +308,22 @@ fhandler_console::read (void *pv, size_t buflen)
 		 converting a CTRL-U. */
 	      if ((unsigned char)ich > 0x7f)
 		con_to_str (tmp + 1, tmp + 1, 1);
-	      /* Determine if the keystroke is modified by META. */
-	      if (!(input_rec.Event.KeyEvent.dwControlKeyState & meta_mask))
+	      /* Determine if the keystroke is modified by META.  The tricky
+		 part is to distinguish whether the right Alt key should be
+		 recognized as Alt, or as AltGr. */
+	      bool meta;
+	      if (os_being_run == winNT)
+		/* WinNT: AltGr is reported as Ctrl+Alt, and Ctrl+Alt is
+		   treated just like AltGr.  However, if Ctrl+Alt+key generates
+		   an ASCII control character, interpret is as META. */
+		meta = (control_key_state & ALT_PRESSED) != 0
+		       && ((control_key_state & CTRL_PRESSED) == 0
+		           || (ich >= 0 && ich <= 0x1f || ich == 0x7f));
+	      else
+		/* Win9x: there's no way to distinguish Alt from AltGr, so rely
+		   on meta_mask heuristic (see fhandler_console constructor). */
+		meta = (control_key_state & meta_mask) != 0;
+	      if (!meta)
 		toadd = tmp + 1;
 	      else
 		{
@@ -319,6 +335,8 @@ fhandler_console::read (void *pv, size_t buflen)
 	    }
 #undef ich
 #undef wch
+#undef ALT_PRESSED
+#undef CTRL_PRESSED
 	  break;
 
 	case MOUSE_EVENT: