Debian Codegraph @

tools/nds_firmware_tool/src/console.cpp

raw
1 /* NDS Firmware tools
2
3 Copyright 2009 DeSmuME team
4
5 This file is part of DeSmuME
6
7 DeSmuME is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 DeSmuME is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with DeSmuME; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "console.h"
23 #include "main.h"
24
25 ///////////////////////////////////////////////////////////////// Console
26 #ifdef DEVEL_VERSION
27 #include <windows.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <io.h>
31
32 #define BUFFER_SIZE 100
33 HANDLE hConsole;
34 void OpenConsole()
35 {
36 COORD csize;
37 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
38 SMALL_RECT srect;
39 char buf[256];
40
41 //dont do anything if we're already attached
42 if (hConsole) return;
43
44 //attach to an existing console (if we can; this is circuitous because AttachConsole wasnt added until XP)
45 //remember to abstract this late bound function notion if we end up having to do this anywhere else
46 bool attached = false;
47 HMODULE lib = LoadLibrary("kernel32.dll");
48 if(lib)
49 {
50 typedef BOOL (WINAPI *_TAttachConsole)(DWORD dwProcessId);
51 _TAttachConsole _AttachConsole = (_TAttachConsole)GetProcAddress(lib,"AttachConsole");
52 if(_AttachConsole)
53 {
54 if(_AttachConsole(-1))
55 attached = true;
56 }
57 FreeLibrary(lib);
58 }
59
60 //if we failed to attach, then alloc a new console
61 if(!attached)
62 {
63 AllocConsole();
64 }
65
66 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
67
68 //redirect stdio
69 long lStdHandle = (long)hConsole;
70 int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
71 if(hConHandle == -1)
72 return; //this fails from a visual studio command prompt
73
74 #if 1
75 FILE *fp = _fdopen( hConHandle, "w" );
76 #else
77 FILE *fp = fopen( "c:\\desmume.log", "w" );
78 #endif
79 *stdout = *fp;
80 //and stderr
81 *stderr = *fp;
82
83 memset(buf,0,256);
84 sprintf(buf,"%s OUTPUT", _TITLE);
85 SetConsoleTitle(TEXT(buf));
86 csize.X = 60;
87 csize.Y = 800;
88 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), csize);
89 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
90 srect = csbiInfo.srWindow;
91 srect.Right = srect.Left + 99;
92 srect.Bottom = srect.Top + 64;
93 SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &srect);
94 SetConsoleCP(GetACP());
95 SetConsoleOutputCP(GetACP());
96 if(attached) printf("\n");
97 printf("%s\n",_TITLE);
98 printf("- compiled: %s %s\n\n",__DATE__,__TIME__);
99 }
100
101 void CloseConsole()
102 {
103 if (hConsole == NULL) return;
104 INFO("Closing...");
105 FreeConsole();
106 hConsole = NULL;
107 }
108 #else
109 void OpenConsole() {}
110 void CloseConsole() {}
111 #endif