aboutsummaryrefslogtreecommitdiff
path: root/tio.jai
blob: 9ccd04b33c701713225b244d6cccde9a095feda6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

#module_parameters(ENABLE_SIGINT := true, ENABLE_SIGQUIT := true);

#import "Basic";

// TODO Eventually, I'll need to add #scope_module ...

terminal_state : struct {
    // size : ivec2;
    width: s32;
    height: s32;
	// cursor := ivec2.{-1, -1}; // {-1, -1} if cursor hidden
    x: s32 = -1;
    y: s32 = -1;
	last_mode : Graphics_Mode;
}

#if OS == .LINUX {
    #import "POSIX";

	__term : My_Termios;

	My_Termios :: struct {
		c_iflag : u32;  // Input mode flags.
		c_oflag : u32;  // Output mode flags.
		c_cflag : u32;  // Control mode flags.
		c_lflag : u32;  // Local mode flags.
		c_line : u8;    // Line discipline.
		c_cc : [32]u8;  // Control characters.
		c_ispeed : u32; // Input speed.
		c_ospeed : u32; // Output speed.
	}

	libc :: #system_library "libc";

	// https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcsetattr.c.html
	tcsetattr :: (fd : s32, optional_actions : s32, termios_p : *My_Termios) -> s32 #foreign libc;

	// https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcgetattr.c.html
	tcgetattr :: (fd : s32, termios_p : *My_Termios) -> s32 #foreign libc;	
}
else #if OS == .WINDOWS {
	#run print("TODO tio\n", to_standard_error = true);
}
else {
    #run assert(false, "unsupported OS\n");
}

Graphics_Mode :: struct {
	foreground : Color;
	background : Color;
	attr_flags : enum_flags u8 {
		F_BOLD 			:: 0x1; 
		F_DIM 			:: 0x2; 
		F_ITALIC 		:: 0x4; 
		F_UNDERLINE 	:: 0x8; 
		F_BLINKING 		:: 0x10; 
		F_INVERSE 		:: 0x20; 
		F_STRIKETHROUGH :: 0x40; 
	}
	// attrs : [MAX_ATTRS]bool;
	// 0 - bold (on/off/keep)
	// 1 - dim/faint
	// 2 - italic
	// 3 - underline
	// 4 - blinking
	// 5 - inverse
	// 6 - strikethrough
	fcol256 : u8;
	bcol256 : u8;
}

Key :: enum u64 {
	READ_ERROR		:: 0xffffffff_ffffffff;

	UP				:: 0x41_5b1b;
	DOWN			:: 0x42_5b1b;
	RIGHT			:: 0x43_5b1b;
	LEFT			:: 0x44_5b1b;

	CTRL_UP 		:: 0x4135_3b315b1b;
	CTRL_DOWN 		:: 0x4235_3b315b1b;
	CTRL_RIGHT 		:: 0x4335_3b315b1b;
	CTRL_LEFT 		:: 0x4435_3b315b1b;

	SHIFT_UP		:: 0x4132_3b315b1b;
	SHIFT_DOWN		:: 0x4232_3b315b1b;
	SHIFT_RIGHT		:: 0x4332_3b315b1b;
	SHIFT_LEFT		:: 0x4432_3b315b1b;

	CTRL_SHIFT_UP	:: 0x4136_3b315b1b;
	CTRL_SHIFT_DOWN	:: 0x4236_3b315b1b;
	CTRL_SHIFT_RIGHT:: 0x4336_3b315b1b;
	CTRL_SHIFT_LEFT	:: 0x4436_3b315b1b;

	ALT_UP			:: 0x4133_3b315b1b;
	ALT_DOWN		:: 0x4233_3b315b1b;
	ALT_RIGHT		:: 0x4333_3b315b1b;
	ALT_LEFT		:: 0x4433_3b315b1b;

	CTRL_C			:: 0x03;
	CTRL_V			:: 0x16;
	CTRL_X			:: 0x18;
	CTRL_Y			:: 0x19;
	CTRL_Z			:: 0x1A;
	CTRL_BACKSLASH	:: 0x1C;

	// ALT_SHIFT_UP	:: 0x4133_3b315b1b;
	// ALT_SHIFT_DOWN	:: 0x4233_3b315b1b;
	// ALT_SHIFT_RIGHT	:: 0x4333_3b315b1b;
	// ALT_SHIFT_LEFT	:: 0x4433_3b315b1b;

	ENTER			:: 0x0D;
	ESCAPE			:: 0x1B;
	BACKSPACE		:: 0x7F;
	DELETE			:: 0x7E335B1B;
}

Color :: enum u8 {
	RESET 			:: 0;
	DEFAULT 		:: 39;
	COLOR256		:: 38;

	BLACK 			:: 30;
	RED 			:: 31;
	GREEN 			:: 32;
	YELLOW 			:: 33;
	BLUE 			:: 34;
	MAGENTA 		:: 35;
	CYAN 			:: 36;
	WHITE 			:: 37;

	BRIGHT_BLACK 	:: 90;
	BRIGHT_RED 		:: 91;
	BRIGHT_GREEN 	:: 92;
	BRIGHT_YELLOW 	:: 93;
	BRIGHT_BLUE 	:: 94;
	BRIGHT_MAGENTA 	:: 95;
	BRIGHT_CYAN 	:: 96;
	BRIGHT_WHITE 	:: 97;
}

update_terminal_size :: () {
	#if OS == .LINUX {
		TIOCGWINSZ :: 0x5413;
		winsize : struct {
			ws_row, ws_col, ws_xpixel, ws_ypixel : u16;
		}
		ioctl(0, TIOCGWINSZ, *winsize);
		terminal_state.width = xx winsize.ws_col;
		terminal_state.height = xx winsize.ws_row;
	}
	else #if OS == .WINDOWS {
		print("TODO tio\n", to_standard_error = true);
	}
	else {
		assert(false, "unsupported OS\n");
	}
}

initialize :: () {
    #if OS == .LINUX {

		tui_write("\e[?25l");	// hide cursor
		tui_write("\e7");		// save cursor position
		tui_write("\e[?1047h");	// switch screen
		tui_write("\e[?30l");	// hide scrollbar

		tui_write("\e[H"); 		// move to top left corner
		tui_write("\e[0m");		// set default mode
		tui_write("\e[2J"); 		// clear screen
		{
			tcgetattr(STDIN_FILENO, *__term);
			term_new := __term;

			term_new.c_iflag &= 0xFFFFFA14;// ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
			term_new.c_oflag &= 0xFFFFFFFE;// ~OPOST;
			term_new.c_lflag &= 0xFFFF7FB4;// ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
			term_new.c_cflag &= 0xFFFFFECF;// ~(CSIZE | PARENB);
			term_new.c_cflag |= 0x00000030;

			tcsetattr(STDIN_FILENO, 0, *term_new);	
		}
		update_terminal_size();
	}
	else #if OS == .WINDOWS {
		print("TODO tio\n", to_standard_error = true);
	}
	else {
		assert(false, "unsupported OS\n");
	}
}

terminate :: () {
	#if OS == .LINUX {
		tcsetattr(STDIN_FILENO, 0, *__term); // return echo

		tui_write("\e[?47l");	// restore screen
		tui_write("\e8");		// restore cursor
		tui_write("\e[?25h");	// show cursor
		tui_write("\e[?30h");	// show scrollbar
		terminal_state = .{};
	}
	else #if OS == .WINDOWS {
		print("TODO tio\n", to_standard_error = true);
	}
	else {
		assert(false, "unsupported OS\n");
	}
}

tui_write :: (str : string) {
	#if OS == .LINUX {
        write(STDIN_FILENO, str.data, xx str.count);
    }
	else #if OS == .WINDOWS {
		#run print("TODO tio\n", to_standard_error = true);
	}
	else {
		#run assert(false, "unsupported OS\n");
	}
}

buffer: [..] Key;

tui_ungetch :: (key: Key) {
    array_add(*buffer, key);
}

tui_getch :: (block := true) -> Key {
	#if OS == .LINUX {
		buf : Key = xx 0;
		if buffer.count > 0 return pop(*buffer);
		l := read(STDIN_FILENO, (cast(*u8)*buf), 8); //!!!
		check_signal :: inline (key : Key) {
			#if ENABLE_SIGINT if key == .CTRL_C raise(SIGINT); //!!!
			#if ENABLE_SIGQUIT if key == .CTRL_BACKSLASH raise(SIGQUIT);
		}
		check_signal(buf);
		return ifx l <= 0 then Key.READ_ERROR else buf;		
	}
	else #if OS == .WINDOWS {
		print("TODO tio\n", to_standard_error = true);
		return .READ_ERROR;
	}
	else {
		assert(false, "unsupported OS\n");
		return .READ_ERROR;
	}
}

tui_clear_screen :: inline () {	
	tui_write("\e[2J");
}