]> git.sesse.net Git - vlc/blob - modules/misc/inhibit/xdg.c
input: add b_net variable in item
[vlc] / modules / misc / inhibit / xdg.c
1 /*****************************************************************************
2  * xdg-screensaver.c
3  *****************************************************************************
4  * Copyright (C) 2008 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_inhibit.h>
28 #include <assert.h>
29 #include <signal.h>
30 #include <spawn.h>
31 #include <sys/wait.h>
32
33 static int Open (vlc_object_t *);
34 static void Close (vlc_object_t *);
35
36 vlc_module_begin ()
37     set_shortname (N_("XDG-screensaver") )
38     set_description (N_("XDG screen saver inhibition") )
39     set_capability ("inhibit", 10 )
40     set_callbacks (Open, Close)
41     set_category (CAT_ADVANCED)
42     set_subcategory (SUBCAT_ADVANCED_MISC)
43 vlc_module_end ()
44
45 struct vlc_inhibit_sys
46 {
47     vlc_timer_t timer;
48     posix_spawnattr_t attr;
49 };
50
51 extern char **environ;
52
53 static void Timer (void *data)
54 {
55     vlc_inhibit_t *ih = data;
56     vlc_inhibit_sys_t *sys = ih->p_sys;
57     char *argv[3] = {
58         (char *)"xdg-screensaver", (char *)"reset", NULL
59     };
60     pid_t pid;
61
62     int err = posix_spawnp (&pid, "xdg-screensaver", NULL, &sys->attr,
63                             argv, environ);
64     if (err == 0)
65     {
66         int status;
67
68         while (waitpid (pid, &status, 0) == -1);
69     }
70     else
71         msg_Warn (ih, "error starting xdg-screensaver: %s",
72                   vlc_strerror_c(err));
73 }
74
75 static void Inhibit (vlc_inhibit_t *ih, unsigned mask)
76 {
77     vlc_inhibit_sys_t *sys = ih->p_sys;
78     bool suspend = (mask & VLC_INHIBIT_DISPLAY) != 0;
79     mtime_t delay = suspend ? 30 * CLOCK_FREQ : INT64_C(0);
80
81     vlc_timer_schedule (sys->timer, false, delay, delay);
82 }
83
84 static int Open (vlc_object_t *obj)
85 {
86     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
87     vlc_inhibit_sys_t *p_sys = malloc (sizeof (*p_sys));
88     if (p_sys == NULL)
89         return VLC_ENOMEM;
90
91     posix_spawnattr_init (&p_sys->attr);
92     /* Reset signal handlers to default and clear mask in the child process */
93     {
94         sigset_t set;
95
96         sigemptyset (&set);
97         posix_spawnattr_setsigmask (&p_sys->attr, &set);
98         sigaddset (&set, SIGPIPE);
99         posix_spawnattr_setsigdefault (&p_sys->attr, &set);
100         posix_spawnattr_setflags (&p_sys->attr, POSIX_SPAWN_SETSIGDEF
101                                               | POSIX_SPAWN_SETSIGMASK);
102     }
103
104     ih->p_sys = p_sys;
105     if (vlc_timer_create (&p_sys->timer, Timer, ih))
106     {
107         posix_spawnattr_destroy (&p_sys->attr);
108         free (p_sys);
109         return VLC_ENOMEM;
110     }
111
112     ih->inhibit = Inhibit;
113     return VLC_SUCCESS;
114 }
115
116 static void Close (vlc_object_t *obj)
117 {
118     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
119     vlc_inhibit_sys_t *p_sys = ih->p_sys;
120
121     vlc_timer_destroy (p_sys->timer);
122     posix_spawnattr_destroy (&p_sys->attr);
123     free (p_sys);
124 }