-
Notifications
You must be signed in to change notification settings - Fork 1
/
rime_api.ahk
1618 lines (1455 loc) · 59.1 KB
/
rime_api.ahk
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
/*
* Copyright (c) 2023, 2024 Xuesong Peng <pengxuesong.cn@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
global A_BitsPerByte := 8
global A_IntSize := 4
global A_IntPaddingSize := A_PtrSize - A_IntSize
global Rime_BufferSize := 512
class RimeError extends Error {
;
}
/**
* UTF-8 C-String, null-terminated
*/
class RimeString extends Buffer {
__New(val := "") {
super.__New(StrPut(val, "UTF-8"), 0)
StrPut(val, this, "UTF-8")
}
}
/**
* UTF-8 C-String array, may not be null-terminated and null value can exist in the middle
*
* The consecutive `Length * A_PtrSize` bytes from beginning
* stores the pointers to each C-string, `0` for null value
*
* After all pointers, the rest consecutive memory to the end
* stores all null-terminated C-string one by one
*/
class RimeStringArray extends Buffer {
; Copyright (c) thqby <https://www.autohotkey.com/boards/viewtopic.php?t=122600#p544500>
__New(val_arr := []) {
sz := 0, ptrsz := 0
for val in val_arr {
ptrsz += A_PtrSize
if not IsSet(val)
continue
sz += StrPut(val, "UTF-8")
}
super.__New(sz + ptrsz, 0)
p := this.Ptr, pstr := p + val_arr.Length * A_PtrSize
for idx, val in val_arr {
if not IsSet(val)
p := NumPut("Ptr", 0, p)
else {
p := NumPut("Ptr", pstr, p)
pstr += StrPut(val, pstr, "UTF-8")
}
}
}
}
/**
* The base class to wrap a struct in librime
*
* `static struct_size` - returns the size of the underlying data in bytes
*/
class RimeStruct extends Buffer {
; set this.Size before calling it
copy(src, tgt := this, offset := 0, length := %Type(this)%.struct_size) {
if src and tgt
Loop length {
byte := NumGet(src, A_Index - 1, "Char")
NumPut("Char", byte, tgt, A_Index - 1)
}
}
num_get(offset := 0, type := "Int") {
return NumGet(this, offset, type)
}
num_put(type := "Int", val := 0, offset := 0) {
NumPut(type, val, this, offset)
}
c_str_get(src := this, offset := 0, length := -1) {
if not src or not p := NumGet(src, offset, "Ptr")
return ""
if length < 0
return StrGet(p, "UTF-8")
buf := Buffer(length + 1, 0)
Loop length {
byte := NumGet(p, A_Index - 1, "Char")
NumPut("Char", byte, buf, A_Index - 1)
}
return StrGet(buf, "UTF-8")
}
c_str_put(val, tgt := this, offset := 0) {
if not tgt
return Buffer()
buf := RimeString(val)
NumPut("Ptr", buf.Ptr, tgt, offset)
return buf
}
; librime's string arrays are null-terminated
c_str_array_get(offset) {
res := Array()
if ptr := this.num_get(offset, "Ptr")
Loop {
if not val := this.c_str_get(ptr, (A_Index - 1) * A_PtrSize)
break
res.Push(val)
}
return res
}
c_str_array_put(val_array, tgt := this, offset := 0) {
if not tgt
return Buffer()
arr_buf := RimeStringArray(val_array)
NumPut("Ptr", arr_buf.Ptr, tgt, offset)
return arr_buf
}
struct_array_get(offset, length, struct) {
res := Array()
if ptr := this.num_get(offset, "Ptr")
Loop length {
res.Push(struct.Call(ptr + (A_Index - 1) * struct.struct_size))
}
return res
}
} ; RimeStruct
/**
* The class to wrap a struct with version control
*
* Traits
*
* `static data_size_offset` - returns the pointer to `data_size`
*/
class RimeVersionedStruct extends RimeStruct {
data_size {
get => this.num_get(%Type(this)%.data_size_offset)
}
/**
* The method is **NOT** static
* @param member type of `Str`
* @returns `True` if has member, `False` if not
*/
has_member(member) {
offset := member . "_offset"
if not %Type(this)%.HasOwnProp(offset)
return False
return this.data_size + A_IntSize > %Type(this)%.%offset%
}
/**
* Assume member is a non-null pointer in this struct
* @param member type of `Str`
* @returns `True` if member has value, `False` if no member or null
*/
provided(member) {
return this.has_member(member) and !!this.%member%
}
}
class RimeApiStruct extends RimeVersionedStruct {
fp(offset) {
return this.num_get(offset, "Ptr")
}
/**
* Clients should test if an api function is available in the current version before calling it.
* @param func type of `Str`
* @returns `True` if API available, `False` if not
*/
api_available(func) {
return this.has_member(func) and !!this.%func%
}
}
class RimeTraits extends RimeVersionedStruct {
__New() {
super.__New(RimeTraits.struct_size, 0)
this.data_size := RimeTraits.struct_size - A_IntSize
}
static data_size_offset := 0
static shared_data_dir_offset := RimeTraits.data_size_offset + A_IntSize + A_IntPaddingSize
static user_data_dir_offset := RimeTraits.shared_data_dir_offset + A_PtrSize
static distribution_name_offset := RimeTraits.user_data_dir_offset + A_PtrSize
static distribution_code_name_offset := RimeTraits.distribution_name_offset + A_PtrSize
static distribution_version_offset := RimeTraits.distribution_code_name_offset + A_PtrSize
static app_name_offset := RimeTraits.distribution_version_offset + A_PtrSize
static modules_offset := RimeTraits.app_name_offset + A_PtrSize
static min_log_level_offset := RimeTraits.modules_offset + A_PtrSize
static log_dir_offset := RimeTraits.min_log_level_offset + A_IntSize + A_IntPaddingSize
static prebuilt_data_dir_offset := RimeTraits.log_dir_offset + A_PtrSize
static staging_dir_offset := RimeTraits.prebuilt_data_dir_offset + A_PtrSize
static struct_size := RimeTraits.staging_dir_offset + A_PtrSize
data_size {
set => this.num_put(, Value, RimeTraits.data_size_offset)
}
shared_data_dir {
get => this.c_str_get(, RimeTraits.shared_data_dir_offset)
set => this.__shared_data_dir := this.c_str_put(Value, , RimeTraits.shared_data_dir_offset)
}
user_data_dir {
get => this.c_str_get(, RimeTraits.user_data_dir_offset)
set => this.__user_data_dir := this.c_str_put(Value, , RimeTraits.user_data_dir_offset)
}
distribution_name {
get => this.c_str_get(, RimeTraits.distribution_name_offset)
set => this.__distribution_name := this.c_str_put(Value, , RimeTraits.distribution_name_offset)
}
distribution_code_name {
get => this.c_str_get(, RimeTraits.distribution_code_name_offset)
set => this.__distribution_code_name := this.c_str_put(Value, , RimeTraits.distribution_code_name_offset)
}
distribution_version {
get => this.c_str_get(, RimeTraits.distribution_version_offset)
set => this.__distribution_version := this.c_str_put(Value, , RimeTraits.distribution_version_offset)
}
app_name {
get => this.c_str_get(, RimeTraits.app_name_offset)
set => this.__app_name := this.c_str_put(Value, , RimeTraits.app_name_offset)
}
modules {
get => this.c_str_array_get(RimeTraits.modules_offset)
set => this.__modules := this.c_str_array_put(Value, , RimeTraits.modules_offset)
}
min_log_level {
get => this.num_get(RimeTraits.min_log_level_offset)
set => this.num_put(, Value, RimeTraits.min_log_level_offset)
}
log_dir {
get => this.c_str_get(, RimeTraits.log_dir_offset)
set => this.__log_dir := this.c_str_put(Value, , RimeTraits.log_dir_offset)
}
prebuilt_data_dir {
get => this.c_str_get(, RimeTraits.prebuilt_data_dir_offset)
set => this.__prebuilt_data_dir := this.c_str_put(Value, , RimeTraits.prebuilt_data_dir_offset)
}
staging_dir {
get => this.c_str_get(, RimeTraits.staging_dir_offset)
set => this.__staging_dir := this.c_str_put(Value, , RimeTraits.staging_dir_offset)
}
} ; RimeTraits
class RimeComposition extends RimeStruct {
__New(ptr := 0) {
super.__New(RimeComposition.struct_size, 0)
this.copy(ptr)
}
static length_offset := 0
static cursor_pos_offset := RimeComposition.length_offset + A_IntSize
static sel_start_offset := RimeComposition.cursor_pos_offset + A_IntSize
static sel_end_offset := RimeComposition.sel_start_offset + A_IntSize
static preedit_offset := RimeComposition.sel_end_offset + A_IntSize
static struct_size := RimeComposition.preedit_offset + A_PtrSize
length {
get => this.num_get(RimeComposition.length_offset)
}
cursor_pos {
get => this.num_get(RimeComposition.cursor_pos_offset)
}
sel_start {
get => this.num_get(RimeComposition.sel_start_offset)
}
sel_end {
get => this.num_get(RimeComposition.sel_end_offset)
}
preedit {
get => this.c_str_get(, RimeComposition.preedit_offset)
}
} ; RimeComposition
class RimeCandidate extends RimeStruct {
__New(ptr := 0) {
super.__New(RimeCandidate.struct_size, 0)
this.copy(ptr)
}
static text_offset := 0
static comment_offset := RimeCandidate.text_offset + A_PtrSize
static reserved_offset := RimeCandidate.comment_offset + A_PtrSize
static struct_size := RimeCandidate.reserved_offset + A_PtrSize
text {
get => this.c_str_get(, RimeCandidate.text_offset)
}
comment {
get => this.c_str_get(, RimeCandidate.comment_offset)
}
} ; RimeCandidate
class RimeMenu extends RimeStruct {
__New(ptr := 0) {
super.__New(RimeMenu.struct_size, 0)
this.copy(ptr)
}
static page_size_offset := 0
static page_no_offset := RimeMenu.page_size_offset + A_IntSize
static is_last_page_offset := RimeMenu.page_no_offset + A_IntSize
static highlighted_candidate_index_offset := RimeMenu.is_last_page_offset + A_IntSize
static num_candidates_offset := RimeMenu.highlighted_candidate_index_offset + A_IntSize
static candidates_offset := RimeMenu.num_candidates_offset + A_IntSize + A_IntPaddingSize
static select_keys_offset := RimeMenu.candidates_offset + A_PtrSize
static struct_size := RimeMenu.select_keys_offset + A_PtrSize
page_size {
get => this.num_get(RimeMenu.page_size_offset)
}
page_no {
get => this.num_get(RimeMenu.page_no_offset)
}
is_last_page {
get => this.num_get(RimeMenu.is_last_page_offset)
}
highlighted_candidate_index {
get => this.num_get(RimeMenu.highlighted_candidate_index_offset)
}
num_candidates {
get => this.num_get(RimeMenu.num_candidates_offset)
}
candidates {
get => this.struct_array_get(RimeMenu.candidates_offset, this.num_candidates, RimeCandidate)
}
select_keys {
get => this.c_str_get(, RimeMenu.select_keys_offset)
}
} ; RimeMenu
class RimeCommit extends RimeVersionedStruct {
__New(ptr := 0) {
super.__New(RimeCommit.struct_size, 0)
this.data_size := RimeCommit.struct_size - A_IntSize
this.copy(ptr)
}
static data_size_offset := 0
static text_offset := RimeCommit.data_size_offset + A_IntSize + A_IntPaddingSize
static struct_size := RimeCommit.text_offset + A_PtrSize
data_size {
set => this.num_put(, Value, RimeCommit.data_size_offset)
}
text {
get => this.c_str_get(, RimeCommit.text_offset)
}
} ; RimeCommit
class RimeContext extends RimeVersionedStruct {
__New(ptr := 0) {
super.__New(RimeContext.struct_size, 0)
this.data_size := RimeContext.struct_size - A_IntSize
this.copy(ptr)
}
static data_size_offset := 0
static composition_offset := RimeContext.data_size_offset + A_IntSize + A_IntPaddingSize
static menu_offset := RimeContext.composition_offset + RimeComposition.struct_size
static commit_text_preview_offset := RimeContext.menu_offset + RimeMenu.struct_size
static select_labels_offset := RimeContext.commit_text_preview_offset + A_PtrSize
static struct_size := RimeContext.select_labels_offset + A_PtrSize
data_size {
set => this.num_put(, Value, RimeContext.data_size_offset)
}
composition {
get => RimeComposition(this.Ptr + RimeContext.composition_offset)
}
menu {
get => RimeMenu(this.Ptr + RimeContext.menu_offset)
}
} ; RimeContext
class RimeStatus extends RimeVersionedStruct {
__New(ptr := 0) {
super.__New(RimeStatus.struct_size, 0)
this.data_size := RimeStatus.struct_size - A_IntSize
this.copy(ptr)
}
static data_size_offset := 0
static schema_id_offset := RimeStatus.data_size_offset + A_IntSize + A_IntPaddingSize
static schema_name_offset := RimeStatus.schema_id_offset + A_PtrSize
static is_disabled_offset := RimeStatus.schema_name_offset + A_PtrSize
static is_composing_offset := RimeStatus.is_disabled_offset + A_IntSize
static is_ascii_mode_offset := RimeStatus.is_composing_offset + A_IntSize
static is_full_shape_offset := RimeStatus.is_ascii_mode_offset + A_IntSize
static is_simplified_offset := RimeStatus.is_full_shape_offset + A_IntSize
static is_traditional_offset := RimeStatus.is_simplified_offset + A_IntSize
static is_ascii_punct_offset := RimeStatus.is_traditional_offset + A_IntSize
; NOTE: is there a tail padding? (which will affect this.data_size)
static struct_size := RimeStatus.is_ascii_punct_offset + A_IntSize + A_IntPaddingSize
data_size {
set => this.num_put(, Value, RimeStatus.data_size_offset)
}
schema_id {
get => this.c_str_get(, RimeStatus.schema_id_offset)
}
schema_name {
get => this.c_str_get(, RimeStatus.schema_name_offset)
}
is_disabled {
get => this.num_get(RimeStatus.is_disabled_offset)
}
is_composing {
get => this.num_get(RimeStatus.is_composing_offset)
}
is_ascii_mode {
get => this.num_get(RimeStatus.is_ascii_mode_offset)
}
is_full_shape {
get => this.num_get(RimeStatus.is_full_shape_offset)
}
is_simplified {
get => this.num_get(RimeStatus.is_simplified_offset)
}
is_traditional {
get => this.num_get(RimeStatus.is_traditional_offset)
}
is_ascii_punct {
get => this.num_get(RimeStatus.is_ascii_punct_offset)
}
} ; RimeStatus
class RimeCandidateListIterator extends RimeStruct {
__New() {
super.__New(RimeCandidateListIterator.struct_size, 0)
}
static ptr_offset := 0
static index_offset := RimeCandidateListIterator.ptr_offset + A_PtrSize
static candidate_offset := RimeCandidateListIterator.index_offset + A_IntSize + A_IntPaddingSize
static struct_size := RimeCandidateListIterator.candidate_offset + RimeCandidate.struct_size
index {
get => this.num_get(RimeCandidateListIterator.index_offset)
}
candidate {
get => RimeCandidate(this.Ptr + RimeCandidateListIterator.candidate_offset)
}
} ; RimeCandidateListIterator
class RimeConfig extends RimeStruct {
__New(ptr := 0) {
super.__New(RimeConfig.struct_size, 0)
this.copy(ptr)
}
static ptr_offset := 0
static struct_size := RimeConfig.ptr_offset + A_PtrSize
ptr {
get => this.num_get(RimeConfig.ptr_offset, "Ptr")
}
} ; RimeConfig
class RimeConfigIterator extends RimeStruct {
__New(ptr := 0) {
super.__New(RimeConfigIterator.struct_size, 0)
this.copy(ptr)
}
static list_offset := 0
static map_offset := RimeConfigIterator.list_offset + A_PtrSize
static index_offset := RimeConfigIterator.map_offset + A_PtrSize
static key_offset := RimeConfigIterator.index_offset + A_IntSize + A_IntPaddingSize
static path_offset := RimeConfigIterator.key_offset + A_PtrSize
static struct_size := RimeConfigIterator.path_offset + A_PtrSize
list {
get => this.num_get(RimeConfigIterator.list_offset, "Ptr")
}
map {
get => this.num_get(RimeConfigIterator.map_offset, "Ptr")
}
index {
get => this.num_get(RimeConfigIterator.index_offset)
}
key {
get => this.c_str_get(, RimeConfigIterator.key_offset)
}
path {
get => this.c_str_get(, RimeConfigIterator.path_offset)
}
}
class RimeSchemaListItem extends RimeStruct {
__New(ptr := 0) {
super.__New(RimeSchemaListItem.struct_size, 0)
this.copy(ptr)
}
static schema_id_offset := 0
static name_offset := RimeSchemaListItem.schema_id_offset + A_PtrSize
static reserved_offset := RimeSchemaListItem.name_offset + A_PtrSize
static struct_size := RimeSchemaListItem.reserved_offset + A_PtrSize
schema_id {
get => this.c_str_get(, RimeSchemaListItem.schema_id_offset)
}
name {
get => this.c_str_get(, RimeSchemaListItem.name_offset)
}
reserved {
get => this.num_get(RimeSchemaListItem.reserved_offset, "Ptr")
}
} ; RimeSchemaListItem
class RimeSchemaList extends RimeStruct {
__New() {
super.__New(RimeSchemaList.struct_size 0)
}
static size_offset := 0
static list_offset := RimeSchemaList.size_offset + A_PtrSize ; size_t size
static struct_size := RimeSchemaList.list_offset + A_PtrSize
size {
get => this.num_get(RimeSchemaList.size_offset, "UInt") ; returns size_t
}
list {
get => this.struct_array_get(RimeSchemaList.list_offset, this.size, RimeSchemaListItem)
}
} ; RimeSchemaList
/**
* Do not exist in 1.8.5 and before
*/
class RimeStringSlice extends RimeStruct {
__New(str := "", length := 0, ptr := 0) {
super.__New(RimeStringSlice.str_offset, 0)
this.str := str
this.length := length
if ptr
this.copy(ptr)
}
static str_offset := 0
static length_offset := RimeStringSlice.str_offset + A_PtrSize
static struct_size := RimeStringSlice.length_offset + A_PtrSize ; size_t length
str {
get => this.c_str_get(, RimeStringSlice.str_offset)
set => this.__str := this.c_str_put(Value, , RimeStringSlice.str_offset)
}
length {
get => this.num_get(RimeStringSlice.length_offset, "UInt") ; returns size_t
set => this.num_put("UInt", Value, RimeStringSlice.length_offset)
}
slice {
get => this.c_str_get(, RimeStringSlice.str_offset, this.length)
}
} ; RimeStringSlice
; Just a template struct
; Check out the actual example RimeLeversApi
; RimeCustomApi
class RimeModule extends RimeVersionedStruct {
__New(ptr := 0) {
super.__New(RimeModule.struct_size, 0)
this.data_size := RimeModule.struct_size - A_IntSize
this.copy(ptr)
}
static data_size_offset := 0
static module_name_offset := RimeModule.data_size_offset + A_IntSize + A_IntPaddingSize
static initialize_offset := RimeModule.module_name_offset + A_PtrSize
static finalize_offset := RimeModule.initialize_offset + A_PtrSize
static get_api_offset := RimeModule.finalize_offset + A_PtrSize
static struct_size := RimeModule.get_api_offset + A_PtrSize
data_size {
set => this.num_put(, Value, RimeModule.data_size_offset)
}
module_name {
get => this.c_str_get(, RimeModule.module_name_offset)
set => this.__module_name := this.c_str_put(Value, , RimeModule.module_name_offset)
}
initialize := (*) => DllCall(this.num_get(RimeModule.initialize_offset, "Ptr"), "CDecl")
finalize := (*) => DllCall(this.num_get(RimeModule.finalize_offset, "Ptr"), "CDecl")
get_api := (*) => DllCall(this.num_get(RimeModule.get_api_offset, "Ptr"), "CDecl Ptr")
} ; RimeModule
class RimeApi extends RimeApiStruct {
__New() {
if not RimeApi.rimeDll {
if librime_lib_dir := EnvGet("LIBRIME_LIB_DIR")
RimeApi.rimeDll := DllCall("LoadLibrary", "Str", librime_lib_dir . "\rime.dll", "Ptr")
if not RimeApi.rimeDll and weasel_root := RegRead("HKEY_LOCAL_MACHINE\Software\Rime\Weasel", "WeaselRoot", "")
RimeApi.rimeDll := DllCall("LoadLibrary", "Str", weasel_root . "\rime.dll", "Ptr")
if not RimeApi.rimeDll
throw RimeError("The library rime.dll not found.")
}
super.__New(RimeApi.struct_size, 0)
if not ptr := DllCall("rime\rime_get_api", "CDecl Ptr")
throw RimeError("Failed to get rime API.")
this.copy(ptr)
if VerCompare(this.get_version(), RimeApi.min_version) < 0
throw RimeError(Format("Current librime version {} is smaller than required {}.", this.get_version(), RimeApi.min_version))
}
static rimeDll := DllCall("LoadLibrary", "Str", "rime.dll", "Ptr")
static min_version := "1.8.5"
static data_size_offset := 0
static setup_offset := RimeApi.data_size_offset + A_IntSize + A_IntPaddingSize
static set_notification_handler_offset := RimeApi.setup_offset + A_PtrSize
static initialize_offset := RimeApi.set_notification_handler_offset + A_PtrSize
static finalize_offset := RimeApi.initialize_offset + A_PtrSize
static start_maintenance_offset := RimeApi.finalize_offset + A_PtrSize
static is_maintenance_mode_offset := RimeApi.start_maintenance_offset + A_PtrSize
static join_maintenance_thread_offset := RimeApi.is_maintenance_mode_offset + A_PtrSize
static deployer_initialize_offset := RimeApi.join_maintenance_thread_offset + A_PtrSize
static prebuild_offset := RimeApi.deployer_initialize_offset + A_PtrSize
static deploy_offset := RimeApi.prebuild_offset + A_PtrSize
static deploy_schema_offset := RimeApi.deploy_offset + A_PtrSize
static deploy_config_file_offset := RimeApi.deploy_schema_offset + A_PtrSize
static sync_user_data_offset := RimeApi.deploy_config_file_offset + A_PtrSize
static create_session_offset := RimeApi.sync_user_data_offset + A_PtrSize
static find_session_offset := RimeApi.create_session_offset + A_PtrSize
static destroy_session_offset := RimeApi.find_session_offset + A_PtrSize
static cleanup_stale_sessions_offset := RimeApi.destroy_session_offset + A_PtrSize
static cleanup_all_sessions_offset := RimeApi.cleanup_stale_sessions_offset + A_PtrSize
static process_key_offset := RimeApi.cleanup_all_sessions_offset + A_PtrSize
static commit_composition_offset := RimeApi.process_key_offset + A_PtrSize
static clear_composition_offset := RimeApi.commit_composition_offset + A_PtrSize
static get_commit_offset := RimeApi.clear_composition_offset + A_PtrSize
static free_commit_offset := RimeApi.get_commit_offset + A_PtrSize
static get_context_offset := RimeApi.free_commit_offset + A_PtrSize
static free_context_offset := RimeApi.get_context_offset + A_PtrSize
static get_status_offset := RimeApi.free_context_offset + A_PtrSize
static free_status_offset := RimeApi.get_status_offset + A_PtrSize
static set_option_offset := RimeApi.free_status_offset + A_PtrSize
static get_option_offset := RimeApi.set_option_offset + A_PtrSize
static set_property_offset := RimeApi.get_option_offset + A_PtrSize
static get_property_offset := RimeApi.set_property_offset + A_PtrSize
static get_schema_list_offset := RimeApi.get_property_offset + A_PtrSize
static free_schema_list_offset := RimeApi.get_schema_list_offset + A_PtrSize
static get_current_schema_offset := RimeApi.free_schema_list_offset + A_PtrSize
static select_schema_offset := RimeApi.get_current_schema_offset + A_PtrSize
static schema_open_offset := RimeApi.select_schema_offset + A_PtrSize
static config_open_offset := RimeApi.schema_open_offset + A_PtrSize
static config_close_offset := RimeApi.config_open_offset + A_PtrSize
static config_get_bool_offset := RimeApi.config_close_offset + A_PtrSize
static config_get_int_offset := RimeApi.config_get_bool_offset + A_PtrSize
static config_get_double_offset := RimeApi.config_get_int_offset + A_PtrSize
static config_get_string_offset := RimeApi.config_get_double_offset + A_PtrSize
static config_get_cstring_offset := RimeApi.config_get_string_offset + A_PtrSize
static config_update_signature_offset := RimeApi.config_get_cstring_offset + A_PtrSize
static config_begin_map_offset := RimeApi.config_update_signature_offset + A_PtrSize
static config_next_offset := RimeApi.config_begin_map_offset + A_PtrSize
static config_end_offset := RimeApi.config_next_offset + A_PtrSize
static simulate_key_sequence_offset := RimeApi.config_end_offset + A_PtrSize
static register_module_offset := RimeApi.simulate_key_sequence_offset + A_PtrSize
static find_module_offset := RimeApi.register_module_offset + A_PtrSize
static run_task_offset := RimeApi.find_module_offset + A_PtrSize
static get_shared_data_dir_offset := RimeApi.run_task_offset + A_PtrSize
static get_user_data_dir_offset := RimeApi.get_shared_data_dir_offset + A_PtrSize
static get_sync_dir_offset := RimeApi.get_user_data_dir_offset + A_PtrSize
static get_user_id_offset := RimeApi.get_sync_dir_offset + A_PtrSize
static get_user_data_sync_dir_offset := RimeApi.get_user_id_offset + A_PtrSize
static config_init_offset := RimeApi.get_user_data_sync_dir_offset + A_PtrSize
static config_load_string_offset := RimeApi.config_init_offset + A_PtrSize
static config_set_bool_offset := RimeApi.config_load_string_offset + A_PtrSize
static config_set_int_offset := RimeApi.config_set_bool_offset + A_PtrSize
static config_set_double_offset := RimeApi.config_set_int_offset + A_PtrSize
static config_set_string_offset := RimeApi.config_set_double_offset + A_PtrSize
static config_get_item_offset := RimeApi.config_set_string_offset + A_PtrSize
static config_set_item_offset := RimeApi.config_get_item_offset + A_PtrSize
static config_clear_offset := RimeApi.config_set_item_offset + A_PtrSize
static config_create_list_offset := RimeApi.config_clear_offset + A_PtrSize
static config_create_map_offset := RimeApi.config_create_list_offset + A_PtrSize
static config_list_size_offset := RimeApi.config_create_map_offset + A_PtrSize
static config_begin_list_offset := RimeApi.config_list_size_offset + A_PtrSize
static get_input_offset := RimeApi.config_begin_list_offset + A_PtrSize
static get_caret_pos_offset := RimeApi.get_input_offset + A_PtrSize
static select_candidate_offset := RimeApi.get_caret_pos_offset + A_PtrSize
static get_version_offset := RimeApi.select_candidate_offset + A_PtrSize
static set_caret_pos_offset := RimeApi.get_version_offset + A_PtrSize
static select_candidate_on_current_page_offset := RimeApi.set_caret_pos_offset + A_PtrSize
static candidate_list_begin_offset := RimeApi.select_candidate_on_current_page_offset + A_PtrSize
static candidate_list_next_offset := RimeApi.candidate_list_begin_offset + A_PtrSize
static candidate_list_end_offset := RimeApi.candidate_list_next_offset + A_PtrSize
static user_config_open_offset := RimeApi.candidate_list_end_offset + A_PtrSize
static candidate_list_from_index_offset := RimeApi.user_config_open_offset + A_PtrSize
static get_prebuilt_data_dir_offset := RimeApi.candidate_list_from_index_offset + A_PtrSize
static get_staging_dir_offset := RimeApi.get_prebuilt_data_dir_offset + A_PtrSize
static commit_proto_offset := RimeApi.get_staging_dir_offset + A_PtrSize
static context_proto_offset := RimeApi.commit_proto_offset + A_PtrSize
static status_proto_offset := RimeApi.context_proto_offset + A_PtrSize
static get_state_label_offset := RimeApi.status_proto_offset + A_PtrSize
static delete_candidate_offset := RimeApi.get_state_label_offset + A_PtrSize
static delete_candidate_on_current_page_offset := RimeApi.delete_candidate_offset + A_PtrSize
; librime 1.9.0
static get_state_label_abbreviated_offset := RimeApi.delete_candidate_on_current_page_offset + A_PtrSize
; librime 1.10.0
static set_input_offset := RimeApi.get_state_label_abbreviated_offset + A_PtrSize
static get_shared_data_dir_s_offset := RimeApi.set_input_offset + A_PtrSize
static get_user_data_dir_s_offset := RimeApi.get_shared_data_dir_s_offset + A_PtrSize
static get_prebuilt_data_dir_s_offset := RimeApi.get_user_data_dir_s_offset + A_PtrSize
static get_staging_dir_s_offset := RimeApi.get_prebuilt_data_dir_s_offset + A_PtrSize
static get_sync_dir_s_offset := RimeApi.get_staging_dir_s_offset + A_PtrSize
static highlight_candidate_offset := RimeApi.get_sync_dir_s_offset + A_PtrSize
static highlight_candidate_on_current_page_offset := RimeApi.highlight_candidate_offset + A_PtrSize
static change_page_offset := RimeApi.highlight_candidate_on_current_page_offset + A_PtrSize
static struct_size := RimeApi.change_page_offset + A_PtrSize
; (RimeTraits) => void
setup(traits) {
DllCall(this.fp(RimeApi.setup_offset), "Ptr", traits, "CDecl")
}
/**
* Set the notification handler
*
* @param handler a function that takes four parameters (type in ahk / type in c):
*
* `Ptr` / `void *`
*
* `UInt` / `RimeSessionId`
*
* `Ptr` / `const char *`
*
* `Ptr` / `const char *`
* @param context_object `Ptr` in ahk, `void *` in librime
*/
set_notification_handler(handler, context_object) {
DllCall(this.fp(RimeApi.set_notification_handler_offset), "Ptr", CallbackCreate(handler, "C", 4), "Ptr", context_object, "CDecl")
}
; (RimeTraits) => void
initialize(traits) {
DllCall(this.fp(RimeApi.initialize_offset), "Ptr", traits, "CDecl")
}
; () => void
finalize() {
DllCall(this.fp(RimeApi.finalize_offset), "CDecl")
DllCall("FreeLibrary", "Ptr", RimeApi.rimeDll)
RimeApi.rimeDll := 0
}
; (Int) => Int
start_maintenance(full_check) {
return DllCall(this.fp(RimeApi.start_maintenance_offset), "Int", full_check, "CDecl Int")
}
; () => Int
is_maintenance_mode() {
return DllCall(this.fp(RimeApi.is_maintenance_mode_offset), "CDecl Int")
}
; () => void
join_maintenance_thread() {
DllCall(this.fp(RimeApi.join_maintenance_thread_offset), "CDecl")
}
; (RimeTraits) => void
deployer_initialize(traits) {
DllCall(this.fp(RimeApi.deployer_initialize_offset), "Ptr", traits, "CDecl")
}
; () => Int
prebuild() {
return DllCall(this.fp(RimeApi.prebuild_offset), "CDecl Int")
}
; () => Int
deploy() {
return DllCall(this.fp(RimeApi.deploy_offset), "CDecl Int")
}
; (Str) => Int
deploy_schema(schema_file) {
return DllCall(this.fp(RimeApi.deploy_schema_offset), "Ptr", RimeString(schema_file), "CDecl Int")
}
; (Str, Str) => Int
deploy_config_file(file_name, version_key) {
return DllCall(this.fp(RimeApi.deploy_config_file_offset), "Ptr", RimeString(file_name), "Ptr", RimeString(version_key), "CDecl Int")
}
; () => Int
sync_user_data() {
return DllCall(this.fp(RimeApi.sync_user_data_offset), "CDecl Int")
}
; () => UInt
create_session() {
return DllCall(this.fp(RimeApi.create_session_offset), "CDecl UInt")
}
; (UInt) => Int
find_session(session_id) {
return DllCall(this.fp(RimeApi.find_session_offset), "UInt", session_id, "CDecl Int")
}
; (UInt) => Int
destroy_session(session_id) {
return DllCall(this.fp(RimeApi.destroy_session_offset), "UInt", session_id, "CDecl Int")
}
; () => void
cleanup_stale_sessions() {
DllCall(this.fp(RimeApi.cleanup_stale_sessions_offset), "CDecl")
}
; () => void
cleanup_all_sessions() {
DllCall(this.fp(RimeApi.cleanup_all_sessions_offset), "CDecl")
}
; (UInt, Int, Int) => Int
process_key(session_id, keycode, mask) {
return DllCall(this.fp(RimeApi.process_key_offset), "UInt", session_id, "Int", keycode, "Int", mask, "CDecl Int")
}
; (UInt) => Int
commit_composition(session_id) {
return DllCall(this.fp(RimeApi.commit_composition_offset), "UInt", session_id, "CDecl Int")
}
; (UInt) => void
clear_composition(session_id) {
DllCall(this.fp(RimeApi.clear_composition_offset), "UInt", session_id, "CDecl")
}
; (UInt) => RimeCommit or 0
get_commit(session_id) {
commit := RimeCommit()
res := DllCall(this.fp(RimeApi.get_commit_offset), "UInt", session_id, "Ptr", commit, "CDecl Int")
return res ? commit : 0
}
; (RimeCommit) => Int
free_commit(commit) {
return DllCall(this.fp(RimeApi.free_commit_offset), "Ptr", commit, "CDecl Int")
}
; (UInt) => RimeContext or 0
get_context(session_id) {
context := RimeContext()
res := DllCall(this.fp(RimeApi.get_context_offset), "UInt", session_id, "Ptr", context, "CDecl Int")
return res ? context : 0
}
; (RimeContext) => Int
free_context(context) {
return DllCall(this.fp(RimeApi.free_context_offset), "Ptr", context, "CDecl Int")
}
; (UInt) => RimeStatus or 0
get_status(session_id) {
status := RimeStatus()
res := DllCall(this.fp(RimeApi.get_status_offset), "UInt", session_id, "Ptr", status, "CDecl Int")
return res ? status : 0
}
; (RimeStatus) => Int
free_status(status) {
return DllCall(this.fp(RimeApi.free_status_offset), "Ptr", status, "CDecl Int")
}
; (UInt, Str, Int) => void
set_option(session_id, option, value) {
DllCall(this.fp(RimeApi.set_option_offset), "UInt", session_id, "Ptr", RimeString(option), "Int", value, "CDecl")
}
; (UInt, Str) => Int
get_option(session_id, option) {
return DllCall(this.fp(RimeApi.get_option_offset), "UInt", session_id, "Ptr", RimeString(option), "CDecl Int")
}
; (UInt, Str, Str) => void
set_property(session_id, prop, value) {
DllCall(this.fp(RimeApi.set_property_offset), "UInt", session_id, "Ptr", RimeString(prop), "Ptr", RimeString(value), "CDecl")
}
; (UInt, Str, UInt) => Str
get_property(session_id, prop, buffer_size := Rime_BufferSize) {
buf := Buffer(buffer_size)
res := DllCall(this.fp(RimeApi.get_property_offset), "UInt", session_id, "Ptr", RimeString(prop), "Ptr", buf, "UInt", buffer_size, "CDecl Int")
return res ? StrGet(buf, "UTF-8") : ""
}
; () => RimeSchemaList or 0
get_schema_list() {
list := RimeSchemaList()
res := DllCall(this.fp(RimeApi.get_schema_list_offset), "Ptr", list, "CDecl Int")
return res ? list : 0
}
; (RimeSchemaList) => void
free_schema_list(schema_list) {
DllCall(this.fp(RimeApi.free_schema_list_offset), "Ptr", schema_list, "CDecl")
}
; (UInt, UInt) => Str
get_current_schema(session_id, buffer_size := Rime_BufferSize) {
buf := Buffer(buffer_size)
res := DllCall(this.fp(RimeApi.get_current_schema_offset), "UInt", session_id, "Ptr", buf, "UInt", buffer_size, "CDecl Int")
return res ? StrGet(buf, "UTF-8") : ""
}
; (UInt, Str) => Int
select_schema(session_id, schema_id) {
return DllCall(this.fp(RimeApi.select_schema_offset), "UInt", session_id, "Ptr", RimeString(schema_id), "CDecl Int")
}
/**
* Open a schema
*
* @param schema_id type of `Str`
* @returns type of `RimeConfig` on success, `0` on failure
*/
schema_open(schema_id) {
config := RimeConfig()
res := DllCall(this.fp(RimeApi.schema_open_offset), "Ptr", RimeString(schema_id), "Ptr", config, "CDecl Int")
return res ? config : 0
}
/**
* Open a config
*
* @param config_id type of `Str`
* @returns type of `RimeConfig` on success, `0` on failure
*/
config_open(config_id) {
config := RimeConfig()
res := DllCall(this.fp(RimeApi.config_open_offset), "Ptr", RimeString(config_id), "Ptr", config, "CDecl Int")
return res ? config : 0
}
; (RimeConfig) => void
/**
* Close a config
*
* @param config type of `RimeConfig`
* @returns `True` on success, `False` on failure
*/
config_close(config) {
return DllCall(this.fp(RimeApi.config_close_offset), "Ptr", config, "CDecl Int")
}
/**
* Test the existence of a boolean value in the config
*
* @param config type of `RimeConfig`
* @param key type of `Str`
* @returns `True` if exist, `False` if not
*/
config_exist_bool(config, key) {
buf := Buffer(A_IntSize)
return DllCall(this.fp(RimeApi.config_get_bool_offset), "Ptr", config, "Ptr", RimeString(key), "Ptr", buf, "CDecl Int")
}
/**
* Get a boolean value from the config
*
* Suggest to use `config_exist_bool` before this function, or use `config_test_get_bool` instead
*
* @param config type of `RimeConfig`
* @param key type of `Str`
* @returns the boolean value in the config if exists, or `False` if not exists
*/
config_get_bool(config, key) {
buf := Buffer(A_IntSize)
res := DllCall(this.fp(RimeApi.config_get_bool_offset), "Ptr", config, "Ptr", RimeString(key), "Ptr", buf, "CDecl Int")
return res ? NumGet(buf, "Int") : 0
}
/**
* Test and get a boolean value from the config
*
* @param config type of `RimeConfig`
* @param key type of `Str`
* @param value pass by reference, type of `Int`
* @returns `True` on success, `False` on failure
*/
config_test_get_bool(config, key, &value) {
buf := Buffer(A_IntSize)
if res := DllCall(this.fp(RimeApi.config_get_bool_offset), "Ptr", config, "Ptr", RimeString(key), "Ptr", buf, "CDecl Int")
value := NumGet(buf, "Int")