8f22096c262d63c5c3097fb2196142075d876bf9
[omnplay] / 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 "omnplay.h"
32 #include "ui.h"
33 #include "timecode.h"
34
35 static int load_file_ply(omnplay_instance_t* app, char* filename)
36 {
37 FILE* f;
38 char *ID, *CH, *B, *IN, *OUT, *DUR, *REST, *l;
39 int count = 0, i;
40 playlist_item_t* items;
41
42 /* allocate space for strings and items */
43 items = malloc(sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
44 memset(items, 0, sizeof(playlist_item_t) * MAX_PLAYLIST_ITEMS);
45 ID = malloc(PATH_MAX);
46 CH = malloc(PATH_MAX);
47 B = malloc(PATH_MAX);
48 IN = malloc(PATH_MAX);
49 OUT = malloc(PATH_MAX);
50 DUR = malloc(PATH_MAX);
51 REST = malloc(PATH_MAX);
52 l = malloc(PATH_MAX);
53
54 /* open and process file */
55 f = fopen(filename, "rt");
56 if(f)
57 {
58 while( !feof(f) )
59 {
60 char* s;
61
62 /* load string */
63 memset(l, 0, PATH_MAX);
64 fgets(l, PATH_MAX, f);
65
66 /* remove newlines */
67 if( (s = strchr(l, '\n')) ) *s = 0;
68 if( (s = strchr(l, '\r')) ) *s = 0;
69 if( (s = strchr(l, '\t')) ) *s = 0;
70
71 /* check for empty line */
72 if(l[0] && l[0] != '#')
73 {
74 if (6 != sscanf(l, "%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%s",
75 ID, CH, B, IN, OUT, DUR, REST))
76 {
77 int b = atol(B);
78 /* setup item */
79 tc2frames(IN, 25.0, &items[count].in);
80 tc2frames(DUR, 25.0, &items[count].dur);
81 strncpy(items[count].id, ID, PATH_MAX);
82 items[count].player = atol(CH) - 1;
83 switch(b)
84 {
85 case 1: items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE; break;
86 case 2: items[count].type = OMNPLAY_PLAYLIST_ITEM_LOOP_BEGIN; break;
87 case 3: items[count].type = OMNPLAY_PLAYLIST_ITEM_LOOP_BODY; break;
88 case 4: items[count].type = OMNPLAY_PLAYLIST_ITEM_LOOP_END; break;
89 case 6: items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_END; break;
90 case 0:
91 if(!count)
92 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
93 else if(items[count - 1].type == OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN ||
94 items[count - 1].type == OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY)
95 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY;
96 else
97 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
98 break;
99 default:
100 if(b >= 1024)
101 items[count].type = b - 1024;
102 };
103 #if 0
104 {
105 char* n;
106 switch(items[count].type)
107 {
108 case OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN: n = "BLOCK_BEGIN"; break;
109 case OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY: n = "BLOCK_BODY"; break;
110 case OMNPLAY_PLAYLIST_ITEM_BLOCK_END: n = "BLOCK_END"; break;
111 case OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE: n = "BLOCK_SINGLE"; break;
112 case OMNPLAY_PLAYLIST_ITEM_LOOP_BEGIN: n = "LOOP_BEGIN"; break;
113 case OMNPLAY_PLAYLIST_ITEM_LOOP_BODY: n = "LOOP_BODY"; break;
114 case OMNPLAY_PLAYLIST_ITEM_LOOP_END: n = "LOOP_END"; break;
115 case OMNPLAY_PLAYLIST_ITEM_LOOP_SINGLE: n = "LOOP_SINGLE"; break;
116 };
117 fprintf(stderr, "src=[%s]\ndst=[idx=%d,block=%s,block_id=%d,in=%d,out=%d]\n",
118 l, count, n, items[count].type, items[count].in, items[count].dur);
119 };
120 #endif
121
122 count++;
123 }
124 };
125 }
126
127 fclose(f);
128 }
129
130 /* add loaded items to playlist */
131 if(count)
132 {
133 pthread_mutex_lock(&app->playlist.lock);
134 for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
135 {
136 omnplay_library_normalize_item(app, &items[i]);
137 app->playlist.item[app->playlist.count++] = items[i];
138 };
139 app->playlist.ver_curr++;
140 pthread_mutex_unlock(&app->playlist.lock);
141 }
142
143 /* free data */
144 free(items);
145 free(ID);
146 free(CH);
147 free(IN);
148 free(OUT);
149 free(DUR);
150 free(REST);
151 free(l);
152
153 return count;
154 };
155
156 void omnplay_playlist_load(omnplay_instance_t* app)
157 {
158 int r;
159 GtkWidget *dialog;
160 GtkFileFilter *filter;
161
162 dialog = gtk_file_chooser_dialog_new("Open File",
163 GTK_WINDOW (app->window),
164 GTK_FILE_CHOOSER_ACTION_OPEN,
165 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
166 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
167 NULL);
168
169 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
170 (app->playlist.path)?app->playlist.path:getenv("HOME"));
171
172 filter = gtk_file_filter_new();
173 gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
174 gtk_file_filter_add_pattern(filter, "*.ply");
175 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
176 filter = gtk_file_filter_new();
177 gtk_file_filter_set_name(filter, "All types (*.*)");
178 gtk_file_filter_add_pattern(filter, "*.*");
179 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
180
181 r = gtk_dialog_run(GTK_DIALOG(dialog));
182
183 if(r == GTK_RESPONSE_ACCEPT)
184 {
185 char *filename;
186
187 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
188
189 r = load_file_ply(app, filename);
190
191 if(r)
192 omnplay_playlist_draw(app);
193
194 if(app->playlist.path)
195 g_free(app->playlist.path);
196 if((app->playlist.path = filename))
197 {
198 char* e = strrchr(app->playlist.path, '/');
199 if(e) *e = 0;
200 }
201 }
202
203 gtk_widget_destroy (dialog);
204 };
205
206 static int save_file_ply(omnplay_instance_t* app, char* filename)
207 {
208 int i;
209 FILE* f;
210 char tc1[12], tc2[12], tc3[12];
211
212 if((f = fopen(filename, "wt")))
213 {
214 for(i = 0; i < app->playlist.count; i++)
215 fprintf(f, "%s,%d,%d,%s,%s,%s,,,,,,,,\n",
216 app->playlist.item[i].id,
217 app->playlist.item[i].player + 1,
218 app->playlist.item[i].type + 1024,
219 frames2tc(app->playlist.item[i].in, 25.0, tc1),
220 frames2tc(app->playlist.item[i].in + app->playlist.item[i].dur, 25.0, tc2),
221 frames2tc(app->playlist.item[i].dur, 25.0, tc3));
222 };
223
224 return 0;
225 };
226
227 void omnplay_playlist_save(omnplay_instance_t* app)
228 {
229 int r;
230 GtkWidget *dialog;
231 GtkFileFilter *filter;
232
233 dialog = gtk_file_chooser_dialog_new("Save File",
234 GTK_WINDOW (app->window),
235 GTK_FILE_CHOOSER_ACTION_SAVE,
236 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
237 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
238 NULL);
239
240 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
241
242 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
243 (app->playlist.path)?app->playlist.path:getenv("HOME"));
244
245 filter = gtk_file_filter_new();
246 gtk_file_filter_set_name(filter, "Playlist formatted (*.ply)");
247 gtk_file_filter_add_pattern(filter, "*.ply");
248 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
249 g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(0));
250 filter = gtk_file_filter_new();
251 gtk_file_filter_set_name(filter, "Text (*.txt)");
252 gtk_file_filter_add_pattern(filter, "*.*");
253 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog), filter);
254 g_object_set_data(G_OBJECT(filter), "id", GINT_TO_POINTER(1));
255
256 r = gtk_dialog_run(GTK_DIALOG(dialog));
257
258 if(r == GTK_RESPONSE_ACCEPT)
259 {
260 char *filename;
261
262 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
263
264 r = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog))), "id"));
265
266 r = save_file_ply(app, filename);
267
268 if(app->playlist.path)
269 g_free(app->playlist.path);
270 if((app->playlist.path = filename))
271 {
272 char* e = strrchr(app->playlist.path, '/');
273 if(e) *e = 0;
274 }
275 }
276
277 gtk_widget_destroy (dialog);
278
279 };
280
281 void omnplay_playlist_draw(omnplay_instance_t* app)
282 {
283 int i;
284 int* sels;
285 char tc1[12], tc2[12];
286 GtkListStore *list_store;
287 GtkTreeIter iter;
288
289 sels = omnplay_selected_idxs_playlist(app);
290
291 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
292 gtk_list_store_clear(list_store);
293
294 pthread_mutex_lock(&app->playlist.lock);
295
296 for(i = 0;i < app->playlist.count; i++)
297 {
298 char ch[3];
299
300 if(OMNPLAY_PLAYLIST_BLOCK_BEGIN & app->playlist.item[i].type)
301 snprintf(ch, sizeof(ch), "%c", 'A' + app->playlist.item[i].player);
302 else
303 ch[0] = 0;
304
305 gtk_list_store_append(list_store, &iter);
306
307 gtk_list_store_set(list_store, &iter,
308 0, "",
309 1, app->playlist.block_icons[app->playlist.item[i].type],
310 2, ch,
311 3, app->playlist.item[i].id,
312 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
313 5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
314 6, app->playlist.item[i].title,
315 7, i,
316 8, (app->playlist.item[i].error != 0),
317 9, (app->playlist.item[i].error & PLAYLIST_ITEM_ERROR_LIB)?"red":"orange",
318 -1 );
319 }
320
321 app->playlist.ver_prev = app->playlist.ver_curr;
322
323 if(sels)
324 {
325 GtkTreePath *path;
326
327 /* select */
328 path = gtk_tree_path_new_from_indices(sels[1], -1);
329 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
330 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
331 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE, 0, 0);
332 gtk_tree_path_free(path);
333
334 free(sels);
335 };
336
337 pthread_mutex_unlock(&app->playlist.lock);
338 };
339
340 typedef struct omnplay_playlist_draw_item_desc
341 {
342 GtkListStore *list_store;
343 omnplay_instance_t* app;
344 int idx;
345 } omnplay_playlist_draw_item_t;
346
347 static gboolean omnplay_playlist_draw_item_proc(
348 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
349 {
350 int i;
351 char tc1[12], tc2[12];
352 char ch[3];
353 omnplay_playlist_draw_item_t* item = (omnplay_playlist_draw_item_t*)user_data;
354 omnplay_instance_t* app = item->app;
355
356 gtk_tree_model_get(model, iter, 7, &i, -1);
357
358 if(i != item->idx) return FALSE;
359
360 if(OMNPLAY_PLAYLIST_BLOCK_BEGIN & app->playlist.item[i].type)
361 snprintf(ch, sizeof(ch), "%c", 'A' + app->playlist.item[i].player);
362 else
363 ch[0] = 0;
364
365 gtk_list_store_set(item->list_store, iter,
366 0, "",
367 1, app->playlist.block_icons[app->playlist.item[i].type],
368 2, ch,
369 3, app->playlist.item[i].id,
370 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
371 5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
372 6, app->playlist.item[i].title,
373 7, i,
374 8, (app->playlist.item[i].error != 0),
375 9, (app->playlist.item[i].error & PLAYLIST_ITEM_ERROR_LIB)?"red":"orange",
376 -1 );
377
378 return TRUE;
379 };
380
381 void omnplay_playlist_draw_item(omnplay_instance_t* app, int idx)
382 {
383 GtkListStore *list_store;
384 omnplay_playlist_draw_item_t item;
385
386 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
387
388 pthread_mutex_lock(&app->playlist.lock);
389
390 item.idx = idx;
391 item.app = app;
392 item.list_store = list_store;
393 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_proc, &item);
394
395 pthread_mutex_unlock(&app->playlist.lock);
396 };
397
398 static gboolean omnplay_playlist_draw_item_rem_proc(
399 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
400 {
401 int i;
402 void** args = (void**)user_data;
403 GtkListStore *list_store = (GtkListStore *)args[1];
404 int idx = (int)args[2];
405 char* rem = (char*)args[3];
406
407 gtk_tree_model_get(model, iter, 7, &i, -1);
408
409 if(i != idx) return FALSE;
410
411 gtk_list_store_set(list_store, iter, 0, rem, -1);
412
413 return TRUE;
414 };
415
416 void omnplay_playlist_draw_item_rem(omnplay_instance_t* app, int idx, char* rem)
417 {
418 void* item[4];
419 GtkListStore *list_store;
420
421 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
422
423 item[0] = (void*)app;
424 item[1] = (void*)list_store;
425 item[2] = (void*)idx;
426 item[3] = (void*)rem;
427
428 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_rem_proc, item);
429 };