1 /*
2  * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
3  *
4  * This is free software: you can redistribute it and/or modify
5  * it under the terms of the Artistic License 2.0 as published by
6  * The Perl Foundation.
7  *
8  * This source is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * Artistic License 2.0 for more details.
12  *
13  * You should have received a copy of the Artistic License 2.0
14  * along the source as a COPYING file. If not, obtain it from
15  * http://www.perlfoundation.org/artistic_license_2_0.
16  */
17 
18 #include <stdatomic.h>
19 
20 #include <d2tk/base.h>
21 #include <d2tk/hash.h>
22 #include "core_internal.h"
23 
24 #define _D2TK_MAX_ATOM 0x1000
25 #define _D2TK_MASK_ATOMS (_D2TK_MAX_ATOM - 1)
26 
27 typedef enum _d2tk_atom_type_t {
28 	D2TK_ATOM_NONE,
29 	D2TK_ATOM_SCROLL,
30 	D2TK_ATOM_PANE,
31 	D2TK_ATOM_FLOW,
32 	D2TK_ATOM_FLOW_NODE,
33 	D2TK_ATOM_FLOW_ARC,
34 #if D2TK_PTY
35 	D2TK_ATOM_PTY,
36 #endif
37 #if D2TK_EVDEV
38 	D2TK_ATOM_VKB,
39 #endif
40 } d2tk_atom_type_t;
41 
42 typedef enum _d2tk_atom_event_type_t {
43 	D2TK_ATOM_EVENT_NONE,
44 	D2TK_ATOM_EVENT_FD,
45 	D2TK_ATOM_EVENT_DEINIT
46 } d2tk_atom_event_type_t;
47 
48 typedef struct _d2tk_flip_t d2tk_flip_t;
49 typedef struct _d2tk_atom_t d2tk_atom_t;
50 typedef int (*d2tk_atom_event_t)(d2tk_atom_event_type_t event, void *data);
51 
52 struct _d2tk_flip_t {
53 	d2tk_id_t old;
54 	d2tk_id_t cur;
55 };
56 
57 struct _d2tk_atom_t {
58 	d2tk_id_t id;
59 	d2tk_atom_type_t type;
60 	void *body;
61 	d2tk_atom_event_t event;
62 };
63 
64 struct _d2tk_base_t {
65 	d2tk_flip_t hotitem;
66 	d2tk_flip_t activeitem;
67 	d2tk_flip_t focusitem;
68 	d2tk_id_t lastitem;
69 
70 	bool not_first_time;
71 	bool unicode_mode;
72 	uint32_t unicode_acc;
73 
74 	struct {
75 		d2tk_coord_t x;
76 		d2tk_coord_t y;
77 		d2tk_coord_t ox;
78 		d2tk_coord_t oy;
79 		d2tk_coord_t dx;
80 		d2tk_coord_t dy;
81 		d2tk_butmask_t mask;
82 		d2tk_butmask_t mask_prev;
83 	} mouse;
84 
85 	struct {
86 		int32_t odx;
87 		int32_t ody;
88 		int32_t dx;
89 		int32_t dy;
90 	} scroll;
91 
92 	struct {
93 		size_t nchars;
94 		utf8_int32_t chars [32];
95 		unsigned keymod;
96 		d2tk_keymask_t mask;
97 		d2tk_keymask_t mask_prev;
98 		d2tk_modmask_t mod;
99 	} keys;
100 
101 	struct {
102 		char text_in [1024];
103 		char text_out [1024];
104 	} edit;
105 
106 	struct {
107 		char buf [1024];
108 		size_t len;
109 		d2tk_coord_t h;
110 	} tooltip;
111 
112 	const d2tk_style_t *style;
113 
114 	atomic_bool again;
115 	bool clear_focus;
116 	bool focused;
117 
118 	d2tk_core_t *core;
119 
120 	d2tk_atom_t atoms [_D2TK_MAX_ATOM];
121 };
122 
123 extern const size_t d2tk_atom_body_flow_sz;
124 extern const size_t d2tk_atom_body_pane_sz;
125 extern const size_t d2tk_atom_body_scroll_sz;
126 #if D2TK_PTY
127 extern const size_t d2tk_atom_body_pty_sz;
128 #endif
129 #if D2TK_EVDEV
130 extern const size_t d2tk_atom_body_vkb_sz;
131 #endif
132 
133 void *
134 _d2tk_base_get_atom(d2tk_base_t *base, d2tk_id_t id, d2tk_atom_type_t type,
135 	d2tk_atom_event_t event);
136 
137 d2tk_state_t
138 _d2tk_base_is_active_hot_vertical_scroll(d2tk_base_t *base);
139 
140 d2tk_state_t
141 _d2tk_base_is_active_hot_horizontal_scroll(d2tk_base_t *base);
142 
143 void
144 _d2tk_base_clear_chars(d2tk_base_t *base);
145 
146 d2tk_state_t
147 _d2tk_base_tooltip_draw(d2tk_base_t *base, ssize_t lbl_len, const char *lbl,
148 	d2tk_coord_t h);
149