comparison src/aosd/aosd_trigger.c @ 597:05bc0456fabb trunk

[svn] - aosd: add missing aosd_trigger* sources
author giacomo
date Thu, 01 Feb 2007 16:46:20 -0800
parents
children e83e6fb3ebfa
comparison
equal deleted inserted replaced
596:1708b03e116c 597:05bc0456fabb
1 /*
2 *
3 * Author: Giacomo Lozito <james@develia.org>, (C) 2005-2007
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "aosd_trigger.h"
22 #include "aosd_trigger_private.h"
23 #include "aosd_cfg.h"
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <audacious/playlist.h>
27 #include <audacious/strings.h>
28 #include <audacious/hook.h>
29
30
31 extern aosd_cfg_t * global_config;
32
33
34 /* trigger codes ( the code values do not need to be sequential ) */
35 enum
36 {
37 AOSD_TRIGGER_PB_START = 0,
38 AOSD_TRIGGER_PB_TITLECHANGE = 1
39 };
40
41 /* trigger codes array size */
42 #define AOSD_TRIGGER_CODES_ARRAY_SIZE 2
43
44 /* trigger codes array */
45 gint aosd_trigger_codes[] =
46 {
47 AOSD_TRIGGER_PB_START,
48 AOSD_TRIGGER_PB_TITLECHANGE
49 };
50
51 /* prototypes of trigger functions */
52 static void aosd_trigger_func_pb_start_onoff ( gboolean );
53 static void aosd_trigger_func_pb_start_cb ( gpointer , gpointer );
54 static void aosd_trigger_func_pb_titlechange_onff ( gboolean );
55 static void aosd_trigger_func_pb_titlechange_cb ( gpointer , gpointer );
56
57 /* map trigger codes to trigger objects */
58 aosd_trigger_t aosd_triggers[] =
59 {
60 [AOSD_TRIGGER_PB_START] = { N_("Playback Start") ,
61 N_("Triggers OSD when a playlist entry is played.") ,
62 aosd_trigger_func_pb_start_onoff ,
63 aosd_trigger_func_pb_start_cb },
64
65 [AOSD_TRIGGER_PB_TITLECHANGE] = { N_("Title Change") ,
66 N_("Trigger OSD when, during playback, the song title changes "
67 "but the filename is the same. This is mostly useful to display "
68 "title changes in internet streams.") ,
69 aosd_trigger_func_pb_titlechange_onff ,
70 aosd_trigger_func_pb_titlechange_cb }
71 };
72
73
74
75 /* TRIGGER API */
76
77 void
78 aosd_trigger_get_codes_array ( gint ** array , gint * array_size )
79 {
80 *array = aosd_trigger_codes;
81 *array_size = AOSD_TRIGGER_CODES_ARRAY_SIZE;
82 return;
83 }
84
85
86 const gchar *
87 aosd_trigger_get_name ( gint trig_code )
88 {
89 return aosd_triggers[trig_code].name;
90 }
91
92
93 const gchar *
94 aosd_trigger_get_desc ( gint trig_code )
95 {
96 return aosd_triggers[trig_code].desc;
97 }
98
99
100 void
101 aosd_trigger_start ( aosd_cfg_osd_trigger_t * cfg_trigger )
102 {
103 gint i = 0;
104 for ( i = 0 ; i < cfg_trigger->active->len ; i++ )
105 {
106 gint trig_code = g_array_index( cfg_trigger->active , gint , i );
107 aosd_triggers[trig_code].onoff_func( TRUE );
108 }
109 return;
110 }
111
112
113 void
114 aosd_trigger_stop ( aosd_cfg_osd_trigger_t * cfg_trigger )
115 {
116 gint i = 0;
117 for ( i = 0 ; i < cfg_trigger->active->len ; i++ )
118 {
119 gint trig_code = g_array_index( cfg_trigger->active , gint , i );
120 aosd_triggers[trig_code].onoff_func( FALSE );
121 }
122 return;
123 }
124
125
126 /* TRIGGER FUNCTIONS */
127
128 static void
129 aosd_trigger_func_pb_start_onoff ( gboolean turn_on )
130 {
131 if ( turn_on == TRUE )
132 hook_associate( "playback begin" , aosd_trigger_func_pb_start_cb , NULL );
133 else
134 hook_dissociate( "playback begin" , aosd_trigger_func_pb_start_cb );
135 return;
136 }
137
138 static void
139 aosd_trigger_func_pb_start_cb ( gpointer plentry_gp , gpointer unused )
140 {
141 PlaylistEntry *pl_entry = plentry_gp;
142 if (( plentry_gp != NULL ) && ( pl_entry->title != NULL ))
143 {
144 gchar *utf8_title = str_to_utf8( pl_entry->title );
145 gchar *utf8_title_markup = g_markup_printf_escaped(
146 "<span font_desc='%s'>%s</span>" , global_config->osd->text.fonts_name[0] , utf8_title );
147 aosd_display( utf8_title_markup , global_config->osd , FALSE );
148 g_free( utf8_title_markup );
149 g_free( utf8_title );
150 }
151 return;
152 }
153
154
155
156 typedef struct
157 {
158 gchar *title;
159 gchar *filename;
160 }
161 aosd_pb_titlechange_prevs_t;
162
163
164 static void
165 aosd_trigger_func_pb_titlechange_onff ( gboolean turn_on )
166 {
167 static aosd_pb_titlechange_prevs_t *prevs = NULL;
168
169 if ( turn_on == TRUE )
170 {
171 prevs = g_malloc0(sizeof(aosd_pb_titlechange_prevs_t));
172 prevs->title = NULL;
173 prevs->filename = NULL;
174 hook_associate( "playlist set info" , aosd_trigger_func_pb_titlechange_cb , prevs );
175 }
176 else
177 {
178 hook_dissociate( "playlist set info" , aosd_trigger_func_pb_titlechange_cb );
179 if ( prevs != NULL )
180 {
181 if ( prevs->title != NULL ) g_free( prevs->title );
182 if ( prevs->filename != NULL ) g_free( prevs->filename );
183 g_free( prevs );
184 prevs = NULL;
185 }
186 }
187 return;
188 }
189
190 static void
191 aosd_trigger_func_pb_titlechange_cb ( gpointer plentry_gp , gpointer prevs_gp )
192 {
193 if ( ip_data.playing )
194 {
195 aosd_pb_titlechange_prevs_t *prevs = prevs_gp;
196 PlaylistEntry *pl_entry = plentry_gp;
197
198 /* same filename but title changed, useful to detect http stream song changes */
199
200 if ( ( prevs->title != NULL ) && ( prevs->filename != NULL ) )
201 {
202 if ( !strcmp(pl_entry->filename,prevs->filename) )
203 {
204 if ( strcmp(pl_entry->title,prevs->title) )
205 {
206 /* string formatting is done here a.t.m. - TODO - improve this area */
207 gchar *utf8_title = str_to_utf8( pl_entry->title );
208 gchar *utf8_title_markup = g_markup_printf_escaped(
209 "<span font_desc='%s'>%s</span>" , global_config->osd->text.fonts_name[0] , utf8_title );
210 aosd_display( utf8_title_markup , global_config->osd , FALSE );
211 g_free( utf8_title_markup );
212 g_free( utf8_title );
213 g_free( prevs->title );
214 prevs->title = g_strdup(pl_entry->title);
215 }
216 }
217 else
218 {
219 g_free(prevs->filename);
220 prevs->filename = g_strdup(pl_entry->filename);
221 }
222 }
223 else
224 {
225 if ( prevs->title != NULL )
226 g_free(prevs->title);
227 prevs->title = g_strdup(pl_entry->title);
228 if ( prevs->filename != NULL )
229 g_free(prevs->filename);
230 prevs->filename = g_strdup(pl_entry->filename);
231 }
232 }
233 }