fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosImplemented a way to customize music file weights (#62/#119) Fixed some issues with the GUI backend.ce51c77

v2.1.1a
3.3 KiB170 linesraw
1if jit.os ~= "Windows" then return end
2
3local ffi = require("ffi")
4local log = require("log")
5local kernel = ffi.load("Kernel32.dll")
6local user = ffi.load("User32.dll")
7
8io.stdout:setvbuf("no")
9
10local INVALID_HANDLE_VALUE = -1
11
12local STD_INPUT_HANDLE  = -10
13local STD_OUTPUT_HANDLE = -11
14local STD_ERROR_HANDLE  = -12
15
16local ENABLE_PROCESSED_OUTPUT = 0x0001
17local ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
18
19local ATTACH_PARENT_PROCESS = 0x1
20
21local ERROR_ACCESS_DENIED = 0x5
22
23local SW_HIDE = 0x0
24local SW_SHOW = 0x5
25
26ffi.cdef [[
27// Windows types
28typedef int BOOL;
29typedef char CHAR;
30typedef unsigned long DWORD;
31typedef void *PVOID;
32typedef PVOID HANDLE;
33typedef unsigned short WORD;
34typedef const CHAR *LPCSTR;
35typedef LPCSTR LPCTSTR;
36typedef DWORD *LPDWORD;
37
38BOOL AttachConsole(
39  DWORD dwProcessId
40);
41
42HANDLE GetConsoleWindow(void);
43BOOL AllocConsole(void);
44BOOL FreeConsole(void);
45
46BOOL ShowWindow(
47  HANDLE hWnd,
48  int  nCmdShow
49);
50
51HANDLE GetStdHandle(
52  DWORD nStdHandle
53);
54
55BOOL SetConsoleTextAttribute(
56  HANDLE hConsoleOutput,
57  WORD   wAttributes
58);
59
60BOOL SetConsoleMode(
61  HANDLE hConsoleHandle,
62  DWORD  dwMode
63);
64
65BOOL GetConsoleMode(
66  HANDLE  hConsoleHandle,
67  LPDWORD lpMode
68);
69
70BOOL SetConsoleTitleA(
71  LPCTSTR lpConsoleTitle
72);
73
74DWORD GetLastError();
75
76void *freopen(
77   const char *path,
78   const char *mode,
79   void *stream
80);
81
82int fclose(void *stream);
83]]
84
85function love.setConsoleTitle(title)
86	kernel.SetConsoleTitleA(title)
87end
88
89function love.enableConsoleFlag(flag)
90	local hStdOut = kernel.GetStdHandle(STD_OUTPUT_HANDLE)
91
92	if (hStdOut == INVALID_HANDLE_VALUE) then
93		return false, "could not get stdhandle for console."
94	end
95
96	local mode = ffi.new("DWORD[1]")
97	kernel.GetConsoleMode(hStdOut, mode)
98	kernel.SetConsoleMode(hStdOut, bit.bor(mode[0], flag))
99	return true
100end
101
102function love.enableConsoleColors()
103	love.enableConsoleFlag(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
104end
105
106function love.hasConsole()
107	kernel.AttachConsole(ATTACH_PARENT_PROCESS)
108	return kernel.GetLastError() == ERROR_ACCESS_DENIED
109end
110
111function love.createConsole()
112	if kernel.AttachConsole(ATTACH_PARENT_PROCESS) == 0 then
113		local winerr = kernel.GetLastError()
114
115		if winerr == ERROR_ACCESS_DENIED then
116			return false, "console is already open."
117		end
118
119		if not kernel.AllocConsole() then
120			return false, "could not create console."
121		end
122	end
123
124	local fp = ffi.C.freopen("CONOUT$", "w", io.stdout)
125
126	if fp == nil then
127		return false, "console redirection of stdout failed."
128	end
129
130	local fp = ffi.C.freopen("CONOUT$", "r", io.stdin)
131
132	if fp == nil then
133		return false, "console redirection of stdin failed."
134	end
135
136	local fp = ffi.C.freopen("CONOUT$", "w", io.stderr)
137
138	if fp == nil then
139		return false, "console redirection of stderr failed."
140	end
141
142	local hStdOut = kernel.GetStdHandle(STD_OUTPUT_HANDLE)
143
144	if (hStdOut == INVALID_HANDLE_VALUE) then
145		return false, "could not get stdhandle for console."
146	end
147
148	--local MAX_CONSOLE_LINES = 5000
149	return true
150end
151
152function love.console(opened)
153	if not love.hasConsole() then
154		love.createConsole()
155	end
156	local chdl = kernel.GetConsoleWindow()
157	if not chdl then return end
158	if opened then
159		love.setConsoleTitle("M'Overlay Console")
160		love.enableConsoleColors()
161		user.ShowWindow(chdl, SW_SHOW)
162	else
163		user.ShowWindow(chdl, SW_HIDE)
164	end
165end
166
167if love.hasConsole() then
168	love.setConsoleTitle("M'Overlay Console")
169	love.enableConsoleColors()
170end