Skip to content

Commit

Permalink
Moving items up and down: u and j commands.
Browse files Browse the repository at this point in the history
git-svn-id: svn://svn.daper.net/moc/trunk@1722 910807d9-36e0-0310-a014-e9ea483e2ba4
  • Loading branch information
daper committed Oct 18, 2005
1 parent 76465c0 commit baf17d1
Show file tree
Hide file tree
Showing 16 changed files with 391 additions and 4 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Before 2.4:
are visible (as an option).
*$ Tags cache saved to a file on exit and read at startup.
* Count speex time by reading granule position from the last page.
* Moving items on the playlist up and down.
*$ Support for Audio-CD.
*$ Support for CDDB.
* Move the playlist to see the song that is currently played.
Expand Down
8 changes: 8 additions & 0 deletions audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,14 @@ void audio_plist_set_serial (const int serial)
UNLOCK (plist_mut);
}

/* Swap 2 file on the playlist. */
void audio_plist_move (const char *file1, const char *file2)
{
LOCK (plist_mut);
plist_swap_files (&playlist, file1, file2);
UNLOCK (plist_mut);
}

struct file_tags *audio_get_curr_tags ()
{
return player_get_curr_tags ();
Expand Down
1 change: 1 addition & 0 deletions audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,6 @@ void audio_plist_set_serial (const int serial);
struct file_tags *audio_get_curr_tags ();
char *audio_get_mixer_channel_name ();
void audio_toggle_mixer_channel ();
void audio_plist_move (const char *file1, const char *file2);

#endif
97 changes: 97 additions & 0 deletions interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ static struct tag_ev_response *recv_tags_data_from_srv ()
return r;
}

static struct move_ev_data *recv_move_ev_data_from_srv ()
{
struct move_ev_data *d;

if (!(d = recv_move_ev_data(srv_sock)))
fatal ("Can't receive move data from the server");

return d;
}

/* Receive data for the given type of event and return them. Return NULL if
* there is no data for the event. */
static void *get_event_data (const int type)
Expand All @@ -218,6 +228,8 @@ static void *get_event_data (const int type)
return get_str_from_srv ();
case EV_FILE_TAGS:
return recv_tags_data_from_srv ();
case EV_PLIST_MOVE:
return recv_move_ev_data_from_srv ();
}

return NULL;
Expand Down Expand Up @@ -858,6 +870,26 @@ static void event_plist_del (char *file)
" playlist.");
}

/* Swap 2 file on the playlist. */
static void swap_playlist_items (const char *file1, const char *file2)
{
assert (file1 != NULL);
assert (file2 != NULL);

plist_swap_files (playlist, file1, file2);
iface_swap_plist_items (file1, file2);
}

/* Handle EV_PLIST_MOVE. */
static void event_plist_move (const struct move_ev_data *d)
{
assert (d != NULL);
assert (d->from != NULL);
assert (d->to != NULL);

swap_playlist_items (d->from, d->to);
}

/* Handle server event. */
static void server_event (const int event, void *data)
{
Expand Down Expand Up @@ -908,6 +940,10 @@ static void server_event (const int event, void *data)
if (options_get_int("SyncPlaylist"))
event_plist_del ((char *)data);
break;
case EV_PLIST_MOVE:
if (options_get_int("SyncPlaylist"))
event_plist_move ((struct move_ev_data *)data);
break;
case EV_TAGS:
update_curr_tags ();
break;
Expand Down Expand Up @@ -2126,6 +2162,61 @@ static void seek_silent (const int sec)
}
}

/* Move the current playlist item (direction: 1 - up, -1 - down). */
static void move_item (const int direction)
{
char *file;
int second;
char *second_file;

if (!iface_in_plist_menu()) {
error ("You can move only playlist items.");
return;
}

if (!(file = iface_get_curr_file()))
return;

second = plist_find_fname (playlist, file);
assert (second != -1);

if (direction == -1)
second = plist_next (playlist, second);
else if (direction == 1)
second = plist_prev (playlist, second);
else
abort (); /* BUG */

if (second == -1) {
free (file);
return;
}

second_file = plist_get_file (playlist, second);

send_int_to_srv (CMD_LOCK);

if (options_get_int("SyncPlaylist")) {
send_int_to_srv (CMD_CLI_PLIST_MOVE);
send_str_to_srv (file);
send_str_to_srv (second_file);
}
else
swap_playlist_items (file, second_file);

/* update the server's playlist */
if (get_server_plist_serial() == plist_get_serial(playlist)) {
send_int_to_srv (CMD_LIST_MOVE);
send_str_to_srv (file);
send_str_to_srv (second_file);
}

send_int_to_srv (CMD_UNLOCK);

free (second_file);
free (file);
}

/* Handle releasing silent seek key. */
static void do_silent_seek ()
{
Expand Down Expand Up @@ -2398,6 +2489,12 @@ static void menu_key (const int ch)
case KEY_CMD_TOGGLE_LAYOUT:
iface_toggle_layout ();
break;
case KEY_CMD_PLIST_MOVE_UP:
move_item (1);
break;
case KEY_CMD_PLIST_MOVE_DOWN:
move_item (-1);
break;
default:
abort ();
}
Expand Down
34 changes: 34 additions & 0 deletions interface_elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,20 @@ static void side_menu_use_main (struct side_menu *m)
}
}

static void side_menu_swap_items (struct side_menu *m, const char *file1,
const char *file2)
{
assert (m != NULL);
assert (m->visible);
assert (m->type == MENU_PLAYLIST || m->type == MENU_DIR);
assert (file1 != NULL);
assert (file2 != NULL);
assert (m->menu.list.main != NULL);
assert (m->menu.list.copy == NULL);

menu_swap_items (m->menu.list.main, file1, file2);
}

static void side_menu_select_file (struct side_menu *m, const char *file)
{
assert (m != NULL);
Expand Down Expand Up @@ -1723,6 +1737,20 @@ static void main_win_handle_help_key (struct main_win *w, const int ch)
main_win_draw (w);
}

static void main_win_swap_plist_items (struct main_win *w, const char *file1,
const char *file2)
{
struct side_menu *m;

assert (w != NULL);
assert (file1 != NULL);
assert (file2 != NULL);

m = find_side_menu (w, MENU_PLAYLIST);
side_menu_swap_items (m, file1, file2);
main_win_draw (w);
}

static void main_win_use_layout (struct main_win *w, const char *layout_fmt)
{
struct main_win_layout l;
Expand Down Expand Up @@ -3231,3 +3259,9 @@ void iface_toggle_layout ()
main_win_use_layout (&main_win, layout_fmt);
wrefresh (main_win.win);
}

void iface_swap_plist_items (const char *file1, const char *file2)
{
main_win_swap_plist_items (&main_win, file1, file2);
wrefresh (main_win.win);
}
1 change: 1 addition & 0 deletions interface_elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ int iface_in_help ();
void iface_switch_to_help ();
void iface_handle_help_key (const int ch);
void iface_toggle_layout ();
void iface_swap_plist_items (const char *file1, const char *file2);

#endif
2 changes: 2 additions & 0 deletions keymap.example
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ go_to_fast_dir10 = )
toggle_mixer = x
history_up = UP
history_down = DOWN
plist_move_up = u
plist_move_down = j
16 changes: 16 additions & 0 deletions keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,22 @@ static struct command commands[] = {
CON_MENU,
{ 'x', -1 },
1
},
{
KEY_CMD_PLIST_MOVE_UP,
"plist_move_up",
"Move playlist item up",
CON_MENU,
{ 'u', -1 },
1
},
{
KEY_CMD_PLIST_MOVE_DOWN,
"plist_move_down",
"Move playlist item down",
CON_MENU,
{ 'j', -1 },
1
}
};

Expand Down
2 changes: 2 additions & 0 deletions keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ enum key_cmd
KEY_CMD_TOGGLE_MIXER,
KEY_CMD_HISTORY_UP,
KEY_CMD_HISTORY_DOWN,
KEY_CMD_PLIST_MOVE_UP,
KEY_CMD_PLIST_MOVE_DOWN,
KEY_CMD_WRONG
};

Expand Down
83 changes: 83 additions & 0 deletions menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,86 @@ int menu_is_visible (const struct menu *menu, const struct menu_item *mi)

return 0;
}

static void menu_items_swap (struct menu *menu, struct menu_item *mi1,
struct menu_item *mi2)
{
int t;

assert (menu != NULL);
assert (mi1 != NULL);
assert (mi2 != NULL);
assert (mi1 != mi2);

/* if they are next to each other, change the pointers so that mi2
* is the second one */
if (mi2->next == mi1) {
struct menu_item *i = mi1;

mi1 = mi2;
mi2 = i;
}

if (mi1->next == mi2) {
if (mi2->next)
mi2->next->prev = mi1;
if (mi1->prev)
mi1->prev->next = mi2;

mi1->next = mi2->next;
mi2->prev = mi1->prev;
mi1->prev = mi2;
mi2->next = mi1;
}
else {
if (mi2->next)
mi2->next->prev = mi1;
if (mi2->prev)
mi2->prev->next = mi1;
mi2->next = mi1->next;
mi2->prev = mi1->prev;

if (mi1->next)
mi1->next->prev = mi2;
if (mi1->prev)
mi1->prev->next = mi2;
mi1->next = mi2->next;
mi1->prev = mi2->prev;
}

t = mi1->num;
mi1->num = mi2->num;
mi2->num = t;

if (menu->top == mi1)
menu->top = mi2;
else if (menu->top == mi2)
menu->top = mi1;

if (menu->last == mi1)
menu->last = mi2;
else if (menu->last == mi2)
menu->last = mi1;

if (menu->items == mi1)
menu->items = mi2;
else if (menu->items == mi2)
menu->items = mi1;
}

void menu_swap_items (struct menu *menu, const char *file1, const char *file2)
{
struct menu_item *mi1, *mi2;

assert (menu != NULL);
assert (file1 != NULL);
assert (file2 != NULL);

if ((mi1 = menu_find(menu, file1)) && (mi2 = menu_find(menu, file2))
&& mi1 != mi2) {
menu_items_swap (menu, mi1, mi2);

/* make sure that the selected item is visible */
menu_setcurritem (menu, menu->selected);
}
}
1 change: 1 addition & 0 deletions menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@ struct menu_item *menu_find (struct menu *menu, const char *fname);
void menu_del_item (struct menu *menu, const char *fname);
void menu_item_set_align (struct menu_item *mi, const enum menu_align align);
int menu_is_visible (const struct menu *menu, const struct menu_item *mi);
void menu_swap_items (struct menu *menu, const char *file1, const char *file2);

#endif
24 changes: 24 additions & 0 deletions playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,27 @@ struct file_tags *plist_get_tags (const struct plist *plist, const int num)

return NULL;
}

/* Swap 2 file on the playlist. */
void plist_swap_files (struct plist *plist, const char *file1,
const char *file2)
{
struct rb_node *x1, *x2;

assert (plist != NULL);
assert (file1 != NULL);
assert (file2 != NULL);

x1 = rb_search (&plist->search_tree, file1);
x2 = rb_search (&plist->search_tree, file2);

if (!rb_is_null(x1) && !rb_is_null(x2)) {
int t;

plist_swap (plist, (int)x1->data, (int)x2->data);

t = (int)x1->data;
x1->data = x2->data;
x2->data = (void *)t;
}
}
2 changes: 2 additions & 0 deletions playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ void plist_discard_tags (struct plist *plist);
void plist_set_tags (struct plist *plist, const int num,
const struct file_tags *tags);
struct file_tags *plist_get_tags (const struct plist *plist, const int num);
void plist_swap_files (struct plist *plist, const char *file1,
const char *file2);

#endif
Loading

0 comments on commit baf17d1

Please sign in to comment.