1 // 2 // settings.c 3 // 4 // Load/Save settings from registry 5 // 6 #include <stdlib.h> 7 #include <windows.h> 8 #include <tchar.h> 9 #include "globals.h" 10 #include "message.h" 11 #include "matrix.h" 12 13 TCHAR g_szMessages[MAX_MESSAGES][MAXMSG_LENGTH]; 14 int g_nNumMessages = 0; 15 int g_nMessageSpeed = 5; 16 TCHAR g_szFontName[512] = _T("Arial"); 17 18 int g_nMatrixSpeed = 10; 19 int g_nDensity = 32; 20 int g_nFontSize = 12; 21 BOOL g_fRandomizeMessages = FALSE; 22 BOOL g_fFontBold = TRUE; 23 BOOL g_fScreenSaving = FALSE; 24 25 HFONT g_hFont; 26 27 void LoadSettings() 28 { 29 HKEY hkey; 30 LONG value; 31 ULONG len; 32 TCHAR *hugechar = malloc(4096); 33 TCHAR *hptr = hugechar; 34 35 if(hugechar == 0) 36 return; 37 38 ZeroMemory(hugechar, 4096); 39 40 RegCreateKeyEx(HKEY_CURRENT_USER, _T("Software\\Catch22\\Matrix Screen Saver"), 0, 41 _T(""), 0, KEY_READ, NULL, &hkey, NULL); 42 43 len = sizeof value; 44 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("MessageSpeed"), 0, 0, (BYTE *)&value, &len)) 45 { 46 if(value >= MSGSPEED_MIN && value <= MSGSPEED_MAX) 47 g_nMessageSpeed = value; 48 } 49 50 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("MatrixSpeed"), 0, 0, (BYTE *)&value, &len)) 51 { 52 if(value >= SPEED_MIN && value <= SPEED_MAX) 53 g_nMatrixSpeed = value; 54 } 55 56 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("Density"), 0, 0, (BYTE *)&value, &len)) 57 { 58 if(value >= DENSITY_MIN && value <= DENSITY_MAX) 59 g_nDensity = value; 60 } 61 62 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("FontSize"), 0, 0, (BYTE *)&value, &len)) 63 { 64 if(value >= FONT_MIN && value <= FONT_MAX) 65 g_nFontSize = value; 66 } 67 68 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("FontBold"), 0, 0, (BYTE *)&value, &len)) 69 g_fFontBold = (value == 0 ? FALSE : TRUE); 70 71 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("Randomize"), 0, 0, (BYTE *)&value, &len)) 72 g_fRandomizeMessages = (value == 0 ? FALSE : TRUE); 73 74 len = 512; 75 if(RegQueryValueEx(hkey, _T("FontName"), 0, 0, (BYTE *)g_szFontName, &len) != ERROR_SUCCESS) 76 lstrcpy(g_szFontName, _T("Arial")); 77 78 len = 4096; 79 80 if(ERROR_SUCCESS == RegQueryValueEx(hkey, _T("Messages"), 0, 0 , (BYTE *)hugechar, &len)) 81 { 82 while(len > 0 && *hptr && _istascii(*hptr)) 83 { 84 if(lstrlen(hptr) > 0) 85 { 86 lstrcpyn(g_szMessages[g_nNumMessages], hptr, MAXMSG_LENGTH); 87 ++g_nNumMessages; 88 hptr += lstrlen(hptr) + 1; 89 } 90 } 91 } 92 else 93 { 94 /* built-in coded message for first run */ 95 lstrcpyn(g_szMessages[0], _T("ReactOS"), MAXMSG_LENGTH); 96 ++g_nNumMessages; 97 } 98 99 RegCloseKey(hkey); 100 free(hugechar); 101 } 102 103 void SaveSettings() 104 { 105 HKEY hkey; 106 TCHAR *hugechar = malloc(4096 * sizeof(TCHAR)); 107 TCHAR *msgptr = hugechar; 108 int totallen = 0; 109 int i,len; 110 LONG value; 111 112 if(hugechar == 0) 113 return; 114 115 ZeroMemory(hugechar, 4096 * sizeof(TCHAR)); 116 msgptr = hugechar; 117 118 RegCreateKeyEx(HKEY_CURRENT_USER, _T("Software\\Catch22\\Matrix Screen Saver"), 0, 119 _T(""), 0, KEY_WRITE, NULL, &hkey, NULL); 120 121 value = g_nMessageSpeed; 122 RegSetValueEx(hkey, _T("MessageSpeed"), 0, REG_DWORD, (BYTE *)&value, sizeof value); 123 124 value = g_nMatrixSpeed; 125 RegSetValueEx(hkey, _T("MatrixSpeed"), 0, REG_DWORD, (BYTE *)&value, sizeof value); 126 127 value = g_nDensity; 128 RegSetValueEx(hkey, _T("Density"), 0, REG_DWORD, (BYTE *)&value, sizeof value); 129 130 value = g_nFontSize; 131 RegSetValueEx(hkey, _T("FontSize"), 0, REG_DWORD, (BYTE *)&value, sizeof value); 132 133 value = g_fRandomizeMessages; 134 RegSetValueEx(hkey, _T("Randomize"), 0, REG_DWORD, (BYTE *)&value, sizeof value); 135 136 value = g_fFontBold; 137 RegSetValueEx(hkey, _T("FontBold"), 0, REG_DWORD, (BYTE *)&value, sizeof value); 138 139 RegSetValueEx(hkey, _T("FontName"), 0, REG_SZ, (BYTE *)g_szFontName, lstrlen(g_szFontName) * sizeof(TCHAR)); 140 141 for(i = 0; i < g_nNumMessages; i++) 142 { 143 len = lstrlen(g_szMessages[i]); 144 145 if(len > 0 && totallen+len < 4096) 146 { 147 lstrcpyn(msgptr, g_szMessages[i], 4096-totallen); 148 totallen += len + 1; 149 msgptr += len + 1; 150 } 151 } 152 153 //*msgptr = _T('\0'); 154 totallen++; 155 156 RegSetValueEx(hkey, _T("Messages"), 0, REG_MULTI_SZ, (BYTE *)hugechar, totallen * sizeof(TCHAR)); 157 RegCloseKey(hkey); 158 159 free(hugechar); 160 } 161