Project

General

Profile

notifyactionsupport.patch

Jean-Alexandre Anglès d'Auriac, April 10, 2013 02:40

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
        GList *server_caps;
47
        server_caps = notify_get_server_caps ();
48

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

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

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

  
40
static void osd_closed_handler (void)
91

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

  
49
void osd_show (const char * title, const char * _message, const char * icon,
50
 GdkPixbuf * pixbuf)
99
void gonext (NotifyNotification *notification,
100
        const char *action,
101
        gpointer user_data)
51 102
{
52
    char * message = g_markup_escape_text (_message, -1);
103
    aud_drct_pl_next();
104
}
53 105

  
54
    if (pixbuf)
55
        icon = NULL;
106
void show_audacious (NotifyNotification *notification,
107
        const char *action,
108
        gpointer user_data)
109
{
110
    aud_interface_show(TRUE);
111
}
56 112

  
57
    if (notification)
58
        notify_notification_update (notification, title, message, icon);
113
void playpause (NotifyNotification *notification,
114
           const char *action,
115
           gpointer user_data)
116
{
117
    if(aud_drct_get_playing())
118
    {aud_drct_pause();}
59 119
    else
120
    {aud_drct_play();}
121
}
122

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

  
129
        notify_notification_clear_actions (notification);
130

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

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

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

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

  
165

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

  
171
    if (!actions_available && actiononly)
60 172
    {
61
        notification = notify_notification_new (title, message, icon);
62
        g_signal_connect (notification, "closed", (GCallback) osd_closed_handler, NULL);
173
        g_free (message);
174
        return;
63 175
    }
64 176

  
65
    if (pixbuf)
66
        notify_notification_set_icon_from_pixbuf (notification, pixbuf);
177
    if(cover)
178
        notify_notification_set_image_from_pixbuf(notification, cover);
179

  
180
    osd_update_button();
181

  
182
    if(!notify_notification_update(notification, title, message, "audacious"))
183
         AUDDBG("Could not update osd!\n");
67 184

  
68
    notify_notification_show (notification, NULL);
185
    if(!notify_notification_show(notification, &error))
186
        AUDDBG("Error “%s” when trying to send notification\n", error->message);
187
    if(error)
188
        g_error_free(error);
69 189

  
70 190
    g_free (message);
71 191
}
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);