Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.6 KiB

  1. // vidacc.cpp : Video acceleration manipulation tool.
  2. //
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. #include "vidacc.h"
  6. #include <devguid.h>
  7. #include <setupapi.h>
  8. #include <regstr.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. using namespace std;
  15. /////////////////////////////////////////////////////////////////////////////
  16. // The one and only application object
  17. inline
  18. void
  19. CDrvchkApp::PrintOut (LPCSTR str)
  20. {
  21. if (m_logf)
  22. fprintf (m_logf, "%s", str);
  23. else
  24. cerr << str;
  25. }
  26. inline
  27. void
  28. CDrvchkApp::PrintOut (unsigned num)
  29. {
  30. if (m_logf)
  31. fprintf (m_logf, "%d", num);
  32. else
  33. cerr << num;
  34. }
  35. int __cdecl _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  36. {
  37. int nRetCode = 0;
  38. // cerr << ::GetCommandLine() << endl;
  39. // initialize MFC and print and error on failure
  40. if (AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  41. {
  42. CDrvchkApp theApp;
  43. theApp.InitInstance ();
  44. }
  45. return nRetCode;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CDrvchkApp construction
  49. CDrvchkApp::CDrvchkApp() :
  50. m_logf(NULL),
  51. m_drv_name ("")
  52. {
  53. // TODO: add construction code here,
  54. // Place all significant initialization in InitInstance
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // The one and only CDrvchkApp object
  58. /*------------------------------------------------------------------------
  59. vchk /drv driver.dll /log logname.log /allow videoptr.sys
  60. /allowed_modules module1.sys FnName1 FnName2 FnName3 /allowed_modules module2.dll FnName4
  61. ------------------------------------------------------------------------*/
  62. void
  63. CommandLine::ParseParam( LPCTSTR lpszParam, BOOL bFlag, BOOL bLast )
  64. {
  65. if (m_parse_error)
  66. return;
  67. CString param (lpszParam);
  68. if (bFlag) {
  69. param.MakeUpper();
  70. if (m_last_flag.GetLength()) {
  71. m_parse_error = TRUE;
  72. m_error_msg = CString("Flag ") + m_last_flag + CString(" requires a parameter.");
  73. } else if ((param==CString("ACC")) || (param==CString("LOG")) || (param==CString("MON"))) {
  74. m_last_flag = param;
  75. m_first_param = TRUE;
  76. } else if (param==CString("?")) {
  77. m_help = TRUE;
  78. m_last_flag="";
  79. } else {
  80. m_last_flag = "";
  81. m_parse_error = TRUE;
  82. m_error_msg = CString("Unrecognized flag: ") + param;
  83. }
  84. } else {
  85. if ((m_last_flag==CString("ACC"))) {
  86. DWORD acc_lvl = atoi(param);
  87. if ((acc_lvl==0) && !(param==CString("0"))) {
  88. m_error_msg = "bad command line: /ACC flag has wrong parameter";
  89. m_parse_error = TRUE;
  90. }
  91. else if (acc_lvl>ACC_DISABLED)
  92. m_acc_level=ACC_DISABLED;
  93. else
  94. m_acc_level=acc_lvl;
  95. m_last_flag = "";
  96. } else if (m_last_flag==CString("MON")) {
  97. if (param.GetLength()==1) {
  98. char c = ((LPCSTR)param)[0];
  99. m_monitor = c - '1';
  100. } else {
  101. m_monitor = -1;
  102. m_error_msg = "bad command line: /MON flag has wrong parameter";
  103. m_parse_error = TRUE;
  104. }
  105. m_last_flag="";
  106. } else if (m_last_flag==CString("LOG")) {
  107. m_log_fname = param;
  108. m_last_flag="";
  109. } else {
  110. m_parse_error = TRUE;
  111. m_error_msg = CString("Wrong parameter: ") + param;
  112. m_last_flag="";
  113. }
  114. }
  115. if (bLast) {
  116. if (m_last_flag==CString("ACC")) {
  117. m_parse_error = TRUE;
  118. m_error_msg = CString("Flag ") + m_last_flag + CString(" requires a parameter.");
  119. }
  120. }
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CDrvchkApp initialization
  124. BOOL CDrvchkApp::InitInstance()
  125. {
  126. // Standard initialization
  127. // If you are not using these features and wish to reduce the size
  128. ParseCommandLine (m_cmd_line);
  129. if (m_cmd_line.m_help) {
  130. PrintOut ("\nVIDACC [/ACC level] [/MON monitor] [/LOG logfile] [/?]\n\n");
  131. PrintOut (" /ACC explicitely defines acceleration level\n\n");
  132. PrintOut (" level a digit from 0 to 5, 0 means full acceleration,\n"
  133. " and 5 (default) disables acceleration at all.\n\n");
  134. PrintOut (" monitor monitor number (if running on multimon)\n\n");
  135. PrintOut (" logfile a file to dump the output messages\n\n");
  136. return TRUE;
  137. }
  138. m_os_ver_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  139. GetVersionEx (&m_os_ver_info);
  140. if (m_os_ver_info.dwPlatformId != VER_PLATFORM_WIN32_NT) { // doesn't work on Win9x
  141. PrintOut ("warning: unsupported OS (Win9x), nothing done.\n");
  142. return FALSE;
  143. }
  144. if (m_os_ver_info.dwMajorVersion<5) { // doesn't work on NT version prior to Win2K
  145. PrintOut ("warning: unsupported OS (");
  146. PrintOut (m_os_ver_info.dwMajorVersion);
  147. PrintOut (".");
  148. PrintOut (m_os_ver_info.dwMinorVersion);
  149. PrintOut ("): nothing done.\n");
  150. return FALSE;
  151. }
  152. if (m_cmd_line.m_log_fname.GetLength()) {
  153. m_logf = fopen (m_cmd_line.m_log_fname, "a+");
  154. }
  155. if (m_cmd_line.m_parse_error) {
  156. PrintOut ("error: ");
  157. PrintOut ((LPCSTR)m_cmd_line.m_error_msg);
  158. PrintOut ("\n");
  159. } else {
  160. //
  161. // Let's find all the video drivers that are installed in the system
  162. //
  163. DISPLAY_DEVICE DisplayDevice;
  164. DisplayDevice.cb = sizeof (DisplayDevice);
  165. // cerr << "looking for device #" << m_cmd_line.m_monitor << endl;
  166. for (DWORD d=0, index=0; EnumDisplayDevices(NULL, index, &DisplayDevice, 0); index++) {
  167. // cerr << "device #" << d << endl;
  168. if (DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) {
  169. // cerr << "DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER\n";
  170. continue;
  171. }
  172. if (m_cmd_line.m_monitor!=d++) {
  173. // cerr << "m_cmd_line.m_monitor!=d\n";
  174. continue;
  175. }
  176. HKEY hKey = NULL;
  177. TCHAR device_key[256]; // Name of service (drivers)
  178. // cerr << DisplayDevice.DeviceKey << endl;
  179. _tcscpy (device_key, DisplayDevice.DeviceKey+18); // chop "\Registry\Machine\" off
  180. // cerr << device_key << endl;
  181. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,device_key,(DWORD)0,KEY_ALL_ACCESS,&hKey)==ERROR_SUCCESS) {
  182. RegSetValueEx(hKey,
  183. _TEXT("Acceleration.Level"),
  184. 0,
  185. REG_DWORD,
  186. (CONST BYTE *)&m_cmd_line.m_acc_level,
  187. sizeof(DWORD));
  188. }
  189. ChangeDisplaySettings(NULL, 0);
  190. switch (m_cmd_line.m_acc_level) {
  191. case 0:
  192. PrintOut ("Level 0: Full acceleration enabled\n");
  193. break;
  194. case 1:
  195. PrintOut ("Level 1: Only cursor and birmap acceleration disabled\n");
  196. break;
  197. case 2:
  198. PrintOut ("Level 2: Cursor and advanced drawing acceleration disabled\n");
  199. break;
  200. case 3:
  201. PrintOut ("Level 3: DirectX, cursor and advanced drawing acceleration disabled\n");
  202. break;
  203. case 4:
  204. PrintOut ("Level 4: All but basic accelerations disabled\n");
  205. break;
  206. case 5:
  207. PrintOut ("Level 5: All accelerations disabled\n");
  208. break;
  209. }
  210. if (hKey)
  211. RegCloseKey(hKey);
  212. } // for each device
  213. } // if no cmd line error
  214. if (m_logf)
  215. fclose (m_logf);
  216. m_logf = NULL;
  217. return TRUE;
  218. }