Debian packaging: 0.0.4-1
[beifahrer] / src / orientation.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer 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  * Beifahrer 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 Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 public class Orientation : Object {
20         private const string MCE_SERVICE = "com.nokia.mce";
21         private const string MCE_REQUEST_PATH = "/com/nokia/mce/request";
22         private const string MCE_SIGNAL_PATH = "/com/nokia/mce/signal";
23         private const string MCE_REQUEST_IF = "com.nokia.mce.request";
24         private const string MCE_SIGNAL_IF = "com.nokia.mce.signal";
25
26         private const string HAL_SERVICE = "org.freedesktop.Hal";
27         private const string HAL_SLIDE_PATH = "/org/freedesktop/Hal/devices/platform_slide";
28         private const string HAL_DEVICE_IF = "org.freedesktop.Hal.Device";
29
30         public bool portrait;
31         public bool keyboard;
32         private DBus.Connection conn;
33         private dynamic DBus.Object mce_request;
34         private dynamic DBus.Object mce_signal;
35         private dynamic DBus.Object hal_slide;
36
37         public signal void changed ();
38
39         construct {
40                 portrait = false;
41                 try {
42                         conn = DBus.Bus.get (DBus.BusType.SYSTEM);
43
44                         mce_request = conn.get_object (MCE_SERVICE, MCE_REQUEST_PATH, MCE_REQUEST_IF);
45                         mce_enable_accelerometer ();
46
47                         mce_signal = conn.get_object (MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF);
48                         mce_signal.sig_device_orientation_ind.connect (on_mce_orientation_changed);
49
50                         hal_slide = conn.get_object (HAL_SERVICE, HAL_SLIDE_PATH, HAL_DEVICE_IF);
51                         hal_slide.Condition.connect (on_hal_slide_condition);
52                         bool closed = hal_slide.GetPropertyBoolean ("button.state.value");
53                         keyboard = !closed;
54                 } catch (Error e) {
55                         stderr.printf ("Error: %s\n", e.message);
56                 }
57         }
58
59         private void mce_enable_accelerometer () {
60                 string orientation, stand, face;
61                 int x, y, z;
62
63                 mce_request.req_accelerometer_enable (out orientation, out stand, out face, out x, out y, out z);
64                 on_mce_orientation_changed (mce_request, orientation, stand, face, x, y, z);
65         }
66
67         private void on_mce_orientation_changed (dynamic DBus.Object mce, string orientation,
68                                                  string stand, string face, int x, int y , int z) {
69                 portrait = (orientation == "portrait" & !keyboard);
70                 changed ();
71         }
72
73         private void on_hal_slide_condition (dynamic DBus.Object slide, string cond_name, string cond_details) {
74                 if (cond_name == "ButtonPressed" && cond_details == "cover") {
75                         bool closed = hal_slide.GetPropertyBoolean ("button.state.value");
76                         keyboard = !closed;
77                         portrait &= closed;
78                 }
79                 changed ();
80         }
81 }