]> www.fi.muni.cz Git - evince.git/blob - shell/ev-media-player-keys.c
[shell] Use new gdbus API for the mediaplayerkeys
[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 };
46
47 struct _EvMediaPlayerKeysClass
48 {
49         GObjectClass parent_class;
50
51         /* Signals */
52         void (* key_pressed) (EvMediaPlayerKeys *keys,
53                               const gchar       *key);
54 };
55
56 static guint signals[LAST_SIGNAL];
57
58 G_DEFINE_TYPE (EvMediaPlayerKeys, ev_media_player_keys, G_TYPE_OBJECT)
59
60 static void ev_media_player_keys_finalize (GObject *object);
61
62 static void
63 ev_media_player_keys_class_init (EvMediaPlayerKeysClass *klass)
64 {
65         GObjectClass *object_class = G_OBJECT_CLASS (klass);
66
67         object_class->finalize = ev_media_player_keys_finalize;
68
69         signals[KEY_PRESSED] =
70                 g_signal_new ("key_pressed",
71                               EV_TYPE_MEDIA_PLAYER_KEYS,
72                               G_SIGNAL_RUN_LAST,
73                               G_STRUCT_OFFSET (EvMediaPlayerKeysClass, key_pressed),
74                               NULL, NULL,
75                               g_cclosure_marshal_VOID__STRING,
76                               G_TYPE_NONE,
77                               1, G_TYPE_STRING);
78 }
79
80 static void
81 ev_media_player_keys_grab_keys (EvMediaPlayerKeys *keys)
82 {
83         /*
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).
87          */
88         g_dbus_proxy_call (keys->proxy,
89                            "GrabMediaPlayerKeys",
90                            g_variant_new ("(su)", "Evince", 1),
91                            G_DBUS_CALL_FLAGS_NO_AUTO_START,
92                            -1,
93                            NULL, NULL, NULL);
94 }
95
96 static void
97 ev_media_player_keys_release_keys (EvMediaPlayerKeys *keys)
98 {
99         g_dbus_proxy_call (keys->proxy,
100                            "ReleaseMediaPlayerKeys",
101                            g_variant_new ("(s)", "Evince"),
102                            G_DBUS_CALL_FLAGS_NO_AUTO_START,
103                            -1,
104                            NULL, NULL, NULL);
105 }
106
107 static void
108 media_player_key_pressed_cb (GDBusProxy *proxy,
109                              gchar      *sender_name,
110                              gchar      *signal_name,
111                              GVariant   *parameters,
112                              gpointer    user_data)
113 {
114         const char *application, *key;
115
116         if (g_strcmp0 (sender_name, SD_NAME) != 0)
117                 return;
118
119         if (g_strcmp0 (signal_name, "MediaPlayerKeyPressed") != 0)
120                 return;
121
122         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
123                 return;
124
125         g_variant_get (parameters, "(&s&s)", &application, &key);
126
127         if (strcmp ("Evince", application) == 0) {
128                 g_signal_emit (user_data, signals[KEY_PRESSED], 0, key);
129         }
130 }
131
132 static void
133 mediakeys_service_appeared_cb (GObject      *source_object,
134                                GAsyncResult *res,
135                                gpointer      user_data)
136 {
137         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (user_data);
138         GDBusProxy *proxy;
139
140         proxy = g_dbus_proxy_new_for_bus_finish (res, NULL);
141
142         if (proxy == NULL) {
143                 return;
144         }
145
146         g_signal_connect (proxy, "g-signal",
147                           G_CALLBACK (media_player_key_pressed_cb),
148                           keys);
149
150         keys->proxy = proxy;
151         ev_media_player_keys_grab_keys (keys);
152 }
153
154 static void
155 ev_media_player_keys_init (EvMediaPlayerKeys *keys)
156 {
157         g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION,
158                                   G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
159                                   NULL,
160                                   SD_NAME,
161                                   SD_OBJECT_PATH,
162                                   SD_INTERFACE,
163                                   NULL,
164                                   mediakeys_service_appeared_cb,
165                                   keys);
166 }
167
168 void
169 ev_media_player_keys_focused (EvMediaPlayerKeys *keys)
170 {
171         if (keys->proxy == NULL)
172                 return;
173         
174         ev_media_player_keys_grab_keys (keys);
175 }
176
177 static void
178 ev_media_player_keys_finalize (GObject *object)
179 {
180         EvMediaPlayerKeys *keys = EV_MEDIA_PLAYER_KEYS (object);
181
182         if (keys->proxy != NULL) {
183                 ev_media_player_keys_release_keys (keys);
184                 g_object_unref (keys->proxy);
185                 keys->proxy = NULL;
186         }
187
188         G_OBJECT_CLASS (ev_media_player_keys_parent_class)->finalize (object);
189 }
190
191 EvMediaPlayerKeys *
192 ev_media_player_keys_new (void)
193 {
194         return g_object_new (EV_TYPE_MEDIA_PLAYER_KEYS, NULL);
195 }
196