fix playlist closing
[omnplay] / src / omnplay.cpp
index b9badfb..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,16 +233,22 @@ 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);
     pthread_mutex_unlock(&player->app->players.lock);
     if(r)
     {
-        fprintf(stderr, "ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
+        g_warning("ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
             player->host, player->name, r);
 
         return (void*)r;
@@ -250,7 +268,7 @@ static void* omnplay_thread_proc(void* data)
 
         if(r)
         {
-            fprintf(stderr, "ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
+            g_warning("ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
                 player->app->players.path, r);
 
             pthread_mutex_lock(&player->app->players.lock);
@@ -265,7 +283,11 @@ static void* omnplay_thread_proc(void* data)
     for(r = 0 ; !player->app->f_exit && !r;)
     {
         /* sleep */
+#ifdef _WIN32
+        Sleep(100);
+#else
         usleep(100000);
+#endif
 
         /* get status */
         pthread_mutex_lock(&player->app->players.lock);
@@ -274,10 +296,13 @@ static void* omnplay_thread_proc(void* data)
         pthread_mutex_unlock(&player->app->players.lock);
 
         if(r)
-            fprintf(stderr, "ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", 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);
@@ -295,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;
@@ -373,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;
@@ -402,7 +427,7 @@ static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t bu
         };
 
         /* update border items */
-        if(!start && !(app->playlist.item[start - 1].type & OMNPLAY_PLAYLIST_BLOCK_END))
+        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);
@@ -416,7 +441,7 @@ static void omnplay_playlist_block(omnplay_instance_t* app, control_buttons_t bu
         };
     }
     else
-        fprintf(stderr, "omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
+        g_warning("omnplay_playlist_block: range [%d %d] do OVERLAP player\n",
             start, stop);
 
     pthread_mutex_unlock(&app->players.lock);
@@ -428,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);
@@ -447,7 +472,7 @@ static int get_playlist_block(omnplay_instance_t* app, int idx, int* start_ptr,
         if(app->playlist.item[stop].type & OMNPLAY_PLAYLIST_BLOCK_END)
             break;
 
-    fprintf(stderr, "get_playlist_block: range %d -> %d\n", start, stop);
+    g_warning("get_playlist_block: range %d -> %d\n", start, stop);
 
     /* check block range */
     if(start >= 0 && stop < app->playlist.count)
@@ -469,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);
@@ -482,12 +506,16 @@ static void omnplay_playlist_delete_items(omnplay_instance_t* app, int* idxs, in
         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);
+        if( app->playlist.item[idx].type != OMNPLAY_PLAYLIST_ITEM_BLOCK_BODY &&
+            app->playlist.item[idx].type != OMNPLAY_PLAYLIST_ITEM_LOOP_BODY)
+        {
+            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
@@ -512,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);
@@ -527,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]);
@@ -543,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;
@@ -611,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);
@@ -636,7 +693,7 @@ static void omnplay_playlist_item_add(omnplay_instance_t* app, int after)
     if(!omnplay_playlist_insert_check(app, idx, &t))
         return;
 
-    fprintf(stderr, "allowed insert into idx=%d\n", idx);
+    g_warning("allowed insert into idx=%d\n", idx);
 
     /* clear item */
     memset(&item, 0, sizeof(playlist_item_t));
@@ -689,7 +746,7 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
         return;
     };
 
-    fprintf(stderr, "cue: selected item is %d\n", idx);
+    g_warning("cue: selected item is %d\n", idx);
 
     if(get_playlist_block(app, idx, &start, &stop) < 0)
     {
@@ -697,7 +754,7 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
         return;
     };
 
-    fprintf(stderr, "cue: range %d -> %d\n", start, stop);
+    g_warning("cue: range %d -> %d\n", start, stop);
 
     player = get_player_at_pos(app, start);
 
@@ -735,24 +792,30 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
 
             if(!r)
             {
-                unsigned int l;
+                unsigned int l = 0;
 
-                fprintf(stderr, "OmPlrClipGetInfo(%s): firstFrame=%d, lastFrame=%d\n",
+                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)
                 {
-                    fprintf(stderr, "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);
                 };
 
@@ -765,7 +828,7 @@ static void omnplay_ctl(omnplay_instance_t* app, control_buttons_t button)
 
             if(r)
             {
-                fprintf(stderr, "cue: failed with %d, %s\n", r, OmPlrGetErrorString((OmPlrError)r));
+                g_warning("cue: failed with %d, %s\n", r, OmPlrGetErrorString((OmPlrError)r));
                 app->playlist.item[i].omn_idx = -1;
                 app->playlist.item[i].omn_offset = -1;
                 app->playlist.item[i].error |= PLAYLIST_ITEM_ERROR_CUE;
@@ -971,6 +1034,15 @@ static gboolean omnplay_button_click(omnplay_instance_t* app, control_buttons_t
         case BUTTON_LIBRARY_REFRESH:
             omnplay_library_refresh(app);
             break;
+        case BUTTON_LIBRARY_FIND:
+            omnplay_library_search(app, 0);
+            break;
+        case BUTTON_LIBRARY_FIND_NEXT:
+            omnplay_library_search(app, 1);
+            break;
+        case BUTTON_PLAYLIST_RELINK:
+            omnplay_playlist_relink(app);
+            break;
     };
 
     return TRUE;
@@ -988,6 +1060,48 @@ static gboolean on_button_click(GtkWidget *button, gpointer user_data)
     return FALSE;
 };
 
+static void omnplay_playlist_item_copy(omnplay_instance_t* app)
+{
+    int *list, i;
+
+    list = omnplay_selected_idxs_playlist(app);
+    if(!list) return;
+
+    for(i = 0; i < list[0]; i++)
+        app->clipboard.item[i] = app->playlist.item[list[i + 1]];
+    app->clipboard.count = list[0];
+
+    free(list);
+};
+
+static void omnplay_playlist_item_paste(omnplay_instance_t* app, int after)
+{
+    int idx, i;
+    playlist_item_t* items;
+    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;
+
+    /* clear item */
+    if(app->clipboard.count)
+    {
+        for(i = 0; i < app->clipboard.count; i++)
+        {
+            app->clipboard.item[i].type = t;
+            app->clipboard.item[i].error = 0;
+        };
+        omnplay_playlist_insert_items(app, idx, app->clipboard.item, app->clipboard.count);
+    };
+};
+
 static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
     omnplay_instance_t* app = (omnplay_instance_t*)data;
@@ -998,7 +1112,7 @@ static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpoi
         case GDK_c:
             if(event->state & GDK_CONTROL_MASK)
             {
-                fprintf(stderr, "CTRL+c\n");
+                omnplay_playlist_item_copy(app);
                 return TRUE;
             };
             break;
@@ -1006,7 +1120,7 @@ static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpoi
         case GDK_v:
             if(event->state & GDK_CONTROL_MASK)
             {
-                fprintf(stderr, "CTRL+v\n");
+                omnplay_playlist_item_paste(app, 0);
                 return TRUE;
             };
             break;
@@ -1014,7 +1128,8 @@ static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpoi
         case GDK_x:
             if(event->state & GDK_CONTROL_MASK)
             {
-                fprintf(stderr, "CTRL+x\n");
+                omnplay_playlist_item_copy(app);
+                omnplay_playlist_item_del(app);
                 return TRUE;
             };
             break;
@@ -1034,6 +1149,20 @@ static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpoi
                 return TRUE;
             };
             break;
+        case GDK_KEY_uparrow:
+            if(event->state & GDK_CONTROL_MASK)
+            {
+                omnplay_playlist_item_swap(app, -1);
+                return TRUE;
+            };
+            break;
+        case GDK_KEY_downarrow:
+            if(event->state & GDK_CONTROL_MASK)
+            {
+                omnplay_playlist_item_swap(app, -1);
+                return TRUE;
+            };
+            break;
         case GDK_KEY_space:
             omnplay_ctl(app, BUTTON_PLAYER_PLAY);
             return TRUE;
@@ -1046,7 +1175,8 @@ static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpoi
         case GDK_KEY_Delete:
             omnplay_playlist_item_del(app);
             return TRUE;
-        case GDK_KEY_BackSpace:
+        case GDK_E:
+        case GDK_e:
             omnplay_playlist_item_edit(app);
             return TRUE;
     };
@@ -1054,11 +1184,275 @@ static gboolean on_playlist_grid_key(GtkWidget *widget, GdkEventKey *event, gpoi
     return FALSE;
 };
 
+static gboolean on_library_grid_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
+{
+    omnplay_instance_t* app = (omnplay_instance_t*)data;
+
+    switch(event->keyval)
+    {
+        case GDK_C:
+        case GDK_c:
+            if(event->state & GDK_CONTROL_MASK)
+            {
+                int count;
+                playlist_item_t* items;
+
+                items = omnplay_library_get_selected(app, &count);
+
+                if(items)
+                {
+                    int i;
+
+                    for(i = 0; i < count; i++)
+                        app->clipboard.item[i] = items[i];
+
+                    app->clipboard.count = count;
+                };
+
+                return TRUE;
+            };
+            break;
+        case GDK_V:
+        case GDK_v:
+            if(event->state & GDK_CONTROL_MASK)
+            {
+                g_warning("CTRL+v\n");
+                return TRUE;
+            };
+            break;
+        case GDK_X:
+        case GDK_x:
+            if(event->state & GDK_CONTROL_MASK)
+            {
+                g_warning("CTRL+x\n");
+                return TRUE;
+            };
+            break;
+        case GDK_KEY_BackSpace:
+            omnplay_library_add(app, 0);
+            return TRUE;
+        case GDK_KEY_F5:
+            omnplay_library_refresh(app);
+            return TRUE;
+    };
+
+    return FALSE;
+};
+
+static gboolean on_library_grid_button(GtkWidget *widget, GdkEventButton *event, gpointer data)
+{
+//    g_warning("on_library_grid_button: event->button=%d, event->type=%d", event->button, event->type);
+
+    if(event->button==1 && event->type==GDK_2BUTTON_PRESS)
+    {
+        omnplay_library_add((omnplay_instance_t* )data, 0);
+        return TRUE;
+    };
+
+    return FALSE;
+};
+
+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_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;
     pthread_mutexattr_t attr;
 
+    pthread_mutexattr_init(&attr);
     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
 
     gtk_signal_connect( GTK_OBJECT( app->window ), "delete-event",
@@ -1068,42 +1462,71 @@ void omnplay_init(omnplay_instance_t* app)
         GTK_SIGNAL_FUNC(on_main_window_destroy), app);
 
     gtk_widget_add_events(app->playlist_grid, GDK_BUTTON_PRESS_MASK);
+    gtk_widget_add_events(app->playlist_grid, GDK_KEY_PRESS_MASK);
     gtk_signal_connect(GTK_OBJECT(app->playlist_grid), "key-press-event",
         GTK_SIGNAL_FUNC(on_playlist_grid_key), app);
 
+    gtk_widget_add_events(app->library_grid, GDK_BUTTON_PRESS_MASK);
+    gtk_widget_add_events(app->library_grid, GDK_KEY_PRESS_MASK);
+    gtk_signal_connect(GTK_OBJECT(app->library_grid), "key-press-event",
+        GTK_SIGNAL_FUNC(on_library_grid_key), app);
+
+    gtk_signal_connect(GTK_OBJECT(app->playlist_grid), "button-press-event",
+        GTK_SIGNAL_FUNC(on_playlist_grid_button), app);
+
+    gtk_signal_connect(GTK_OBJECT(app->library_grid), "button-press-event",
+        GTK_SIGNAL_FUNC(on_library_grid_button), app);
+
     /* create lock */
     pthread_mutex_init(&app->players.lock, &attr);
+    pthread_mutex_init(&app->playlist.lock, &attr);
+    pthread_mutex_init(&app->library.lock, &attr);
 
     /* create a omneon status thread */
     for(i = 0; i < app->players.count; i++)
-        pthread_create(&app->players.item[i].thread, NULL,
-            omnplay_thread_proc, &app->players.item[i]);
-
-    /* create lock */
-    pthread_mutex_init(&app->playlist.lock, &attr);
+        app->players.item[i].thread = g_thread_create(
+            omnplay_thread_proc, &app->players.item[i], TRUE, NULL);
 
     /* attach buttons click */
     for(i = 1; i < BUTTON_LAST; i++)
         gtk_signal_connect(GTK_OBJECT(app->buttons[i]), "clicked",
             GTK_SIGNAL_FUNC( on_button_click), app );
 
-    /* create lock */
-    pthread_mutex_init(&app->library.lock, &attr);
-
     /* load library */
     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)
 {
     int i;
-    void* r;
 
     app->f_exit = 1;
 
     for(i = 0; i < app->players.count; i++)
         /* create a omneon status thread */
-        pthread_join(app->players.item[i].thread, &r);
+        g_thread_join(app->players.item[i].thread);
 
     /* destroy lock */
     pthread_mutex_destroy(&app->players.lock);
@@ -1127,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();
+};
+