| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 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_interface.h> |
|---|
| 37 |
#include <vlc_aout.h> |
|---|
| 38 |
#include <vlc_vout.h> |
|---|
| 39 |
|
|---|
| 40 |
#include <sys/types.h> |
|---|
| 41 |
#include <sys/wait.h> |
|---|
| 42 |
#include <unistd.h> |
|---|
| 43 |
|
|---|
| 44 |
#ifdef HAVE_SIGNAL_H |
|---|
| 45 |
# include <signal.h> |
|---|
| 46 |
#endif |
|---|
| 47 |
|
|---|
| 48 |
#ifdef HAVE_DBUS |
|---|
| 49 |
|
|---|
| 50 |
#include <dbus/dbus.h> |
|---|
| 51 |
|
|---|
| 52 |
#define GS_SERVICE "org.gnome.ScreenSaver" |
|---|
| 53 |
#define GS_PATH "/org/gnome/ScreenSaver" |
|---|
| 54 |
#define GS_INTERFACE "org.gnome.ScreenSaver" |
|---|
| 55 |
|
|---|
| 56 |
#endif |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
static int Activate ( vlc_object_t * ); |
|---|
| 62 |
static void Deactivate ( vlc_object_t * ); |
|---|
| 63 |
|
|---|
| 64 |
static void Run ( intf_thread_t *p_intf ); |
|---|
| 65 |
|
|---|
| 66 |
#ifdef HAVE_DBUS |
|---|
| 67 |
|
|---|
| 68 |
static DBusConnection * dbus_init( intf_thread_t *p_intf ); |
|---|
| 69 |
static void poke_screensaver( intf_thread_t *p_intf, |
|---|
| 70 |
DBusConnection *p_connection ); |
|---|
| 71 |
static void screensaver_send_message_void ( intf_thread_t *p_intf, |
|---|
| 72 |
DBusConnection *p_connection, |
|---|
| 73 |
const char *psz_name ); |
|---|
| 74 |
static bool screensaver_is_running( DBusConnection *p_connection ); |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
struct intf_sys_t |
|---|
| 78 |
{ |
|---|
| 79 |
DBusConnection *p_connection; |
|---|
| 80 |
}; |
|---|
| 81 |
|
|---|
| 82 |
#endif |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
vlc_module_begin(); |
|---|
| 88 |
set_description( N_("X Screensaver disabler") ); |
|---|
| 89 |
set_capability( "interface", 0 ); |
|---|
| 90 |
set_callbacks( Activate, Deactivate ); |
|---|
| 91 |
vlc_module_end(); |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
static int Activate( vlc_object_t *p_this ) |
|---|
| 97 |
{ |
|---|
| 98 |
intf_thread_t *p_intf = (intf_thread_t*)p_this; |
|---|
| 99 |
|
|---|
| 100 |
p_intf->pf_run = Run; |
|---|
| 101 |
|
|---|
| 102 |
#ifdef HAVE_DBUS |
|---|
| 103 |
p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) ); |
|---|
| 104 |
if( !p_intf->p_sys ) return VLC_ENOMEM; |
|---|
| 105 |
#endif |
|---|
| 106 |
|
|---|
| 107 |
return VLC_SUCCESS; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
static void Deactivate( vlc_object_t *p_this ) |
|---|
| 114 |
{ |
|---|
| 115 |
#ifdef HAVE_DBUS |
|---|
| 116 |
intf_thread_t *p_intf = (intf_thread_t*)p_this; |
|---|
| 117 |
|
|---|
| 118 |
if( p_intf->p_sys->p_connection ) |
|---|
| 119 |
{ |
|---|
| 120 |
dbus_connection_unref( p_intf->p_sys->p_connection ); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
free( p_intf->p_sys ); |
|---|
| 124 |
p_intf->p_sys = NULL; |
|---|
| 125 |
#endif |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
static void Execute( intf_thread_t *p_this, const char *const *ppsz_args ) |
|---|
| 132 |
{ |
|---|
| 133 |
pid_t pid = fork(); |
|---|
| 134 |
switch( pid ) |
|---|
| 135 |
{ |
|---|
| 136 |
case 0: |
|---|
| 137 |
{ |
|---|
| 138 |
sigset_t set; |
|---|
| 139 |
sigemptyset (&set); |
|---|
| 140 |
pthread_sigmask (SIG_SETMASK, &set, NULL); |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
if( ( freopen( "/dev/null", "w", stdout ) != NULL ) |
|---|
| 144 |
&& ( freopen( "/dev/null", "w", stderr ) != NULL ) ) |
|---|
| 145 |
execv( ppsz_args[0] , (char *const *)ppsz_args ); |
|---|
| 146 |
|
|---|
| 147 |
exit( EXIT_FAILURE ); |
|---|
| 148 |
} |
|---|
| 149 |
case -1: |
|---|
| 150 |
msg_Dbg( p_this, "Couldn't fork() while launching %s", |
|---|
| 151 |
ppsz_args[0] ); |
|---|
| 152 |
break; |
|---|
| 153 |
default: |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
while( waitpid( pid, NULL, 0 ) != pid); |
|---|
| 157 |
break; |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
static void Run( intf_thread_t *p_intf ) |
|---|
| 168 |
{ |
|---|
| 169 |
int canc = vlc_savecancel(); |
|---|
| 170 |
#ifdef HAVE_DBUS |
|---|
| 171 |
p_intf->p_sys->p_connection = dbus_init( p_intf ); |
|---|
| 172 |
#endif |
|---|
| 173 |
|
|---|
| 174 |
for( ;; ) |
|---|
| 175 |
{ |
|---|
| 176 |
vlc_object_t *p_vout; |
|---|
| 177 |
|
|---|
| 178 |
p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE ); |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
if( p_vout ) |
|---|
| 182 |
{ |
|---|
| 183 |
input_thread_t *p_input; |
|---|
| 184 |
p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT ); |
|---|
| 185 |
vlc_object_release( p_vout ); |
|---|
| 186 |
if( p_input ) |
|---|
| 187 |
{ |
|---|
| 188 |
if( PLAYING_S == p_input->i_state ) |
|---|
| 189 |
{ |
|---|
| 190 |
|
|---|
| 191 |
const char *const ppsz_xsargs[] = { "/bin/sh", "-c", |
|---|
| 192 |
"xscreensaver-command -deactivate &", (char*)NULL }; |
|---|
| 193 |
Execute( p_intf, ppsz_xsargs ); |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
#ifdef HAVE_DBUS |
|---|
| 199 |
poke_screensaver( p_intf, p_intf->p_sys->p_connection ); |
|---|
| 200 |
#else |
|---|
| 201 |
const char *const ppsz_gsargs[] = { "/bin/sh", "-c", |
|---|
| 202 |
"gnome-screensaver-command --poke &", (char*)NULL }; |
|---|
| 203 |
Execute( p_intf, ppsz_gsargs ); |
|---|
| 204 |
#endif |
|---|
| 205 |
|
|---|
| 206 |
} |
|---|
| 207 |
vlc_object_release( p_input ); |
|---|
| 208 |
} |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
vlc_restorecancel( canc ); |
|---|
| 212 |
|
|---|
| 213 |
msleep( 30 * CLOCK_FREQ ); |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
#ifdef HAVE_DBUS |
|---|
| 218 |
|
|---|
| 219 |
static DBusConnection * dbus_init( intf_thread_t *p_intf ) |
|---|
| 220 |
{ |
|---|
| 221 |
DBusError dbus_error; |
|---|
| 222 |
|
|---|
| 223 |
dbus_error_init (&dbus_error); |
|---|
| 224 |
DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error ); |
|---|
| 225 |
|
|---|
| 226 |
if ( !p_connection ) |
|---|
| 227 |
{ |
|---|
| 228 |
msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s", |
|---|
| 229 |
dbus_error.message); |
|---|
| 230 |
dbus_error_free( &dbus_error ); |
|---|
| 231 |
return NULL; |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
return p_connection; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
static void poke_screensaver( intf_thread_t *p_intf, |
|---|
| 238 |
DBusConnection *p_connection ) |
|---|
| 239 |
{ |
|---|
| 240 |
if( screensaver_is_running( p_connection ) ) |
|---|
| 241 |
{ |
|---|
| 242 |
# ifdef SCREENSAVER_DEBUG |
|---|
| 243 |
msg_Dbg( p_intf, "found a running gnome-screensaver instance" ); |
|---|
| 244 |
# endif |
|---|
| 245 |
|
|---|
| 246 |
screensaver_send_message_void( p_intf, p_connection, "Poke" ); |
|---|
| 247 |
screensaver_send_message_void( p_intf, p_connection, |
|---|
| 248 |
"SimulateUserActivity" ); |
|---|
| 249 |
} |
|---|
| 250 |
# ifdef SCREENSAVER_DEBUG |
|---|
| 251 |
else |
|---|
| 252 |
{ |
|---|
| 253 |
msg_Dbg( p_intf, "found no running gnome-screensaver instance" ); |
|---|
| 254 |
} |
|---|
| 255 |
# endif |
|---|
| 256 |
} |
|---|
| 257 |
|
|---|
| 258 |
static void screensaver_send_message_void ( intf_thread_t *p_intf, |
|---|
| 259 |
DBusConnection *p_connection, |
|---|
| 260 |
const char *psz_name ) |
|---|
| 261 |
{ |
|---|
| 262 |
DBusMessage *p_message; |
|---|
| 263 |
|
|---|
| 264 |
if( !p_connection || !psz_name ) return; |
|---|
| 265 |
|
|---|
| 266 |
p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH, |
|---|
| 267 |
GS_INTERFACE, psz_name ); |
|---|
| 268 |
if( p_message == NULL ) |
|---|
| 269 |
{ |
|---|
| 270 |
msg_Err( p_intf, "DBUS initialization failed: message initialization" ); |
|---|
| 271 |
return; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
if( !dbus_connection_send( p_connection, p_message, NULL ) ) |
|---|
| 275 |
{ |
|---|
| 276 |
msg_Err( p_intf, "DBUS communication failed" ); |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
dbus_connection_flush( p_connection ); |
|---|
| 280 |
|
|---|
| 281 |
dbus_message_unref( p_message ); |
|---|
| 282 |
} |
|---|
| 283 |
|
|---|
| 284 |
static bool screensaver_is_running( DBusConnection *p_connection ) |
|---|
| 285 |
{ |
|---|
| 286 |
DBusError error; |
|---|
| 287 |
bool b_return; |
|---|
| 288 |
|
|---|
| 289 |
if( !p_connection ) return false; |
|---|
| 290 |
|
|---|
| 291 |
dbus_error_init( &error ); |
|---|
| 292 |
b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error ); |
|---|
| 293 |
if( dbus_error_is_set( &error ) ) dbus_error_free (&error); |
|---|
| 294 |
|
|---|
| 295 |
return b_return; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
#endif |
|---|
| 299 |
|
|---|