forked from lipnitsk/mocp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.c
4379 lines (3620 loc) · 96.5 KB
/
interface.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
/*
* MOC - music on console
* Copyright (C) 2004 - 2006 Damian Pietras <[email protected]>
*
* 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.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdarg.h>
#include <locale.h>
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <dirent.h>
#include <sys/select.h>
#define DEBUG
#include "common.h"
#include "log.h"
#include "interface_elements.h"
#include "interface.h"
#include "lists.h"
#include "playlist.h"
#include "playlist_file.h"
#include "protocol.h"
#include "keys.h"
#include "options.h"
#include "files.h"
#include "decoder.h"
#include "themes.h"
#include "softmixer.h"
#include "utf8.h"
#define INTERFACE_LOG "mocp_client_log"
#define PLAYLIST_FILE "playlist.m3u"
#define QUEUE_CLEAR_THRESH 128
/* Socket of the server connection. */
static int srv_sock = -1;
static struct plist *playlist = NULL; /* our playlist */
static struct plist *queue = NULL; /* our queue */
static struct plist *dir_plist = NULL; /* contents of the current directory */
/* Queue for events coming from the server. */
static struct event_queue events;
/* Current working directory (the directory we show). */
static char cwd[PATH_MAX] = "";
/* If the user presses quit, or we receive a termination signal. */
static volatile enum want_quit want_quit = NO_QUIT;
/* If user presses CTRL-C, set this to 1. This should interrupt long
* operations which block the interface. */
static volatile int wants_interrupt = 0;
#ifdef SIGWINCH
/* If we get SIGWINCH. */
static volatile int want_resize = 0;
#endif
/* Are we waiting for the playlist we have loaded and sent to the clients? */
static int waiting_for_plist_load = 0;
/* Information about the currently played file. */
static struct file_info curr_file;
/* Silent seeking - where we are in seconds. -1 - no seeking. */
static int silent_seek_pos = -1;
static time_t silent_seek_key_last = (time_t)0; /* when the silent seek key was
last used */
/* When the menu was last moved (arrow keys, page up, etc.) */
static time_t last_menu_move_time = (time_t)0;
static void sig_quit (int sig LOGIT_ONLY)
{
log_signal (sig);
want_quit = QUIT_CLIENT;
}
static void sig_interrupt (int sig LOGIT_ONLY)
{
log_signal (sig);
wants_interrupt = 1;
}
#ifdef SIGWINCH
static void sig_winch (int sig LOGIT_ONLY)
{
log_signal (sig);
want_resize = 1;
}
#endif
int user_wants_interrupt ()
{
return wants_interrupt;
}
static void clear_interrupt ()
{
wants_interrupt = 0;
}
static void send_int_to_srv (const int num)
{
if (!send_int(srv_sock, num))
fatal ("Can't send() int to the server!");
}
static void send_bool_to_srv (const bool t)
{
if (!send_int(srv_sock, t ? 1 : 0))
fatal ("Can't send() bool to the server!");
}
static void send_str_to_srv (const char *str)
{
if (!send_str(srv_sock, str))
fatal ("Can't send() string to the server!");
}
static void send_item_to_srv (const struct plist_item *item)
{
if (!send_item(srv_sock, item))
fatal ("Can't send() item to the server!");
}
static int get_int_from_srv ()
{
int num;
if (!get_int(srv_sock, &num))
fatal ("Can't receive value from the server!");
return num;
}
static bool get_bool_from_srv ()
{
int num;
if (!get_int(srv_sock, &num))
fatal ("Can't receive value from the server!");
return num == 1 ? true : false;
}
/* Returned memory is malloc()ed. */
static char *get_str_from_srv ()
{
char *str = get_str (srv_sock);
if (!str)
fatal ("Can't receive string from the server!");
return str;
}
static struct file_tags *recv_tags_from_srv ()
{
struct file_tags *tags = recv_tags (srv_sock);
if (!tags)
fatal ("Can't receive tags from the server!");
return tags;
}
/* Noblocking version of get_int_from_srv(): return 0 if there are no data. */
static int get_int_from_srv_noblock (int *num)
{
enum noblock_io_status st;
if ((st = get_int_noblock(srv_sock, num)) == NB_IO_ERR)
fatal ("Can't receive value from the server!");
return st == NB_IO_OK ? 1 : 0;
}
static struct plist_item *recv_item_from_srv ()
{
struct plist_item *item;
if (!(item = recv_item(srv_sock)))
fatal ("Can't receive item from the server!");
return item;
}
static struct tag_ev_response *recv_tags_data_from_srv ()
{
struct tag_ev_response *r;
r = (struct tag_ev_response *)xmalloc (sizeof(struct tag_ev_response));
r->file = get_str_from_srv ();
if (!(r->tags = recv_tags(srv_sock)))
fatal ("Can't receive tags event's data from the server!");
return r;
}
static struct move_ev_data *recv_move_ev_data_from_srv ()
{
struct move_ev_data *d;
if (!(d = recv_move_ev_data(srv_sock)))
fatal ("Can't receive move data from the server!");
return d;
}
/* Receive data for the given type of event and return them. Return NULL if
* there is no data for the event. */
static void *get_event_data (const int type)
{
switch (type) {
case EV_PLIST_ADD:
case EV_QUEUE_ADD:
return recv_item_from_srv ();
case EV_PLIST_DEL:
case EV_QUEUE_DEL:
case EV_STATUS_MSG:
case EV_SRV_ERROR:
return get_str_from_srv ();
case EV_FILE_TAGS:
return recv_tags_data_from_srv ();
case EV_PLIST_MOVE:
case EV_QUEUE_MOVE:
return recv_move_ev_data_from_srv ();
}
return NULL;
}
/* Wait for EV_DATA handling other events. */
static void wait_for_data ()
{
int event;
do {
event = get_int_from_srv ();
if (event == EV_EXIT)
interface_fatal ("The server exited!");
if (event != EV_DATA)
event_push (&events, event, get_event_data(event));
} while (event != EV_DATA);
}
/* Get an integer value from the server that will arrive after EV_DATA. */
static int get_data_int ()
{
wait_for_data ();
return get_int_from_srv ();
}
/* Get a boolean value from the server that will arrive after EV_DATA. */
static bool get_data_bool ()
{
wait_for_data ();
return get_bool_from_srv ();
}
/* Get a string value from the server that will arrive after EV_DATA. */
static char *get_data_str ()
{
wait_for_data ();
return get_str_from_srv ();
}
static struct file_tags *get_data_tags ()
{
wait_for_data ();
return recv_tags_from_srv ();
}
static int send_tags_request (const char *file, const int tags_sel)
{
assert (file != NULL);
assert (tags_sel != 0);
if (file_type(file) == F_SOUND) {
send_int_to_srv (CMD_GET_FILE_TAGS);
send_str_to_srv (file);
send_int_to_srv (tags_sel);
debug ("Asking for tags for %s", file);
return 1;
}
else {
debug ("Not sending tags request for URL (%s)", file);
return 0;
}
}
/* Send all items from this playlist to other clients. */
static void send_items_to_clients (const struct plist *plist)
{
int i;
for (i = 0; i < plist->num; i++)
if (!plist_deleted(plist, i)) {
send_int_to_srv (CMD_CLI_PLIST_ADD);
send_item_to_srv (&plist->items[i]);
}
}
static void init_playlists ()
{
dir_plist = (struct plist *)xmalloc (sizeof(struct plist));
plist_init (dir_plist);
playlist = (struct plist *)xmalloc (sizeof(struct plist));
plist_init (playlist);
queue = (struct plist *)xmalloc (sizeof(struct plist));
plist_init (queue);
/* set serial numbers for the playlist */
send_int_to_srv (CMD_GET_SERIAL);
plist_set_serial (playlist, get_data_int());
}
static void file_info_reset (struct file_info *f)
{
f->file = NULL;
f->tags = NULL;
f->title = NULL;
f->bitrate = -1;
f->rate = -1;
f->curr_time = -1;
f->total_time = -1;
f->channels = 1;
f->state = STATE_STOP;
}
static void file_info_cleanup (struct file_info *f)
{
if (f->tags)
tags_free (f->tags);
if (f->file)
free (f->file);
if (f->title)
free (f->title);
f->file = NULL;
f->tags = NULL;
f->title = NULL;
}
/* Initialise the block marker information. */
static void file_info_block_init (struct file_info *f)
{
f->block_file = NULL;
}
/* Reset the block start and end markers. */
static void file_info_block_reset (struct file_info *f)
{
if (f->block_file)
free (f->block_file);
f->block_file = NULL;
}
/* Enter the current time into a block start or end marker. */
static void file_info_block_mark (int *marker)
{
if (curr_file.state == STATE_STOP) {
error ("Cannot make block marks while stopped.");
} else if (file_type (curr_file.file) == F_URL) {
error ("Cannot make block marks in URLs.");
} else if (file_type (curr_file.file) != F_SOUND) {
error ("Cannot make block marks in non-audio files.");
} else if (!curr_file.block_file) {
error ("Cannot make block marks in files of unknown duration.");
} else {
*marker = curr_file.curr_time;
iface_set_block (curr_file.block_start, curr_file.block_end);
}
}
/* Get a boolean option from the server (like Shuffle) and set it. */
static void sync_bool_option (const char *name)
{
bool value;
send_int_to_srv (CMD_GET_OPTION);
send_str_to_srv (name);
value = get_data_bool ();
options_set_bool (name, value);
iface_set_option_state (name, value);
}
/* Get the server options and set our options like them. */
static void get_server_options ()
{
sync_bool_option ("Shuffle");
sync_bool_option ("Repeat");
sync_bool_option ("AutoNext");
}
static int get_server_plist_serial ()
{
send_int_to_srv (CMD_PLIST_GET_SERIAL);
return get_data_int ();
}
static int get_mixer_value ()
{
send_int_to_srv (CMD_GET_MIXER);
return get_data_int ();
}
static int get_state ()
{
send_int_to_srv (CMD_GET_STATE);
return get_data_int ();
}
static int get_channels ()
{
send_int_to_srv (CMD_GET_CHANNELS);
return get_data_int ();
}
static int get_rate ()
{
send_int_to_srv (CMD_GET_RATE);
return get_data_int ();
}
static int get_bitrate ()
{
send_int_to_srv (CMD_GET_BITRATE);
return get_data_int ();
}
static int get_avg_bitrate ()
{
send_int_to_srv (CMD_GET_AVG_BITRATE);
return get_data_int ();
}
static int get_curr_time ()
{
send_int_to_srv (CMD_GET_CTIME);
return get_data_int ();
}
static char *get_curr_file ()
{
send_int_to_srv (CMD_GET_SNAME);
return get_data_str ();
}
static void update_mixer_value ()
{
int val;
val = get_mixer_value ();
iface_set_mixer_value (MAX(val, 0));
}
static void update_mixer_name ()
{
char *name;
send_int_to_srv (CMD_GET_MIXER_CHANNEL_NAME);
name = get_data_str();
debug ("Mixer name: %s", name);
iface_set_mixer_name (name);
free (name);
update_mixer_value ();
}
/* Make new cwd path from CWD and this path. */
static void set_cwd (const char *path)
{
if (path[0] == '/')
strcpy (cwd, "/"); /* for absolute path */
else if (!cwd[0]) {
if (!getcwd(cwd, sizeof(cwd)))
fatal ("Can't get CWD: %s", xstrerror (errno));
}
resolve_path (cwd, sizeof(cwd), path);
}
/* Try to find the directory we can start and set cwd to it. */
static void set_start_dir ()
{
if (!getcwd(cwd, sizeof (cwd))) {
if (errno == ERANGE)
fatal ("CWD is larger than PATH_MAX!");
strncpy (cwd, get_home (), sizeof (cwd));
if (cwd[sizeof (cwd) - 1])
fatal ("Home directory path is longer than PATH_MAX!");
}
}
/* Set cwd to last directory written to a file, return 1 on success. */
static int read_last_dir ()
{
FILE *dir_file;
int res = 1;
int read;
if (!(dir_file = fopen(create_file_name("last_directory"), "r")))
return 0;
if ((read = fread(cwd, sizeof(char), sizeof(cwd)-1, dir_file)) == 0)
res = 0;
else
cwd[read] = 0;
fclose (dir_file);
return res;
}
/* Check if dir2 is in dir1. */
static int is_subdir (const char *dir1, const char *dir2)
{
return !strncmp(dir1, dir2, strlen(dir1)) ? 1 : 0;
}
static int sort_strcmp_func (const void *a, const void *b)
{
return strcoll (*(char **)a, *(char **)b);
}
static int sort_dirs_func (const void *a, const void *b)
{
char *sa = *(char **)a;
char *sb = *(char **)b;
/* '../' is always first */
if (!strcmp(sa, "../"))
return -1;
if (!strcmp(sb, "../"))
return 1;
return strcoll (sa, sb);
}
static int get_tags_setting ()
{
int needed_tags = 0;
if (options_get_bool("ReadTags"))
needed_tags |= TAGS_COMMENTS;
if (!strcasecmp(options_get_symb("ShowTime"), "yes"))
needed_tags |= TAGS_TIME;
return needed_tags;
}
/* For each file in the playlist, send a request for all the given tags if
* the file is missing any of those tags. Return the number of requests. */
static int ask_for_tags (const struct plist *plist, const int tags_sel)
{
int i;
int req = 0;
assert (plist != NULL);
if (tags_sel != 0) {
for (i = 0; i < plist->num; i++) {
if (!plist_deleted(plist, i) &&
(!plist->items[i].tags ||
~plist->items[i].tags->filled & tags_sel)) {
char *file;
file = plist_get_file (plist, i);
req += send_tags_request (file, tags_sel);
free (file);
}
}
}
return req;
}
static void interface_message (const char *format, ...)
{
va_list va;
char *msg;
va_start (va, format);
msg = format_msg_va (format, va);
va_end (va);
iface_message (msg);
free (msg);
}
/* Update tags (and titles) for the given item on the playlist with new tags. */
static void update_item_tags (struct plist *plist, const int num,
const struct file_tags *tags)
{
struct file_tags *old_tags = plist_get_tags (plist, num);
plist_set_tags (plist, num, tags);
/* Get the time from the old tags if it's not present in the new tags.
* FIXME: There is risk, that the file was modified and the time
* from the old tags is not valid. */
if (!(tags->filled & TAGS_TIME) && old_tags && old_tags->time != -1)
plist_set_item_time (plist, num, old_tags->time);
if (plist->items[num].title_tags) {
free (plist->items[num].title_tags);
plist->items[num].title_tags = NULL;
}
make_tags_title (plist, num);
if (options_get_bool ("ReadTags") && !plist->items[num].title_tags) {
if (!plist->items[num].title_file)
make_file_title (plist, num,
options_get_bool ("HideFileExtension"));
}
if (old_tags)
tags_free (old_tags);
}
/* Truncate string at screen-upsetting whitespace. */
static void sanitise_string (char *str)
{
if (!str)
return;
while (*str) {
if (*str != ' ' && isspace (*str)) {
*str = 0x00;
break;
}
str++;
}
}
/* Handle EV_FILE_TAGS. */
static void ev_file_tags (const struct tag_ev_response *data)
{
int n;
assert (data != NULL);
assert (data->file != NULL);
assert (data->tags != NULL);
debug ("Received tags for %s", data->file);
sanitise_string (data->tags->title);
sanitise_string (data->tags->artist);
sanitise_string (data->tags->album);
if ((n = plist_find_fname(dir_plist, data->file)) != -1) {
update_item_tags (dir_plist, n, data->tags);
iface_update_item (IFACE_MENU_DIR, dir_plist, n);
}
if ((n = plist_find_fname(playlist, data->file)) != -1) {
update_item_tags (playlist, n, data->tags);
iface_update_item (IFACE_MENU_PLIST, playlist, n);
}
if (curr_file.file && !strcmp(data->file, curr_file.file)) {
debug ("Tags apply to the currently played file.");
if (data->tags->time != -1) {
curr_file.total_time = data->tags->time;
iface_set_total_time (curr_file.total_time);
if (file_type (curr_file.file) == F_SOUND) {
if (!curr_file.block_file) {
curr_file.block_file = xstrdup(curr_file.file);
curr_file.block_start = 0;
curr_file.block_end = curr_file.total_time;
}
iface_set_block (curr_file.block_start, curr_file.block_end);
}
}
else
debug ("No time information");
if (data->tags->title) {
if (curr_file.title)
free (curr_file.title);
curr_file.title = build_title (data->tags);
iface_set_played_file_title (curr_file.title);
}
if (curr_file.tags)
tags_free (curr_file.tags);
curr_file.tags = tags_dup (data->tags);
}
}
/* Update the current time. */
static void update_ctime ()
{
curr_file.curr_time = get_curr_time ();
if (silent_seek_pos == -1)
iface_set_curr_time (curr_file.curr_time);
}
/* Use new tags for current file title (for Internet streams). */
static void update_curr_tags ()
{
if (curr_file.file && is_url(curr_file.file)) {
if (curr_file.tags)
tags_free (curr_file.tags);
send_int_to_srv (CMD_GET_TAGS);
curr_file.tags = get_data_tags ();
if (curr_file.tags->title) {
if (curr_file.title)
free (curr_file.title);
curr_file.title = build_title (curr_file.tags);
iface_set_played_file_title (curr_file.title);
}
}
}
/* Make sure that the currently played file is visible if it is in one of our
* menus. */
static void follow_curr_file ()
{
if (curr_file.file && file_type(curr_file.file) == F_SOUND
&& last_menu_move_time <= time(NULL) - 2) {
int server_plist_serial = get_server_plist_serial();
if (server_plist_serial == plist_get_serial(playlist))
iface_make_visible (IFACE_MENU_PLIST, curr_file.file);
else if (server_plist_serial == plist_get_serial(dir_plist))
iface_make_visible (IFACE_MENU_DIR, curr_file.file);
else
logit ("Not my playlist.");
}
}
static void update_curr_file ()
{
char *file;
file = get_curr_file ();
if (!file[0] || curr_file.state == STATE_STOP) {
/* Nothing is played/paused. */
file_info_cleanup (&curr_file);
file_info_reset (&curr_file);
iface_set_played_file (NULL);
iface_load_lyrics (NULL);
free (file);
}
else if (file[0] &&
(!curr_file.file || strcmp(file, curr_file.file))) {
/* played file has changed */
file_info_cleanup (&curr_file);
if (curr_file.block_file && strcmp(file, curr_file.block_file))
file_info_block_reset (&curr_file);
/* The total time could not get reset. */
iface_set_total_time (-1);
iface_set_played_file (file);
send_tags_request (file, TAGS_COMMENTS | TAGS_TIME);
curr_file.file = file;
/* make a title that will be used until we get tags */
if (file_type(file) == F_URL || !strchr(file, '/')) {
curr_file.title = xstrdup (file);
update_curr_tags ();
}
else
{
if (options_get_bool ("FileNamesIconv"))
{
curr_file.title = files_iconv_str (
strrchr(file, '/') + 1);
}
else
{
curr_file.title = xstrdup (
strrchr(file, '/') + 1);
}
}
iface_set_played_file (file);
iface_set_played_file_title (curr_file.title);
/* Try to load the lyrics of the new file. */
iface_load_lyrics (file);
/* Silent seeking makes no sense if the playing file has changed. */
silent_seek_pos = -1;
iface_set_curr_time (curr_file.curr_time);
if (options_get_bool("FollowPlayedFile"))
follow_curr_file ();
}
else
free (file);
}
static void update_rate ()
{
curr_file.rate = get_rate ();
iface_set_rate (curr_file.rate);
}
static void update_channels ()
{
curr_file.channels = get_channels () == 2 ? 2 : 1;
iface_set_channels (curr_file.channels);
}
static void update_bitrate ()
{
curr_file.bitrate = get_bitrate ();
iface_set_bitrate (curr_file.bitrate);
}
/* Get and show the server state. */
static void update_state ()
{
int old_state = curr_file.state;
/* play | stop | pause */
curr_file.state = get_state ();
iface_set_state (curr_file.state);
/* Silent seeking makes no sense if the state has changed. */
if (old_state != curr_file.state)
silent_seek_pos = -1;
update_curr_file ();
update_channels ();
update_bitrate ();
update_rate ();
update_ctime ();
}
/* Handle EV_PLIST_ADD. */
static void event_plist_add (const struct plist_item *item)
{
if (plist_find_fname(playlist, item->file) == -1) {
int item_num = plist_add_from_item (playlist, item);
int needed_tags = 0;
int i;
if (options_get_bool("ReadTags")
&& (!item->tags || !item->tags->title))
needed_tags |= TAGS_COMMENTS;
if (!strcasecmp(options_get_symb("ShowTime"), "yes")
&& (!item->tags || item->tags->time == -1))
needed_tags |= TAGS_TIME;
if (needed_tags)
send_tags_request (item->file, needed_tags);
if (options_get_bool ("ReadTags"))
make_tags_title (playlist, item_num);
else
make_file_title (playlist, item_num,
options_get_bool ("HideFileExtension"));
/* Just calling iface_update_queue_positions (queue, playlist,
* NULL, NULL) is too slow in cases when we receive a large
* number of items from server (e.g., loading playlist w/
* SyncPlaylist on). Since we know the filename in question,
* we try to find it in queue and eventually update the value.
*/
if ((i = plist_find_fname(queue, item->file)) != -1) {
playlist->items[item_num].queue_pos
= plist_get_position (queue, i);
}
iface_add_to_plist (playlist, item_num);
if (waiting_for_plist_load) {
if (iface_in_dir_menu())
iface_switch_to_plist ();
waiting_for_plist_load = 0;
}
}
}
/* Handle EV_QUEUE_ADD. */
static void event_queue_add (const struct plist_item *item)
{
if (plist_find_fname(queue, item->file) == -1) {
plist_add_from_item (queue, item);
iface_set_files_in_queue (plist_count(queue));
iface_update_queue_position_last (queue, playlist, dir_plist);
logit ("Adding %s to queue", item->file);
}
else
logit ("Adding file already present in queue");
}
/* Get error message from the server and show it. */
static void update_error (char *err)
{
error ("%s", err);
}
/* Send the playlist to the server to be forwarded to another client. */
static void forward_playlist ()
{
int i;
debug ("Forwarding the playlist...");
send_int_to_srv (CMD_SEND_PLIST);
send_int_to_srv (plist_get_serial(playlist));
for (i = 0; i < playlist->num; i++)
if (!plist_deleted(playlist, i))
send_item_to_srv (&playlist->items[i]);
send_item_to_srv (NULL);
}
static int recv_server_plist (struct plist *plist)
{
int end_of_list = 0;
struct plist_item *item;
logit ("Asking server for the playlist from other client.");
send_int_to_srv (CMD_GET_PLIST);
logit ("Waiting for response");
wait_for_data ();
if (!get_int_from_srv()) {
debug ("There is no playlist");
return 0; /* there are no other clients with a playlist */
}
logit ("There is a playlist, getting...");
wait_for_data ();
logit ("Transfer...");
plist_set_serial (plist, get_int_from_srv());
do {
item = recv_item_from_srv ();
if (item->file[0])
plist_add_from_item (plist, item);
else
end_of_list = 1;
plist_free_item_fields (item);
free (item);
} while (!end_of_list);
return 1;
}
static void recv_server_queue (struct plist *queue)
{
int end_of_list = 0;
struct plist_item *item;
logit ("Asking server for the queue.");
send_int_to_srv (CMD_GET_QUEUE);
logit ("Waiting for response");
wait_for_data (); /* There must always be (possibly empty) queue. */
do {
item = recv_item_from_srv ();
if (item->file[0])
plist_add_from_item (queue, item);
else