1 /*
2 *
3 * (c) 2006 by Kalle Wallin <kaw@linux.se>
4 * Thu Dec 28 23:17:38 2006
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include <unistd.h>
22 #include <string.h>
24 #include "../config.h"
25 #include "src_lyrics.h"
27 #define PLUGIN_DIR_USER "/.ncmpc/plugins"
29 int get_text_line(formed_text *text, int num, char *dest, int len)
30 {
31 int linelen;
33 memset(dest, '\0', len*sizeof(char));
34 if (num >= text->lines->len - 1)
35 return -1;
37 if (num == 0) {
38 linelen = g_array_index(text->lines, int, num);
39 memcpy(dest, text->text->str, linelen*sizeof(char));
40 } else if (num == 1) { //dont ask me why, but this is needed....
41 linelen = g_array_index(text->lines, int, num)
42 - g_array_index(text->lines, int, num-1);
43 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
44 linelen*sizeof(char));
45 } else {
46 linelen = g_array_index(text->lines, int, num+1)
47 - g_array_index(text->lines, int, num);
48 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
49 linelen*sizeof(char));
50 }
52 dest[linelen] = '\n';
53 dest[linelen + 1] = '\0';
55 return 0;
56 }
58 void add_text_line(formed_text *dest, const char *src, int len)
59 {
61 // need this because g_array_append_val doesnt work with literals
62 // and expat sends "\n" as an extra line everytime
63 if(len == 0) {
64 dest->val = strlen(src);
65 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
66 dest->lines->len-1);
67 g_string_append(dest->text, src);
68 g_array_append_val(dest->lines, dest->val);
69 return;
70 }
72 if(len > 1 || dest->val == 0) {
73 dest->val = len;
74 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
75 dest->lines->len-1);
76 } else if (len < 6 && dest->val != 0)
77 dest->val = 0;
79 if (dest->val > 0) {
80 g_string_append_len(dest->text, src, len);
81 g_array_append_val(dest->lines, dest->val);
82 }
83 }
85 void formed_text_init(formed_text *text)
86 {
87 if (text->text != NULL)
88 g_string_free(text->text, TRUE);
89 text->text = g_string_new("");
91 if (text->lines != NULL)
92 g_array_free(text->lines, TRUE);
93 text->lines = g_array_new(FALSE, TRUE, 4);
95 text->val = 0;
96 }
98 #ifdef ENABLE_LYRSRC_LEOSLYRICS
99 int deregister_lyr_leoslyrics ();
100 int register_lyr_leoslyrics (src_lyr *source_descriptor);
101 #endif
103 #ifdef ENABLE_LYRSRC_HD
104 int deregister_lyr_hd ();
105 int register_lyr_hd (src_lyr *source_descriptor);
106 #endif
108 static int src_lyr_plugins_load(void);
110 void src_lyr_stack_init(void)
111 {
112 src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*));
114 #ifdef ENABLE_LYRSRC_HD
115 src_lyr *src_lyr_hd = malloc (sizeof (src_lyr));
116 src_lyr_hd->register_src_lyr = register_lyr_hd;
117 g_array_append_val (src_lyr_stack, src_lyr_hd);
118 #endif
119 #ifdef ENABLE_LYRSRC_LEOSLYRICS
120 src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr));
121 src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics;
122 g_array_append_val (src_lyr_stack, src_lyr_leoslyrics);
123 #endif
125 #ifndef DISABLE_PLUGIN_SYSTEM
127 src_lyr_plugins_load ();
128 #endif
129 }
131 int src_lyr_init(void)
132 {
133 int i = 0;
135 src_lyr_stack_init ();
137 while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL) {
138 src_lyr *i_stack;
139 i_stack = g_array_index (src_lyr_stack, src_lyr*, i);
140 i_stack->register_src_lyr (i_stack);
141 i++;
142 }
143 return 0;
144 }
146 int get_lyr_by_src (int priority, char *artist, char *title)
147 {
148 if(src_lyr_stack->len == 0) return -1;
149 g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title);
150 return 0;
151 }
153 static int src_lyr_load_plugin_file(const char *file)
154 {
155 GString *path;
156 src_lyr *new_src;
157 src_lyr_plugin_register register_func;
159 path = g_string_new (PLUGIN_DIR_SYSTEM);
160 g_string_append (path, "/");
161 g_string_append (path, file);
163 new_src = malloc(sizeof(src_lyr));
164 new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY);
165 if (!g_module_symbol (new_src->module, "register_me", (gpointer*) ®ister_func))
166 return -1;
167 new_src->register_src_lyr = register_func;
168 g_array_append_val (src_lyr_stack, new_src);
169 return 0;
170 }
172 static void src_lyr_plugins_load_from_dir(GDir *plugin_dir)
173 {
174 const gchar *cur_file;
176 for (;;) {
177 cur_file = g_dir_read_name (plugin_dir);
178 if (cur_file == NULL) break;
179 src_lyr_load_plugin_file (cur_file);
180 }
181 }
183 static int src_lyr_plugins_load(void)
184 {
185 GDir *plugin_dir;
186 GString *user_dir_path;
188 plugin_dir = g_dir_open (PLUGIN_DIR_SYSTEM, 0, NULL);
189 if (plugin_dir == NULL)
190 return -1;
191 src_lyr_plugins_load_from_dir (plugin_dir);
193 user_dir_path = g_string_new (g_get_home_dir());
194 g_string_append (user_dir_path, PLUGIN_DIR_USER);
196 plugin_dir = g_dir_open (user_dir_path->str, 0, NULL);
197 if (plugin_dir == NULL)
198 return -1;
199 src_lyr_plugins_load_from_dir (plugin_dir);
201 return 0;
202 }