1 // Written in the D programming language.
2 module BearLibTerminal;
3 
4 private import std.string: toStringz;
5 private import std.array: join;
6 
7 private alias color_t = uint;
8 private alias colour_t = uint;
9 
10 
11 private string format(T...)(string s, T args) {
12 	import std.array: appender;
13 	import std.format: formattedWrite;
14 	auto w = appender!string();
15 	formattedWrite(w, s, args);
16 	return w.data;
17 }
18 
19 private extern (C) {
20 	public struct dimensions_t { int width, height; }
21 	int terminal_open();
22 	void terminal_close();
23 	int terminal_set8(const char*);
24 	char *terminal_get8(const char*, const char*);
25 	void terminal_color(color_t);
26 	void terminal_bkcolor(color_t);
27 	void terminal_composition(int);
28 	void terminal_layer(int);
29 	void terminal_clear();
30 	void terminal_clear_area(int, int, int, int);
31 	void terminal_crop(int, int, int, int);
32 	void terminal_refresh();
33 	void terminal_put(int, int, int);
34 	int terminal_pick(int, int, int);
35 	color_t terminal_pick_color(int, int, int);
36 	color_t terminal_pick_bkcolor(int, int);
37 	void terminal_put_ext(int, int, int, int, int, color_t*);
38 	void terminal_print_ext8(int, int, int, int, int, const char*, int*, int*);
39 	void terminal_measure_ext8(int, int, const char*, int*, int*);
40 	int terminal_state(int);
41 	int terminal_check(int);
42 	int terminal_has_input();
43 	int terminal_read();
44 	int terminal_peek();
45 	color_t terminal_read_str8(int, int, char*, int);
46 	void terminal_delay(int);
47 	color_t color_from_name8(const char*);
48 	color_t color_from_argb(byte, byte, byte, byte);
49 }
50 
51 // namespace called "terminal"
52 pragma(inline, true) { struct terminal { static {
53 
54 
55 	// Has to be inline, for now
56 	enum keycode {
57 		a = 0x04,
58 		b = 0x05,
59 		c = 0x06,
60 		d = 0x07,
61 		e = 0x08,
62 		f = 0x09,
63 		g = 0x0a,
64 		h = 0x0b,
65 		i = 0x0c,
66 		j = 0x0d,
67 		k = 0x0e,
68 		l = 0x0f,
69 		m = 0x10,
70 		n = 0x11,
71 		o = 0x12,
72 		p = 0x13,
73 		q = 0x14,
74 		r = 0x15,
75 		s = 0x16,
76 		t = 0x17,
77 		u = 0x18,
78 		v = 0x19,
79 		w = 0x1a,
80 		x = 0x1b,
81 		y = 0x1c,
82 		z = 0x1d,
83 		K_1 = 0x1E,
84 		K_2 = 0x1F,
85 		K_3 = 0x20,
86 		K_4 = 0x21,
87 		K_5 = 0x22,
88 		K_6 = 0x23,
89 		K_7 = 0x24,
90 		K_8 = 0x25,
91 		K_9 = 0x26,
92 		K_0 = 0x27,
93 		enter = 0x28,
94 		escape = 0x29,
95 		backspace = 0x2a,
96 		tab = 0x2b,
97 		space = 0x2c,
98 		minus = 0x2d /*  -  */,
99 		equals = 0x2e /*  =  */,
100 		lbracket = 0x2f /*  [  */,
101 		rbracket = 0x30 /*  ]  */,
102 		backslash = 0x31 /*  \  */,
103 		semicolon = 0x33 /*  ,  */,
104 		apostrophe = 0x34 /*  '  */,
105 		grave = 0x35 /*  `  */,
106 		comma = 0x36 /*  ,  */,
107 		period = 0x37 /*  .  */,
108 		slash = 0x38 /*  /  */,
109 		F1 = 0x3A,
110 		F2 = 0x3B,
111 		F3 = 0x3C,
112 		F4 = 0x3D,
113 		F5 = 0x3E,
114 		F6 = 0x3F,
115 		F7 = 0x40,
116 		F8 = 0x41,
117 		F9 = 0x42,
118 		F10 = 0x43,
119 		F11 = 0x44,
120 		F12 = 0x45,
121 		pause = 0x48 /* Pause/Break */,
122 		insert = 0x49,
123 		home = 0x4a,
124 		pageup = 0x4b,
125 		K_delete = 0x4c,
126 		end = 0x4d,
127 		pagedown = 0x4e,
128 		right = 0x4F /* Right arrow */,
129 		left = 0x50 /* Left arrow */,
130 		down = 0x51 /* Down arrow */,
131 		up = 0x52 /* Up arrow */,
132 		KP_divide = 0x54 /* '/' on numpad */,
133 		KP_multiply = 0x55 /* '*' on numpad */,
134 		KP_minus = 0x56 /* '-' on numpad */,
135 		KP_plus = 0x57 /* '+' on numpad */,
136 		KP_enter = 0x58,
137 		KP_1 = 0x59,
138 		KP_2 = 0x5A,
139 		KP_3 = 0x5B,
140 		KP_4 = 0x5C,
141 		KP_5 = 0x5D,
142 		KP_6 = 0x5E,
143 		KP_7 = 0x5F,
144 		KP_8 = 0x60,
145 		KP_9 = 0x61,
146 		KP_0 = 0x62,
147 		kp_period = 0x63 /* '.' on numpad */,
148 		shift = 0x70,
149 		control = 0x71,
150 		alt = 0x72,
151 
152 		/*
153 		 * Mouse events/states
154 		 */
155 		mouse_left = 0x80 /* Buttons */,
156 		mouse_right = 0x81,
157 		mouse_middle = 0x82,
158 		mouse_x1 = 0x83,
159 		mouse_x2 = 0x84,
160 		mouse_move = 0x85 /* Movement event */,
161 		mouse_scroll = 0x86 /* Mouse scroll event */,
162 		mouse_x = 0x87 /* Cusor position in cells */,
163 		mouse_y = 0x88,
164 		mouse_pixel_x = 0x89 /* Cursor position in pixels */,
165 		mouse_pixel_y = 0x8A,
166 		mouse_wheel = 0x8B /* Scroll direction and amount */,
167 		mouse_clicks = 0x8C /* Number of consecutive clicks */,
168 
169 		/*
170 		 * If key was released instead of pressed, it's code will be OR'ed with key_released:
171 		 * a) pressed 'A': 0x04
172 		 * b) released 'A': 0x04|terminal.keycodes.key_released = 0x104
173 		 */
174 		key_released = 0x100,
175 
176 		/*
177 		 * Virtual key-codes for internal terminal states/variables.
178 		 * These can be accessed via terminal_state function.
179 		 */
180 		width = 0xC0 /* Terminal window size in cells */,
181 		height = 0xC1,
182 		cell_width = 0xC2 /* Character cell size in pixels */,
183 		cell_height = 0xC3,
184 		color = 0xC4 /* Current foregroung color */,
185 		bkcolor = 0xC5 /* Current background color */,
186 		layer = 0xC6 /* Current layer */,
187 		composition = 0xC7 /* Current composition state */,
188 		character = 0xC8 /* Translated ANSI code of last produced character */,
189 		wcharacter = 0xC9 /* Unicode codepoint of last produced character */,
190 		event = 0xCA /* Last dequeued event */,
191 		fullscreen = 0xCB /* Fullscreen state */,
192 
193 		/*
194 		 * Other events
195 		 */
196 		close = 0xe0,
197 		resized = 0xe1,
198 
199 		/*
200 		 * Generic mode enum.
201 		 * Right now it is used for composition option only.
202 		 */
203 		off = 0,
204 		on = 1,
205 
206 		// Input result codes for the terminal_read function.
207 		input_none = 0,
208 		input_cancelled = -1,
209 
210 		// Text printing alignment
211 		align_default = 0,
212 		align_left = 1,
213 		align_right = 2,
214 		align_center = 3,
215 		align_centre = 3,
216 		align_top = 4,
217 		align_bottom = 8,
218 		align_middle = 12
219 	}
220 
221 	int open(string title="BearLibTerminal") { int c = terminal_open(); setf("window.title=%s", title); return c; };
222 	void close() { terminal_close(); };
223 	int set(string[] s...) { return terminal_set8(toStringz(join(s))); };
224 	string get(string key, string defaultval) { import std.conv: to; return to!string(terminal_get8(toStringz(key), toStringz(defaultval))); }
225 	int setf(T...)(string s, T args) { return terminal_set8(toStringz(format(s, args))); }
226 	void color(color_t clr) { terminal_color(clr); };
227 	void bkcolor(color_t clr) { terminal_bkcolor(clr); };
228 	void composition(int mode) { terminal_composition(mode); };
229 	void layer(int lyr) { terminal_layer(lyr); };
230 	void clear() { terminal_clear(); };
231 	void clear_area(int x, int y, int w, int h) { terminal_clear_area(x, y, w, h); };
232 	void crop(int x, int y, int w, int h) { terminal_crop(x, y, w, h); };
233 	void refresh() { terminal_refresh(); };
234 	void put(int x, int y, int code) { terminal_put(x, y, code); };
235 	int pick(int x, int y, int index) { return terminal_pick(x, y, index); };
236 	color_t pick_color(int x, int y, int index) { return terminal_pick_color(x, y, index); };
237 	color_t pick_bkcolor(int x, int y) { return terminal_pick_bkcolor(x, y); };
238 	void put_ext(int x, int y, int dx, int dy, int code) { terminal_put_ext(x, y, dx, dy, code, null); };
239 	void put_ext(int x, int y, int dx, int dy, int code, color_t[4] corners) { terminal_put_ext(x, y, dx, dy, code, corners.ptr); };
240 
241 	dimensions_t print_ext(int x, int y, int w, int h, int alignment, string[] s...) {
242 		dimensions_t tmp;
243 		terminal_print_ext8(x, y, w, h, alignment, toStringz(join(s)), &tmp.width, &tmp.height);
244 		return tmp;
245 	}
246 	dimensions_t printf_ext(T...)(int x, int y, int w, int h, int alignment, string s, T args) { return print_ext(x, y, w, h, alignment, format(s, args)); }
247 	dimensions_t print(int x, int y, string[] s...) {
248 		return print_ext(x, y, 0, 0, 0, join(s));
249 	};
250 	dimensions_t printf(T...)(int x, int y, string s, T args) { return print(x, y, format(s, args)); };
251 
252 	dimensions_t measure_ext(int w, int h, string[] s...) {
253 		dimensions_t tmp;
254 		terminal_measure_ext8(w, h, toStringz(join(s)), &tmp.width, &tmp.height);
255 		return tmp;
256 	}
257 	dimensions_t measuref_ext(T...)(int w, in h, string s, T args) { return measure_ext(w, h, format(s, args)); }
258 	dimensions_t measure(string[] s...) { return measure_ext(0, 0, s); };
259 	dimensions_t measuref(T...)(string s, T args) { return measure(format(s, args)); };
260 	
261 	int state(int slot) { return terminal_state(slot); };
262 	bool check(int slot) { return terminal_state(slot) > 0; };
263 	int has_input() { return terminal_has_input(); };
264 	int read() { return terminal_read(); };
265 	int peek() { return terminal_peek(); };
266 	string read_str(int x, int y, int max, string prompt="") {
267 		assert (prompt.length <= max);
268 		import std.conv: to;
269 		import core.stdc.stdlib: malloc, free;
270 
271 		print(x, y, prompt);
272 
273 		char[] buf = new char[](max);
274 		buf[] = 0;
275 
276 		string tmp;
277 
278 		terminal_read_str8(x+cast(int)prompt.length, y, buf.ptr, max);
279 		tmp = to!string(buf);
280 		delete buf;
281 		return tmp;
282 	};
283 	void delay(int period) { terminal_delay(period); };
284 	color_t color_from_name(string name) { return color_from_name8(toStringz(name)); };
285 	pure color_t color_from_argb(ubyte a, ubyte r, ubyte g, ubyte b) { return (a << 24) | (r << 16) | (g << 8) | b; }
286 	pure color_t color_from_rgb(ubyte r, ubyte g, ubyte b) { return (r << 16) | (g << 8) | b; }
287 }}}