Skip to content

Commit

Permalink
Added "axis" attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
Penguin-Guru authored and Penguin-Guru committed Dec 3, 2024
1 parent 6f82690 commit 2cfdecd
Show file tree
Hide file tree
Showing 16 changed files with 203 additions and 38 deletions.
27 changes: 17 additions & 10 deletions src/config/config.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,31 @@ std::string Config::getGlobalSetting(const std::string &name) const {
void Config::saveGestureConfig(
const std::string &application, GestureType gestureType,
const std::string &numFingers, GestureDirection gestureDirection,
ActionType actionType,
GestureAxis gestureAxis, ActionType actionType,
const std::unordered_map<std::string, std::string> &actionSettings) {
std::string key = Config::getConfigKey(application, gestureType, numFingers,
gestureDirection);
gestureDirection, gestureAxis);
this->config[key] = std::make_pair(actionType, actionSettings);
}

bool Config::hasGestureConfig(const std::string &application,
GestureType gestureType, int numFingers,
GestureDirection gestureDirection) const {
std::string key = Config::getConfigKey(
application, gestureType, std::to_string(numFingers), gestureDirection);
GestureDirection gestureDirection,
GestureAxis gestureAxis) const {
std::string key = Config::getConfigKey(application,
gestureType, std::to_string(numFingers),
gestureDirection, gestureAxis);
return (this->config.count(key) == 1);
}

std::pair<ActionType, std::unordered_map<std::string, std::string>>
Config::getGestureConfig(const std::string &application,
GestureType gestureType, int numFingers,
GestureDirection gestureDirection) const {
std::string key = Config::getConfigKey(
application, gestureType, std::to_string(numFingers), gestureDirection);
GestureDirection gestureDirection,
GestureAxis gestureAxis) const {
std::string key = Config::getConfigKey(application,
gestureType, std::to_string(numFingers),
gestureDirection, gestureAxis);
return this->config.at(key);
}

Expand All @@ -78,9 +82,12 @@ void Config::loadDefaultGlobalSettings() {
std::string Config::getConfigKey(const std::string &application,
GestureType gestureType,
const std::string &numFingers,
GestureDirection gestureDirection) {
GestureDirection gestureDirection,
GestureAxis gestureAxis) {
auto gestureTypeInt = static_cast<int>(gestureType);
auto gestureDirectionInt = static_cast<int>(gestureDirection);
auto gestureAxisInt = static_cast<int>(gestureAxis);
return toLower(application) + "_" + std::to_string(gestureTypeInt) + "_" +
numFingers + "_" + std::to_string(gestureDirectionInt);
numFingers + "_" + std::to_string(gestureDirectionInt) + "_" +
std::to_string(gestureAxisInt);
}
13 changes: 8 additions & 5 deletions src/config/config.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <utility>

#include "actions/action-type.h"
#include "gesture/gesture-axis.h"
#include "gesture/gesture-direction.h"
#include "gesture/gesture-type.h"

Expand Down Expand Up @@ -66,22 +67,23 @@ class Config {
void saveGestureConfig(
const std::string &application, GestureType gestureType,
const std::string &numFingers, GestureDirection gestureDirection,
ActionType actionType,
GestureAxis gestureAxis, ActionType actionType,
const std::unordered_map<std::string, std::string> &actionSettings);

/**
* @returns If there is an action configured for the gesture.
*/
bool hasGestureConfig(const std::string &application, GestureType gestureType,
int numFingers,
GestureDirection gestureDirection) const;
int numFingers, GestureDirection gestureDirection,
GestureAxis gestureAxis) const;

/**
* @returns The action configured for the gesture.
*/
std::pair<ActionType, std::unordered_map<std::string, std::string>>
getGestureConfig(const std::string &application, GestureType gestureType,
int numFingers, GestureDirection gestureDirection) const;
int numFingers, GestureDirection gestureDirection,
GestureAxis gestureAxis) const;

private:
/**
Expand Down Expand Up @@ -113,7 +115,8 @@ class Config {
static std::string getConfigKey(const std::string &application,
GestureType gestureType,
const std::string &numFingers,
GestureDirection gestureDirection);
GestureDirection gestureDirection,
GestureAxis gestureAxis);
};

#endif // CONFIG_CONFIG_H_
5 changes: 3 additions & 2 deletions src/config/xml-config-loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void XmlConfigLoader::parseApplicationXmlNodes(const pugi::xml_node &rootNode) {
const std::string gestureType = gestureNode.attribute("type").value();
const std::string fingers = gestureNode.attribute("fingers").value();
const std::string direction = gestureNode.attribute("direction").value();
const std::string axis = gestureNode.attribute("axis").value();

pugi::xml_node actionNode = gestureNode.child("action");
const std::string actionType = actionNode.attribute("type").value();
Expand All @@ -116,8 +117,8 @@ void XmlConfigLoader::parseApplicationXmlNodes(const pugi::xml_node &rootNode) {
for (const std::string &application : applications) {
this->config->saveGestureConfig(
trim(application), gestureTypeFromStr(gestureType), fingers,
gestureDirectionFromStr(direction), actionTypeFromStr(actionType),
actionSettings);
gestureDirectionFromStr(direction), gestureAxisFromStr(axis),
actionTypeFromStr(actionType), actionSettings);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/daemon/daemon-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,16 @@ std::unique_ptr<Gesture> DaemonClient::makeGestureFromSignalParams(
GVariant *signalParameters) {
GestureType type = GestureType::NOT_SUPPORTED;
GestureDirection direction = GestureDirection::UNKNOWN;
GestureAxis axis = GestureAxis::UNKNOWN;
double percentage = -1;
int fingers = -1;
DeviceType deviceType = DeviceType::UNKNOWN;
uint64_t elapsedTime = -1;

g_variant_get(signalParameters, // NOLINT
"(uudiut)", &type, &direction, &percentage, &fingers,
"(uuudiut)", &type, &direction, &axis, &percentage, &fingers,
&deviceType, &elapsedTime);

return std::make_unique<Gesture>(type, direction, percentage, fingers,
return std::make_unique<Gesture>(type, direction, axis, percentage, fingers,
deviceType, elapsedTime);
}
3 changes: 2 additions & 1 deletion src/daemon/daemon-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ void DaemonServer::send(const std::string &signalName,

// Copy every gesture field into the signal parameters for serialization
GVariant *signalParams =
g_variant_new("(uudiut)", // NOLINT
g_variant_new("(uuudiut)", // NOLINT
static_cast<int>(gesture->type()), // u
static_cast<int>(gesture->direction()), // u
static_cast<int>(gesture->axis()), // u
gesture->percentage(), // d
gesture->fingers(), // i
static_cast<int>(gesture->performedOnDeviceType()), // u
Expand Down
3 changes: 3 additions & 0 deletions src/daemon/dbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ constexpr auto DBUS_INTROSPECTION_XML =
" <signal name='OnGestureBegin'>"
" <arg name='gesture_type' type='u' />"
" <arg name='gesture_direction' type='u' />"
" <arg name='gesture_axis' type='u' />"
" <arg name='percentage' type='d' />"
" <arg name='fingers' type='i' />"
" <arg name='performed_on_device_type' type='u' />"
Expand All @@ -46,6 +47,7 @@ constexpr auto DBUS_INTROSPECTION_XML =
" <signal name='OnGestureUpdate'>"
" <arg name='gesture_type' type='u' />"
" <arg name='gesture_direction' type='u' />"
" <arg name='gesture_axis' type='u' />"
" <arg name='percentage' type='d' />"
" <arg name='fingers' type='i' />"
" <arg name='performed_on_device_type' type='u' />"
Expand All @@ -54,6 +56,7 @@ constexpr auto DBUS_INTROSPECTION_XML =
" <signal name='OnGestureEnd'>"
" <arg name='gesture_type' type='u' />"
" <arg name='gesture_direction' type='u' />"
" <arg name='gesture_axis' type='u' />"
" <arg name='percentage' type='d' />"
" <arg name='fingers' type='i' />"
" <arg name='performed_on_device_type' type='u' />"
Expand Down
15 changes: 12 additions & 3 deletions src/gesture-controller/gesture-controller.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "actions/action-type.h"
#include "actions/action.h"
#include "config/config.h"
#include "gesture/gesture-axis.h"
#include "gesture/gesture-direction.h"
#include "gesture/gesture-type.h"
#include "gesture/gesture.h"
Expand All @@ -36,19 +37,26 @@ GestureController::GestureController(const Config &config,
: config(config), windowSystem(windowSystem) {}

void GestureController::onGestureBegin(std::unique_ptr<Gesture> gesture) {
// Deduce axis from direction when possible
if (gesture->axis() == GestureAxis::UNKNOWN) {
gesture->setAxis(gestureAxisFromDirection(gesture->direction()));
}

tlg::debug << "Gesture begin detected" << std::endl;
tlg::debug << "\tGesture information:" << std::endl;
tlg::debug << "\t\tFingers: " << gesture->fingers() << std::endl;
tlg::debug << "\t\tType: " << gestureTypeToStr(gesture->type()) << std::endl;
tlg::debug << "\t\tDirection: " << gestureDirectionToStr(gesture->direction())
<< std::endl;
tlg::debug << "\t\tAxis: " << gestureAxisToStr(gesture->axis()) << std::endl;

this->rotatedDirection = this->windowSystem.calculateRotation(
gesture->type(), gesture->performedOnDeviceType(), gesture->direction());
if (this->rotatedDirection != gesture->direction()) {
tlg::debug << "\t\tDirection after rotation: "
<< gestureDirectionToStr(this->rotatedDirection) << std::endl;
gesture->setDirection(this->rotatedDirection);
gesture->setAxis(gestureAxisFromDirection(this->rotatedDirection));
}

this->window = this->windowSystem.getWindowUnderCursor();
Expand Down Expand Up @@ -102,18 +110,19 @@ std::unique_ptr<Action> GestureController::getActionForGesture(
const GestureType gestureType = gesture.type();
int fingers = gesture.fingers();
const GestureDirection direction = gesture.direction();
const GestureAxis axis = gesture.axis();

// First check if there is an specific application gesture
application = this->windowSystem.getWindowClassName(window);
tlg::debug << "\tGesture performed on app: " << application << std::endl;
hasAction = this->config.hasGestureConfig(application, gestureType, fingers,
direction);
direction, axis);

// If no gesture was configured, check the global gestures
if (!hasAction) {
application = "All";
hasAction = this->config.hasGestureConfig(application, gestureType, fingers,
direction);
direction, axis);
}

if (!hasAction) {
Expand All @@ -124,7 +133,7 @@ std::unique_ptr<Action> GestureController::getActionForGesture(
tlg::debug << "\tAction configured for this gesture" << std::endl;

auto pair = this->config.getGestureConfig(application, gestureType, fingers,
direction);
direction, axis);
ActionType actionType = pair.first;
std::unordered_map<std::string, std::string> actionSettings = pair.second;

Expand Down
5 changes: 3 additions & 2 deletions src/gesture-controller/gesture-controller.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ class GestureController : public GestureControllerDelegate {
bool executeAction = false;

/**
* If the screen is rotated, this values is set on gesture begin to match the
* screen rotation.
* If the screen is rotated, these values are set on gesture begin to match
* the screen rotation.
*/
GestureDirection rotatedDirection = GestureDirection::UNKNOWN;
GestureAxis rotatedAxis = GestureAxis::UNKNOWN;

/**
* @returns The action associated to a gesture or nullptr.
Expand Down
6 changes: 6 additions & 0 deletions src/gesture-gatherer/libinput-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ GestureDirection LininputHandler::calculateSwipeDirection(double deltaX,
return (deltaY > 0) ? GestureDirection::DOWN : GestureDirection::UP;
}

GestureAxis LininputHandler::calculatePinchAxis(double deltaX,
double deltaY) {
return (std::abs(deltaX) > std::abs(deltaY)) ?
GestureAxis::HORIZONTAL : GestureAxis::VERTICAL;
}

double LininputHandler::calculateSwipeAnimationPercentage(
const LibinputDeviceInfo &info, GestureDirection direction, double deltaX,
double deltaY) {
Expand Down
5 changes: 5 additions & 0 deletions src/gesture-gatherer/libinput-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class LininputHandler {
*/
static GestureDirection calculateSwipeDirection(double deltaX, double deltaY);

/**
* @returns The primary axis of a pinch gesture.
*/
static GestureAxis calculatePinchAxis(double deltaX, double deltaY);

/**
* @returns The percentage (between 0 and 100) of the gesture animation.
*/
Expand Down
32 changes: 25 additions & 7 deletions src/gesture-gatherer/libinput-pinch-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@

#include "gesture/device-type.h"

void LininputPinchHandler::handlePinchBegin(struct libinput_event * /*event*/) {
void LininputPinchHandler::handlePinchBegin(struct libinput_event *event) {
this->state.reset();

struct libinput_event_gesture *gestureEvent =
libinput_event_get_gesture_event(event);
if (
std::abs(libinput_event_gesture_get_dx(gestureEvent))
> std::abs(libinput_event_gesture_get_dy(gestureEvent))
) {
this->state.axis = GestureAxis::HORIZONTAL;
} else {
this->state.axis = GestureAxis::VERTICAL;
}
}

void LininputPinchHandler::handlePinchUpdate(struct libinput_event *event) {
Expand All @@ -36,14 +47,19 @@ void LininputPinchHandler::handlePinchUpdate(struct libinput_event *event) {
this->state.startTimestamp = LininputHandler::getTimestamp();
this->state.direction =
(this->state.delta > 1) ? GestureDirection::OUT : GestureDirection::IN;
this->state.axis = (std::abs(libinput_event_gesture_get_dx(gestureEvent)) >
std::abs(libinput_event_gesture_get_dy(gestureEvent)))
? GestureAxis::HORIZONTAL
: GestureAxis::VERTICAL;
this->state.percentage = LininputHandler::calculatePinchAnimationPercentage(
this->state.direction, this->state.delta);
this->state.fingers = libinput_event_gesture_get_finger_count(gestureEvent);
uint64_t elapsedTime = 0;

auto gesture = std::make_unique<Gesture>(
GestureType::PINCH, this->state.direction, this->state.percentage,
this->state.fingers, DeviceType::TOUCHPAD, elapsedTime);
GestureType::PINCH, this->state.direction, this->state.axis,
this->state.percentage, this->state.fingers, DeviceType::TOUCHPAD,
elapsedTime);
this->gestureController->onGestureBegin(std::move(gesture));
} else {
this->state.percentage = LininputHandler::calculatePinchAnimationPercentage(
Expand All @@ -52,8 +68,9 @@ void LininputPinchHandler::handlePinchUpdate(struct libinput_event *event) {
LininputHandler::calculateElapsedTime(this->state.startTimestamp);

auto gesture = std::make_unique<Gesture>(
GestureType::PINCH, this->state.direction, this->state.percentage,
this->state.fingers, DeviceType::TOUCHPAD, elapsedTime);
GestureType::PINCH, this->state.direction, this->state.axis,
this->state.percentage, this->state.fingers, DeviceType::TOUCHPAD,
elapsedTime);
this->gestureController->onGestureUpdate(std::move(gesture));
}
}
Expand All @@ -69,8 +86,9 @@ void LininputPinchHandler::handlePinchEnd(struct libinput_event *event) {
LininputHandler::calculateElapsedTime(this->state.startTimestamp);

auto gesture = std::make_unique<Gesture>(
GestureType::PINCH, this->state.direction, this->state.percentage,
this->state.fingers, DeviceType::TOUCHPAD, elapsedTime);
GestureType::PINCH, this->state.direction, this->state.axis,
this->state.percentage, this->state.fingers, DeviceType::TOUCHPAD,
elapsedTime);
this->gestureController->onGestureEnd(std::move(gesture));
}

Expand Down
2 changes: 2 additions & 0 deletions src/gesture-gatherer/libinput-pinch-handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct LibinputPinchState {
uint64_t startTimestamp = 0;
double delta = 1;
GestureDirection direction = GestureDirection::UNKNOWN;
GestureAxis axis = GestureAxis::UNKNOWN;
double percentage = 0;
int fingers = 0;

Expand All @@ -40,6 +41,7 @@ struct LibinputPinchState {
startTimestamp = 0;
delta = 1;
direction = GestureDirection::UNKNOWN;
axis = GestureAxis::UNKNOWN;
percentage = 0;
fingers = 0;
}
Expand Down
15 changes: 9 additions & 6 deletions src/gesture-gatherer/libinput-touch-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ void LibinputTouchHandler::handleTouchMotion(struct libinput_event *event) {
this->state.startFingers = this->state.currentFingers;
this->state.startTimestamp = LininputHandler::getTimestamp();
this->state.type = this->getGestureType();
this->state.direction =
(this->state.type == GestureType::SWIPE)
? LininputHandler::calculateSwipeDirection(deltaX, deltaY)
: this->calculatePinchDirection();
if (this->state.type == GestureType::SWIPE) {
this->state.direction = LininputHandler::calculateSwipeDirection(deltaX, deltaY);
} else {
this->state.direction = this->calculatePinchDirection();
this->state.axis = this->calculatePinchAxis(deltaX, deltaY);
}

// Once the direction is calculated, save the currentX/Y as startX/Y so
// the startThreshold is not included in the percentage calculations
Expand All @@ -136,8 +138,9 @@ void LibinputTouchHandler::handleTouchMotion(struct libinput_event *event) {
this->state.startY = this->state.currentY;

auto gesture = std::make_unique<Gesture>(
this->state.type, this->state.direction, percentage,
this->state.startFingers, DeviceType::TOUCHSCREEN, 0);
this->state.type, this->state.direction, this->state.axis,
percentage, this->state.startFingers, DeviceType::TOUCHSCREEN,
0);
this->gestureController->onGestureBegin(std::move(gesture));
}
} else {
Expand Down
Loading

0 comments on commit 2cfdecd

Please sign in to comment.