1 /*
2  * Copyright 2008-2013 Various Authors
3  * Copyright 2004-2006 Timo Hirvonen
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef CMUS_LIB_H
20 #define CMUS_LIB_H
21 
22 #include "editable.h"
23 #include "search.h"
24 #include "track_info.h"
25 #include "expr.h"
26 #include "rbtree.h"
27 
28 struct tree_track {
29 	struct shuffle_track shuffle_track;
30 
31 	/* position in track search tree */
32 	struct rb_node tree_node;
33 
34 	struct album *album;
35 };
36 
tree_track_info(const struct tree_track * track)37 static inline struct track_info *tree_track_info(const struct tree_track *track)
38 {
39 	return ((struct simple_track *)track)->info;
40 }
41 
to_tree_track(const struct rb_node * node)42 static inline struct tree_track *to_tree_track(const struct rb_node *node)
43 {
44 	return container_of(node, struct tree_track, tree_node);
45 }
46 
47 
48 struct album {
49 	/* position in album search tree */
50 	struct rb_node tree_node;
51 
52 	/* root of track tree */
53 	struct rb_root track_root;
54 
55 	struct artist *artist;
56 	char *name;
57 	char *sort_name;
58 	char *collkey_name;
59 	char *collkey_sort_name;
60 	/* max date of the tracks added to this album */
61 	int date;
62 	/* min date of the tracks added to this album */
63 	int min_date;
64 };
65 
66 struct artist {
67 	/* position in artist search tree */
68 	struct rb_node tree_node;
69 
70 	/* root of album tree */
71 	struct rb_root album_root;
72 
73 	char *name;
74 	char *sort_name;
75 	char *auto_sort_name;
76 	char *collkey_name;
77 	char *collkey_sort_name;
78 	char *collkey_auto_sort_name;
79 
80 	/* albums visible for this artist in the tree_win? */
81 	unsigned int expanded : 1;
82 	unsigned int is_compilation : 1;
83 };
84 
85 const char *artist_sort_name(const struct artist *);
86 
87 enum aaa_mode {
88 	AAA_MODE_ALL,
89 	AAA_MODE_ARTIST,
90 	AAA_MODE_ALBUM
91 };
92 
93 extern struct editable lib_editable;
94 extern struct tree_track *lib_cur_track;
95 extern struct rb_root lib_shuffle_root;
96 extern enum aaa_mode aaa_mode;
97 extern unsigned int play_sorted;
98 extern char *lib_live_filter;
99 
100 extern struct searchable *tree_searchable;
101 extern struct window *lib_tree_win;
102 extern struct window *lib_track_win;
103 extern struct window *lib_cur_win;
104 extern struct rb_root lib_artist_root;
105 
106 #define CUR_ALBUM	(lib_cur_track->album)
107 #define CUR_ARTIST	(lib_cur_track->album->artist)
108 
109 void lib_init(void);
110 void tree_init(void);
111 struct track_info *lib_goto_next(void);
112 struct track_info *lib_goto_prev(void);
113 void lib_add_track(struct track_info *track_info, void *opaque);
114 void lib_set_filter(struct expr *expr);
115 void lib_set_live_filter(const char *str);
116 void lib_set_add_filter(struct expr *expr);
117 int lib_remove(struct track_info *ti);
118 void lib_clear_store(void);
119 void lib_reshuffle(void);
120 void lib_set_view(int view);
121 int lib_for_each(int (*cb)(void *data, struct track_info *ti), void *data,
122 		void *opaque);
123 int lib_for_each_filtered(int (*cb)(void *data, struct track_info *ti),
124 		void *data, void *opaque);
125 
126 struct tree_track *lib_find_track(struct track_info *ti);
127 struct track_info *lib_set_track(struct tree_track *track);
128 void lib_store_cur_track(struct track_info *ti);
129 struct track_info *lib_get_cur_stored_track(void);
130 
131 struct tree_track *tree_get_selected(void);
132 struct track_info *tree_activate_selected(void);
133 void tree_sort_artists(void);
134 void tree_add_track(struct tree_track *track);
135 void tree_remove(struct tree_track *track);
136 void tree_remove_sel(void);
137 void tree_toggle_active_window(void);
138 void tree_toggle_expand_artist(void);
139 void tree_expand_matching(const char *text);
140 void tree_expand_all(void);
141 void tree_sel_update(int changed);
142 void tree_sel_current(int auto_expand_albums);
143 void tree_sel_first(void);
144 void tree_sel_track(struct tree_track *t, int auto_expand_albums);
145 int tree_for_each_sel(int (*cb)(void *data, struct track_info *ti), void *data, int reverse, int advance);
146 int _tree_for_each_sel(int (*cb)(void *data, struct track_info *ti), void *data, int reverse);
147 
148 struct track_info *sorted_activate_selected(void);
149 void sorted_sel_current(void);
150 
iter_to_sorted_track(const struct iter * iter)151 static inline struct tree_track *iter_to_sorted_track(const struct iter *iter)
152 {
153 	return iter->data1;
154 }
155 
iter_to_artist(const struct iter * iter)156 static inline struct artist *iter_to_artist(const struct iter *iter)
157 {
158 	return iter->data1;
159 }
160 
iter_to_album(const struct iter * iter)161 static inline struct album *iter_to_album(const struct iter *iter)
162 {
163 	return iter->data2;
164 }
165 
iter_to_tree_track(const struct iter * iter)166 static inline struct tree_track *iter_to_tree_track(const struct iter *iter)
167 {
168 	return iter->data1;
169 }
170 
to_artist(const struct rb_node * node)171 static inline struct artist *to_artist(const struct rb_node *node)
172 {
173 	return container_of(node, struct artist, tree_node);
174 }
175 
to_album(const struct rb_node * node)176 static inline struct album *to_album(const struct rb_node *node)
177 {
178 	return container_of(node, struct album, tree_node);
179 }
180 
181 #endif
182