Module Name:    src
Committed By:   macallan
Date:           Thu Feb  6 02:17:24 UTC 2020

Modified Files:
        src/sys/arch/macppc/dev: lmu.c

Log Message:
listen to PMF events in order to turn off keyboard lights when the lid is
closed or the screen is blanked


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/macppc/dev/lmu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/macppc/dev/lmu.c
diff -u src/sys/arch/macppc/dev/lmu.c:1.1 src/sys/arch/macppc/dev/lmu.c:1.2
--- src/sys/arch/macppc/dev/lmu.c:1.1	Fri Jan 10 06:24:17 2020
+++ src/sys/arch/macppc/dev/lmu.c	Thu Feb  6 02:17:24 2020
@@ -1,4 +1,4 @@
- /* $NetBSD: lmu.c,v 1.1 2020/01/10 06:24:17 macallan Exp $ */
+ /* $NetBSD: lmu.c,v 1.2 2020/02/06 02:17:24 macallan Exp $ */
 
 /*-
  * Copyright (c) 2020 Michael Lorenz
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: lmu.c,v 1.1 2020/01/10 06:24:17 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lmu.c,v 1.2 2020/02/06 02:17:24 macallan Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -56,7 +56,8 @@ struct lmu_softc {
 	struct sysmon_envsys *sc_sme;
 	envsys_data_t	sc_sensors[2];
 	callout_t	sc_adjust;
-	int sc_thresh, sc_hyst;
+	int sc_thresh, sc_hyst, sc_level;
+	int		sc_lid_state, sc_video_state;
 };
 
 static int	lmu_match(device_t, cfdata_t, void *);
@@ -76,6 +77,38 @@ static const struct device_compatible_en
 	{ NULL,			0 }
 };
 
+static void
+lmu_lid_open(device_t dev)
+{
+	struct lmu_softc * const sc = device_private(dev);
+
+	sc->sc_lid_state = true;
+}
+
+static void
+lmu_lid_close(device_t dev)
+{
+	struct lmu_softc * const sc = device_private(dev);
+
+	sc->sc_lid_state = false;
+}
+
+static void
+lmu_video_on(device_t dev)
+{
+	struct lmu_softc * const sc = device_private(dev);
+
+	sc->sc_video_state = true;
+}
+
+static void
+lmu_video_off(device_t dev)
+{
+	struct lmu_softc * const sc = device_private(dev);
+
+	sc->sc_video_state = false;
+}
+
 static int
 lmu_match(device_t parent, cfdata_t match, void *aux)
 {
@@ -103,6 +136,17 @@ lmu_attach(device_t parent, device_t sel
 	aprint_naive("\n");
 	aprint_normal(": ambient light sensor\n");
 
+	sc->sc_lid_state = true;
+	pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_OPEN,
+	    lmu_lid_open, true);
+	pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_CLOSE,
+	    lmu_lid_close, true);
+	sc->sc_video_state = true;
+	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
+	    lmu_video_on, true);
+	pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
+	    lmu_video_off, true);
+
 	sc->sc_sme = sysmon_envsys_create();
 	sc->sc_sme->sme_name = device_xname(self);
 	sc->sc_sme->sme_cookie = sc;
@@ -127,6 +171,7 @@ lmu_attach(device_t parent, device_t sel
 	/* TODO: make this adjustable via sysctl */
 	sc->sc_thresh = 300;
 	sc->sc_hyst = 30;
+	sc->sc_level = 100;
 
 	callout_init(&sc->sc_adjust, 0);
 	callout_setfunc(&sc->sc_adjust, lmu_adjust, sc);
@@ -189,10 +234,11 @@ lmu_adjust(void *cookie)
 	right = lmu_get_brightness(sc, 0);
 	b = left > right ? left : right;
 
-	if (b > (sc->sc_thresh + sc->sc_hyst)) {
+	if ((b > (sc->sc_thresh + sc->sc_hyst)) ||
+	   !(sc->sc_lid_state && sc->sc_video_state)) {
 		lmu_set_brightness(sc, 0);
 	} else if (b < sc->sc_thresh) {
-		lmu_set_brightness(sc, 100);
+		lmu_set_brightness(sc, sc->sc_level);
 	}
 
 	callout_schedule(&sc->sc_adjust, hz * 2);	

Reply via email to