Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Build gainput as a shared library by using BUILD_SHARED option in CMake. #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.0)

project(gainput)

set(GAINPUT_MAJOR_VERSION 1)
set(GAINPUT_MINOR_VERSION 0)
set(GAINPUT_PATCH_VERSION 0)
set(GAINPUT_VERSION ${GAINPUT_MAJOR_VERSION}.${GAINPUT_MINOR_VERSION}.${GAINPUT_PATCH_VERSION})

option(GAINPUT_SAMPLES "Build Samples for Gainput" ON)
option(GAINPUT_TESTS "Build Tests for Gainput" ON)
option(GAINPUT_BUILD_SHARED "BUILD_SHARED" ON)
option(GAINPUT_BUILD_STATIC "BUILD_STATIC" ON)
option(BUILD_SHARED_LIBS "Build gainput as a shared library." ON)

if(!WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")
else()
set(XINPUT "Xinput9_1_0")
if ( ${CMAKE_SYSTEM_VERSION} LESS 6.1 )
set(XINPUT, "xinput")
endif()
set(XINPUT "Xinput9_1_0")
if ( ${CMAKE_SYSTEM_VERSION} LESS 6.1 )
set(XINPUT, "xinput")
endif()
endif()

if(ANDROID)
include(extern/cmake/AndroidNdkModules.cmake)
android_ndk_import_module_native_app_glue()
include(extern/cmake/AndroidNdkModules.cmake)
android_ndk_import_module_native_app_glue()
endif()

add_subdirectory(lib)

if(GAINPUT_SAMPLES)
add_subdirectory(samples)
add_subdirectory(samples)
endif()

if(GAINPUT_TESTS)
add_subdirectory(test)
add_subdirectory(test)
endif()

2 changes: 1 addition & 1 deletion extern/catch/catch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5728,7 +5728,7 @@ namespace Catch {
bool operator() (TestCase i,TestCase j) const { return (i<j);}
};
struct RandomNumberGenerator {
int operator()( int n ) const { return std::rand() % n; }
size_t operator()( size_t n ) const { return static_cast<size_t>( std::rand() ) % n; }
};

public:
Expand Down
72 changes: 31 additions & 41 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,61 @@ message(STATUS "GAINPUT version ${GAINPUT_VERSION}")
set(CMAKE_MACOSX_RPATH 1)

if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98 -Wall -Wextra -pedantic -Wshadow -Wno-variadic-macros")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98 -Wall -Wextra -pedantic -Wshadow -Wno-variadic-macros")
endif()

include_directories (include/)

file(GLOB_RECURSE sources source/*.cpp source/*.h include/*.h)

if(APPLE)
file(GLOB_RECURSE mmsources source/*.mm)
file(GLOB_RECURSE mmsources source/*.mm)
endif()

## build STATIC *or* SHARED
if (GAINPUT_BUILD_SHARED)
message(STATUS "..Building shared libraries (-DGAINPUT_BUILD_SHARED=OFF to disable)")
add_library(gainput SHARED ${sources} ${mmsources})
set_target_properties(gainput PROPERTIES
OUTPUT_NAME gainput
DEBUG_POSTFIX -d
VERSION ${GAINPUT_VERSION}
SOVERSION ${GAINPUT_MAJOR_VERSION}
FOLDER gainput
)
set(install_libs ${install_libs} gainput)
endif (GAINPUT_BUILD_SHARED)
add_library(gainput ${sources} ${mmsources})
set_target_properties(gainput PROPERTIES
OUTPUT_NAME gainput
DEBUG_POSTFIX -d
VERSION ${GAINPUT_VERSION}
SOVERSION ${GAINPUT_MAJOR_VERSION}
FOLDER gainput
)
set(install_libs ${install_libs} gainput)

if (GAINPUT_BUILD_STATIC)
message(STATUS "..Building static libraries (-DGAINPUT_BUILD_STATIC=OFF to disable)")
add_library(gainputstatic STATIC ${sources} ${mmsources})
set_target_properties(gainputstatic PROPERTIES DEBUG_POSTFIX -d FOLDER gainput)
set(install_libs ${install_libs} gainputstatic)
endif (GAINPUT_BUILD_STATIC)
target_include_directories(gainput PUBLIC include)

if(WIN32)
target_link_libraries(gainput ${XINPUT} ws2_32)
target_link_libraries(gainputstatic ${XINPUT} ws2_32)
add_definitions(-DGAINPUT_LIB_DYNAMIC=1)
target_link_libraries(gainput PUBLIC ${XINPUT} ws2_32)
if(BUILD_SHARED_LIBS)
target_compile_definitions(gainput PRIVATE -DGAINPUT_LIB_DYNAMIC=1)
# target_compile_definitions(gainput INTERFACE -DGAINPUT_LIB_DYNAMIC_USE=1)
endif(BUILD_SHARED_LIBS)
elseif(ANDROID)
target_link_libraries(gainputstatic native_app_glue log android)
target_link_libraries(gainput native_app_glue log android)
target_link_libraries(gainput PUBLIC native_app_glue log android)
elseif(APPLE)
find_library(FOUNDATION Foundation)
find_library(IOKIT IOKit)
find_library(FOUNDATION Foundation)
find_library(IOKIT IOKit)
find_library(GAME_CONTROLLER GameController)
target_link_libraries(gainput ${FOUNDATION} ${IOKIT} ${GAME_CONTROLLER})
target_link_libraries(gainput PUBLIC ${FOUNDATION} ${IOKIT} ${GAME_CONTROLLER})
if(IOS)
find_library(UIKIT UIKit)
find_library(COREMOTION CoreMotion)
find_library(QUARTZCORE QuartzCore)
target_link_libraries(gainput ${UIKIT} ${COREMOTION})
find_library(UIKIT UIKit)
find_library(COREMOTION CoreMotion)
find_library(QUARTZCORE QuartzCore)
target_link_libraries(gainput PUBLIC ${UIKIT} ${COREMOTION})
else()
find_library(APPKIT AppKit)
target_link_libraries(gainput ${APPKIT})
find_library(APPKIT AppKit)
target_link_libraries(gainput PUBLIC ${APPKIT})
endif()
endif()

# Library installation directory
if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
set(CMAKE_INSTALL_LIBDIR lib)
set(CMAKE_INSTALL_LIBDIR lib)
endif(NOT DEFINED CMAKE_INSTALL_LIBDIR)
set(libdir ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})

install(
DIRECTORY "include/gainput"
DESTINATION "include"
FILES_MATCHING PATTERN "*.h"
DIRECTORY "include/gainput"
DESTINATION "include"
FILES_MATCHING PATTERN "*.h"
)

install(
Expand Down
4 changes: 2 additions & 2 deletions lib/include/gainput/GainputContainers.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ class GAINPUT_LIBEXPORT HashMap

if (vi == InvalidKey)
{
keys_[ha] = values_.size();
keys_[ha] = static_cast<uint32_t>(values_.size());
}
else
{
for (;;)
{
if (values_[vi].next == InvalidKey)
{
values_[vi].next = values_.size();
values_[vi].next = static_cast<uint32_t>( values_.size() );
break;
}
else
Expand Down
2 changes: 1 addition & 1 deletion lib/include/gainput/gainput.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ template <class T> T Abs(T a) { return a < T() ? -a : a; }
* The pages can also be found hosted here:
* http://gainput.johanneskuhlmann.de/html5client/
*/
void DevSetHttp(bool enable);
void GAINPUT_LIBEXPORT DevSetHttp(bool enable);
}

#define GAINPUT_VER_MAJOR_SHIFT 16
Expand Down
5 changes: 3 additions & 2 deletions lib/source/gainput/GainputInputMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ InputMap::InputMap(InputManager& manager, const char* name, Allocator& allocator
listeners_(allocator_),
sortedListeners_(allocator_),
nextListenerId_(0),
managerListener_(0)
managerListener_(0),
managerListenerId_(0)
{
static unsigned nextId = 0;
id_ = nextId++;

if (name)
{
name_ = static_cast<char*>(allocator_.Allocate(strlen(name) + 1));
strcpy(name_, name);
strcpy_s(name_, strlen(name) + 1, name);
}
GAINPUT_DEV_NEW_MAP(this);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/source/gainput/builtin/GainputInputDeviceBuiltIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ InputDeviceBuiltIn::GetButtonName(DeviceButtonId deviceButton, char* buffer, siz
GAINPUT_ASSERT(IsValidButtonId(deviceButton));
GAINPUT_ASSERT(buffer);
GAINPUT_ASSERT(bufferLength > 0);
strncpy(buffer, deviceButtonInfos[deviceButton].name, bufferLength);
strncpy_s(buffer, bufferLength, deviceButtonInfos[deviceButton].name, bufferLength);
buffer[bufferLength-1] = 0;
const size_t nameLen = strlen(deviceButtonInfos[deviceButton].name);
return nameLen >= bufferLength ? bufferLength : nameLen+1;
Expand Down
2 changes: 1 addition & 1 deletion lib/source/gainput/keyboard/GainputInputDeviceKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ InputDeviceKeyboard::GetButtonName(DeviceButtonId deviceButton, char* buffer, si
{
return 0;
}
strncpy(buffer, it->second, bufferLength);
strncpy_s(buffer, bufferLength, it->second, bufferLength);
buffer[bufferLength-1] = 0;
const size_t nameLen = strlen(it->second);
return nameLen >= bufferLength ? bufferLength : nameLen+1;
Expand Down
6 changes: 3 additions & 3 deletions lib/source/gainput/keyboard/GainputInputDeviceKeyboardWin.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class InputDeviceKeyboardImplWin : public InputDeviceKeyboardImpl
return;
}

const int key = msg.wParam;
const int key = (int)msg.wParam;
if (key == 0x08 // backspace
|| key == 0x0A // linefeed
|| key == 0x1B // escape
Expand All @@ -189,12 +189,12 @@ class InputDeviceKeyboardImplWin : public InputDeviceKeyboardImpl
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
pressed = true;
winKey = msg.wParam;
winKey = (unsigned)msg.wParam;
break;
case WM_KEYUP:
case WM_SYSKEYUP:
pressed = false;
winKey = msg.wParam;
winKey = (unsigned)msg.wParam;
break;
default: // Non-keyboard message
return;
Expand Down
2 changes: 1 addition & 1 deletion lib/source/gainput/mouse/GainputInputDeviceMouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ InputDeviceMouse::GetButtonName(DeviceButtonId deviceButton, char* buffer, size_
GAINPUT_ASSERT(IsValidButtonId(deviceButton));
GAINPUT_ASSERT(buffer);
GAINPUT_ASSERT(bufferLength > 0);
strncpy(buffer, deviceButtonInfos[deviceButton].name, bufferLength);
strncpy_s(buffer, bufferLength, deviceButtonInfos[deviceButton].name, bufferLength);
buffer[bufferLength-1] = 0;
const size_t nameLen = strlen(deviceButtonInfos[deviceButton].name);
return nameLen >= bufferLength ? bufferLength : nameLen+1;
Expand Down
2 changes: 1 addition & 1 deletion lib/source/gainput/pad/GainputInputDevicePad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ InputDevicePad::GetButtonName(DeviceButtonId deviceButton, char* buffer, size_t
GAINPUT_ASSERT(IsValidButtonId(deviceButton));
GAINPUT_ASSERT(buffer);
GAINPUT_ASSERT(bufferLength > 0);
strncpy(buffer, deviceButtonInfos[deviceButton].name, bufferLength);
strncpy_s(buffer, bufferLength, deviceButtonInfos[deviceButton].name, bufferLength);
buffer[bufferLength-1] = 0;
const size_t nameLen = strlen(deviceButtonInfos[deviceButton].name);
return nameLen >= bufferLength ? bufferLength : nameLen+1;
Expand Down
2 changes: 1 addition & 1 deletion lib/source/gainput/touch/GainputInputDeviceTouch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ InputDeviceTouch::GetButtonName(DeviceButtonId deviceButton, char* buffer, size_
GAINPUT_ASSERT(IsValidButtonId(deviceButton));
GAINPUT_ASSERT(buffer);
GAINPUT_ASSERT(bufferLength > 0);
strncpy(buffer, deviceButtonInfos[deviceButton].name, bufferLength);
strncpy_s(buffer, bufferLength, deviceButtonInfos[deviceButton].name, bufferLength);
buffer[bufferLength-1] = 0;
const size_t nameLen = strlen(deviceButtonInfos[deviceButton].name);
return nameLen >= bufferLength ? bufferLength : nameLen+1;
Expand Down
15 changes: 8 additions & 7 deletions samples/basic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

project(basicsample)

include_directories(../../lib/include/)

file(GLOB_RECURSE sources *.cpp)

if(APPLE)
Expand All @@ -25,13 +23,16 @@ else()
add_executable(basicsample WIN32 ${sources} ${mmsources})
endif()

target_link_libraries(basicsample gainputstatic)
target_link_libraries(basicsample gainput)

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(basicsample X11 GL rt)
elseif(WIN32)
target_link_libraries(basicsample ${XINPUT} ws2_32)
elseif(APPLE)
target_link_libraries(basicsample ${FOUNDATION} ${IOKIT} ${APPKIT} ${GAME_CONTROLLER})
endif()

if(BUILD_SHARED_LIBS)
# Copy the output of gainput shared library to output folder.
add_custom_command( TARGET basicsample
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:gainput> $<TARGET_FILE_DIR:basicsample>
)
endif(BUILD_SHARED_LIBS)
4 changes: 2 additions & 2 deletions samples/basic/basicsample_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <windows.h>

#include <stdio.h>
#define LOG(...) {char buf[256]; sprintf(buf, __VA_ARGS__); OutputDebugStringA(buf); }
#define LOG(...) {char buf[256]; sprintf_s(buf, 256, __VA_ARGS__); OutputDebugStringA(buf); }


// Define your user buttons
Expand Down Expand Up @@ -38,7 +38,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 5, 5, greeting, strlen(greeting));
TextOut(hdc, 5, 5, greeting, (int)strlen(greeting));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
Expand Down
16 changes: 10 additions & 6 deletions samples/dynamic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

project(dynamicsample)

include_directories(../../lib/include/)
include_directories(../samplefw/)

file(GLOB_RECURSE sources *.cpp)
Expand All @@ -13,13 +12,18 @@ else()
add_executable(dynamicsample WIN32 ${sources} ${sfwsources})
endif()

target_link_libraries(dynamicsample gainputstatic)
target_include_directories(dynamicsample PRIVATE ../samplefw/)
target_link_libraries(dynamicsample gainput)

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(dynamicsample X11 GL rt)
elseif(WIN32)
target_link_libraries(dynamicsample ${XINPUT} ws2_32)
elseif(APPLE)
target_link_libraries(dynamicsample ${FOUNDATION} ${IOKIT} ${APPKIT})
endif()

if(BUILD_SHARED_LIBS)
# Copy the output of gainput shared library to output folder.
add_custom_command( TARGET dynamicsample
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:gainput> $<TARGET_FILE_DIR:dynamicsample>
)
endif(BUILD_SHARED_LIBS)

16 changes: 10 additions & 6 deletions samples/gesture/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

project(gesturesample)

include_directories(../../lib/include/)
include_directories(../samplefw/)

file(GLOB_RECURSE sources *.cpp)
Expand All @@ -13,13 +12,18 @@ else()
add_executable(gesturesample WIN32 ${sources} ${sfwsources})
endif()

target_link_libraries(gesturesample gainputstatic)
target_include_directories(gesturesample PRIVATE ../samplefw/)
target_link_libraries(gesturesample gainput)

if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(gesturesample X11 GL rt)
elseif(WIN32)
target_link_libraries(gesturesample ${XINPUT} ws2_32)
elseif(APPLE)
target_link_libraries(gesturesample ${FOUNDATION} ${IOKIT} ${APPKIT})
endif()

if(BUILD_SHARED_LIBS)
# Copy the output of gainput shared library to output folder.
add_custom_command( TARGET gesturesample
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:gainput> $<TARGET_FILE_DIR:gesturesample>
)
endif(BUILD_SHARED_LIBS)

Loading