Audacious
$Id:Doxyfile42802007-03-2104:39:00Znenolod$
|
00001 /* 00002 * drct.c 00003 * Copyright 2009-2010 John Lindgren 00004 * 00005 * This file is part of Audacious. 00006 * 00007 * Audacious is free software: you can redistribute it and/or modify it under 00008 * the terms of the GNU General Public License as published by the Free Software 00009 * Foundation, version 2 or version 3 of the License. 00010 * 00011 * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY 00012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00013 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License along with 00016 * Audacious. If not, see <http://www.gnu.org/licenses/>. 00017 * 00018 * The Audacious team does not consider modular code linking to Audacious or 00019 * using our public API to be a derived work. 00020 */ 00021 00022 #include <glib.h> 00023 #include <libaudcore/hook.h> 00024 #include <libaudcore/vfs.h> 00025 00026 #include "audconfig.h" 00027 #include "compatibility.h" 00028 #include "config.h" 00029 #include "drct.h" 00030 #include "i18n.h" 00031 #include "main.h" 00032 #include "playback.h" 00033 #include "playlist.h" 00034 00035 /* --- PROGRAM CONTROL --- */ 00036 00037 void drct_quit (void) 00038 { 00039 aud_quit (); 00040 } 00041 00042 /* --- PLAYBACK CONTROL --- */ 00043 00044 void drct_play (void) 00045 { 00046 if (playback_get_playing ()) 00047 { 00048 if (playback_get_paused ()) 00049 playback_pause (); 00050 else 00051 playback_seek (0); 00052 } 00053 else 00054 playback_play (0, FALSE); 00055 } 00056 00057 void drct_pause (void) 00058 { 00059 if (playback_get_playing ()) 00060 playback_pause (); 00061 } 00062 00063 void drct_stop (void) 00064 { 00065 if (playback_get_playing ()) 00066 playback_stop (); 00067 } 00068 00069 gboolean drct_get_playing (void) 00070 { 00071 return playback_get_playing (); 00072 } 00073 00074 gboolean drct_get_paused (void) 00075 { 00076 return playback_get_paused (); 00077 } 00078 00079 gchar * drct_get_title (void) 00080 { 00081 return playback_get_title (); 00082 } 00083 00084 void drct_get_info (gint * bitrate, gint * samplerate, gint * channels) 00085 { 00086 playback_get_info (bitrate, samplerate, channels); 00087 } 00088 00089 gint drct_get_time (void) 00090 { 00091 return playback_get_time (); 00092 } 00093 00094 gint drct_get_length (void) 00095 { 00096 return playback_get_length (); 00097 } 00098 00099 void drct_seek (gint time) 00100 { 00101 playback_seek (time); 00102 } 00103 00104 /* --- VOLUME CONTROL --- */ 00105 00106 void drct_get_volume (gint * left, gint * right) 00107 { 00108 input_get_volume (left, right); 00109 * left = CLAMP (* left, 0, 100); 00110 * right = CLAMP (* right, 0, 100); 00111 } 00112 00113 void drct_set_volume (gint left, gint right) 00114 { 00115 input_set_volume (CLAMP (left, 0, 100), CLAMP (right, 0, 100)); 00116 } 00117 00118 void drct_get_volume_main (gint * volume) 00119 { 00120 gint left, right; 00121 drct_get_volume (& left, & right); 00122 * volume = MAX (left, right); 00123 } 00124 00125 void drct_set_volume_main (gint volume) 00126 { 00127 gint left, right, current; 00128 drct_get_volume (& left, & right); 00129 current = MAX (left, right); 00130 00131 if (current > 0) 00132 drct_set_volume (volume * left / current, volume * right / current); 00133 else 00134 drct_set_volume (volume, volume); 00135 } 00136 00137 void drct_get_volume_balance (gint * balance) 00138 { 00139 gint left, right; 00140 drct_get_volume (& left, & right); 00141 00142 if (left == right) 00143 * balance = 0; 00144 else if (left > right) 00145 * balance = -100 + right * 100 / left; 00146 else 00147 * balance = 100 - left * 100 / right; 00148 } 00149 00150 void drct_set_volume_balance (gint balance) 00151 { 00152 gint left, right; 00153 drct_get_volume_main (& left); 00154 00155 if (balance < 0) 00156 right = left * (100 + balance) / 100; 00157 else 00158 { 00159 right = left; 00160 left = right * (100 - balance) / 100; 00161 } 00162 00163 drct_set_volume (left, right); 00164 } 00165 00166 /* --- PLAYLIST CONTROL --- */ 00167 00168 gint drct_pl_get_length (void) 00169 { 00170 return playlist_entry_count (playlist_get_active ()); 00171 } 00172 00173 void drct_pl_next (void) 00174 { 00175 gboolean play = playback_get_playing (); 00176 if (playlist_next_song (playlist_get_playing (), cfg.repeat) && play) 00177 playback_play (0, FALSE); 00178 } 00179 00180 void drct_pl_prev (void) 00181 { 00182 gboolean play = playback_get_playing (); 00183 if (playlist_prev_song (playlist_get_playing ()) && play) 00184 playback_play (0, FALSE); 00185 } 00186 00187 gint drct_pl_get_pos (void) 00188 { 00189 return playlist_get_position (playlist_get_active ()); 00190 } 00191 00192 void drct_pl_set_pos (gint pos) 00193 { 00194 gint playlist = playlist_get_active (); 00195 gboolean play = playback_get_playing (); 00196 00197 playlist_set_position (playlist, pos); 00198 00199 if (play) 00200 { 00201 playlist_set_playing (playlist); 00202 playback_play (0, FALSE); 00203 } 00204 } 00205 00206 gboolean drct_pl_repeat_is_enabled (void) 00207 { 00208 return cfg.repeat; 00209 } 00210 00211 void drct_pl_repeat_toggle (void) 00212 { 00213 cfg.repeat = ! cfg.repeat; 00214 hook_call ("toggle repeat", NULL); 00215 } 00216 00217 gboolean drct_pl_shuffle_is_enabled (void) 00218 { 00219 return cfg.shuffle; 00220 } 00221 00222 void drct_pl_shuffle_toggle (void) 00223 { 00224 cfg.shuffle = ! cfg.shuffle; 00225 hook_call ("toggle shuffle", NULL); 00226 } 00227 00228 gchar * drct_pl_get_file (gint entry) 00229 { 00230 const gchar * filename = playlist_entry_get_filename 00231 (playlist_get_active (), entry); 00232 return (filename == NULL) ? NULL : g_strdup (filename); 00233 } 00234 00235 gchar * drct_pl_get_title (gint entry) 00236 { 00237 const gchar * title = playlist_entry_get_title (playlist_get_active (), 00238 entry, FALSE); 00239 return (title == NULL) ? NULL : g_strdup (title); 00240 } 00241 00242 gint drct_pl_get_time (gint pos) 00243 { 00244 return playlist_entry_get_length (playlist_get_active (), pos, FALSE); 00245 } 00246 00247 static void add_list (GList * list, gint at, gboolean play) 00248 { 00249 gint playlist = playlist_get_active (); 00250 00251 if (play) 00252 { 00253 if (cfg.clear_playlist) 00254 playlist_entry_delete (playlist, 0, playlist_entry_count (playlist)); 00255 else 00256 playlist_queue_delete (playlist, 0, playlist_queue_count (playlist)); 00257 } 00258 00259 gint entries = playlist_entry_count (playlist); 00260 if (at < 0) 00261 at = entries; 00262 00263 gint added = 0; 00264 GQueue folders = G_QUEUE_INIT; 00265 struct index * filenames = index_new (); 00266 00267 for (; list != NULL; list = list->next) 00268 { 00269 if (filename_is_playlist (list->data)) 00270 { 00271 playlist_insert_playlist (playlist, at + added, list->data); 00272 added += playlist_entry_count (playlist) - entries; 00273 entries = playlist_entry_count (playlist); 00274 } 00275 else if (vfs_file_test (list->data, G_FILE_TEST_IS_DIR)) 00276 g_queue_push_tail (& folders, list->data); 00277 else 00278 index_append (filenames, g_strdup (list->data)); 00279 } 00280 00281 playlist_entry_insert_batch (playlist, at + added, filenames, NULL); 00282 added += playlist_entry_count (playlist) - entries; 00283 00284 if (added && play) 00285 { 00286 playlist_set_playing (playlist); 00287 if (! cfg.shuffle) 00288 playlist_set_position (playlist, at); 00289 playback_play (0, FALSE); 00290 play = FALSE; 00291 } 00292 00293 const gchar * folder; 00294 while ((folder = g_queue_pop_head (& folders)) != NULL) 00295 { 00296 playlist_insert_folder (playlist, at + added, folder, play); 00297 play = FALSE; 00298 } 00299 } 00300 00301 void drct_pl_add (const gchar * filename, gint at) 00302 { 00303 GList * list = g_list_prepend (NULL, (void *) filename); 00304 add_list (list, at, FALSE); 00305 g_list_free (list); 00306 } 00307 00308 void drct_pl_add_list (GList * list, gint at) 00309 { 00310 add_list (list, at, FALSE); 00311 } 00312 00313 void drct_pl_open (const gchar * filename) 00314 { 00315 GList * list = g_list_prepend (NULL, (void *) filename); 00316 add_list (list, -1, TRUE); 00317 g_list_free (list); 00318 } 00319 00320 void drct_pl_open_list (GList * list) 00321 { 00322 add_list (list, -1, TRUE); 00323 } 00324 00325 static void activate_temp (void) 00326 { 00327 gint playlists = playlist_count (); 00328 const gchar * title = _("Temporary Playlist"); 00329 00330 for (gint playlist = 0; playlist < playlists; playlist ++) 00331 { 00332 if (! strcmp (playlist_get_title (playlist), title)) 00333 { 00334 playlist_set_active (playlist); 00335 return; 00336 } 00337 } 00338 00339 playlist_insert (playlists); 00340 playlist_set_title (playlists, title); 00341 playlist_set_active (playlists); 00342 } 00343 00344 void drct_pl_open_temp (const gchar * filename) 00345 { 00346 activate_temp (); 00347 drct_pl_open (filename); 00348 } 00349 00350 void drct_pl_open_temp_list (GList * list) 00351 { 00352 activate_temp (); 00353 drct_pl_open_list (list); 00354 } 00355 00356 void drct_pl_delete (gint entry) 00357 { 00358 playlist_entry_delete (playlist_get_active (), entry, 1); 00359 } 00360 00361 void drct_pl_clear (void) 00362 { 00363 gint playlist = playlist_get_active (); 00364 playlist_entry_delete (playlist, 0, playlist_entry_count (playlist)); 00365 } 00366 00367 /* --- PLAYLIST QUEUE CONTROL --- */ 00368 00369 gint drct_pq_get_length (void) 00370 { 00371 return playlist_queue_count (playlist_get_active ()); 00372 } 00373 00374 gint drct_pq_get_entry (gint queue_position) 00375 { 00376 return playlist_queue_get_entry (playlist_get_active (), queue_position); 00377 } 00378 00379 gboolean drct_pq_is_queued (gint entry) 00380 { 00381 return (drct_pq_get_queue_position (entry) >= 0); 00382 } 00383 00384 gint drct_pq_get_queue_position (gint entry) 00385 { 00386 return playlist_queue_find_entry (playlist_get_active (), entry); 00387 } 00388 00389 void drct_pq_add (gint entry) 00390 { 00391 playlist_queue_insert (playlist_get_active (), -1, entry); 00392 } 00393 00394 void drct_pq_remove (gint entry) 00395 { 00396 gint playlist = playlist_get_active (); 00397 playlist_queue_delete (playlist, playlist_queue_find_entry (playlist, 00398 entry), 1); 00399 } 00400 00401 void drct_pq_clear (void) 00402 { 00403 gint playlist = playlist_get_active (); 00404 playlist_queue_delete (playlist, 0, playlist_queue_count (playlist)); 00405 }