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>
5 * Copyright © 2010 Christian Persch
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "ev-media-player-keys.h"
31 #define SD_NAME "org.gnome.SettingsDaemon"
32 #define SD_OBJECT_PATH "/org/gnome/SettingsDaemon/MediaKeys"
33 #define SD_INTERFACE "org.gnome.SettingsDaemon.MediaKeys"
40 struct _EvMediaPlayerKeys
47 struct _EvMediaPlayerKeysClass
49 GObjectClass parent_class;
52 void (* key_pressed) (EvMediaPlayerKeys *keys,
56 static guint signals[LAST_SIGNAL];
58 G_DEFINE_TYPE (EvMediaPlayerKeys, ev_media_player_keys, G_TYPE_OBJECT)
60 static void ev_media_player_keys_finalize (GObject *object);
63 ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
65 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67 object_class->finalize = ev_media_player_keys_finalize;
69 signals[KEY_PRESSED] =
70 g_signal_new ("key_pressed",
71 EV_TYPE_MEDIA_PLAYER_KEYS,
73 G_STRUCT_OFFSET (EvMediaPlayerKeysClass, key_pressed),
75 g_cclosure_marshal_VOID__STRING,
81 ev_media_player_keys_grab_keys (EvMediaPlayerKeys *keys)
84 * The uint as second argument is time. We give a very low value so that
85 * if a media player is there it gets higher priority on the keys (0 is
86 * a special value having maximum priority).
88 g_dbus_proxy_call (keys->proxy,
89 "GrabMediaPlayerKeys",
90 g_variant_new ("(su)", "Evince", 1),
91 G_DBUS_CALL_FLAGS_NO_AUTO_START,
97 ev_media_player_keys_release_keys (EvMediaPlayerKeys *keys)
99 g_dbus_proxy_call (keys->proxy,
100 "ReleaseMediaPlayerKeys",
101 g_variant_new ("(s)", "Evince"),
102 G_DBUS_CALL_FLAGS_NO_AUTO_START,
108 media_player_key_pressed_cb (GDBusProxy *proxy,
111 GVariant *parameters,
114 const char *application, *key;
116 if (g_strcmp0 (sender_name, SD_NAME) != 0)
119 if (g_strcmp0 (signal_name, "MediaPlayerKeyPressed") != 0)
122 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
125 g_variant_get (parameters, "(&s&s)", &application, &key);
127 if (strcmp ("Evince", application) == 0) {
128 g_signal_emit (user_data, signals[KEY_PRESSED], 0, key);
133 mediakeys_service_appeared_cb (GObject *source_object,
137 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
140 proxy = g_dbus_proxy_new_for_bus_finish (res, NULL);
146 g_signal_connect (proxy, "g-signal",
147 G_CALLBACK (media_player_key_pressed_cb),
151 ev_media_player_keys_grab_keys (keys);
155 ev_media_player_keys_init (EvMediaPlayerKeys *keys)
157 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
158 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
164 mediakeys_service_appeared_cb,
169 ev_media_player_keys_focused (EvMediaPlayerKeys *keys)
171 if (keys->proxy == NULL)
174 ev_media_player_keys_grab_keys (keys);
178 ev_media_player_keys_finalize (GObject *object)
180 EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
182 if (keys->proxy != NULL) {
183 ev_media_player_keys_release_keys (keys);
184 g_object_unref (keys->proxy);
188 G_OBJECT_CLASS (ev_media_player_keys_parent_class)->finalize (object);
192 ev_media_player_keys_new (void)
194 return g_object_new (EV_TYPE_MEDIA_PLAYER_KEYS, NULL);