fix playlist closing
[omnplay] / src / omnplay.cpp
index 801cbaa..6189862 100644 (file)
 #include "opts.h"
 #include "timecode.h"
 
+#if defined(_WIN32)
+#include <windows.h>
+#endif
+
 #include "omplrclnt.h"
 
 int omnplay_get_content(omnplay_instance_t* app, playlist_item_t *items, int limit,
@@ -46,6 +50,8 @@ int omnplay_get_content(omnplay_instance_t* app, playlist_item_t *items, int lim
     OmPlrClipInfo clip_info;
     char clip_name[omPlrMaxClipDirLen];
 
+    app->library.id_display_idx = 0;
+
     pthread_mutex_lock(&app->players.lock);
 
     r = OmPlrClipGetFirst((OmPlrHandle)app->players.item[0].handle, clip_name, sizeof(clip_name));
@@ -124,6 +130,9 @@ void omnplay_destroy(omnplay_instance_t* app)
 
 static int find_index_of_playlist_item(omnplay_instance_t* app, int start, int idx)
 {
+    if(start < 0 || start >= app->playlist.count)
+        return -1;
+
     while(1)
     {
         if(app->playlist.item[start].omn_idx == idx)
@@ -138,7 +147,7 @@ static int find_index_of_playlist_item(omnplay_instance_t* app, int start, int i
     return -1;
 };
 
-static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev , OmPlrStatus *curr)
+static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev , OmPlrStatus *curr, int *playlist_start_prev)
 {
     int idx;
     char tc_cur[32], tc_rem[32], state[32], status[32];
@@ -183,31 +192,34 @@ static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev ,
     gdk_threads_enter();
     pthread_mutex_lock(&player->app->playlist.lock);
     pthread_mutex_lock(&player->app->players.lock);
-    if(curr->state == omPlrStatePlay || curr->state == omPlrStateCuePlay)
+
+    /* check if playlist exist */
+    if(player->playlist_length)
     {
-        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)
+        /* clear remain on "previous" item */
+        if((curr->currClipNum != prev->currClipNum && 1 != prev->numClips) ||
+            (*playlist_start_prev != player->playlist_start))
         {
             tc_rem[0] = 0;
-            idx = find_index_of_playlist_item(player->app, player->playlist_start, prev->currClipNum);
+            idx = find_index_of_playlist_item(player->app, *playlist_start_prev, prev->currClipNum);
             if(idx >= 0)
                 omnplay_playlist_draw_item_rem(player->app, idx, tc_rem);
         };
-    }
-    else
-    {
-        tc_rem[0] = 0;
+
+        /* update current item */
         idx = find_index_of_playlist_item(player->app, player->playlist_start, curr->currClipNum);
         if(idx >= 0)
+        {
+            /* reset value */
+            tc_rem[0] = 0;
+
+            /* for play and cue calc new value */
+            if(curr->state == omPlrStatePlay || curr->state == omPlrStateCuePlay)
+                frames2tc(curr->currClipStartPos + curr->currClipLen - curr->pos, 25.0, tc_rem);
+
+            /* setup that value */
             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);
@@ -221,11 +233,15 @@ static void omnplay_update_status(omnplay_player_t* player, OmPlrStatus *prev ,
 static void* omnplay_thread_proc(void* data)
 {
     int r;
+    int playlist_start_prev = 0;
     OmPlrStatus st_curr, st_prev;
     omnplay_player_t* player = (omnplay_player_t*)data;
 
     g_warning("omnplay_thread_proc\n");
 
+    memset(&st_curr, 0, sizeof(OmPlrStatus));
+    memset(&st_prev, 0, sizeof(OmPlrStatus));
+
     /* connect */
     pthread_mutex_lock(&player->app->players.lock);
     r = OmPlrOpen(player->host, player->name, (OmPlrHandle*)&player->handle);
@@ -282,8 +298,11 @@ static void* omnplay_thread_proc(void* data)
         if(r)
             g_warning("ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", r);
         else
-            if(memcmp(&st_curr, &st_prev, sizeof(OmPlrStatus)))
-                omnplay_update_status(player, &st_prev , &st_curr);
+        {
+            omnplay_update_status(player, &st_prev , &st_curr, &playlist_start_prev);
+            playlist_start_prev = player->playlist_start;
+            memcmp(&st_curr, &st_prev, sizeof(OmPlrStatus));
+        };
     };
 
     pthread_mutex_lock(&player->app->players.lock);
@@ -301,7 +320,7 @@ void get_selected_items_playlist_proc(GtkTreeModel *model, GtkTreePath *path, Gt
     list[0] = list[0] + 1;
 };
 
-static int* get_selected_items_playlist(omnplay_instance_t* app)
+int* omnplay_selected_idxs_playlist(omnplay_instance_t* app)
 {
     int* list = NULL;
     GtkTreeSelection *selection;
@@ -379,7 +398,7 @@ static int idxs_in_players_range(omnplay_instance_t* app, int start, int stop)
 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);
+    int* list = omnplay_selected_idxs_playlist(app);
 
     if(!list)
         return;
@@ -434,7 +453,7 @@ static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t bu
 static int get_first_selected_item_playlist(omnplay_instance_t* app)
 {
     int idx;
-    int* list = get_selected_items_playlist(app);
+    int* list = omnplay_selected_idxs_playlist(app);
     if(!list) return -1;
     idx = list[1];
     free(list);
@@ -475,10 +494,9 @@ 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)
+static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, int count, int sel)
 {
     int i, j, idx;
-    GtkTreePath* path;
 
     pthread_mutex_lock(&app->playlist.lock);
     pthread_mutex_lock(&app->players.lock);
@@ -522,11 +540,14 @@ static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, in
     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);
-
+    if(sel)
+    {
+        GtkTreePath* path;
+        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);
@@ -537,7 +558,7 @@ static void omnplay_playlist_item_del(omnplay_instance_t* app)
     int i, idx, c;
     int *list, *list2;
 
-    list = get_selected_items_playlist(app);
+    list = omnplay_selected_idxs_playlist(app);
     if(!list) return;
 
     list2 = (int*)malloc(sizeof(int) * list[0]);
@@ -553,12 +574,38 @@ static void omnplay_playlist_item_del(omnplay_instance_t* app)
     };
 
     if(c)
-        omnplay_playlist_delete_items(app, list2, c);
+        omnplay_playlist_delete_items(app, list2, c, 1);
 
     free(list2);
     free(list);
 };
 
+void omnplay_playlist_relink(omnplay_instance_t* app)
+{
+    int i, idx, c;
+    int *list;
+
+    pthread_mutex_lock(&app->playlist.lock);
+    list = omnplay_selected_idxs_playlist(app);
+    if(list)
+    {
+        for(i = 0, c = 0; i < list[0]; i++)
+        {
+            /* check for playing block */
+            if(idx_in_players_range(app, list[i + 1]))
+                continue;
+            /* relink item */
+            omnplay_library_relink_item(app, &app->playlist.item[list[i + 1]]);
+        };
+
+        free(list);
+    };
+    pthread_mutex_unlock(&app->playlist.lock);
+
+    /* redraw playlist */
+    omnplay_playlist_draw(app);
+};
+
 static int omnplay_playlist_insert_check(omnplay_instance_t* app, int idx, playlist_item_type_t* t)
 {
     *t = OMNPLAY_PLAYLIST_ITEM_BLOCK_SINGLE;
@@ -621,7 +668,7 @@ static void omnplay_playlist_insert_items(omnplay_instance_t* app, int idx,
     omnplay_playlist_draw(app);
 
     /* select */
-    path = gtk_tree_path_new_from_indices(idx + count, -1);
+    path = gtk_tree_path_new_from_indices(idx, -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);
@@ -745,24 +792,30 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
 
             if(!r)
             {
-                unsigned int l;
+                unsigned int l = 0;
 
                 g_warning("OmPlrClipGetInfo(%s): firstFrame=%d, lastFrame=%d\n",
                     app->playlist.item[i].id, clip.firstFrame, clip.lastFrame);
 
-                /* should we fix playlist clip timings */
-                if(!(
-                    app->playlist.item[i].in >= clip.firstFrame &&
-                    app->playlist.item[i].in + app->playlist.item[i].dur <= clip.lastFrame) ||
-                    !app->playlist.item[i].dur)
+                /* fix IN */
+                if(app->playlist.item[i].in < clip.firstFrame || app->playlist.item[i].in > clip.lastFrame)
                 {
-                    g_warning("cue: item [%s] will be updated [%d;%d]->[%d;%d]\n",
-                        app->playlist.item[i].id,
-                        app->playlist.item[i].in, app->playlist.item[i].dur,
-                        clip.firstFrame, clip.lastFrame - clip.firstFrame);
-
                     app->playlist.item[i].in = clip.firstFrame;
-                    app->playlist.item[i].dur = clip.lastFrame - clip.firstFrame;
+                    l++;
+                };
+
+                /* fix DUR */
+                if(app->playlist.item[i].in + app->playlist.item[i].dur > clip.lastFrame || !app->playlist.item[i].dur)
+                {
+                    app->playlist.item[i].dur = clip.firstFrame - app->playlist.item[i].in;
+                    l++;
+                };
+
+                /* notify */
+                if(l)
+                {
+                    g_warning("cue: item [%s] will be updated to [%d;%d]\n",
+                        app->playlist.item[i].id, app->playlist.item[i].in, app->playlist.item[i].dur);
                     omnplay_playlist_draw_item(app, i);
                 };
 
@@ -987,6 +1040,9 @@ static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t
         case BUTTON_LIBRARY_FIND_NEXT:
             omnplay_library_search(app, 1);
             break;
+        case BUTTON_PLAYLIST_RELINK:
+            omnplay_playlist_relink(app);
+            break;
     };
 
     return TRUE;
@@ -1008,7 +1064,7 @@ static void omnplay_playlist_item_copy(omnplay_instance_t* app)
 {
     int *list, i;
 
-    list = get_selected_items_playlist(app);
+    list = omnplay_selected_idxs_playlist(app);
     if(!list) return;
 
     for(i = 0; i < list[0]; i++)
@@ -1198,17 +1254,199 @@ static gboolean on_library_grid_button(GtkWidget *widget, GdkEventButton *event,
 
 static gboolean on_playlist_grid_button(GtkWidget *widget, GdkEventButton *event, gpointer data)
 {
+    omnplay_instance_t* app = (omnplay_instance_t*)data;
+
 //    g_warning("on_playlist_grid_button");
 
     if(event->button==1 && event->type==GDK_2BUTTON_PRESS)
     {
-        omnplay_ctl((omnplay_instance_t* )data, BUTTON_PLAYER_CUE);
+        omnplay_playlist_item_edit(app);
         return TRUE;
     };
 
     return FALSE;
 };
 
+
+
+static void library_grid_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
+    GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
+{
+    int c;
+    playlist_item_t* items;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    g_warning("library_grid_drag_data_get_cb");
+
+    items = omnplay_library_get_selected(app, &c);
+
+    /* clear item */
+    if(items)
+    {
+        gtk_selection_data_set(selection_data, selection_data->target, 8,
+            (const guchar *)items, sizeof(playlist_item_t) * c);
+        free(items);
+    };
+};
+
+static void playlist_grid_drag_data_get_cb(GtkWidget *widget, GdkDragContext *context,
+    GtkSelectionData *selection_data, guint info, guint time, gpointer userdata)
+{
+    int *list, i;
+    playlist_item_t* items;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    list = omnplay_selected_idxs_playlist(app);
+    if(!list) return;
+
+    /* clear delete flag */
+    for(i = 0; i < app->playlist.count; i++)
+        app->playlist.item[i].del = 0;
+
+    items = (playlist_item_t*)malloc(sizeof(playlist_item_t) * list[0]);
+    for(i = 0; i < list[0]; i++)
+    {
+        items[i] = app->playlist.item[list[i + 1]];
+        if(context->action == GDK_ACTION_MOVE)
+            app->playlist.item[list[i + 1]].del = 1;
+    }
+    gtk_selection_data_set(selection_data, selection_data->target, 8,
+        (const guchar *)items, sizeof(playlist_item_t) * list[0]);
+
+    free(items);
+    free(list);
+};
+
+static void library_grid_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
+{
+    g_warning("library_grid_drag_begin_cb");
+    gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
+};
+
+static void playlist_grid_drag_begin_cb(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
+{
+    g_warning("playlist_grid_drag_begin_cb");
+    gtk_drag_source_set_icon_stock(widget, GTK_STOCK_DND);
+};
+
+static void playlist_grid_drag_data_received(GtkWidget *widget, GdkDragContext *context,
+    gint x, gint y,  GtkSelectionData *selection_data,  guint info, guint time, gpointer userdata)
+{
+    int c, i, idx;
+    playlist_item_type_t t;
+    playlist_item_t* items;
+    GtkTreePath *path = NULL;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    g_warning("playlist_grid_drag_data_received: context->action=%d", context->action);
+
+    items = (playlist_item_t*)gtk_selection_data_get_data(selection_data);
+    c = gtk_selection_data_get_length(selection_data);
+
+    if(c % sizeof(playlist_item_t))
+    {
+        g_warning("playlist_grid_drag_data_received: ODD ITEMS");
+    }
+    else
+    {
+        c /= sizeof(playlist_item_t);
+
+        if(gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y, &path, NULL, NULL, NULL))
+        {
+            idx = gtk_tree_path_get_indices(path)[0];
+            gtk_tree_path_free(path);
+
+            g_warning("playlist_grid_drag_data_received: gtk_tree_path_get_indice[0]=%d", idx);
+
+            /* normalize, FIX ME */
+            idx--; if(idx < 0) idx = 0;
+        }
+        else
+            idx = app->playlist.count;
+
+        g_warning("playlist_grid_drag_data_received: idx=%d", idx);
+
+        if(omnplay_playlist_insert_check(app, idx, &t))
+        {
+            for(i = 0; i < c; i++)
+            {
+                items[i].type = t;
+                items[i].error = 0;
+            };
+            omnplay_playlist_insert_items(app, idx, items, c);
+        };
+    };
+
+    /* Finish the drag */
+    gtk_drag_finish(context, TRUE, FALSE, time);
+};
+
+static void playlist_grid_drag_data_delete(GtkWidget *widget, GdkDragContext *context, gpointer userdata)
+{
+    int c, i, *list;
+    omnplay_instance_t* app = (omnplay_instance_t*)userdata;
+
+    g_warning("playlist_grid_drag_data_delete");
+
+    list = (int*)malloc(sizeof(int) * MAX_PLAYLIST_ITEMS);
+
+    for(i = 0, c = 0; i < app->playlist.count; i++)
+        if(app->playlist.item[i].del)
+            if(!idx_in_players_range(app, i))
+            {
+                /* save index */
+                list[c++] = i;
+                g_warning("playlist_grid_drag_data_delete: i=%d, c=%d", i, c);
+            };
+
+    if(c)
+        omnplay_playlist_delete_items(app, list, c, 0);
+
+    free(list);
+};
+
+/*
+ * http://www.mail-archive.com/mahogany-users@lists.sourceforge.net/msg00286.html
+ */
+static gboolean playlist_grid_drag_motion(GtkWidget *widget, GdkDragContext *context,
+    gint x, gint y, guint time, gpointer data)
+{
+    gboolean same;
+    GtkWidget *source_widget;
+
+    g_warning("playlist_grid_drag_motion");
+
+    /* Get source widget and check if it is the same as the
+     * destination widget.
+     */
+    source_widget = gtk_drag_get_source_widget(context);
+    same = ((source_widget == widget) ? TRUE : FALSE);
+
+    /* Put additional checks here, perhaps if same is FALSE then
+     * set the default drag to GDK_ACTION_COPY.
+     */
+
+    /* Say we just want to allow GDK_ACTION_MOVE, first we check
+     * if that is in the list of allowed actions on the dc. If
+     * so then we set it to that. Note if the user holds down the
+     * ctrl key then the only flag in dc->actions will be
+     * GDK_ACTION_COPY. The constraint for dc->actions is that
+     * specified from the given actions in gtk_drag_dest_set() and
+     * gtk_drag_source_set().
+     */
+    if(same)
+    {
+        if(context->actions == GDK_ACTION_MOVE)
+            gdk_drag_status(context, GDK_ACTION_COPY, time);
+        else
+            gdk_drag_status(context, GDK_ACTION_MOVE, time);
+    }
+    else
+        gdk_drag_status(context, context->actions, time);
+
+    return(TRUE);
+}
+
 void omnplay_init(omnplay_instance_t* app)
 {
     int i;
@@ -1258,6 +1496,26 @@ void omnplay_init(omnplay_instance_t* app)
     omnplay_library_load(app);
 
     pthread_mutexattr_destroy(&attr);
+
+    /* setup drag n drop source/target */
+    static GtkTargetEntry drag_targets[] = { { (char*) "application/playlist_item_t", 0, 0 } };
+
+    gtk_drag_source_set(app->library_grid, GDK_BUTTON1_MASK,
+        drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY));
+
+    gtk_drag_source_set(app->playlist_grid, GDK_BUTTON1_MASK,
+        drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
+
+    gtk_drag_dest_set(app->playlist_grid, (GtkDestDefaults)(GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP),
+        drag_targets, 1, (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE));
+
+    g_signal_connect (app->library_grid, "drag_data_get", G_CALLBACK(library_grid_drag_data_get_cb), app);
+    g_signal_connect (app->playlist_grid, "drag_data_get", G_CALLBACK(playlist_grid_drag_data_get_cb), app);
+    g_signal_connect (app->library_grid, "drag_begin", G_CALLBACK(library_grid_drag_begin_cb), app);
+    g_signal_connect (app->playlist_grid, "drag_begin", G_CALLBACK(playlist_grid_drag_begin_cb), app);
+    g_signal_connect (app->playlist_grid, "drag_data_received", G_CALLBACK (playlist_grid_drag_data_received), app);
+    g_signal_connect (app->playlist_grid, "drag_data_delete", G_CALLBACK (playlist_grid_drag_data_delete), app);
+    g_signal_connect (app->playlist_grid, "drag_motion", G_CALLBACK (playlist_grid_drag_motion), app);
 };
 
 void omnplay_release(omnplay_instance_t* app)
@@ -1292,3 +1550,12 @@ void omnplay_playlist_normalize(omnplay_instance_t* app)
         if(omnplay_library_normalize_item(app, &app->playlist.item[i]))
             omnplay_playlist_draw_item(app, i);
 };
+
+void omnplay_set_status(omnplay_instance_t* app, char* str)
+{
+    gdk_threads_enter();
+    gtk_label_set_text(GTK_LABEL(app->status_label), str);
+    gdk_flush();
+    gdk_threads_leave();
+};
+