2 * omnplay.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.
32 #include <gdk/gdkkeysyms.h>
40 #include "omplrclnt.h"
42 int omnplay_get_content(omnplay_instance_t
* app
, playlist_item_t
*items
, int limit
,
43 omnplay_get_content_cb_proc proc
, void* data
)
46 OmPlrClipInfo clip_info
;
47 char clip_name
[omPlrMaxClipDirLen
];
49 app
->library
.id_display_idx
= 0;
51 pthread_mutex_lock(&app
->players
.lock
);
53 r
= OmPlrClipGetFirst((OmPlrHandle
)app
->players
.item
[0].handle
, clip_name
, sizeof(clip_name
));
54 for(; c
< limit
&& !r
;)
57 clip_info
.maxMsTracks
= 0;
58 clip_info
.size
= sizeof(clip_info
);
60 r
= OmPlrClipGetInfo((OmPlrHandle
)app
->players
.item
[0].handle
, clip_name
, &clip_info
);
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
;
70 pthread_mutex_unlock(&app
->players
.lock
);
72 proc(app
, &items
[c
], data
);
73 pthread_mutex_lock(&app
->players
.lock
);
78 r
= OmPlrClipGetNext((OmPlrHandle
)app
->players
.item
[0].handle
, clip_name
, sizeof(clip_name
));
81 pthread_mutex_unlock(&app
->players
.lock
);
87 static gboolean
on_main_window_delete_event( GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
)
89 g_print ("delete event occurred [start]\n");
91 omnplay_release((omnplay_instance_t
*)user_data
);
93 g_print ("delete event occurred [finish]\n");
98 static void on_main_window_destroy( GtkWidget
*widget
, gpointer user_data
)
100 g_print ("destroy occurred\n");
104 omnplay_instance_t
* omnplay_create(int argc
, char** argv
)
107 omnplay_instance_t
* app
;
109 /* prepare application instance */
110 app
= (omnplay_instance_t
*)malloc(sizeof(omnplay_instance_t
));
111 memset(app
, 0, sizeof(omnplay_instance_t
));
113 /* load parameters from command line */
114 if(!omnplay_opt(argc
, argv
, app
) && app
->players
.count
)
115 app
->window
= ui_omnplay(app
);
122 void omnplay_destroy(omnplay_instance_t
* app
)
127 static int find_index_of_playlist_item(omnplay_instance_t
* app
, int start
, int idx
)
129 if(start
< 0 || start
>= app
->playlist
.count
)
134 if(app
->playlist
.item
[start
].omn_idx
== idx
)
137 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_END
)
146 static void omnplay_update_status(omnplay_player_t
* player
, OmPlrStatus
*prev
, OmPlrStatus
*curr
)
149 char tc_cur
[32], tc_rem
[32], state
[32], status
[32];
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
;
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;
174 strcpy(status
, "OFFLINE");
177 /* update status in status page */
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
);
187 /* update remaining time */
189 pthread_mutex_lock(&player
->app
->playlist
.lock
);
190 pthread_mutex_lock(&player
->app
->players
.lock
);
191 if(curr
->state
== omPlrStatePlay
|| curr
->state
== omPlrStateCuePlay
)
193 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, curr
->currClipNum
);
196 frames2tc(curr
->currClipStartPos
+ curr
->currClipLen
- curr
->pos
, 25.0, tc_rem
);
197 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
199 if(curr
->currClipNum
!= prev
->currClipNum
&& 1 != prev
->numClips
)
202 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, prev
->currClipNum
);
204 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
210 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, curr
->currClipNum
);
212 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
213 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, prev
->currClipNum
);
215 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
217 pthread_mutex_unlock(&player
->app
->players
.lock
);
218 pthread_mutex_unlock(&player
->app
->playlist
.lock
);
223 memcpy(prev
, curr
, sizeof(OmPlrStatus
));
226 static void* omnplay_thread_proc(void* data
)
229 OmPlrStatus st_curr
, st_prev
;
230 omnplay_player_t
* player
= (omnplay_player_t
*)data
;
232 g_warning("omnplay_thread_proc\n");
234 memset(&st_curr
, 0, sizeof(OmPlrStatus
));
235 memset(&st_prev
, 0, sizeof(OmPlrStatus
));
238 pthread_mutex_lock(&player
->app
->players
.lock
);
239 r
= OmPlrOpen(player
->host
, player
->name
, (OmPlrHandle
*)&player
->handle
);
240 pthread_mutex_unlock(&player
->app
->players
.lock
);
243 g_warning("ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
244 player
->host
, player
->name
, r
);
249 /* setup to do not reconnect */
250 pthread_mutex_lock(&player
->app
->players
.lock
);
251 OmPlrSetRetryOpen((OmPlrHandle
)player
->handle
, 0);
252 pthread_mutex_unlock(&player
->app
->players
.lock
);
254 /* setup directory */
255 if(player
->app
->players
.path
[0])
257 pthread_mutex_lock(&player
->app
->players
.lock
);
258 r
= OmPlrClipSetDirectory((OmPlrHandle
)player
->handle
, player
->app
->players
.path
);
259 pthread_mutex_unlock(&player
->app
->players
.lock
);
263 g_warning("ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
264 player
->app
->players
.path
, r
);
266 pthread_mutex_lock(&player
->app
->players
.lock
);
267 OmPlrClose((OmPlrHandle
)player
->handle
);
268 pthread_mutex_unlock(&player
->app
->players
.lock
);
275 for(r
= 0 ; !player
->app
->f_exit
&& !r
;)
285 pthread_mutex_lock(&player
->app
->players
.lock
);
286 st_curr
.size
= sizeof(OmPlrStatus
);
287 r
= OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &st_curr
);
288 pthread_mutex_unlock(&player
->app
->players
.lock
);
291 g_warning("ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", r
);
293 if(memcmp(&st_curr
, &st_prev
, sizeof(OmPlrStatus
)))
294 omnplay_update_status(player
, &st_prev
, &st_curr
);
297 pthread_mutex_lock(&player
->app
->players
.lock
);
298 OmPlrClose((OmPlrHandle
)player
->handle
);
299 pthread_mutex_unlock(&player
->app
->players
.lock
);
304 void get_selected_items_playlist_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
306 int idx
, *list
= (int*)data
;
307 gtk_tree_model_get(model
, iter
, 7, &idx
, -1);
308 list
[list
[0] + 1] = idx
;
309 list
[0] = list
[0] + 1;
312 int* omnplay_selected_idxs_playlist(omnplay_instance_t
* app
)
315 GtkTreeSelection
*selection
;
317 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
));
320 list
= (int*)malloc(sizeof(int) * (MAX_PLAYLIST_ITEMS
+ 1));
321 memset(list
, 0, sizeof(int) * (MAX_PLAYLIST_ITEMS
+ 1));
323 gtk_tree_selection_selected_foreach(
325 get_selected_items_playlist_proc
,
338 static int idx_in_players_range(omnplay_instance_t
* app
, int idx
)
342 for(i
= 0; i
< app
->players
.count
&& !r
; i
++)
346 a
= app
->players
.item
[i
].playlist_start
;
347 b
= app
->players
.item
[i
].playlist_length
;
354 if(idx
>= a
&& idx
<= b
) r
= 1;
360 static int idxs_in_players_range(omnplay_instance_t
* app
, int start
, int stop
)
364 for(i
= 0; i
< app
->players
.count
&& !r
; i
++)
368 a
= app
->players
.item
[i
].playlist_start
;
369 b
= app
->players
.item
[i
].playlist_length
;
376 #define IN_RANGE(A,B,C) (A <= C && C <= B)
377 if( IN_RANGE(a
,b
,start
) ||
378 IN_RANGE(a
,b
,stop
) ||
379 IN_RANGE(start
,stop
,a
) ||
380 IN_RANGE(start
,stop
,b
))
387 static void omnplay_playlist_block(omnplay_instance_t
* app
, control_buttons_t button
)
389 int start
, stop
, r
, i
;
390 int* list
= omnplay_selected_idxs_playlist(app
);
395 pthread_mutex_lock(&app
->playlist
.lock
);
396 pthread_mutex_lock(&app
->players
.lock
);
399 stop
= list
[list
[0]];
401 if(!idxs_in_players_range(app
, start
, stop
))
403 int loop
= (button
== BUTTON_PLAYLIST_BLOCK_LOOP
)?OMNPLAY_PLAYLIST_BLOCK_LOOP
:0;
405 /* update selected item */
406 for(i
= start
; i
<= stop
; i
++)
408 int t
= OMNPLAY_PLAYLIST_BLOCK_BODY
| loop
;
410 if(i
== start
) t
|= OMNPLAY_PLAYLIST_BLOCK_BEGIN
;
411 if(i
== stop
) t
|= OMNPLAY_PLAYLIST_BLOCK_END
;
413 app
->playlist
.item
[i
].type
= (playlist_item_type_t
)t
;
415 omnplay_playlist_draw_item(app
, i
);
418 /* update border items */
419 if(start
&& !(app
->playlist
.item
[start
- 1].type
& OMNPLAY_PLAYLIST_BLOCK_END
))
421 app
->playlist
.item
[start
- 1].type
= (playlist_item_type_t
)(OMNPLAY_PLAYLIST_BLOCK_END
422 | app
->playlist
.item
[start
- 1].type
);
423 omnplay_playlist_draw_item(app
, start
- 1);
425 if((stop
+ 1) < app
->playlist
.count
&& !(app
->playlist
.item
[stop
+ 1].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
))
427 app
->playlist
.item
[stop
+ 1].type
= (playlist_item_type_t
)(OMNPLAY_PLAYLIST_BLOCK_BEGIN
428 | app
->playlist
.item
[stop
+ 1].type
);
429 omnplay_playlist_draw_item(app
, stop
+ 1);
433 g_warning("omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
436 pthread_mutex_unlock(&app
->players
.lock
);
437 pthread_mutex_unlock(&app
->playlist
.lock
);
442 static int get_first_selected_item_playlist(omnplay_instance_t
* app
)
445 int* list
= omnplay_selected_idxs_playlist(app
);
452 static int get_playlist_block(omnplay_instance_t
* app
, int idx
, int* start_ptr
, int* stop_ptr
)
456 for(start
= idx
; start
>= 0; start
--)
457 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
)
460 for(stop
= idx
; stop
< app
->playlist
.count
; stop
++)
461 if(app
->playlist
.item
[stop
].type
& OMNPLAY_PLAYLIST_BLOCK_END
)
464 g_warning("get_playlist_block: range %d -> %d\n", start
, stop
);
466 /* check block range */
467 if(start
>= 0 && stop
< app
->playlist
.count
)
471 return (stop
- start
+ 1);
477 static omnplay_player_t
*get_player_at_pos(omnplay_instance_t
* app
, int pos
)
479 /* check player range */
480 if(app
->playlist
.item
[pos
].player
> -1 && app
->playlist
.item
[pos
].player
< app
->players
.count
)
481 return &app
->players
.item
[app
->playlist
.item
[pos
].player
];
486 static void omnplay_playlist_delete_items(omnplay_instance_t
* app
, int* idxs
, int count
, int sel
)
490 pthread_mutex_lock(&app
->playlist
.lock
);
491 pthread_mutex_lock(&app
->players
.lock
);
493 for(j
= 0; j
< count
; j
++)
497 /* fix block types */
498 if( app
->playlist
.item
[idx
].type
!= OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY
&&
499 app
->playlist
.item
[idx
].type
!= OMNPLAY_PLAYLIST_ITEM_LOOP_BODY
)
502 app
->playlist
.item
[idx
- 1].type
= (playlist_item_type_t
)(app
->playlist
.item
[idx
- 1].type
|
503 OMNPLAY_PLAYLIST_BLOCK_END
);
504 if(idx
+ 1 < app
->playlist
.count
)
505 app
->playlist
.item
[idx
+ 1].type
= (playlist_item_type_t
)(app
->playlist
.item
[idx
+ 1].type
|
506 OMNPLAY_PLAYLIST_BLOCK_BEGIN
);
509 /* shift playlist items */
512 &app
->playlist
.item
[idx
],
513 &app
->playlist
.item
[idx
+ 1],
514 (app
->playlist
.count
- idx
- 1) * sizeof(playlist_item_t
)
517 /* decrement items count */
518 app
->playlist
.count
--;
520 /* increment servers indexes */
521 for(i
= 0; i
< app
->players
.count
; i
++)
522 if(app
->players
.item
[i
].playlist_start
>= idx
)
523 app
->players
.item
[i
].playlist_start
--;
528 /* redraw playlist */
529 omnplay_playlist_draw(app
);
535 path
= gtk_tree_path_new_from_indices(idxs
[0], -1);
536 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
)), path
);
537 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app
->playlist_grid
), path
, NULL
, FALSE
);
538 gtk_tree_path_free(path
);
541 pthread_mutex_unlock(&app
->players
.lock
);
542 pthread_mutex_unlock(&app
->playlist
.lock
);
545 static void omnplay_playlist_item_del(omnplay_instance_t
* app
)
550 list
= omnplay_selected_idxs_playlist(app
);
553 list2
= (int*)malloc(sizeof(int) * list
[0]);
555 for(i
= 0, c
= 0; i
< list
[0]; i
++)
557 /* check for playing block */
558 if(idx_in_players_range(app
, list
[i
+ 1]))
562 list2
[c
++] = list
[i
+ 1];
566 omnplay_playlist_delete_items(app
, list2
, c
, 1);
572 static int omnplay_playlist_insert_check(omnplay_instance_t
* app
, int idx
, playlist_item_type_t
* t
)
574 *t
= OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE
;
576 /* before or after playlist */
577 if(!idx
|| idx
== app
->playlist
.count
)
580 /* check for block borders */
581 if( app
->playlist
.item
[idx
- 1].type
& OMNPLAY_PLAYLIST_BLOCK_END
&&
582 app
->playlist
.item
[idx
+ 0].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
)
585 /* check for playing block */
586 if(idx_in_players_range(app
, idx
))
589 if(app
->playlist
.item
[idx
].type
& OMNPLAY_PLAYLIST_BLOCK_LOOP
)
590 *t
= OMNPLAY_PLAYLIST_ITEM_LOOP_BODY
;
592 *t
= OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY
;
597 static void omnplay_playlist_insert_items(omnplay_instance_t
* app
, int idx
,
598 playlist_item_t
* items
, int count
)
603 pthread_mutex_lock(&app
->playlist
.lock
);
604 pthread_mutex_lock(&app
->players
.lock
);
606 /* shift playlist items */
609 &app
->playlist
.item
[idx
+ count
],
610 &app
->playlist
.item
[idx
],
611 (app
->playlist
.count
- idx
) * sizeof(playlist_item_t
)
617 &app
->playlist
.item
[idx
],
619 count
* sizeof(playlist_item_t
)
622 /* increment servers indexes */
623 for(i
= 0; i
< app
->players
.count
; i
++)
624 if(app
->players
.item
[i
].playlist_start
>= idx
)
625 app
->players
.item
[i
].playlist_start
+= idx
;
627 /* increment items count */
628 app
->playlist
.count
+= count
;
630 /* redraw playlist */
631 omnplay_playlist_draw(app
);
634 path
= gtk_tree_path_new_from_indices(idx
, -1);
635 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
)), path
);
636 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app
->playlist_grid
), path
, NULL
, FALSE
);
637 gtk_tree_path_free(path
);
639 pthread_mutex_unlock(&app
->players
.lock
);
640 pthread_mutex_unlock(&app
->playlist
.lock
);
643 static void omnplay_playlist_item_add(omnplay_instance_t
* app
, int after
)
646 playlist_item_t item
;
647 playlist_item_type_t t
;
649 /* find insert position */
650 idx
= get_first_selected_item_playlist(app
);
656 if(!omnplay_playlist_insert_check(app
, idx
, &t
))
659 g_warning("allowed insert into idx=%d\n", idx
);
662 memset(&item
, 0, sizeof(playlist_item_t
));
663 if(ui_playlist_item_dialog(app
, &item
))
665 omnplay_library_normalize_item(app
, &item
);
667 omnplay_playlist_insert_items(app
, idx
, &item
, 1);
671 static void omnplay_playlist_item_edit(omnplay_instance_t
* app
)
674 playlist_item_t item
;
676 /* find insert position */
677 idx
= get_first_selected_item_playlist(app
);
682 /* check for playing block */
683 if(idx_in_players_range(app
, idx
))
686 item
= app
->playlist
.item
[idx
];
688 if(ui_playlist_item_dialog(app
, &item
))
690 omnplay_library_normalize_item(app
, &item
);
691 app
->playlist
.item
[idx
] = item
;
692 omnplay_playlist_draw_item(app
, idx
);
696 static void omnplay_ctl(omnplay_instance_t
* app
, control_buttons_t button
)
699 int idx
, start
, stop
;
700 omnplay_player_t
*player
;
702 pthread_mutex_lock(&app
->playlist
.lock
);
704 idx
= get_first_selected_item_playlist(app
);
708 pthread_mutex_unlock(&app
->playlist
.lock
);
712 g_warning("cue: selected item is %d\n", idx
);
714 if(get_playlist_block(app
, idx
, &start
, &stop
) < 0)
716 pthread_mutex_unlock(&app
->playlist
.lock
);
720 g_warning("cue: range %d -> %d\n", start
, stop
);
722 player
= get_player_at_pos(app
, start
);
726 pthread_mutex_unlock(&app
->playlist
.lock
);
730 pthread_mutex_lock(&app
->players
.lock
);
732 if(BUTTON_PLAYER_STOP
== button
|| BUTTON_PLAYER_CUE
== button
)
735 OmPlrStop((OmPlrHandle
)player
->handle
);
737 /* detach previous clips */
738 player
->playlist_length
= -1;
739 OmPlrDetachAllClips((OmPlrHandle
)player
->handle
);
742 if(BUTTON_PLAYER_CUE
== button
)
746 /* Attach clips to timeline */
747 for(i
= start
, c
= 0, o
= 0; i
<= stop
; i
++)
752 clip
.maxMsTracks
= 0;
753 clip
.size
= sizeof(clip
);
754 r
= OmPlrClipGetInfo((OmPlrHandle
)player
->handle
, app
->playlist
.item
[i
].id
, &clip
);
760 g_warning("OmPlrClipGetInfo(%s): firstFrame=%d, lastFrame=%d\n",
761 app
->playlist
.item
[i
].id
, clip
.firstFrame
, clip
.lastFrame
);
763 /* should we fix playlist clip timings */
765 app
->playlist
.item
[i
].in
>= clip
.firstFrame
&&
766 app
->playlist
.item
[i
].in
+ app
->playlist
.item
[i
].dur
<= clip
.lastFrame
) ||
767 !app
->playlist
.item
[i
].dur
)
769 g_warning("cue: item [%s] will be updated [%d;%d]->[%d;%d]\n",
770 app
->playlist
.item
[i
].id
,
771 app
->playlist
.item
[i
].in
, app
->playlist
.item
[i
].dur
,
772 clip
.firstFrame
, clip
.lastFrame
- clip
.firstFrame
);
774 app
->playlist
.item
[i
].in
= clip
.firstFrame
;
775 app
->playlist
.item
[i
].dur
= clip
.lastFrame
- clip
.firstFrame
;
776 omnplay_playlist_draw_item(app
, i
);
779 r
= OmPlrAttach((OmPlrHandle
)player
->handle
,
780 app
->playlist
.item
[i
].id
,
781 app
->playlist
.item
[i
].in
,
782 app
->playlist
.item
[i
].in
+ app
->playlist
.item
[i
].dur
,
783 0, omPlrShiftModeAfter
, &l
);
788 g_warning("cue: failed with %d, %s\n", r
, OmPlrGetErrorString((OmPlrError
)r
));
789 app
->playlist
.item
[i
].omn_idx
= -1;
790 app
->playlist
.item
[i
].omn_offset
= -1;
791 app
->playlist
.item
[i
].error
|= PLAYLIST_ITEM_ERROR_CUE
;
795 app
->playlist
.item
[i
].omn_idx
= c
;
796 app
->playlist
.item
[i
].omn_offset
= o
;
797 app
->playlist
.item
[i
].error
&= 0xF ^ PLAYLIST_ITEM_ERROR_CUE
;
799 /* save selected item offset */
803 o
+= app
->playlist
.item
[i
].dur
;
811 /* Set timeline min/max */
812 OmPlrSetMinPosMin((OmPlrHandle
)player
->handle
);
813 OmPlrSetMaxPosMax((OmPlrHandle
)player
->handle
);
815 /* Set timeline position */
817 hs
.size
= sizeof(OmPlrStatus
);
818 OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &hs
);
819 OmPlrSetPos((OmPlrHandle
)player
->handle
, hs
.minPos
+ p
);
822 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_LOOP
)
823 OmPlrLoop((OmPlrHandle
)player
->handle
, hs
.minPos
, hs
.maxPos
);
825 OmPlrLoop((OmPlrHandle
)player
->handle
, hs
.minPos
, hs
.minPos
);
827 player
->playlist_start
= start
;
828 player
->playlist_length
= stop
- start
+ 1;
831 OmPlrCuePlay((OmPlrHandle
)player
->handle
, 0.0);
835 if(BUTTON_PLAYER_PLAY
== button
)
838 OmPlrPlay((OmPlrHandle
)player
->handle
, 1.0);
841 if(BUTTON_PLAYER_PAUSE
== button
)
843 OmPlrPlay((OmPlrHandle
)player
->handle
, 0.0);
845 pthread_mutex_unlock(&app
->players
.lock
);
847 pthread_mutex_unlock(&app
->playlist
.lock
);
850 static void omnplay_playlist_item_swap(omnplay_instance_t
* app
, int dir
)
852 int sel
, a
, b
, e
= 1;
854 playlist_item_t item
;
856 /* find insert position */
857 sel
= get_first_selected_item_playlist(app
);
874 /* check for playing block */
875 if(idx_in_players_range(app
, a
) || idx_in_players_range(app
, b
))
878 pthread_mutex_lock(&app
->playlist
.lock
);
879 pthread_mutex_lock(&app
->players
.lock
);
882 item
= app
->playlist
.item
[a
];
883 app
->playlist
.item
[a
] = app
->playlist
.item
[b
];
884 app
->playlist
.item
[b
] = item
;
887 if(app
->playlist
.item
[a
].type
!= app
->playlist
.item
[b
].type
)
890 app
->playlist
.item
[a
].type
= OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE
;
891 app
->playlist
.item
[b
].type
= OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE
;
894 /* redraw main items */
895 omnplay_playlist_draw_item(app
, a
);
896 omnplay_playlist_draw_item(app
, b
);
898 /* fix block types */
901 app
->playlist
.item
[a
- 1].type
= (playlist_item_type_t
)(app
->playlist
.item
[a
- 1].type
|
902 OMNPLAY_PLAYLIST_BLOCK_END
);
903 omnplay_playlist_draw_item(app
, a
- 1);
905 if(b
+ 1 < app
->playlist
.count
&& !e
)
907 app
->playlist
.item
[b
+ 1].type
= (playlist_item_type_t
)(app
->playlist
.item
[b
+ 1].type
|
908 OMNPLAY_PLAYLIST_BLOCK_BEGIN
);
909 omnplay_playlist_draw_item(app
, b
+ 1);
913 path
= gtk_tree_path_new_from_indices(sel
, -1);
914 gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
)), path
);
915 gtk_tree_view_set_cursor(GTK_TREE_VIEW(app
->playlist_grid
), path
, NULL
, FALSE
);
916 gtk_tree_path_free(path
);
918 pthread_mutex_unlock(&app
->players
.lock
);
919 pthread_mutex_unlock(&app
->playlist
.lock
);
922 static void omnplay_library_add(omnplay_instance_t
* app
, int after
)
925 playlist_item_t
* items
;
926 playlist_item_type_t t
;
928 /* find insert position */
929 idx
= get_first_selected_item_playlist(app
);
935 if(!omnplay_playlist_insert_check(app
, idx
, &t
))
938 items
= omnplay_library_get_selected(app
, &c
);
943 for(i
= 0; i
< c
; i
++)
948 omnplay_playlist_insert_items(app
, idx
, items
, c
);
953 static gboolean
omnplay_button_click(omnplay_instance_t
* app
, control_buttons_t button
)
957 case BUTTON_PLAYLIST_ITEM_ADD
:
958 omnplay_playlist_item_add(app
, 0);
960 case BUTTON_PLAYLIST_ITEM_DEL
:
961 omnplay_playlist_item_del(app
);
963 case BUTTON_PLAYLIST_ITEM_EDIT
:
964 omnplay_playlist_item_edit(app
);
966 case BUTTON_PLAYLIST_LOAD
:
967 omnplay_playlist_load(app
);
969 case BUTTON_PLAYLIST_SAVE
:
970 omnplay_playlist_save(app
);
972 case BUTTON_PLAYLIST_BLOCK_SINGLE
:
973 case BUTTON_PLAYLIST_BLOCK_LOOP
:
974 omnplay_playlist_block(app
, button
);
976 case BUTTON_PLAYLIST_ITEM_UP
:
977 omnplay_playlist_item_swap(app
, -1);
979 case BUTTON_PLAYLIST_ITEM_DOWN
:
980 omnplay_playlist_item_swap(app
, +1);
982 case BUTTON_PLAYER_CUE
:
983 case BUTTON_PLAYER_PLAY
:
984 case BUTTON_PLAYER_PAUSE
:
985 case BUTTON_PLAYER_STOP
:
986 omnplay_ctl(app
, button
);
988 case BUTTON_LIBRARY_ADD
:
989 omnplay_library_add(app
, 0);
991 case BUTTON_LIBRARY_REFRESH
:
992 omnplay_library_refresh(app
);
994 case BUTTON_LIBRARY_FIND
:
995 omnplay_library_search(app
, 0);
997 case BUTTON_LIBRARY_FIND_NEXT
:
998 omnplay_library_search(app
, 1);
1005 static gboolean
on_button_click(GtkWidget
*button
, gpointer user_data
)
1008 omnplay_instance_t
* app
= (omnplay_instance_t
*)user_data
;
1010 for(i
= 1; i
< BUTTON_LAST
; i
++)
1011 if(app
->buttons
[i
] == button
)
1012 return omnplay_button_click(app
, (control_buttons_t
)i
);
1017 static void omnplay_playlist_item_copy(omnplay_instance_t
* app
)
1021 list
= omnplay_selected_idxs_playlist(app
);
1024 for(i
= 0; i
< list
[0]; i
++)
1025 app
->clipboard
.item
[i
] = app
->playlist
.item
[list
[i
+ 1]];
1026 app
->clipboard
.count
= list
[0];
1031 static void omnplay_playlist_item_paste(omnplay_instance_t
* app
, int after
)
1034 playlist_item_t
* items
;
1035 playlist_item_type_t t
;
1037 /* find insert position */
1038 idx
= get_first_selected_item_playlist(app
);
1044 if(!omnplay_playlist_insert_check(app
, idx
, &t
))
1048 if(app
->clipboard
.count
)
1050 for(i
= 0; i
< app
->clipboard
.count
; i
++)
1052 app
->clipboard
.item
[i
].type
= t
;
1053 app
->clipboard
.item
[i
].error
= 0;
1055 omnplay_playlist_insert_items(app
, idx
, app
->clipboard
.item
, app
->clipboard
.count
);
1059 static gboolean
on_playlist_grid_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1061 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
1063 switch(event
->keyval
)
1067 if(event
->state
& GDK_CONTROL_MASK
)
1069 omnplay_playlist_item_copy(app
);
1075 if(event
->state
& GDK_CONTROL_MASK
)
1077 omnplay_playlist_item_paste(app
, 0);
1083 if(event
->state
& GDK_CONTROL_MASK
)
1085 omnplay_playlist_item_copy(app
);
1086 omnplay_playlist_item_del(app
);
1092 if(event
->state
& GDK_CONTROL_MASK
)
1094 omnplay_playlist_save(app
);
1100 if(event
->state
& GDK_CONTROL_MASK
)
1102 omnplay_playlist_load(app
);
1106 case GDK_KEY_uparrow
:
1107 if(event
->state
& GDK_CONTROL_MASK
)
1109 omnplay_playlist_item_swap(app
, -1);
1113 case GDK_KEY_downarrow
:
1114 if(event
->state
& GDK_CONTROL_MASK
)
1116 omnplay_playlist_item_swap(app
, -1);
1121 omnplay_ctl(app
, BUTTON_PLAYER_PLAY
);
1123 case GDK_KEY_Return
:
1124 omnplay_ctl(app
, BUTTON_PLAYER_CUE
);
1126 case GDK_KEY_Insert
:
1127 omnplay_playlist_item_add(app
, 0);
1129 case GDK_KEY_Delete
:
1130 omnplay_playlist_item_del(app
);
1134 omnplay_playlist_item_edit(app
);
1141 static gboolean
on_library_grid_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1143 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
1145 switch(event
->keyval
)
1149 if(event
->state
& GDK_CONTROL_MASK
)
1152 playlist_item_t
* items
;
1154 items
= omnplay_library_get_selected(app
, &count
);
1160 for(i
= 0; i
< count
; i
++)
1161 app
->clipboard
.item
[i
] = items
[i
];
1163 app
->clipboard
.count
= count
;
1171 if(event
->state
& GDK_CONTROL_MASK
)
1173 g_warning("CTRL+v\n");
1179 if(event
->state
& GDK_CONTROL_MASK
)
1181 g_warning("CTRL+x\n");
1185 case GDK_KEY_BackSpace
:
1186 omnplay_library_add(app
, 0);
1189 omnplay_library_refresh(app
);
1196 static gboolean
on_library_grid_button(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1198 // g_warning("on_library_grid_button: event->button=%d, event->type=%d", event->button, event->type);
1200 if(event
->button
==1 && event
->type
==GDK_2BUTTON_PRESS
)
1202 omnplay_library_add((omnplay_instance_t
* )data
, 0);
1209 static gboolean
on_playlist_grid_button(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1211 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
1213 // g_warning("on_playlist_grid_button");
1215 if(event
->button
==1 && event
->type
==GDK_2BUTTON_PRESS
)
1217 omnplay_playlist_item_edit(app
);
1226 static void library_grid_drag_data_get_cb(GtkWidget
*widget
, GdkDragContext
*context
,
1227 GtkSelectionData
*selection_data
, guint info
, guint time
, gpointer userdata
)
1230 playlist_item_t
* items
;
1231 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1233 g_warning("library_grid_drag_data_get_cb");
1235 items
= omnplay_library_get_selected(app
, &c
);
1240 gtk_selection_data_set(selection_data
, selection_data
->target
, 8,
1241 (const guchar
*)items
, sizeof(playlist_item_t
) * c
);
1246 static void playlist_grid_drag_data_get_cb(GtkWidget
*widget
, GdkDragContext
*context
,
1247 GtkSelectionData
*selection_data
, guint info
, guint time
, gpointer userdata
)
1250 playlist_item_t
* items
;
1251 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1253 list
= omnplay_selected_idxs_playlist(app
);
1256 /* clear delete flag */
1257 for(i
= 0; i
< app
->playlist
.count
; i
++)
1258 app
->playlist
.item
[i
].del
= 0;
1260 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * list
[0]);
1261 for(i
= 0; i
< list
[0]; i
++)
1263 items
[i
] = app
->playlist
.item
[list
[i
+ 1]];
1264 if(context
->action
== GDK_ACTION_MOVE
)
1265 app
->playlist
.item
[list
[i
+ 1]].del
= 1;
1267 gtk_selection_data_set(selection_data
, selection_data
->target
, 8,
1268 (const guchar
*)items
, sizeof(playlist_item_t
) * list
[0]);
1274 static void library_grid_drag_begin_cb(GtkWidget
*widget
, GdkDragContext
*context
, gpointer userdata
)
1276 g_warning("library_grid_drag_begin_cb");
1277 gtk_drag_source_set_icon_stock(widget
, GTK_STOCK_DND
);
1280 static void playlist_grid_drag_begin_cb(GtkWidget
*widget
, GdkDragContext
*context
, gpointer userdata
)
1282 g_warning("playlist_grid_drag_begin_cb");
1283 gtk_drag_source_set_icon_stock(widget
, GTK_STOCK_DND
);
1286 static void playlist_grid_drag_data_received(GtkWidget
*widget
, GdkDragContext
*context
,
1287 gint x
, gint y
, GtkSelectionData
*selection_data
, guint info
, guint time
, gpointer userdata
)
1290 playlist_item_type_t t
;
1291 playlist_item_t
* items
;
1292 GtkTreePath
*path
= NULL
;
1293 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1295 g_warning("playlist_grid_drag_data_received: context->action=%d", context
->action
);
1297 items
= (playlist_item_t
*)gtk_selection_data_get_data(selection_data
);
1298 c
= gtk_selection_data_get_length(selection_data
);
1300 if(c
% sizeof(playlist_item_t
))
1302 g_warning("playlist_grid_drag_data_received: ODD ITEMS");
1306 c
/= sizeof(playlist_item_t
);
1308 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget
), x
, y
, &path
, NULL
, NULL
, NULL
))
1310 idx
= gtk_tree_path_get_indices(path
)[0];
1311 gtk_tree_path_free(path
);
1313 g_warning("playlist_grid_drag_data_received: gtk_tree_path_get_indice[0]=%d", idx
);
1315 /* normalize, FIX ME */
1316 idx
--; if(idx
< 0) idx
= 0;
1319 idx
= app
->playlist
.count
;
1321 g_warning("playlist_grid_drag_data_received: idx=%d", idx
);
1323 if(omnplay_playlist_insert_check(app
, idx
, &t
))
1325 for(i
= 0; i
< c
; i
++)
1330 omnplay_playlist_insert_items(app
, idx
, items
, c
);
1334 /* Finish the drag */
1335 gtk_drag_finish(context
, TRUE
, FALSE
, time
);
1338 static void playlist_grid_drag_data_delete(GtkWidget
*widget
, GdkDragContext
*context
, gpointer userdata
)
1341 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1343 g_warning("playlist_grid_drag_data_delete");
1345 list
= (int*)malloc(sizeof(int) * MAX_PLAYLIST_ITEMS
);
1347 for(i
= 0, c
= 0; i
< app
->playlist
.count
; i
++)
1348 if(app
->playlist
.item
[i
].del
)
1349 if(!idx_in_players_range(app
, i
))
1353 g_warning("playlist_grid_drag_data_delete: i=%d, c=%d", i
, c
);
1357 omnplay_playlist_delete_items(app
, list
, c
, 0);
1363 * http://www.mail-archive.com/mahogany-users@lists.sourceforge.net/msg00286.html
1365 static gboolean
playlist_grid_drag_motion(GtkWidget
*widget
, GdkDragContext
*context
,
1366 gint x
, gint y
, guint time
, gpointer data
)
1369 GtkWidget
*source_widget
;
1371 g_warning("playlist_grid_drag_motion");
1373 /* Get source widget and check if it is the same as the
1374 * destination widget.
1376 source_widget
= gtk_drag_get_source_widget(context
);
1377 same
= ((source_widget
== widget
) ? TRUE
: FALSE
);
1379 /* Put additional checks here, perhaps if same is FALSE then
1380 * set the default drag to GDK_ACTION_COPY.
1383 /* Say we just want to allow GDK_ACTION_MOVE, first we check
1384 * if that is in the list of allowed actions on the dc. If
1385 * so then we set it to that. Note if the user holds down the
1386 * ctrl key then the only flag in dc->actions will be
1387 * GDK_ACTION_COPY. The constraint for dc->actions is that
1388 * specified from the given actions in gtk_drag_dest_set() and
1389 * gtk_drag_source_set().
1393 if(context
->actions
== GDK_ACTION_MOVE
)
1394 gdk_drag_status(context
, GDK_ACTION_COPY
, time
);
1396 gdk_drag_status(context
, GDK_ACTION_MOVE
, time
);
1399 gdk_drag_status(context
, context
->actions
, time
);
1404 void omnplay_init(omnplay_instance_t
* app
)
1407 pthread_mutexattr_t attr
;
1409 pthread_mutexattr_init(&attr
);
1410 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
1412 gtk_signal_connect( GTK_OBJECT( app
->window
), "delete-event",
1413 GTK_SIGNAL_FUNC(on_main_window_delete_event
), app
);
1415 gtk_signal_connect( GTK_OBJECT( app
->window
), "destroy",
1416 GTK_SIGNAL_FUNC(on_main_window_destroy
), app
);
1418 gtk_widget_add_events(app
->playlist_grid
, GDK_BUTTON_PRESS_MASK
);
1419 gtk_widget_add_events(app
->playlist_grid
, GDK_KEY_PRESS_MASK
);
1420 gtk_signal_connect(GTK_OBJECT(app
->playlist_grid
), "key-press-event",
1421 GTK_SIGNAL_FUNC(on_playlist_grid_key
), app
);
1423 gtk_widget_add_events(app
->library_grid
, GDK_BUTTON_PRESS_MASK
);
1424 gtk_widget_add_events(app
->library_grid
, GDK_KEY_PRESS_MASK
);
1425 gtk_signal_connect(GTK_OBJECT(app
->library_grid
), "key-press-event",
1426 GTK_SIGNAL_FUNC(on_library_grid_key
), app
);
1428 gtk_signal_connect(GTK_OBJECT(app
->playlist_grid
), "button-press-event",
1429 GTK_SIGNAL_FUNC(on_playlist_grid_button
), app
);
1431 gtk_signal_connect(GTK_OBJECT(app
->library_grid
), "button-press-event",
1432 GTK_SIGNAL_FUNC(on_library_grid_button
), app
);
1435 pthread_mutex_init(&app
->players
.lock
, &attr
);
1436 pthread_mutex_init(&app
->playlist
.lock
, &attr
);
1437 pthread_mutex_init(&app
->library
.lock
, &attr
);
1439 /* create a omneon status thread */
1440 for(i
= 0; i
< app
->players
.count
; i
++)
1441 app
->players
.item
[i
].thread
= g_thread_create(
1442 omnplay_thread_proc
, &app
->players
.item
[i
], TRUE
, NULL
);
1444 /* attach buttons click */
1445 for(i
= 1; i
< BUTTON_LAST
; i
++)
1446 gtk_signal_connect(GTK_OBJECT(app
->buttons
[i
]), "clicked",
1447 GTK_SIGNAL_FUNC( on_button_click
), app
);
1450 omnplay_library_load(app
);
1452 pthread_mutexattr_destroy(&attr
);
1454 /* setup drag n drop source/target */
1455 static GtkTargetEntry drag_targets
[] = { { (char*) "application/playlist_item_t", 0, 0 } };
1457 gtk_drag_source_set(app
->library_grid
, GDK_BUTTON1_MASK
,
1458 drag_targets
, 1, (GdkDragAction
)(GDK_ACTION_COPY
));
1460 gtk_drag_source_set(app
->playlist_grid
, GDK_BUTTON1_MASK
,
1461 drag_targets
, 1, (GdkDragAction
)(GDK_ACTION_COPY
| GDK_ACTION_MOVE
));
1463 gtk_drag_dest_set(app
->playlist_grid
, (GtkDestDefaults
)(GTK_DEST_DEFAULT_HIGHLIGHT
| GTK_DEST_DEFAULT_DROP
),
1464 drag_targets
, 1, (GdkDragAction
)(GDK_ACTION_COPY
| GDK_ACTION_MOVE
));
1466 g_signal_connect (app
->library_grid
, "drag_data_get", G_CALLBACK(library_grid_drag_data_get_cb
), app
);
1467 g_signal_connect (app
->playlist_grid
, "drag_data_get", G_CALLBACK(playlist_grid_drag_data_get_cb
), app
);
1468 g_signal_connect (app
->library_grid
, "drag_begin", G_CALLBACK(library_grid_drag_begin_cb
), app
);
1469 g_signal_connect (app
->playlist_grid
, "drag_begin", G_CALLBACK(playlist_grid_drag_begin_cb
), app
);
1470 g_signal_connect (app
->playlist_grid
, "drag_data_received", G_CALLBACK (playlist_grid_drag_data_received
), app
);
1471 g_signal_connect (app
->playlist_grid
, "drag_data_delete", G_CALLBACK (playlist_grid_drag_data_delete
), app
);
1472 g_signal_connect (app
->playlist_grid
, "drag_motion", G_CALLBACK (playlist_grid_drag_motion
), app
);
1475 void omnplay_release(omnplay_instance_t
* app
)
1481 for(i
= 0; i
< app
->players
.count
; i
++)
1482 /* create a omneon status thread */
1483 g_thread_join(app
->players
.item
[i
].thread
);
1486 pthread_mutex_destroy(&app
->players
.lock
);
1489 pthread_mutex_destroy(&app
->playlist
.lock
);
1492 omnplay_library_save(app
);
1494 /* destroy library lock */
1495 pthread_mutex_destroy(&app
->library
.lock
);
1498 void omnplay_playlist_normalize(omnplay_instance_t
* app
)
1502 /* normalize playlist */
1503 for(i
= 0; i
< app
->playlist
.count
; i
++)
1504 if(omnplay_library_normalize_item(app
, &app
->playlist
.item
[i
]))
1505 omnplay_playlist_draw_item(app
, i
);
1508 void omnplay_set_status(omnplay_instance_t
* app
, char* str
)
1510 gdk_threads_enter();
1511 gtk_label_set_text(GTK_LABEL(app
->status_label
), str
);
1513 gdk_threads_leave();