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