mpd_interval -> music_player_interval
[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 #include <stdio.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <audacious/beepctrl.h>
30
31 #include "config.h"
32 #include "conky.h"
33 #include "audacious.h"
34 #include "timed_thread.h"
35
36 /* access to this item array is synchronized */
37 static audacious_t audacious_items;
38
39 /* -----------------------------------------
40  * Conky update function for audacious data.
41  * ----------------------------------------- */
42 void update_audacious(void)
43 {
44   /* 
45     The worker thread is updating audacious_items array asynchronously to the main 
46     conky thread.  We merely copy the audacious_items array into the main thread's 
47     info structure when the main thread's update cycle fires. 
48   */
49   if (!info.audacious.p_timed_thread)
50     return;
51
52   timed_thread_lock (info.audacious.p_timed_thread);
53   memcpy(&info.audacious.items,audacious_items,sizeof(audacious_items));
54   timed_thread_unlock (info.audacious.p_timed_thread);
55 }
56
57
58 /* ------------------------------------------------------------
59  * Create a worker thread for audacious media player status.
60  *
61  * Returns 0 on success, -1 on error. 
62  * ------------------------------------------------------------*/
63 int create_audacious_thread(void)
64 {
65   if (!info.audacious.p_timed_thread)
66     info.audacious.p_timed_thread = 
67       timed_thread_create (audacious_thread_func, NULL, info.music_player_interval * 1000000);
68
69   if (!info.audacious.p_timed_thread || timed_thread_run (info.audacious.p_timed_thread))
70     return (-1);
71
72   return 0;
73 }
74
75 /* ------------------------------------------------
76  * Destroy audacious player status thread. 
77  *
78  * Returns 0 on success, -1 on error.
79  * ------------------------------------------------ */
80 int destroy_audacious_thread(void)
81 {
82   /* Is a worker is thread running? If not, no error. */
83   if (!info.audacious.p_timed_thread)
84     return(0);
85
86   timed_thread_destroy (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
87
88   return 0;
89 }
90
91 /* ---------------------------------------------------
92  * Worker thread function for audacious data sampling.
93  * --------------------------------------------------- */ 
94 void *audacious_thread_func(void *pvoid)
95 {
96   static audacious_t items;
97   gint session,playpos,frames,length;
98   gint rate,freq,chans;
99   gchar *psong,*pfilename;
100
101   pvoid=(void *)pvoid;  /* avoid warning */
102   session=0;
103   psong=NULL;
104   pfilename=NULL;
105
106   /* Loop until the main thread sets the runnable signal to 0i via timed_thread_destroy. */
107   while (1) {
108
109     if (!xmms_remote_is_running (session)) 
110     {
111       memset(&items,0,sizeof(items));
112       strcpy(items[AUDACIOUS_STATUS],"Not running");
113         goto next_iter;
114     }
115
116     /* Player status */
117     if (xmms_remote_is_paused (session))
118       strcpy(items[AUDACIOUS_STATUS],"Paused");
119     else if (xmms_remote_is_playing (session))
120       strcpy(items[AUDACIOUS_STATUS],"Playing");
121     else
122       strcpy(items[AUDACIOUS_STATUS],"Stopped");
123
124     /* Current song title */
125     playpos = xmms_remote_get_playlist_pos (session);
126     psong = xmms_remote_get_playlist_title (session, playpos);
127     if (psong) 
128     {
129       strncpy(items[AUDACIOUS_TITLE],psong,sizeof(items[AUDACIOUS_TITLE])-1);
130       g_free (psong);
131       psong=NULL;
132     }
133
134     /* Current song length as MM:SS */
135     frames = xmms_remote_get_playlist_time (session,playpos);
136     length = frames / 1000;
137     snprintf(items[AUDACIOUS_LENGTH],sizeof(items[AUDACIOUS_LENGTH])-1, "%d:%.2d", length / 60, length % 60);
138
139     /* Current song length in seconds */
140     snprintf(items[AUDACIOUS_LENGTH_SECONDS],sizeof(items[AUDACIOUS_LENGTH_SECONDS])-1, "%d", length);
141
142     /* Current song position as MM:SS */
143     frames = xmms_remote_get_output_time (session);
144     length = frames / 1000;
145     snprintf(items[AUDACIOUS_POSITION],sizeof(items[AUDACIOUS_POSITION])-1,
146              "%d:%.2d", length / 60, length % 60);
147
148     /* Current song position in seconds */
149     snprintf(items[AUDACIOUS_POSITION_SECONDS],sizeof(items[AUDACIOUS_POSITION_SECONDS])-1, "%d", length);
150
151     /* Current song bitrate */
152     xmms_remote_get_info (session, &rate, &freq, &chans);
153     snprintf(items[AUDACIOUS_BITRATE],sizeof(items[AUDACIOUS_BITRATE])-1, "%d", rate);
154
155     /* Current song frequency */
156     snprintf(items[AUDACIOUS_FREQUENCY],sizeof(items[AUDACIOUS_FREQUENCY])-1, "%d", freq);
157
158     /* Current song channels */
159     snprintf(items[AUDACIOUS_CHANNELS],sizeof(items[AUDACIOUS_CHANNELS])-1, "%d", chans);
160
161     /* Current song filename */
162     pfilename = xmms_remote_get_playlist_file (session,playpos);
163     if (pfilename) 
164     {
165       strncpy(items[AUDACIOUS_FILENAME],pfilename,sizeof(items[AUDACIOUS_FILENAME])-1);
166       g_free (pfilename);
167       pfilename=NULL;
168     }
169
170     /* Length of the Playlist (number of songs) */
171     length = xmms_remote_get_playlist_length (session);
172     snprintf(items[AUDACIOUS_PLAYLIST_LENGTH],sizeof(items[AUDACIOUS_PLAYLIST_LENGTH])-1, "%d", length);
173
174     /* Playlist position (index of song) */
175     snprintf(items[AUDACIOUS_PLAYLIST_POSITION],sizeof(items[AUDACIOUS_PLAYLIST_POSITION])-1, 
176            "%d", playpos+1);
177
178 next_iter:
179
180     /* Deliver the refreshed items array to audacious_items. */
181     timed_thread_lock (info.audacious.p_timed_thread);
182     memcpy(&audacious_items,items,sizeof(items));
183     timed_thread_unlock (info.audacious.p_timed_thread);
184
185     if (timed_thread_test (info.audacious.p_timed_thread))
186       timed_thread_exit (info.audacious.p_timed_thread);
187   }
188 }