1 /**********************************************************
2  *
3  * mp3splt-gtk -- utility based on mp3splt,
4  *                for mp3/ogg splitting without decoding
5  *
6  * Copyright: (C) 2005-2014 Alexandru Munteanu
7  * Contact: m@ioalex.net
8  *
9  * from BMP to Audacious patch from Roberto Neri - 2007,2008
10  *
11  * http://mp3splt.sourceforge.net/
12  *
13  *********************************************************/
14 
15 /**********************************************************
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * as published by the Free Software Foundation; either version 2
20  * of the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
30  * USA.
31  *
32  *********************************************************/
33 
34 /*!********************************************************
35  * \file
36  * audacious control
37  *
38  * this file contains the functions that control the audacious
39  * player
40  ********************************************************/
41 
42 #include "audacious_control.h"
43 
44 #ifndef NO_AUDACIOUS
45 
46 #include <audacious/audctrl.h>
47 #include <audacious/dbus.h>
48 
49 #include <dbus/dbus-glib.h>
50 
51 //!Acquires informations about the song
myaudacious_get_song_infos(gchar * total_infos,ui_state * ui)52 void myaudacious_get_song_infos(gchar *total_infos, ui_state *ui)
53 {
54   gint freq, rate, nch;
55   audacious_remote_get_info(ui->pi->dbus_proxy, &rate, &freq, &nch);
56 
57   gchar rate_str[32] = { '\0' };
58   gchar freq_str[32] = { '\0' };
59   gchar nch_str[32] = { '\0' };
60 
61   g_snprintf(rate_str,32, "%d", rate/1000);
62   g_snprintf(freq_str,32, "%d", freq/1000);
63 
64   if (nch == 2)
65   {
66     snprintf(nch_str, 32, "%s", _("stereo"));
67   }
68   else
69   {
70     snprintf(nch_str, 32, "%s", _("mono"));
71   }
72 
73   gchar *_Kbps = _("Kbps");
74   gchar *_Khz = _("Khz");
75 
76   if (rate != 0)
77   {
78     g_snprintf(total_infos, 512, "%s %s     %s %s    %s", rate_str,_Kbps,freq_str, _Khz,nch_str);
79     return;
80   }
81 
82   total_infos[0] = '\0';
83 }
84 
85 /*!returns the filename
86 
87 The filename is allocated by this function and must be g_free'ed after use.
88 */
myaudacious_get_filename(ui_state * ui)89 gchar *myaudacious_get_filename(ui_state *ui)
90 {
91   gint playlist_position = audacious_remote_get_playlist_pos(ui->pi->dbus_proxy);
92   gchar *fname = audacious_remote_get_playlist_file(ui->pi->dbus_proxy, playlist_position);
93 
94   if (fname == NULL)
95   {
96     return NULL;
97   }
98 
99   gchar *fname2 = g_filename_from_uri(fname, NULL, NULL);
100   g_free(fname);
101 
102   return fname2;
103 }
104 
105 //!returns the number of songs in the playlist
myaudacious_get_playlist_number(ui_state * ui)106 gint myaudacious_get_playlist_number(ui_state *ui)
107 {
108   return audacious_remote_get_playlist_length(ui->pi->dbus_proxy);
109 }
110 
111 /*!returns the title of the song
112 
113 The filename is allocated by this function and must be g_free'ed after use.
114 */
myaudacious_get_title_song(ui_state * ui)115 gchar *myaudacious_get_title_song(ui_state *ui)
116 {
117   gint playlist_position = audacious_remote_get_playlist_pos(ui->pi->dbus_proxy);
118   return audacious_remote_get_playlist_title(ui->pi->dbus_proxy, playlist_position);
119 }
120 
121 //!returns elapsed time
myaudacious_get_time_elapsed(ui_state * ui)122 gint myaudacious_get_time_elapsed(ui_state *ui)
123 {
124   return audacious_remote_get_output_time(ui->pi->dbus_proxy);
125 }
126 
127 //!starts audacious
myaudacious_start(ui_state * ui)128 void myaudacious_start(ui_state *ui)
129 {
130   static gchar *exec_command = "audacious";
131   gchar *exec_this = g_strdup_printf("%s &", exec_command);
132   system(exec_this);
133 
134   time_t lt;
135   gint timer = time(&lt);
136   while (!audacious_remote_is_running(ui->pi->dbus_proxy) && ((time(&lt) - timer) < 4))
137   {
138     usleep(0);
139   }
140 
141   g_free(exec_this);
142 }
143 
144 //!selects the last file in the playlist
myaudacious_select_last_file(ui_state * ui)145 void myaudacious_select_last_file(ui_state *ui)
146 {
147   gint number = audacious_remote_get_playlist_length(ui->pi->dbus_proxy);
148   audacious_remote_set_playlist_pos(ui->pi->dbus_proxy, number - 1);
149 }
150 
151 //!plays the last file of the playlist
myaudacious_play_last_file(ui_state * ui)152 void myaudacious_play_last_file(ui_state *ui)
153 {
154   myaudacious_select_last_file(ui);
155   audacious_remote_play(ui->pi->dbus_proxy);
156 }
157 
158 //!add files to the audacious playlist
myaudacious_add_files(GList * list,ui_state * ui)159 void myaudacious_add_files(GList *list, ui_state *ui)
160 {
161   GList *list_pos = list;
162   while (list_pos)
163   {
164     gchar *dup_filename = strdup(list_pos->data);
165     list_pos->data = g_filename_to_uri(dup_filename,NULL,NULL);
166     g_free(dup_filename);
167     list_pos = g_list_next(list_pos);
168   }
169 
170   audacious_remote_playlist_add(ui->pi->dbus_proxy, list);
171 }
172 
173 //!sets the volume level
myaudacious_set_volume(gint volume,ui_state * ui)174 void myaudacious_set_volume(gint volume, ui_state *ui)
175 {
176   audacious_remote_set_main_volume(ui->pi->dbus_proxy, volume);
177 }
178 
179 //!returns volume level
myaudacious_get_volume(ui_state * ui)180 gint myaudacious_get_volume(ui_state *ui)
181 {
182   return audacious_remote_get_main_volume(ui->pi->dbus_proxy);
183 }
184 
185 /*!starts audacious with songs
186 
187   \param list The list of the songs to start audacious with
188  */
myaudacious_start_with_songs(GList * list,ui_state * ui)189 void myaudacious_start_with_songs(GList *list, ui_state *ui)
190 {
191   myaudacious_start(ui);
192   myaudacious_add_files(list, ui);
193 }
194 
195 //!returns TRUE if audacious is running; if not, FALSE
myaudacious_is_running(ui_state * ui)196 gint myaudacious_is_running(ui_state *ui)
197 {
198   if (!ui->pi->dbus_connection)
199   {
200     ui->pi->dbus_connection = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
201   }
202 
203   if (!ui->pi->dbus_proxy)
204   {
205     ui->pi->dbus_proxy = dbus_g_proxy_new_for_name(ui->pi->dbus_connection,
206         AUDACIOUS_DBUS_SERVICE,
207         AUDACIOUS_DBUS_PATH,
208         AUDACIOUS_DBUS_INTERFACE);
209   }
210 
211   if (!audacious_remote_is_running(ui->pi->dbus_proxy))
212   {
213     return FALSE;
214   }
215 
216   return TRUE;
217 }
218 
219 //!returns TRUE if audacious is paused, if not, FALSE
myaudacious_is_paused(ui_state * ui)220 gint myaudacious_is_paused(ui_state *ui)
221 {
222   if (!audacious_remote_is_paused(ui->pi->dbus_proxy))
223   {
224     return FALSE;
225   }
226 
227   return TRUE;
228 }
229 
230 //!Start playing the current song
myaudacious_play(ui_state * ui)231 void myaudacious_play(ui_state *ui)
232 {
233   audacious_remote_play(ui->pi->dbus_proxy);
234 }
235 
236 //!Stop playing the current song
myaudacious_stop(ui_state * ui)237 void myaudacious_stop(ui_state *ui)
238 {
239   audacious_remote_stop(ui->pi->dbus_proxy);
240 }
241 
242 //!Pause playing the current song
myaudacious_pause(ui_state * ui)243 void myaudacious_pause(ui_state *ui)
244 {
245   audacious_remote_pause(ui->pi->dbus_proxy);
246 }
247 
248 //!Switch to the next song
myaudacious_next(ui_state * ui)249 void myaudacious_next(ui_state *ui)
250 {
251   audacious_remote_playlist_next(ui->pi->dbus_proxy);
252 }
253 
254 //!Switch to the previous song
myaudacious_prev(ui_state * ui)255 void myaudacious_prev(ui_state *ui)
256 {
257   audacious_remote_playlist_prev(ui->pi->dbus_proxy);
258 }
259 
260 //!jump to time
myaudacious_jump(gint position,ui_state * ui)261 void myaudacious_jump(gint position, ui_state *ui)
262 {
263   audacious_remote_jump_to_time(ui->pi->dbus_proxy, position);
264 }
265 
266 //!returns the total duration of the current song
myaudacious_get_total_time(ui_state * ui)267 gint myaudacious_get_total_time(ui_state *ui)
268 {
269   gint playlist_position = audacious_remote_get_playlist_pos(ui->pi->dbus_proxy);
270   return audacious_remote_get_playlist_time(ui->pi->dbus_proxy, playlist_position);
271 }
272 
273 //!returns TRUE if audacious is playing, else FALSE
myaudacious_is_playing(ui_state * ui)274 gint myaudacious_is_playing(ui_state *ui)
275 {
276   if (audacious_remote_is_playing(ui->pi->dbus_proxy))
277   {
278     return TRUE;
279   }
280 
281   return FALSE;
282 }
283 
284 #endif
285 
286