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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
|
/*
Hotkeys plugin for DeaDBeeF
Copyright (C) 2009-2011 Viktor Semykin <thesame.ml@gmail.com>
Copyright (C) 2012-2013 Alexey Yakovenko <waker@users.sourceforge.net>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef __APPLE__
#include <X11/Xlib.h>
#endif
#include <ctype.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include "../libparser/parser.h"
#include "hotkeys.h"
#include "../../deadbeef.h"
#include "actionhandlers.h"
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
static DB_hotkeys_plugin_t plugin;
DB_functions_t *deadbeef;
#ifndef __APPLE__
static int finished;
static Display *disp;
static intptr_t loop_tid;
static int need_reset = 0;
#endif
#define MAX_COMMAND_COUNT 256
typedef struct {
const char *name;
int keysym;
#ifndef __APPLE__
int keycode; // after mapping
#endif
} xkey_t;
#define KEY(kname, kcode) { .name=kname, .keysym=kcode},
static xkey_t keys[] = {
#include "keysyms.inc"
};
typedef struct command_s {
int keycode;
#ifndef __APPLE__
int x11_keycode;
#endif
int modifier;
int ctx;
int isglobal;
DB_plugin_action_t *action;
} command_t;
static command_t commands [MAX_COMMAND_COUNT];
static int command_count = 0;
#ifndef __APPLE__
static void
init_mapped_keycodes (Display *disp, Atom *syms, int first_kk, int last_kk, int ks_per_kk) {
int i, ks;
for (i = 0; i < last_kk-first_kk; i++)
{
int sym = * (syms + i*ks_per_kk);
for (ks = 0; keys[ks].name; ks++)
{
if (keys[ ks ].keysym == sym)
{
keys[ks].keycode = i+first_kk;
}
}
}
}
#endif
static int
get_keycode (const char* name) {
for (int i = 0; keys[i].name; i++) {
if (!strcmp (name, keys[i].name)) {
trace ("init: key %s code %x\n", name, keys[i].keysym);
return keys[i].keysym;
}
}
return 0;
}
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 void
cmd_invoke_plugin_command (DB_plugin_action_t *action, int ctx)
{
if (action->callback) {
if (ctx == DDB_ACTION_CTX_MAIN) {
// collect stuff for 1.4 user data
// common action
if (action->flags & DB_ACTION_COMMON)
{
action->callback (action, NULL);
return;
}
// playlist action
if (action->flags & DB_ACTION_PLAYLIST)
{
ddb_playlist_t *plt = deadbeef->plt_get_curr ();
if (plt) {
action->callback (action, plt);
deadbeef->plt_unref (plt);
}
return;
}
int selected_count = 0;
DB_playItem_t *pit = deadbeef->pl_get_first (PL_MAIN);
DB_playItem_t *selected = NULL;
while (pit) {
if (deadbeef->pl_is_selected (pit))
{
if (!selected)
selected = pit;
selected_count++;
}
DB_playItem_t *next = deadbeef->pl_get_next (pit, PL_MAIN);
deadbeef->pl_item_unref (pit);
pit = next;
}
//Now we're checking if action is applicable:
if (selected_count == 0)
{
trace ("No tracks selected\n");
return;
}
if ((selected_count == 1) && (!(action->flags & DB_ACTION_SINGLE_TRACK)))
{
trace ("Hotkeys: action %s not allowed for single track\n", action->name);
return;
}
if ((selected_count > 1) && (!(action->flags & DB_ACTION_MULTIPLE_TRACKS)))
{
trace ("Hotkeys: action %s not allowed for multiple tracks\n", action->name);
return;
}
//So, action is allowed, do it.
if (action->flags & DB_ACTION_CAN_MULTIPLE_TRACKS)
{
action->callback (action, NULL);
}
else {
pit = deadbeef->pl_get_first (PL_MAIN);
while (pit) {
if (deadbeef->pl_is_selected (pit))
{
action->callback (action, pit);
}
DB_playItem_t *next = deadbeef->pl_get_next (pit, PL_MAIN);
deadbeef->pl_item_unref (pit);
pit = next;
}
}
}
}
else {
action->callback2 (action, ctx);
}
}
static DB_plugin_action_t *
find_action_by_name (const char *command) {
// find action with this name, and add to list
DB_plugin_action_t *actions = NULL;
DB_plugin_t **plugins = deadbeef->plug_get_list ();
for (int i = 0; plugins[i]; i++) {
DB_plugin_t *p = plugins[i];
if (p->get_actions) {
actions = p->get_actions (NULL);
while (actions) {
if (actions->name && actions->title && !strcasecmp (actions->name, command)) {
break; // found
}
actions = actions->next;
}
if (actions) {
break;
}
}
}
return actions;
}
#ifndef __APPLE__
static int
get_x11_keycode (const char *name, Atom *syms, int first_kk, int last_kk, int ks_per_kk) {
int i, ks;
for (i = 0; i < last_kk-first_kk; i++)
{
int sym = * (syms + i*ks_per_kk);
for (ks = 0; keys[ks].name; ks++)
{
if ( (keys[ ks ].keysym == sym) && (0 == strcmp (name, keys[ ks ].name)))
{
return i+first_kk;
}
}
}
return 0;
}
static int
read_config (Display *disp) {
int ks_per_kk;
int first_kk, last_kk;
Atom* syms;
XDisplayKeycodes (disp, &first_kk, &last_kk);
syms = XGetKeyboardMapping (disp, first_kk, last_kk - first_kk, &ks_per_kk);
#else
#define ShiftMask (1<<0)
#define LockMask (1<<1)
#define ControlMask (1<<2)
#define Mod1Mask (1<<3)
#define Mod2Mask (1<<4)
#define Mod3Mask (1<<5)
#define Mod4Mask (1<<6)
#define Mod5Mask (1<<7)
int ks_per_kk = -1;
int first_kk = -1, last_kk = -1;
int* syms = NULL;
static int
read_config (void) {
#endif
DB_conf_item_t *item = deadbeef->conf_find ("hotkey.", NULL);
while (item) {
if (command_count == MAX_COMMAND_COUNT)
{
fprintf (stderr, "hotkeys: maximum number (%d) of commands exceeded\n", MAX_COMMAND_COUNT);
break;
}
command_t *cmd_entry = &commands[ command_count ];
memset (cmd_entry, 0, sizeof (command_t));
char token[MAX_TOKEN];
char keycombo[MAX_TOKEN];
const char *script = item->value;
if ((script = gettoken (script, keycombo)) == 0) {
trace ("hotkeys: unexpected eol (keycombo)\n");
goto out;
}
if ((script = gettoken (script, token)) == 0) {
trace ("hotkeys: unexpected eol (ctx)\n");
goto out;
}
cmd_entry->ctx = atoi (token);
if (cmd_entry->ctx < 0 || cmd_entry->ctx >= DDB_ACTION_CTX_COUNT) {
trace ("hotkeys: invalid ctx %d\n", cmd_entry->ctx);
goto out;
}
if ((script = gettoken (script, token)) == 0) {
trace ("hotkeys: unexpected eol (isglobal)\n");
goto out;
}
cmd_entry->isglobal = atoi (token);
if ((script = gettoken (script, token)) == 0) {
trace ("hotkeys: unexpected eol (action)\n");
goto out;
}
cmd_entry->action = find_action_by_name (token);
if (!cmd_entry->action) {
trace ("hotkeys: action not found %s\n", token);
goto out;
}
// parse key combo
int done = 0;
char* p;
char* space = keycombo;
do {
p = space;
space = strchr (p, ' ');
if (space) {
*space = 0;
space++;
}
else
done = 1;
if (0 == strcasecmp (p, "Ctrl"))
cmd_entry->modifier |= ControlMask;
else if (0 == strcasecmp (p, "Alt"))
cmd_entry->modifier |= Mod1Mask;
else if (0 == strcasecmp (p, "Shift"))
cmd_entry->modifier |= ShiftMask;
else if (0 == strcasecmp (p, "Super")) {
cmd_entry->modifier |= Mod4Mask;
}
else {
if (p[0] == '0' && p[1] == 'x') {
// parse hex keycode
int r = sscanf (p, "0x%x", &cmd_entry->keycode);
if (!r) {
cmd_entry->keycode = 0;
}
}
else {
// lookup name table
cmd_entry->keycode = get_keycode (p);
#ifndef __APPLE__
cmd_entry->x11_keycode = get_x11_keycode (p, syms, first_kk, last_kk, ks_per_kk);
trace ("%s: kc=%d, xkc=%d\n", p, cmd_entry->keycode, cmd_entry->x11_keycode);
#endif
}
if (!cmd_entry->keycode)
{
trace ("hotkeys: got 0 from get_keycode while adding hotkey: %s %s\n", item->key, item->value);
break;
}
}
} while (!done);
if (done) {
if (cmd_entry->keycode == 0) {
trace ("hotkeys: Key not found while parsing %s %s\n", item->key, item->value);
}
else {
command_count++;
}
}
out:
item = deadbeef->conf_find ("hotkey.", item);
}
#ifndef __APPLE__
XFree (syms);
int i;
// need to grab it here to prevent gdk_x_error from being called while we're
// doing it on other thread
for (i = 0; i < command_count; i++) {
if (!commands[i].isglobal) {
continue;
}
for (int f = 0; f < 16; f++) {
uint32_t flags = 0;
if (f & 1) {
flags |= LockMask;
}
if (f & 2) {
flags |= Mod2Mask;
}
if (f & 4) {
flags |= Mod3Mask;
}
if (f & 8) {
flags |= Mod5Mask;
}
trace ("XGrabKey %d %x\n", commands[i].keycode, commands[i].modifier | flags);
XGrabKey (disp, commands[i].x11_keycode, commands[i].modifier | flags, DefaultRootWindow (disp), False, GrabModeAsync, GrabModeAsync);
}
}
#endif
return 0;
}
DB_plugin_t *
hotkeys_load (DB_functions_t *api) {
deadbeef = api;
return DB_PLUGIN (&plugin);
}
static void
cleanup () {
command_count = 0;
#ifndef __APPLE__
if (disp) {
XCloseDisplay (disp);
disp = NULL;
}
#endif
}
#ifndef __APPLE__
static int
x_err_handler (Display *d, XErrorEvent *evt) {
#if 0
// this code crashes if gtk plugin is active
char buffer[1024];
XGetErrorText (d, evt->error_code, buffer, sizeof (buffer));
trace ("hotkeys: xlib error: %s\n", buffer);
#endif
return 0;
}
static void
hotkeys_event_loop (void *unused) {
int i;
#ifdef __linux__
prctl (PR_SET_NAME, "deadbeef-hotkeys", 0, 0, 0, 0);
#endif
while (!finished) {
if (need_reset) {
trace ("hotkeys: reinitializing\n");
XSetErrorHandler (x_err_handler);
for (int i = 0; i < command_count; i++) {
for (int f = 0; f < 16; f++) {
uint32_t flags = 0;
if (f & 1) {
flags |= LockMask;
}
if (f & 2) {
flags |= Mod2Mask;
}
if (f & 4) {
flags |= Mod3Mask;
}
if (f & 8) {
flags |= Mod5Mask;
}
XUngrabKey (disp, commands[i].x11_keycode, commands[i].modifier | flags, DefaultRootWindow (disp));
}
}
memset (commands, 0, sizeof (commands));
command_count = 0;
read_config (disp);
need_reset = 0;
}
XEvent event;
while (XPending (disp))
{
XNextEvent (disp, &event);
if (event.xkey.type == KeyPress)
{
int state = event.xkey.state;
// ignore caps/scroll/numlock
state &= (ShiftMask|ControlMask|Mod1Mask|Mod4Mask);
trace ("hotkeys: key %d mods %X (%X)\n", event.xkey.keycode, state, event.xkey.state);
trace ("filtered state=%X\n", state);
for (i = 0; i < command_count; i++) {
if ( (event.xkey.keycode == commands[ i ].x11_keycode) &&
(state == commands[ i ].modifier))
{
trace ("matches to commands[%d]!\n", i);
cmd_invoke_plugin_command (commands[i].action, commands[i].ctx);
break;
}
}
if (i == command_count) {
trace ("keypress doesn't match to any global hotkey\n");
}
}
}
usleep (200 * 1000);
}
}
#endif
static int
hotkeys_connect (void) {
#ifndef __APPLE__
finished = 0;
loop_tid = 0;
disp = XOpenDisplay (NULL);
if (!disp)
{
fprintf (stderr, "hotkeys: could not open display\n");
return -1;
}
XSetErrorHandler (x_err_handler);
read_config (disp);
int ks_per_kk;
int first_kk, last_kk;
Atom* syms;
XDisplayKeycodes (disp, &first_kk, &last_kk);
syms = XGetKeyboardMapping (disp, first_kk, last_kk - first_kk, &ks_per_kk);
init_mapped_keycodes (disp, syms, first_kk, last_kk, ks_per_kk);
XFree (syms);
XSync (disp, 0);
loop_tid = deadbeef->thread_start (hotkeys_event_loop, 0);
#else
read_config ();
#endif
return 0;
}
static int
hotkeys_disconnect (void) {
#ifndef __APPLE__
if (loop_tid) {
finished = 1;
deadbeef->thread_join (loop_tid);
}
#endif
cleanup ();
return 0;
}
const char *
hotkeys_get_name_for_keycode (int keycode) {
for (int i = 0; keys[i].name; i++) {
if (keycode == keys[i].keysym) {
return keys[i].name;
}
}
return NULL;
}
DB_plugin_action_t*
hotkeys_get_action_for_keycombo (int key, int mods, int isglobal, int *ctx) {
int i;
// find mapped keycode
if (key < 0x7f && isupper (key)) {
key = tolower (key);
}
int keycode = key;
trace ("hotkeys: keysym 0x%X mapped to 0x%X\n", key, keycode);
for (i = 0; i < command_count; i++) {
trace ("hotkeys: command %s keycode %x mods %x\n", commands[i].action->name, commands[i].keycode, commands[i].modifier);
if (commands[i].keycode == keycode && commands[i].modifier == mods && commands[i].isglobal == isglobal) {
*ctx = commands[i].ctx;
return commands[i].action;
}
}
return NULL;
}
void
hotkeys_reset (void) {
#ifndef __APPLE__
need_reset = 1;
trace ("hotkeys: reset flagged\n");
#endif
}
int
action_play_cb (struct DB_plugin_action_s *action, int ctx) {
// NOTE: this function is copied as on_playbtn_clicked in gtkui
DB_output_t *output = deadbeef->get_output ();
if (output->state () == OUTPUT_STATE_PAUSED) {
ddb_playlist_t *plt = deadbeef->plt_get_curr ();
int cur = deadbeef->plt_get_cursor (plt, PL_MAIN);
if (cur != -1) {
ddb_playItem_t *it = deadbeef->plt_get_item_for_idx (plt, cur, PL_MAIN);
ddb_playItem_t *it_playing = deadbeef->streamer_get_playing_track ();
if (it) {
deadbeef->pl_item_unref (it);
}
if (it_playing) {
deadbeef->pl_item_unref (it_playing);
}
if (it != it_playing) {
deadbeef->sendmessage (DB_EV_PLAY_NUM, 0, cur, 0);
}
else {
deadbeef->sendmessage (DB_EV_PLAY_CURRENT, 0, 1, 0);
}
}
else {
deadbeef->sendmessage (DB_EV_PLAY_CURRENT, 0, 0, 0);
}
deadbeef->plt_unref (plt);
}
else {
ddb_playlist_t *plt = deadbeef->plt_get_curr ();
int cur = -1;
if (plt) {
cur = deadbeef->plt_get_cursor (plt, PL_MAIN);
deadbeef->plt_unref (plt);
}
deadbeef->sendmessage (DB_EV_PLAY_NUM, 0, cur, 0);
}
return 0;
}
int
action_prev_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->sendmessage (DB_EV_PREV, 0, 0, 0);
return 0;
}
int
action_next_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->sendmessage (DB_EV_NEXT, 0, 0, 0);
return 0;
}
int
action_stop_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->sendmessage (DB_EV_STOP, 0, 0, 0);
return 0;
}
int
action_toggle_pause_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->sendmessage (DB_EV_TOGGLE_PAUSE, 0, 0, 0);
return 0;
}
int
action_play_pause_cb (struct DB_plugin_action_s *action, int ctx) {
int state = deadbeef->get_output ()->state ();
if (state == OUTPUT_STATE_PLAYING) {
deadbeef->sendmessage (DB_EV_PAUSE, 0, 0, 0);
}
else {
deadbeef->sendmessage (DB_EV_PLAY_CURRENT, 0, 0, 0);
}
return 0;
}
int
action_play_random_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->sendmessage (DB_EV_PLAY_RANDOM, 0, 0, 0);
return 0;
}
int
action_seek_5p_forward_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->pl_lock ();
DB_playItem_t *it = deadbeef->streamer_get_playing_track ();
if (it) {
float dur = deadbeef->pl_get_item_duration (it);
if (dur > 0) {
float pos = deadbeef->streamer_get_playpos ();
deadbeef->sendmessage (DB_EV_SEEK, 0, (pos + dur * 0.05f) * 1000, 0);
}
deadbeef->pl_item_unref (it);
}
deadbeef->pl_unlock ();
return 0;
}
int
action_seek_5p_backward_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->pl_lock ();
DB_playItem_t *it = deadbeef->streamer_get_playing_track ();
if (it) {
float dur = deadbeef->pl_get_item_duration (it);
if (dur > 0) {
float pos = deadbeef->streamer_get_playpos ();
deadbeef->sendmessage (DB_EV_SEEK, 0, (pos - dur * 0.05f) * 1000, 0);
}
deadbeef->pl_item_unref (it);
}
deadbeef->pl_unlock ();
return 0;
}
int
action_seek_1p_forward_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->pl_lock ();
DB_playItem_t *it = deadbeef->streamer_get_playing_track ();
if (it) {
float dur = deadbeef->pl_get_item_duration (it);
if (dur > 0) {
float pos = deadbeef->streamer_get_playpos ();
deadbeef->sendmessage (DB_EV_SEEK, 0, (pos + dur * 0.01f) * 1000, 0);
}
deadbeef->pl_item_unref (it);
}
deadbeef->pl_unlock ();
return 0;
}
int
action_seek_1p_backward_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->pl_lock ();
DB_playItem_t *it = deadbeef->streamer_get_playing_track ();
if (it) {
float dur = deadbeef->pl_get_item_duration (it);
if (dur > 0) {
float pos = deadbeef->streamer_get_playpos ();
deadbeef->sendmessage (DB_EV_SEEK, 0, (pos - dur * 0.01f) * 1000, 0);
}
deadbeef->pl_item_unref (it);
}
deadbeef->pl_unlock ();
return 0;
}
int
action_volume_up_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->volume_set_db (deadbeef->volume_get_db () + 2);
return 0;
}
int
action_volume_down_cb (struct DB_plugin_action_s *action, int ctx) {
deadbeef->volume_set_db (deadbeef->volume_get_db () - 2);
return 0;
}
int
action_toggle_stop_after_current_cb (struct DB_plugin_action_s *action, int ctx) {
int var = deadbeef->conf_get_int ("playlist.stop_after_current", 0);
var = 1 - var;
deadbeef->conf_set_int ("playlist.stop_after_current", var);
deadbeef->sendmessage (DB_EV_CONFIGCHANGED, 0, 0, 0);
return 0;
}
int
action_toggle_stop_after_album_cb (struct DB_plugin_action_s *action, int ctx) {
int var = deadbeef->conf_get_int ("playlist.stop_after_album", 0);
var = 1 - var;
deadbeef->conf_set_int ("playlist.stop_after_album", var);
deadbeef->sendmessage (DB_EV_CONFIGCHANGED, 0, 0, 0);
return 0;
}
static DB_plugin_action_t action_reload_metadata = {
.title = "Reload Metadata",
.name = "reload_metadata",
.flags = DB_ACTION_MULTIPLE_TRACKS,
.callback2 = action_reload_metadata_handler,
.next = NULL
};
static DB_plugin_action_t action_jump_to_current = {
.title = "Playback/Jump To Currently Playing Track",
.name = "jump_to_current_track",
.flags = DB_ACTION_COMMON,
.callback2 = action_jump_to_current_handler,
.next = &action_reload_metadata
};
static DB_plugin_action_t action_next_playlist = {
.title = "Next Playlist",
.name = "next_playlist",
.flags = DB_ACTION_COMMON,
.callback2 = action_next_playlist_handler,
.next = &action_jump_to_current
};
static DB_plugin_action_t action_prev_playlist = {
.title = "Prev Playlist",
.name = "prev_playlist",
.flags = DB_ACTION_COMMON,
.callback2 = action_prev_playlist_handler,
.next = &action_next_playlist
};
static DB_plugin_action_t action_playlist10 = {
.title = "Switch To Playlist 10",
.name = "playlist10",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist10_handler,
.next = &action_prev_playlist
};
static DB_plugin_action_t action_playlist9 = {
.title = "Switch To Playlist 9",
.name = "playlist9",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist9_handler,
.next = &action_playlist10
};
static DB_plugin_action_t action_playlist8 = {
.title = "Switch To Playlist 8",
.name = "playlist8",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist8_handler,
.next = &action_playlist9
};
static DB_plugin_action_t action_playlist7 = {
.title = "Switch To Playlist 7",
.name = "playlist7",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist7_handler,
.next = &action_playlist8
};
static DB_plugin_action_t action_playlist6 = {
.title = "Switch To Playlist 6",
.name = "playlist6",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist6_handler,
.next = &action_playlist7
};
static DB_plugin_action_t action_playlist5 = {
.title = "Switch To Playlist 5",
.name = "playlist5",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist5_handler,
.next = &action_playlist6
};
static DB_plugin_action_t action_playlist4 = {
.title = "Switch To Playlist 4",
.name = "playlist4",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist4_handler,
.next = &action_playlist5
};
static DB_plugin_action_t action_playlist3 = {
.title = "Switch To Playlist 3",
.name = "playlist3",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist3_handler,
.next = &action_playlist4
};
static DB_plugin_action_t action_playlist2 = {
.title = "Switch To Playlist 2",
.name = "playlist2",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist2_handler,
.next = &action_playlist3
};
static DB_plugin_action_t action_playlist1 = {
.title = "Switch To Playlist 1",
.name = "playlist1",
.flags = DB_ACTION_COMMON,
.callback2 = action_playlist1_handler,
.next = &action_playlist2
};
static DB_plugin_action_t action_sort_randomize = {
.title = "Edit/Sort Randomize",
.name = "sort_randomize",
.flags = DB_ACTION_COMMON,
.callback2 = action_sort_randomize_handler,
.next = &action_playlist1
};
static DB_plugin_action_t action_sort_by_date = {
.title = "Edit/Sort By Date",
.name = "sort_date",
.flags = DB_ACTION_COMMON,
.callback2 = action_sort_by_date_handler,
.next = &action_sort_randomize
};
static DB_plugin_action_t action_sort_by_artist = {
.title = "Edit/Sort By Artist",
.name = "sort_artist",
.flags = DB_ACTION_COMMON,
.callback2 = action_sort_by_artist_handler,
.next = &action_sort_by_date
};
static DB_plugin_action_t action_sort_by_album = {
.title = "Edit/Sort By Album",
.name = "sort_album",
.flags = DB_ACTION_COMMON,
.callback2 = action_sort_by_album_handler,
.next = &action_sort_by_artist
};
static DB_plugin_action_t action_sort_by_tracknr = {
.title = "Edit/Sort By Track Number",
.name = "sort_tracknr",
.flags = DB_ACTION_COMMON,
.callback2 = action_sort_by_tracknr_handler,
.next = &action_sort_by_album
};
static DB_plugin_action_t action_sort_by_title = {
.title = "Edit/Sort By Title",
.name = "sort_title",
.flags = DB_ACTION_COMMON,
.callback2 = action_sort_by_title_handler,
.next = &action_sort_by_tracknr
};
static DB_plugin_action_t action_invert_selection = {
.title = "Edit/Invert Selection",
.name = "invert_selection",
.flags = DB_ACTION_COMMON,
.callback2 = action_invert_selection_handler,
.next = &action_sort_by_tracknr
};
static DB_plugin_action_t action_clear_playlist = {
.title = "Edit/Clear Playlist",
.name = "clear_playlist",
.flags = DB_ACTION_COMMON,
.callback2 = action_clear_playlist_handler,
.next = &action_invert_selection
};
static DB_plugin_action_t action_remove_from_playqueue = {
.title = "Playback/Remove From Playback Queue",
.name = "remove_from_playback_queue",
.flags = DB_ACTION_MULTIPLE_TRACKS,
.callback2 = action_remove_from_playqueue_handler,
.next = &action_clear_playlist
};
static DB_plugin_action_t action_add_to_playqueue = {
.title = "Playback/Add To Playback Queue",
.name = "add_to_playback_queue",
.flags = DB_ACTION_MULTIPLE_TRACKS,
.callback2 = action_add_to_playqueue_handler,
.next = &action_remove_from_playqueue
};
static DB_plugin_action_t action_toggle_mute = {
.title = "Playback/Toggle Mute",
.name = "toggle_mute",
.flags = DB_ACTION_COMMON,
.callback2 = action_toggle_mute_handler,
.next = &action_add_to_playqueue
};
static DB_plugin_action_t action_play = {
.title = "Playback/Play",
.name = "play",
.flags = DB_ACTION_COMMON,
.callback2 = action_play_cb,
.next = &action_toggle_mute
};
static DB_plugin_action_t action_stop = {
.title = "Playback/Stop",
.name = "stop",
.flags = DB_ACTION_COMMON,
.callback2 = action_stop_cb,
.next = &action_play
};
static DB_plugin_action_t action_prev = {
.title = "Playback/Previous",
.name = "prev",
.flags = DB_ACTION_COMMON,
.callback2 = action_prev_cb,
.next = &action_stop
};
static DB_plugin_action_t action_next = {
.title = "Playback/Next",
.name = "next",
.flags = DB_ACTION_COMMON,
.callback2 = action_next_cb,
.next = &action_prev
};
static DB_plugin_action_t action_toggle_pause = {
.title = "Playback/Toggle Pause",
.name = "toggle_pause",
.flags = DB_ACTION_COMMON,
.callback2 = action_toggle_pause_cb,
.next = &action_next
};
static DB_plugin_action_t action_play_pause = {
.title = "Playback/Play\\/Pause",
.name = "play_pause",
.flags = DB_ACTION_COMMON,
.callback2 = action_play_pause_cb,
.next = &action_toggle_pause
};
static DB_plugin_action_t action_play_random = {
.title = "Playback/Play Random",
.name = "playback_random",
.flags = DB_ACTION_COMMON,
.callback2 = action_play_random_cb,
.next = &action_play_pause
};
static DB_plugin_action_t action_seek_1p_forward = {
.title = "Playback/Seek 1% Forward",
.name = "seek_1p_fwd",
.flags = DB_ACTION_COMMON,
.callback2 = action_seek_1p_forward_cb,
.next = &action_play_random
};
static DB_plugin_action_t action_seek_1p_backward = {
.title = "Playback/Seek 1% Backward",
.name = "seek_1p_back",
.flags = DB_ACTION_COMMON,
.callback2 = action_seek_1p_backward_cb,
.next = &action_seek_1p_forward
};
static DB_plugin_action_t action_seek_5p_forward = {
.title = "Playback/Seek 5% Forward",
.name = "seek_5p_fwd",
.flags = DB_ACTION_COMMON,
.callback2 = action_seek_5p_forward_cb,
.next = &action_seek_1p_backward
};
static DB_plugin_action_t action_seek_5p_backward = {
.title = "Playback/Seek 5% Backward",
.name = "seek_5p_back",
.flags = DB_ACTION_COMMON,
.callback2 = action_seek_5p_backward_cb,
.next = &action_seek_5p_forward
};
static DB_plugin_action_t action_volume_up = {
.title = "Playback/Volume Up",
.name = "volume_up",
.flags = DB_ACTION_COMMON,
.callback2 = action_volume_up_cb,
.next = &action_seek_5p_backward
};
static DB_plugin_action_t action_volume_down = {
.title = "Playback/Volume Down",
.name = "volume_down",
.flags = DB_ACTION_COMMON,
.callback2 = action_volume_down_cb,
.next = &action_volume_up
};
static DB_plugin_action_t action_toggle_stop_after_current = {
.title = "Playback/Toggle Stop After Current",
.name = "toggle_stop_after_current",
.flags = DB_ACTION_COMMON,
.callback2 = action_toggle_stop_after_current_cb,
.next = &action_volume_down
};
static DB_plugin_action_t action_toggle_stop_after_album = {
.title = "Playback/Toggle Stop After Current Album",
.name = "toggle_stop_after_album",
.flags = DB_ACTION_COMMON,
.callback2 = action_toggle_stop_after_album_cb,
.next = &action_toggle_stop_after_current
};
static DB_plugin_action_t *
hotkeys_get_actions (DB_playItem_t *it)
{
return &action_toggle_stop_after_album;
}
// define plugin interface
static DB_hotkeys_plugin_t plugin = {
.misc.plugin.api_vmajor = 1,
.misc.plugin.api_vminor = 5,
.misc.plugin.version_major = 1,
.misc.plugin.version_minor = 1,
.misc.plugin.type = DB_PLUGIN_MISC,
.misc.plugin.id = "hotkeys",
.misc.plugin.name = "Hotkey manager",
.misc.plugin.descr =
"Manages local and global hotkeys, and executes actions when the assigned key combinations are pressed\n\n"
"This plugin has its own API, to allow 3rd party GUI plugins to reuse the code.\n"
"Check the plugins/hotkeys/hotkeys.h in the source tree if you need this.\n\n"
"Changes in version 1.1\n"
" * adaptation to new deadbeef 0.6 plugin API\n"
" * added local hotkeys support\n"
,
.misc.plugin.copyright =
"Copyright (C) 2012-2013 Alexey Yakovenko <waker@users.sourceforge.net>\n"
"Copyright (C) 2009-2011 Viktor Semykin <thesame.ml@gmail.com>\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"
,
.misc.plugin.website = "http://deadbeef.sf.net",
.misc.plugin.get_actions = hotkeys_get_actions,
.misc.plugin.start = hotkeys_connect,
.misc.plugin.stop = hotkeys_disconnect,
.get_name_for_keycode = hotkeys_get_name_for_keycode,
.get_action_for_keycombo = hotkeys_get_action_for_keycombo,
.reset = hotkeys_reset,
};
|