1 // Written in the D programming language.
2 module BearLibTerminal;
3 
4 private import std.string: toStringz;
5 
6 private alias color_t = uint;
7 private alias colour_t = uint;
8 
9 
10 private const(char*) format(T...)(string s, T args) {
11 	import std.array: appender;
12 	import std.format: formattedWrite;
13 	auto w = appender!string();
14 	formattedWrite(w, s, args);
15 	return toStringz(w.data);
16 }
17 
18 private extern (C) {
19 	int terminal_open();
20 	void terminal_close();
21 	int terminal_set8(const char*);
22 	void terminal_color(color_t);
23 	void terminal_bkcolor(color_t);
24 	void terminal_composition(int);
25 	void terminal_layer(int);
26 	void terminal_clear();
27 	void terminal_clear_area(int, int, int, int);
28 	void terminal_crop(int, int, int, int);
29 	void terminal_refresh();
30 	void terminal_put(int, int, int);
31 	int terminal_pick(int, int, int);
32 	color_t terminal_pick_color(int, int, int);
33 	color_t terminal_pick_bkcolor(int, int);
34 	void terminal_put_ext(int, int, int, int, int, color_t*);
35 	void terminal_print8(int, int, const char*);
36 	void terminal_measure8(const char*);
37 	int terminal_state(int);
38 	int terminal_check(int);
39 	int terminal_has_input();
40 	int terminal_read();
41 	int terminal_peek();
42 	color_t terminal_read_str8(int, int, char*, int);
43 	void terminal_delay(int);
44 	color_t color_from_name8(const char*);
45 	color_t color_from_argb(byte, byte, byte, byte);
46 }
47 
48 // namespace called "terminal"
49 struct terminal { static {
50 
51 
52 	// Has to be inline, for now
53 	enum keycode {
54 		a = 0x04,
55 		b = 0x05,
56 		c = 0x06,
57 		d = 0x07,
58 		e = 0x08,
59 		f = 0x09,
60 		g = 0x0a,
61 		h = 0x0b,
62 		i = 0x0c,
63 		j = 0x0d,
64 		k = 0x0e,
65 		l = 0x0f,
66 		m = 0x10,
67 		n = 0x11,
68 		o = 0x12,
69 		p = 0x13,
70 		q = 0x14,
71 		r = 0x15,
72 		s = 0x16,
73 		t = 0x17,
74 		u = 0x18,
75 		v = 0x19,
76 		w = 0x1a,
77 		x = 0x1b,
78 		y = 0x1c,
79 		z = 0x1d,
80 		K_1 = 0x1E,
81 		K_2 = 0x1F,
82 		K_3 = 0x20,
83 		K_4 = 0x21,
84 		K_5 = 0x22,
85 		K_6 = 0x23,
86 		K_7 = 0x24,
87 		K_8 = 0x25,
88 		K_9 = 0x26,
89 		K_0 = 0x27,
90 		enter = 0x28,
91 		escape = 0x29,
92 		backspace = 0x2a,
93 		tab = 0x2b,
94 		space = 0x2c,
95 		minus = 0x2d /*  -  */,
96 		equals = 0x2e /*  =  */,
97 		lbracket = 0x2f /*  [  */,
98 		rbracket = 0x30 /*  ]  */,
99 		backslash = 0x31 /*  \  */,
100 		semicolon = 0x33 /*  ,  */,
101 		apostrophe = 0x34 /*  '  */,
102 		grave = 0x35 /*  `  */,
103 		comma = 0x36 /*  ,  */,
104 		period = 0x37 /*  .  */,
105 		slash = 0x38 /*  /  */,
106 		F1 = 0x3A,
107 		F2 = 0x3B,
108 		F3 = 0x3C,
109 		F4 = 0x3D,
110 		F5 = 0x3E,
111 		F6 = 0x3F,
112 		F7 = 0x40,
113 		F8 = 0x41,
114 		F9 = 0x42,
115 		F10 = 0x43,
116 		F11 = 0x44,
117 		F12 = 0x45,
118 		pause = 0x48 /* Pause/Break */,
119 		insert = 0x49,
120 		home = 0x4a,
121 		pageup = 0x4b,
122 		K_delete = 0x4c,
123 		end = 0x4d,
124 		pagedown = 0x4e,
125 		right = 0x4F /* Right arrow */,
126 		left = 0x50 /* Left arrow */,
127 		down = 0x51 /* Down arrow */,
128 		up = 0x52 /* Up arrow */,
129 		KP_divide = 0x54 /* '/' on numpad */,
130 		KP_multiply = 0x55 /* '*' on numpad */,
131 		KP_minus = 0x56 /* '-' on numpad */,
132 		KP_plus = 0x57 /* '+' on numpad */,
133 		KP_enter = 0x58,
134 		KP_1 = 0x59,
135 		KP_2 = 0x5A,
136 		KP_3 = 0x5B,
137 		KP_4 = 0x5C,
138 		KP_5 = 0x5D,
139 		KP_6 = 0x5E,
140 		KP_7 = 0x5F,
141 		KP_8 = 0x60,
142 		KP_9 = 0x61,
143 		KP_0 = 0x62,
144 		kp_period = 0x63 /* '.' on numpad */,
145 		shift = 0x70,
146 		control = 0x71,
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 TK_KEY_RELEASED:
168 		* a) pressed 'A': 0x04
169 		* b) released 'A': 0x04|VK_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 		bkcolor = 0xC5 /* Current background color */,
183 		layer = 0xC6 /* Current layer */,
184 		composition = 0xC7 /* Current composition state */,
185 		character = 0xC8 /* Translated ANSI code of last produced character */,
186 		wcharacter = 0xC9 /* Unicode codepoint of last produced character */,
187 		event = 0xCA /* Last dequeued event */,
188 		fullscreen = 0xCB /* Fullscreen state */,
189 
190 		/*
191 		* Other events
192 		*/
193 		close = 0xe0,
194 		resized = 0xe1,
195 
196 		/*
197 		* Generic mode enum.
198 		* Right now it is used for composition option only.
199 		*/
200 		off = 0,
201 		on = 1
202 	}
203 
204 	int open(string title="BearLibTerminal") { int c = terminal_open(); setf("window.title=%s", title); return c; };
205 	void close() { terminal_close(); };
206 	int set(string s) { return terminal_set8(toStringz(s)); };
207 	int setf(T...)(string s, T args) { return terminal_set8(format(s, args)); }
208 	void color(color_t clr) { terminal_color(clr); };
209 	void bkcolor(color_t clr) { terminal_bkcolor(clr); };
210 	void composition(int mode) { terminal_composition(mode); };
211 	void layer(int lyr) { terminal_layer(lyr); };
212 	void clear() { terminal_clear(); };
213 	void clear_area(int x, int y, int w, int h) { terminal_clear_area(x, y, w, h); };
214 	void crop(int x, int y, int w, int h) { terminal_crop(x, y, w, h); };
215 	void refresh() { terminal_refresh(); };
216 	void put(int x, int y, int code) { terminal_put(x, y, code); };
217 	int pick(int x, int y, int index) { return terminal_pick(x, y, index); };
218 	color_t pick_color(int x, int y, int index) { return terminal_pick_color(x, y, index); };
219 	color_t pick_bkcolor(int x, int y) { return terminal_pick_bkcolor(x, y); };
220 	void put_ext(int x, int y, int dx, int dy, int code, color_t *corners) { terminal_put_ext(x, y, dx, dy, code, corners); };
221 	void print(int x, int y, string s) { terminal_print8(x, y, toStringz(s)); };
222 	void printf(T...)(int x, int y, string s, T args) { terminal_print8(x, y, format(s, args)); };
223 	void measure(string s) { terminal_measure8(toStringz(s)); };
224 	void measuref(T...)(string s, T args) { terminal_measure8(format(s, args)); };
225 	int state(int slot) { return terminal_state(slot); };
226 	bool check(int slot) { return terminal_state(slot) > 0; };
227 	int has_input() { return terminal_has_input(); };
228 	int read() { return terminal_read(); };
229 	int peek() { return terminal_peek(); };
230 	string read_str(int x, int y, int max, string prompt="") {
231 		assert (prompt.length <= max);
232 		import std.conv: to;
233 		import core.stdc.stdlib: malloc, free;
234 
235 		print(x, y, prompt);
236 
237 		char[] buf = new char[](max);
238 		buf[] = 0;
239 
240 		string tmp;
241 
242 		terminal_read_str8(x+cast(int)prompt.length, y, buf.ptr, max);
243 		tmp = to!string(buf);
244 		delete buf;
245 		return tmp;
246 	};
247 	void delay(int period) { terminal_delay(period); };
248 	color_t color_from_name(string name) { return color_from_name8(toStringz(name)); };
249 }}