aboutsummaryrefslogtreecommitdiff
path: root/core/terminal/terminal_windows.odin
blob: 6c77330b55637fa2a8afcfd5f362eb7895a2d279 (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
#+private
package terminal

import "base:runtime"
import "core:os"
import "core:sys/windows"

_is_terminal :: proc "contextless" (f: ^os.File) -> bool {
	return os.is_tty(f)
}

old_modes: [2]struct{
	handle: windows.DWORD,
	mode: windows.DWORD,
} = {
	{windows.STD_OUTPUT_HANDLE, 0},
	{windows.STD_ERROR_HANDLE, 0},
}

@(init)
_init_terminal :: proc "contextless" () {
	vtp_enabled: bool

	for &v in old_modes {
		handle := windows.GetStdHandle(v.handle)
		if handle == windows.INVALID_HANDLE || handle == nil {
			return
		}
		if windows.GetConsoleMode(handle, &v.mode) {
			windows.SetConsoleMode(handle, v.mode | windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)

			new_mode: windows.DWORD
			windows.GetConsoleMode(handle, &new_mode)

			if new_mode & (windows.ENABLE_PROCESSED_OUTPUT | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0 {
				vtp_enabled = true
			}
		}
	}

	if vtp_enabled {
		// This color depth is available on Windows 10 since build 10586.
		color_depth = .Four_Bit
	} else {
		context = runtime.default_context()

		// The user may be on a non-default terminal emulator.
		color_depth = get_environment_color()
	}
}

@(fini)
_fini_terminal :: proc "contextless" () {
	for v in old_modes {
		handle := windows.GetStdHandle(v.handle)
		if handle == windows.INVALID_HANDLE || handle == nil {
			return
		}
		
		windows.SetConsoleMode(handle, v.mode)
	}
}