]> www.fi.muni.cz Git - evince.git/blob - shell/ev-media-player-keys.c
[shell] Use g_bus_watch_proxy instead of g_bus_watch_name in media-keys
[evince.git] / shell / ev-media-player-keys.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* 
3  * Copyright (C) 2007 Jan Arne Petersen <jap@gnome.org>
4  * Copyright (C) 2008 Bastien Nocera <hadess@hadess.net>
5  * Copyright © 2010 Christian Persch
6  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #include "config.h"
24
25 #include "ev-media-player-keys.h"
26
27 #include <string.h>
28 #include <glib.h>
29 #include <gio/gio.h>
30
31 #define SD_NAME        "org.gnome.SettingsDaemon"
32 #define SD_OBJECT_PATH "/org/gnome/SettingsDaemon/MediaKeys"
33 #define SD_INTERFACE   "org.gnome.SettingsDaemon.MediaKeys"
34
35 enum {
36         KEY_PRESSED,
37         LAST_SIGNAL
38 };
39
40 struct _EvMediaPlayerKeys
41 {
42         GObject        parent;
43
44         GDBusProxy *proxy;
45         guint watch_id;
46 };
47
48 struct _EvMediaPlayerKeysClass
49 {
50         GObjectClass parent_class;
51
52         /* Signals */
53         void (* key_pressed) (EvMediaPlayerKeys *keys,
54                               const gchar       *key);
55 };
56
57 static guint signals[LAST_SIGNAL];
58
59 G_DEFINE_TYPE (EvMediaPlayerKeys, ev_media_player_keys, G_TYPE_OBJECT)
60
61 static void ev_media_player_keys_finalize (GObject *object);
62
63 static void
64 ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
65 {
66         GObjectClass *object_class = G_OBJECT_CLASS (klass);
67
68         object_class->finalize = ev_media_player_keys_finalize;
69
70         signals[KEY_PRESSED] =
71                 g_signal_new ("key_pressed",
72                               EV_TYPE_MEDIA_PLAYER_KEYS,
73                               G_SIGNAL_RUN_LAST,
74                               G_STRUCT_OFFSET (EvMediaPlayerKeysClass, key_pressed),
75                               NULL, NULL,
76                               g_cclosure_marshal_VOID__STRING,
77                               G_TYPE_NONE,
78                               1, G_TYPE_STRING);
79 }
80
81 static void
82 ev_media_player_keys_grab_keys (EvMediaPlayerKeys *keys)
83 {
84         /*
85          * The uint as second argument is time. We give a very low value so that
86          * if a media player is there it gets higher priority on the keys (0 is
87          * a special value having maximum priority).
88          */
89         g_dbus_proxy_call (keys->proxy,
90                            "GrabMediaPlayerKeys",
91                            g_variant_new ("(su)", "Evince", 1),
92                            G_DBUS_CALL_FLAGS_NO_AUTO_START,
93                            -1,
94                            NULL, NULL, NULL);
95 }
96
97 static void
98 ev_media_player_keys_release_keys (EvMediaPlayerKeys *keys)
99 {
100         g_dbus_proxy_call (keys->proxy,
101                            "ReleaseMediaPlayerKeys",
102                            g_variant_new ("(s)", "Evince"),
103                            G_DBUS_CALL_FLAGS_NO_AUTO_START,
104                            -1,
105                            NULL, NULL, NULL);
106 }
107
108 static void
109 media_player_key_pressed_cb (GDBusProxy *proxy,
110                              gchar      *sender_name,
111                              gchar      *signal_name,
112                              GVariant   *parameters,
113                              gpointer    user_data)
114 {
115         const char *application, *key;
116
117         if (g_strcmp0 (sender_name, SD_NAME) != 0)
118                 return;
119
120         if (g_strcmp0 (signal_name, "MediaPlayerKeyPressed") != 0)
121                 return;
122
123         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
124                 return;
125
126         g_variant_get (parameters, "(&s&s)", &application, &key);
127
128         if (strcmp ("Evince", application) == 0) {
129                 g_signal_emit (user_data, signals[KEY_PRESSED], 0, key);
130         }
131 }
132
133 static void
134 mediakeys_service_appeared_cb (GDBusConnection *connection,
135                                const gchar     *name,
136                                const gchar     *name_owner,
137                                GDBusProxy      *proxy,
138                                gpointer         user_data)
139 {
140         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
141
142         keys->proxy = g_object_ref (proxy);
143         g_signal_connect (keys->proxy, "g-signal",
144                           G_CALLBACK (media_player_key_pressed_cb),
145                           keys);
146
147         ev_media_player_keys_grab_keys (keys);
148 }
149
150 static void
151 mediakeys_service_disappeared_cb (GDBusConnection *connection,
152                                   const gchar     *name,
153                                   gpointer         user_data)
154 {
155         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
156
157         if (keys->proxy) {
158                 g_object_unref (keys->proxy);
159                 keys->proxy = NULL;
160         }
161 }
162
163 static void
164 ev_media_player_keys_init (EvMediaPlayerKeys *keys)
165 {
166         keys->watch_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
167                                             SD_NAME,
168                                             G_BUS_NAME_WATCHER_FLAGS_NONE,
169                                             SD_OBJECT_PATH,
170                                             SD_INTERFACE,
171                                             G_TYPE_DBUS_PROXY,
172                                             G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
173                                             mediakeys_service_appeared_cb,
174                                             mediakeys_service_disappeared_cb,
175                                             keys, NULL);
176 }
177
178 void
179 ev_media_player_keys_focused (EvMediaPlayerKeys *keys)
180 {
181         if (keys->proxy == NULL)
182                 return;
183         
184         ev_media_player_keys_grab_keys (keys);
185 }
186
187 static void
188 ev_media_player_keys_finalize (GObject *object)
189 {
190         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
191
192         ev_media_player_keys_release_keys (keys);
193
194         if (keys->watch_id > 0)
195                 g_bus_unwatch_name (keys->watch_id);
196
197         if (keys->proxy != NULL)
198                 g_object_unref (keys->proxy);
199
200         G_OBJECT_CLASS (ev_media_player_keys_parent_class)->finalize (object);
201 }
202
203 EvMediaPlayerKeys *
204 ev_media_player_keys_new (void)
205 {
206         return g_object_new (EV_TYPE_MEDIA_PLAYER_KEYS, NULL);
207 }
208