]> git.sesse.net Git - vlc/blob - modules/misc/screensaver.c
912be56114dae3da67c097ff6feae8b463e3663a
[vlc] / modules / misc / screensaver.c
1 /*****************************************************************************
2  * screensaver.c : disable screen savers when VLC is playing
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Benjamin Pracht <bigben AT videolan DOT org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_playlist.h>
37 #include <vlc_interface.h>
38
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42 #include <signal.h>
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int  Activate     ( vlc_object_t * );
48 static void  Deactivate   ( vlc_object_t * );
49
50 static void Timer( void * );
51
52 struct intf_sys_t
53 {
54     vlc_timer_t timer;
55 };
56
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 vlc_module_begin ()
62     set_description( N_("X Screensaver disabler") )
63     set_capability( "interface", 0 )
64     set_callbacks( Activate, Deactivate )
65 vlc_module_end ()
66
67 /*****************************************************************************
68  * Activate: initialize and create stuff
69  *****************************************************************************/
70 static int Activate( vlc_object_t *p_this )
71 {
72     intf_thread_t *p_intf = (intf_thread_t*)p_this;
73     intf_sys_t *p_sys;
74
75     p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
76     if( !p_sys )
77         return VLC_ENOMEM;
78
79     if( vlc_timer_create( &p_sys->timer, Timer, p_intf ) )
80     {
81         free( p_sys );
82         return VLC_ENOMEM;
83     }
84     vlc_timer_schedule( p_sys->timer, false, 30*CLOCK_FREQ, 30*CLOCK_FREQ );
85
86     return VLC_SUCCESS;
87 }
88
89 /*****************************************************************************
90  * Deactivate: uninitialize and cleanup
91  *****************************************************************************/
92 static void Deactivate( vlc_object_t *p_this )
93 {
94     intf_thread_t *p_intf = (intf_thread_t*)p_this;
95     intf_sys_t *p_sys = p_intf->p_sys;
96
97     vlc_timer_destroy( p_sys->timer );
98
99     free( p_sys );
100 }
101
102 /*****************************************************************************
103  * Execute: Spawns a process using execv()
104  *****************************************************************************/
105 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
106 {
107     pid_t pid = fork();
108     switch( pid )
109     {
110         case 0:     /* we're the child */
111         {
112             sigset_t set;
113             sigemptyset (&set);
114             pthread_sigmask (SIG_SETMASK, &set, NULL);
115
116             /* We don't want output */
117             if( ( freopen( "/dev/null", "w", stdout ) != NULL )
118              && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
119                 execv( ppsz_args[0] , (char *const *)ppsz_args );
120             /* If the file we want to execute doesn't exist we exit() */
121             exit( EXIT_FAILURE );
122         }
123         case -1:    /* we're the error */
124             msg_Dbg( p_this, "Couldn't fork() while launching %s",
125                      ppsz_args[0] );
126             break;
127         default:    /* we're the parent */
128             /* Wait for the child to exit.
129              * We will not deadlock because we ran "/bin/sh &" */
130             while( waitpid( pid, NULL, 0 ) != pid);
131             break;
132     }
133 }
134
135 /*****************************************************************************
136  * Run: main thread
137  *****************************************************************************
138  * This part of the module is in a separate thread so that we do not have
139  * too much system() overhead.
140  *****************************************************************************/
141 static void Timer( void *data )
142 {
143     intf_thread_t *p_intf = data;
144     playlist_t *p_playlist = pl_Hold( p_intf );
145     input_thread_t *p_input = playlist_CurrentInput( p_playlist );
146     pl_Release( p_intf );
147     if( !p_input )
148         return;
149
150     vlc_object_t *p_vout;
151     if( var_GetInteger( p_input, "state" ) == PLAYING_S )
152         p_vout = (vlc_object_t *)input_GetVout( p_input );
153     else
154         p_vout = NULL;
155     vlc_object_release( p_input );
156     if( !p_vout )
157         return;
158     vlc_object_release( p_vout );
159
160     /* If there is a playing video output, disable xscreensaver */
161     /* http://www.jwz.org/xscreensaver/faq.html#dvd */
162     const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
163         "xscreensaver-command -deactivate &", (char*)NULL };
164     Execute( p_intf, ppsz_xsargs );
165
166     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
167         "gnome-screensaver-command --poke &", (char*)NULL };
168     Execute( p_intf, ppsz_gsargs );
169 }