fix multiple selection index getting
[omnplay] / src / omnplay.cpp
index 8c6d525..8144bd8 100644 (file)
@@ -68,8 +68,25 @@ void omnplay_destroy(omnplay_instance_t* app)
     free(app);
 };
 
+static int find_index_of_playlist_item(omnplay_instance_t* app, int start, int idx)
+{
+    while(1)
+    {
+        if(app->playlist.item[start].omn_idx == idx)
+            return start;
+
+        if(app->playlist.item[start].type & OMNPLAY_PLAYLIST_BLOCK_END)
+            break;
+
+        start++;
+    };
+
+    return -1;
+};
+
 static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev , OmPlrStatus *curr)
 {
+    int idx;
     char tc_cur[32], tc_rem[32], state[32], status[32];
     const char *clip;
 
@@ -98,6 +115,7 @@ static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev ,
         strcpy(status, "OFFLINE");
     };
 
+    /* update status in status page */
     gdk_threads_enter();
     gtk_label_set_text(GTK_LABEL (player->label_tc_cur), tc_cur);
     gtk_label_set_text(GTK_LABEL (player->label_tc_rem), tc_rem);
@@ -107,6 +125,42 @@ static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev ,
     gdk_flush();
     gdk_threads_leave();
 
+    /* update remaining time */
+    gdk_threads_enter();
+    pthread_mutex_lock(&player->app->playlist.lock);
+    pthread_mutex_lock(&player->app->players.lock);
+    if(curr->state == omPlrStatePlay || curr->state == omPlrStateCuePlay)
+    {
+        idx = find_index_of_playlist_item(player->app, player->playlist_start, curr->currClipNum);
+        if(idx >= 0)
+        {
+            frames2tc(curr->currClipStartPos + curr->currClipLen - curr->pos, 25.0, tc_rem);
+            omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
+        }
+        if(curr->currClipNum != prev->currClipNum && 1 != prev->numClips)
+        {
+            tc_rem[0] = 0;
+            idx = find_index_of_playlist_item(player->app, player->playlist_start, prev->currClipNum);
+            if(idx >= 0)
+                omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
+        };
+    }
+    else
+    {
+        tc_rem[0] = 0;
+        idx = find_index_of_playlist_item(player->app, player->playlist_start, curr->currClipNum);
+        if(idx >= 0)
+            omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
+        idx = find_index_of_playlist_item(player->app, player->playlist_start, prev->currClipNum);
+        if(idx >= 0)
+            omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
+    };
+    pthread_mutex_unlock(&player->app->players.lock);
+    pthread_mutex_unlock(&player->app->playlist.lock);
+    gdk_flush();
+    gdk_threads_leave();
+
+
     memcpy(prev, curr, sizeof(OmPlrStatus));
 };
 
@@ -179,20 +233,48 @@ static void* omnplay_thread_proc(void* data)
     return NULL;
 };
 
-static int get_first_selected_item_playlist(omnplay_instance_t* app)
+void get_selected_items_playlist_proc(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 {
-    int idx;
-    GtkTreeIter iter;
-    GtkTreeModel *model;
+    int idx, *list = (int*)data;
+    gtk_tree_model_get(model, iter, 7, &idx, -1);
+    list[list[0] + 1] = idx;
+    list[0] = list[0] + 1;
+};
+
+static int* get_selected_items_playlist(omnplay_instance_t* app)
+{
+    int* list = NULL;
     GtkTreeSelection *selection;
 
     selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid));
-    if(selection && gtk_tree_selection_get_selected(selection, &model, &iter))
+    if(selection)
     {
-        gtk_tree_model_get(model, &iter, 7, &idx, -1);
-        return idx;
+        list = (int*)malloc(sizeof(int) * (MAX_PLAYLIST_ITEMS + 1));
+        memset(list, 0, sizeof(int) * (MAX_PLAYLIST_ITEMS + 1));
+
+        gtk_tree_selection_selected_foreach(
+            selection,
+            get_selected_items_playlist_proc,
+            list);
+
+        if(!list[0])
+        {
+            free(list);
+            list = NULL;
+        };
     };
-    return -1;
+
+    return list;
+};
+
+static int get_first_selected_item_playlist(omnplay_instance_t* app)
+{
+    int idx;
+    int* list = get_selected_items_playlist(app);
+    if(!list) return -1;
+    idx = list[1];
+    free(list);
+    return idx;
 };
 
 static int get_playlist_block(omnplay_instance_t* app, int idx, int* start_ptr, int* stop_ptr)
@@ -271,8 +353,8 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
         OmPlrStop((OmPlrHandle)player->handle);
 
         /* detach previous clips */
-        player->playlist_start = -1;
-        player->playlist_count = -1;
+//        player->playlist_start = -1;
+//        player->playlist_count = -1;
         OmPlrDetachAllClips((OmPlrHandle)player->handle);
     };
 
@@ -353,12 +435,15 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
             OmPlrGetPlayerStatus((OmPlrHandle)player->handle, &hs);
             OmPlrSetPos((OmPlrHandle)player->handle, hs.minPos + p);
 
+            /* setup loop */
+            if(app->playlist.item[start].type & OMNPLAY_PLAYLIST_BLOCK_LOOP)
+                OmPlrLoop((OmPlrHandle)player->handle, hs.minPos, hs.maxPos);
+
+            player->playlist_start = start;
+
             /* Cue */
             OmPlrCuePlay((OmPlrHandle)player->handle, 0.0);
             OmPlrPlay((OmPlrHandle)player->handle, 0.0);
-
-            player->playlist_start = start;
-            player->playlist_count = stop - start + 1;
         };
     };