forked from splushii/deadbeef-rating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rating.c
345 lines (310 loc) · 12 KB
/
rating.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <deadbeef.h>
#include <string.h>
#include <stdlib.h>
// #define trace(...) { fprintf(stderr, __VA_ARGS__); }
static DB_misc_t plugin;
static DB_functions_t *deadbeef;
DB_plugin_t * rating_load(DB_functions_t *api) {
deadbeef = api;
return DB_PLUGIN(&plugin);
}
static int set_tag(DB_plugin_action_t *action, ddb_action_context_t ctx, const char *key, const char *value) {
DB_playItem_t *it = NULL;
ddb_playlist_t *plt = NULL;
int num = 0;
int count = 0;
if (ctx == DDB_ACTION_CTX_SELECTION) {
plt = deadbeef->plt_get_curr();
if (plt) {
num = deadbeef->plt_getselcount(plt);
it = deadbeef->plt_get_first(plt, PL_MAIN);
while (it) {
if (deadbeef->pl_is_selected(it)) {
break;
}
DB_playItem_t *next = deadbeef->pl_get_next(it, PL_MAIN);
deadbeef->pl_item_unref(it);
it = next;
}
deadbeef->plt_unref(plt);
}
} else if (ctx == DDB_ACTION_CTX_NOWPLAYING) {
it = deadbeef->streamer_get_playing_track();
num = 1;
}
if (it) {
if (num >= 1) {
while (it) {
if (deadbeef->pl_is_selected(it) || ctx == DDB_ACTION_CTX_NOWPLAYING) {
if (value) {
deadbeef->pl_replace_meta(it, key, value);
} else {
deadbeef->pl_delete_meta(it, key);
}
deadbeef->pl_lock();
const char *dec = deadbeef->pl_find_meta_raw(it, ":DECODER");
char decoder_id[100];
if (dec) {
strncpy(decoder_id, dec, sizeof(decoder_id) - 1);
}
int match = it && dec;
deadbeef->pl_unlock();
if (match) {
DB_decoder_t *dec = NULL;
DB_decoder_t **decoders = deadbeef->plug_get_decoder_list();
for (int i = 0; decoders[i]; i++) {
if (!strcmp(decoders[i]->plugin.id, decoder_id)) {
dec = decoders[i];
if (dec->write_metadata) {
dec->write_metadata(it);
}
break;
}
}
}
if (++count >= num) {
break;
}
}
DB_playItem_t *next = deadbeef->pl_get_next(it, PL_MAIN);
deadbeef->pl_item_unref(it);
it = next;
}
if (count) {
deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
}
}
deadbeef->pl_item_unref(it);
}
return 0;
}
static int clear_tag(DB_plugin_action_t *action, ddb_action_context_t ctx, const char *key) {
return set_tag(action, ctx, key, NULL);
}
static int set_numeric_tag(DB_plugin_action_t *action, ddb_action_context_t ctx, const char *key, int value) {
DB_playItem_t *it = NULL;
ddb_playlist_t *plt = NULL;
int num = 0;
int count = 0;
if (ctx == DDB_ACTION_CTX_SELECTION) {
plt = deadbeef->plt_get_curr();
if (plt) {
num = deadbeef->plt_getselcount(plt);
it = deadbeef->plt_get_first(plt, PL_MAIN);
while (it) {
if (deadbeef->pl_is_selected(it)) {
break;
}
DB_playItem_t *next = deadbeef->pl_get_next(it, PL_MAIN);
deadbeef->pl_item_unref(it);
it = next;
}
deadbeef->plt_unref(plt);
}
} else if (ctx == DDB_ACTION_CTX_NOWPLAYING) {
it = deadbeef->streamer_get_playing_track();
num = 1;
}
if (it) {
if (num >= 1) {
while (it) {
if (deadbeef->pl_is_selected(it) || ctx == DDB_ACTION_CTX_NOWPLAYING) {
deadbeef->pl_set_meta_int(it, key, value);
deadbeef->pl_lock();
const char *dec = deadbeef->pl_find_meta_raw(it, ":DECODER");
char decoder_id[100];
if (dec) {
strncpy(decoder_id, dec, sizeof(decoder_id) - 1);
}
int match = it && dec;
deadbeef->pl_unlock();
if (match) {
DB_decoder_t *dec = NULL;
DB_decoder_t **decoders = deadbeef->plug_get_decoder_list();
for (int i = 0; decoders[i]; i++) {
if (!strcmp(decoders[i]->plugin.id, decoder_id)) {
dec = decoders[i];
if (dec->write_metadata) {
dec->write_metadata(it);
}
break;
}
}
}
if (++count >= num) {
break;
}
}
DB_playItem_t *next = deadbeef->pl_get_next(it, PL_MAIN);
deadbeef->pl_item_unref(it);
it = next;
}
if (count) {
deadbeef->sendmessage(DB_EV_PLAYLISTCHANGED, 0, DDB_PLAYLIST_CHANGE_CONTENT, 0);
}
}
deadbeef->pl_item_unref(it);
}
return 0;
}
static const char rating_tag[] = "RATING";
static int rating_action_rate0(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 0);
}
static int rating_action_rate0h(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 10);
}
static int rating_action_rate1(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 20);
}
static int rating_action_rate1h(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 30);
}
static int rating_action_rate2(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 40);
}
static int rating_action_rate2h(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 50);
}
static int rating_action_rate3(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 60);
}
static int rating_action_rate3h(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 70);
}
static int rating_action_rate4(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 80);
}
static int rating_action_rate4h(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 90);
}
static int rating_action_rate5(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return set_numeric_tag(action, ctx, rating_tag, 100);
}
static int rating_action_remove(DB_plugin_action_t *action, ddb_action_context_t ctx) {
return clear_tag(action, ctx, rating_tag);
}
static DB_plugin_action_t remove_rating_action = {
.title = "Song rating/Remove rating tag",
.name = "rating_remove",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_remove,
.next = NULL
};
static DB_plugin_action_t rate5_action = {
.title = "Song rating/10 Masterpiece",
.name = "rating_rate5",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate5,
.next = &remove_rating_action
};
static DB_plugin_action_t rate4h_action = {
.title = "Song rating/9 Excellent",
.name = "rating_rate4h",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate4h,
.next = &rate5_action
};
static DB_plugin_action_t rate4_action = {
.title = "Song rating/8 Very good",
.name = "rating_rate4",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate4,
.next = &rate4h_action
};
static DB_plugin_action_t rate3h_action = {
.title = "Song rating/7 Good",
.name = "rating_rate3h",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate3h,
.next = &rate4_action
};
static DB_plugin_action_t rate3_action = {
.title = "Song rating/6 Decent",
.name = "rating_rate3",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate3,
.next = &rate3h_action
};
static DB_plugin_action_t rate2h_action = {
.title = "Song rating/5 So-so",
.name = "rating_rate2h",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate2h,
.next = &rate3_action
};
static DB_plugin_action_t rate2_action = {
.title = "Song rating/4 Not really good",
.name = "rating_rate2",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate2,
.next = &rate2h_action
};
static DB_plugin_action_t rate1h_action = {
.title = "Song rating/3 Weak",
.name = "rating_rate1h",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate1h,
.next = &rate2_action
};
static DB_plugin_action_t rate1_action = {
.title = "Song rating/2 Bad",
.name = "rating_rate1",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate1,
.next = &rate1h_action
};
static DB_plugin_action_t rate0h_action = {
.title = "Song rating/1 Awful",
.name = "rating_rate0h",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate0h,
.next = &rate1_action
};
static DB_plugin_action_t rate0_action = {
.title = "Song rating/0 Worst ever",
.name = "rating_rate0",
.flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_MULTIPLE_TRACKS | DB_ACTION_ADD_MENU,
.callback2 = rating_action_rate0,
.next = &rate0h_action
};
static int plugin_start(void) {
return 0;
}
static int plugin_stop(void) {
return 0;
}
static DB_plugin_action_t * plugin_get_actions(DB_playItem_t *it) {
return &rate0_action;
}
static DB_misc_t plugin = {
.plugin.api_vmajor = 1,
.plugin.api_vminor = 5,
.plugin.version_major = 1,
.plugin.version_minor = 2,
.plugin.type = DB_PLUGIN_MISC,
.plugin.name = "Song Rating",
.plugin.descr = "Enables commands to rate song(s) by editing the metadata tag rating.",
.plugin.copyright =
"Rating plugin for DeaDBeeF Player\n"
"Author: Christian Hernvall\n"
"\n"
"This program is free software; you can redistribute it and/or\n"
"modify it under the terms of the GNU General Public License\n"
"as published by the Free Software Foundation; either version 2\n"
"of the License, or (at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
,
.plugin.website = "http://deadbeef.sf.net",
.plugin.start = plugin_start,
.plugin.stop = plugin_stop,
.plugin.get_actions = plugin_get_actions,
};