~phablet-team/powerd/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright 2013 Canonical Ltd.
 *
 * Authors:
 * Ricardo Mendoza: ricardo.mendoza@canonical.com
 *
 * This file is part of powerd.
 *
 * powerd is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * powerd is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "powerd-internal.h"
#include "powerd-sensors.h"
#include "log.h"
#include <atomic>

#include <ubuntu/application/sensors/light.h>
#include <ubuntu/application/sensors/proximity.h>

namespace
{

UASensorsProximity* prox_sensor;
UASensorsLight* light_sensor;
GMainLoop* main_loop = NULL;
std::atomic<bool> allow_synthetic{false};
enum class Proximity { unknown, near, far };
std::atomic<Proximity> current_proximity{Proximity::unknown};

void update_and_emit_proximity(Proximity proximity)
{
    current_proximity = proximity;
    if (proximity == Proximity::near)
        powerd_proximity_event(TRUE);
    else if (proximity == Proximity::far)
        powerd_proximity_event(FALSE);
}

gboolean emit_synthetic_proximity_far(void *context)
{
    if (allow_synthetic)
        update_and_emit_proximity(Proximity::far);

    return FALSE;
}

void on_new_proximity_event(UASProximityEvent *event, void *context)
{
    allow_synthetic = false;
    switch (uas_proximity_event_get_distance(event))
    {
        case U_PROXIMITY_NEAR:
        {
            update_and_emit_proximity(Proximity::near);
            break;
        }
        case U_PROXIMITY_FAR:
        {
            update_and_emit_proximity(Proximity::far);
            break;
        }
    }
}

}

void powerd_sensors_proximity_enable(void)
{
    //FIXME: Some proximity sensors do not
    //send an initial event when enabled and nothing is close
    //To work around this, we schedule a synthetic proximity
    //far event in case no event has been emitted 500ms
    //after enabling the proximity sensor
    allow_synthetic = true;
    current_proximity = Proximity::unknown;
    g_timeout_add(500, emit_synthetic_proximity_far, NULL);

    ua_sensors_proximity_enable(prox_sensor);
}

void powerd_sensors_proximity_disable(void)
{
    allow_synthetic = false;
    ua_sensors_proximity_disable(prox_sensor);
    current_proximity = Proximity::unknown;
}

void powerd_sensors_proximity_emit(void)
{
    update_and_emit_proximity(current_proximity);
}

void on_new_als_event(UASLightEvent *event, void *context)
{
    float lux;
    uas_light_event_get_light(event, &lux);
    powerd_new_als_event(lux);
}

void powerd_sensors_als_enable(void)
{
    if (light_sensor)
        ua_sensors_light_enable(light_sensor);
}

void powerd_sensors_als_disable(void)
{
    if (light_sensor)
        ua_sensors_light_disable(light_sensor);
}

void powerd_sensors_init(GMainLoop *ml)
{
    main_loop = ml;
    prox_sensor = ua_sensors_proximity_new();
    if (prox_sensor != NULL) {
        ua_sensors_proximity_set_reading_cb(prox_sensor,
                                            on_new_proximity_event,
                                            NULL);
    } else {
        powerd_warn("Failed to allocate proximity sensor");
    }

    light_sensor = ua_sensors_light_new();
    if (light_sensor) {
        ua_sensors_light_set_reading_cb(light_sensor, on_new_als_event,
                                        NULL);
    } else {
        powerd_warn("Failed to allocate ambient light sensor");
    }
}