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 "src_lyrics.h"
22 #include <options.h>
23 #include <unistd.h>
24 #include "../config.h"
26 #define PLUGIN_DIR_USER "/home/andi/.ncmpc/plugins"
28 int get_text_line(formed_text *text, int num, char *dest, int len)
29 {
30 memset(dest, '\0', len*sizeof(char));
31 if(num >= text->lines->len-1) return -1;
32 int linelen;
33 if(num == 0)
34 {
35 linelen = g_array_index(text->lines, int, num);
36 memcpy(dest, text->text->str, linelen*sizeof(char));
37 }
38 else if(num == 1)
39 { //dont ask me why, but this is needed....
40 linelen = g_array_index(text->lines, int, num)
41 - g_array_index(text->lines, int, num-1);
42 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
43 linelen*sizeof(char));
44 }
45 else
46 {
47 linelen = g_array_index(text->lines, int, num+1)
48 - g_array_index(text->lines, int, num);
49 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
50 linelen*sizeof(char));
51 }
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 {
60 // need this because g_array_append_val doesnt work with literals
61 // and expat sends "\n" as an extra line everytime
62 if(len == 0)
63 {
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 }
71 if(len > 1 || dest->val == 0)
72 {
73 dest->val = len;
74 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
75 dest->lines->len-1);
76 }
77 else if (len == 1 && dest->val != 0) dest->val = 0;
79 if(dest->val > 0)
80 {
81 g_string_append_len(dest->text, src, len);
82 g_array_append_val(dest->lines, dest->val);
83 }
84 }
86 void formed_text_init(formed_text *text)
87 {
88 if(text->text != NULL) g_string_free(text->text, TRUE);
89 text->text = g_string_new("");
91 if(text->lines != NULL) g_array_free(text->lines, TRUE);
92 text->lines = g_array_new(FALSE, TRUE, 4);
94 text->val = 0;
95 }
97 #ifdef ENABLE_LYRSRC_LEOSLYRICS
98 int deregister_lyr_leoslyrics ();
99 int register_lyr_leoslyrics (src_lyr *source_descriptor);
100 #endif
102 #ifdef ENABLE_LYRSRC_HD
103 int deregister_lyr_hd ();
104 int register_lyr_hd (src_lyr *source_descriptor);
105 #endif
107 int init_src_lyr_stack ()
108 {
109 src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*));
111 #ifdef ENABLE_LYRSRC_HD
112 src_lyr *src_lyr_hd = malloc (sizeof (src_lyr));
113 src_lyr_hd->register_src_lyr = register_lyr_hd;
114 g_array_append_val (src_lyr_stack, src_lyr_hd);
115 #endif
116 #ifdef ENABLE_LYRSRC_LEOSLYRICS
117 src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr));
118 src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics;
119 g_array_append_val (src_lyr_stack, src_lyr_leoslyrics);
120 #endif
122 #ifndef DISABLE_PLUGIN_SYSTEM
123 src_lyr_plugins_load ();
124 #endif
126 }
128 int init_src_lyr ()
129 {
130 init_src_lyr_stack();
132 int i = 0;
133 while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL)
134 {
135 src_lyr *i_stack;
136 i_stack = g_array_index (src_lyr_stack, src_lyr*, i);
137 i_stack->register_src_lyr (i_stack);
138 i++;
139 }
140 return 0;
141 }
143 int get_lyr_by_src (int priority, char *artist, char *title)
144 {
145 //if (g_array_index (src_lyr_stack, src_lyr*, priority)->check_lyr() == 0)
146 //{
147 g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title);
148 //}
149 return 0;
150 }
152 int src_lyr_load_plugin_file (const char *file)
153 {
154 GString *path;
155 path = g_string_new (PLUGIN_DIR_SYSTEM);
156 g_string_append (path, "/");
157 g_string_append (path, file);
159 src_lyr_plugin_register register_func;
160 src_lyr *new_src = malloc (sizeof (src_lyr));
161 new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY);
162 //if (new_src->module == NULL) for (;;) fprintf (stderr, g_module_error());
163 if (!g_module_symbol (new_src->module, "register_me", (gpointer*) ®ister_func))
164 return -1;
165 new_src->register_src_lyr = register_func;
166 g_array_append_val (src_lyr_stack, new_src);
167 return 0;
168 }
170 void src_lyr_plugins_load_from_dir (GDir *plugin_dir)
171 {
172 const gchar *cur_file;
174 for (;;)
175 {
176 cur_file = g_dir_read_name (plugin_dir);
177 if (cur_file == NULL) break;
178 src_lyr_load_plugin_file (cur_file);
179 }
180 }
184 int src_lyr_plugins_load ()
185 {
186 GDir *plugin_dir;
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 plugin_dir = g_dir_open (PLUGIN_DIR_USER, 0, NULL);
194 if (plugin_dir == NULL)
195 return -1;
196 src_lyr_plugins_load_from_dir (plugin_dir);
198 return 0;
199 }