-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathgtkui.c
2397 lines (2075 loc) · 84 KB
/
gtkui.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
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
/*
DeaDBeeF -- the music player
Copyright (C) 2009-2015 Oleksiy Yakovenko and other contributors
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.
*/
#ifdef HAVE_CONFIG_H
# include "../../config.h"
#endif
#ifdef __linux__
# include <sys/prctl.h>
#endif
#include <dispatch/dispatch.h>
#include <Block.h>
#include <gtk/gtk.h>
#include <jansson.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <deadbeef/deadbeef.h>
#include "../../gettext.h"
#include "../hotkeys/hotkeys.h"
#include "../libparser/parser.h"
#include "actionhandlers.h"
#include "actions.h"
#include "callbacks.h"
#include "clipboard.h"
#include "covermanager/covermanager.h"
#include "ddbtabstrip.h"
#include "drawing.h"
#include "eq.h"
#include "gtkui.h"
#include "gtkui_api.h"
#include "hotkeys.h"
#include "interface.h"
#include "medialib/medialibmanager.h"
#include "medialib/medialibwidget.h"
#include "playlist/plcommon.h"
#include "pluginconf.h"
#include "prefwin/prefwin.h"
#include "progress.h"
#include "rg.h"
#include "search.h"
#include "support.h"
#include "trkproperties.h"
#include "widgets.h"
#include "wingeom.h"
#include "plmenu.h"
#include "covermanager/albumartwidget.h"
#include "selpropertieswidget.h"
#include "undostack.h"
#include "undointegration.h"
#define USE_GTK_APPLICATION 1
#if GTK_CHECK_VERSION(3, 10, 0) && USE_GTK_APPLICATION
# include "deadbeefapp.h"
#endif
#define trace(...) \
{ fprintf (stderr, __VA_ARGS__); }
//#define trace(fmt,...)
static const char conf_autoopen_key[] = "gtkui.log.autoopen";
static const int conf_autoopen_default = 1;
ddb_gtkui_t plugin;
DB_functions_t *deadbeef;
// main widgets
GtkWidget *mainwin;
int gtkui_override_statusicon = 0;
GtkStatusIcon *trayicon;
GtkWidget *traymenu;
GtkWidget *logwindow;
static int gtkui_accept_messages = 0;
static gint refresh_timeout = 0;
static guint set_title_timeout_id;
int fileadded_listener_id;
int fileadd_beginend_listener_id;
// cached config variables
int gtkui_embolden_current_track;
int gtkui_embolden_tracks;
int gtkui_embolden_selected_tracks;
int gtkui_italic_selected_tracks;
int gtkui_italic_tracks;
int gtkui_italic_current_track;
int gtkui_tabstrip_embolden_playing;
int gtkui_tabstrip_italic_playing;
int gtkui_tabstrip_embolden_selected;
int gtkui_tabstrip_italic_selected;
int gtkui_groups_pinned;
int gtkui_listview_busy;
int gtkui_groups_spacing;
int gtkui_unicode_playstate = 0;
int gtkui_disable_seekbar_overlay = 0;
static gboolean _quitting_normally = FALSE;
#define TRAY_ICON "deadbeef_tray_icon"
const char *gtkui_default_titlebar_playing = "DeaDBeeF - %artist% - %title%";
const char *gtkui_default_titlebar_stopped = "DeaDBeeF";
static char *titlebar_playing_bc;
static char *titlebar_stopped_bc;
static char *statusbar_bc;
static char *statusbar_stopped_bc;
enum { INFO_TARGET_URIS };
static void
init_widget_layout (void);
// update status bar and window title
static int sb_context_id = -1;
static char sb_text[512];
static gboolean
_dispatch_on_main_wrapper (void *context) {
void (^block) (void) = context;
block ();
Block_release (block);
return FALSE;
}
void
gtkui_dispatch_on_main (void (^block) (void)) {
dispatch_block_t copy_block = Block_copy (block);
g_idle_add (_dispatch_on_main_wrapper, copy_block);
}
static void
format_timestr (char *buf, int sz, float time) {
time = roundf (time);
int daystotal = (int)time / (3600 * 24);
int hourtotal = ((int)time / 3600) % 24;
int mintotal = ((int)time / 60) % 60;
int sectotal = ((int)time) % 60;
if (daystotal == 0) {
snprintf (buf, sz, "%d:%02d:%02d", hourtotal, mintotal, sectotal);
}
else if (daystotal == 1) {
snprintf (buf, sz, _ ("1 day %d:%02d:%02d"), hourtotal, mintotal, sectotal);
}
else {
snprintf (buf, sz, _ ("%d days %d:%02d:%02d"), daystotal, hourtotal, mintotal, sectotal);
}
}
static gboolean
update_songinfo (gpointer unused) {
if (w_get_rootwidget () == NULL)
return FALSE;
int iconified = gdk_window_get_state (gtk_widget_get_window (mainwin)) & GDK_WINDOW_STATE_ICONIFIED;
if (!gtk_widget_get_visible (mainwin) || iconified) {
return FALSE;
}
DB_output_t *output = deadbeef->get_output ();
char sbtext_new[512] = "-";
float pl_totaltime = deadbeef->pl_get_totaltime ();
char totaltime_str[512] = "";
format_timestr (totaltime_str, sizeof (totaltime_str), pl_totaltime);
DB_playItem_t *track = deadbeef->streamer_get_playing_track_safe ();
ddb_tf_context_t ctx = { ._size = sizeof (ddb_tf_context_t),
.it = track,
.iter = PL_MAIN,
.plt = deadbeef->plt_get_curr () };
char buffer[200];
char *bc = NULL;
if (!output || (output->state () == DDB_PLAYBACK_STATE_STOPPED || !track)) {
bc = statusbar_stopped_bc;
}
else {
bc = statusbar_bc;
}
deadbeef->tf_eval (&ctx, bc, buffer, sizeof (buffer));
snprintf (
sbtext_new,
sizeof (sbtext_new),
"%s | %d tracks | %s %s",
buffer,
deadbeef->pl_getcount (PL_MAIN),
totaltime_str,
_ ("total playtime"));
if (strcmp (sbtext_new, sb_text)) {
strcpy (sb_text, sbtext_new);
// form statusline
GtkStatusbar *sb = GTK_STATUSBAR (lookup_widget (mainwin, "statusbar"));
if (sb_context_id == -1) {
sb_context_id = gtk_statusbar_get_context_id (sb, "msg");
}
gtk_statusbar_pop (sb, sb_context_id);
gtk_statusbar_push (sb, sb_context_id, sb_text);
}
if (track) {
deadbeef->pl_item_unref (track);
}
deadbeef->plt_unref (ctx.plt);
return FALSE;
}
void
set_tray_tooltip (const char *text) {
if (trayicon) {
#if !GTK_CHECK_VERSION(2, 16, 0)
gtk_status_icon_set_tooltip (trayicon, text);
#else
gtk_status_icon_set_tooltip_text (trayicon, text);
#endif
}
}
gboolean
on_trayicon_scroll_event (GtkWidget *widget, GdkEventScroll *event, gpointer user_data) {
int change_track = deadbeef->conf_get_int ("tray.scroll_changes_track", 0)
? ((event->state & GDK_CONTROL_MASK) == 0)
: ((event->state & GDK_CONTROL_MASK) != 0);
if (change_track) {
if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT) {
deadbeef->sendmessage (DB_EV_NEXT, 0, 0, 0);
}
else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT) {
deadbeef->sendmessage (DB_EV_PREV, 0, 0, 0);
}
return FALSE;
}
float vol = deadbeef->volume_get_db ();
int sens = deadbeef->conf_get_int ("gtkui.tray_volume_sensitivity", 1);
if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_RIGHT) {
vol += sens;
}
else if (event->direction == GDK_SCROLL_DOWN || event->direction == GDK_SCROLL_LEFT) {
vol -= sens;
}
if (vol > 0) {
vol = 0;
}
else if (vol < deadbeef->volume_get_min_db ()) {
vol = deadbeef->volume_get_min_db ();
}
deadbeef->volume_set_db (vol);
#if 0
char str[100];
if (deadbeef->conf_get_int ("gtkui.show_gain_in_db", 1)) {
snprintf (str, sizeof (str), "Gain: %s%d dB", vol == 0 ? "+" : "", (int)vol);
}
else {
snprintf (str, sizeof (str), "Gain: %d%%", (int)(deadbeef->volume_get_amp () * 100));
}
set_tray_tooltip (str);
#endif
return FALSE;
}
static gboolean
show_traymenu_cb (gpointer data) {
if (!traymenu) {
traymenu = create_traymenu ();
}
gtk_menu_popup (GTK_MENU (traymenu), NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time ());
return FALSE;
}
static void
show_traymenu (void) {
g_idle_add (show_traymenu_cb, NULL);
}
#if GTK_CHECK_VERSION(3, 0, 0)
static gboolean
mainwin_hide_cb (gpointer data) {
gtk_widget_hide (mainwin);
return FALSE;
}
#endif
void
mainwin_toggle_visible (void) {
int iconified = gdk_window_get_state (gtk_widget_get_window (mainwin)) & GDK_WINDOW_STATE_ICONIFIED;
if (gtk_widget_get_visible (mainwin) && !iconified) {
gtk_widget_hide (mainwin);
}
else {
if (w_get_rootwidget () == NULL)
init_widget_layout ();
wingeom_restore (mainwin, "mainwin", 40, 40, 500, 300, 0);
if (iconified) {
gtk_window_deiconify (GTK_WINDOW (mainwin));
}
else {
gtk_window_present (GTK_WINDOW (mainwin));
}
}
}
#if !GTK_CHECK_VERSION(2, 14, 0)
gboolean
on_trayicon_activate (GtkWidget *widget, gpointer user_data) {
mainwin_toggle_visible ();
return FALSE;
}
#else
gboolean
on_trayicon_button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data) {
if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
mainwin_toggle_visible ();
}
else if (event->button == 2 && event->type == GDK_BUTTON_PRESS) {
deadbeef->sendmessage (DB_EV_TOGGLE_PAUSE, 0, 0, 0);
}
return FALSE;
}
#endif
gboolean
on_trayicon_popup_menu (GtkWidget *widget, guint button, guint time, gpointer user_data) {
gtk_menu_popup (GTK_MENU (traymenu), NULL, NULL, gtk_status_icon_position_menu, trayicon, button, time);
return FALSE;
}
static gboolean
activate_cb (gpointer nothing) {
if (w_get_rootwidget () == NULL)
init_widget_layout ();
wingeom_restore (mainwin, "mainwin", 40, 40, 500, 300, 0);
gtk_widget_show (mainwin);
gtk_window_present (GTK_WINDOW (mainwin));
return FALSE;
}
static void
titlebar_tf_free (void) {
if (titlebar_playing_bc) {
deadbeef->tf_free (titlebar_playing_bc);
titlebar_playing_bc = NULL;
}
if (titlebar_stopped_bc) {
deadbeef->tf_free (titlebar_stopped_bc);
titlebar_stopped_bc = NULL;
}
if (statusbar_bc) {
deadbeef->tf_free (statusbar_bc);
statusbar_bc = NULL;
}
if (statusbar_stopped_bc) {
deadbeef->tf_free (statusbar_stopped_bc);
statusbar_stopped_bc = NULL;
}
}
void
gtkui_titlebar_tf_init (void) {
titlebar_tf_free ();
char fmt[500];
deadbeef->conf_get_str ("gtkui.titlebar_playing_tf", gtkui_default_titlebar_playing, fmt, sizeof (fmt));
titlebar_playing_bc = deadbeef->tf_compile (fmt);
deadbeef->conf_get_str ("gtkui.titlebar_stopped_tf", gtkui_default_titlebar_stopped, fmt, sizeof (fmt));
titlebar_stopped_bc = deadbeef->tf_compile (fmt);
static const char statusbar_tf_with_seltime_fmt[] =
"$if2($strcmp(%%ispaused%%,),%s | )$if2($upper(%%codec%%),-) |[ %%playback_bitrate%% kbps |][ %%samplerate%%Hz |][ %%:BPS%% %s |][ %%channels%% |] %%playback_time%% / %%length%% | %%selection_playback_time%% %s";
static const char statusbar_tf_fmt[] =
"$if2($strcmp(%%ispaused%%,),%s | )$if2($upper(%%codec%%),-) |[ %%playback_bitrate%% kbps |][ %%samplerate%%Hz |][ %%:BPS%% %s |][ %%channels%% |] %%playback_time%% / %%length%%";
const char statusbar_stopped_tf_with_seltime_fmt[] = "%s | %%selection_playback_time%% %s";
const char statusbar_stopped_tf_fmt[] = "%s";
char statusbar_tf[1024];
char statusbar_stopped_tf[1024];
if (deadbeef->conf_get_int ("gtkui.statusbar_seltime", 0)) {
snprintf (
statusbar_tf,
sizeof (statusbar_tf),
statusbar_tf_with_seltime_fmt,
_ ("Paused"),
_ ("bit"),
_ ("selection playtime"));
snprintf (
statusbar_stopped_tf,
sizeof (statusbar_stopped_tf),
statusbar_stopped_tf_with_seltime_fmt,
_ ("Stopped"),
_ ("selection playtime"));
}
else {
snprintf (statusbar_tf, sizeof (statusbar_tf), statusbar_tf_fmt, _ ("Paused"), _ ("bit"));
snprintf (statusbar_stopped_tf, sizeof (statusbar_stopped_tf), statusbar_stopped_tf_fmt, _ ("Stopped"));
}
statusbar_bc = deadbeef->tf_compile (statusbar_tf);
statusbar_stopped_bc = deadbeef->tf_compile (statusbar_stopped_tf);
}
static gboolean
set_title_cb (gpointer data) {
gtkui_set_titlebar (NULL);
return FALSE;
}
void
gtkui_set_titlebar (DB_playItem_t *it) {
if (!it) {
it = deadbeef->streamer_get_playing_track_safe ();
}
else {
deadbeef->pl_item_ref (it);
}
char str[1024];
ddb_tf_context_t ctx = {
._size = sizeof (ddb_tf_context_t),
.it = it,
// FIXME: current playlist is not correct here.
// need the playlist corresponding to the pointed track
.plt = deadbeef->plt_get_curr (),
};
deadbeef->tf_eval (&ctx, it ? titlebar_playing_bc : titlebar_stopped_bc, str, sizeof (str));
if (ctx.plt) {
deadbeef->plt_unref (ctx.plt);
ctx.plt = NULL;
}
gtk_window_set_title (GTK_WINDOW (mainwin), str);
if (it) {
deadbeef->pl_item_unref (it);
}
set_tray_tooltip (str);
if (ctx.update > 0) {
set_title_timeout_id = g_timeout_add (ctx.update, set_title_cb, NULL);
}
}
static gboolean
trackinfochanged_cb (gpointer data) {
DB_playItem_t *track = data;
DB_playItem_t *curr = deadbeef->streamer_get_playing_track_safe ();
if (track == curr) {
gtkui_set_titlebar (track);
}
if (track) {
deadbeef->pl_item_unref (track);
}
if (curr) {
deadbeef->pl_item_unref (curr);
}
return FALSE;
}
static gboolean
playlistcontentchanged_cb (gpointer none) {
trkproperties_fill_metadata ();
return FALSE;
}
static gboolean
gtkui_on_frameupdate (gpointer data) {
update_songinfo (NULL);
return TRUE;
}
static gboolean
gtkui_update_status_icon (gpointer unused) {
int hide_tray_icon = deadbeef->conf_get_int ("gtkui.hide_tray_icon", 0);
if (gtkui_override_statusicon) {
hide_tray_icon = 1;
}
if (hide_tray_icon && !trayicon) {
return FALSE;
}
if (trayicon) {
if (hide_tray_icon) {
g_object_set (trayicon, "visible", FALSE, NULL);
}
else {
g_object_set (trayicon, "visible", TRUE, NULL);
}
return FALSE;
}
// system tray icon
traymenu = create_traymenu ();
char tmp[1000];
const char *icon_name = tmp;
deadbeef->conf_get_str ("gtkui.custom_tray_icon", TRAY_ICON, tmp, sizeof (tmp));
GtkIconTheme *theme = gtk_icon_theme_get_default ();
if (!gtk_icon_theme_has_icon (theme, icon_name))
icon_name = "deadbeef";
else {
GtkIconInfo *icon_info = gtk_icon_theme_lookup_icon (theme, icon_name, 48, GTK_ICON_LOOKUP_USE_BUILTIN);
const gboolean icon_is_builtin = gtk_icon_info_get_filename (icon_info) == NULL;
gtk_icon_info_free (icon_info);
icon_name = icon_is_builtin ? "deadbeef" : icon_name;
}
if (!gtk_icon_theme_has_icon (theme, icon_name)) {
char iconpath[1024];
snprintf (iconpath, sizeof (iconpath), "%s/deadbeef.png", deadbeef->get_system_dir (DDB_SYS_DIR_PREFIX));
trayicon = gtk_status_icon_new_from_file (iconpath);
}
else {
trayicon = gtk_status_icon_new_from_icon_name (icon_name);
}
if (hide_tray_icon) {
g_object_set (trayicon, "visible", FALSE, NULL);
}
#if !GTK_CHECK_VERSION(2, 14, 0)
g_signal_connect ((gpointer)trayicon, "activate", G_CALLBACK (on_trayicon_activate), NULL);
#else
printf ("connecting button tray signals\n");
g_signal_connect ((gpointer)trayicon, "scroll_event", G_CALLBACK (on_trayicon_scroll_event), NULL);
g_signal_connect ((gpointer)trayicon, "button_press_event", G_CALLBACK (on_trayicon_button_press_event), NULL);
#endif
g_signal_connect ((gpointer)trayicon, "popup_menu", G_CALLBACK (on_trayicon_popup_menu), NULL);
gtkui_set_titlebar (NULL);
return FALSE;
}
static void
override_builtin_statusicon (int override) {
gtkui_override_statusicon = override;
g_idle_add (gtkui_update_status_icon, NULL);
}
static void
gtkui_hide_status_icon (void) {
if (trayicon) {
g_object_set (trayicon, "visible", FALSE, NULL);
}
}
int
gtkui_get_curr_playlist_mod (void) {
ddb_playlist_t *plt = deadbeef->plt_get_curr ();
int res = plt ? deadbeef->plt_get_modification_idx (plt) : 0;
if (plt) {
deadbeef->plt_unref (plt);
}
return res;
}
int
gtkui_rename_playlist (ddb_playlist_t *plt) {
GtkWidget *dlg = create_entrydialog ();
gtk_window_set_transient_for (GTK_WINDOW (dlg), GTK_WINDOW (mainwin));
gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_RESPONSE_OK);
gtk_window_set_title (GTK_WINDOW (dlg), _ ("Rename Playlist"));
GtkWidget *e;
e = lookup_widget (dlg, "title_label");
gtk_label_set_text (GTK_LABEL (e), _ ("Title:"));
e = lookup_widget (dlg, "title");
char t[1000];
deadbeef->plt_get_title (plt, t, sizeof (t));
gtk_entry_set_text (GTK_ENTRY (e), t);
int res = gtk_dialog_run (GTK_DIALOG (dlg));
if (res == GTK_RESPONSE_OK) {
const char *text = gtk_entry_get_text (GTK_ENTRY (e));
deadbeef->plt_set_title (plt, text);
}
gtk_widget_destroy (dlg);
return 0;
}
int
gtkui_remove_playlist (ddb_playlist_t *plt) {
int plt_idx = deadbeef->plt_get_idx (plt);
if (plt_idx == -1) {
return -1;
}
char title[500];
if (deadbeef->plt_get_first (plt, PL_MAIN) != NULL) {
// playlist not empty, confirm first.
deadbeef->plt_get_title (plt, title, sizeof (title));
GtkWidget *dlg = gtk_message_dialog_new (
GTK_WINDOW (mainwin),
GTK_DIALOG_MODAL,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_YES_NO,
_ ("Removing playlist"));
gtk_message_dialog_format_secondary_text (
GTK_MESSAGE_DIALOG (dlg),
_ ("Do you really want to remove the playlist '%s'?"),
title);
gtk_window_set_title (GTK_WINDOW (dlg), _ ("Warning"));
int response = gtk_dialog_run (GTK_DIALOG (dlg));
gtk_widget_destroy (dlg);
if (response != GTK_RESPONSE_YES) {
return -1;
}
}
deadbeef->plt_remove (plt_idx);
return 0;
}
int
gtkui_remove_playlist_at_index (int plt_idx) {
int res = -1;
ddb_playlist_t *p = deadbeef->plt_get_for_idx (plt_idx);
if (p != NULL) {
res = gtkui_remove_playlist (p);
}
return res;
}
int
gtkui_rename_playlist_at_index (int plt_idx) {
int res = -1;
ddb_playlist_t *p = deadbeef->plt_get_for_idx (plt_idx);
if (p != NULL) {
res = gtkui_rename_playlist (p);
deadbeef->plt_unref (p);
}
return res;
}
void
gtkui_setup_gui_refresh (void) {
int tm = 1000 / gtkui_get_gui_refresh_rate ();
if (refresh_timeout) {
g_source_remove (refresh_timeout);
refresh_timeout = 0;
}
refresh_timeout = g_timeout_add (tm, gtkui_on_frameupdate, NULL);
}
static gboolean
gtkui_on_configchanged (void *data) {
// order and looping
const char *w;
// order
const char *orderwidgets[4] = { "order_linear", "order_shuffle", "order_random", "order_shuffle_albums" };
w = orderwidgets[deadbeef->streamer_get_shuffle ()];
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (lookup_widget (mainwin, w)), TRUE);
// looping
const char *loopingwidgets[3] = { "loop_all", "loop_disable", "loop_single" };
w = loopingwidgets[deadbeef->streamer_get_repeat ()];
gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (lookup_widget (mainwin, w)), TRUE);
// scroll follows playback
gtk_check_menu_item_set_active (
GTK_CHECK_MENU_ITEM (lookup_widget (mainwin, "scroll_follows_playback")),
deadbeef->conf_get_int ("playlist.scroll.followplayback", 1) ? TRUE : FALSE);
// cursor follows playback
gtk_check_menu_item_set_active (
GTK_CHECK_MENU_ITEM (lookup_widget (mainwin, "cursor_follows_playback")),
deadbeef->conf_get_int ("playlist.scroll.cursorfollowplayback", 1) ? TRUE : FALSE);
// stop after current track
int stop_after_current = deadbeef->conf_get_int ("playlist.stop_after_current", 0);
gtk_check_menu_item_set_active (
GTK_CHECK_MENU_ITEM (lookup_widget (mainwin, "stop_after_current")),
stop_after_current ? TRUE : FALSE);
// stop after current album
int stop_after_album = deadbeef->conf_get_int ("playlist.stop_after_album", 0);
gtk_check_menu_item_set_active (
GTK_CHECK_MENU_ITEM (lookup_widget (mainwin, "stop_after_album")),
stop_after_album ? TRUE : FALSE);
gtkui_embolden_current_track = deadbeef->conf_get_int ("gtkui.embolden_current_track", 0);
gtkui_embolden_tracks = deadbeef->conf_get_int ("gtkui.embolden_tracks", 0);
gtkui_embolden_selected_tracks = deadbeef->conf_get_int ("gtkui.embolden_selected_tracks", 0);
gtkui_italic_current_track = deadbeef->conf_get_int ("gtkui.italic_current_track", 0);
gtkui_italic_tracks = deadbeef->conf_get_int ("gtkui.italic_tracks", 0);
gtkui_italic_selected_tracks = deadbeef->conf_get_int ("gtkui.italic_selected_tracks", 0);
gtkui_tabstrip_embolden_playing = deadbeef->conf_get_int ("gtkui.tabstrip_embolden_playing", 0);
gtkui_tabstrip_italic_playing = deadbeef->conf_get_int ("gtkui.tabstrip_italic_playing", 0);
gtkui_tabstrip_embolden_selected = deadbeef->conf_get_int ("gtkui.tabstrip_embolden_selected", 0);
gtkui_tabstrip_italic_selected = deadbeef->conf_get_int ("gtkui.tabstrip_italic_selected", 0);
// titlebar tf
gtkui_titlebar_tf_init ();
// pin groups
gtkui_groups_pinned = deadbeef->conf_get_int ("playlist.pin.groups", 0);
gtkui_groups_spacing = deadbeef->conf_get_int ("playlist.groups.spacing", 0);
// play state images
gtkui_unicode_playstate = deadbeef->conf_get_int ("gtkui.unicode_playstate", 0);
// seekbar overlay
gtkui_disable_seekbar_overlay = deadbeef->conf_get_int ("gtkui.disable_seekbar_overlay", 0);
// tray icon
gtkui_update_status_icon (NULL);
// statusbar refresh
gtkui_setup_gui_refresh ();
return FALSE;
}
static gboolean
outputchanged_cb (gpointer nothing) {
prefwin_fill_soundcards ();
return FALSE;
}
void
save_playlist_as (void) {
gdk_threads_add_idle (action_save_playlist_handler_cb, NULL);
}
void
on_playlist_save_as_activate (GtkMenuItem *menuitem, gpointer user_data) {
save_playlist_as ();
}
void
on_playlist_load_activate (GtkMenuItem *menuitem, gpointer user_data) {
gdk_threads_add_idle (action_load_playlist_handler_cb, NULL);
}
void
on_add_location_activate (GtkMenuItem *menuitem, gpointer user_data) {
gdk_threads_add_idle (action_add_location_handler_cb, NULL);
}
int
gtkui_add_new_playlist (void) {
int cnt = deadbeef->plt_get_count ();
int i;
int idx = 0;
for (;;) {
char name[100];
if (!idx) {
strcpy (name, _ ("New Playlist"));
}
else {
snprintf (name, sizeof (name), _ ("New Playlist (%d)"), idx);
}
deadbeef->pl_lock ();
for (i = 0; i < cnt; i++) {
char t[100];
ddb_playlist_t *plt = deadbeef->plt_get_for_idx (i);
deadbeef->plt_get_title (plt, t, sizeof (t));
deadbeef->plt_unref (plt);
if (!strcasecmp (t, name)) {
break;
}
}
deadbeef->pl_unlock ();
if (i == cnt) {
return deadbeef->plt_add (cnt, name);
}
idx++;
}
return -1;
}
int
gtkui_get_gui_refresh_rate (void) {
int fps = deadbeef->conf_get_int ("gtkui.refresh_rate", 10);
if (fps < 1) {
fps = 1;
}
else if (fps > 30) {
fps = 30;
}
return fps;
}
static void
send_messages_to_widgets (ddb_gtkui_widget_t *w, uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2) {
for (ddb_gtkui_widget_t *c = w->children; c; c = c->next) {
send_messages_to_widgets (c, id, ctx, p1, p2);
}
if (w->message) {
w->message (w, id, ctx, p1, p2);
}
}
gboolean
add_mainmenu_actions_cb (void *data) {
add_mainmenu_actions ();
return FALSE;
}
int
gtkui_plt_add_dir (
ddb_playlist_t *plt,
const char *dirname,
int (*cb) (DB_playItem_t *it, void *data),
void *user_data);
int
gtkui_plt_add_file (
ddb_playlist_t *plt,
const char *filename,
int (*cb) (DB_playItem_t *it, void *data),
void *user_data);
int
gtkui_pl_add_files_begin (ddb_playlist_t *plt);
void
gtkui_pl_add_files_end (void);
DB_playItem_t *
gtkui_plt_load (
ddb_playlist_t *plt,
DB_playItem_t *after,
const char *fname,
int *pabort,
int (*cb) (DB_playItem_t *it, void *data),
void *user_data);
static int
is_current_playlist (DB_playItem_t *it) {
ddb_playlist_t *plt = deadbeef->plt_get_curr ();
if (plt) {
ddb_playlist_t *it_plt = deadbeef->pl_get_playlist (it);
if (it_plt) {
if (it_plt == plt) {
deadbeef->plt_unref (it_plt);
deadbeef->plt_unref (plt);
return 1;
}
deadbeef->plt_unref (it_plt);
}
deadbeef->plt_unref (plt);
}
return 0;
}
// The intended scroll position is set for background tabs, etc.
// This will be overwritten if there is a current main listview, or when the playlist is loaded by a listview
static void
playlist_set_intended_scroll (DB_playItem_t *it) {
ddb_playlist_t *plt = deadbeef->pl_get_playlist (it);
if (plt) {
int idx = deadbeef->plt_get_item_idx (plt, it, PL_MAIN);
if (idx != -1) {
deadbeef->plt_set_scroll (plt, -1 * idx);
}
deadbeef->plt_unref (plt);
}
}
static gboolean
pre_songstarted_cb (gpointer data) {
DB_playItem_t *it = data;
if ((!gtkui_listview_busy || !is_current_playlist (it)) &&
deadbeef->conf_get_int ("playlist.scroll.followplayback", 1)) {
playlist_set_intended_scroll (it);
}
deadbeef->pl_item_unref (it);
return FALSE;
}
static gboolean
pre_trackfocus_cb (gpointer data) {
DB_playItem_t *it = deadbeef->streamer_get_playing_track_safe ();
if (it) {
playlist_set_intended_scroll (it);
deadbeef->pl_item_unref (it);
}
return FALSE;
}
// These functions handle cases that need a playlist switch, or when there is no main listview to take the message
static void
playlist_set_cursor (ddb_playlist_t *plt, DB_playItem_t *it) {
int idx = deadbeef->plt_get_item_idx (plt, it, PL_MAIN);
if (idx != -1) {
if (deadbeef->pl_is_selected (it) || idx != deadbeef->plt_get_cursor (plt, PL_MAIN) ||
deadbeef->plt_getselcount (plt) != 1) {
deadbeef->plt_deselect_all (plt);
deadbeef->pl_set_selected (it, 1);
deadbeef->plt_set_cursor (plt, PL_MAIN, idx);
ddb_event_track_t *event = (ddb_event_track_t *)deadbeef->event_alloc (DB_EV_CURSOR_MOVED);
event->track = it;
deadbeef->pl_item_ref (it);
deadbeef->event_send ((ddb_event_t *)event, PL_MAIN, 0);
}
}
}
static gboolean
songstarted_cb (gpointer data) {
DB_playItem_t *it = data;
if ((!gtkui_listview_busy || !is_current_playlist (it)) &&
deadbeef->conf_get_int ("playlist.scroll.cursorfollowplayback", 1)) {
deadbeef->pl_lock ();
ddb_playlist_t *plt = deadbeef->pl_get_playlist (it);
if (plt) {
playlist_set_cursor (plt, it);
deadbeef->plt_unref (plt);
}
deadbeef->pl_unlock ();
}
deadbeef->pl_item_unref (it);
return FALSE;
}
static gboolean
trackfocus_cb (gpointer data) {
DB_playItem_t *it = deadbeef->streamer_get_playing_track_safe ();
if (it) {
deadbeef->pl_lock ();
ddb_playlist_t *plt = deadbeef->pl_get_playlist (it);
if (plt) {
deadbeef->plt_set_curr (plt);
playlist_set_cursor (plt, it);
deadbeef->plt_unref (plt);
}
deadbeef->pl_unlock ();
deadbeef->pl_item_unref (it);
}
return FALSE;
}
static int
gtkui_message (uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2) {
if (!gtkui_accept_messages) {
return -1;
}
if (id == DB_EV_PLAYLISTCHANGED) {
gtkui_dispatch_on_main(^{
deadbeef->undo_process();
});
}
switch (id) {
case DB_EV_SONGSTARTED: {
ddb_event_track_t *ev = (ddb_event_track_t *)ctx;
if (ev->track) {
deadbeef->pl_item_ref (ev->track);
g_idle_add (pre_songstarted_cb, ev->track);
}
break;
}
case DB_EV_TRACKFOCUSCURRENT:
g_idle_add (pre_trackfocus_cb, NULL);
break;
case DB_EV_SONGCHANGED: