Source code of Windows XP (NT5)
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.

521 lines
11 KiB

  1. #include "Quickres.h"
  2. #include "tchar.h"
  3. extern HINSTANCE hInstApp;
  4. extern LPQRMONITORINFO pMonitors;
  5. extern INT iMonitors;
  6. extern WORD QuickResFlags;
  7. extern WORD FreqMenuLocation;
  8. #ifdef SAVEFLAGS
  9. //
  10. //****************************************************************************
  11. //
  12. // CreateQuickResKey( )
  13. //
  14. // Create 'Quickres' key in the registry if it doesnt exist
  15. //
  16. //****************************************************************************
  17. //
  18. BOOL CreateQuickResKey( )
  19. {
  20. HKEY hKeyOpen; // key we try to open
  21. HKEY hKeyCreate; // key to create: Quickres
  22. DWORD RegReturn; // for reg APIs
  23. DWORD Disposition;
  24. INT i; // counter
  25. BOOL bRet = FALSE; // return value
  26. //
  27. // Get the key which the QuickRes key will go under
  28. //
  29. if( (RegReturn = RegOpenKeyEx(HKEY_CURRENT_USER,
  30. REGSTR_SOFTWARE,
  31. 0,
  32. KEY_CREATE_SUB_KEY,
  33. &hKeyOpen)) == ERROR_SUCCESS )
  34. {
  35. //
  36. // Create my quickres key
  37. //
  38. if (RegReturn=RegCreateKeyEx(hKeyOpen,
  39. QUICKRES_KEY,
  40. 0,
  41. 0,
  42. REG_OPTION_NON_VOLATILE,
  43. KEY_SET_VALUE,
  44. NULL,
  45. &hKeyCreate,
  46. &Disposition) == ERROR_SUCCESS)
  47. {
  48. bRet = TRUE;
  49. }
  50. RegCloseKey(hKeyOpen);
  51. }
  52. return bRet;
  53. }
  54. //
  55. //****************************************************************************
  56. //
  57. // ReadRegistryValue( LPTSTR, PDWORD, PVOID, PDWORD)
  58. //
  59. // Read a quickres value from the registry (either modes, flags or BPP)
  60. //
  61. //****************************************************************************
  62. //
  63. BOOL ReadRegistryValue( LPTSTR ValueName, PDWORD KeyType, PVOID Value, PDWORD RegKeySize )
  64. {
  65. HKEY hKeyOpen; // Reg Key with mode flags
  66. LONG RegReturn; // ret value for reg APIs
  67. BOOL ret=FALSE; // return value
  68. //
  69. // Try to open the quickres key
  70. //
  71. if( (RegReturn=RegOpenKeyEx(HKEY_CURRENT_USER,
  72. REGSTR_QUICKRES,
  73. 0,
  74. KEY_QUERY_VALUE,
  75. &hKeyOpen)) == ERROR_SUCCESS )
  76. {
  77. //
  78. // Try to get the value
  79. //
  80. if ( (RegReturn=RegQueryValueEx(hKeyOpen,
  81. ValueName,
  82. NULL,
  83. KeyType,
  84. (LPBYTE)Value,
  85. RegKeySize)) == ERROR_SUCCESS )
  86. {
  87. ret = TRUE;
  88. }
  89. RegCloseKey(hKeyOpen);
  90. }
  91. else
  92. {
  93. CreateQuickResKey();
  94. }
  95. return ret;
  96. }
  97. //
  98. //****************************************************************************
  99. //
  100. // SetRegistryValue( UINT, UINT, PVOID, UINT )
  101. //
  102. // Set requested value (modes, flags, or BPP) in the registry
  103. //
  104. //****************************************************************************
  105. //
  106. VOID SetRegistryValue(LPTSTR ValueName, UINT ValueType, PVOID Value, UINT size)
  107. {
  108. HKEY hKeyOpen; // Quickres key
  109. LONG RegReturn; // reg APIs return value
  110. //
  111. // try to open QuickRes key
  112. //
  113. if( (RegReturn=RegOpenKeyEx(HKEY_CURRENT_USER,
  114. REGSTR_QUICKRES,
  115. 0,
  116. KEY_WRITE,
  117. &hKeyOpen)) == ERROR_SUCCESS )
  118. {
  119. //
  120. // Set the value under that key
  121. //
  122. RegSetValueEx( hKeyOpen,
  123. ValueName,
  124. 0,
  125. ValueType,
  126. (LPBYTE)Value,
  127. size );
  128. RegCloseKey(hKeyOpen);
  129. }
  130. }
  131. #endif // SAVEFLAGS
  132. //
  133. //****************************************************************************
  134. //
  135. // SetDevmodeFlags( INT, BOOL )
  136. //
  137. // Upload value in ModeFlags, and current BPP to registry
  138. //
  139. //****************************************************************************
  140. //
  141. VOID SetDevmodeFlags( INT iDisplay, BOOL ClearAll )
  142. {
  143. #ifdef SAVEFLAGS
  144. PBYTE ModeFlags;
  145. INT i;
  146. TCHAR RegModes[16] = REGDEVMODES;
  147. //
  148. // Alloc Modes/4 bytes so each mode has 2 bits
  149. //
  150. ModeFlags = LocalAlloc ( LPTR, (pMonitors[iDisplay].iModes+3) >> 2 );
  151. if (ModeFlags)
  152. {
  153. if (ClearAll)
  154. {
  155. //
  156. // Clear out all Mode flags if requested
  157. // need to set current mode as the only valid one
  158. //
  159. for ( i=0; i < pMonitors[iDisplay].iModes; i++)
  160. {
  161. VALIDMODE(&pMonitors[iDisplay].pModes[i]) = MODE_UNTESTED;
  162. }
  163. }
  164. //
  165. // Pack valid mode flags into ModeFlags[]
  166. //
  167. for ( i=0; i < pMonitors[iDisplay].iModes; i++)
  168. {
  169. ModeFlags[i>>2] |= (VALIDMODE(&pMonitors[iDisplay].pModes[i]) << ((i%4)<<1) );
  170. }
  171. //
  172. // Store modeflags in the registry
  173. //
  174. if (iDisplay)
  175. {
  176. TCHAR buff[4];
  177. _itot(iDisplay, buff, 4);
  178. lstrcat(RegModes, buff);
  179. }
  180. SetRegistryValue(RegModes, REG_BINARY,
  181. ModeFlags, (pMonitors[iDisplay].iModes+3) >> 2 );
  182. LocalFree ( ModeFlags );
  183. }
  184. #endif
  185. }
  186. //
  187. //****************************************************************************
  188. //
  189. // GetDevmodeFlags( )
  190. //
  191. // Read value from registry into global variable ModeFlags
  192. //
  193. //****************************************************************************
  194. //
  195. VOID GetDevmodeFlags( INT iDisplay )
  196. {
  197. INT i;
  198. #ifdef SAVEFLAGS
  199. PBYTE ModeFlags;
  200. DWORD KeyType;
  201. DWORD SavedBPP;
  202. DWORD RegKeySize = (pMonitors[iDisplay].iModes+3) >> 2 ;
  203. BOOL bClear=FALSE;
  204. TCHAR RegModes[16] = REGDEVMODES;
  205. ModeFlags = LocalAlloc ( LPTR, RegKeySize );
  206. //
  207. // Try to read value
  208. //
  209. if (iDisplay)
  210. {
  211. TCHAR buff[4];
  212. _itot(iDisplay, buff, 4);
  213. lstrcat(RegModes, buff);
  214. }
  215. if ( ReadRegistryValue(RegModes, &KeyType,
  216. ModeFlags, &RegKeySize) )
  217. {
  218. //
  219. // Changing BPP on the fly IS allowed on NT 4.0 - NOT on Win95
  220. // NT ONLY : 'Good' modes are still good even if they are different BPP
  221. // fShowFreqs is essentially the NT vs Win95 flag.
  222. //
  223. if (!fShowFreqs)
  224. {
  225. //
  226. // Make sure user hasnt changed BPP via the desktop applet
  227. //
  228. RegKeySize = sizeof( DWORD );
  229. if (ReadRegistryValue(REGBPP, &KeyType,
  230. &SavedBPP, &RegKeySize))
  231. {
  232. //
  233. // If BPP HAS changed, modeflags is now bogus.
  234. // clear the flags. Tell the user.
  235. //
  236. DEVMODE dm;
  237. GetCurrentDevMode(iDisplay, &dm);
  238. if ( SavedBPP != BPP(&dm) )
  239. {
  240. bClear=TRUE;
  241. MsgBox(IDS_CHANGEDBPP, SavedBPP, MB_OK|MB_ICONEXCLAMATION);
  242. }
  243. }
  244. }
  245. }
  246. else
  247. {
  248. //
  249. // Couldnt read value from registry.
  250. // Assume no modes work; clear the flags
  251. //
  252. bClear = TRUE;
  253. }
  254. if (bClear)
  255. {
  256. SetDevmodeFlags( iDisplay, TRUE );
  257. }
  258. else
  259. {
  260. //
  261. // Unpack ModeFlags into a field in each devmode
  262. // 2 bits per devmode - shift right, and with %11
  263. //
  264. for (i=0; i < pMonitors[iDisplay].iModes; i++ )
  265. {
  266. VALIDMODE(&pMonitors[iDisplay].pModes[i]) = ((ModeFlags[i>>2]) >> ((i%4)<<1)) & 0x03;
  267. }
  268. }
  269. LocalFree ( ModeFlags );
  270. #else
  271. //
  272. // Not reading from the registry - assign all as untested
  273. //
  274. for (i=0; i < pMonitors[iDisplay].iModes; i++ )
  275. {
  276. VALIDMODE(&pMonitors[iDisplay].pModes[i]) = MODE_UNTESTED;
  277. }
  278. #endif
  279. }
  280. //
  281. //****************************************************************************
  282. //
  283. // SetQuickResFlags( )
  284. //
  285. // Upload QuickResFlags value to registry
  286. //
  287. //****************************************************************************
  288. //
  289. VOID SetQuickResFlags( )
  290. {
  291. #ifdef SAVEFLAGS
  292. DWORD BothFlags = (FreqMenuLocation << (8*sizeof(WORD))) | QuickResFlags;
  293. SetRegistryValue(REGFLAGS, REG_DWORD,
  294. &BothFlags, sizeof(DWORD));
  295. #endif
  296. }
  297. //
  298. //****************************************************************************
  299. //
  300. // GetQuickResFlags( )
  301. //
  302. // Read value from registry into global QuickResFlags
  303. //
  304. //****************************************************************************
  305. //
  306. VOID GetQuickResFlags( )
  307. {
  308. #ifdef SAVEFLAGS
  309. DWORD KeyType;
  310. DWORD RegKeySize=sizeof(DWORD);
  311. DWORD BothFlags;
  312. //
  313. // Try to read value
  314. //
  315. if (!ReadRegistryValue(REGFLAGS, &KeyType,
  316. &BothFlags, &RegKeySize) )
  317. {
  318. //
  319. // assume a flag value, and create it.
  320. //
  321. QuickResFlags = QF_SHOWRESTART | QF_REMMODES;
  322. FreqMenuLocation= IDD_SUBMENUS;
  323. SetQuickResFlags();
  324. #ifdef DONTPANIC
  325. MsgBox(IDS_DONTPANIC, KEEP_RES_TIMEOUT, MB_OK | MB_ICONEXCLAMATION );
  326. #endif
  327. }
  328. else
  329. {
  330. if ( !( FreqMenuLocation = (WORD)(BothFlags >> (8*sizeof(WORD))) ) )
  331. FreqMenuLocation = IDD_SUBMENUS;
  332. QuickResFlags = (WORD)(0xFFFF & BothFlags);
  333. }
  334. //
  335. // Do this always!
  336. //
  337. QuickResFlags |= QF_HIDE_4BPP;
  338. #else
  339. #ifdef DONTPANIC
  340. MsgBox(IDS_DONTPANIC, KEEP_RES_TIMEOUT, MB_OK | MB_ICONEXCLAMATION );
  341. #endif
  342. QuickResFlags = QF_SHOWRESTART | QF_REMMODES | QF_HIDE_4BPP;
  343. #endif
  344. }
  345. //
  346. //****************************************************************************
  347. //
  348. // SaveAllSettings( )
  349. //
  350. // Write QuickResFlags, devmode flags, and BPP to the registry
  351. //
  352. //****************************************************************************
  353. //
  354. VOID SaveAllSettings()
  355. {
  356. SetQuickResFlags( );
  357. if (fRememberModes)
  358. {
  359. INT iDisplay;
  360. for (iDisplay = 0; iDisplay < iMonitors; iDisplay++)
  361. {
  362. SetDevmodeFlags( iDisplay, FALSE );
  363. }
  364. }
  365. SetRegistryValue(REGBPP, REG_DWORD,
  366. &( BPP(&pMonitors[0].pCurrentdm->dm) ), sizeof(DWORD) );
  367. // FEATURE only pays attention to monitor 0 (is this still necessary?)
  368. }