From: Kalle Wallin Date: Fri, 7 May 2004 07:49:06 +0000 (+0000) Subject: Added support for moving songs in a playlist (move-up,move-down). X-Git-Tag: v0.12_alpha1~555 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ab032e2b5a5499f783c034eeb64a1dd3f3387a1c;p=ncmpc.git Added support for moving songs in a playlist (move-up,move-down). git-svn-id: https://svn.musicpd.org/ncmpc/trunk@936 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- diff --git a/command.c b/command.c index f900f37..1e9edcd 100644 --- a/command.c +++ b/command.c @@ -57,6 +57,7 @@ extern void screen_resize(void); #define F5 KEY_F(5) #define F6 KEY_F(6) + static command_definition_t cmds[] = { { { 13, 0, 0 }, CMD_PLAY, "play", @@ -101,6 +102,11 @@ static command_definition_t cmds[] = { { 'S', 0, 0 }, CMD_SAVE_PLAYLIST, "save", "Save playlist" }, + { { 0, 0, 0 }, CMD_LIST_MOVE_UP, "move-up", + "Move item up" }, + { { 0, 0, 0 }, CMD_LIST_MOVE_DOWN, "move-down", + "Move item down" }, + { { UP, ',', 0 }, CMD_LIST_PREVIOUS, "up", "Move cursor up" }, { { DWN, '.', 0 }, CMD_LIST_NEXT, "down", @@ -157,7 +163,7 @@ get_command_definitions(void) char * key2str(int key) { - static char buf[4]; + static char buf[32]; int i; buf[0] = 0; @@ -201,10 +207,17 @@ key2str(int key) for(i=0; i<=63; i++) if( key==KEY_F(i) ) { - snprintf(buf, 4, "F%d", i ); + snprintf(buf, 32, "F%d", i ); return buf; } - snprintf(buf, 4, "%c", key); + if( !(key & ~037) ) + snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 ); + else if( (key & ~037) == 224 ) + snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 ); + else if( key>32 && key<256 ) + snprintf(buf, 32, "%c", key); + else + snprintf(buf, 32, "0x%03X", key); } return buf; } diff --git a/command.h b/command.h index 7b6bf30..1ce67e8 100644 --- a/command.h +++ b/command.h @@ -35,6 +35,8 @@ typedef enum CMD_LIST_FIND_NEXT, CMD_LIST_RFIND, CMD_LIST_RFIND_NEXT, + CMD_LIST_MOVE_UP, + CMD_LIST_MOVE_DOWN, CMD_SCREEN_UPDATE, CMD_SCREEN_PREVIOUS, CMD_SCREEN_NEXT, diff --git a/screen_help.c b/screen_help.c index 37bf9a3..c55c963 100644 --- a/screen_help.c +++ b/screen_help.c @@ -83,6 +83,8 @@ static help_text_row_t help_text[] = { 0, CMD_PLAY, "Play" }, { 0, CMD_DELETE, NULL }, { 0, CMD_CLEAR, NULL }, + { 0, CMD_LIST_MOVE_UP, "Move song up" }, + { 0, CMD_LIST_MOVE_DOWN, "Move song down" }, { 0, CMD_SAVE_PLAYLIST, NULL }, { 0, CMD_SCREEN_UPDATE, "Center" }, { 0, CMD_TOGGLE_AUTOCENTER, NULL }, diff --git a/screen_play.c b/screen_play.c index db42f58..cdec4f7 100644 --- a/screen_play.c +++ b/screen_play.c @@ -32,6 +32,12 @@ #include "screen_file.h" #include "screen_play.h" +#ifdef DEBUG +#define D(x) x +#else +#define D(x) +#endif + #define BUFSIZE 256 static list_window_t *lw = NULL; @@ -207,6 +213,12 @@ play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd) case CMD_SCREEN_UPDATE: center_playing_item(screen, c); return 1; + case CMD_LIST_MOVE_UP: + playlist_move_song(c, lw->selected, lw->selected-1); + break; + case CMD_LIST_MOVE_DOWN: + playlist_move_song(c, lw->selected, lw->selected+1); + break; case CMD_LIST_FIND: case CMD_LIST_RFIND: case CMD_LIST_FIND_NEXT: @@ -234,6 +246,56 @@ play_get_selected(void) return lw->selected; } +int +playlist_move_song(mpd_client_t *c, int old_index, int new_index) +{ + int index1, index2; + GList *item1, *item2; + gpointer data1, data2; + + if( old_index==new_index || new_index<0 || new_index>=c->playlist_length ) + return -1; + + /* send the move command to mpd */ + mpd_sendMoveCommand(c->connection, old_index, new_index); + mpd_finishCommand(c->connection); + if( mpc_error(c) ) + return -1; + + index1 = MIN(old_index, new_index); + index2 = MAX(old_index, new_index); + item1 = g_list_nth(c->playlist, index1); + item2 = g_list_nth(c->playlist, index2); + data1 = item1->data; + data2 = item2->data; + + /* move the second item */ + D(fprintf(stderr, "move second item [%d->%d]...\n", index2, index1)); + c->playlist = g_list_remove(c->playlist, data2); + c->playlist = g_list_insert_before(c->playlist, item1, data2); + + /* move the first item */ + if( index2-index1 >1 ) + { + D(fprintf(stderr, "move first item [%d->%d]...\n", index1, index2)); + item2 = g_list_nth(c->playlist, index2); + c->playlist = g_list_remove(c->playlist, data1); + c->playlist = g_list_insert_before(c->playlist, item2, data1); + } + + /* increment the playlist id, so we dont retrives a new playlist */ + c->playlist_id++; + + /* make shure the playlist is repainted */ + lw->clear = 1; + lw->repaint = 1; + + /* keep song selected */ + lw->selected = new_index; + + return 0; +} + int playlist_add_song(mpd_client_t *c, mpd_Song *song) { @@ -246,7 +308,6 @@ playlist_add_song(mpd_client_t *c, mpd_Song *song) if( mpc_error(c) ) return -1; -#ifndef DISABLE_FANCY_PLAYLIST_MANAGMENT /* add the song to playlist */ c->playlist = g_list_append(c->playlist, (gpointer) mpd_songDup(song)); c->playlist_length++; @@ -257,7 +318,6 @@ playlist_add_song(mpd_client_t *c, mpd_Song *song) /* make shure the playlist is repainted */ lw->clear = 1; lw->repaint = 1; -#endif /* set selected highlight in the browse screen */ file_set_highlight(c, song, 1); @@ -285,7 +345,6 @@ playlist_delete_song(mpd_client_t *c, int index) /* clear selected highlight in the browse screen */ file_set_highlight(c, song, 0); -#ifndef DISABLE_FANCY_PLAYLIST_MANAGMENT /* increment the playlist id, so we dont retrives a new playlist */ c->playlist_id++; @@ -305,7 +364,6 @@ playlist_delete_song(mpd_client_t *c, int index) lw->clear = 1; lw->repaint = 1; list_window_check_selected(lw, c->playlist_length); -#endif return 0; } diff --git a/screen_play.h b/screen_play.h index 535fc18..2155ae5 100644 --- a/screen_play.h +++ b/screen_play.h @@ -1,6 +1,7 @@ int play_get_selected(void); +int playlist_move_song(mpd_client_t *c, int old_index, int new_index); int playlist_add_song(mpd_client_t *c, mpd_Song *song); int playlist_delete_song(mpd_client_t *c, int index);