summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny Baumann <dannybaumann@web.de>2008-01-11 07:58:26 +0100
committerDanny Baumann <dannybaumann@web.de>2008-01-11 07:58:26 +0100
commitd345e4d29a30dc7c76ce6cca2e0d70d8d3197a35 (patch)
tree986a84eabdd9871782650e5d907e6a485293c001
parent8206aa863e5c98f7cd00b4a460c9e72e3173e746 (diff)
downloadmousegestures-d345e4d29a30dc7c76ce6cca2e0d70d8d3197a35.tar.gz
mousegestures-d345e4d29a30dc7c76ce6cca2e0d70d8d3197a35.tar.bz2
Clean up gesture detection code.
-rw-r--r--mousegestures.c136
1 files changed, 60 insertions, 76 deletions
diff --git a/mousegestures.c b/mousegestures.c
index 5d08af3..11a8a42 100644
--- a/mousegestures.c
+++ b/mousegestures.c
@@ -26,8 +26,8 @@
#define MOUSEGESTURES_DISPLAY_OPTION_ACTION 3
#define MOUSEGESTURES_DISPLAY_OPTION_NUM 4
-#define MOUSEGESTURE_MAX_VARIANCE 30
#define MOUSEGESTURE_MIN_DELTA 80
+#define MOUSEGESTURE_MAX_VARIANCE (MOUSEGESTURE_MIN_DELTA / 2)
#define MOUSEGESTURES_MAX_STROKE 32
#define MOUSEGESTURES_MAX_GESTURES 128
@@ -239,98 +239,82 @@ static void
mousegesturesHandleEvent (CompDisplay *d,
XEvent *event)
{
- char currentStroke = '\0';
- float diff = 0.0;
-
MOUSEGESTURES_DISPLAY (d);
- if (!md->grabIndex)
- {
- UNWRAP (md, d, handleEvent);
- (*d->handleEvent) (d, event);
- WRAP (md, d, handleEvent, mousegesturesHandleEvent);
- return;
- }
+ switch (event->type) {
+ case MotionNotify:
+ if (md->grabIndex)
+ {
+ char currentStroke = '\0';
+ int deltaX = 0, deltaY = 0;
- switch (event->type)
- {
- case MotionNotify:
- md->deltaX += (lastPointerX - pointerX);
- md->deltaY += (lastPointerY - pointerY);
+ md->deltaX += (pointerX - lastPointerX);
+ md->deltaY += (pointerY - lastPointerY);
- if (abs (md->deltaY) > MOUSEGESTURE_MAX_VARIANCE)
- diff = (float) abs (md->deltaX) / (float) abs (md->deltaY);
- else if (abs (md->deltaX) > MOUSEGESTURE_MAX_VARIANCE)
- diff = (float) abs (md->deltaY) / (float) abs (md->deltaX);
+ if (abs (md->deltaX) > MOUSEGESTURE_MIN_DELTA)
+ {
+ deltaX = md->deltaX;
+ if (abs (md->deltaY) > MOUSEGESTURE_MAX_VARIANCE)
+ deltaY = md->deltaY;
+ }
+ else if (abs (md->deltaY) > MOUSEGESTURE_MIN_DELTA)
+ {
+ deltaY = md->deltaY;
+ if (abs (md->deltaX) > MOUSEGESTURE_MAX_VARIANCE)
+ deltaX = md->deltaX;
+ }
- /* dx and dy are roughly the same so they are going diagonally */
- if (diff > 0.1 && diff < 1.9)
+ /* handle horizontal and vertical cases first */
+ if (!deltaY && deltaX < 0)
+ currentStroke = M_LEFT;
+ else if (!deltaY && deltaX > 0)
+ currentStroke = M_RIGHT;
+ else if (!deltaX && deltaY < 0)
+ currentStroke = M_UP;
+ else if (!deltaX && deltaY > 0)
+ currentStroke = M_DOWN;
+ else if (deltaX && deltaY)
{
- if (md->deltaY > 0 &&
- (abs (md->deltaY) > MOUSEGESTURE_MAX_VARIANCE))
+ /* deltaX and deltaY != 0, thus diagonal */
+ if (deltaX < 0)
{
- /* UP */
- if (abs(md->deltaX) > MOUSEGESTURE_MAX_VARIANCE)
- {
- if (md->deltaX < 0)
- currentStroke = M_9;
- else if (md->deltaX > 0)
- currentStroke = M_7;
- }
+ if (deltaY < 0)
+ currentStroke = M_7;
+ else
+ currentStroke = M_1;
}
- else if (md->deltaY < 0 &&
- (abs (md->deltaY) > MOUSEGESTURE_MAX_VARIANCE))
+ else
{
- /* DOWN */
- if (abs(md->deltaX) > MOUSEGESTURE_MAX_VARIANCE)
- {
- if (md->deltaX < 0)
- currentStroke = M_3;
- else if (md->deltaX > 0)
- currentStroke = M_1;
- }
+ if (deltaY < 0)
+ currentStroke = M_9;
+ else
+ currentStroke = M_3;
}
}
- else if (abs (md->deltaX) > MOUSEGESTURE_MAX_VARIANCE)
+ if (currentStroke && md->currentStroke != currentStroke)
{
- if (md->deltaX < 0)
- currentStroke = M_RIGHT;
- else
- currentStroke = M_LEFT;
-
- }
- else if (abs (md->deltaY) > MOUSEGESTURE_MAX_VARIANCE)
- {
- if (md->deltaY > 0)
- currentStroke = M_UP;
- else
- currentStroke = M_DOWN;
+ md->currentStroke = currentStroke;
+ if (md->currentStroke != md->lastStroke)
+ {
+ md->lastStroke = md->currentStroke;
+ if (md->nCurrentGesture < MOUSEGESTURES_MAX_STROKE)
+ {
+ md->currentGesture[md->nCurrentGesture] =
+ md->currentStroke;
+ md->nCurrentGesture++;
+ }
+ }
+ md->deltaX = 0;
+ md->deltaY = 0;
}
- break;
- default:
- break;
- }
-
- if (currentStroke && md->currentStroke != currentStroke)
- {
- md->currentStroke = currentStroke;
- if (md->currentStroke != md->lastStroke)
- {
- md->lastStroke = md->currentStroke;
- if (md->nCurrentGesture < MOUSEGESTURES_MAX_STROKE)
+ else if (md->currentStroke && md->currentStroke == currentStroke)
{
- md->currentGesture[md->nCurrentGesture] = md->currentStroke;
- md->nCurrentGesture++;
+ md->deltaX = 0;
+ md->deltaY = 0;
}
}
- md->deltaX = 0;
- md->deltaY = 0;
- }
- else if (md->currentStroke && md->currentStroke == currentStroke)
- {
- md->deltaX = 0;
- md->deltaY = 0;
+ break;
}
UNWRAP (md, d, handleEvent);