2021-06-06 01:26:18 +00:00
|
|
|
/**
|
|
|
|
* Looking Glass
|
|
|
|
* Copyright (C) 2017-2021 The Looking Glass Authors
|
|
|
|
* https://looking-glass.io
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
|
|
|
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
#include "platform.h"
|
2020-08-11 02:22:22 +00:00
|
|
|
#include "service.h"
|
2021-07-17 04:55:22 +00:00
|
|
|
#include "windows/delay.h"
|
2019-04-10 11:07:56 +00:00
|
|
|
#include "windows/mousehook.h"
|
2019-05-09 09:30:09 +00:00
|
|
|
|
2019-02-28 08:20:35 +00:00
|
|
|
#include <windows.h>
|
2019-05-09 09:30:09 +00:00
|
|
|
#include <shellapi.h>
|
2021-01-29 01:39:05 +00:00
|
|
|
#include <shlwapi.h>
|
2019-12-13 12:22:11 +00:00
|
|
|
#include <fcntl.h>
|
2021-02-03 22:42:54 +00:00
|
|
|
#include <powrprof.h>
|
|
|
|
#include <ntstatus.h>
|
2021-03-18 04:12:40 +00:00
|
|
|
#include <wtsapi32.h>
|
|
|
|
#include <userenv.h>
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-04-09 06:28:11 +00:00
|
|
|
#include "interface/platform.h"
|
2019-04-11 01:03:30 +00:00
|
|
|
#include "common/debug.h"
|
2020-01-02 11:21:42 +00:00
|
|
|
#include "common/windebug.h"
|
2019-05-10 09:48:38 +00:00
|
|
|
#include "common/option.h"
|
2019-12-17 05:36:43 +00:00
|
|
|
#include "common/locking.h"
|
2020-01-02 11:21:42 +00:00
|
|
|
#include "common/thread.h"
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2020-08-11 07:44:32 +00:00
|
|
|
#define ID_MENU_SHOW_LOG 3000
|
2019-05-21 07:52:58 +00:00
|
|
|
#define ID_MENU_EXIT 3001
|
2021-01-29 01:39:05 +00:00
|
|
|
#define LOG_NAME "looking-glass-host.txt"
|
2019-05-21 07:52:58 +00:00
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
struct AppState
|
|
|
|
{
|
2019-12-17 03:59:58 +00:00
|
|
|
LARGE_INTEGER perfFreq;
|
2019-05-21 07:52:58 +00:00
|
|
|
HINSTANCE hInst;
|
|
|
|
|
2019-05-09 09:32:19 +00:00
|
|
|
int argc;
|
|
|
|
char ** argv;
|
|
|
|
|
2020-08-11 02:22:22 +00:00
|
|
|
char executable[MAX_PATH + 1];
|
2021-01-29 01:39:05 +00:00
|
|
|
char systemLogDir[MAX_PATH];
|
2020-08-11 02:22:22 +00:00
|
|
|
HWND messageWnd;
|
|
|
|
NOTIFYICONDATA iconData;
|
|
|
|
UINT trayRestartMsg;
|
|
|
|
HMENU trayMenu;
|
2021-07-19 10:16:27 +00:00
|
|
|
|
|
|
|
HANDLE exitThreadEvent;
|
|
|
|
LGThread * exitThread;
|
2019-05-09 09:30:09 +00:00
|
|
|
};
|
|
|
|
|
2020-01-03 03:53:56 +00:00
|
|
|
static struct AppState app = {0};
|
2020-04-24 10:34:58 +00:00
|
|
|
HWND MessageHWND;
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2021-03-18 04:12:40 +00:00
|
|
|
CreateProcessAsUserA_t f_CreateProcessAsUserA = NULL;
|
|
|
|
|
|
|
|
bool windowsSetupAPI(void)
|
|
|
|
{
|
|
|
|
/* first look in kernel32.dll */
|
|
|
|
HMODULE mod;
|
|
|
|
|
|
|
|
mod = GetModuleHandleA("kernel32.dll");
|
|
|
|
if (mod)
|
|
|
|
{
|
|
|
|
f_CreateProcessAsUserA = (CreateProcessAsUserA_t)
|
|
|
|
GetProcAddress(mod, "CreateProcessAsUserA");
|
|
|
|
if (f_CreateProcessAsUserA)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod = GetModuleHandleA("advapi32.dll");
|
|
|
|
if (mod)
|
|
|
|
{
|
|
|
|
f_CreateProcessAsUserA = (CreateProcessAsUserA_t)
|
|
|
|
GetProcAddress(mod, "CreateProcessAsUserA");
|
|
|
|
if (f_CreateProcessAsUserA)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
static void RegisterTrayIcon(void)
|
2020-08-11 02:22:22 +00:00
|
|
|
{
|
|
|
|
// register our TrayIcon
|
|
|
|
if (!app.iconData.cbSize)
|
|
|
|
{
|
|
|
|
app.iconData.cbSize = sizeof(NOTIFYICONDATA);
|
|
|
|
app.iconData.hWnd = app.messageWnd;
|
|
|
|
app.iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
|
|
|
|
app.iconData.uCallbackMessage = WM_TRAYICON;
|
|
|
|
strncpy(app.iconData.szTip, "Looking Glass (host)", sizeof(app.iconData.szTip));
|
|
|
|
app.iconData.hIcon = LoadIcon(app.hInst, IDI_APPLICATION);
|
|
|
|
}
|
|
|
|
Shell_NotifyIcon(NIM_ADD, &app.iconData);
|
|
|
|
}
|
|
|
|
|
2021-03-18 04:12:40 +00:00
|
|
|
// This function executes notepad as the logged in user, and therefore is secure to use.
|
|
|
|
static bool OpenLogFile(const char * logFile)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
DWORD console = WTSGetActiveConsoleSessionId();
|
|
|
|
if (console == 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to get active console session ID", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WTS_CONNECTSTATE_CLASS * state;
|
|
|
|
DWORD size;
|
|
|
|
if (!WTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, console, WTSConnectState,
|
|
|
|
(LPSTR *) &state, &size))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to get session information", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*state != WTSActive)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Will not open log file because user is not logged in", GetLastError());
|
|
|
|
WTSFreeMemory(state);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
WTSFreeMemory(state);
|
|
|
|
|
|
|
|
char system32[MAX_PATH];
|
|
|
|
if (!GetSystemDirectoryA(system32, MAX_PATH))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to get system directory", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!f_CreateProcessAsUserA && !windowsSetupAPI())
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to get CreateProcessAsUserA", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE hToken;
|
|
|
|
if (!WTSQueryUserToken(console, &hToken))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to get active console session user token", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPVOID env;
|
|
|
|
if (!CreateEnvironmentBlock(&env, hToken, FALSE))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to create environment", GetLastError());
|
|
|
|
goto fail_token;
|
|
|
|
}
|
|
|
|
|
|
|
|
char notepad[MAX_PATH];
|
|
|
|
PathCombineA(notepad, system32, "notepad.exe");
|
|
|
|
|
|
|
|
char cmdline[MAX_PATH + 10];
|
|
|
|
snprintf(cmdline, sizeof(cmdline), "notepad \"%s\"", logFile);
|
|
|
|
|
|
|
|
STARTUPINFO si = { .cb = sizeof(STARTUPINFO) };
|
|
|
|
PROCESS_INFORMATION pi = {0};
|
|
|
|
if (!f_CreateProcessAsUserA(
|
|
|
|
hToken,
|
|
|
|
notepad,
|
|
|
|
cmdline,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
FALSE,
|
|
|
|
CREATE_UNICODE_ENVIRONMENT,
|
|
|
|
env,
|
|
|
|
os_getDataPath(),
|
|
|
|
&si,
|
|
|
|
&pi
|
|
|
|
))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to open log file", GetLastError());
|
|
|
|
goto fail_env;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
|
|
|
|
fail_env:
|
|
|
|
DestroyEnvironmentBlock(env);
|
|
|
|
fail_token:
|
|
|
|
CloseHandle(hToken);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
2019-02-28 08:20:35 +00:00
|
|
|
{
|
2019-03-01 04:45:46 +00:00
|
|
|
switch(msg)
|
|
|
|
{
|
|
|
|
case WM_DESTROY:
|
2021-07-19 10:16:27 +00:00
|
|
|
Shell_NotifyIcon(NIM_DELETE, &app.iconData);
|
2019-03-01 04:45:46 +00:00
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
case WM_CALL_FUNCTION:
|
|
|
|
{
|
|
|
|
struct MSG_CALL_FUNCTION * cf = (struct MSG_CALL_FUNCTION *)lParam;
|
|
|
|
return cf->fn(cf->wParam, cf->lParam);
|
|
|
|
}
|
|
|
|
|
2019-05-21 07:52:58 +00:00
|
|
|
case WM_TRAYICON:
|
|
|
|
{
|
|
|
|
if (lParam == WM_RBUTTONDOWN)
|
|
|
|
{
|
|
|
|
POINT curPoint;
|
|
|
|
GetCursorPos(&curPoint);
|
|
|
|
SetForegroundWindow(hwnd);
|
|
|
|
UINT clicked = TrackPopupMenu(
|
|
|
|
app.trayMenu,
|
|
|
|
TPM_RETURNCMD | TPM_NONOTIFY,
|
|
|
|
curPoint.x,
|
|
|
|
curPoint.y,
|
|
|
|
0,
|
|
|
|
hwnd,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
if (clicked == ID_MENU_EXIT ) app_quit();
|
2020-08-11 07:44:32 +00:00
|
|
|
else if (clicked == ID_MENU_SHOW_LOG)
|
2019-05-21 07:52:58 +00:00
|
|
|
{
|
|
|
|
const char * logFile = option_get_string("os", "logFile");
|
|
|
|
if (strcmp(logFile, "stderr") == 0)
|
|
|
|
DEBUG_INFO("Ignoring request to open the logFile, logging to stderr");
|
2021-03-18 04:12:40 +00:00
|
|
|
else if (!OpenLogFile(logFile))
|
2020-08-11 07:42:00 +00:00
|
|
|
MessageBoxA(hwnd, logFile, "Log File Location", MB_OK | MB_ICONINFORMATION);
|
2019-05-21 07:52:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
default:
|
2020-08-11 02:22:22 +00:00
|
|
|
if (msg == app.trayRestartMsg)
|
|
|
|
RegisterTrayIcon();
|
|
|
|
break;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
2020-08-11 02:22:22 +00:00
|
|
|
|
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int appThread(void * opaque)
|
|
|
|
{
|
2020-08-11 02:22:22 +00:00
|
|
|
RegisterTrayIcon();
|
2019-05-09 09:30:09 +00:00
|
|
|
int result = app_main(app.argc, app.argv);
|
2021-07-19 10:16:27 +00:00
|
|
|
DestroyWindow(app.messageWnd);
|
2019-03-01 04:45:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-19 10:16:27 +00:00
|
|
|
static int exitThread(void * opaque)
|
|
|
|
{
|
|
|
|
HANDLE handles[2] = { (HANDLE) opaque, app.exitThreadEvent };
|
|
|
|
|
|
|
|
switch (WaitForMultipleObjects(2, handles, FALSE, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
DEBUG_INFO("Received exit event");
|
|
|
|
SendMessage(app.messageWnd, WM_CLOSE, 0, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
DEBUG_ERROR("WaitForMultipleObjects failed: 0x%lx", GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
LRESULT sendAppMessage(UINT Msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
return SendMessage(app.messageWnd, Msg, wParam, lParam);
|
2019-04-10 11:07:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL WINAPI CtrlHandler(DWORD dwCtrlType)
|
|
|
|
{
|
|
|
|
if (dwCtrlType == CTRL_C_EVENT)
|
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
SendMessage(app.messageWnd, WM_CLOSE, 0, 0);
|
2019-04-10 11:07:56 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:39:05 +00:00
|
|
|
const char *getSystemLogDirectory(void)
|
|
|
|
{
|
|
|
|
return app.systemLogDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void populateSystemLogDirectory()
|
|
|
|
{
|
|
|
|
char programData[MAX_PATH];
|
|
|
|
if (GetEnvironmentVariableA("ProgramData", programData, sizeof(programData)) &&
|
|
|
|
PathIsDirectoryA(programData))
|
|
|
|
{
|
|
|
|
if (!PathCombineA(app.systemLogDir, programData, "Looking Glass (host)"))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!PathIsDirectoryA(app.systemLogDir) && !CreateDirectoryA(app.systemLogDir, NULL))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
strcpy(app.systemLogDir, "");
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
|
|
|
{
|
2021-07-07 13:05:46 +00:00
|
|
|
// initialize for DEBUG_* macros
|
|
|
|
debug_init();
|
|
|
|
|
2020-08-11 02:22:22 +00:00
|
|
|
// convert the command line to the standard argc and argv
|
|
|
|
LPWSTR * wargv = CommandLineToArgvW(GetCommandLineW(), &app.argc);
|
|
|
|
app.argv = malloc(sizeof(char *) * app.argc);
|
|
|
|
for(int i = 0; i < app.argc; ++i)
|
|
|
|
{
|
|
|
|
const size_t s = (wcslen(wargv[i])+1) * 2;
|
|
|
|
app.argv[i] = malloc(s);
|
|
|
|
wcstombs(app.argv[i], wargv[i], s);
|
|
|
|
}
|
|
|
|
LocalFree(wargv);
|
|
|
|
|
|
|
|
GetModuleFileName(NULL, app.executable, sizeof(app.executable));
|
2021-01-29 01:39:05 +00:00
|
|
|
populateSystemLogDirectory();
|
|
|
|
|
2020-08-11 02:22:22 +00:00
|
|
|
if (HandleService(app.argc, app.argv))
|
2021-01-21 04:44:19 +00:00
|
|
|
return LG_HOST_EXIT_FAILED;
|
2020-08-11 02:22:22 +00:00
|
|
|
|
2019-12-13 12:22:11 +00:00
|
|
|
/* this is a bit of a hack but without this --help will produce no output in a windows command prompt */
|
2019-12-13 12:33:11 +00:00
|
|
|
if (!IsDebuggerPresent() && AttachConsole(ATTACH_PARENT_PROCESS))
|
2019-12-13 12:22:11 +00:00
|
|
|
{
|
|
|
|
HANDLE std_err = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
HANDLE std_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
int std_err_fd = _open_osfhandle((intptr_t)std_err, _O_TEXT);
|
|
|
|
int std_out_fd = _open_osfhandle((intptr_t)std_out, _O_TEXT);
|
|
|
|
|
|
|
|
if (std_err_fd > 0)
|
|
|
|
*stderr = *_fdopen(std_err_fd, "w");
|
|
|
|
|
|
|
|
if (std_out_fd > 0)
|
|
|
|
*stdout = *_fdopen(std_out_fd, "w");
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:13:31 +00:00
|
|
|
int result = 0;
|
2019-05-21 07:52:58 +00:00
|
|
|
app.hInst = hInstance;
|
2019-05-09 12:13:31 +00:00
|
|
|
|
2021-01-29 01:39:05 +00:00
|
|
|
char logFilePath[MAX_PATH];
|
|
|
|
if (!PathCombineA(logFilePath, app.systemLogDir, LOG_NAME))
|
|
|
|
strcpy(logFilePath, LOG_NAME);
|
2019-05-16 23:26:42 +00:00
|
|
|
|
2019-05-10 09:48:38 +00:00
|
|
|
struct Option options[] =
|
|
|
|
{
|
2019-05-16 23:26:42 +00:00
|
|
|
{
|
|
|
|
.module = "os",
|
|
|
|
.name = "logFile",
|
|
|
|
.description = "The log file to write to",
|
|
|
|
.type = OPTION_TYPE_STRING,
|
|
|
|
.value.x_string = logFilePath
|
2019-05-10 09:48:38 +00:00
|
|
|
},
|
2021-07-19 10:16:27 +00:00
|
|
|
{
|
|
|
|
.module = "os",
|
|
|
|
.name = "exitEvent",
|
|
|
|
.description = "Exit when the specified event is signaled",
|
|
|
|
.type = OPTION_TYPE_STRING,
|
|
|
|
.value.x_string = ""
|
|
|
|
},
|
2019-05-10 09:48:38 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
option_register(options);
|
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
// setup a handler for ctrl+c
|
|
|
|
SetConsoleCtrlHandler(CtrlHandler, TRUE);
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
// create a message window so that our message pump works
|
|
|
|
WNDCLASSEX wx = {};
|
|
|
|
wx.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wx.lpfnWndProc = DummyWndProc;
|
|
|
|
wx.hInstance = hInstance;
|
|
|
|
wx.lpszClassName = "DUMMY_CLASS";
|
2019-05-21 07:52:58 +00:00
|
|
|
wx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wx.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
wx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE;
|
2020-08-11 02:22:22 +00:00
|
|
|
ATOM class;
|
|
|
|
if (!(class = RegisterClassEx(&wx)))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to register message window class");
|
2021-01-21 04:44:19 +00:00
|
|
|
result = LG_HOST_EXIT_FAILED;
|
2019-05-09 12:13:31 +00:00
|
|
|
goto finish;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
2020-08-11 02:22:22 +00:00
|
|
|
|
|
|
|
app.trayRestartMsg = RegisterWindowMessage("TaskbarCreated");
|
|
|
|
|
|
|
|
app.messageWnd = CreateWindowEx(0, MAKEINTATOM(class), NULL, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
|
2020-08-11 03:07:23 +00:00
|
|
|
|
2021-07-19 10:16:27 +00:00
|
|
|
app.exitThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
|
2020-08-11 03:07:23 +00:00
|
|
|
// this is needed so that unprivileged processes can send us this message
|
2021-07-20 02:03:05 +00:00
|
|
|
ChangeWindowMessageFilterEx(app.messageWnd, app.trayRestartMsg, MSGFLT_ALLOW, NULL);
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2020-04-24 10:34:58 +00:00
|
|
|
// set the global
|
|
|
|
MessageHWND = app.messageWnd;
|
|
|
|
|
2019-05-21 07:52:58 +00:00
|
|
|
app.trayMenu = CreatePopupMenu();
|
2021-03-18 04:12:40 +00:00
|
|
|
AppendMenu(app.trayMenu, MF_STRING , ID_MENU_SHOW_LOG, "Open Log File");
|
|
|
|
AppendMenu(app.trayMenu, MF_SEPARATOR, 0 , NULL );
|
|
|
|
AppendMenu(app.trayMenu, MF_STRING , ID_MENU_EXIT , "Exit" );
|
2019-05-21 07:52:58 +00:00
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
// create the application thread
|
2020-01-02 11:21:42 +00:00
|
|
|
LGThread * thread;
|
|
|
|
if (!lgCreateThread("appThread", appThread, NULL, &thread))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the main application thread");
|
2021-01-21 04:44:19 +00:00
|
|
|
result = LG_HOST_EXIT_FAILED;
|
2019-05-09 12:13:31 +00:00
|
|
|
goto finish;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
2019-02-28 09:50:22 +00:00
|
|
|
|
2019-03-02 09:31:33 +00:00
|
|
|
while(true)
|
2019-02-28 09:50:22 +00:00
|
|
|
{
|
2019-03-01 04:45:46 +00:00
|
|
|
MSG msg;
|
|
|
|
BOOL bRet = GetMessage(&msg, NULL, 0, 0);
|
|
|
|
if (bRet > 0)
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
2019-04-10 11:07:56 +00:00
|
|
|
continue;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
|
|
|
else if (bRet < 0)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Unknown error from GetMessage");
|
2021-01-21 04:44:19 +00:00
|
|
|
result = LG_HOST_EXIT_FAILED;
|
2019-03-01 04:45:46 +00:00
|
|
|
goto shutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2019-02-28 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
shutdown:
|
2019-05-21 07:52:58 +00:00
|
|
|
DestroyMenu(app.trayMenu);
|
2021-01-21 04:14:50 +00:00
|
|
|
app_shutdown();
|
2021-07-19 10:16:27 +00:00
|
|
|
SetEvent(app.exitThreadEvent);
|
|
|
|
|
2020-01-02 11:21:42 +00:00
|
|
|
if (!lgJoinThread(thread, &result))
|
2019-03-01 04:45:46 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to join the main application thread");
|
2021-01-21 04:14:50 +00:00
|
|
|
result = LG_HOST_EXIT_FAILED;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
2019-05-09 12:13:31 +00:00
|
|
|
|
2021-07-19 10:16:27 +00:00
|
|
|
if (app.exitThread && !lgJoinThread(app.exitThread, &result))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to join the exit thread");
|
|
|
|
result = LG_HOST_EXIT_FAILED;
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
finish:
|
2019-05-09 09:30:09 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < app.argc; ++i)
|
|
|
|
free(app.argv[i]);
|
|
|
|
free(app.argv);
|
|
|
|
|
2019-02-28 08:20:35 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-19 23:42:20 +00:00
|
|
|
typedef enum _D3DKMT_SCHEDULINGPRIORITYCLASS
|
2021-07-10 02:27:30 +00:00
|
|
|
{
|
2021-07-19 23:42:20 +00:00
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_IDLE = 0,
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_BELOW_NORMAL = 1,
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_NORMAL = 2,
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_ABOVE_NORMAL = 3,
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_HIGH = 4,
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_REALTIME = 5,
|
|
|
|
}
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS;
|
2021-07-10 02:27:30 +00:00
|
|
|
|
2021-07-19 23:42:20 +00:00
|
|
|
NTSTATUS APIENTRY D3DKMTSetProcessSchedulingPriorityClass(
|
|
|
|
_In_ HANDLE, _In_ D3DKMT_SCHEDULINGPRIORITYCLASS
|
|
|
|
);
|
2021-07-10 02:27:30 +00:00
|
|
|
|
2021-07-19 23:42:20 +00:00
|
|
|
void boostPriority(void)
|
|
|
|
{
|
|
|
|
if (FAILED(D3DKMTSetProcessSchedulingPriorityClass(GetCurrentProcess(),
|
|
|
|
D3DKMT_SCHEDULINGPRIORITYCLASS_REALTIME)))
|
2021-07-10 02:27:30 +00:00
|
|
|
{
|
|
|
|
DEBUG_WARN("Failed to set realtime GPU priority.");
|
|
|
|
DEBUG_INFO("This is not a failure, please do not report this as an issue.");
|
|
|
|
DEBUG_INFO("To fix this, install and run the Looking Glass host as a service.");
|
|
|
|
DEBUG_INFO("looking-glass-host.exe InstallService");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
bool app_init(void)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2021-07-17 04:55:22 +00:00
|
|
|
const char * logFile = option_get_string("os", "logFile");
|
2019-05-16 23:26:42 +00:00
|
|
|
|
|
|
|
// redirect stderr to a file
|
|
|
|
if (logFile && strcmp(logFile, "stderr") != 0)
|
|
|
|
freopen(logFile, "a", stderr);
|
|
|
|
|
|
|
|
// always flush stderr
|
|
|
|
setbuf(stderr, NULL);
|
2019-05-10 09:48:38 +00:00
|
|
|
|
2021-07-17 04:55:22 +00:00
|
|
|
delayInit();
|
2019-12-15 05:20:07 +00:00
|
|
|
|
2019-12-17 03:59:58 +00:00
|
|
|
// get the performance frequency for spinlocks
|
|
|
|
QueryPerformanceFrequency(&app.perfFreq);
|
|
|
|
|
2021-07-10 02:27:30 +00:00
|
|
|
// try to boost the scheduler priority
|
|
|
|
boostPriority();
|
|
|
|
|
2021-07-19 10:16:27 +00:00
|
|
|
// open exit signaling event
|
|
|
|
HANDLE exitEvent = NULL;
|
|
|
|
const char * exitEventName = option_get_string("os", "exitEvent");
|
|
|
|
if (exitEventName && exitEventName[0])
|
|
|
|
{
|
|
|
|
exitEvent = OpenEvent(SYNCHRONIZE, FALSE, exitEventName);
|
|
|
|
if (!exitEvent)
|
|
|
|
DEBUG_WARN("Failed to open exitEvent with error 0x%lx: %s", GetLastError(), exitEventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exitEvent && !lgCreateThread("exitThread", exitThread, exitEvent, &app.exitThread))
|
|
|
|
DEBUG_ERROR("Failed to create the exit thread");
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
const char * os_getExecutable(void)
|
2019-04-11 07:15:17 +00:00
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
return app.executable;
|
2020-04-24 10:34:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
const char * os_getDataPath(void)
|
2020-05-30 02:31:26 +00:00
|
|
|
{
|
|
|
|
static char path[MAX_PATH] = { 0 };
|
|
|
|
if (!path[0])
|
|
|
|
{
|
|
|
|
if (!GetModuleFileName(NULL, path, MAX_PATH))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *p = strrchr(path, '\\');
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
++p;
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:05:26 +00:00
|
|
|
HWND os_getMessageWnd(void)
|
2020-04-24 10:34:58 +00:00
|
|
|
{
|
|
|
|
return app.messageWnd;
|
|
|
|
}
|
2021-02-03 22:42:54 +00:00
|
|
|
|
|
|
|
bool os_blockScreensaver()
|
|
|
|
{
|
|
|
|
static bool lastResult = false;
|
|
|
|
static ULONGLONG lastCheck = 0;
|
|
|
|
|
|
|
|
ULONGLONG now = GetTickCount64();
|
|
|
|
if (now - lastCheck >= 1000)
|
|
|
|
{
|
|
|
|
ULONG executionState;
|
|
|
|
NTSTATUS status = CallNtPowerInformation(SystemExecutionState, NULL, 0,
|
|
|
|
&executionState, sizeof executionState);
|
|
|
|
|
|
|
|
if (status == STATUS_SUCCESS)
|
|
|
|
lastResult = executionState & ES_DISPLAY_REQUIRED;
|
|
|
|
else
|
|
|
|
DEBUG_ERROR("Failed to call CallNtPowerInformation(SystemExecutionState): %ld", status);
|
|
|
|
lastCheck = now;
|
|
|
|
}
|
|
|
|
return lastResult;
|
|
|
|
}
|
2021-06-04 02:31:15 +00:00
|
|
|
|
|
|
|
void os_showMessage(const char * caption, const char * msg)
|
|
|
|
{
|
|
|
|
MessageBoxA(NULL, msg, caption, MB_OK | MB_ICONINFORMATION);
|
|
|
|
}
|