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  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <libaudcore/hook.h>
22 #include <libaudcore/vfs.h>
23 
24 #include "config.h"
25 #include "drct.h"
26 #include "i18n.h"
27 #include "misc.h"
28 #include "playback.h"
29 #include "playlist.h"
30 
31 /* --- PROGRAM CONTROL --- */
32 
33 void drct_quit (void)
34 {
35  hook_call ("quit", NULL);
36 }
37 
38 /* --- PLAYBACK CONTROL --- */
39 
40 void drct_play (void)
41 {
43  if (playlist < 0)
44  playlist = playlist_get_active ();
45 
46  drct_play_playlist (playlist);
47 }
48 
50 {
51  bool_t same_playlist = (playlist_get_playing () == playlist);
52 
53  if (! same_playlist)
54  playlist_set_playing (playlist);
55 
56  if (playback_get_playing ())
57  {
58  if (playback_get_paused ())
59  playback_pause ();
60  else if (same_playlist)
61  playback_seek (0);
62  }
63  else
64  {
65  if (playlist_get_position (playlist) < 0)
66  playlist_next_song (playlist, TRUE);
67 
68  playback_play (0, FALSE);
69  }
70 }
71 
72 void drct_pause (void)
73 {
74  if (playback_get_playing ())
75  playback_pause ();
76 }
77 
78 void drct_stop (void)
79 {
80  if (playback_get_playing ())
81  playback_stop ();
82 }
83 
85 {
86  return playback_get_playing ();
87 }
88 
90 {
91  return playback_get_ready ();
92 }
93 
95 {
96  return playback_get_paused ();
97 }
98 
99 char * drct_get_filename (void)
100 {
101  return playback_get_filename ();
102 }
103 
104 char * drct_get_title (void)
105 {
106  return playback_get_title ();
107 }
108 
109 void drct_get_info (int * bitrate, int * samplerate, int * channels)
110 {
111  playback_get_info (bitrate, samplerate, channels);
112 }
113 
114 int drct_get_time (void)
115 {
116  return playback_get_time ();
117 }
118 
119 int drct_get_length (void)
120 {
121  return playback_get_length ();
122 }
123 
124 void drct_seek (int time)
125 {
126  playback_seek (time);
127 }
128 
129 /* --- VOLUME CONTROL --- */
130 
131 void drct_get_volume (int * left, int * right)
132 {
133  playback_get_volume (left, right);
134  * left = CLAMP (* left, 0, 100);
135  * right = CLAMP (* right, 0, 100);
136 }
137 
138 void drct_set_volume (int left, int right)
139 {
140  playback_set_volume (CLAMP (left, 0, 100), CLAMP (right, 0, 100));
141 }
142 
143 void drct_get_volume_main (int * volume)
144 {
145  int left, right;
146  drct_get_volume (& left, & right);
147  * volume = MAX (left, right);
148 }
149 
150 void drct_set_volume_main (int volume)
151 {
152  int left, right, current;
153  drct_get_volume (& left, & right);
154  current = MAX (left, right);
155 
156  if (current > 0)
157  drct_set_volume (volume * left / current, volume * right / current);
158  else
159  drct_set_volume (volume, volume);
160 }
161 
162 void drct_get_volume_balance (int * balance)
163 {
164  int left, right;
165  drct_get_volume (& left, & right);
166 
167  if (left == right)
168  * balance = 0;
169  else if (left > right)
170  * balance = -100 + right * 100 / left;
171  else
172  * balance = 100 - left * 100 / right;
173 }
174 
175 void drct_set_volume_balance (int balance)
176 {
177  int left, right;
178  drct_get_volume_main (& left);
179 
180  if (balance < 0)
181  right = left * (100 + balance) / 100;
182  else
183  {
184  right = left;
185  left = right * (100 - balance) / 100;
186  }
187 
188  drct_set_volume (left, right);
189 }
190 
191 /* --- PLAYLIST CONTROL --- */
192 
193 void drct_pl_next (void)
194 {
196 
198  if (playlist < 0)
199  playlist = playlist_get_active ();
200 
201  if (playlist_next_song (playlist, get_bool (NULL, "repeat")) && play)
202  {
203  playlist_set_playing (playlist);
204  playback_play (0, FALSE);
205  }
206 }
207 
208 void drct_pl_prev (void)
209 {
211 
213  if (playlist < 0)
214  playlist = playlist_get_active ();
215 
216  if (playlist_prev_song (playlist) && play)
217  {
218  playlist_set_playing (playlist);
219  playback_play (0, FALSE);
220  }
221 }
222 
223 static void add_list (Index * filenames, int at, bool_t to_temp, bool_t play)
224 {
225  if (to_temp)
227 
228  int playlist = playlist_get_active ();
229 
230  if (play)
231  {
232  if (get_bool (NULL, "clear_playlist"))
233  playlist_entry_delete (playlist, 0, playlist_entry_count (playlist));
234  else
235  playlist_queue_delete (playlist, 0, playlist_queue_count (playlist));
236  }
237 
238  playlist_entry_insert_batch (playlist, at, filenames, NULL, play);
239 }
240 
241 void drct_pl_add (const char * filename, int at)
242 {
243  Index * filenames = index_new ();
244  index_append (filenames, str_get (filename));
245  add_list (filenames, at, FALSE, FALSE);
246 }
247 
248 void drct_pl_add_list (Index * filenames, int at)
249 {
250  add_list (filenames, at, FALSE, FALSE);
251 }
252 
253 void drct_pl_open (const char * filename)
254 {
255  Index * filenames = index_new ();
256  index_append (filenames, str_get (filename));
257  add_list (filenames, -1, get_bool (NULL, "open_to_temporary"), TRUE);
258 }
259 
261 {
262  add_list (filenames, -1, get_bool (NULL, "open_to_temporary"), TRUE);
263 }
264 
265 void drct_pl_open_temp (const char * filename)
266 {
267  Index * filenames = index_new ();
268  index_append (filenames, str_get (filename));
269  add_list (filenames, -1, TRUE, TRUE);
270 }
271 
273 {
274  add_list (filenames, -1, TRUE, TRUE);
275 }
276 
277 /* Advancing to the next song when the current one is deleted is tricky. First,
278  * we delete all the selected songs except the current one. We can then advance
279  * to a new song without worrying about picking one that is also selected.
280  * Finally, we can delete the former current song without stopping playback. */
281 
282 void drct_pl_delete_selected (int list)
283 {
284  int pos = playlist_get_position (list);
285 
286  if (get_bool (NULL, "advance_on_delete")
287  && ! get_bool (NULL, "no_playlist_advance")
288  && playback_get_playing () && list == playlist_get_playing ()
289  && pos >= 0 && playlist_entry_get_selected (list, pos))
290  {
291  playlist_entry_set_selected (list, pos, FALSE);
293  pos = playlist_get_position (list); /* it may have moved */
294 
295  if (playlist_next_song (list, get_bool (NULL, "repeat"))
296  && playlist_get_position (list) != pos)
297  playback_play (0, FALSE);
298 
299  playlist_entry_delete (list, pos, 1);
300  }
301  else
303 }