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>
29 #include <dbus/dbus-glib.h>
31 #include "rb-plugin.h"
34 #define RB_TYPE_GPM_PLUGIN (rb_gpm_plugin_get_type ())
35 #define RB_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_GPM_PLUGIN, RBGPMPlugin))
36 #define RB_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
37 #define RB_IS_GPM_PLUGIN(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_GPM_PLUGIN))
38 #define RB_IS_GPM_PLUGIN_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_GPM_PLUGIN))
39 #define RB_GPM_PLUGIN_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_GPM_PLUGIN, RBGPMPluginClass))
53 RBPluginClass parent_class
;
56 G_MODULE_EXPORT GType
register_rb_plugin (GTypeModule
*module
);
57 GType
rb_gpm_plugin_get_type (void) G_GNUC_CONST
;
59 static void rb_gpm_plugin_init (RBGPMPlugin
*plugin
);
60 static void impl_activate (RBPlugin
*plugin
, RBShell
*shell
);
61 static void impl_deactivate (RBPlugin
*plugin
, RBShell
*shell
);
63 RB_PLUGIN_REGISTER(RBGPMPlugin
, rb_gpm_plugin
)
66 rb_gpm_plugin_class_init (RBGPMPluginClass
*klass
)
68 RBPluginClass
*plugin_class
= RB_PLUGIN_CLASS (klass
);
69 plugin_class
->activate
= impl_activate
;
70 plugin_class
->deactivate
= impl_deactivate
;
74 rb_gpm_plugin_init (RBGPMPlugin
*plugin
)
76 rb_debug ("RBGPMPlugin initialising");
80 ignore_error (GError
*error
)
85 /* ignore 'no such service' type errors */
86 if (error
->domain
== DBUS_GERROR
) {
87 if (error
->code
== DBUS_GERROR_NAME_HAS_NO_OWNER
||
88 error
->code
== DBUS_GERROR_SERVICE_UNKNOWN
)
96 inhibit_cb (DBusGProxy
*proxy
,
97 DBusGProxyCall
*call_id
,
100 GError
*error
= NULL
;
102 dbus_g_proxy_end_call (proxy
,
105 G_TYPE_UINT
, &plugin
->cookie
,
108 if (!ignore_error (error
)) {
109 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
112 rb_debug ("inhibit failed: %s", error
->message
);
114 g_error_free (error
);
116 rb_debug ("got cookie %u", plugin
->cookie
);
119 g_object_unref (plugin
);
123 inhibit (RBGPMPlugin
*plugin
)
125 plugin
->timeout_id
= 0;
126 if (plugin
->proxy
== NULL
)
129 if (plugin
->cookie
!= 0) {
130 rb_debug ("Was going to inhibit gnome-power-manager, but we already have done");
134 rb_debug ("inhibiting");
135 g_object_ref (plugin
);
136 dbus_g_proxy_begin_call (plugin
->proxy
, "Inhibit",
137 (DBusGProxyCallNotify
) inhibit_cb
,
140 G_TYPE_STRING
, _("Music Player"),
141 G_TYPE_STRING
, _("Playing"),
148 uninhibit_cb (DBusGProxy
*proxy
,
149 DBusGProxyCall
*call_id
,
152 GError
*error
= NULL
;
154 dbus_g_proxy_end_call (proxy
,
159 if (!ignore_error (error
)) {
160 g_warning ("Failed to invoke org.gnome.PowerManager.Inhibit: %s",
163 rb_debug ("uninhibit failed: %s", error
->message
);
165 g_error_free (error
);
167 rb_debug ("uninhibited");
171 g_object_unref (plugin
);
175 uninhibit (RBGPMPlugin
*plugin
)
177 plugin
->timeout_id
= 0;
178 if (plugin
->proxy
== NULL
)
181 if (plugin
->cookie
== 0) {
182 rb_debug ("Was going to uninhibit power manager, but we haven't inhibited it");
186 rb_debug ("uninhibiting; cookie = %u", plugin
->cookie
);
187 g_object_ref (plugin
);
188 dbus_g_proxy_begin_call (plugin
->proxy
, "UnInhibit",
189 (DBusGProxyCallNotify
) uninhibit_cb
,
192 G_TYPE_UINT
, plugin
->cookie
,
198 playing_changed_cb (GObject
*player
, gboolean playing
, RBGPMPlugin
*plugin
)
200 if (plugin
->timeout_id
!= 0) {
201 g_source_remove (plugin
->timeout_id
);
202 plugin
->timeout_id
= 0;
205 /* small delay to avoid uninhibit/inhibit
206 * cycles when changing sources etc.
208 plugin
->timeout_id
= g_timeout_add (1000,
209 (GSourceFunc
) (playing
? inhibit
: uninhibit
),
214 impl_activate (RBPlugin
*rbplugin
,
218 GError
*error
= NULL
;
219 DBusGConnection
*bus
;
220 GObject
*shell_player
;
223 plugin
= RB_GPM_PLUGIN (rbplugin
);
225 bus
= dbus_g_bus_get (DBUS_BUS_SESSION
, &error
);
227 g_warning ("Couldn't connect to system bus: %s", (error
) ? error
->message
: "(null)");
231 plugin
->proxy
= dbus_g_proxy_new_for_name (bus
,
232 "org.gnome.PowerManager",
233 "/org/gnome/PowerManager",
234 "org.gnome.PowerManager");
236 g_object_get (shell
, "shell-player", &shell_player
, NULL
);
238 plugin
->handler_id
= g_signal_connect (shell_player
,
240 (GCallback
) playing_changed_cb
,
243 g_object_get (shell_player
, "playing", &playing
, NULL
);
248 g_object_unref (shell_player
);
252 impl_deactivate (RBPlugin
*rbplugin
,
256 GObject
*shell_player
;
258 plugin
= RB_GPM_PLUGIN (rbplugin
);
260 if (plugin
->timeout_id
!= 0) {
261 g_source_remove (plugin
->timeout_id
);
262 plugin
->timeout_id
= 0;
265 if (plugin
->cookie
!= 0) {
270 g_object_get (shell
, "shell-player", &shell_player
, NULL
);
272 g_signal_handler_disconnect (shell_player
, plugin
->handler_id
);
273 plugin
->handler_id
= 0;
275 g_object_unref (shell_player
);
277 g_object_unref (plugin
->proxy
);
278 plugin
->proxy
= NULL
;