playlist basic operations
[melted_gui] / src / playlist.c
1 /*
2 * playlist.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <pthread.h>
30
31 #include "playlist.h"
32 #include "ui.h"
33 #include "timecode.h"
34 #include "library.h"
35
36 extern GtkTargetEntry drag_targets[];
37
38 static void playlist_get_selected_items_idx_iter
39 (
40 GtkTreeModel *model,
41 GtkTreePath *path,
42 GtkTreeIter *iter,
43 gpointer data
44 )
45 {
46 int idx, l;
47 int **plist = (int**)data;
48 int *list = *plist;
49
50 gtk_tree_model_get(model, iter, 7, &idx, -1);
51
52 if(!list)
53 {
54 list = (int*)malloc(sizeof(int));
55 list[0] = -1;
56 };
57
58 /* find numbers of items in list */
59 for(l = 0; -1 != list[l]; l++);
60 g_warning("playlist_get_selected_items_idx_iter: l=%d", l);
61
62 /* realloc items */
63 list = (int*)realloc(list, (l + 2) * sizeof(int));
64
65 /* clean last item */
66 list[l + 1] = -1;
67
68 /* setup items */
69 list[l] = idx;
70
71 *plist = list;
72 };
73
74 int* playlist_get_selected_items_idx(instance_t* app, int *count)
75 {
76 int* list = NULL, l = 0;
77 GtkTreeSelection *selection;
78
79 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid));
80 if(selection)
81 {
82 gtk_tree_selection_selected_foreach(
83 selection,
84 playlist_get_selected_items_idx_iter,
85 &list);
86
87 if(list)
88 for(l = 0; -1 != list[l]; l++);
89 };
90
91 *count = l;
92 return list;
93 };
94
95
96 static void playlist_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
97 GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
98 {
99 int *list, i, c;
100 playlist_item_t* items;
101 instance_t* app = (instance_t*)userdata;
102
103 g_warning("playlist_drag_data_get_cb");
104
105 list = playlist_get_selected_items_idx(app, &c);
106 if(!list) return;
107
108 /* clear delete flag */
109 for(i = 0; i < app->playlist.count; i++)
110 app->playlist.item[i].del = 0;
111
112 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * c);
113 for(i = 0; i < c; i++)
114 {
115 items[i] = app->playlist.item[list[i]];
116 if(context->action == GDK_ACTION_MOVE)
117 app->playlist.item[list[i]].del = 1;
118 }
119 gtk_selection_data_set(selection_data, selection_data->target, 8,
120 (const guchar *)items, sizeof(playlist_item_t) * c);
121
122 free(items);
123 free(list);
124 };
125
126 static void playlist_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
127 {
128 g_warning("playlist_drag_begin_cb");
129 gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
130 };
131
132 static void playlist_drag_data_received(GtkWidget *widget, GdkDragContext *context,
133 gint x, gint y, GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
134 {
135 int c, i, idx;
136 playlist_item_type_t t;
137 playlist_item_t* items;
138 GtkTreePath *path = NULL;
139 instance_t* app = (instance_t*)userdata;
140
141 g_warning("playlist_drag_data_received: context->action=%d", context->action);
142
143 items = (playlist_item_t*)gtk_selection_data_get_data(selection_data);
144 c = gtk_selection_data_get_length(selection_data);
145
146 if(c % sizeof(playlist_item_t))
147 {
148 g_warning("playlist_drag_data_received: ODD ITEMS");
149 }
150 else
151 {
152 c /= sizeof(playlist_item_t);
153
154 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y, &path, NULL, NULL, NULL))
155 {
156 idx = gtk_tree_path_get_indices(path)[0];
157 gtk_tree_path_free(path);
158
159 g_warning("playlist_drag_data_received: gtk_tree_path_get_indice[0]=%d", idx);
160
161 /* normalize, FIX ME */
162 idx--; if(idx < 0) idx = 0;
163 }
164 else
165 idx = app->playlist.count;
166
167 g_warning("playlist_drag_data_received: idx=%d", idx);
168
169 if(playlist_insert_check(app, idx, &t))
170 {
171 for(i = 0; i < c; i++)
172 {
173 items[i].type = t;
174 items[i].error = 0;
175 };
176 playlist_insert_items(app, idx, items, c);
177 };
178 };
179
180 /* Finish the drag */
181 gtk_drag_finish(context, TRUE, FALSE, time);
182 };
183
184 static void playlist_drag_data_delete(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
185 {
186 int c, i, *list;
187 instance_t* app = (instance_t*)userdata;
188
189 g_warning("playlist_drag_data_delete");
190
191 list = (int*)malloc(sizeof(int) * MAX_PLAYLIST_ITEMS);
192
193 for(i = 0, c = 0; i < app->playlist.count; i++)
194 if(app->playlist.item[i].del)
195 if(!playlist_idx_cued(app, i, NULL))
196 {
197 /* save index */
198 list[c++] = i;
199 g_warning("playlist_drag_data_delete: i=%d, c=%d", i, c);
200 };
201
202 if(c)
203 playlist_delete_items(app, list, c, 0);
204
205 free(list);
206 };
207
208 /*
209 * http://www.mail-archive.com/mahogany-users@lists.sourceforge.net/msg00286.html
210 */
211 static gboolean playlist_drag_motion(GtkWidget *widget, GdkDragContext *context,
212 gint x, gint y, guint time, gpointer data)
213 {
214 gboolean same;
215 GtkWidget *source_widget;
216
217 g_warning("playlist_grid_drag_motion");
218
219 /* Get source widget and check if it is the same as the
220 * destination widget.
221 */
222 source_widget = gtk_drag_get_source_widget(context);
223 same = ((source_widget == widget) ? TRUE : FALSE);
224
225 /* Put additional checks here, perhaps if same is FALSE then
226 * set the default drag to GDK_ACTION_COPY.
227 */
228
229 /* Say we just want to allow GDK_ACTION_MOVE, first we check
230 * if that is in the list of allowed actions on the dc. If
231 * so then we set it to that. Note if the user holds down the
232 * ctrl key then the only flag in dc->actions will be
233 * GDK_ACTION_COPY. The constraint for dc->actions is that
234 * specified from the given actions in gtk_drag_dest_set() and
235 * gtk_drag_source_set().
236 */
237 if(same)
238 {
239 if(context->actions == GDK_ACTION_MOVE)
240 gdk_drag_status(context, GDK_ACTION_COPY, time);
241 else
242 gdk_drag_status(context, GDK_ACTION_MOVE, time);
243 }
244 else
245 gdk_drag_status(context, context->actions, time);
246
247 return(TRUE);
248 }
249
250
251 int playlist_item_index(instance_t* app, int start, int int_idx)
252 {
253 if(start < 0 || start >= app->playlist.count)
254 return -1;
255
256 while(1)
257 {
258 if(app->playlist.item[start].int_idx == int_idx)
259 return start;
260
261 if(app->playlist.item[start].type & PLAYLIST_BLOCK_END)
262 break;
263
264 start++;
265 };
266
267 return -1;
268 };
269
270
271 void playlist_init(instance_t* app)
272 {
273 gtk_drag_source_set(app->playlist_grid, GDK_BUTTON1_MASK,
274 drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
275
276 gtk_drag_dest_set(app->playlist_grid, (GtkDestDefaults)(GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
277 drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
278
279 g_signal_connect (app->playlist_grid, "drag_data_get", G_CALLBACK(playlist_drag_data_get_cb), app);
280 g_signal_connect (app->playlist_grid, "drag_begin", G_CALLBACK(playlist_drag_begin_cb), app);
281 g_signal_connect (app->playlist_grid, "drag_data_received", G_CALLBACK (playlist_drag_data_received), app);
282 g_signal_connect (app->playlist_grid, "drag_data_delete", G_CALLBACK (playlist_drag_data_delete), app);
283 g_signal_connect (app->playlist_grid, "drag_motion", G_CALLBACK (playlist_drag_motion), app);
284 };
285
286 void playlist_release(instance_t* app)
287 {
288 };
289
290 int playlist_idx_cued(instance_t* app, int idx, int* player_idx)
291 {
292 int i;
293
294 for(i = 0; i < app->players.count; i++)
295 {
296 int a, b;
297
298 a = app->players.item[i].playlist_start;
299 b = app->players.item[i].playlist_length;
300
301 if(b <= 0)
302 continue;
303
304 b = a + b - 1;
305
306 if(idx >= a && idx <= b)
307 {
308 if(player_idx)
309 *player_idx = i;
310 return 1;
311 };
312 };
313
314 return 0;
315 };
316
317 int playlist_range_cued(instance_t* app, int start, int stop)
318 {
319 int i;
320
321 for(i = start; i <= stop; i++)
322 if(playlist_idx_cued(app, i, NULL))
323 return 1;
324
325 return 0;
326 };
327
328 void playlist_block(instance_t* app, int loop)
329 {
330 int start, stop, i, c;
331 int* list = playlist_get_selected_items_idx(app, &c);
332
333 if(!list)
334 return;
335
336 pthread_mutex_lock(&app->playlist.lock);
337 pthread_mutex_lock(&app->players.lock);
338
339 start = list[0];
340 stop = list[c - 1];
341
342 if(loop)
343 loop = PLAYLIST_BLOCK_LOOP;
344
345 if(!playlist_range_cued(app, start, stop))
346 {
347 /* update selected item */
348 for(i = start; i <= stop; i++)
349 {
350 int t = PLAYLIST_BLOCK_BODY | loop;
351
352 if(i == start) t |= PLAYLIST_BLOCK_BEGIN;
353 if(i == stop) t |= PLAYLIST_BLOCK_END;
354
355 app->playlist.item[i].type = (playlist_item_type_t)t;
356
357 ui_playlist_draw_item(app, i);
358 };
359
360 /* update border items */
361 if(start && !(app->playlist.item[start - 1].type & PLAYLIST_BLOCK_END))
362 {
363 app->playlist.item[start - 1].type = (playlist_item_type_t)(PLAYLIST_BLOCK_END
364 | app->playlist.item[start - 1].type);
365 ui_playlist_draw_item(app, start - 1);
366 };
367 if((stop + 1) < app->playlist.count && !(app->playlist.item[stop + 1].type & PLAYLIST_BLOCK_BEGIN))
368 {
369 app->playlist.item[stop + 1].type = (playlist_item_type_t)(PLAYLIST_BLOCK_BEGIN
370 | app->playlist.item[stop + 1].type);
371 ui_playlist_draw_item(app, stop + 1);
372 };
373 }
374 else
375 g_warning("omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
376 start, stop);
377
378 pthread_mutex_unlock(&app->players.lock);
379 pthread_mutex_unlock(&app->playlist.lock);
380
381 free(list);
382 };
383
384 int playlist_get_first_selected_item_idx(instance_t* app)
385 {
386 int idx, c;
387 int* list = playlist_get_selected_items_idx(app, &c);
388 if(!list)
389 return -1;
390 idx = list[0];
391 free(list);
392 return idx;
393 };
394
395 int playlist_get_block(instance_t* app, int idx, int* pstart, int* pstop)
396 {
397 int start, stop;
398
399 for(start = idx; start >= 0; start--)
400 if(app->playlist.item[start].type & PLAYLIST_BLOCK_BEGIN)
401 break;
402
403 for(stop = idx; stop < app->playlist.count; stop++)
404 if(app->playlist.item[stop].type & PLAYLIST_BLOCK_END)
405 break;
406
407 g_warning("playlist_get_block: range %d -> %d\n", start, stop);
408
409 /* check block range */
410 if(start >= 0 && stop < app->playlist.count)
411 {
412 *pstart = start;
413 *pstop = stop;
414 return (stop - start + 1);
415 };
416
417 return -1;
418 };
419
420 player_t *playlist_get_player_at_pos(instance_t* app, int pos)
421 {
422 /* check player range */
423 if(app->playlist.item[pos].player > -1 && app->playlist.item[pos].player < app->players.count)
424 return &app->players.item[app->playlist.item[pos].player];
425
426 return NULL;
427 };
428
429 void playlist_delete_items(instance_t* app, int* idxs, int count, int sel)
430 {
431 int i, j, idx;
432
433 pthread_mutex_lock(&app->playlist.lock);
434 pthread_mutex_lock(&app->players.lock);
435
436 for(j = 0; j < count; j++)
437 {
438 idx = idxs[j] - j;
439
440 /* fix block types */
441 if( app->playlist.item[idx].type != PLAYLIST_ITEM_BLOCK_BODY &&
442 app->playlist.item[idx].type != PLAYLIST_ITEM_LOOP_BODY)
443 {
444 if(idx)
445 app->playlist.item[idx - 1].type = (playlist_item_type_t)(app->playlist.item[idx - 1].type |
446 PLAYLIST_BLOCK_END);
447 if(idx + 1 < app->playlist.count)
448 app->playlist.item[idx + 1].type = (playlist_item_type_t)(app->playlist.item[idx + 1].type |
449 PLAYLIST_BLOCK_BEGIN);
450 };
451
452 /* shift playlist items */
453 memmove
454 (
455 &app->playlist.item[idx],
456 &app->playlist.item[idx + 1],
457 (app->playlist.count - idx - 1) * sizeof(playlist_item_t)
458 );
459
460 /* decrement items count */
461 app->playlist.count--;
462
463 /* increment servers indexes */
464 for(i = 0; i < app->players.count; i++)
465 if(app->players.item[i].playlist_start >= idx)
466 app->players.item[i].playlist_start--;
467 };
468
469 /* redraw playlist */
470 ui_playlist_draw(app);
471
472 /* select */
473 if(sel)
474 ui_playlist_select_item(app, idxs[0]);
475
476 pthread_mutex_unlock(&app->players.lock);
477 pthread_mutex_unlock(&app->playlist.lock);
478 };
479
480 void playlist_delete_selected_items(instance_t* app)
481 {
482 int i, cnt1, cnt2;
483 int *list1, *list2;
484
485 list1 = playlist_get_selected_items_idx(app, &cnt1);
486 if(!list1) return;
487
488 list2 = (int*)malloc(sizeof(int) * cnt1);
489
490 for(i = 0, cnt2 = 0; i < cnt1; i++)
491 {
492 /* check for playing block */
493 if(playlist_idx_cued(app, list1[i], NULL))
494 continue;
495
496 /* save index */
497 list2[cnt2++] = list1[i];
498 };
499
500 if(cnt2)
501 playlist_delete_items(app, list2, cnt2, 1);
502
503 free(list2);
504 free(list1);
505 };
506
507 int playlist_insert_check(instance_t* app, int idx, playlist_item_type_t* t)
508 {
509 *t = PLAYLIST_ITEM_BLOCK_SINGLE;
510
511 /* before or after playlist */
512 if(!idx || idx == app->playlist.count)
513 return 1;
514
515 /* check for block borders */
516 if( app->playlist.item[idx - 1].type & PLAYLIST_BLOCK_END &&
517 app->playlist.item[idx + 0].type & PLAYLIST_BLOCK_BEGIN)
518 return 1;
519
520 /* check for playing block */
521 if(playlist_idx_cued(app, idx, NULL))
522 return 0;
523
524 if(app->playlist.item[idx].type & PLAYLIST_BLOCK_LOOP)
525 *t = PLAYLIST_ITEM_LOOP_BODY;
526 else
527 *t = PLAYLIST_ITEM_BLOCK_BODY;
528
529 return 1;
530 };
531
532 void playlist_insert_items(instance_t* app, int idx, playlist_item_t* items, int count)
533 {
534 int i;
535
536 pthread_mutex_lock(&app->playlist.lock);
537 pthread_mutex_lock(&app->players.lock);
538
539 /* shift playlist items */
540 memmove
541 (
542 &app->playlist.item[idx + count],
543 &app->playlist.item[idx],
544 (app->playlist.count - idx) * sizeof(playlist_item_t)
545 );
546
547 /* copy new items */
548 memcpy
549 (
550 &app->playlist.item[idx],
551 items,
552 count * sizeof(playlist_item_t)
553 );
554
555 /* increment servers indexes */
556 for(i = 0; i < app->players.count; i++)
557 if(app->players.item[i].playlist_start >= idx)
558 app->players.item[i].playlist_start += idx;
559
560 /* increment items count */
561 app->playlist.count += count;
562
563 /* redraw playlist */
564 ui_playlist_draw(app);
565
566 /* select */
567 ui_playlist_select_item(app, idx);
568
569 pthread_mutex_unlock(&app->players.lock);
570 pthread_mutex_unlock(&app->playlist.lock);
571 };
572
573 void playlist_item_copy(instance_t* app)
574 {
575 int *list, i, c;
576
577 list = playlist_get_selected_items_idx(app, &c);
578 if(!list) return;
579
580 for(i = 0; i < c; i++)
581 app->clipboard.item[i] = app->playlist.item[list[i]];
582 app->clipboard.count = c;
583
584 free(list);
585 };
586
587 void playlist_item_paste(instance_t* app, int after)
588 {
589 int idx, i;
590 playlist_item_type_t t;
591
592 /* find insert position */
593 idx = playlist_get_first_selected_item_idx(app);
594 if(idx < 0)
595 idx = 0;
596 else
597 idx += (after)?1:0;
598
599 if(!playlist_insert_check(app, idx, &t))
600 return;
601
602 /* clear item */
603 if(app->clipboard.count)
604 {
605 for(i = 0; i < app->clipboard.count; i++)
606 {
607 app->clipboard.item[i].type = t;
608 app->clipboard.item[i].error = 0;
609 };
610 playlist_insert_items(app, idx, app->clipboard.item, app->clipboard.count);
611 };
612 };
613
614 void playlist_item_swap(instance_t* app, int dir)
615 {
616 int sel, a, b, e = 1;
617 playlist_item_t item;
618
619 /* find insert position */
620 sel = playlist_get_first_selected_item_idx(app);
621 if(sel < 0)
622 return;
623
624 if(dir < 0)
625 {
626 a = sel - 1;
627 b = sel;
628 sel = a;
629 }
630 else
631 {
632 a = sel;
633 b = sel + 1;
634 sel = b;
635 };
636
637 /* check for playing block */
638 if(playlist_idx_cued(app, a, NULL) || playlist_idx_cued(app, b, NULL))
639 return;
640
641 pthread_mutex_lock(&app->playlist.lock);
642 pthread_mutex_lock(&app->players.lock);
643
644 /* swap */
645 item = app->playlist.item[a];
646 app->playlist.item[a] = app->playlist.item[b];
647 app->playlist.item[b] = item;
648
649 /* rewite type */
650 if(app->playlist.item[a].type != app->playlist.item[b].type)
651 {
652 e = 0;
653 app->playlist.item[a].type = PLAYLIST_ITEM_BLOCK_SINGLE;
654 app->playlist.item[b].type = PLAYLIST_ITEM_BLOCK_SINGLE;
655 };
656
657 /* redraw main items */
658 ui_playlist_draw_item(app, a);
659 ui_playlist_draw_item(app, b);
660
661 /* fix block types */
662 if(a && !e)
663 {
664 app->playlist.item[a - 1].type = (playlist_item_type_t)(app->playlist.item[a - 1].type |
665 PLAYLIST_BLOCK_END);
666 ui_playlist_draw_item(app, a - 1);
667 };
668 if(b + 1 < app->playlist.count && !e)
669 {
670 app->playlist.item[b + 1].type = (playlist_item_type_t)(app->playlist.item[b + 1].type |
671 PLAYLIST_BLOCK_BEGIN);
672 ui_playlist_draw_item(app, b + 1);
673 };
674
675 /* select */
676 ui_playlist_select_item(app, sel);
677
678 pthread_mutex_unlock(&app->players.lock);
679 pthread_mutex_unlock(&app->playlist.lock);
680 };
681
682
683 static int playlist_load_file_ply(instance_t* app, char* filename)
684 {
685 FILE* f;
686 char *ID, *CH, *B, *IN, *OUT, *DUR, *REST, *l;
687 int count = 0, i;
688 playlist_item_t* items;
689
690 /* allocate space for strings and items */
691 items = malloc(sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
692 memset(items, 0, sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
693 ID = malloc(PATH_MAX);
694 CH = malloc(PATH_MAX);
695 B = malloc(PATH_MAX);
696 IN = malloc(PATH_MAX);
697 OUT = malloc(PATH_MAX);
698 DUR = malloc(PATH_MAX);
699 REST = malloc(PATH_MAX);
700 l = malloc(PATH_MAX);
701
702 /* open and process file */
703 f = fopen(filename, "rt");
704 if(f)
705 {
706 while( !feof(f) )
707 {
708 char* s;
709
710 /* load string */
711 memset(l, 0, PATH_MAX);
712 fgets(l, PATH_MAX, f);
713
714 /* remove newlines */
715 if( (s = strchr(l, '\n')) ) *s = 0;
716 if( (s = strchr(l, '\r')) ) *s = 0;
717 if( (s = strchr(l, '\t')) ) *s = 0;
718
719 /* check for empty line */
720 if(l[0] && l[0] != '#')
721 {
722 if (6 != sscanf(l, "%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%s",
723 ID, CH, B, IN, OUT, DUR, REST))
724 {
725 int b = atol(B);
726 /* setup item */
727 tc2frames(IN, 25.0, &items[count].in);
728 tc2frames(DUR, 25.0, &items[count].dur);
729 strncpy(items[count].id, ID, PATH_MAX);
730 items[count].player = atol(CH) - 1;
731 switch(b)
732 {
733 case 1: items[count].type = PLAYLIST_ITEM_BLOCK_SINGLE; break;
734 case 2: items[count].type = PLAYLIST_ITEM_LOOP_BEGIN; break;
735 case 3: items[count].type = PLAYLIST_ITEM_LOOP_BODY; break;
736 case 4: items[count].type = PLAYLIST_ITEM_LOOP_END; break;
737 case 6: items[count].type = PLAYLIST_ITEM_BLOCK_END; break;
738 case 0:
739 if(!count)
740 items[count].type = PLAYLIST_ITEM_BLOCK_BEGIN;
741 else if(items[count - 1].type == PLAYLIST_ITEM_BLOCK_BEGIN ||
742 items[count - 1].type == PLAYLIST_ITEM_BLOCK_BODY)
743 items[count].type = PLAYLIST_ITEM_BLOCK_BODY;
744 else
745 items[count].type = PLAYLIST_ITEM_BLOCK_BEGIN;
746 break;
747 default:
748 if(b >= 1024)
749 items[count].type = b - 1024;
750 };
751 #if 0
752 {
753 char* n;
754 switch(items[count].type)
755 {
756 case PLAYLIST_ITEM_BLOCK_BEGIN: n = "BLOCK_BEGIN"; break;
757 case PLAYLIST_ITEM_BLOCK_BODY: n = "BLOCK_BODY"; break;
758 case PLAYLIST_ITEM_BLOCK_END: n = "BLOCK_END"; break;
759 case PLAYLIST_ITEM_BLOCK_SINGLE: n = "BLOCK_SINGLE"; break;
760 case PLAYLIST_ITEM_LOOP_BEGIN: n = "LOOP_BEGIN"; break;
761 case PLAYLIST_ITEM_LOOP_BODY: n = "LOOP_BODY"; break;
762 case PLAYLIST_ITEM_LOOP_END: n = "LOOP_END"; break;
763 case PLAYLIST_ITEM_LOOP_SINGLE: n = "LOOP_SINGLE"; break;
764 };
765 fprintf(stderr, "src=[%s]\ndst=[idx=%d,block=%s,block_id=%d,in=%d,out=%d]\n",
766 l, count, n, items[count].type, items[count].in, items[count].dur);
767 };
768 #endif
769
770 count++;
771 }
772 };
773 }
774
775 fclose(f);
776 }
777
778 /* add loaded items to playlist */
779 if(count)
780 {
781 pthread_mutex_lock(&app->playlist.lock);
782 for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
783 {
784 // omnplay_library_normalize_item(app, &items[i]);
785 app->playlist.item[app->playlist.count++] = items[i];
786 };
787 app->playlist.ver_curr++;
788 pthread_mutex_unlock(&app->playlist.lock);
789 }
790
791 /* free data */
792 free(items);
793 free(ID);
794 free(CH);
795 free(IN);
796 free(OUT);
797 free(DUR);
798 free(REST);
799 free(l);
800
801 return count;
802 };
803
804 void playlist_load(instance_t* app)
805 {
806 int r;
807 GtkWidget *dialog;
808 GtkFileFilter *filter;
809
810 dialog = gtk_file_chooser_dialog_new("Open File",
811 GTK_WINDOW (app->window),
812 GTK_FILE_CHOOSER_ACTION_OPEN,
813 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
814 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
815 NULL);
816
817 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
818 (app->playlist.path)?app->playlist.path:getenv("HOME"));
819
820 filter = gtk_file_filter_new();
821 gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
822 gtk_file_filter_add_pattern(filter, "*.ply");
823 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
824 filter = gtk_file_filter_new();
825 gtk_file_filter_set_name(filter, "All types (*.*)");
826 gtk_file_filter_add_pattern(filter, "*.*");
827 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
828
829 r = gtk_dialog_run(GTK_DIALOG(dialog));
830
831 if(r == GTK_RESPONSE_ACCEPT)
832 {
833 char *filename;
834
835 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
836
837 r = playlist_load_file_ply(app, filename);
838
839 if(r)
840 ui_playlist_draw(app);
841
842 if(app->playlist.path)
843 g_free(app->playlist.path);
844 if((app->playlist.path = filename))
845 {
846 char* e = strrchr(app->playlist.path, '/');
847 if(e) *e = 0;
848 }
849 }
850
851 gtk_widget_destroy (dialog);
852 };
853
854 static int playlist_save_file_ply(instance_t* app, char* filename)
855 {
856 int i;
857 FILE* f;
858 char tc1[12], tc2[12], tc3[12];
859 char* fname = filename;
860
861 filename = (char*)malloc(PATH_MAX);
862 strncpy(filename, fname, PATH_MAX);
863 i = strlen(filename);
864 if(i < 4 || strcasecmp(filename + i - 4, ".ply"))
865 strcat(filename, ".ply");
866
867 if((f = fopen(filename, "wt")))
868 {
869 for(i = 0; i < app->playlist.count; i++)
870 fprintf(f, "%s,%d,%d,%s,%s,%s,,,,,,,,\n",
871 app->playlist.item[i].id,
872 app->playlist.item[i].player + 1,
873 app->playlist.item[i].type + 1024,
874 frames2tc(app->playlist.item[i].in, 25.0, tc1),
875 frames2tc(app->playlist.item[i].in + app->playlist.item[i].dur, 25.0, tc2),
876 frames2tc(app->playlist.item[i].dur, 25.0, tc3));
877
878 fclose(f);
879 };
880
881 free(filename);
882
883 return 0;
884 };
885
886 void playlist_save(instance_t* app)
887 {
888 int r;
889 GtkWidget *dialog;
890 GtkFileFilter *filter;
891
892 dialog = gtk_file_chooser_dialog_new("Save File",
893 GTK_WINDOW (app->window),
894 GTK_FILE_CHOOSER_ACTION_SAVE,
895 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
896 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
897 NULL);
898
899 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
900
901 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
902 (app->playlist.path)?app->playlist.path:getenv("HOME"));
903
904 filter = gtk_file_filter_new();
905 gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
906 gtk_file_filter_add_pattern(filter, "*.ply");
907 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
908 g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(0));
909 filter = gtk_file_filter_new();
910 gtk_file_filter_set_name(filter, "Text (*.txt)");
911 gtk_file_filter_add_pattern(filter, "*.*");
912 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
913 g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(1));
914
915 r = gtk_dialog_run(GTK_DIALOG(dialog));
916
917 if(r == GTK_RESPONSE_ACCEPT)
918 {
919 char *filename;
920
921 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
922
923 r = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id"));
924
925 r = playlist_save_file_ply(app, filename);
926
927 if(app->playlist.path)
928 g_free(app->playlist.path);
929 if((app->playlist.path = filename))
930 {
931 char* e = strrchr(app->playlist.path, '/');
932 if(e) *e = 0;
933 }
934 }
935
936 gtk_widget_destroy (dialog);
937 };
938
939
940 void playlist_relink(instance_t* app)
941 {
942 int i, cnt;
943 int *list;
944
945 pthread_mutex_lock(&app->playlist.lock);
946 list = playlist_get_selected_items_idx(app, &cnt);
947 if(list)
948 {
949 for(i = 0; i < cnt; i++)
950 {
951 /* check for playing block */
952 if(playlist_idx_cued(app, list[i], NULL))
953 continue;
954
955 /* relink item */
956 library_relink_item(app, &app->playlist.item[list[i]]);
957 };
958
959 free(list);
960 };
961 pthread_mutex_unlock(&app->playlist.lock);
962
963 /* redraw playlist */
964 ui_playlist_draw(app);
965 };
966
967 void playlist_item_add(instance_t* app, int after)
968 {
969 int idx;
970 playlist_item_t item;
971 playlist_item_type_t t;
972
973 /* find insert position */
974 idx = playlist_get_first_selected_item_idx(app);
975 if(idx < 0)
976 idx = 0;
977 else
978 idx += (after)?1:0;
979
980 if(!playlist_insert_check(app, idx, &t))
981 return;
982
983 g_warning("allowed insert into idx=%d\n", idx);
984
985 /* clear item */
986 memset(&item, 0, sizeof(playlist_item_t));
987 if(ui_playlist_item_dialog(app, &item))
988 {
989 library_normalize_item(app, &item);
990 item.type = t;
991 playlist_insert_items(app, idx, &item, 1);
992 };
993 };
994
995 void playlist_item_edit(instance_t* app)
996 {
997 int idx;
998 playlist_item_t item;
999
1000 /* find insert position */
1001 idx = playlist_get_first_selected_item_idx(app);
1002
1003 if(idx < 0)
1004 return;
1005
1006 /* check for playing block */
1007 if(playlist_idx_cued(app, idx, NULL))
1008 return;
1009
1010 item = app->playlist.item[idx];
1011
1012 if(ui_playlist_item_dialog(app, &item))
1013 {
1014 library_normalize_item(app, &item);
1015 app->playlist.item[idx] = item;
1016 ui_playlist_draw_item(app, idx);
1017 };
1018 };
1019
1020 void playlist_item_add_from_library(instance_t* app, int after)
1021 {
1022 };
1023
1024 void playlist_normalize(instance_t* app)
1025 {
1026 int i;
1027
1028 /* normalize playlist */
1029 for(i = 0; i < app->playlist.count; i++)
1030 if(library_normalize_item(app, &app->playlist.item[i]))
1031 ui_playlist_draw_item(app, i);
1032 };