DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 504L1a701949888 Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 504L1a701949888 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=OmxyAb5N X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A0F653858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1736024495; bh=YoUHdVE17BhuRf3087sjCHD4iwq+OTFN10XmPHX8oco=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=OmxyAb5NjL6NbFjA0Wp/E97mOLft9wnLHZ20EJwHKvAEjfCVp8aAN2IWbYgRSUjEl ENeJQSBwjNGkYoyM0KHSJMdOGQTXiIERIqBhyLVOvnzRZhZ9yJBoX62UYz91pWwLzi 1jx3U7WKDJ29Ou876PK/wgd39G49r2gr5Lf0eWkk= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C0E193858D28 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C0E193858D28 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736024469; cv=none; b=ZbQUew2l38523FiEjVesIDbZZd1T6qrZvyKzYTGqViCxR0rTnIA8EKYiAh3UMaMoptl45dBhrk2WLSnz6Fex2syKQ9HrDYW7AbPQaMOOBfb6YCRx1Cok1BmyhRr1GGEI7lvHTMIjcuTpIwr6R0ZBq/QQio+QU8AS2ffPqtxRmUI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736024469; c=relaxed/simple; bh=OGAWXhTsNv1sMie+bCVt6MS/fc5CZAJ0y9WpqTsRfDs=; h=Message-ID:Date:MIME-Version:From:Subject:To:DKIM-Signature; b=bNtIGh53mXmSkPddOMhNCWaeXaNfSB99612edQ+sudtIRnaGamIsc/qvKoJheefqZfccyFQM+WYI/6lad0eHu91lnqUk00MRkZxWHoQVQAuWLHe5m8+mykzcsy3I9l36BO8mFuJXTlpzoryNV/XFDcJHOnbuLBqWBPJ5Oi3R7XQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0E193858D28 Message-ID: Date: Sat, 4 Jan 2025 14:01:07 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Alt Key Parsing on Cygwin Content-Language: en-CA To: cygwin AT cygwin DOT com References: Organization: Systematic Software In-Reply-To: X-Rspamd-Queue-Id: F07AB2000D X-Rspamd-Server: rspamout05 X-Stat-Signature: rpxpn3kbrqbke5hc9hdur3apef1ckmy9 X-Session-Marker: 427269616E2E496E676C69734053797374656D6174696353572E61622E6361 X-Session-ID: U2FsdGVkX18DbYHsSEApxZBH882VZfa40IxDbTGT3kE= X-HE-Tag: 1736024467-905683 X-HE-Meta: U2FsdGVkX1818AJGJToo/4i+tBQ1in777GkBXE4hXbcUxL0WaJeMzMihbL0r1Na+Oas+e+s6PPujJTNR2UJtVmdilLQnB6vbsqAtG89bK+HUs5u7t4Sy7XRxNgTblfqfrvd1yTFJpb8NlBa/u2CnYE5JtrgmwdicCH+Ijqf6hn11W8vUv1CVimF2L9uOdOpfUXZ6gzS5QdUfSEZO7p49rlde1EWnGmeBMFUrHFstMWFV97s0znli2hvt8PwlCsLA7toOy9V7jFNkr6eR5FnlJy+V2ivHGbvTXFVuXjqSjeKs3JpkmfMa6zJ2VNvl13v0r/7j4WnAszZqnonjCa3KwateYj1U1v8XKM4FynvOiZ/bhkM31QhgJ/xKfUPkV8NtvKr0tZJH6H9cPdGUKoCprzk98yBBzyPci4K1w6oE3v3yf2yAaRTOBQ== X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Brian Inglis via Cygwin Reply-To: cygwin AT cygwin DOT com Cc: Brian Inglis Content-Type: text/plain; charset="utf-8"; Format="flowed" Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 504L1a701949888 On 2025-01-04 12:13, William Hu via Cygwin wrote: > Hi all, > > I've noticed that the Alt key parsing of the following program has different behavior across the different compiler chains/shells on Windows. Consider the following program that reads 4 characters from standard input. > ``` > #include > > int main() { > char x[4] {}; > fgets(reinterpret_cast(&x), 4, stdin); > for (int i {0}; i < 4; ++i) { > printf("%x ", x[i]); > } > return 0; > } > ``` > Perform these steps: > 1. compile (g++ test.cc under cygwin, cl test.cc under MSVC) > 2. execute > 3. hit "Alt" and "1" at the same time and then hit enter > > Here is a table of the output of this program when compiled with different toolchains and executed in various terminals. > > +-------------------------------+----------------+--------------+------------+ > | COMPILER | Cygwin Mintty | MSYS2 Mintty | CMD | > +-------------------------------+----------------+--------------+------------+ > | Cygwin g++ | 1b 31 a 0 | 1b 31 0 0* | 1b 31 0 0* | > | Cygwin x86_64-w64-mingw32-g++ | 31 a 0 0 | 31 a 0 0 | 31 a 0 0 | > | MSYS2 UCRT g++ | 31 a 0 0 | 31 a 0 0 | 31 a 0 0 | > | Visual Studio 2022 | 31 a 0 0 | 31 a 0 0 | 31 a 0 0 | > +-------------------------------+----------------+--------------+------------+ > ----------------------------------------------------------------------------- > * cygwin1.dll was copied into the current directory in order to run the program within these terminals. I didn't get a chance to hit enter - the application just quits. > > I thought different C libraries might be the problem, so I tried a pure-winapi version: > ``` > #include > #include > > int main() { > // On Cygwin, apparently these are distinct - stdin points to a pipe rather than a console buffer > HANDLE hstdin {GetStdHandle(STD_INPUT_HANDLE)}; > HANDLE hconin {CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0)}; > printf("STDIN Handle: %p\n", hstdin); > printf("CONIN Handle: %p\n", hconin); > > DWORD stdtype {GetFileType(hstdin)}; > DWORD contype {GetFileType(hconin)}; > printf("STDIN Filetype: %x\n", stdtype); > printf("CONIN Filetype: %x\n", contype); > > char x[4] {}; > DWORD nread {0}; > // You might ask: Why not read from hconin? The test program will hang > ReadFile(hstdin, &x, 4, &nread, NULL); > for (int i {0}; i < 4; ++i) { > printf("%x ", x[i]); > } > printf("\n"); > > return 0; > } > ``` > New matrix: > +-------------------------------+----------------+--------------+------------+ > | COMPILER | Cygwin Mintty | MSYS2 Mintty | CMD | > +-------------------------------+----------------+--------------+------------+ > | Cygwin g++ | 1b 31 a 0 | 1b 31 0 0* | 1b 31 0 0* | > | Cygwin x86_64-w64-mingw32-g++ | 31 d a 0 | 31 d a 0 | 31 d a 0 | > | MSYS2 UCRT g++ | 31 d a 0 | 31 d a 0 | 31 d a 0 | > | Visual Studio 2022 | 31 d a 0 | 31 d a 0 | 31 d a 0 | > +-------------------------------+----------------+--------------+------------+ > > What does Cygwin do differently that allows it to capture Alt key presses? > The above tables suggest the presence of cygwin1.dll in the program is relevant. > I'm guessing that the backend magic (e.g. pipes for standard input, invisible consoles) > that Cygwin performs plays some role, but thought people on this list would be much more > familiar with the inner workings and would be able to provide a fuller explanation. Cygwin console device driver interfaces to Windows conhost, and tries to get out of the way of non-Cygwin programs; most information about the console is given in What's New notes: https://cygwin.com/cygwin-ug-net/ov-new.html Use the supported POSIX APIs documented in man-pages-posix package: https://cygwin.com/cygwin-api/ Also cygcheck -k|--keycheck from e.g. cmd allows you to see key processing, for example -1q: > cygcheck -k This key check works only in a console window, _NOT_ in a terminal session! Abort with Ctrl+C if in a terminal session. Press 'q' to exit. Pressed 1x VK: 0x0012 VS: 0x0038 C: 0x0000 CTRL: -- -- LA -- NL -- -- -- -- Pressed 1x VK: 0x0031 VS: 0x0002 C: 0x0031 CTRL: -- -- LA -- NL -- -- -- -- Released 1x VK: 0x0031 VS: 0x0002 C: 0x0000 CTRL: -- -- LA -- NL -- -- -- -- Released 1x VK: 0x0012 VS: 0x0038 C: 0x0000 CTRL: -- -- -- -- NL -- -- -- -- Pressed 1x VK: 0x000d VS: 0x001c C: 0x000d CTRL: -- -- -- -- NL -- -- -- -- Released 1x VK: 0x000d VS: 0x001c C: 0x000d CTRL: -- -- -- -- NL -- -- -- -- Pressed 1x VK: 0x0051 VS: 0x0010 C: 0x0071 CTRL: -- -- -- -- NL -- -- -- -- Released 1x VK: 0x0051 VS: 0x0010 C: 0x0071 CTRL: -- -- -- -- NL -- -- -- -- -- Take care. Thanks, Brian Inglis Calgary, Alberta, Canada La perfection est atteinte Perfection is achieved non pas lorsqu'il n'y a plus rien à ajouter not when there is no more to add mais lorsqu'il n'y a plus rien à retirer but when there is no more to cut -- Antoine de Saint-Exupéry -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple