From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 0EED4385E02A; Wed, 14 Feb 2024 14:29:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0EED4385E02A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1707920993; bh=M+G4mq1k5Uth1HuqVdVmAEKRAeYlwlca+w+3LK6lQ88=; h=From:To:Subject:Date:From; b=Gb+Up6fiRjSP0sz2sLPWSvwQvrWg1ciyE9zWhxu0MhzneRO6b1Jnbe7sFw/BZZVzI MjkNmEBNJio87ezk69QMwc0QZiOfLKLN7ew6Q7LUHs+5b5XzYV5qdO8bjCBaQ8yF7b B9uQqj0nTvlV7vpY0cat35EnyDKQmTp+cWzcSu/M= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_5-branch] Cygwin: console: Unify EnumWindows() callback functions. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/cygwin-3_5-branch X-Git-Oldrev: 79db49a1846dd15b4a9b6fc5606e402252d1d50e X-Git-Newrev: 309c1994526ff4d1a6c58765ed28442d1e59965f Message-Id: <20240214142953.0EED4385E02A@sourceware.org> Date: Wed, 14 Feb 2024 14:29:53 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D309c1994526= ff4d1a6c58765ed28442d1e59965f commit 309c1994526ff4d1a6c58765ed28442d1e59965f Author: Takashi Yano Date: Wed Feb 14 22:57:48 2024 +0900 Cygwin: console: Unify EnumWindows() callback functions. =20 Previously, three similar callback fuctions were used in console code. This patch unifies these functions to ease maintenance cost. =20 Fixes: 8aad3a7edeb2 ("Cygwin: console: Fix a problem that minor ID is i= ncorrect in ConEmu.") Suggested-by: Johannes Schindelin Signed-off-by: Takashi Yano Diff: --- 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; } =20 -static BOOL CALLBACK -enum_cons_dev (HWND hw, LPARAM lp) -{ - unsigned long *bitmask =3D (unsigned long *) lp; - HANDLE h =3D NULL; - fhandler_console::console_state *cs; - if ((cs =3D fhandler_console::open_shared_console (hw, h))) - { - *bitmask |=3D (1UL << cs->tty_min_state.getntty ()); - UnmapViewOfFile ((void *) cs); - CloseHandle (h); - } - else - { /* Only for ConEmu */ - char class_hw[32]; - if (19 =3D=3D GetClassName (hw, class_hw, sizeof (class_hw)) - && 0 =3D=3D 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 =3D 0; - EnumWindows (enum_cons_dev, (LPARAM) &bitmask); + unsigned long bitmask =3D 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; } =20 -static BOOL CALLBACK -enum_cons_dev (HWND hw, LPARAM lp) -{ - unsigned long *bitmask =3D (unsigned long *) lp; - HANDLE h =3D NULL; - fhandler_console::console_state *cs; - if ((cs =3D fhandler_console::open_shared_console (hw, h))) - { - *bitmask |=3D (1UL << cs->tty_min_state.getntty ()); - UnmapViewOfFile ((void *) cs); - CloseHandle (h); - } - else - { /* Only for ConEmu */ - char class_hw[32]; - if (19 =3D=3D GetClassName (hw, class_hw, sizeof (class_hw)) - && 0 =3D=3D 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 =3D 0; - EnumWindows (enum_cons_dev, (LPARAM) &bitmask); + unsigned long bitmask =3D 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/con= sole.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, HANDL= E& h, bool& created) return res; } =20 -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 =3D (console_unit *) lp; - if (hw =3D=3D this1->me) - return TRUE; HANDLE h =3D NULL; fhandler_console::console_state *cs; if ((cs =3D fhandler_console::open_shared_console (hw, h))) { - this1->bitmask ^=3D 1UL << cs->tty_min_state.getntty (); - UnmapViewOfFile ((void *) cs); CloseHandle (h); + if (major (cs->tty_min_state.getntty ()) =3D=3D DEV_CONS_MAJOR) + this1->bitmask |=3D 1UL << minor (cs->tty_min_state.getntty ()); + if (this1->n =3D=3D minor (cs->tty_min_state.getntty ())) + { + this1->shared_console_info =3D cs; + return FALSE; + } + UnmapViewOfFile ((void *) cs); } else { /* Only for ConEmu */ char class_hw[32]; if (19 =3D=3D GetClassName (hw, class_hw, sizeof (class_hw)) && 0 =3D=3D strcmp (class_hw, "VirtualConsoleClass")) - EnumChildWindows (hw, enum_windows, lp); + EnumChildWindows (hw, fhandler_console::enum_windows, lp); } return TRUE; } =20 -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 =3D (_minor_t) ffs (bitmask) - 1; + EnumWindows (fhandler_console::enum_windows, (LPARAM) this); + if (n < 0) + n =3D (_minor_t) ffsl (~bitmask) - 1; if (n < 0) - api_fatal ("console device allocation failure - too many consoles in u= se, max consoles is 64"); + api_fatal (sizeof (bitmask) =3D=3D 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"); } =20 static DWORD @@ -640,39 +637,6 @@ skip_writeback: free (input_tmp); } =20 -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 =3D (scan_console_args_t *) lp; - HANDLE h =3D NULL; - fhandler_console::console_state *cs; - if ((cs =3D fhandler_console::open_shared_console (hw, h))) - { - if (p->unit =3D=3D minor (cs->tty_min_state.getntty ())) - { - *p->shared_console_info =3D cs; - CloseHandle (h); - return FALSE; - } - UnmapViewOfFile ((void *) cs); - CloseHandle (h); - } - else - { /* Only for ConEmu */ - char class_hw[32]; - if (19 =3D=3D GetClassName (hw, class_hw, sizeof (class_hw)) - && 0 =3D=3D 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 !=3D get_device ()) - { - /* Scan for existing shared console info */ - scan_console_args_t arg =3D { unit, &shared_console_info[unit] }; - EnumWindows (scan_console, (LPARAM) &arg); - } + shared_console_info[unit] =3D console_unit (unit); if (generic_console || !shared_console_info[unit]) { me =3D GetConsoleWindow (); @@ -714,7 +674,7 @@ fhandler_console::set_unit () ProtectHandleINH (cygheap->console_h); if (created) { - unit =3D console_unit (me); + unit =3D 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 1dc02608b..15ea3f89f 100644 --- a/winsup/cygwin/local_includes/fhandler.h +++ b/winsup/cygwin/local_includes/fhandler.h @@ -2245,6 +2245,8 @@ private: static void set_output_mode (tty::cons_mode m, const termios *t, const handle_set_t *p); =20 + static BOOL CALLBACK enum_windows (HWND hw, LPARAM lp); + public: pid_t tc_getpgid () { @@ -2366,6 +2368,19 @@ private: void wpbuf_send (); int fstat (struct stat *buf); =20 + 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 (); };