]> git.sesse.net Git - vlc/blob - modules/misc/inhibit/mce.c
MCE plug-in for screen unblanking on Maemo devices
[vlc] / modules / misc / inhibit / mce.c
1 /**
2  * @file mce.c
3  * @brief Nokia MCE screen unblanking for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009-2011 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 #include <dbus/dbus.h>
31
32 static int  Open (vlc_object_t *);
33 static void Close (vlc_object_t *);
34
35 /*
36  * Module descriptor
37  */
38 vlc_module_begin ()
39     set_shortname (N_("MCE"))
40     set_description (N_("Nokia MCE screen unblanking"))
41     set_category (CAT_ADVANCED)
42     set_subcategory (SUBCAT_ADVANCED_MISC)
43     set_capability ("inhibit", 20)
44     set_callbacks (Open, Close)
45 vlc_module_end ()
46
47 static void Inhibit (vlc_inhibit_t *, bool);
48 static void Timer (void *data);
49
50 struct vlc_inhibit_sys
51 {
52     DBusConnection *conn;
53     vlc_timer_t timer;
54 };
55
56 static int Open (vlc_object_t *obj)
57 {
58     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
59     vlc_inhibit_sys_t *sys = malloc (sizeof (*sys));
60     if (unlikely(sys == NULL))
61         return VLC_ENOMEM;
62
63     DBusError err;
64
65     dbus_error_init (&err);
66     sys->conn = dbus_bus_get_private (DBUS_BUS_SYSTEM, &err);
67     if (sys->conn == NULL)
68     {
69         msg_Err (obj, "cannot connect to system bus: %s", err.message);
70         dbus_error_free (&err);
71         goto error;
72     }
73
74     if (vlc_timer_create (&sys->timer, Timer, sys->conn))
75     {
76         dbus_connection_unref (sys->conn);
77         goto error;
78     }
79
80     ih->p_sys = sys;
81     ih->inhibit = Inhibit;
82     return VLC_SUCCESS;
83
84 error:
85     free (sys);
86     return VLC_EGENERIC;
87 }
88
89 static void Close (vlc_object_t *obj)
90 {
91     vlc_inhibit_t *ih = (vlc_inhibit_t *)obj;
92     vlc_inhibit_sys_t *sys = ih->p_sys;
93
94     vlc_timer_destroy (sys->timer);
95     dbus_connection_close (sys->conn);
96     dbus_connection_unref (sys->conn);
97     free (sys);
98 }
99
100 static void Inhibit (vlc_inhibit_t *ih, bool unblank)
101 {
102     vlc_inhibit_sys_t *sys = ih->p_sys;
103
104     /* The shortest blanking interval is 10s on N900, 15s on N9 */
105     const mtime_t interval = 9 * CLOCK_FREQ;
106     vlc_timer_schedule (sys->timer, false, unblank, interval);
107 }
108
109 /* NOTE: This plug-in could be compiled without MCE development files easily.
110  * But then it would get included on all platforms with D-Bus. */
111 #include <mce/dbus-names.h>
112
113 static void Timer (void *data)
114 {
115     DBusConnection *conn = data;
116     DBusMessage *msg = dbus_message_new_method_call (MCE_SERVICE,
117                                                      MCE_REQUEST_PATH,
118                                                      MCE_REQUEST_IF,
119                                                      MCE_DISPLAY_ON_REQ);
120     if (unlikely(msg == NULL))
121         return;
122
123     if (dbus_connection_send (conn, msg, NULL))
124         dbus_connection_flush (conn);
125     dbus_message_unref (msg);
126 }