1 #include "burner.h"
2 
3 bool bEnableHighResTimer = true; // default value
4 
5 bool bIsWindowsXPorGreater = false;
6 bool bIsWindowsXP = false;
7 bool bIsWindows8OrGreater = false;
8 
9 // Detect if we are using Windows XP/Vista/7
DetectWindowsVersion()10 BOOL DetectWindowsVersion()
11 {
12     OSVERSIONINFO osvi;
13     BOOL bIsWindowsXPorLater;
14 
15     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
16     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
17 
18     GetVersionEx(&osvi);
19 
20 	// osvi.dwMajorVersion returns the windows version: 5 = XP 6 = Vista/7
21     // osvi.dwMinorVersion returns the minor version, XP and 7 = 1, Vista = 0
22     bIsWindowsXPorLater = ((osvi.dwMajorVersion > 5) || ( (osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion >= 1)));
23 	bIsWindowsXP = (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1);
24 	bIsWindows8OrGreater = ((osvi.dwMajorVersion > 6) || ((osvi.dwMajorVersion == 6) && (osvi.dwMinorVersion >= 2)));
25 
26 	return bIsWindowsXPorLater;
27 }
28 
29 // Set the current directory to be the application's directory
AppDirectory()30 int AppDirectory()
31 {
32 	TCHAR szPath[MAX_PATH] = _T("");
33 	int nLen = 0;
34 	TCHAR *pc1, *pc2;
35 	TCHAR* szCmd = GetCommandLine();
36 
37 	// Find the end of the "c:\directory\program.exe" bit
38 	if (szCmd[0] == _T('\"')) {						// Filename is enclosed in quotes
39 		szCmd++;
40 		for (pc1 = szCmd; *pc1; pc1++) {
41 			if (*pc1 == _T('\"')) break;			// Find the last "
42 		}
43 	} else {
44 		for (pc1 = szCmd; *pc1; pc1++) {
45 			if (*pc1 == _T(' ')) break;				// Find the first space
46 		}
47 	}
48 	// Find the last \ or /
49 	for (pc2 = pc1; pc2 >= szCmd; pc2--) {
50 		if (*pc2 == _T('\\')) break;
51 		if (*pc2 == _T('/')) break;
52 	}
53 
54 	// Copy the name of the executable into a variable
55 	nLen = pc1 - pc2 - 1;
56 	if (nLen > EXE_NAME_SIZE) {
57 		nLen = EXE_NAME_SIZE;
58 	}
59 	_tcsncpy(szAppExeName, pc2 + 1, nLen);
60 	szAppExeName[nLen] = 0;
61 
62 	// strip .exe
63 	if ((pc1 = _tcschr(szAppExeName, _T('.'))) != 0) {
64 		*pc1 = 0;
65 	}
66 
67 	nLen = pc2 - szCmd;
68 	if (nLen <= 0) return 1;			// No path
69 
70 	// Now copy the path into a new buffer
71 	_tcsncpy(szPath, szCmd, nLen);
72 	SetCurrentDirectory(szPath);		// Finally set the current directory to be the application's directory
73 
74 	dprintf(szPath);
75 	dprintf(_T("\n"));
76 
77 	return 0;
78 }
79 
UpdatePath(TCHAR * path)80 void UpdatePath(TCHAR* path)
81 {
82 	int pathlen = _tcslen(path);
83 	if (pathlen) {
84 		DWORD attrib = INVALID_FILE_ATTRIBUTES;
85 		TCHAR curdir[MAX_PATH] = _T("");
86 		int curlen = 0;
87 
88 		attrib = GetFileAttributes(path);
89 		if (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) && path[pathlen - 1] != _T('\\')) {
90 			path[pathlen] = _T('\\');
91 			path[pathlen + 1] = _T('\0');
92 
93 			pathlen++;
94 		}
95 
96 		GetCurrentDirectory(sizeof(curdir), curdir);
97 		curlen = _tcslen(curdir);
98 
99 		if (_tcsnicmp(curdir, path, curlen) == 0 && path[curlen] == _T('\\')) {
100 			TCHAR newpath[MAX_PATH];
101 
102 			_tcscpy(newpath, path + curlen + 1);
103 			_tcscpy(path, newpath);
104 		}
105 	}
106 }
107 
108 // ---------------------------------------------------------------------------
109 
MyRegCreateKeys(int nDepth,TCHAR * pNames[],HKEY * pResult)110 static void MyRegCreateKeys(int nDepth, TCHAR* pNames[], HKEY* pResult)
111 {
112 	for (int i = 0; i < nDepth; i++) {
113 		pResult[i] = NULL;
114 		RegCreateKeyEx((i ? pResult[i - 1] : HKEY_CLASSES_ROOT), pNames[i], 0, _T(""), 0, KEY_WRITE, NULL, &pResult[i], NULL);
115 	}
116 }
117 
MyRegCloseKeys(int nDepth,HKEY * pKeys)118 static void MyRegCloseKeys(int nDepth, HKEY* pKeys)
119 {
120 	for (int i = nDepth - 1; i >= 0; i--) {
121 		if (pKeys[i]) {
122 			RegCloseKey(pKeys[i]);
123 		}
124 	}
125 }
126 
MyRegDeleteKeys(int nDepth,TCHAR * pNames[],HKEY * pKeys)127 static void MyRegDeleteKeys(int nDepth, TCHAR* pNames[], HKEY* pKeys)
128 {
129 	for (int i = 0; i < nDepth - 1; i++) {
130 		pKeys[i] = NULL;
131 		RegOpenKeyEx((i ? pKeys[i - 1] : HKEY_CLASSES_ROOT), pNames[i], 0, 0, &pKeys[i]);
132 	}
133 	for (int i = nDepth - 1; i >= 0; i--) {
134 		RegDeleteKey((i ? pKeys[i - 1] : HKEY_CLASSES_ROOT), pNames[i]);
135 		if (i) {
136 			RegCloseKey(pKeys[i - 1]);
137 		}
138 	}
139 }
140 
RegisterExtensions(bool bCreateKeys)141 void RegisterExtensions(bool bCreateKeys)
142 {
143 	HKEY myKeys[4];
144 
145 	TCHAR* myKeynames1[1] = { _T(".fr") };
146 	TCHAR* myKeynames2[1] = { _T(".fs") };
147 	TCHAR* myKeynames3[4] = { _T("FBAlpha"), _T("shell"), _T("open"), _T("command") };
148 	TCHAR* myKeynames4[2] = { _T("FBAlpha"), _T("DefaultIcon") };
149 	TCHAR myKeyValue[MAX_PATH + 32] = _T("");
150 
151 	if (bCreateKeys) {
152 		TCHAR szExename[MAX_PATH] = _T("");
153 		GetModuleFileName(NULL, szExename, MAX_PATH);
154 
155 		MyRegCreateKeys(1, myKeynames1, myKeys);
156 		_stprintf(myKeyValue, _T("FBAlpha"));
157 		RegSetValueEx(myKeys[0], NULL, 0, REG_SZ, (BYTE*)myKeyValue, (_tcslen(myKeyValue) + 1) * sizeof(TCHAR));
158 		MyRegCloseKeys(2, myKeys);
159 
160 		MyRegCreateKeys(1, myKeynames2, myKeys);
161 		_stprintf(myKeyValue, _T("FBAlpha"));
162 		RegSetValueEx(myKeys[0], NULL, 0, REG_SZ, (BYTE*)myKeyValue, (_tcslen(myKeyValue) + 1) * sizeof(TCHAR));
163 		MyRegCloseKeys(2, myKeys);
164 
165 		MyRegCreateKeys(4, myKeynames3, myKeys);
166 		_stprintf(myKeyValue, _T("\"%s\" \"%%1\" -w"), szExename);
167 		RegSetValueEx(myKeys[3], NULL, 0, REG_SZ, (BYTE*)myKeyValue, (_tcslen(myKeyValue) + 1) * sizeof(TCHAR));
168 		_stprintf(myKeyValue, _T("FB Alpha file"));
169 		RegSetValueEx(myKeys[0], NULL, 0, REG_SZ, (BYTE*)myKeyValue, (_tcslen(myKeyValue) + 1) * sizeof(TCHAR));
170 		MyRegCloseKeys(4, myKeys);
171 
172 		MyRegCreateKeys(2, myKeynames4, myKeys);
173 		_stprintf(myKeyValue, _T("\"%s\", 0"), szExename);
174 		RegSetValueEx(myKeys[1], NULL, 0, REG_SZ, (BYTE*)myKeyValue, (_tcslen(myKeyValue) + 1) * sizeof(TCHAR));
175 		MyRegCloseKeys(2, myKeys);
176 	} else {
177 		MyRegDeleteKeys(2, myKeynames4, myKeys);
178 		MyRegDeleteKeys(4, myKeynames3, myKeys);
179 		MyRegDeleteKeys(1, myKeynames2, myKeys);
180 		MyRegDeleteKeys(1, myKeynames1, myKeys);
181 	}
182 
183 	return;
184 }
185 
186 // ---------------------------------------------------------------------------
187 
188 // Get the position of the client area of a window on the screen
GetClientScreenRect(HWND hWnd,RECT * pRect)189 int GetClientScreenRect(HWND hWnd, RECT *pRect)
190 {
191 	POINT Corner = {0, 0};
192 
193 	GetClientRect(hWnd, pRect);
194 	ClientToScreen(hWnd, &Corner);
195 
196 	pRect->left += Corner.x;
197 	pRect->right += Corner.x;
198 	pRect->top += Corner.y;
199 	pRect->bottom += Corner.y;
200 
201 	return 0;
202 }
203 
204 // Put a window in the middle of another window
WndInMid(HWND hMid,HWND hBase)205 int WndInMid(HWND hMid, HWND hBase)
206 {
207 	RECT MidRect = {0, 0, 0, 0};
208 	int mw = 0, mh = 0;
209 	RECT BaseRect = {0, 0, 0, 0};
210 	int bx = 0, by = 0;
211 
212 	// Find the height and width of the Mid window
213 	GetWindowRect(hMid, &MidRect);
214 	mw = MidRect.right - MidRect.left;
215 	mh = MidRect.bottom - MidRect.top;
216 
217 	// Find the center of the Base window
218 	if (hBase && IsWindowVisible(hBase)) {
219 		GetWindowRect(hBase, &BaseRect);
220 		if (hBase == hScrnWnd) {
221 			// For the main window, center in the client area.
222 			BaseRect.left += GetSystemMetrics(SM_CXSIZEFRAME);
223 			BaseRect.right -= GetSystemMetrics(SM_CXSIZEFRAME);
224 			BaseRect.top += GetSystemMetrics(SM_CYSIZEFRAME);
225 			if (bMenuEnabled) {
226 				BaseRect.top += GetSystemMetrics(SM_CYCAPTION) + nMenuHeight;
227 			}
228 			BaseRect.bottom -= GetSystemMetrics(SM_CYSIZEFRAME);
229 		}
230 	} else {
231 		SystemParametersInfo(SPI_GETWORKAREA, 0, &BaseRect, 0);
232 	}
233 
234 	bx = BaseRect.left + BaseRect.right;
235 	bx = (bx - mw) >> 1;
236 	by = BaseRect.top + BaseRect.bottom;
237 	by = (by - mh) >> 1;
238 
239 	if (hBase) {
240 		SystemParametersInfo(SPI_GETWORKAREA, 0, &SystemWorkArea, 0);
241 
242 		if (bx + mw > SystemWorkArea.right) {
243 			bx = SystemWorkArea.right - mw;
244 		}
245 		if (by + mh > SystemWorkArea.bottom) {
246 			by = SystemWorkArea.bottom - mh;
247 		}
248 		if (bx < SystemWorkArea.left) {
249 			bx = SystemWorkArea.left;
250 		}
251 		if (by < SystemWorkArea.top) {
252 			by = SystemWorkArea.top;
253 		}
254 	}
255 
256 	// Center the window
257 	SetWindowPos(hMid, NULL, bx, by, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
258 
259 	return 0;
260 }
261 
262 // ---------------------------------------------------------------------------
263 
264 // Set-up high resolution timing
265 
266 static int bHighResolutionTimerActive = 0;
267 
EnableHighResolutionTiming()268 void EnableHighResolutionTiming()
269 {
270 	TIMECAPS hTCaps;
271 
272 	bHighResolutionTimerActive = 0;
273 
274 	if (bEnableHighResTimer) {
275 #ifdef PRINT_DEBUG_INFO
276 		dprintf(_T(" ** Enabling High-Resolution system timer.\n"));
277 #endif
278 
279 		if (timeGetDevCaps(&hTCaps, sizeof(hTCaps)) == TIMERR_NOERROR) {
280 			bHighResolutionTimerActive = hTCaps.wPeriodMin;
281 			timeBeginPeriod(hTCaps.wPeriodMin);
282 		}
283 	}
284 }
285 
DisableHighResolutionTiming()286 void DisableHighResolutionTiming()
287 {
288 	if (bHighResolutionTimerActive) {
289 		timeEndPeriod(bHighResolutionTimerActive);
290 		bHighResolutionTimerActive = 0;
291 	}
292 }
293