]> git.sesse.net Git - vlc/blob - modules/misc/inhibit/xdg.c
demux: ogg: handle broken chapter extension
[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 <errno.h>
30 #include <signal.h>
31 #include <spawn.h>
32 #include <sys/wait.h>
33
34 static int Open (vlc_object_t *);
35 static void Close (vlc_object_t *);
36
37 vlc_module_begin ()
38     set_shortname (N_("XDG-screensaver") )
39     set_description (N_("XDG screen saver inhibition") )
40     set_capability ("inhibit", 10 )
41     set_callbacks (Open, Close)
42     set_category (CAT_ADVANCED)
43     set_subcategory (SUBCAT_ADVANCED_MISC)
44 vlc_module_end ()
45
46 struct vlc_inhibit_sys
47 {
48     vlc_timer_t timer;
49     posix_spawnattr_t attr;
50 };
51
52 extern char **environ;
53
54 static void Timer (void *data)
55 {
56     vlc_inhibit_t *ih = data;
57     vlc_inhibit_sys_t *sys = ih->p_sys;
58     char *argv[3] = {
59         (char *)"xdg-screensaver", (char *)"reset", NULL
60     };
61     pid_t pid;
62
63     int err = posix_spawnp (&pid, "xdg-screensaver", NULL, &sys->attr,
64                             argv, environ);
65     if (err == 0)
66     {
67         int status;
68
69         while (waitpid (pid, &status, 0) == -1);
70     }
71     else
72     {
73         errno = err;
74         msg_Warn (ih, "error starting xdg-screensaver: %m");
75     }
76 }
77
78 static void Inhibit (vlc_inhibit_t *ih, unsigned mask)
79 {
80     vlc_inhibit_sys_t *sys = ih->p_sys;
81     bool suspend = (mask & VLC_INHIBIT_DISPLAY) != 0;
82     mtime_t delay = suspend ? 30 * CLOCK_FREQ : INT64_C(0);
83
84     vlc_timer_schedule (sys->timer, false, delay, delay);
85 }
86
87 static int Open (vlc_object_t *obj)
88 {
89     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
90     vlc_inhibit_sys_t *p_sys = malloc (sizeof (*p_sys));
91     if (p_sys == NULL)
92         return VLC_ENOMEM;
93
94     posix_spawnattr_init (&p_sys->attr);
95     /* Reset signal handlers to default and clear mask in the child process */
96     {
97         sigset_t set;
98
99         sigemptyset (&set);
100         posix_spawnattr_setsigmask (&p_sys->attr, &set);
101         sigaddset (&set, SIGPIPE);
102         posix_spawnattr_setsigdefault (&p_sys->attr, &set);
103         posix_spawnattr_setflags (&p_sys->attr, POSIX_SPAWN_SETSIGDEF
104                                               | POSIX_SPAWN_SETSIGMASK);
105     }
106
107     ih->p_sys = p_sys;
108     if (vlc_timer_create (&p_sys->timer, Timer, ih))
109     {
110         posix_spawnattr_destroy (&p_sys->attr);
111         free (p_sys);
112         return VLC_ENOMEM;
113     }
114
115     ih->inhibit = Inhibit;
116     return VLC_SUCCESS;
117 }
118
119 static void Close (vlc_object_t *obj)
120 {
121     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
122     vlc_inhibit_sys_t *p_sys = ih->p_sys;
123
124     vlc_timer_destroy (p_sys->timer);
125     posix_spawnattr_destroy (&p_sys->attr);
126     free (p_sys);
127 }