2 * playlist.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
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.
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.
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.
28 #include <gdk/gdkkeysyms.h>
35 static int load_file_ply(omnplay_instance_t
* app
, char* filename
)
38 char *ID
, *CH
, *B
, *IN
, *OUT
, *DUR
, *REST
, *l
;
40 playlist_item_t
* items
;
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
);
48 IN
= malloc(PATH_MAX
);
49 OUT
= malloc(PATH_MAX
);
50 DUR
= malloc(PATH_MAX
);
51 REST
= malloc(PATH_MAX
);
54 /* open and process file */
55 f
= fopen(filename
, "rt");
63 memset(l
, 0, PATH_MAX
);
64 fgets(l
, PATH_MAX
, f
);
67 if( (s
= strchr(l
, '\n')) ) *s
= 0;
68 if( (s
= strchr(l
, '\r')) ) *s
= 0;
69 if( (s
= strchr(l
, '\t')) ) *s
= 0;
71 /* check for empty line */
72 if(l
[0] && l
[0] != '#')
74 if (6 != sscanf(l
, "%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%128[^,],%s",
75 ID
, CH
, B
, IN
, OUT
, DUR
, REST
))
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;
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;
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
;
97 items
[count
].type
= OMNPLAY_PLAYLIST_ITEM_BLOCK_BEGIN
;
101 items
[count
].type
= b
- 1024;
106 switch(items
[count
].type
)
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;
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
);
130 /* add loaded items to playlist */
133 pthread_mutex_lock(&app
->playlist
.lock
);
134 for(i
= 0; i
< count
&& app
->playlist
.count
+ 1 < MAX_PLAYLIST_ITEMS
; i
++)
136 omnplay_library_normalize_item(app
, &items
[i
]);
137 app
->playlist
.item
[app
->playlist
.count
++] = items
[i
];
139 app
->playlist
.ver_curr
++;
140 pthread_mutex_unlock(&app
->playlist
.lock
);
156 void omnplay_playlist_load(omnplay_instance_t
* app
)
160 GtkFileFilter
*filter
;
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
,
169 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
),
170 (app
->playlist
.path
)?app
->playlist
.path
:getenv("HOME"));
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
);
181 r
= gtk_dialog_run(GTK_DIALOG(dialog
));
183 if(r
== GTK_RESPONSE_ACCEPT
)
187 filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
189 r
= load_file_ply(app
, filename
);
192 omnplay_playlist_draw(app
);
194 if(app
->playlist
.path
)
195 g_free(app
->playlist
.path
);
196 if((app
->playlist
.path
= filename
))
198 char* e
= strrchr(app
->playlist
.path
, '/');
203 gtk_widget_destroy (dialog
);
206 static int save_file_ply(omnplay_instance_t
* app
, char* filename
)
210 char tc1
[12], tc2
[12], tc3
[12];
211 char* fname
= filename
;
213 filename
= (char*)malloc(PATH_MAX
);
214 strncpy(filename
, fname
, PATH_MAX
);
215 i
= strlen(filename
);
216 if(i
< 4 || strcasecmp(filename
+ i
- 4, ".ply"))
217 strcat(filename
, ".ply");
219 if((f
= fopen(filename
, "wt")))
221 for(i
= 0; i
< app
->playlist
.count
; i
++)
222 fprintf(f
, "%s,%d,%d,%s,%s,%s,,,,,,,,\n",
223 app
->playlist
.item
[i
].id
,
224 app
->playlist
.item
[i
].player
+ 1,
225 app
->playlist
.item
[i
].type
+ 1024,
226 frames2tc(app
->playlist
.item
[i
].in
, 25.0, tc1
),
227 frames2tc(app
->playlist
.item
[i
].in
+ app
->playlist
.item
[i
].dur
, 25.0, tc2
),
228 frames2tc(app
->playlist
.item
[i
].dur
, 25.0, tc3
));
236 void omnplay_playlist_save(omnplay_instance_t
* app
)
240 GtkFileFilter
*filter
;
242 dialog
= gtk_file_chooser_dialog_new("Save File",
243 GTK_WINDOW (app
->window
),
244 GTK_FILE_CHOOSER_ACTION_SAVE
,
245 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
246 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
,
249 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog
), TRUE
);
251 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
),
252 (app
->playlist
.path
)?app
->playlist
.path
:getenv("HOME"));
254 filter
= gtk_file_filter_new();
255 gtk_file_filter_set_name(filter
, "Playlist formatted (*.ply)");
256 gtk_file_filter_add_pattern(filter
, "*.ply");
257 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog
), filter
);
258 g_object_set_data(G_OBJECT(filter
), "id", GINT_TO_POINTER(0));
259 filter
= gtk_file_filter_new();
260 gtk_file_filter_set_name(filter
, "Text (*.txt)");
261 gtk_file_filter_add_pattern(filter
, "*.*");
262 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (dialog
), filter
);
263 g_object_set_data(G_OBJECT(filter
), "id", GINT_TO_POINTER(1));
265 r
= gtk_dialog_run(GTK_DIALOG(dialog
));
267 if(r
== GTK_RESPONSE_ACCEPT
)
271 filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
273 r
= GPOINTER_TO_INT(g_object_get_data(G_OBJECT(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog
))), "id"));
275 r
= save_file_ply(app
, filename
);
277 if(app
->playlist
.path
)
278 g_free(app
->playlist
.path
);
279 if((app
->playlist
.path
= filename
))
281 char* e
= strrchr(app
->playlist
.path
, '/');
286 gtk_widget_destroy (dialog
);
290 void omnplay_playlist_draw(omnplay_instance_t
* app
)
294 char tc1
[12], tc2
[12];
295 GtkListStore
*list_store
;
298 sels
= omnplay_selected_idxs_playlist(app
);
300 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->playlist_grid
)));
301 gtk_list_store_clear(list_store
);
303 pthread_mutex_lock(&app
->playlist
.lock
);
305 for(i
= 0;i
< app
->playlist
.count
; i
++)
309 if(OMNPLAY_PLAYLIST_BLOCK_BEGIN
& app
->playlist
.item
[i
].type
)
310 snprintf(ch
, sizeof(ch
), "%c", 'A' + app
->playlist
.item
[i
].player
);
314 gtk_list_store_append(list_store
, &iter
);
316 gtk_list_store_set(list_store
, &iter
,
318 1, app
->playlist
.block_icons
[app
->playlist
.item
[i
].type
],
320 3, app
->playlist
.item
[i
].id
,
321 4, frames2tc(app
->playlist
.item
[i
].in
, 25.0, tc1
),
322 5, frames2tc(app
->playlist
.item
[i
].dur
, 25.0, tc2
),
323 6, app
->playlist
.item
[i
].title
,
325 8, (app
->playlist
.item
[i
].error
!= 0),
326 9, (app
->playlist
.item
[i
].error
& PLAYLIST_ITEM_ERROR_LIB
)?
"red":"orange",
330 app
->playlist
.ver_prev
= app
->playlist
.ver_curr
;
337 path
= gtk_tree_path_new_from_indices(sels
[1], -1);
338 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
)), path
);
339 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app
->playlist_grid
), path
, NULL
, FALSE
);
340 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(app
->playlist_grid
), path
, NULL
, FALSE
, 0, 0);
341 gtk_tree_path_free(path
);
346 pthread_mutex_unlock(&app
->playlist
.lock
);
349 typedef struct omnplay_playlist_draw_item_desc
351 GtkListStore
*list_store
;
352 omnplay_instance_t
* app
;
354 } omnplay_playlist_draw_item_t
;
356 static gboolean
omnplay_playlist_draw_item_proc(
357 GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer user_data
)
360 char tc1
[12], tc2
[12];
362 omnplay_playlist_draw_item_t
* item
= (omnplay_playlist_draw_item_t
*)user_data
;
363 omnplay_instance_t
* app
= item
->app
;
365 gtk_tree_model_get(model
, iter
, 7, &i
, -1);
367 if(i
!= item
->idx
) return FALSE
;
369 if(OMNPLAY_PLAYLIST_BLOCK_BEGIN
& app
->playlist
.item
[i
].type
)
370 snprintf(ch
, sizeof(ch
), "%c", 'A' + app
->playlist
.item
[i
].player
);
374 gtk_list_store_set(item
->list_store
, iter
,
376 1, app
->playlist
.block_icons
[app
->playlist
.item
[i
].type
],
378 3, app
->playlist
.item
[i
].id
,
379 4, frames2tc(app
->playlist
.item
[i
].in
, 25.0, tc1
),
380 5, frames2tc(app
->playlist
.item
[i
].dur
, 25.0, tc2
),
381 6, app
->playlist
.item
[i
].title
,
383 8, (app
->playlist
.item
[i
].error
!= 0),
384 9, (app
->playlist
.item
[i
].error
& PLAYLIST_ITEM_ERROR_LIB
)?
"red":"orange",
390 void omnplay_playlist_draw_item(omnplay_instance_t
* app
, int idx
)
392 GtkListStore
*list_store
;
393 omnplay_playlist_draw_item_t item
;
395 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->playlist_grid
)));
397 pthread_mutex_lock(&app
->playlist
.lock
);
401 item
.list_store
= list_store
;
402 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store
), omnplay_playlist_draw_item_proc
, &item
);
404 pthread_mutex_unlock(&app
->playlist
.lock
);
407 static gboolean
omnplay_playlist_draw_item_rem_proc(
408 GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer user_data
)
411 void** args
= (void**)user_data
;
412 GtkListStore
*list_store
= (GtkListStore
*)args
[1];
413 int idx
= (int)args
[2];
414 char* rem
= (char*)args
[3];
416 gtk_tree_model_get(model
, iter
, 7, &i
, -1);
418 if(i
!= idx
) return FALSE
;
420 gtk_list_store_set(list_store
, iter
, 0, rem
, -1);
425 void omnplay_playlist_draw_item_rem(omnplay_instance_t
* app
, int idx
, char* rem
)
428 GtkListStore
*list_store
;
430 list_store
= GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app
->playlist_grid
)));
432 item
[0] = (void*)app
;
433 item
[1] = (void*)list_store
;
434 item
[2] = (void*)idx
;
435 item
[3] = (void*)rem
;
437 gtk_tree_model_foreach(GTK_TREE_MODEL(list_store
), omnplay_playlist_draw_item_rem_proc
, item
);