summaryrefslogtreecommitdiff
path: root/plugins/shellexec/shellexec.c
blob: 178add2e8dcd7e8f4d453b467f45d42ec6039cd9 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
    Shellexec plugin for DeaDBeeF
    Copyright (C) 2009 Viktor Semykin <thesame.ml@gmail.com>

    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 2 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 <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../../deadbeef.h"

#define trace(...) { fprintf(stderr, __VA_ARGS__); }
//#define trace(fmt,...)

static DB_misc_t plugin;
static DB_functions_t *deadbeef;

static DB_plugin_action_t *actions;

DB_plugin_t *
shellexec_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}

static char*
trim (char* s)
{
    char *h, *t;
    
    for (h = s; *h == ' ' || *h == '\t'; h++);
    for (t = s + strlen (s); *t == ' ' || *t == '\t'; t--);
    * (t+1) = 0;
    return h;
}

static int
shx_callback (DB_playItem_t *it, void *data)
{
    char fmt[1024]; //FIXME: possible stack corruption
    deadbeef->pl_format_title (it, -1, fmt, sizeof (fmt), -1, data);
    printf ("%s\n", fmt);
    return 0;
}

static DB_plugin_action_t *
shx_get_actions (DB_playItem_t *unused)
{
    return actions;
}

static int
shx_start ()
{
    actions = NULL;
    DB_plugin_action_t *prev = NULL;

    DB_conf_item_t *item = deadbeef->conf_find ("shellexec.", NULL);
    while (item)
    {
        size_t l = strlen (item->value) + 1;
        char tmp[l];
        strcpy (tmp, item->value);
        trace ("Shellexec: %s\n", tmp);

        char *semicolon = strchr (tmp, ':');
        if (!semicolon)
        {
            fprintf (stdout, "Shellexec: wrong option <%s>\n", tmp);
            continue;
        }

        *semicolon = 0;

        DB_plugin_action_t *action = calloc (sizeof (DB_plugin_action_t), 1);

        action->title = strdup (trim (semicolon + 1));
        action->callback = shx_callback;
        action->data = strdup (trim (tmp));
        action->flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_ALLOW_MULTIPLE_TRACKS;
        action->next = NULL;

        if (prev)
            prev->next = action;
        prev = action;

        if (!actions)
            actions = action;

        item = deadbeef->conf_find ("shellexec.", item);
    }
    return 0;
}

// define plugin interface
static DB_misc_t plugin = {
    .plugin.api_vmajor = DB_API_VERSION_MAJOR,
    .plugin.api_vminor = DB_API_VERSION_MINOR,
    .plugin.type = DB_PLUGIN_MISC,
    .plugin.id = "shellexec",
    .plugin.name = "Shell commands",
    .plugin.descr = "Executes configurable shell commands for tracks",
    .plugin.author = "Viktor Semykin",
    .plugin.email = "thesame.ml@gmail.com",
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = shx_start,
    .plugin.get_actions = shx_get_actions
};