From ce77102293bc1ffbfe15b0665b4aeee5cf8af757 Mon Sep 17 00:00:00 2001 From: Tom Bettany Date: Mon, 29 Oct 2018 21:13:30 +0000 Subject: [PATCH] Touch: Make detection into a library Split the detection script into a library to allow other apps to cleanly use it. Also add optional Conan support. --- .gitignore | 4 ++ Makefile | 6 ++- debian/changelog | 1 + debian/control | 14 +++++ debian/copyright | 2 +- debian/kano-touch-support-dbg.install | 2 + debian/kano-touch-support-dev.install | 2 + debian/kano-touch-support.install | 8 +-- debian/rules | 2 +- pytest.ini | 6 ++- touch-detect/CMakeLists.txt | 52 +++++++++++++++++++ touch-detect/Makefile | 9 ++++ touch-detect/README.md | 44 ++++++++++++++++ touch-detect/build.sh | 3 -- touch-detect/conanfile.py | 48 +++++++++++++++++ .../system/kano-touch-flip-xorg.service | 0 .../etc/systemd}/user/kano-touch-flip.service | 0 .../conf}/xorg.conf.d/80-kano-touch.conf | 0 .../include/Kano/TouchDetect/touch_detect.h | 23 ++++++++ .../platforms/linux/touch_detect.cpp} | 51 +++++++++--------- .../lib/platforms/macosx/touch_detect.cpp | 20 +++++++ touch-detect/{ => scripts}/flip_qt.sh | 0 touch-detect/{ => scripts}/flip_xorg.sh | 0 touch-detect/{ => scripts}/rotate_fns.sh | 0 touch-detect/{ => scripts}/set_rotate.sh | 0 touch-detect/src/main.cpp | 25 +++++++++ touch-detect/test_package/CMakeLists.txt | 15 ++++++ touch-detect/test_package/README.md | 4 ++ touch-detect/test_package/conanfile.py | 33 ++++++++++++ touch-detect/test_package/example.cpp | 20 +++++++ 30 files changed, 359 insertions(+), 35 deletions(-) create mode 100644 debian/kano-touch-support-dbg.install create mode 100644 debian/kano-touch-support-dev.install create mode 100644 touch-detect/CMakeLists.txt create mode 100644 touch-detect/Makefile create mode 100644 touch-detect/README.md delete mode 100755 touch-detect/build.sh create mode 100644 touch-detect/conanfile.py rename {systemd => touch-detect/conf/etc/systemd}/system/kano-touch-flip-xorg.service (100%) rename {systemd => touch-detect/conf/etc/systemd}/user/kano-touch-flip.service (100%) rename {conf => touch-detect/conf}/xorg.conf.d/80-kano-touch.conf (100%) create mode 100644 touch-detect/include/Kano/TouchDetect/touch_detect.h rename touch-detect/{touch-detect.c => lib/platforms/linux/touch_detect.cpp} (60%) create mode 100644 touch-detect/lib/platforms/macosx/touch_detect.cpp rename touch-detect/{ => scripts}/flip_qt.sh (100%) rename touch-detect/{ => scripts}/flip_xorg.sh (100%) rename touch-detect/{ => scripts}/rotate_fns.sh (100%) rename touch-detect/{ => scripts}/set_rotate.sh (100%) create mode 100644 touch-detect/src/main.cpp create mode 100644 touch-detect/test_package/CMakeLists.txt create mode 100644 touch-detect/test_package/README.md create mode 100644 touch-detect/test_package/conanfile.py create mode 100644 touch-detect/test_package/example.cpp diff --git a/.gitignore b/.gitignore index ab30947..3c24a65 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,10 @@ cscope.po.out reports .pytest_cache +# Build dirs +touch-detect/release +touch-detect/debug + # Created by https://www.gitignore.io/api/python,c,c++,vim,osx,sublimetext,cmake diff --git a/Makefile b/Makefile index 7850392..1c1cae0 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ REPO:= kano-peripherals -.PHONY: clean docs libs +.PHONY: clean docs libs touch-detect clean: cd docs && make clean @@ -18,6 +18,10 @@ docs: libs: cd libs/pi_hat && cmake . && make +touch-detect: + cd touch-detect && make release + cd touch-detect && make debug + # # Add test targets # diff --git a/debian/changelog b/debian/changelog index 22087d7..cfb307a 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,7 @@ kano-peripherals (4.2.0-0) unstable; urgency=low * Fixed an issue where the sound module wouldn't be loaded when daemon crashed + * Make the touch-detect tool into a library -- Team Kano Thu, 4 Oct 2018 17:54:00 +0100 diff --git a/debian/control b/debian/control index 49da131..48b4de9 100644 --- a/debian/control +++ b/debian/control @@ -59,3 +59,17 @@ Depends: libxi6 Description: Support for touchscreen detection and flipping +Package: kano-touch-support-dev +Architecture: any +Depends: + ${misc:Depends}, + kano-touch-support +Description: Development tools to support touchscreen detection and flipping + +Package: kano-touch-support-dbg +Architecture: any +Depends: + ${misc:Depends}, + kano-touch-support +Description: Debug symbols for support for touchscreen detection and flipping + diff --git a/debian/copyright b/debian/copyright index 98f48ef..635a823 100644 --- a/debian/copyright +++ b/debian/copyright @@ -9,6 +9,6 @@ Files: kano_peripherals/speaker_leds/driver/pwm_driver.py: Copyright: 2012-2013 Limor Fried, Kevin Townsend and Mikey Sklar for Adafruit Industries. Licence: BSD-3-clause -Files: touch-detect/touch-detect.c +Files: touch-detect/lib/platforms/macosx/touch_detect.cpp Copyright: 1996-1997 by Frederic Lepied, France, 2007 Peter Hutterer, 2009 Red Hat, Inc, 2018 Kano Computing Ltd Licence: MIT diff --git a/debian/kano-touch-support-dbg.install b/debian/kano-touch-support-dbg.install new file mode 100644 index 0000000..edd5d32 --- /dev/null +++ b/debian/kano-touch-support-dbg.install @@ -0,0 +1,2 @@ +touch-detect/build/debug/bin/touch-detect usr/lib/debug/usr/bin/ +touch-detect/build/debug/lib/*.so usr/lib/debug/usr/lib diff --git a/debian/kano-touch-support-dev.install b/debian/kano-touch-support-dev.install new file mode 100644 index 0000000..794fe0a --- /dev/null +++ b/debian/kano-touch-support-dev.install @@ -0,0 +1,2 @@ +touch-detect/include/* usr/include/ +touch-detect/build/release/lib/*.a usr/lib diff --git a/debian/kano-touch-support.install b/debian/kano-touch-support.install index 57abf5b..641e711 100644 --- a/debian/kano-touch-support.install +++ b/debian/kano-touch-support.install @@ -1,3 +1,5 @@ -touch-detect/touch-detect usr/bin/ -touch-detect/*.sh usr/share/kano-peripherals/scripts/ -conf/xorg.conf.d/* usr/share/kano-peripherals/xorg.conf.d/ +touch-detect/build/release/bin/touch-detect usr/bin/ +touch-detect/build/release/lib/*.so usr/lib/ +touch-detect/scripts/*.sh usr/share/kano-peripherals/scripts/ +touch-detect/conf/xorg.conf.d/* usr/share/kano-peripherals/xorg.conf.d/ +touch-detect/conf/etc/* etc/ diff --git a/debian/rules b/debian/rules index b15c6e4..0552b45 100755 --- a/debian/rules +++ b/debian/rules @@ -7,7 +7,7 @@ override_dh_auto_build: cd libs/pi_hat && cmake . && make cd po && make messages.pot cd po && make - cd touch-detect && ./build.sh + make touch-detect override_dh_fixperms: dh_fixperms diff --git a/pytest.ini b/pytest.ini index 867de76..c7c760d 100644 --- a/pytest.ini +++ b/pytest.ini @@ -10,5 +10,7 @@ flake8-ignore = F841 # Local variable is assigned to but never used features/* ALL tests/* ALL - libs/pi_hat/examples/python/* ALL - kano-doc/* ALL + kano_peripherals/speaker_leds/driver/pwm_driver.py ALL + libs/pi_hat/examples/python/* ALL + kano-doc/* ALL + **/conanfile.py ALL diff --git a/touch-detect/CMakeLists.txt b/touch-detect/CMakeLists.txt new file mode 100644 index 0000000..ed919d2 --- /dev/null +++ b/touch-detect/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 2.8) +project(TouchDetect CXX) + +if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) + include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) + conan_basic_setup() +endif() + + +if (APPLE) # OS X + set(LINK_LIBS "") + set(PLATFORM_SRC lib/platforms/macosx) +elseif (UNIX AND NOT APPLE) # Linux + set(LINK_LIBS X11 Xi) + set(PLATFORM_SRC lib/platforms/linux) +endif() + + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib) + + +add_library(touch_detect_lib SHARED + ${PLATFORM_SRC}/touch_detect.cpp +) +target_link_libraries(touch_detect_lib ${LINK_LIBS}) +set_target_properties(touch_detect_lib PROPERTIES OUTPUT_NAME "touch_detect") +target_include_directories(touch_detect_lib PUBLIC + $ + $ +) + + +add_library(touch_detect_lib_static STATIC + ${PLATFORM_SRC}/touch_detect.cpp +) +target_link_libraries(touch_detect_lib_static ${LINK_LIBS}) +set_target_properties(touch_detect_lib_static PROPERTIES OUTPUT_NAME "touch_detect") +target_include_directories(touch_detect_lib_static PUBLIC + $ + $ +) + + +add_executable(touch_detect + src/main.cpp +) +set_target_properties(touch_detect PROPERTIES OUTPUT_NAME "touch-detect") +target_link_libraries(touch_detect + touch_detect_lib +) diff --git a/touch-detect/Makefile b/touch-detect/Makefile new file mode 100644 index 0000000..3041e0f --- /dev/null +++ b/touch-detect/Makefile @@ -0,0 +1,9 @@ +all: debug release + +release: + mkdir -p build/release + cd build/release && cmake -DCMAKE_BUILD_TYPE=Release ../.. && make + +debug: + mkdir -p build/debug + cd build/debug && cmake -DCMAKE_BUILD_TYPE=Debug ../.. && make diff --git a/touch-detect/README.md b/touch-detect/README.md new file mode 100644 index 0000000..af8a6ba --- /dev/null +++ b/touch-detect/README.md @@ -0,0 +1,44 @@ +# Touch Detect + +## Building + +Create the build directory + +``` +mkdir -p build +cd build +``` + +(Optional) Install dependencies with `conan` (otherwise ensure that you have +the dependencies available to the build system) + +``` +conan install .. +``` + +Build + +``` +cmake .. +make +``` + +## Conan (Experimental) + +This package has been created to be (optionally) shipped with a conan package, +hence you can do an operation like: + +``` +cd build +conan create .. KanoComputing/stable +``` + +This can then be pushed to the artifactory and used as a dependency of another +project. + +To use this package, make sure that your `conanfile.txt` looks like this + +``` +[requires] +touch-detect/4.2.0@KanoComputing/stable +``` diff --git a/touch-detect/build.sh b/touch-detect/build.sh deleted file mode 100755 index b028e99..0000000 --- a/touch-detect/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -gcc -g touch-detect.c -o touch-detect -lX11 -lXi - diff --git a/touch-detect/conanfile.py b/touch-detect/conanfile.py new file mode 100644 index 0000000..b6614bc --- /dev/null +++ b/touch-detect/conanfile.py @@ -0,0 +1,48 @@ +# +# conanfile.py +# +# Copyright (C) 2018 Kano Computing Ltd. +# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2 +# +# Specifies the touch-detect package for Conan +# + + +from conans import ConanFile, CMake + + +class TouchdetectConan(ConanFile): + name = "touch-detect" + version = "4.2.0" + license = "" + url = "" + description = "" + settings = "os", "compiler", "build_type", "arch" + options = {"shared": [True, False]} + default_options = "shared=False" + generators = "cmake" + exports_sources = "src/*", "lib/*", "include/*", "CMakeLists.txt" + + def build(self): + cmake = CMake(self) + cmake.configure(source_folder="") + cmake.build() + + def package(self): + self.copy("*.h", dst="include", src="include") + self.copy("*.lib", dst="lib", keep_path=False) + self.copy("*.dll", dst="bin", keep_path=False) + self.copy("*.dylib*", dst="lib", keep_path=False) + self.copy("*.so", dst="lib", keep_path=False) + self.copy("*.a", dst="lib", keep_path=False) + + def package_info(self): + self.cpp_info.libs = ["touch_detect"] + + def deploy(self): + self.copy("*.h") + self.copy("*.lib") + self.copy("*.dll") + self.copy("*.dylib*") + self.copy("*.so") + self.copy("*.a") diff --git a/systemd/system/kano-touch-flip-xorg.service b/touch-detect/conf/etc/systemd/system/kano-touch-flip-xorg.service similarity index 100% rename from systemd/system/kano-touch-flip-xorg.service rename to touch-detect/conf/etc/systemd/system/kano-touch-flip-xorg.service diff --git a/systemd/user/kano-touch-flip.service b/touch-detect/conf/etc/systemd/user/kano-touch-flip.service similarity index 100% rename from systemd/user/kano-touch-flip.service rename to touch-detect/conf/etc/systemd/user/kano-touch-flip.service diff --git a/conf/xorg.conf.d/80-kano-touch.conf b/touch-detect/conf/xorg.conf.d/80-kano-touch.conf similarity index 100% rename from conf/xorg.conf.d/80-kano-touch.conf rename to touch-detect/conf/xorg.conf.d/80-kano-touch.conf diff --git a/touch-detect/include/Kano/TouchDetect/touch_detect.h b/touch-detect/include/Kano/TouchDetect/touch_detect.h new file mode 100644 index 0000000..170afea --- /dev/null +++ b/touch-detect/include/Kano/TouchDetect/touch_detect.h @@ -0,0 +1,23 @@ +/** + * touch_detect.h + * + * Copyright (C) 2018 Kano Computing Ltd. + * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2 + * + * vim: filetype=cpp + * + * Detect if a touch device is present + * + */ + + +#pragma once + + +namespace Kano +{ + namespace TouchDetect + { + bool isTouchSupported(); + } +} diff --git a/touch-detect/touch-detect.c b/touch-detect/lib/platforms/linux/touch_detect.cpp similarity index 60% rename from touch-detect/touch-detect.c rename to touch-detect/lib/platforms/linux/touch_detect.cpp index ac06d6f..984c531 100644 --- a/touch-detect/touch-detect.c +++ b/touch-detect/lib/platforms/linux/touch_detect.cpp @@ -1,6 +1,6 @@ /** * - * touch-detect + * touch_detect.cpp * * Copyright (C) 2018 Kano Computing Ltd. * License: MIT @@ -17,46 +17,49 @@ * */ + #include #include #include #include #include -int isTouch(void) { - int found = 1; - Display *display = XOpenDisplay(NULL); - int ndevices=0, i, j; - if(!display) - { + +#include + + +bool Kano::TouchDetect::isTouchSupported() { + bool found = false; + Display *display = XOpenDisplay(nullptr); + int ndevices = 0; + + if (!display) { syslog(LOG_ERR | LOG_USER, "touch-detect: could not connect to display\n"); - exit(1); + return false; } - XIDeviceInfo *info, *dev; - info = XIQueryDevice(display, XIAllDevices, &ndevices); - for(i = 0; i < ndevices; i++) - { - dev = &info[i]; - if(dev->use != XISlavePointer) + XIDeviceInfo *info = XIQueryDevice(display, XIAllDevices, &ndevices); + + for (int i = 0; i < ndevices; i++) { + XIDeviceInfo *dev = &info[i]; + + if (dev->use != XISlavePointer) continue; + dev->classes, dev->num_classes; - for (j = 0; j < dev->num_classes; j++) - { - if(dev->classes[j]->type == XITouchClass) - { + + for (int j = 0; j < dev->num_classes; j++) { + if (dev->classes[j]->type == XITouchClass) { printf("%d\n", dev->deviceid); - found = 0; + found = true; } } } + XIFreeDeviceInfo(info); + if (display) XCloseDisplay(display); - return found; -} -int main(int argc, char * argv[]) -{ - return isTouch(); + return found; } diff --git a/touch-detect/lib/platforms/macosx/touch_detect.cpp b/touch-detect/lib/platforms/macosx/touch_detect.cpp new file mode 100644 index 0000000..a9a23d1 --- /dev/null +++ b/touch-detect/lib/platforms/macosx/touch_detect.cpp @@ -0,0 +1,20 @@ +/** + * touch_detect.cpp + * + * Copyright (C) 2018 Kano Computing Ltd. + * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2 + * + * Detect if a touch device is present + * + */ + + +#include + +#include + + +bool Kano::TouchDetect::isTouchSupported() { + std::cout << "Not implemented for OSX, returning true\n"; + return true; +} diff --git a/touch-detect/flip_qt.sh b/touch-detect/scripts/flip_qt.sh similarity index 100% rename from touch-detect/flip_qt.sh rename to touch-detect/scripts/flip_qt.sh diff --git a/touch-detect/flip_xorg.sh b/touch-detect/scripts/flip_xorg.sh similarity index 100% rename from touch-detect/flip_xorg.sh rename to touch-detect/scripts/flip_xorg.sh diff --git a/touch-detect/rotate_fns.sh b/touch-detect/scripts/rotate_fns.sh similarity index 100% rename from touch-detect/rotate_fns.sh rename to touch-detect/scripts/rotate_fns.sh diff --git a/touch-detect/set_rotate.sh b/touch-detect/scripts/set_rotate.sh similarity index 100% rename from touch-detect/set_rotate.sh rename to touch-detect/scripts/set_rotate.sh diff --git a/touch-detect/src/main.cpp b/touch-detect/src/main.cpp new file mode 100644 index 0000000..8736a55 --- /dev/null +++ b/touch-detect/src/main.cpp @@ -0,0 +1,25 @@ +/** + * main.cpp + * + * Copyright (C) 2018 Kano Computing Ltd. + * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2 + * + * Tool to detect a touchscreen is present, and list xinput devices + * + * Exit code is 0 when this is the case, 1 when no touch device is present + * Stdout is xinput ids of the touch devices, one per line. + * + */ + + +#include + + +int main(int argc, char *argv[]) +{ + if (Kano::TouchDetect::isTouchSupported()) { + return 0; + } else { + return 1; + } +} diff --git a/touch-detect/test_package/CMakeLists.txt b/touch-detect/test_package/CMakeLists.txt new file mode 100644 index 0000000..0c137cd --- /dev/null +++ b/touch-detect/test_package/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 2.8.12) +project(PackageTest CXX) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup() + +add_executable(example example.cpp) +target_link_libraries(example ${CONAN_LIBS}) +message(${CONAN_LIBS}) + +# CTest is a testing tool that can be used to test your project. +# enable_testing() +# add_test(NAME example +# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin +# COMMAND example) diff --git a/touch-detect/test_package/README.md b/touch-detect/test_package/README.md new file mode 100644 index 0000000..c4e6abc --- /dev/null +++ b/touch-detect/test_package/README.md @@ -0,0 +1,4 @@ +# Conan Test Package + +This exists in order to test that the touch-detect package has been correctly +configured for packaging. diff --git a/touch-detect/test_package/conanfile.py b/touch-detect/test_package/conanfile.py new file mode 100644 index 0000000..28a8f90 --- /dev/null +++ b/touch-detect/test_package/conanfile.py @@ -0,0 +1,33 @@ +# +# conanfile.py +# +# Copyright (C) 2018 Kano Computing Ltd. +# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2 +# +# Conan descriptor to test the touch-detect package is correctly packaged +# + + +import os + +from conans import ConanFile, CMake, tools + + +class TouchdetectTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "cmake" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def imports(self): + self.copy("*.dll", dst="bin", src="bin") + self.copy("*.dylib*", dst="bin", src="lib") + self.copy('*.so*', dst='bin', src='lib') + + def test(self): + if not tools.cross_building(self.settings): + os.chdir("bin") + self.run(".%sexample" % os.sep) diff --git a/touch-detect/test_package/example.cpp b/touch-detect/test_package/example.cpp new file mode 100644 index 0000000..885d947 --- /dev/null +++ b/touch-detect/test_package/example.cpp @@ -0,0 +1,20 @@ +/** + * example.cpp + * + * Copyright (C) 2018 Kano Computing Ltd. + * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2 + * + * Simple program to test Conan packaging setup + * + */ + + +#include + +#include + + +int main(int argc, char *argv[]) +{ + std::cout << Kano::TouchDetect::isTouchSupported(); +}