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.

185 lines
4.9 KiB

  1. /****************/
  2. /* file: pref.c */
  3. /****************/
  4. #define _WINDOWS
  5. #include <windows.h>
  6. #include <port1632.h>
  7. #include "main.h"
  8. #include "res.h"
  9. #include "rtns.h"
  10. #include "grafix.h"
  11. #include "pref.h"
  12. #include "sound.h"
  13. BOOL fUpdateIni = fFalse;
  14. HKEY g_hReg;
  15. extern TCHAR szDefaultName[];
  16. extern INT xBoxMac;
  17. extern INT yBoxMac;
  18. extern PREF Preferences;
  19. TCHAR * rgszPref[iszPrefMax] =
  20. {
  21. TEXT("Difficulty"),
  22. TEXT("Mines" ),
  23. TEXT("Height" ),
  24. TEXT("Width" ),
  25. TEXT("Xpos" ),
  26. TEXT("Ypos" ),
  27. TEXT("Sound" ),
  28. TEXT("Mark" ),
  29. TEXT("Menu" ),
  30. TEXT("Tick" ),
  31. TEXT("Color" ),
  32. TEXT("Time1" ),
  33. TEXT("Name1" ),
  34. TEXT("Time2" ),
  35. TEXT("Name2" ),
  36. TEXT("Time3" ),
  37. TEXT("Name3" ),
  38. TEXT("AlreadyPlayed")
  39. };
  40. /****** PREFERENCES ******/
  41. INT ReadInt(INT iszPref, INT valDefault, INT valMin, INT valMax)
  42. {
  43. DWORD dwIntRead;
  44. DWORD dwSizeOfData = sizeof(INT);
  45. // If value not present, return default value.
  46. if (RegQueryValueEx(g_hReg, rgszPref[iszPref], NULL, NULL, (LPBYTE) &dwIntRead,
  47. &dwSizeOfData) != ERROR_SUCCESS)
  48. return valDefault;
  49. return max(valMin, min(valMax, (INT) dwIntRead));
  50. }
  51. #define ReadBool(iszPref, valDefault) ReadInt(iszPref, valDefault, 0, 1)
  52. VOID ReadSz(INT iszPref, TCHAR FAR * szRet)
  53. {
  54. DWORD dwSizeOfData = cchNameMax * sizeof(TCHAR);
  55. // If string not present, return default string.
  56. if (RegQueryValueEx(g_hReg, rgszPref[iszPref], NULL, NULL, (LPBYTE) szRet,
  57. &dwSizeOfData) != ERROR_SUCCESS)
  58. lstrcpy(szRet, szDefaultName) ;
  59. return;
  60. }
  61. VOID ReadPreferences(VOID)
  62. {
  63. DWORD dwDisposition;
  64. // Open the registry key; if it fails, there is not much we can do about it.
  65. RegCreateKeyEx(HKEY_CURRENT_USER, SZWINMINEREG, 0, NULL, 0, KEY_READ, NULL,
  66. &g_hReg, &dwDisposition);
  67. yBoxMac= Preferences.Height= ReadInt(iszPrefHeight,MINHEIGHT,DEFHEIGHT,25);
  68. xBoxMac= Preferences.Width= ReadInt(iszPrefWidth,MINWIDTH,DEFWIDTH,30);
  69. Preferences.wGameType = (WORD)ReadInt(iszPrefGame,wGameBegin, wGameBegin, wGameExpert+1);
  70. Preferences.Mines = ReadInt(iszPrefMines, 10, 10, 999);
  71. Preferences.xWindow = ReadInt(iszPrefxWindow, 80, 0, 1024);
  72. Preferences.yWindow = ReadInt(iszPrefyWindow, 80, 0, 1024);
  73. Preferences.fSound = ReadInt(iszPrefSound, 0, 0, fsoundOn);
  74. Preferences.fMark = ReadBool(iszPrefMark, fTrue);
  75. Preferences.fTick = ReadBool(iszPrefTick, fFalse);
  76. Preferences.fMenu = ReadInt(iszPrefMenu, fmenuAlwaysOn, fmenuAlwaysOn, fmenuOn);
  77. Preferences.rgTime[wGameBegin] = ReadInt(iszPrefBeginTime, 999, 0, 999);
  78. Preferences.rgTime[wGameInter] = ReadInt(iszPrefInterTime, 999, 0, 999);
  79. Preferences.rgTime[wGameExpert] = ReadInt(iszPrefExpertTime, 999, 0, 999);
  80. ReadSz(iszPrefBeginName, Preferences.szBegin);
  81. ReadSz(iszPrefInterName, Preferences.szInter);
  82. ReadSz(iszPrefExpertName, Preferences.szExpert);
  83. // set the color preference so we will use the right bitmaps
  84. // numcolors may return -1 on true color devices
  85. {
  86. HDC hDC = GetDC(GetDesktopWindow());
  87. Preferences.fColor = ReadBool(iszPrefColor, (GetDeviceCaps(hDC, NUMCOLORS) != 2));
  88. ReleaseDC(GetDesktopWindow(),hDC);
  89. }
  90. if (FSoundOn())
  91. Preferences.fSound = FInitTunes();
  92. RegCloseKey(g_hReg);
  93. }
  94. VOID WriteInt(INT iszPref, INT val)
  95. {
  96. // No check for return value for if it fails, can't do anything
  97. // to rectify the situation.
  98. RegSetValueEx(g_hReg, rgszPref[iszPref], 0, REG_DWORD, (LPBYTE) &val, sizeof(val));
  99. return;
  100. }
  101. VOID WriteSz(INT iszPref, TCHAR FAR * sz)
  102. {
  103. // No check for return value for if it fails, can't do anything
  104. // to rectify the situation.
  105. RegSetValueEx(g_hReg, rgszPref[iszPref], 0, REG_SZ, (LPBYTE) sz,
  106. sizeof(TCHAR) * (lstrlen(sz)+1));
  107. return;
  108. }
  109. VOID WritePreferences(VOID)
  110. {
  111. DWORD dwDisposition;
  112. // Open the registry key; if it fails, there is not much we can do about it.
  113. RegCreateKeyEx(HKEY_CURRENT_USER, SZWINMINEREG, 0, NULL, 0, KEY_WRITE, NULL,
  114. &g_hReg, &dwDisposition);
  115. WriteInt(iszPrefGame, Preferences.wGameType);
  116. WriteInt(iszPrefHeight, Preferences.Height);
  117. WriteInt(iszPrefWidth, Preferences.Width);
  118. WriteInt(iszPrefMines, Preferences.Mines);
  119. WriteInt(iszPrefMark, Preferences.fMark);
  120. WriteInt(iszPrefAlreadyPlayed, 1);
  121. #ifdef WRITE_HIDDEN
  122. WriteInt(iszPrefMenu, Preferences.fMenu);
  123. WriteInt(iszPrefTick, Preferences.fTick);
  124. #endif
  125. WriteInt(iszPrefColor, Preferences.fColor);
  126. WriteInt(iszPrefSound, Preferences.fSound);
  127. WriteInt(iszPrefxWindow,Preferences.xWindow);
  128. WriteInt(iszPrefyWindow,Preferences.yWindow);
  129. WriteInt(iszPrefBeginTime, Preferences.rgTime[wGameBegin]);
  130. WriteInt(iszPrefInterTime, Preferences.rgTime[wGameInter]);
  131. WriteInt(iszPrefExpertTime, Preferences.rgTime[wGameExpert]);
  132. WriteSz(iszPrefBeginName, Preferences.szBegin);
  133. WriteSz(iszPrefInterName, Preferences.szInter);
  134. WriteSz(iszPrefExpertName, Preferences.szExpert);
  135. RegCloseKey(g_hReg);
  136. }