aboutsummaryrefslogtreecommitdiffstats
path: root/src/src_lyrics.c
blob: 4d41aea3752db1aba0459a9c076bbd75b2ae1cd3 (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* 
 *	
 * (c) 2006 by Kalle Wallin <kaw@linux.se>
 * Thu Dec 28 23:17:38 2006
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <unistd.h>
#include <string.h>

#include "../config.h"
#include "src_lyrics.h"

#define PLUGIN_DIR_USER "/.ncmpc/plugins"

int get_text_line(formed_text *text, int num, char *dest, int len)
{
	memset(dest, '\0', len*sizeof(char));
	if (num >= text->lines->len - 1)
		return -1;
	int linelen;
	if (num == 0) {
		linelen = g_array_index(text->lines, int, num);
		memcpy(dest, text->text->str, linelen*sizeof(char));
	} else if (num == 1) { //dont ask me why, but this is needed....
		linelen = g_array_index(text->lines, int, num)
			- g_array_index(text->lines, int, num-1);
		memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
		       linelen*sizeof(char));
	} else {
		linelen = g_array_index(text->lines, int, num+1)
			- g_array_index(text->lines, int, num);
		memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
		       linelen*sizeof(char));
	}

	dest[linelen] = '\n';
	dest[linelen + 1] = '\0';

	return 0;
}

void add_text_line(formed_text *dest, const char *src, int len)
{

	// need this because g_array_append_val doesnt work with literals
	// and expat sends "\n" as an extra line everytime
	if(len == 0) {
		dest->val = strlen(src);
		if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
								    dest->lines->len-1);
		g_string_append(dest->text, src);
		g_array_append_val(dest->lines, dest->val);
		return;
	}

	if(len > 1 || dest->val == 0) {
			dest->val = len;
			if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
									    dest->lines->len-1);
	} else if (len < 6 && dest->val != 0)
		dest->val = 0;

	if (dest->val > 0) {
		g_string_append_len(dest->text, src, len);
		g_array_append_val(dest->lines, dest->val);
	}
}

void formed_text_init(formed_text *text)
{
	if (text->text != NULL)
		g_string_free(text->text, TRUE);
	text->text = g_string_new("");

	if (text->lines != NULL)
		g_array_free(text->lines, TRUE);
	text->lines = g_array_new(FALSE, TRUE, 4);

	text->val = 0;
}

#ifdef ENABLE_LYRSRC_LEOSLYRICS
int deregister_lyr_leoslyrics ();
int register_lyr_leoslyrics (src_lyr *source_descriptor);
#endif

#ifdef ENABLE_LYRSRC_HD
int deregister_lyr_hd ();
int register_lyr_hd (src_lyr *source_descriptor);
#endif

static int src_lyr_plugins_load(void);

void src_lyr_stack_init(void)
{
	src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*));

#ifdef ENABLE_LYRSRC_HD
	src_lyr *src_lyr_hd = malloc (sizeof (src_lyr));
	src_lyr_hd->register_src_lyr = register_lyr_hd;
	g_array_append_val (src_lyr_stack, src_lyr_hd);
#endif
#ifdef ENABLE_LYRSRC_LEOSLYRICS
	src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr));
	src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics;
	g_array_append_val (src_lyr_stack, src_lyr_leoslyrics);
#endif

#ifndef DISABLE_PLUGIN_SYSTEM

	src_lyr_plugins_load ();
#endif
}

int src_lyr_init(void)
{
	src_lyr_stack_init ();

	int i = 0;
	while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL) {
		src_lyr *i_stack;
		i_stack = g_array_index (src_lyr_stack, src_lyr*, i);
		i_stack->register_src_lyr (i_stack);
		i++;
	}
	return 0;
}

int get_lyr_by_src (int priority, char *artist, char *title)
{
	if(src_lyr_stack->len == 0) return -1;
	g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title);
	return 0;
}

static int src_lyr_load_plugin_file(const char *file)
{
	GString *path;
	path = g_string_new (PLUGIN_DIR_SYSTEM);
	g_string_append (path, "/");
	g_string_append (path, file);

	src_lyr_plugin_register register_func;
	src_lyr *new_src = malloc (sizeof (src_lyr));
	new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY);
	if (!g_module_symbol (new_src->module, "register_me", (gpointer*) &register_func))
		return -1;
	new_src->register_src_lyr = register_func;
	g_array_append_val (src_lyr_stack, new_src);
	return 0;
}

static void src_lyr_plugins_load_from_dir(GDir *plugin_dir)
{
	const gchar *cur_file;

	for (;;) {
		cur_file = g_dir_read_name (plugin_dir);
		if (cur_file == NULL) break;
		src_lyr_load_plugin_file (cur_file);
	}
}

static int src_lyr_plugins_load(void)
{
	GDir *plugin_dir;

	plugin_dir = g_dir_open (PLUGIN_DIR_SYSTEM, 0, NULL);
	if (plugin_dir == NULL)
		return -1;
	src_lyr_plugins_load_from_dir (plugin_dir);

	GString *user_dir_path;
	user_dir_path = g_string_new (g_get_home_dir());
	g_string_append (user_dir_path, PLUGIN_DIR_USER);

	plugin_dir = g_dir_open (user_dir_path->str, 0, NULL);
	if (plugin_dir == NULL)
		return -1;
	src_lyr_plugins_load_from_dir (plugin_dir);

	return 0;
}