codecin 7.22.10__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +1057 -0
  3. codecin/assembler.py +619 -0
  4. codecin/cbackend.py +3024 -0
  5. codecin/cin.py +4265 -0
  6. codecin/cli.py +670 -0
  7. codecin/codecin_native.dll +0 -0
  8. codecin/config.py +50 -0
  9. codecin/console.py +183 -0
  10. codecin/cpu.py +312 -0
  11. codecin/crom.py +449 -0
  12. codecin/cvm.py +708 -0
  13. codecin/disasm.py +149 -0
  14. codecin/environment.py +176 -0
  15. codecin/errors.py +54 -0
  16. codecin/isa.py +560 -0
  17. codecin/lib/array/pic.toml +10 -0
  18. codecin/lib/array/src/array.cin +103 -0
  19. codecin/lib/bigint/pic.toml +10 -0
  20. codecin/lib/bigint/src/bigint.cin +589 -0
  21. codecin/lib/bits/pic.toml +10 -0
  22. codecin/lib/bits/src/bits.cin +143 -0
  23. codecin/lib/bitset/pic.toml +10 -0
  24. codecin/lib/bitset/src/bitset.cin +374 -0
  25. codecin/lib/codec/pic.toml +10 -0
  26. codecin/lib/codec/src/codec.cin +563 -0
  27. codecin/lib/combin/pic.toml +10 -0
  28. codecin/lib/combin/src/combin.cin +414 -0
  29. codecin/lib/conv/pic.toml +10 -0
  30. codecin/lib/conv/src/conv.cin +151 -0
  31. codecin/lib/cppstd/pic.toml +10 -0
  32. codecin/lib/cppstd/src/cppstd.cin +418 -0
  33. codecin/lib/cstd/pic.toml +10 -0
  34. codecin/lib/cstd/src/cstd.cin +424 -0
  35. codecin/lib/csv/pic.toml +10 -0
  36. codecin/lib/csv/src/csv.cin +405 -0
  37. codecin/lib/dp/pic.toml +10 -0
  38. codecin/lib/dp/src/dp.cin +325 -0
  39. codecin/lib/ffi/pic.toml +10 -0
  40. codecin/lib/ffi/src/ffi.cin +163 -0
  41. codecin/lib/fmt/pic.toml +10 -0
  42. codecin/lib/fmt/src/fmt.cin +325 -0
  43. codecin/lib/frac/pic.toml +10 -0
  44. codecin/lib/frac/src/frac.cin +252 -0
  45. codecin/lib/gostd/pic.toml +10 -0
  46. codecin/lib/gostd/src/gostd.cin +415 -0
  47. codecin/lib/graph/pic.toml +10 -0
  48. codecin/lib/graph/src/graph.cin +367 -0
  49. codecin/lib/gui/pic.toml +10 -0
  50. codecin/lib/gui/src/gui.cin +218 -0
  51. codecin/lib/hash/pic.toml +10 -0
  52. codecin/lib/hash/src/hash.cin +67 -0
  53. codecin/lib/heap/pic.toml +10 -0
  54. codecin/lib/heap/src/heap.cin +199 -0
  55. codecin/lib/io/pic.toml +10 -0
  56. codecin/lib/io/src/io.cin +141 -0
  57. codecin/lib/json/pic.toml +10 -0
  58. codecin/lib/json/src/json.cin +84 -0
  59. codecin/lib/key/pic.toml +10 -0
  60. codecin/lib/key/src/key.cin +78 -0
  61. codecin/lib/math/pic.toml +10 -0
  62. codecin/lib/math/src/math.cin +50 -0
  63. codecin/lib/matrix/pic.toml +10 -0
  64. codecin/lib/matrix/src/matrix.cin +155 -0
  65. codecin/lib/net/pic.toml +10 -0
  66. codecin/lib/net/src/net.cin +171 -0
  67. codecin/lib/path/pic.toml +10 -0
  68. codecin/lib/path/src/path.cin +372 -0
  69. codecin/lib/queue/pic.toml +10 -0
  70. codecin/lib/queue/src/queue.cin +112 -0
  71. codecin/lib/rand/pic.toml +10 -0
  72. codecin/lib/rand/src/rand.cin +46 -0
  73. codecin/lib/set/pic.toml +10 -0
  74. codecin/lib/set/src/set.cin +269 -0
  75. codecin/lib/sort/pic.toml +10 -0
  76. codecin/lib/sort/src/sort.cin +93 -0
  77. codecin/lib/stat/pic.toml +10 -0
  78. codecin/lib/stat/src/stat.cin +138 -0
  79. codecin/lib/str/pic.toml +10 -0
  80. codecin/lib/str/src/str.cin +54 -0
  81. codecin/lib/termux/pic.toml +10 -0
  82. codecin/lib/termux/src/termux.cin +118 -0
  83. codecin/lib/test/pic.toml +10 -0
  84. codecin/lib/test/src/test.cin +74 -0
  85. codecin/lib/text/pic.toml +10 -0
  86. codecin/lib/text/src/text.cin +323 -0
  87. codecin/lib/time/pic.toml +10 -0
  88. codecin/lib/time/src/time.cin +54 -0
  89. codecin/lib/token/pic.toml +10 -0
  90. codecin/lib/token/src/token.cin +183 -0
  91. codecin/lib/tree/pic.toml +10 -0
  92. codecin/lib/tree/src/tree.cin +256 -0
  93. codecin/lib/unionfind/pic.toml +10 -0
  94. codecin/lib/unionfind/src/unionfind.cin +115 -0
  95. codecin/lib/validate/pic.toml +10 -0
  96. codecin/lib/validate/src/validate.cin +148 -0
  97. codecin/lib/vec/pic.toml +10 -0
  98. codecin/lib/vec/src/vec.cin +88 -0
  99. codecin/logger.py +145 -0
  100. codecin/memory.py +306 -0
  101. codecin/mkn.py +916 -0
  102. codecin/native/aot/aot.go +114 -0
  103. codecin/native/aot/stub_main.go.txt +23 -0
  104. codecin/native/build.ps1 +33 -0
  105. codecin/native/build.sh +48 -0
  106. codecin/native/cmd/cincc/main.go +66 -0
  107. codecin/native/compiler/codegen.go +2238 -0
  108. codecin/native/compiler/compile_test.go +1069 -0
  109. codecin/native/compiler/parser.go +1331 -0
  110. codecin/native/compiler/syscalls.go +166 -0
  111. codecin/native/compiler/tokenizer.go +394 -0
  112. codecin/native/compiler/types.go +217 -0
  113. codecin/native/engine/args.go +37 -0
  114. codecin/native/engine/audio.go +402 -0
  115. codecin/native/engine/audio_other.go +105 -0
  116. codecin/native/engine/audio_test.go +279 -0
  117. codecin/native/engine/audio_windows.go +190 -0
  118. codecin/native/engine/canvas.go +271 -0
  119. codecin/native/engine/console_other.go +9 -0
  120. codecin/native/engine/console_windows.go +43 -0
  121. codecin/native/engine/crom.go +86 -0
  122. codecin/native/engine/encode.go +93 -0
  123. codecin/native/engine/engine_test.go +198 -0
  124. codecin/native/engine/ffi_nocgo.go +20 -0
  125. codecin/native/engine/ffi_registry.go +69 -0
  126. codecin/native/engine/ffi_unix.go +109 -0
  127. codecin/native/engine/ffi_windows.go +117 -0
  128. codecin/native/engine/gui.go +209 -0
  129. codecin/native/engine/gui_stub.go +13 -0
  130. codecin/native/engine/gui_test.go +148 -0
  131. codecin/native/engine/gui_windows.go +604 -0
  132. codecin/native/engine/gui_x11.go +723 -0
  133. codecin/native/engine/host_test.go +139 -0
  134. codecin/native/engine/hostcrypto.go +30 -0
  135. codecin/native/engine/hostdesktop.go +173 -0
  136. codecin/native/engine/hostnet.go +127 -0
  137. codecin/native/engine/isa_gen.go +689 -0
  138. codecin/native/engine/keyboard.go +432 -0
  139. codecin/native/engine/keyboard_other.go +94 -0
  140. codecin/native/engine/keyboard_stub.go +17 -0
  141. codecin/native/engine/keyboard_termios_darwin.go +9 -0
  142. codecin/native/engine/keyboard_termios_linux.go +9 -0
  143. codecin/native/engine/keyboard_test.go +246 -0
  144. codecin/native/engine/keyboard_windows.go +208 -0
  145. codecin/native/engine/keyboard_windows_test.go +123 -0
  146. codecin/native/engine/lineedit.go +262 -0
  147. codecin/native/engine/lineedit_test.go +105 -0
  148. codecin/native/engine/lineint_unix.go +17 -0
  149. codecin/native/engine/lineint_windows.go +19 -0
  150. codecin/native/engine/mem_other.go +37 -0
  151. codecin/native/engine/mem_windows.go +33 -0
  152. codecin/native/engine/native.go +640 -0
  153. codecin/native/engine/netsock.go +321 -0
  154. codecin/native/engine/ops_ext.go +515 -0
  155. codecin/native/engine/stdin.go +72 -0
  156. codecin/native/engine/stdin_other.go +9 -0
  157. codecin/native/engine/stdin_windows.go +60 -0
  158. codecin/native/engine/system.go +313 -0
  159. codecin/native/engine/termux.go +180 -0
  160. codecin/native/engine/version_gen.go +7 -0
  161. codecin/native/engine/vkb_darwin.go +62 -0
  162. codecin/native/engine/vkb_linux.go +95 -0
  163. codecin/native/engine/vkb_stub.go +11 -0
  164. codecin/native/engine/vm.go +1677 -0
  165. codecin/native/go.mod +3 -0
  166. codecin/native/ir/ir.go +48 -0
  167. codecin/native/main.go +448 -0
  168. codecin/native/mc/aarch64.go +165 -0
  169. codecin/native/mc/aarch64_test.go +30 -0
  170. codecin/native/mc/elf.go +88 -0
  171. codecin/native/mc/print_test.go +52 -0
  172. codecin/native/mc/x86_64.go +1262 -0
  173. codecin/native/mc/x86_64_test.go +102 -0
  174. codecin/native.py +432 -0
  175. codecin/native_codegen.py +987 -0
  176. codecin/nativegen.py +786 -0
  177. codecin/object.py +539 -0
  178. codecin/optimizer.py +236 -0
  179. codecin/packaging/__init__.py +46 -0
  180. codecin/packaging/archive.py +177 -0
  181. codecin/packaging/model.py +519 -0
  182. codecin/packaging/spec.py +193 -0
  183. codecin/packaging/store.py +372 -0
  184. codecin/packaging/toml.py +541 -0
  185. codecin/packaging/vcs.py +215 -0
  186. codecin/pic.py +1917 -0
  187. codecin/pic_api.py +374 -0
  188. codecin/pic_login.py +519 -0
  189. codecin/pic_registry.py +515 -0
  190. codecin/registers.py +130 -0
  191. codecin/sdgcc.py +493 -0
  192. codecin/stats.py +230 -0
  193. codecin/stdlib.py +138 -0
  194. codecin/toolchain.py +396 -0
  195. codecin/toolkit.py +26 -0
  196. codecin/version.py +260 -0
  197. codecin-7.22.10.dist-info/METADATA +1077 -0
  198. codecin-7.22.10.dist-info/RECORD +202 -0
  199. codecin-7.22.10.dist-info/WHEEL +5 -0
  200. codecin-7.22.10.dist-info/entry_points.txt +5 -0
  201. codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
  202. codecin-7.22.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,604 @@
1
+ //go:build windows
2
+
3
+ package engine
4
+
5
+ import (
6
+ "image"
7
+ "runtime"
8
+ "sync"
9
+ "syscall"
10
+ "time"
11
+ "unsafe"
12
+ )
13
+
14
+ var (
15
+ user32 = syscall.NewLazyDLL("user32.dll")
16
+ procGetModuleHandleW = kernel32.NewProc("GetModuleHandleW")
17
+ procRegisterClassExW = user32.NewProc("RegisterClassExW")
18
+ procCreateWindowExW = user32.NewProc("CreateWindowExW")
19
+ procDefWindowProcW = user32.NewProc("DefWindowProcW")
20
+ procPeekMessageW = user32.NewProc("PeekMessageW")
21
+ procTranslateMessage = user32.NewProc("TranslateMessage")
22
+ procDispatchMessageW = user32.NewProc("DispatchMessageW")
23
+ procDestroyWindow = user32.NewProc("DestroyWindow")
24
+ procInvalidateRect = user32.NewProc("InvalidateRect")
25
+ procUpdateWindow = user32.NewProc("UpdateWindow")
26
+ procBeginPaint = user32.NewProc("BeginPaint")
27
+ procEndPaint = user32.NewProc("EndPaint")
28
+ procAdjustWindowRect = user32.NewProc("AdjustWindowRect")
29
+ procLoadCursorW = user32.NewProc("LoadCursorW")
30
+ procGetKeyState = user32.NewProc("GetKeyState")
31
+ gdi32 = syscall.NewLazyDLL("gdi32.dll")
32
+ procStretchDIBits = gdi32.NewProc("StretchDIBits")
33
+ procSetStretchBltMode = gdi32.NewProc("SetStretchBltMode")
34
+ )
35
+
36
+ const (
37
+ wmDestroy = 0x0002
38
+ wmPaint = 0x000F
39
+ wmClose = 0x0010
40
+ wmKeydown = 0x0100
41
+ wmChar = 0x0102
42
+ wmSyskeydown = 0x0104
43
+ wmUnichar = 0x0109 // UTF-32 字符提交 (输入法/虚拟键盘)
44
+ wmMousemove = 0x0200
45
+ wmLbuttondown = 0x0201
46
+ wmLbuttonup = 0x0202
47
+ wmRbuttondown = 0x0204
48
+ wmRbuttonup = 0x0205
49
+ wmMbuttondown = 0x0207
50
+ wmMbuttonup = 0x0208
51
+ wmImeChar = 0x0286 // IME/触摸键盘提交的 UTF-16 字符
52
+
53
+ unicodeNoChar = 0xFFFF // WM_UNICHAR 探测: 应答复 1 表示支持
54
+
55
+ wsOverlappedWindow = 0x00CF0000
56
+ wsVisible = 0x10000000
57
+ cwUseDefault = 0x80000000
58
+ idcArrow = 32512
59
+ srccopy = 0x00CC0020
60
+ colorWindow = 5
61
+
62
+ vkShift = 0x10
63
+ vkControl = 0x11
64
+ )
65
+
66
+ // wndClassExW 手工布局 (x64, sizeof=80)。
67
+ type wndClassExW struct {
68
+ cbSize uint32
69
+ style uint32
70
+ lpfnWndProc uintptr
71
+ cbClsExtra int32
72
+ cbWndExtra int32
73
+ hInstance uintptr
74
+ hIcon uintptr
75
+ hCursor uintptr
76
+ hbrBackground uintptr
77
+ lpszMenuName uintptr
78
+ lpszClassName uintptr
79
+ hIconSm uintptr
80
+ }
81
+
82
+ // msgW 手工布局 (x64, sizeof=48)。
83
+ type msgW struct {
84
+ hwnd uintptr
85
+ message uint32
86
+ wParam uintptr
87
+ lParam uintptr
88
+ time uint32
89
+ ptX, ptY int32
90
+ }
91
+
92
+ // rectL 手工布局 (sizeof=16)。
93
+ type rectL struct{ left, top, right, bottom int32 }
94
+
95
+ // paintStructW 手工布局 (sizeof=72): hdc + fErase + rcPaint + 其余。
96
+ type paintStructW struct {
97
+ hdc uintptr
98
+ fErase int32
99
+ rcPaint rectL
100
+ fRestore int32
101
+ fIncUpdate int32
102
+ reserved [32]byte
103
+ }
104
+
105
+ // bmiHeader BITMAPINFOHEADER (sizeof=40)。
106
+ type bmiHeader struct {
107
+ biSize uint32
108
+ biWidth int32
109
+ biHeight int32 // 负值 = 自顶向下
110
+ biPlanes uint16
111
+ biBitCount uint16
112
+ biCompression uint32
113
+ biSizeImage uint32
114
+ biXPelsPerMeter int32
115
+ biYPelsPerMeter int32
116
+ biClrUsed uint32
117
+ biClrImportant uint32
118
+ }
119
+
120
+ // ---------------- 窗口线程 ----------------
121
+
122
+ type guiWinCmd struct {
123
+ kind int // 0 create / 1 update / 2 close
124
+ w, h int
125
+ title string
126
+ result chan bool
127
+ }
128
+
129
+ var (
130
+ guiWinCh chan guiWinCmd
131
+ guiWinOnce sync.Once
132
+ guiWinReady chan bool
133
+ guiWinMu sync.Mutex
134
+ guiWinHwnd uintptr
135
+ winW, winH int
136
+ winScratch []byte // 呈现用 BGRA 转换缓冲 (窗口线程私有访问)
137
+ winBMI bmiHeader
138
+ winClassReg bool
139
+ )
140
+
141
+ func guiPlatformOpen(w, h int, title string, buf *image.RGBA) bool {
142
+ guiWinOnce.Do(func() {
143
+ guiWinCh = make(chan guiWinCmd, 8)
144
+ guiWinReady = make(chan bool, 1)
145
+ go guiWinThread()
146
+ })
147
+ if !<-guiWinReady {
148
+ return false // 类注册失败 (极端环境)
149
+ }
150
+ res := make(chan bool, 1)
151
+ guiWinCh <- guiWinCmd{kind: 0, w: w, h: h, title: title, result: res}
152
+ if !<-res {
153
+ return false
154
+ }
155
+ _ = buf
156
+ return true
157
+ }
158
+
159
+ func guiPlatformUpdate(buf *image.RGBA) bool {
160
+ guiWinMu.Lock()
161
+ h := guiWinHwnd
162
+ guiWinMu.Unlock()
163
+ if h == 0 {
164
+ return false
165
+ }
166
+ // 快照 + RGBA→BGRA 转换 (窗口线程消费)
167
+ pix := guiSnapshot(buf)
168
+ guiWinMu.Lock()
169
+ if guiWinHwnd == 0 {
170
+ guiWinMu.Unlock()
171
+ return false
172
+ }
173
+ if pix != nil {
174
+ rgbaToBgra(pix, buf.Rect.Dx(), buf.Rect.Dy())
175
+ }
176
+ guiWinMu.Unlock()
177
+ procInvalidateRect.Call(h, 0, 0)
178
+ r, _, _ := procUpdateWindow.Call(h) // 同步触发 WM_PAINT
179
+ return r != 0
180
+ }
181
+
182
+ func guiPlatformClose() {
183
+ guiWinMu.Lock()
184
+ h := guiWinHwnd
185
+ guiWinMu.Unlock()
186
+ if h != 0 {
187
+ res := make(chan bool, 1)
188
+ guiWinCh <- guiWinCmd{kind: 2, result: res}
189
+ <-res
190
+ }
191
+ }
192
+
193
+ func guiWinThread() {
194
+ runtime.LockOSThread()
195
+ defer runtime.UnlockOSThread()
196
+ hInst, _, _ := procGetModuleHandleW.Call(0)
197
+ if hInst == 0 {
198
+ guiWinReady <- false
199
+ return
200
+ }
201
+ guiWinRegisterClassOnce(hInst)
202
+ guiWinReady <- true
203
+
204
+ quit := false
205
+ var m msgW
206
+ for !quit {
207
+ // 泵空所有待处理消息 (WM_PAINT 由 UpdateWindow 同步触发)
208
+ for {
209
+ r, _, _ := procPeekMessageW.Call(uintptr(unsafe.Pointer(&m)),
210
+ 0, 0, 0, 1 /*PM_REMOVE*/)
211
+ if r == 0 {
212
+ break
213
+ }
214
+ if m.message == wmQuit {
215
+ quit = true
216
+ break
217
+ }
218
+ procTranslateMessage.Call(uintptr(unsafe.Pointer(&m)))
219
+ procDispatchMessageW.Call(uintptr(unsafe.Pointer(&m)))
220
+ }
221
+ if quit {
222
+ break
223
+ }
224
+ select {
225
+ case cmd := <-guiWinCh:
226
+ guiWinHandleCmd(cmd)
227
+ case <-time.After(8 * time.Millisecond):
228
+ }
229
+ }
230
+ }
231
+
232
+ const wmQuit = 0x0012
233
+
234
+ func guiWinRegisterClassOnce(hInst uintptr) {
235
+ if winClassReg {
236
+ return
237
+ }
238
+ name, _ := syscall.UTF16PtrFromString("CodeCINWindow")
239
+ wc := wndClassExW{
240
+ cbSize: uint32(unsafe.Sizeof(wndClassExW{})),
241
+ style: 0x0003, // CS_HREDRAW | CS_VREDRAW
242
+ lpfnWndProc: syscall.NewCallback(guiWndProc),
243
+ hInstance: hInst,
244
+ hCursor: loadCursor(idcArrow),
245
+ hbrBackground: colorWindow + 1, // COLOR_WINDOW+1
246
+ lpszClassName: uintptr(unsafe.Pointer(name)),
247
+ }
248
+ procRegisterClassExW.Call(uintptr(unsafe.Pointer(&wc)))
249
+ winClassReg = true
250
+ }
251
+
252
+ func loadCursor(id uintptr) uintptr {
253
+ c, _, _ := procLoadCursorW.Call(0, id)
254
+ return c
255
+ }
256
+
257
+ func guiWinHandleCmd(cmd guiWinCmd) {
258
+ switch cmd.kind {
259
+ case 0: // create
260
+ title, _ := syscall.UTF16PtrFromString(cmd.title)
261
+ rc := rectL{0, 0, int32(cmd.w), int32(cmd.h)}
262
+ procAdjustWindowRect.Call(uintptr(unsafe.Pointer(&rc)), wsOverlappedWindow, 0)
263
+ ww := uintptr(rc.right - rc.left)
264
+ wh := uintptr(rc.bottom - rc.top)
265
+ hInst, _, _ := procGetModuleHandleW.Call(0)
266
+ h, _, _ := procCreateWindowExW.Call(0,
267
+ uintptr(unsafe.Pointer(wcClassName())), // 类名 (确保已注册)
268
+ uintptr(unsafe.Pointer(title)),
269
+ wsOverlappedWindow|wsVisible,
270
+ cwUseDefault, cwUseDefault, ww, wh,
271
+ 0, 0, hInst, 0)
272
+ guiWinMu.Lock()
273
+ guiWinHwnd = h
274
+ winW, winH = cmd.w, cmd.h
275
+ winBMI = bmiHeader{
276
+ biSize: uint32(unsafe.Sizeof(bmiHeader{})),
277
+ biWidth: int32(cmd.w),
278
+ biHeight: -int32(cmd.h),
279
+ biPlanes: 1,
280
+ biBitCount: 32,
281
+ }
282
+ guiWinMu.Unlock()
283
+ cmd.result <- h != 0
284
+ case 2: // close
285
+ guiWinMu.Lock()
286
+ h := guiWinHwnd
287
+ guiWinHwnd = 0
288
+ winScratch = nil
289
+ guiWinMu.Unlock()
290
+ if h != 0 {
291
+ procDestroyWindow.Call(h)
292
+ }
293
+ cmd.result <- true
294
+ // 线程保持存活 (消息泵常驻), 允许之后再次 gui_new
295
+ }
296
+ }
297
+
298
+ var wcNamePtr, _ = syscall.UTF16PtrFromString("CodeCINWindow")
299
+
300
+ func wcClassName() *uint16 { return wcNamePtr }
301
+
302
+ func guiWndProc(hwnd uintptr, msg uintptr, wp, lp uintptr) uintptr {
303
+ switch msg {
304
+ case wmPaint:
305
+ var ps paintStructW
306
+ if r, _, _ := procBeginPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps))); r != 0 {
307
+ guiWinMu.Lock()
308
+ scratch, w, h, bmi := winScratch, winW, winH, winBMI
309
+ guiWinMu.Unlock()
310
+ if scratch != nil && w > 0 && h > 0 {
311
+ procSetStretchBltMode.Call(ps.hdc, 3 /*COLORONCOLOR*/)
312
+ procStretchDIBits.Call(ps.hdc,
313
+ 0, 0, uintptr(w), uintptr(h),
314
+ 0, 0, uintptr(w), uintptr(h),
315
+ uintptr(unsafe.Pointer(&scratch[0])),
316
+ uintptr(unsafe.Pointer(&bmi)),
317
+ 0 /*DIB_RGB_COLORS*/, srccopy)
318
+ }
319
+ procEndPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
320
+ }
321
+ return 0
322
+ case wmClose:
323
+ guiVkbHide() // 程序窗口关闭时连带收起触摸键盘
324
+ guiMarkClosed() // gui_closed() 置 1; 交给 DefWindowProc 销毁窗口
325
+ case wmDestroy:
326
+ // 窗口已销毁 (无论来自 gui_close 还是用户点 X): 清理句柄,
327
+ // 避免残留的失效 hwnd 被后续 update/close 误用。
328
+ // 不 PostQuitMessage: 窗口线程常驻, 允许之后再次 gui_new。
329
+ guiWinMu.Lock()
330
+ guiWinHwnd = 0
331
+ winScratch = nil
332
+ guiWinMu.Unlock()
333
+ return 0
334
+ case wmKeydown:
335
+ guiWinKeyDown(wp, lp)
336
+ return 0
337
+ case wmChar:
338
+ guiWinChar(wp, lp)
339
+ return 0
340
+ case wmImeChar:
341
+ // IME / 屏幕键盘提交的字符可能绕过 WM_CHAR
342
+ guiWinChar(wp, lp)
343
+ return 0
344
+ case wmUnichar:
345
+ if wp == unicodeNoChar {
346
+ return 1 // 宣告本窗口支持 WM_UNICHAR
347
+ }
348
+ guiWinUniChar(wp)
349
+ return 0
350
+ case wmSyskeydown:
351
+ // Alt+F4 等交给 DefWindowProc (产生 WM_CLOSE)
352
+ case wmMousemove:
353
+ x, y := guiMouseXY(lp)
354
+ guiSetMouse(x, y, -1)
355
+ return 0
356
+ case wmLbuttondown:
357
+ x, y := guiMouseXY(lp)
358
+ guiSetMouse(x, y, -1)
359
+ guiMouseButtonBit(1, true)
360
+ case wmLbuttonup:
361
+ guiMouseButtonBit(1, false)
362
+ case wmRbuttondown:
363
+ guiMouseButtonBit(2, true)
364
+ case wmRbuttonup:
365
+ guiMouseButtonBit(2, false)
366
+ case wmMbuttondown:
367
+ guiMouseButtonBit(4, true)
368
+ case wmMbuttonup:
369
+ guiMouseButtonBit(4, false)
370
+ }
371
+ r, _, _ := procDefWindowProcW.Call(hwnd, uintptr(msg), wp, lp)
372
+ return r
373
+ }
374
+
375
+ // guiWinKeyDown WM_KEYDOWN: VK 映射扩展键 (方向键/功能键, 含修饰变体)。
376
+ func guiWinKeyDown(vk, lp uintptr) {
377
+ repeat := int(lp & 0xFFFF)
378
+ if repeat < 1 {
379
+ repeat = 1
380
+ }
381
+ if repeat > 8 {
382
+ repeat = 8
383
+ }
384
+ if code, ok := consoleKeyCode(uint16(vk), 0, guiWinKeyState()); ok {
385
+ guiInjectKey(code)
386
+ for i := 1; i < repeat; i++ {
387
+ guiInjectKey(code)
388
+ }
389
+ }
390
+ }
391
+
392
+ // guiWinChar WM_CHAR / WM_IME_CHAR: UTF-16 字符
393
+ // (含代理对组合 / Ctrl+字母控制字节)。
394
+ func guiWinChar(wp, lp uintptr) {
395
+ repeat := int(lp & 0xFFFF)
396
+ if repeat < 1 {
397
+ repeat = 1
398
+ }
399
+ if repeat > 8 {
400
+ repeat = 8
401
+ }
402
+ if code, ok := consoleKeyCode(0, rune(wp), guiWinKeyState()); ok {
403
+ guiInjectKey(code)
404
+ for i := 1; i < repeat; i++ {
405
+ guiInjectKey(code)
406
+ }
407
+ }
408
+ }
409
+
410
+ // guiWinUniChar WM_UNICHAR: UTF-32 字符 (虚拟键盘/输入法的文本提交通道)。
411
+ func guiWinUniChar(wp uintptr) {
412
+ if wp > 0x10FFFF || (wp >= 0xD800 && wp <= 0xDFFF) {
413
+ return // 越界码点或裸代理项: 丢弃
414
+ }
415
+ if wp == 0 {
416
+ return
417
+ }
418
+ if code, ok := consoleKeyCode(0, rune(wp), guiWinKeyState()); ok {
419
+ guiInjectKey(code)
420
+ }
421
+ }
422
+
423
+ // guiWinKeyState 取当前 Shift/Ctrl 状态位 (与控制台 dwControlKeyState 对齐)。
424
+ func guiWinKeyState() uint32 {
425
+ state := uint32(0)
426
+ if s, _, _ := procGetKeyState.Call(vkShift); s&0x8000 != 0 {
427
+ state |= ksShift
428
+ }
429
+ if s, _, _ := procGetKeyState.Call(vkControl); s&0x8000 != 0 {
430
+ state |= ksLeftCtrl
431
+ }
432
+ return state
433
+ }
434
+
435
+ // guiMouseXY lParam 拆坐标 (16 位有符号)。
436
+ func guiMouseXY(lp uintptr) (int, int) {
437
+ return int(int16(lp)), int(int16(lp >> 16))
438
+ }
439
+
440
+ // rgbaToBgra RGBA 画布 → BGRA 呈现缓冲 (窗口线程, 每帧一次)。
441
+ func rgbaToBgra(pix []byte, w, h int) {
442
+ n := w * h * 4
443
+ if cap(winScratch) < n {
444
+ winScratch = make([]byte, n)
445
+ }
446
+ winScratch = winScratch[:n]
447
+ for i := 0; i < n; i += 4 {
448
+ winScratch[i] = pix[i+2] // B
449
+ winScratch[i+1] = pix[i+1] // G
450
+ winScratch[i+2] = pix[i] // R
451
+ winScratch[i+3] = 0
452
+ }
453
+ }
454
+
455
+ // ---------------- 虚拟键盘 (Windows 屏幕/触摸键盘) ----------------
456
+ //
457
+ // 通过 WinRT InputPane 的桌面互操作接口为指定顶层窗口弹出/收起触摸键盘
458
+ // (TabTip / 输入体验)。这类键盘的按键由系统注入 (WM_KEYDOWN VK_PACKET +
459
+ // WM_CHAR / WM_IME_CHAR / WM_UNICHAR), 已由窗口过程统一转入按键队列,
460
+ // 因此 get_key 无需感知按键来源。
461
+
462
+ var (
463
+ combaseDLL = syscall.NewLazyDLL("combase.dll")
464
+ procRoInitialize = combaseDLL.NewProc("RoInitialize")
465
+ procRoGetActivationFactory = combaseDLL.NewProc("RoGetActivationFactory")
466
+ procWindowsCreateString = combaseDLL.NewProc("WindowsCreateString")
467
+ procWindowsDeleteString = combaseDLL.NewProc("WindowsDeleteString")
468
+ procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
469
+ procIsWindowVisible = user32.NewProc("IsWindowVisible")
470
+ procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
471
+ )
472
+
473
+ // winGUID GUID (Data1..Data4, sizeof=16)。
474
+ type winGUID struct {
475
+ Data1 uint32
476
+ Data2 uint16
477
+ Data3 uint16
478
+ Data4 [8]byte
479
+ }
480
+
481
+ var (
482
+ // IInputPaneInterop (Win32 桌面互操作, inputpaneinterop.h)
483
+ iidInputPaneInterop = winGUID{0x75CF2C57, 0x9195, 0x4931,
484
+ [8]byte{0x83, 0x32, 0xF0, 0xB4, 0x09, 0xE9, 0x16, 0xAF}}
485
+ // IInputPane2 提供 TryShow / TryHide
486
+ iidInputPane2 = winGUID{0x8A6B3F26, 0x7090, 0x4793,
487
+ [8]byte{0x94, 0x4C, 0xC3, 0xF2, 0xCD, 0xE2, 0x62, 0x76}}
488
+ )
489
+
490
+ const (
491
+ vtRelease = 2 // IUnknown::Release
492
+ vtFirstMethod = 6 // IUnknown(3) + IInspectable(3) 之后的第一个接口方法
493
+ vtInputPaneShow = vtFirstMethod // IInputPane2::TryShow
494
+ vtInputPaneHide = vtFirstMethod + 1 // IInputPane2::TryHide
495
+ rpcEChangedMode = 0x80010106
496
+ )
497
+
498
+ // winVtFn 读取 COM 对象虚表第 idx 项的函数指针。
499
+ func winVtFn(obj unsafe.Pointer, idx int) uintptr {
500
+ vt := *(*unsafe.Pointer)(obj)
501
+ return *(*uintptr)(unsafe.Add(vt, uintptr(idx)*unsafe.Sizeof(uintptr(0))))
502
+ }
503
+
504
+ // guiVkbTargetHwnd 虚拟键盘的绑定窗口: 优先 GUI 窗口; 终端模式下用可见的
505
+ // 控制台窗口; ConPTY (Windows Terminal) / 无控制台时退回前台窗口。
506
+ func guiVkbTargetHwnd() uintptr {
507
+ guiWinMu.Lock()
508
+ h := guiWinHwnd
509
+ guiWinMu.Unlock()
510
+ if h != 0 {
511
+ return h
512
+ }
513
+ if ch, _, _ := procGetConsoleWindow.Call(); ch != 0 {
514
+ if v, _, _ := procIsWindowVisible.Call(ch); v != 0 {
515
+ return ch
516
+ }
517
+ }
518
+ fg, _, _ := procGetForegroundWindow.Call()
519
+ return fg
520
+ }
521
+
522
+ // guiVkbAction 在 STA 线程上取得目标窗口的 IInputPane2 并调用 TryShow/TryHide。
523
+ func guiVkbAction(show bool) bool {
524
+ hwnd := guiVkbTargetHwnd()
525
+ if hwnd == 0 {
526
+ return false
527
+ }
528
+ // InputPane 只支持 STA: 固定 OS 线程并初始化单线程套间。
529
+ // 已初始化时 RoInitialize 返回 S_FALSE (1), 仍可继续。
530
+ runtime.LockOSThread()
531
+ defer runtime.UnlockOSThread()
532
+ if hr, _, _ := procRoInitialize.Call(0 /*RO_INIT_SINGLETHREADED*/); int32(hr) < 0 &&
533
+ uint32(hr) != rpcEChangedMode {
534
+ return false
535
+ }
536
+ name := "Windows.UI.ViewManagement.InputPane"
537
+ u16, _ := syscall.UTF16FromString(name)
538
+ var hstr uintptr
539
+ if hr, _, _ := procWindowsCreateString.Call(uintptr(unsafe.Pointer(&u16[0])),
540
+ uintptr(len(u16)-1), uintptr(unsafe.Pointer(&hstr))); int32(hr) < 0 || hstr == 0 {
541
+ return false
542
+ }
543
+ defer procWindowsDeleteString.Call(hstr)
544
+ var factory unsafe.Pointer
545
+ if hr, _, _ := procRoGetActivationFactory.Call(hstr,
546
+ uintptr(unsafe.Pointer(&iidInputPaneInterop)),
547
+ uintptr(unsafe.Pointer(&factory))); int32(hr) < 0 || factory == nil {
548
+ return false
549
+ }
550
+ defer syscall.SyscallN(winVtFn(factory, vtRelease), uintptr(factory))
551
+ var pane unsafe.Pointer
552
+ if hr, _, _ := syscall.SyscallN(winVtFn(factory, vtFirstMethod), uintptr(factory), hwnd,
553
+ uintptr(unsafe.Pointer(&iidInputPane2)),
554
+ uintptr(unsafe.Pointer(&pane))); int32(hr) < 0 || pane == nil {
555
+ return false
556
+ }
557
+ defer syscall.SyscallN(winVtFn(pane, vtRelease), uintptr(pane))
558
+ idx := vtInputPaneShow
559
+ if !show {
560
+ idx = vtInputPaneHide
561
+ }
562
+ var ret byte // WinRT boolean (1 字节)
563
+ hr, _, _ := syscall.SyscallN(winVtFn(pane, idx), uintptr(pane),
564
+ uintptr(unsafe.Pointer(&ret)))
565
+ return int32(hr) >= 0 && ret != 0
566
+ }
567
+
568
+ // vkbWinShown 记录触摸键盘是否由本程序弹出 (退出时据此收起)。
569
+ var (
570
+ vkbWinMu sync.Mutex
571
+ vkbWinShown bool
572
+ )
573
+
574
+ // guiVkbShow 弹出屏幕/触摸键盘。
575
+ func guiVkbShow() bool {
576
+ vkbWinMu.Lock()
577
+ defer vkbWinMu.Unlock()
578
+ ok := guiVkbAction(true)
579
+ if ok {
580
+ vkbWinShown = true
581
+ }
582
+ return ok
583
+ }
584
+
585
+ // guiVkbHide 收起屏幕/触摸键盘。
586
+ func guiVkbHide() bool {
587
+ vkbWinMu.Lock()
588
+ defer vkbWinMu.Unlock()
589
+ ok := guiVkbAction(false)
590
+ if ok {
591
+ vkbWinShown = false
592
+ }
593
+ return ok
594
+ }
595
+
596
+ // vkbProcessCleanup 程序退出时收起本程序弹出的触摸键盘。
597
+ func vkbProcessCleanup() {
598
+ vkbWinMu.Lock()
599
+ defer vkbWinMu.Unlock()
600
+ if vkbWinShown {
601
+ _ = guiVkbAction(false)
602
+ vkbWinShown = false
603
+ }
604
+ }