]> git.sesse.net Git - vlc/blob - modules/misc/inhibit/osso.c
a858dd12b0038ca61976e9b1507fcc46299e15f8
[vlc] / modules / misc / inhibit / osso.c
1 /**
2  * @file osso.c
3  * @brief Maemo screen unblanking for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_inhibit.h>
30
31 #include <libosso.h>
32
33 static int  Open (vlc_object_t *);
34 static void Close (vlc_object_t *);
35
36 /*
37  * Module descriptor
38  */
39 vlc_module_begin ()
40     set_shortname (N_("OSSO"))
41     set_description (N_("OSSO screen unblanking"))
42     set_category (CAT_ADVANCED)
43     set_subcategory (SUBCAT_ADVANCED_MISC)
44     set_capability ("inhibit", 20)
45     set_callbacks (Open, Close)
46 vlc_module_end ()
47
48 static void Inhibit (vlc_inhibit_t *, bool);
49
50 /* We keep a single context per process */
51 static struct
52 {
53     vlc_mutex_t lock;
54     unsigned refs;
55     unsigned suspensions;
56     osso_context_t *ctx;
57     vlc_timer_t timer;
58 } osso = {
59     .lock = VLC_STATIC_MUTEX,
60     .refs = 0,
61     .suspensions = 0,
62 };
63
64 static void vlc_osso_unblank (void *dummy)
65 {
66     (void) dummy;
67
68     vlc_mutex_lock (&osso.lock);
69     osso_display_blanking_pause (osso.ctx);
70     vlc_mutex_unlock (&osso.lock);
71 }
72
73 #define BLANKING   (NULL)
74 #define UNBLANKING ((vlc_inhibit_sys_t *)ih)
75
76 static int Open (vlc_object_t *obj)
77 {
78     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
79     int ret = VLC_EGENERIC;
80
81     vlc_mutex_lock (&osso.lock);
82     if (osso.refs++ == 0)
83     {
84         if (vlc_timer_create (&osso.timer, vlc_osso_unblank, NULL))
85             goto out;
86
87         osso.ctx = osso_initialize (PACKAGE, VERSION, 0, NULL);
88         if (osso.ctx == NULL)
89         {
90             vlc_timer_destroy (osso.timer);
91             goto out;
92         }
93
94         msg_Dbg (obj, "initialized OSSO context");
95         ret = VLC_SUCCESS;
96     }
97 out:
98     vlc_mutex_unlock (&osso.lock);
99
100     ih->p_sys = BLANKING;
101     ih->inhibit = Inhibit;
102     return ret;
103 }
104
105 static void Close (vlc_object_t *obj)
106 {
107     Inhibit ((vlc_inhibit_t *)obj, false);
108
109     vlc_mutex_lock (&osso.lock);
110     if (--osso.refs == 0)
111     {
112         msg_Dbg (obj, "deinitializing OSSO context");
113         vlc_timer_destroy (osso.timer);
114         osso_deinitialize (osso.ctx);
115     }
116     vlc_mutex_unlock (&osso.lock);
117 }
118
119 static void Inhibit (vlc_inhibit_t *ih, bool unblank)
120 {
121     if (unblank == (ih->p_sys != BLANKING))
122         return; /* already in right state */
123
124     vlc_mutex_lock (&osso.lock);
125     if (unblank)
126     {
127         /* 10 seconds is the shortest blanking interval */
128         mtime_t start = (mdate() / CLOCK_FREQ + 8) * CLOCK_FREQ;
129         mtime_t interval = 9 * CLOCK_FREQ;
130
131         osso_display_state_on (osso.ctx);
132         if (osso.suspensions++ == 0) /* arm timer */
133             vlc_timer_schedule (osso.timer, true, start, interval);
134         ih->p_sys = UNBLANKING;
135     }
136     else
137     {
138         if (--osso.suspensions == 0) /* disarm timer */
139             vlc_timer_schedule (osso.timer, false, 0, 0);
140         ih->p_sys = BLANKING;
141     }
142     vlc_mutex_unlock (&osso.lock);
143 }
144