2
// Support for the ThinkPad accelerometer.
14
public class Accelerometer {
15
public const string SYSFS_FILE = "/sys/devices/platform/hdaps/position";
17
public static event EventHandler OrientationChanged;
25
private static Orient current_orientation;
27
public static Orient CurrentOrientation {
29
return current_orientation;
33
public Accelerometer ()
37
public static ImageOrientation GetViewOrientation (ImageOrientation po)
39
if (timer == 0 && available)
40
SetupAccelerometer ();
42
if (current_orientation == Orient.TiltCounterclockwise)
43
return FSpot.Utils.PixbufUtils.Rotate90 (po);
45
if (current_orientation == Orient.TiltClockwise)
46
return FSpot.Utils.PixbufUtils.Rotate270 (po);
51
static uint timer = 0;
52
static bool available = true;
54
public static void SetupAccelerometer ()
56
if (!System.IO.File.Exists (SYSFS_FILE)) {
63
// Call once to set a baseline value.
64
// Hopefully the laptop is flat when this is
66
GetHDAPSCoords (out x, out y);
68
timer = GLib.Timeout.Add (500, new GLib.TimeoutHandler (CheckOrientation));
71
private static bool CheckOrientation ()
73
Orient new_orient = GetScreenOrientation ();
75
if (new_orient != current_orientation) {
76
current_orientation = new_orient;
78
EventHandler eh = OrientationChanged;
80
eh (null, EventArgs.Empty);
82
Log.Debug ("Laptop orientation changed...");
88
public static Orient GetScreenOrientation ()
92
GetHDAPSCoords (out x, out y);
95
return Orient.TiltClockwise;
98
return Orient.TiltCounterclockwise;
100
return Orient.Normal;
103
static int base_x = -1000; // initial nonsense values
104
static int base_y = -1000;
106
private static void GetHDAPSCoords (out int x, out int y)
109
using (Stream file = System.IO.File.OpenRead (SYSFS_FILE)) {
110
StreamReader sr = new StreamReader (file);
112
string s = sr.ReadLine ();
113
string [] ss = s.Substring (1, s.Length - 2).Split (',');
114
x = int.Parse (ss [0]);
115
y = int.Parse (ss [1]);
128
} catch (IOException) {