Restructure and add compiler tests

This commit is contained in:
Rico Tiongson 2020-04-18 01:05:01 +08:00
parent 07acbb4d1d
commit 0a8ec16256
32 changed files with 211 additions and 100 deletions

View File

@ -1,7 +0,0 @@
#!/bin/sh
dir="$(dirname $0)"
g++ "$dir/comfortable-swipe.cpp" \
-o "$1" \
-std=c++11 \
-O2 -lxdo -Wno-unused-result \
-DCOMFORTABLE_SWIPE_VERSION="\"$(cat $dir/VERSION | tr -d '[:space:]')\""

View File

@ -1,5 +1,12 @@
#ifndef __COMFORTABLE_SWIPE__index_hpp__
#define __COMFORTABLE_SWIPE__index_hpp__
/**
* Add header files will be imported here.
* You can import this as a shorthand:
*
* #include "../all_headers.hpp"
*/
#ifndef __COMFORTABLE_SWIPE__all_headers_hpp__
#define __COMFORTABLE_SWIPE__all_headers_hpp__
/*
Comfortable Swipe
@ -36,21 +43,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <map> // std::map
#include <string> // std::string
/**
* Make sure to include your header files here so that they can be imported by other modules.
*/
#include "gesture/xdo_gesture.h"
#include "gesture/swipe_gesture.h"
extern "C"
{
namespace comfortable_swipe
{
// driver method
int driver();
// this is found in the util/ folder
namespace util
{
const char* autostart_filename();
constexpr const char* conf_filename();
std::map<std::string, std::string> read_config_file(const char*);
}
// this is found in the service/ folder
namespace service
{
void autostart();
@ -66,4 +72,10 @@ extern "C"
}
}
#endif /* __COMFORTABLE_SWIPE__index_hpp__ */
// add headers for classes here so it can be imported during include
#include "gesture/xdo_gesture.h"
#include "gesture/swipe_gesture.h"
#include "gesture/keyboard_swipe_gesture.h"
#include "gesture/mouse_hold_gesture.h"
#endif /* __COMFORTABLE_SWIPE__all_headers_hpp__ */

View File

@ -1,5 +1,5 @@
#ifndef __COMFORTABLE_SWIPE__service_buffer__
#define __COMFORTABLE_SWIPE__service_buffer__
#ifndef __COMFORTABLE_SWIPE__driver__
#define __COMFORTABLE_SWIPE__driver__
/*
Comfortable Swipe
@ -21,15 +21,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cstdio> // fgets_unlocked, stdin
#include <iostream> // std::ios, std::cout, std::cin
#include "../index.hpp"
#include "all_headers.hpp"
/**
* Starts the comfortable-swipe service by buffering libinput debug-events.
* The main driver program.
*/
namespace comfortable_swipe::service
namespace comfortable_swipe
{
void buffer()
int driver()
{
// unsync for faster IO
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
@ -40,6 +41,19 @@ namespace comfortable_swipe::service
comfortable_swipe::util::conf_filename()
);
// initialize mouse hold gesture handler
// for now, this supports 3-finger and 4-finger hold
// Examples:
// hold3=move move mouse on 3 fingers
// hold3=button1 hold button 1 on 3 fingers
// hold4=button3 hold button 3 (right click) on 3 fingers
// hold3=ignore <do nothing>
comfortable_swipe::gesture::mouse_hold_gesture mouse_hold
(
config["hold3"].c_str(),
config["hold4"].c_str()
);
// initialize keyboard swipe gesture handler
comfortable_swipe::gesture::keyboard_swipe_gesture keyboard_swipe
(
@ -54,13 +68,6 @@ namespace comfortable_swipe::service
config["down4"].c_str()
);
// initialize mouse swipe gesture handler
comfortable_swipe::gesture::mouse_swipe_gesture mouse_hold
(
"",
"mouse1"
);
// prepare data containers
std::array<char, 256> line;
@ -77,7 +84,9 @@ namespace comfortable_swipe::service
keyboard_swipe.parse_line(line.data());
}
}
return 0;
}
}
#endif /* __COMFORTABLE_SWIPE__service_buffer__ */
#endif /* __COMFORTABLE_SWIPE__driver__ */

View File

@ -1,5 +1,5 @@
#ifndef __COMFORTABLE_SWIPE__gesture_mouse_swipe_gesture__
#define __COMFORTABLE_SWIPE__gesture_mouse_swipe_gesture__
#ifndef __COMFORTABLE_SWIPE__gesture_mouse_hold_gesture__
#define __COMFORTABLE_SWIPE__gesture_mouse_hold_gesture__
/*
Comfortable Swipe
@ -21,8 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <iostream> // std::cout, std::endl
#include <cstdio> // std::sscanf
#include <cstdlib> // strncmp
#include "mouse_swipe_gesture.h"
#include <cstring> // strncmp
#include "mouse_hold_gesture.h"
extern "C"
{
@ -36,7 +36,7 @@ namespace comfortable_swipe::gesture
/**
* Constructs a new mouse gesture, given "hold3" and "hold4" configurations.
*/
mouse_swipe_gesture::mouse_swipe_gesture
mouse_hold_gesture::mouse_hold_gesture
(
const char* hold3,
const char* hold4
@ -50,19 +50,24 @@ namespace comfortable_swipe::gesture
/**
* Destructs this mouse swipe gesture.
*/
mouse_swipe_gesture::~mouse_swipe_gesture()
mouse_hold_gesture::~mouse_hold_gesture()
{ }
/**
* Run mousedown command on hold input.
*/
void mouse_swipe_gesture::do_mousedown(const char * mouseinput)
void mouse_hold_gesture::do_mousedown(const char * mouseinput)
{
int mouse = this->parse_mouse_input(mouseinput);
if (mouse != -1)
int button = this->parse_mouse_button(mouseinput);
if (button != -1)
{
// eg. MOUSE DOWN hold3 mouse1
std::printf("MOUSE DOWN hold%d %s\n", this->fingers, mouseinput);
if (button != 0)
{
// send mouse down on associated button
xdo_mouse_down(this->xdo, CURRENTWINDOW, button);
}
this->flag_mousedown = true;
}
}
@ -70,17 +75,17 @@ namespace comfortable_swipe::gesture
/**
* Run mouseup command on hold output.
*/
void mouse_swipe_gesture::do_mouseup(const char * mouseinput)
void mouse_hold_gesture::do_mouseup(const char * mouseinput)
{
int mouse = this->parse_mouse_input(mouseinput);
if (mouse != -1)
int button = this->parse_mouse_button(mouseinput);
if (button != -1)
{
std::printf("MOUSE UP hold%d %s\n", this->fingers, mouseinput);
if (mouse != 0)
if (button != 0)
{
// send mouse up to xdo
// send mouse up on associated button
xdo_mouse_up(this->xdo, CURRENTWINDOW, button);
}
this->flag_mousedown = false;
}
}
@ -89,22 +94,17 @@ namespace comfortable_swipe::gesture
* Utility method to parse mouse number from input.
* Returns -1 on failure.
*/
int mouse_swipe_gesture::parse_mouse_input(const char *input)
int mouse_hold_gesture::parse_mouse_button(const char *input)
{
// check if "mouse" is prefix
if (strncmp(input, "mouse", 5) == 0)
{
if (input[5] == '\0')
{
// just "mouse" without a number
// just move without holding button down
if (std::strcmp(input, "move") == 0)
return 0;
}
int mouseno;
if (std::sscanf(input + 5, "%d", &mouseno) == 1)
// get button number
int button;
if (std::sscanf(input, "button%d", &button) == 1)
{
// parse the number after "mouse"
return mouseno;
}
return button;
}
return -1;
@ -113,7 +113,7 @@ namespace comfortable_swipe::gesture
/**
* Hook on begin of mouse swipe gesture.
*/
void mouse_swipe_gesture::begin()
void mouse_hold_gesture::begin()
{
// call superclass method
swipe_gesture::begin();
@ -131,7 +131,7 @@ namespace comfortable_swipe::gesture
/**
* Hook on end of mouse swipe gesture.
*/
void mouse_swipe_gesture::update()
void mouse_hold_gesture::update()
{
// call superclass method
swipe_gesture::update();
@ -149,7 +149,7 @@ namespace comfortable_swipe::gesture
/**
* Hook on end of swipe gesture.
*/
void mouse_swipe_gesture::end()
void mouse_hold_gesture::end()
{
if (this->is_mousedown())
{
@ -170,10 +170,10 @@ namespace comfortable_swipe::gesture
/**
* Utility method to check if mouse is current held.
*/
bool mouse_swipe_gesture::is_mousedown()
bool mouse_hold_gesture::is_mousedown()
{
return this->flag_mousedown;
}
}
#endif /* __COMFORTABLE_SWIPE__gesture_mouse_swipe_gesture__ */
#endif /* __COMFORTABLE_SWIPE__gesture_mouse_hold_gesture__ */

View File

@ -1,5 +1,5 @@
#ifndef __COMFORTABLE_SWIPE__gesture_mouse_swipe_gesture_h__
#define __COMFORTABLE_SWIPE__gesture_mouse_swipe_gesture_h__
#ifndef __COMFORTABLE_SWIPE__gesture_mouse_hold_gesture_h__
#define __COMFORTABLE_SWIPE__gesture_mouse_hold_gesture_h__
/*
Comfortable Swipe
@ -27,16 +27,16 @@ extern "C" {
namespace comfortable_swipe::gesture
{
class mouse_swipe_gesture : public swipe_gesture
class mouse_hold_gesture : public swipe_gesture
{
public:
// constructor
mouse_swipe_gesture(
mouse_hold_gesture(
const char* hold3, // 3 finger mouse down
const char* hold4 // 4 finger mouse down
);
virtual ~mouse_swipe_gesture();
virtual ~mouse_hold_gesture();
// override begin and end for mousedown
virtual void begin() override;
@ -49,7 +49,7 @@ namespace comfortable_swipe::gesture
virtual bool is_mousedown();
// utility method to parse mouse input given config characters
virtual int parse_mouse_input(const char*);
virtual int parse_mouse_button(const char*);
protected:
// command holders
@ -65,4 +65,4 @@ namespace comfortable_swipe::gesture
}
#endif
#endif /* __COMFORTABLE_SWIPE__gesture_mouse_swipe_gesture_h__ */
#endif /* __COMFORTABLE_SWIPE__gesture_mouse_hold_gesture_h__ */

View File

@ -19,17 +19,17 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "index.hpp"
#include "all_headers.hpp"
/**
* Make sure to include all implementation (.cpp) files below to be ready for export.
* Add all cpp files below to be ready for export.
*/
#include "gesture/xdo_gesture.cpp"
#include "gesture/swipe_gesture.cpp"
#include "gesture/swipe_gesture.regex.cpp"
#include "gesture/keyboard_swipe_gesture.cpp"
#include "gesture/mouse_swipe_gesture.cpp"
#include "gesture/mouse_hold_gesture.cpp"
#include "service/autostart.cpp"
#include "service/buffer.cpp"
#include "service/config.cpp"
@ -42,5 +42,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "util/autostart_filename.cpp"
#include "util/conf_filename.cpp"
#include "util/read_config_file.cpp"
#include "driver.cpp"
#endif /* __COMFORTABLE_SWIPE__ */

View File

@ -24,7 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <string> // std::string
#include <cstdio> // std::remove
#include <cstdlib> // std::system
#include "../index.hpp"
#include "../all_headers.hpp"
namespace comfortable_swipe::service
{

View File

@ -0,0 +1,36 @@
#ifndef __COMFORTABLE_SWIPE__service_buffer__
#define __COMFORTABLE_SWIPE__service_buffer__
/*
Comfortable Swipe
by Rico Tiongson
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <cstdlib> // std::exit
#include "../all_headers.hpp"
/**
* Starts the comfortable-swipe service by buffering libinput debug-events.
*/
namespace comfortable_swipe::service
{
void buffer()
{
std::exit(comfortable_swipe::driver());
}
}
#endif /* __COMFORTABLE_SWIPE__service_buffer__ */

View File

@ -19,8 +19,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../index.hpp"
#include <cstdio> // std::puts
#include "../all_headers.hpp"
namespace comfortable_swipe::service
{

View File

@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio> // std::puts, std::printf
#include "../index.hpp"
#include "../all_headers.hpp"
namespace comfortable_swipe::service
{

View File

@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../index.hpp"
#include "../all_headers.hpp"
namespace comfortable_swipe::service
{

View File

@ -19,8 +19,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../index.hpp"
#include <cstdlib> // std::system
#include "../all_headers.hpp"
namespace comfortable_swipe::service
{

View File

@ -19,7 +19,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../index.hpp"
#include <stdexcept> // std::runtime_error
#include <unistd.h> // popen, pclose, getpid, access, F_OK
#include <memory> // std::unique_ptr
@ -27,6 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cstdlib> // std::atoi
#include <cstdio> // FILE, std::feof, std::fgets, std::printf
#include <regex> // std::cmatch, std::regex, std::regex_match
#include "../all_headers.hpp"
namespace comfortable_swipe::service
{

View File

@ -1,9 +1,6 @@
#ifndef __COMFORTABLE_SWIPE__service_stop__
#define __COMFORTABLE_SWIPE__service_stop__
#include <cstdio> // std::FILE, std::feof, std::fgets
#include <cstdlib> // std::atoi, std::system
#include <string> // std::string, std::to_string
/*
Comfortable Swipe
by Rico Tiongson
@ -26,8 +23,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <unistd.h> // popen, pclose, getpid
#include <memory> // std::unique_ptr
#include <array> // std::array
#include <cstdlib> // std::atoi
#include <cstdio> // FILE, std::feof, std::fgets
#include <cstdio> // std::FILE, std::feof, std::fgets
#include <cstdlib> // std::atoi, std::system
#include <string> // std::string, std::to_string
namespace comfortable_swipe::service
{

View File

@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../index.hpp"
#include "../all_headers.hpp"
namespace comfortable_swipe::util
{

View File

@ -19,9 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <ios> // std::ios
#include <iostream> // std::cin, std::cout, std::cerr
#include <string> // std::string
#include "lib/comfortable_swipe"
#include "comfortable_swipe/lib"
/* MAIN DRIVER FUNCTION */
/* Command-line function. */
int main(int argc, char** args)
{

9
cpp.compile.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
dir="$(dirname $0)"
version="$(cat "$dir/VERSION" | tr -d '[:space:]')"
exec g++ $@ \
-std=c++11 \
-O2 -lxdo -Wno-unused-result \
-DCOMFORTABLE_SWIPE_VERSION="\"$version\""

View File

@ -1,11 +1,13 @@
#!/bin/bash
set -e
function install {
# prefer running as root
local dir="$(dirname $0)"
local dir="$(dirname "$0")"
local program=comfortable-swipe
local program_exe=/usr/local/bin/$program
local compile_command="$dir/$program.compile.sh"
local compile_command="$dir/cpp.compile.sh $dir/command_line.cpp"
local conf_path=/usr/local/share/$program/$program.conf
local dconf_path="$dir/defaults.conf"
local old_conf_path="${XDG_CONFIG_HOME:-$HOME/.config}/$program.conf"
@ -70,7 +72,7 @@ function install {
fi
# compile library
sudo "$compile_command" "$program_exe" || abort
sudo "$compile_command" -o "$program_exe" || abort
# add permissions to input group (defer)
# GROUP=$(ls -l /dev/input/event* | awk '{print $4}' | head --line=1) || abort

View File

@ -1,6 +1,24 @@
#!/bin/sh
#!/bin/bash
DIR=$(dirname $0)
g++ -std=c++11 -O2 $DIR/test_regex.cpp -lxdo -o test.out || exec "Test aborted"
./test.out || rm test.out
rm test.out
set -e
DIR="$(dirname "$0")"
# just call abort on error
tempout="$(mktemp)"
abort () {
rm "$tempout"
echo "Test aborted"
exit 1
}
# run all shell files in this directory
for sh in "$DIR/test_"*.sh; do
/bin/bash "$sh" || abort
done
# run all cpp files in this directory
for cpp in "$DIR/test_"*.cpp; do
g++ -std=c++11 -O2 "$cpp" -lxdo -o "$tempout" || abort
"$tempout" || abort
done

32
tests/test_compiler.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash
set -ex
DIR="$(dirname "$0")"
root="$(dirname "$DIR")"
compiler="$root/cpp.compile.sh"
command_line="$root/command_line.cpp"
module="$root/comfortable_swipe"
# just call abort on error
tempout="$(mktemp)"
abort () {
rm "$tempout"
echo "Test aborted"
exit 1
}
# try to compile every cpp/hpp file in module
# header files first
# then compile cpp files
# finally, compile command line
shopt -s nullglob
for cpp in \
"$module"/**/*.h \
"$module"/**/*.hpp \
"$module"/**/*.cpp \
"$module"/lib \
"$command_line"
do
"$compiler" -c "$cpp" -o "$tempout" || abort
done

4
tests/test_regex.cpp Normal file → Executable file
View File

@ -2,8 +2,8 @@
#include <cassert>
#include <regex>
#include <string>
#include "../lib/gesture/swipe_gesture.h"
#include "../lib/gesture/swipe_gesture.regex.cpp"
#include "../comfortable_swipe/gesture/swipe_gesture.h"
#include "../comfortable_swipe/gesture/swipe_gesture.regex.cpp"
/*
Comfortable Swipe

View File

@ -1,7 +1,8 @@
#!/bin/bash
echo "Uninstalling..."
rm ${XDG_CONFIG_HOME:-$HOME/.config}/autostart/comfortable-swipe.desktop 2> /dev/null
rm "${XDG_CONFIG_HOME:-$HOME/.config}/autostart/comfortable-swipe.desktop" 2> /dev/null
comfortable-swipe stop 2> /dev/null
rm $HOME/.local/bin/comfortable-swipe 2> /dev/null # compat
rm "$HOME/.local/bin/comfortable-swipe" 2> /dev/null # compat
sudo rm /usr/local/bin/comfortable-swipe 2> /dev/null
echo "Successfully uninstalled comfortable-swipe"