/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

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