1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Copyright (C) 2006 Jonathan Matthew <jonathan@kaolin.wh9.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22 * gnome-power-manager integration.
23 * currently consists of inhibiting suspend while playing.
28 #include <glib/gi18n.h>
30 #include <dbus/dbus-glib.h>
33 #include "rb-plugin.h"
36 #define RB_TYPE_GPM_PLUGIN (rb_gpm_plugin_get_type ())
37 #define RB_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_GPM_PLUGIN, RBGPMPlugin))
38 #define RB_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
39 #define RB_IS_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_GPM_PLUGIN))
40 #define RB_IS_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_GPM_PLUGIN))
41 #define RB_GPM_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
55 RBPluginClass parent_class
;
58 G_MODULE_EXPORT GType
register_rb_plugin (GTypeModule
*module
);
59 GType
rb_gpm_plugin_get_type (void) G_GNUC_CONST
;
61 static void rb_gpm_plugin_init (RBGPMPlugin
*plugin
);
62 static void impl_activate (RBPlugin
*plugin
, RBShell
*shell
);
63 static void impl_deactivate (RBPlugin
*plugin
, RBShell
*shell
);
65 RB_PLUGIN_REGISTER(RBGPMPlugin
, rb_gpm_plugin
)
68 rb_gpm_plugin_class_init (RBGPMPluginClass
*klass
)
70 RBPluginClass
*plugin_class
= RB_PLUGIN_CLASS (klass
);
71 plugin_class
->activate
= impl_activate
;
72 plugin_class
->deactivate
= impl_deactivate
;
76 rb_gpm_plugin_init (RBGPMPlugin
*plugin
)
78 rb_debug ("RBGPMPlugin initialising");
82 ignore_error (GError
*error
)
87 /* ignore 'no such service' type errors */
88 if (error
->domain
== DBUS_GERROR
) {
89 if (error
->code
== DBUS_GERROR_NAME_HAS_NO_OWNER
||
90 error
->code
== DBUS_GERROR_SERVICE_UNKNOWN
)
98 inhibit_cb (DBusGProxy
*proxy
,
99 DBusGProxyCall
*call_id
,
102 GError
*error
= NULL
;
104 dbus_g_proxy_end_call (proxy
,
107 G_TYPE_UINT
, &plugin
->cookie
,
110 if (!ignore_error (error
)) {
111 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
114 rb_debug ("inhibit failed: %s", error
->message
);
116 g_error_free (error
);
118 rb_debug ("got cookie %u", plugin
->cookie
);
121 g_object_unref (plugin
);
125 inhibit (RBGPMPlugin
*plugin
)
127 plugin
->timeout_id
= 0;
128 if (plugin
->proxy
== NULL
)
131 if (plugin
->cookie
!= 0) {
132 rb_debug ("Was going to inhibit gnome-power-manager, but we already have done");
136 rb_debug ("inhibiting");
137 g_object_ref (plugin
);
138 dbus_g_proxy_begin_call (plugin
->proxy
, "Inhibit",
139 (DBusGProxyCallNotify
) inhibit_cb
,
142 G_TYPE_STRING
, _("Music Player"),
143 G_TYPE_STRING
, _("Playing"),
150 uninhibit_cb (DBusGProxy
*proxy
,
151 DBusGProxyCall
*call_id
,
154 GError
*error
= NULL
;
156 dbus_g_proxy_end_call (proxy
,
161 if (!ignore_error (error
)) {
162 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
165 rb_debug ("uninhibit failed: %s", error
->message
);
167 g_error_free (error
);
169 rb_debug ("uninhibited");
173 g_object_unref (plugin
);
177 uninhibit (RBGPMPlugin
*plugin
)
179 plugin
->timeout_id
= 0;
180 if (plugin
->proxy
== NULL
)
183 if (plugin
->cookie
== 0) {
184 rb_debug ("Was going to uninhibit power manager, but we haven't inhibited it");
188 rb_debug ("uninhibiting; cookie = %u", plugin
->cookie
);
189 g_object_ref (plugin
);
190 dbus_g_proxy_begin_call (plugin
->proxy
, "UnInhibit",
191 (DBusGProxyCallNotify
) uninhibit_cb
,
194 G_TYPE_UINT
, plugin
->cookie
,
200 playing_changed_cb (GObject
*player
, gboolean playing
, RBGPMPlugin
*plugin
)
202 if (plugin
->timeout_id
!= 0) {
203 g_source_remove (plugin
->timeout_id
);
204 plugin
->timeout_id
= 0;
207 /* small delay to avoid uninhibit/inhibit
208 * cycles when changing sources etc.
210 plugin
->timeout_id
= g_timeout_add (1000,
211 (GSourceFunc
) (playing
? inhibit
: uninhibit
),
216 impl_activate (RBPlugin
*rbplugin
,
220 GError
*error
= NULL
;
221 DBusGConnection
*bus
;
222 GObject
*shell_player
;
225 plugin
= RB_GPM_PLUGIN (rbplugin
);
227 bus
= dbus_g_bus_get (DBUS_BUS_SESSION
, &error
);
229 g_warning ("Couldn't connect to system bus: %s", (error
) ? error
->message
: "(null)");
233 plugin
->proxy
= dbus_g_proxy_new_for_name (bus
,
234 "org.gnome.PowerManager",
235 "/org/gnome/PowerManager",
236 "org.gnome.PowerManager");
238 g_object_get (shell
, "shell-player", &shell_player
, NULL
);
240 plugin
->handler_id
= g_signal_connect (shell_player
,
242 (GCallback
) playing_changed_cb
,
245 g_object_get (shell_player
, "playing", &playing
, NULL
);
250 g_object_unref (shell_player
);
254 impl_deactivate (RBPlugin
*rbplugin
,
258 GObject
*shell_player
;
260 plugin
= RB_GPM_PLUGIN (rbplugin
);
262 if (plugin
->timeout_id
!= 0) {
263 g_source_remove (plugin
->timeout_id
);
264 plugin
->timeout_id
= 0;
267 if (plugin
->cookie
!= 0) {
272 g_object_get (shell
, "shell-player", &shell_player
, NULL
);
274 g_signal_handler_disconnect (shell_player
, plugin
->handler_id
);
275 plugin
->handler_id
= 0;
277 g_object_unref (shell_player
);
279 g_object_unref (plugin
->proxy
);
280 plugin
->proxy
= NULL
;