aboutsummaryrefslogtreecommitdiff
path: root/src/base.c
blob: 75cd8c487ed1c67743c5b4ee8a0aad7b2016e7ab (plain)
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
/*
 * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <inttypes.h>

#include "base_internal.h"

static inline d2tk_id_t
_d2tk_flip_get_cur(d2tk_flip_t *flip)
{
	return flip->cur;
}

static inline d2tk_id_t
_d2tk_flip_get_old(d2tk_flip_t *flip)
{
	return flip->old;
}

static inline bool
_d2tk_flip_equal_cur(d2tk_flip_t *flip, d2tk_id_t id)
{
	return _d2tk_flip_get_cur(flip) == id;
}

static inline bool
_d2tk_flip_equal_old(d2tk_flip_t *flip, d2tk_id_t id)
{
	return _d2tk_flip_get_old(flip) == id;
}

static inline bool
_d2tk_flip_invalid(d2tk_flip_t *flip)
{
	return _d2tk_flip_equal_cur(flip, 0);
}

static inline bool
_d2tk_flip_invalid_old(d2tk_flip_t *flip)
{
	return _d2tk_flip_equal_old(flip, 0);
}

static inline void
_d2tk_flip_set_old(d2tk_flip_t *flip, d2tk_id_t old)
{
	flip->old = old;
}

static inline void
_d2tk_flip_set(d2tk_flip_t *flip, d2tk_id_t new)
{
	if(_d2tk_flip_invalid_old(flip))
	{
		_d2tk_flip_set_old(flip, flip->cur);
	}

	flip->cur = new;
}

static inline void
_d2tk_flip_clear(d2tk_flip_t *flip)
{
	_d2tk_flip_set(flip, 0);
}

static inline void
_d2tk_flip_clear_old(d2tk_flip_t *flip)
{
	_d2tk_flip_set_old(flip, 0);
}

void *
_d2tk_base_get_atom(d2tk_base_t *base, d2tk_id_t id, d2tk_atom_type_t type,
	d2tk_atom_event_t event)
{
	for(unsigned i = 0, idx = (id + i*i) & _D2TK_MASK_ATOMS;
		i < _D2TK_MAX_ATOM;
		i++, idx = (id + i*i) & _D2TK_MASK_ATOMS)
	{
		d2tk_atom_t *atom = &base->atoms[idx];

		if( (atom->id != 0) && (atom->id != id) )
		{
			continue;
		}

		if( (atom->id == 0) || (atom->type != type) || !atom->body) // new atom or changed type
		{
			atom->id = id;
			atom->type = type;
			atom->event = event;

			size_t len;
			switch(atom->type)
			{
				case D2TK_ATOM_SCROLL:
				{
					len = d2tk_atom_body_scroll_sz;
				} break;
				case D2TK_ATOM_PANE:
				{
					len = d2tk_atom_body_pane_sz;
				} break;
				case D2TK_ATOM_FLOW:
				{
					len = d2tk_atom_body_flow_sz;
				} break;
#if D2TK_PTY
				case D2TK_ATOM_PTY:
				{
					len = d2tk_atom_body_pty_sz;
				} break;
#endif
				case D2TK_ATOM_FLOW_NODE:
					// fall-through
				case D2TK_ATOM_FLOW_ARC:
					// fall-through
				default:
				{
					len = 0;
				} break;
			}

			if(len == 0)
			{
				if(atom->event)
				{
					atom->event(D2TK_ATOM_EVENT_DEINIT, atom->body);
					atom->event = NULL;
				}
				free(atom->body);
				atom->body = 0;
			}
			else
			{
				void *body = realloc(atom->body, len);
				if(!body)
				{
					return NULL;
				}

				memset(body, 0x0, len);
				atom->body = body;
			}
		}

		return atom->body;
	}

	return NULL; // no space left
}

D2TK_API void
d2tk_clip_int32(int32_t min, int32_t *value, int32_t max)
{
	if(*value < min)
	{
		*value = min;
	}
	else if(*value > max)
	{
		*value = max;
	}
}

D2TK_API void
d2tk_clip_int64(int64_t min, int64_t *value, int64_t max)
{
	if(*value < min)
	{
		*value = min;
	}
	else if(*value > max)
	{
		*value = max;
	}
}

D2TK_API void
d2tk_clip_float(float min, float *value, float max)
{
	if(*value < min)
	{
		*value = min;
	}
	else if(*value > max)
	{
		*value = max;
	}
}

D2TK_API void
d2tk_clip_double(double min, double *value, double max)
{
	if(*value < min)
	{
		*value = min;
	}
	else if(*value > max)
	{
		*value = max;
	}
}

D2TK_API bool
d2tk_base_get_mod(d2tk_base_t *base)
{
	return base->keys.mod != D2TK_MODMASK_NONE;
}

D2TK_API const char *
d2tk_state_dump(d2tk_state_t state)
{
#define LEN 16
	static char buf [LEN + 1];

	for(unsigned i = 0; i < LEN; i++)
	{
		buf[LEN - 1 - i] = (1 << i) & state
			? '1'
			: '.';
	}

	buf[LEN] = '\0';

	return buf;
#undef LEN
}

D2TK_API bool
d2tk_state_is_down(d2tk_state_t state)
{
	return (state & D2TK_STATE_DOWN);
}

D2TK_API bool
d2tk_state_is_up(d2tk_state_t state)
{
	return (state & D2TK_STATE_UP);
}

D2TK_API bool
d2tk_state_is_scroll_up(d2tk_state_t state)
{
	return (state & D2TK_STATE_SCROLL_UP);
}

D2TK_API bool
d2tk_state_is_scroll_down(d2tk_state_t state)
{
	return (state & D2TK_STATE_SCROLL_DOWN);
}

D2TK_API bool
d2tk_state_is_motion(d2tk_state_t state)
{
	return (state & D2TK_STATE_MOTION);
}

D2TK_API bool
d2tk_state_is_scroll_left(d2tk_state_t state)
{
	return (state & D2TK_STATE_SCROLL_LEFT);
}

D2TK_API bool
d2tk_state_is_scroll_right(d2tk_state_t state)
{
	return (state & D2TK_STATE_SCROLL_RIGHT);
}

D2TK_API bool
d2tk_state_is_active(d2tk_state_t state)
{
	return (state & D2TK_STATE_ACTIVE);
}

D2TK_API bool
d2tk_state_is_hot(d2tk_state_t state)
{
	return (state & D2TK_STATE_HOT);
}

D2TK_API bool
d2tk_state_is_focused(d2tk_state_t state)
{
	return (state & D2TK_STATE_FOCUS);
}

D2TK_API bool
d2tk_state_is_focus_in(d2tk_state_t state)
{
	return (state & D2TK_STATE_FOCUS_IN);
}

D2TK_API bool
d2tk_state_is_focus_out(d2tk_state_t state)
{
	return (state & D2TK_STATE_FOCUS_OUT);
}

D2TK_API bool
d2tk_state_is_changed(d2tk_state_t state)
{
	return (state & D2TK_STATE_CHANGED);
}

D2TK_API bool
d2tk_state_is_enter(d2tk_state_t state)
{
	return (state & D2TK_STATE_ENTER);
}

D2TK_API bool
d2tk_state_is_over(d2tk_state_t state)
{
	return (state & D2TK_STATE_OVER);
}

D2TK_API bool
d2tk_base_is_hit(d2tk_base_t *base, const d2tk_rect_t *rect)
{
	if(  (base->mouse.x < rect->x)
		|| (base->mouse.y < rect->y)
		|| (base->mouse.x >= rect->x + rect->w)
		|| (base->mouse.y >= rect->y + rect->h) )
	{
		return false;
	}

	return true;
}

static inline bool
_d2tk_base_set_focus(d2tk_base_t *base, d2tk_id_t id)
{
	_d2tk_flip_set(&base->focusitem, id);

	return true;
}

static inline void
_d2tk_base_change_focus(d2tk_base_t *base)
{
	// copy edit.text_in to edit.text_out
	strncpy(base->edit.text_out, base->edit.text_in, sizeof(base->edit.text_out));
}

void
_d2tk_base_clear_chars(d2tk_base_t *base)
{
	base->keys.nchars = 0;
}

D2TK_API void
d2tk_base_append_utf8(d2tk_base_t *base, utf8_int32_t utf8)
{
	if(base->keys.nchars < sizeof(base->keys.nchars))
	{
		base->keys.chars[base->keys.nchars++] = utf8;
	}
}

D2TK_API void
d2tk_base_get_utf8(d2tk_base_t *base, ssize_t *len, const utf8_int32_t **utf8)
{
	if(len)
	{
		*len = base->keys.nchars;
	}

	if(utf8)
	{
		*utf8 = base->keys.chars;
	}

	_d2tk_base_clear_chars(base);
}

d2tk_state_t
_d2tk_base_is_active_hot_vertical_scroll(d2tk_base_t *base)
{
	d2tk_state_t state = D2TK_STATE_NONE;

	// test for vertical scrolling
	if(base->scroll.dy != 0.f)
	{
		if(base->scroll.dy > 0.f)
		{
			state |= D2TK_STATE_SCROLL_UP;
		}
		else
		{
			state |= D2TK_STATE_SCROLL_DOWN;
		}

		base->scroll.ody = base->scroll.dy;
		base->scroll.dy = 0; // eat scrolling
	}

	return state;
}

d2tk_state_t
_d2tk_base_is_active_hot_horizontal_scroll(d2tk_base_t *base)
{
	d2tk_state_t state = D2TK_STATE_NONE;

	// test for horizontal scrolling
	if(base->scroll.dx != 0.f)
	{
		if(base->scroll.dx > 0.f)
		{
			state |= D2TK_STATE_SCROLL_RIGHT;
		}
		else
		{
			state |= D2TK_STATE_SCROLL_LEFT;
		}

		base->scroll.odx = base->scroll.dx;
		base->scroll.dx = 0; // eat scrolling
	}

	return state;
}

D2TK_API d2tk_state_t
d2tk_base_is_active_hot(d2tk_base_t *base, d2tk_id_t id,
	const d2tk_rect_t *rect, d2tk_flag_t flags)
{
	d2tk_state_t state = D2TK_STATE_NONE;
	bool is_active = _d2tk_flip_equal_cur(&base->activeitem, id);
	bool is_hot = false;
	bool is_over = false;
	bool curfocus = _d2tk_flip_equal_cur(&base->focusitem, id);
	bool newfocus = curfocus;
	const bool lastfocus = _d2tk_flip_equal_old(&base->focusitem, id);

	// handle forward focus
	if(curfocus)
	{
		if(d2tk_base_get_modmask(base, D2TK_MODMASK_CTRL, false))
		{
			if(d2tk_base_get_keymask(base, D2TK_KEYMASK_RIGHT, false))
			{
				newfocus = false; // do NOT change curfocus
				base->focused = false; // clear focused flag
			}
		}
		else
		{
			if(d2tk_base_get_keymask(base, D2TK_KEYMASK_LEFT, false))
			{
				state |= D2TK_STATE_SCROLL_LEFT;
				base->scroll.odx = -1;
			}

			if(d2tk_base_get_keymask(base, D2TK_KEYMASK_RIGHT, false))
			{
				state |= D2TK_STATE_SCROLL_RIGHT;
				base->scroll.odx = 1;
			}

			if(d2tk_base_get_keymask(base, D2TK_KEYMASK_UP, false))
			{
				state |= D2TK_STATE_SCROLL_UP;
				base->scroll.ody = 1;
			}

			if(d2tk_base_get_keymask(base, D2TK_KEYMASK_DOWN, false))
			{
				state |= D2TK_STATE_SCROLL_DOWN;
				base->scroll.ody = -1;
			}
		}

		if(d2tk_base_get_keymask(base, D2TK_KEYMASK_ENTER, false))
		{
			is_active = true;
			state |= D2TK_STATE_ENTER;
		}
	}
	else if(!base->focused)
	{
		curfocus = _d2tk_base_set_focus(base, id);
		newfocus = curfocus;
		base->focused = true; // set focused flag
	}

	// test for mouse up
	if(  is_active
		&& !d2tk_base_get_butmask(base, D2TK_BUTMASK_LEFT, false) )
	{
		_d2tk_flip_clear(&base->activeitem);
		is_active = false;
		state |= D2TK_STATE_UP;
	}

	// test for mouse over
	if(d2tk_base_is_hit(base, rect))
	{
		// test for mouse down
		if(  _d2tk_flip_invalid(&base->activeitem)
			&& d2tk_base_get_butmask(base, D2TK_BUTMASK_LEFT, false) )
		{
			_d2tk_flip_set(&base->activeitem, id);
			is_active = true;
			curfocus = _d2tk_base_set_focus(base, id);
			newfocus = curfocus;
			state |= D2TK_STATE_DOWN;
		}

		if(d2tk_base_get_butmask(base, D2TK_BUTMASK_LEFT, false)  && !is_active)
		{
			// another widget is active with mouse down, so don't be hot
		}
		else
		{
			_d2tk_flip_set(&base->hotitem, id);
			is_hot = true;
		}

		is_over = true;

		// test whether to handle scrolling
		if(flags & D2TK_FLAG_SCROLL_Y)
		{
			state |= _d2tk_base_is_active_hot_vertical_scroll(base);
		}

		if(flags & D2TK_FLAG_SCROLL_X)
		{
			state |= _d2tk_base_is_active_hot_horizontal_scroll(base);
		}
	}

	if(is_active)
	{
		if( (base->mouse.dx != 0) || (base->mouse.dy != 0) )
		{
			state |= D2TK_STATE_MOTION;
		}

		state |= D2TK_STATE_ACTIVE;
	}

	if(is_hot)
	{
		state |= D2TK_STATE_HOT;
	}

	if(is_over)
	{
		state |= D2TK_STATE_OVER;
	}

	if(newfocus)
	{
		state |= D2TK_STATE_FOCUS;
	}

	{
		if(lastfocus && !curfocus)
		{
			state |= D2TK_STATE_FOCUS_OUT;
			_d2tk_flip_clear_old(&base->focusitem); // clear previous focus
#ifdef D2TK_DEBUG
			fprintf(stderr, "\tfocus out 0x%016"PRIx64"\n", id);
#endif
		}
		else if(!lastfocus && curfocus)
		{
			if(_d2tk_flip_invalid_old(&base->focusitem) && base->not_first_time)
			{
				_d2tk_flip_set(&base->focusitem, _d2tk_flip_get_cur(&base->focusitem));
			}
			else
			{
				state |= D2TK_STATE_FOCUS_IN;
#ifdef D2TK_DEBUG
				fprintf(stderr, "\tfocus in 0x%016"PRIx64"\n", id);
#endif
				_d2tk_base_change_focus(base);
			}
		}
	}

	// handle backwards focus
	if(newfocus)
	{
		if(d2tk_base_get_modmask(base, D2TK_MODMASK_CTRL, false))
		{
			if(d2tk_base_get_keymask(base, D2TK_KEYMASK_LEFT, false))
			{
				_d2tk_base_set_focus(base, base->lastitem);
			}
		}
	}

	base->lastitem = id;

	base->not_first_time = true;

	return state;
}

#define nocol 0x0
#define light_grey 0x7f7f7fff
#define dark_grey 0x3f3f3fff
#define darker_grey 0x222222ff
#define black 0x000000ff
#define white 0xffffffff
#define light_orange 0xffcf00ff
#define dark_orange 0xcf9f00ff

D2TK_API const d2tk_style_t *
d2tk_base_get_default_style()
{
	static const d2tk_style_t style = {
		.font_face                       = "FiraSans-Bold.ttf",
		.border_width                    = 1,
		.padding                         = 1,
		.rounding                        = 4,
		.bg_color                        = darker_grey,
		.fill_color = {
			[D2TK_TRIPLE_NONE]             = dark_grey,
			[D2TK_TRIPLE_HOT]              = light_grey,
			[D2TK_TRIPLE_ACTIVE]           = dark_orange,
			[D2TK_TRIPLE_ACTIVE_HOT]       = light_orange,
			[D2TK_TRIPLE_FOCUS]            = dark_grey,
			[D2TK_TRIPLE_HOT_FOCUS]        = light_grey,
			[D2TK_TRIPLE_ACTIVE_FOCUS]     = dark_orange,
			[D2TK_TRIPLE_ACTIVE_HOT_FOCUS] = light_orange,
		},
		.stroke_color = {
			[D2TK_TRIPLE_NONE]             = black,
			[D2TK_TRIPLE_HOT]              = black,
			[D2TK_TRIPLE_ACTIVE]           = black,
			[D2TK_TRIPLE_ACTIVE_HOT]       = black,
			[D2TK_TRIPLE_FOCUS]            = white,
			[D2TK_TRIPLE_HOT_FOCUS]        = white,
			[D2TK_TRIPLE_ACTIVE_FOCUS]     = white,
			[D2TK_TRIPLE_ACTIVE_HOT_FOCUS] = white,
		},
		.text_stroke_color = {
			[D2TK_TRIPLE_NONE]             = white,
			[D2TK_TRIPLE_HOT]              = light_orange,
			[D2TK_TRIPLE_ACTIVE]           = white,
			[D2TK_TRIPLE_ACTIVE_HOT]       = dark_grey,
			[D2TK_TRIPLE_FOCUS]            = white,
			[D2TK_TRIPLE_HOT_FOCUS]        = light_orange,
			[D2TK_TRIPLE_ACTIVE_FOCUS]     = white,
			[D2TK_TRIPLE_ACTIVE_HOT_FOCUS] = dark_grey
		},
		.text_fill_color = {
			[D2TK_TRIPLE_NONE]             = nocol,
			[D2TK_TRIPLE_HOT]              = nocol,
			[D2TK_TRIPLE_ACTIVE]           = nocol,
			[D2TK_TRIPLE_ACTIVE_HOT]       = nocol,
			[D2TK_TRIPLE_FOCUS]            = nocol,
			[D2TK_TRIPLE_HOT_FOCUS]        = nocol,
			[D2TK_TRIPLE_ACTIVE_FOCUS]     = nocol,
			[D2TK_TRIPLE_ACTIVE_HOT_FOCUS] = nocol
		}
	};

	return &style;
}

D2TK_API const d2tk_style_t *
d2tk_base_get_style(d2tk_base_t *base)
{
	return base->style ? base->style : d2tk_base_get_default_style();
}

D2TK_API void
d2tk_base_set_style(d2tk_base_t *base, const d2tk_style_t *style)
{
	base->style = style;
}

D2TK_API void
d2tk_base_set_default_style(d2tk_base_t *base)
{
	d2tk_base_set_style(base, NULL);
}

D2TK_API d2tk_base_t *
d2tk_base_new(const d2tk_core_driver_t *driver, void *data)
{
	d2tk_base_t *base = calloc(1, sizeof(d2tk_base_t));
	if(!base)
	{
		return NULL;
	}

	atomic_init(&base->again, false);

	base->core = d2tk_core_new(driver, data);

	return base;
}

D2TK_API void
d2tk_base_set_ttls(d2tk_base_t *base, uint32_t sprites, uint32_t memcaches)
{
	d2tk_core_set_ttls(base->core, sprites, memcaches);
}

D2TK_API void
d2tk_base_free(d2tk_base_t *base)
{
	for(unsigned i = 0; i < _D2TK_MAX_ATOM; i++)
	{
		d2tk_atom_t *atom = &base->atoms[i];

		atom->id = 0;
		atom->type = 0;
		if(atom->event)
		{
			atom->event(D2TK_ATOM_EVENT_DEINIT, atom->body);
			atom->event = NULL;
		}
		free(atom->body);
		atom->body = NULL;
	}

	d2tk_core_free(base->core);
	free(base);
}

D2TK_API void
d2tk_base_pre(d2tk_base_t *base)
{
	// reset hot item
	_d2tk_flip_clear(&base->hotitem);

	// calculate mouse motion
	base->mouse.dx = (int32_t)base->mouse.x - base->mouse.ox;
	base->mouse.dy = (int32_t)base->mouse.y - base->mouse.oy;

	// reset clear-focus flag
	base->clear_focus = false;

	const d2tk_style_t *style = d2tk_base_get_style(base);
	d2tk_core_set_bg_color(base->core, style->bg_color);

	d2tk_core_pre(base->core);
}

D2TK_API void
d2tk_base_post(d2tk_base_t *base)
{
	// clear scroll
	base->scroll.dx = 0.f;
	base->scroll.dy = 0.f;

	// store old mouse position
	base->mouse.ox = base->mouse.x;
	base->mouse.oy = base->mouse.y;

	if(base->clear_focus)
	{
		_d2tk_flip_clear(&base->activeitem);

		base->focused = false;
	}

	_d2tk_base_clear_chars(base);

	d2tk_core_post(base->core);
}

D2TK_API void
d2tk_base_probe(d2tk_base_t *base)
{
	for(unsigned i = 0; i < _D2TK_MAX_ATOM; i++)
	{
		d2tk_atom_t *atom = &base->atoms[i];

		if(atom->id && atom->type && atom->event)
		{
			if(atom->event(D2TK_ATOM_EVENT_PROBE, atom->body))
			{
				d2tk_base_set_again(base);
				break;
			}
		}
	}
}

D2TK_API void
d2tk_base_clear_focus(d2tk_base_t *base)
{
	base->clear_focus = true;
}

D2TK_API bool
d2tk_base_set_again(d2tk_base_t *base)
{
	return atomic_exchange(&base->again, true);
}

D2TK_API bool
d2tk_base_get_again(d2tk_base_t *base)
{
	return atomic_exchange(&base->again, false);
}

D2TK_API void
d2tk_base_set_mouse_pos(d2tk_base_t *base, d2tk_coord_t x, d2tk_coord_t y)
{
	base->mouse.x = x;
	base->mouse.y = y;
}

D2TK_API void
d2tk_base_get_mouse_pos(d2tk_base_t *base, d2tk_coord_t *x, d2tk_coord_t *y)
{
	if(x)
	{
		*x = base->mouse.x;
	}

	if(y)
	{
		*y = base->mouse.y;
	}
}

D2TK_API void
d2tk_base_add_mouse_scroll(d2tk_base_t *base, int32_t dx, int32_t dy)
{
	base->scroll.dx += dx;
	base->scroll.dy += dy;
}

D2TK_API void
d2tk_base_get_mouse_scroll(d2tk_base_t *base, int32_t *dx, int32_t *dy,
	bool clear)
{
	if(dx)
	{
		*dx = base->scroll.dx;
	}

	if(dy)
	{
		*dy = base->scroll.dy;
	}

	if(clear)
	{
		base->scroll.dx = 0;
		base->scroll.dy = 0;
	}
}

D2TK_API bool
d2tk_base_set_butmask(d2tk_base_t *base, d2tk_butmask_t mask, bool down)
{
	const bool old_state = (base->mouse.mask & mask) == mask;

	if(down)
	{
		base->mouse.mask |= mask;
	}
	else
	{
		base->mouse.mask &= ~mask;
	}

	return old_state;
}

D2TK_API bool
d2tk_base_get_butmask(d2tk_base_t *base, d2tk_butmask_t mask, bool clear)
{
	const bool old_state = (base->mouse.mask & mask) == mask;

	if(clear)
	{
		base->mouse.mask &= ~mask;
	}

	return old_state;

}

D2TK_API bool
d2tk_base_set_modmask(d2tk_base_t *base, d2tk_modmask_t mask, bool down)
{
	const bool old_state = (base->keys.mod & mask) == mask;

	if(down)
	{
		base->keys.mod |= mask;
	}
	else
	{
		base->keys.mod &= ~mask;
	}

	return old_state;
}

D2TK_API bool
d2tk_base_get_modmask(d2tk_base_t *base, d2tk_modmask_t mask, bool clear)
{
	const bool old_state = (base->keys.mod & mask) == mask;

	if(clear)
	{
		base->keys.mod &= ~mask;
	}

	return old_state;

}

D2TK_API bool
d2tk_base_set_keymask(d2tk_base_t *base, d2tk_keymask_t mask, bool down)
{
	const bool old_state = (base->keys.mask & mask) == mask;

	if(down)
	{
		base->keys.mask |= mask;
	}
	else
	{
		base->keys.mask &= ~mask;
	}

	return old_state;
}

D2TK_API bool
d2tk_base_get_keymask(d2tk_base_t *base, d2tk_keymask_t mask, bool clear)
{
	const bool old_state = (base->keys.mask & mask) == mask;

	if(clear)
	{
		base->keys.mask &= ~mask;
	}

	return old_state;

}

D2TK_API void
d2tk_base_set_dimensions(d2tk_base_t *base, d2tk_coord_t w, d2tk_coord_t h)
{
	d2tk_core_set_dimensions(base->core, w, h);
}

D2TK_API void
d2tk_base_get_dimensions(d2tk_base_t *base, d2tk_coord_t *w, d2tk_coord_t *h)
{
	d2tk_core_get_dimensions(base->core, w, h);
}

D2TK_API void
d2tk_base_set_full_refresh(d2tk_base_t *base)
{
	d2tk_core_set_full_refresh(base->core);
}