xmmssensor.cpp
00001 /*************************************************************************** 00002 * Copyright (C) 2003 by Hans Karlsson * 00003 * karlsson.h@home.se * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 ***************************************************************************/ 00010 #include "xmmssensor.h" 00011 00012 #ifdef HAVE_XMMS 00013 #include <tqlibrary.h> 00014 00015 class XMMSSensor::XMMS 00016 { 00017 public: 00018 XMMS() : libxmms( 0 ) 00019 { 00020 libxmms = new TQLibrary( "xmms.so.1" ); 00021 if ( !libxmms->load() ) 00022 { 00023 delete libxmms; 00024 libxmms = 0; 00025 } 00026 00027 if ( libxmms != 0 ) 00028 { 00029 // resolve functions 00030 *(void**) (&xmms_remote_is_running) = 00031 libxmms->resolve( "xmms_remote_is_running" ); 00032 00033 *(void**) (&xmms_remote_is_playing) = 00034 libxmms->resolve( "xmms_remote_is_playing" ); 00035 00036 *(void**) (&xmms_remote_get_playlist_title) = 00037 libxmms->resolve( "xmms_remote_get_playlist_title" ); 00038 00039 *(void**) (&xmms_remote_get_playlist_time) = 00040 libxmms->resolve( "xmms_remote_get_playlist_time" ); 00041 00042 *(void**) (&xmms_remote_get_playlist_pos) = 00043 libxmms->resolve( "xmms_remote_get_playlist_pos" ); 00044 00045 *(void**) (&xmms_remote_get_output_time) = 00046 libxmms->resolve( "xmms_remote_get_output_time" ); 00047 } 00048 } 00049 00050 bool isInitialized() const 00051 { 00052 return libxmms != 0 && 00053 xmms_remote_is_running != 0 && 00054 xmms_remote_is_playing != 0 && 00055 xmms_remote_get_playlist_title != 0 && 00056 xmms_remote_get_playlist_time != 0 && 00057 xmms_remote_get_playlist_pos != 0 && 00058 xmms_remote_get_output_time != 0; 00059 } 00060 00061 bool isRunning(int session) 00062 { 00063 if ( !isInitialized() ) return false; 00064 00065 return (*xmms_remote_is_running)(session); 00066 } 00067 00068 bool isPlaying(int session) 00069 { 00070 if ( !isInitialized() ) return false; 00071 00072 return (*xmms_remote_is_playing)(session); 00073 } 00074 00075 char* getPlaylistTitle(int session, int pos) 00076 { 00077 if ( !isInitialized() ) return ""; 00078 00079 return (*xmms_remote_get_playlist_title)(session, pos); 00080 } 00081 00082 int getPlaylistTime(int session, int pos) 00083 { 00084 if ( !isInitialized() ) return 0; 00085 00086 return (*xmms_remote_get_playlist_time)(session, pos); 00087 } 00088 00089 int getPlaylistPos(int session) 00090 { 00091 if ( !isInitialized() ) return 0; 00092 00093 return (*xmms_remote_get_playlist_pos)(session); 00094 } 00095 00096 int getOutputTime(int session) 00097 { 00098 if ( !isInitialized() ) return 0; 00099 00100 return (*xmms_remote_get_output_time)(session); 00101 } 00102 00103 private: 00104 TQLibrary* libxmms; 00105 00106 bool (*xmms_remote_is_running)(int); 00107 bool (*xmms_remote_is_playing)(int); 00108 00109 char* (*xmms_remote_get_playlist_title)(int, int); 00110 int (*xmms_remote_get_playlist_time)(int, int); 00111 int (*xmms_remote_get_playlist_pos)(int); 00112 int (*xmms_remote_get_output_time)(int); 00113 }; 00114 00115 #else // No XMMS 00116 00117 class XMMSSensor::XMMS 00118 { 00119 public: 00120 XMMS() {} 00121 00122 bool isInitialized() const { return false; } 00123 }; 00124 #endif // HAVE_XMMS 00125 00126 00127 XMMSSensor::XMMSSensor( int interval, const TQString &encoding ) 00128 : Sensor( interval ), xmms( 0 ) 00129 { 00130 if( !encoding.isEmpty() ) 00131 { 00132 codec = TQTextCodec::codecForName( encoding.ascii() ); 00133 if ( codec == 0) 00134 codec = TQTextCodec::codecForLocale(); 00135 } 00136 else 00137 codec = TQTextCodec::codecForLocale(); 00138 00139 xmms = new XMMS(); 00140 00141 } 00142 XMMSSensor::~XMMSSensor() 00143 { 00144 delete xmms; 00145 } 00146 00147 void XMMSSensor::update() 00148 { 00149 TQString format; 00150 SensorParams *sp; 00151 Meter *meter; 00152 TQObjectListIt it( *objList ); 00153 00154 #ifdef HAVE_XMMS 00155 00156 int pos; 00157 TQString title; 00158 int songLength = 0; 00159 int currentTime = 0; 00160 bool isPlaying = false; 00161 bool isRunning = xmms->isRunning(0); 00162 00163 if( isRunning ) 00164 { 00165 isPlaying = xmms->isPlaying(0); 00166 pos = xmms->getPlaylistPos(0); 00167 tqDebug("unicode start"); 00168 title = codec->toUnicode( TQCString( xmms->getPlaylistTitle( 0, pos ) ) ); 00169 tqDebug("unicode end"); 00170 if( title.isEmpty() ) 00171 title = "XMMS"; 00172 00173 tqDebug("Title: %s", title.ascii()); 00174 songLength = xmms->getPlaylistTime( 0, pos ); 00175 currentTime = xmms->getOutputTime( 0 ); 00176 } 00177 #endif // HAVE_XMMS 00178 00179 while (it != 0) 00180 { 00181 sp = (SensorParams*)(*it); 00182 meter = sp->getMeter(); 00183 00184 #ifdef HAVE_XMMS 00185 00186 if( isRunning ) 00187 { 00188 00189 format = sp->getParam("FORMAT"); 00190 00191 00192 if (format.length() == 0 ) 00193 { 00194 format = "%title %time / %length"; 00195 } 00196 00197 if( format == "%ms" ) 00198 { 00199 meter->setMax( songLength ); 00200 meter->setValue( currentTime ); 00201 } 00202 else 00203 00204 if ( format == "%full" ) 00205 { 00206 meter->setValue( 1 ); 00207 } 00208 else 00209 00210 { 00211 00212 00213 format.replace( TQRegExp("%title", false), title ); 00214 00215 format.replace( TQRegExp("%length", false), TQTime( 0,0,0 ). 00216 addMSecs( songLength ) 00217 .toString( "h:mm:ss" ) ); 00218 00219 format.replace( TQRegExp("%time", false), TQTime( 0,0,0 ). 00220 addMSecs( currentTime ) 00221 .toString( "h:mm:ss" ) ); 00222 00223 if( isPlaying ) 00224 { 00225 format.replace( TQRegExp("%remain", false), TQTime( 0,0,0 ). 00226 addMSecs( songLength ) 00227 .addMSecs(-currentTime ) 00228 .toString( "h:mm:ss" ) ); 00229 } 00230 00231 else 00232 { 00233 format.replace( TQRegExp("%remain", false), TQTime( 0,0,0 ).toString("h:mm:ss" ) ); 00234 } 00235 meter->setValue(format); 00236 } 00237 } 00238 else 00239 #endif // HAVE_XMMS 00240 00241 { 00242 meter->setValue(""); 00243 } 00244 ++it; 00245 00246 } 00247 00248 } 00249 00250 void XMMSSensor::setMaxValue( SensorParams *sp) 00251 { 00252 Meter *meter; 00253 meter = sp->getMeter(); 00254 TQString f; 00255 f = sp->getParam("FORMAT"); 00256 00257 if ( f == "%full" ) 00258 meter->setMax( 1 ); 00259 00260 } 00261 00262 bool XMMSSensor::hasXMMS() const 00263 { 00264 return xmms->isInitialized(); 00265 } 00266 00267 #include "xmmssensor.moc"