playlist save/load: step 1
[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 #if 0
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 static int playlist_save_file_ply(instance_t* app, char* filename)
805 {
806 int i;
807 FILE* f;
808 char tc1[12], tc2[12], tc3[12];
809 char* fname = filename;
810
811 filename = (char*)malloc(PATH_MAX);
812 strncpy(filename, fname, PATH_MAX);
813 i = strlen(filename);
814 if(i < 4 || strcasecmp(filename + i - 4, ".ply"))
815 strcat(filename, ".ply");
816
817 if((f = fopen(filename, "wt")))
818 {
819 for(i = 0; i < app->playlist.count; i++)
820 fprintf(f, "%s,%d,%d,%s,%s,%s,,,,,,,,\n",
821 app->playlist.item[i].id,
822 app->playlist.item[i].player + 1,
823 app->playlist.item[i].type + 1024,
824 frames2tc(app->playlist.item[i].in, 25.0, tc1),
825 frames2tc(app->playlist.item[i].in + app->playlist.item[i].dur, 25.0, tc2),
826 frames2tc(app->playlist.item[i].dur, 25.0, tc3));
827
828 fclose(f);
829 };
830
831 free(filename);
832
833 return 0;
834 };
835 #endif
836
837 static int playlist_load_plm(instance_t* app, char* filename)
838 {
839 return 0;
840 };
841
842 static int playlist_save_plm(instance_t* app, char* filename)
843 {
844 return 0;
845 };
846
847 static int playlist_load_plx(instance_t* app, char* filename)
848 {
849 return -1;
850 };
851
852 static int playlist_save_plx(instance_t* app, char* filename)
853 {
854 return -1;
855 };
856
857 static struct ui_playlist_io_funcs playlist_io[] =
858 {
859 {
860 "Text formatted playlist (*.plt)",
861 "*.plm",
862 playlist_load_plm,
863 playlist_save_plm
864 },
865 {
866 "Xml formatted playlist (*.plx)",
867 "*.plx",
868 playlist_load_plx,
869 playlist_save_plx,
870 },
871 {
872 NULL,
873 NULL,
874 NULL,
875 NULL
876 }
877 };
878
879 void playlist_load(instance_t* app)
880 {
881 ui_playlist_load(app, (app->playlist.path)?app->playlist.path:getenv("HOME"), playlist_io);
882 };
883
884 void playlist_save(instance_t* app)
885 {
886 ui_playlist_save(app, (app->playlist.path)?app->playlist.path:getenv("HOME"), playlist_io);
887 };
888
889
890 void playlist_relink(instance_t* app)
891 {
892 int i, cnt;
893 int *list;
894
895 pthread_mutex_lock(&app->playlist.lock);
896 list = playlist_get_selected_items_idx(app, &cnt);
897 if(list)
898 {
899 for(i = 0; i < cnt; i++)
900 {
901 /* check for playing block */
902 if(playlist_idx_cued(app, list[i], NULL))
903 continue;
904
905 /* relink item */
906 library_relink_item(app, &app->playlist.item[list[i]]);
907 };
908
909 free(list);
910 };
911 pthread_mutex_unlock(&app->playlist.lock);
912
913 /* redraw playlist */
914 ui_playlist_draw(app);
915 };
916
917 void playlist_item_add(instance_t* app, int after)
918 {
919 int idx;
920 playlist_item_t item;
921 playlist_item_type_t t;
922
923 /* find insert position */
924 idx = playlist_get_first_selected_item_idx(app);
925 if(idx < 0)
926 idx = 0;
927 else
928 idx += (after)?1:0;
929
930 if(!playlist_insert_check(app, idx, &t))
931 return;
932
933 g_warning("allowed insert into idx=%d\n", idx);
934
935 /* clear item */
936 memset(&item, 0, sizeof(playlist_item_t));
937 if(ui_playlist_item_dialog(app, &item))
938 {
939 library_normalize_item(app, &item);
940 item.type = t;
941 playlist_insert_items(app, idx, &item, 1);
942 };
943 };
944
945 void playlist_item_edit(instance_t* app)
946 {
947 int idx;
948 playlist_item_t item;
949
950 /* find insert position */
951 idx = playlist_get_first_selected_item_idx(app);
952
953 if(idx < 0)
954 return;
955
956 /* check for playing block */
957 if(playlist_idx_cued(app, idx, NULL))
958 return;
959
960 item = app->playlist.item[idx];
961
962 if(ui_playlist_item_dialog(app, &item))
963 {
964 library_normalize_item(app, &item);
965 app->playlist.item[idx] = item;
966 ui_playlist_draw_item(app, idx);
967 };
968 };
969
970 void playlist_item_add_from_library(instance_t* app, int after)
971 {
972 };
973
974 void playlist_normalize(instance_t* app)
975 {
976 int i;
977
978 /* normalize playlist */
979 for(i = 0; i < app->playlist.count; i++)
980 if(library_normalize_item(app, &app->playlist.item[i]))
981 ui_playlist_draw_item(app, i);
982 };