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 memset(dest, '\0', len*sizeof(char));
32 if (num >= text->lines->len - 1)
33 return -1;
34 int linelen;
35 if (num == 0) {
36 linelen = g_array_index(text->lines, int, num);
37 memcpy(dest, text->text->str, linelen*sizeof(char));
38 } else if (num == 1) { //dont ask me why, but this is needed....
39 linelen = g_array_index(text->lines, int, num)
40 - g_array_index(text->lines, int, num-1);
41 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num-1)],
42 linelen*sizeof(char));
43 } else {
44 linelen = g_array_index(text->lines, int, num+1)
45 - g_array_index(text->lines, int, num);
46 memcpy(dest, &text->text->str[g_array_index(text->lines, int, num)],
47 linelen*sizeof(char));
48 }
50 dest[linelen] = '\n';
51 dest[linelen + 1] = '\0';
53 return 0;
54 }
56 void add_text_line(formed_text *dest, const char *src, int len)
57 {
59 // need this because g_array_append_val doesnt work with literals
60 // and expat sends "\n" as an extra line everytime
61 if(len == 0) {
62 dest->val = strlen(src);
63 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
64 dest->lines->len-1);
65 g_string_append(dest->text, src);
66 g_array_append_val(dest->lines, dest->val);
67 return;
68 }
70 if(len > 1 || dest->val == 0) {
71 dest->val = len;
72 if(dest->lines->len > 0) dest->val += g_array_index(dest->lines, int,
73 dest->lines->len-1);
74 } else if (len < 6 && dest->val != 0)
75 dest->val = 0;
77 if (dest->val > 0) {
78 g_string_append_len(dest->text, src, len);
79 g_array_append_val(dest->lines, dest->val);
80 }
81 }
83 void formed_text_init(formed_text *text)
84 {
85 if (text->text != NULL)
86 g_string_free(text->text, TRUE);
87 text->text = g_string_new("");
89 if (text->lines != NULL)
90 g_array_free(text->lines, TRUE);
91 text->lines = g_array_new(FALSE, TRUE, 4);
93 text->val = 0;
94 }
96 #ifdef ENABLE_LYRSRC_LEOSLYRICS
97 int deregister_lyr_leoslyrics ();
98 int register_lyr_leoslyrics (src_lyr *source_descriptor);
99 #endif
101 #ifdef ENABLE_LYRSRC_HD
102 int deregister_lyr_hd ();
103 int register_lyr_hd (src_lyr *source_descriptor);
104 #endif
106 static int src_lyr_plugins_load(void);
108 void src_lyr_stack_init(void)
109 {
110 src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*));
112 #ifdef ENABLE_LYRSRC_HD
113 src_lyr *src_lyr_hd = malloc (sizeof (src_lyr));
114 src_lyr_hd->register_src_lyr = register_lyr_hd;
115 g_array_append_val (src_lyr_stack, src_lyr_hd);
116 #endif
117 #ifdef ENABLE_LYRSRC_LEOSLYRICS
118 src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr));
119 src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics;
120 g_array_append_val (src_lyr_stack, src_lyr_leoslyrics);
121 #endif
123 #ifndef DISABLE_PLUGIN_SYSTEM
125 src_lyr_plugins_load ();
126 #endif
127 }
129 int src_lyr_init(void)
130 {
131 src_lyr_stack_init ();
133 int i = 0;
134 while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL) {
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(src_lyr_stack->len == 0) return -1;
146 g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title);
147 return 0;
148 }
150 static int src_lyr_load_plugin_file(const char *file)
151 {
152 GString *path;
153 path = g_string_new (PLUGIN_DIR_SYSTEM);
154 g_string_append (path, "/");
155 g_string_append (path, file);
157 src_lyr_plugin_register register_func;
158 src_lyr *new_src = malloc (sizeof (src_lyr));
159 new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY);
160 if (!g_module_symbol (new_src->module, "register_me", (gpointer*) ®ister_func))
161 return -1;
162 new_src->register_src_lyr = register_func;
163 g_array_append_val (src_lyr_stack, new_src);
164 return 0;
165 }
167 static void src_lyr_plugins_load_from_dir(GDir *plugin_dir)
168 {
169 const gchar *cur_file;
171 for (;;) {
172 cur_file = g_dir_read_name (plugin_dir);
173 if (cur_file == NULL) break;
174 src_lyr_load_plugin_file (cur_file);
175 }
176 }
178 static int src_lyr_plugins_load(void)
179 {
180 GDir *plugin_dir;
182 plugin_dir = g_dir_open (PLUGIN_DIR_SYSTEM, 0, NULL);
183 if (plugin_dir == NULL)
184 return -1;
185 src_lyr_plugins_load_from_dir (plugin_dir);
187 GString *user_dir_path;
188 user_dir_path = g_string_new (g_get_home_dir());
189 g_string_append (user_dir_path, PLUGIN_DIR_USER);
191 plugin_dir = g_dir_open (user_dir_path->str, 0, NULL);
192 if (plugin_dir == NULL)
193 return -1;
194 src_lyr_plugins_load_from_dir (plugin_dir);
196 return 0;
197 }