[PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Takashi Yano
---
 winsup/cygwin/environ.cc          |  2 +-
 winsup/cygwin/fhandler.h          |  1 +
 winsup/cygwin/fhandler_console.cc | 46 ++++++++++++++++++++-----------
 winsup/cygwin/fhandler_tty.cc     | 14 ++++++++++
 4 files changed, 46 insertions(+), 17 deletions(-)

diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index 8fa01b2d5..8c5ce64e1 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -859,6 +859,7 @@ environ_init (char **envp, int envc)
   __endtry
 }
 
+int sawTERM = 0;
 
 char ** __reg2
 win32env_to_cygenv (PWCHAR rawenv, bool posify)
@@ -868,7 +869,6 @@ win32env_to_cygenv (PWCHAR rawenv, bool posify)
   int envc;
   char *newp;
   int i;
-  int sawTERM = 0;
   const char cygterm[] = "TERM=cygwin";
   const char xterm[] = "TERM=xterm-256color";
   char *tmpbuf = tp.t_get ();
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index d5aa57300..313172ec5 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1831,6 +1831,7 @@ enum cltype
 class dev_console
 {
   pid_t owner;
+  bool is_legacy;
 
   WORD default_color, underline_color, dim_color;
 
diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
index 241759203..d875ad65c 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -309,7 +309,7 @@ fhandler_console::set_cursor_maybe ()
 {
   con.fillin (get_output_handle ());
   /* Nothing to do for xterm compatible mode. */
-  if (wincap.has_con_24bit_colors ())
+  if (wincap.has_con_24bit_colors () && !con.is_legacy)
     return;
   if (con.dwLastCursorPosition.X != con.b.dwCursorPosition.X ||
       con.dwLastCursorPosition.Y != con.b.dwCursorPosition.Y)
@@ -349,7 +349,7 @@ fhandler_console::send_winch_maybe ()
     {
       con.scroll_region.Top = 0;
       con.scroll_region.Bottom = -1;
-      if (wincap.has_con_24bit_colors ())
+      if (wincap.has_con_24bit_colors () && !con.is_legacy)
  fix_tab_position (get_output_handle (), con.dwWinSize.X);
       get_ttyp ()->kill_pgrp (SIGWINCH);
       return true;
@@ -483,7 +483,7 @@ sig_exit:
 fhandler_console::input_states
 fhandler_console::process_input_message (void)
 {
-  if (wincap.has_con_24bit_colors ())
+  if (wincap.has_con_24bit_colors () && !con.is_legacy)
     {
       DWORD dwMode;
       /* Enable xterm compatible mode in input */
@@ -589,7 +589,8 @@ fhandler_console::process_input_message (void)
     }
   /* Allow Ctrl-Space to emit ^@ */
   else if (input_rec[i].Event.KeyEvent.wVirtualKeyCode
-   == (wincap.has_con_24bit_colors () ? '2' : VK_SPACE)
+   == ((wincap.has_con_24bit_colors () && !con.is_legacy) ?
+       '2' : VK_SPACE)
    && (ctrl_key_state & CTRL_PRESSED)
    && !(ctrl_key_state & ALT_PRESSED))
     toadd = "";
@@ -1023,17 +1024,28 @@ fhandler_console::open (int flags, mode_t)
       /* Enable xterm compatible mode in output */
       GetConsoleMode (get_output_handle (), &dwMode);
       dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
-      SetConsoleMode (get_output_handle (), dwMode);
+      if (!SetConsoleMode (get_output_handle (), dwMode))
+ con.is_legacy = true;
+      else
+ con.is_legacy = false;
       /* Enable xterm compatible mode in input */
-      GetConsoleMode (get_handle (), &dwMode);
-      dwMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
-      SetConsoleMode (get_handle (), dwMode);
+      if (!con.is_legacy)
+ {
+  GetConsoleMode (get_handle (), &dwMode);
+  dwMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
+  if (!SetConsoleMode (get_handle (), dwMode))
+    con.is_legacy = true;
+ }
+      extern int sawTERM;
+      if (con.is_legacy && !sawTERM)
+ setenv ("TERM", "cygwin", 1);
     }
 
   DWORD cflags;
   if (GetConsoleMode (get_handle (), &cflags))
     SetConsoleMode (get_handle (), ENABLE_WINDOW_INPUT
-    | (wincap.has_con_24bit_colors () ? 0 : ENABLE_MOUSE_INPUT)
+    | ((wincap.has_con_24bit_colors () && !con.is_legacy) ?
+       0 : ENABLE_MOUSE_INPUT)
     | cflags);
 
   debug_printf ("opened conin$ %p, conout$ %p", get_handle (),
@@ -1062,7 +1074,7 @@ fhandler_console::close ()
   output_mutex = NULL;
 
   if (shared_console_info && getpid () == con.owner &&
-      wincap.has_con_24bit_colors ())
+      wincap.has_con_24bit_colors () && !con.is_legacy)
     {
       DWORD dwMode;
       /* Disable xterm compatible mode in input */
@@ -1209,7 +1221,7 @@ fhandler_console::output_tcsetattr (int, struct termios const *t)
   acquire_output_mutex (INFINITE);
   DWORD flags = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
   /* If system has 24 bit color capability, use xterm compatible mode. */
-  if (wincap.has_con_24bit_colors ())
+  if (wincap.has_con_24bit_colors () && !con.is_legacy)
     {
       flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
       if (!(t->c_oflag & OPOST) || !(t->c_oflag & ONLCR))
@@ -1274,9 +1286,10 @@ fhandler_console::input_tcsetattr (int, struct termios const *t)
     }
 
   flags |= ENABLE_WINDOW_INPUT |
-    (wincap.has_con_24bit_colors () ? 0 : ENABLE_MOUSE_INPUT);
+    ((wincap.has_con_24bit_colors () && !con.is_legacy) ?
+     0 : ENABLE_MOUSE_INPUT);
   /* if system has 24 bit color capability, use xterm compatible mode. */
-  if (wincap.has_con_24bit_colors ())
+  if (wincap.has_con_24bit_colors () && !con.is_legacy)
     flags |= ENABLE_VIRTUAL_TERMINAL_INPUT;
 
   int res;
@@ -1650,7 +1663,7 @@ bool fhandler_console::write_console (PWCHAR buf, DWORD len, DWORD& done)
 {
   bool need_fix_tab_position = false;
   /* Check if screen will be alternated. */
-  if (wincap.has_con_24bit_colors ()
+  if (wincap.has_con_24bit_colors () && !con.is_legacy
       && memmem (buf, len*sizeof (WCHAR), L"\033[?1049", 7*sizeof (WCHAR)))
     need_fix_tab_position = true;
 
@@ -2498,7 +2511,8 @@ fhandler_console::write_normal (const unsigned char *src,
   memset (&ps, 0, sizeof ps);
   while (found < end
  && found - src < CONVERT_LIMIT
- && (wincap.has_con_24bit_colors () || base_chars[*found] == NOR) )
+ && ((wincap.has_con_24bit_colors () && !con.is_legacy)
+     || base_chars[*found] == NOR) )
     {
       switch (ret = f_mbtowc (_REENT, NULL, (const char *) found,
        end - found, &ps))
@@ -2958,7 +2972,7 @@ fhandler_console::fixup_after_fork_exec (bool execing)
 {
   set_unit ();
   setup_io_mutex ();
-  if (wincap.has_con_24bit_colors ())
+  if (wincap.has_con_24bit_colors () && !con.is_legacy)
     {
       DWORD dwMode;
       /* Disable xterm compatible mode in input */
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 0109d452b..c71603068 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -26,6 +26,7 @@ details. */
 #include <asm/socket.h>
 #include "cygwait.h"
 #include "tls_pbuf.h"
+#include "registry.h"
 
 #define ALWAYS_USE_PCON false
 #define USE_API_HOOK true
@@ -3104,6 +3105,19 @@ is_running_as_service (void)
 bool
 fhandler_pty_master::setup_pseudoconsole ()
 {
+  /* If the legacy console mode is enabled, pseudo console seems
+     not to work as expected. To determine console mode, registry
+     key ForceV2 in HKEY_CURRENT_USER\Console is checked. */
+  reg_key reg (HKEY_CURRENT_USER, KEY_READ, L"Console", NULL);
+  if (reg.error ())
+    return false;
+  if (reg.get_dword (L"ForceV2", 1) == 0)
+    {
+      termios_printf ("Pseudo console is disabled "
+      "because the legacy console mode is enabled.");
+      return false;
+    }
+
   /* Pseudo console supprot is realized using a tricky technic.
      PTY need the pseudo console handles, however, they cannot
      be retrieved by normal procedure. Therefore, run a helper
--
2.21.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Corinna Vinschen-2
On Nov  7 01:29, Takashi Yano wrote:
> ---

Pushed, albeit I'm still missing a bit of description here.  Just a one
liner is a bit low on info during `git log'.  I'd really appreciate more
descriptive log messages...

>  winsup/cygwin/environ.cc          |  2 +-
>  winsup/cygwin/fhandler.h          |  1 +
>  winsup/cygwin/fhandler_console.cc | 46 ++++++++++++++++++++-----------
>  winsup/cygwin/fhandler_tty.cc     | 14 ++++++++++
>  4 files changed, 46 insertions(+), 17 deletions(-)

Corinna

--
Corinna Vinschen
Cygwin Maintainer

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Takashi Yano
Hi Corinna,

On Fri, 8 Nov 2019 10:22:30 +0100
Corinna Vinschen wrote:
> Pushed, albeit I'm still missing a bit of description here.  Just a one
> liner is a bit low on info during `git log'.  I'd really appreciate more
> descriptive log messages...

Oh! Does "log message" mean git commit message? I misundersood that
it meant strace log message. Sorry.

--
Takashi Yano <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Corinna Vinschen-2
On Nov  8 21:01, Takashi Yano wrote:

> Hi Corinna,
>
> On Fri, 8 Nov 2019 10:22:30 +0100
> Corinna Vinschen wrote:
> > Pushed, albeit I'm still missing a bit of description here.  Just a one
> > liner is a bit low on info during `git log'.  I'd really appreciate more
> > descriptive log messages...
>
> Oh! Does "log message" mean git commit message? I misundersood that
> it meant strace log message. Sorry.
Yeah I meant git commit message.  Sorry for being unclear.


Corinna

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Ken Brown-6
In reply to this post by Takashi Yano
Hi Takashi,

On 11/6/2019 11:29 AM, Takashi Yano wrote:
> ---
>   winsup/cygwin/environ.cc          |  2 +-
>   winsup/cygwin/fhandler.h          |  1 +
>   winsup/cygwin/fhandler_console.cc | 46 ++++++++++++++++++++-----------
>   winsup/cygwin/fhandler_tty.cc     | 14 ++++++++++
>   4 files changed, 46 insertions(+), 17 deletions(-)

After this commit, the XWin Server Start Menu shortcut no longer works.  I think
it's /usr/bin/xwin-xdg-menu.exe that fails, but I haven't checked this carefully.

I'm attaching the log file, in which you can see that GetSystemMenu fails.

Ken

XWin.0.log (41K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Takashi Yano
Hi Ken,

On Mon, 11 Nov 2019 19:39:46 +0000
Ken Brown wrote:
> After this commit, the XWin Server Start Menu shortcut no longer works.  I think
> it's /usr/bin/xwin-xdg-menu.exe that fails, but I haven't checked this carefully.

Could you please check whether the attached patch solves the issue?

--
Takashi Yano <[hidden email]>

revise-check-con-is-legacy.patch (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Ken Brown-6
On 11/11/2019 9:55 PM, Takashi Yano wrote:
> Hi Ken,
>
> On Mon, 11 Nov 2019 19:39:46 +0000
> Ken Brown wrote:
>> After this commit, the XWin Server Start Menu shortcut no longer works.  I think
>> it's /usr/bin/xwin-xdg-menu.exe that fails, but I haven't checked this carefully.
>
> Could you please check whether the attached patch solves the issue?

Yes, that fixes it.  Thanks.

Ken
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v3] Cygwin: console, pty: Prevent error in legacy console mode.

Takashi Yano
On Tue, 12 Nov 2019 13:46:04 +0000
Ken Brown wrote:

> On 11/11/2019 9:55 PM, Takashi Yano wrote:
> > Hi Ken,
> >
> > On Mon, 11 Nov 2019 19:39:46 +0000
> > Ken Brown wrote:
> >> After this commit, the XWin Server Start Menu shortcut no longer works.  I think
> >> it's /usr/bin/xwin-xdg-menu.exe that fails, but I haven't checked this carefully.
> >
> > Could you please check whether the attached patch solves the issue?
>
> Yes, that fixes it.  Thanks.

Thanks for testing. I will submit the patch.

--
Takashi Yano <[hidden email]>