From 067a6b838d9d0c7b1e3a675290136ac95d945fd0 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Mon, 24 Nov 2008 11:14:43 +0100 Subject: Adjust fdi file and rename addon, emphasizing lcd-backlight Signed-off-by: Henrik Rydberg --- Makefile | 2 +- fdi/10-nvidia-bl.fdi | 32 ++--- hald-addon-generic-backlight.c | 241 ------------------------------------- hald-addon-generic-lcd-backlight.c | 241 +++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 265 deletions(-) delete mode 100644 hald-addon-generic-backlight.c create mode 100644 hald-addon-generic-lcd-backlight.c diff --git a/Makefile b/Makefile index 2b7bb21..fd7bac8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -ADDONS = hald-addon-generic-backlight +ADDONS = hald-addon-generic-lcd-backlight FDIS = 10-nvidia-bl.fdi TFDI = $(addprefix fdi/,$(FDIS)) diff --git a/fdi/10-nvidia-bl.fdi b/fdi/10-nvidia-bl.fdi index 89f0014..f9357f3 100644 --- a/fdi/10-nvidia-bl.fdi +++ b/fdi/10-nvidia-bl.fdi @@ -3,33 +3,19 @@ - - - + + - - keyboard_backlight - Applesmc Keyboard Backlight - 256 - custom - /sys/class/leds/smc::kbd_backlight - hald-addon-generic-kbd-backlight - - - - - - light_sensor - Applesmc Light Sensor - 1 - 256 - left - custom - /sys/devices/platform/applesmc.768/light - hald-addon-generic-light-sensor + + laptop_panel + Nvidia Backlight Interface + 16 + custom + /sys/class/backlight/mbp_backlight + hald-addon-generic-lcd-backlight diff --git a/hald-addon-generic-backlight.c b/hald-addon-generic-backlight.c deleted file mode 100644 index 09ab668..0000000 --- a/hald-addon-generic-backlight.c +++ /dev/null @@ -1,241 +0,0 @@ -/*************************************************************************** - * - * addon-generic-backlight.c: - * Copyright (C) 2008 Henrik Rydberg - * - * Based on upstream addon-generic-backlight.c: - * Copyright (C) 2008 Danny Kukawka - * - * 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 -#include -#include -#include -#include -#include -#include - -#include "glib-abi.h" -#include "dbus-abi.h" -#include "hal-abi.h" - -#define HAL_PROPERTY "org.freedesktop.Hal.Device.LaptopPanel" - -static const char prop_name[] = HAL_PROPERTY; -static const char prop_invalid[] = HAL_PROPERTY ".Invalid"; -static const char prop_set[] = "SetBrightness"; -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 levels; - -/** - * Get LCD backlight level - * - * Tries to read from the specified sysfs device, and if successful, - * updates the internal LCD backlight state. Warns if the device - * cannot be opened or be read from. Always returns the last succesful - * read. - */ -static int get_lcd_backlight() -{ - static int level; - int fd; - char buf[64]; - - fd = open(sysfs_path, O_RDONLY); - if (fd < 0) { - HAL_WARNING(("Could not open '%s'", sysfs_path)); - return level; - } - - memset(buf, 0, sizeof(buf)); - if (read(fd, buf, sizeof(buf)) < 0) - HAL_WARNING(("Could not read from '%s'", sysfs_path)); - else - level = atoi(buf); - - close(fd); - return level; -} - -/** - * Set LCD backlight level - * - * Warns if the device cannot be opened or be written to. - * Returns bytes written on success, negative value on failure. - */ -static int set_lcd_backlight(int level) -{ - int fd, ret; - char buf[64]; - - sprintf(buf, "%d", level); - - fd = open(sysfs_path, O_WRONLY); - if (fd < 0) { - HAL_WARNING(("Could not open '%s'", sysfs_path)); - return -1; - } - - ret = write(fd, buf, strlen(buf)); - if (ret < 0) - HAL_WARNING(("Could not write '%s' to '%s'", buf, sysfs_path)); - - close(fd); - return ret; -} - -/* DBus set LCD backlight level */ -static struct DBusMessage *dbus_get_lcd_backlight(struct DBusMessage *msg) -{ - struct DBusError err; - struct DBusMessage *reply; - int level; - - dbus_error_init(&err); - if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INVALID)) - return dbus_message_new_error(msg, prop_invalid, - "Invalid message"); - - level = get_lcd_backlight(); - - reply = dbus_message_new_method_return(msg); - if (reply) - dbus_message_append_args(reply, DBUS_TYPE_INT32, &level, - DBUS_TYPE_INVALID); - - return reply; -} - -/* DBus set LCD backlight level */ -static struct DBusMessage *dbus_set_lcd_backlight(struct DBusMessage *msg) -{ - struct DBusError err; - int level; - - dbus_error_init(&err); - if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &level, - DBUS_TYPE_INVALID)) - return dbus_message_new_error(msg, prop_invalid, - "Brightness level argument missing"); - - if (level < 0 || level > levels - 1) - return dbus_message_new_error(msg, prop_invalid, - "Brightness level is invalid"); - - set_lcd_backlight(level); - - return dbus_message_new_method_return(msg); -} - -/* 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_lcd_backlight(message); - else if (dbus_message_is_method_call(message, prop_name, prop_set)) - reply = dbus_set_lcd_backlight(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 *level_str = getenv("HAL_PROP_LAPTOP_PANEL_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 (level_str == NULL) { - HAL_ERROR(("No laptop_panel.num_levels defined")); - return -2; - } - - levels = atoi(level_str); - snprintf(sysfs_path, sizeof(sysfs_path), "%s/brightness", path); - - HAL_DEBUG(("udi='%s', path='%s', levels='%d'", udi, path, levels)); - - 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, - " \n" - " \n" - " \n" - " \n" - " \n" - " \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; -} - diff --git a/hald-addon-generic-lcd-backlight.c b/hald-addon-generic-lcd-backlight.c new file mode 100644 index 0000000..09ab668 --- /dev/null +++ b/hald-addon-generic-lcd-backlight.c @@ -0,0 +1,241 @@ +/*************************************************************************** + * + * addon-generic-backlight.c: + * Copyright (C) 2008 Henrik Rydberg + * + * Based on upstream addon-generic-backlight.c: + * Copyright (C) 2008 Danny Kukawka + * + * 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 +#include +#include +#include +#include +#include +#include + +#include "glib-abi.h" +#include "dbus-abi.h" +#include "hal-abi.h" + +#define HAL_PROPERTY "org.freedesktop.Hal.Device.LaptopPanel" + +static const char prop_name[] = HAL_PROPERTY; +static const char prop_invalid[] = HAL_PROPERTY ".Invalid"; +static const char prop_set[] = "SetBrightness"; +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 levels; + +/** + * Get LCD backlight level + * + * Tries to read from the specified sysfs device, and if successful, + * updates the internal LCD backlight state. Warns if the device + * cannot be opened or be read from. Always returns the last succesful + * read. + */ +static int get_lcd_backlight() +{ + static int level; + int fd; + char buf[64]; + + fd = open(sysfs_path, O_RDONLY); + if (fd < 0) { + HAL_WARNING(("Could not open '%s'", sysfs_path)); + return level; + } + + memset(buf, 0, sizeof(buf)); + if (read(fd, buf, sizeof(buf)) < 0) + HAL_WARNING(("Could not read from '%s'", sysfs_path)); + else + level = atoi(buf); + + close(fd); + return level; +} + +/** + * Set LCD backlight level + * + * Warns if the device cannot be opened or be written to. + * Returns bytes written on success, negative value on failure. + */ +static int set_lcd_backlight(int level) +{ + int fd, ret; + char buf[64]; + + sprintf(buf, "%d", level); + + fd = open(sysfs_path, O_WRONLY); + if (fd < 0) { + HAL_WARNING(("Could not open '%s'", sysfs_path)); + return -1; + } + + ret = write(fd, buf, strlen(buf)); + if (ret < 0) + HAL_WARNING(("Could not write '%s' to '%s'", buf, sysfs_path)); + + close(fd); + return ret; +} + +/* DBus set LCD backlight level */ +static struct DBusMessage *dbus_get_lcd_backlight(struct DBusMessage *msg) +{ + struct DBusError err; + struct DBusMessage *reply; + int level; + + dbus_error_init(&err); + if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INVALID)) + return dbus_message_new_error(msg, prop_invalid, + "Invalid message"); + + level = get_lcd_backlight(); + + reply = dbus_message_new_method_return(msg); + if (reply) + dbus_message_append_args(reply, DBUS_TYPE_INT32, &level, + DBUS_TYPE_INVALID); + + return reply; +} + +/* DBus set LCD backlight level */ +static struct DBusMessage *dbus_set_lcd_backlight(struct DBusMessage *msg) +{ + struct DBusError err; + int level; + + dbus_error_init(&err); + if (!dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &level, + DBUS_TYPE_INVALID)) + return dbus_message_new_error(msg, prop_invalid, + "Brightness level argument missing"); + + if (level < 0 || level > levels - 1) + return dbus_message_new_error(msg, prop_invalid, + "Brightness level is invalid"); + + set_lcd_backlight(level); + + return dbus_message_new_method_return(msg); +} + +/* 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_lcd_backlight(message); + else if (dbus_message_is_method_call(message, prop_name, prop_set)) + reply = dbus_set_lcd_backlight(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 *level_str = getenv("HAL_PROP_LAPTOP_PANEL_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 (level_str == NULL) { + HAL_ERROR(("No laptop_panel.num_levels defined")); + return -2; + } + + levels = atoi(level_str); + snprintf(sysfs_path, sizeof(sysfs_path), "%s/brightness", path); + + HAL_DEBUG(("udi='%s', path='%s', levels='%d'", udi, path, levels)); + + 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, + " \n" + " \n" + " \n" + " \n" + " \n" + " \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; +} + -- cgit v1.2.3