2019-02-28 08:20:35 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
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-04-10 11:07:56 +00:00
|
|
|
#include "platform.h"
|
|
|
|
#include "windows/platform.h"
|
|
|
|
#include "windows/mousehook.h"
|
2019-05-09 09:30:09 +00:00
|
|
|
|
2019-02-28 08:20:35 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <setupapi.h>
|
2019-05-09 09:30:09 +00:00
|
|
|
#include <shellapi.h>
|
2019-12-13 12:22:11 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <io.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"
|
2019-05-10 09:48:38 +00:00
|
|
|
#include "common/option.h"
|
2019-04-11 01:03:30 +00:00
|
|
|
#include "windows/debug.h"
|
2019-05-28 04:51:47 +00:00
|
|
|
#include "ivshmem.h"
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-05-21 07:52:58 +00:00
|
|
|
#define ID_MENU_OPEN_LOG 3000
|
|
|
|
#define ID_MENU_EXIT 3001
|
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
struct AppState
|
|
|
|
{
|
2019-05-21 07:52:58 +00:00
|
|
|
HINSTANCE hInst;
|
|
|
|
|
2019-05-09 09:32:19 +00:00
|
|
|
int argc;
|
|
|
|
char ** argv;
|
|
|
|
|
|
|
|
char executable[MAX_PATH + 1];
|
|
|
|
HANDLE shmemHandle;
|
|
|
|
bool shmemOwned;
|
|
|
|
IVSHMEM_MMAP shmemMap;
|
|
|
|
HWND messageWnd;
|
2019-05-21 07:52:58 +00:00
|
|
|
HMENU trayMenu;
|
2019-05-09 09:30:09 +00:00
|
|
|
};
|
|
|
|
|
2019-05-09 09:32:19 +00:00
|
|
|
static struct AppState app =
|
2019-05-09 09:30:09 +00:00
|
|
|
{
|
|
|
|
.shmemHandle = INVALID_HANDLE_VALUE,
|
|
|
|
.shmemOwned = false,
|
|
|
|
.shmemMap = {0}
|
|
|
|
};
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-03-01 01:24:23 +00:00
|
|
|
struct osThreadHandle
|
|
|
|
{
|
|
|
|
const char * name;
|
|
|
|
osThreadFunction function;
|
|
|
|
void * opaque;
|
|
|
|
HANDLE handle;
|
|
|
|
DWORD threadID;
|
2019-04-10 11:07:56 +00:00
|
|
|
|
2019-03-01 01:24:23 +00:00
|
|
|
int resultCode;
|
|
|
|
};
|
|
|
|
|
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:
|
|
|
|
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();
|
|
|
|
else if (clicked == ID_MENU_OPEN_LOG)
|
|
|
|
{
|
|
|
|
const char * logFile = option_get_string("os", "logFile");
|
|
|
|
if (strcmp(logFile, "stderr") == 0)
|
|
|
|
DEBUG_INFO("Ignoring request to open the logFile, logging to stderr");
|
|
|
|
else
|
|
|
|
ShellExecute(NULL, NULL, logFile, NULL, NULL, SW_SHOWNORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
default:
|
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int appThread(void * opaque)
|
|
|
|
{
|
2019-05-21 07:52:58 +00:00
|
|
|
// register our TrayIcon
|
|
|
|
NOTIFYICONDATA iconData =
|
|
|
|
{
|
|
|
|
.cbSize = sizeof(NOTIFYICONDATA),
|
|
|
|
.hWnd = app.messageWnd,
|
|
|
|
.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP,
|
|
|
|
.uCallbackMessage = WM_TRAYICON,
|
|
|
|
.szTip = "Looking Glass (host)"
|
|
|
|
};
|
|
|
|
iconData.hIcon = LoadIcon(app.hInst, IDI_APPLICATION);
|
|
|
|
Shell_NotifyIcon(NIM_ADD, &iconData);
|
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
int result = app_main(app.argc, app.argv);
|
2019-05-21 07:52:58 +00:00
|
|
|
|
|
|
|
Shell_NotifyIcon(NIM_DELETE, &iconData);
|
|
|
|
mouseHook_remove();
|
|
|
|
SendMessage(app.messageWnd, WM_DESTROY, 0, 0);
|
2019-03-01 04:45:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
|
|
|
|
{
|
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 */
|
|
|
|
if (AttachConsole(ATTACH_PARENT_PROCESS))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2019-05-16 23:26:42 +00:00
|
|
|
char tempPath[MAX_PATH+1];
|
|
|
|
GetTempPathA(sizeof(tempPath), tempPath);
|
|
|
|
int len = snprintf(NULL, 0, "%slooking-glass-host.txt", tempPath);
|
|
|
|
char * logFilePath = malloc(len + 1);
|
|
|
|
sprintf(logFilePath, "%slooking-glass-host.txt", tempPath);
|
|
|
|
|
2019-05-10 09:48:38 +00:00
|
|
|
struct Option options[] =
|
|
|
|
{
|
|
|
|
{
|
2019-05-16 23:26:42 +00:00
|
|
|
.module = "os",
|
|
|
|
.name = "shmDevice",
|
|
|
|
.description = "The IVSHMEM device to use",
|
|
|
|
.type = OPTION_TYPE_INT,
|
|
|
|
.value.x_int = 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.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
|
|
|
},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
|
|
|
option_register(options);
|
2019-05-16 23:26:42 +00:00
|
|
|
free(logFilePath);
|
2019-05-10 09:48:38 +00:00
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
// convert the command line to the standard argc and argv
|
2019-05-09 09:32:19 +00:00
|
|
|
LPWSTR * wargv = CommandLineToArgvW(GetCommandLineW(), &app.argc);
|
2019-05-09 09:30:09 +00:00
|
|
|
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);
|
2019-06-12 05:31:18 +00:00
|
|
|
wcstombs(app.argv[i], wargv[i], s);
|
2019-05-09 09:30:09 +00:00
|
|
|
}
|
|
|
|
LocalFree(wargv);
|
|
|
|
|
|
|
|
GetModuleFileName(NULL, app.executable, sizeof(app.executable));
|
2019-04-11 07:15:17 +00:00
|
|
|
|
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;
|
2019-03-01 04:45:46 +00:00
|
|
|
if (!RegisterClassEx(&wx))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to register message window class");
|
|
|
|
result = -1;
|
2019-05-09 12:13:31 +00:00
|
|
|
goto finish;
|
2019-03-01 04:45:46 +00:00
|
|
|
}
|
2019-05-09 09:30:09 +00:00
|
|
|
app.messageWnd = CreateWindowEx(0, "DUMMY_CLASS", "DUMMY_NAME", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-05-21 07:52:58 +00:00
|
|
|
app.trayMenu = CreatePopupMenu();
|
|
|
|
AppendMenu(app.trayMenu, MF_STRING , ID_MENU_OPEN_LOG, "Open Log File");
|
|
|
|
AppendMenu(app.trayMenu, MF_SEPARATOR, 0 , NULL );
|
|
|
|
AppendMenu(app.trayMenu, MF_STRING , ID_MENU_EXIT , "Exit" );
|
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
// create the application thread
|
2019-03-01 04:45:46 +00:00
|
|
|
osThreadHandle * thread;
|
|
|
|
if (!os_createThread("appThread", appThread, NULL, &thread))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to create the main application thread");
|
|
|
|
result = -1;
|
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");
|
|
|
|
result = -1;
|
|
|
|
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);
|
2019-03-02 09:31:33 +00:00
|
|
|
app_quit();
|
2019-03-01 04:45:46 +00:00
|
|
|
if (!os_joinThread(thread, &result))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to join the main application thread");
|
|
|
|
result = -1;
|
|
|
|
}
|
2019-05-09 12:13:31 +00:00
|
|
|
|
2019-03-01 04:45:46 +00:00
|
|
|
finish:
|
2019-05-09 12:13:31 +00:00
|
|
|
os_shmemUnmap();
|
|
|
|
|
|
|
|
if (app.shmemHandle != INVALID_HANDLE_VALUE)
|
|
|
|
CloseHandle(app.shmemHandle);
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
bool app_init()
|
|
|
|
{
|
2019-05-16 23:26:42 +00:00
|
|
|
const int shmDevice = option_get_int ("os", "shmDevice");
|
|
|
|
const char * logFile = option_get_string("os", "logFile" );
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
HDEVINFO deviceInfoSet;
|
|
|
|
PSP_DEVICE_INTERFACE_DETAIL_DATA infData = NULL;
|
|
|
|
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
|
|
|
|
|
|
|
|
deviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE);
|
|
|
|
memset(&deviceInterfaceData, 0, sizeof(SP_DEVICE_INTERFACE_DATA));
|
|
|
|
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
|
|
|
|
2019-05-10 09:48:38 +00:00
|
|
|
if (SetupDiEnumDeviceInterfaces(deviceInfoSet, NULL, &GUID_DEVINTERFACE_IVSHMEM, shmDevice, &deviceInterfaceData) == FALSE)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
|
|
|
DWORD error = GetLastError();
|
|
|
|
if (error == ERROR_NO_MORE_ITEMS)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Unable to enumerate the device, is it attached?", error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_WINERROR("SetupDiEnumDeviceInterfaces failed", error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD reqSize = 0;
|
|
|
|
SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &reqSize, NULL);
|
|
|
|
if (!reqSize)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("SetupDiGetDeviceInterfaceDetail", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
infData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)calloc(reqSize, 1);
|
|
|
|
infData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
|
|
|
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, infData, reqSize, NULL, NULL))
|
|
|
|
{
|
|
|
|
free(infData);
|
|
|
|
DEBUG_WINERROR("SetupDiGetDeviceInterfaceDetail", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
app.shmemHandle = CreateFile(infData->DevicePath, 0, 0, NULL, OPEN_EXISTING, 0, 0);
|
|
|
|
if (app.shmemHandle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetupDiDestroyDeviceInfoList(deviceInfoSet);
|
|
|
|
free(infData);
|
|
|
|
DEBUG_WINERROR("CreateFile returned INVALID_HANDLE_VALUE", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(infData);
|
|
|
|
SetupDiDestroyDeviceInfoList(deviceInfoSet);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:15:17 +00:00
|
|
|
const char * os_getExecutable()
|
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
return app.executable;
|
2019-04-11 07:15:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:20:35 +00:00
|
|
|
unsigned int os_shmemSize()
|
|
|
|
{
|
|
|
|
IVSHMEM_SIZE size;
|
2019-05-09 09:30:09 +00:00
|
|
|
if (!DeviceIoControl(app.shmemHandle, IOCTL_IVSHMEM_REQUEST_SIZE, NULL, 0, &size, sizeof(IVSHMEM_SIZE), NULL, NULL))
|
2019-02-28 08:20:35 +00:00
|
|
|
{
|
|
|
|
DEBUG_WINERROR("DeviceIoControl Failed", GetLastError());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned int)size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool os_shmemMmap(void **ptr)
|
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
if (app.shmemOwned)
|
2019-02-28 08:27:17 +00:00
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
*ptr = app.shmemMap.ptr;
|
2019-02-28 08:27:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-02-28 08:20:35 +00:00
|
|
|
|
2019-10-24 08:46:09 +00:00
|
|
|
IVSHMEM_MMAP_CONFIG config =
|
|
|
|
{
|
|
|
|
.cacheMode = IVSHMEM_CACHE_WRITECOMBINED
|
|
|
|
};
|
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
memset(&app.shmemMap, 0, sizeof(IVSHMEM_MMAP));
|
2019-02-28 08:20:35 +00:00
|
|
|
if (!DeviceIoControl(
|
2019-05-09 09:30:09 +00:00
|
|
|
app.shmemHandle,
|
2019-02-28 08:20:35 +00:00
|
|
|
IOCTL_IVSHMEM_REQUEST_MMAP,
|
2019-10-24 08:46:09 +00:00
|
|
|
&config, sizeof(IVSHMEM_MMAP_CONFIG),
|
2019-05-09 09:30:09 +00:00
|
|
|
&app.shmemMap, sizeof(IVSHMEM_MMAP),
|
2019-02-28 08:20:35 +00:00
|
|
|
NULL, NULL))
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("DeviceIoControl Failed", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
*ptr = app.shmemMap.ptr;
|
|
|
|
app.shmemOwned = true;
|
2019-02-28 08:20:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void os_shmemUnmap()
|
|
|
|
{
|
2019-05-09 09:30:09 +00:00
|
|
|
if (!app.shmemOwned)
|
2019-02-28 08:27:17 +00:00
|
|
|
return;
|
|
|
|
|
2019-05-09 09:30:09 +00:00
|
|
|
if (!DeviceIoControl(app.shmemHandle, IOCTL_IVSHMEM_RELEASE_MMAP, NULL, 0, NULL, 0, NULL, NULL))
|
2019-02-28 08:27:17 +00:00
|
|
|
DEBUG_WINERROR("DeviceIoControl failed", GetLastError());
|
|
|
|
else
|
2019-05-09 09:30:09 +00:00
|
|
|
app.shmemOwned = false;
|
2019-03-01 01:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD WINAPI threadWrapper(LPVOID lpParameter)
|
|
|
|
{
|
|
|
|
osThreadHandle * handle = (osThreadHandle *)lpParameter;
|
|
|
|
handle->resultCode = handle->function(handle->opaque);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool os_createThread(const char * name, osThreadFunction function, void * opaque, osThreadHandle ** handle)
|
|
|
|
{
|
2019-03-01 01:41:37 +00:00
|
|
|
*handle = (osThreadHandle *)malloc(sizeof(osThreadHandle));
|
|
|
|
(*handle)->name = name;
|
|
|
|
(*handle)->function = function;
|
|
|
|
(*handle)->opaque = opaque;
|
|
|
|
(*handle)->handle = CreateThread(NULL, 0, threadWrapper, *handle, 0, &(*handle)->threadID);
|
2019-03-01 01:24:23 +00:00
|
|
|
|
|
|
|
if (!(*handle)->handle)
|
|
|
|
{
|
|
|
|
free(*handle);
|
|
|
|
*handle = NULL;
|
|
|
|
DEBUG_WINERROR("CreateThread failed", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool os_joinThread(osThreadHandle * handle, int * resultCode)
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
switch(WaitForSingleObject(handle->handle, INFINITE))
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
if (resultCode)
|
|
|
|
*resultCode = handle->resultCode;
|
|
|
|
CloseHandle(handle->handle);
|
|
|
|
free(handle);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
DEBUG_WINERROR("Wait for thread failed", GetLastError());
|
|
|
|
CloseHandle(handle->handle);
|
|
|
|
free(handle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_WINERROR("Unknown failure waiting for thread", GetLastError());
|
|
|
|
return false;
|
2019-03-03 12:30:02 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 02:38:17 +00:00
|
|
|
osEventHandle * os_createEvent(bool autoReset)
|
2019-03-03 12:30:02 +00:00
|
|
|
{
|
2019-03-04 02:38:17 +00:00
|
|
|
HANDLE event = CreateEvent(NULL, autoReset ? FALSE : TRUE, FALSE, NULL);
|
|
|
|
if (!event)
|
|
|
|
{
|
|
|
|
DEBUG_WINERROR("Failed to create the event", GetLastError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:30:02 +00:00
|
|
|
return (osEventHandle*)event;
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
osEventHandle * os_wrapEvent(HANDLE event)
|
|
|
|
{
|
|
|
|
return (osEventHandle*)event;
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:30:02 +00:00
|
|
|
void os_freeEvent(osEventHandle * handle)
|
|
|
|
{
|
|
|
|
CloseHandle((HANDLE)handle);
|
|
|
|
}
|
|
|
|
|
2019-03-04 05:56:45 +00:00
|
|
|
bool os_waitEvent(osEventHandle * handle, unsigned int timeout)
|
2019-03-03 12:30:02 +00:00
|
|
|
{
|
2019-03-04 05:56:45 +00:00
|
|
|
const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout;
|
2019-03-03 12:30:02 +00:00
|
|
|
while(true)
|
|
|
|
{
|
2019-03-04 05:56:45 +00:00
|
|
|
switch(WaitForSingleObject((HANDLE)handle, to))
|
2019-03-03 12:30:02 +00:00
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
continue;
|
|
|
|
|
2019-03-04 05:56:45 +00:00
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
if (timeout == TIMEOUT_INFINITE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
2019-03-03 12:30:02 +00:00
|
|
|
case WAIT_FAILED:
|
|
|
|
DEBUG_WINERROR("Wait for event failed", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_ERROR("Unknown wait event return code");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:07:56 +00:00
|
|
|
bool os_waitEvents(osEventHandle * handles[], int count, bool waitAll, unsigned int timeout)
|
|
|
|
{
|
|
|
|
const DWORD to = (timeout == TIMEOUT_INFINITE) ? INFINITE : (DWORD)timeout;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
DWORD result = WaitForMultipleObjects(count, (HANDLE*)handles, waitAll, to);
|
|
|
|
if (result >= WAIT_OBJECT_0 && result < count)
|
|
|
|
{
|
|
|
|
// null non signalled events from the handle list
|
|
|
|
for(int i = 0; i < count; ++i)
|
|
|
|
if (i != result && !os_waitEvent(handles[i], 0))
|
|
|
|
handles[i] = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result >= WAIT_ABANDONED_0 && result - WAIT_ABANDONED_0 < count)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch(result)
|
|
|
|
{
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
if (timeout == TIMEOUT_INFINITE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case WAIT_FAILED:
|
|
|
|
DEBUG_WINERROR("Wait for event failed", GetLastError());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_ERROR("Unknown wait event return code");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 12:30:02 +00:00
|
|
|
bool os_signalEvent(osEventHandle * handle)
|
|
|
|
{
|
|
|
|
return SetEvent((HANDLE)handle);
|
2019-03-04 02:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool os_resetEvent(osEventHandle * handle)
|
|
|
|
{
|
|
|
|
return ResetEvent((HANDLE)handle);
|
2019-02-28 08:20:35 +00:00
|
|
|
}
|