File: playlist.c

package info (click to toggle)
audtty 0.1.12-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 592 kB
  • sloc: ansic: 1,560; sh: 193; makefile: 63
file content (150 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
    This file is part of xmms-curses, copyright 2003-2005 Knut Auvor Grythe.

    xmms-curses is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    xmms-curses is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with xmms-curses; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <audacious/audctrl.h>
#include <stdlib.h>
#include <glib.h>
#include <ncursesw/curses.h>
#include <string.h>
#include <libgen.h>
#include "curses_printf.h"
#include "main.h"
#include "playlist.h"
#include "dbus.h"

extern playlist list;
extern windows wins;
extern prefs settings;

gint song_length(gint song)
{
	if(song == -1) return 0;
	if(list.song[song].length == 0){
		list.song[song].length = audacious_remote_get_playlist_time(dbus_proxy, song)/1000;
	}
	return list.song[song].length;
}

gchar *song_title(gint song)
{
	if (song == -1 || list.length==0) return "No song loaded...";
	if (list.song[song].title == NULL) return "No song loaded...";
	if (strlen(list.song[song].title) == 0) {
		g_free(list.song[song].title);
		if (settings.read_title) {
			list.song[song].title = audacious_remote_get_playlist_title(dbus_proxy, song);
		} else {
			gchar *tmp = g_strdup(list.song[song].file);
			gchar *end = g_strrstr(tmp, ".");
			*end = '\0';
			list.song[song].title = g_strdup(basename(tmp));
			if(list.length>0) g_free(tmp);
		}
	}

	return list.song[song].title;
}

static gint playlist_pos(gint *songs, gint pos)
{
	if (songs == NULL)
		return pos;
	else
		return songs[pos];
}

void playlist_paint(playlist *plist)
{
	gint i, width, height;
	getmaxyx(wins.list, height, width);

	if (plist->pos != plist->prev_pos &&
			(plist->pos < plist->scrolledto || plist->pos >= plist->scrolledto+height)) {
		plist->scrolledto = plist->pos - height/2;
	} else if (plist->selector >= (plist->scrolledto+height)){
		plist->scrolledto = plist->selector-height+1;
	} else if (plist->selector < plist->scrolledto) {
		plist->scrolledto = plist->selector;
	}

	if (plist->scrolledto > plist->length - height) {
		plist->scrolledto = plist->length - height;
	}

	if (plist->scrolledto < 0) {
		plist->scrolledto = 0;
	}

	for (i=0; i<height; i++) {
		gint hits_song = i+plist->scrolledto;
		gint list_song = playlist_pos(plist->song_mask, hits_song);

		if (hits_song >= plist->length) {
			wmove(wins.list, i, 0);
			wclrtoeol(wins.list);
			wchgat(wins.list, -1, A_NORMAL, 1, NULL);
			continue;
		}

		// print playlist entries
		wmove(wins.list, i, 0);
		if (settings.show_numbers && plist->song_mask == NULL)
			waddnstrf(wins.list, "%d. ", -1, list_song+1);
		waddnstr(wins.list, song_title(list_song), -1);
		wclrtoeol(wins.list);
		if (settings.read_length)
			mvwaddstrf(wins.list, i, width-6, " %02i:%02i",
				song_length(list_song)/60, (song_length(list_song)%60));

		// color playlist entries
		if (hits_song == plist->selector) {
			mvwchgat(wins.list, i, 0, -1, A_REVERSE, 2, NULL);
		} else if (hits_song == plist->pos) {
			mvwchgat(wins.list, i, 0, -1, A_BOLD, 1, NULL);
		} else {
			mvwchgat(wins.list, i, 0, -1, A_NORMAL, 1, NULL);
		}
	}

	wnoutrefresh(wins.list);
}

void playlist_read(gint length)
{
	gint i;

	for(i=0; i<list.length; i++){
		g_free(list.song[i].title);
		g_free(list.song[i].file);
	}
	list.song = (song*) realloc(list.song, sizeof(song)*length);
	for (i=0; i<length; i++) {
		list.song[i].file =  audacious_remote_get_playlist_file(dbus_proxy, i);
		list.song[i].title = malloc(sizeof(gchar));
		list.song[i].title=g_strdup("");
		list.song[i].length = 0;
		if (settings.read_all) {
			if (settings.read_title)
				song_title(i);
			if (settings.read_length)
				song_length(i);
		}
	}
	list.length = length;
    return;
}