Initial help window support, show sudo errors.
[qcpufreq] / src / mainwindow.cpp
index 5e69f2b..dcc6f7a 100755 (executable)
 #include <QFile>
 #include <QMessageBox>
 #include <QTextStream>
-#include <QProcess>
 #include <QDesktopWidget>
+#if defined(Q_WS_MAEMO_5)
+    #include <QMaemo5InformationBox>
+#endif
 
 
 #define APPNAME "QCPUFreq"
-#define APPVERSION "0.1"
+#define APPVERSION "0.2"
 
 MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
+    //this is a stacked window on Maemo 5
+    #if defined(Q_WS_MAEMO_5)
+       setAttribute(Qt::WA_Maemo5StackedWindow);
+    #endif
+
     ui->setupUi(this);
+
     refresh();
 
     // enable auto rotation
-    setAutoRotaion();
+    setAutoRotation();
 
     //create a QGraphicsScene for the little chip icon
     scene = new QGraphicsScene();
     orientationChanged();
 
+    //create the refresh timer
+    refreshTimer = new QTimer();
+    //refresh UI every 10 seconds
+    refreshTimer->start( 10000 );
+
+    //create helper process
+    helperProcess = new QProcess;
+
+    //create a new, stackable help window
+    helpWindow = new HelpWindow( this );
+    #if defined(Q_WS_MAEMO_5)
+       helpWindow->setAttribute(Qt::WA_Maemo5StackedWindow);
+    #endif
+    helpWindow->setWindowFlags( windowFlags() | Qt::Window );
+
+    //show errors about the sudo setup only once
+    showSudoError = true;
+
     //connect signals and slots
+    connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(showHelp()));
     connect( ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()) );
     connect( ui->freq_adjust, SIGNAL(valueChanged(int)), this, SLOT(adjustFreq()) );
     connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(orientationChanged()));
-
+    connect(ui->sr_box, SIGNAL(clicked()), this, SLOT(setSmartReflex()));
+    connect(refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
 }
 
 MainWindow::~MainWindow()
 {
-    delete ui;
+    delete helpWindow;
+    delete refreshTimer;
     delete scene;
+    delete ui;
 }
 
 
@@ -63,6 +93,7 @@ MainWindow::~MainWindow()
 void MainWindow::about()
 {
     QMessageBox::about(this, APPNAME " " APPVERSION, "<p style=\"align:center;\">&copy; 2010 Daniel Klaffenbach</p>" );
+    refresh();
 }
 
 
@@ -74,24 +105,59 @@ void MainWindow::adjustFreq()
     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
     QString max;
     max.setNum( newmax );
-    QStringList arguments;
-#if defined(Q_WS_MAEMO_5)
-    //on Maemo5 the set_scalingmaxfreq-Script is not in $PATH
-    arguments.append( "/opt/usr/bin/set_scalingmaxfreq" );
-#else
-    arguments.append( "set_scalingmaxfreq" );
-#endif
-    arguments.append( max );
 
-    //execute the scaling script
-    QProcess script;
-    script.execute( "sudo", arguments );
+    callHelper( "set_maxfreq", max );
 
     refresh();
 }
 
 
 /**
+  * Calls the QCPUFreq helper script with "sudo action param"
+  *
+  * @param  action : the action of the helper script
+  * @param  param : the parameter for the action
+  * @return exit code
+  */
+int MainWindow::callHelper(QString action, QString param)
+{
+    QStringList arguments;
+
+    #if defined(Q_WS_MAEMO_5)
+       //On Maemo 5 the helper script resides in /opt/usr/bin, which us usually not in $PATH
+       arguments.append( "/opt/usr/bin/QCPUFreq.helper" );
+    #else
+       arguments.append( "QCPUFreq.helper" );
+    #endif
+
+    arguments.append( action );
+    arguments.append( param );
+
+    helperProcess->start( "sudo", arguments, QIODevice::NotOpen );
+
+    if ( showSudoError && !helperProcess->waitForFinished( 200 )) {
+       //do not show this error again
+       showSudoError = false;
+       QMessageBox::critical(this, tr("QCPUFreq"), tr("There seems to be a problem with your sudo setup!"));
+    }
+
+    return helperProcess->exitCode();
+}
+
+
+/**
+  * Returns the current CPU temperature
+  */
+QString MainWindow::getCPUTemp()
+{
+#if defined(Q_WS_MAEMO_5)
+    return readSysFile( "devices/platform/omap34xx_temp/temp1_input_raw" );
+#endif
+    return tr( "Unknown" );
+}
+
+
+/**
   * Returns the maximum CPU frequency
   */
 int MainWindow::getMaxFreq()
@@ -129,11 +195,17 @@ int MainWindow::getScalingFreq(int step)
 }
 
 
+/**
+  * Returns the name of the current CPU frequency scaling governor
+  *
+  * \return    name of governor
+  */
 QString MainWindow::getScalingGovernor()
 {
     return readSysFile( "devices/system/cpu/cpu0/cpufreq/scaling_governor" );
 }
 
+
 /**
   * Returns the amount of available scaling steps.
   */
@@ -158,14 +230,45 @@ int MainWindow::getScalingStep( int freq )
     return 1;
 }
 
+
+/**
+  * Returns the SmartReflex(tm) state
+  *
+  * \return    0|1
+  */
+int MainWindow::getSmartReflexState()
+{
+//SmartReflex is only supprted on Maemo5
+#if defined(Q_WS_MAEMO_5)
+    QString tmp = readSysFile( "power/sr_vdd1_autocomp" );
+
+    if ( tmp == "1" )
+       return 1;
+    else
+       return 0;
+#else
+    //disable UI checkbox
+    ui->sr_box->setDisabled( true );
+
+    return 0;
+#endif
+}
+
+
+/**
+  * Reads any file in /sys/
+  *
+  * \param     sys_file : full path to sys file - omit "/sys/"
+  * \return    content of sys file
+  */
 QString MainWindow::readSysFile(QString sys_file)
 {
     QFile file( "/sys/"+sys_file );
 
     //open the file
     if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) {
-        QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
-        return "";
+       QMessageBox::critical(this, tr("QCPUFreq"), tr("Could not get information from /sys!"));
+       return "";
     }
 
     //read the file
@@ -197,12 +300,23 @@ void MainWindow::refresh()
     //display the current governor
     ui->freq_governor->setText( getScalingGovernor() );
 
-    //display.setNum( getScalingFreq(1) );
-    //ui->freq_max->setText( display );
+    //display current temperature
+    ui->cpu_temp->setText( getCPUTemp() );
+
+    //smart reflex button
+    if ( getSmartReflexState() == 1 )
+       ui->sr_box->setCheckState( Qt::Checked );
+    else
+       ui->sr_box->setCheckState( Qt::Unchecked );
+
+
+    //display frequency slider
     ui->freq_adjust->setMinimum( 1 );
     ui->freq_adjust->setMaximum( getScalingSteps() );
     ui->freq_adjust->setInvertedAppearance( true );
     ui->freq_adjust->setSliderPosition( getScalingStep(getMaxFreq()) );
+
+    //ui->retranslateUi(this);
 }
 
 
@@ -238,7 +352,7 @@ void MainWindow::orientationChanged()
 /**
   * Enables the auto-rotation feature of Maemo5 devices
   */
-void MainWindow::setAutoRotaion()
+void MainWindow::setAutoRotation()
 {
 #if defined(Q_WS_MAEMO_5)
     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
@@ -247,6 +361,35 @@ void MainWindow::setAutoRotaion()
 
 
 /**
+  * SLOT: Enables or disables Smart Reflex(tm) after pressing sr_btn
+  */
+void MainWindow::setSmartReflex()
+{
+//SmartReflex is only supported on Maemo5
+#if defined(Q_WS_MAEMO_5)
+    if ( getSmartReflexState() == 1 )
+       callHelper( "set_sr", "off");
+    else {
+       QMaemo5InformationBox::information(this, tr( "SmartReflex support is known to be unstable on some devices and may cause random reboots." ), QMaemo5InformationBox::DefaultTimeout);
+       callHelper( "set_sr", "on");
+    }
+
+#endif
+    //refresh the UI
+    refresh();
+}
+
+
+/**
+  * SLOT: display the help window
+  */
+void MainWindow::showHelp()
+{
+    helpWindow->show();
+}
+
+
+/**
   * Returns true when the device is in portrait mode
   */
 bool MainWindow::usePortrait()