diff --git a/c-host/Makefile b/c-host/Makefile index 75d52b6d..b8f2b1a7 100644 --- a/c-host/Makefile +++ b/c-host/Makefile @@ -7,12 +7,15 @@ CFLAGS += -g -O0 CFLAGS += -I. CFLAGS += -I../common -OBJS = main.o +OBJS = app.o # if windows ifdef OS LDFLAGS = -L./dll - OBJS += windebug.o + LIBS += -lsetupapi + OBJS += windows/platform.o + OBJS += windows/windebug.o + CFLAGS += -I../vendor/kvm-guest-drivers-windows ifeq ($(USE_DXGI), 1) CFLAGS += -DUSE_DXGI -DCOBJMACROS -DINITGUID diff --git a/c-host/main.c b/c-host/app.c similarity index 83% rename from c-host/main.c rename to c-host/app.c index ec4c249b..c6a1873b 100644 --- a/c-host/main.c +++ b/c-host/app.c @@ -17,13 +17,25 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "app.h" + #include #include #include "debug.h" #include "capture/interfaces.h" -int WINAPI WinMain(HINSTANCE hInstnace, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +int app_main() { + unsigned int shmemSize = os_shmemSize(); + void * shmemMap = NULL; + + DEBUG_INFO("IVSHMEM Size : %u MiB", shmemSize / 1048576); + if (!os_shmemMmap(&shmemMap) || !shmemMap) + { + DEBUG_ERROR("Failed to map the shared memory"); + return -1; + } + struct CaptureInterface * iface = NULL; for(int i = 0; CaptureInterfaces[i]; ++i) { diff --git a/c-host/app.h b/c-host/app.h new file mode 100644 index 00000000..5004061b --- /dev/null +++ b/c-host/app.h @@ -0,0 +1,30 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017-2019 Geoffrey McRae +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 +*/ + +#pragma once + +#include + +int app_main(); + +// these must be implemented for each OS + +unsigned int os_shmemSize(); +bool os_shmemMmap(void **ptr); +void os_shmemUnmap(); \ No newline at end of file diff --git a/c-host/capture/dxgi.c b/c-host/capture/dxgi.c index e2dd38e8..ad453313 100644 --- a/c-host/capture/dxgi.c +++ b/c-host/capture/dxgi.c @@ -19,7 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "interface.h" #include "debug.h" -#include "windebug.h" +#include "windows/windebug.h" #include #include diff --git a/c-host/windows/platform.c b/c-host/windows/platform.c new file mode 100644 index 00000000..a6c5f25c --- /dev/null +++ b/c-host/windows/platform.c @@ -0,0 +1,121 @@ +/* +Looking Glass - KVM FrameRelay (KVMFR) Client +Copyright (C) 2017-2019 Geoffrey McRae +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 +*/ + +#include +#include + +#include "app.h" +#include "debug.h" +#include "windebug.h" +#include "ivshmem/Public.h" + +static HANDLE shmemHandle = INVALID_HANDLE_VALUE; + +int WINAPI WinMain(HINSTANCE hInstnace, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +{ + 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); + + if (SetupDiEnumDeviceInterfaces(deviceInfoSet, NULL, &GUID_DEVINTERFACE_IVSHMEM, 0, &deviceInterfaceData) == FALSE) + { + DWORD error = GetLastError(); + if (error == ERROR_NO_MORE_ITEMS) + { + DEBUG_WINERROR("Unable to enumerate the device, is it attached?", error); + return -1; + } + + DEBUG_WINERROR("SetupDiEnumDeviceInterfaces failed", error); + return -1; + } + + DWORD reqSize = 0; + SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &reqSize, NULL); + if (!reqSize) + { + DEBUG_WINERROR("SetupDiGetDeviceInterfaceDetail", GetLastError()); + return -1; + } + + 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 -1; + } + + shmemHandle = CreateFile(infData->DevicePath, 0, 0, NULL, OPEN_EXISTING, 0, 0); + if (shmemHandle == INVALID_HANDLE_VALUE) + { + SetupDiDestroyDeviceInfoList(deviceInfoSet); + free(infData); + DEBUG_WINERROR("CreateFile returned INVALID_HANDLE_VALUE", GetLastError()); + return -1; + } + + free(infData); + SetupDiDestroyDeviceInfoList(deviceInfoSet); + + int result = app_main(); + + CloseHandle(shmemHandle); + return result; +} + +unsigned int os_shmemSize() +{ + IVSHMEM_SIZE size; + if (!DeviceIoControl(shmemHandle, IOCTL_IVSHMEM_REQUEST_SIZE, NULL, 0, &size, sizeof(IVSHMEM_SIZE), NULL, NULL)) + { + DEBUG_WINERROR("DeviceIoControl Failed", GetLastError()); + return 0; + } + + return (unsigned int)size; +} + +bool os_shmemMmap(void **ptr) +{ + IVSHMEM_MMAP map = {0}; + + if (!DeviceIoControl( + shmemHandle, + IOCTL_IVSHMEM_REQUEST_MMAP, + NULL, 0, + &map, sizeof(IVSHMEM_MMAP), + NULL, NULL)) + { + DEBUG_WINERROR("DeviceIoControl Failed", GetLastError()); + return false; + } + + *ptr = map.ptr; + return true; +} + +void os_shmemUnmap() +{ +} \ No newline at end of file diff --git a/c-host/windebug.c b/c-host/windows/windebug.c similarity index 100% rename from c-host/windebug.c rename to c-host/windows/windebug.c diff --git a/c-host/windebug.h b/c-host/windows/windebug.h similarity index 100% rename from c-host/windebug.h rename to c-host/windows/windebug.h