Mercurial > audlegacy-plugins
annotate src/aosd/aosd_trigger.c @ 637:a1687bd302ce trunk
[svn] - aosd: completed volume change trigger, works now
author | giacomo |
---|---|
date | Mon, 12 Feb 2007 07:20:56 -0800 |
parents | 443de962d0a0 |
children | 40fb4189fa88 |
rev | line source |
---|---|
597 | 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, | |
623 | 38 AOSD_TRIGGER_PB_TITLECHANGE = 1, |
39 AOSD_TRIGGER_VOL_CHANGE = 2 | |
597 | 40 }; |
41 | |
42 /* trigger codes array size */ | |
623 | 43 #define AOSD_TRIGGER_CODES_ARRAY_SIZE 3 |
597 | 44 |
45 /* trigger codes array */ | |
46 gint aosd_trigger_codes[] = | |
47 { | |
48 AOSD_TRIGGER_PB_START, | |
623 | 49 AOSD_TRIGGER_PB_TITLECHANGE, |
50 AOSD_TRIGGER_VOL_CHANGE | |
597 | 51 }; |
52 | |
53 /* prototypes of trigger functions */ | |
54 static void aosd_trigger_func_pb_start_onoff ( gboolean ); | |
55 static void aosd_trigger_func_pb_start_cb ( gpointer , gpointer ); | |
623 | 56 static void aosd_trigger_func_pb_titlechange_onoff ( gboolean ); |
597 | 57 static void aosd_trigger_func_pb_titlechange_cb ( gpointer , gpointer ); |
623 | 58 static void aosd_trigger_func_vol_change_onoff ( gboolean ); |
59 static void aosd_trigger_func_vol_change_cb ( gpointer , gpointer ); | |
597 | 60 |
61 /* map trigger codes to trigger objects */ | |
62 aosd_trigger_t aosd_triggers[] = | |
63 { | |
64 [AOSD_TRIGGER_PB_START] = { N_("Playback Start") , | |
65 N_("Triggers OSD when a playlist entry is played.") , | |
66 aosd_trigger_func_pb_start_onoff , | |
67 aosd_trigger_func_pb_start_cb }, | |
68 | |
69 [AOSD_TRIGGER_PB_TITLECHANGE] = { N_("Title Change") , | |
637
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
70 N_("Triggers OSD when, during playback, the song title changes " |
597 | 71 "but the filename is the same. This is mostly useful to display " |
72 "title changes in internet streams.") , | |
623 | 73 aosd_trigger_func_pb_titlechange_onoff , |
74 aosd_trigger_func_pb_titlechange_cb }, | |
75 | |
76 [AOSD_TRIGGER_VOL_CHANGE] = { N_("Volume Change") , | |
637
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
77 N_("Triggers OSD when volume is changed.") , |
623 | 78 aosd_trigger_func_vol_change_onoff , |
79 aosd_trigger_func_vol_change_cb } | |
597 | 80 }; |
81 | |
82 | |
83 | |
84 /* TRIGGER API */ | |
85 | |
86 void | |
87 aosd_trigger_get_codes_array ( gint ** array , gint * array_size ) | |
88 { | |
89 *array = aosd_trigger_codes; | |
90 *array_size = AOSD_TRIGGER_CODES_ARRAY_SIZE; | |
91 return; | |
92 } | |
93 | |
94 | |
95 const gchar * | |
96 aosd_trigger_get_name ( gint trig_code ) | |
97 { | |
98 return aosd_triggers[trig_code].name; | |
99 } | |
100 | |
101 | |
102 const gchar * | |
103 aosd_trigger_get_desc ( gint trig_code ) | |
104 { | |
105 return aosd_triggers[trig_code].desc; | |
106 } | |
107 | |
108 | |
109 void | |
110 aosd_trigger_start ( aosd_cfg_osd_trigger_t * cfg_trigger ) | |
111 { | |
112 gint i = 0; | |
113 for ( i = 0 ; i < cfg_trigger->active->len ; i++ ) | |
114 { | |
115 gint trig_code = g_array_index( cfg_trigger->active , gint , i ); | |
116 aosd_triggers[trig_code].onoff_func( TRUE ); | |
117 } | |
118 return; | |
119 } | |
120 | |
121 | |
122 void | |
123 aosd_trigger_stop ( aosd_cfg_osd_trigger_t * cfg_trigger ) | |
124 { | |
125 gint i = 0; | |
126 for ( i = 0 ; i < cfg_trigger->active->len ; i++ ) | |
127 { | |
128 gint trig_code = g_array_index( cfg_trigger->active , gint , i ); | |
129 aosd_triggers[trig_code].onoff_func( FALSE ); | |
130 } | |
131 return; | |
132 } | |
133 | |
134 | |
135 /* TRIGGER FUNCTIONS */ | |
136 | |
137 static void | |
138 aosd_trigger_func_pb_start_onoff ( gboolean turn_on ) | |
139 { | |
140 if ( turn_on == TRUE ) | |
141 hook_associate( "playback begin" , aosd_trigger_func_pb_start_cb , NULL ); | |
142 else | |
143 hook_dissociate( "playback begin" , aosd_trigger_func_pb_start_cb ); | |
144 return; | |
145 } | |
146 | |
147 static void | |
148 aosd_trigger_func_pb_start_cb ( gpointer plentry_gp , gpointer unused ) | |
149 { | |
150 PlaylistEntry *pl_entry = plentry_gp; | |
598
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
151 if ( plentry_gp != NULL ) |
597 | 152 { |
598
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
153 gchar *title; |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
154 if ( pl_entry->title != NULL ) |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
155 { |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
156 /* if there is a proper title, use it */ |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
157 title = g_strdup(pl_entry->title); |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
158 } |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
159 else |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
160 { |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
161 /* pick what we have as song title */ |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
162 Playlist *active = playlist_get_active(); |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
163 gint pos = playlist_get_position(active); |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
164 title = playlist_get_songtitle(active, pos); |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
165 } |
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
166 gchar *utf8_title = str_to_utf8( title ); |
597 | 167 gchar *utf8_title_markup = g_markup_printf_escaped( |
168 "<span font_desc='%s'>%s</span>" , global_config->osd->text.fonts_name[0] , utf8_title ); | |
169 aosd_display( utf8_title_markup , global_config->osd , FALSE ); | |
170 g_free( utf8_title_markup ); | |
171 g_free( utf8_title ); | |
598
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
172 g_free( title ); |
597 | 173 } |
174 return; | |
175 } | |
176 | |
177 | |
178 | |
179 typedef struct | |
180 { | |
181 gchar *title; | |
182 gchar *filename; | |
183 } | |
184 aosd_pb_titlechange_prevs_t; | |
185 | |
186 | |
187 static void | |
623 | 188 aosd_trigger_func_pb_titlechange_onoff ( gboolean turn_on ) |
597 | 189 { |
190 static aosd_pb_titlechange_prevs_t *prevs = NULL; | |
191 | |
192 if ( turn_on == TRUE ) | |
193 { | |
194 prevs = g_malloc0(sizeof(aosd_pb_titlechange_prevs_t)); | |
195 prevs->title = NULL; | |
196 prevs->filename = NULL; | |
197 hook_associate( "playlist set info" , aosd_trigger_func_pb_titlechange_cb , prevs ); | |
198 } | |
199 else | |
200 { | |
201 hook_dissociate( "playlist set info" , aosd_trigger_func_pb_titlechange_cb ); | |
202 if ( prevs != NULL ) | |
203 { | |
204 if ( prevs->title != NULL ) g_free( prevs->title ); | |
205 if ( prevs->filename != NULL ) g_free( prevs->filename ); | |
206 g_free( prevs ); | |
207 prevs = NULL; | |
208 } | |
209 } | |
210 return; | |
211 } | |
212 | |
213 static void | |
214 aosd_trigger_func_pb_titlechange_cb ( gpointer plentry_gp , gpointer prevs_gp ) | |
215 { | |
216 if ( ip_data.playing ) | |
217 { | |
218 aosd_pb_titlechange_prevs_t *prevs = prevs_gp; | |
219 PlaylistEntry *pl_entry = plentry_gp; | |
220 | |
221 /* same filename but title changed, useful to detect http stream song changes */ | |
222 | |
223 if ( ( prevs->title != NULL ) && ( prevs->filename != NULL ) ) | |
224 { | |
598
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
225 if ( ( pl_entry->filename != NULL ) && ( !strcmp(pl_entry->filename,prevs->filename) ) ) |
597 | 226 { |
598
e83e6fb3ebfa
[svn] - aosd: stability fixes for triggers (handle situation where a plentry exists but plentry->title is null)
giacomo
parents:
597
diff
changeset
|
227 if ( ( pl_entry->title != NULL ) && ( strcmp(pl_entry->title,prevs->title) ) ) |
597 | 228 { |
229 /* string formatting is done here a.t.m. - TODO - improve this area */ | |
230 gchar *utf8_title = str_to_utf8( pl_entry->title ); | |
231 gchar *utf8_title_markup = g_markup_printf_escaped( | |
232 "<span font_desc='%s'>%s</span>" , global_config->osd->text.fonts_name[0] , utf8_title ); | |
233 aosd_display( utf8_title_markup , global_config->osd , FALSE ); | |
234 g_free( utf8_title_markup ); | |
235 g_free( utf8_title ); | |
236 g_free( prevs->title ); | |
237 prevs->title = g_strdup(pl_entry->title); | |
238 } | |
239 } | |
240 else | |
241 { | |
242 g_free(prevs->filename); | |
243 prevs->filename = g_strdup(pl_entry->filename); | |
609
9b73eb35f4ff
[svn] - aosd: avoid collision between playback start trigger and titlechange trigger, that occurred when the next file in playlist was played
giacomo
parents:
598
diff
changeset
|
244 /* if filename changes, reset title as well */ |
9b73eb35f4ff
[svn] - aosd: avoid collision between playback start trigger and titlechange trigger, that occurred when the next file in playlist was played
giacomo
parents:
598
diff
changeset
|
245 if ( prevs->title != NULL ) |
9b73eb35f4ff
[svn] - aosd: avoid collision between playback start trigger and titlechange trigger, that occurred when the next file in playlist was played
giacomo
parents:
598
diff
changeset
|
246 g_free(prevs->title); |
9b73eb35f4ff
[svn] - aosd: avoid collision between playback start trigger and titlechange trigger, that occurred when the next file in playlist was played
giacomo
parents:
598
diff
changeset
|
247 prevs->title = g_strdup(pl_entry->title); |
597 | 248 } |
249 } | |
250 else | |
251 { | |
252 if ( prevs->title != NULL ) | |
253 g_free(prevs->title); | |
254 prevs->title = g_strdup(pl_entry->title); | |
255 if ( prevs->filename != NULL ) | |
256 g_free(prevs->filename); | |
257 prevs->filename = g_strdup(pl_entry->filename); | |
258 } | |
259 } | |
260 } | |
623 | 261 |
262 | |
263 static void | |
264 aosd_trigger_func_vol_change_onoff ( gboolean turn_on ) | |
265 { | |
637
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
266 if ( turn_on == TRUE ) |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
267 hook_associate( "volume set" , aosd_trigger_func_vol_change_cb , NULL ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
268 else |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
269 hook_dissociate( "volume set" , aosd_trigger_func_vol_change_cb ); |
623 | 270 return; |
271 } | |
272 | |
637
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
273 typedef struct |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
274 { |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
275 gint h_vol[2]; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
276 gint sid; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
277 } |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
278 aosd_vol_change_bucket_t; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
279 |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
280 static gboolean |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
281 aosd_trigger_func_vol_change_timeout ( gpointer bucket_gp ) |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
282 { |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
283 aosd_vol_change_bucket_t *bucket = bucket_gp; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
284 gchar *utf8_title_markup = g_markup_printf_escaped( |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
285 "<span font_desc='%s'>Volume Change - L: %i , R: %i</span>" , |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
286 global_config->osd->text.fonts_name[0] , bucket->h_vol[0] , bucket->h_vol[1] ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
287 aosd_display( utf8_title_markup , global_config->osd , FALSE ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
288 g_free( utf8_title_markup ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
289 bucket->sid = 0; /* reset source id value */ |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
290 return FALSE; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
291 } |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
292 |
623 | 293 static void |
637
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
294 aosd_trigger_func_vol_change_cb ( gpointer h_vol_gp , gpointer unused ) |
623 | 295 { |
637
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
296 gint *h_vol = h_vol_gp; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
297 static aosd_vol_change_bucket_t bucket = { { 0 , 0 } , 0 }; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
298 |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
299 bucket.h_vol[0] = h_vol[0]; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
300 bucket.h_vol[1] = h_vol[1]; |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
301 |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
302 /* in order to avoid repeated display of osd for each volume variation, use a |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
303 timer to prevent it from appearing more than once when multiple volume |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
304 changes are performed in a short time interval (500 msec) */ |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
305 if ( bucket.sid == 0 ) |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
306 { |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
307 /* first call in the time interval */ |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
308 bucket.sid = g_timeout_add( 500 , aosd_trigger_func_vol_change_timeout , &bucket ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
309 } |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
310 else |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
311 { |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
312 /* another call in the same interval, reset the interval */ |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
313 g_source_remove( bucket.sid ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
314 bucket.sid = g_timeout_add( 500 , aosd_trigger_func_vol_change_timeout , &bucket ); |
a1687bd302ce
[svn] - aosd: completed volume change trigger, works now
giacomo
parents:
623
diff
changeset
|
315 } |
623 | 316 return; |
317 } |