-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcompat.x11.lua
More file actions
690 lines (639 loc) · 19.6 KB
/
Copy pathcompat.x11.lua
File metadata and controls
690 lines (639 loc) · 19.6 KB
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
package = {
spec = "1",
namespace = "compat",
name = "x11",
description = "Xlib runtime library and public headers built from upstream sources",
licenses = {"BSD-1-Clause", "HPND-sell-variant", "ISC", "MIT", "MIT-Open-Group", "X11"},
repo = "https://gitlab.freedesktop.org/xorg/lib/libx11",
type = "package",
xpm = {
linux = {
["1.8.13"] = {
url = {
GLOBAL = "https://xorg.freedesktop.org/releases/individual/lib/libX11-1.8.13.tar.gz",
CN = "https://gitcode.com/mcpp-res/x11/releases/download/1.8.13/x11-1.8.13.tar.gz",
},
sha256 = "acf0e7cd7541110e6330ecb539441a2d53061f386ec7be6906dfde0de2598470",
},
},
},
mcpp = {
language = "c++23",
import_std = false,
c_standard = "c11",
include_dirs = {
".",
"include",
"include/X11",
"src",
"src/xcms",
"src/xkb",
"src/xlibi18n",
"modules/im/ximcp",
"modules/lc/def",
"modules/lc/gen",
"modules/lc/Utf8",
"modules/om/generic",
},
cflags = {
"-D_GNU_SOURCE",
"-D_DEFAULT_SOURCE",
"-D_BSD_SOURCE",
"-DXIM_t=1",
"-DTRANS_CLIENT=1",
"-DHAVE_CONFIG_H",
},
sources = {
"src/*.c",
"src/xcms/*.c",
"src/xkb/*.c",
"src/xlibi18n/*.c",
"!src/xlibi18n/XlcDL.c",
"!src/xlibi18n/XlcSL.c",
"modules/im/ximcp/*.c",
"modules/lc/def/*.c",
"modules/lc/gen/*.c",
"modules/lc/Utf8/*.c",
"modules/om/generic/*.c",
},
targets = { ["X11"] = { kind = "shared", soname = "libX11.so.6" } },
deps = {
["compat.xcb"] = "1.17.0",
["compat.xorgproto"] = "2025.1",
["compat.xtrans"] = "1.6.0",
},
},
}
import("xim.libxpkg.pkginfo")
import("xim.libxpkg.log")
local makekeys_py = [=[
import re
import sys
KTNUM = 4000
MIN_REHASH = 15
MATCHES = 10
XK_VOID_SYMBOL = 0xFFFFFF
defs = []
def parse_line(line):
m = re.match(r"#define\s+(\S+)\s+0x([0-9a-fA-F]+)", line)
if m and "XK_" in m.group(1):
key = m.group(1)
i = key.find("XK_")
return key[:i] + key[i + 3:], int(m.group(2), 16)
m = re.match(r"#define\s+(\S+)\s+_EVDEVK\(0x([0-9a-fA-F]+)\)", line)
if m and "XK_" in m.group(1):
key = m.group(1)
i = key.find("XK_")
return key[:i] + key[i + 3:], int(m.group(2), 16) + 0x10081000
m = re.match(r"#define\s+(\S+)\s+(\S+)", line)
if m and "XK_" in m.group(1) and "XK_" in m.group(2):
key = m.group(1)
alias = m.group(2)
i = key.find("XK_")
ai = alias.find("XK_")
name = key[:i] + key[i + 3:]
alias_name = alias[:ai] + alias[ai + 3:]
for old_name, old_value in reversed(defs):
if old_name == alias_name:
return name, old_value
return None
for filename in sys.argv[1:]:
with open(filename, "r", encoding="utf-8", errors="ignore") as f:
for line in f:
parsed = parse_line(line)
if not parsed:
continue
name, value = parsed
if value == XK_VOID_SYMBOL:
value = 0
if value > 0x1FFFFFFF:
continue
defs.append((name, value))
def signature(name):
sig = 0
for ch in name:
sig = ((sig << 1) + ord(ch)) & 0xFFFFFFFF
return sig
def find_hash_by_name():
best_max = len(defs)
best_z = 0
found = 0
for z in range(len(defs), KTNUM):
used = [False] * z
max_rehash = 0
ok = True
for name, _ in defs:
sig = signature(name)
first = j = sig % z
k = 0
while used[j]:
k += 1
j += first + 1
if j >= z:
j -= z
if j == first:
ok = False
break
if not ok:
break
used[j] = True
max_rehash = max(max_rehash, k)
if ok and max_rehash < MIN_REHASH:
if max_rehash < best_max:
best_max = max_rehash
best_z = z
found += 1
if found >= MATCHES:
break
return best_z, best_max
def find_hash_by_value():
best_max = len(defs)
best_z = 0
found = 0
for z in range(len(defs), KTNUM):
used = [False] * z
values = [None] * z
max_rehash = 0
ok = True
for _, value in defs:
first = j = value % z
k = 0
while used[j]:
if values[j] == value:
break
k += 1
j += first + 1
if j >= z:
j -= z
if j == first:
ok = False
break
if not ok:
break
used[j] = True
values[j] = value
max_rehash = max(max_rehash, k)
if ok and max_rehash < MIN_REHASH:
if max_rehash < best_max:
best_max = max_rehash
best_z = z
found += 1
if found >= MATCHES:
break
return best_z, best_max
def c_char(ch):
if ch == "'":
return "'\\''"
if ch == "\\":
return "'\\\\'"
return "'" + ch + "'"
z, max_rehash = find_hash_by_name()
if not z:
raise SystemExit("makekeys: failed to find string hash")
offsets = [0] * z
indexes = [0] * len(defs)
rows = []
k = 1
for i, (name, value) in enumerate(defs):
sig = signature(name)
first = j = sig % z
while offsets[j]:
j += first + 1
if j >= z:
j -= z
offsets[j] = k
indexes[i] = k
row = [
"0x%.2x" % ((sig >> 8) & 0xff),
"0x%.2x" % (sig & 0xff),
"0x%.2x" % ((value >> 24) & 0xff),
"0x%.2x" % ((value >> 16) & 0xff),
"0x%.2x" % ((value >> 8) & 0xff),
"0x%.2x" % (value & 0xff),
]
row.extend(c_char(ch) for ch in name)
row.append("0")
rows.append(row)
k += 7 + len(name)
print("/* This file is generated from keysymdef.h. */")
print("/* Do not edit. */")
print()
print("#ifdef NEEDKTABLE")
print("const unsigned char _XkeyTable[] = {")
print("0,")
for i, row in enumerate(rows):
end = "" if i == len(rows) - 1 else ","
print(", ".join(row) + end)
print("};")
print()
print("#define KTABLESIZE %d" % z)
print("#define KMAXHASH %d" % (max_rehash + 1))
print()
print("static const unsigned short hashString[KTABLESIZE] = {")
for i in range(0, z, 8):
chunk = offsets[i:i + 8]
line = ", ".join("0x%.4x" % x for x in chunk)
if i + 8 < z:
line += ","
print(line)
print()
print("};")
print("#endif /* NEEDKTABLE */")
z, max_rehash = find_hash_by_value()
if not z:
raise SystemExit("makekeys: failed to find value hash")
offsets = [0] * z
values = [None] * z
for i, (_, value) in enumerate(defs):
first = j = value % z
while offsets[j]:
if values[j] == value:
break
j += first + 1
if j >= z:
j -= z
if not offsets[j]:
offsets[j] = indexes[i] + 2
values[j] = value
print()
print("#ifdef NEEDVTABLE")
print("#define VTABLESIZE %d" % z)
print("#define VMAXHASH %d" % (max_rehash + 1))
print()
print("static const unsigned short hashKeysym[VTABLESIZE] = {")
for i in range(0, z, 8):
chunk = offsets[i:i + 8]
line = ", ".join("0x%.4x" % x for x in chunk)
if i + 8 < z:
line += ","
print(line)
print()
print("};")
print("#endif /* NEEDVTABLE */")
]=]
local function first_existing(paths)
for _, candidate in ipairs(paths) do
if os.isfile(candidate) then
return candidate
end
end
return nil
end
local function xorgproto_header(root, name)
return first_existing({
path.join(root, "include", "X11", name),
path.join(root, "xorgproto-2025.1", "include", "X11", name),
})
end
local function c_string(value)
return '"' .. tostring(value):gsub("\\", "\\\\"):gsub('"', '\\"') .. '"'
end
local function strip_xk_prefix(symbol)
local pos = symbol:find("XK_", 1, true)
if not pos then
return nil
end
return symbol:sub(1, pos - 1) .. symbol:sub(pos + 3)
end
local function parse_keysym_headers(headers)
local defs = {}
for _, filename in ipairs(headers) do
local content = io.readfile(filename)
for line in content:gmatch("[^\r\n]+") do
local key, hex = line:match("^%s*#define%s+(%S+)%s+0x([0-9a-fA-F]+)")
if key and key:find("XK_", 1, true) then
local name = strip_xk_prefix(key)
local value = tonumber(hex, 16)
if value == 0xFFFFFF then
value = 0
end
if value <= 0x1FFFFFFF then
table.insert(defs, { name = name, value = value })
end
else
key, hex = line:match("^%s*#define%s+(%S+)%s+_EVDEVK%(0x([0-9a-fA-F]+)%)")
if key and key:find("XK_", 1, true) then
local name = strip_xk_prefix(key)
table.insert(defs, { name = name, value = tonumber(hex, 16) + 0x10081000 })
else
local alias
key, alias = line:match("^%s*#define%s+(%S+)%s+(%S+)")
if key and alias and key:find("XK_", 1, true) and alias:find("XK_", 1, true) then
local name = strip_xk_prefix(key)
local alias_name = strip_xk_prefix(alias)
for i = #defs, 1, -1 do
if defs[i].name == alias_name then
table.insert(defs, { name = name, value = defs[i].value })
break
end
end
end
end
end
end
end
return defs
end
local function signature(name)
local sig = 0
for i = 1, #name do
sig = (sig * 2 + name:byte(i)) % 0x100000000
end
return sig
end
local function find_hash_by_name(defs)
local best_max = #defs
local best_z = 0
local found = 0
for z = #defs, 3999 do
local used = {}
local max_rehash = 0
local ok = true
for _, def in ipairs(defs) do
local sig = signature(def.name)
local first = sig % z
local j = first
local k = 0
while used[j] do
k = k + 1
j = j + first + 1
if j >= z then
j = j - z
end
if j == first then
ok = false
break
end
end
if not ok then
break
end
used[j] = true
if k > max_rehash then
max_rehash = k
end
end
if ok and max_rehash < 15 then
if max_rehash < best_max then
best_max = max_rehash
best_z = z
end
found = found + 1
if found >= 10 then
break
end
end
end
return best_z, best_max
end
local function find_hash_by_value(defs)
local best_max = #defs
local best_z = 0
local found = 0
for z = #defs, 3999 do
local used = {}
local values = {}
local max_rehash = 0
local ok = true
for _, def in ipairs(defs) do
local first = def.value % z
local j = first
local k = 0
while used[j] do
if values[j] == def.value then
break
end
k = k + 1
j = j + first + 1
if j >= z then
j = j - z
end
if j == first then
ok = false
break
end
end
if not ok then
break
end
used[j] = true
values[j] = def.value
if k > max_rehash then
max_rehash = k
end
end
if ok and max_rehash < 15 then
if max_rehash < best_max then
best_max = max_rehash
best_z = z
end
found = found + 1
if found >= 10 then
break
end
end
end
return best_z, best_max
end
local function c_char(ch)
if ch == "'" then
return "'\\''"
end
if ch == "\\" then
return "'\\\\'"
end
return "'" .. ch .. "'"
end
local function hex2(value)
return string.format("0x%.2x", value % 0x100)
end
local function hex4(value)
return string.format("0x%.4x", value % 0x10000)
end
local function write_ks_tables(headers, out)
local defs = parse_keysym_headers(headers)
local z, max_rehash = find_hash_by_name(defs)
if z == 0 then
log.error("makekeys: failed to find string hash")
return false
end
local lines = {
"/* This file is generated from keysymdef.h. */",
"/* Do not edit. */",
"",
"#ifdef NEEDKTABLE",
"const unsigned char _XkeyTable[] = {",
"0,",
}
local offsets = {}
local indexes = {}
local rows = {}
local k = 1
for i, def in ipairs(defs) do
local sig = signature(def.name)
local first = sig % z
local j = first
while offsets[j] do
j = j + first + 1
if j >= z then
j = j - z
end
end
offsets[j] = k
indexes[i] = k
local row = {
hex2(math.floor(sig / 0x100)),
hex2(sig),
hex2(math.floor(def.value / 0x1000000)),
hex2(math.floor(def.value / 0x10000)),
hex2(math.floor(def.value / 0x100)),
hex2(def.value),
}
for pos = 1, #def.name do
table.insert(row, c_char(def.name:sub(pos, pos)))
end
table.insert(row, "0")
table.insert(rows, row)
k = k + 7 + #def.name
end
for i, row in ipairs(rows) do
local suffix = i == #rows and "" or ","
table.insert(lines, table.concat(row, ", ") .. suffix)
end
table.insert(lines, "};")
table.insert(lines, "")
table.insert(lines, "#define KTABLESIZE " .. tostring(z))
table.insert(lines, "#define KMAXHASH " .. tostring(max_rehash + 1))
table.insert(lines, "")
table.insert(lines, "static const unsigned short hashString[KTABLESIZE] = {")
for i = 0, z - 1, 8 do
local chunk = {}
for j = i, math.min(i + 7, z - 1) do
table.insert(chunk, hex4(offsets[j] or 0))
end
local suffix = i + 8 < z and "," or ""
table.insert(lines, table.concat(chunk, ", ") .. suffix)
end
table.insert(lines, "")
table.insert(lines, "};")
table.insert(lines, "#endif /* NEEDKTABLE */")
z, max_rehash = find_hash_by_value(defs)
if z == 0 then
log.error("makekeys: failed to find value hash")
return false
end
offsets = {}
local values = {}
for i, def in ipairs(defs) do
local first = def.value % z
local j = first
while offsets[j] do
if values[j] == def.value then
break
end
j = j + first + 1
if j >= z then
j = j - z
end
end
if not offsets[j] then
offsets[j] = indexes[i] + 2
values[j] = def.value
end
end
table.insert(lines, "")
table.insert(lines, "#ifdef NEEDVTABLE")
table.insert(lines, "#define VTABLESIZE " .. tostring(z))
table.insert(lines, "#define VMAXHASH " .. tostring(max_rehash + 1))
table.insert(lines, "")
table.insert(lines, "static const unsigned short hashKeysym[VTABLESIZE] = {")
for i = 0, z - 1, 8 do
local chunk = {}
for j = i, math.min(i + 7, z - 1) do
table.insert(chunk, hex4(offsets[j] or 0))
end
local suffix = i + 8 < z and "," or ""
table.insert(lines, table.concat(chunk, ", ") .. suffix)
end
table.insert(lines, "")
table.insert(lines, "};")
table.insert(lines, "#endif /* NEEDVTABLE */")
io.writefile(out, table.concat(lines, "\n") .. "\n")
return true
end
local function write_config(installdir)
local x11datadir = path.join(installdir, "share", "X11")
io.writefile(path.join(installdir, "config.h"), [[#ifndef MCPP_COMPAT_X11_CONFIG_H
#define MCPP_COMPAT_X11_CONFIG_H
#define HAVE_UNISTD_H 1
#define HAVE_SYS_IOCTL_H 1
#define HAVE_SYS_SELECT_H 1
#define HAVE_SYS_SOCKET_H 1
#define HAVE_STRCASECMP 1
#define HAVE_STRNCASECMP 1
#define HAVE_STRDUP 1
#define HAVE_POLL 1
#define USE_POLL 1
#define HAS_SHM 1
#define XTHREADS 1
#define XUSE_MTSAFE_API 1
#define XKB 1
#define XCMS 1
#define XLOCALE 1
#define XCMSDIR ]] .. c_string(x11datadir) .. "\n" .. [[
#define XLOCALEDIR ]] .. c_string(path.join(x11datadir, "locale")) .. "\n" .. [[
#define XLOCALELIBDIR ]] .. c_string(path.join(x11datadir, "locale")) .. "\n" .. [[
#define XLOCALEDATADIR ]] .. c_string(path.join(x11datadir, "locale")) .. "\n" .. [[
#define XERRORDB ]] .. c_string(path.join(x11datadir, "XErrorDB")) .. [[
#endif
]])
io.writefile(path.join(installdir, "include", "X11", "XlibConf.h"), [[#ifndef _XLIBCONF_H_
#define _XLIBCONF_H_
#define XTHREADS 1
#define XUSE_MTSAFE_API 1
#endif
]])
end
local function generate_ks_tables(installdir)
-- Addressed by declared identity `<namespace>:<package.name>` — see the
-- same ladder in compat.xcb.lua. The trailing spellings are pre-SPEC-001.
local proto_dir = pkginfo.install_dir("compat:xorgproto", "2025.1")
or pkginfo.install_dir("compat:compat.xorgproto", "2025.1")
or pkginfo.install_dir("compat.xorgproto", "2025.1")
-- `os.isdir` matters as much as the nil check: a miss can hand back a
-- plausible path that was never populated, and the keysym tables would
-- then be generated empty instead of the build failing here.
if not proto_dir or not os.isdir(proto_dir) then
log.error("xorgproto@2025.1 install dir not found")
return false
end
local headers = {
xorgproto_header(proto_dir, "keysymdef.h"),
xorgproto_header(proto_dir, "XF86keysym.h"),
xorgproto_header(proto_dir, "Sunkeysym.h"),
xorgproto_header(proto_dir, "DECkeysym.h"),
xorgproto_header(proto_dir, "HPkeysym.h"),
}
for _, header in ipairs(headers) do
if not header then
log.error("missing xorgproto keysym header")
return false
end
end
local out = path.join(installdir, "src", "ks_tables.h")
return write_ks_tables(headers, out) and os.isfile(out)
end
function install()
local srcdir = pkginfo.install_file():replace(".tar.gz", "")
if not os.isdir(srcdir) then
srcdir = "libX11-" .. pkginfo.version()
end
os.tryrm(pkginfo.install_dir())
os.mv(srcdir, pkginfo.install_dir())
local datadir = path.join(pkginfo.install_dir(), "share", "X11")
os.mkdir(datadir)
os.cp(path.join(pkginfo.install_dir(), "src", "XErrorDB"), path.join(datadir, "XErrorDB"))
os.cp(path.join(pkginfo.install_dir(), "src", "xcms", "Xcms.txt"), path.join(datadir, "Xcms.txt"))
write_config(pkginfo.install_dir())
return generate_ks_tables(pkginfo.install_dir())
end