| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- #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;
- }
|