public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: console: Unify EnumWindows() callback functions.
@ 2024-02-14 14:26 Takashi Yano
  0 siblings, 0 replies; only message in thread
From: Takashi Yano @ 2024-02-14 14:26 UTC (permalink / raw)
  To: cygwin-patches; +Cc: Takashi Yano, Johannes Schindelin

Previously, three similar callback fuctions were used in console
code. This patch unifies these functions to ease maintenance cost.

Fixes: 8aad3a7edeb2 ("Cygwin: console: Fix a problem that minor ID is incorrect in ConEmu.")
Suggested-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
---
 winsup/cygwin/devices.cc                | 25 +-------
 winsup/cygwin/devices.in                | 25 +-------
 winsup/cygwin/fhandler/console.cc       | 84 +++++++------------------
 winsup/cygwin/local_includes/fhandler.h | 15 +++++
 4 files changed, 39 insertions(+), 110 deletions(-)

diff --git a/winsup/cygwin/devices.cc b/winsup/cygwin/devices.cc
index ca1fdf3be..b14613bc7 100644
--- a/winsup/cygwin/devices.cc
+++ b/winsup/cygwin/devices.cc
@@ -69,28 +69,6 @@ exists_ntdev_silent (const device& dev)
   return exists_ntdev (dev) ? -1 : false;
 }
 
-static BOOL CALLBACK
-enum_cons_dev (HWND hw, LPARAM lp)
-{
-  unsigned long *bitmask = (unsigned long *) lp;
-  HANDLE h = NULL;
-  fhandler_console::console_state *cs;
-  if ((cs = fhandler_console::open_shared_console (hw, h)))
-    {
-      *bitmask |= (1UL << cs->tty_min_state.getntty ());
-      UnmapViewOfFile ((void *) cs);
-      CloseHandle (h);
-    }
-  else
-    { /* Only for ConEmu */
-      char class_hw[32];
-      if (19 == GetClassName (hw, class_hw, sizeof (class_hw))
-	  && 0 == strcmp (class_hw, "VirtualConsoleClass"))
-	EnumChildWindows (hw, enum_cons_dev, lp);
-    }
-  return TRUE;
-}
-
 static int
 exists_console (const device& dev)
 {
@@ -105,8 +83,7 @@ exists_console (const device& dev)
     default:
       if (dev.get_minor () < MAX_CONS_DEV)
 	{
-	  unsigned long bitmask = 0;
-	  EnumWindows (enum_cons_dev, (LPARAM) &bitmask);
+	  unsigned long bitmask = fhandler_console::console_unit (-1);
 	  return bitmask & (1UL << dev.get_minor ());
 	}
       return false;
diff --git a/winsup/cygwin/devices.in b/winsup/cygwin/devices.in
index 842f09c18..e15a35f25 100644
--- a/winsup/cygwin/devices.in
+++ b/winsup/cygwin/devices.in
@@ -65,28 +65,6 @@ exists_ntdev_silent (const device& dev)
   return exists_ntdev (dev) ? -1 : false;
 }
 
-static BOOL CALLBACK
-enum_cons_dev (HWND hw, LPARAM lp)
-{
-  unsigned long *bitmask = (unsigned long *) lp;
-  HANDLE h = NULL;
-  fhandler_console::console_state *cs;
-  if ((cs = fhandler_console::open_shared_console (hw, h)))
-    {
-      *bitmask |= (1UL << cs->tty_min_state.getntty ());
-      UnmapViewOfFile ((void *) cs);
-      CloseHandle (h);
-    }
-  else
-    { /* Only for ConEmu */
-      char class_hw[32];
-      if (19 == GetClassName (hw, class_hw, sizeof (class_hw))
-	  && 0 == strcmp (class_hw, "VirtualConsoleClass"))
-	EnumChildWindows (hw, enum_cons_dev, lp);
-    }
-  return TRUE;
-}
-
 static int
 exists_console (const device& dev)
 {
@@ -101,8 +79,7 @@ exists_console (const device& dev)
     default:
       if (dev.get_minor () < MAX_CONS_DEV)
 	{
-	  unsigned long bitmask = 0;
-	  EnumWindows (enum_cons_dev, (LPARAM) &bitmask);
+	  unsigned long bitmask = fhandler_console::console_unit (-1);
 	  return bitmask & (1UL << dev.get_minor ());
 	}
       return false;
diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 70824e694..66b4905f4 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -224,49 +224,46 @@ fhandler_console::open_shared_console (HWND hw, HANDLE& h, bool& created)
   return res;
 }
 
-class console_unit
-{
-  int n;
-  unsigned long bitmask;
-  HWND me;
-
-public:
-  operator int () const {return n;}
-  console_unit (HWND);
-  friend BOOL CALLBACK enum_windows (HWND, LPARAM);
-};
-
 BOOL CALLBACK
-enum_windows (HWND hw, LPARAM lp)
+fhandler_console::enum_windows (HWND hw, LPARAM lp)
 {
   console_unit *this1 = (console_unit *) lp;
-  if (hw == this1->me)
-    return TRUE;
   HANDLE h = NULL;
   fhandler_console::console_state *cs;
   if ((cs = fhandler_console::open_shared_console (hw, h)))
     {
-      this1->bitmask ^= 1UL << cs->tty_min_state.getntty ();
-      UnmapViewOfFile ((void *) cs);
       CloseHandle (h);
+      if (major (cs->tty_min_state.getntty ()) == DEV_CONS_MAJOR)
+	this1->bitmask |= 1UL << minor (cs->tty_min_state.getntty ());
+      if (this1->n == minor (cs->tty_min_state.getntty ()))
+	{
+	  this1->shared_console_info = cs;
+	  return FALSE;
+	}
+      UnmapViewOfFile ((void *) cs);
     }
   else
     { /* Only for ConEmu */
       char class_hw[32];
       if (19 == GetClassName (hw, class_hw, sizeof (class_hw))
 	  && 0 == strcmp (class_hw, "VirtualConsoleClass"))
-	EnumChildWindows (hw, enum_windows, lp);
+	EnumChildWindows (hw, fhandler_console::enum_windows, lp);
     }
   return TRUE;
 }
 
-console_unit::console_unit (HWND me0):
-  bitmask (~0UL), me (me0)
+fhandler_console::console_unit::console_unit (int n0):
+  n (n0), bitmask (0)
 {
-  EnumWindows (enum_windows, (LPARAM) this);
-  n = (_minor_t) ffs (bitmask) - 1;
+  EnumWindows (fhandler_console::enum_windows, (LPARAM) this);
+  if (n < 0)
+    n = (_minor_t) ffsl (~bitmask) - 1;
   if (n < 0)
-    api_fatal ("console device allocation failure - too many consoles in use, max consoles is 64");
+    api_fatal (sizeof (bitmask) == 8 ?
+	       "console device allocation failure - "
+	       "too many consoles in use, max consoles is 64" :
+	       "console device allocation failure - "
+	       "too many consoles in use, max consoles is 32");
 }
 
 static DWORD
@@ -640,39 +637,6 @@ skip_writeback:
   free (input_tmp);
 }
 
-struct scan_console_args_t
-{
-  _minor_t unit;
-  fhandler_console::console_state **shared_console_info;
-};
-
-BOOL CALLBACK
-scan_console (HWND hw, LPARAM lp)
-{
-  scan_console_args_t *p = (scan_console_args_t *) lp;
-  HANDLE h = NULL;
-  fhandler_console::console_state *cs;
-  if ((cs = fhandler_console::open_shared_console (hw, h)))
-    {
-     if (p->unit == minor (cs->tty_min_state.getntty ()))
-       {
-	 *p->shared_console_info = cs;
-	 CloseHandle (h);
-	 return FALSE;
-       }
-      UnmapViewOfFile ((void *) cs);
-      CloseHandle (h);
-    }
-  else
-    { /* Only for ConEmu */
-      char class_hw[32];
-      if (19 == GetClassName (hw, class_hw, sizeof (class_hw))
-	  && 0 == strcmp (class_hw, "VirtualConsoleClass"))
-	EnumChildWindows (hw, scan_console, lp);
-    }
-  return TRUE;
-}
-
 bool
 fhandler_console::set_unit ()
 {
@@ -696,11 +660,7 @@ fhandler_console::set_unit ()
   else
     {
       if (!generic_console && (dev_t) myself->ctty != get_device ())
-	{
-	  /* Scan for existing shared console info */
-	  scan_console_args_t arg = { unit,  &shared_console_info[unit] };
-	  EnumWindows (scan_console, (LPARAM) &arg);
-	}
+	shared_console_info[unit] = console_unit (unit);
       if (generic_console || !shared_console_info[unit])
 	{
 	  me = GetConsoleWindow ();
@@ -714,7 +674,7 @@ fhandler_console::set_unit ()
 	      ProtectHandleINH (cygheap->console_h);
 	      if (created)
 		{
-		  unit = console_unit (me);
+		  unit = console_unit (-1);
 		  cs->tty_min_state.setntty (DEV_CONS_MAJOR, unit);
 		}
 	      else
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index 8e3088447..a2017f618 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -2259,6 +2259,8 @@ private:
   static void set_output_mode (tty::cons_mode m, const termios *t,
 			       const handle_set_t *p);
 
+  static BOOL CALLBACK enum_windows (HWND hw, LPARAM lp);
+
  public:
   pid_t tc_getpgid ()
   {
@@ -2380,6 +2382,19 @@ private:
   void wpbuf_send ();
   int fstat (struct stat *buf);
 
+  class console_unit
+  {
+    int n;
+    unsigned long bitmask;
+    console_state *shared_console_info;
+  public:
+    operator _minor_t () const {return n;}
+    operator console_state * () const {return shared_console_info;}
+    operator unsigned long () const {return bitmask;}
+    console_unit (int);
+    friend BOOL CALLBACK fhandler_console::enum_windows (HWND, LPARAM);
+  };
+
   friend tty_min * tty_list::get_cttyp ();
 };
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-14 14:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-14 14:26 [PATCH] Cygwin: console: Unify EnumWindows() callback functions Takashi Yano

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).