Code

const pointers
[ncmpc.git] / src / src_lyrics.c
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 int src_lyr_stack_init ()
108         src_lyr_stack = g_array_new (TRUE, FALSE, sizeof (src_lyr*));
110 #ifdef ENABLE_LYRSRC_HD
111         src_lyr *src_lyr_hd = malloc (sizeof (src_lyr));
112         src_lyr_hd->register_src_lyr = register_lyr_hd;
113         g_array_append_val (src_lyr_stack, src_lyr_hd);
114 #endif
115 #ifdef ENABLE_LYRSRC_LEOSLYRICS
116         src_lyr *src_lyr_leoslyrics = malloc (sizeof (src_lyr));
117         src_lyr_leoslyrics->register_src_lyr = register_lyr_leoslyrics;
118         g_array_append_val (src_lyr_stack, src_lyr_leoslyrics);
119 #endif
121 #ifndef DISABLE_PLUGIN_SYSTEM
123         src_lyr_plugins_load ();
124 #endif
127 int src_lyr_init ()
129         src_lyr_stack_init ();
131         int i = 0;
132         while (g_array_index (src_lyr_stack, src_lyr*, i) != NULL) {
133                 src_lyr *i_stack;
134                 i_stack = g_array_index (src_lyr_stack, src_lyr*, i);
135                 i_stack->register_src_lyr (i_stack);
136                 i++;
137         }
138         return 0;
141 int get_lyr_by_src (int priority, char *artist, char *title)
143         if(src_lyr_stack->len == 0) return -1;
144         g_array_index (src_lyr_stack, src_lyr*, priority)->get_lyr (artist, title);
145         return 0;
148 int src_lyr_load_plugin_file (const char *file)
150         GString *path;
151         path = g_string_new (PLUGIN_DIR_SYSTEM);
152         g_string_append (path, "/");
153         g_string_append (path, file);
155         src_lyr_plugin_register register_func;
156         src_lyr *new_src = malloc (sizeof (src_lyr));
157         new_src->module = g_module_open (path->str, G_MODULE_BIND_LAZY);
158         if (!g_module_symbol (new_src->module, "register_me", (gpointer*) &register_func))
159                 return -1;
160         new_src->register_src_lyr = register_func;
161         g_array_append_val (src_lyr_stack, new_src);
162         return 0;
165 void src_lyr_plugins_load_from_dir (GDir *plugin_dir)
167         const gchar *cur_file;
169         for (;;) {
170                 cur_file = g_dir_read_name (plugin_dir);
171                 if (cur_file == NULL) break;
172                 src_lyr_load_plugin_file (cur_file);
173         }
176 int src_lyr_plugins_load ()
178         GDir *plugin_dir;
180         plugin_dir = g_dir_open (PLUGIN_DIR_SYSTEM, 0, NULL);
181         if (plugin_dir == NULL)
182                 return -1;
183         src_lyr_plugins_load_from_dir (plugin_dir);
185         GString *user_dir_path;
186         user_dir_path = g_string_new (g_get_home_dir());
187         g_string_append (user_dir_path, PLUGIN_DIR_USER);
189         plugin_dir = g_dir_open (user_dir_path->str, 0, NULL);
190         if (plugin_dir == NULL)
191                 return -1;
192         src_lyr_plugins_load_from_dir (plugin_dir);
194         return 0;