basic key press add to playlist grid
[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)
91 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
92 else if(items[count - 1].type == OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN ||
93 items[count - 1].type == OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY)
94 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY;
95 else
96 items[count].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN;
97 break;
98 };
99 #if 0
100 {
101 char* n;
102 switch(items[count].type)
103 {
104 case OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN: n = "BLOCK_BEGIN"; break;
105 case OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY: n = "BLOCK_BODY"; break;
106 case OMNPLAY_PLAYLIST_ITEM_BLOCK_END: n = "BLOCK_END"; break;
107 case OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE: n = "BLOCK_SINGLE"; break;
108 case OMNPLAY_PLAYLIST_ITEM_LOOP_BEGIN: n = "LOOP_BEGIN"; break;
109 case OMNPLAY_PLAYLIST_ITEM_LOOP_BODY: n = "LOOP_BODY"; break;
110 case OMNPLAY_PLAYLIST_ITEM_LOOP_END: n = "LOOP_END"; break;
111 case OMNPLAY_PLAYLIST_ITEM_LOOP_SINGLE: n = "LOOP_SINGLE"; break;
112 };
113 fprintf(stderr, "src=[%s]\ndst=[idx=%d,block=%s,block_id=%d,in=%d,out=%d]\n",
114 l, count, n, items[count].type, items[count].in, items[count].dur);
115 };
116 #endif
117
118 count++;
119 }
120 };
121 }
122
123 fclose(f);
124 }
125
126 /* add loaded items to playlist */
127 if(count)
128 {
129 pthread_mutex_lock(&app->playlist.lock);
130 for(i = 0; i < count && app->playlist.count + 1 < MAX_PLAYLIST_ITEMS; i++)
131 app->playlist.item[app->playlist.count++] = items[i];
132 app->playlist.ver_curr++;
133 pthread_mutex_unlock(&app->playlist.lock);
134 }
135
136 /* free data */
137 free(items);
138 free(ID);
139 free(CH);
140 free(IN);
141 free(OUT);
142 free(DUR);
143 free(REST);
144 free(l);
145
146 return count;
147 };
148
149 void omnplay_playlist_load(omnplay_instance_t* app)
150 {
151 int r;
152 GtkWidget *dialog;
153
154 dialog = gtk_file_chooser_dialog_new("Open File",
155 GTK_WINDOW (app->window),
156 GTK_FILE_CHOOSER_ACTION_OPEN,
157 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
158 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
159 NULL);
160
161 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),
162 (app->playlist.path)?app->playlist.path:getenv("HOME"));
163
164 r = gtk_dialog_run(GTK_DIALOG(dialog));
165
166 if(r == GTK_RESPONSE_ACCEPT)
167 {
168 char *filename;
169
170 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
171
172 r = load_file_ply(app, filename);
173
174 if(r)
175 omnplay_playlist_draw(app);
176
177 if(app->playlist.path)
178 g_free(app->playlist.path);
179 if((app->playlist.path = filename))
180 {
181 char* e = strrchr(app->playlist.path, '/');
182 if(e) *e = 0;
183 }
184 }
185
186 gtk_widget_destroy (dialog);
187 };
188
189 void omnplay_playlist_save(omnplay_instance_t* app)
190 {
191 };
192
193 void omnplay_playlist_draw(omnplay_instance_t* app)
194 {
195 int i;
196 char tc1[12], tc2[12];
197 GtkListStore *list_store;
198 GtkTreeIter iter;
199
200 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
201 gtk_list_store_clear(list_store);
202
203 pthread_mutex_lock(&app->playlist.lock);
204
205 for(i = 0;i < app->playlist.count; i++)
206 {
207 gtk_list_store_append(list_store, &iter);
208
209 gtk_list_store_set(list_store, &iter,
210 0, "",
211 1, app->playlist.block_icons[app->playlist.item[i].type],
212 2, (0 == app->playlist.item[i].player)?"A":"B",
213 3, app->playlist.item[i].id,
214 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
215 5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
216 6, app->playlist.item[i].title,
217 7, i,
218 -1 );
219 }
220
221 app->playlist.ver_prev = app->playlist.ver_curr;
222
223 pthread_mutex_unlock(&app->playlist.lock);
224 };
225
226 typedef struct omnplay_playlist_draw_item_desc
227 {
228 GtkListStore *list_store;
229 omnplay_instance_t* app;
230 int idx;
231 } omnplay_playlist_draw_item_t;
232
233 static gboolean omnplay_playlist_draw_item_proc(
234 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
235 {
236 int i;
237 char tc1[12], tc2[12];
238 omnplay_playlist_draw_item_t* item = (omnplay_playlist_draw_item_t*)user_data;
239 omnplay_instance_t* app = item->app;
240
241 gtk_tree_model_get(model, iter, 7, &i, -1);
242
243 if(i != item->idx) return FALSE;
244
245 gtk_list_store_set(item->list_store, iter,
246 0, "",
247 1, app->playlist.block_icons[app->playlist.item[i].type],
248 2, (0 == app->playlist.item[i].player)?"A":"B",
249 3, app->playlist.item[i].id,
250 4, frames2tc(app->playlist.item[i].in, 25.0, tc1),
251 5, frames2tc(app->playlist.item[i].dur, 25.0, tc2),
252 6, app->playlist.item[i].title,
253 7, i,
254 -1 );
255
256 return TRUE;
257 };
258
259 void omnplay_playlist_draw_item(omnplay_instance_t* app, int idx)
260 {
261 GtkListStore *list_store;
262 omnplay_playlist_draw_item_t item;
263
264 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
265
266 pthread_mutex_lock(&app->playlist.lock);
267
268 item.idx = idx;
269 item.app = app;
270 item.list_store = list_store;
271 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_proc, &item);
272
273 pthread_mutex_unlock(&app->playlist.lock);
274 };
275
276 static gboolean omnplay_playlist_draw_item_rem_proc(
277 GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
278 {
279 int i;
280 void** args = (void**)user_data;
281 GtkListStore *list_store = (GtkListStore *)args[1];
282 int idx = (int)args[2];
283 char* rem = (char*)args[3];
284
285 gtk_tree_model_get(model, iter, 7, &i, -1);
286
287 if(i != idx) return FALSE;
288
289 gtk_list_store_set(list_store, iter, 0, rem, -1);
290
291 return TRUE;
292 };
293
294 void omnplay_playlist_draw_item_rem(omnplay_instance_t* app, int idx, char* rem)
295 {
296 void* item[4];
297 GtkListStore *list_store;
298
299 list_store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->playlist_grid)));
300
301 item[0] = (void*)app;
302 item[1] = (void*)list_store;
303 item[2] = (void*)idx;
304 item[3] = (void*)rem;
305
306 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store), omnplay_playlist_draw_item_rem_proc, item);
307 };