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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/***************************************************************************
*
* addon-generic-light.c:
* Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
*
* Based on addon-generic-backlight.c:
* Copyright (C) 2008 Danny Kukawka <danny.kukawka@web.de>
*
* Licensed under the Academic Free License version 2.1
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "glib-abi.h"
#include "dbus-abi.h"
#include "hal-abi.h"
#define HAL_PROPERTY "org.freedesktop.Hal.Device.LightSensor"
#define MAX_SENSORS 16
static const char prop_name[] = HAL_PROPERTY;
static const char prop_invalid[] = HAL_PROPERTY ".Invalid";
static const char prop_get[] = "GetBrightness";
static struct GMainLoop *main_loop;
static struct LibHalContext *halctx;
static struct DBusConnection *conn;
static char sysfs_path[512];
static int sensors, levels;
/**
* Get keyboard backlight level
*
* Tries to read from the specified sysfs device, and if successful,
* updates the internal keyboard backlight state. Warns if the device
* cannot be opened or be read from. Always returns the last succesful
* read.
*/
static void get_light_sensor(int *level, int nlevel)
{
static const char delim[] = " ,()";
static int sensor[MAX_SENSORS];
int fd, i;
char buf[64], *p;
fd = open(sysfs_path, O_RDONLY);
if (fd < 0) {
HAL_WARNING(("Could not open '%s'", sysfs_path));
goto err;
}
memset(buf, 0, sizeof(buf));
if (read(fd, buf, sizeof(buf)) < 0) {
HAL_WARNING(("Could not read from '%s'", sysfs_path));
goto close;
}
p = strtok(buf, delim);
for (i = 0; p && i < nlevel; i++) {
sensor[i] = atoi(p);
p = strtok(NULL, delim);
HAL_DEBUG(("light_sensor[%d]=%d", i, sensor[i]));
}
close:
close(fd);
err:
for (i = 0; i < nlevel; i++)
level[i] = sensor[i] < levels ? sensor[i]: levels - 1;
}
/* DBus set keyboard backlight level */
static struct DBusMessage *dbus_get_light_sensor(struct DBusMessage *msg)
{
struct DBusError err;
struct DBusMessage *reply;
int level[MAX_SENSORS];
dbus_error_init(&err);
if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INVALID))
return dbus_message_new_error(msg, prop_invalid,
"Invalid message");
get_light_sensor(level, sensors);
reply = dbus_message_new_method_return(msg);
if (reply) {
int *pb = level;
dbus_message_append_args(reply, DBUS_TYPE_ARRAY,
DBUS_TYPE_INT32, &pb, sensors, DBUS_TYPE_INVALID);
}
return reply;
}
/* DBus filter function */
static DBusHandlerResult filter_function(struct DBusConnection *connection,
struct DBusMessage *message,
void *userdata)
{
struct DBusMessage *reply = NULL;
if (dbus_message_is_method_call(message, prop_name, prop_get))
reply = dbus_get_light_sensor(message);
if (reply) {
dbus_connection_send(connection, reply, NULL);
dbus_message_unref(reply);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
int main(int argc, char *argv[])
{
struct DBusError err;
struct stat fs;
char *udi = getenv("UDI");
char *path = getenv("HAL_PROP_LINUX_SYSFS_PATH");
char *sensor_str = getenv("HAL_PROP_LIGHT_SENSOR_NUM_SENSORS");
char *level_str = getenv("HAL_PROP_LIGHT_SENSOR_NUM_LEVELS");
setup_logger();
if (udi == NULL) {
HAL_ERROR(("No device specified"));
return -2;
}
if (path == NULL) {
HAL_ERROR(("No sysfs path specified"));
return -2;
}
if (sensor_str == NULL) {
HAL_ERROR(("No light_sensor.num_sensors defined"));
return -2;
}
if (level_str == NULL) {
HAL_ERROR(("No light_sensor.num_levels defined"));
return -2;
}
sensors = atoi(sensor_str);
levels = atoi(level_str);
snprintf(sysfs_path, sizeof(sysfs_path), "%s", path);
HAL_DEBUG(("udi='%s', path='%s', sensors='%d', levels='%d'",
udi, path, sensors, levels));
if (sensors >= MAX_SENSORS) {
HAL_ERROR(("light_sensor.num_sensors is too large"));
return -2;
}
if (stat(sysfs_path, &fs)) {
HAL_ERROR(("The sysfs property path does not exist"));
return -2;
}
dbus_error_init(&err);
halctx = libhal_ctx_init_direct(&err);
if (halctx == NULL) {
HAL_ERROR(("Cannot connect to hald"));
return -3;
}
conn = libhal_ctx_get_dbus_connection(halctx);
dbus_connection_setup_with_g_main(conn, NULL);
dbus_connection_add_filter(conn, filter_function, NULL, NULL);
if (!libhal_device_claim_interface(halctx, udi, prop_name,
" <method name=\"GetBrightness\">\n"
" <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
" </method>\n",
&err)) {
HAL_ERROR(("Cannot claim interface '" HAL_PROPERTY "'"));
return -4;
}
dbus_error_init(&err);
if (!libhal_device_addon_is_ready(halctx, udi, &err))
return -4;
main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(main_loop);
return 0;
}
|