aboutsummaryrefslogtreecommitdiff
path: root/Src/auth/Loginbox/common.cpp
blob: 2a92d830b0d0993e05ba876dcd317d36fb18b0ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include "./common.h"
#include "../api.h"

#include "../../winamp/accessibilityConfigGroup.h"

#include <shlwapi.h>
#include <strsafe.h>


LPWSTR LoginBox_MallocString(size_t cchLen)
{
	return (LPWSTR)calloc(cchLen, sizeof(WCHAR));
}

void LoginBox_FreeString(LPWSTR pszString)
{
	if (NULL != pszString)
	{
		free(pszString);
	}
}

void LoginBox_FreeStringSecure(LPWSTR pszString)
{
	if (NULL != pszString)
	{
		size_t size = LoginBox_GetAllocSize(pszString);
		if (0 != size) 
			SecureZeroMemory(pszString, size);

		free(pszString);
	}
}

LPWSTR LoginBox_ReAllocString(LPWSTR pszString, size_t cchLen)
{
	return (LPWSTR)realloc(pszString, sizeof(WCHAR) * cchLen);
}

LPWSTR LoginBox_CopyString(LPCWSTR pszSource)
{
	if (NULL == pszSource)
		return NULL;

	INT cchSource = lstrlenW(pszSource) + 1;
		
	LPWSTR copy = LoginBox_MallocString(cchSource);
	if (NULL != copy)
	{
		CopyMemory(copy, pszSource, sizeof(WCHAR) * cchSource);
	}
	return copy;
}

LPSTR LoginBox_MallocAnsiString(size_t cchLen)
{
	return (LPSTR)calloc(cchLen, sizeof(CHAR));
}

LPSTR LoginBox_CopyAnsiString(LPCSTR pszSource)
{
	if (NULL == pszSource)
		return NULL;

	INT cchSource = lstrlenA(pszSource) + 1;
		
	LPSTR copy = LoginBox_MallocAnsiString(cchSource);
	if (NULL != copy)
	{
		CopyMemory(copy, pszSource, sizeof(CHAR) * cchSource);
	}
	return copy;

}
void LoginBox_FreeAnsiString(LPSTR pszString)
{
	LoginBox_FreeString((LPWSTR)pszString);
}

void LoginBox_FreeAnsiStringSecure(LPSTR pszString)
{
	LoginBox_FreeStringSecure((LPWSTR)pszString);
}

size_t LoginBox_GetAllocSize(void *memory)
{
	return (NULL != memory) ? _msize(memory) : 0;
}

size_t LoginBox_GetStringMax(LPWSTR pszString)
{
	return LoginBox_GetAllocSize(pszString)/sizeof(WCHAR);
}

size_t LoginBox_GetAnsiStringMax(LPSTR pszString)
{
	return LoginBox_GetAllocSize(pszString)/sizeof(CHAR);
}

HRESULT LoginBox_WideCharToMultiByte(UINT codePage, DWORD dwFlags, LPCWSTR lpWideCharStr, INT cchWideChar, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar, LPSTR *ppResult)
{
	if (NULL == ppResult)
		return E_POINTER;

	INT resultMax = WideCharToMultiByte(codePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
	if (0 == resultMax) 
	{
		DWORD errorCode = GetLastError();
		*ppResult = NULL;
		return HRESULT_FROM_WIN32(errorCode);
	}

	if (cchWideChar > 0)
		resultMax++;


	*ppResult = LoginBox_MallocAnsiString(resultMax);
	if (NULL == *ppResult) return E_OUTOFMEMORY;
	resultMax = WideCharToMultiByte(codePage, dwFlags, lpWideCharStr, cchWideChar, *ppResult, resultMax, lpDefaultChar, lpUsedDefaultChar);
	if (0 == resultMax)
	{
		DWORD errorCode = GetLastError();
		LoginBox_FreeAnsiString(*ppResult);
		*ppResult = NULL;
		return HRESULT_FROM_WIN32(errorCode);
	}

	if (cchWideChar > 0)
		(*ppResult)[resultMax] = '\0';

	return S_OK;
}

HRESULT LoginBox_MultiByteToWideChar(UINT codePage, DWORD dwFlags, LPCSTR lpMultiByteStr, INT cbMultiByte, LPWSTR *ppResult)
{
	if (NULL == ppResult)
		return E_POINTER;

	INT resultMax = MultiByteToWideChar(codePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
	if (0 == resultMax) 
	{
		DWORD errorCode = GetLastError();
		*ppResult = NULL;
		return HRESULT_FROM_WIN32(errorCode);
	}

	if (cbMultiByte > 0)
		resultMax++;

	*ppResult = LoginBox_MallocString(resultMax);
	if (NULL == *ppResult) return E_OUTOFMEMORY;
	resultMax = MultiByteToWideChar(codePage, dwFlags, lpMultiByteStr, cbMultiByte, *ppResult, resultMax);
	if (0 == resultMax)
	{
		DWORD errorCode = GetLastError();
		LoginBox_FreeString(*ppResult);
		*ppResult = NULL;
		return HRESULT_FROM_WIN32(errorCode);
	}

	if (cbMultiByte > 0)
		(*ppResult)[resultMax] = L'\0';

	return S_OK;
}

HRESULT LoginBox_GetConfigPath(LPWSTR pszConfig, BOOL fEnsureExist)
{
	if (NULL == pszConfig) 
		return E_INVALIDARG;

	LPCWSTR pszWinamp;
	pszWinamp = (NULL != WASABI_API_APP) ? WASABI_API_APP->path_getUserSettingsPath(): NULL;
	if (NULL == pszWinamp) 
		return E_FAIL;	
	
	if (NULL == PathCombine(pszConfig, pszWinamp, L"Plugins\\loginBox"))
		return E_FAIL;
	
	if (FALSE != fEnsureExist)
	{
		HRESULT hr;
		hr = LoginBox_EnsurePathExist(pszConfig);
		if (FAILED(hr)) return hr;
	}
	return S_OK;
}

HRESULT LoginBox_EnsurePathExist(LPCWSTR pszDirectory)
{
	DWORD ec = ERROR_SUCCESS;
	UINT errorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);

	if (0 == CreateDirectory(pszDirectory, NULL))
	{
		ec = GetLastError();
		if (ERROR_PATH_NOT_FOUND == ec)
		{
			LPCWSTR pszBlock = pszDirectory;
			WCHAR szBuffer[MAX_PATH] = {0};
			
			LPCTSTR pszCursor = PathFindNextComponent(pszBlock);
			ec = (pszCursor == pszBlock || S_OK != StringCchCopyN(szBuffer, ARRAYSIZE(szBuffer), pszBlock, (pszCursor - pszBlock))) ?
					ERROR_INVALID_NAME : ERROR_SUCCESS;
			
			pszBlock = pszCursor;
			
			while (ERROR_SUCCESS == ec && NULL != (pszCursor = PathFindNextComponent(pszBlock)))
			{
				if (pszCursor == pszBlock || S_OK != StringCchCatN(szBuffer, ARRAYSIZE(szBuffer), pszBlock, (pszCursor - pszBlock)))
					ec = ERROR_INVALID_NAME;

				if (ERROR_SUCCESS == ec && !CreateDirectory(szBuffer, NULL))
				{
					ec = GetLastError();
					if (ERROR_ALREADY_EXISTS == ec) ec = ERROR_SUCCESS;
				}
				pszBlock = pszCursor;
			}
		}

		if (ERROR_ALREADY_EXISTS == ec) 
			ec = ERROR_SUCCESS;
	}

	SetErrorMode(errorMode);
	SetLastError(ec);
	return HRESULT_FROM_WIN32(ec);
}

HRESULT LoginBox_GetWindowText(HWND hwnd, LPWSTR *ppszText, UINT *pcchText)
{
	if (NULL == ppszText) return E_POINTER;
	if (NULL == hwnd) return E_INVALIDARG;
	
	UINT cchText = (UINT)SNDMSG(hwnd, WM_GETTEXTLENGTH, 0, 0L);
	
	cchText++;
	*ppszText = LoginBox_MallocString(cchText);
	if (NULL == *ppszText)
	{
		if (NULL != pcchText) *pcchText = 0;
		return E_OUTOFMEMORY;
	}
	
	cchText = (UINT)SNDMSG(hwnd, WM_GETTEXT, (WPARAM)cchText, (LPARAM)*ppszText);
	if (NULL != pcchText)
		*pcchText = cchText;
	
	return S_OK;
}

BOOL LoginBox_PrintWindow(HWND hwnd, HDC hdc, UINT flags)
{
	typedef BOOL (WINAPI *PRINTWINDOW)(HWND /*hwnd*/, HDC /*hdc*/, UINT /*nFlags*/);
	static PRINTWINDOW printWindow = NULL;
	static HMODULE moduleUser32 = NULL;
	if (NULL == moduleUser32)
	{
		moduleUser32 = GetModuleHandle(L"USER32");
		if (NULL == moduleUser32) return FALSE;
		
		printWindow = (PRINTWINDOW)GetProcAddress(moduleUser32, "PrintWindow");
	}
	
	return (NULL != printWindow && FALSE != printWindow(hwnd, hdc, flags));

}


BOOL LoginBox_MessageBeep(UINT beepType)
{
	BOOL result = FALSE;
	ifc_configitem *beepEnabled = AGAVE_API_CONFIG->GetItem(accessibilityConfigGroupGUID, L"modalbeep");
	if (NULL != beepEnabled)
	{
		if (false != beepEnabled->GetBool())
		{
			result = MessageBeep(beepType);
		}
		beepEnabled->Release();
	}

	return result;
}

HRESULT LoginBox_IsStringEqualEx(LCID locale, BOOL ignoreCase, LPCWSTR str1, LPCWSTR str2)
{
	if ((NULL == str1) != (NULL == str2))
		return S_FALSE;
	
	if (NULL != str1 && CSTR_EQUAL != CompareString(locale, (FALSE != ignoreCase) ? NORM_IGNORECASE : 0, str1, -1, str2, -1))
		return S_FALSE;

	return S_OK;
}

UINT LoginBox_GetCurrentTime()
{
	SYSTEMTIME st;
	FILETIME ft;

	GetSystemTime(&st);
	if(FALSE == SystemTimeToFileTime(&st, &ft))
		return 0;
	
	ULARGE_INTEGER t1;
	t1.LowPart = ft.dwLowDateTime;
	t1.HighPart = ft.dwHighDateTime;
	
	return (UINT)((t1.QuadPart - 116444736000000000) / 10000000);
}

HRESULT LoginBox_GetCurrentLang(LPSTR *ppLang)
{
	if (NULL == ppLang)
		return E_POINTER;

	if (NULL == WASABI_API_LNG)
		return E_UNEXPECTED;
	
	LPCWSTR lang = WASABI_API_LNG->GetLanguageIdentifier(LANG_LANG_CODE);
	
	if (NULL != lang && L'\0' != *lang)
		return LoginBox_WideCharToMultiByte(CP_UTF8, 0, lang, -1, NULL, NULL, ppLang);
	
	*ppLang = NULL;
	return S_OK;
	
}

HDWP LoginBox_LayoutButtonBar(HDWP hdwp, HWND hwnd, const INT *buttonList, UINT buttonCount, const RECT *prcClient, LONG buttonHeight, LONG buttonSpace, BOOL fRedraw, RECT *prcResult)
{
	if (NULL == hdwp && NULL == prcClient)
		return NULL;
	
	RECT rect;
	CopyRect(&rect, prcClient);
	
	LONG top = rect.bottom - buttonHeight;
	if (top < rect.top) top = rect.top;
	LONG height = rect.bottom - top;
	LONG width;
	LONG right = rect.right;

	if (NULL == buttonList || 0 == buttonCount)
	{
		if (NULL != prcResult)
			SetRect(prcResult, right, top, rect.right, top + height);

		return (NULL != hdwp) ? hdwp :(HDWP)TRUE;
	}


	UINT flags = SWP_NOACTIVATE | SWP_NOZORDER;
	if (FALSE == fRedraw) flags |= SWP_NOREDRAW;

	WCHAR szText[256] = {0};
	INT cchText;
	HFONT font(NULL), fontOrig;
	SIZE textSize;

	RECT buttonRect;
	while(buttonCount--)
	{
		HWND hControl = GetDlgItem(hwnd, buttonList[buttonCount]);
		if (NULL == hControl || 0 == (WS_VISIBLE & GetWindowStyle(hControl)) || 
			FALSE == GetWindowRect(hControl, &buttonRect))
		{
			continue;
		}

		if (right != rect.right)
			 right -= buttonSpace;

		width = buttonRect.right - buttonRect.left;

		cchText = (INT)SendMessage(hControl, WM_GETTEXT, (WPARAM)ARRAYSIZE(szText), (LPARAM)szText);
		if (cchText > 0)
		{
			HDC hdc = GetDCEx(hControl, NULL,  DCX_CACHE | DCX_WINDOW | DCX_NORESETATTRS);
			if (NULL != hdc)
			{
				if (NULL == font)
					font = (HFONT)SendMessage(hControl, WM_GETFONT, 0, 0L);

				fontOrig = (HFONT)SelectObject(hdc, font);

				if (FALSE != GetTextExtentPoint32W(hdc, szText, cchText, &textSize))
				{
					width = textSize.cx + 4*LoginBox_GetAveCharWidth(hdc);
				}

				SelectObject(hdc, fontOrig);
				ReleaseDC(hControl, hdc);
			}
		}
		
		if (width < 75) 
			width = 75;

		if (NULL != hdwp)
		{
			hdwp = DeferWindowPos(hdwp, hControl, NULL, right - width, top, width, height, flags);
			if (NULL == hdwp) return NULL;
		}

		right -= width;
	}

	if (NULL != prcResult)
		SetRect(prcResult, right, top, rect.right, top + height);

	return (NULL != hdwp) ? hdwp :(HDWP)TRUE;
}

BYTE LoginBox_GetSysFontQuality()
{
	BOOL smoothingEnabled;
	if (FALSE == SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &smoothingEnabled, 0) ||
		FALSE == smoothingEnabled)
	{
		return DEFAULT_QUALITY;
	}
    
    OSVERSIONINFO vi;
    vi.dwOSVersionInfoSize = sizeof(vi);
    if (FALSE == GetVersionEx(&vi)) 
		return DEFAULT_QUALITY;

	if (vi.dwMajorVersion > 5 || (vi.dwMajorVersion == 5 && vi.dwMinorVersion >= 1))
	{
		UINT smootingType;
	    if (FALSE == SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &smootingType, 0))
			return DEFAULT_QUALITY;
	    
	    if (FE_FONTSMOOTHINGCLEARTYPE == smootingType)
			return CLEARTYPE_QUALITY;
	}

	return ANTIALIASED_QUALITY;
}

INT LoginBox_GetAveStrWidth(HDC hdc, INT cchLen)
{
	const char szTest[] = 
	{ 
		'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z',
		'a','b','c','d','e','f','g','h','i','j','k','l', 'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
	};

	SIZE textSize;
	if (FALSE == GetTextExtentPointA(hdc, szTest, ARRAYSIZE(szTest) -1, &textSize))
		return 0;

	INT result;
	if (1 == cchLen)
	{
		result = (textSize.cx + ARRAYSIZE(szTest)/2)/ARRAYSIZE(szTest);
	}
	else
	{
		result = MulDiv(cchLen, textSize.cx + ARRAYSIZE(szTest)/2, ARRAYSIZE(szTest));
		if (0 != result)
		{
			TEXTMETRIC tm;
			if (FALSE != GetTextMetrics(hdc, &tm))
				result += tm.tmOverhang;
		}
	}
	return result;
}

INT LoginBox_GetAveCharWidth(HDC hdc)
{
	return LoginBox_GetAveStrWidth(hdc, 1);
}

BOOL LoginBox_GetWindowBaseUnits(HWND hwnd, INT *pBaseUnitX, INT *pBaseUnitY)
{
	INT baseunitX(0), baseunitY(0);
	BOOL result = FALSE;

	if (NULL != hwnd)
	{		
		HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
		if (NULL != hdc)
		{
			HFONT font = (HFONT)SNDMSG(hwnd, WM_GETFONT, 0, 0L);
			HFONT fontOrig = (HFONT)SelectObject(hdc, font);
			
			TEXTMETRIC tm;
			if (FALSE != GetTextMetrics(hdc, &tm))
			{
				baseunitY = tm.tmHeight;
				baseunitX = LoginBox_GetAveCharWidth(hdc);
				result = TRUE;
			}

			SelectObject(hdc, fontOrig);
			ReleaseDC(hwnd, hdc);
		}
	}

	if (NULL != pBaseUnitX) *pBaseUnitX = baseunitX;
	if (NULL != pBaseUnitY) *pBaseUnitY = baseunitY;

	return result;

}

INT LoginBox_GetWindowTextHeight(HWND hwnd, INT paddingDlgUnit)
{
	if (NULL == hwnd) return 0;
	
	HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
	if (NULL == hdc) return 0;

	INT height = 0;

	HFONT font = (HFONT)SNDMSG(hwnd, WM_GETFONT, 0, 0L);
	HFONT fontOrig = (HFONT)SelectObject(hdc, font);

	TEXTMETRIC tm;
	if (FALSE != GetTextMetrics(hdc, &tm))
	{
		height = tm.tmHeight;
		if (0 != paddingDlgUnit)
			height += MulDiv(2 * paddingDlgUnit, tm.tmHeight, 8);
	}


	SelectObject(hdc, fontOrig);
	ReleaseDC(hwnd, hdc);

	return height;

}
BOOL LoginBox_GetWindowTextSize(HWND hwnd, INT idealWidth, INT *pWidth, INT *pHeight)
{
	INT width(0), height(0);
	BOOL result = FALSE;

	if (NULL != hwnd)
	{		
		HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
		if (NULL != hdc)
		{
			HFONT font = (HFONT)SNDMSG(hwnd, WM_GETFONT, 0, 0L);
			HFONT fontOrig = (HFONT)SelectObject(hdc, font);
			
			LPWSTR pszText;
			UINT cchText;
			if (SUCCEEDED(LoginBox_GetWindowText(hwnd, &pszText, &cchText)))
			{
				if (0 == cchText)
				{
					TEXTMETRIC tm;
					if (FALSE != GetTextMetrics(hdc, &tm))
					{
						height = tm.tmHeight;
						width = 0;
						result = TRUE;
					}
				}
				else
				{
					RECT rect;
					SetRect(&rect, 0, 0, idealWidth, 0);
					if (0 != DrawText(hdc, pszText, cchText, &rect, DT_CALCRECT | DT_NOPREFIX | DT_WORDBREAK))
					{
						width = rect.right - rect.left;
						height = rect.bottom  - rect.top;
						result = TRUE;
					}

				}
				LoginBox_FreeString(pszText);
			}

			SelectObject(hdc, fontOrig);
			ReleaseDC(hwnd, hdc);
		}
	}

	if (NULL != pWidth) *pWidth = width;
	if (NULL != pHeight) *pHeight = height;

	return result;
}

BOOL LoginBox_OpenUrl(HWND hOwner, LPCWSTR pszUrl, BOOL forceExternal)
{
	if (NULL == WASABI_API_WINAMP)
		return FALSE;

	HCURSOR hCursor = LoadCursor(NULL, IDC_APPSTARTING);
	if (NULL != hCursor) 
		hCursor = SetCursor(hCursor);

	BOOL result;

	if (FALSE != forceExternal)
	{
		HINSTANCE hInst = ShellExecute(hOwner, L"open", pszUrl, NULL, NULL, SW_SHOWNORMAL);
		result = ((INT_PTR)hInst > 32) ? TRUE: FALSE;
	}
	else
	{
		HRESULT hr = WASABI_API_WINAMP->OpenUrl(hOwner, pszUrl);
		result = SUCCEEDED(hr);
	}
		
	if (NULL != hCursor) 
		SetCursor(hCursor);

	return result;
}