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
);
192 /* check if playlist exist */
193 if(player
->playlist_length
)
195 /* clear remain on "previous" item */
196 if(curr
->currClipNum
!= prev
->currClipNum
&& 1 != prev
->numClips
)
199 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, prev
->currClipNum
);
201 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
204 /* update current item */
205 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, curr
->currClipNum
);
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
);
215 /* setup that value */
216 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
219 pthread_mutex_unlock(&player
->app
->players
.lock
);
220 pthread_mutex_unlock(&player
->app
->playlist
.lock
);
225 memcpy(prev
, curr
, sizeof(OmPlrStatus
));
228 static void* omnplay_thread_proc(void* data
)
231 OmPlrStatus st_curr
, st_prev
;
232 omnplay_player_t
* player
= (omnplay_player_t
*)data
;
234 g_warning("omnplay_thread_proc\n");
236 memset(&st_curr
, 0, sizeof(OmPlrStatus
));
237 memset(&st_prev
, 0, sizeof(OmPlrStatus
));
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
);
245 g_warning("ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
246 player
->host
, player
->name
, r
);
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
);
256 /* setup directory */
257 if(player
->app
->players
.path
[0])
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
);
265 g_warning("ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
266 player
->app
->players
.path
, r
);
268 pthread_mutex_lock(&player
->app
->players
.lock
);
269 OmPlrClose((OmPlrHandle
)player
->handle
);
270 pthread_mutex_unlock(&player
->app
->players
.lock
);
277 for(r
= 0 ; !player
->app
->f_exit
&& !r
;)
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
);
293 g_warning("ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", r
);
295 if(memcmp(&st_curr
, &st_prev
, sizeof(OmPlrStatus
)))
296 omnplay_update_status(player
, &st_prev
, &st_curr
);
299 pthread_mutex_lock(&player
->app
->players
.lock
);
300 OmPlrClose((OmPlrHandle
)player
->handle
);
301 pthread_mutex_unlock(&player
->app
->players
.lock
);
306 void get_selected_items_playlist_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
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;
314 int* omnplay_selected_idxs_playlist(omnplay_instance_t
* app
)
317 GtkTreeSelection
*selection
;
319 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
));
322 list
= (int*)malloc(sizeof(int) * (MAX_PLAYLIST_ITEMS
+ 1));
323 memset(list
, 0, sizeof(int) * (MAX_PLAYLIST_ITEMS
+ 1));
325 gtk_tree_selection_selected_foreach(
327 get_selected_items_playlist_proc
,
340 static int idx_in_players_range(omnplay_instance_t
* app
, int idx
)
344 for(i
= 0; i
< app
->players
.count
&& !r
; i
++)
348 a
= app
->players
.item
[i
].playlist_start
;
349 b
= app
->players
.item
[i
].playlist_length
;
356 if(idx
>= a
&& idx
<= b
) r
= 1;
362 static int idxs_in_players_range(omnplay_instance_t
* app
, int start
, int stop
)
366 for(i
= 0; i
< app
->players
.count
&& !r
; i
++)
370 a
= app
->players
.item
[i
].playlist_start
;
371 b
= app
->players
.item
[i
].playlist_length
;
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
))
389 static void omnplay_playlist_block(omnplay_instance_t
* app
, control_buttons_t button
)
391 int start
, stop
, r
, i
;
392 int* list
= omnplay_selected_idxs_playlist(app
);
397 pthread_mutex_lock(&app
->playlist
.lock
);
398 pthread_mutex_lock(&app
->players
.lock
);
401 stop
= list
[list
[0]];
403 if(!idxs_in_players_range(app
, start
, stop
))
405 int loop
= (button
== BUTTON_PLAYLIST_BLOCK_LOOP
)?OMNPLAY_PLAYLIST_BLOCK_LOOP
:0;
407 /* update selected item */
408 for(i
= start
; i
<= stop
; i
++)
410 int t
= OMNPLAY_PLAYLIST_BLOCK_BODY
| loop
;
412 if(i
== start
) t
|= OMNPLAY_PLAYLIST_BLOCK_BEGIN
;
413 if(i
== stop
) t
|= OMNPLAY_PLAYLIST_BLOCK_END
;
415 app
->playlist
.item
[i
].type
= (playlist_item_type_t
)t
;
417 omnplay_playlist_draw_item(app
, i
);
420 /* update border items */
421 if(start
&& !(app
->playlist
.item
[start
- 1].type
& OMNPLAY_PLAYLIST_BLOCK_END
))
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);
427 if((stop
+ 1) < app
->playlist
.count
&& !(app
->playlist
.item
[stop
+ 1].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
))
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);
435 g_warning("omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
438 pthread_mutex_unlock(&app
->players
.lock
);
439 pthread_mutex_unlock(&app
->playlist
.lock
);
444 static int get_first_selected_item_playlist(omnplay_instance_t
* app
)
447 int* list
= omnplay_selected_idxs_playlist(app
);
454 static int get_playlist_block(omnplay_instance_t
* app
, int idx
, int* start_ptr
, int* stop_ptr
)
458 for(start
= idx
; start
>= 0; start
--)
459 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
)
462 for(stop
= idx
; stop
< app
->playlist
.count
; stop
++)
463 if(app
->playlist
.item
[stop
].type
& OMNPLAY_PLAYLIST_BLOCK_END
)
466 g_warning("get_playlist_block: range %d -> %d\n", start
, stop
);
468 /* check block range */
469 if(start
>= 0 && stop
< app
->playlist
.count
)
473 return (stop
- start
+ 1);
479 static omnplay_player_t
*get_player_at_pos(omnplay_instance_t
* app
, int pos
)
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
];
488 static void omnplay_playlist_delete_items(omnplay_instance_t
* app
, int* idxs
, int count
, int sel
)
492 pthread_mutex_lock(&app
->playlist
.lock
);
493 pthread_mutex_lock(&app
->players
.lock
);
495 for(j
= 0; j
< count
; j
++)
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
)
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
);
511 /* shift playlist items */
514 &app
->playlist
.item
[idx
],
515 &app
->playlist
.item
[idx
+ 1],
516 (app
->playlist
.count
- idx
- 1) * sizeof(playlist_item_t
)
519 /* decrement items count */
520 app
->playlist
.count
--;
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
--;
530 /* redraw playlist */
531 omnplay_playlist_draw(app
);
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
);
543 pthread_mutex_unlock(&app
->players
.lock
);
544 pthread_mutex_unlock(&app
->playlist
.lock
);
547 static void omnplay_playlist_item_del(omnplay_instance_t
* app
)
552 list
= omnplay_selected_idxs_playlist(app
);
555 list2
= (int*)malloc(sizeof(int) * list
[0]);
557 for(i
= 0, c
= 0; i
< list
[0]; i
++)
559 /* check for playing block */
560 if(idx_in_players_range(app
, list
[i
+ 1]))
564 list2
[c
++] = list
[i
+ 1];
568 omnplay_playlist_delete_items(app
, list2
, c
, 1);
574 static int omnplay_playlist_insert_check(omnplay_instance_t
* app
, int idx
, playlist_item_type_t
* t
)
576 *t
= OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE
;
578 /* before or after playlist */
579 if(!idx
|| idx
== app
->playlist
.count
)
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
)
587 /* check for playing block */
588 if(idx_in_players_range(app
, idx
))
591 if(app
->playlist
.item
[idx
].type
& OMNPLAY_PLAYLIST_BLOCK_LOOP
)
592 *t
= OMNPLAY_PLAYLIST_ITEM_LOOP_BODY
;
594 *t
= OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY
;
599 static void omnplay_playlist_insert_items(omnplay_instance_t
* app
, int idx
,
600 playlist_item_t
* items
, int count
)
605 pthread_mutex_lock(&app
->playlist
.lock
);
606 pthread_mutex_lock(&app
->players
.lock
);
608 /* shift playlist items */
611 &app
->playlist
.item
[idx
+ count
],
612 &app
->playlist
.item
[idx
],
613 (app
->playlist
.count
- idx
) * sizeof(playlist_item_t
)
619 &app
->playlist
.item
[idx
],
621 count
* sizeof(playlist_item_t
)
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
;
629 /* increment items count */
630 app
->playlist
.count
+= count
;
632 /* redraw playlist */
633 omnplay_playlist_draw(app
);
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
);
641 pthread_mutex_unlock(&app
->players
.lock
);
642 pthread_mutex_unlock(&app
->playlist
.lock
);
645 static void omnplay_playlist_item_add(omnplay_instance_t
* app
, int after
)
648 playlist_item_t item
;
649 playlist_item_type_t t
;
651 /* find insert position */
652 idx
= get_first_selected_item_playlist(app
);
658 if(!omnplay_playlist_insert_check(app
, idx
, &t
))
661 g_warning("allowed insert into idx=%d\n", idx
);
664 memset(&item
, 0, sizeof(playlist_item_t
));
665 if(ui_playlist_item_dialog(app
, &item
))
667 omnplay_library_normalize_item(app
, &item
);
669 omnplay_playlist_insert_items(app
, idx
, &item
, 1);
673 static void omnplay_playlist_item_edit(omnplay_instance_t
* app
)
676 playlist_item_t item
;
678 /* find insert position */
679 idx
= get_first_selected_item_playlist(app
);
684 /* check for playing block */
685 if(idx_in_players_range(app
, idx
))
688 item
= app
->playlist
.item
[idx
];
690 if(ui_playlist_item_dialog(app
, &item
))
692 omnplay_library_normalize_item(app
, &item
);
693 app
->playlist
.item
[idx
] = item
;
694 omnplay_playlist_draw_item(app
, idx
);
698 static void omnplay_ctl(omnplay_instance_t
* app
, control_buttons_t button
)
701 int idx
, start
, stop
;
702 omnplay_player_t
*player
;
704 pthread_mutex_lock(&app
->playlist
.lock
);
706 idx
= get_first_selected_item_playlist(app
);
710 pthread_mutex_unlock(&app
->playlist
.lock
);
714 g_warning("cue: selected item is %d\n", idx
);
716 if(get_playlist_block(app
, idx
, &start
, &stop
) < 0)
718 pthread_mutex_unlock(&app
->playlist
.lock
);
722 g_warning("cue: range %d -> %d\n", start
, stop
);
724 player
= get_player_at_pos(app
, start
);
728 pthread_mutex_unlock(&app
->playlist
.lock
);
732 pthread_mutex_lock(&app
->players
.lock
);
734 if(BUTTON_PLAYER_STOP
== button
|| BUTTON_PLAYER_CUE
== button
)
737 OmPlrStop((OmPlrHandle
)player
->handle
);
739 /* detach previous clips */
740 player
->playlist_length
= -1;
741 OmPlrDetachAllClips((OmPlrHandle
)player
->handle
);
744 if(BUTTON_PLAYER_CUE
== button
)
748 /* Attach clips to timeline */
749 for(i
= start
, c
= 0, o
= 0; i
<= stop
; i
++)
754 clip
.maxMsTracks
= 0;
755 clip
.size
= sizeof(clip
);
756 r
= OmPlrClipGetInfo((OmPlrHandle
)player
->handle
, app
->playlist
.item
[i
].id
, &clip
);
762 g_warning("OmPlrClipGetInfo(%s): firstFrame=%d, lastFrame=%d\n",
763 app
->playlist
.item
[i
].id
, clip
.firstFrame
, clip
.lastFrame
);
765 /* should we fix playlist clip timings */
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
)
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
);
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
);
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
);
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
;
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
;
801 /* save selected item offset */
805 o
+= app
->playlist
.item
[i
].dur
;
813 /* Set timeline min/max */
814 OmPlrSetMinPosMin((OmPlrHandle
)player
->handle
);
815 OmPlrSetMaxPosMax((OmPlrHandle
)player
->handle
);
817 /* Set timeline position */
819 hs
.size
= sizeof(OmPlrStatus
);
820 OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &hs
);
821 OmPlrSetPos((OmPlrHandle
)player
->handle
, hs
.minPos
+ p
);
824 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_LOOP
)
825 OmPlrLoop((OmPlrHandle
)player
->handle
, hs
.minPos
, hs
.maxPos
);
827 OmPlrLoop((OmPlrHandle
)player
->handle
, hs
.minPos
, hs
.minPos
);
829 player
->playlist_start
= start
;
830 player
->playlist_length
= stop
- start
+ 1;
833 OmPlrCuePlay((OmPlrHandle
)player
->handle
, 0.0);
837 if(BUTTON_PLAYER_PLAY
== button
)
840 OmPlrPlay((OmPlrHandle
)player
->handle
, 1.0);
843 if(BUTTON_PLAYER_PAUSE
== button
)
845 OmPlrPlay((OmPlrHandle
)player
->handle
, 0.0);
847 pthread_mutex_unlock(&app
->players
.lock
);
849 pthread_mutex_unlock(&app
->playlist
.lock
);
852 static void omnplay_playlist_item_swap(omnplay_instance_t
* app
, int dir
)
854 int sel
, a
, b
, e
= 1;
856 playlist_item_t item
;
858 /* find insert position */
859 sel
= get_first_selected_item_playlist(app
);
876 /* check for playing block */
877 if(idx_in_players_range(app
, a
) || idx_in_players_range(app
, b
))
880 pthread_mutex_lock(&app
->playlist
.lock
);
881 pthread_mutex_lock(&app
->players
.lock
);
884 item
= app
->playlist
.item
[a
];
885 app
->playlist
.item
[a
] = app
->playlist
.item
[b
];
886 app
->playlist
.item
[b
] = item
;
889 if(app
->playlist
.item
[a
].type
!= app
->playlist
.item
[b
].type
)
892 app
->playlist
.item
[a
].type
= OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE
;
893 app
->playlist
.item
[b
].type
= OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE
;
896 /* redraw main items */
897 omnplay_playlist_draw_item(app
, a
);
898 omnplay_playlist_draw_item(app
, b
);
900 /* fix block types */
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);
907 if(b
+ 1 < app
->playlist
.count
&& !e
)
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);
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
);
920 pthread_mutex_unlock(&app
->players
.lock
);
921 pthread_mutex_unlock(&app
->playlist
.lock
);
924 static void omnplay_library_add(omnplay_instance_t
* app
, int after
)
927 playlist_item_t
* items
;
928 playlist_item_type_t t
;
930 /* find insert position */
931 idx
= get_first_selected_item_playlist(app
);
937 if(!omnplay_playlist_insert_check(app
, idx
, &t
))
940 items
= omnplay_library_get_selected(app
, &c
);
945 for(i
= 0; i
< c
; i
++)
950 omnplay_playlist_insert_items(app
, idx
, items
, c
);
955 static gboolean
omnplay_button_click(omnplay_instance_t
* app
, control_buttons_t button
)
959 case BUTTON_PLAYLIST_ITEM_ADD
:
960 omnplay_playlist_item_add(app
, 0);
962 case BUTTON_PLAYLIST_ITEM_DEL
:
963 omnplay_playlist_item_del(app
);
965 case BUTTON_PLAYLIST_ITEM_EDIT
:
966 omnplay_playlist_item_edit(app
);
968 case BUTTON_PLAYLIST_LOAD
:
969 omnplay_playlist_load(app
);
971 case BUTTON_PLAYLIST_SAVE
:
972 omnplay_playlist_save(app
);
974 case BUTTON_PLAYLIST_BLOCK_SINGLE
:
975 case BUTTON_PLAYLIST_BLOCK_LOOP
:
976 omnplay_playlist_block(app
, button
);
978 case BUTTON_PLAYLIST_ITEM_UP
:
979 omnplay_playlist_item_swap(app
, -1);
981 case BUTTON_PLAYLIST_ITEM_DOWN
:
982 omnplay_playlist_item_swap(app
, +1);
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
);
990 case BUTTON_LIBRARY_ADD
:
991 omnplay_library_add(app
, 0);
993 case BUTTON_LIBRARY_REFRESH
:
994 omnplay_library_refresh(app
);
996 case BUTTON_LIBRARY_FIND
:
997 omnplay_library_search(app
, 0);
999 case BUTTON_LIBRARY_FIND_NEXT
:
1000 omnplay_library_search(app
, 1);
1007 static gboolean
on_button_click(GtkWidget
*button
, gpointer user_data
)
1010 omnplay_instance_t
* app
= (omnplay_instance_t
*)user_data
;
1012 for(i
= 1; i
< BUTTON_LAST
; i
++)
1013 if(app
->buttons
[i
] == button
)
1014 return omnplay_button_click(app
, (control_buttons_t
)i
);
1019 static void omnplay_playlist_item_copy(omnplay_instance_t
* app
)
1023 list
= omnplay_selected_idxs_playlist(app
);
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];
1033 static void omnplay_playlist_item_paste(omnplay_instance_t
* app
, int after
)
1036 playlist_item_t
* items
;
1037 playlist_item_type_t t
;
1039 /* find insert position */
1040 idx
= get_first_selected_item_playlist(app
);
1046 if(!omnplay_playlist_insert_check(app
, idx
, &t
))
1050 if(app
->clipboard
.count
)
1052 for(i
= 0; i
< app
->clipboard
.count
; i
++)
1054 app
->clipboard
.item
[i
].type
= t
;
1055 app
->clipboard
.item
[i
].error
= 0;
1057 omnplay_playlist_insert_items(app
, idx
, app
->clipboard
.item
, app
->clipboard
.count
);
1061 static gboolean
on_playlist_grid_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1063 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
1065 switch(event
->keyval
)
1069 if(event
->state
& GDK_CONTROL_MASK
)
1071 omnplay_playlist_item_copy(app
);
1077 if(event
->state
& GDK_CONTROL_MASK
)
1079 omnplay_playlist_item_paste(app
, 0);
1085 if(event
->state
& GDK_CONTROL_MASK
)
1087 omnplay_playlist_item_copy(app
);
1088 omnplay_playlist_item_del(app
);
1094 if(event
->state
& GDK_CONTROL_MASK
)
1096 omnplay_playlist_save(app
);
1102 if(event
->state
& GDK_CONTROL_MASK
)
1104 omnplay_playlist_load(app
);
1108 case GDK_KEY_uparrow
:
1109 if(event
->state
& GDK_CONTROL_MASK
)
1111 omnplay_playlist_item_swap(app
, -1);
1115 case GDK_KEY_downarrow
:
1116 if(event
->state
& GDK_CONTROL_MASK
)
1118 omnplay_playlist_item_swap(app
, -1);
1123 omnplay_ctl(app
, BUTTON_PLAYER_PLAY
);
1125 case GDK_KEY_Return
:
1126 omnplay_ctl(app
, BUTTON_PLAYER_CUE
);
1128 case GDK_KEY_Insert
:
1129 omnplay_playlist_item_add(app
, 0);
1131 case GDK_KEY_Delete
:
1132 omnplay_playlist_item_del(app
);
1136 omnplay_playlist_item_edit(app
);
1143 static gboolean
on_library_grid_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
1145 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
1147 switch(event
->keyval
)
1151 if(event
->state
& GDK_CONTROL_MASK
)
1154 playlist_item_t
* items
;
1156 items
= omnplay_library_get_selected(app
, &count
);
1162 for(i
= 0; i
< count
; i
++)
1163 app
->clipboard
.item
[i
] = items
[i
];
1165 app
->clipboard
.count
= count
;
1173 if(event
->state
& GDK_CONTROL_MASK
)
1175 g_warning("CTRL+v\n");
1181 if(event
->state
& GDK_CONTROL_MASK
)
1183 g_warning("CTRL+x\n");
1187 case GDK_KEY_BackSpace
:
1188 omnplay_library_add(app
, 0);
1191 omnplay_library_refresh(app
);
1198 static gboolean
on_library_grid_button(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1200 // g_warning("on_library_grid_button: event->button=%d, event->type=%d", event->button, event->type);
1202 if(event
->button
==1 && event
->type
==GDK_2BUTTON_PRESS
)
1204 omnplay_library_add((omnplay_instance_t
* )data
, 0);
1211 static gboolean
on_playlist_grid_button(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1213 omnplay_instance_t
* app
= (omnplay_instance_t
*)data
;
1215 // g_warning("on_playlist_grid_button");
1217 if(event
->button
==1 && event
->type
==GDK_2BUTTON_PRESS
)
1219 omnplay_playlist_item_edit(app
);
1228 static void library_grid_drag_data_get_cb(GtkWidget
*widget
, GdkDragContext
*context
,
1229 GtkSelectionData
*selection_data
, guint info
, guint time
, gpointer userdata
)
1232 playlist_item_t
* items
;
1233 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1235 g_warning("library_grid_drag_data_get_cb");
1237 items
= omnplay_library_get_selected(app
, &c
);
1242 gtk_selection_data_set(selection_data
, selection_data
->target
, 8,
1243 (const guchar
*)items
, sizeof(playlist_item_t
) * c
);
1248 static void playlist_grid_drag_data_get_cb(GtkWidget
*widget
, GdkDragContext
*context
,
1249 GtkSelectionData
*selection_data
, guint info
, guint time
, gpointer userdata
)
1252 playlist_item_t
* items
;
1253 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1255 list
= omnplay_selected_idxs_playlist(app
);
1258 /* clear delete flag */
1259 for(i
= 0; i
< app
->playlist
.count
; i
++)
1260 app
->playlist
.item
[i
].del
= 0;
1262 items
= (playlist_item_t
*)malloc(sizeof(playlist_item_t
) * list
[0]);
1263 for(i
= 0; i
< list
[0]; i
++)
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;
1269 gtk_selection_data_set(selection_data
, selection_data
->target
, 8,
1270 (const guchar
*)items
, sizeof(playlist_item_t
) * list
[0]);
1276 static void library_grid_drag_begin_cb(GtkWidget
*widget
, GdkDragContext
*context
, gpointer userdata
)
1278 g_warning("library_grid_drag_begin_cb");
1279 gtk_drag_source_set_icon_stock(widget
, GTK_STOCK_DND
);
1282 static void playlist_grid_drag_begin_cb(GtkWidget
*widget
, GdkDragContext
*context
, gpointer userdata
)
1284 g_warning("playlist_grid_drag_begin_cb");
1285 gtk_drag_source_set_icon_stock(widget
, GTK_STOCK_DND
);
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
)
1292 playlist_item_type_t t
;
1293 playlist_item_t
* items
;
1294 GtkTreePath
*path
= NULL
;
1295 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1297 g_warning("playlist_grid_drag_data_received: context->action=%d", context
->action
);
1299 items
= (playlist_item_t
*)gtk_selection_data_get_data(selection_data
);
1300 c
= gtk_selection_data_get_length(selection_data
);
1302 if(c
% sizeof(playlist_item_t
))
1304 g_warning("playlist_grid_drag_data_received: ODD ITEMS");
1308 c
/= sizeof(playlist_item_t
);
1310 if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget
), x
, y
, &path
, NULL
, NULL
, NULL
))
1312 idx
= gtk_tree_path_get_indices(path
)[0];
1313 gtk_tree_path_free(path
);
1315 g_warning("playlist_grid_drag_data_received: gtk_tree_path_get_indice[0]=%d", idx
);
1317 /* normalize, FIX ME */
1318 idx
--; if(idx
< 0) idx
= 0;
1321 idx
= app
->playlist
.count
;
1323 g_warning("playlist_grid_drag_data_received: idx=%d", idx
);
1325 if(omnplay_playlist_insert_check(app
, idx
, &t
))
1327 for(i
= 0; i
< c
; i
++)
1332 omnplay_playlist_insert_items(app
, idx
, items
, c
);
1336 /* Finish the drag */
1337 gtk_drag_finish(context
, TRUE
, FALSE
, time
);
1340 static void playlist_grid_drag_data_delete(GtkWidget
*widget
, GdkDragContext
*context
, gpointer userdata
)
1343 omnplay_instance_t
* app
= (omnplay_instance_t
*)userdata
;
1345 g_warning("playlist_grid_drag_data_delete");
1347 list
= (int*)malloc(sizeof(int) * MAX_PLAYLIST_ITEMS
);
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
))
1355 g_warning("playlist_grid_drag_data_delete: i=%d, c=%d", i
, c
);
1359 omnplay_playlist_delete_items(app
, list
, c
, 0);
1365 * http://www.mail-archive.com/mahogany-users@lists.sourceforge.net/msg00286.html
1367 static gboolean
playlist_grid_drag_motion(GtkWidget
*widget
, GdkDragContext
*context
,
1368 gint x
, gint y
, guint time
, gpointer data
)
1371 GtkWidget
*source_widget
;
1373 g_warning("playlist_grid_drag_motion");
1375 /* Get source widget and check if it is the same as the
1376 * destination widget.
1378 source_widget
= gtk_drag_get_source_widget(context
);
1379 same
= ((source_widget
== widget
) ? TRUE
: FALSE
);
1381 /* Put additional checks here, perhaps if same is FALSE then
1382 * set the default drag to GDK_ACTION_COPY.
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().
1395 if(context
->actions
== GDK_ACTION_MOVE
)
1396 gdk_drag_status(context
, GDK_ACTION_COPY
, time
);
1398 gdk_drag_status(context
, GDK_ACTION_MOVE
, time
);
1401 gdk_drag_status(context
, context
->actions
, time
);
1406 void omnplay_init(omnplay_instance_t
* app
)
1409 pthread_mutexattr_t attr
;
1411 pthread_mutexattr_init(&attr
);
1412 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
1414 gtk_signal_connect( GTK_OBJECT( app
->window
), "delete-event",
1415 GTK_SIGNAL_FUNC(on_main_window_delete_event
), app
);
1417 gtk_signal_connect( GTK_OBJECT( app
->window
), "destroy",
1418 GTK_SIGNAL_FUNC(on_main_window_destroy
), app
);
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
);
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
);
1430 gtk_signal_connect(GTK_OBJECT(app
->playlist_grid
), "button-press-event",
1431 GTK_SIGNAL_FUNC(on_playlist_grid_button
), app
);
1433 gtk_signal_connect(GTK_OBJECT(app
->library_grid
), "button-press-event",
1434 GTK_SIGNAL_FUNC(on_library_grid_button
), app
);
1437 pthread_mutex_init(&app
->players
.lock
, &attr
);
1438 pthread_mutex_init(&app
->playlist
.lock
, &attr
);
1439 pthread_mutex_init(&app
->library
.lock
, &attr
);
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
);
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
);
1452 omnplay_library_load(app
);
1454 pthread_mutexattr_destroy(&attr
);
1456 /* setup drag n drop source/target */
1457 static GtkTargetEntry drag_targets
[] = { { (char*) "application/playlist_item_t", 0, 0 } };
1459 gtk_drag_source_set(app
->library_grid
, GDK_BUTTON1_MASK
,
1460 drag_targets
, 1, (GdkDragAction
)(GDK_ACTION_COPY
));
1462 gtk_drag_source_set(app
->playlist_grid
, GDK_BUTTON1_MASK
,
1463 drag_targets
, 1, (GdkDragAction
)(GDK_ACTION_COPY
| GDK_ACTION_MOVE
));
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
));
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
);
1477 void omnplay_release(omnplay_instance_t
* app
)
1483 for(i
= 0; i
< app
->players
.count
; i
++)
1484 /* create a omneon status thread */
1485 g_thread_join(app
->players
.item
[i
].thread
);
1488 pthread_mutex_destroy(&app
->players
.lock
);
1491 pthread_mutex_destroy(&app
->playlist
.lock
);
1494 omnplay_library_save(app
);
1496 /* destroy library lock */
1497 pthread_mutex_destroy(&app
->library
.lock
);
1500 void omnplay_playlist_normalize(omnplay_instance_t
* app
)
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
);
1510 void omnplay_set_status(omnplay_instance_t
* app
, char* str
)
1512 gdk_threads_enter();
1513 gtk_label_set_text(GTK_LABEL(app
->status_label
), str
);
1515 gdk_threads_leave();