delete playlist item implemented
[omnplay] / src / omnplay.cpp
index 8c6d525..e35479f 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,152 @@ 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 idx_in_players_range(omnplay_instance_t* app, int idx)
+{
+    int i, r = 0;
+
+    for(i = 0; i < app->players.count && !r; i++)
+    {
+        int a, b;
+
+        a = app->players.item[i].playlist_start;
+        b = app->players.item[i].playlist_length;
+
+        if(b <= 0)
+            continue;
+
+        b = a + b - 1;
+
+        if(idx >= a && idx <= b) r = 1;
+    };
+
+    return r;
+};
+
+static int idxs_in_players_range(omnplay_instance_t* app, int start, int stop)
+{
+    int i, r = 0;
+
+    for(i = 0; i < app->players.count && !r; i++)
+    {
+        int a, b;
+
+        a = app->players.item[i].playlist_start;
+        b = app->players.item[i].playlist_length;
+
+        if(b <= 0)
+            continue;
+
+        b = a + b - 1;
+
+#define IN_RANGE(A,B,C) (A <= C && C <= B)
+        if( IN_RANGE(a,b,start) ||
+            IN_RANGE(a,b,stop) ||
+            IN_RANGE(start,stop,a) ||
+            IN_RANGE(start,stop,b))
+            r = 1;
+    };
+
+    return r;
+};
+
+static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t button)
+{
+    int start, stop, r, i;
+    int* list = get_selected_items_playlist(app);
+
+    if(!list)
+        return;
+
+    pthread_mutex_lock(&app->playlist.lock);
+    pthread_mutex_lock(&app->players.lock);
+
+    start = list[1];
+    stop = list[list[0]];
+
+    if(!idxs_in_players_range(app, start, stop))
+    {
+        int loop = (button == BUTTON_PLAYLIST_BLOCK_LOOP)?OMNPLAY_PLAYLIST_BLOCK_LOOP:0;
+
+        /* update selected item */
+        for(i = start; i <= stop; i++)
+        {
+            int t = OMNPLAY_PLAYLIST_BLOCK_BODY | loop;
+
+            if(i == start)      t |= OMNPLAY_PLAYLIST_BLOCK_BEGIN;
+            if(i == stop)       t |= OMNPLAY_PLAYLIST_BLOCK_END;
+
+            app->playlist.item[i].type = (playlist_item_type_t)t;
+
+            omnplay_playlist_draw_item(app, i);
+        };
+
+        /* update border items */
+        if(!start && !(app->playlist.item[start - 1].type & OMNPLAY_PLAYLIST_BLOCK_END))
+        {
+            app->playlist.item[start - 1].type = (playlist_item_type_t)(OMNPLAY_PLAYLIST_BLOCK_END
+                | app->playlist.item[start - 1].type);
+            omnplay_playlist_draw_item(app, start - 1);
+        };
+        if((stop + 1) < app->playlist.count && !(app->playlist.item[stop + 1].type & OMNPLAY_PLAYLIST_BLOCK_BEGIN))
+        {
+            app->playlist.item[stop + 1].type = (playlist_item_type_t)(OMNPLAY_PLAYLIST_BLOCK_BEGIN
+                | app->playlist.item[stop + 1].type);
+            omnplay_playlist_draw_item(app, stop + 1);
+        };
+    }
+    else
+        fprintf(stderr, "omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
+            start, stop);
+
+    pthread_mutex_unlock(&app->players.lock);
+    pthread_mutex_unlock(&app->playlist.lock);
+
+    free(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)
@@ -229,6 +415,208 @@ static omnplay_player_t *get_player_at_pos(omnplay_instance_t* app, int pos)
     return NULL;
 };
 
+static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, int count)
+{
+    int i, j, idx;
+    GtkTreePath* path;
+
+    pthread_mutex_lock(&app->playlist.lock);
+    pthread_mutex_lock(&app->players.lock);
+
+    for(j = 0; j < count; j++)
+    {
+        idx = idxs[j] - j;
+
+        /* fix block types */
+        if(!idx)
+            app->playlist.item[idx - 1].type = (playlist_item_type_t)(app->playlist.item[idx - 1].type |
+                OMNPLAY_PLAYLIST_BLOCK_END);
+        if(idx + 1 < app->playlist.count)
+            app->playlist.item[idx + 1].type = (playlist_item_type_t)(app->playlist.item[idx + 1].type |
+                OMNPLAY_PLAYLIST_BLOCK_BEGIN);
+
+        /* shift playlist items */
+        memmove
+        (
+            &app->playlist.item[idx],
+            &app->playlist.item[idx + 1],
+            (app->playlist.count - idx - 1) * sizeof(playlist_item_t)
+        );
+
+        /* decrement items count */
+        app->playlist.count--;
+
+        /* increment servers indexes */
+        for(i = 0; i < app->players.count; i++)
+            if(app->players.item[i].playlist_start >= idx)
+                app->players.item[i].playlist_start--;
+
+
+    };
+
+    /* redraw playlist */
+    omnplay_playlist_draw(app);
+
+    /* select */
+    path = gtk_tree_path_new_from_indices(idxs[0], -1);
+    gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
+    gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
+    gtk_tree_path_free(path);
+
+
+    pthread_mutex_unlock(&app->players.lock);
+    pthread_mutex_unlock(&app->playlist.lock);
+};
+
+static void omnplay_playlist_item_del(omnplay_instance_t* app)
+{
+    int i, idx, c;
+    int *list, *list2;
+
+    list = get_selected_items_playlist(app);
+    if(!list) return;
+
+    list2 = (int*)malloc(sizeof(int) * list[0]);
+
+    for(i = 0, c = 0; i < list[0]; i++)
+    {
+        /* check for playing block */
+        if(idx_in_players_range(app, list[i + 1]))
+            continue;
+
+        /* save index */
+        list2[c++] = list[i + 1];
+    };
+
+    if(c)
+        omnplay_playlist_delete_items(app, list2, c);
+
+    free(list2);
+    free(list);
+};
+
+static int omnplay_playlist_insert_check(omnplay_instance_t* app, int idx, playlist_item_type_t* t)
+{
+    *t = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
+
+    /* before or after playlist */
+    if(!idx || idx == app->playlist.count)
+        return 1;
+
+    /* check for block borders */
+    if( app->playlist.item[idx - 1].type & OMNPLAY_PLAYLIST_BLOCK_END &&
+        app->playlist.item[idx + 0].type & OMNPLAY_PLAYLIST_BLOCK_BEGIN)
+        return 1;
+
+    /* check for playing block */
+    if(idx_in_players_range(app, idx))
+        return 0;
+
+    if(app->playlist.item[idx].type & OMNPLAY_PLAYLIST_BLOCK_LOOP)
+        *t = OMNPLAY_PLAYLIST_ITEM_LOOP_BODY;
+    else
+        *t = OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY;
+
+    return 1;
+};
+
+static void omnplay_playlist_insert_items(omnplay_instance_t* app, int idx,
+    playlist_item_t* items, int count)
+{
+    int i;
+    GtkTreePath* path;
+
+    pthread_mutex_lock(&app->playlist.lock);
+    pthread_mutex_lock(&app->players.lock);
+
+    /* shift playlist items */
+    memmove
+    (
+        &app->playlist.item[idx + count],
+        &app->playlist.item[idx],
+        (app->playlist.count - idx) * sizeof(playlist_item_t)
+    );
+
+    /* copy new items */
+    memcpy
+    (
+        &app->playlist.item[idx],
+        items,
+        count * sizeof(playlist_item_t)
+    );
+
+    /* increment servers indexes */
+    for(i = 0; i < app->players.count; i++)
+        if(app->players.item[i].playlist_start >= idx)
+            app->players.item[i].playlist_start += idx;
+
+    /* increment items count */
+    app->playlist.count += count;
+
+    /* redraw playlist */
+    omnplay_playlist_draw(app);
+
+    /* select */
+    path = gtk_tree_path_new_from_indices(idx + count, -1);
+    gtk_tree_selection_select_path(gtk_tree_view_get_selection(GTK_TREE_VIEW(app->playlist_grid)), path);
+    gtk_tree_view_set_cursor(GTK_TREE_VIEW(app->playlist_grid), path, NULL, FALSE);
+    gtk_tree_path_free(path);
+
+    pthread_mutex_unlock(&app->players.lock);
+    pthread_mutex_unlock(&app->playlist.lock);
+};
+
+static void omnplay_playlist_item_add(omnplay_instance_t* app, int after)
+{
+    int idx;
+    playlist_item_t item;
+    playlist_item_type_t t;
+
+    /* find insert position */
+    idx = get_first_selected_item_playlist(app);
+    if(idx < 0)
+        idx = 0;
+    else
+        idx += (after)?1:0;
+
+    if(!omnplay_playlist_insert_check(app, idx, &t))
+        return;
+
+    fprintf(stderr, "allowed insert into idx=%d\n", idx);
+
+    /* clear item */
+    memset(&item, 0, sizeof(playlist_item_t));
+    if(ui_playlist_item_dialog(app, &item))
+    {
+        item.type = t;
+        omnplay_playlist_insert_items(app, idx, &item, 1);
+    };
+};
+
+static void omnplay_playlist_item_edit(omnplay_instance_t* app)
+{
+    int idx;
+    playlist_item_t item;
+
+    /* find insert position */
+    idx = get_first_selected_item_playlist(app);
+
+    if(idx < 0)
+        return;
+
+    /* check for playing block */
+    if(idx_in_players_range(app, idx))
+        return;
+
+    item = app->playlist.item[idx];
+
+    if(ui_playlist_item_dialog(app, &item))
+    {
+        app->playlist.item[idx] = item;
+        omnplay_playlist_draw_item(app, idx);
+    };
+};
+
 static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
 {
     int i, r;
@@ -271,8 +659,7 @@ 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_length = -1;
         OmPlrDetachAllClips((OmPlrHandle)player->handle);
     };
 
@@ -353,12 +740,17 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
             OmPlrGetPlayerStatus((OmPlrHandle)player->handle, &hs);
             OmPlrSetPos((OmPlrHandle)player->handle, hs.minPos + p);
 
-            /* Cue */
-            OmPlrCuePlay((OmPlrHandle)player->handle, 0.0);
-            OmPlrPlay((OmPlrHandle)player->handle, 0.0);
+            /* setup loop */
+            if(app->playlist.item[start].type & OMNPLAY_PLAYLIST_BLOCK_LOOP)
+                OmPlrLoop((OmPlrHandle)player->handle, hs.minPos, hs.maxPos);
+            else
+                OmPlrLoop((OmPlrHandle)player->handle, hs.minPos, hs.minPos);
 
             player->playlist_start = start;
-            player->playlist_count = stop - start + 1;
+            player->playlist_length = stop - start + 1;
+
+            /* Cue */
+            OmPlrCuePlay((OmPlrHandle)player->handle, 0.0);
         };
     };
 
@@ -382,8 +774,13 @@ static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t
     switch(button)
     {
         case BUTTON_PLAYLIST_ITEM_ADD:
+            omnplay_playlist_item_add(app, 0);
+            break;
         case BUTTON_PLAYLIST_ITEM_DEL:
+            omnplay_playlist_item_del(app);
+            break;
         case BUTTON_PLAYLIST_ITEM_EDIT:
+            omnplay_playlist_item_edit(app);
             break;
         case BUTTON_PLAYLIST_LOAD:
             omnplay_playlist_load(app);
@@ -393,6 +790,7 @@ static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t
             break;
         case BUTTON_PLAYLIST_BLOCK_SINGLE:
         case BUTTON_PLAYLIST_BLOCK_LOOP:
+            omnplay_playlist_block(app, button);
             break;
         case BUTTON_PLAYLIST_ITEM_UP:
         case BUTTON_PLAYLIST_ITEM_DOWN: