summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2008-11-24 10:51:54 +0100
committerHenrik Rydberg <rydberg@euromail.se>2008-11-24 10:51:54 +0100
commit8a6c3c55a7b5ff2042ad6973ddd7ab73236de047 (patch)
treea4ad1692c6b30f6080afbec1b31664afcf407385
Initial version
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
-rw-r--r--.gitignore4
-rw-r--r--GIT1
-rw-r--r--Makefile37
-rw-r--r--dbus-abi.h34
-rw-r--r--debian/README.Debian14
-rw-r--r--debian/changelog5
-rw-r--r--debian/compat1
-rw-r--r--debian/control17
-rw-r--r--debian/copyright27
-rw-r--r--debian/dirs3
-rwxr-xr-xdebian/rules46
-rw-r--r--fdi/10-applesmc.fdi36
-rw-r--r--glib-abi.h19
-rw-r--r--hal-abi.h28
-rw-r--r--hald-addon-generic-backlight.c241
15 files changed, 513 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5f1bda1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
1.bzr
2.bzrignore
3bin
4obj
diff --git a/GIT b/GIT
new file mode 100644
index 0000000..c55d54a
--- /dev/null
+++ b/GIT
@@ -0,0 +1 @@
git-clone http://bitmath.org/git/hal-applesmc.git
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..2b7bb21
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,37 @@
1ADDONS = hald-addon-generic-backlight
2FDIS = 10-nvidia-bl.fdi
3
4TFDI = $(addprefix fdi/,$(FDIS))
5TBIN = $(addprefix bin/,$(ADDONS))
6TOBJ = $(addprefix obj/,$(addsuffix .o,$(ADDONS)))
7
8LIBS = -lhal -ldbus-glib-1
9
10DFDI = usr/share/hal/fdi/policy/10osvendor
11DBIN = usr/lib/hal
12
13.PHONY: all clean
14.PRECIOUS: obj/%.o
15
16all: $(TBIN) $(TOBJ)
17
18bin/%: obj/%.o
19 @mkdir -p $(@D)
20 gcc $< $(LIBS) -o $@
21
22obj/%.o: %.c
23 @mkdir -p $(@D)
24 gcc -c $< -o $@
25
26clean:
27 rm -rf bin obj
28
29distclean: clean
30 rm -rf *-stamp debian/hal-nvidia-bl* debian/*.log debian/files
31
32install: $(TBIN) $(TFDI)
33 install -d "$(DESTDIR)/$(DBIN)"
34 install -d "$(DESTDIR)/$(DFDI)"
35 install -m 755 $(TBIN) "$(DESTDIR)/$(DBIN)"
36 install -m 644 $(TFDI) "$(DESTDIR)/$(DFDI)"
37
diff --git a/dbus-abi.h b/dbus-abi.h
new file mode 100644
index 0000000..ed406f1
--- /dev/null
+++ b/dbus-abi.h
@@ -0,0 +1,34 @@
1/*
2 * DBUS ABI
3 *
4 * The following is all dbus information needed in order to compile
5 * a hald-addon.
6 */
7
8/* Structure declarations */
9struct DBusMessage;
10
11/* Allocate 32 bytes for the error structure. */
12struct DBusError {
13 char bytes[32];
14};
15
16/* Define DBus arguments types */
17const int DBUS_TYPE_INVALID;
18const int DBUS_TYPE_INT32 = 'i';
19const int DBUS_TYPE_ARRAY = 'a';
20
21/* Define DBus handler results */
22typedef enum {
23 DBUS_HANDLER_RESULT_HANDLED,
24 DBUS_HANDLER_RESULT_NOT_YET_HANDLED,
25 DBUS_HANDLER_RESULT_NEED_MEMORY
26} DBusHandlerResult;
27
28/* Function declarations */
29struct DBusMessage *
30dbus_message_new_error(struct DBusMessage *reply_to, const char *error_name,
31 const char *error_message);
32struct DBusMessage *
33dbus_message_new_method_return(struct DBusMessage *method_call);
34
diff --git a/debian/README.Debian b/debian/README.Debian
new file mode 100644
index 0000000..67859ad
--- /dev/null
+++ b/debian/README.Debian
@@ -0,0 +1,14 @@
1hal-nvidia-bl package for Debian
2
3This package adds LCD backlight support via the nvidia-bl sysfs
4interface.
5
6* Install the debian package
7
8* Restart hal
9
10* Restart gnome-power-manager
11
12Enjoy!
13
14Henrik Rydberg <rydberg@euromail.se>
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..458b1a6
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
1hal-nvidia-bl (0.1.0) unstable; urgency=low
2
3 * Add LCD backlight support for nvidia-bl
4
5 -- Henrik Rydberg <rydberg@euromail.se> Sat, 25 Oct 2008 20:12:00 +0200
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..7ed6ff8
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
5
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..04dbc2d
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,17 @@
1Source: hal-nvidia-bl
2Section: misc
3Priority: optional
4Maintainer: Henrik Rydberg <rydberg@euromail.se>
5Build-Depends: debhelper (>= 5),
6 libhal-dev (>= 0.5.8),
7 libdbus-glib-1-dev (>= 0.71)
8Standards-Version: 3.7.2
9Homepage: http://bitmath.org/code/hal-nvidia-bl/
10
11Package: hal-nvidia-bl
12Architecture: any
13Depends: bash (>> 1.99)
14Description: LCD backlight support
15 This package adds LCD backlight support via the nvidia-bl sysfs
16 interface.
17
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..b0aa765
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,27 @@
1Source URL: http://bitmath.org/code/hal-nvidia-bl/
2
3Upstream Author: Henrik Rydberg <rydberg@euromail.se>
4
5Copyright:
6 Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se)
7
8License:
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
24Packaging:
25 Copyright (C) 2008 by Henrik Rydberg <rydberg@euromail.se>
26 released under GPL 2
27
diff --git a/debian/dirs b/debian/dirs
new file mode 100644
index 0000000..ced47fc
--- /dev/null
+++ b/debian/dirs
@@ -0,0 +1,3 @@
1usr/share/hal/fdi/policy/10osvendor
2usr/lib/hal
3
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..90568c9
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,46 @@
1#!/usr/bin/make -f
2# -*- makefile -*-
3
4# Uncomment this to turn on verbose mode.
5#export DH_VERBOSE=1
6
7build-arch:
8 dh_testdir
9 $(MAKE)
10
11build-indep:
12
13build: build-arch build-indep
14
15install:
16 dh_testdir
17 dh_testroot
18 dh_clean -k
19 dh_installdirs
20 $(MAKE) DESTDIR=$(CURDIR)/debian/hal-applesmc install
21
22binary-arch: build-arch install
23 dh_testdir
24 dh_testroot
25 dh_link
26 dh_strip
27 dh_compress
28 dh_fixperms
29 dh_installdeb
30 dh_shlibdeps
31 dh_gencontrol
32 dh_md5sums
33 dh_builddeb
34
35binary-indep: build-indep
36
37binary: binary-arch binary-indep
38
39clean:
40 dh_testdir
41 dh_testroot
42 $(MAKE) clean
43 dh_clean
44
45.PHONY: build-arch build-indep build install binary-arch binary-indep binary clean
46
diff --git a/fdi/10-applesmc.fdi b/fdi/10-applesmc.fdi
new file mode 100644
index 0000000..89f0014
--- /dev/null
+++ b/fdi/10-applesmc.fdi
@@ -0,0 +1,36 @@
1<?xml version="1.0" encoding="UTF-8"?>
2
3<deviceinfo version="0.2">
4
5 <device>
6 <match key="platform.id" contains="applesmc">
7 <spawn udi="/org/freedesktop/Hal/devices/applesmc_keyboard_backlight"/>
8 <spawn udi="/org/freedesktop/Hal/devices/applesmc_light_sensor"/>
9 </match>
10 </device>
11
12 <device>
13 <match key="info.udi" string="/org/freedesktop/Hal/devices/applesmc_keyboard_backlight">
14 <append key="info.capabilities" type="strlist">keyboard_backlight</append>
15 <merge key="info.product" type="string">Applesmc Keyboard Backlight</merge>
16 <merge key="keyboard_backlight.num_levels" type="int">256</merge>
17 <merge key="keyboard_backlight.access_method" type="string">custom</merge>
18 <merge key="linux.sysfs_path" type="string">/sys/class/leds/smc::kbd_backlight</merge>
19 <append key="info.addons" type="strlist">hald-addon-generic-kbd-backlight</append>
20 </match>
21 </device>
22
23 <device>
24 <match key="info.udi" string="/org/freedesktop/Hal/devices/applesmc_light_sensor">
25 <append key="info.capabilities" type="strlist">light_sensor</append>
26 <merge key="info.product" type="string">Applesmc Light Sensor</merge>
27 <merge key="light_sensor.num_sensors" type="int">1</merge>
28 <merge key="light_sensor.num_levels" type="int">256</merge>
29 <append key="light_sensor.sensor_locations" type="strlist">left</append>
30 <merge key="light_sensor.access_method" type="string">custom</merge>
31 <merge key="linux.sysfs_path" type="string">/sys/devices/platform/applesmc.768/light</merge>
32 <append key="info.addons" type="strlist">hald-addon-generic-light-sensor</append>
33 </match>
34 </device>
35
36</deviceinfo>
diff --git a/glib-abi.h b/glib-abi.h
new file mode 100644
index 0000000..62b85d5
--- /dev/null
+++ b/glib-abi.h
@@ -0,0 +1,19 @@
1/*
2 * GLIB ABI
3 *
4 * The following is all glib information needed in order to compile
5 * a hald-addon.
6 */
7
8/* logical values */
9const int TRUE = 1;
10const int FALSE;
11
12/* Structure declarations */
13struct GMainContext;
14struct GMainLoop;
15
16/* Function declarations */
17struct GMainLoop *g_main_loop_new(struct GMainContext *context, int is_running);
18void g_main_loop_run(struct GMainLoop *loop);
19
diff --git a/hal-abi.h b/hal-abi.h
new file mode 100644
index 0000000..277580f
--- /dev/null
+++ b/hal-abi.h
@@ -0,0 +1,28 @@
1/*
2 * HAL ABI
3 *
4 * The following is all hal information needed in order to compile
5 * a hald-addon.
6 */
7
8/* Structure declarations */
9struct LibHalContext;
10struct DBusConnection;
11
12/* Simple error handling */
13#define setup_logger()
14#define eprintf(...) (fprintf(stderr, __VA_ARGS__), fprintf(stderr, "\n"))
15#define WARNING(...) eprintf("addon-warning: " __VA_ARGS__)
16#define DEBUG(...) eprintf("addon-debug: " __VA_ARGS__)
17#define ERROR(...) eprintf("addon-error: " __VA_ARGS__)
18#define HAL_WARNING(x) WARNING x
19#define HAL_DEBUG(x) DEBUG x
20#define HAL_ERROR(x) ERROR x
21
22/* Function declarations */
23struct LibHalContext *
24libhal_ctx_init_direct(struct DBusError *error);
25
26struct DBusConnection *
27libhal_ctx_get_dbus_connection(struct LibHalContext *ctx);
28
diff --git a/hald-addon-generic-backlight.c b/hald-addon-generic-backlight.c
new file mode 100644
index 0000000..537ab69
--- /dev/null
+++ b/hald-addon-generic-backlight.c
@@ -0,0 +1,241 @@
1/***************************************************************************
2 *
3 * addon-generic-kbd-backlight.c:
4 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
5 *
6 * Based on addon-generic-backlight.c:
7 * Copyright (C) 2008 Danny Kukawka <danny.kukawka@web.de>
8 *
9 * Licensed under the Academic Free License version 2.1
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 **************************************************************************/
26
27#include <string.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <stdio.h>
33#include <stdlib.h>
34
35#include "glib-abi.h"
36#include "dbus-abi.h"
37#include "hal-abi.h"
38
39#define HAL_PROPERTY "org.freedesktop.Hal.Device.KeyboardBacklight"
40
41static const char prop_name[] = HAL_PROPERTY;
42static const char prop_invalid[] = HAL_PROPERTY ".Invalid";
43static const char prop_set[] = "SetBrightness";
44static const char prop_get[] = "GetBrightness";
45
46static struct GMainLoop *main_loop;
47static struct LibHalContext *halctx;
48static struct DBusConnection *conn;
49
50static char sysfs_path[512];
51static int levels;
52
53/**
54 * Get keyboard backlight level
55 *
56 * Tries to read from the specified sysfs device, and if successful,
57 * updates the internal keyboard backlight state. Warns if the device
58 * cannot be opened or be read from. Always returns the last succesful
59 * read.
60 */
61static int get_kbd_backlight()
62{
63 static int level;
64 int fd;
65 char buf[64];
66
67 fd = open(sysfs_path, O_RDONLY);
68 if (fd < 0) {
69 HAL_WARNING(("Could not open '%s'", sysfs_path));
70 return level;
71 }
72
73 memset(buf, 0, sizeof(buf));
74 if (read(fd, buf, sizeof(buf)) < 0)
75 HAL_WARNING(("Could not read from '%s'", sysfs_path));
76 else
77 level = atoi(buf);
78
79 close(fd);
80 return level;
81}
82
83/**
84 * Set keyboard backlight level
85 *
86 * Warns if the device cannot be opened or be written to.
87 * Returns bytes written on success, negative value on failure.
88 */
89static int set_kbd_backlight(int level)
90{
91 int fd, ret;
92 char buf[64];
93
94 sprintf(buf, "%d", level);
95
96 fd = open(sysfs_path, O_WRONLY);
97 if (fd < 0) {
98 HAL_WARNING(("Could not open '%s'", sysfs_path));
99 return -1;
100 }
101
102 ret = write(fd, buf, strlen(buf));
103 if (ret < 0)
104 HAL_WARNING(("Could not write '%s' to '%s'", buf, sysfs_path));
105
106 close(fd);
107 return ret;
108}
109
110/* DBus set keyboard backlight level */
111static struct DBusMessage *dbus_get_kbd_backlight(struct DBusMessage *msg)
112{
113 struct DBusError err;
114 struct DBusMessage *reply;
115 int level;
116
117 dbus_error_init(&err);
118 if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INVALID))
119 return dbus_message_new_error(msg, prop_invalid,
120 "Invalid message");
121
122 level = get_kbd_backlight();
123
124 reply = dbus_message_new_method_return(msg);
125 if (reply)
126 dbus_message_append_args(reply, DBUS_TYPE_INT32, &level,
127 DBUS_TYPE_INVALID);
128
129 return reply;
130}
131
132/* DBus set keyboard backlight level */
133static struct DBusMessage *dbus_set_kbd_backlight(struct DBusMessage *msg)
134{
135 struct DBusError err;
136 int level;
137
138 dbus_error_init(&err);
139 if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &level,
140 DBUS_TYPE_INVALID))
141 return dbus_message_new_error(msg, prop_invalid,
142 "Brightness level argument missing");
143
144 if (level < 0 || level > levels - 1)
145 return dbus_message_new_error(msg, prop_invalid,
146 "Brightness level is invalid");
147
148 set_kbd_backlight(level);
149
150 return dbus_message_new_method_return(msg);
151}
152
153/* DBus filter function */
154static DBusHandlerResult filter_function(struct DBusConnection *connection,
155 struct DBusMessage *message,
156 void *userdata)
157{
158 struct DBusMessage *reply = NULL;
159
160 if (dbus_message_is_method_call(message, prop_name, prop_get))
161 reply = dbus_get_kbd_backlight(message);
162 else if (dbus_message_is_method_call(message, prop_name, prop_set))
163 reply = dbus_set_kbd_backlight(message);
164
165 if (reply) {
166 dbus_connection_send(connection, reply, NULL);
167 dbus_message_unref(reply);
168 }
169
170 return DBUS_HANDLER_RESULT_HANDLED;
171}
172
173int main(int argc, char *argv[])
174{
175 struct DBusError err;
176 struct stat fs;
177
178 char *udi = getenv("UDI");
179 char *path = getenv("HAL_PROP_LINUX_SYSFS_PATH");
180 char *level_str = getenv("HAL_PROP_KEYBOARD_BACKLIGHT_NUM_LEVELS");
181
182 setup_logger();
183
184 if (udi == NULL) {
185 HAL_ERROR(("No device specified"));
186 return -2;
187 }
188 if (path == NULL) {
189 HAL_ERROR(("No sysfs path specified"));
190 return -2;
191 }
192 if (level_str == NULL) {
193 HAL_ERROR(("No keyboard_backlight.num_levels defined"));
194 return -2;
195 }
196
197 levels = atoi(level_str);
198 snprintf(sysfs_path, sizeof(sysfs_path), "%s/brightness", path);
199
200 HAL_DEBUG(("udi='%s', path='%s', levels='%d'", udi, path, levels));
201
202 if (stat(sysfs_path, &fs)) {
203 HAL_ERROR(("The sysfs property path does not exist"));
204 return -2;
205 }
206
207 dbus_error_init(&err);
208 halctx = libhal_ctx_init_direct(&err);
209
210 if (halctx == NULL) {
211 HAL_ERROR(("Cannot connect to hald"));
212 return -3;
213 }
214
215 conn = libhal_ctx_get_dbus_connection(halctx);
216 dbus_connection_setup_with_g_main(conn, NULL);
217
218 dbus_connection_add_filter(conn, filter_function, NULL, NULL);
219
220 if (!libhal_device_claim_interface(halctx, udi, prop_name,
221 " <method name=\"GetBrightness\">\n"
222 " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
223 " </method>\n"
224 " <method name=\"SetBrightness\">\n"
225 " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
226 " </method>\n",
227 &err)) {
228 HAL_ERROR(("Cannot claim interface '" HAL_PROPERTY "'"));
229 return -4;
230 }
231
232 dbus_error_init(&err);
233 if (!libhal_device_addon_is_ready(halctx, udi, &err))
234 return -4;
235
236 main_loop = g_main_loop_new(NULL, FALSE);
237 g_main_loop_run(main_loop);
238
239 return 0;
240}
241