aboutsummaryrefslogtreecommitdiff
path: root/tio.jai
blob: 37c7e4a7c9745737d81baf0e8d5c430fc1ff2b64 (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

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;
		c_oflag : u32;
		c_cflag : u32;
		c_lflag : u32;
		unknown_pad : u8;
		c_cc : [32]u8;
		c_ispeed : u32;
		c_ospeed : u32;
	}

	libc :: #system_library "libc";
	tcsetattr :: (fd : s32, optional_actions : s32, termios_p : *My_Termios) -> s32 #foreign libc;
	tcgetattr :: (fd : s32, termios_p : *My_Termios) -> s32 #foreign libc;	
}
else {
    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;
}

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;
}

__old_logger : type_of(context.logger);

initialize :: () {
    #if OS == .LINUX {
        tcgetattr(STDIN_FILENO, *__term);
    } else {
		assert(false, "procedure call on unsupported OS\n");
    }
}

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

		tio_write("\e[?47l");	// restore screen
		tio_write("\e8");		// restore cursor
		tio_write("\e[?25h");	// show cursor
		tio_write("\e[?30h");	// show scrollbar
		terminal_state = .{};

		context.logger = __old_logger;		
	} else {
		assert(false, "procedure call on unsupported OS\n");
	}
}

tio_write :: (str : string) {
	#if OS == .LINUX {
        write(STDIN_FILENO, str.data, xx str.count);
    } else {
		assert(false, "procedure call on unsupported OS\n");
	}
}