diff --git a/c-host/.gitignore b/c-host/.gitignore new file mode 100644 index 00000000..bbbf9283 --- /dev/null +++ b/c-host/.gitignore @@ -0,0 +1,2 @@ +build/ +*.swp diff --git a/c-host/CMakeLists.txt b/c-host/CMakeLists.txt new file mode 100644 index 00000000..c47b7807 --- /dev/null +++ b/c-host/CMakeLists.txt @@ -0,0 +1,63 @@ +cmake_minimum_required(VERSION 3.0) +project(looking-glass-host C) + +set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/") + +include(CheckCCompilerFlag) +include(FeatureSummary) + +option(OPTIMIZE_FOR_NATIVE "Build with -march=native" ON) +if(OPTIMIZE_FOR_NATIVE) + CHECK_C_COMPILER_FLAG("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE) + if(COMPILER_SUPPORTS_MARCH_NATIVE) + add_compile_options("-march=native") + endif() +endif() + +add_compile_options( + "-Wall" + "-Werror" + "-Wfatal-errors" + "-ffast-math" + "-fdata-sections" + "-ffunction-sections" + "$<$:-O0;-g3;-ggdb>" +) +set(CMAKE_EXE_LINKER FLAGS "-Wl,--gc-sections") +set(CMAKE_C_STANDARD 11) + +execute_process( + COMMAND cat ../VERSION + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + OUTPUT_VARIABLE BUILD_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +add_definitions(-D BUILD_VERSION='"${BUILD_VERSION}"') + +include_directories( + ${PROJECT_SOURCE_DIR}/include + ${CMAKE_BINARY_DIR}/include + ${PROJECT_SOURCE_DIR}/../common + ${PROJECT_SOURCE_DIR}/../vendor/kvm-guest-drivers-windows + ${PKGCONFIG_INCLUDE_DIRS} + ${GMP_INCLUDE_DIR} +) + +#link_libraries( +#) + +set(SOURCES + src/app.c +) + +add_subdirectory(platform) + +add_executable(looking-glass-host ${SOURCES}) +target_link_libraries(looking-glass-host + platform +) + +install(PROGRAMS ${CMAKE_BINARY_DIR}/looking-glass-host DESTINATION bin/ COMPONENT binary) + +feature_summary(WHAT ENABLED_FEATURES DISABLED_FEATURES) diff --git a/c-host/Makefile b/c-host/Makefile deleted file mode 100644 index e5f940d7..00000000 --- a/c-host/Makefile +++ /dev/null @@ -1,52 +0,0 @@ -USE_DXGI ?= 1 -USE_XCB ?= 1 - -OUTPUT = looking-glass-host -EXT = - -CFLAGS = -std=gnu99 -Wall -Werror -CFLAGS += -g -O3 -CFLAGS += -I. -CFLAGS += -I../common - -OBJS = app.o - -# if windows -ifdef OS - CC = gcc.exe - LDFLAGS = -L./windows/dll -mwindows - LIBS += -lsetupapi - OBJS += windows/platform.o - OBJS += windows/windebug.o - CFLAGS += -I../vendor/kvm-guest-drivers-windows - EXT = .exe - - ifeq ($(USE_DXGI), 1) - CFLAGS += -DUSE_DXGI -DCOBJMACROS -DINITGUID - LIBS += -ld3d11 -ldxgi - DLLS += windows/dll/libd3d11.a - OBJS += windows/capture/dxgi.o - endif -else - CC = gcc - OBJS += linux/platform.o - LIBS += -lpthread - - ifeq ($(USE_XCB), 1) - CFLAGS += -DUSE_XCB - LIBS += -lxcb -lxcb-shm -lXfixes - OBJS += linux/capture/xcb.o - endif -endif - -all: $(OBJS) $(DLLS) - $(CC) $(CFLAGS) $(LDFLAGS) -o $(OUTPUT)$(EXT) $(OBJS) $(LIBS) - -clean: - rm -v $(OBJS) $(DLLS) $(OUTPUT)$(EXT) - -%.o: %.c - $(CC) $(CFLAGS) -c -o $@ $< - -%.a: %.def - dlltool --def $< --output-lib $@ diff --git a/c-host/README.md b/c-host/README.md index c7c9e022..52175bc2 100644 --- a/c-host/README.md +++ b/c-host/README.md @@ -25,10 +25,20 @@ Yes, but only when it is feature complete. # Why doesn't this use CMake? -Because win-builds doesn't distribute it, so to make it easy for everyone to compile we do not require it. +It does now... +~~Because win-builds doesn't distribute it, so to make it easy for everyone to compile we do not require it.~~ # How do I build it? Don't ask if you can't figure it out, this code is the very definition of experiemental and incomplete and should not be in use yet. +Hint: + +``` +mkdir build +cd build +cmake -G "MSYS Makefiles" .. +make +``` + _-Geoff_ diff --git a/c-host/capture/interfaces.h b/c-host/capture/interfaces.h deleted file mode 100644 index 24b581d2..00000000 --- a/c-host/capture/interfaces.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -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 "interface.h" - -#if defined(USE_DXGI) -struct CaptureInterface Capture_DXGI; -#endif - -#if defined(USE_XCB) -struct CaptureInterface Capture_XCB; -#endif - -struct CaptureInterface * CaptureInterfaces[] = -{ -#if defined(USE_DXGI) - &Capture_DXGI, -#endif -#if defined(USE_XCB) - &Capture_XCB, -#endif - NULL -}; \ No newline at end of file diff --git a/c-host/cmake/PostCapture.cmake b/c-host/cmake/PostCapture.cmake new file mode 100644 index 00000000..586f85d9 --- /dev/null +++ b/c-host/cmake/PostCapture.cmake @@ -0,0 +1,15 @@ +list(REMOVE_AT CAPTURE 0) +list(REMOVE_AT CAPTURE_LINK 0) + +list(LENGTH CAPTURE CAPTURE_COUNT) +file(APPEND ${CAPTURE_H} "#define LG_CAPTURE_COUNT ${CAPTURE_COUNT}\n") + +foreach(renderer ${CAPTURE}) + file(APPEND ${CAPTURE_C} "extern CaptureInterface Capture_${renderer};\n") +endforeach() + +file(APPEND ${CAPTURE_C} "\nconst CaptureInterface * CaptureInterfaces[] =\n{\n") +foreach(renderer ${CAPTURE}) + file(APPEND ${CAPTURE_C} " &Capture_${renderer},\n") +endforeach() +file(APPEND ${CAPTURE_C} " NULL\n};") diff --git a/c-host/cmake/PreCapture.cmake b/c-host/cmake/PreCapture.cmake new file mode 100644 index 00000000..fbb60b73 --- /dev/null +++ b/c-host/cmake/PreCapture.cmake @@ -0,0 +1,16 @@ +set(CAPTURE_H "${CMAKE_BINARY_DIR}/include/dynamic/capture.h") +set(CAPTURE_C "${CMAKE_BINARY_DIR}/src/capture.c") + +file(WRITE ${CAPTURE_H} "#include \"interface/capture.h\"\n\n") +file(APPEND ${CAPTURE_H} "extern CaptureInterface * CaptureInterfaces[];\n\n") + +file(WRITE ${CAPTURE_C} "#include \"interface/capture.h\"\n\n") +file(APPEND ${CAPTURE_C} "#include \n\n") + +set(CAPTURE "_") +set(CAPTURE_LINK "_") +function(add_capture name) + set(CAPTURE "${CAPTURE};${name}" PARENT_SCOPE) + set(CAPTURE_LINK "${CAPTURE_LINK};capture_${name}" PARENT_SCOPE) + add_subdirectory(${name}) +endfunction() diff --git a/c-host/capture/interface.h b/c-host/include/interface/capture.h similarity index 100% rename from c-host/capture/interface.h rename to c-host/include/interface/capture.h diff --git a/c-host/app.h b/c-host/include/interface/platform.h similarity index 100% rename from c-host/app.h rename to c-host/include/interface/platform.h diff --git a/c-host/platform/CMakeLists.txt b/c-host/platform/CMakeLists.txt new file mode 100644 index 00000000..9a9db670 --- /dev/null +++ b/c-host/platform/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.0) +project(platform LANGUAGES C) + +if (UNIX) + set(PLATFORM "Linux") +elseif(WIN32) + set(PLATFORM "Windows") +endif() + +add_subdirectory(${PLATFORM}) + +add_library(platform INTERFACE) +target_link_libraries(platform INTERFACE platform_${PLATFORM}) diff --git a/c-host/platform/Linux/CMakeLists.txt b/c-host/platform/Linux/CMakeLists.txt new file mode 100644 index 00000000..4aea377d --- /dev/null +++ b/c-host/platform/Linux/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.0) +project(platform_Linux LANGUAGES C) + +include_directories( + ${PROJECT_SOURCE_DIR}/include +) + +add_library(platform_Linux STATIC + src/platform.c +) + +add_subdirectory("capture") + +target_link_libraries(platform_Linux + capture + pthread +) + +target_include_directories(platform_Linux + PRIVATE + src +) diff --git a/c-host/platform/Linux/capture/CMakeLists.txt b/c-host/platform/Linux/capture/CMakeLists.txt new file mode 100644 index 00000000..5fd4d17b --- /dev/null +++ b/c-host/platform/Linux/capture/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) +project(capture LANGUAGES C) + +include("PreCapture") + +add_capture("XCB") + +include("PostCapture") + +add_library(capture STATIC ${CAPTURE_C}) +target_link_libraries(capture ${CAPTURE_LINK}) diff --git a/c-host/platform/Linux/capture/XCB/CMakeLists.txt b/c-host/platform/Linux/capture/XCB/CMakeLists.txt new file mode 100644 index 00000000..034bdb4d --- /dev/null +++ b/c-host/platform/Linux/capture/XCB/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 3.0) +project(capture_XCB LANGUAGES C) + +add_library(capture_XCB STATIC + src/xcb.c +) + +target_link_libraries(capture_XCB + xcb + xcb-shm + Xfixes +) + +target_include_directories(capture_XCB + PRIVATE + src +) diff --git a/c-host/linux/capture/xcb.c b/c-host/platform/Linux/capture/XCB/src/xcb.c similarity index 100% rename from c-host/linux/capture/xcb.c rename to c-host/platform/Linux/capture/XCB/src/xcb.c diff --git a/c-host/linux/platform.c b/c-host/platform/Linux/src/platform.c similarity index 100% rename from c-host/linux/platform.c rename to c-host/platform/Linux/src/platform.c diff --git a/c-host/platform/Windows/CMakeLists.txt b/c-host/platform/Windows/CMakeLists.txt new file mode 100644 index 00000000..e8dff3fe --- /dev/null +++ b/c-host/platform/Windows/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.0) +project(platform_Windows LANGUAGES C) + +include_directories( + ${PROJECT_SOURCE_DIR}/include +) + +add_library(platform_Windows STATIC + src/platform.c + src/windebug.c +) + +add_subdirectory("capture") + +target_link_libraries(platform_Windows + capture + setupapi +) + +target_include_directories(platform_Windows + PRIVATE + src +) diff --git a/c-host/platform/Windows/capture/CMakeLists.txt b/c-host/platform/Windows/capture/CMakeLists.txt new file mode 100644 index 00000000..d489f1ac --- /dev/null +++ b/c-host/platform/Windows/capture/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.0) +project(capture LANGUAGES C) + +include("PreCapture") + +add_capture("DXGI") + +include("PostCapture") + +add_library(capture STATIC ${CAPTURE_C}) +target_link_libraries(capture ${CAPTURE_LINK}) diff --git a/c-host/platform/Windows/capture/DXGI/CMakeLists.txt b/c-host/platform/Windows/capture/DXGI/CMakeLists.txt new file mode 100644 index 00000000..c78f9484 --- /dev/null +++ b/c-host/platform/Windows/capture/DXGI/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.0) +project(capture_DXGI LANGUAGES C) + +add_library(capture_DXGI STATIC + src/dxgi.c +) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOBJMACROS -DINITGUID") + +FIND_PROGRAM(DLLTOOL_EXECUTABLE NAMES "dlltool.exe" DOC "dlltool executable") +ADD_CUSTOM_COMMAND(TARGET capture_DXGI POST_BUILD + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/dll" + COMMAND ${DLLTOOL_EXECUTABLE} --def libd3d11.def --output-lib "${PROJECT_BINARY_DIR}/libd3d11.dll" + VERBATIM +) + +target_link_libraries(capture_DXGI + ${PROJECT_BINARY_DIR}/libd3d11.dll + dxgi +) + +target_include_directories(capture_DXGI + PRIVATE + src +) diff --git a/c-host/windows/dll/libd3d11.def b/c-host/platform/Windows/capture/DXGI/dll/libd3d11.def similarity index 100% rename from c-host/windows/dll/libd3d11.def rename to c-host/platform/Windows/capture/DXGI/dll/libd3d11.def diff --git a/c-host/windows/capture/dxgi.c b/c-host/platform/Windows/capture/DXGI/src/dxgi.c similarity index 99% rename from c-host/windows/capture/dxgi.c rename to c-host/platform/Windows/capture/DXGI/src/dxgi.c index 5daf66a9..11d5a1f3 100644 --- a/c-host/windows/capture/dxgi.c +++ b/c-host/platform/Windows/capture/DXGI/src/dxgi.c @@ -17,8 +17,8 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "capture/interface.h" -#include "app.h" +#include "interface/capture.h" +#include "interface/platform.h" #include "debug.h" #include "windows/windebug.h" diff --git a/c-host/windows/capture/dxgi_extra.h b/c-host/platform/Windows/capture/DXGI/src/dxgi_extra.h similarity index 100% rename from c-host/windows/capture/dxgi_extra.h rename to c-host/platform/Windows/capture/DXGI/src/dxgi_extra.h diff --git a/c-host/windows/windebug.h b/c-host/platform/Windows/include/windows/windebug.h similarity index 100% rename from c-host/windows/windebug.h rename to c-host/platform/Windows/include/windows/windebug.h diff --git a/c-host/windows/platform.c b/c-host/platform/Windows/src/platform.c similarity index 99% rename from c-host/windows/platform.c rename to c-host/platform/Windows/src/platform.c index 28dcd299..3a139fa4 100644 --- a/c-host/windows/platform.c +++ b/c-host/platform/Windows/src/platform.c @@ -20,9 +20,9 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include -#include "app.h" +#include "interface/platform.h" #include "debug.h" -#include "windebug.h" +#include "windows/windebug.h" #include "ivshmem/Public.h" static HANDLE shmemHandle = INVALID_HANDLE_VALUE; diff --git a/c-host/windows/windebug.c b/c-host/platform/Windows/src/windebug.c similarity index 97% rename from c-host/windows/windebug.c rename to c-host/platform/Windows/src/windebug.c index fabe56cd..9d7fef2c 100644 --- a/c-host/windows/windebug.c +++ b/c-host/platform/Windows/src/windebug.c @@ -17,7 +17,7 @@ this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "windebug.h" +#include "windows/windebug.h" #include void DebugWinError(const char * file, const unsigned int line, const char * function, const char * desc, HRESULT status) diff --git a/c-host/app.c b/c-host/src/app.c similarity index 99% rename from c-host/app.c rename to c-host/src/app.c index f6866bd9..c540881b 100644 --- a/c-host/app.c +++ b/c-host/src/app.c @@ -17,7 +17,9 @@ 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 "interface/platform.h" +#include "interface/capture.h" +#include "dynamic/capture.h" #include #include @@ -26,7 +28,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include "debug.h" #include "locking.h" -#include "capture/interfaces.h" #include "KVMFR.h" #define ALIGN_DN(x) ((uintptr_t)(x) & ~0x7F)