-
Notifications
You must be signed in to change notification settings - Fork 1
/
qk.c
3035 lines (2878 loc) · 89.4 KB
/
qk.c
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
#include "qk.h"
/* system includes */
#include <assert.h> /* assert */
#include <stdlib.h> /* calloc, free */
#include <string.h> /* memcpy, memset */
#include <limits.h> /* INT_MAX */
#include <stdio.h> /* fprintf, fputc */
#include <emmintrin.h> /* _mm_pause */
/* constants */
#define VERSION 1
#define UTF_SIZE 4
#define UTF_INVALID 0xFFFD
/* utf-8 */
static const unsigned char utf_byte[UTF_SIZE+1] = {0x80,0,0xC0,0xE0,0xF0};
static const unsigned char utf_mask[UTF_SIZE+1] = {0xC0,0x80,0xE0,0xF0,0xF8};
static const unsigned long utf_min[UTF_SIZE+1] = {0,0,0x80,0x800,0x10000};
static const unsigned long utf_max[UTF_SIZE+1] = {0x10FFFF,0x7F,0x7FF,0xFFFF,0x10FFFF};
/* memory */
#define qalloc(a,sz) (a)->alloc((a)->usr, sz, __FILE__, __LINE__)
#define qdealloc(a,ptr) (a)->dealloc((a)->usr, ptr, __FILE__, __LINE__)
static void *dalloc(void *usr, int s, const char *file, int line){return malloc((size_t)s);}
static void dfree(void *usr, void *d, const char *file, int line){free(d);}
static const struct allocator default_allocator = {0,dalloc,dfree};
/* repository member object alignment */
static const int uint_align = alignof(unsigned);
static const int uiid_align = alignof(uiid);
static const int int_align = alignof(int);
static const int ptr_align = alignof(void*);
static const int box_align = alignof(struct box);
static const int param_align = alignof(union param);
static const int repo_align = alignof(struct repository);
/* repository member object size */
static const int int_size = szof(int);
static const int ptr_size = szof(void*);
static const int uint_size = szof(unsigned);
static const int uiid_size = szof(uiid);
static const int box_size = szof(struct box);
static const int param_size = szof(union param);
static const int repo_size = szof(struct repository);
/* root tables */
static const struct element g_root_elements[] = {
/* type id, parent, wid, depth,flags */
{WIDGET_ROOT, 0, 0, WIDGET_ROOT, 0,0},
{WIDGET_OVERLAY, 1, 0, WIDGET_OVERLAY, 1,0},
{WIDGET_POPUP, 2, 1, WIDGET_POPUP, 2,0},
{WIDGET_CONTEXTUAL, 3, 2, WIDGET_CONTEXTUAL, 3,0},
{WIDGET_UNBLOCKING, 4, 3, WIDGET_UNBLOCKING, 4,0},
{WIDGET_BLOCKING, 5, 4, WIDGET_BLOCKING, 5,0},
{WIDGET_UI, 6, 5, WIDGET_UI, 6,0},
};
static union param g_root_params[1];
static const unsigned char g_root_data[1];
static const uiid g_root_table_keys[] = {0,1,2,3,4,5,6,0};
static const int g_root_table_vals[] = {0,1,2,3,4,5,6,0};
static const struct component g_root = {
VERSION, 0, 0, 7,
g_root_elements, cntof(g_root_elements),
g_root_table_vals, g_root_table_keys, 8,
g_root_data, 0, g_root_params, 0,
0, 0, 0, 0
};
/* opcodes */
#define OPCODES(OP)\
OP(BUF_BEGIN, 1, MIDFMT)\
OP(BUF_END, 1, MIDFMT)\
OP(ULNK, 2, MIDFMT" "IDFMT)\
OP(DLNK, 1, MIDFMT)\
OP(CONCT, 2, MIDFMT" %d")\
OP(WIDGET_BEGIN, 2, "%d %d")\
OP(WIDGET_END, 0, "") \
OP(BOX_PUSH, 2, IDFMT" "IDFMT)\
OP(BOX_POP, 0, "")\
OP(PROPERTY_SET, 1, "%u")\
OP(PROPERTY_CLR, 1, "%u")\
OP(PUSH_FLOAT, 1, "%f")\
OP(PUSH_INT, 1, "%d")\
OP(PUSH_UINT, 1, "%u")\
OP(PUSH_ID, 1, IDFMT)\
OP(PUSH_MID, 1, MIDFMT)\
OP(PUSH_STR, 1, "%s")\
OP(NEXT_BUF, 1, "%p")\
OP(EOF, 0, "")
enum opcodes {
#define OP(a,b,c) OP_ ## a,
OPCODES(OP)
#undef OP
OPCNT
};
static const struct opdef {
enum opcodes type;
int argc;
const char *name;
const char *fmt;
const char *str;
} opdefs[] = {
#define OP(a,b,c) {OP_ ## a, b, #a, c, #a " " c},
OPCODES(OP)
#undef OP
{OPCNT,0,0}
};
/* ---------------------------------------------------------------------------
* Platform
* --------------------------------------------------------------------------- */
intern unsigned
cas(volatile unsigned *dst, unsigned swap, unsigned cmp)
{
#if defined(_WIN32) && !(defined(__MINGW32__) || defined(__MINGW64__))
#pragma intrinsic(_InterlockedCompareExchange);
return _InterlockedCompareExchange((volatile long*)dst, swap, cmp);
#else
return __sync_val_compare_and_swap(dst, cmp, swap);
#endif
}
intern void
spinlock_begin(volatile unsigned *lock)
{
int i, mask = 1;
static const int max = 64;
while (cas(lock, 1, 0) != 0) {
for (i = mask; i; --i)
_mm_pause();
mask = mask < max ? mask << 1: max;
}
}
intern void
spinlock_end(volatile unsigned *slock)
{
_mm_sfence();
*slock = 0;
}
/* ---------------------------------------------------------------------------
* Math
* --------------------------------------------------------------------------- */
api int
npow2(int n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return ++n;
}
api int
floori(float x)
{
x = cast(float,(cast(int,x) - ((x < 0.0f) ? 1 : 0)));
return cast(int,x);
}
api int
ceili(float x)
{
if (x < 0) {
int t = cast(int,x);
float r = x - cast(float,t);
return (r > 0.0f) ? (t+1): t;
} else {
int i = cast(int,x);
return (x > i) ? (i+1): i;
}
}
api float
roundf(float x)
{
int e = 0;
float y = 0;
static const float toint = 1.0f/(1.1920928955078125e-07F);
union {float f; unsigned long i;} u;
u.f = x;
e = (u.i >> 23) & 0xff;
if (e >= 0x7f+23) return x;
if (u.i >> 31) x = -x;
if (e < 0x7f-1)
return 0*u.f;
y = x + toint - toint - x;
if (y > 0.5f)
y = y + x - 1;
else if (y <= -0.5f)
y = y + x + 1;
else y = y + x;
if (u.i >> 31)
y = -y;
return y;
}
api int
roundi(float x)
{
return cast(int, roundf(x));
}
api int
strn(const char *s)
{
const char *a = s;
const unsigned *w;
if (!s) return 0;
#define UONES (((unsigned)-1)/UCHAR_MAX)
#define UHIGHS (UONES * (UCHAR_MAX/2+1))
#define UHASZERO(x) ((x)-UONES & ~(x) & UHIGHS)
for (;ucast(s)&3; ++s) if (!*s) return (int)(s-a);
for (w = (const void*)s; !UHASZERO(*w); ++w);
for (s = (const void*)w; *s; ++s);
return (int)(s-a);
#undef UONES
#undef UHIGHS
#undef UHASZERO
}
/* ---------------------------------------------------------------------------
* Overflow
* --------------------------------------------------------------------------- */
intern int
size_add_valid(int a, int b)
{
if (a < 0 || b < 0) return 0;
return a <= (INT_MAX-b);
}
intern int
size_add2_valid(int a, int b, int c)
{
return size_add_valid(a, b) && size_add_valid(a+b, c);
}
intern int
size_mul_valid(int a, int b)
{
if (a < 0 || b < 0) return 0;
if (b == 0) return 1;
return a <= (INT_MAX/b);
}
intern int
size_add_mul2_valid(int mula, int a, int mulb, int b)
{
/* mula * a + mulb * b */
return size_mul_valid(mula, a) && size_mul_valid(mulb, b) && size_add_valid(mula*a,mulb*b);
}
intern int
size_mul_add2_valid(int mul, int a, int b, int c)
{
/* mul*a + b + c */
return size_mul_valid(a,mul) && size_add2_valid(a*mul,b,c);
}
intern int
size_mul2_add_valid(int mula, int a, int mulb, int b, int c)
{
/* mula*a + mulb*b + c */
return size_add_mul2_valid(mula,a,mulb,b) && size_add_valid(mula*a+mulb*b,c);
}
intern int
size_mul2_add2_valid(int mula, int a, int mulb, int b, int c, int d)
{
/* mula*a + mulb*b + c + d */
return size_mul2_add_valid(mula,a,mulb,b,c) && size_add_valid(mula*a+mulb*b+c,d);
}
/* ---------------------------------------------------------------------------
* UTF-8
* --------------------------------------------------------------------------- */
intern int
utf_validate(unsigned long *u, int i)
{
assert(u);
if (!u) return 0;
if (!between(*u, utf_min[i], utf_max[i]) ||
between(*u, 0xD800, 0xDFFF))
*u = UTF_INVALID;
for (i = 1; *u > utf_max[i]; ++i);
return i;
}
intern unsigned long
utf_decode_byte(char c, int *i)
{
assert(i);
if (!i) return 0;
for(*i = 0; *i < cntof(utf_mask); ++(*i)) {
if (((unsigned char)c & utf_mask[*i]) == utf_byte[*i])
return (unsigned char)(c & ~utf_mask[*i]);
} return 0;
}
api int
utf_decode(unsigned long *u, const char *s, int slen)
{
int i,j, len, type = 0;
unsigned long udecoded;
assert(s); assert(u);
if (!s || !u || !slen)
return 0;
*u = UTF_INVALID;
udecoded = utf_decode_byte(s[0], &len);
if (!between(len, 1, UTF_SIZE))
return 1;
for (i = 1, j = 1; i < slen && j < len; ++i, ++j) {
udecoded = (udecoded << 6) | utf_decode_byte(s[i], &type);
if (type != 0) return j;
} if (j < len) return 0;
*u = udecoded;
utf_validate(u, len);
return len;
}
intern char
utf_encode_byte(unsigned long u, int i)
{
return (char)((utf_byte[i]) | ((unsigned char)u & ~utf_mask[i]));
}
api int
utf_encode(char *s, int cap, unsigned long u)
{
int n, i;
n = utf_validate(&u, 0);
if (cap < n || !n || n > UTF_SIZE)
return 0;
for (i = n - 1; i != 0; --i) {
s[i] = utf_encode_byte(u, 0);
u >>= 6;
} s[0] = utf_encode_byte(u, n);
return n;
}
api int
utf_len(const char *s, int n)
{
int result = 0;
int rune_len = 0;
unsigned long rune = 0;
if (!s) return 0;
while ((rune_len = utf_decode(&rune, s, n))) {
n = max(0, n-rune_len);
s += rune_len;
result++;
} return result;
}
api const char*
utf_at(unsigned long *rune, int *rune_len,
const char *s, int len, int idx)
{
int runes = 0;
assert(s);
assert(rune);
assert(rune_len);
if (!s || !rune || !rune_len) return 0;
while ((*rune_len = utf_decode(rune, s, len))) {
if (runes++ == idx) return s;
len = max(0, len - *rune_len);
s += *rune_len;
} return 0;
}
/* ---------------------------------------------------------------------------
* List
* --------------------------------------------------------------------------- */
api void
list_init(struct list_hook *list)
{
list->next = list->prev = list;
}
intern void
list__add(struct list_hook *n,
struct list_hook *prev, struct list_hook *next)
{
next->prev = n;
n->next = next;
n->prev = prev;
prev->next = n;
}
api void
list_add_head(struct list_hook *list, struct list_hook *n)
{
list__add(n, list, list->next);
}
api void
list_add_tail(struct list_hook *list, struct list_hook *n)
{
list__add(n, list->prev, list);
}
intern void
list__del(struct list_hook *prev, struct list_hook *next)
{
next->prev = prev;
prev->next = next;
}
api void
list_del(struct list_hook *entry)
{
list__del(entry->prev, entry->next);
entry->next = entry;
entry->prev = entry;
}
api void
list_move_head(struct list_hook *list, struct list_hook *entry)
{
list_del(entry);
list_add_head(list, entry);
}
api void
list_move_tail(struct list_hook *list, struct list_hook *entry)
{
list_del(entry);
list_add_tail(list, entry);
}
intern void
list__splice(const struct list_hook *list,
struct list_hook *prev, struct list_hook *next)
{
struct list_hook *first = list->next;
struct list_hook *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
intern void
list_splice_head(struct list_hook *dst,
struct list_hook *list)
{
if (!list_empty(list)) {
list__splice(list, dst, dst->next);
list_init(list);
}
}
intern void
list_splice_tail(struct list_hook *dst,
struct list_hook *list)
{
if (!list_empty(list)) {
list__splice(list, dst->prev, dst);
list_init(list);
}
}
/* ---------------------------------------------------------------------------
* Block-Alloator
* --------------------------------------------------------------------------- */
intern void
block_alloc_init(struct block_allocator *a)
{
list_init(&a->blks);
list_init(&a->freelist);
}
intern struct memory_block*
block_alloc(struct block_allocator *a, int blksz)
{
struct memory_block *blk = 0;
assert(a);
assert(a->mem);
assert(a->mem->alloc);
spinlock_begin(&a->lock);
blksz = max(blksz, DEFAULT_MEMORY_BLOCK_SIZE);
if (blksz == DEFAULT_MEMORY_BLOCK_SIZE && !list_empty(&a->freelist)) {
/* allocate from freelist */
blk = list_entry(a->freelist.next, struct memory_block, hook);
list_del(&blk->hook);
} else blk = qalloc(a->mem, blksz);
assert(blk);
/* setup block */
zero(blk, szof(*blk));
blk->size = blksz - szof(*blk);
blk->base = cast(unsigned char*, (blk+1));
/* add block into list */
a->blkcnt++;
list_init(&blk->hook);
list_add_head(&a->blks, &blk->hook);
spinlock_end(&a->lock);
return blk;
}
intern void
block_dealloc(struct block_allocator *a, struct memory_block *blk)
{
assert(a && blk);
if (!a || !blk) return;
list_del(&blk->hook);
if ((blk->size + szof(*blk)) == DEFAULT_MEMORY_BLOCK_SIZE)
list_add_head(&a->freelist, &blk->hook);
else qdealloc(a->mem, blk);
a->blkcnt--;
}
intern void
free_blocks(struct block_allocator *a)
{
struct list_hook *i, *n = 0;
assert(a);
assert(a->mem);
assert(a->mem->alloc);
list_foreach_s(i, n, &a->freelist) {
struct memory_block *blk = 0;
blk = list_entry(i, struct memory_block, hook);
list_del(&blk->hook);
qdealloc(a->mem, blk);
}
list_foreach_s(i, n, &a->blks) {
struct memory_block *blk = 0;
blk = list_entry(i, struct memory_block, hook);
list_del(&blk->hook);
qdealloc(a->mem, blk);
} a->blkcnt = 0;
}
/* ---------------------------------------------------------------------------
* Arena
* --------------------------------------------------------------------------- */
intern void
arena_grow(struct memory_arena *a, int minsz)
{
/* allocate new memory block */
int blksz = max(minsz, DEFAULT_MEMORY_BLOCK_SIZE);
struct memory_block *blk = block_alloc(a->mem, blksz);
assert(blk);
/* link memory block into list */
blk->prev = a->blk;
a->blk = blk;
a->blkcnt++;
}
api void*
arena_push(struct memory_arena *a, int cnt, int size, int align)
{
int valid = 0;
assert(a);
if (!a) return 0;
/* validate enough space */
valid = a->blk && size_mul_add2_valid(size, cnt, a->blk->used, align);
if (!valid || ((cnt*size) + a->blk->used + align) > a->blk->size) {
if (!size_mul_add2_valid(size, cnt, szof(struct memory_block), align))
return 0;
arena_grow(a, cnt*size + szof(struct memory_block) + align);
}
/* allocate memory from block */
align = max(align,1);
{struct memory_block *blk = a->blk;
unsigned char *raw = blk->base + blk->used;
unsigned char *res = align(raw, align);
blk->used += (cnt*size) + (res-raw);
zero(res, (cnt*size));
return res;}
}
intern void
arena_free_last_blk(struct memory_arena *a)
{
struct memory_block *blk = a->blk;
a->blk = blk->prev;
block_dealloc(a->mem, blk);
a->blkcnt--;
}
intern void
arena_clear(struct memory_arena *a)
{
assert(a);
if (!a) return;
while (a->blk)
arena_free_last_blk(a);
}
intern struct temp_memory
temp_memory_begin(struct memory_arena *a)
{
struct temp_memory res;
assert(a);
res.used = a->blk ? a->blk->used: 0;
res.blk = a->blk;
res.arena = a;
a->tmpcnt++;
return res;
}
intern void
temp_memory_end(struct temp_memory tmp)
{
struct memory_arena *a = tmp.arena;
assert(a);
while (a->blk != tmp.blk)
arena_free_last_blk(a);
if (a->blk) a->blk->used = tmp.used;
a->tmpcnt--;
}
/* ---------------------------------------------------------------------------
* Hash-Table
* --------------------------------------------------------------------------- */
intern void
insert(struct table *t, uiid key, int val)
{
uiid n = cast(uiid, t->cnt);
uiid i = key & (n-1), b = i;
do {if (t->keys[i]) continue;
t->keys[i] = key;
t->vals[i] = val; return;
} while ((i = ((i+1) & (n-1))) != b);
}
intern int
lookup(struct table *t, uiid key)
{
uiid k, n = cast(uiid, t->cnt);
uiid i = key & (n-1), b = i;
do {if (!(k = t->keys[i])) return 0;
if (k == key) return t->vals[i];
} while ((i = ((i+1) & (n-1))) != b);
return 0;
}
/* ---------------------------------------------------------------------------
* Box
* --------------------------------------------------------------------------- */
api void
box_shrink(struct box *d, const struct box *s, int pad)
{
assert(d && s);
if (!d || !s) return;
d->x = s->x + pad;
d->y = s->y + pad;
d->w = max(0, s->w - 2*pad);
d->h = max(0, s->h - 2*pad);
}
api void
box_pad(struct box *d, const struct box *s, int padx, int pady)
{
assert(d && s);
if (!d || !s) return;
d->x = s->x + padx;
d->y = s->y + pady;
d->w = max(0, s->w - 2*padx);
d->h = max(0, s->h - 2*pady);
}
api int
box_intersect(const struct box *a, const struct box *b)
{
return intersect(a->x, a->y, a->w, a->h, b->x, b->y, b->w, b->h);
}
/* ---------------------------------------------------------------------------
* Buffer
* --------------------------------------------------------------------------- */
intern union param*
op_push(struct state *s, int n)
{
union param *op;
assert(s);
assert(n > 0);
assert(n < MAX_OPS-2);
{struct param_buf *ob = s->opbuf;
if ((s->op_idx + n) >= (MAX_OPS-2)) {
/* allocate new param buffer */
struct param_buf *b = 0;
b = arena_push(&s->arena, 1, szof(*ob), 0);
ob->ops[s->op_idx + 0].op = OP_NEXT_BUF;
ob->ops[s->op_idx + 1].p = b;
s->opbuf = ob = b;
s->op_idx = 0;
}
assert(s->op_idx + n < (MAX_OPS-2));
op = ob->ops + s->op_idx;
s->op_idx += n;}
return op;
}
intern const char*
str_store(struct state *s, const char *str, int n)
{
assert(s && str);
assert(s->buf);
assert(n < MAX_STR_BUF-1);
{struct str_buf *ob = s->buf;
if ((s->buf_off + n) > MAX_STR_BUF-1) {
/* allocate new data buffer */
struct str_buf *b = arena_push(&s->arena, 1, szof(*ob), 0);
s->buf_off = 0;
ob->next = b;
s->buf = ob = b;
} assert((s->buf_off + n) <= MAX_STR_BUF-1);
copy(ob->buf + s->buf_off, str, n);
/* store zero-terminated string */
{int off = s->buf_off;
ob->buf[s->buf_off + n] = 0;
s->total_buf_size += n + 1;
s->buf_off += n + 1;
return ob->buf + off;}}
}
intern void
cmd_add(struct cmd_buf *s, struct memory_arena *a, const union cmd *cmd)
{
assert(s);
assert(s->list);
assert(s->buf);
spinlock_begin(&s->lock);
{
struct cmd_blk *blk = s->buf;
if (s->idx >= MAX_OPS) {
/* allocate new cmd buffer block */
struct cmd_blk *b = 0;
b = arena_push(a, 1, szof(*blk), 0);
blk->next = b;
s->buf = blk = b;
s->idx = 0;
}
/* push cmd into buffer */
assert(s->idx < MAX_OPS);
blk->cmds[s->idx++] = *cmd;
}
spinlock_end(&s->lock);
}
/* ---------------------------------------------------------------------------
* IDs
* --------------------------------------------------------------------------- */
api void
pushid(struct state *s, unsigned id)
{
struct idrange *idr = 0;
assert(s);
assert(s->stkcnt < cntof(s->idstk));
if (!s || s->stkcnt >= cntof(s->idstk))
return;
idr = &s->idstk[s->stkcnt++];
idr->base = id, idr->cnt = 0;
s->lastid = (idr->base & 0xFFFFFFFFlu) << 32lu;
}
api void
setid(struct state *s, uiid id)
{
assert(s);
assert(s->stkcnt < cntof(s->idstk));
if (!s) return;
/* set one time only ID */
s->idstate = ID_GEN_ONE_TIME;
s->otid = id;
}
api uiid
genwid(struct state *s)
{
uiid id = 0;
assert(s);
if (!s) return 0;
/* generate widget ID */
{int idx = max(0, s->stkcnt-1);
struct idrange *idr = &s->idstk[idx];
id |= (idr->base & 0xFFFFFFFFlu) << 32lu;
id |= (++idr->cnt);
s->lastid = id;
return id;}
}
api uiid
genbid(struct state *s)
{
/* generate box ID */
switch (s->idstate) {
case ID_GEN_DEFAULT:
return genwid(s);
case ID_GEN_ONE_TIME: {
s->idstate = ID_GEN_DEFAULT;
return s->otid;
}}
}
api void
popid(struct state *s)
{
assert(s);
assert(s->stkcnt);
if (!s || !s->stkcnt) return;
s->stkcnt--;
}
/* ---------------------------------------------------------------------------
* Repository
* --------------------------------------------------------------------------- */
api struct box*
find(struct repository *repo, uiid id)
{
int idx = lookup(&repo->tbl, id);
if (!idx) return 0;
return repo->boxes + idx;
}
/* ---------------------------------------------------------------------------
* State
* --------------------------------------------------------------------------- */
intern struct state*
state_find(struct context *ctx, uiid id)
{
struct list_hook *i = 0;
assert(ctx);
if (!ctx) return 0;
list_foreach(i, &ctx->states) {
struct state *s = list_entry(i, struct state, hook);
if (s->id == id) return s;
} return 0;
}
api struct box*
polls(struct state *s, uiid id)
{
struct repository *repo = 0;
repo = s->repo;
if (!repo) return 0;
return find(repo, id);
}
/* ---------------------------------------------------------------------------
* Module
* --------------------------------------------------------------------------- */
intern struct module*
module_find(struct context *ctx, mid id)
{
struct list_hook *i = 0;
assert(ctx);
if (!ctx) return 0;
list_foreach(i, &ctx->mod) {
struct module *m = list_entry(i, struct module, hook);
if (m->id == id) return m;
} return 0;
}
api struct state*
module_begin(struct context *ctx, mid id,
enum relationship rel, mid parent, mid owner, uiid bid)
{
struct state *s = 0;
assert(ctx);
if (!ctx) return 0;
/* try to pick up previous state if possible */
spinlock_begin(&ctx->module_lock);
s = state_find(ctx, id);
spinlock_end(&ctx->module_lock);
if (s) {s->op_idx -= 2; return s;}
/* allocate temporary state */
spinlock_begin(&ctx->mem_lock);
s = arena_push_type(&ctx->arena, struct state);
spinlock_end(&ctx->mem_lock);
assert(s);
if (!s) return 0;
/* setup state */
s->id = id;
s->ctx = ctx;
s->cfg = &ctx->cfg;
s->arena.mem = &ctx->blkmem;
s->param_list = s->opbuf = arena_push(&s->arena, 1, szof(struct param_buf), 0);
s->buf_list = s->buf = arena_push(&s->arena, 1, szof(struct str_buf), 0);
s->mod = module_find(ctx, id);
if (s->mod) s->repo = s->mod->repo[s->mod->repoid];
/* append state into list */
list_init(&s->hook);
spinlock_begin(&ctx->module_lock);
list_add_tail(&ctx->states, &s->hook);
spinlock_end(&ctx->module_lock);
pushid(s, id);
/* serialize api call */
{union param *p = op_push(s, 8);
p[0].op = OP_BUF_BEGIN;
p[1].mid = id;
p[2].op = OP_ULNK;
p[3].mid = parent;
p[4].id = bid;
p[5].op = OP_CONCT;
p[6].mid = owner;
p[7].i = (int)rel;}
return s;
}
api void
module_end(struct state *s)
{
assert(s);
if (!s) return;
{union param *p = op_push(s,2);
p[0].op = OP_BUF_END;
p[1].id = s->id;}
popid(s);
assert(!s->wtop);
assert(!s->depth);
}
api int
module_destroy(struct context *ctx, mid id)
{
struct list_hook tmp;
struct module *m = 0;
struct list_hook *i = 0;
struct list_hook *n = 0;
struct repository *repo = 0;
assert(ctx);
assert(id);
if (!ctx || !id)
return 0;
/* find and unlink module */
m = module_find(ctx, id);
if (!m) return 0;
repo = m->repo[m->repoid];
if (repo) {
/* unlink root box */
struct box *root = m->root;
list_del(&root->node);
}
list_del(&m->hook);
list_move_tail(&tmp, &m->hook);
/* remove sub-tree */
list_foreach_s(i, n, &ctx->mod) {
struct list_hook *k = 0;
struct module *sub = list_entry(i, struct module, hook);
list_foreach(k, &tmp) {
struct module *gb = list_entry(k, struct module, hook);
if (sub->parent == gb) {
list_move_tail(&tmp, &sub->hook);
break;
}
}
} list_splice_tail(&ctx->garbage, &tmp);
return 1;
}
api struct state*
section_begin(struct state *s, mid id)
{
assert(s);
assert(id && "Section are not allowed to overwrite root");
if (!s || !id) return 0;
return module_begin(s->ctx, id, RELATIONSHIP_INDEPENDENT, s->id, s->id, s->lastid);
}
api void
section_end(struct state *s)
{
module_end(s);
}
api void
link(struct state *s, mid id, enum relationship rel)
{
assert(s);
if (!s) return;
{union param *p = op_push(s, 5);
p[0].op = OP_DLNK;
p[1].mid = id;
p[2].op = OP_CONCT;
p[3].mid = id;
p[4].i = (int)rel;}
}
api struct state*
begin(struct context *ctx, mid id)
{
assert(ctx);
if (!ctx) return 0;
return module_begin(ctx, id, RELATIONSHIP_INDEPENDENT,
0, 0, WIDGET_UI - WIDGET_LAYER_BEGIN);
}
api void
end(struct state *s)
{
assert(s);
if (!s) return;
module_end(s);
}
api void
slot(struct state *s, uiid id)
{
assert(s);
assert(s->wtop > 0);
if (!s || s->wtop <= 0) return;
widget_begin(s, WIDGET_SLOT);
{union param *p = op_push(s, 4);
p[0].op = OP_BOX_PUSH;
p[1].id = id;
p[2].id = s->wstk[s->wtop-1].id;
p[3].op = OP_BOX_POP;
widget_end(s);}
}
/* ---------------------------------------------------------------------------
* Popup
* --------------------------------------------------------------------------- */
api struct state*
popup_begin(struct state *s, mid id, enum popup_type type)
{
unsigned bid = 0;
switch (type) {
case POPUP_BLOCKING:
bid = WIDGET_POPUP-WIDGET_LAYER_BEGIN; break;
case POPUP_NON_BLOCKING:
bid = WIDGET_CONTEXTUAL-WIDGET_LAYER_BEGIN; break;}
return module_begin(s->ctx, id, RELATIONSHIP_INDEPENDENT, 0, s->id, bid);
}
api void