Project

General

Profile

notifyactionsupport.patch

Jean-Alexandre Anglès d'Auriac, April 08, 2013 03:01

View differences:

src/notify/event.c
21 21
#include <audacious/drct.h>
22 22
#include <audacious/i18n.h>
23 23
#include <audacious/playlist.h>
24
#include <audacious/misc.h>
24 25
#include <libaudcore/hook.h>
25 26
#include <libaudgui/libaudgui-gtk.h>
26 27

  
28
#include "config.h"
27 29
#include "event.h"
28 30
#include "osd.h"
29 31

  
32

  
30 33
static char * last_title = NULL, * last_message = NULL; /* pooled */
31 34

  
32 35
static void clear (void)
......
37 40
    last_message = NULL;
38 41
}
39 42

  
40
static void reshow (void)
43
static void update_button (void * unused, void * useless)
41 44
{
42
    if (! last_title || ! last_message)
43
        return;
44

  
45
    GdkPixbuf * pb = audgui_pixbuf_request_current ();
46
    if (pb)
47
        audgui_pixbuf_scale_within (& pb, 96);
48

  
49
    osd_show (last_title, last_message, "audio-x-generic", pb);
50

  
51
    if (pb)
52
        g_object_unref (pb);
45
    osd_update_button ();
53 46
}
54 47

  
55
static void update (void * unused, void * explicit)
48

  
49
static void update (void * unused, void * forcedisplay)
56 50
{
57 51
    if (! aud_drct_get_playing () || ! aud_drct_get_ready ())
58 52
    {
59
        if (GPOINTER_TO_INT (explicit))
60
            osd_show (_("Stopped"), _("Audacious is not playing."), NULL, NULL);
61

  
62 53
        return;
63 54
    }
64 55

  
......
85 76
    str_unref (album);
86 77

  
87 78
    /* pointer comparison works for pooled strings */
88
    if (! GPOINTER_TO_INT (explicit) && title == last_title && message == last_message)
79
    if (title == last_title && message == last_message && !GPOINTER_TO_INT (forcedisplay))
89 80
    {
90 81
        str_unref (title);
91 82
        str_unref (message);
......
97 88
    str_unref (last_message);
98 89
    last_message = message;
99 90

  
100
    reshow ();
91
    GdkPixbuf *pixbuf = audgui_pixbuf_request_current();
92

  
93
    if (pixbuf)
94
        audgui_pixbuf_scale_within (& pixbuf, 96);
95

  
96
    osd_show (title, message, pixbuf, FALSE);
97

  
98
    if (pixbuf)
99
        g_object_unref (pixbuf);
100
}
101

  
102
static void update_explicit (void * unused, void * useless)
103
{
104
    if (! aud_drct_get_playing () || ! aud_drct_get_ready ())
105
        osd_show (_("Stopped"), _("Audacious is not playing."), NULL, FALSE);
106
    else
107
        update(unused, GINT_TO_POINTER (TRUE));
108
    return;
101 109
}
102 110

  
103
void event_init (void)
111
void event_init ()
104 112
{
105
    update (NULL, GINT_TO_POINTER (FALSE));
106
    hook_associate ("aosd toggle", (HookFunction) update, GINT_TO_POINTER (TRUE));
107
    hook_associate ("playback ready", (HookFunction) update, GINT_TO_POINTER (FALSE));
108
    hook_associate ("playlist update", (HookFunction) update, GINT_TO_POINTER (FALSE));
109
    hook_associate ("current art ready", (HookFunction) reshow, NULL);
113
    hook_associate ("aosd toggle", (HookFunction) update_explicit, NULL);
114
    hook_associate ("playback ready", (HookFunction) update, NULL);
115
    hook_associate ("playlist update", (HookFunction) update, NULL);
116
    hook_associate ("current art ready", (HookFunction) update, NULL);
117
    hook_associate ("playback pause", (HookFunction) update_button, NULL);
118
    hook_associate ("playback unpause", (HookFunction) update_button, NULL);
119
    hook_associate ("playback begin", (HookFunction) update_button, NULL);
120
    hook_associate ("playback stop", (HookFunction) update_button, NULL);
110 121
    hook_associate ("playback begin", (HookFunction) clear, NULL);
111 122
    hook_associate ("playback stop", (HookFunction) clear, NULL);
112 123
}
113 124

  
114 125
void event_uninit (void)
115 126
{
116
    hook_dissociate ("aosd toggle", (HookFunction) update);
127
    hook_dissociate ("aosd toggle", (HookFunction) update_explicit);
117 128
    hook_dissociate ("playback ready", (HookFunction) update);
118 129
    hook_dissociate ("playlist update", (HookFunction) update);
119
    hook_dissociate ("current art ready", (HookFunction) reshow);
130
    hook_dissociate ("current art ready", (HookFunction) update);
131
    hook_dissociate ("playback pause", (HookFunction) update_button);
132
    hook_dissociate ("playback unpause", (HookFunction) update_button);
133
    hook_dissociate ("playback begin", (HookFunction) update_button);
134
    hook_dissociate ("playback stop", (HookFunction) update_button);
120 135
    hook_dissociate ("playback begin", (HookFunction) clear);
121 136
    hook_dissociate ("playback stop", (HookFunction) clear);
122 137
    clear ();
src/notify/event.h
17 17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 18
 */
19 19

  
20
void event_init (void);
20
void event_init ();
21 21
void event_uninit (void);
src/notify/notify.c
23 23
#include <audacious/i18n.h>
24 24
#include <audacious/plugin.h>
25 25
#include <audacious/debug.h>
26
#include <audacious/preferences.h>
27
#include <audacious/misc.h>
26 28

  
29
#include "config.h"
27 30
#include "event.h"
28 31
#include "osd.h"
29 32

  
......
46 49
    "You should have received a copy of the GNU General Public License "
47 50
    "along with this program.  If not, see <http://www.gnu.org/licenses/>.");
48 51

  
52
static const char * const notify_defaults[] = {
53
 "actions", "TRUE",
54
 NULL};
55

  
56

  
57
static const PreferencesWidget notify_widgets[] = {
58
 {WIDGET_CHK_BTN, N_("Activate controls inside of the notification"),
59
 .cfg_type = VALUE_BOOLEAN, .csect = "notify", .cname = "actions"},
60
};
61

  
62
static const PluginPreferences notify_prefs = {
63
 .widgets = notify_widgets,
64
 .n_widgets = G_N_ELEMENTS (notify_widgets)
65
};
66

  
67

  
68

  
49 69
AUD_GENERAL_PLUGIN
50 70
(
51 71
    .name = N_("Desktop Notifications"),
52 72
    .domain = PACKAGE,
53 73
    .about_text = plugin_about,
54 74
    .init = plugin_init,
75
    .prefs = & notify_prefs,
55 76
    .cleanup = plugin_cleanup
56 77
)
57 78

  
......
59 80

  
60 81
gboolean plugin_init (void)
61 82
{
62
    AUDDBG("started!\n");
63
    if(!osd_init()) {
64
        AUDDBG("osd_init failed!\n");
65
        return FALSE;
66
    }
83
    aud_config_set_defaults ("notify", notify_defaults);
84
    osd_init();
67 85
    event_init();
68

  
69 86
    plugin_active = 1;
87
    AUDDBG("Osd plugin initted\n");
70 88
    return TRUE;
71 89
}
72 90

  
73 91

  
74 92
void plugin_cleanup() {
75 93
    if(plugin_active) {
76
        AUDDBG("started!\n");
77 94
        event_uninit();
78 95
        osd_uninit();
79 96
        plugin_active = 0;
80
        AUDDBG("done!\n");
97
        AUDDBG("Osd plugin uninitted\n");
81 98
    }
82 99
}
src/notify/osd.c
17 17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 18
 */
19 19

  
20
#include <libaudcore/core.h>
21
#include <libaudcore/hook.h>
22
#include <audacious/debug.h>
23
#include <audacious/i18n.h>
24
#include <audacious/drct.h>
25
#include <audacious/misc.h>
26
#include <libnotify/notify.h>
20 27
#include "osd.h"
21 28

  
22
static NotifyNotification * notification = NULL;
29
static NotifyNotification *notification = NULL;
30
static gboolean actions_available = FALSE;
23 31

  
24
bool_t osd_init()
25
{
26
    return notify_init ("Audacious");
32
void osd_init() {
33
    if (notify_is_initted())
34
    {
35
        AUDDBG("Notify is already initted, that shouldn't happen\n");
36
        return;
37
    }
38
    if (notify_init("Audacious"))
39
    {
40
        notification = notify_notification_new("Notification", NULL, NULL);
41

  
42
        notify_notification_set_urgency (notification, NOTIFY_URGENCY_LOW);
43
        notify_notification_set_hint (notification, "desktop-entry", g_variant_new_string ("audacious"));
44
        notify_notification_set_category (notification, "x-gnome.music");
45

  
46
        if(!aud_get_bool ("notify", "actions"))
47
            return;
48

  
49
        GList *server_caps;
50
        server_caps = notify_get_server_caps ();
51

  
52
        if (g_list_find_custom (server_caps, "actions", (GCompareFunc)g_strcmp0))
53
        {
54
            actions_available = TRUE;
55
            if (g_list_find_custom (server_caps, "action-icons", (GCompareFunc)g_strcmp0))
56
                notify_notification_set_hint (notification, "action-icons", g_variant_new_boolean (TRUE));
57
        }
58
        if (g_list_find_custom (server_caps, "persistence", (GCompareFunc)g_strcmp0))
59
        {
60
            AUDDBG("Notification server supports persistence\n");
61
            if (aud_get_bool ("notify", "actions") && actions_available)
62
                notify_notification_set_hint (notification, "resident", g_variant_new_boolean (TRUE));
63
            else
64
                notify_notification_set_hint (notification, "resident", g_variant_new_boolean (FALSE));
65
        }
66
        else
67
        {
68
            AUDDBG("Notification server doesn't supports persistence\n");
69
        }
70
        g_list_free_full(server_caps, g_free);
71
    }
72
    return;
27 73
}
28 74

  
29 75
void osd_uninit (void)
30 76
{
31 77
    if (notification)
32 78
    {
79
        GError *error = NULL;
80
        if (notify_notification_close (notification, &error) == FALSE)
81
            AUDDBG("%s!\n", error->message);
82
        if(error)
83
            g_error_free(error);
33 84
        g_object_unref (notification);
34 85
        notification = NULL;
35 86
    }
36 87

  
37 88
    notify_uninit();
89
    AUDDBG("Notify uninitted\n");
38 90
}
39 91

  
40
static void osd_closed_handler (void)
92

  
93
void goprevious (NotifyNotification *notification,
94
            const char *action,
95
            gpointer user_data)
41 96
{
42
    if (notification)
43
    {
44
        g_object_unref (notification);
45
        notification = NULL;
97
    aud_drct_pl_prev();
98
}
99

  
100
void gonext (NotifyNotification *notification,
101
        const char *action,
102
        gpointer user_data)
103
{
104
    aud_drct_pl_next();
105
}
106

  
107
void show_audacious (NotifyNotification *notification,
108
        const char *action,
109
        gpointer user_data)
110
{
111
    aud_interface_show(TRUE);
112
}
113

  
114
void playpause (NotifyNotification *notification,
115
           const char *action,
116
           gpointer user_data)
117
{
118
    if(aud_drct_get_playing())
119
    {aud_drct_pause();}
120
    else
121
    {aud_drct_play();}
122
}
123

  
124
void osd_update_button(void){
125
    if(actions_available){
126
        GError *error = NULL;
127
        gboolean paused = TRUE;
128
        if (aud_drct_get_playing()) {paused = aud_drct_get_paused();}
129

  
130
        notify_notification_clear_actions (notification);
131

  
132
        notify_notification_add_action (notification,
133
                    "media-skip-backward",
134
                    N_("Previous"),
135
                    NOTIFY_ACTION_CALLBACK(goprevious),
136
                    NULL,
137
                    NULL);
138

  
139
        notify_notification_add_action (notification,
140
                    paused ? "media-playback-start" : "media-playback-pause",
141
                    paused ? N_("Play") : N_("Pause"),
142
                    NOTIFY_ACTION_CALLBACK(playpause),
143
                    NULL,
144
                    NULL);
145

  
146
        notify_notification_add_action (notification,
147
                    "media-skip-forward",
148
                    N_("Next"),
149
                    NOTIFY_ACTION_CALLBACK(gonext),
150
                    NULL,
151
                    NULL);
152

  
153
        notify_notification_add_action (notification,
154
                    "default",
155
                    N_("Show Audacious"),
156
                    NOTIFY_ACTION_CALLBACK(show_audacious),
157
                    NULL,
158
                    NULL);
159
        if(!notify_notification_show(notification, &error))
160
            AUDDBG("Error “%s” when trying to send notification\n", error->message);
161
        if(error)
162
            g_error_free(error);
46 163
    }
47 164
}
48 165

  
49
void osd_show (const char * title, const char * _message, const char * icon,
50
 GdkPixbuf * pixbuf)
166

  
167
void osd_show (const gchar * title, const gchar * _message, GdkPixbuf *cover, gboolean actiononly)
51 168
{
52
    char * message = g_markup_escape_text (_message, -1);
169
    gchar * message = g_markup_escape_text (_message, -1);
170
    GError *error = NULL;
53 171

  
54
    if (pixbuf)
55
        icon = NULL;
172
    if(cover)
173
        notify_notification_set_image_from_pixbuf(notification, cover);
56 174

  
57
    if (notification)
58
        notify_notification_update (notification, title, message, icon);
175
    if (actions_available)
176
    {
177
        osd_update_button();
178
    }
59 179
    else
60 180
    {
61
        notification = notify_notification_new (title, message, icon);
62
        g_signal_connect (notification, "closed", (GCallback) osd_closed_handler, NULL);
181
        if (actiononly)
182
        {
183
            g_free (message);
184
            return;
185
        }
63 186
    }
64 187

  
65
    if (pixbuf)
66
        notify_notification_set_icon_from_pixbuf (notification, pixbuf);
188
    if(!notify_notification_update(notification, title, message, "audacious"))
189
         AUDDBG("Could not update osd!\n");
67 190

  
68
    notify_notification_show (notification, NULL);
191
    if(!notify_notification_show(notification, &error))
192
        AUDDBG("Error “%s” when trying to send notification\n", error->message);
193
    if(error)
194
        g_error_free(error);
69 195

  
70 196
    g_free (message);
71 197
}
src/notify/osd.h
17 17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 18
 */
19 19

  
20
#include <libaudcore/core.h>
21
#include <libnotify/notify.h>
22

  
23
bool_t osd_init (void);
24
void osd_uninit (void);
25
void osd_show (const char * title, const char * message, const char * icon, GdkPixbuf * pixbuf);
20
void osd_init();
21
void osd_uninit();
22
void osd_show(const gchar *title, const gchar *message, GdkPixbuf *pixbuf, gboolean actiononly);
23
void osd_update_button(void);