Toggle play/pause on play command.
[gmpc-mmkeys.git] / mmkeys.vala
blobb7b9818c3387ee6b225677754ca797f530ced253
1 /* Gnome Music Player Client Multimedia Keys plugin (gmpc-mmkeys)
2 * Copyright (C) 2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpc.wikia.com/
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 using GLib;
20 using Gmpc;
21 using Gmpc.Plugin;
23 [DBus (name = "org.gnome.SettingsDaemon.MediaKeys")]
24 interface MediaKeys : GLib.Object {
25 public abstract void GrabMediaPlayerKeys (string application, uint32 time);
26 public abstract void ReleaseMediaPlayerKeys (string application);
27 public signal void MediaPlayerKeyPressed (string application, string keys);
30 public class MMKeys : Gmpc.Plugin.Base {
31 public const int[] pversion = {0,0,0};
32 private DBus.Connection conn;
33 private MediaKeys keys;
34 /* Name */
35 public override weak string get_name() {
36 return "Gnome multimedia keys plugin";
38 /* Version */
39 public override weak int[] get_version() {
40 return pversion;
42 private void callback (MediaKeys mkeys, string application, string keys) {
43 if(this.get_enabled() == false) return;
44 if(application != "gmpc") return;
45 if(keys == "Play") {
46 if(MPD.Player.get_state(server) == MPD.Player.State.PLAY)
47 MPD.Player.pause(server);
48 else
49 MPD.Player.play(server);
51 else if(keys == "Pause") MPD.Player.pause(server);
52 else if(keys == "Next") MPD.Player.next(server);
53 else if(keys == "Previous") MPD.Player.prev(server);
54 else if(keys == "Stop") MPD.Player.stop(server);
56 construct {
57 /* Set the plugin as an internal one and of type pl_browser */
58 this.plugin_type = 4;
59 conn = DBus.Bus.get (DBus.BusType.SESSION);
60 keys = (MediaKeys) conn.get_object ( "org.gnome.SettingsDaemon",
61 "/org/gnome/SettingsDaemon/MediaKeys");
63 keys.GrabMediaPlayerKeys("gmpc", (uint)0);
64 keys.MediaPlayerKeyPressed += callback;
66 ~MMKeys() {
67 keys.ReleaseMediaPlayerKeys("gmpc");
70 [ModuleInit]
71 public Type plugin_get_type () {
72 return typeof (MMKeys);