rework a remain timecode display
[omnplay] / src / omnplay.cpp
1 /*
2 * omnplay.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 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE
22 #endif
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <pthread.h>
34
35 #include "omnplay.h"
36 #include "ui.h"
37 #include "opts.h"
38 #include "timecode.h"
39
40 #include "omplrclnt.h"
41
42 int omnplay_get_content(omnplay_instance_t* app, playlist_item_t *items, int limit,
43 omnplay_get_content_cb_proc proc, void* data)
44 {
45 int r, c = 0;
46 OmPlrClipInfo clip_info;
47 char clip_name[omPlrMaxClipDirLen];
48
49 app->library.id_display_idx = 0;
50
51 pthread_mutex_lock(&app->players.lock);
52
53 r = OmPlrClipGetFirst((OmPlrHandle)app->players.item[0].handle, clip_name, sizeof(clip_name));
54 for(; c < limit && !r;)
55 {
56 /* get clip info */
57 clip_info.maxMsTracks = 0;
58 clip_info.size = sizeof(clip_info);
59
60 r = OmPlrClipGetInfo((OmPlrHandle)app->players.item[0].handle, clip_name, &clip_info);
61
62 if(!r)
63 {
64 /* copy item props */
65 strncpy(items[c].id, clip_name, PATH_MAX);
66 items[c].in = clip_info.firstFrame;
67 items[c].dur = clip_info.lastFrame - clip_info.firstFrame;
68
69 /* callback */
70 pthread_mutex_unlock(&app->players.lock);
71 if(proc)
72 proc(app, &items[c], data);
73 pthread_mutex_lock(&app->players.lock);
74
75 c++;
76 };
77
78 r = OmPlrClipGetNext((OmPlrHandle)app->players.item[0].handle, clip_name, sizeof(clip_name));
79 };
80
81 pthread_mutex_unlock(&app->players.lock);
82
83 return c;
84 };
85
86
87 static gboolean on_main_window_delete_event( GtkWidget *widget, GdkEvent *event, gpointer user_data )
88 {
89 g_print ("delete event occurred [start]\n");
90 gdk_threads_leave();
91 omnplay_release((omnplay_instance_t*)user_data);
92 gdk_threads_enter();
93 g_print ("delete event occurred [finish]\n");
94
95 return FALSE;
96 }
97
98 static void on_main_window_destroy( GtkWidget *widget, gpointer user_data )
99 {
100 g_print ("destroy occurred\n");
101 gtk_main_quit();
102 }
103
104 omnplay_instance_t* omnplay_create(int argc, char** argv)
105 {
106 int i, c;
107 omnplay_instance_t* app;
108
109 /* prepare application instance */
110 app = (omnplay_instance_t*)malloc(sizeof(omnplay_instance_t));
111 memset(app, 0, sizeof(omnplay_instance_t));
112
113 /* load parameters from command line */
114 if(!omnplay_opt(argc, argv, app) && app->players.count)
115 app->window = ui_omnplay(app);
116 else
117 omnplay_usage();
118
119 return app;
120 };
121
122 void omnplay_destroy(omnplay_instance_t* app)
123 {
124 free(app);
125 };
126
127 static int find_index_of_playlist_item(omnplay_instance_t* app, int start, int idx)
128 {
129 if(start < 0 || start >= app->playlist.count)
130 return -1;
131
132 while(1)
133 {
134 if(app->playlist.item[start].omn_idx == idx)
135 return start;
136
137 if(app->playlist.item[start].type & OMNPLAY_PLAYLIST_BLOCK_END)
138 break;
139
140 start++;
141 };
142
143 return -1;
144 };
145
146 static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev , OmPlrStatus *curr)
147 {
148 int idx;
149 char tc_cur[32], tc_rem[32], state[32], status[32];
150 const char *clip;
151
152 if(curr)
153 {
154 frames2tc(curr->pos - curr->minPos, 25.0, tc_cur);
155 frames2tc(curr->maxPos - curr->pos, 25.0, tc_rem);
156 strcpy(status, "ONLINE");
157 clip = curr->currClipName;
158
159 switch(curr->state)
160 {
161 case omPlrStateStopped: strcpy(state, "STOPPED"); break;
162 case omPlrStateCuePlay: strcpy(state, "CUE_PLAY"); break;
163 case omPlrStatePlay: strcpy(state, "PLAY"); break;
164 case omPlrStateCueRecord: strcpy(state, "CUE_RECORD"); break;
165 case omPlrStateRecord: strcpy(state, "RECORD"); break;
166 };
167 }
168 else
169 {
170 tc_cur[0] = 0;
171 tc_rem[0] = 0;
172 clip = "";
173 state[0] = 0;
174 strcpy(status, "OFFLINE");
175 };
176
177 /* update status in status page */
178 gdk_threads_enter();
179 gtk_label_set_text(GTK_LABEL (player->label_tc_cur), tc_cur);
180 gtk_label_set_text(GTK_LABEL (player->label_tc_rem), tc_rem);
181 gtk_label_set_text(GTK_LABEL (player->label_state), state);
182 gtk_label_set_text(GTK_LABEL (player->label_status), status);
183 gtk_label_set_text(GTK_LABEL (player->label_clip), clip);
184 gdk_flush();
185 gdk_threads_leave();
186
187 /* update remaining time */
188 gdk_threads_enter();
189 pthread_mutex_lock(&player->app->playlist.lock);
190 pthread_mutex_lock(&player->app->players.lock);
191
192 /* check if playlist exist */
193 if(player->playlist_length)
194 {
195 /* clear remain on "previous" item */
196 if(curr->currClipNum != prev->currClipNum && 1 != prev->numClips)
197 {
198 tc_rem[0] = 0;
199 idx = find_index_of_playlist_item(player->app, player->playlist_start, prev->currClipNum);
200 if(idx >= 0)
201 omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
202 };
203
204 /* update current item */
205 idx = find_index_of_playlist_item(player->app, player->playlist_start, curr->currClipNum);
206 if(idx >= 0)
207 {
208 /* reset value */
209 tc_rem[0] = 0;
210
211 /* for play and cue calc new value */
212 if(curr->state == omPlrStatePlay || curr->state == omPlrStateCuePlay)
213 frames2tc(curr->currClipStartPos + curr->currClipLen - curr->pos, 25.0, tc_rem);
214
215 /* setup that value */
216 omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
217 };
218 };
219 pthread_mutex_unlock(&player->app->players.lock);
220 pthread_mutex_unlock(&player->app->playlist.lock);
221 gdk_flush();
222 gdk_threads_leave();
223
224
225 memcpy(prev, curr, sizeof(OmPlrStatus));
226 };
227
228 static void* omnplay_thread_proc(void* data)
229 {
230 int r;
231 OmPlrStatus st_curr, st_prev;
232 omnplay_player_t* player = (omnplay_player_t*)data;
233
234 g_warning("omnplay_thread_proc\n");
235
236 memset(&st_curr, 0, sizeof(OmPlrStatus));
237 memset(&st_prev, 0, sizeof(OmPlrStatus));
238
239 /* connect */
240 pthread_mutex_lock(&player->app->players.lock);
241 r = OmPlrOpen(player->host, player->name, (OmPlrHandle*)&player->handle);
242 pthread_mutex_unlock(&player->app->players.lock);
243 if(r)
244 {
245 g_warning("ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
246 player->host, player->name, r);
247
248 return (void*)r;
249 };
250
251 /* setup to do not reconnect */
252 pthread_mutex_lock(&player->app->players.lock);
253 OmPlrSetRetryOpen((OmPlrHandle)player->handle, 0);
254 pthread_mutex_unlock(&player->app->players.lock);
255
256 /* setup directory */
257 if(player->app->players.path[0])
258 {
259 pthread_mutex_lock(&player->app->players.lock);
260 r = OmPlrClipSetDirectory((OmPlrHandle)player->handle, player->app->players.path);
261 pthread_mutex_unlock(&player->app->players.lock);
262
263 if(r)
264 {
265 g_warning("ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
266 player->app->players.path, r);
267
268 pthread_mutex_lock(&player->app->players.lock);
269 OmPlrClose((OmPlrHandle)player->handle);
270 pthread_mutex_unlock(&player->app->players.lock);
271
272 return (void*)r;
273 };
274 };
275
276 /* endless loop */
277 for(r = 0 ; !player->app->f_exit && !r;)
278 {
279 /* sleep */
280 #ifdef _WIN32
281 Sleep(100);
282 #else
283 usleep(100000);
284 #endif
285
286 /* get status */
287 pthread_mutex_lock(&player->app->players.lock);
288 st_curr.size = sizeof(OmPlrStatus);
289 r = OmPlrGetPlayerStatus((OmPlrHandle)player->handle, &st_curr);
290 pthread_mutex_unlock(&player->app->players.lock);
291
292 if(r)
293 g_warning("ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", r);
294 else
295 if(memcmp(&st_curr, &st_prev, sizeof(OmPlrStatus)))
296 omnplay_update_status(player, &st_prev , &st_curr);
297 };
298
299 pthread_mutex_lock(&player->app->players.lock);
300 OmPlrClose((OmPlrHandle)player->handle);
301 pthread_mutex_unlock(&player->app->players.lock);
302
303 return NULL;
304 };
305
306 void get_selected_items_playlist_proc(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
307 {
308 int idx, *list = (int*)data;
309 gtk_tree_model_get(model, iter, 7, &idx, -1);
310 list[list[0] + 1] = idx;
311 list[0] = list[0] + 1;
312 };
313
314 int* omnplay_selected_idxs_playlist(omnplay_instance_t* app)
315 {
316 int* list = NULL;
317 GtkTreeSelection *selection;
318
319 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid));
320 if(selection)
321 {
322 list = (int*)malloc(sizeof(int) * (MAX_PLAYLIST_ITEMS + 1));
323 memset(list, 0, sizeof(int) * (MAX_PLAYLIST_ITEMS + 1));
324
325 gtk_tree_selection_selected_foreach(
326 selection,
327 get_selected_items_playlist_proc,
328 list);
329
330 if(!list[0])
331 {
332 free(list);
333 list = NULL;
334 };
335 };
336
337 return list;
338 };
339
340 static int idx_in_players_range(omnplay_instance_t* app, int idx)
341 {
342 int i, r = 0;
343
344 for(i = 0; i < app->players.count && !r; i++)
345 {
346 int a, b;
347
348 a = app->players.item[i].playlist_start;
349 b = app->players.item[i].playlist_length;
350
351 if(b <= 0)
352 continue;
353
354 b = a + b - 1;
355
356 if(idx >= a && idx <= b) r = 1;
357 };
358
359 return r;
360 };
361
362 static int idxs_in_players_range(omnplay_instance_t* app, int start, int stop)
363 {
364 int i, r = 0;
365
366 for(i = 0; i < app->players.count && !r; i++)
367 {
368 int a, b;
369
370 a = app->players.item[i].playlist_start;
371 b = app->players.item[i].playlist_length;
372
373 if(b <= 0)
374 continue;
375
376 b = a + b - 1;
377
378 #define IN_RANGE(A,B,C) (A <= C && C <= B)
379 if( IN_RANGE(a,b,start) ||
380 IN_RANGE(a,b,stop) ||
381 IN_RANGE(start,stop,a) ||
382 IN_RANGE(start,stop,b))
383 r = 1;
384 };
385
386 return r;
387 };
388
389 static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t button)
390 {
391 int start, stop, r, i;
392 int* list = omnplay_selected_idxs_playlist(app);
393
394 if(!list)
395 return;
396
397 pthread_mutex_lock(&app->playlist.lock);
398 pthread_mutex_lock(&app->players.lock);
399
400 start = list[1];
401 stop = list[list[0]];
402
403 if(!idxs_in_players_range(app, start, stop))
404 {
405 int loop = (button == BUTTON_PLAYLIST_BLOCK_LOOP)?OMNPLAY_PLAYLIST_BLOCK_LOOP:0;
406
407 /* update selected item */
408 for(i = start; i <= stop; i++)
409 {
410 int t = OMNPLAY_PLAYLIST_BLOCK_BODY | loop;
411
412 if(i == start) t |= OMNPLAY_PLAYLIST_BLOCK_BEGIN;
413 if(i == stop) t |= OMNPLAY_PLAYLIST_BLOCK_END;
414
415 app->playlist.item[i].type = (playlist_item_type_t)t;
416
417 omnplay_playlist_draw_item(app, i);
418 };
419
420 /* update border items */
421 if(start && !(app->playlist.item[start - 1].type & OMNPLAY_PLAYLIST_BLOCK_END))
422 {
423 app->playlist.item[start - 1].type = (playlist_item_type_t)(OMNPLAY_PLAYLIST_BLOCK_END
424 | app->playlist.item[start - 1].type);
425 omnplay_playlist_draw_item(app, start - 1);
426 };
427 if((stop + 1) < app->playlist.count && !(app->playlist.item[stop + 1].type & OMNPLAY_PLAYLIST_BLOCK_BEGIN))
428 {
429 app->playlist.item[stop + 1].type = (playlist_item_type_t)(OMNPLAY_PLAYLIST_BLOCK_BEGIN
430 | app->playlist.item[stop + 1].type);
431 omnplay_playlist_draw_item(app, stop + 1);
432 };
433 }
434 else
435 g_warning("omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
436 start, stop);
437
438 pthread_mutex_unlock(&app->players.lock);
439 pthread_mutex_unlock(&app->playlist.lock);
440
441 free(list);
442 };
443
444 static int get_first_selected_item_playlist(omnplay_instance_t* app)
445 {
446 int idx;
447 int* list = omnplay_selected_idxs_playlist(app);
448 if(!list) return -1;
449 idx = list[1];
450 free(list);
451 return idx;
452 };
453
454 static int get_playlist_block(omnplay_instance_t* app, int idx, int* start_ptr, int* stop_ptr)
455 {
456 int start, stop;
457
458 for(start = idx; start >= 0; start--)
459 if(app->playlist.item[start].type & OMNPLAY_PLAYLIST_BLOCK_BEGIN)
460 break;
461
462 for(stop = idx; stop < app->playlist.count; stop++)
463 if(app->playlist.item[stop].type & OMNPLAY_PLAYLIST_BLOCK_END)
464 break;
465
466 g_warning("get_playlist_block: range %d -> %d\n", start, stop);
467
468 /* check block range */
469 if(start >= 0 && stop < app->playlist.count)
470 {
471 *start_ptr = start;
472 *stop_ptr = stop;
473 return (stop - start + 1);
474 };
475
476 return -1;
477 };
478
479 static omnplay_player_t *get_player_at_pos(omnplay_instance_t* app, int pos)
480 {
481 /* check player range */
482 if(app->playlist.item[pos].player > -1 && app->playlist.item[pos].player < app->players.count)
483 return &app->players.item[app->playlist.item[pos].player];
484
485 return NULL;
486 };
487
488 static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, int count, int sel)
489 {
490 int i, j, idx;
491
492 pthread_mutex_lock(&app->playlist.lock);
493 pthread_mutex_lock(&app->players.lock);
494
495 for(j = 0; j < count; j++)
496 {
497 idx = idxs[j] - j;
498
499 /* fix block types */
500 if( app->playlist.item[idx].type != OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY &&
501 app->playlist.item[idx].type != OMNPLAY_PLAYLIST_ITEM_LOOP_BODY)
502 {
503 if(idx)
504 app->playlist.item[idx - 1].type = (playlist_item_type_t)(app->playlist.item[idx - 1].type |
505 OMNPLAY_PLAYLIST_BLOCK_END);
506 if(idx + 1 < app->playlist.count)
507 app->playlist.item[idx + 1].type = (playlist_item_type_t)(app->playlist.item[idx + 1].type |
508 OMNPLAY_PLAYLIST_BLOCK_BEGIN);
509 };
510
511 /* shift playlist items */
512 memmove
513 (
514 &app->playlist.item[idx],
515 &app->playlist.item[idx + 1],
516 (app->playlist.count - idx - 1) * sizeof(playlist_item_t)
517 );
518
519 /* decrement items count */
520 app->playlist.count--;
521
522 /* increment servers indexes */
523 for(i = 0; i < app->players.count; i++)
524 if(app->players.item[i].playlist_start >= idx)
525 app->players.item[i].playlist_start--;
526
527
528 };
529
530 /* redraw playlist */
531 omnplay_playlist_draw(app);
532
533 /* select */
534 if(sel)
535 {
536 GtkTreePath* path;
537 path = gtk_tree_path_new_from_indices(idxs[0], -1);
538 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
539 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
540 gtk_tree_path_free(path);
541 };
542
543 pthread_mutex_unlock(&app->players.lock);
544 pthread_mutex_unlock(&app->playlist.lock);
545 };
546
547 static void omnplay_playlist_item_del(omnplay_instance_t* app)
548 {
549 int i, idx, c;
550 int *list, *list2;
551
552 list = omnplay_selected_idxs_playlist(app);
553 if(!list) return;
554
555 list2 = (int*)malloc(sizeof(int) * list[0]);
556
557 for(i = 0, c = 0; i < list[0]; i++)
558 {
559 /* check for playing block */
560 if(idx_in_players_range(app, list[i + 1]))
561 continue;
562
563 /* save index */
564 list2[c++] = list[i + 1];
565 };
566
567 if(c)
568 omnplay_playlist_delete_items(app, list2, c, 1);
569
570 free(list2);
571 free(list);
572 };
573
574 static int omnplay_playlist_insert_check(omnplay_instance_t* app, int idx, playlist_item_type_t* t)
575 {
576 *t = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
577
578 /* before or after playlist */
579 if(!idx || idx == app->playlist.count)
580 return 1;
581
582 /* check for block borders */
583 if( app->playlist.item[idx - 1].type & OMNPLAY_PLAYLIST_BLOCK_END &&
584 app->playlist.item[idx + 0].type & OMNPLAY_PLAYLIST_BLOCK_BEGIN)
585 return 1;
586
587 /* check for playing block */
588 if(idx_in_players_range(app, idx))
589 return 0;
590
591 if(app->playlist.item[idx].type & OMNPLAY_PLAYLIST_BLOCK_LOOP)
592 *t = OMNPLAY_PLAYLIST_ITEM_LOOP_BODY;
593 else
594 *t = OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY;
595
596 return 1;
597 };
598
599 static void omnplay_playlist_insert_items(omnplay_instance_t* app, int idx,
600 playlist_item_t* items, int count)
601 {
602 int i;
603 GtkTreePath* path;
604
605 pthread_mutex_lock(&app->playlist.lock);
606 pthread_mutex_lock(&app->players.lock);
607
608 /* shift playlist items */
609 memmove
610 (
611 &app->playlist.item[idx + count],
612 &app->playlist.item[idx],
613 (app->playlist.count - idx) * sizeof(playlist_item_t)
614 );
615
616 /* copy new items */
617 memcpy
618 (
619 &app->playlist.item[idx],
620 items,
621 count * sizeof(playlist_item_t)
622 );
623
624 /* increment servers indexes */
625 for(i = 0; i < app->players.count; i++)
626 if(app->players.item[i].playlist_start >= idx)
627 app->players.item[i].playlist_start += idx;
628
629 /* increment items count */
630 app->playlist.count += count;
631
632 /* redraw playlist */
633 omnplay_playlist_draw(app);
634
635 /* select */
636 path = gtk_tree_path_new_from_indices(idx, -1);
637 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
638 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
639 gtk_tree_path_free(path);
640
641 pthread_mutex_unlock(&app->players.lock);
642 pthread_mutex_unlock(&app->playlist.lock);
643 };
644
645 static void omnplay_playlist_item_add(omnplay_instance_t* app, int after)
646 {
647 int idx;
648 playlist_item_t item;
649 playlist_item_type_t t;
650
651 /* find insert position */
652 idx = get_first_selected_item_playlist(app);
653 if(idx < 0)
654 idx = 0;
655 else
656 idx += (after)?1:0;
657
658 if(!omnplay_playlist_insert_check(app, idx, &t))
659 return;
660
661 g_warning("allowed insert into idx=%d\n", idx);
662
663 /* clear item */
664 memset(&item, 0, sizeof(playlist_item_t));
665 if(ui_playlist_item_dialog(app, &item))
666 {
667 omnplay_library_normalize_item(app, &item);
668 item.type = t;
669 omnplay_playlist_insert_items(app, idx, &item, 1);
670 };
671 };
672
673 static void omnplay_playlist_item_edit(omnplay_instance_t* app)
674 {
675 int idx;
676 playlist_item_t item;
677
678 /* find insert position */
679 idx = get_first_selected_item_playlist(app);
680
681 if(idx < 0)
682 return;
683
684 /* check for playing block */
685 if(idx_in_players_range(app, idx))
686 return;
687
688 item = app->playlist.item[idx];
689
690 if(ui_playlist_item_dialog(app, &item))
691 {
692 omnplay_library_normalize_item(app, &item);
693 app->playlist.item[idx] = item;
694 omnplay_playlist_draw_item(app, idx);
695 };
696 };
697
698 static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
699 {
700 int i, r;
701 int idx, start, stop;
702 omnplay_player_t *player;
703
704 pthread_mutex_lock(&app->playlist.lock);
705
706 idx = get_first_selected_item_playlist(app);
707
708 if(idx < 0)
709 {
710 pthread_mutex_unlock(&app->playlist.lock);
711 return;
712 };
713
714 g_warning("cue: selected item is %d\n", idx);
715
716 if(get_playlist_block(app, idx, &start, &stop) < 0)
717 {
718 pthread_mutex_unlock(&app->playlist.lock);
719 return;
720 };
721
722 g_warning("cue: range %d -> %d\n", start, stop);
723
724 player = get_player_at_pos(app, start);
725
726 if(!player)
727 {
728 pthread_mutex_unlock(&app->playlist.lock);
729 return;
730 };
731
732 pthread_mutex_lock(&app->players.lock);
733
734 if(BUTTON_PLAYER_STOP == button || BUTTON_PLAYER_CUE == button)
735 {
736 /* stop */
737 OmPlrStop((OmPlrHandle)player->handle);
738
739 /* detach previous clips */
740 player->playlist_length = -1;
741 OmPlrDetachAllClips((OmPlrHandle)player->handle);
742 };
743
744 if(BUTTON_PLAYER_CUE == button)
745 {
746 int o, c, p = 0;
747
748 /* Attach clips to timeline */
749 for(i = start, c = 0, o = 0; i <= stop; i++)
750 {
751 OmPlrClipInfo clip;
752
753 /* get clip info */
754 clip.maxMsTracks = 0;
755 clip.size = sizeof(clip);
756 r = OmPlrClipGetInfo((OmPlrHandle)player->handle, app->playlist.item[i].id, &clip);
757
758 if(!r)
759 {
760 unsigned int l;
761
762 g_warning("OmPlrClipGetInfo(%s): firstFrame=%d, lastFrame=%d\n",
763 app->playlist.item[i].id, clip.firstFrame, clip.lastFrame);
764
765 /* should we fix playlist clip timings */
766 if(!(
767 app->playlist.item[i].in >= clip.firstFrame &&
768 app->playlist.item[i].in + app->playlist.item[i].dur <= clip.lastFrame) ||
769 !app->playlist.item[i].dur)
770 {
771 g_warning("cue: item [%s] will be updated [%d;%d]->[%d;%d]\n",
772 app->playlist.item[i].id,
773 app->playlist.item[i].in, app->playlist.item[i].dur,
774 clip.firstFrame, clip.lastFrame - clip.firstFrame);
775
776 app->playlist.item[i].in = clip.firstFrame;
777 app->playlist.item[i].dur = clip.lastFrame - clip.firstFrame;
778 omnplay_playlist_draw_item(app, i);
779 };
780
781 r = OmPlrAttach((OmPlrHandle)player->handle,
782 app->playlist.item[i].id,
783 app->playlist.item[i].in,
784 app->playlist.item[i].in + app->playlist.item[i].dur,
785 0, omPlrShiftModeAfter, &l);
786 };
787
788 if(r)
789 {
790 g_warning("cue: failed with %d, %s\n", r, OmPlrGetErrorString((OmPlrError)r));
791 app->playlist.item[i].omn_idx = -1;
792 app->playlist.item[i].omn_offset = -1;
793 app->playlist.item[i].error |= PLAYLIST_ITEM_ERROR_CUE;
794 }
795 else
796 {
797 app->playlist.item[i].omn_idx = c;
798 app->playlist.item[i].omn_offset = o;
799 app->playlist.item[i].error &= 0xF ^ PLAYLIST_ITEM_ERROR_CUE;
800
801 /* save selected item offset */
802 if(i == idx) p = o;
803
804 c++;
805 o += app->playlist.item[i].dur;
806 };
807 };
808
809 if(c)
810 {
811 OmPlrStatus hs;
812
813 /* Set timeline min/max */
814 OmPlrSetMinPosMin((OmPlrHandle)player->handle);
815 OmPlrSetMaxPosMax((OmPlrHandle)player->handle);
816
817 /* Set timeline position */
818 hs.minPos = 0;
819 hs.size = sizeof(OmPlrStatus);
820 OmPlrGetPlayerStatus((OmPlrHandle)player->handle, &hs);
821 OmPlrSetPos((OmPlrHandle)player->handle, hs.minPos + p);
822
823 /* setup loop */
824 if(app->playlist.item[start].type & OMNPLAY_PLAYLIST_BLOCK_LOOP)
825 OmPlrLoop((OmPlrHandle)player->handle, hs.minPos, hs.maxPos);
826 else
827 OmPlrLoop((OmPlrHandle)player->handle, hs.minPos, hs.minPos);
828
829 player->playlist_start = start;
830 player->playlist_length = stop - start + 1;
831
832 /* Cue */
833 OmPlrCuePlay((OmPlrHandle)player->handle, 0.0);
834 };
835 };
836
837 if(BUTTON_PLAYER_PLAY == button)
838 {
839 /* play */
840 OmPlrPlay((OmPlrHandle)player->handle, 1.0);
841 };
842
843 if(BUTTON_PLAYER_PAUSE == button)
844 /* pause */
845 OmPlrPlay((OmPlrHandle)player->handle, 0.0);
846
847 pthread_mutex_unlock(&app->players.lock);
848
849 pthread_mutex_unlock(&app->playlist.lock);
850 };
851
852 static void omnplay_playlist_item_swap(omnplay_instance_t* app, int dir)
853 {
854 int sel, a, b, e = 1;
855 GtkTreePath* path;
856 playlist_item_t item;
857
858 /* find insert position */
859 sel = get_first_selected_item_playlist(app);
860 if(sel < 0)
861 return;
862
863 if(dir < 0)
864 {
865 a = sel - 1;
866 b = sel;
867 sel = a;
868 }
869 else
870 {
871 a = sel;
872 b = sel + 1;
873 sel = b;
874 };
875
876 /* check for playing block */
877 if(idx_in_players_range(app, a) || idx_in_players_range(app, b))
878 return;
879
880 pthread_mutex_lock(&app->playlist.lock);
881 pthread_mutex_lock(&app->players.lock);
882
883 /* swap */
884 item = app->playlist.item[a];
885 app->playlist.item[a] = app->playlist.item[b];
886 app->playlist.item[b] = item;
887
888 /* rewite type */
889 if(app->playlist.item[a].type != app->playlist.item[b].type)
890 {
891 e = 0;
892 app->playlist.item[a].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
893 app->playlist.item[b].type = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
894 };
895
896 /* redraw main items */
897 omnplay_playlist_draw_item(app, a);
898 omnplay_playlist_draw_item(app, b);
899
900 /* fix block types */
901 if(a && !e)
902 {
903 app->playlist.item[a - 1].type = (playlist_item_type_t)(app->playlist.item[a - 1].type |
904 OMNPLAY_PLAYLIST_BLOCK_END);
905 omnplay_playlist_draw_item(app, a - 1);
906 };
907 if(b + 1 < app->playlist.count && !e)
908 {
909 app->playlist.item[b + 1].type = (playlist_item_type_t)(app->playlist.item[b + 1].type |
910 OMNPLAY_PLAYLIST_BLOCK_BEGIN);
911 omnplay_playlist_draw_item(app, b + 1);
912 };
913
914 /* select */
915 path = gtk_tree_path_new_from_indices(sel, -1);
916 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
917 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
918 gtk_tree_path_free(path);
919
920 pthread_mutex_unlock(&app->players.lock);
921 pthread_mutex_unlock(&app->playlist.lock);
922 };
923
924 static void omnplay_library_add(omnplay_instance_t* app, int after)
925 {
926 int idx, c, i;
927 playlist_item_t* items;
928 playlist_item_type_t t;
929
930 /* find insert position */
931 idx = get_first_selected_item_playlist(app);
932 if(idx < 0)
933 idx = 0;
934 else
935 idx += (after)?1:0;
936
937 if(!omnplay_playlist_insert_check(app, idx, &t))
938 return;
939
940 items = omnplay_library_get_selected(app, &c);
941
942 /* clear item */
943 if(items)
944 {
945 for(i = 0; i < c; i++)
946 {
947 items[i].type = t;
948 items[i].error = 0;
949 };
950 omnplay_playlist_insert_items(app, idx, items, c);
951 };
952 };
953
954
955 static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t button)
956 {
957 switch(button)
958 {
959 case BUTTON_PLAYLIST_ITEM_ADD:
960 omnplay_playlist_item_add(app, 0);
961 break;
962 case BUTTON_PLAYLIST_ITEM_DEL:
963 omnplay_playlist_item_del(app);
964 break;
965 case BUTTON_PLAYLIST_ITEM_EDIT:
966 omnplay_playlist_item_edit(app);
967 break;
968 case BUTTON_PLAYLIST_LOAD:
969 omnplay_playlist_load(app);
970 break;
971 case BUTTON_PLAYLIST_SAVE:
972 omnplay_playlist_save(app);
973 break;
974 case BUTTON_PLAYLIST_BLOCK_SINGLE:
975 case BUTTON_PLAYLIST_BLOCK_LOOP:
976 omnplay_playlist_block(app, button);
977 break;
978 case BUTTON_PLAYLIST_ITEM_UP:
979 omnplay_playlist_item_swap(app, -1);
980 break;
981 case BUTTON_PLAYLIST_ITEM_DOWN:
982 omnplay_playlist_item_swap(app, +1);
983 break;
984 case BUTTON_PLAYER_CUE:
985 case BUTTON_PLAYER_PLAY:
986 case BUTTON_PLAYER_PAUSE:
987 case BUTTON_PLAYER_STOP:
988 omnplay_ctl(app, button);
989 break;
990 case BUTTON_LIBRARY_ADD:
991 omnplay_library_add(app, 0);
992 break;
993 case BUTTON_LIBRARY_REFRESH:
994 omnplay_library_refresh(app);
995 break;
996 case BUTTON_LIBRARY_FIND:
997 omnplay_library_search(app, 0);
998 break;
999 case BUTTON_LIBRARY_FIND_NEXT:
1000 omnplay_library_search(app, 1);
1001 break;
1002 };
1003
1004 return TRUE;
1005 };
1006
1007 static gboolean on_button_click(GtkWidget *button, gpointer user_data)
1008 {
1009 int i;
1010 omnplay_instance_t* app = (omnplay_instance_t*)user_data;
1011
1012 for(i = 1; i < BUTTON_LAST; i++)
1013 if(app->buttons[i] == button)
1014 return omnplay_button_click(app, (control_buttons_t)i);
1015
1016 return FALSE;
1017 };
1018
1019 static void omnplay_playlist_item_copy(omnplay_instance_t* app)
1020 {
1021 int *list, i;
1022
1023 list = omnplay_selected_idxs_playlist(app);
1024 if(!list) return;
1025
1026 for(i = 0; i < list[0]; i++)
1027 app->clipboard.item[i] = app->playlist.item[list[i + 1]];
1028 app->clipboard.count = list[0];
1029
1030 free(list);
1031 };
1032
1033 static void omnplay_playlist_item_paste(omnplay_instance_t* app, int after)
1034 {
1035 int idx, i;
1036 playlist_item_t* items;
1037 playlist_item_type_t t;
1038
1039 /* find insert position */
1040 idx = get_first_selected_item_playlist(app);
1041 if(idx < 0)
1042 idx = 0;
1043 else
1044 idx += (after)?1:0;
1045
1046 if(!omnplay_playlist_insert_check(app, idx, &t))
1047 return;
1048
1049 /* clear item */
1050 if(app->clipboard.count)
1051 {
1052 for(i = 0; i < app->clipboard.count; i++)
1053 {
1054 app->clipboard.item[i].type = t;
1055 app->clipboard.item[i].error = 0;
1056 };
1057 omnplay_playlist_insert_items(app, idx, app->clipboard.item, app->clipboard.count);
1058 };
1059 };
1060
1061 static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
1062 {
1063 omnplay_instance_t* app = (omnplay_instance_t*)data;
1064
1065 switch(event->keyval)
1066 {
1067 case GDK_C:
1068 case GDK_c:
1069 if(event->state & GDK_CONTROL_MASK)
1070 {
1071 omnplay_playlist_item_copy(app);
1072 return TRUE;
1073 };
1074 break;
1075 case GDK_V:
1076 case GDK_v:
1077 if(event->state & GDK_CONTROL_MASK)
1078 {
1079 omnplay_playlist_item_paste(app, 0);
1080 return TRUE;
1081 };
1082 break;
1083 case GDK_X:
1084 case GDK_x:
1085 if(event->state & GDK_CONTROL_MASK)
1086 {
1087 omnplay_playlist_item_copy(app);
1088 omnplay_playlist_item_del(app);
1089 return TRUE;
1090 };
1091 break;
1092 case GDK_S:
1093 case GDK_s:
1094 if(event->state & GDK_CONTROL_MASK)
1095 {
1096 omnplay_playlist_save(app);
1097 return TRUE;
1098 };
1099 break;
1100 case GDK_O:
1101 case GDK_o:
1102 if(event->state & GDK_CONTROL_MASK)
1103 {
1104 omnplay_playlist_load(app);
1105 return TRUE;
1106 };
1107 break;
1108 case GDK_KEY_uparrow:
1109 if(event->state & GDK_CONTROL_MASK)
1110 {
1111 omnplay_playlist_item_swap(app, -1);
1112 return TRUE;
1113 };
1114 break;
1115 case GDK_KEY_downarrow:
1116 if(event->state & GDK_CONTROL_MASK)
1117 {
1118 omnplay_playlist_item_swap(app, -1);
1119 return TRUE;
1120 };
1121 break;
1122 case GDK_KEY_space:
1123 omnplay_ctl(app, BUTTON_PLAYER_PLAY);
1124 return TRUE;
1125 case GDK_KEY_Return:
1126 omnplay_ctl(app, BUTTON_PLAYER_CUE);
1127 return TRUE;
1128 case GDK_KEY_Insert:
1129 omnplay_playlist_item_add(app, 0);
1130 return TRUE;
1131 case GDK_KEY_Delete:
1132 omnplay_playlist_item_del(app);
1133 return TRUE;
1134 case GDK_E:
1135 case GDK_e:
1136 omnplay_playlist_item_edit(app);
1137 return TRUE;
1138 };
1139
1140 return FALSE;
1141 };
1142
1143 static gboolean on_library_grid_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
1144 {
1145 omnplay_instance_t* app = (omnplay_instance_t*)data;
1146
1147 switch(event->keyval)
1148 {
1149 case GDK_C:
1150 case GDK_c:
1151 if(event->state & GDK_CONTROL_MASK)
1152 {
1153 int count;
1154 playlist_item_t* items;
1155
1156 items = omnplay_library_get_selected(app, &count);
1157
1158 if(items)
1159 {
1160 int i;
1161
1162 for(i = 0; i < count; i++)
1163 app->clipboard.item[i] = items[i];
1164
1165 app->clipboard.count = count;
1166 };
1167
1168 return TRUE;
1169 };
1170 break;
1171 case GDK_V:
1172 case GDK_v:
1173 if(event->state & GDK_CONTROL_MASK)
1174 {
1175 g_warning("CTRL+v\n");
1176 return TRUE;
1177 };
1178 break;
1179 case GDK_X:
1180 case GDK_x:
1181 if(event->state & GDK_CONTROL_MASK)
1182 {
1183 g_warning("CTRL+x\n");
1184 return TRUE;
1185 };
1186 break;
1187 case GDK_KEY_BackSpace:
1188 omnplay_library_add(app, 0);
1189 return TRUE;
1190 case GDK_KEY_F5:
1191 omnplay_library_refresh(app);
1192 return TRUE;
1193 };
1194
1195 return FALSE;
1196 };
1197
1198 static gboolean on_library_grid_button(GtkWidget *widget, GdkEventButton *event, gpointer data)
1199 {
1200 // g_warning("on_library_grid_button: event->button=%d, event->type=%d", event->button, event->type);
1201
1202 if(event->button==1 && event->type==GDK_2BUTTON_PRESS)
1203 {
1204 omnplay_library_add((omnplay_instance_t* )data, 0);
1205 return TRUE;
1206 };
1207
1208 return FALSE;
1209 };
1210
1211 static gboolean on_playlist_grid_button(GtkWidget *widget, GdkEventButton *event, gpointer data)
1212 {
1213 omnplay_instance_t* app = (omnplay_instance_t*)data;
1214
1215 // g_warning("on_playlist_grid_button");
1216
1217 if(event->button==1 && event->type==GDK_2BUTTON_PRESS)
1218 {
1219 omnplay_playlist_item_edit(app);
1220 return TRUE;
1221 };
1222
1223 return FALSE;
1224 };
1225
1226
1227
1228 static void library_grid_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
1229 GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
1230 {
1231 int c;
1232 playlist_item_t* items;
1233 omnplay_instance_t* app = (omnplay_instance_t*)userdata;
1234
1235 g_warning("library_grid_drag_data_get_cb");
1236
1237 items = omnplay_library_get_selected(app, &c);
1238
1239 /* clear item */
1240 if(items)
1241 {
1242 gtk_selection_data_set(selection_data, selection_data->target, 8,
1243 (const guchar *)items, sizeof(playlist_item_t) * c);
1244 free(items);
1245 };
1246 };
1247
1248 static void playlist_grid_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
1249 GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
1250 {
1251 int *list, i;
1252 playlist_item_t* items;
1253 omnplay_instance_t* app = (omnplay_instance_t*)userdata;
1254
1255 list = omnplay_selected_idxs_playlist(app);
1256 if(!list) return;
1257
1258 /* clear delete flag */
1259 for(i = 0; i < app->playlist.count; i++)
1260 app->playlist.item[i].del = 0;
1261
1262 items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * list[0]);
1263 for(i = 0; i < list[0]; i++)
1264 {
1265 items[i] = app->playlist.item[list[i + 1]];
1266 if(context->action == GDK_ACTION_MOVE)
1267 app->playlist.item[list[i + 1]].del = 1;
1268 }
1269 gtk_selection_data_set(selection_data, selection_data->target, 8,
1270 (const guchar *)items, sizeof(playlist_item_t) * list[0]);
1271
1272 free(items);
1273 free(list);
1274 };
1275
1276 static void library_grid_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
1277 {
1278 g_warning("library_grid_drag_begin_cb");
1279 gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
1280 };
1281
1282 static void playlist_grid_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
1283 {
1284 g_warning("playlist_grid_drag_begin_cb");
1285 gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
1286 };
1287
1288 static void playlist_grid_drag_data_received(GtkWidget *widget, GdkDragContext *context,
1289 gint x, gint y, GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
1290 {
1291 int c, i, idx;
1292 playlist_item_type_t t;
1293 playlist_item_t* items;
1294 GtkTreePath *path = NULL;
1295 omnplay_instance_t* app = (omnplay_instance_t*)userdata;
1296
1297 g_warning("playlist_grid_drag_data_received: context->action=%d", context->action);
1298
1299 items = (playlist_item_t*)gtk_selection_data_get_data(selection_data);
1300 c = gtk_selection_data_get_length(selection_data);
1301
1302 if(c % sizeof(playlist_item_t))
1303 {
1304 g_warning("playlist_grid_drag_data_received: ODD ITEMS");
1305 }
1306 else
1307 {
1308 c /= sizeof(playlist_item_t);
1309
1310 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y, &path, NULL, NULL, NULL))
1311 {
1312 idx = gtk_tree_path_get_indices(path)[0];
1313 gtk_tree_path_free(path);
1314
1315 g_warning("playlist_grid_drag_data_received: gtk_tree_path_get_indice[0]=%d", idx);
1316
1317 /* normalize, FIX ME */
1318 idx--; if(idx < 0) idx = 0;
1319 }
1320 else
1321 idx = app->playlist.count;
1322
1323 g_warning("playlist_grid_drag_data_received: idx=%d", idx);
1324
1325 if(omnplay_playlist_insert_check(app, idx, &t))
1326 {
1327 for(i = 0; i < c; i++)
1328 {
1329 items[i].type = t;
1330 items[i].error = 0;
1331 };
1332 omnplay_playlist_insert_items(app, idx, items, c);
1333 };
1334 };
1335
1336 /* Finish the drag */
1337 gtk_drag_finish(context, TRUE, FALSE, time);
1338 };
1339
1340 static void playlist_grid_drag_data_delete(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
1341 {
1342 int c, i, *list;
1343 omnplay_instance_t* app = (omnplay_instance_t*)userdata;
1344
1345 g_warning("playlist_grid_drag_data_delete");
1346
1347 list = (int*)malloc(sizeof(int) * MAX_PLAYLIST_ITEMS);
1348
1349 for(i = 0, c = 0; i < app->playlist.count; i++)
1350 if(app->playlist.item[i].del)
1351 if(!idx_in_players_range(app, i))
1352 {
1353 /* save index */
1354 list[c++] = i;
1355 g_warning("playlist_grid_drag_data_delete: i=%d, c=%d", i, c);
1356 };
1357
1358 if(c)
1359 omnplay_playlist_delete_items(app, list, c, 0);
1360
1361 free(list);
1362 };
1363
1364 /*
1365 * http://www.mail-archive.com/mahogany-users@lists.sourceforge.net/msg00286.html
1366 */
1367 static gboolean playlist_grid_drag_motion(GtkWidget *widget, GdkDragContext *context,
1368 gint x, gint y, guint time, gpointer data)
1369 {
1370 gboolean same;
1371 GtkWidget *source_widget;
1372
1373 g_warning("playlist_grid_drag_motion");
1374
1375 /* Get source widget and check if it is the same as the
1376 * destination widget.
1377 */
1378 source_widget = gtk_drag_get_source_widget(context);
1379 same = ((source_widget == widget) ? TRUE : FALSE);
1380
1381 /* Put additional checks here, perhaps if same is FALSE then
1382 * set the default drag to GDK_ACTION_COPY.
1383 */
1384
1385 /* Say we just want to allow GDK_ACTION_MOVE, first we check
1386 * if that is in the list of allowed actions on the dc. If
1387 * so then we set it to that. Note if the user holds down the
1388 * ctrl key then the only flag in dc->actions will be
1389 * GDK_ACTION_COPY. The constraint for dc->actions is that
1390 * specified from the given actions in gtk_drag_dest_set() and
1391 * gtk_drag_source_set().
1392 */
1393 if(same)
1394 {
1395 if(context->actions == GDK_ACTION_MOVE)
1396 gdk_drag_status(context, GDK_ACTION_COPY, time);
1397 else
1398 gdk_drag_status(context, GDK_ACTION_MOVE, time);
1399 }
1400 else
1401 gdk_drag_status(context, context->actions, time);
1402
1403 return(TRUE);
1404 }
1405
1406 void omnplay_init(omnplay_instance_t* app)
1407 {
1408 int i;
1409 pthread_mutexattr_t attr;
1410
1411 pthread_mutexattr_init(&attr);
1412 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
1413
1414 gtk_signal_connect( GTK_OBJECT( app->window ), "delete-event",
1415 GTK_SIGNAL_FUNC(on_main_window_delete_event), app);
1416
1417 gtk_signal_connect( GTK_OBJECT( app->window ), "destroy",
1418 GTK_SIGNAL_FUNC(on_main_window_destroy), app);
1419
1420 gtk_widget_add_events(app->playlist_grid, GDK_BUTTON_PRESS_MASK);
1421 gtk_widget_add_events(app->playlist_grid, GDK_KEY_PRESS_MASK);
1422 gtk_signal_connect(GTK_OBJECT(app->playlist_grid), "key-press-event",
1423 GTK_SIGNAL_FUNC(on_playlist_grid_key), app);
1424
1425 gtk_widget_add_events(app->library_grid, GDK_BUTTON_PRESS_MASK);
1426 gtk_widget_add_events(app->library_grid, GDK_KEY_PRESS_MASK);
1427 gtk_signal_connect(GTK_OBJECT(app->library_grid), "key-press-event",
1428 GTK_SIGNAL_FUNC(on_library_grid_key), app);
1429
1430 gtk_signal_connect(GTK_OBJECT(app->playlist_grid), "button-press-event",
1431 GTK_SIGNAL_FUNC(on_playlist_grid_button), app);
1432
1433 gtk_signal_connect(GTK_OBJECT(app->library_grid), "button-press-event",
1434 GTK_SIGNAL_FUNC(on_library_grid_button), app);
1435
1436 /* create lock */
1437 pthread_mutex_init(&app->players.lock, &attr);
1438 pthread_mutex_init(&app->playlist.lock, &attr);
1439 pthread_mutex_init(&app->library.lock, &attr);
1440
1441 /* create a omneon status thread */
1442 for(i = 0; i < app->players.count; i++)
1443 app->players.item[i].thread = g_thread_create(
1444 omnplay_thread_proc, &app->players.item[i], TRUE, NULL);
1445
1446 /* attach buttons click */
1447 for(i = 1; i < BUTTON_LAST; i++)
1448 gtk_signal_connect(GTK_OBJECT(app->buttons[i]), "clicked",
1449 GTK_SIGNAL_FUNC( on_button_click), app );
1450
1451 /* load library */
1452 omnplay_library_load(app);
1453
1454 pthread_mutexattr_destroy(&attr);
1455
1456 /* setup drag n drop source/target */
1457 static GtkTargetEntry drag_targets[] = { { (char*) "application/playlist_item_t", 0, 0 } };
1458
1459 gtk_drag_source_set(app->library_grid, GDK_BUTTON1_MASK,
1460 drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY));
1461
1462 gtk_drag_source_set(app->playlist_grid, GDK_BUTTON1_MASK,
1463 drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
1464
1465 gtk_drag_dest_set(app->playlist_grid, (GtkDestDefaults)(GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
1466 drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
1467
1468 g_signal_connect (app->library_grid, "drag_data_get", G_CALLBACK(library_grid_drag_data_get_cb), app);
1469 g_signal_connect (app->playlist_grid, "drag_data_get", G_CALLBACK(playlist_grid_drag_data_get_cb), app);
1470 g_signal_connect (app->library_grid, "drag_begin", G_CALLBACK(library_grid_drag_begin_cb), app);
1471 g_signal_connect (app->playlist_grid, "drag_begin", G_CALLBACK(playlist_grid_drag_begin_cb), app);
1472 g_signal_connect (app->playlist_grid, "drag_data_received", G_CALLBACK (playlist_grid_drag_data_received), app);
1473 g_signal_connect (app->playlist_grid, "drag_data_delete", G_CALLBACK (playlist_grid_drag_data_delete), app);
1474 g_signal_connect (app->playlist_grid, "drag_motion", G_CALLBACK (playlist_grid_drag_motion), app);
1475 };
1476
1477 void omnplay_release(omnplay_instance_t* app)
1478 {
1479 int i;
1480
1481 app->f_exit = 1;
1482
1483 for(i = 0; i < app->players.count; i++)
1484 /* create a omneon status thread */
1485 g_thread_join(app->players.item[i].thread);
1486
1487 /* destroy lock */
1488 pthread_mutex_destroy(&app->players.lock);
1489
1490 /* destroy lock */
1491 pthread_mutex_destroy(&app->playlist.lock);
1492
1493 /* load library */
1494 omnplay_library_save(app);
1495
1496 /* destroy library lock */
1497 pthread_mutex_destroy(&app->library.lock);
1498 };
1499
1500 void omnplay_playlist_normalize(omnplay_instance_t* app)
1501 {
1502 int i;
1503
1504 /* normalize playlist */
1505 for(i = 0; i < app->playlist.count; i++)
1506 if(omnplay_library_normalize_item(app, &app->playlist.item[i]))
1507 omnplay_playlist_draw_item(app, i);
1508 };
1509
1510 void omnplay_set_status(omnplay_instance_t* app, char* str)
1511 {
1512 gdk_threads_enter();
1513 gtk_label_set_text(GTK_LABEL(app->status_label), str);
1514 gdk_flush();
1515 gdk_threads_leave();
1516 };
1517