[gnome-settings-daemon] common: Add (non-working) orientation calculation
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-settings-daemon] common: Add (non-working) orientation calculation
- Date: Wed, 1 Jun 2011 17:38:07 +0000 (UTC)
commit bd2ce171ec16db79bebfd742bcd278ebccdf05f0
Author: Bastien Nocera <hadess hadess net>
Date: Wed Jun 1 18:37:17 2011 +0100
common: Add (non-working) orientation calculation
The code currently seems to get the orientations the wrong
way around.
plugins/orientation/Makefile.am | 59 ++++++++++++++
plugins/orientation/gsd-orientation-calc.c | 80 ++++++++++++++++++++
plugins/orientation/gsd-orientation-calc.h | 44 +++++++++++
plugins/orientation/test-orientation-calculation.c | 67 ++++++++++++++++
4 files changed, 250 insertions(+), 0 deletions(-)
---
diff --git a/plugins/orientation/Makefile.am b/plugins/orientation/Makefile.am
new file mode 100644
index 0000000..b79ecc0
--- /dev/null
+++ b/plugins/orientation/Makefile.am
@@ -0,0 +1,59 @@
+plugin_name = orientation
+
+noinst_PROGRAMS = test-orientation-calculation
+
+test_orientation_calculation_SOURCES = \
+ gsd-orientation-calc.c \
+ gsd-orientation-calc.h \
+ test-orientation-calculation.c
+
+test_orientation_calculation_CFLAGS = \
+ $(PLUGIN_CFLAGS) \
+ $(SETTINGS_PLUGIN_CFLAGS) \
+ $(AM_CFLAGS)
+
+test_orientation_calculation_LDADD = \
+ $(SETTINGS_PLUGIN_LIBS)
+
+plugin_LTLIBRARIES = liborientation.la
+
+liborientation_la_SOURCES = \
+ gsd-orientation-plugin.h \
+ gsd-orientation-plugin.c \
+ gsd-orientation-manager.h \
+ gsd-orientation-manager.c \
+ gsd-orientation-calc.c \
+ gsd-orientation-calc.h
+
+liborientation_la_CPPFLAGS = \
+ -I$(top_srcdir)/gnome-settings-daemon \
+ -I$(top_srcdir)/plugins/common/ \
+ -I$(top_srcdir)/data/ \
+ -DGNOME_SETTINGS_LOCALEDIR=\""$(datadir)/locale"\" \
+ -DLIBEXECDIR=\""$(libexecdir)"\" \
+ $(AM_CPPFLAGS)
+
+liborientation_la_CFLAGS = \
+ $(PLUGIN_CFLAGS) \
+ $(GUDEV_CFLAGS) \
+ $(SETTINGS_PLUGIN_CFLAGS) \
+ $(AM_CFLAGS)
+
+liborientation_la_LDFLAGS = \
+ $(GSD_PLUGIN_LDFLAGS)
+
+liborientation_la_LIBADD = \
+ $(top_builddir)/plugins/common/libcommon.la \
+ $(SETTINGS_PLUGIN_LIBS) \
+ $(X11_LIBS) \
+ $(XINPUT_LIBS)
+
+plugin_in_files = orientation.gnome-settings-plugin.in
+
+plugin_DATA = $(plugin_in_files:.gnome-settings-plugin.in=.gnome-settings-plugin)
+
+EXTRA_DIST = $(plugin_in_files)
+CLEANFILES = $(plugin_DATA)
+DISTCLEANFILES = $(plugin_DATA)
+
+ GSD_INTLTOOL_PLUGIN_RULE@
diff --git a/plugins/orientation/gsd-orientation-calc.c b/plugins/orientation/gsd-orientation-calc.c
new file mode 100644
index 0000000..8bdec79
--- /dev/null
+++ b/plugins/orientation/gsd-orientation-calc.c
@@ -0,0 +1,80 @@
+/**
+ @file orientationinterpreter.cpp
+ @brief OrientationInterpreter
+
+ <p>
+ Copyright (C) 2009-2010 Nokia Corporation
+ Copyright (C) 2011 Red Hat, Inc.
+
+ @author �stün Ergenoglu <ext-ustun ergenoglu nokia com>
+ @author Timo Rongas <ext-timo 2 rongas nokia com>
+ @author Lihan Guo <lihan guo digia com>
+ @author Bastien Nocera <hadess hadess net> (C port)
+
+ This file is part of Sensord.
+
+ Sensord is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License
+ version 2.1 as published by the Free Software Foundation.
+
+ Sensord 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with Sensord. If not, see <http://www.gnu.org/licenses/>.
+ </p>
+ */
+#include "config.h"
+
+#include <math.h>
+#include <stdlib.h>
+
+#include "gsd-orientation-calc.h"
+
+#define DEFAULT_THRESHOLD 250
+#define RADIANS_TO_DEGREES 180.0/M_PI
+#define SAME_AXIS_LIMIT 5
+
+#define THRESHOLD_LANDSCAPE 25
+#define THRESHOLD_PORTRAIT 20
+
+OrientationUp gsd_orientation_calc (OrientationUp prev,
+ int x, int y, int z)
+{
+ int rotation;
+ OrientationUp ret = ORIENTATION_UNDEFINED;
+
+ /* Portrait check */
+ rotation = round (atan ((double) x / sqrt (y * y + z * z)) * RADIANS_TO_DEGREES);
+
+ if (abs (rotation) > THRESHOLD_PORTRAIT) {
+ ret = (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) {
+ 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;
+
+ /* Some threshold to switching between landscape modes */
+ if (prev == ORIENTATION_BOTTOM_UP || prev == ORIENTATION_NORMAL) {
+ if (abs (rotation) < SAME_AXIS_LIMIT) {
+ ret = prev;
+ }
+ }
+ }
+ }
+
+ return ret;
+}
+
diff --git a/plugins/orientation/gsd-orientation-calc.h b/plugins/orientation/gsd-orientation-calc.h
new file mode 100644
index 0000000..ef7446e
--- /dev/null
+++ b/plugins/orientation/gsd-orientation-calc.h
@@ -0,0 +1,44 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 William Jon McCann <mccann jhu edu>
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef __GSD_ORIENTATION_CALC_H
+#define __GSD_ORIENTATION_CALC_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef enum {
+ ORIENTATION_UNDEFINED,
+ ORIENTATION_NORMAL,
+ ORIENTATION_BOTTOM_UP,
+ ORIENTATION_LEFT_UP,
+ ORIENTATION_RIGHT_UP
+} OrientationUp;
+
+#define ORIENTATION_UP_UP ORIENTATION_NORMAL
+
+OrientationUp gsd_orientation_calc (OrientationUp prev,
+ int x, int y, int z);
+
+G_END_DECLS
+
+#endif /* __GSD_ORIENTATION_CALC_H */
diff --git a/plugins/orientation/test-orientation-calculation.c b/plugins/orientation/test-orientation-calculation.c
new file mode 100644
index 0000000..0c46d64
--- /dev/null
+++ b/plugins/orientation/test-orientation-calculation.c
@@ -0,0 +1,67 @@
+/* Adapted from
+ * http://gitorious.org/sensorfw/sensorfw/blobs/master/tests/contextfw/orientation/testorientation.py
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "gsd-orientation-calc.h"
+
+struct {
+ int x;
+ int y;
+ int z;
+ OrientationUp o;
+} tests[] = {
+ { 60, 960, 18, ORIENTATION_NORMAL },
+ { 936, 162, 180, ORIENTATION_RIGHT_UP },
+ { 72, -990, -162, ORIENTATION_BOTTOM_UP },
+ { -954, -90, -36, ORIENTATION_LEFT_UP }
+};
+
+static const char *
+orientation_to_string (OrientationUp o)
+{
+ switch (o) {
+ case ORIENTATION_UNDEFINED:
+ return "undefined";
+ case ORIENTATION_NORMAL:
+ return "normal";
+ case ORIENTATION_BOTTOM_UP:
+ return "bottom up";
+ case ORIENTATION_LEFT_UP:
+ return "left up";
+ case ORIENTATION_RIGHT_UP:
+ return "right up";
+ default:
+ g_assert_not_reached ();
+ }
+}
+
+static void
+test_no_prev (void)
+{
+ guint i;
+
+ for (i = 0 ; i < G_N_ELEMENTS (tests); i++) {
+ OrientationUp calculated;
+ const char *got, *expected;
+
+ calculated = gsd_orientation_calc (ORIENTATION_UNDEFINED, tests[i].x, tests[i].y, tests[i].z);
+ got = orientation_to_string (calculated);
+ expected = orientation_to_string (tests[i].o);
+
+ g_assert_cmpstr (got, ==, expected);
+ }
+}
+
+int main (int argc, char **argv)
+{
+ g_type_init ();
+ g_test_init (&argc, &argv, NULL);
+ g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");
+
+ g_test_add_func ("/orientation/no-prev", test_no_prev);
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]