psmux 3.3.2

Terminal multiplexer for Windows - tmux alternative for PowerShell and Windows Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
/// Spawn a server process with a hidden console window on Windows.
///
/// Uses raw `CreateProcessW` with `STARTF_USESHOWWINDOW` + `SW_HIDE` and
/// `CREATE_NEW_CONSOLE` so that ConPTY has a real console session while the
/// window remains invisible.  This replicates the behaviour of
/// `Start-Process -WindowStyle Hidden` in PowerShell.
#[cfg(windows)]
pub fn spawn_server_hidden(exe: &std::path::Path, args: &[String]) -> std::io::Result<()> {
    #[repr(C)]
    #[allow(non_snake_case)]
    struct STARTUPINFOW {
        cb: u32,
        lpReserved: *mut u16,
        lpDesktop: *mut u16,
        lpTitle: *mut u16,
        dwX: u32,
        dwY: u32,
        dwXSize: u32,
        dwYSize: u32,
        dwXCountChars: u32,
        dwYCountChars: u32,
        dwFillAttribute: u32,
        dwFlags: u32,
        wShowWindow: u16,
        cbReserved2: u16,
        lpReserved2: *mut u8,
        hStdInput: isize,
        hStdOutput: isize,
        hStdError: isize,
    }

    #[repr(C)]
    #[allow(non_snake_case)]
    struct PROCESS_INFORMATION {
        hProcess: isize,
        hThread: isize,
        dwProcessId: u32,
        dwThreadId: u32,
    }

    #[link(name = "kernel32")]
    extern "system" {
        fn CreateProcessW(
            lpApplicationName: *const u16,
            lpCommandLine: *mut u16,
            lpProcessAttributes: *const std::ffi::c_void,
            lpThreadAttributes: *const std::ffi::c_void,
            bInheritHandles: i32,
            dwCreationFlags: u32,
            lpEnvironment: *const std::ffi::c_void,
            lpCurrentDirectory: *const u16,
            lpStartupInfo: *const STARTUPINFOW,
            lpProcessInformation: *mut PROCESS_INFORMATION,
        ) -> i32;
        fn CloseHandle(handle: isize) -> i32;
    }

    const STARTF_USESHOWWINDOW: u32 = 0x00000001;
    const SW_HIDE: u16 = 0;
    const CREATE_NEW_CONSOLE: u32 = 0x00000010;
    const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
    const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x01000000;

    // Build command line: "exe" arg1 arg2 ...
    // Each argument is quoted to handle spaces.
    let mut cmdline = format!("\"{}\"", exe.display());
    for arg in args {
        if arg.contains(' ') || arg.contains('"') {
            cmdline.push_str(&format!(" \"{}\"", arg.replace('"', "\\\"")));
        } else {
            cmdline.push(' ');
            cmdline.push_str(arg);
        }
    }
    let mut cmdline_wide: Vec<u16> = cmdline.encode_utf16().chain(std::iter::once(0)).collect();

    let mut si: STARTUPINFOW = unsafe { std::mem::zeroed() };
    si.cb = std::mem::size_of::<STARTUPINFOW>() as u32;
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;

    let mut pi: PROCESS_INFORMATION = unsafe { std::mem::zeroed() };

    // Try with CREATE_BREAKAWAY_FROM_JOB first so the server escapes the
    // parent's Job Object (e.g. sshd's kill-on-close job).  If the job
    // disallows breakaway the call fails with ERROR_ACCESS_DENIED; in
    // that case fall back without the flag.
    let base_flags = CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP;
    let mut ok = unsafe {
        CreateProcessW(
            std::ptr::null(),
            cmdline_wide.as_mut_ptr(),
            std::ptr::null(),
            std::ptr::null(),
            0, // don't inherit handles
            base_flags | CREATE_BREAKAWAY_FROM_JOB,
            std::ptr::null(),
            std::ptr::null(),
            &si,
            &mut pi,
        )
    };

    if ok == 0 {
        // Retry without breakaway (job may disallow it)
        // Re-encode cmdline_wide since CreateProcessW may have modified it
        cmdline_wide = cmdline.encode_utf16().chain(std::iter::once(0)).collect();
        ok = unsafe {
            CreateProcessW(
                std::ptr::null(),
                cmdline_wide.as_mut_ptr(),
                std::ptr::null(),
                std::ptr::null(),
                0,
                base_flags,
                std::ptr::null(),
                std::ptr::null(),
                &si,
                &mut pi,
            )
        };
    }

    if ok == 0 {
        return Err(std::io::Error::last_os_error());
    }

    // Close handles – we don't need to wait for the child.
    unsafe {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }

    Ok(())
}

/// Enable virtual terminal processing on Windows Console Host.
/// This is required for ANSI color codes to work in conhost.exe (legacy console).
#[cfg(windows)]
pub fn enable_virtual_terminal_processing() {
    const STD_OUTPUT_HANDLE: u32 = -11i32 as u32;
    const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
    const CP_UTF8: u32 = 65001;

    #[link(name = "kernel32")]
    extern "system" {
        fn GetStdHandle(nStdHandle: u32) -> *mut std::ffi::c_void;
        fn GetConsoleMode(hConsoleHandle: *mut std::ffi::c_void, lpMode: *mut u32) -> i32;
        fn SetConsoleMode(hConsoleHandle: *mut std::ffi::c_void, dwMode: u32) -> i32;
        fn SetConsoleOutputCP(wCodePageID: u32) -> i32;
        fn SetConsoleCP(wCodePageID: u32) -> i32;
    }

    unsafe {
        // Set console code page to UTF-8 so multi-byte Unicode characters
        // (e.g. â–¶ U+25B6, â—€ U+25C0) render correctly instead of as mojibake.
        SetConsoleOutputCP(CP_UTF8);
        SetConsoleCP(CP_UTF8);

        let handle = GetStdHandle(STD_OUTPUT_HANDLE);
        if !handle.is_null() {
            let mut mode: u32 = 0;
            if GetConsoleMode(handle, &mut mode) != 0 {
                SetConsoleMode(handle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
            }
        }
    }
}

#[cfg(not(windows))]
pub fn enable_virtual_terminal_processing() {
    // No-op on non-Windows platforms
}

/// Clear `ENABLE_VIRTUAL_TERMINAL_INPUT` (VTI, 0x0200) from the console stdin.
///
/// crossterm 0.28's `enable_raw_mode()` sets VTI.  When psmux runs inside a
/// ConPTY-based terminal (e.g. WezTerm), VTI tells conhost to pass VT bytes
/// through as raw KEY_EVENT records instead of properly translating them to
/// INPUT_RECORDs with virtual-key codes.  This breaks crossterm's event parser
/// because it expects translated INPUT_RECORDs for regular key events.
///
/// For local (non-SSH) sessions, we do not need VTI — crossterm reads native
/// INPUT_RECORDs via `ReadConsoleInputW`.  The SSH input path has its OWN
/// `SetConsoleMode(+VTI)` call, so this only runs for local mode.
///
/// Windows Terminal is unaffected because it IS the console host (no ConPTY
/// pipe translation).  The fix specifically helps ConPTY-hosted terminals.
#[cfg(windows)]
pub fn disable_vti_on_stdin() {
    const STD_INPUT_HANDLE: u32 = (-10i32) as u32;
    const ENABLE_VIRTUAL_TERMINAL_INPUT: u32 = 0x0200;

    #[link(name = "kernel32")]
    extern "system" {
        fn GetStdHandle(nStdHandle: u32) -> *mut std::ffi::c_void;
        fn GetConsoleMode(hConsoleHandle: *mut std::ffi::c_void, lpMode: *mut u32) -> i32;
        fn SetConsoleMode(hConsoleHandle: *mut std::ffi::c_void, dwMode: u32) -> i32;
    }

    unsafe {
        let handle = GetStdHandle(STD_INPUT_HANDLE);
        if handle.is_null() || handle == (-1isize) as *mut std::ffi::c_void {
            return;
        }
        let mut mode: u32 = 0;
        if GetConsoleMode(handle, &mut mode) != 0 {
            let had_vti = mode & ENABLE_VIRTUAL_TERMINAL_INPUT != 0;
            crate::debug_log::input_log("console", &format!(
                "stdin mode before: 0x{:04X} VTI={}", mode, had_vti
            ));
            if had_vti {
                let new_mode = mode & !ENABLE_VIRTUAL_TERMINAL_INPUT;
                SetConsoleMode(handle, new_mode);
                crate::debug_log::input_log("console", &format!(
                    "stdin mode after: 0x{:04X} (VTI cleared)", new_mode
                ));
            }
        }
    }
}

#[cfg(not(windows))]
pub fn disable_vti_on_stdin() {
    // No-op on non-Windows platforms
}

/// Install a console control handler on Windows to prevent termination on client detach.
#[cfg(windows)]
pub fn install_console_ctrl_handler() {
    type HandlerRoutine = unsafe extern "system" fn(u32) -> i32;

    #[link(name = "kernel32")]
    extern "system" {
        fn SetConsoleCtrlHandler(handler: Option<HandlerRoutine>, add: i32) -> i32;
    }

    const CTRL_CLOSE_EVENT: u32 = 2;
    const CTRL_LOGOFF_EVENT: u32 = 5;
    const CTRL_SHUTDOWN_EVENT: u32 = 6;

    unsafe extern "system" fn handler(ctrl_type: u32) -> i32 {
        match ctrl_type {
            CTRL_CLOSE_EVENT | CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT => 1,
            _ => 0,
        }
    }

    unsafe {
        SetConsoleCtrlHandler(Some(handler), 1);
    }
}

#[cfg(not(windows))]
pub fn install_console_ctrl_handler() {
    // No-op on non-Windows platforms
}

// ---------------------------------------------------------------------------
// Windows Console API mouse injection
// ---------------------------------------------------------------------------
// ConPTY does NOT translate VT mouse escape sequences (e.g. SGR \x1b[<0;10;5M)
// into MOUSE_EVENT INPUT_RECORDs. Writing them to the PTY master appears as
// garbage text in the child app.
//
// The solution: use WriteConsoleInput to inject native MOUSE_EVENT records
// directly into the child's console input buffer.
//
// Flow:
//   1. On first mouse event targeting a pane, lazily acquire the console handle:
//      FreeConsole() → AttachConsole(child_pid) → CreateFileW("CONIN$") → FreeConsole()
//   2. The handle remains valid after FreeConsole on modern Windows (real kernel handles).
//   3. Use WriteConsoleInputW(handle, MOUSE_EVENT record) for each mouse event.
// ---------------------------------------------------------------------------

#[cfg(windows)]
pub mod mouse_inject {
    use std::ffi::c_void;

    const GENERIC_READ: u32  = 0x80000000;
    const GENERIC_WRITE: u32 = 0x40000000;
    const FILE_SHARE_READ: u32  = 0x00000001;
    const FILE_SHARE_WRITE: u32 = 0x00000002;
    const OPEN_EXISTING: u32 = 3;
    const INVALID_HANDLE: isize = -1;

    const MOUSE_EVENT: u16 = 0x0002;
    const ATTACH_PARENT_PROCESS: u32 = 0xFFFFFFFF;

    // dwButtonState flags
    pub const FROM_LEFT_1ST_BUTTON_PRESSED: u32 = 0x0001;
    pub const RIGHTMOST_BUTTON_PRESSED: u32     = 0x0002;
    pub const FROM_LEFT_2ND_BUTTON_PRESSED: u32 = 0x0004; // middle button

    // dwEventFlags
    pub const MOUSE_MOVED: u32       = 0x0001;
    pub const MOUSE_WHEELED: u32     = 0x0004;

    use std::sync::Mutex;
    use std::time::{Duration, Instant};
    static LAST_DRAG_INJECT: Mutex<Option<Instant>> = Mutex::new(None);
    const DRAG_THROTTLE: Duration = Duration::from_millis(16); // ~60fps

    #[repr(C)]
    #[derive(Copy, Clone)]
    struct COORD {
        x: i16,
        y: i16,
    }

    #[repr(C)]
    #[derive(Copy, Clone)]
    struct MOUSE_EVENT_RECORD {
        mouse_position: COORD,
        button_state: u32,
        control_key_state: u32,
        event_flags: u32,
    }

    #[repr(C)]
    struct INPUT_RECORD {
        event_type: u16,
        _padding: u16,
        event: MOUSE_EVENT_RECORD,
    }

    #[link(name = "kernel32")]
    extern "system" {
        fn FreeConsole() -> i32;
        fn AttachConsole(process_id: u32) -> i32;
        fn GetConsoleWindow() -> isize;
        fn CreateFileW(
            file_name: *const u16,
            desired_access: u32,
            share_mode: u32,
            security_attributes: *const c_void,
            creation_disposition: u32,
            flags_and_attributes: u32,
            template_file: *const c_void,
        ) -> isize;
        fn WriteConsoleInputW(
            console_input: isize,
            buffer: *const INPUT_RECORD,
            length: u32,
            events_written: *mut u32,
        ) -> i32;
        fn CloseHandle(handle: isize) -> i32;
        fn GetProcessId(process: isize) -> u32;
        fn GetLastError() -> u32;
    }

    /// Console input mode flags
    const ENABLE_MOUSE_INPUT: u32         = 0x0010;
    const ENABLE_EXTENDED_FLAGS: u32      = 0x0080;
    const ENABLE_QUICK_EDIT_MODE: u32     = 0x0040;
    const ENABLE_VIRTUAL_TERMINAL_INPUT: u32 = 0x0200;

    #[inline]
    fn debug_log(msg: &str) {
        // Write to mouse_debug.log when PSMUX_MOUSE_DEBUG=1 is set.
        use std::sync::atomic::{AtomicBool, Ordering};
        static CHECKED: AtomicBool = AtomicBool::new(false);
        static ENABLED: AtomicBool = AtomicBool::new(false);

        if !CHECKED.swap(true, Ordering::Relaxed) {
            let on = std::env::var("PSMUX_MOUSE_DEBUG").map_or(false, |v| v == "1" || v == "true");
            ENABLED.store(on, Ordering::Relaxed);
        }
        if !ENABLED.load(Ordering::Relaxed) { return; }

        let home = std::env::var("USERPROFILE").or_else(|_| std::env::var("HOME")).unwrap_or_default();
        let path = format!("{}/.psmux/mouse_debug.log", home);
        if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(&path) {
            use std::io::Write;
            let _ = writeln!(f, "[platform] {}", msg);
        }
    }

    /// Extract the process ID from a portable_pty::Child trait object.
    ///
    /// Uses the `Child::process_id()` trait method provided by portable-pty 0.9+.
    pub fn get_child_pid(child: &dyn portable_pty::Child) -> Option<u32> {
        child.process_id()
    }

    /// Query whether the child process's console input has
    /// ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) set.
    ///
    /// When this flag is ON, the process uses VT-based input processing
    /// (crossterm, ratatui apps).  VT mouse sequences written to the ConPTY
    /// input pipe are passed through as KEY_EVENT records, and the app's VT
    /// parser handles them.  If the flag is OFF (e.g. Node.js libuv raw mode
    /// which sets only ENABLE_WINDOW_INPUT), VT mouse sequences should NOT
    /// be written because the app cannot parse them and they appear as garbage.
    pub fn query_vti_enabled(child_pid: u32) -> Option<bool> {
        unsafe {
            let had_console = GetConsoleWindow() != 0;
            FreeConsole();

            if AttachConsole(child_pid) == 0 {
                debug_log(&format!("query_vti_enabled: AttachConsole({}) FAILED", child_pid));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return None;
            }

            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                debug_log("query_vti_enabled: CreateFileW(CONIN$) FAILED");
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return None;
            }

            #[link(name = "kernel32")]
            extern "system" {
                fn GetConsoleMode(hConsoleHandle: *mut c_void, lpMode: *mut u32) -> i32;
            }
            let mut mode: u32 = 0;
            let ok = GetConsoleMode(handle as *mut c_void, &mut mode);

            CloseHandle(handle);
            FreeConsole();
            if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }

            if ok == 0 {
                debug_log("query_vti_enabled: GetConsoleMode FAILED");
                return None;
            }

            let vti = (mode & ENABLE_VIRTUAL_TERMINAL_INPUT) != 0;
            debug_log(&format!("query_vti_enabled: pid={} mode=0x{:04X} VTI={}", child_pid, mode, vti));
            Some(vti)
        }
    }

    /// Inject a mouse event into a child process's console input buffer.
    ///
    /// Performs the full cycle: FreeConsole → AttachConsole(pid) → open CONIN$
    /// → WriteConsoleInputW → CloseHandle → FreeConsole.
    ///
    /// Console handles are pseudo-handles that are invalidated by FreeConsole,
    /// so we must do the entire cycle atomically for each event.
    ///
    /// `reattach`: if true, re-attaches to original console after injection
    /// (needed for app/standalone mode where crossterm uses the console).
    /// Server mode should pass false to avoid conhost cycling.
    pub fn send_mouse_event(
        child_pid: u32,
        col: i16,
        row: i16,
        button_state: u32,
        event_flags: u32,
        reattach: bool,
    ) -> bool {
        // Throttle drag events to ~60fps to avoid excessive console attach/detach cycling
        if event_flags & MOUSE_MOVED != 0 {
            if let Ok(mut guard) = LAST_DRAG_INJECT.lock() {
                if let Some(t) = *guard {
                    if t.elapsed() < DRAG_THROTTLE {
                        return false;
                    }
                }
                *guard = Some(Instant::now());
            }
        }

        unsafe {
            // Check if we currently own a console (app mode yes, server mode no after first call)
            let had_console = reattach && GetConsoleWindow() != 0;

            // Detach from current console (no-op if already detached)
            FreeConsole();

            // Attach to child's pseudo-console
            if AttachConsole(child_pid) == 0 {
                let err = GetLastError();
                debug_log(&format!("send_mouse_event: AttachConsole({}) FAILED err={}", child_pid, err));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            // Open the console input buffer
            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                let err = GetLastError();
                debug_log(&format!("send_mouse_event: CreateFileW(CONIN$) FAILED err={}", err));
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            // Temporarily ensure ENABLE_MOUSE_INPUT is set on the console so
            // mouse events are delivered to the foreground process.  Save and
            // restore original mode to prevent polluting the child's console
            // state (which would confuse query_mouse_input_enabled).
            {
                // Re-use the top-level GetConsoleMode/SetConsoleMode declarations
                // (they use *mut c_void for the handle parameter).
                #[link(name = "kernel32")]
                extern "system" {
                    fn GetConsoleMode(hConsoleHandle: *mut c_void, lpMode: *mut u32) -> i32;
                    fn SetConsoleMode(hConsoleHandle: *mut c_void, dwMode: u32) -> i32;
                }
                let mut mode: u32 = 0;
                let h = handle as *mut c_void;
                if GetConsoleMode(h, &mut mode) != 0 {
                    let desired = (mode | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS)
                                  & !ENABLE_QUICK_EDIT_MODE;
                    if desired != mode {
                        SetConsoleMode(h, desired);
                    }
                }
            }

            // Write the mouse event
            let record = INPUT_RECORD {
                event_type: MOUSE_EVENT,
                _padding: 0,
                event: MOUSE_EVENT_RECORD {
                    mouse_position: COORD { x: col, y: row },
                    button_state,
                    control_key_state: 0,
                    event_flags,
                },
            };
            let mut written: u32 = 0;
            let result = WriteConsoleInputW(handle, &record, 1, &mut written);
            let write_err = GetLastError();

            debug_log(&format!("send_mouse_event: pid={} ({},{}) btn=0x{:X} flags=0x{:X} => ok={} written={} err={}",
                child_pid, col, row, button_state, event_flags, result, written, write_err));

            // Clean up: close handle, detach from child's console
            CloseHandle(handle);
            FreeConsole();
            // Only re-attach if we had our own console (app/standalone mode)
            // Server mode: leave detached to avoid conhost cycling
            if had_console {
                AttachConsole(ATTACH_PARENT_PROCESS);
            }

            result != 0
        }
    }

    /// Query whether the child process's console input has
    /// ENABLE_MOUSE_INPUT (0x0010) set.
    ///
    /// When this flag is ON, the child uses ReadConsoleInputW to read
    /// MOUSE_EVENT INPUT_RECORDs (crossterm/ratatui apps).  When OFF, the
    /// child reads input as text (ReadConsole/ReadFile) and expects VT
    /// mouse sequences delivered as KEY_EVENT records (nvim, vim).
    pub fn query_mouse_input_enabled(child_pid: u32) -> Option<bool> {
        unsafe {
            let had_console = GetConsoleWindow() != 0;
            FreeConsole();

            if AttachConsole(child_pid) == 0 {
                debug_log(&format!("query_mouse_input_enabled: AttachConsole({}) FAILED", child_pid));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return None;
            }

            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                debug_log("query_mouse_input_enabled: CreateFileW(CONIN$) FAILED");
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return None;
            }

            #[link(name = "kernel32")]
            extern "system" {
                fn GetConsoleMode(hConsoleHandle: *mut c_void, lpMode: *mut u32) -> i32;
            }
            let mut mode: u32 = 0;
            let ok = GetConsoleMode(handle as *mut c_void, &mut mode);

            CloseHandle(handle);
            FreeConsole();
            if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }

            if ok == 0 {
                debug_log("query_mouse_input_enabled: GetConsoleMode FAILED");
                return None;
            }

            let mouse_input = (mode & ENABLE_MOUSE_INPUT) != 0;
            debug_log(&format!("query_mouse_input_enabled: pid={} mode=0x{:04X} ENABLE_MOUSE_INPUT={}", child_pid, mode, mouse_input));
            Some(mouse_input)
        }
    }

    /// Inject a VT escape sequence into a child process's console input buffer
    /// as a series of KEY_EVENT records.
    ///
    /// This bypasses ConPTY's VT input parser entirely — the raw characters of
    /// the escape sequence are delivered directly to the foreground process
    /// (e.g. wsl.exe) as keyboard input.  wsl.exe forwards them to the Linux
    /// PTY, where the terminal application (e.g. htop) interprets them as
    /// mouse events.
    ///
    /// This is more reliable than writing to the PTY master pipe because
    /// ConPTY's input engine may not correctly handle SGR mouse sequences
    /// written to hInput.
    pub fn send_vt_sequence(child_pid: u32, sequence: &[u8]) -> bool {
        unsafe {
            let had_console = GetConsoleWindow() != 0;
            FreeConsole();

            if AttachConsole(child_pid) == 0 {
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            // Save original console mode, temporarily set VTI for injection,
            // then restore after writing.  This prevents mode pollution which
            // would confuse the query_mouse_input_enabled() heuristic used to
            // distinguish console-API apps (crossterm) from VT apps (nvim).
            #[link(name = "kernel32")]
            extern "system" {
                fn GetConsoleMode(hConsoleHandle: *mut c_void, lpMode: *mut u32) -> i32;
                fn SetConsoleMode(hConsoleHandle: *mut c_void, dwMode: u32) -> i32;
            }
            let h = handle as *mut c_void;
            let mut original_mode: u32 = 0;
            let got_mode = GetConsoleMode(h, &mut original_mode) != 0;
            if got_mode {
                let desired = (original_mode | ENABLE_EXTENDED_FLAGS | 0x0200 /*ENABLE_VIRTUAL_TERMINAL_INPUT*/)
                              & !ENABLE_QUICK_EDIT_MODE;
                if desired != original_mode {
                    SetConsoleMode(h, desired);
                }
            }

            // Build KEY_EVENT records for each byte of the VT sequence.
            // Each record is a "key down" event with the character set.
            const KEY_EVENT: u16 = 0x0001;

            #[repr(C)]
            #[derive(Copy, Clone)]
            struct KEY_EVENT_RECORD {
                key_down: i32,
                repeat_count: u16,
                virtual_key_code: u16,
                virtual_scan_code: u16,
                u_char: u16,       // UnicodeChar
                control_key_state: u32,
            }

            #[repr(C)]
            struct KEY_INPUT_RECORD {
                event_type: u16,
                _padding: u16,
                event: KEY_EVENT_RECORD,
            }

            // Build the array of input records
            let mut records: Vec<KEY_INPUT_RECORD> = Vec::with_capacity(sequence.len());
            for &byte in sequence {
                records.push(KEY_INPUT_RECORD {
                    event_type: KEY_EVENT,
                    _padding: 0,
                    event: KEY_EVENT_RECORD {
                        key_down: 1,
                        repeat_count: 1,
                        virtual_key_code: 0,
                        virtual_scan_code: 0,
                        u_char: byte as u16,
                        control_key_state: 0,
                    },
                });
            }

            let mut written: u32 = 0;
            let result = WriteConsoleInputW(
                handle,
                records.as_ptr() as *const INPUT_RECORD,
                records.len() as u32,
                &mut written,
            );

            // Restore original console mode to prevent pollution
            if got_mode {
                SetConsoleMode(h, original_mode);
            }

            CloseHandle(handle);
            FreeConsole();
            if had_console {
                AttachConsole(ATTACH_PARENT_PROCESS);
            }

            result != 0
        }
    }

    /// Inject bracketed paste text into a child process's console input buffer.
    ///
    /// Sends `\x1b[200~` + text + `\x1b[201~` as KEY_EVENT records via
    /// WriteConsoleInputW, bypassing ConPTY's VT input parser entirely.
    /// ConPTY strips bracketed paste sequences written to the PTY master pipe,
    /// so this direct injection is the only way to deliver them to the child.
    ///
    /// The text is encoded as UTF-16 for proper Unicode support (file paths
    /// may contain non-ASCII characters).
    pub fn send_bracketed_paste(child_pid: u32, text: &str, bracket: bool) -> bool {
        unsafe {
            let had_console = GetConsoleWindow() != 0;
            FreeConsole();

            if AttachConsole(child_pid) == 0 {
                let err = GetLastError();
                debug_log(&format!("send_bracketed_paste: AttachConsole({}) FAILED err={}", child_pid, err));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                let err = GetLastError();
                debug_log(&format!("send_bracketed_paste: CreateFileW(CONIN$) FAILED err={}", err));
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            const KEY_EVENT: u16 = 0x0001;

            #[repr(C)]
            #[derive(Copy, Clone)]
            struct KEY_EVENT_RECORD {
                key_down: i32,
                repeat_count: u16,
                virtual_key_code: u16,
                virtual_scan_code: u16,
                u_char: u16,
                control_key_state: u32,
            }

            #[repr(C)]
            struct KEY_INPUT_RECORD {
                event_type: u16,
                _padding: u16,
                event: KEY_EVENT_RECORD,
            }

            // Build bracket-open, text, bracket-close as UTF-16 chars
            let bracket_open: &[u8] = b"\x1b[200~";
            let bracket_close: &[u8] = b"\x1b[201~";

            // Collect all UTF-16 code units to send
            let mut chars: Vec<u16> = Vec::new();
            if bracket {
                for &b in bracket_open {
                    chars.push(b as u16);
                }
            }
            // Encode paste text as UTF-16, normalizing \n → \r for the
            // console input buffer (Windows apps expect CR for line breaks;
            // PSReadLine and other readline implementations treat \r as Enter).
            let mut prev_cr = false;
            for c in text.chars() {
                if c == '\n' {
                    if !prev_cr {
                        // Bare \n → \r
                        chars.push('\r' as u16);
                    }
                    // If preceded by \r, the \r was already pushed; skip this \n
                    prev_cr = false;
                    continue;
                }
                prev_cr = c == '\r';
                let mut buf = [0u16; 2];
                let encoded = c.encode_utf16(&mut buf);
                for &unit in encoded.iter() {
                    chars.push(unit);
                }
            }
            if bracket {
                for &b in bracket_close {
                    chars.push(b as u16);
                }
            }

            // Build KEY_EVENT records (key-down only; key-up not needed for
            // console input injection — only key-down events carry characters).
            let mut records: Vec<KEY_INPUT_RECORD> = Vec::with_capacity(chars.len());
            for &wch in &chars {
                records.push(KEY_INPUT_RECORD {
                    event_type: KEY_EVENT,
                    _padding: 0,
                    event: KEY_EVENT_RECORD {
                        key_down: 1,
                        repeat_count: 1,
                        virtual_key_code: 0,
                        virtual_scan_code: 0,
                        u_char: wch,
                        control_key_state: 0,
                    },
                });
            }

            // WriteConsoleInputW can perform partial writes (returns fewer
            // records than requested).  Retry in a loop so that large pastes
            // are delivered in full; without this the closing bracket sequence
            // can be silently dropped, breaking bracket paste mode in the
            // child application.
            //
            // For very large pastes, the console input buffer may fill up.
            // We limit each write to CHUNK_SIZE records and yield briefly
            // between chunks to let the consumer (PSReadLine etc.) drain.
            const CHUNK_SIZE: usize = 2048;
            let mut offset: usize = 0;
            let mut last_result: i32 = 1;
            while offset < records.len() {
                let mut written: u32 = 0;
                let remaining = (records.len() - offset).min(CHUNK_SIZE);
                last_result = WriteConsoleInputW(
                    handle,
                    records[offset..].as_ptr() as *const INPUT_RECORD,
                    remaining as u32,
                    &mut written,
                );
                if last_result == 0 || written == 0 {
                    // Brief yield and retry once (buffer may temporarily be full)
                    std::thread::sleep(std::time::Duration::from_millis(10));
                    last_result = WriteConsoleInputW(
                        handle,
                        records[offset..].as_ptr() as *const INPUT_RECORD,
                        remaining as u32,
                        &mut written,
                    );
                    if last_result == 0 || written == 0 {
                        break;
                    }
                }
                offset += written as usize;
                // Yield between chunks to let the consumer drain the buffer
                if offset < records.len() && remaining >= CHUNK_SIZE {
                    std::thread::sleep(std::time::Duration::from_millis(5));
                }
            }

            debug_log(&format!("send_bracketed_paste: pid={} bracket={} text_len={} records={} written={} ok={}",
                child_pid, bracket, text.len(), records.len(), offset, last_result != 0));

            CloseHandle(handle);
            FreeConsole();
            if had_console {
                AttachConsole(ATTACH_PARENT_PROCESS);
            }

            last_result != 0 && offset == records.len()
        }
    }

    /// Send a CTRL_C_EVENT to all processes on the child's console.
    ///
    /// TUI applications (pstop, btop, etc.) often disable ENABLE_PROCESSED_INPUT
    /// on the ConPTY console and fail to restore it on exit.  When this flag is
    /// off, writing 0x03 to the ConPTY input pipe no longer generates a
    /// CTRL_C_EVENT signal — the byte is delivered as a regular key event that
    /// most programs ignore.
    ///
    /// This function works around the issue by:
    ///   1. Attaching to the child's hidden ConPTY console
    ///   2. Re-enabling ENABLE_PROCESSED_INPUT if it was cleared
    ///   3. Calling GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)
    ///
    /// The combination ensures Ctrl+C always delivers a signal regardless of
    /// what a previous TUI application did to the console mode.
    pub fn send_ctrl_c_event(child_pid: u32, reattach: bool) -> bool {
        const CTRL_C_EVENT: u32 = 0;
        const ENABLE_PROCESSED_INPUT: u32 = 0x0001;

        type HandlerRoutine = unsafe extern "system" fn(u32) -> i32;

        #[link(name = "kernel32")]
        extern "system" {
            fn SetConsoleCtrlHandler(
                handler: Option<HandlerRoutine>,
                add: i32,
            ) -> i32;
            fn GenerateConsoleCtrlEvent(
                ctrl_event: u32,
                process_group_id: u32,
            ) -> i32;
            fn GetConsoleMode(h: *mut c_void, mode: *mut u32) -> i32;
            fn SetConsoleMode(h: *mut c_void, mode: u32) -> i32;
        }

        // Always log to file for Ctrl+C events (critical signal path).
        fn log(msg: &str) {
            debug_log(&format!("ctrl_c: {}", msg));
        }

        unsafe {
            let had_console = reattach && GetConsoleWindow() != 0;

            FreeConsole();

            log(&format!("called: pid={} reattach={} had_console={}", child_pid, reattach, had_console));

            if AttachConsole(child_pid) == 0 {
                let err = GetLastError();
                log(&format!("AttachConsole({}) FAILED err={}", child_pid, err));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            // Open the console input buffer to check / fix ENABLE_PROCESSED_INPUT
            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle != INVALID_HANDLE && handle != 0 {
                let mut mode: u32 = 0;
                if GetConsoleMode(handle as *mut c_void, &mut mode) != 0 {
                    log(&format!("console mode=0x{:04X} PROCESSED_INPUT={}", mode, mode & ENABLE_PROCESSED_INPUT != 0));
                    if mode & ENABLE_PROCESSED_INPUT == 0 {
                        log(&format!("re-enabling ENABLE_PROCESSED_INPUT for pid={}", child_pid));
                        SetConsoleMode(handle as *mut c_void, mode | ENABLE_PROCESSED_INPUT);
                    }
                }
                CloseHandle(handle);
            }

            // Ignore CTRL_C in our own process so GenerateConsoleCtrlEvent
            // doesn't kill psmux (we're temporarily on the child's console).
            // Passing None as handler with add=1 tells the system to ignore
            // Ctrl+C signals in this process.
            SetConsoleCtrlHandler(None, 1);

            let ok = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
            let err = GetLastError();

            log(&format!("GenerateConsoleCtrlEvent => ok={} err={}", ok, err));

            // Detach from the child's console BEFORE restoring Ctrl+C handling.
            // GenerateConsoleCtrlEvent dispatches asynchronously via a new thread;
            // if we restore the default handler while still attached, the async
            // handler thread might terminate psmux.  Detaching first ensures the
            // event only targets processes that remain on the console.
            FreeConsole();

            // Brief sleep to let the async CTRL_C_EVENT handler thread finish
            // before we re-enable default handling.
            std::thread::sleep(std::time::Duration::from_millis(5));

            // Restore default Ctrl+C handling now that we're detached
            SetConsoleCtrlHandler(None, 0);

            if had_console {
                AttachConsole(ATTACH_PARENT_PROCESS);
            }

            ok != 0
        }
    }

    /// Inject a modified key event into a child process's console input buffer.
    ///
    /// Uses WriteConsoleInputW with the appropriate control_key_state flags
    /// (LEFT_CTRL_PRESSED, LEFT_ALT_PRESSED, SHIFT_PRESSED) matching how
    /// Windows Terminal synthesises input events.
    ///
    /// This is necessary because ConPTY does NOT reassemble ESC+char into
    /// native Alt+key events — PSReadLine and other console apps receive
    /// them as separate key events.  Similarly, Ctrl+Alt+key written as
    /// ESC + control-char is not reassembled.
    ///
    /// For Ctrl+key: `u_char` = control character (ch & 0x1F), matching the
    /// Windows console convention.  For Alt+key: `u_char` = the plain char.
    /// For Ctrl+Alt: `u_char` = control character.
    ///
    /// Sends both key-down and key-up events for proper event pairing.
    ///
    /// Convenience wrapper: `send_alt_key_event` calls this with ctrl=false, alt=true, shift=false.
    pub fn send_modified_key_event(child_pid: u32, ch: char, ctrl: bool, alt: bool, shift: bool) -> bool {
        unsafe {
            let had_console = GetConsoleWindow() != 0;
            FreeConsole();

            if AttachConsole(child_pid) == 0 {
                debug_log(&format!("send_modified_key_event: AttachConsole({}) FAILED", child_pid));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                debug_log(&format!("send_modified_key_event: CreateFileW(CONIN$) FAILED"));
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            const KEY_EVENT: u16 = 0x0001;
            const LEFT_ALT_PRESSED: u32 = 0x0002;
            const LEFT_CTRL_PRESSED: u32 = 0x0008;
            const SHIFT_PRESSED: u32 = 0x0010;

            #[repr(C)]
            #[derive(Copy, Clone)]
            struct KEY_EVENT_RECORD {
                key_down: i32,
                repeat_count: u16,
                virtual_key_code: u16,
                virtual_scan_code: u16,
                u_char: u16,
                control_key_state: u32,
            }

            #[repr(C)]
            struct KEY_INPUT_RECORD {
                event_type: u16,
                _padding: u16,
                event: KEY_EVENT_RECORD,
            }

            #[link(name = "user32")]
            extern "system" {
                fn VkKeyScanW(ch: u16) -> i16;
                fn MapVirtualKeyW(code: u32, map_type: u32) -> u32;
            }

            // Build control_key_state flags (matching Windows Terminal convention)
            let mut flags: u32 = 0;
            if ctrl { flags |= LEFT_CTRL_PRESSED; }
            if alt  { flags |= LEFT_ALT_PRESSED; }
            if shift { flags |= SHIFT_PRESSED; }

            // Determine the character to send:
            // - Ctrl+key: u_char = control character (ch & 0x1F)
            // - Alt+key: u_char = plain character
            // - Ctrl+Alt+key: u_char = control character
            // - Shift+key: u_char = uppercase/shifted character
            let base_char = if shift && !ctrl {
                ch.to_ascii_uppercase()
            } else {
                ch
            };

            let u_char_value: u16 = if ctrl {
                // Control character: letter & 0x1F
                (base_char.to_ascii_lowercase() as u16) & 0x1F
            } else {
                let mut buf = [0u16; 2];
                let encoded = base_char.encode_utf16(&mut buf);
                encoded[0]
            };

            // VK code is always the unmodified letter key
            let mut buf = [0u16; 2];
            let plain_wch = ch.to_ascii_lowercase().encode_utf16(&mut buf)[0];
            let vk_result = VkKeyScanW(plain_wch);
            let vk = if vk_result == -1 { 0u16 } else { (vk_result & 0xFF) as u16 };

            // MAPVK_VK_TO_VSC = 0
            let scan = MapVirtualKeyW(vk as u32, 0) as u16;

            let records = [
                KEY_INPUT_RECORD {
                    event_type: KEY_EVENT,
                    _padding: 0,
                    event: KEY_EVENT_RECORD {
                        key_down: 1,
                        repeat_count: 1,
                        virtual_key_code: vk,
                        virtual_scan_code: scan,
                        u_char: u_char_value,
                        control_key_state: flags,
                    },
                },
                KEY_INPUT_RECORD {
                    event_type: KEY_EVENT,
                    _padding: 0,
                    event: KEY_EVENT_RECORD {
                        key_down: 0,
                        repeat_count: 1,
                        virtual_key_code: vk,
                        virtual_scan_code: scan,
                        u_char: u_char_value,
                        control_key_state: flags,
                    },
                },
            ];

            let mut written: u32 = 0;
            let result = WriteConsoleInputW(
                handle,
                records.as_ptr() as *const INPUT_RECORD,
                2,
                &mut written,
            );

            debug_log(&format!("send_modified_key_event: pid={} char='{}' ctrl={} alt={} shift={} vk=0x{:02X} scan=0x{:02X} u_char=0x{:04X} flags=0x{:04X} => ok={} written={}",
                child_pid, ch, ctrl, alt, shift, vk, scan, u_char_value, flags, result != 0, written));

            CloseHandle(handle);
            FreeConsole();
            if had_console {
                AttachConsole(ATTACH_PARENT_PROCESS);
            }

            result != 0 && written >= 1
        }
    }

    /// Convenience: inject Alt+key event.
    pub fn send_alt_key_event(child_pid: u32, ch: char) -> bool {
        send_modified_key_event(child_pid, ch, false, true, false)
    }

    /// Inject a modified Enter (VK_RETURN) event via WriteConsoleInputW.
    ///
    /// ConPTY cannot reconstruct Shift+Enter from VT sequences (\x1b\r is
    /// misinterpreted as Alt+Enter).  Native injection delivers the exact
    /// KEY_EVENT_RECORD with the correct modifier flags, so PSReadLine and
    /// other console-API-based readers see the true Shift/Ctrl/Alt+Enter.
    pub fn send_modified_enter_event(child_pid: u32, ctrl: bool, alt: bool, shift: bool) -> bool {
        unsafe {
            let had_console = GetConsoleWindow() != 0;
            FreeConsole();

            if AttachConsole(child_pid) == 0 {
                debug_log(&format!("send_modified_enter_event: AttachConsole({}) FAILED", child_pid));
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            let conin: [u16; 7] = [
                'C' as u16, 'O' as u16, 'N' as u16,
                'I' as u16, 'N' as u16, '$' as u16, 0,
            ];
            let handle = CreateFileW(
                conin.as_ptr(),
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                std::ptr::null(),
                OPEN_EXISTING,
                0,
                std::ptr::null(),
            );

            if handle == INVALID_HANDLE || handle == 0 {
                debug_log(&format!("send_modified_enter_event: CreateFileW(CONIN$) FAILED"));
                FreeConsole();
                if had_console { AttachConsole(ATTACH_PARENT_PROCESS); }
                return false;
            }

            const KEY_EVENT: u16 = 0x0001;
            const LEFT_ALT_PRESSED: u32 = 0x0002;
            const LEFT_CTRL_PRESSED: u32 = 0x0008;
            const SHIFT_PRESSED: u32 = 0x0010;
            const VK_RETURN: u16 = 0x0D;

            #[repr(C)]
            #[derive(Copy, Clone)]
            struct KEY_EVENT_RECORD {
                key_down: i32,
                repeat_count: u16,
                virtual_key_code: u16,
                virtual_scan_code: u16,
                u_char: u16,
                control_key_state: u32,
            }

            #[repr(C)]
            struct KEY_INPUT_RECORD {
                event_type: u16,
                _padding: u16,
                event: KEY_EVENT_RECORD,
            }

            #[link(name = "user32")]
            extern "system" {
                fn MapVirtualKeyW(code: u32, map_type: u32) -> u32;
            }

            let mut flags: u32 = 0;
            if ctrl  { flags |= LEFT_CTRL_PRESSED; }
            if alt   { flags |= LEFT_ALT_PRESSED; }
            if shift { flags |= SHIFT_PRESSED; }

            // MAPVK_VK_TO_VSC = 0
            let scan = MapVirtualKeyW(VK_RETURN as u32, 0) as u16;

            let records = [
                KEY_INPUT_RECORD {
                    event_type: KEY_EVENT,
                    _padding: 0,
                    event: KEY_EVENT_RECORD {
                        key_down: 1,
                        repeat_count: 1,
                        virtual_key_code: VK_RETURN,
                        virtual_scan_code: scan,
                        u_char: '\r' as u16,
                        control_key_state: flags,
                    },
                },
                KEY_INPUT_RECORD {
                    event_type: KEY_EVENT,
                    _padding: 0,
                    event: KEY_EVENT_RECORD {
                        key_down: 0,
                        repeat_count: 1,
                        virtual_key_code: VK_RETURN,
                        virtual_scan_code: scan,
                        u_char: '\r' as u16,
                        control_key_state: flags,
                    },
                },
            ];

            let mut written: u32 = 0;
            let result = WriteConsoleInputW(
                handle,
                records.as_ptr() as *const INPUT_RECORD,
                2,
                &mut written,
            );

            debug_log(&format!("send_modified_enter_event: pid={} ctrl={} alt={} shift={} scan=0x{:02X} flags=0x{:04X} => ok={} written={}",
                child_pid, ctrl, alt, shift, scan, flags, result != 0, written));

            CloseHandle(handle);
            FreeConsole();
            if had_console {
                AttachConsole(ATTACH_PARENT_PROCESS);
            }

            result != 0 && written >= 1
        }
    }
}

#[cfg(not(windows))]
pub mod mouse_inject {
    pub fn get_child_pid(_child: &dyn portable_pty::Child) -> Option<u32> { None }
    pub fn send_mouse_event(_pid: u32, _col: i16, _row: i16, _btn: u32, _flags: u32, _reattach: bool) -> bool { false }
    pub fn send_vt_sequence(_pid: u32, _sequence: &[u8]) -> bool { false }
    pub fn query_vti_enabled(_pid: u32) -> Option<bool> { None }
    pub fn send_ctrl_c_event(_pid: u32, _reattach: bool) -> bool { false }
    pub fn query_mouse_input_enabled(_pid: u32) -> Option<bool> { None }
    pub fn send_bracketed_paste(_pid: u32, _text: &str, _bracket: bool) -> bool { false }
    pub fn send_modified_key_event(_pid: u32, _ch: char, _ctrl: bool, _alt: bool, _shift: bool) -> bool { false }
    pub fn send_alt_key_event(_pid: u32, _ch: char) -> bool { false }
    pub fn send_modified_enter_event(_pid: u32, _ctrl: bool, _alt: bool, _shift: bool) -> bool { false }
}

// ---------------------------------------------------------------------------
// Process tree killing — ensures all descendant processes are terminated
// ---------------------------------------------------------------------------

#[cfg(windows)]
pub mod process_kill {
    const TH32CS_SNAPPROCESS: u32 = 0x00000002;
    const PROCESS_TERMINATE: u32 = 0x0001;
    const PROCESS_QUERY_INFORMATION: u32 = 0x0400;
    const INVALID_HANDLE: isize = -1;

    #[repr(C)]
    struct PROCESSENTRY32W {
        dw_size: u32,
        cnt_usage: u32,
        th32_process_id: u32,
        th32_default_heap_id: usize,
        th32_module_id: u32,
        cnt_threads: u32,
        th32_parent_process_id: u32,
        pc_pri_class_base: i32,
        dw_flags: u32,
        sz_exe_file: [u16; 260],
    }

    #[link(name = "kernel32")]
    extern "system" {
        fn CreateToolhelp32Snapshot(dw_flags: u32, th32_process_id: u32) -> isize;
        fn Process32FirstW(h_snapshot: isize, lppe: *mut PROCESSENTRY32W) -> i32;
        fn Process32NextW(h_snapshot: isize, lppe: *mut PROCESSENTRY32W) -> i32;
        fn OpenProcess(desired_access: u32, inherit_handle: i32, process_id: u32) -> isize;
        fn TerminateProcess(h_process: isize, exit_code: u32) -> i32;
        fn CloseHandle(handle: isize) -> i32;
    }

    /// Collect all descendant PIDs of `root_pid` (children, grandchildren, etc.).
    /// Uses a breadth-first traversal of the process tree snapshot.
    fn collect_descendants(root_pid: u32) -> Vec<u32> {
        let mut descendants = Vec::new();
        unsafe {
            let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
            if snap == INVALID_HANDLE || snap == 0 { return descendants; }

            // Build full process table from snapshot
            let mut entries: Vec<(u32, u32)> = Vec::with_capacity(256); // (pid, parent_pid)
            let mut pe: PROCESSENTRY32W = std::mem::zeroed();
            pe.dw_size = std::mem::size_of::<PROCESSENTRY32W>() as u32;

            if Process32FirstW(snap, &mut pe) != 0 {
                entries.push((pe.th32_process_id, pe.th32_parent_process_id));
                while Process32NextW(snap, &mut pe) != 0 {
                    entries.push((pe.th32_process_id, pe.th32_parent_process_id));
                }
            }
            CloseHandle(snap);

            // BFS from root_pid
            let mut queue: Vec<u32> = vec![root_pid];
            let mut head = 0;
            while head < queue.len() {
                let parent = queue[head];
                head += 1;
                for &(pid, ppid) in &entries {
                    if ppid == parent && pid != root_pid && !queue.contains(&pid) {
                        queue.push(pid);
                        descendants.push(pid);
                    }
                }
            }
        }
        descendants
    }

    /// Force-terminate a single process by PID.
    fn terminate_pid(pid: u32) {
        unsafe {
            let h = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, 0, pid);
            if h != 0 && h != INVALID_HANDLE {
                let _ = TerminateProcess(h, 1);
                CloseHandle(h);
            }
        }
    }

    /// Kill an entire process tree: all descendants first (leaves → root order),
    /// then the root process itself.  Calls `child.kill()` via portable_pty as a
    /// fallback.  Does NOT call `child.wait()` so `try_wait()` still works for
    /// the reaper (`prune_exited`), which will detect the dead process and clean
    /// up the tree node.
    ///
    /// This mirrors how tmux on Linux sends SIGKILL to the pane's process group.
    pub fn kill_process_tree(child: &mut Box<dyn portable_pty::Child>) {
        // Try to get the PID
        let pid = super::mouse_inject::get_child_pid(child.as_ref());

        if let Some(root_pid) = pid {
            // Collect all descendants, kill them leaf-first (reverse order)
            let mut descs = collect_descendants(root_pid);
            descs.reverse();
            for &dpid in &descs {
                terminate_pid(dpid);
            }
            // Kill the root process
            terminate_pid(root_pid);
        }

        // Fallback: tell portable_pty to kill the direct child process.
        // Do NOT call child.wait() here — the reaper (prune_exited) needs
        // try_wait() to detect the dead process and remove the tree node.
        let _ = child.kill();
    }

    /// Kill multiple process trees using a SINGLE process snapshot.
    /// Much faster than calling `kill_process_tree` N times when
    /// killing an entire session (avoids N separate system snapshots).
    pub fn kill_process_trees_batch(children: &mut [&mut Box<dyn portable_pty::Child>]) {
        // Collect all root PIDs
        let root_pids: Vec<Option<u32>> = children.iter()
            .map(|c| super::mouse_inject::get_child_pid(c.as_ref()))
            .collect();

        // Take ONE process snapshot for all trees
        let entries = snapshot_process_table();

        // For each root PID, find descendants using the shared snapshot
        for (i, root_pid_opt) in root_pids.iter().enumerate() {
            if let Some(root_pid) = root_pid_opt {
                let mut descs = collect_descendants_from_table(&entries, *root_pid);
                descs.reverse();
                for &dpid in &descs {
                    terminate_pid(dpid);
                }
                terminate_pid(*root_pid);
            }
            let _ = children[i].kill();
        }
    }

    /// Take a system-wide process snapshot and return the process table.
    fn snapshot_process_table() -> Vec<(u32, u32)> {
        let mut entries: Vec<(u32, u32)> = Vec::with_capacity(256);
        unsafe {
            let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
            if snap == INVALID_HANDLE || snap == 0 { return entries; }

            let mut pe: PROCESSENTRY32W = std::mem::zeroed();
            pe.dw_size = std::mem::size_of::<PROCESSENTRY32W>() as u32;

            if Process32FirstW(snap, &mut pe) != 0 {
                entries.push((pe.th32_process_id, pe.th32_parent_process_id));
                while Process32NextW(snap, &mut pe) != 0 {
                    entries.push((pe.th32_process_id, pe.th32_parent_process_id));
                }
            }
            CloseHandle(snap);
        }
        entries
    }

    /// BFS from root_pid using a pre-built process table.
    fn collect_descendants_from_table(entries: &[(u32, u32)], root_pid: u32) -> Vec<u32> {
        let mut descendants = Vec::new();
        let mut queue: Vec<u32> = vec![root_pid];
        let mut head = 0;
        while head < queue.len() {
            let parent = queue[head];
            head += 1;
            for &(pid, ppid) in entries {
                if ppid == parent && pid != root_pid && !queue.contains(&pid) {
                    queue.push(pid);
                    descendants.push(pid);
                }
            }
        }
        descendants
    }
}

#[cfg(not(windows))]
pub mod process_kill {
    /// On non-Windows, fall back to simple kill (no wait — let the reaper handle it).
    pub fn kill_process_tree(child: &mut Box<dyn portable_pty::Child>) {
        let _ = child.kill();
    }

    /// Batch kill — on non-Windows, just kill each child individually.
    pub fn kill_process_trees_batch(children: &mut [&mut Box<dyn portable_pty::Child>]) {
        for child in children.iter_mut() {
            let _ = child.kill();
        }
    }
}

// ---------------------------------------------------------------------------
// Process info queries — get CWD and process name from PID (for format vars)
// ---------------------------------------------------------------------------

#[cfg(windows)]
pub mod process_info {
    use std::ffi::OsString;
    use std::os::windows::ffi::OsStringExt;

    const PROCESS_QUERY_LIMITED_INFORMATION: u32 = 0x1000;
    const PROCESS_QUERY_INFORMATION: u32 = 0x0400;
    const PROCESS_VM_READ: u32 = 0x0010;
    const MAX_PATH: usize = 260;
    const TH32CS_SNAPPROCESS: u32 = 0x00000002;
    const INVALID_HANDLE: isize = -1;

    #[allow(non_snake_case)]
    #[repr(C)]
    struct PROCESS_BASIC_INFORMATION {
        Reserved1: isize,
        PebBaseAddress: isize, // pointer to PEB
        Reserved2: [isize; 2],
        UniqueProcessId: isize,
        Reserved3: isize,
    }

    #[allow(non_snake_case)]
    #[repr(C)]
    struct UNICODE_STRING {
        Length: u16,
        MaximumLength: u16,
        Buffer: isize, // pointer to wide string
    }

    #[repr(C)]
    struct PROCESSENTRY32W {
        dw_size: u32,
        cnt_usage: u32,
        th32_process_id: u32,
        th32_default_heap_id: usize,
        th32_module_id: u32,
        cnt_threads: u32,
        th32_parent_process_id: u32,
        pc_pri_class_base: i32,
        dw_flags: u32,
        sz_exe_file: [u16; 260],
    }

    #[link(name = "kernel32")]
    extern "system" {
        fn OpenProcess(desired_access: u32, inherit_handle: i32, process_id: u32) -> isize;
        fn CloseHandle(handle: isize) -> i32;
        fn QueryFullProcessImageNameW(h: isize, flags: u32, name: *mut u16, size: *mut u32) -> i32;
        fn ReadProcessMemory(
            h_process: isize,
            base_address: isize,
            buffer: *mut u8,
            size: usize,
            bytes_read: *mut usize,
        ) -> i32;
        fn CreateToolhelp32Snapshot(dw_flags: u32, th32_process_id: u32) -> isize;
        fn Process32FirstW(h_snapshot: isize, lppe: *mut PROCESSENTRY32W) -> i32;
        fn Process32NextW(h_snapshot: isize, lppe: *mut PROCESSENTRY32W) -> i32;
    }

    #[link(name = "ntdll")]
    extern "system" {
        fn NtQueryInformationProcess(
            process_handle: isize,
            process_information_class: u32,
            process_information: *mut u8,
            process_information_length: u32,
            return_length: *mut u32,
        ) -> i32;
    }

    /// Get the executable name of a process by PID (e.g. "pwsh" or "vim").
    pub fn get_process_name(pid: u32) -> Option<String> {
        unsafe {
            let h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
            if h == 0 || h == -1 { return None; }
            let mut buf = [0u16; 1024];
            let mut size = buf.len() as u32;
            let ok = QueryFullProcessImageNameW(h, 0, buf.as_mut_ptr(), &mut size);
            CloseHandle(h);
            if ok == 0 { return None; }
            let full_path = OsString::from_wide(&buf[..size as usize])
                .to_string_lossy()
                .into_owned();
            let name = std::path::Path::new(&full_path)
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())?;
            Some(name)
        }
    }

    /// Get the current working directory of a process by PID.
    /// Reads the PEB → ProcessParameters → CurrentDirectory from the target process.
    pub fn get_process_cwd(pid: u32) -> Option<String> {
        unsafe {
            let h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, pid);
            if h == 0 || h == -1 { return None; }
            let result = read_process_cwd(h);
            CloseHandle(h);
            result
        }
    }

    /// Read CWD from a process handle via NtQueryInformationProcess + ReadProcessMemory.
    unsafe fn read_process_cwd(h: isize) -> Option<String> {
        // Step 1: Get PEB address
        let mut pbi: PROCESS_BASIC_INFORMATION = std::mem::zeroed();
        let mut ret_len: u32 = 0;
        let status = NtQueryInformationProcess(
            h,
            0, // ProcessBasicInformation
            &mut pbi as *mut _ as *mut u8,
            std::mem::size_of::<PROCESS_BASIC_INFORMATION>() as u32,
            &mut ret_len,
        );
        if status != 0 { return None; }
        let peb_addr = pbi.PebBaseAddress;
        if peb_addr == 0 { return None; }

        // Step 2: Read ProcessParameters pointer from PEB.
        // PEB layout (x64): offset 0x20 = ProcessParameters pointer
        // PEB layout (x86): offset 0x10 = ProcessParameters pointer
        let params_ptr_offset = if std::mem::size_of::<usize>() == 8 { 0x20 } else { 0x10 };
        let mut process_params_ptr: isize = 0;
        let mut bytes_read: usize = 0;
        let ok = ReadProcessMemory(
            h,
            peb_addr + params_ptr_offset,
            &mut process_params_ptr as *mut isize as *mut u8,
            std::mem::size_of::<isize>(),
            &mut bytes_read,
        );
        if ok == 0 || process_params_ptr == 0 { return None; }

        // Step 3: Read CurrentDirectory.DosPath (UNICODE_STRING) from RTL_USER_PROCESS_PARAMETERS.
        // x64 offset: 0x38 = CurrentDirectory.DosPath
        // x86 offset: 0x24 = CurrentDirectory.DosPath
        let cwd_offset = if std::mem::size_of::<usize>() == 8 { 0x38 } else { 0x24 };
        let mut cwd_ustr: UNICODE_STRING = std::mem::zeroed();
        let ok = ReadProcessMemory(
            h,
            process_params_ptr + cwd_offset,
            &mut cwd_ustr as *mut UNICODE_STRING as *mut u8,
            std::mem::size_of::<UNICODE_STRING>(),
            &mut bytes_read,
        );
        if ok == 0 || cwd_ustr.Length == 0 || cwd_ustr.Buffer == 0 { return None; }

        // Step 4: Read the actual CWD wide string
        let char_count = (cwd_ustr.Length / 2) as usize;
        let mut wchars: Vec<u16> = vec![0u16; char_count];
        let ok = ReadProcessMemory(
            h,
            cwd_ustr.Buffer,
            wchars.as_mut_ptr() as *mut u8,
            cwd_ustr.Length as usize,
            &mut bytes_read,
        );
        if ok == 0 { return None; }

        let path = OsString::from_wide(&wchars)
            .to_string_lossy()
            .into_owned();
        // Remove trailing backslash (tmux convention)
        Some(path.trim_end_matches('\\').to_string())
    }

    /// Append a line to ~/.psmux/autorename.log (first 100 entries only).
    fn autorename_log(msg: &str) {
        use std::sync::atomic::{AtomicU32, Ordering};
        static COUNT: AtomicU32 = AtomicU32::new(0);
        let n = COUNT.fetch_add(1, Ordering::Relaxed);
        if n > 100 { return; }
        let home = std::env::var("USERPROFILE").or_else(|_| std::env::var("HOME")).unwrap_or_default();
        let path = format!("{}/.psmux/autorename.log", home);
        if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(&path) {
            use std::io::Write;
            let _ = writeln!(f, "[{}] {}", chrono::Local::now().format("%H:%M:%S%.3f"), msg);
        }
    }

    /// Get the name of the foreground process in the pane.
    /// Walks the process tree from the shell PID to find the deepest
    /// non-system descendant (the user's foreground command).
    pub fn get_foreground_process_name(pid: u32) -> Option<String> {
        // Walk the process tree to find the foreground child.
        let result = find_foreground_child_pid(pid);
        match result {
            Some(target) if target != pid => {
                let name = get_process_name(target);
                autorename_log(&format!("pid={} fg_child={} name={:?}", pid, target, name));
                if let Some(n) = name {
                    return Some(n);
                }
            }
            Some(_) => {
                autorename_log(&format!("pid={} fg_child=self (no children)", pid));
            }
            None => {
                autorename_log(&format!("pid={} fg_child=None (BFS found nothing)", pid));
            }
        }
        // Fallback: shell's own process name.
        let shell_name = get_process_name(pid);
        autorename_log(&format!("pid={} fallback_name={:?}", pid, shell_name));
        shell_name
    }

    /// Get the CWD of the foreground process in the pane.
    pub fn get_foreground_cwd(pid: u32) -> Option<String> {
        if let Some(target) = find_foreground_child_pid(pid) {
            if target != pid {
                if let Some(cwd) = get_process_cwd(target) {
                    return Some(cwd);
                }
            }
        }
        get_process_cwd(pid)
    }

    /// Known system/infrastructure processes that should be skipped when
    /// walking the process tree to find the user's foreground command.
    fn is_system_exe(name: &str) -> bool {
        matches!(name,
            "conhost.exe" | "csrss.exe" | "dwm.exe" | "services.exe"
            | "svchost.exe" | "wininit.exe" | "winlogon.exe"
            | "openconsole.exe" | "runtimebroker.exe"
        )
    }

    /// Walk the process tree from `root_pid` downward and return the PID of
    /// the process most likely to be the user's foreground command.
    ///
    /// Strategy: BFS all descendants, then pick the deepest non-system leaf.
    /// When multiple candidates exist at the same depth, prefer the largest
    /// PID (heuristic for "most recently created").
    fn find_foreground_child_pid(root_pid: u32) -> Option<u32> {
        unsafe {
            let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
            if snap == INVALID_HANDLE || snap == 0 {
                autorename_log(&format!("root={} SNAPSHOT FAILED", root_pid));
                return None;
            }

            // Collect (pid, ppid, exe_name_lower) for every process.
            let mut entries: Vec<(u32, u32, String)> = Vec::with_capacity(512);
            let mut pe: PROCESSENTRY32W = std::mem::zeroed();
            pe.dw_size = std::mem::size_of::<PROCESSENTRY32W>() as u32;

            if Process32FirstW(snap, &mut pe) != 0 {
                let name = exe_name_from_entry(&pe);
                entries.push((pe.th32_process_id, pe.th32_parent_process_id, name));
                while Process32NextW(snap, &mut pe) != 0 {
                    let name = exe_name_from_entry(&pe);
                    entries.push((pe.th32_process_id, pe.th32_parent_process_id, name));
                }
            }
            CloseHandle(snap);

            autorename_log(&format!("root={} snapshot_entries={}", root_pid, entries.len()));

            // Log direct children of root_pid
            let direct: Vec<_> = entries.iter()
                .filter(|(_, ppid, _)| *ppid == root_pid)
                .collect();
            for (pid, _, name) in &direct {
                autorename_log(&format!("  direct_child: pid={} name={}", pid, name));
            }

            // BFS: collect all descendants with their depth.
            // Each entry is (pid, exe_name, depth).
            let mut descendants: Vec<(u32, String, u32)> = Vec::new();
            let mut queue: Vec<(u32, u32)> = vec![(root_pid, 0)]; // (pid, depth)
            let mut head = 0;
            while head < queue.len() {
                let (parent, depth) = queue[head];
                head += 1;
                for (pid, ppid, name) in &entries {
                    if *ppid == parent && *pid != root_pid
                        && !descendants.iter().any(|(p, _, _)| p == pid)
                    {
                        descendants.push((*pid, name.clone(), depth + 1));
                        queue.push((*pid, depth + 1));
                    }
                }
            }

            autorename_log(&format!("root={} descendants={}", root_pid, descendants.len()));
            for (pid, name, depth) in &descendants {
                autorename_log(&format!("  desc: pid={} name={} depth={}", pid, name, depth));
            }

            if descendants.is_empty() {
                return None;
            }

            // A "leaf" is a descendant that has no children in our descendant set.
            let desc_pids: std::collections::HashSet<u32> =
                descendants.iter().map(|(p, _, _)| *p).collect();
            let leaves: Vec<(u32, &str, u32)> = descendants.iter()
                .filter(|(pid, _, _)| {
                    // No entry in the process table has this pid as parent
                    // while also being in our descendant set.
                    !entries.iter().any(|(ep, eppid, _)| *eppid == *pid && desc_pids.contains(ep))
                })
                .map(|(pid, name, depth)| (*pid, name.as_str(), *depth))
                .collect();

            // Choose from leaves if available, otherwise from all descendants.
            let pool: Vec<(u32, &str, u32)> = if !leaves.is_empty() {
                leaves
            } else {
                descendants.iter().map(|(p, n, d)| (*p, n.as_str(), *d)).collect()
            };

            // Prefer non-system candidates.
            let user_pool: Vec<&(u32, &str, u32)> = pool.iter()
                .filter(|(_, name, _)| !is_system_exe(name))
                .collect();

            let selection = if !user_pool.is_empty() { user_pool } else { pool.iter().collect() };

            // Deepest first, then largest PID as tiebreaker.
            let result = selection.iter()
                .max_by(|a, b| a.2.cmp(&b.2).then(a.0.cmp(&b.0)))
                .map(|(pid, _, _)| *pid);

            autorename_log(&format!("root={} selected={:?}", root_pid, result));
            result
        }
    }

    /// Extract the lowercased executable name from a PROCESSENTRY32W.
    fn exe_name_from_entry(pe: &PROCESSENTRY32W) -> String {
        let nul = pe.sz_exe_file.iter().position(|&c| c == 0).unwrap_or(pe.sz_exe_file.len());
        String::from_utf16_lossy(&pe.sz_exe_file[..nul]).to_lowercase()
    }

    /// Check if an executable name is a VT bridge process (WSL, SSH, etc.)
    /// that requires VT mouse injection instead of Win32 console injection.
    fn is_vt_bridge_exe(name: &str) -> bool {
        let stem = name.strip_suffix(".exe").unwrap_or(name);
        matches!(stem, "wsl" | "ssh" | "ubuntu" | "debian" | "kali"
                      | "fedoraremix" | "opensuse-leap" | "sles" | "arch")
            || stem.starts_with("wsl")
    }

    /// Walk the process tree from `root_pid` and check if any descendant
    /// is a VT bridge process (wsl.exe, ssh.exe, etc.).
    /// This is used for mouse injection: VT bridge processes need VT mouse
    /// sequences written to the PTY master, not Win32 MOUSE_EVENT records.
    pub fn has_vt_bridge_descendant(root_pid: u32) -> bool {
        unsafe {
            let snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
            if snap == INVALID_HANDLE || snap == 0 { return false; }

            let mut entries: Vec<(u32, u32, String)> = Vec::with_capacity(256);
            let mut pe: PROCESSENTRY32W = std::mem::zeroed();
            pe.dw_size = std::mem::size_of::<PROCESSENTRY32W>() as u32;

            if Process32FirstW(snap, &mut pe) != 0 {
                let name = exe_name_from_entry(&pe);
                entries.push((pe.th32_process_id, pe.th32_parent_process_id, name));
                while Process32NextW(snap, &mut pe) != 0 {
                    let name = exe_name_from_entry(&pe);
                    entries.push((pe.th32_process_id, pe.th32_parent_process_id, name));
                }
            }
            CloseHandle(snap);

            // BFS from root_pid to check all descendants
            let mut queue: Vec<u32> = vec![root_pid];
            let mut head = 0;
            while head < queue.len() {
                let parent = queue[head];
                head += 1;
                for (pid, ppid, name) in &entries {
                    if *ppid == parent && *pid != root_pid
                        && !queue.contains(pid)
                    {
                        if is_vt_bridge_exe(name) {
                            return true;
                        }
                        queue.push(*pid);
                    }
                }
            }
            false
        }
    }
}

#[cfg(not(windows))]
pub mod process_info {
    pub fn get_process_name(_pid: u32) -> Option<String> { None }
    pub fn get_process_cwd(_pid: u32) -> Option<String> { None }
    pub fn get_foreground_process_name(_pid: u32) -> Option<String> { None }
    pub fn get_foreground_cwd(_pid: u32) -> Option<String> { None }
    pub fn has_vt_bridge_descendant(_root_pid: u32) -> bool { false }
}

// ─── UTF-16 Console Writer (Windows) ────────────────────────────────────
//
// On Windows, Rust's `Stdout::write()` uses `WriteFile` which sends raw
// bytes to the console.  The console interprets those bytes according to
// the *output code page* (typically 437 or 1252, **not** UTF-8).  Even
// after calling `SetConsoleOutputCP(65001)`, ConPTY has incomplete support
// for multi-byte UTF-8 sequences delivered through `WriteFile`, causing
// characters like â–¶ (U+25B6, 3 bytes: E2 96 B6) to render as mojibake
// (e.g. `â¶`).
//
// The fix is to bypass `WriteFile` entirely and use `WriteConsoleW`, which
// accepts UTF-16 wide strings and renders them correctly regardless of
// the console codepage.  This wrapper converts incoming UTF-8 bytes to
// UTF-16 on the fly and writes them with `WriteConsoleW`.

/// A [`std::io::Write`] implementation that renders Unicode correctly on
/// Windows by converting UTF-8 → UTF-16 and calling `WriteConsoleW`.
///
/// Crucially, this buffers incomplete trailing UTF-8 sequences between
/// `write()` calls.  `write_all()` may split a buffer at any byte
/// boundary — including in the middle of a multi-byte character like
/// `â–¶` (U+25B6, bytes E2 96 B6).  Without buffering, each orphaned byte
/// would be emitted as a Latin-1 code point (`â`, `¶`), producing the
/// exact garbling the user sees.
#[cfg(windows)]
pub struct Utf16ConsoleWriter {
    handle: *mut std::ffi::c_void,
    /// Frame buffer: accumulates all `write()` output so that `flush()`
    /// can emit the complete frame as a single `WriteConsoleW` call.
    /// This eliminates the visible top-to-bottom "curtain" repaint that
    /// occurs when ratatui's many small per-cell writes are each sent to
    /// the console individually.
    frame_buf: Vec<u8>,
}

#[cfg(windows)]
unsafe impl Send for Utf16ConsoleWriter {}

#[cfg(windows)]
impl Utf16ConsoleWriter {
    pub fn new() -> Self {
        #[link(name = "kernel32")]
        extern "system" {
            fn GetStdHandle(nStdHandle: u32) -> *mut std::ffi::c_void;
        }
        const STD_OUTPUT_HANDLE: u32 = -11i32 as u32;
        let handle = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
        // Pre-allocate ~128KB for the frame buffer — large enough for a
        // typical full-screen frame's escape sequences without reallocation.
        Self { handle, frame_buf: Vec::with_capacity(131072) }
    }

    /// Write a valid UTF-8 string via `WriteConsoleW`.
    fn write_wide(&self, s: &str) -> std::io::Result<()> {
        if s.is_empty() {
            return Ok(());
        }

        #[link(name = "kernel32")]
        extern "system" {
            fn WriteConsoleW(
                hConsoleOutput: *mut std::ffi::c_void,
                lpBuffer: *const u16,
                nNumberOfCharsToWrite: u32,
                lpNumberOfCharsWritten: *mut u32,
                lpReserved: *mut std::ffi::c_void,
            ) -> i32;
        }

        let wide: Vec<u16> = s.encode_utf16().collect();
        let mut total: u32 = 0;
        let len = wide.len() as u32;
        while total < len {
            let mut written: u32 = 0;
            let ok = unsafe {
                WriteConsoleW(
                    self.handle,
                    wide.as_ptr().add(total as usize),
                    len - total,
                    &mut written,
                    std::ptr::null_mut(),
                )
            };
            if ok == 0 {
                return Err(std::io::Error::last_os_error());
            }
            if written == 0 {
                break;
            }
            total += written;
        }
        Ok(())
    }
}

#[cfg(windows)]
impl std::io::Write for Utf16ConsoleWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        // Append to the frame buffer — actual console output is deferred
        // until flush(), so all of ratatui's per-cell writes within a
        // single draw() call are batched into one atomic WriteConsoleW.
        self.frame_buf.extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        if self.frame_buf.is_empty() {
            return Ok(());
        }

        // Convert the buffered UTF-8 to a valid string, handling any
        // incomplete trailing multi-byte sequence.
        let (valid, remainder) = match std::str::from_utf8(&self.frame_buf) {
            Ok(s) => (s.len(), 0),
            Err(e) => {
                let valid_end = e.valid_up_to();
                // If error_len is None, trailing bytes are an incomplete
                // sequence — they'll be completed by the next write.
                // If it's Some, those bytes are genuinely invalid — skip.
                let skip = e.error_len().unwrap_or(0);
                (valid_end, self.frame_buf.len() - valid_end - skip)
            }
        };

        if valid > 0 {
            // Safety: we just validated this range is valid UTF-8.
            let s = unsafe { std::str::from_utf8_unchecked(&self.frame_buf[..valid]) };
            self.write_wide(s)?;
        }

        // Keep any incomplete trailing bytes for the next flush.
        if remainder > 0 {
            let start = self.frame_buf.len() - remainder;
            // Rotate trailing bytes to front.
            let mut i = 0;
            while i < remainder {
                self.frame_buf[i] = self.frame_buf[start + i];
                i += 1;
            }
            self.frame_buf.truncate(remainder);
        } else {
            self.frame_buf.clear();
        }

        Ok(())
    }
}

/// Platform-independent writer type for the TUI backend.
///
/// On Windows this uses [`Utf16ConsoleWriter`] (WriteConsoleW) so that
/// multi-byte UTF-8 characters render correctly.  On other platforms it
/// is simply [`std::io::Stdout`].
#[cfg(windows)]
pub type PsmuxWriter = Utf16ConsoleWriter;
#[cfg(not(windows))]
pub type PsmuxWriter = std::io::Stdout;

/// Create a new [`PsmuxWriter`].
pub fn create_writer() -> PsmuxWriter {
    #[cfg(windows)]
    { Utf16ConsoleWriter::new() }
    #[cfg(not(windows))]
    { std::io::stdout() }
}

// ---------------------------------------------------------------------------
// Win32 System Caret — Accessibility / Speech-to-Text support
// ---------------------------------------------------------------------------
// Speech-to-text tools like Wispr Flow use GetGUIThreadInfo() to locate the
// system caret.  When psmux enters raw mode + alternate screen, the default
// console caret is hidden and accessibility tools lose track of the text
// insertion point.
//
// By creating a Win32 caret on the console window and updating its position
// every frame, accessibility tools can detect the active text input context
// and inject transcribed text.
//
// These functions are safe to call on all platforms; non-Windows builds are
// no-ops.  SSH sessions should skip calling these (no local console window).
// ---------------------------------------------------------------------------

#[cfg(windows)]
pub mod caret {
    use std::sync::atomic::{AtomicBool, Ordering};

    static CARET_CREATED: AtomicBool = AtomicBool::new(false);

    #[link(name = "kernel32")]
    extern "system" {
        fn GetConsoleWindow() -> isize;
        fn GetCurrentConsoleFontEx(
            hConsoleOutput: *mut std::ffi::c_void,
            bMaximumWindow: i32,
            lpConsoleCurrentFontEx: *mut CONSOLE_FONT_INFOEX,
        ) -> i32;
        fn GetStdHandle(nStdHandle: u32) -> *mut std::ffi::c_void;
    }

    #[link(name = "user32")]
    extern "system" {
        fn CreateCaret(hWnd: isize, hBitmap: isize, nWidth: i32, nHeight: i32) -> i32;
        fn SetCaretPos(x: i32, y: i32) -> i32;
        fn ShowCaret(hWnd: isize) -> i32;
        fn DestroyCaret() -> i32;
    }

    #[repr(C)]
    #[allow(non_snake_case)]
    struct CONSOLE_FONT_INFOEX {
        cbSize: u32,
        nFont: u32,
        dwFontSize_X: i16,
        dwFontSize_Y: i16,
        FontFamily: u32,
        FontWeight: u32,
        FaceName: [u16; 32],
    }

    /// Query the current console font cell size in pixels.
    /// Returns (cell_width, cell_height).  Falls back to (8, 16) on failure.
    fn console_cell_size() -> (i32, i32) {
        const STD_OUTPUT_HANDLE: u32 = (-11i32) as u32;
        unsafe {
            let handle = GetStdHandle(STD_OUTPUT_HANDLE);
            if handle.is_null() || handle == (-1isize) as *mut std::ffi::c_void {
                return (8, 16);
            }
            let mut info: CONSOLE_FONT_INFOEX = std::mem::zeroed();
            info.cbSize = std::mem::size_of::<CONSOLE_FONT_INFOEX>() as u32;
            if GetCurrentConsoleFontEx(handle, 0, &mut info) != 0 {
                let w = if info.dwFontSize_X > 0 { info.dwFontSize_X as i32 } else { 8 };
                let h = if info.dwFontSize_Y > 0 { info.dwFontSize_Y as i32 } else { 16 };
                (w, h)
            } else {
                (8, 16)
            }
        }
    }

    /// Create the system caret on the console window (if not already created)
    /// and update its position to the given terminal cell coordinates.
    ///
    /// `col` and `row` are 0-based terminal cell coordinates (the same values
    /// used for VT CUP positioning).
    pub fn update(col: u16, row: u16) {
        unsafe {
            let hwnd = GetConsoleWindow();
            if hwnd == 0 {
                return;
            }
            if !CARET_CREATED.load(Ordering::Relaxed) {
                let (cw, ch) = console_cell_size();
                if CreateCaret(hwnd, 0, cw.max(1), ch.max(1)) != 0 {
                    CARET_CREATED.store(true, Ordering::Relaxed);
                    ShowCaret(hwnd);
                }
            }
            let (cw, ch) = console_cell_size();
            SetCaretPos(col as i32 * cw, row as i32 * ch);
        }
    }

    /// Hide and destroy the system caret.  Call on exit.
    pub fn destroy() {
        if CARET_CREATED.swap(false, Ordering::Relaxed) {
            unsafe { DestroyCaret(); }
        }
    }
}

#[cfg(not(windows))]
pub mod caret {
    pub fn update(_col: u16, _row: u16) {}
    pub fn destroy() {}
}

/// On Windows ConPTY, Shift+Enter is misreported by crossterm:
///
/// VS Code's xterm.js sends `\x1b\r` (ESC + CR) for Shift+Enter.
/// ConPTY interprets the ESC prefix as Alt, so crossterm reports
/// `KeyModifiers::ALT` instead of `KeyModifiers::SHIFT`.
///
/// This function polls the physical keyboard state to detect the real
/// modifiers and remaps accordingly.
#[cfg(windows)]
pub fn augment_enter_shift(key: &mut crossterm::event::KeyEvent) {
    use crossterm::event::{KeyCode, KeyModifiers};

    if !matches!(key.code, KeyCode::Enter) {
        return;
    }
    if key.modifiers.contains(KeyModifiers::SHIFT) {
        return;
    }

    #[link(name = "user32")]
    extern "system" {
        fn GetAsyncKeyState(vKey: i32) -> i16;
    }

    const VK_SHIFT: i32 = 0x10;
    const VK_CONTROL: i32 = 0x11;
    const VK_MENU: i32 = 0x12; // Alt

    unsafe {
        let shift_down = GetAsyncKeyState(VK_SHIFT) < 0;
        let ctrl_down = GetAsyncKeyState(VK_CONTROL) < 0;
        let alt_down = GetAsyncKeyState(VK_MENU) < 0;

        if shift_down {
            key.modifiers.insert(KeyModifiers::SHIFT);
            // Windows Terminal + crossterm sometimes reports a phantom CONTROL
            // modifier on the Press event for Shift+Enter while the physical
            // Ctrl key is not held.  Remove it.
            if !ctrl_down && key.modifiers.contains(KeyModifiers::CONTROL) {
                key.modifiers.remove(KeyModifiers::CONTROL);
            }
            if !alt_down && key.modifiers.contains(KeyModifiers::ALT) {
                key.modifiers.remove(KeyModifiers::ALT);
            }
        } else if !shift_down && !ctrl_down && !alt_down {
            // No physical modifiers held; ConPTY may have injected a phantom
            // ALT from ESC+CR.  Already handled by the early return for SHIFT
            // above, but guard plain Enter too.
        } else if !shift_down && alt_down {
            // Physical Alt is held, leave as is.
        }
    }
}