[Pommed-commits] r340 - in trunk: . pommed pommed/mactel pommed/pmac

jblache at alioth.debian.org jblache at alioth.debian.org
Thu Jul 26 15:23:27 UTC 2007


Author: jblache
Date: 2007-07-26 15:23:27 +0000 (Thu, 26 Jul 2007)
New Revision: 340

Added:
   trunk/pommed/mactel/acpi.c
   trunk/pommed/pmac/pmu.c
   trunk/pommed/power.c
   trunk/pommed/power.h
Modified:
   trunk/ChangeLog
   trunk/pommed.conf.mactel
   trunk/pommed.conf.pmac
   trunk/pommed/Makefile
   trunk/pommed/conffile.c
   trunk/pommed/conffile.h
   trunk/pommed/lcd_backlight.h
   trunk/pommed/mactel/gma950_backlight.c
   trunk/pommed/mactel/x1600_backlight.c
   trunk/pommed/pmac/sysfs_backlight.c
   trunk/pommed/pommed.c
   trunk/pommed/pommed.h
Log:
Add automatic LCD backlight dimming when switching from AC to battery power.

Support for both mactel & pmac (PMU at least).


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/ChangeLog	2007-07-26 15:23:27 UTC (rev 340)
@@ -4,6 +4,8 @@
 version 1.8:
 	- pommed: probe for the applesmc sysfs path in the ambient light
 	handling code. The path has changed in Linux 2.6.22.
+	- pommed: added automatic LCD backlight dimming when switching
+	from AC to battery power (and vice-versa).
 
 version 1.7:
 	- pommed: add partial support for the MacBookPro3,1 (Core2 Duo,

Modified: trunk/pommed/Makefile
===================================================================
--- trunk/pommed/Makefile	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/Makefile	2007-07-26 15:23:27 UTC (rev 340)
@@ -18,8 +18,8 @@
 
 LDFLAGS = -lz $(DBUS_LIBS) $(ALSA_LIBS) $(CONFUSE_LIBS)
 
-SOURCES = pommed.c cd_eject.c evdev.c conffile.c audio.c dbus.c \
-		pmac/sysfs_backlight.c \
+SOURCES = pommed.c cd_eject.c evdev.c conffile.c audio.c dbus.c power.c \
+		pmac/sysfs_backlight.c pmac/pmu.c \
 		pmac/kbd_backlight.c pmac/ambient.c
 
 OF_SOURCES = pmac/ofapi/of_externals.c pmac/ofapi/of_internals.c \
@@ -38,9 +38,9 @@
 
 LDFLAGS = -lz -lsmbios $(DBUS_LIBS) $(ALSA_LIBS) $(CONFUSE_LIBS)
 
-SOURCES = pommed.c cd_eject.c evdev.c conffile.c audio.c dbus.c \
+SOURCES = pommed.c cd_eject.c evdev.c conffile.c audio.c dbus.c power.c \
 		mactel/x1600_backlight.c mactel/gma950_backlight.c \
-		mactel/kbd_backlight.c mactel/ambient.c
+		mactel/kbd_backlight.c mactel/ambient.c mactel/acpi.c
 
 LIBS = /usr/lib/libpci.a
 endif
@@ -62,7 +62,9 @@
 
 dbus.o: dbus.c dbus.h pommed.h lcd_backlight.h kbd_backlight.h ambient.h audio.h
 
+power.o: power.c power.h pommed.h lcd_backlight.h
 
+
 # PowerMac-specific files
 pmac/kbd_backlight.o: pmac/kbd_backlight.c kbd_auto.c kbd_backlight.h pommed.h ambient.h conffile.h dbus.h
 
@@ -70,6 +72,8 @@
 
 pmac/ambient.o: pmac/ambient.c ambient.h pommed.h dbus.h
 
+pmac/pmu.o: pmac/pmu.c power.h
+
 # OFlib
 pmac/ofapi/oflib.a: $(OF_OBJS)
 	ar cru $@ $^
@@ -90,7 +94,9 @@
 
 mactel/ambient.o: mactel/ambient.c ambient.h pommed.h dbus.h
 
+mactel/acpi.o: mactel/acpi.c power.h
 
+
 clean:
 	rm -f pommed $(OBJS) $(OF_OBJS) pmac/ofapi/oflib.a
 	rm -f *~ mactel/*~ pmac/*~ pmac/ofapi/*~

Modified: trunk/pommed/conffile.c
===================================================================
--- trunk/pommed/conffile.c	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/conffile.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -62,6 +62,7 @@
   {
     CFG_INT("init", -1, CFGF_NONE),
     CFG_INT("step", 8, CFGF_NONE),
+    CFG_INT("on_batt", 0, CFGF_NONE),
     CFG_END()
   };
 
@@ -80,6 +81,7 @@
   {
     CFG_INT("init", -1, CFGF_NONE),
     CFG_INT("step", 10, CFGF_NONE),
+    CFG_INT("on_batt", 0, CFGF_NONE),
     CFG_END()
   };
 
@@ -87,6 +89,7 @@
   {
     CFG_INT("init", -1, CFGF_NONE),
     CFG_INT("step", 0x0f, CFGF_NONE),
+    CFG_INT("on_batt", 0, CFGF_NONE),
     CFG_END()
   };
 
@@ -186,13 +189,16 @@
   printf(" + sysfs backlight control:\n");
   printf("    initial level: %d\n", lcd_sysfs_cfg.init);
   printf("    step: %d\n", lcd_sysfs_cfg.step);
+  printf("    on_batt: %d\n", lcd_sysfs_cfg.on_batt);
 #else
   printf(" + ATI X1600 backlight control:\n");
   printf("    initial level: %d\n", lcd_x1600_cfg.init);
   printf("    step: %d\n", lcd_x1600_cfg.step);
+  printf("    on_batt: %d\n", lcd_x1600_cfg.on_batt);
   printf(" + Intel GMA950 backlight control:\n");
   printf("    initial level: 0x%x\n", lcd_gma950_cfg.init);
   printf("    step: 0x%x\n", lcd_gma950_cfg.step);
+  printf("    on_batt: 0x%x\n", lcd_gma950_cfg.on_batt);
 #endif /* __powerpc__ */
   printf(" + Audio volume control:\n");
   printf("    card: %s\n", audio_cfg.card);
@@ -238,13 +244,16 @@
   /* general */
   cfg_set_validate_func(cfg, "general|fnmode", config_validate_positive_integer);
 #ifdef __powerpc__
-  /* lcd_r9600 */
+  /* lcd_sysfs */
   cfg_set_validate_func(cfg, "lcd_sysfs|step", config_validate_positive_integer);
+  cfg_set_validate_func(cfg, "lcd_sysfs|on_batt", config_validate_positive_integer);
 #else
   /* lcd_x1600 */
   cfg_set_validate_func(cfg, "lcd_x1600|step", config_validate_positive_integer);
+  cfg_set_validate_func(cfg, "lcd_x1600|on_batt", config_validate_positive_integer);
   /* lcd_gma950 */
   cfg_set_validate_func(cfg, "lcd_gma950|step", config_validate_positive_integer);
+  cfg_set_validate_func(cfg, "lcd_gma950|on_batt", config_validate_positive_integer);
 #endif /* __powerpc__ */
   /* audio */
   cfg_set_validate_func(cfg, "audio|card", config_validate_string);
@@ -290,16 +299,19 @@
   sec = cfg_getsec(cfg, "lcd_sysfs");
   lcd_sysfs_cfg.init = cfg_getint(sec, "init");
   lcd_sysfs_cfg.step = cfg_getint(sec, "step");
+  lcd_sysfs_cfg.on_batt = cfg_getint(sec, "on_batt");
   /* No _fix_config() call here, it's done at probe time */
 #else
   sec = cfg_getsec(cfg, "lcd_x1600");
   lcd_x1600_cfg.init = cfg_getint(sec, "init");
   lcd_x1600_cfg.step = cfg_getint(sec, "step");
+  lcd_x1600_cfg.on_batt = cfg_getint(sec, "on_batt");
   x1600_backlight_fix_config();
 
   sec = cfg_getsec(cfg, "lcd_gma950");
   lcd_gma950_cfg.init = cfg_getint(sec, "init");
   lcd_gma950_cfg.step = cfg_getint(sec, "step");
+  lcd_gma950_cfg.on_batt = cfg_getint(sec, "on_batt");
   /* No _fix_config() call here, as we're hardware-dependent
    * for the max backlight value */
 #endif /* __powerpc__ */

Modified: trunk/pommed/conffile.h
===================================================================
--- trunk/pommed/conffile.h	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/conffile.h	2007-07-26 15:23:27 UTC (rev 340)
@@ -13,6 +13,7 @@
 struct _lcd_sysfs_cfg {
   int init;
   int step;
+  int on_batt;
 };
 
 #else
@@ -20,11 +21,13 @@
 struct _lcd_x1600_cfg {
   int init;
   int step;
+  int on_batt;
 };
 
 struct _lcd_gma950_cfg {
   unsigned int init;
   unsigned int step;
+  unsigned int on_batt;
 };
 #endif /* __powerpc__ */
 
@@ -60,7 +63,6 @@
 extern struct _general_cfg general_cfg;
 #ifdef __powerpc__
 extern struct _lcd_sysfs_cfg lcd_sysfs_cfg;
-extern struct _lcd_r128_cfg lcd_r128_cfg;
 #else
 extern struct _lcd_x1600_cfg lcd_x1600_cfg;
 extern struct _lcd_gma950_cfg lcd_gma950_cfg;

Modified: trunk/pommed/lcd_backlight.h
===================================================================
--- trunk/pommed/lcd_backlight.h	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/lcd_backlight.h	2007-07-26 15:23:27 UTC (rev 340)
@@ -9,11 +9,17 @@
 struct _lcd_bck_info
 {
   int level;
+  int ac_lvl;
   int max;
 };
 
 extern struct _lcd_bck_info lcd_bck_info;
 
+
+#define LCD_ON_AC_LEVEL    0
+#define LCD_ON_BATT_LEVEL  1
+
+
 #ifndef __powerpc__
 /* x1600_backlight.c */
 #define X1600_BACKLIGHT_OFF       0
@@ -22,6 +28,9 @@
 void
 x1600_backlight_step(int dir);
 
+void
+x1600_backlight_toggle(int lvl);
+
 int
 x1600_backlight_probe(void);
 
@@ -36,6 +45,9 @@
 void
 gma950_backlight_step(int dir);
 
+void
+gma950_backlight_toggle(int lvl);
+
 int
 gma950_backlight_probe(void);
 
@@ -50,8 +62,14 @@
 sysfs_backlight_step(int dir);
 
 void
+sysfs_backlight_toggle(int lvl);
+
+void
 sysfs_backlight_step_kernel(int dir);
 
+void
+sysfs_backlight_toggle_kernel(int lvl);
+
 int
 aty128_sysfs_backlight_probe(void);
 

Added: trunk/pommed/mactel/acpi.c
===================================================================
--- trunk/pommed/mactel/acpi.c	                        (rev 0)
+++ trunk/pommed/mactel/acpi.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -0,0 +1,72 @@
+/*
+ * pommed - Apple laptops hotkeys handler daemon
+ *
+ * $Id$
+ *
+ * Copyright (C) 2006-2007 Julien BLACHE <jb at jblache.org>
+ *
+ * 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; version 2 of the License.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "../pommed.h"
+#include "../power.h"
+
+
+#define ACPI_AC_STATE   "/proc/acpi/ac_adapter/ADP1/state"
+#define ACPI_AC_ONLINE  "on-line\n"
+#define ACPI_AC_OFFLINE "off-line\n"
+
+
+/* Internal API */
+int
+check_ac_state(void)
+{
+  FILE *fp;
+  char buf[128];
+  int ret;
+
+  fp = fopen(ACPI_AC_STATE, "r");
+  if (fp == NULL)
+    return AC_STATE_ERROR;
+
+  ret = fread(buf, 1, 127, fp);
+
+  if (ferror(fp) != 0)
+    {
+      logdebug("acpi: Error reading AC state: %s\n", strerror(errno));
+      return AC_STATE_ERROR;
+    }
+
+  if (feof(fp) == 0)
+    {
+      logdebug("acpi: Error reading AC state: buffer too small\n");
+      return AC_STATE_ERROR;
+    }
+
+  fclose(fp);
+
+  buf[ret] = '\0';
+
+  if (strstr(buf, ACPI_AC_ONLINE) != NULL)
+    return AC_STATE_ONLINE;
+
+  if (strstr(buf, ACPI_AC_OFFLINE) != NULL)
+    return AC_STATE_OFFLINE;
+
+  return AC_STATE_UNKNOWN;
+}


Property changes on: trunk/pommed/mactel/acpi.c
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: trunk/pommed/mactel/gma950_backlight.c
===================================================================
--- trunk/pommed/mactel/gma950_backlight.c	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/mactel/gma950_backlight.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -207,6 +207,41 @@
 }
 
 
+void
+gma950_backlight_toggle(int lvl)
+{
+  int ret;
+
+  if (lcd_gma950_cfg.on_batt == 0)
+    return;
+
+  ret = gma950_backlight_map();
+  if (ret < 0)
+    return;
+
+  switch (lvl)
+    {
+      case LCD_ON_AC_LEVEL:
+	logdebug("LCD switching to AC level\n");
+	gma950_backlight_set(lcd_bck_info.ac_lvl);
+	lcd_bck_info.level = lcd_bck_info.ac_lvl;
+	break;
+
+      case LCD_ON_BATT_LEVEL:
+	logdebug("LCD switching to battery level\n");
+	lcd_bck_info.ac_lvl = lcd_bck_info.level;
+	if (lcd_bck_info.level > lcd_gma950_cfg.on_batt)
+	  {
+	    gma950_backlight_set(lcd_gma950_cfg.on_batt);
+	    lcd_bck_info.level = lcd_gma950_cfg.on_batt;
+	  }
+	break;
+    }
+
+  gma950_backlight_unmap();
+}
+
+
 /*
  * We are hardware-dependent for GMA950_BACKLIGHT_MAX,
  * so here _fix_config() is static and called at probe time.
@@ -229,6 +264,10 @@
 
   if (lcd_gma950_cfg.step > 0x20)
     lcd_gma950_cfg.step = 0x20;
+
+  if ((lcd_gma950_cfg.on_batt > GMA950_BACKLIGHT_MAX)
+      || (lcd_gma950_cfg.on_batt < GMA950_BACKLIGHT_MIN))
+    lcd_gma950_cfg.on_batt = 0;
 }
 
 
@@ -294,6 +333,7 @@
 
   lcd_bck_info.max = GMA950_BACKLIGHT_MAX;
   lcd_bck_info.level = gma950_backlight_get();
+  lcd_bck_info.ac_lvl = lcd_bck_info.level;
 
   gma950_backlight_unmap();
 

Modified: trunk/pommed/mactel/x1600_backlight.c
===================================================================
--- trunk/pommed/mactel/x1600_backlight.c	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/mactel/x1600_backlight.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -174,7 +174,41 @@
   lcd_bck_info.level = newval;
 }
 
+void
+x1600_backlight_toggle(int lvl)
+{
+  int ret;
 
+  if (lcd_x1600_cfg.on_batt == 0)
+    return;
+
+  ret = x1600_backlight_map();
+  if (ret < 0)
+    return;
+
+  switch (lvl)
+    {
+      case LCD_ON_AC_LEVEL:
+	logdebug("LCD switching to AC level\n");
+	x1600_backlight_set(lcd_bck_info.ac_lvl);
+	lcd_bck_info.level = lcd_bck_info.ac_lvl;
+	break;
+
+      case LCD_ON_BATT_LEVEL:
+	logdebug("LCD switching to battery level\n");
+	lcd_bck_info.ac_lvl = lcd_bck_info.level;
+	if (lcd_bck_info.level > lcd_x1600_cfg.on_batt)
+	  {
+	    x1600_backlight_set(lcd_x1600_cfg.on_batt);
+	    lcd_bck_info.level = lcd_x1600_cfg.on_batt;
+	  }
+	break;
+    }
+
+  x1600_backlight_unmap();
+}
+
+
 #define PCI_ID_VENDOR_ATI        0x1002
 #define PCI_ID_PRODUCT_X1600     0x71c5
 
@@ -238,6 +272,7 @@
     }
 
   lcd_bck_info.level = x1600_backlight_get();
+  lcd_bck_info.ac_lvl = lcd_bck_info.level;
 
   x1600_backlight_unmap();
 
@@ -259,4 +294,8 @@
 
   if (lcd_x1600_cfg.step > (X1600_BACKLIGHT_MAX / 2))
     lcd_x1600_cfg.step = X1600_BACKLIGHT_MAX / 2;
+
+  if ((lcd_x1600_cfg.on_batt > X1600_BACKLIGHT_MAX)
+      || (lcd_x1600_cfg.on_batt < X1600_BACKLIGHT_OFF))
+    lcd_x1600_cfg.on_batt = 0;
 }

Added: trunk/pommed/pmac/pmu.c
===================================================================
--- trunk/pommed/pmac/pmu.c	                        (rev 0)
+++ trunk/pommed/pmac/pmu.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -0,0 +1,82 @@
+/*
+ * pommed - Apple laptops hotkeys handler daemon
+ *
+ * $Id$
+ *
+ * Copyright (C) 2006-2007 Julien BLACHE <jb at jblache.org>
+ *
+ * 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; version 2 of the License.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "../pommed.h"
+#include "../power.h"
+
+
+#define PMU_AC_STATE_FILE  "/proc/pmu/info"
+#define PMU_AC_STATE       "AC Power"
+#define PMU_AC_ONLINE      '1'
+#define PMU_AC_OFFLINE     '0'
+
+
+/* Internal API */
+int
+check_ac_state(void)
+{
+  FILE *fp;
+  char buf[128];
+  char *ac_state;
+  int ret;
+
+  fp = fopen(PMU_AC_STATE_FILE, "r");
+  if (fp == NULL)
+    return AC_STATE_ERROR;
+
+  ret = fread(buf, 1, 127, fp);
+
+  if (ferror(fp) != 0)
+    {
+      logdebug("pmu: Error reading AC state: %s\n", strerror(errno));
+      return AC_STATE_ERROR;
+    }
+
+  if (feof(fp) == 0)
+    {
+      logdebug("pmu: Error reading AC state: buffer too small\n");
+      return AC_STATE_ERROR;
+    }
+
+  fclose(fp);
+
+  buf[ret] = '\0';
+
+  ac_state = strstr(buf, PMU_AC_STATE);
+  if (ac_state == NULL)
+    return AC_STATE_ERROR;
+
+  ac_state = strchr(ac_state, '\n');
+  if ((ac_state == NULL) || (ac_state == buf))
+    return AC_STATE_ERROR;
+
+  if (ac_state[-1] == PMU_AC_ONLINE)
+    return AC_STATE_ONLINE;
+
+  if (ac_state[-1] == PMU_AC_OFFLINE)
+    return AC_STATE_OFFLINE;
+
+  return AC_STATE_UNKNOWN;
+}


Property changes on: trunk/pommed/pmac/pmu.c
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: trunk/pommed/pmac/sysfs_backlight.c
===================================================================
--- trunk/pommed/pmac/sysfs_backlight.c	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/pmac/sysfs_backlight.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -198,6 +198,33 @@
 }
 
 
+void
+sysfs_backlight_toggle(int lvl)
+{
+  if (lcd_sysfs_cfg.on_batt == 0)
+    return;
+
+  switch (lvl)
+    {
+      case LCD_ON_AC_LEVEL:
+	logdebug("LCD switching to AC level\n");
+	sysfs_backlight_set(lcd_bck_info.ac_lvl);
+	lcd_bck_info.level = lcd_bck_info.ac_lvl;
+	break;
+
+      case LCD_ON_BATT_LEVEL:
+	logdebug("LCD switching to battery level\n");
+	lcd_bck_info.ac_lvl = lcd_bck_info.level;
+	if (lcd_bck_info.level > lcd_sysfs_cfg.on_batt)
+	  {
+	    sysfs_backlight_set(lcd_sysfs_cfg.on_batt);
+	    lcd_bck_info.level = lcd_sysfs_cfg.on_batt;
+	  }
+	break;
+    }
+}
+
+
 /* When brightness keys are handled by the kernel itself,
  * we're only updating our internal buffers
  */
@@ -215,7 +242,13 @@
   lcd_bck_info.level = val;
 }
 
+void
+sysfs_backlight_toggle_kernel(int lvl)
+{
+  return;
+}
 
+
 /* We can't fix the config until we know the max backlight value,
  * so, here, fix_config() is static and called at probe time
  */
@@ -233,6 +266,10 @@
 
   if (lcd_sysfs_cfg.step > (lcd_bck_info.max / 2))
     lcd_sysfs_cfg.step = lcd_bck_info.max / 2;
+
+  if ((lcd_sysfs_cfg.on_batt > lcd_bck_info.max)
+      || (lcd_sysfs_cfg.on_batt < SYSFS_BACKLIGHT_OFF))
+    lcd_sysfs_cfg.on_batt = 0;
 }
 
 
@@ -275,6 +312,7 @@
     }
 
   lcd_bck_info.level = sysfs_backlight_get();
+  lcd_bck_info.ac_lvl = lcd_bck_info.level;
 
   return 0;
 }

Modified: trunk/pommed/pommed.c
===================================================================
--- trunk/pommed/pommed.c	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/pommed.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -53,6 +53,7 @@
 #include "conffile.h"
 #include "audio.h"
 #include "dbus.h"
+#include "power.h"
 
 
 /* Machine-specific operations */
@@ -74,6 +75,7 @@
     .type = MACHINE_POWERBOOK_32,
     .lcd_backlight_probe = aty128_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step_kernel,
+    .lcd_backlight_toggle = sysfs_backlight_toggle_kernel,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -81,6 +83,7 @@
     .type = MACHINE_POWERBOOK_33,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -88,6 +91,7 @@
     .type = MACHINE_POWERBOOK_34,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -95,6 +99,7 @@
     .type = MACHINE_POWERBOOK_35,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -104,6 +109,7 @@
     .type = MACHINE_POWERBOOK_51,
     .lcd_backlight_probe = nvidia_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -111,6 +117,7 @@
     .type = MACHINE_POWERBOOK_52,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -118,6 +125,7 @@
     .type = MACHINE_POWERBOOK_53,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -125,6 +133,7 @@
     .type = MACHINE_POWERBOOK_54,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -132,6 +141,7 @@
     .type = MACHINE_POWERBOOK_55,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -139,6 +149,7 @@
     .type = MACHINE_POWERBOOK_56,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_fountain,
   },
 
@@ -146,6 +157,7 @@
     .type = MACHINE_POWERBOOK_57,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_fountain,
   },
 
@@ -153,6 +165,7 @@
     .type = MACHINE_POWERBOOK_58,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_geyser,
   },
 
@@ -160,6 +173,7 @@
     .type = MACHINE_POWERBOOK_59,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_geyser,
   },
 
@@ -169,6 +183,7 @@
     .type = MACHINE_POWERBOOK_61,
     .lcd_backlight_probe = nvidia_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -176,6 +191,7 @@
     .type = MACHINE_POWERBOOK_62,
     .lcd_backlight_probe = nvidia_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -183,6 +199,7 @@
     .type = MACHINE_POWERBOOK_63,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -190,6 +207,7 @@
     .type = MACHINE_POWERBOOK_64,
     .lcd_backlight_probe = nvidia_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -197,6 +215,7 @@
     .type = MACHINE_POWERBOOK_65,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -206,6 +225,7 @@
     .type = MACHINE_POWERBOOK_67,
     .lcd_backlight_probe = r9x00_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   },
 
@@ -213,6 +233,7 @@
     .type = MACHINE_POWERBOOK_68,
     .lcd_backlight_probe = nvidia_sysfs_backlight_probe,
     .lcd_backlight_step = sysfs_backlight_step,
+    .lcd_backlight_toggle = sysfs_backlight_toggle,
     .evdev_identify = evdev_is_adb,
   }
 };
@@ -229,6 +250,11 @@
   return;
 }
 
+void dummy_backlight_toggle(int lvl)
+{
+  return;
+}
+
 struct machine_ops mb_mops[] = {
   /* MacBook Pro machines */
 
@@ -236,6 +262,7 @@
     .type = MACHINE_MACBOOKPRO_1,
     .lcd_backlight_probe = x1600_backlight_probe,
     .lcd_backlight_step = x1600_backlight_step,
+    .lcd_backlight_toggle = x1600_backlight_toggle,
     .evdev_identify = evdev_is_geyser3,
   },
 
@@ -243,6 +270,7 @@
     .type = MACHINE_MACBOOKPRO_2,
     .lcd_backlight_probe = x1600_backlight_probe,
     .lcd_backlight_step = x1600_backlight_step,
+    .lcd_backlight_toggle = x1600_backlight_toggle,
     .evdev_identify = evdev_is_geyser4,
   },
 
@@ -251,6 +279,7 @@
     /* dummy backlight operations for now - nVidia support needed */
     .lcd_backlight_probe = dummy_backlight_probe,
     .lcd_backlight_step = dummy_backlight_step,
+    .lcd_backlight_toggle = dummy_backlight_toggle,
     .evdev_identify = evdev_is_geyser4,
   },
 
@@ -260,6 +289,7 @@
     .type = MACHINE_MACBOOK_1,
     .lcd_backlight_probe = gma950_backlight_probe,
     .lcd_backlight_step = gma950_backlight_step,
+    .lcd_backlight_toggle = gma950_backlight_toggle,
     .evdev_identify = evdev_is_geyser3,
   },
 
@@ -267,6 +297,7 @@
     .type = MACHINE_MACBOOK_2,
     .lcd_backlight_probe = gma950_backlight_probe,
     .lcd_backlight_step = gma950_backlight_step,
+    .lcd_backlight_toggle = gma950_backlight_toggle,
     .evdev_identify = evdev_is_geyser4,
   }
 };
@@ -718,6 +749,8 @@
       logmsg(LOG_WARNING, "Could not connect to DBus system bus");
     }
 
+  power_init();
+
   if (!console)
     {
       /*
@@ -803,36 +836,45 @@
 		evdev_process_events(fds[i].fd);
 	    }
 
-	  if (has_kbd_backlight())
+	  /* is it time to chek the ambient light sensors and AC state ? */
+	  gettimeofday(&tv_now, NULL);
+	  tv_diff.tv_sec = tv_now.tv_sec - tv_als.tv_sec;
+	  if (tv_diff.tv_sec < 0)
+	    tv_diff.tv_sec = 0;
+
+	  if (tv_diff.tv_sec == 0)
 	    {
-	      /* is it time to chek the ambient light sensors ? */
-	      gettimeofday(&tv_now, NULL);
-	      tv_diff.tv_sec = tv_now.tv_sec - tv_als.tv_sec;
-	      if (tv_diff.tv_sec < 0)
-		tv_diff.tv_sec = 0;
+	      tv_diff.tv_usec = tv_now.tv_usec - tv_als.tv_usec;
+	    }
+	  else
+	    {
+	      tv_diff.tv_sec--;
+	      tv_diff.tv_usec = 1000000 - tv_als.tv_usec + tv_now.tv_usec;
+	      tv_diff.tv_usec += tv_diff.tv_sec * 1000000;
+	    }
 
-	      if (tv_diff.tv_sec == 0)
+	  if (tv_diff.tv_usec >= (1000 * LOOP_TIMEOUT))
+	    {
+	      if (has_kbd_backlight())
 		{
-		  tv_diff.tv_usec = tv_now.tv_usec - tv_als.tv_usec;
+		  kbd_backlight_ambient_check();
 		}
-	      else
-		{
-		  tv_diff.tv_sec--;
-		  tv_diff.tv_usec = 1000000 - tv_als.tv_usec + tv_now.tv_usec;
-		  tv_diff.tv_usec += tv_diff.tv_sec * 1000000;
-		}
 
-	      if (tv_diff.tv_usec >= (1000 * LOOP_TIMEOUT))
-		{
-		  kbd_backlight_ambient_check();
-		  tv_als = tv_now;
-		}
+	      power_check_ac_state();
+
+	      tv_als = tv_now;
 	    }
 	}
-      else if (has_kbd_backlight())
+      else
 	{
-	  /* poll() timed out, check ambient light sensors */
-	  kbd_backlight_ambient_check();
+	  /* poll() timed out, check ambient light sensors and AC state */
+	  if (has_kbd_backlight())
+	    {
+	      kbd_backlight_ambient_check();
+	    }
+
+	  power_check_ac_state();
+
 	  gettimeofday(&tv_als, NULL);
 	}
 

Modified: trunk/pommed/pommed.h
===================================================================
--- trunk/pommed/pommed.h	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed/pommed.h	2007-07-26 15:23:27 UTC (rev 340)
@@ -67,6 +67,7 @@
   machine_type type;
   int (*lcd_backlight_probe) (void);
   void (*lcd_backlight_step) (int dir);
+  void (*lcd_backlight_toggle) (int lvl);
   int (*evdev_identify) (unsigned short *id);
 };
 

Added: trunk/pommed/power.c
===================================================================
--- trunk/pommed/power.c	                        (rev 0)
+++ trunk/pommed/power.c	2007-07-26 15:23:27 UTC (rev 340)
@@ -0,0 +1,78 @@
+/*
+ * pommed - Apple laptops hotkeys handler daemon
+ *
+ * $Id$
+ *
+ * Copyright (C) 2006-2007 Julien BLACHE <jb at jblache.org>
+ *
+ * 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; version 2 of the License.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+
+#include <syslog.h>
+
+#include "pommed.h"
+#include "lcd_backlight.h"
+#include "power.h"
+
+
+/* Internal API */
+int
+check_ac_state(void);
+
+
+static int prev_state;
+
+
+void
+power_check_ac_state(void)
+{
+  int ac_state;
+
+  ac_state = check_ac_state();
+
+  if (ac_state == prev_state)
+    return;
+  else
+      prev_state = ac_state;
+
+  switch (ac_state)
+    {
+      case AC_STATE_ONLINE:
+	logdebug("power: switched to AC\n");
+	mops->lcd_backlight_toggle(LCD_ON_AC_LEVEL);
+	break;
+
+      case AC_STATE_OFFLINE:
+	logdebug("power: switched to battery\n");
+	mops->lcd_backlight_toggle(LCD_ON_BATT_LEVEL);
+	break;
+
+      case AC_STATE_ERROR:
+	logmsg(LOG_ERR, "power: error reading AC state");
+	break;
+
+      case AC_STATE_UNKNOWN:
+	logmsg(LOG_INFO, "power: unknown AC state");
+	break;
+    }
+}
+
+
+void
+power_init(void)
+{
+  prev_state = check_ac_state();
+}


Property changes on: trunk/pommed/power.c
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/pommed/power.h
===================================================================
--- trunk/pommed/power.h	                        (rev 0)
+++ trunk/pommed/power.h	2007-07-26 15:23:27 UTC (rev 340)
@@ -0,0 +1,25 @@
+/*
+ * $Id$
+ */
+
+#ifndef __POWER_H__
+#define __POWER_H__
+
+
+#define AC_STATE_ERROR   -1
+#define AC_STATE_UNKNOWN -2
+
+#define AC_STATE_ONLINE   1
+#define AC_STATE_OFFLINE  0
+
+
+void
+power_check_ac_state(void);
+
+
+void
+power_init(void);
+
+
+#endif /* !__POWER_H__ */
+


Property changes on: trunk/pommed/power.h
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: trunk/pommed.conf.mactel
===================================================================
--- trunk/pommed.conf.mactel	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed.conf.mactel	2007-07-26 15:23:27 UTC (rev 340)
@@ -15,6 +15,8 @@
 	init = -1
 	# step value (1 - 127)
 	step = 10
+	# backlight level when on battery [80] (1 - 255, 0 to disable)
+	on_batt = 80
 }
 
 # Intel GMA950 backlight control (MacBook)
@@ -23,6 +25,8 @@
 	init = -1
 	# step value (0x01 - 0x20)
 	step = 0x0f
+	# backlight level when on battery [0x40] (0x1f - 0x94 usually, 0 to disable)
+	on_batt = 0x40
 }
 
 # Audio support

Modified: trunk/pommed.conf.pmac
===================================================================
--- trunk/pommed.conf.pmac	2007-07-15 20:23:28 UTC (rev 339)
+++ trunk/pommed.conf.pmac	2007-07-26 15:23:27 UTC (rev 340)
@@ -19,11 +19,14 @@
 	init = -1
 	# step value (1 - 63)
 	step = 8
+	# backlight level when on battery [40] (1 - 127, 0 to disable)
+	on_batt = 40
 
 	# WARNING
 	# On some machines, the backlight is handled by the kernel, so
-	# the "step" configuration value is useless, as pommed doesn't
-	# get to set the backlight level when you press on the keys.
+	# the "step" and "on_batt" configuration values are useless, as
+	# pommed doesn't get to set the backlight level when you press
+	# on the keys.
 }
 
 # Audio support




More information about the Pommed-commits mailing list