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