summarylogtreecommitdiffstats
path: root/psipublish.c
blob: 68a79457cf036b6ae14d1380135a096a78ded726 (plain)
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
#include "deadbeef.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

DB_functions_t *deadbeef;
DB_misc_t plugin;
char tunefilepath[100];
int isplayed;

void updatetune(const char *title, const char *artist, const char *album, int length)
{
    FILE *tunefile;
    tunefile = fopen(tunefilepath, "w");
    if (tunefile) {
        fprintf(tunefile, "%s%s%s\n%s\n%s\n%s\n%d\n", title, deadbeef->conf_get_int("psipublish.showplayer", 0) ? " [DeaDBeeF]" : "", isplayed ? "" : " [PAUSED]", artist, album, title, length);
        fclose(tunefile); 
    }
}

static int psipublish_songstarted(ddb_event_track_t *ev) {
    if (deadbeef->conf_get_int("psipublish.enable", 0)) {
        DB_playItem_t *track = ev->track;
        if (track) {
            isplayed = 1;
            updatetune(deadbeef->pl_find_meta(track, "title"), deadbeef->pl_find_meta(track, "artist"), deadbeef->pl_find_meta(track, "album"), deadbeef->pl_get_item_duration(track));
        }
    }
    return 0;
}

static int psipublish_songfinished(ddb_event_track_t *ev) {
    remove(tunefilepath);
    return 0;
}


static int psipublish_paused() {
    if (deadbeef->conf_get_int("psipublish.enable", 0)) {
        DB_playItem_t *track = deadbeef->streamer_get_playing_track();
        if (track) {
            isplayed = deadbeef->get_output()->state() == OUTPUT_STATE_PLAYING;
            updatetune(deadbeef->pl_find_meta(track, "title"), deadbeef->pl_find_meta(track, "artist"), deadbeef->pl_find_meta(track, "album"), deadbeef->pl_get_item_duration(track));
            deadbeef->pl_item_unref(track);
        }
    }
    return 0; 
}

static int psipublish_pluginmessage(uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2) {
    switch (id) {
    case DB_EV_SONGSTARTED:
        psipublish_songstarted((ddb_event_track_t *)ctx);
        break;
    case DB_EV_SONGFINISHED:
        psipublish_songfinished((ddb_event_track_t *)ctx);
        break;
    case DB_EV_PAUSED:
        psipublish_paused();
        break;
    }
    return 0;
}

int psipublish_pluginstart(void) {
    sprintf(tunefilepath, "%s/.cache/psi+/tune", getenv("HOME"));
    return 0;
}

int psipublish_pluginstop (void) {
    remove(tunefilepath);
    return 0;
}

static const char settings_dlg[] =
    "property \"Enable publishing\" checkbox psipublish.enable 0;\n"
    "property \"Show player\" checkbox psipublish.showplayer 0;\n"
;

DB_misc_t plugin = {
    DB_PLUGIN_SET_API_VERSION
    .plugin.type = DB_PLUGIN_MISC,
    .plugin.version_major = 1,
    .plugin.version_minor = 3,
    .plugin.id = "psipublish",
    .plugin.name = "Psi IM Publish Tune",
    .plugin.descr = "Now playing for Psi IM",
    .plugin.website = "",
    .plugin.start = psipublish_pluginstart,
    .plugin.stop = psipublish_pluginstop,
    .plugin.message = psipublish_pluginmessage,
    .plugin.configdialog = settings_dlg
};

DB_plugin_t *
psipublish_load(DB_functions_t *ddb) {
    deadbeef = ddb;
    return &plugin.plugin;
}