dsh-gufa-code 1.3.49 → 1.3.51

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 (34) hide show
  1. package/README.md +14 -4
  2. package/client.js +59 -1
  3. package/desktop/float/GufaFloat.m +12 -0
  4. package/desktop/float/PLATFORM-PROTOCOL.md +120 -0
  5. package/desktop/float/bin/README.md +12 -0
  6. package/desktop/float/bin/gufa-float-macos-arm64 +0 -0
  7. package/desktop/float/bin/gufa-float-macos-universal +0 -0
  8. package/desktop/float/bin/gufa-float-macos-x64 +0 -0
  9. package/desktop/float/bin/linux-x64/gufa-float-linux +0 -0
  10. package/desktop/float/bin/win-x64/gufa-float-win.exe +0 -0
  11. package/desktop/float/linux/Makefile +85 -0
  12. package/desktop/float/linux/README-LINUX.md +219 -0
  13. package/desktop/float/linux/json.c +498 -0
  14. package/desktop/float/linux/json.h +56 -0
  15. package/desktop/float/linux/log.c +48 -0
  16. package/desktop/float/linux/log.h +19 -0
  17. package/desktop/float/linux/main.c +1271 -0
  18. package/desktop/float/linux/x11.c +608 -0
  19. package/desktop/float/linux/x11.h +118 -0
  20. package/desktop/float/win/CMakeLists.txt +159 -0
  21. package/desktop/float/win/README-WIN.md +184 -0
  22. package/desktop/float/win/json.h +505 -0
  23. package/desktop/float/win/main.cpp +2246 -0
  24. package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/Info.plist +2 -2
  25. package/desktop/float//345/217/244/346/263/225/347/274/226/347/250/213/346/202/254/346/265/256/345/261/202.app/Contents/MacOS/gufa-float +0 -0
  26. package/docs/FLOAT-ACCEPTANCE.md +85 -0
  27. package/docs/MODES.md +7 -0
  28. package/host/dev.js +384 -57
  29. package/package.json +1 -1
  30. package/scripts/build-float-linux.sh +15 -0
  31. package/scripts/build-float-macos.sh +1 -0
  32. package/scripts/build-float-win.ps1 +33 -0
  33. package/src/client/00-core.js +3 -0
  34. package/src/client/90-app.js +55 -0
@@ -0,0 +1,505 @@
1
+ /**
2
+ * 古法编程 · Windows 悬浮层 helper —— 极简 JSON 解析/序列化(header-only)
3
+ *
4
+ * 只依赖 C++17 标准库。数字保留原始文本(raw),保证 payload.state 这类
5
+ * 「主机原样塞进来的 JSON」在解析 → 重新序列化后不丢精度、不改变写法。
6
+ *
7
+ * 用法:
8
+ * gufa::JsonValue root;
9
+ * std::wstring error;
10
+ * if (!gufa::ParseJson(text, root, &error)) { ... }
11
+ * const gufa::JsonValue* field = root.Find(L"appUrl");
12
+ */
13
+ #pragma once
14
+
15
+ #include <cstddef>
16
+ #include <string>
17
+ #include <utility>
18
+ #include <vector>
19
+
20
+ namespace gufa {
21
+
22
+ struct JsonValue {
23
+ enum class Type { Null, Bool, Number, String, Array, Object };
24
+
25
+ Type type = Type::Null;
26
+ bool boolean = false;
27
+ std::wstring number; // 原始 JSON 数字文本(如 "55"、"1.25e3")
28
+ std::wstring text; // String 的值
29
+ std::vector<JsonValue> items;
30
+ std::vector<std::pair<std::wstring, JsonValue>> members;
31
+
32
+ bool IsNull() const { return type == Type::Null; }
33
+ bool IsBool() const { return type == Type::Bool; }
34
+ bool IsNumber() const { return type == Type::Number; }
35
+ bool IsString() const { return type == Type::String; }
36
+ bool IsArray() const { return type == Type::Array; }
37
+ bool IsObject() const { return type == Type::Object; }
38
+
39
+ const JsonValue* Find(const std::wstring& key) const {
40
+ if (type != Type::Object) return nullptr;
41
+ for (const auto& member : members) {
42
+ if (member.first == key) return &member.second;
43
+ }
44
+ return nullptr;
45
+ }
46
+
47
+ const JsonValue* At(std::size_t index) const {
48
+ if (type != Type::Array || index >= items.size()) return nullptr;
49
+ return &items[index];
50
+ }
51
+ };
52
+
53
+ namespace json_detail {
54
+
55
+ constexpr int kMaxDepth = 128;
56
+
57
+ class Parser {
58
+ public:
59
+ explicit Parser(const std::wstring& text) : text_(text) {}
60
+
61
+ bool Parse(JsonValue& out) {
62
+ SkipWhitespace();
63
+ if (!ParseValue(out, 0)) return false;
64
+ SkipWhitespace();
65
+ if (pos_ != text_.size()) {
66
+ error_ = L"trailing characters after JSON value";
67
+ return false;
68
+ }
69
+ return true;
70
+ }
71
+
72
+ const std::wstring& error() const { return error_; }
73
+
74
+ private:
75
+ static bool IsDigit(wchar_t c) { return c >= L'0' && c <= L'9'; }
76
+
77
+ void SkipWhitespace() {
78
+ while (pos_ < text_.size()) {
79
+ const wchar_t c = text_[pos_];
80
+ if (c == L' ' || c == L'\t' || c == L'\n' || c == L'\r') {
81
+ ++pos_;
82
+ } else {
83
+ break;
84
+ }
85
+ }
86
+ }
87
+
88
+ bool ConsumeKeyword(const wchar_t* keyword, std::size_t length) {
89
+ if (pos_ + length > text_.size()) return false;
90
+ if (text_.compare(pos_, length, keyword) != 0) return false;
91
+ pos_ += length;
92
+ return true;
93
+ }
94
+
95
+ bool ParseValue(JsonValue& out, int depth) {
96
+ if (depth > kMaxDepth) {
97
+ error_ = L"JSON nesting too deep";
98
+ return false;
99
+ }
100
+ if (pos_ >= text_.size()) {
101
+ error_ = L"unexpected end of JSON";
102
+ return false;
103
+ }
104
+ const wchar_t c = text_[pos_];
105
+ switch (c) {
106
+ case L'{':
107
+ return ParseObject(out, depth);
108
+ case L'[':
109
+ return ParseArray(out, depth);
110
+ case L'"':
111
+ out.type = JsonValue::Type::String;
112
+ return ParseString(&out.text);
113
+ case L't':
114
+ if (!ConsumeKeyword(L"true", 4)) {
115
+ error_ = L"invalid literal (expected true)";
116
+ return false;
117
+ }
118
+ out.type = JsonValue::Type::Bool;
119
+ out.boolean = true;
120
+ return true;
121
+ case L'f':
122
+ if (!ConsumeKeyword(L"false", 5)) {
123
+ error_ = L"invalid literal (expected false)";
124
+ return false;
125
+ }
126
+ out.type = JsonValue::Type::Bool;
127
+ out.boolean = false;
128
+ return true;
129
+ case L'n':
130
+ if (!ConsumeKeyword(L"null", 4)) {
131
+ error_ = L"invalid literal (expected null)";
132
+ return false;
133
+ }
134
+ out.type = JsonValue::Type::Null;
135
+ return true;
136
+ default:
137
+ break;
138
+ }
139
+ if (c == L'-' || IsDigit(c)) return ParseNumber(out);
140
+ error_ = L"unexpected character in JSON";
141
+ return false;
142
+ }
143
+
144
+ bool ParseObject(JsonValue& out, int depth) {
145
+ ++pos_; // '{'
146
+ out.type = JsonValue::Type::Object;
147
+ out.members.clear();
148
+ SkipWhitespace();
149
+ if (pos_ < text_.size() && text_[pos_] == L'}') {
150
+ ++pos_;
151
+ return true;
152
+ }
153
+ for (;;) {
154
+ SkipWhitespace();
155
+ if (pos_ >= text_.size() || text_[pos_] != L'"') {
156
+ error_ = L"object key must be a string";
157
+ return false;
158
+ }
159
+ std::wstring key;
160
+ if (!ParseString(&key)) return false;
161
+ SkipWhitespace();
162
+ if (pos_ >= text_.size() || text_[pos_] != L':') {
163
+ error_ = L"missing ':' in object";
164
+ return false;
165
+ }
166
+ ++pos_;
167
+ SkipWhitespace();
168
+ JsonValue value;
169
+ if (!ParseValue(value, depth + 1)) return false;
170
+ out.members.emplace_back(std::move(key), std::move(value));
171
+ SkipWhitespace();
172
+ if (pos_ >= text_.size()) {
173
+ error_ = L"unterminated object";
174
+ return false;
175
+ }
176
+ if (text_[pos_] == L',') {
177
+ ++pos_;
178
+ continue;
179
+ }
180
+ if (text_[pos_] == L'}') {
181
+ ++pos_;
182
+ return true;
183
+ }
184
+ error_ = L"expected ',' or '}' in object";
185
+ return false;
186
+ }
187
+ }
188
+
189
+ bool ParseArray(JsonValue& out, int depth) {
190
+ ++pos_; // '['
191
+ out.type = JsonValue::Type::Array;
192
+ out.items.clear();
193
+ SkipWhitespace();
194
+ if (pos_ < text_.size() && text_[pos_] == L']') {
195
+ ++pos_;
196
+ return true;
197
+ }
198
+ for (;;) {
199
+ SkipWhitespace();
200
+ JsonValue value;
201
+ if (!ParseValue(value, depth + 1)) return false;
202
+ out.items.push_back(std::move(value));
203
+ SkipWhitespace();
204
+ if (pos_ >= text_.size()) {
205
+ error_ = L"unterminated array";
206
+ return false;
207
+ }
208
+ if (text_[pos_] == L',') {
209
+ ++pos_;
210
+ continue;
211
+ }
212
+ if (text_[pos_] == L']') {
213
+ ++pos_;
214
+ return true;
215
+ }
216
+ error_ = L"expected ',' or ']' in array";
217
+ return false;
218
+ }
219
+ }
220
+
221
+ bool ParseHex4(unsigned int* out) {
222
+ if (pos_ + 4 > text_.size()) {
223
+ error_ = L"truncated \\u escape";
224
+ return false;
225
+ }
226
+ unsigned int value = 0;
227
+ for (int i = 0; i < 4; ++i) {
228
+ const wchar_t c = text_[pos_++];
229
+ value <<= 4;
230
+ if (c >= L'0' && c <= L'9') {
231
+ value |= static_cast<unsigned int>(c - L'0');
232
+ } else if (c >= L'a' && c <= L'f') {
233
+ value |= static_cast<unsigned int>(c - L'a' + 10);
234
+ } else if (c >= L'A' && c <= L'F') {
235
+ value |= static_cast<unsigned int>(c - L'A' + 10);
236
+ } else {
237
+ error_ = L"invalid hex digit in \\u escape";
238
+ return false;
239
+ }
240
+ }
241
+ *out = value;
242
+ return true;
243
+ }
244
+
245
+ bool ParseString(std::wstring* out) {
246
+ if (pos_ >= text_.size() || text_[pos_] != L'"') {
247
+ error_ = L"expected string";
248
+ return false;
249
+ }
250
+ ++pos_;
251
+ out->clear();
252
+ while (pos_ < text_.size()) {
253
+ const wchar_t c = text_[pos_++];
254
+ if (c == L'"') return true;
255
+ if (c != L'\\') {
256
+ out->push_back(c);
257
+ continue;
258
+ }
259
+ if (pos_ >= text_.size()) {
260
+ error_ = L"truncated escape sequence";
261
+ return false;
262
+ }
263
+ const wchar_t escape = text_[pos_++];
264
+ switch (escape) {
265
+ case L'"':
266
+ out->push_back(L'"');
267
+ break;
268
+ case L'\\':
269
+ out->push_back(L'\\');
270
+ break;
271
+ case L'/':
272
+ out->push_back(L'/');
273
+ break;
274
+ case L'b':
275
+ out->push_back(L'\b');
276
+ break;
277
+ case L'f':
278
+ out->push_back(L'\f');
279
+ break;
280
+ case L'n':
281
+ out->push_back(L'\n');
282
+ break;
283
+ case L'r':
284
+ out->push_back(L'\r');
285
+ break;
286
+ case L't':
287
+ out->push_back(L'\t');
288
+ break;
289
+ case L'u': {
290
+ unsigned int code = 0;
291
+ if (!ParseHex4(&code)) return false;
292
+ if (code >= 0xD800 && code <= 0xDBFF && pos_ + 5 < text_.size() &&
293
+ text_[pos_] == L'\\' && text_[pos_ + 1] == L'u') {
294
+ const std::size_t saved = pos_;
295
+ pos_ += 2;
296
+ unsigned int low = 0;
297
+ if (!ParseHex4(&low)) {
298
+ pos_ = saved;
299
+ out->push_back(static_cast<wchar_t>(code));
300
+ break;
301
+ }
302
+ if (low >= 0xDC00 && low <= 0xDFFF) {
303
+ // 合并代理对,重新拆成 UTF-16 码元放回 wstring。
304
+ out->push_back(static_cast<wchar_t>(code));
305
+ out->push_back(static_cast<wchar_t>(low));
306
+ } else {
307
+ out->push_back(static_cast<wchar_t>(code));
308
+ out->push_back(static_cast<wchar_t>(low));
309
+ }
310
+ break;
311
+ }
312
+ if (code >= 0xD800 && code <= 0xDFFF) {
313
+ // 孤立代理项:WebView2 传来的 JSON 不应出现;原样保留。
314
+ out->push_back(static_cast<wchar_t>(code));
315
+ } else {
316
+ out->push_back(static_cast<wchar_t>(code));
317
+ }
318
+ break;
319
+ }
320
+ default:
321
+ error_ = L"invalid escape sequence";
322
+ return false;
323
+ }
324
+ }
325
+ error_ = L"unterminated string";
326
+ return false;
327
+ }
328
+
329
+ bool ParseNumber(JsonValue& out) {
330
+ const std::size_t start = pos_;
331
+ if (pos_ < text_.size() && text_[pos_] == L'-') ++pos_;
332
+ if (pos_ >= text_.size() || !IsDigit(text_[pos_])) {
333
+ error_ = L"invalid number";
334
+ return false;
335
+ }
336
+ if (text_[pos_] == L'0') {
337
+ ++pos_;
338
+ } else {
339
+ while (pos_ < text_.size() && IsDigit(text_[pos_])) ++pos_;
340
+ }
341
+ if (pos_ < text_.size() && text_[pos_] == L'.') {
342
+ ++pos_;
343
+ if (pos_ >= text_.size() || !IsDigit(text_[pos_])) {
344
+ error_ = L"invalid number fraction";
345
+ return false;
346
+ }
347
+ while (pos_ < text_.size() && IsDigit(text_[pos_])) ++pos_;
348
+ }
349
+ if (pos_ < text_.size() && (text_[pos_] == L'e' || text_[pos_] == L'E')) {
350
+ ++pos_;
351
+ if (pos_ < text_.size() && (text_[pos_] == L'+' || text_[pos_] == L'-')) ++pos_;
352
+ if (pos_ >= text_.size() || !IsDigit(text_[pos_])) {
353
+ error_ = L"invalid number exponent";
354
+ return false;
355
+ }
356
+ while (pos_ < text_.size() && IsDigit(text_[pos_])) ++pos_;
357
+ }
358
+ out.type = JsonValue::Type::Number;
359
+ out.number = text_.substr(start, pos_ - start);
360
+ return true;
361
+ }
362
+
363
+ const std::wstring& text_;
364
+ std::size_t pos_ = 0;
365
+ std::wstring error_;
366
+ };
367
+
368
+ inline void AppendHexEscape(wchar_t c, std::wstring* out) {
369
+ static constexpr wchar_t kHexDigits[] = L"0123456789abcdef";
370
+ const unsigned int code = static_cast<unsigned int>(c);
371
+ out->push_back(L'\\');
372
+ out->push_back(L'u');
373
+ out->push_back(kHexDigits[(code >> 12) & 0xF]);
374
+ out->push_back(kHexDigits[(code >> 8) & 0xF]);
375
+ out->push_back(kHexDigits[(code >> 4) & 0xF]);
376
+ out->push_back(kHexDigits[code & 0xF]);
377
+ }
378
+
379
+ inline void AppendEscapedString(const std::wstring& text, std::wstring* out) {
380
+ out->push_back(L'"');
381
+ for (const wchar_t c : text) {
382
+ switch (c) {
383
+ case L'"':
384
+ *out += L"\\\"";
385
+ break;
386
+ case L'\\':
387
+ *out += L"\\\\";
388
+ break;
389
+ case L'\b':
390
+ *out += L"\\b";
391
+ break;
392
+ case L'\f':
393
+ *out += L"\\f";
394
+ break;
395
+ case L'\n':
396
+ *out += L"\\n";
397
+ break;
398
+ case L'\r':
399
+ *out += L"\\r";
400
+ break;
401
+ case L'\t':
402
+ *out += L"\\t";
403
+ break;
404
+ default:
405
+ if (c < 0x20 || c == 0x2028 || c == 0x2029) {
406
+ AppendHexEscape(c, out);
407
+ } else {
408
+ out->push_back(c);
409
+ }
410
+ break;
411
+ }
412
+ }
413
+ out->push_back(L'"');
414
+ }
415
+
416
+ inline void SerializeValue(const JsonValue& value, std::wstring* out) {
417
+ switch (value.type) {
418
+ case JsonValue::Type::Null:
419
+ *out += L"null";
420
+ break;
421
+ case JsonValue::Type::Bool:
422
+ *out += value.boolean ? L"true" : L"false";
423
+ break;
424
+ case JsonValue::Type::Number:
425
+ *out += value.number.empty() ? L"0" : value.number;
426
+ break;
427
+ case JsonValue::Type::String:
428
+ AppendEscapedString(value.text, out);
429
+ break;
430
+ case JsonValue::Type::Array: {
431
+ out->push_back(L'[');
432
+ for (std::size_t i = 0; i < value.items.size(); ++i) {
433
+ if (i != 0) out->push_back(L',');
434
+ SerializeValue(value.items[i], out);
435
+ }
436
+ out->push_back(L']');
437
+ break;
438
+ }
439
+ case JsonValue::Type::Object: {
440
+ out->push_back(L'{');
441
+ for (std::size_t i = 0; i < value.members.size(); ++i) {
442
+ if (i != 0) out->push_back(L',');
443
+ AppendEscapedString(value.members[i].first, out);
444
+ out->push_back(L':');
445
+ SerializeValue(value.members[i].second, out);
446
+ }
447
+ out->push_back(L'}');
448
+ break;
449
+ }
450
+ }
451
+ }
452
+
453
+ } // namespace json_detail
454
+
455
+ /** 解析一段 JSON 文本;失败时把原因写入 error(可选)。 */
456
+ inline bool ParseJson(const std::wstring& text, JsonValue& out, std::wstring* error) {
457
+ json_detail::Parser parser(text);
458
+ if (parser.Parse(out)) return true;
459
+ if (error != nullptr) *error = parser.error();
460
+ return false;
461
+ }
462
+
463
+ /** 紧凑序列化(保持数字原始文本)。 */
464
+ inline std::wstring SerializeJson(const JsonValue& value) {
465
+ std::wstring out;
466
+ json_detail::SerializeValue(value, &out);
467
+ return out;
468
+ }
469
+
470
+ /** 转义成 JS 单引号字符串字面量的内容(不含首尾引号)。 */
471
+ inline std::wstring EscapeJsSingleQuoted(const std::wstring& text) {
472
+ std::wstring out;
473
+ out.reserve(text.size() + 16);
474
+ for (const wchar_t c : text) {
475
+ switch (c) {
476
+ case L'\\':
477
+ out += L"\\\\";
478
+ break;
479
+ case L'\'':
480
+ out += L"\\'";
481
+ break;
482
+ case L'\n':
483
+ out += L"\\n";
484
+ break;
485
+ case L'\r':
486
+ out += L"\\r";
487
+ break;
488
+ case L'\t':
489
+ out += L"\\t";
490
+ break;
491
+ case 0x2028:
492
+ out += L"\\u2028";
493
+ break;
494
+ case 0x2029:
495
+ out += L"\\u2029";
496
+ break;
497
+ default:
498
+ out.push_back(c);
499
+ break;
500
+ }
501
+ }
502
+ return out;
503
+ }
504
+
505
+ } // namespace gufa