fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
ce51c77
v2.1.4
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 ( 39DWORD dwProcessId 40); 41 42HANDLE GetConsoleWindow ( void ); 43BOOL AllocConsole ( void ); 44BOOL FreeConsole ( void ); 45 46BOOL ShowWindow ( 47HANDLE hWnd , 48int nCmdShow 49); 50 51HANDLE GetStdHandle ( 52DWORD nStdHandle 53); 54 55BOOL SetConsoleTextAttribute ( 56HANDLE hConsoleOutput , 57WORD wAttributes 58); 59 60BOOL SetConsoleMode ( 61HANDLE hConsoleHandle , 62DWORD dwMode 63); 64 65BOOL GetConsoleMode ( 66HANDLE hConsoleHandle , 67LPDWORD lpMode 68); 69 70BOOL SetConsoleTitleA ( 71LPCTSTR lpConsoleTitle 72); 73 74DWORD GetLastError (); 75 76void * freopen ( 77const char * path , 78const char * mode , 79void * stream 80); 81 82int fclose ( void * stream ); 83]] 84 85function love . setConsoleTitle ( title ) 86kernel . SetConsoleTitleA ( title ) 87end 88 89function love . enableConsoleFlag ( flag ) 90local hStdOut =kernel . GetStdHandle ( STD_OUTPUT_HANDLE ) 91 92if ( hStdOut == INVALID_HANDLE_VALUE ) then 93return false , "could not get stdhandle for console." 94end 95 96local mode =ffi . new ( "DWORD[1]" ) 97kernel . GetConsoleMode ( hStdOut , mode ) 98kernel . SetConsoleMode ( hStdOut , bit . bor ( mode [ 0 ], flag )) 99return true 100end 101 102function love . enableConsoleColors () 103love . enableConsoleFlag ( ENABLE_VIRTUAL_TERMINAL_PROCESSING ) 104end 105 106function love . hasConsole () 107kernel . AttachConsole ( ATTACH_PARENT_PROCESS ) 108return kernel . GetLastError () == ERROR_ACCESS_DENIED 109end 110 111function love . createConsole () 112if kernel . AttachConsole ( ATTACH_PARENT_PROCESS ) == 0 then 113local winerr =kernel . GetLastError () 114 115if winerr == ERROR_ACCESS_DENIED then 116return false , "console is already open." 117end 118 119if not kernel . AllocConsole () then 120return false , "could not create console." 121end 122end 123 124local fp =ffi . C . freopen ( "CONOUT$" , "w" , io . stdout ) 125 126if fp == nil then 127return false , "console redirection of stdout failed." 128end 129 130local fp =ffi . C . freopen ( "CONOUT$" , "r" , io . stdin ) 131 132if fp == nil then 133return false , "console redirection of stdin failed." 134end 135 136local fp =ffi . C . freopen ( "CONOUT$" , "w" , io . stderr ) 137 138if fp == nil then 139return false , "console redirection of stderr failed." 140end 141 142local hStdOut =kernel . GetStdHandle ( STD_OUTPUT_HANDLE ) 143 144if ( hStdOut == INVALID_HANDLE_VALUE ) then 145return false , "could not get stdhandle for console." 146end 147 148--local MAX_CONSOLE_LINES = 5000 149return true 150end 151 152function love . console ( opened ) 153if not love . hasConsole () then 154love . createConsole () 155end 156local chdl =kernel . GetConsoleWindow () 157if not chdl then return end 158if opened then 159love . setConsoleTitle ( "M'Overlay Console" ) 160love . enableConsoleColors () 161user . ShowWindow ( chdl , SW_SHOW ) 162else 163user . ShowWindow ( chdl , SW_HIDE ) 164end 165end 166 167if love . hasConsole () then 168love . setConsoleTitle ( "M'Overlay Console" ) 169love . enableConsoleColors () 170end