1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007 Jan Arne Petersen <jap@gnome.org>
4 * Copyright (C) 2008 Bastien Nocera <hadess@hadess.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "ev-media-player-keys.h"
30 #define SD_NAME "org.gnome.SettingsDaemon"
31 #define SD_OBJECT_PATH "/org/gnome/SettingsDaemon/MediaKeys"
32 #define SD_INTERFACE "org.gnome.SettingsDaemon.MediaKeys"
44 struct _EvMediaPlayerKeys
48 GDBusConnection *connection;
50 guint subscription_id;
53 struct _EvMediaPlayerKeysClass
55 GObjectClass parent_class;
58 void (* key_pressed) (EvMediaPlayerKeys *keys,
62 static guint signals[LAST_SIGNAL];
64 G_DEFINE_TYPE (EvMediaPlayerKeys, ev_media_player_keys, G_TYPE_OBJECT)
66 static void ev_media_player_keys_set_property (GObject *object,
70 static void ev_media_player_keys_constructed (GObject *object);
71 static void ev_media_player_keys_finalize (GObject *object);
74 ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 object_class->set_property = ev_media_player_keys_set_property;
79 object_class->constructed = ev_media_player_keys_constructed;
80 object_class->finalize = ev_media_player_keys_finalize;
82 signals[KEY_PRESSED] =
83 g_signal_new ("key_pressed",
84 EV_TYPE_MEDIA_PLAYER_KEYS,
86 G_STRUCT_OFFSET (EvMediaPlayerKeysClass, key_pressed),
88 g_cclosure_marshal_VOID__STRING,
92 g_object_class_install_property (object_class,
94 g_param_spec_object ("connection", NULL, NULL,
95 G_TYPE_DBUS_CONNECTION,
97 G_PARAM_CONSTRUCT_ONLY |
98 G_PARAM_STATIC_STRINGS));
102 ev_media_player_keys_grab_keys (EvMediaPlayerKeys *keys)
105 * The uint as second argument is time. We give a very low value so that
106 * if a media player is there it gets higher priority on the keys (0 is
107 * a special value having maximum priority).
109 g_dbus_connection_call (keys->connection,
113 "GrabMediaPlayerKeys",
114 g_variant_new ("(su)", "Evince", 1),
115 G_DBUS_CALL_FLAGS_NO_AUTO_START,
121 ev_media_player_keys_release_keys (EvMediaPlayerKeys *keys)
123 g_dbus_connection_call (keys->connection,
127 "ReleaseMediaPlayerKeys",
128 g_variant_new ("(s)", "Evince"),
129 G_DBUS_CALL_FLAGS_NO_AUTO_START,
135 media_player_key_pressed_cb (GDBusConnection *connection,
136 const gchar *sender_name,
137 const gchar *object_path,
138 const gchar *interface_name,
139 const gchar *signal_name,
140 GVariant *parameters,
143 const char *application, *key;
145 if (g_strcmp0 (sender_name, SD_NAME) != 0)
147 if (g_strcmp0 (signal_name, "MediaPlayerKeyPressed") != 0)
150 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
153 g_variant_get (parameters, "(&s&s)", &application, &key);
155 if (strcmp ("Evince", application) == 0) {
156 g_signal_emit (user_data, signals[KEY_PRESSED], 0, key);
161 mediakeys_service_appeared_cb (GDBusConnection *connection,
163 const char *name_owner,
166 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
168 g_assert (connection == keys->connection);
170 keys->subscription_id = g_dbus_connection_signal_subscribe (keys->connection,
173 "MediaPlayerKeyPressed",
176 media_player_key_pressed_cb,
179 ev_media_player_keys_grab_keys (keys);
183 mediakeys_service_disappeared_cb (GDBusConnection *connection,
187 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
189 g_assert (connection == keys->connection);
191 if (keys->subscription_id != 0) {
192 g_dbus_connection_signal_unsubscribe (connection, keys->subscription_id);
193 keys->subscription_id = 0;
198 ev_media_player_keys_init (EvMediaPlayerKeys *keys)
203 ev_media_player_keys_constructed (GObject *object)
205 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
207 keys->watch_id = g_bus_watch_name_on_connection (keys->connection,
209 G_BUS_NAME_WATCHER_FLAGS_NONE,
210 mediakeys_service_appeared_cb,
211 mediakeys_service_disappeared_cb,
216 ev_media_player_keys_focused (EvMediaPlayerKeys *keys)
218 if (keys->connection == NULL)
221 ev_media_player_keys_grab_keys (keys);
225 ev_media_player_keys_finalize (GObject *object)
227 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
229 ev_media_player_keys_release_keys (keys);
231 g_bus_unwatch_name (keys->watch_id);
233 if (keys->subscription_id != 0) {
234 g_dbus_connection_signal_unsubscribe (keys->connection, keys->subscription_id);
237 if (keys->connection != NULL) {
238 g_object_unref (keys->connection);
241 G_OBJECT_CLASS (ev_media_player_keys_parent_class)->finalize (object);
245 ev_media_player_keys_set_property (GObject *object,
250 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
253 case PROP_CONNECTION:
254 keys->connection = g_value_dup_object (value);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 ev_media_player_keys_new (GDBusConnection *connection)
265 return g_object_new (EV_TYPE_MEDIA_PLAYER_KEYS,
266 "connection", connection,