Macro qtTrIdx() replaced by tr() and QT_TRANSLATE_NOOP()
[mafwsubrenderer] / mafw-gst-subtitles-renderer / libmafw-gst-renderer / blanking.c
1 /*
2  * This file is a part of MAFW
3  *
4  * Copyright (C) 2007, 2008, 2009 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Visa Smolander <visa.smolander@nokia.com>
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; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser 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
21  * 02110-1301 USA
22  *
23  */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <glib.h>
29 #include <libosso.h>
30 #include "blanking.h"
31
32 #undef  G_LOG_DOMAIN
33 #define G_LOG_DOMAIN "mafw-gst-renderer-blanking"
34
35 /* In seconds */
36 #define VIDEO_BLANKING_TIMER_INTERVAL   45
37
38 static guint blanking_timeout_id = 0;
39 static osso_context_t *osso_ctx = NULL;
40 static gboolean can_control_blanking = TRUE;
41 static gboolean is_blanking_prohibited = FALSE;
42
43 static void remove_blanking_timeout(void)
44 {
45         if (blanking_timeout_id) {
46                 g_source_remove(blanking_timeout_id);
47                 blanking_timeout_id = 0;
48         }
49 }
50
51 /*
52  * Re-enables screen blanking.
53  */
54 void blanking_allow(void)
55 {
56         is_blanking_prohibited = FALSE;
57         remove_blanking_timeout();
58 }
59
60 static gboolean no_blanking_timeout(void)
61 {
62         /* Stop trying if it fails. */
63         return osso_display_blanking_pause(osso_ctx) == OSSO_OK;
64 }
65
66 /*
67  * Adds a timeout to periodically disable screen blanking.
68  */
69 void blanking_prohibit(void)
70 {
71         is_blanking_prohibited = TRUE;
72         if ((!osso_ctx) || (!can_control_blanking))
73                 return;
74         osso_display_state_on(osso_ctx);
75         osso_display_blanking_pause(osso_ctx);
76         if (blanking_timeout_id == 0) {
77                 blanking_timeout_id =
78                         g_timeout_add_seconds(VIDEO_BLANKING_TIMER_INTERVAL,
79                                               (gpointer)no_blanking_timeout,
80                                               NULL);
81         }
82 }
83
84 void blanking_init(void)
85 {
86         /* It's enough to initialize it once for a process. */
87         if (osso_ctx)
88                 return;
89         osso_ctx = osso_initialize(PACKAGE, VERSION, 0, NULL);
90         if (!osso_ctx)
91                 g_warning("osso_initialize failed, screen may go black");
92         is_blanking_prohibited = FALSE;
93         /* Default policy is to allow user to control blanking */
94         blanking_control(TRUE);
95 }
96
97 void blanking_deinit(void)
98 {
99         if (!osso_ctx)
100                 return;
101         blanking_control(FALSE);
102         osso_deinitialize(osso_ctx);
103         osso_ctx = NULL;
104 }
105
106 void blanking_control(gboolean activate)
107 {
108         can_control_blanking = activate;
109         if (!can_control_blanking) {
110                 remove_blanking_timeout();
111         } else {
112                 /* Restore the last state */
113                 if (is_blanking_prohibited) {
114                         blanking_prohibit();
115                 } else {
116                         blanking_allow();
117                 }
118         }
119 }