summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Src/ConsoleWindow.cpp
blob: 9e6404b9fe01fed6c6ac683de4b26687c4c3ffde (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
// Copyright (C) 2003-2008 Dolphin Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/


// Includes
#include <string> // System: To be able to add strings with "+"
#include <stdio.h>
#ifdef _WIN32
	#include <windows.h>
#else
#include <stdarg.h>
#endif

#include "Common.h"
#include "ConsoleWindow.h" // Common


// Declarations and definitions

namespace Console
{

// Create handles
	FILE* __fStdOut = NULL;
#ifdef _WIN32
	HANDLE __hStdOut = NULL;
#endif


/* Start console window - width and height is the size of console window, if you enable
   File the output will also be written to this file. */
void Open(int Width, int Height, char * Name, bool File)
{
#ifdef _WIN32
	// Open the console window and create the window handle for GetStdHandle()
	AllocConsole();

	// Save the window handle that AllocConsole() created
	__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

	// Set the console window title
	SetConsoleTitle(Name);

	// Set the total letter space
	COORD co = {Width, Height};
	SetConsoleScreenBufferSize(__hStdOut, co);

	/* Set the window size in number of letters. The height is hardcoded here because it can
	   be changed with MoveWindow() later */
	SMALL_RECT coo = {0,0, (Width - 1),50}; // Top, left, right, bottom
	SetConsoleWindowInfo(__hStdOut, TRUE, &coo);


#endif
	// Create a file and a file handle if File is enabled and we don't already have a file handle
	if(File && !__fStdOut)
	{
		// Edit the log file name
		std::string FileEnding = ".log";
		std::string FileName = Name;	
		std::string FullFilename = (FileName + FileEnding);

		// Open the file handle
		__fStdOut = fopen(FullFilename.c_str(), "w");
	}

}


/* Close the console window and close the eventual file handle */
void Close()
{
#ifdef _WIN32
	FreeConsole(); // Close the console window
#endif
	if(__fStdOut) fclose(__fStdOut); // Close the file handle

}


// Print to screen and file
int Print(const char *fmt, ...)
{
#if defined(_WIN32)
	if(__hStdOut)
	{
#endif
		char s[1024*20]; // Warning, mind this value
		va_list argptr;
		int cnt; // To store the vsnprintf return message

		va_start(argptr, fmt);
		cnt = vsnprintf(s, 500, fmt, argptr);
		va_end(argptr);


#if defined(_WIN32)
		DWORD cCharsWritten; // We will get a value back here

		WriteConsole(__hStdOut, s, (DWORD)strlen(s), &cCharsWritten, NULL);
#else
		fprintf(stderr, "%s", s);
#endif
		// Write to the file
		if(__fStdOut)
		{
		    fprintf(__fStdOut, "%s", s);
		    fflush(__fStdOut); // Write file now, don't wait
		}

		return(cnt);
	
#if defined(_WIN32) 
	} else
	{
		return 0;
	}
#endif

}


// Clear console screen
void ClearScreen() 
{ 
#if defined(_WIN32)
	if(__hStdOut) // Check that we have a window handle
	{
		COORD coordScreen = { 0, 0 }; 
		DWORD cCharsWritten; 
		CONSOLE_SCREEN_BUFFER_INFO csbi; 
		DWORD dwConSize; 

		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
	    
		GetConsoleScreenBufferInfo(hConsole, &csbi); 
		dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
		FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, 
			coordScreen, &cCharsWritten); 
		GetConsoleScreenBufferInfo(hConsole, &csbi); 
		FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, 
			coordScreen, &cCharsWritten); 
		SetConsoleCursorPosition(hConsole, coordScreen); 
	}
#endif
}


/* Get window handle of console window to be able to resize it. We use
   GetConsoleTitle() and FindWindow() to locate the console window handle. */
#if defined(_WIN32)
HWND GetHwnd(void)
{
   #define MY_BUFSIZE 1024 // Buffer size for console window titles
   HWND hwndFound;         // This is what is returned to the caller
   char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle
   char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle

   // Fetch current window title.
   GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);

   // Format a "unique" NewWindowTitle
   wsprintf(pszNewWindowTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());

   // Change current window title
   SetConsoleTitle(pszNewWindowTitle);

   // Ensure window title has been updated
   Sleep(40);

   // Look for NewWindowTitle
   hwndFound = FindWindow(NULL, pszNewWindowTitle);

   // Restore original window title
   SetConsoleTitle(pszOldWindowTitle);

   return(hwndFound);
}
#endif // _WIN32

} // namespace