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
00027
00028
00029
00030
00031 #include "odevice_beagle.h"
00032 #include "odevice_ipaq.h"
00033 #include "odevice_mypal.h"
00034 #include "odevice_jornada.h"
00035 #include "odevice_ramses.h"
00036 #include "odevice_simpad.h"
00037 #include "odevice_yopy.h"
00038 #include "odevice_zaurus.h"
00039 #include "odevice_genuineintel.h"
00040
00041
00042 #include <qapplication.h>
00043 #include <qfile.h>
00044 #include <qtextstream.h>
00045 #include <qwindowsystem_qws.h>
00046
00047
00048 #include <qpe/config.h>
00049 #include <qpe/sound.h>
00050 #include <qpe/qcopenvelope_qws.h>
00051 #include <qpe/sound.h>
00052
00053 #include <opie2/okeyfilter.h>
00054 #include <opie2/oresource.h>
00055
00056
00057 #include <fcntl.h>
00058 #include <math.h>
00059 #include <stdlib.h>
00060 #include <signal.h>
00061 #include <sys/ioctl.h>
00062 #include <sys/time.h>
00063 #include <unistd.h>
00064 #ifndef QT_NO_SOUND
00065 #include <linux/soundcard.h>
00066 #endif
00067
00068 namespace Opie {
00069 namespace Core {
00070
00071 static const char* PATH_PROC_CPUINFO = "/proc/cpuinfo";
00072
00073
00074
00075 ODistribution distributions[] = {
00076 { System_Familiar, "FamiliarLinux", "/etc/familiar-version" },
00077 { System_OpenZaurus, "OpenZaurus", "/etc/oz_version" },
00078 { System_OpenEmbedded, "OpenEmbedded", "/etc/oe-version" },
00079 { System_Unknown, "Linux", "/etc/issue" },
00080 };
00081
00082
00083 bool isQWS(){
00084 return qApp ? ( qApp->type() == QApplication::GuiServer ) : false;
00085 }
00086
00087 QCString makeChannel ( const char *str ){
00088 if ( str && !::strchr ( str, '/' ))
00089 return QCString ( "QPE/Application/" ) + str;
00090 else
00091 return str;
00092 }
00093
00094
00095
00096
00097
00098 struct default_button default_buttons [] = {
00099 { Qt::Key_F9, QT_TRANSLATE_NOOP("Button", "Calendar Button"),
00100 "devicebuttons/z_calendar",
00101 "datebook", "nextView()",
00102 "today", "raise()" },
00103 { Qt::Key_F10, QT_TRANSLATE_NOOP("Button", "Contacts Button"),
00104 "devicebuttons/z_contact",
00105 "addressbook", "raise()",
00106 "addressbook", "beamBusinessCard()" },
00107 { Qt::Key_F12, QT_TRANSLATE_NOOP("Button", "Home Button"),
00108 "devicebuttons/z_home",
00109 "QPE/Launcher", "home()",
00110 "buttonsettings", "raise()" },
00111 { Qt::Key_F11, QT_TRANSLATE_NOOP("Button", "Menu Button"),
00112 "devicebuttons/z_menu",
00113 "QPE/TaskBar", "toggleMenu()",
00114 "QPE/TaskBar", "toggleStartMenu()" },
00115 { Qt::Key_F13, QT_TRANSLATE_NOOP("Button", "Mail Button"),
00116 "devicebuttons/z_mail",
00117 "opiemail", "raise()",
00118 "opiemail", "newMail()" },
00119 };
00120
00121 ODevice *ODevice::inst()
00122 {
00123 static ODevice *dev = 0;
00124 QString cpu_info;
00125
00126 if ( !dev )
00127 {
00128 QFile f( PATH_PROC_CPUINFO );
00129 if ( f.open( IO_ReadOnly ) )
00130 {
00131 QTextStream s( &f );
00132 while ( !s.atEnd() )
00133 {
00134 QString line;
00135 line = s.readLine();
00136 if ( line.startsWith( "Hardware" ) )
00137 {
00138 qDebug( "ODevice() - found '%s'", (const char*) line );
00139 cpu_info = line;
00140 if ( line.contains( "sharp", false ) ) dev = new Internal::Zaurus();
00141 else if ( line.contains( "ipaq", false ) ) dev = new Internal::iPAQ();
00142 else if ( line.contains( "mypal", false ) ) dev = new Internal::MyPal();
00143 else if ( line.contains( "simpad", false ) ) dev = new Internal::SIMpad();
00144 else if ( line.contains( "jornada", false ) ) dev = new Internal::Jornada();
00145 else if ( line.contains( "ramses", false ) ) dev = new Internal::Ramses();
00146 else if ( line.contains( "Tradesquare.NL", false ) ) dev = new Internal::Beagle();
00147 else qWarning( "ODevice() - unknown hardware - using default." );
00148 break;
00149 } else if ( line.startsWith( "vendor_id" ) ) {
00150 qDebug( "ODevice() - found '%s'", (const char*) line );
00151 cpu_info = line;
00152 if( line.contains( "genuineintel", false ) ) {
00153 dev = new Internal::GenuineIntel();
00154 break;
00155 }
00156 }
00157 }
00158 }
00159 else
00160 {
00161 qWarning( "ODevice() - can't open '%s' - unknown hardware - using default.", PATH_PROC_CPUINFO );
00162 }
00163 if ( !dev ) dev = new ODevice();
00164 dev->init(cpu_info);
00165 }
00166 return dev;
00167 }
00168
00169 ODevice::ODevice()
00170 {
00171 d = new ODeviceData;
00172
00173 d->m_modelstr = "Unknown";
00174 d->m_model = Model_Unknown;
00175 d->m_vendorstr = "Unknown";
00176 d->m_vendor = Vendor_Unknown;
00177 d->m_systemstr = "Unknown";
00178 d->m_system = System_Unknown;
00179 d->m_sysverstr = "0.0";
00180 d->m_rotation = Rot0;
00181 d->m_direction = CW;
00182 d->m_qteDriver = "Transformed";
00183
00184 d->m_holdtime = 1000;
00185 d->m_buttons = 0;
00186 d->m_cpu_frequencies = new QStrList;
00187
00188
00189
00190 d->m_sound = d->m_vol = d->m_mixer = -1;
00191
00192
00193 d->m_initializedButtonQcop = false;
00194
00195
00196
00197
00198
00199 for ( unsigned int i = 0; i < sizeof(distributions)/sizeof(ODistribution); ++i )
00200 {
00201 if ( QFile::exists( distributions[i].sysvfile ) )
00202 {
00203 d->m_systemstr = distributions[i].sysstr;
00204 d->m_system = distributions[i].system;
00205 d->m_sysverstr = "<Unknown>";
00206 QFile f( distributions[i].sysvfile );
00207 if ( f.open( IO_ReadOnly ) )
00208 {
00209 QTextStream ts( &f );
00210 d->m_sysverstr = ts.readLine().replace( QRegExp( "\\\\." ), "" );
00211 }
00212 break;
00213 }
00214 }
00215 }
00216
00217 void ODevice::systemMessage( const QCString &msg, const QByteArray & )
00218 {
00219 if ( msg == "deviceButtonMappingChanged()" ) {
00220 reloadButtonMapping();
00221 }
00222 }
00223
00224 void ODevice::init(const QString&)
00225 {
00226 }
00227
00231 void ODevice::initButtons()
00232 {
00233 if ( d->m_buttons )
00234 return;
00235
00236 qDebug ( "init Buttons" );
00237 d->m_buttons = new QValueList <ODeviceButton>;
00238 for ( uint i = 0; i < ( sizeof( default_buttons ) / sizeof( default_button )); i++ ) {
00239 default_button *db = default_buttons + i;
00240 ODeviceButton b;
00241 b. setKeycode ( db->code );
00242 b. setUserText ( QObject::tr ( "Button", db->utext ));
00243 b. setPixmap ( OResource::loadPixmap ( db->pix ));
00244 b. setFactoryPresetPressedAction ( OQCopMessage ( makeChannel ( db->fpressedservice ), db->fpressedaction ));
00245 b. setFactoryPresetHeldAction ( OQCopMessage ( makeChannel ( db->fheldservice ), db->fheldaction ));
00246 d->m_buttons->append ( b );
00247 }
00248
00249 reloadButtonMapping();
00250 }
00251
00252 ODevice::~ODevice()
00253 {
00254
00255
00256
00257 delete d;
00258 }
00259
00271 bool ODevice::suspend()
00272 {
00273 return false;
00274 }
00275
00279 bool ODevice::setDisplayStatus( bool on )
00280 {
00281 qDebug( "ODevice::setDisplayStatus( %d ) - please override me.", on );
00282 return false;
00283 }
00284
00291 bool ODevice::setDisplayBrightness( int b )
00292 {
00293 qDebug( "ODevice::setDisplayBrightness( %d ) - please override me.", b );
00294 return false;
00295 }
00296
00306 int ODevice::displayBrightnessResolution() const
00307 {
00308 qDebug( "ODevice::displayBrightnessResolution() - please override me." );
00309 return 16;
00310 }
00311
00317 bool ODevice::setDisplayContrast( int p )
00318 {
00319 qDebug( "ODevice::setDisplayContrast( %d ) - please override me.", p );
00320 return false;
00321 }
00322
00327 int ODevice::displayContrastResolution() const
00328 {
00329 qDebug( "ODevice::displayBrightnessResolution() - please override me." );
00330 return 0;
00331 }
00332
00337 QString ODevice::vendorString() const
00338 {
00339 return d->m_vendorstr;
00340 }
00341
00346 OVendor ODevice::vendor() const
00347 {
00348 return d->m_vendor;
00349 }
00350
00355 QString ODevice::modelString() const
00356 {
00357 return d->m_modelstr;
00358 }
00359
00363 OModel ODevice::model() const
00364 {
00365 return d->m_model;
00366 }
00367
00371 QString ODevice::systemString() const
00372 {
00373 return d->m_systemstr;
00374 }
00375
00379 OSystem ODevice::system() const
00380 {
00381 return d->m_system;
00382 }
00383
00387 QString ODevice::systemVersionString() const
00388 {
00389 return d->m_sysverstr;
00390 }
00391
00395 Transformation ODevice::rotation() const
00396 {
00397 return d->m_rotation;
00398 }
00399
00403 ODirection ODevice::direction() const
00404 {
00405 return d->m_direction;
00406 }
00407
00408 QString ODevice::qteDriver() const
00409 {
00410 return d->m_qteDriver;
00411 }
00412
00416 void ODevice::playAlarmSound()
00417 {
00418 #ifndef QT_NO_SOUND
00419 static Sound snd ( "alarm" );
00420
00421 if ( snd. isFinished())
00422 snd. play();
00423 #endif
00424 }
00425
00429 void ODevice::playKeySound()
00430 {
00431 #ifndef QT_NO_SOUND
00432 static Sound snd ( "keysound" );
00433
00434 if ( snd. isFinished())
00435 snd. play();
00436 #endif
00437 }
00438
00442 void ODevice::playTouchSound()
00443 {
00444 #ifndef QT_NO_SOUND
00445 static Sound snd ( "touchsound" );
00446
00447 if ( snd. isFinished())
00448 snd. play();
00449 #endif
00450 }
00451
00457 QValueList <OLed> ODevice::ledList() const
00458 {
00459 return QValueList <OLed>();
00460 }
00461
00465 QValueList <OLedState> ODevice::ledStateList ( OLed ) const
00466 {
00467 return QValueList <OLedState>();
00468 }
00469
00473 OLedState ODevice::ledState ( OLed ) const
00474 {
00475 return Led_Off;
00476 }
00477
00484 bool ODevice::setLedState ( OLed which, OLedState st )
00485 {
00486 Q_UNUSED( which )
00487 Q_UNUSED( st )
00488 return false;
00489 }
00490
00494 bool ODevice::hasLightSensor() const
00495 {
00496 return false;
00497 }
00498
00502 int ODevice::readLightSensor()
00503 {
00504 return -1;
00505 }
00506
00510 int ODevice::lightSensorResolution() const
00511 {
00512 return 0;
00513 }
00514
00518 bool ODevice::hasHingeSensor() const
00519 {
00520 return false;
00521 }
00522
00526 OHingeStatus ODevice::readHingeSensor()const
00527 {
00528 return CASE_UNKNOWN;
00529 }
00530
00534 const QStrList &ODevice::allowedCpuFrequencies() const
00535 {
00536 return *d->m_cpu_frequencies;
00537 }
00538
00539
00545 bool ODevice::setCurrentCpuFrequency(uint index)
00546 {
00547 if (index >= d->m_cpu_frequencies->count())
00548 return false;
00549
00550 char *freq = d->m_cpu_frequencies->at(index);
00551 qWarning("set freq to %s", freq);
00552
00553 int fd;
00554
00555 if ((fd = ::open("/proc/sys/cpu/0/speed", O_WRONLY)) >= 0) {
00556 char writeCommand[50];
00557 const int count = sprintf(writeCommand, "%s\n", freq);
00558 int res = (::write(fd, writeCommand, count) != -1);
00559 ::close(fd);
00560 return res;
00561 }
00562
00563 return false;
00564 }
00565
00566
00570 const QValueList <ODeviceButton> &ODevice::buttons()
00571 {
00572 initButtons();
00573
00574 return *d->m_buttons;
00575 }
00576
00580 uint ODevice::buttonHoldTime() const
00581 {
00582 return d->m_holdtime;
00583 }
00584
00592 const ODeviceButton *ODevice::buttonForKeycode ( ushort code )
00593 {
00594 initButtons();
00595
00596 for ( QValueListConstIterator<ODeviceButton> it = d->m_buttons->begin(); it != d->m_buttons->end(); ++it ) {
00597 if ( (*it).keycode() == code )
00598 return &(*it);
00599 }
00600 return 0;
00601 }
00602
00603 void ODevice::reloadButtonMapping()
00604 {
00605 if(!d->m_buttons)
00606 initButtons();
00607
00608 if(!d->m_initializedButtonQcop) {
00609 QCopChannel *chan = new QCopChannel("QPE/System", this, "ODevice button channel");
00610 connect(chan,SIGNAL(received(const QCString&,const QByteArray&)),
00611 this,SLOT(systemMessage(const QCString&,const QByteArray&)));
00612 d->m_initializedButtonQcop = true;
00613 }
00614
00615 Config cfg ( "ButtonSettings" );
00616
00617 for ( uint i = 0; i < d->m_buttons->count(); i++ ) {
00618 ODeviceButton &b = ( *d->m_buttons ) [i];
00619 QString group = "Button" + QString::number ( i );
00620
00621 QCString pch, hch;
00622 QCString pm, hm;
00623 QByteArray pdata, hdata;
00624
00625 if ( cfg. hasGroup ( group )) {
00626 cfg. setGroup ( group );
00627 pch = cfg. readEntry ( "PressedActionChannel" ). latin1();
00628 pm = cfg. readEntry ( "PressedActionMessage" ). latin1();
00629
00630
00631 hch = cfg. readEntry ( "HeldActionChannel" ). latin1();
00632 hm = cfg. readEntry ( "HeldActionMessage" ). latin1();
00633
00634 }
00635
00636 b. setPressedAction ( OQCopMessage ( pch, pm, pdata ));
00637 b. setHeldAction ( OQCopMessage ( hch, hm, hdata ));
00638 }
00639 }
00640
00641 void ODevice::remapPressedAction ( int button, const OQCopMessage &action )
00642 {
00643 initButtons();
00644
00645 QString mb_chan;
00646
00647 if ( button >= (int) d->m_buttons->count())
00648 return;
00649
00650 ODeviceButton &b = ( *d->m_buttons ) [button];
00651 b. setPressedAction ( action );
00652
00653 mb_chan=b. pressedAction(). channel();
00654
00655 Config buttonFile ( "ButtonSettings" );
00656 buttonFile. setGroup ( "Button" + QString::number ( button ));
00657 buttonFile. writeEntry ( "PressedActionChannel", (const char*) mb_chan);
00658 buttonFile. writeEntry ( "PressedActionMessage", (const char*) b. pressedAction(). message());
00659
00660
00661
00662 QCopEnvelope ( "QPE/System", "deviceButtonMappingChanged()" );
00663 }
00664
00665 void ODevice::remapHeldAction ( int button, const OQCopMessage &action )
00666 {
00667 initButtons();
00668
00669 if ( button >= (int) d->m_buttons->count())
00670 return;
00671
00672 ODeviceButton &b = ( *d->m_buttons ) [button];
00673 b. setHeldAction ( action );
00674
00675 Config buttonFile ( "ButtonSettings" );
00676 buttonFile. setGroup ( "Button" + QString::number ( button ));
00677 buttonFile. writeEntry ( "HeldActionChannel", (const char *) b. heldAction(). channel());
00678 buttonFile. writeEntry ( "HeldActionMessage", (const char *) b. heldAction(). message());
00679
00680
00681
00682 QCopEnvelope ( "QPE/System", "deviceButtonMappingChanged()" );
00683 }
00684
00688 void ODevice::virtual_hook(int, void* ){
00689
00690 }
00691
00703 void ODevice::sendSuspendmsg()
00704 {
00705 if ( !isQWS() )
00706 return;
00707
00708 QCopEnvelope ( "QPE/System", "aboutToSuspend()" );
00709 }
00710
00722 void ODevice::addPreHandler(QWSServer::KeyboardFilter*aFilter)
00723 {
00724 Opie::Core::OKeyFilter::inst()->addPreHandler(aFilter);
00725 }
00726
00736 void ODevice::remPreHandler(QWSServer::KeyboardFilter*aFilter)
00737 {
00738 Opie::Core::OKeyFilter::inst()->remPreHandler(aFilter);
00739 }
00740
00741
00747 void ODevice::playingStopped() {
00748 if ( sender() )
00749 const_cast<QObject*>(sender())->disconnect( this );
00750
00751 #ifndef QT_NO_SOUND
00752 if ( d->m_sound >= 0 ) {
00753 ::ioctl ( d->m_sound, MIXER_WRITE( d->m_mixer ), &d->m_vol );
00754 ::close ( d->m_sound );
00755 }
00756 #endif
00757 }
00758
00759
00794 void ODevice::changeMixerForAlarm( int mixer, const char* file, Sound *snd ) {
00795 #ifndef QT_NO_SOUND
00796 if (( d->m_sound = ::open ( file, O_RDWR )) >= 0 ) {
00797 if ( ::ioctl ( d->m_sound, MIXER_READ( mixer ), &d->m_vol ) >= 0 ) {
00798 Config cfg ( "qpe" );
00799 cfg. setGroup ( "Volume" );
00800
00801 int volalarm = cfg. readNumEntry ( "AlarmPercent", 50 );
00802 if ( volalarm < 0 )
00803 volalarm = 0;
00804 else if ( volalarm > 100 )
00805 volalarm = 100;
00806 volalarm |= ( volalarm << 8 );
00807
00808 if ( ::ioctl ( d->m_sound, MIXER_WRITE( mixer ), &volalarm ) >= 0 )
00809 register_qpe_sound_finished(snd, this, SLOT(playingStopped()));
00810 }
00811 d->m_mixer = mixer;
00812 }
00813 #endif
00814 }
00815
00816 }
00817 }