Add support for /sys/class/backlight

Holger Macht hmacht at suse.de
Thu Oct 5 14:52:21 PDT 2006


Patch adding support for the /sys/class/backlight interface. If available,
a device /org/freedesktop/Hal/devices/computer_backlight is created. The
patch adds a class device and adjusts the scripts for the methods
GetBrightness and SetBrightness on the interface
org.freedesktop.Hal.Device.LaptopPanel. As soon as all kernel drivers are
converted to the new interface, we may drop the vendor specific code in
acpi.c.

Signed-off-by: Holger Macht <hmacht at suse.de>
---
diff --git a/hald/linux/acpi.c b/hald/linux/acpi.c
diff --git a/hald/linux/classdev.c b/hald/linux/classdev.c
index 5e61ce9..aa03ea0 100644
--- a/hald/linux/classdev.c
+++ b/hald/linux/classdev.c
@@ -1015,6 +1015,46 @@ mmc_host_compute_udi (HalDevice *d)
 	return TRUE;
 }
 
+static HalDevice *
+backlight_add (const gchar *sysfs_path, const gchar *device_file, HalDevice *physdev, 
+	       const gchar *sysfs_path_in_devices)
+{
+	HalDevice *d;
+	int max_brightness;
+
+	d = NULL;
+
+	d = hal_device_new ();
+	hal_device_add_capability (d, "laptop_panel");
+	hal_device_property_set_string (d, "linux.sysfs_path", sysfs_path);
+	hal_device_property_set_string (d, "info.parent", "/org/freedesktop/Hal/devices/computer");
+
+	hal_device_property_set_string (d, "info.category", "laptop_panel");
+
+	hal_util_get_int_from_file (sysfs_path, "max_brightness", &max_brightness, 10);
+	hal_device_property_set_int (d, "laptop_panel.num_levels", max_brightness + 1);
+
+	return d;
+}
+
+static gboolean
+backlight_compute_udi (HalDevice *d)
+{
+	gchar udi[256];
+
+	const char *dir;
+	dir = hal_device_property_get_string (d, "linux.sysfs_path");
+	const char *name;
+	name = hal_util_get_last_element(dir);
+
+	hal_util_compute_udi (hald_get_gdl (), udi, sizeof (udi),
+			      "%s_backlight",
+			      hal_device_property_get_string (d, "info.parent"));
+	hal_device_set_udi (d, udi);
+	hal_device_property_set_string (d, "info.udi", udi);
+	return TRUE;
+}
+
 /*--------------------------------------------------------------------------------------------------------------*/
 
 static gboolean
@@ -1180,6 +1220,16 @@ static ClassDevHandler classdev_handler_
 	.remove       = classdev_remove
 };
 
+static ClassDevHandler classdev_handler_backlight =
+{
+	.subsystem    = "backlight",
+	.add          = backlight_add,
+	.get_prober   = NULL,
+	.post_probing = NULL,
+	.compute_udi  = backlight_compute_udi,
+	.remove       = classdev_remove
+};
+
 static ClassDevHandler *classdev_handlers[] = {
 	&classdev_handler_input,
 	&classdev_handler_bluetooth,
@@ -1195,6 +1245,7 @@ static ClassDevHandler *classdev_handler
 	&classdev_handler_tape,
 	&classdev_handler_tape390,
 	&classdev_handler_mmc_host,
+	&classdev_handler_backlight,
 	NULL
 };
 
diff --git a/tools/hal-system-lcd-get-brightness b/tools/hal-system-lcd-get-brightness
index bf8fcfc..e647095 100755
--- a/tools/hal-system-lcd-get-brightness
+++ b/tools/hal-system-lcd-get-brightness
@@ -8,7 +8,7 @@ # the Free Software Foundation; either v
 # (at your option) any later version.
 
 # Check for environment variables
-if [ -z "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" ]; then
+if [ -z "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" -a -z "$HAL_PROP_LINUX_SYSFS_PATH" ]; then
         echo "org.freedesktop.Hal.Device.UnknownError" >&2
 	echo "Missing or empty environment variable(s)." >&2
 	echo "This script should be started by hald." >&2
diff --git a/tools/hal-system-lcd-set-brightness b/tools/hal-system-lcd-set-brightness
index a677412..f288f99 100755
--- a/tools/hal-system-lcd-set-brightness
+++ b/tools/hal-system-lcd-set-brightness
@@ -8,7 +8,7 @@ # the Free Software Foundation; either v
 # (at your option) any later version.
 
 # Check for environment variables
-if [ -z "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" ] || 
+if [ -z "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" -a -z "$HAL_PROP_LINUX_SYSFS_PATH" ] || 
    [ -z "$HAL_PROP_LAPTOP_PANEL_NUM_LEVELS" ] ; then
         echo "org.freedesktop.Hal.Device.UnknownError" >&2
         echo "Missing or empty environment variable(s)." >&2
diff --git a/tools/linux/hal-system-lcd-get-brightness-linux b/tools/linux/hal-system-lcd-get-brightness-linux
index 3ff9284..477a22d 100755
--- a/tools/linux/hal-system-lcd-get-brightness-linux
+++ b/tools/linux/hal-system-lcd-get-brightness-linux
@@ -7,6 +7,12 @@ # it under the terms of the GNU General 
 # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
 
+# we have /sys/class/backlight
+if [ -r "$HAL_PROP_LINUX_SYSFS_PATH/actual_brightness" ]; then
+	value="`cat $HAL_PROP_LINUX_SYSFS_PATH/actual_brightness`"
+	exit ${value}
+fi
+
 if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "pmu" ]; then
 	value="`hal-system-power-pmu getlcd`"
 	if [ $? -ne 0 ]; then
diff --git a/tools/linux/hal-system-lcd-set-brightness-linux b/tools/linux/hal-system-lcd-set-brightness-linux
index f73e680..38e5730 100755
--- a/tools/linux/hal-system-lcd-set-brightness-linux
+++ b/tools/linux/hal-system-lcd-set-brightness-linux
@@ -7,6 +7,12 @@ # it under the terms of the GNU General 
 # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
 
+# we have /sys/class/backlight
+if [ -w "$HAL_PROP_LINUX_SYSFS_PATH/brightness" ]; then
+	echo "$value" > $HAL_PROP_LINUX_SYSFS_PATH/brightness
+	exit 0
+fi
+
 if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" = "pmu" ]; then
 	hal-system-power-pmu setlcd $value
 	if [ $? -ne 0 ]; then


More information about the hal mailing list