diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2014-07-14 16:19:33 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2014-07-21 08:56:12 +1000 |
commit | 876a8959ab1abe8a231860703320bb5fcc43c603 (patch) | |
tree | 58efc6fa29f4b8bda38818d46240106fc11c60b0 | |
parent | e1484cb7f820ae8284140d3a5ece43b564f19b45 (diff) | |
download | libinput-876a8959ab1abe8a231860703320bb5fcc43c603.tar.gz |
filter: move get_direction into shared header
Makes it possible to use from the touchpad code.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Hans de Goede <hdegoede@redhat.com>
-rw-r--r-- | src/filter.c | 60 | ||||
-rw-r--r-- | src/libinput-util.h | 58 |
2 files changed, 60 insertions, 58 deletions
diff --git a/src/filter.c b/src/filter.c index e1fb2a96..d65adfb7 100644 --- a/src/filter.c +++ b/src/filter.c @@ -29,6 +29,7 @@ #include <math.h> #include "filter.h" +#include "libinput-util.h" void filter_dispatch(struct motion_filter *filter, @@ -84,63 +85,6 @@ struct pointer_accelerator { int cur_tracker; }; -enum directions { - N = 1 << 0, - NE = 1 << 1, - E = 1 << 2, - SE = 1 << 3, - S = 1 << 4, - SW = 1 << 5, - W = 1 << 6, - NW = 1 << 7, - UNDEFINED_DIRECTION = 0xff -}; - -static int -get_direction(int dx, int dy) -{ - int dir = UNDEFINED_DIRECTION; - int d1, d2; - double r; - - if (abs(dx) < 2 && abs(dy) < 2) { - if (dx > 0 && dy > 0) - dir = S | SE | E; - else if (dx > 0 && dy < 0) - dir = N | NE | E; - else if (dx < 0 && dy > 0) - dir = S | SW | W; - else if (dx < 0 && dy < 0) - dir = N | NW | W; - else if (dx > 0) - dir = NE | E | SE; - else if (dx < 0) - dir = NW | W | SW; - else if (dy > 0) - dir = SE | S | SW; - else if (dy < 0) - dir = NE | N | NW; - } else { - /* Calculate r within the interval [0 to 8) - * - * r = [0 .. 2π] where 0 is North - * d_f = r / 2π ([0 .. 1)) - * d_8 = 8 * d_f - */ - r = atan2(dy, dx); - r = fmod(r + 2.5*M_PI, 2*M_PI); - r *= 4*M_1_PI; - - /* Mark one or two close enough octants */ - d1 = (int)(r + 0.9) % 8; - d2 = (int)(r + 0.1) % 8; - - dir = (1 << d1) | (1 << d2); - } - - return dir; -} - static void feed_trackers(struct pointer_accelerator *accel, double dx, double dy, @@ -160,7 +104,7 @@ feed_trackers(struct pointer_accelerator *accel, trackers[current].dx = 0.0; trackers[current].dy = 0.0; trackers[current].time = time; - trackers[current].dir = get_direction(dx, dy); + trackers[current].dir = vector_get_direction(dx, dy); } static struct pointer_tracker * diff --git a/src/libinput-util.h b/src/libinput-util.h index 2558a3d9..c0235ef3 100644 --- a/src/libinput-util.h +++ b/src/libinput-util.h @@ -24,6 +24,7 @@ #define LIBINPUT_UTIL_H #include <unistd.h> +#include <math.h> #include "libinput.h" @@ -92,4 +93,61 @@ msleep(unsigned int ms) usleep(ms * 1000); } +enum directions { + N = 1 << 0, + NE = 1 << 1, + E = 1 << 2, + SE = 1 << 3, + S = 1 << 4, + SW = 1 << 5, + W = 1 << 6, + NW = 1 << 7, + UNDEFINED_DIRECTION = 0xff +}; + +static inline int +vector_get_direction(int dx, int dy) +{ + int dir = UNDEFINED_DIRECTION; + int d1, d2; + double r; + + if (abs(dx) < 2 && abs(dy) < 2) { + if (dx > 0 && dy > 0) + dir = S | SE | E; + else if (dx > 0 && dy < 0) + dir = N | NE | E; + else if (dx < 0 && dy > 0) + dir = S | SW | W; + else if (dx < 0 && dy < 0) + dir = N | NW | W; + else if (dx > 0) + dir = NE | E | SE; + else if (dx < 0) + dir = NW | W | SW; + else if (dy > 0) + dir = SE | S | SW; + else if (dy < 0) + dir = NE | N | NW; + } else { + /* Calculate r within the interval [0 to 8) + * + * r = [0 .. 2π] where 0 is North + * d_f = r / 2π ([0 .. 1)) + * d_8 = 8 * d_f + */ + r = atan2(dy, dx); + r = fmod(r + 2.5*M_PI, 2*M_PI); + r *= 4*M_1_PI; + + /* Mark one or two close enough octants */ + d1 = (int)(r + 0.9) % 8; + d2 = (int)(r + 0.1) % 8; + + dir = (1 << d1) | (1 << d2); + } + + return dir; +} + #endif /* LIBINPUT_UTIL_H */ |