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