~ubuntu-branches/debian/jessie/f-spot/jessie

« back to all changes in this revision

Viewing changes to src/Clients/MainApp/FSpot/Accelerometer.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-08-11 22:17:42 UTC
  • mfrom: (2.4.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100811221742-2qv3a5uya7gfe1t1
* New upstream release 0.7.2 "Retooled"
  + Third release of the unstable 0.7 development series. Features a
    fully restructured source tree with full Monodevelop build
    support. Solves some of the regressions introduced in 0.7.1.
  + Reorganized source tree for clarity, builds with Monodevelop.
  + Switched from QueuedSqliteDatabase to HyenaSqliteConnection (Mike
    Gemünde)
  + Build tweaks (Christian Krause)
  + More GtkBuilder transition (Eric Faehnrich) 
  + Reliability improvements (lots of them) for metadata handling (Mike
    Gemünde, Ruben Vermeersch)
  + Prune empty directories when deleting photos, import usability
    enhancements (Mike Wallick)
  + Big race-condition fix in import (Paul Wellner Bou)
  + Loads of improvements to Taglib#, in terms of handling broken files,
    extra format support (Pentax, Panasonic, Leica), stability and
    correctness (Ruben Vermeersch)
    - Runs out of memory Importing photo with suspect EXIF data
      (LP: #272822)
    - Metadata parsing of broken file causes large memory allocation
      (LP: #597720)
    - Photo import: cancel & copy have same keyboard shortcut (LP: #244423)
    - Facebook export will not create new album (LP: #563495)
    - Allow export to iPod (LP: #518344)
  + Reporting of import errors.
  + Speedups to repeated imports of the same directory.
  + Piles of cleanups and general stability improvements.
  + Over 50 bugs closed (http://bit.ly/cqpC3y)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Support for the ThinkPad accelerometer.
 
3
//
 
4
 
 
5
using System;
 
6
using System.IO;
 
7
 
 
8
using FSpot.Utils;
 
9
using Hyena;
 
10
using TagLib.Image;
 
11
 
 
12
namespace FSpot {
 
13
 
 
14
        public class Accelerometer {
 
15
                public const string SYSFS_FILE = "/sys/devices/platform/hdaps/position";
 
16
 
 
17
                public static event EventHandler OrientationChanged;
 
18
 
 
19
                public enum Orient {
 
20
                        Normal,
 
21
                        TiltClockwise,
 
22
                        TiltCounterclockwise,
 
23
                }
 
24
 
 
25
                private static Orient current_orientation;
 
26
 
 
27
                public static Orient CurrentOrientation {
 
28
                        get {
 
29
                                return current_orientation;
 
30
                        }
 
31
                }
 
32
 
 
33
                public Accelerometer ()
 
34
                {
 
35
                }
 
36
 
 
37
                public static ImageOrientation GetViewOrientation (ImageOrientation po)
 
38
                {
 
39
                        if (timer == 0 && available)
 
40
                                SetupAccelerometer ();
 
41
 
 
42
                        if (current_orientation == Orient.TiltCounterclockwise)
 
43
                                return FSpot.Utils.PixbufUtils.Rotate90 (po);
 
44
 
 
45
                        if (current_orientation == Orient.TiltClockwise)
 
46
                                return FSpot.Utils.PixbufUtils.Rotate270 (po);
 
47
 
 
48
                        return po;
 
49
                }
 
50
 
 
51
                static uint timer = 0;
 
52
                static bool available = true;
 
53
 
 
54
                public static void SetupAccelerometer ()
 
55
                {
 
56
                        if (!System.IO.File.Exists (SYSFS_FILE)) {
 
57
                                available = false;
 
58
                                return;
 
59
                        }
 
60
 
 
61
                        int x, y;
 
62
 
 
63
                        // Call once to set a baseline value.
 
64
                        // Hopefully the laptop is flat when this is
 
65
                        // called.
 
66
                        GetHDAPSCoords (out x, out y);
 
67
 
 
68
                        timer = GLib.Timeout.Add (500, new GLib.TimeoutHandler (CheckOrientation));
 
69
                }
 
70
 
 
71
                private static bool CheckOrientation ()
 
72
                {
 
73
                        Orient new_orient = GetScreenOrientation ();
 
74
 
 
75
                        if (new_orient != current_orientation) {
 
76
                                current_orientation = new_orient;
 
77
 
 
78
                                EventHandler eh = OrientationChanged;
 
79
                                if (eh != null)
 
80
                                        eh (null, EventArgs.Empty);
 
81
 
 
82
                                Log.Debug ("Laptop orientation changed...");
 
83
                        }
 
84
 
 
85
                        return true;
 
86
                }
 
87
 
 
88
                public static Orient GetScreenOrientation ()
 
89
                {
 
90
                        int x, y;
 
91
 
 
92
                        GetHDAPSCoords (out x, out y);
 
93
 
 
94
                        if (x > 40)
 
95
                                return Orient.TiltClockwise;
 
96
 
 
97
                        if (x < -40)
 
98
                                return Orient.TiltCounterclockwise;
 
99
 
 
100
                        return Orient.Normal;
 
101
                }
 
102
 
 
103
                static int base_x = -1000; // initial nonsense values
 
104
                static int base_y = -1000;
 
105
 
 
106
                private static void GetHDAPSCoords (out int x, out int y)
 
107
                {
 
108
                        try {
 
109
                                using (Stream file = System.IO.File.OpenRead (SYSFS_FILE)) {
 
110
                                        StreamReader sr = new StreamReader (file);
 
111
 
 
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]);
 
116
 
 
117
                                        if (base_x == -1000)
 
118
                                                base_x = x;
 
119
 
 
120
                                        if (base_y == -1000)
 
121
                                                base_y = y;
 
122
 
 
123
                                        x -= base_x;
 
124
                                        y -= base_y;
 
125
 
 
126
                                        return;
 
127
                                }
 
128
                        } catch (IOException) {
 
129
                                x = 0;
 
130
                                y = 0;
 
131
                        }
 
132
                }
 
133
        }
 
134
}