Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
drct.c
Go to the documentation of this file.
1 /*
2  * drct.c
3  * Copyright 2009-2011 John Lindgren
4  *
5  * This file is part of Audacious.
6  *
7  * Audacious is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation, version 2 or version 3 of the License.
10  *
11  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Audacious. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The Audacious team does not consider modular code linking to Audacious or
19  * using our public API to be a derived work.
20  */
21 
22 #include <glib.h>
23 #include <libaudcore/hook.h>
24 #include <libaudcore/vfs.h>
25 
26 #include "config.h"
27 #include "drct.h"
28 #include "i18n.h"
29 #include "misc.h"
30 #include "playback.h"
31 #include "playlist.h"
32 
33 /* --- PROGRAM CONTROL --- */
34 
35 void drct_quit (void)
36 {
37  hook_call ("quit", NULL);
38 }
39 
40 /* --- PLAYBACK CONTROL --- */
41 
42 void drct_play (void)
43 {
44  if (playback_get_playing ())
45  {
46  if (playback_get_paused ())
47  playback_pause ();
48  else
49  playback_seek (0);
50  }
51  else
52  {
54  playback_play (0, FALSE);
55  }
56 }
57 
58 void drct_pause (void)
59 {
60  if (playback_get_playing ())
61  playback_pause ();
62 }
63 
64 void drct_stop (void)
65 {
66  if (playback_get_playing ())
67  playback_stop ();
68 }
69 
71 {
72  return playback_get_playing ();
73 }
74 
76 {
77  return playback_get_ready ();
78 }
79 
81 {
82  return playback_get_paused ();
83 }
84 
85 char * drct_get_filename (void)
86 {
87  return playback_get_filename ();
88 }
89 
90 char * drct_get_title (void)
91 {
92  return playback_get_title ();
93 }
94 
95 void drct_get_info (int * bitrate, int * samplerate, int * channels)
96 {
97  playback_get_info (bitrate, samplerate, channels);
98 }
99 
100 int drct_get_time (void)
101 {
102  return playback_get_time ();
103 }
104 
105 int drct_get_length (void)
106 {
107  return playback_get_length ();
108 }
109 
110 void drct_seek (int time)
111 {
112  playback_seek (time);
113 }
114 
115 /* --- VOLUME CONTROL --- */
116 
117 void drct_get_volume (int * left, int * right)
118 {
119  playback_get_volume (left, right);
120  * left = CLAMP (* left, 0, 100);
121  * right = CLAMP (* right, 0, 100);
122 }
123 
124 void drct_set_volume (int left, int right)
125 {
126  playback_set_volume (CLAMP (left, 0, 100), CLAMP (right, 0, 100));
127 }
128 
129 void drct_get_volume_main (int * volume)
130 {
131  int left, right;
132  drct_get_volume (& left, & right);
133  * volume = MAX (left, right);
134 }
135 
136 void drct_set_volume_main (int volume)
137 {
138  int left, right, current;
139  drct_get_volume (& left, & right);
140  current = MAX (left, right);
141 
142  if (current > 0)
143  drct_set_volume (volume * left / current, volume * right / current);
144  else
145  drct_set_volume (volume, volume);
146 }
147 
148 void drct_get_volume_balance (int * balance)
149 {
150  int left, right;
151  drct_get_volume (& left, & right);
152 
153  if (left == right)
154  * balance = 0;
155  else if (left > right)
156  * balance = -100 + right * 100 / left;
157  else
158  * balance = 100 - left * 100 / right;
159 }
160 
161 void drct_set_volume_balance (int balance)
162 {
163  int left, right;
164  drct_get_volume_main (& left);
165 
166  if (balance < 0)
167  right = left * (100 + balance) / 100;
168  else
169  {
170  right = left;
171  left = right * (100 - balance) / 100;
172  }
173 
174  drct_set_volume (left, right);
175 }
176 
177 /* --- PLAYLIST CONTROL --- */
178 
179 void drct_pl_next (void)
180 {
182  if (playlist_get_playing () < 0)
184  if (playlist_next_song (playlist_get_playing (), get_bool (NULL, "repeat")) && play)
185  playback_play (0, FALSE);
186 }
187 
188 void drct_pl_prev (void)
189 {
191  if (playlist_get_playing () < 0)
193  if (playlist_prev_song (playlist_get_playing ()) && play)
194  playback_play (0, FALSE);
195 }
196 
197 static void add_list (Index * filenames, int at, bool_t to_temp, bool_t play)
198 {
199  if (to_temp)
201 
202  int playlist = playlist_get_active ();
203 
204  if (play)
205  {
206  if (get_bool (NULL, "clear_playlist"))
207  playlist_entry_delete (playlist, 0, playlist_entry_count (playlist));
208  else
209  playlist_queue_delete (playlist, 0, playlist_queue_count (playlist));
210  }
211 
212  playlist_entry_insert_batch (playlist, at, filenames, NULL, play);
213 }
214 
215 void drct_pl_add (const char * filename, int at)
216 {
217  Index * filenames = index_new ();
218  index_append (filenames, str_get (filename));
219  add_list (filenames, at, FALSE, FALSE);
220 }
221 
222 void drct_pl_add_list (Index * filenames, int at)
223 {
224  add_list (filenames, at, FALSE, FALSE);
225 }
226 
227 void drct_pl_open (const char * filename)
228 {
229  Index * filenames = index_new ();
230  index_append (filenames, str_get (filename));
231  add_list (filenames, -1, get_bool (NULL, "open_to_temporary"), TRUE);
232 }
233 
235 {
236  add_list (filenames, -1, get_bool (NULL, "open_to_temporary"), TRUE);
237 }
238 
239 void drct_pl_open_temp (const char * filename)
240 {
241  Index * filenames = index_new ();
242  index_append (filenames, str_get (filename));
243  add_list (filenames, -1, TRUE, TRUE);
244 }
245 
247 {
248  add_list (filenames, -1, TRUE, TRUE);
249 }
250 
251 /* Advancing to the next song when the current one is deleted is tricky. First,
252  * we delete all the selected songs except the current one. We can then advance
253  * to a new song without worrying about picking one that is also selected.
254  * Finally, we can delete the former current song without stopping playback. */
255 
256 void drct_pl_delete_selected (int list)
257 {
258  int pos = playlist_get_position (list);
259 
260  if (get_bool (NULL, "advance_on_delete")
261  && ! get_bool (NULL, "no_playlist_advance")
262  && playback_get_playing () && list == playlist_get_playing ()
263  && pos >= 0 && playlist_entry_get_selected (list, pos))
264  {
265  playlist_entry_set_selected (list, pos, FALSE);
267  pos = playlist_get_position (list); /* it may have moved */
268 
269  if (playlist_next_song (list, get_bool (NULL, "repeat"))
270  && playlist_get_position (list) != pos)
271  playback_play (0, FALSE);
272 
273  playlist_entry_delete (list, pos, 1);
274  }
275  else
277 }