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