[mythtv] Tv power control idea

Neil Whelchel koyama at firstlight.net
Thu Dec 29 21:11:06 EST 2005


Hello,
I am using a large plasma TV with Myth and from time to time it gets
forgotten about and left on overnight or even longer.
So I came up with the idea of powering off the TV when the screensaver
would come on. (Also this has the ability to turn the TV on as well.)
At the moment I hacked the fillowing into screensaver-x11.cpp. Knowing
that this ais a temporary hack I am posting here for ideas as to what the
correct way to go about this is...
The hack simply checks for the presence of a FIFO in the /tmp directory.
If the file is there it simply writes a number to it to indicate the state
of the screensaver. The number is read by another (daemon) which calls
irsend to switch the power of the TV. I am watching a pin of the parallel
port to get the power state of the TV.
At the moment it is all very crude but it works very well, no more
TV-on-all-night problem and pressing any button on the remote turns the TV
on!

I would like to clean this up and possibly get it merged into Myth, so any
comments or suggestions are welcome..
Thanks!



Here is the diff for screensaver-x11.cpp:
--- screensaver-x11.cpp.orig    Fri Dec 23 02:14:15 2005
+++ screensaver-x11.cpp Fri Dec 23 03:32:22 2005
@@ -2,6 +2,10 @@
 #include "screensaver-x11.h"
 #include <qtimer.h>

+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 #include <X11/Xlib.h>

 extern "C" {
@@ -53,6 +57,12 @@

 void ScreenSaverX11::Disable(void)
 {
+    int tvd;
+    char one='1';
+    if((tvd=open("/tmp/tvd", O_WRONLY))>0){
+        write(tvd, &one, 1);
+       close(tvd);
+    }
     if (!d->state.saved)
     {
         XGetScreenSaver(qt_xdisplay(),
@@ -102,6 +112,13 @@

 void ScreenSaverX11::Restore(void)
 {
+    int tvd;
+    char zero='0';
+    if((tvd=open("/tmp/tvd", O_WRONLY))>0){
+        write(tvd, &zero, 1);
+        close(tvd);
+    }
+
     XResetScreenSaver(qt_xdisplay());
     XSetScreenSaver(qt_xdisplay(),
                     d->state.timeout, d->state.interval,
@@ -126,6 +143,13 @@

 void ScreenSaverX11::Reset(void)
 {
+    int tvd;
+    char two='2';
+    if((tvd=open("/tmp/tvd", O_WRONLY))>0){
+        write(tvd, &two, 1);
+        close(tvd);
+    }
+
     XResetScreenSaver(qt_xdisplay());
     if (d->state.xscreensaverRunning)
         resetSlot();



Here is the daemon that sits at the other end of the FIFO.
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

#define PORTBASE 0x378

// Call with 1 to turn tv on... 0 to turn tv off.
void switch_tv(int pwr){
        int power;
        power=inb(PORTBASE)&1;
        if(power!=pwr){
                system("/usr/bin/irsend SEND_ONCE tv power");
                printf("switch_pwer\n");
        }
}


// States:
// 0=Timer stopped
// 1=Timer started
// 2=Timer expired
int main(){
        int state=0;
        int pipe;
        char buffer;
        int timer=0;

        if(ioperm(PORTBASE, 3, 1)){
                printf("Could not get access to port");
                return(1);
        }
        outb(32, PORTBASE+2);
        outb(0, PORTBASE);
        mkfifo("/tmp/tvd", 384);
        pipe=open("/tmp/tvd", O_RDONLY);
        fcntl(pipe, F_SETFL, O_NONBLOCK);
        while(1){
                sleep(1);
                if(read(pipe,&buffer,1)>0){
                        if(buffer=='2'){
                                timer=0;
                                if(state==2)
                                        state=1;
                                switch_tv(1);
                                printf("Received reset_timer\n");
                        }
                        if(buffer=='1'){
                                state=0;
                                timer=0;
                                switch_tv(1);
                                printf("Received tv_on command\n");
                        }
                        if(buffer=='0'){
                                timer=0;
                                state=1;
                                printf("Received sleep_wait command\n");
                        }
                }
                if(state==1){
                        timer++;
                }
                if(timer>120){
                        switch_tv(0);
                        state=2;
                        timer=0;
                }
        }
        return(0);
}




-Neil Whelchel-
First Light Internet Services
760 366-0145
- We don't do Window$, that's what the janitor is for -

Bubble Memory, n.:
        A derogatory term, usually referring to a person's
intelligence.  See also "vacuum tube".



More information about the mythtv-dev mailing list