gmpc-awn version 0.19.96
[gmpc-awn.git] / src / plugin.c
blob2b06e54b611f21be209b4c924aa5ab3135733268
1 /* gmpc-awn (GMPC plugin)
2 * Copyright (C) 2007-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.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdio.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <glib/gi18n-lib.h>
28 #include <gdk/gdkx.h>
29 #include <gmpc/plugin.h>
30 #include <gmpc/metadata.h>
31 #include <gmpc/misc.h>
32 #include <dbus/dbus-glib.h>
33 #include <dbus/dbus-glib-bindings.h>
35 static void awn_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, MetaData *met);
36 static int awn_get_enabled(void)
38 return cfg_get_single_value_as_int_with_default(config, "awn-plugin", "enable", TRUE);
40 static void awn_set_enabled(int enabled)
42 cfg_set_single_value_as_int(config, "awn-plugin", "enable", enabled);
45 /* from gseal-transition.h */
46 #ifndef GSEAL
47 #define gtk_widget_get_window(x) (x)->window
48 #endif
50 static XID get_main_window_xid() {
51 GtkWidget* window = NULL;
53 window = playlist3_get_window();
55 if (!window || playlist3_window_is_hidden()) {
56 return None;
59 return GDK_WINDOW_XID(gtk_widget_get_window(window));
62 static void setAwnIcon(const char *filename) {
63 XID xid;
64 DBusGConnection *connection;
65 DBusGProxy *proxy;
66 GError *error;
68 xid = get_main_window_xid();
70 if (xid == None) {
71 return;
74 error = NULL;
75 connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
77 if (connection != NULL)
79 proxy = dbus_g_proxy_new_for_name(
80 connection,
81 "com.google.code.Awn",
82 "/com/google/code/Awn",
83 "com.google.code.Awn");
84 error = NULL;
85 dbus_g_proxy_call(proxy, "SetTaskIconByXid", &error, G_TYPE_INT64,
86 (gint64)xid, G_TYPE_STRING, filename, G_TYPE_INVALID);
90 static void unsetAwnIcon() {
91 XID xid;
92 DBusGConnection *connection;
93 DBusGProxy *proxy;
94 GError *error;
96 xid = get_main_window_xid();
98 if (xid == None) {
99 return;
102 error = NULL;
103 connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
105 if (connection != NULL)
107 proxy = dbus_g_proxy_new_for_name(
108 connection,
109 "com.google.code.Awn",
110 "/com/google/code/Awn",
111 "com.google.code.Awn");
112 error = NULL;
113 dbus_g_proxy_call(proxy, "UnsetTaskIconByXid", &error,
114 G_TYPE_INT64, (gint64)xid, G_TYPE_INVALID);
117 static void setAwnProgress(int progress) {
118 XID xid;
119 DBusGConnection *connection;
120 DBusGProxy *proxy;
121 GError *error;
123 xid = get_main_window_xid();
125 if (xid == None) {
126 return;
129 error = NULL;
130 connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
132 if (connection != NULL)
134 proxy = dbus_g_proxy_new_for_name(
135 connection,
136 "com.google.code.Awn",
137 "/com/google/code/Awn",
138 "com.google.code.Awn");
139 error = NULL;
140 dbus_g_proxy_call(proxy, "SetProgressByXid", &error,G_TYPE_INT64, (gint64)xid,
141 G_TYPE_INT, progress, G_TYPE_INVALID);
145 * Destroy the plugin
147 static void awn_plugin_destroy(void)
153 static void awn_plugin_init(void)
155 bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
156 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
157 g_signal_connect(G_OBJECT(gmw), "data-changed", G_CALLBACK(awn_update_cover), NULL);
160 static void awn_update_cover(GmpcMetaWatcher *gmv, mpd_Song *song, MetaDataType type, MetaDataResult ret, MetaData *met)
162 mpd_Song *song2;
163 if(!awn_get_enabled())
164 return;
166 song2 = mpd_playlist_get_current_song(connection);
168 unsetAwnIcon();
169 if(!song2 || type != META_ALBUM_ART || !gmpc_meta_watcher_match_data(META_ALBUM_ART, song2, song))
170 return;
171 if(ret == META_DATA_AVAILABLE) {
172 if(met->content_type == META_DATA_CONTENT_URI)
174 const gchar *path = meta_data_get_uri(met);
175 setAwnIcon(path);
182 static void awn_song_changed(MpdObj *mi)
184 MetaDataResult ret;
185 MetaData *met = NULL;
186 mpd_Song *song = NULL;
188 song = mpd_playlist_get_current_song(mi);
189 ret = gmpc_meta_watcher_get_meta_path(gmw,song, META_ALBUM_ART,&met);
190 awn_update_cover(gmw, song, META_ALBUM_ART, ret, met);
191 if(met)
192 meta_data_free(met);
195 /* mpd changed */
196 static void awn_mpd_status_changed(MpdObj *mi, ChangedStatusType what, void *data)
198 if(!awn_get_enabled())
199 return;
200 if(what&(MPD_CST_SONGID))
201 awn_song_changed(mi);
202 if(what&(MPD_CST_ELAPSED_TIME|MPD_CST_TOTAL_TIME))
205 int totalTime = mpd_status_get_total_song_time(connection);
206 int elapsedTime = mpd_status_get_elapsed_song_time(connection);
207 gdouble progress = elapsedTime/(gdouble)MAX(totalTime,1)*100;
208 if((int)progress == 0)
209 progress = 100;
210 setAwnProgress((int)progress);
214 static const gchar* awn_get_translation_domain(void) {
215 return GETTEXT_PACKAGE;
218 int plugin_api_version = PLUGIN_API_VERSION;
219 /* main plugin_awn info */
220 gmpcPlugin plugin = {
221 .name = N_("Avant Window Navigator Plugin"),
222 .version = { MAJOR_VERSION, MINOR_VERSION, MICRO_VERSION },
223 .plugin_type = GMPC_PLUGIN_NO_GUI,
224 .init = awn_plugin_init,
225 .destroy = awn_plugin_destroy,
226 .mpd_status_changed = &awn_mpd_status_changed,
227 .get_enabled = awn_get_enabled,
228 .set_enabled = awn_set_enabled,
229 .get_translation_domain = awn_get_translation_domain