screensaver.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:11k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * screensaver.c : disable screen savers when VLC is playing
  3.  *****************************************************************************
  4.  * Copyright (C) 2006 the VideoLAN team
  5.  * $Id: b5d89460d72d0baa8240a85745f802b8ae58cd00 $
  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.  * Preamble
  26.  *****************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <vlc_common.h>
  31. #include <vlc_plugin.h>
  32. #include <vlc_input.h>
  33. #include <vlc_interface.h>
  34. #include <vlc_aout.h>
  35. #include <vlc_vout.h>
  36. #include <sys/types.h>
  37. #include <sys/wait.h>
  38. #include <unistd.h>
  39. #include <signal.h>
  40. #ifdef HAVE_DBUS
  41. #include <dbus/dbus.h>
  42. #define GS_SERVICE   "org.gnome.ScreenSaver"
  43. #define GS_PATH      "/org/gnome/ScreenSaver"
  44. #define GS_INTERFACE "org.gnome.ScreenSaver"
  45. #define FDS_SERVICE   "org.freedesktop.ScreenSaver"
  46. #define FDS_PATH      "/ScreenSaver"
  47. #define FDS_INTERFACE "org.freedesktop.ScreenSaver"
  48. #endif
  49. /*****************************************************************************
  50.  * Local prototypes
  51.  *****************************************************************************/
  52. static int  Activate     ( vlc_object_t * );
  53. static void  Deactivate   ( vlc_object_t * );
  54. static void Run          ( intf_thread_t *p_intf );
  55. #ifdef HAVE_DBUS
  56. static DBusConnection * dbus_init( intf_thread_t *p_intf );
  57. static void poke_screensaver( intf_thread_t *p_intf,
  58.                               DBusConnection *p_connection );
  59. static void screensaver_send_message_void ( intf_thread_t *p_intf,
  60.                                        DBusConnection *p_connection,
  61.                                        const char *psz_service,
  62.                                        const char *psz_path,
  63.                                        const char *psz_interface,
  64.                                        const char *psz_name );
  65. static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service );
  66. struct intf_sys_t
  67. {
  68.     DBusConnection *p_connection;
  69. };
  70. #endif
  71. /*****************************************************************************
  72.  * Module descriptor
  73.  *****************************************************************************/
  74. vlc_module_begin ()
  75.     set_description( N_("X Screensaver disabler") )
  76.     set_capability( "interface", 0 )
  77.     set_callbacks( Activate, Deactivate )
  78. vlc_module_end ()
  79. /*****************************************************************************
  80.  * Activate: initialize and create stuff
  81.  *****************************************************************************/
  82. static int Activate( vlc_object_t *p_this )
  83. {
  84.     intf_thread_t *p_intf = (intf_thread_t*)p_this;
  85.     p_intf->pf_run = Run;
  86. #ifdef HAVE_DBUS
  87.     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
  88.     if( !p_intf->p_sys ) return VLC_ENOMEM;
  89. #endif
  90.     return VLC_SUCCESS;
  91. }
  92. /*****************************************************************************
  93.  * Deactivate: uninitialize and cleanup
  94.  *****************************************************************************/
  95. static void Deactivate( vlc_object_t *p_this )
  96. {
  97. #ifdef HAVE_DBUS
  98.     intf_thread_t *p_intf = (intf_thread_t*)p_this;
  99.     if( p_intf->p_sys->p_connection )
  100.     {
  101.         dbus_connection_unref( p_intf->p_sys->p_connection );
  102.     }
  103.     free( p_intf->p_sys );
  104.     p_intf->p_sys = NULL;
  105. #endif
  106. }
  107. /*****************************************************************************
  108.  * Execute: Spawns a process using execv()
  109.  *****************************************************************************/
  110. static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
  111. {
  112.     pid_t pid = fork();
  113.     switch( pid )
  114.     {
  115.         case 0:     /* we're the child */
  116.         {
  117.             sigset_t set;
  118.             sigemptyset (&set);
  119.             pthread_sigmask (SIG_SETMASK, &set, NULL);
  120.             /* We don't want output */
  121.             if( ( freopen( "/dev/null", "w", stdout ) != NULL )
  122.              && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
  123.                 execv( ppsz_args[0] , (char *const *)ppsz_args );
  124.             /* If the file we want to execute doesn't exist we exit() */
  125.             exit( EXIT_FAILURE );
  126.         }
  127.         case -1:    /* we're the error */
  128.             msg_Dbg( p_this, "Couldn't fork() while launching %s",
  129.                      ppsz_args[0] );
  130.             break;
  131.         default:    /* we're the parent */
  132.             /* Wait for the child to exit.
  133.              * We will not deadlock because we ran "/bin/sh &" */
  134.             while( waitpid( pid, NULL, 0 ) != pid);
  135.             break;
  136.     }
  137. }
  138. /*****************************************************************************
  139.  * Run: main thread
  140.  *****************************************************************************
  141.  * This part of the module is in a separate thread so that we do not have
  142.  * too much system() overhead.
  143.  *****************************************************************************/
  144. static void Run( intf_thread_t *p_intf )
  145. {
  146.     int canc = vlc_savecancel();
  147. #ifdef HAVE_DBUS
  148.     p_intf->p_sys->p_connection = dbus_init( p_intf );
  149. #endif
  150.     for( ;; )
  151.     {
  152.         vlc_object_t *p_vout;
  153.         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
  154.         /* If there is a video output, disable xscreensaver */
  155.         if( p_vout )
  156.         {
  157.             input_thread_t *p_input;
  158.             p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
  159.             vlc_object_release( p_vout );
  160.             if( p_input )
  161.             {
  162.                 if( PLAYING_S == var_GetInteger( p_input, "state" ) )
  163.                 {
  164.                     /* http://www.jwz.org/xscreensaver/faq.html#dvd */
  165.                     const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
  166.                             "xscreensaver-command -deactivate &", (char*)NULL };
  167.                     Execute( p_intf, ppsz_xsargs );
  168.                     /* If we have dbus support, let's communicate directly
  169.                        with gnome-screensave else, run
  170.                        gnome-screensaver-command */
  171. #ifdef HAVE_DBUS
  172.                     poke_screensaver( p_intf, p_intf->p_sys->p_connection );
  173. #else
  174.                     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
  175.                             "gnome-screensaver-command --poke &", (char*)NULL };
  176.                     Execute( p_intf, ppsz_gsargs );
  177. #endif
  178.                     /* FIXME: add support for other screensavers */
  179.                 }
  180.                 vlc_object_release( p_input );
  181.             }
  182.         }
  183.         vlc_restorecancel( canc );
  184.         /* Check screensaver every 30 seconds */
  185.         msleep( 30 * CLOCK_FREQ );
  186.         canc = vlc_savecancel( );
  187.     }
  188. }
  189. #ifdef HAVE_DBUS
  190. static DBusConnection * dbus_init( intf_thread_t *p_intf )
  191. {
  192.     DBusError dbus_error;
  193.     dbus_error_init (&dbus_error);
  194.     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
  195.     if ( !p_connection )
  196.     {
  197.         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
  198.                           dbus_error.message);
  199.         dbus_error_free( &dbus_error );
  200.         return NULL;
  201.     }
  202.     return p_connection;
  203. }
  204. static void poke_screensaver( intf_thread_t *p_intf,
  205.                               DBusConnection *p_connection )
  206. {
  207.     if( screensaver_is_running( p_connection, GS_SERVICE ) )
  208.     {
  209. #   ifdef SCREENSAVER_DEBUG
  210.         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
  211. #   endif
  212.         /* gnome-screensaver changed it's D-Bus interface, so we need both */
  213.         screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
  214.                                        GS_INTERFACE, "Poke" );
  215.         screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
  216.                                        GS_INTERFACE, "SimulateUserActivity" );
  217.     }
  218.     else if( screensaver_is_running( p_connection, FDS_SERVICE ) )
  219.     {
  220. #   ifdef SCREENSAVER_DEBUG
  221.         msg_Dbg( p_intf, "found a running freedesktop-screensaver instance" );
  222. #   endif
  223.         screensaver_send_message_void( p_intf, p_connection, FDS_SERVICE, FDS_PATH,
  224.                                        FDS_INTERFACE, "SimulateUserActivity" );
  225.     }
  226. #   ifdef SCREENSAVER_DEBUG
  227.     else
  228.     {
  229.         msg_Dbg( p_intf, "found no running (gnome|freedesktop)-screensaver instance" );
  230.     }
  231. #   endif
  232. }
  233. static void screensaver_send_message_void ( intf_thread_t *p_intf,
  234.                                        DBusConnection *p_connection,
  235.                                        const char *psz_service,
  236.                                        const char *psz_path,
  237.                                        const char *psz_interface,
  238.                                        const char *psz_name )
  239. {
  240.     DBusMessage *p_message;
  241.     if( !p_connection || !psz_name ) return;
  242.     p_message = dbus_message_new_method_call( psz_service, psz_path,
  243.                                               psz_interface, psz_name );
  244.     if( p_message == NULL )
  245.     {
  246.         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
  247.         return;
  248.     }
  249.     if( !dbus_connection_send( p_connection, p_message, NULL ) )
  250.     {
  251.         msg_Err( p_intf, "DBUS communication failed" );
  252.     }
  253.     dbus_connection_flush( p_connection );
  254.     dbus_message_unref( p_message );
  255. }
  256. static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service )
  257. {
  258.     DBusError error;
  259.     bool b_return;
  260.     if( !p_connection ) return false;
  261.     dbus_error_init( &error );
  262.     b_return = dbus_bus_name_has_owner( p_connection, psz_service, &error );
  263.     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
  264.     return b_return;
  265. }
  266. #endif