-
Notifications
You must be signed in to change notification settings - Fork 2
/
view.c
330 lines (297 loc) · 6.4 KB
/
view.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
/*
* Copyright (c) 2011 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <stdlib.h>
#include "siren.h"
struct view_entry {
enum view_id id;
enum bind_scope bind_scope;
void (*print)(void);
void (*activate_entry)(void);
void (*copy_entry)(enum view_id);
void (*delete_all_entries)(void);
void (*delete_entry)(void);
void (*move_entry_down)(void);
void (*move_entry_up)(void);
void (*reactivate_entry)(void);
void (*search_next)(const char *);
void (*search_prev)(const char *);
void (*select_active_entry)(void);
void (*select_prev_entry)(void);
void (*select_next_entry)(void);
void (*select_first_entry)(void);
void (*select_last_entry)(void);
void (*scroll_down)(enum menu_scroll);
void (*scroll_up)(enum menu_scroll);
};
static struct view_entry view_list[] = {
{
VIEW_ID_LIBRARY,
BIND_SCOPE_LIBRARY,
library_print,
library_activate_entry,
library_copy_entry,
library_delete_all_entries,
library_delete_entry,
NULL,
NULL,
library_reactivate_entry,
library_search_next,
library_search_prev,
library_select_active_entry,
library_select_prev_entry,
library_select_next_entry,
library_select_first_entry,
library_select_last_entry,
library_scroll_down,
library_scroll_up
},
{
VIEW_ID_PLAYLIST,
BIND_SCOPE_PLAYLIST,
playlist_print,
playlist_activate_entry,
playlist_copy_entry,
NULL,
NULL,
NULL,
NULL,
playlist_reactivate_entry,
playlist_search_next,
playlist_search_prev,
playlist_select_active_entry,
playlist_select_prev_entry,
playlist_select_next_entry,
playlist_select_first_entry,
playlist_select_last_entry,
playlist_scroll_down,
playlist_scroll_up
},
{
VIEW_ID_QUEUE,
BIND_SCOPE_QUEUE,
queue_print,
queue_activate_entry,
queue_copy_entry,
queue_delete_all_entries,
queue_delete_entry,
queue_move_entry_down,
queue_move_entry_up,
NULL,
queue_search_next,
queue_search_prev,
NULL,
queue_select_prev_entry,
queue_select_next_entry,
queue_select_first_entry,
queue_select_last_entry,
queue_scroll_down,
queue_scroll_up
},
{
VIEW_ID_BROWSER,
BIND_SCOPE_BROWSER,
browser_print,
browser_activate_entry,
browser_copy_entry,
NULL,
NULL,
NULL,
NULL,
browser_reactivate_entry,
browser_search_next,
browser_search_prev,
browser_select_active_entry,
browser_select_prev_entry,
browser_select_next_entry,
browser_select_first_entry,
browser_select_last_entry,
browser_scroll_down,
browser_scroll_up
}
};
static int view_sel;
static char *view_search = NULL;
void
view_activate_entry(void)
{
view_list[view_sel].activate_entry();
}
void
view_add_dir(enum view_id view, const char *path)
{
switch (view) {
case VIEW_ID_LIBRARY:
library_add_dir(path);
break;
case VIEW_ID_QUEUE:
queue_add_dir(path);
break;
default:
msg_errx("Cannot add tracks to this view");
break;
}
}
void
view_add_track(enum view_id view, struct track *t)
{
switch (view) {
case VIEW_ID_LIBRARY:
library_add_track(t);
break;
case VIEW_ID_QUEUE:
queue_add_track(t);
break;
default:
msg_errx("Cannot add tracks to this view");
break;
}
}
void
view_copy_entry(enum view_id view)
{
view_list[view_sel].copy_entry(view);
}
void
view_delete_all_entries(void)
{
if (view_list[view_sel].delete_all_entries == NULL)
msg_errx("Cannot delete entries in this view");
else
view_list[view_sel].delete_all_entries();
}
void
view_delete_entry(void)
{
if (view_list[view_sel].delete_entry == NULL)
msg_errx("Cannot delete entries in this view");
else
view_list[view_sel].delete_entry();
}
enum view_id
view_get_id(void)
{
return view_list[view_sel].id;
}
void
view_handle_key(int key)
{
msg_clear();
if (bind_execute(view_list[view_sel].bind_scope, key) == 0)
return;
if (bind_execute(BIND_SCOPE_COMMON, key) == 0)
return;
msg_errx("Key not bound");
}
void
view_move_entry_down(void)
{
if (view_list[view_sel].move_entry_down == NULL)
msg_errx("Cannot move entries in this view");
else
view_list[view_sel].move_entry_down();
}
void
view_move_entry_up(void)
{
if (view_list[view_sel].move_entry_up == NULL)
msg_errx("Cannot move entries in this view");
else
view_list[view_sel].move_entry_up();
}
void
view_print(void)
{
view_list[view_sel].print();
}
void
view_scroll_down(enum menu_scroll scroll)
{
view_list[view_sel].scroll_down(scroll);
}
void
view_scroll_up(enum menu_scroll scroll)
{
view_list[view_sel].scroll_up(scroll);
}
void
view_search_next(const char *search)
{
if (search != NULL) {
free(view_search);
view_search = xstrdup(search);
} else if (view_search == NULL) {
msg_errx("No previous search");
return;
}
view_list[view_sel].search_next(view_search);
}
void
view_search_prev(const char *search)
{
if (search != NULL) {
free(view_search);
view_search = xstrdup(search);
} else if (view_search == NULL) {
msg_errx("No previous search");
return;
}
view_list[view_sel].search_prev(view_search);
}
void
view_select_active_entry(void)
{
if (view_list[view_sel].select_active_entry != NULL)
view_list[view_sel].select_active_entry();
}
void
view_select_first_entry(void)
{
view_list[view_sel].select_first_entry();
}
void
view_select_last_entry(void)
{
view_list[view_sel].select_last_entry();
}
void
view_select_next_entry(void)
{
view_list[view_sel].select_next_entry();
}
void
view_select_prev_entry(void)
{
view_list[view_sel].select_prev_entry();
}
void
view_select_view(enum view_id id)
{
size_t i;
if (view_list[view_sel].id != id)
for (i = 0; i < nitems(view_list); i++)
if (view_list[i].id == id) {
view_sel = i;
view_print();
break;
}
}
void
view_reactivate_entry(void)
{
if (view_list[view_sel].reactivate_entry != NULL)
view_list[view_sel].reactivate_entry();
}