PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/win/platform/application.d

http://github.com/wilkie/djehuty
D | 232 lines | 159 code | 51 blank | 22 comment | 72 complexity | 29f0e782a08a564bdc48c3dc85c7e229 MD5 | raw file
  1module platform.application;
  2
  3import platform.win.common;
  4
  5import core.arguments;
  6import core.string;
  7import core.unicode;
  8import core.main;
  9
 10import io.console;
 11
 12import binding.c;
 13
 14import analyzing.debugger;
 15
 16enum : uint
 17{
 18	OsVersionWindows95			= 0,
 19	OsVersionWindowsNT			= 1,
 20	OsVersionWindows98			= 2,
 21	OsVersionWindows98Se		= 3,
 22	OsVersionWindowsMe			= 4,
 23	OsVersionWindows2000		= 5,
 24	OsVersionWindowsXp			= 6,
 25	OsVersionWindowsServer2003	= 7,
 26	OsVersionWindowsVista		= 8,
 27	OsVersionWindowsLonghorn	= 9,
 28	OsVersionWindows7			= 10,
 29	OsVersionWindowsServer2008	= 11,
 30	OsVersionWindowsMax			= uint.max,
 31}
 32
 33//XXX:CommCtrl.h
 34extern(System) void InitCommonControls();
 35
 36// The main ApplicationController
 37class ApplicationController {
 38
 39	this() {
 40		// Read and formalize the command arguments
 41		parseCommandLine();
 42
 43		// some common initialization
 44		initCommon();
 45	}
 46
 47	void exitCode(uint value) {
 48		_exitCode = value;
 49	}
 50	
 51	uint exitCode() {
 52		return _exitCode;
 53	}
 54
 55	void start() {
 56	}
 57
 58	void end() {
 59		exit(_exitCode);
 60	}
 61
 62	static ApplicationController instance() {
 63		if (_app is null) {
 64			_app = new ApplicationController();
 65		}
 66
 67		return _app;
 68	}
 69
 70	// exposed
 71	int windowsVersion() {
 72		return win_osVersion;
 73	}
 74
 75private:
 76
 77	uint _exitCode;
 78
 79	int win_osVersion = -1;
 80
 81	static ApplicationController _app;
 82
 83	void getWindowsVersion() {
 84		//get windows version
 85		DWORD verRet = GetVersion();
 86
 87		OSVERSIONINFOEXW osvi = { 0 };
 88		BOOL bOsVersionInfoEx;
 89
 90		// Try calling GetVersionEx using the OSVERSIONINFOEX structure.
 91		// If that fails, try using the OSVERSIONINFO structure.
 92
 93		osvi.dwOSVersionInfoSize = OSVERSIONINFOEXW.sizeof;
 94
 95		win_osVersion = -1;
 96
 97		bOsVersionInfoEx = GetVersionExW (cast(OSVERSIONINFOW*)&osvi);
 98
 99		if( !bOsVersionInfoEx ) {
100			osvi.dwOSVersionInfoSize = OSVERSIONINFOW.sizeof;
101			if (! GetVersionExW ( cast(OSVERSIONINFOW*)&osvi) ) {
102				//error
103				//just try and assume an OS version
104				//lets say Win95, just in case
105				win_osVersion = OsVersionWindows95;
106			}
107		}
108
109		if (win_osVersion == -1) {
110			switch (osvi.dwPlatformId) {
111				// Test for the Windows NT product family.
112
113				case VER_PLATFORM_WIN32_NT:
114
115				// Test for the specific product.
116
117				if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1) {
118					if( osvi.wProductType == VER_NT_WORKSTATION ) {
119						win_osVersion = OsVersionWindows7;
120					}
121					else {
122						//will be Longhorn Server
123						win_osVersion = OsVersionWindowsServer2008;
124					}
125				}
126				else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 ) {
127					if( osvi.wProductType == VER_NT_WORKSTATION ) {
128						win_osVersion = OsVersionWindowsVista;
129					}
130					else {
131						//will be Longhorn Server
132						win_osVersion = OsVersionWindowsLonghorn;
133					}
134				}
135				else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) {
136					if( GetSystemMetrics(89) ) {
137						//Windows Server 2003
138						win_osVersion = OsVersionWindowsServer2003;
139					}
140					else if( osvi.wProductType == VER_NT_WORKSTATION ) {
141						//Windows XP x64
142						win_osVersion = OsVersionWindowsXp;
143					}
144					else {
145						//Windows Server 2003
146					}
147				}
148				else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
149					win_osVersion = OsVersionWindowsXp;
150				}
151				else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) {
152					win_osVersion = OsVersionWindows2000;
153				}
154				else if ( osvi.dwMajorVersion <= 4 ) {
155					win_osVersion = OsVersionWindowsNT;
156				}
157				break;
158	
159			// Test for the Windows Me/98/95.
160			case VER_PLATFORM_WIN32_WINDOWS:
161	
162			if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) {
163				win_osVersion = OsVersionWindows95;
164			}
165
166			if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) {
167				if ( osvi.szCSDVersion[1]=='A' || osvi.szCSDVersion[1]=='B') {
168					win_osVersion = OsVersionWindows98Se;
169				}
170				else {
171					win_osVersion = OsVersionWindows98;
172				}
173			}
174	
175			if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) {
176				win_osVersion = OsVersionWindowsMe;
177			}
178			break;
179	
180			case VER_PLATFORM_WIN32s:
181				win_osVersion = OsVersionWindows95;
182	
183			break;
184
185			default:
186				win_osVersion = OsVersionWindowsMax;
187			break;
188			}
189		}
190	}
191	
192	void parseCommandLine() {
193		wchar* cmdlne = GetCommandLineW();
194	
195		if (cmdlne is null) {
196			return;
197		}
198
199		Arguments args = Arguments.instance;
200	
201		// tokenize
202		int last = 0;
203	
204		for(int i = 0; ; i++) {
205			auto chr = cmdlne[i];
206
207			if (chr == ' ' || chr == '\t' || chr == '\n' || chr == '\0') {
208				if (last != i) {
209					string token = Unicode.toUtf8(cmdlne[last..i]);
210	
211					args.add(token);
212				}
213	
214				last = i+1;
215			}
216	
217			if (chr == '\0') {
218				break;
219			}
220		}
221	}
222
223	void initCommon() {
224		// set buffer to print without newline
225		setvbuf (stdout, null, _IONBF, 0);
226		//SetConsoleOutputCP(65001);
227
228		getWindowsVersion();
229		InitCommonControls();
230	}
231}
232