From: "Carlos R. Mafra" <[email protected]>

This patch introduces the command line option --session-idle=
which sets the session idle time in seconds.

If X is idle for more than 'session-idle' seconds and the laptop
is running on battery, use xbacklight to set brightness to 20%,
and to 100% otherwise.

The idea and some code added with this patch comes from the GPL'ed
program 'lightum'.

Signed-off-by: Carlos R. Mafra <[email protected]>
---
 Makefile    |  2 +-
 src/event.c | 17 ++++++++++++++++-
 src/main.c  | 24 ++++++++++++++++++++++++
 src/main.h  |  6 ++++++
 4 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 089acf3..cd9c5e3 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ INSTALLDIR = /usr/local/bin/
 CC = gcc
 
 LIBDIR = -L/usr/X11R6/lib
-LIBS   = -lXpm -lXext -lX11 
+LIBS   = -lXpm -lXext -lX11 -lXss
 CFLAGS =  -Wall -g 
 OBJS   =  main.o init.o event.o draw.o battery.o cpu.o autoscript.o pixmap.o
 EXE    = wmlaptop2
diff --git a/src/event.c b/src/event.c
index 8d5677e..72337b1 100644
--- a/src/event.c
+++ b/src/event.c
@@ -22,8 +22,9 @@ void event_handler()
        u_int32 batteryUpdate = args_batteryUpdate;
        u_int32 powerUpdate = args_powerUpdate;
        u_int32 tempUpdate = args_tempUpdate;
+       u_int32 sessionIdle = args_sessionIdle;
        u_int8 lastPercentage;
-       int j;
+       int j, once = 0;
 
        secondsCounter = time(NULL);
        update_seconds = time(NULL);
@@ -84,6 +85,20 @@ void event_handler()
                        read_power();
                }
 
+               if (get_session_idle_time(display) >= sessionIdle && 
!powerState.isCharging) {
+                       if (once == 0) {
+                               system("xbacklight -set 20");
+                               printf("Idle more than %d secs\n", sessionIdle);
+                               once = 1;
+                       }
+               } else {
+                       if (once == 1) {
+                               system("xbacklight -set 100");
+                               printf("Restoring backlight\n");
+                               once = 0;
+                       }
+               }
+
                /* check and redraw cpu load */
                cpuLoad = getCpuLoad();
                cpuReadFreq();
diff --git a/src/main.c b/src/main.c
index bd769e4..5b10154 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,6 +14,8 @@
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+
+#include <X11/extensions/scrnsaver.h>
 #include "main.h"
 
 /* cpu's stuff */
@@ -31,6 +33,7 @@ struct power_str powerState;
        u_int32 args_powerUpdate      = ARGSDEF_LEAVE;
        u_int32 args_tempUpdate       = ARGSDEF_LEAVE;
        u_int32 args_batteryUpdate    = ARGSDEF_LEAVE;
+       u_int32 args_sessionIdle      = ARGSDEF_LEAVE;
        char *  args_XDisplayName     = NULL;
        char    args_autoShutdown     = AUTOSHUTDOWN_LEAVE;
        short   args_shutdownDelay    = AUTOSHUTDOWN_LEAVE;
@@ -127,6 +130,7 @@ void usage(char *prog, char *unknowOpt)
        fprintf(stderr, "   --battery-update=N         how often, in seconds, 
to update battery state\n");
         fprintf(stderr, "   --power-update=N           how often, in seconds, 
to update power state\n");
        fprintf(stderr, "   --temperature-update=N     how often, in seconds, 
to update temperature state\n");
+       fprintf(stderr, "   --session-idle=N           X session inactivity 
time in seconds after which backlight is reduced\n");
        fprintf(stderr, "   -q   --quiet               do not print messages 
and warnings\n");
        fprintf(stderr, "   -d   --default             show the default 
compiled settings and exit\n");
        fprintf(stderr, "   -v   --version             show the version, and 
exit\n");
@@ -221,6 +225,14 @@ void setArgs(int argc, char **argv)
                        WARNING_IS_SET_TO_ZERO(args_tempUpdate, "tempUpdate");
                        continue;
                }
+               if(!strncmp(argv[i], "--session-idle=", 11)) {
+                       EXIT_IF_ALREADY_SET( args_sessionIdle, ARGSDEF_LEAVE, 
"sessionIdle");
+                       ptr = strchr(argv[i], '=');
+                       ptr++;
+                       args_sessionIdle = atoi(ptr);
+                       WARNING_IS_SET_TO_ZERO(args_sessionIdle, "sessionIdle");
+                       continue;
+               }
                if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--quiet")) {
                        EXIT_IF_ALREADY_SET(args_beQuiet, ARGSDEF_LEAVE, 
"beQuiet");
                        args_beQuiet = !ARGSDEF_BEQUIET;
@@ -240,6 +252,7 @@ void setArgs(int argc, char **argv)
        SET_DEFAULT( args_batteryUpdate,   ARGSDEF_LEAVE,      
ARGSDEF_BATTERYUPDATE );
        SET_DEFAULT( args_powerUpdate,     ARGSDEF_LEAVE,      
ARGSDEF_POWERUPDATE );
        SET_DEFAULT( args_tempUpdate,      ARGSDEF_LEAVE,      
ARGSDEF_TEMPUPDATE );
+       SET_DEFAULT( args_sessionIdle,     ARGSDEF_LEAVE,      
ARGSDEF_SESSIONIDLE );
        SET_DEFAULT( args_XDisplayName,    NULL,               
ARGSDEF_XDISPLAYNAME );
        SET_DEFAULT( args_autoShutdown,    AUTOSHUTDOWN_LEAVE, 
ARGSDEF_AUTOSHUTDOWN );
        SET_DEFAULT( args_shutdownDelay,   AUTOSHUTDOWN_LEAVE, 
ARGSDEF_SHUTDOWNDELAY );
@@ -272,3 +285,14 @@ void defaultSettings(void)
 
        free_and_exit(SUCCESS);
 }
+
+float get_session_idle_time(Display *display)
+{
+
+       XScreenSaverInfo info;
+       float seconds;
+
+       XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info);
+       seconds = (float)info.idle/1000.0f;
+       return(seconds);
+}
diff --git a/src/main.h b/src/main.h
index 758edb4..9631c2d 100644
--- a/src/main.h
+++ b/src/main.h
@@ -286,11 +286,17 @@ void defaultSettings(void);
 #ifndef    ARGSDEF_TEMPUPDATE
 #define    ARGSDEF_TEMPUPDATE        5 // seconds
 #endif
+#ifndef    ARGSDEF_SESSIONIDLE
+#define    ARGSDEF_SESSIONIDLE       10 // seconds
+#endif
 
 extern u_int32 args_cpuUpdate;
 extern u_int32 args_batteryUpdate;
 extern u_int32 args_powerUpdate;
 extern u_int32 args_tempUpdate;
+extern u_int32 args_sessionIdle;
+
+float get_session_idle_time(Display *display);
 
 /* name of diplay to open */
 #ifndef    ARGSDEF_XDISPLAYNAME
-- 
1.8.0.1


-- 
To unsubscribe, send mail to [email protected].

Reply via email to