Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
orientation: Correct handling of orientation on threshold
Browse files Browse the repository at this point in the history
The old code gave a priority to portrait orientation, so that if
rotation reached the portrait threshold, it switched straight away.

This is problematic if the device you're dealing with isn't a mostly
portrait phone, but a tablet that can be used in both orientations
equally.

Landscape thresholds and portrait thresholds can (and they do) overlap.
In the overlapping area we should try to keep the previous orientation
until that threshold has passed.

[Bastien Nocera: Added test change]
  • Loading branch information
FooBarrior authored and hadess committed Sep 4, 2019
1 parent 1400548 commit bb5fbcc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 15 additions & 11 deletions src/orientation.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,42 @@ orientation_calc (OrientationUp prev,
int in_x, int in_y, int in_z,
gdouble scale)
{
int rotation;
OrientationUp ret = prev;
int x, y, z;
int portrait_rotation;
int landscape_rotation;

/* this code expects 1G ~= 256 */
x = SCALE(in_x);
y = SCALE(in_y);
z = SCALE(in_z);

/* Portrait check */
rotation = round(atan((double) x / sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
portrait_rotation = round(atan2(x, sqrt(y * y + z * z)) * RADIANS_TO_DEGREES);
landscape_rotation = round(atan2(y, sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);

/* Don't change orientation if we are on the common border of two thresholds */
if (abs(portrait_rotation) > THRESHOLD_PORTRAIT && abs(landscape_rotation) > THRESHOLD_LANDSCAPE)
return prev;

if (abs(rotation) > THRESHOLD_PORTRAIT) {
ret = (rotation > 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;
/* Portrait check */
if (abs(portrait_rotation) > THRESHOLD_PORTRAIT) {
ret = (portrait_rotation > 0) ? ORIENTATION_LEFT_UP : ORIENTATION_RIGHT_UP;

/* Some threshold to switching between portrait modes */
if (prev == ORIENTATION_LEFT_UP || prev == ORIENTATION_RIGHT_UP) {
if (abs(rotation) < SAME_AXIS_LIMIT) {
if (abs(portrait_rotation) < SAME_AXIS_LIMIT) {
ret = prev;
}
}

} else {
/* Landscape check */
rotation = round(atan((double) y / sqrt(x * x + z * z)) * RADIANS_TO_DEGREES);

if (abs(rotation) > THRESHOLD_LANDSCAPE) {
ret = (rotation > 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;
if (abs(landscape_rotation) > THRESHOLD_LANDSCAPE) {
ret = (landscape_rotation > 0) ? ORIENTATION_BOTTOM_UP : ORIENTATION_NORMAL;

/* Some threshold to switching between landscape modes */
if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
if (abs(rotation) < SAME_AXIS_LIMIT) {
if (abs(landscape_rotation) < SAME_AXIS_LIMIT) {
ret = prev;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test-orientation.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ test_orientation_threshold (void)
OrientationUp expected;
} orientations[] = {
{ 0, -ONEG, 0, ORIENTATION_NORMAL },
{ 183, -ONEG, 0, ORIENTATION_LEFT_UP },
{ 183, -ONEG, 0, ORIENTATION_NORMAL },
{ 176, -ONEG, 0, ORIENTATION_NORMAL },
{ 183, -ONEG, 0, ORIENTATION_LEFT_UP },
{ 183, -ONEG, 0, ORIENTATION_NORMAL },
};
guint i, num_failures;
OrientationUp prev;
Expand Down

0 comments on commit bb5fbcc

Please sign in to comment.