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