-
Notifications
You must be signed in to change notification settings - Fork 38
/
interface_elements.c
4500 lines (3649 loc) · 104 KB
/
interface_elements.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.
*
* Other authors:
* - Kamil Tarkowski <[email protected]> - sec_to_min_plist()
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <wctype.h>
#include <wchar.h>
#include "common.h"
#include "menu.h"
#include "themes.h"
#include "lists.h"
#include "options.h"
#include "interface_elements.h"
#include "log.h"
#include "files.h"
#include "decoder.h"
#include "keys.h"
#include "playlist.h"
#include "protocol.h"
#include "interface.h"
#include "utf8.h"
#include "rcc.h"
#include "lyrics.h"
#ifndef PACKAGE_REVISION
#define STARTUP_MESSAGE "Welcome to " PACKAGE_NAME \
" (version " PACKAGE_VERSION ")!"
#else
#define STARTUP_MESSAGE "Welcome to " PACKAGE_NAME \
" (version " PACKAGE_VERSION \
", revision " PACKAGE_REVISION ")!"
#endif
#define HISTORY_SIZE 50
/* TODO: removing/adding a char to the entry may increase width of the text
* by more than one column. */
/* Type of the side menu. */
enum side_menu_type
{
MENU_DIR, /* list of files in a directory */
MENU_PLAYLIST, /* a playlist of files */
MENU_THEMES, /* list of available themes */
MENU_TREE /* tree of directories */
};
struct side_menu
{
enum side_menu_type type;
int visible; /* is it visible (are the other fields initialized) ? */
WINDOW *win; /* window for the menu */
char *title; /* title of the window */
/* Position and size of the menu in the window. */
int posx;
int posy;
int width;
int height;
int total_time; /* total time of the files on the playlist */
int total_time_for_all; /* is the total file counted for all files? */
union
{
struct {
struct menu *main; /* visible menu */
struct menu *copy; /* copy of the menu when we display
matching items while searching */
} list;
/* struct menu_tree *tree;*/
} menu;
};
/* State of the side menu that can be read/restored. It remembers the state
* (position of the view, which file is selected, etc) of the menu. */
struct side_menu_state
{
struct menu_state menu_state;
};
/* When used instead of the size parameter it means: fill to the end of the
* window. */
#define LAYOUT_SIZE_FILL (-1)
struct window_params
{
int x, y;
int width, height;
};
struct main_win_layout
{
struct window_params menus[3];
};
static struct main_win
{
WINDOW *win;
char *curr_file; /* currently played file. */
int in_help; /* are we displaying help screen? */
int too_small; /* is the terminal window too small to display mocp? */
int help_screen_top; /* first visible line of the help screen. */
int in_lyrics; /* are we displaying lyrics screen? */
int lyrics_screen_top; /* first visible line of the lyrics screen. */
struct side_menu menus[3];
lists_t_strs *layout_fmt;
int selected_menu; /* which menu is currently selected by the user */
} main_win;
/* Bar for displaying mixer state or progress. */
struct bar
{
int width; /* width in chars */
float filled; /* how much is it filled in percent */
char *orig_title; /* optional title */
char title[512]; /* title with the percent value */
int show_val; /* show the title and the value? */
int show_pct; /* show percentage in the title value? */
int fill_color; /* color (ncurses attributes) of the filled part */
int empty_color; /* color of the empty part */
};
/* History for entries' values. */
struct entry_history
{
char *items[HISTORY_SIZE];
int num; /* number of items */
};
/* An input area where a user can type text to enter a file name etc. */
struct entry
{
enum entry_type type;
int width; /* width of the entry part for typing */
/* The text the user types: */
wchar_t text_ucs[512]; /* unicode */
wchar_t saved_ucs[512]; /* unicode saved during history scrolling */
char *title; /* displayed title */
char *file; /* optional: file associated with the entry */
int cur_pos; /* cursor position */
int display_from; /* displaying from this char */
struct entry_history *history; /* history to use with this entry or
NULL is history is not used */
int history_pos; /* current position in the history */
};
/* Type of message. */
enum message_type
{
NORMAL_MSG,
ERROR_MSG,
QUERY_MSG
};
/* Save a new message for display. */
struct queued_message
{
struct queued_message *next;
/* What type is this message? */
enum message_type type;
/* Message to be displayed instead of the file's title. */
char *msg;
/* Prompt to use for user query menu. */
char *prompt;
/* How many seconds does the message linger? */
time_t timeout;
/* The callback function and opaque data for user replies. */
t_user_reply_callback *callback;
void *data;
};
static struct info_win
{
WINDOW *win;
struct queued_message *current_message; /* Message currently being displayed */
struct queued_message *queued_message_head; /* FIFO queue on which pending */
struct queued_message *queued_message_tail; /* messages get saved */
int queued_message_total; /* Total messages on queue */
int queued_message_errors; /* Error messages on queue */
int too_small; /* is the current window too small to display this widget? */
struct entry entry;
int in_entry; /* are we using the entry (is the above
structure initialized)? */
struct entry_history urls_history;
struct entry_history dirs_history;
struct entry_history user_history;
/* true/false options values */
bool state_stereo;
bool state_shuffle;
bool state_repeat;
bool state_next;
bool state_net;
int bitrate; /* in kbps */
int rate; /* in kHz */
int files_in_queue;
/* time in seconds */
int curr_time;
int total_time;
int block_start;
int block_end;
int plist_time; /* total time of files displayed in the menu */
int plist_time_for_all; /* is the above time for all files? */
char *title; /* title of the played song. */
char status_msg[26]; /* status message */
int state_play; /* STATE_(PLAY | STOP | PAUSE) */
/* Saved user reply callback data. */
t_user_reply_callback *callback;
void *data;
struct bar mixer_bar;
struct bar time_bar;
} info_win;
/* Are we running on xterm? */
static bool has_xterm = false;
/* Are we running inside screen? */
static bool has_screen = false;
/* Was the interface initialized? */
static int iface_initialized = 0;
/* Was initscr() called? */
static int screen_initialized = 0;
/* Chars used to make lines (for borders etc.). */
static struct
{
chtype vert; /* vertical */
chtype horiz; /* horizontal */
chtype ulcorn; /* upper left corner */
chtype urcorn; /* upper right corner */
chtype llcorn; /* lower left corner */
chtype lrcorn; /* lower right corner */
chtype rtee; /* right tee */
chtype ltee; /* left tee */
} lines;
static void entry_history_init (struct entry_history *h)
{
assert (h != NULL);
h->num = 0;
}
static void entry_history_add (struct entry_history *h, const char *text)
{
assert (h != NULL);
assert (text != NULL);
if (strlen (text) != strspn (text, " ")) {
if (h->num == 0 || strcmp (text, h->items[h->num - 1])) {
if (h->num < HISTORY_SIZE)
h->items[h->num++] = xstrdup (text);
else {
free (h->items[0]);
memmove (h->items, h->items + 1,
(HISTORY_SIZE - 1) * sizeof (char *));
h->items[h->num - 1] = xstrdup (text);
}
}
}
}
static void entry_history_replace (struct entry_history *h, int num, const char *text)
{
assert (h != NULL);
assert (LIMIT(num, h->num));
assert (text != NULL);
if (strlen (text) != strspn (text, " ") &&
strcmp (h->items[num], text)) {
free (h->items[num]);
h->items[num] = xstrdup (text);
}
}
static void entry_history_clear (struct entry_history *h)
{
int i;
assert (h != NULL);
for (i = 0; i < h->num; i++)
free (h->items[i]);
h->num = 0;
}
static int entry_history_nitems (const struct entry_history *h)
{
assert (h != NULL);
return h->num;
}
static char *entry_history_get (const struct entry_history *h, const int num)
{
assert (h != NULL);
assert (LIMIT(num, h->num));
return xstrdup (h->items[num]);
}
/* Draw the entry. Use this function at the end of screen drawing
* because it sets the cursor position in the right place. */
static void entry_draw (const struct entry *e, WINDOW *w, const int posx,
const int posy)
{
char *text;
wchar_t *text_ucs;
int len;
assert (e != NULL);
assert (w != NULL);
assert (posx >= 0);
assert (posy >= 0);
wmove (w, posy, posx);
wattrset (w, get_color(CLR_ENTRY_TITLE));
xwprintw (w, "%s", e->title);
wattrset (w, get_color(CLR_ENTRY));
len = wcslen(e->text_ucs) - e->display_from;
text_ucs = (wchar_t *)xmalloc(sizeof(wchar_t) * (len + 1));
memcpy (text_ucs, e->text_ucs + e->display_from,
sizeof(wchar_t) * (len + 1));
if (len > e->width)
text_ucs[e->width] = L'\0';
len = wcstombs (NULL, text_ucs, -1) + 1;
assert (len >= 1);
text = (char *)xmalloc (len);
wcstombs (text, text_ucs, len);
xwprintw (w, " %-*s", e->width, text);
/* Move the cursor */
wmove (w, posy, e->cur_pos - e->display_from + strwidth(e->title)
+ posx + 1);
free (text);
free (text_ucs);
}
static void entry_init (struct entry *e, const enum entry_type type,
const int width, struct entry_history *history, const char *prompt)
{
const char *title;
assert (e != NULL);
switch (type) {
case ENTRY_SEARCH:
title = "SEARCH";
break;
case ENTRY_PLIST_SAVE:
title = "SAVE PLAYLIST";
break;
case ENTRY_GO_DIR:
title = "GO";
break;
case ENTRY_GO_URL:
title = "URL";
break;
case ENTRY_ADD_URL:
title = "ADD URL";
break;
case ENTRY_PLIST_OVERWRITE:
title = "File exists, overwrite?";
break;
case ENTRY_USER_QUERY:
title = prompt;
break;
default:
abort ();
}
e->type = type;
e->text_ucs[0] = L'\0';
e->saved_ucs[0] = L'\0';
e->file = NULL;
e->title = xmalloc (strlen (title) + 2);
strcpy (e->title, title);
if (e->title[strlen (e->title) - 1] != ':' &&
e->title[strlen (e->title) - 1] != '?')
strcat (e->title, ":");
e->width = width - strwidth(title);
e->cur_pos = 0;
e->display_from = 0;
e->history = history;
if (history)
e->history_pos = history->num;
}
static enum entry_type entry_get_type (const struct entry *e)
{
assert (e != NULL);
return e->type;
}
/* Set the entry text as UCS. Move the cursor to the end. */
static void entry_set_text_ucs (struct entry *e, const wchar_t *text)
{
int width, len;
assert (e != NULL);
len = MIN (wcslen (text) + 1, ARRAY_SIZE (e->text_ucs));
wmemcpy (e->text_ucs, text, len);
e->text_ucs[ARRAY_SIZE (e->text_ucs) - 1] = L'\0';
width = wcswidth (e->text_ucs, WIDTH_MAX);
e->cur_pos = wcslen (e->text_ucs);
e->display_from = 0;
if (e->cur_pos > e->width)
e->display_from = width - e->width;
}
/* Set the entry text. */
static void entry_set_text (struct entry *e, const char *text)
{
wchar_t text_ucs[ARRAY_SIZE (e->text_ucs)];
assert (e != NULL);
mbstowcs (text_ucs, text, ARRAY_SIZE (e->text_ucs));
e->text_ucs[ARRAY_SIZE (e->text_ucs) - 1] = L'\0';
entry_set_text_ucs (e, text_ucs);
}
/* Add a char to the entry where the cursor is placed. */
static void entry_add_char (struct entry *e, const wchar_t c)
{
size_t len;
assert (e != NULL);
len = wcslen (e->text_ucs);
if (len >= ARRAY_SIZE(e->text_ucs) - sizeof(wchar_t))
return;
memmove (e->text_ucs + e->cur_pos + 1,
e->text_ucs + e->cur_pos,
(len - e->cur_pos + 1) * sizeof(e->text_ucs[0]));
e->text_ucs[e->cur_pos] = c;
e->cur_pos++;
if (e->cur_pos - e->display_from > e->width)
e->display_from++;
}
/* Delete 'count' chars before the cursor. */
static void entry_del_chars (struct entry *e, int count)
{
assert (e != NULL);
assert (e->cur_pos > 0);
int width = wcslen (e->text_ucs);
if (e->cur_pos < count)
count = e->cur_pos;
memmove (e->text_ucs + e->cur_pos - count,
e->text_ucs + e->cur_pos,
(width - e->cur_pos) * sizeof (e->text_ucs[0]));
width -= count;
e->text_ucs[width] = L'\0';
e->cur_pos -= count;
if (e->cur_pos < e->display_from)
e->display_from = e->cur_pos;
/* Can we show more after deleting the chars? */
if (e->display_from > 0 && width - e->display_from < e->width)
e->display_from = width - e->width;
if (e->display_from < 0)
e->display_from = 0;
}
/* Delete the char before the cursor. */
static void entry_back_space (struct entry *e)
{
assert (e != NULL);
if (e->cur_pos > 0)
entry_del_chars (e, 1);
}
/* Delete the char under the cursor. */
static void entry_del_char (struct entry *e)
{
int len;
assert (e != NULL);
len = wcslen (e->text_ucs);
if (e->cur_pos < len) {
e->cur_pos += 1;
entry_del_chars (e, 1);
}
}
/* Delete the chars from cursor to start of line. */
static void entry_del_to_start (struct entry *e)
{
assert (e != NULL);
if (e->cur_pos > 0)
entry_del_chars (e, e->cur_pos);
}
/* Delete the chars from cursor to end of line. */
static void entry_del_to_end (struct entry *e)
{
int len;
assert (e != NULL);
len = wcslen (e->text_ucs);
if (e->cur_pos < len) {
int count;
count = len - e->cur_pos;
e->cur_pos = len;
entry_del_chars (e, count);
}
}
/* Move the cursor one char left. */
static void entry_curs_left (struct entry *e)
{
assert (e != NULL);
if (e->cur_pos > 0) {
e->cur_pos--;
if (e->cur_pos < e->display_from)
e->display_from--;
}
}
/* Move the cursor one char right. */
static void entry_curs_right (struct entry *e)
{
int width;
assert (e != NULL);
width = wcslen (e->text_ucs);
if (e->cur_pos < width) {
e->cur_pos++;
if (e->cur_pos > e->width + e->display_from)
e->display_from++;
}
}
/* Move the cursor to the end of the entry text. */
static void entry_end (struct entry *e)
{
int width;
assert (e != NULL);
width = wcslen (e->text_ucs);
e->cur_pos = width;
if (width > e->width)
e->display_from = width - e->width;
else
e->display_from = 0;
}
/* Move the cursor to the beginning of the entry field. */
static void entry_home (struct entry *e)
{
assert (e != NULL);
e->display_from = 0;
e->cur_pos = 0;
}
static void entry_resize (struct entry *e, const int width)
{
assert (e != NULL);
assert (width > 0);
e->width = width - strlen (e->title);
entry_end (e);
}
static char *entry_get_text (const struct entry *e)
{
char *text;
int len;
assert (e != NULL);
len = wcstombs (NULL, e->text_ucs, -1) + 1;
assert (len >= 1);
text = (char *) xmalloc (sizeof (char) * len);
wcstombs (text, e->text_ucs, len);
return text;
}
/* Copy the previous history item to the entry if available, move the entry
* history position down. */
static void entry_set_history_up (struct entry *e)
{
assert (e != NULL);
assert (e->history != NULL);
if (e->history_pos > 0) {
char *t;
if (e->history_pos == entry_history_nitems (e->history))
wmemcpy (e->saved_ucs, e->text_ucs, wcslen (e->text_ucs) + 1);
else {
t = entry_get_text (e);
entry_history_replace (e->history, e->history_pos, t);
free (t);
}
e->history_pos--;
t = entry_history_get (e->history, e->history_pos);
entry_set_text (e, t);
free (t);
}
}
/* Copy the next history item to the entry if available, move the entry history
* position down. */
static void entry_set_history_down (struct entry *e)
{
assert (e != NULL);
assert (e->history != NULL);
if (e->history_pos < entry_history_nitems (e->history)) {
char *t;
t = entry_get_text (e);
entry_history_replace (e->history, e->history_pos, t);
free (t);
e->history_pos++;
if (e->history_pos == entry_history_nitems (e->history))
entry_set_text_ucs (e, e->saved_ucs);
else {
t = entry_history_get (e->history, e->history_pos);
entry_set_text (e, t);
free (t);
}
}
}
static void entry_set_file (struct entry *e, const char *file)
{
assert (e != NULL);
assert (file != NULL);
if (e->file)
free (e->file);
e->file = xstrdup (file);
}
static char *entry_get_file (const struct entry *e)
{
return xstrdup (e->file);
}
static void entry_destroy (struct entry *e)
{
assert (e != NULL);
if (e->file)
free (e->file);
if (e->title)
free (e->title);
}
static void entry_add_text_to_history (struct entry *e)
{
char *text;
assert (e != NULL);
assert (e->history);
text = entry_get_text (e);
entry_history_add (e->history, text);
free (text);
}
/* Return the list menu height inside the side menu. */
static int side_menu_get_menu_height (const struct side_menu *m)
{
if (m->posy + m->height == LINES - 4)
return m->height - 1;
return m->height - 2;
}
static void side_menu_init_menu (struct side_menu *m)
{
assert (m != NULL);
m->menu.list.main = menu_new (m->win, m->posx + 1, m->posy + 1,
m->width - 2, side_menu_get_menu_height (m));
}
static void side_menu_init (struct side_menu *m, const enum side_menu_type type,
WINDOW *parent_win, const struct window_params *wp)
{
assert (m != NULL);
assert (parent_win != NULL);
assert (wp != NULL);
assert (wp->width >= 8);
assert (wp->height >= 3);
m->type = type;
m->win = parent_win;
m->posx = wp->x;
m->posy = wp->y;
m->height = wp->height;
m->width = wp->width;
m->title = NULL;
m->total_time = 0;
m->total_time_for_all = 0;
if (type == MENU_DIR || type == MENU_PLAYLIST) {
side_menu_init_menu (m);
m->menu.list.copy = NULL;
menu_set_items_numbering (m->menu.list.main,
type == MENU_PLAYLIST
&& options_get_bool("PlaylistNumbering"));
menu_set_show_format (m->menu.list.main,
options_get_bool("ShowFormat"));
menu_set_show_time (m->menu.list.main,
strcasecmp(options_get_symb("ShowTime"), "no"));
menu_set_info_attr_normal (m->menu.list.main,
get_color(CLR_MENU_ITEM_INFO));
menu_set_info_attr_sel (m->menu.list.main,
get_color(CLR_MENU_ITEM_INFO_SELECTED));
menu_set_info_attr_marked (m->menu.list.main,
get_color(CLR_MENU_ITEM_INFO_MARKED));
menu_set_info_attr_sel_marked (m->menu.list.main,
get_color(CLR_MENU_ITEM_INFO_MARKED_SELECTED));
}
else if (type == MENU_THEMES) {
side_menu_init_menu (m);
m->menu.list.copy = NULL;
}
else
abort ();
m->visible = 1;
}
static void side_menu_destroy (struct side_menu *m)
{
assert (m != NULL);
if (m->visible) {
if (m->type == MENU_DIR || m->type == MENU_PLAYLIST
|| m->type == MENU_THEMES) {
menu_free (m->menu.list.main);
if (m->menu.list.copy)
menu_free (m->menu.list.copy);
}
else
abort ();
if (m->title)
free (m->title);
m->visible = 0;
}
}
static void side_menu_set_title (struct side_menu *m, const char *title)
{
assert (m != NULL);
assert (title != NULL);
if (m->title)
free (m->title);
m->title = xstrdup (title);
}
/* Parse one layout coordinate from "0,2,54%,1" and put it in val.
* Max is the maximum value of the field. It's also used when processing
* percent values.
* Return false on error. */
static bool parse_layout_coordinate (const char *fmt, int *val, const int max)
{
long v;
const char *e = fmt;
if (!strcasecmp (fmt, "FILL")) {
*val = LAYOUT_SIZE_FILL;
return true;
}
v = strtol (fmt, (char **)&e, 10);
if (e == fmt)
return false;
if (*e == '%')
v = lroundf (max * v / 100.0 - 0.1);
*val = v;
if (!RANGE(0, *val, max)) {
logit ("Coordinate out of range - %d is not in (0, %d)", *val, max);
return false;
}
return true;
}
/* Parse the layout string. Return false on error. */
static bool parse_layout (struct main_win_layout *l, lists_t_strs *fmt)
{
int ix;
bool result = false;
lists_t_strs *format;
assert (l != NULL);
assert (fmt != NULL);
/* default values */
l->menus[0].x = 0;
l->menus[0].y = 0;
l->menus[0].width = COLS;
l->menus[0].height = LINES - 4;
l->menus[1] = l->menus[0];
l->menus[2] = l->menus[0];
format = lists_strs_new (6);
for (ix = 0; ix < lists_strs_size (fmt); ix += 1) {
const char *menu, *name;
struct window_params p;
lists_strs_clear (format);
menu = lists_strs_at (fmt, ix);
if (lists_strs_split (format, menu, "(,)") != 5)
goto err;
name = lists_strs_at (format, 0);
if (!parse_layout_coordinate (lists_strs_at (format, 1), &p.x, COLS)) {
logit ("Coordinate parse error when parsing X");
goto err;
}
if (!parse_layout_coordinate (lists_strs_at (format, 2), &p.y, LINES - 4)) {
logit ("Coordinate parse error when parsing Y");
goto err;
}
if (!parse_layout_coordinate (lists_strs_at (format, 3), &p.width, COLS)) {
logit ("Coordinate parse error when parsing width");
goto err;
}
if (!parse_layout_coordinate (lists_strs_at (format, 4), &p.height, LINES - 4)) {
logit ("Coordinate parse error when parsing height");
goto err;
}
if (p.width == LAYOUT_SIZE_FILL)
p.width = COLS - p.x;
if (p.height == LAYOUT_SIZE_FILL)
p.height = LINES - 4 - p.y;
if (p.width < 15) {
logit ("Width is less than 15");
goto err;
}
if (p.height < 2) {
logit ("Height is less than 2");
goto err;
}
if (p.x + p.width > COLS) {
logit ("X + width is more than COLS (%d)", COLS);
goto err;
}
if (p.y + p.height > LINES - 4) {
logit ("Y + height is more than LINES - 4 (%d)", LINES - 4);
goto err;
}
if (!strcmp(name, "directory"))
l->menus[MENU_DIR] = p;
else if (!strcmp(name, "playlist"))
l->menus[MENU_PLAYLIST] = p;
else {
logit ("Bad subwindow name '%s'", name);
goto err;
}
}
result = true;
err:
lists_strs_free (format);
return result;
}
static void main_win_init (struct main_win *w, lists_t_strs *layout_fmt)
{
struct main_win_layout l;
bool rc ASSERT_ONLY;
assert (w != NULL);
w->win = newwin (LINES - 4, COLS, 0, 0);
wbkgd (w->win, get_color(CLR_BACKGROUND));
nodelay (w->win, TRUE);
keypad (w->win, TRUE);
w->curr_file = NULL;
w->in_help = 0;
w->in_lyrics = 0;
w->too_small = 0;
w->help_screen_top = 0;
w->lyrics_screen_top = 0;
w->layout_fmt = layout_fmt;
rc = parse_layout (&l, layout_fmt);
assert (rc);
side_menu_init (&w->menus[0], MENU_DIR, w->win, &l.menus[0]);
side_menu_init (&w->menus[1], MENU_PLAYLIST, w->win, &l.menus[1]);
side_menu_set_title (&w->menus[1], "Playlist");
w->menus[2].visible = 0;
w->selected_menu = 0;
}
static void main_win_destroy (struct main_win *w)
{
assert (w != NULL);
side_menu_destroy (&w->menus[0]);
side_menu_destroy (&w->menus[1]);
side_menu_destroy (&w->menus[2]);
if (w->win)
delwin (w->win);
if (w->curr_file)
free (w->curr_file);
}
/* Make a title suitable to display in a menu from the title of a playlist item.
* Returned memory is malloc()ed.
* made_from tags - was the playlist title made from tags?