Accelerometer support, v1



Attached is the latest patch to support autorotated viewing using the
ThinkPad accelerometer.  Included is a fix from Larry to make the
rotation a bit smoother.  It still looks slow and strange for me
compared to the rotation you see when you hit '[' and ']'.  Any takers
for this issue?

I'll also be interested to know if anyone else has gotten this running?

Nat

Index: src/Accelerometer.cs
===================================================================
RCS file: src/Accelerometer.cs
diff -N src/Accelerometer.cs
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/Accelerometer.cs	4 Apr 2006 15:18:58 -0000
@@ -0,0 +1,124 @@
+//
+// Support for the ThinkPad accelerometer.
+//
+
+using System;
+using System.IO;
+
+namespace FSpot {
+
+	public delegate void OrientationChangedHandler (object sender);
+
+	public class Accelerometer {
+
+		public static event OrientationChangedHandler OrientationChanged;
+
+		public enum Orient {
+			Normal,
+			TiltClockwise,
+			TiltCounterclockwise,
+		}
+
+		private static Orient current_orientation;
+
+		public Orient CurrentOrientation {
+			get {
+				return current_orientation;
+			}
+		}
+
+		public Accelerometer ()
+		{
+		}
+
+		public static PixbufOrientation GetViewOrientation (PixbufOrientation po)
+		{
+			if (timer == 0)
+				SetupAccelerometer ();
+				
+			if (current_orientation == Orient.TiltCounterclockwise)
+				return PixbufUtils.Rotate90 (po);
+
+			if (current_orientation == Orient.TiltClockwise)
+				return PixbufUtils.Rotate270 (po);
+
+			return po;
+		}
+		
+		static uint timer = 0;
+
+		public static void SetupAccelerometer ()
+		{
+			int x, y;
+
+			// Call once to set a baseline value.
+			// Hopefully the laptop is flat when this is
+			// called.
+			GetHDAPSCoords (out x, out y);
+
+			timer = GLib.Timeout.Add (500, new GLib.TimeoutHandler (CheckOrientation));
+		}
+
+		private static bool CheckOrientation ()
+		{
+			Orient new_orient = GetScreenOrientation ();
+
+			if (new_orient != current_orientation) {
+				current_orientation = new_orient;
+
+				if (OrientationChanged != null)
+					OrientationChanged (null);
+
+				Console.WriteLine ("Laptop orientation changed...");
+			}
+
+			return true;
+		}
+
+		public static Orient GetScreenOrientation ()
+		{
+			int x, y;
+
+			GetHDAPSCoords (out x, out y);
+
+			if (x > 100)
+				return Orient.TiltClockwise;
+
+			if (x < -100)
+				return Orient.TiltCounterclockwise;
+
+			return Orient.Normal;
+		}
+
+		static int base_x = -1000; // initial nonsense values
+		static int base_y = -1000;
+
+		private static void GetHDAPSCoords (out int x, out int y)
+		{
+			try {
+				using (Stream file = File.OpenRead ("/sys/devices/platform/hdaps/position")) {
+					StreamReader sr = new StreamReader (file);
+
+					string s = sr.ReadLine ();
+					string [] ss = s.Substring (1, s.Length - 2).Split (',');
+					x = int.Parse (ss [0]);
+					y = int.Parse (ss [1]);
+
+					if (base_x == -1000)
+						base_x = x;
+					
+					if (base_y == -1000)
+						base_y = y;
+
+					x -= base_x;
+					y -= base_y;
+
+					return;
+				}
+			} catch (Exception e) {
+				x = 0;
+				y = 0;
+			}
+		}
+	}
+}
Index: src/AsyncPixbufLoader.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/AsyncPixbufLoader.cs,v
retrieving revision 1.55
diff -u -r1.55 AsyncPixbufLoader.cs
--- src/AsyncPixbufLoader.cs	23 Mar 2006 04:38:53 -0000	1.55
+++ src/AsyncPixbufLoader.cs	4 Apr 2006 15:18:58 -0000
@@ -114,10 +114,12 @@
 			damage = Gdk.Rectangle.Zero;
 
 			ImageFile img = ImageFile.Create (uri);
-			orientation = img.Orientation;
-
+			orientation = Accelerometer.GetViewOrientation (img.Orientation);
+			
 			try {
+				PixbufOrientation thumb_orientation = Accelerometer.GetViewOrientation (PixbufOrientation.TopLeft);
 				thumb = new Gdk.Pixbuf (ThumbnailGenerator.ThumbnailPath (uri));
+				thumb = PixbufUtils.TransformOrientation (thumb, thumb_orientation);
 			} catch (System.Exception e) {
 				//FSpot.ThumbnailGenerator.Default.Request (uri.ToString (), 0, 256, 256);	
 				if (!(e is GLib.GException)) 
Index: src/Makefile.am
===================================================================
RCS file: /cvs/gnome/f-spot/src/Makefile.am,v
retrieving revision 1.62
diff -u -r1.62 Makefile.am
--- src/Makefile.am	26 Feb 2006 22:37:08 -0000	1.62
+++ src/Makefile.am	4 Apr 2006 15:18:58 -0000
@@ -38,6 +38,7 @@
 	$(srcdir)/Global.cs			\
 	$(srcdir)/GroupAdaptor.cs		\
 	$(srcdir)/GroupSelector.cs		\
+	$(srcdir)/Accelerometer.cs		\
 	$(srcdir)/HigMessageDialog.cs		\
 	$(srcdir)/Histogram.cs			\
 	$(srcdir)/IBrowsableItem.cs		\
Index: src/PhotoImageView.cs
===================================================================
RCS file: /cvs/gnome/f-spot/src/PhotoImageView.cs,v
retrieving revision 1.65
diff -u -r1.65 PhotoImageView.cs
--- src/PhotoImageView.cs	22 Mar 2006 19:13:11 -0000	1.65
+++ src/PhotoImageView.cs	4 Apr 2006 15:18:58 -0000
@@ -15,7 +15,9 @@
 			loader.AreaUpdated += HandlePixbufAreaUpdated;
 			loader.AreaPrepared += HandlePixbufPrepared;
 			loader.Done += HandleDone;
-			
+
+			Accelerometer.OrientationChanged += HandleOrientationChanged;
+
 			this.SizeAllocated += HandleSizeAllocated;
 			this.KeyPressEvent += HandleKeyPressEvent;
 			this.ScrollEvent += HandleScrollEvent;
@@ -59,6 +61,11 @@
 		{
 			loader.LoadToDone ();
 			return this.Pixbuf;
+		}
+
+		public void HandleOrientationChanged (object sender)
+		{
+			Reload ();
 		}
 
 		public void Reload ()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]