File: libnotify-aosd_event.c

package info (click to toggle)
audacious-plugins 3.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,840 kB
  • sloc: ansic: 87,215; cpp: 44,053; sh: 3,666; makefile: 988; xml: 29
file content (114 lines) | stat: -rw-r--r-- 3,868 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/************************************************************************
 * libnotify-aosd_event.c                                               *
 *                                                                      *
 * Copyright (C) 2010 Maximilian Bogner <max@mbogner.de>                *
 * Copyright (C) 2011 John Lindgren <john.lindgren@tds.net>             *
 *                                                                      *
 * This program is free software; you can redistribute it and/or modify *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation; either version 3 of the License,       *
 * or (at your option) any later version.                               *
 *                                                                      *
 * This program is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with this program; if not, see <http://www.gnu.org/licenses/>. *
 ************************************************************************/

#include <glib.h>
#include <string.h>

#include <audacious/drct.h>
#include <audacious/i18n.h>
#include <audacious/playlist.h>
#include <libaudcore/hook.h>
#include <libaudgui/libaudgui-gtk.h>

#include "config.h"
#include "libnotify-aosd_common.h"

static gchar * last_title = NULL, * last_message = NULL;

static void clear (void)
{
    g_free (last_title);
    last_title = NULL;
    g_free (last_message);
    last_message = NULL;
}

static void update (void * unused, void * explicit)
{
    if (! aud_drct_get_playing () || ! aud_drct_get_ready ())
    {
        if (GPOINTER_TO_INT (explicit))
            osd_show (_("Stopped"), _("Audacious is not playing."), NULL, NULL);

        return;
    }

    gint list = aud_playlist_get_playing ();
    gint entry = aud_playlist_get_position (list);

    gchar * title, * artist, * album;
    aud_playlist_entry_describe (list, entry, & title, & artist, & album, FALSE);

    gchar * message;
    if (artist)
    {
        if (album)
            message = g_strdup_printf ("%s\n%s", artist, album);
        else
            message = g_strdup (artist);
    }
    else if (album)
        message = g_strdup (album);
    else
        message = g_strdup ("");

    if (! GPOINTER_TO_INT (explicit) && last_title && last_message && ! strcmp
     (title, last_title) && ! strcmp (message, last_message))
    {
        g_free (message);
        goto FREE;
    }

    GdkPixbuf * pb = audgui_pixbuf_for_current ();
    if (pb)
        audgui_pixbuf_scale_within (& pb, 96);

    osd_show (title, message, "audio-x-generic", pb);

    if (pb)
        g_object_unref (pb);

    clear ();
    last_title = g_strdup (title);
    last_message = message;

FREE:
    str_unref (title);
    str_unref (artist);
    str_unref (album);
}

void event_init (void)
{
    update (NULL, GINT_TO_POINTER (FALSE));
    hook_associate ("aosd toggle", (HookFunction) update, GINT_TO_POINTER (TRUE));
    hook_associate ("playback ready", (HookFunction) update, GINT_TO_POINTER (FALSE));
    hook_associate ("playlist update", (HookFunction) update, GINT_TO_POINTER (FALSE));
    hook_associate ("playback stop", (HookFunction) clear, NULL);
}

void event_uninit (void)
{
    hook_dissociate ("aosd toggle", (HookFunction) update);
    hook_dissociate ("playback ready", (HookFunction) update);
    hook_dissociate ("playlist update", (HookFunction) update);
    hook_dissociate ("playback stop", (HookFunction) clear);
    clear ();
}