1 |
|
/*
|
|
1 |
/*
|
2 |
2 |
* osd.c
|
3 |
3 |
*
|
4 |
4 |
* Copyright (C) 2010 Maximilian Bogner <max@mbogner.de>
|
... | ... | |
19 |
19 |
|
20 |
20 |
#include <glib.h>
|
21 |
21 |
#include <audacious/debug.h>
|
|
22 |
//#define AUDDBG(...) do {printf ("%s:%d %s(): ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__);} while (0)
|
22 |
23 |
#include <libnotify/notify.h>
|
|
24 |
#include <audacious/playlist.h>
|
|
25 |
#include <audacious/drct.h>
|
|
26 |
#include <libaudgui/libaudgui-gtk.h>
|
|
27 |
#include <audacious/i18n.h>
|
|
28 |
#include <audacious/misc.h>
|
23 |
29 |
|
24 |
30 |
#include "osd.h"
|
25 |
31 |
|
26 |
32 |
NotifyNotification *notification = NULL;
|
|
33 |
gboolean notify_supports_actions = FALSE;
|
|
34 |
gboolean notify_supports_icon_buttons = FALSE;
|
|
35 |
gboolean notify_supports_persistence = FALSE;
|
27 |
36 |
|
28 |
37 |
gboolean osd_init() {
|
29 |
38 |
notification = NULL;
|
30 |
|
return notify_init ("Audacious");
|
|
39 |
if (notify_is_initted () == FALSE) {
|
|
40 |
GList *caps;
|
|
41 |
|
|
42 |
if (notify_init ("Audacious") == FALSE) {
|
|
43 |
g_warning ("libnotify initialization failed");
|
|
44 |
return FALSE;
|
|
45 |
}
|
|
46 |
|
|
47 |
/* ask the notification server if it supports actions */
|
|
48 |
caps = notify_get_server_caps ();
|
|
49 |
if (g_list_find_custom (caps, "actions", (GCompareFunc)g_strcmp0) != NULL) {
|
|
50 |
AUDDBG("notification server supports actions\n");
|
|
51 |
notify_supports_actions = TRUE;
|
|
52 |
|
|
53 |
if (g_list_find_custom (caps, "action-icons", (GCompareFunc)g_strcmp0) != NULL) {
|
|
54 |
AUDDBG("notifiction server supports icon buttons\n");
|
|
55 |
notify_supports_icon_buttons = TRUE;
|
|
56 |
}
|
|
57 |
} else {
|
|
58 |
AUDDBG("notification server does not support actions\n");
|
|
59 |
}
|
|
60 |
if (g_list_find_custom (caps, "persistence", (GCompareFunc)g_strcmp0) != NULL) {
|
|
61 |
AUDDBG("notification server supports persistence\n");
|
|
62 |
notify_supports_persistence = TRUE;
|
|
63 |
} else {
|
|
64 |
AUDDBG("notification server does not support persistence\n");
|
|
65 |
}
|
|
66 |
g_list_free_full(caps, g_free);
|
|
67 |
|
|
68 |
}
|
|
69 |
AUDDBG("notification created\n");
|
|
70 |
notification = notify_notification_new ("Notification", NULL, "audacious");
|
|
71 |
return TRUE;
|
31 |
72 |
}
|
32 |
73 |
|
33 |
74 |
void osd_uninit (void)
|
34 |
75 |
{
|
35 |
76 |
if (notification)
|
36 |
77 |
{
|
|
78 |
notify_notification_clear_hints(notification);
|
|
79 |
notify_notification_clear_actions(notification);
|
|
80 |
GError *error = NULL;
|
|
81 |
if (notify_notification_close(notification, &error) == FALSE)
|
|
82 |
{
|
|
83 |
AUDDBG("Failed to close notification : %s", error->message);
|
|
84 |
g_error_free (error);
|
|
85 |
}
|
37 |
86 |
g_object_unref (notification);
|
38 |
87 |
notification = NULL;
|
39 |
88 |
}
|
... | ... | |
41 |
90 |
notify_uninit();
|
42 |
91 |
}
|
43 |
92 |
|
44 |
|
void osd_closed_handler(NotifyNotification *notification2, gpointer data) {
|
45 |
|
if(notification != NULL) {
|
46 |
|
g_object_unref(notification);
|
47 |
|
notification = NULL;
|
48 |
|
AUDDBG("notification unrefed!\n");
|
49 |
|
}
|
|
93 |
static void
|
|
94 |
goprevious (NotifyNotification *notification,
|
|
95 |
const char *action,
|
|
96 |
gpointer user_data)
|
|
97 |
{
|
|
98 |
aud_drct_pl_prev();
|
|
99 |
}
|
|
100 |
|
|
101 |
static void
|
|
102 |
gonext (NotifyNotification *notification,
|
|
103 |
const char *action,
|
|
104 |
gpointer user_data)
|
|
105 |
{
|
|
106 |
aud_drct_pl_next();
|
50 |
107 |
}
|
51 |
108 |
|
52 |
|
void osd_show (const gchar * title, const gchar * _message, const gchar * icon,
|
53 |
|
GdkPixbuf * pb)
|
|
109 |
static void
|
|
110 |
playpause (NotifyNotification *notification,
|
|
111 |
const char *action,
|
|
112 |
gpointer user_data)
|
|
113 |
{
|
|
114 |
if(aud_drct_get_playing())
|
|
115 |
{aud_drct_pause();}
|
|
116 |
else
|
|
117 |
{aud_drct_play();}
|
|
118 |
}
|
|
119 |
|
|
120 |
static void
|
|
121 |
show (NotifyNotification *notification,
|
|
122 |
const char *action,
|
|
123 |
gpointer user_data)
|
|
124 |
{
|
|
125 |
aud_interface_show(TRUE);
|
|
126 |
}
|
|
127 |
|
|
128 |
static void
|
|
129 |
do_notify (guint timeout,
|
|
130 |
const char *primary,
|
|
131 |
const char *secondary,
|
|
132 |
GdkPixbuf * pb)
|
54 |
133 |
{
|
55 |
|
gchar * message = g_markup_escape_text (_message, -1);
|
56 |
134 |
GError *error = NULL;
|
|
135 |
|
|
136 |
if ( !(notify_is_initted() && (notification != NULL))) {return;}
|
|
137 |
|
|
138 |
if (primary == NULL)
|
|
139 |
primary = "";
|
|
140 |
|
|
141 |
if (secondary == NULL)
|
|
142 |
secondary = "";
|
|
143 |
|
|
144 |
notify_notification_clear_hints (notification);
|
|
145 |
notify_notification_update (notification, primary, secondary, "audacious");
|
|
146 |
notify_notification_set_timeout (notification, timeout);
|
|
147 |
|
|
148 |
if (pb != NULL) {
|
|
149 |
notify_notification_clear_hints (notification);
|
|
150 |
/* notify_notification_set_image_from_pixbuf(notification,pb);*/
|
|
151 |
}
|
|
152 |
/* notify_notification_set_hint (notification,*/
|
|
153 |
/* "image_path",*/
|
|
154 |
/* g_variant_new_string ("/home/oxayotl/.cache/rhythmbox/album-art/00000065"));*/
|
|
155 |
|
|
156 |
notify_notification_clear_actions (notification);
|
|
157 |
if (notify_supports_actions) {
|
|
158 |
if (notify_supports_icon_buttons) {
|
|
159 |
gboolean playing = FALSE;
|
|
160 |
if (aud_drct_get_playing()) {playing = !aud_drct_get_paused();}
|
|
161 |
|
|
162 |
notify_notification_add_action (notification,
|
|
163 |
"media-skip-backward",
|
|
164 |
N_("Previous"),
|
|
165 |
(NotifyActionCallback) goprevious,
|
|
166 |
NULL,
|
|
167 |
NULL);
|
|
168 |
notify_notification_add_action (notification,
|
|
169 |
playing ? "media-playback-pause" : "media-playback-start",
|
|
170 |
playing ? N_("Pause") : N_("Play"),
|
|
171 |
(NotifyActionCallback) playpause,
|
|
172 |
NULL,
|
|
173 |
NULL);
|
|
174 |
notify_notification_set_hint (notification, "action-icons", g_variant_new_boolean (TRUE));
|
|
175 |
}
|
|
176 |
|
|
177 |
notify_notification_add_action (notification,
|
|
178 |
"media-skip-forward",
|
|
179 |
N_("Next"),
|
|
180 |
(NotifyActionCallback) gonext,
|
|
181 |
NULL,
|
|
182 |
NULL);
|
|
183 |
notify_notification_add_action (notification,
|
|
184 |
"default",
|
|
185 |
N_("Afficher Audacious"),
|
|
186 |
(NotifyActionCallback) show,
|
|
187 |
NULL,
|
|
188 |
NULL);
|
|
189 |
}
|
57 |
190 |
|
58 |
|
if(notification == NULL) {
|
59 |
|
notification = notify_notification_new(title, message, pb == NULL ? icon : NULL);
|
60 |
|
g_signal_connect(notification, "closed", G_CALLBACK(osd_closed_handler), NULL);
|
61 |
|
AUDDBG("new osd created! (notification=%p)\n", (void *) notification);
|
62 |
|
} else {
|
63 |
|
if(notify_notification_update(notification, title, message, pb == NULL ? icon : NULL)) {
|
64 |
|
AUDDBG("old osd updated! (notification=%p)\n", (void *) notification);
|
|
191 |
if (notify_supports_persistence) {
|
|
192 |
const char *hint;
|
|
193 |
|
|
194 |
if (aud_drct_get_playing()) {
|
|
195 |
hint = "resident";
|
65 |
196 |
} else {
|
66 |
|
AUDDBG("could not update old osd! (notification=%p)\n", (void *) notification);
|
|
197 |
hint = "transient";
|
67 |
198 |
}
|
|
199 |
notify_notification_set_hint (notification, hint, g_variant_new_boolean (TRUE));
|
68 |
200 |
}
|
|
201 |
if (notify_notification_show (notification, &error) == FALSE) {
|
|
202 |
AUDDBG("Failed to send notification (%s): %s", primary, error->message);
|
|
203 |
g_error_free (error);
|
|
204 |
return;
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
void osd_show ()
|
|
209 |
{
|
|
210 |
gchar * title;
|
|
211 |
|
|
212 |
if (aud_drct_get_playing())
|
|
213 |
{
|
|
214 |
gint list = aud_playlist_get_playing ();
|
|
215 |
gint entry = aud_playlist_get_position (list);
|
|
216 |
|
|
217 |
gchar * artist, * album, * message;
|
|
218 |
GdkPixbuf * pb;
|
69 |
219 |
|
70 |
|
if(pb != NULL)
|
71 |
|
notify_notification_set_icon_from_pixbuf(notification, pb);
|
|
220 |
aud_playlist_entry_describe (list, entry, & title, & artist, & album, FALSE);
|
72 |
221 |
|
73 |
|
if(!notify_notification_show(notification, &error))
|
74 |
|
AUDDBG("%s!\n", error->message);
|
|
222 |
if (!title){ title = g_strdup_printf (N_("Unknown track"));};
|
|
223 |
if (artist)
|
|
224 |
{
|
|
225 |
if (album)
|
|
226 |
message = g_strdup_printf ("%s\n%s", artist, album);
|
|
227 |
else
|
|
228 |
message = g_strdup (artist);
|
|
229 |
}
|
|
230 |
else if (album)
|
|
231 |
message = g_strdup (album);
|
|
232 |
else
|
|
233 |
message = g_strdup ("");
|
|
234 |
|
|
235 |
pb = audgui_pixbuf_for_current ();
|
|
236 |
if (pb)
|
|
237 |
audgui_pixbuf_scale_within (& pb, 128);
|
|
238 |
|
|
239 |
do_notify (NOTIFY_EXPIRES_DEFAULT, title, message, pb);
|
|
240 |
|
|
241 |
if (pb)
|
|
242 |
g_object_unref (pb);
|
|
243 |
if (message)
|
|
244 |
g_free (message);
|
|
245 |
str_unref (title);
|
|
246 |
str_unref (artist);
|
|
247 |
str_unref (album);
|
|
248 |
}
|
|
249 |
|
|
250 |
else
|
|
251 |
{
|
|
252 |
title = g_strdup (N_("Not playing"));
|
|
253 |
do_notify (NOTIFY_EXPIRES_DEFAULT, title, NULL, NULL);
|
|
254 |
g_free (title);
|
|
255 |
}
|
75 |
256 |
|
76 |
|
g_free (message);
|
77 |
257 |
}
|