Add orientation class to select portrait or landscape orientation
authorph5 <ph5@monster.localdomain>
Sat, 2 Jan 2010 21:10:37 +0000 (22:10 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Sat, 2 Jan 2010 23:23:27 +0000 (00:23 +0100)
Uses the MCE D-Bus API to obtain the device orientation from accellerometers
and the HAL D-Bus API to check whether the keyboard slide is opened.

Makefile.am
src/orientation.vala [new file with mode: 0644]

index 990a819..e9cabbb 100644 (file)
@@ -49,6 +49,7 @@ cinaest_SOURCES = \
         src/movie-list-window.c \
         src/movie-menu.c \
         src/movie-window.c \
+       src/orientation.c \
        src/plugin-registrar.c \
        src/poster/movie-poster-factory.c \
        src/settings-dialog.c \
@@ -67,6 +68,7 @@ cinaest_VALASOURCES = \
         src/movie-list-window.vala \
         src/movie-menu.vala \
         src/movie-window.vala \
+       src/orientation.vala \
        src/plugin-registrar.vala \
        src/poster/movie-poster-factory.vala \
        src/settings-dialog.vala \
diff --git a/src/orientation.vala b/src/orientation.vala
new file mode 100644 (file)
index 0000000..1e03ace
--- /dev/null
@@ -0,0 +1,78 @@
+/* This file is part of Cinaest.
+ *
+ * Copyright (C) 2009 Philipp Zabel
+ *
+ * Cinaest 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Cinaest 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 Cinaest. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+public class Orientation : Object {
+       public bool portrait;
+       public bool keyboard;
+       public signal void changed ();
+       private DBus.Connection conn;
+       private dynamic DBus.Object mce_request;
+       private dynamic DBus.Object mce_signal;
+       private dynamic DBus.Object hal_slide;
+       private const string MCE_SERVICE = "com.nokia.mce";
+       private const string MCE_REQUEST_PATH = "/com/nokia/mce/request";
+       private const string MCE_SIGNAL_PATH = "/com/nokia/mce/signal";
+       private const string MCE_REQUEST_IF = "com.nokia.mce.request";
+       private const string MCE_SIGNAL_IF = "com.nokia.mce.signal";
+       private const string HAL_SERVICE = "org.freedesktop.Hal";
+       private const string HAL_SLIDE_PATH = "/org/freedesktop/Hal/devices/platform_slide";
+       private const string HAL_DEVICE_IF = "org.freedesktop.Hal.Device";
+
+       construct {
+               portrait = false;
+               try {
+                       conn = DBus.Bus.get (DBus.BusType.SYSTEM);
+
+                       mce_request = conn.get_object (MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF);
+                       mce_enable_accelerometer ();
+
+                       mce_signal = conn.get_object (MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF);
+                       mce_signal.sig_device_orientation_ind += on_mce_orientation_changed;
+
+                       hal_slide = conn.get_object (HAL_SERVICE, HAL_SLIDE_PATH, HAL_DEVICE_IF);
+                       hal_slide.Condition += on_hal_slide_condition;
+                       bool closed = hal_slide.GetPropertyBoolean ("button.state.value");
+                       keyboard = !closed;
+               } catch (Error e) {
+                       stderr.printf ("Error: %s\n", e.message);
+               }
+       }
+
+       private void mce_enable_accelerometer () {
+               string orientation, stand, face;
+               int x, y, z;
+
+               mce_request.req_accelerometer_enable (out orientation, out stand, out face, out x, out y, out z);
+               on_mce_orientation_changed (mce_request, orientation, stand, face, x, y, z);
+       }
+
+       private void on_mce_orientation_changed (dynamic DBus.Object bluez, string orientation,
+                                                string stand, string face, int x, int y , int z) {
+               portrait = (orientation == "portrait" & !keyboard);
+               changed ();
+       }
+
+       private void on_hal_slide_condition (dynamic DBus.Object slide, string cond_name, string cond_details) {
+               if (cond_name == "ButtonPressed" && cond_details == "cover") {
+                       bool closed = hal_slide.GetPropertyBoolean ("button.state.value");
+                       keyboard = !closed;
+                       portrait &= closed;
+               }
+               changed ();
+       }
+}