michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CEHHelper.h" michael@0: #include michael@0: #include "mozilla/widget/MetroD3DCheckHelper.h" michael@0: michael@0: #ifdef SHOW_CONSOLE michael@0: #include // _open_osfhandle michael@0: #endif michael@0: michael@0: HANDLE sCon; michael@0: LPCWSTR metroDX10Available = L"MetroD3DAvailable"; michael@0: LPCWSTR metroLastAHE = L"MetroLastAHE"; michael@0: LPCWSTR cehDumpDebugStrings = L"CEHDump"; michael@0: extern const WCHAR* kFirefoxExe; michael@0: michael@0: void michael@0: Log(const wchar_t *fmt, ...) michael@0: { michael@0: #if !defined(SHOW_CONSOLE) michael@0: DWORD dwRes = 0; michael@0: if (!GetDWORDRegKey(cehDumpDebugStrings, dwRes) || !dwRes) { michael@0: return; michael@0: } michael@0: #endif michael@0: va_list a = nullptr; michael@0: wchar_t szDebugString[1024]; michael@0: if(!lstrlenW(fmt)) michael@0: return; michael@0: va_start(a,fmt); michael@0: vswprintf(szDebugString, 1024, fmt, a); michael@0: va_end(a); michael@0: if(!lstrlenW(szDebugString)) michael@0: return; michael@0: michael@0: DWORD len; michael@0: WriteConsoleW(sCon, szDebugString, lstrlenW(szDebugString), &len, nullptr); michael@0: WriteConsoleW(sCon, L"\n", 1, &len, nullptr); michael@0: michael@0: OutputDebugStringW(szDebugString); michael@0: OutputDebugStringW(L"\n"); michael@0: } michael@0: michael@0: #if defined(SHOW_CONSOLE) michael@0: void michael@0: SetupConsole() michael@0: { michael@0: FILE *fp; michael@0: AllocConsole(); michael@0: sCon = GetStdHandle(STD_OUTPUT_HANDLE); michael@0: int fd = _open_osfhandle(reinterpret_cast(sCon), 0); michael@0: fp = _fdopen(fd, "w"); michael@0: *stdout = *fp; michael@0: setvbuf(stdout, nullptr, _IONBF, 0); michael@0: } michael@0: #endif michael@0: michael@0: bool michael@0: IsImmersiveProcessDynamic(HANDLE process) michael@0: { michael@0: HMODULE user32DLL = LoadLibraryW(L"user32.dll"); michael@0: if (!user32DLL) { michael@0: return false; michael@0: } michael@0: michael@0: decltype(IsImmersiveProcess)* IsImmersiveProcessPtr = michael@0: (decltype(IsImmersiveProcess)*) GetProcAddress(user32DLL, michael@0: "IsImmersiveProcess"); michael@0: if (!IsImmersiveProcessPtr) { michael@0: FreeLibrary(user32DLL); michael@0: return false; michael@0: } michael@0: michael@0: BOOL bImmersiveProcess = IsImmersiveProcessPtr(process); michael@0: FreeLibrary(user32DLL); michael@0: return bImmersiveProcess; michael@0: } michael@0: michael@0: bool michael@0: IsProcessRunning(const wchar_t *processName, bool bCheckIfMetro) michael@0: { michael@0: bool exists = false; michael@0: PROCESSENTRY32W entry; michael@0: entry.dwSize = sizeof(PROCESSENTRY32W); michael@0: michael@0: HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); michael@0: michael@0: if (Process32First(snapshot, &entry)) { michael@0: while (!exists && Process32Next(snapshot, &entry)) { michael@0: if (!_wcsicmp(entry.szExeFile, processName)) { michael@0: HANDLE process = OpenProcess(GENERIC_READ, FALSE, entry.th32ProcessID); michael@0: bool isImmersiveProcess = IsImmersiveProcessDynamic(process); michael@0: if ((bCheckIfMetro && isImmersiveProcess) || michael@0: (!bCheckIfMetro && !isImmersiveProcess)) { michael@0: exists = true; michael@0: } michael@0: CloseHandle(process); michael@0: } michael@0: } michael@0: } michael@0: michael@0: CloseHandle(snapshot); michael@0: return exists; michael@0: } michael@0: michael@0: bool michael@0: IsMetroProcessRunning() michael@0: { michael@0: return IsProcessRunning(kFirefoxExe, true); michael@0: } michael@0: michael@0: bool michael@0: IsDesktopProcessRunning() michael@0: { michael@0: return IsProcessRunning(kFirefoxExe, false); michael@0: } michael@0: michael@0: /* michael@0: * Retrieve the last front end ui we launched so we can target it michael@0: * again. This value is updated down in nsAppRunner when the browser michael@0: * starts up. michael@0: */ michael@0: AHE_TYPE michael@0: GetLastAHE() michael@0: { michael@0: DWORD ahe; michael@0: if (GetDWORDRegKey(metroLastAHE, ahe)) { michael@0: return (AHE_TYPE) ahe; michael@0: } michael@0: return AHE_DESKTOP; michael@0: } michael@0: michael@0: bool michael@0: IsDX10Available() michael@0: { michael@0: DWORD isDX10Available; michael@0: if (GetDWORDRegKey(metroDX10Available, isDX10Available)) { michael@0: return isDX10Available; michael@0: } michael@0: bool check = D3DFeatureLevelCheck(); michael@0: SetDWORDRegKey(metroDX10Available, check); michael@0: return check; michael@0: } michael@0: michael@0: bool michael@0: GetDWORDRegKey(LPCWSTR name, DWORD &value) michael@0: { michael@0: HKEY key; michael@0: LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Mozilla\\Firefox", michael@0: 0, KEY_READ, &key); michael@0: if (result != ERROR_SUCCESS) { michael@0: return false; michael@0: } michael@0: michael@0: DWORD bufferSize = sizeof(DWORD); michael@0: DWORD type; michael@0: result = RegQueryValueExW(key, name, nullptr, &type, michael@0: reinterpret_cast(&value), michael@0: &bufferSize); michael@0: RegCloseKey(key); michael@0: return result == ERROR_SUCCESS; michael@0: } michael@0: michael@0: bool michael@0: SetDWORDRegKey(LPCWSTR name, DWORD value) michael@0: { michael@0: HKEY key; michael@0: LONG result = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Mozilla\\Firefox", michael@0: 0, KEY_WRITE, &key); michael@0: if (result != ERROR_SUCCESS) { michael@0: return false; michael@0: } michael@0: michael@0: result = RegSetValueEx(key, name, 0, REG_DWORD, michael@0: reinterpret_cast(&value), michael@0: sizeof(DWORD)); michael@0: RegCloseKey(key); michael@0: return result == ERROR_SUCCESS; michael@0: }