Windows NT 4.0 source code leak
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.

512 lines
16 KiB

4 years ago
  1. /***************************************************************************\
  2. * profile.c
  3. *
  4. * Microsoft Confidential
  5. * Copyright (c) 1991 Microsoft Corporation
  6. *
  7. * Routines for saving, loading, and resetting program settings
  8. *
  9. * History:
  10. * Written by Ali Partovi (t-alip) summer 1991
  11. *
  12. * Re-written and adapted for NT by Fran Borda (v-franb) Nov.1991
  13. * for Newman Consulting
  14. * Took out all WIN-specific and bargraph code. Added 3 new
  15. * linegraphs (Mem/Paging, Process/Threads/Handles, IO), and
  16. * tailored info to that available under NT.
  17. \***************************************************************************/
  18. #include "winmeter.h"
  19. // main global information structures
  20. extern GLOBAL g;
  21. extern int win_on_top;
  22. // static variables with definitions of .INI file strings
  23. static char szIniFile[] = "winmeter.ini";
  24. static char szTrue[] = "TRUE";
  25. static char szFalse[] = "FALSE";
  26. static char szRefresh[] = "Refresh";
  27. static char szInterval[] = "Interval";
  28. static char szManualSampling[] = "ManualSampling";
  29. static char szApplyChanges[] = "ApplyChanges";
  30. static char szRightSideOfAxis[] = "RightSideOfAxis";
  31. static char szValBottom[] = "BottomValue";
  32. static char szDValAxisHeight[] = "AxisHeight";
  33. static char szMaxValues[] = "MaxValues";
  34. static char szWindowSize[] = "WindowSize";
  35. static char szWindowLeft[] = "WindowLeft";
  36. static char szWindowTop[] = "WindowTop";
  37. static char szWindowWidth[] = "WindowWidth";
  38. static char szWindowHeight[] = "WindowHeight";
  39. static char szLGDisp[] = "LGDisp";
  40. static char szDisplayLegend[] = "DisplayLegend";
  41. static char szDisplayCalibration[] = "DisplayCalibration";
  42. static char szDisplayState[] = "DisplayState";
  43. static char szFLineGraph[] = "LineGraph";
  44. static char szCurrentGraph[] = "CurrentGraph";
  45. static char szWinPos[] = "WinPos";
  46. // some functions to make things easier
  47. BOOL LoadBoolean(LPSTR lpszSection, LPSTR lpszEntry, BOOL fDefault);
  48. // loads a boolean value from .ini file
  49. void SaveBoolean(LPSTR lpszSection, LPSTR lpszEntry, BOOL fValue);
  50. // saves a boolean value to .ini file
  51. void SaveDWORD(LPSTR lpszSection, LPSTR lpszEntry, DWORD dwValue);
  52. // saves a DWORD value to .ini file
  53. void SaveInt(LPSTR lpszSection, LPSTR lpszEntry, int nValue);
  54. // saves a integer value to .ini file
  55. /***************************************************************************\
  56. * LoadBoolean()
  57. *
  58. * Entry: A section and entry name, as well as a default flag
  59. * Exit: Returns the value for the boolean string (TRUE/FALSE) in the
  60. * given entry/section in the .INI file (sets to default if none)
  61. \***************************************************************************/
  62. BOOL LoadBoolean(
  63. LPSTR lpszSection, // the section name
  64. LPSTR lpszEntry, // the entry name
  65. BOOL fDefault) // the default value
  66. {
  67. static char szSmallBuf[SMALL_BUF_LEN];
  68. GetPrivateProfileString(lpszSection, lpszEntry,
  69. (fDefault) ? szTrue : szFalse, szSmallBuf, SMALL_BUF_LEN, szIniFile);
  70. return !lstrcmp(szSmallBuf, szTrue);
  71. }
  72. /***************************************************************************\
  73. * LoadDisplayState()
  74. *
  75. * Entry: None
  76. * Exit: Loads initial display state from .INI file
  77. * Initializes .INI files if none
  78. \***************************************************************************/
  79. void LoadDisplayState(void) // load [DisplayState] settings from .INI
  80. {
  81. char szBuf[TEMP_BUF_LEN]; // to hold current linegraph title
  82. if (!GetPrivateProfileString(szDisplayState, NULL, "",
  83. g.szBuf, TEMP_BUF_LEN, szIniFile)) {
  84. // no .INI file, create default
  85. ResetDisplayState();
  86. SaveDisplayState();
  87. return;
  88. }
  89. // load settings
  90. g.LineGraph = LoadBoolean(szDisplayState, szFLineGraph,DO_CPU);
  91. GetPrivateProfileString(szDisplayState, szCurrentGraph, NULL,
  92. szBuf, TEMP_BUF_LEN, szIniFile);
  93. // now find which lg structure is actually to be the current one
  94. for (g.plg=g.plgList; (g.plg) && (lstrcmp(g.plg->lpszTitle, szBuf));
  95. g.plg = g.plg->plgNext)
  96. if (!g.plg)
  97. // use default if no match
  98. g.plg = DEFAULT_CURRENT_GRAPH;
  99. else
  100. if (g.LineGraph == DO_CPU)
  101. CheckMenuItem(g.hMenu, IDM_CPU_USAGE, MF_CHECKED);
  102. else
  103. if (g.LineGraph == DO_PROCS)
  104. CheckMenuItem(g.hMenu, IDM_PROCS, MF_CHECKED);
  105. else
  106. if (g.LineGraph == DO_MEM)
  107. CheckMenuItem(g.hMenu, IDM_MEM_USAGE, MF_CHECKED);
  108. else
  109. if (g.LineGraph == DO_IO)
  110. CheckMenuItem(g.hMenu, IDM_IO_USAGE, MF_CHECKED);
  111. if (win_on_top)
  112. CheckMenuItem(g.hMenu, IDM_SETTINGS, MF_CHECKED);
  113. return;
  114. }
  115. /***************************************************************************\
  116. * LoadLGDispSettings()
  117. *
  118. * Entry: None
  119. * Exit: Loads line graph displaysettings from .INI file.
  120. * Initializes .INI files if none
  121. \***************************************************************************/
  122. void LoadLGDispSettings(void) // load linegraph display settings from .INI
  123. {
  124. if (!GetPrivateProfileString(szLGDisp, NULL, "",
  125. g.szBuf, TEMP_BUF_LEN, szIniFile)) {
  126. // no .INI file, create default
  127. ResetLGDispSettings();
  128. SaveLGDispSettings();
  129. return;
  130. }
  131. g.fDisplayLegend = LoadBoolean(szLGDisp,
  132. szDisplayLegend, DEFAULT_F_DISPLAY_LEGEND);
  133. g.fDisplayCalibration = LoadBoolean(szLGDisp,
  134. szDisplayCalibration, DEFAULT_F_DISPLAY_CALIBRATION);
  135. return;
  136. }
  137. /***************************************************************************\
  138. * LoadLineGraphSettings()
  139. *
  140. * Entry: None
  141. * Exit: Loads line graph settings from .INI file.
  142. * Initializes .INI files if none
  143. \***************************************************************************/
  144. void LoadLineGraphSettings(void) // load line graph settings from .INI
  145. {
  146. if (!GetPrivateProfileString(g.plg->lpszTitle, NULL, "",
  147. g.szBuf, TEMP_BUF_LEN, szIniFile)) {
  148. // no .INI file, create default
  149. ResetLineGraphSettings();
  150. SaveLineGraphSettings();
  151. return;
  152. }
  153. g.plg->valBottom = GetPrivateProfileInt(g.plg->lpszTitle,
  154. szValBottom, DEFAULT_VAL_BOTTOM, szIniFile);
  155. if (g.plg == g.plgCPU)
  156. {
  157. g.plg->dvalAxisHeight = DEFAULT_DVAL_AXISHEIGHT;
  158. g.plg->nMaxValues = DEFAULT_MAX_VALUES;
  159. }
  160. else
  161. if (g.plg == g.plgProcs)
  162. {
  163. g.plg->dvalAxisHeight = T_DEFAULT_DVAL_AXISHEIGHT;
  164. g.plg->nMaxValues = T_DEFAULT_MAX_VALUES;
  165. }
  166. else
  167. if (g.plg == g.plgMemory)
  168. {
  169. g.plg->dvalAxisHeight = M_DEFAULT_DVAL_AXISHEIGHT;
  170. g.plg->nMaxValues = M_DEFAULT_MAX_VALUES;
  171. }
  172. else
  173. if (g.plg == g.plgIO)
  174. {
  175. g.plg->dvalAxisHeight = I_DEFAULT_DVAL_AXISHEIGHT;
  176. g.plg->nMaxValues = I_DEFAULT_MAX_VALUES;
  177. }
  178. return;
  179. }
  180. /***************************************************************************\
  181. * LoadRefreshSettings()
  182. *
  183. * Entry: None
  184. * Exit: Loads "Refresh" dialog settings from .INI file.
  185. * Initializes .INI files if none
  186. \***************************************************************************/
  187. void LoadRefreshSettings(void) // load [Refresh] settings from .INI
  188. {
  189. if (!GetPrivateProfileString(szRefresh, NULL, "",
  190. g.szBuf, TEMP_BUF_LEN, szIniFile)) {
  191. // no .INI file, create default
  192. ResetRefreshSettings();
  193. SaveRefreshSettings();
  194. return;
  195. }
  196. g.nTimerInterval=GetPrivateProfileInt(szRefresh,
  197. szInterval, DEFAULT_TIMER_INTERVAL, szIniFile);
  198. g.fManualSampling =
  199. LoadBoolean(szRefresh, szManualSampling, DEFAULT_F_MANUAL);
  200. return;
  201. }
  202. /***************************************************************************\
  203. * LoadWindowSettings()
  204. *
  205. * Entry: None
  206. * Exit: Loads [WindowSize] settings from .INI file.
  207. * Initializes .INI files if none
  208. \***************************************************************************/
  209. void LoadWindowSettings(void) // load [WindowSize] settings from .INI
  210. {
  211. if (!GetPrivateProfileString(szWindowSize, NULL, "",
  212. g.szBuf, TEMP_BUF_LEN, szIniFile)) {
  213. // no .INI file, create default
  214. ResetWindowSettings();
  215. return;
  216. }
  217. g.xWindowLeft=GetPrivateProfileInt(szWindowSize,
  218. szWindowLeft, CW_USEDEFAULT, szIniFile);
  219. g.yWindowTop=GetPrivateProfileInt(szWindowSize,
  220. szWindowTop, CW_USEDEFAULT, szIniFile);
  221. g.cxClient=GetPrivateProfileInt(szWindowSize,
  222. szWindowWidth, CW_USEDEFAULT, szIniFile);
  223. g.cyClient=GetPrivateProfileInt(szWindowSize,
  224. szWindowHeight, CW_USEDEFAULT, szIniFile);
  225. return;
  226. }
  227. /***************************************************************************\
  228. * ResetDisplayState()
  229. *
  230. * Entry: None
  231. * Exit: Resets default initial display
  232. \***************************************************************************/
  233. void ResetDisplayState(void) // reset defaults for [DisplayState]
  234. {
  235. // start in CPU linegraph
  236. g.LineGraph = DO_CPU;
  237. g.plg = DEFAULT_CURRENT_GRAPH;
  238. return;
  239. }
  240. /***************************************************************************\
  241. * ResetLGDispSettings()
  242. *
  243. * Entry: None
  244. * Exit: Resets default settings for line graph display
  245. \***************************************************************************/
  246. void ResetLGDispSettings(void) // reset defaults for line graph display
  247. {
  248. g.fDisplayLegend = DEFAULT_F_DISPLAY_LEGEND;
  249. g.fDisplayCalibration = DEFAULT_F_DISPLAY_CALIBRATION;
  250. return;
  251. }
  252. /***************************************************************************\
  253. * ResetLineGraphSettings()
  254. *
  255. * Entry: None
  256. * Exit: Resets default settings for a Line Graph
  257. \***************************************************************************/
  258. void ResetLineGraphSettings(void) // reset linegraph defaults
  259. {
  260. if (g.plg == g.plgProcs)
  261. {
  262. g.plg->dvalAxisHeight = T_DEFAULT_DVAL_AXISHEIGHT;
  263. g.plg->nMaxValues = T_DEFAULT_MAX_VALUES;
  264. }
  265. else
  266. if (g.plg == g.plgMemory)
  267. {
  268. g.plg->dvalAxisHeight = M_DEFAULT_DVAL_AXISHEIGHT;
  269. g.plg->nMaxValues = M_DEFAULT_MAX_VALUES;
  270. }
  271. else
  272. if (g.plg == g.plgIO)
  273. {
  274. g.plg->dvalAxisHeight = I_DEFAULT_DVAL_AXISHEIGHT;
  275. g.plg->nMaxValues = I_DEFAULT_MAX_VALUES;
  276. }
  277. else
  278. if (g.plg == g.plgCPU)
  279. {
  280. g.plg->dvalAxisHeight = DEFAULT_DVAL_AXISHEIGHT;
  281. g.plg->nMaxValues = DEFAULT_MAX_VALUES;
  282. }
  283. g.plg->valBottom = DEFAULT_VAL_BOTTOM;
  284. return;
  285. }
  286. /***************************************************************************\
  287. * ResetRefreshSettings()
  288. *
  289. * Entry: None
  290. * Exit: Resets default settings for "Refresh" dialog box
  291. \***************************************************************************/
  292. void ResetRefreshSettings(void) // reset defaults for [Refresh]
  293. {
  294. g.nTimerInterval = DEFAULT_TIMER_INTERVAL;
  295. g.fManualSampling = DEFAULT_F_MANUAL;
  296. return;
  297. }
  298. /***************************************************************************\
  299. * ResetWindowSettings()
  300. *
  301. * Entry: None
  302. * Exit: Resets default settings for [WindowSize]
  303. \***************************************************************************/
  304. void ResetWindowSettings(void) // reset defaults for [WindowSize]
  305. {
  306. g.xWindowLeft = g.yWindowTop = g.cxClient = g.cyClient =
  307. CW_USEDEFAULT;
  308. return;
  309. }
  310. /***************************************************************************\
  311. * SaveBoolean()
  312. *
  313. * Entry: a section name and entry name, and a flag
  314. * Exit: Writes the flags value to the entry within the section
  315. \***************************************************************************/
  316. void SaveBoolean(
  317. LPSTR lpszSection, // the section name
  318. LPSTR lpszEntry, // entry name
  319. BOOL fValue) // the flags value
  320. {
  321. if (fValue)
  322. WritePrivateProfileString(lpszSection, lpszEntry, szTrue, szIniFile);
  323. else
  324. WritePrivateProfileString(lpszSection, lpszEntry, szFalse, szIniFile);
  325. return;
  326. }
  327. /***************************************************************************\
  328. * SaveDisplayState()
  329. *
  330. * Entry: None
  331. * Exit: Saves settings for display state to .INI file
  332. \***************************************************************************/
  333. void SaveDisplayState(void) // Saves [DisplayState] settings to .INI
  334. {
  335. SaveBoolean(szDisplayState, szFLineGraph, g.LineGraph);
  336. SaveBoolean(szDisplayState, szWinPos, win_on_top);
  337. WritePrivateProfileString(szDisplayState, szCurrentGraph,
  338. g.plg->lpszTitle, szIniFile);
  339. return;
  340. }
  341. /***************************************************************************\
  342. * SaveDWORD()
  343. *
  344. * Entry: a section name and entry name, and a DWORD
  345. * Exit: Writes the DWORD value to the entry within the section
  346. \***************************************************************************/
  347. void SaveDWORD(
  348. LPSTR lpszSection, // the section name
  349. LPSTR lpszEntry, // entry name
  350. DWORD dwValue) // the DWORD
  351. {
  352. static char szSmallBuf[SMALL_BUF_LEN];
  353. wsprintf(szSmallBuf, "%lu", dwValue);
  354. WritePrivateProfileString(lpszSection, lpszEntry, szSmallBuf, szIniFile);
  355. return;
  356. }
  357. /***************************************************************************\
  358. * SaveInt()
  359. *
  360. * Entry: a section name and entry name, and a integer
  361. * Exit: Writes the integer value to the entry within the section
  362. \***************************************************************************/
  363. void SaveInt(
  364. LPSTR lpszSection, // the section name
  365. LPSTR lpszEntry, // entry name
  366. BOOL nValue) // the integer
  367. {
  368. static char szSmallBuf[SMALL_BUF_LEN];
  369. wsprintf(szSmallBuf, "%d", nValue);
  370. WritePrivateProfileString(lpszSection, lpszEntry, szSmallBuf, szIniFile);
  371. return;
  372. }
  373. /***************************************************************************\
  374. * SaveLGDispSettings()
  375. *
  376. * Entry: None
  377. * Exit: Saves settings for line graph display to .INI file.
  378. \***************************************************************************/
  379. void SaveLGDispSettings(void) // Saves line graph display settings to .INI
  380. {
  381. SaveBoolean(szLGDisp, szDisplayLegend, g.fDisplayLegend);
  382. SaveBoolean(szLGDisp, szDisplayCalibration, g.fDisplayCalibration);
  383. return;
  384. }
  385. /***************************************************************************\
  386. * SaveLineGraphSettings()
  387. *
  388. * Entry: None
  389. * Exit: Saves line graph settings to .INI file.
  390. \***************************************************************************/
  391. void SaveLineGraphSettings(void) // Saves line graph settings to .INI
  392. {
  393. SaveDWORD(g.plg->lpszTitle, szValBottom, g.plg->valBottom);
  394. SaveDWORD(g.plg->lpszTitle, szDValAxisHeight, g.plg->dvalAxisHeight);
  395. SaveDWORD(g.plg->lpszTitle, szMaxValues, g.plg->nMaxValues);
  396. return;
  397. }
  398. /***************************************************************************\
  399. * SaveRefreshSettings()
  400. *
  401. * Entry: None
  402. * Exit: Saves settings for "Refresh" dialog to .INI file.
  403. \***************************************************************************/
  404. void SaveRefreshSettings(void) // Saves [Refresh] settings to .INI
  405. {
  406. SaveInt(szRefresh, szInterval, g.nTimerInterval);
  407. SaveBoolean(szRefresh, szManualSampling, g.fManualSampling);
  408. return;
  409. }
  410. /***************************************************************************\
  411. * SaveWindowSettings()
  412. *
  413. * Entry: None
  414. * Exit: Saves settings for [WindowSize] to .INI file.
  415. \***************************************************************************/
  416. void SaveWindowSettings(void) // Saves [WindowSize] settings to .INI
  417. {
  418. RECT rcWindow; // boundaries of current window
  419. // get window position
  420. GetWindowRect(g.hwnd, &rcWindow);
  421. // write values to INI file
  422. SaveInt(szWindowSize, szWindowLeft, rcWindow.left);
  423. SaveInt(szWindowSize, szWindowTop, rcWindow.top);
  424. SaveInt(szWindowSize, szWindowWidth, rcWindow.right - rcWindow.left);
  425. SaveInt(szWindowSize, szWindowHeight, rcWindow.bottom - rcWindow.top);
  426. return;
  427. }