basic playlist draw
[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 /* setup item */
78 tc2frames(IN, 25.0, &items[count].in);
79 tc2frames(DUR, 25.0, &items[count].dur);
80 strncpy(items[count].id, ID, PATH_MAX);
81 items[count].player = atol(CH) - 1;
82 switch(atol(B))
83 {
84 case 1: items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE; break;
85 case 2: items[count].type = OMNPLAY_PLAYLIST_ITEM_LOOP_BEGIN; break;
86 case 3: items[count].type = OMNPLAY_PLAYLIST_ITEM_LOOP_BODY; break;
87 case 4: items[count].type = OMNPLAY_PLAYLIST_ITEM_LOOP_END; break;
88 case 6: items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_END; break;
89 case 0:
90 if(!count || items[count - 1].type != OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY)
91 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
92 else
93 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY;
94 break;
95 };
96
97 count++;
98 }
99 };
100 }
101
102 fclose(f);
103 }
104
105 /* add loaded items to playlist */
106 if(count)
107 {
108 pthread_mutex_lock(&app->playlist.lock);
109 for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
110 app->playlist.item[app->playlist.count++] = items[i];
111 app->playlist.ver_curr++;
112 pthread_mutex_unlock(&app->playlist.lock);
113 }
114
115 /* free data */
116 free(items);
117 free(ID);
118 free(CH);
119 free(IN);
120 free(OUT);
121 free(DUR);
122 free(REST);
123 free(l);
124
125 return count;
126 };
127
128 void omnplay_playlist_load(omnplay_instance_t* app)
129 {
130 int r;
131 GtkWidget *dialog;
132
133 dialog = gtk_file_chooser_dialog_new("Open File",
134 GTK_WINDOW (app->window),
135 GTK_FILE_CHOOSER_ACTION_OPEN,
136 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
137 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
138 NULL);
139
140 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
141 (app->playlist.path)?app->playlist.path:getenv("HOME"));
142
143 r = gtk_dialog_run(GTK_DIALOG(dialog));
144
145 if(r == GTK_RESPONSE_ACCEPT)
146 {
147 char *filename;
148
149 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
150
151 r = load_file_ply(app, filename);
152
153 if(r)
154 omnplay_playlist_draw(app);
155
156 if(app->playlist.path)
157 g_free(app->playlist.path);
158 if((app->playlist.path = filename))
159 {
160 char* e = strrchr(app->playlist.path, '/');
161 if(e) *e = 0;
162 }
163 }
164
165 gtk_widget_destroy (dialog);
166 };
167
168 void omnplay_playlist_save(omnplay_instance_t* app)
169 {
170 };
171
172 void omnplay_playlist_draw(omnplay_instance_t* app)
173 {
174 int i;
175 char tc1[12], tc2[12];
176 GtkListStore *list_store;
177 GtkTreeIter iter;
178
179 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
180 gtk_list_store_clear(list_store);
181
182 pthread_mutex_lock(&app->playlist.lock);
183
184 for(i = 0;i < app->playlist.count; i++)
185 {
186 gtk_list_store_append(list_store, &iter);
187
188 gtk_list_store_set(list_store, &iter,
189 0, "",
190 1, app->playlist.block_icons[app->playlist.item[i].type],
191 2, (0 == app->playlist.item[i].player)?"A":"B",
192 3, app->playlist.item[i].id,
193 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
194 5, frames2tc(app->playlist.item[i].in, 25.0, tc2),
195 6, app->playlist.item[i].title,
196 -1 );
197 }
198
199 app->playlist.ver_prev = app->playlist.ver_curr;
200
201 pthread_mutex_unlock(&app->playlist.lock);
202 };