kmilo
pb_monitor.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "pb_monitor.h"
00027
00028 #include <kgenericfactory.h>
00029 #include <kdebug.h>
00030
00031 #include <sys/types.h>
00032 #include <unistd.h>
00033
00034 extern "C" {
00035
00036
00037 #undef template
00038 #include <pbb.h>
00039
00040
00041
00042 #ifndef TAG_LCDBRIGHTNESS
00043 #define TAG_LCDBRIGHTNESS TAG_BRIGHTNESS
00044 #endif
00045 }
00046
00047 #define BUFFERLEN 200
00048 const QString tpmodes[] = { I18N_NOOP("No Tap"), I18N_NOOP("Tap"), I18N_NOOP("Drag"), I18N_NOOP("Lock") };
00049
00050 namespace KMilo {
00051
00052 PowerBookMonitor::PowerBookMonitor(QObject *parent, const char *name,
00053 const QStringList& args)
00054 : Monitor(parent, name, args),
00055 m_progress( 0 )
00056 {
00057 init_libpbb();
00058 }
00059
00060 PowerBookMonitor::~PowerBookMonitor()
00061 {
00062 ipc_exit();
00063 }
00064
00065
00066 bool PowerBookMonitor::init()
00067 {
00068
00069 #ifdef CLIENT_REGISTER
00070 return ( ipc_init( "kmilo", LIBMODE_CLIENT, CLIENT_REGISTER ) == 0 );
00071 #else
00072 return ( ipc_init( LIBMODE_CLIENT, 1 ) == 0 );
00073 #endif
00074 }
00075
00076
00077 Monitor::DisplayType PowerBookMonitor::poll()
00078 {
00079 Monitor::DisplayType rc = None;
00080
00081 struct tagitem* tag = readMessage();
00082 m_message = QString();
00083 while ( tag && tag->tag != TAG_END ) {
00084 switch ( tag->tag ) {
00085 case TAG_VOLUME:
00086 rc = Monitor::Volume;
00087 m_progress = (int)tag->data;
00088 break;
00089 case TAG_MUTE:
00090 rc = Monitor::Mute;
00091 m_progress = (int)tag->data;
00092 break;
00093 case TAG_LCDBRIGHTNESS:
00094 rc = Monitor::Brightness;
00095 m_progress = ((int)tag->data)*100/15;
00096 break;
00097 case TAG_TPMODE:
00098 {
00099 rc = Monitor::Tap;
00100 QString marg = tpmodes[ tag->data & 3 ];
00101 m_message = i18n( "Operating mode set to: %1.", marg );
00102 }
00103 break;
00104 default:
00105 break;
00106 }
00107 ++tag;
00108 }
00109
00110 if ( m_sleep ) {
00111 rc = Monitor::Sleep;
00112 }
00113
00114 return rc;
00115 }
00116
00117
00118 int PowerBookMonitor::progress() const
00119 {
00120 return m_progress;
00121 }
00122
00123 QString PowerBookMonitor::message() const
00124 {
00125 return m_message;
00126 }
00127
00128 struct tagitem* PowerBookMonitor::readMessage()
00129 {
00130 char buffer[BUFFERLEN];
00131 m_sleep = false;
00132 if ( (ipc_receive(buffer, BUFFERLEN)) >=0 ) {
00133 if ( buffer ) {
00134 struct pbbmessage *msg = reinterpret_cast<struct pbbmessage*>( buffer );
00135 switch ( msg->action ) {
00136 case REGFAILED:
00137 kDebug()<<"PBB registration failed";
00138 break;
00139 case CLIENTEXIT:
00140 kDebug()<<"PBB client exited";
00141 break;
00142 case CHANGEVALUE:
00143 return msg->taglist;
00144 break;
00145 case WARNING:
00146 if ( msg->taglist->data == 0 ) {
00147 m_message = i18n( "The computer will sleep now." );
00148 } else {
00149 m_message = i18np( "The computer will sleep in %1 second.",
00150 "The computer will sleep in %1 seconds.",
00151 msg->taglist->data );
00152 }
00153 m_sleep = true;
00154 kDebug()<<"PBB Warning";
00155 break;
00156 }
00157 }
00158 }
00159 return 0;
00160 }
00161
00162 }
00163
00164 K_EXPORT_COMPONENT_FACTORY(kmilo_powerbook, KGenericFactory<KMilo::PowerBookMonitor>("kmilo_powerbook"))
00165