clean up audacious legacy code; adjust ebuilds
[monky] / src / audacious.c
1 /* $Id$ */
2
3 /*
4  * audacious.c:  conky support for audacious music player
5  *
6  * Copyright (C) 2005-2007 Philip Kovacs pkovacs@users.sourceforge.net
7  * 
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
21  * USA.
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <glib.h>
33 #ifndef AUDACIOUS_LEGACY
34 #include <glib-object.h>
35 #include <audacious/audctrl.h>
36 #include <audacious/dbus.h>
37 #else
38 #include <audacious/beepctrl.h>
39 #define audacious_remote_is_running(x)            xmms_remote_is_running(x) 
40 #define audacious_remote_is_paused(x)             xmms_remote_is_paused(x)
41 #define audacious_remote_is_playing(x)            xmms_remote_is_playing(x)
42 #define audacious_remote_get_playlist_pos(x)      xmms_remote_get_playlist_pos(x)
43 #define audacious_remote_get_playlist_title(x,y)  xmms_remote_get_playlist_title(x,y)
44 #define audacious_remote_get_playlist_time(x,y)   xmms_remote_get_playlist_time(x,y)
45 #define audacious_remote_get_output_time(x)       xmms_remote_get_output_time(x)
46 #define audacious_remote_get_info(w,x,y,z)        xmms_remote_get_info(w,x,y,z)
47 #define audacious_remote_get_playlist_file(x,y)   xmms_remote_get_playlist_file(x,y)
48 #define audacious_remote_get_playlist_length(x)   xmms_remote_get_playlist_length(x)
49 #endif
50
51 #include "config.h"
52 #include "conky.h"
53 #include "audacious.h"
54 #include "timed_thread.h"
55
56 /* access to this item array is synchronized */
57 static audacious_t audacious_items;
58
59 /* -----------------------------------------
60  * Conky update function for audacious data.
61  * ----------------------------------------- */
62 void update_audacious(void)
63 {
64   /* 
65     The worker thread is updating audacious_items array asynchronously to the main 
66     conky thread.  We merely copy the audacious_items array into the main thread's 
67     info structure when the main thread's update cycle fires. 
68   */
69   if (!info.audacious.p_timed_thread)
70     return;
71
72   timed_thread_lock (info.audacious.p_timed_thread);
73   memcpy(&info.audacious.items,audacious_items,sizeof(audacious_items));
74   timed_thread_unlock (info.audacious.p_timed_thread);
75 }
76
77
78 /* ------------------------------------------------------------
79  * Create a worker thread for audacious media player status.
80  *
81  * Returns 0 on success, -1 on error. 
82  * ------------------------------------------------------------*/
83 int create_audacious_thread(void)
84 {
85   if (!info.audacious.p_timed_thread)
86     info.audacious.p_timed_thread = 
87       timed_thread_create (audacious_thread_func, NULL, info.music_player_interval * 1000000);
88
89   if (!info.audacious.p_timed_thread || timed_thread_run (info.audacious.p_timed_thread))
90     return (-1);
91
92   return 0;
93 }
94
95 /* ------------------------------------------------
96  * Destroy audacious player status thread. 
97  *
98  * Returns 0 on success, -1 on error.
99  * ------------------------------------------------ */
100 int destroy_audacious_thread(void)
101 {
102   /* Is a worker is thread running? If not, no error. */
103   if (!info.audacious.p_timed_thread)
104     return(0);
105
106   timed_thread_destroy (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
107
108   return 0;
109 }
110
111 /* ---------------------------------------------------
112  * Worker thread function for audacious data sampling.
113  * --------------------------------------------------- */ 
114 void *audacious_thread_func(void *pvoid)
115 {
116   static audacious_t items;
117   gint playpos,frames,length;
118   gint rate,freq,chans;
119   gchar *psong,*pfilename;
120
121 #ifndef AUDACIOUS_LEGACY
122   DBusGProxy *session = NULL;
123   DBusGConnection *connection = NULL;
124 #else
125   gint session;
126 #endif
127
128
129   pvoid=(void *)pvoid;  /* avoid warning */
130   session=0;
131   psong=NULL;
132   pfilename=NULL;
133
134 #ifndef AUDACIOUS_LEGACY
135   g_type_init ();
136   connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
137   if (!connection) {
138     CRIT_ERR ("unable to establish dbus connection");
139   }
140   session = dbus_g_proxy_new_for_name (connection, 
141                                        AUDACIOUS_DBUS_SERVICE,
142                                        AUDACIOUS_DBUS_PATH,
143                                        AUDACIOUS_DBUS_INTERFACE);
144   if (!session) {
145     CRIT_ERR ("unable to establish dbus proxy");
146   }
147 #endif /* AUDACIOUS_LEGACY */
148
149   /* Loop until the main thread sets the runnable signal to 0i via timed_thread_destroy. */
150   while (1) {
151
152     if (!audacious_remote_is_running (session)) 
153     {
154       memset(&items,0,sizeof(items));
155       strcpy(items[AUDACIOUS_STATUS],"Not running");
156         goto next_iter;
157     }
158
159     /* Player status */
160     if (audacious_remote_is_paused (session))
161       strcpy(items[AUDACIOUS_STATUS],"Paused");
162     else if (audacious_remote_is_playing (session))
163       strcpy(items[AUDACIOUS_STATUS],"Playing");
164     else
165       strcpy(items[AUDACIOUS_STATUS],"Stopped");
166
167     /* Current song title */
168     playpos = audacious_remote_get_playlist_pos (session);
169     psong = audacious_remote_get_playlist_title (session, playpos);
170     if (psong) 
171     {
172       strncpy(items[AUDACIOUS_TITLE],psong,sizeof(items[AUDACIOUS_TITLE])-1);
173       g_free (psong);
174       psong=NULL;
175     }
176
177     /* Current song length as MM:SS */
178     frames = audacious_remote_get_playlist_time (session,playpos);
179     length = frames / 1000;
180     snprintf(items[AUDACIOUS_LENGTH],sizeof(items[AUDACIOUS_LENGTH])-1, "%d:%.2d", length / 60, length % 60);
181
182     /* Current song length in seconds */
183     snprintf(items[AUDACIOUS_LENGTH_SECONDS],sizeof(items[AUDACIOUS_LENGTH_SECONDS])-1, "%d", length);
184
185     /* Current song position as MM:SS */
186     frames = audacious_remote_get_output_time (session);
187     length = frames / 1000;
188     snprintf(items[AUDACIOUS_POSITION],sizeof(items[AUDACIOUS_POSITION])-1,
189              "%d:%.2d", length / 60, length % 60);
190
191     /* Current song position in seconds */
192     snprintf(items[AUDACIOUS_POSITION_SECONDS],sizeof(items[AUDACIOUS_POSITION_SECONDS])-1, "%d", length);
193
194     /* Current song bitrate */
195     audacious_remote_get_info (session, &rate, &freq, &chans);
196     snprintf(items[AUDACIOUS_BITRATE],sizeof(items[AUDACIOUS_BITRATE])-1, "%d", rate);
197
198     /* Current song frequency */
199     snprintf(items[AUDACIOUS_FREQUENCY],sizeof(items[AUDACIOUS_FREQUENCY])-1, "%d", freq);
200
201     /* Current song channels */
202     snprintf(items[AUDACIOUS_CHANNELS],sizeof(items[AUDACIOUS_CHANNELS])-1, "%d", chans);
203
204     /* Current song filename */
205     pfilename = audacious_remote_get_playlist_file (session,playpos);
206     if (pfilename) 
207     {
208       strncpy(items[AUDACIOUS_FILENAME],pfilename,sizeof(items[AUDACIOUS_FILENAME])-1);
209       g_free (pfilename);
210       pfilename=NULL;
211     }
212
213     /* Length of the Playlist (number of songs) */
214     length = audacious_remote_get_playlist_length (session);
215     snprintf(items[AUDACIOUS_PLAYLIST_LENGTH],sizeof(items[AUDACIOUS_PLAYLIST_LENGTH])-1, "%d", length);
216
217     /* Playlist position (index of song) */
218     snprintf(items[AUDACIOUS_PLAYLIST_POSITION],sizeof(items[AUDACIOUS_PLAYLIST_POSITION])-1, 
219            "%d", playpos+1);
220
221 next_iter:
222
223     /* Deliver the refreshed items array to audacious_items. */
224     timed_thread_lock (info.audacious.p_timed_thread);
225     memcpy(&audacious_items,items,sizeof(items));
226     timed_thread_unlock (info.audacious.p_timed_thread);
227
228     if (timed_thread_test (info.audacious.p_timed_thread))
229       timed_thread_exit (info.audacious.p_timed_thread);
230   }
231 }