1e03ace836caf2eedd6a2df50262c1e730875300
[cinaest] / src / orientation.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
5  * Cinaest is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Cinaest is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 public class Orientation : Object {
20         public bool portrait;
21         public bool keyboard;
22         public signal void changed ();
23         private DBus.Connection conn;
24         private dynamic DBus.Object mce_request;
25         private dynamic DBus.Object mce_signal;
26         private dynamic DBus.Object hal_slide;
27         private const string MCE_SERVICE = "com.nokia.mce";
28         private const string MCE_REQUEST_PATH = "/com/nokia/mce/request";
29         private const string MCE_SIGNAL_PATH = "/com/nokia/mce/signal";
30         private const string MCE_REQUEST_IF = "com.nokia.mce.request";
31         private const string MCE_SIGNAL_IF = "com.nokia.mce.signal";
32         private const string HAL_SERVICE = "org.freedesktop.Hal";
33         private const string HAL_SLIDE_PATH = "/org/freedesktop/Hal/devices/platform_slide";
34         private const string HAL_DEVICE_IF = "org.freedesktop.Hal.Device";
35
36         construct {
37                 portrait = false;
38                 try {
39                         conn = DBus.Bus.get (DBus.BusType.SYSTEM);
40
41                         mce_request = conn.get_object (MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF);
42                         mce_enable_accelerometer ();
43
44                         mce_signal = conn.get_object (MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF);
45                         mce_signal.sig_device_orientation_ind += on_mce_orientation_changed;
46
47                         hal_slide = conn.get_object (HAL_SERVICE, HAL_SLIDE_PATH, HAL_DEVICE_IF);
48                         hal_slide.Condition += on_hal_slide_condition;
49                         bool closed = hal_slide.GetPropertyBoolean ("button.state.value");
50                         keyboard = !closed;
51                 } catch (Error e) {
52                         stderr.printf ("Error: %s\n", e.message);
53                 }
54         }
55
56         private void mce_enable_accelerometer () {
57                 string orientation, stand, face;
58                 int x, y, z;
59
60                 mce_request.req_accelerometer_enable (out orientation, out stand, out face, out x, out y, out z);
61                 on_mce_orientation_changed (mce_request, orientation, stand, face, x, y, z);
62         }
63
64         private void on_mce_orientation_changed (dynamic DBus.Object bluez, string orientation,
65                                                  string stand, string face, int x, int y , int z) {
66                 portrait = (orientation == "portrait" & !keyboard);
67                 changed ();
68         }
69
70         private void on_hal_slide_condition (dynamic DBus.Object slide, string cond_name, string cond_details) {
71                 if (cond_name == "ButtonPressed" && cond_details == "cover") {
72                         bool closed = hal_slide.GetPropertyBoolean ("button.state.value");
73                         keyboard = !closed;
74                         portrait &= closed;
75                 }
76                 changed ();
77         }
78 }