Merged helper scripts
authorDaniel Klaffenbach <danielklaffenbach@gmail.com>
Fri, 25 Jun 2010 10:37:15 +0000 (12:37 +0200)
committerDaniel Klaffenbach <danielklaffenbach@gmail.com>
Fri, 25 Jun 2010 10:37:15 +0000 (12:37 +0200)
QCPUFreq now only uses one helper script which can be called by sudo
and contains all the relevant code for changing system settings.

The helper script can now be called using the callHelper() method.

debian/postinst
src/data/scripts/QCPUFreq.helper [new file with mode: 0755]
src/data/scripts/set_scalingmaxfreq [deleted file]
src/data/scripts/set_sr [deleted file]
src/data/sudoers/qcpufreq.sudoers
src/mainwindow.cpp
src/mainwindow.h
src/src.pro

index 5860e2a..1eaca11 100755 (executable)
@@ -8,13 +8,13 @@ set -e
 # update the sudoers file
 update-sudoers
 
-#HACK: make files executable
+#make files executable
 if [ -e /opt/usr/bin/QCPUFreq ]; then
        chmod a+x /opt/usr/bin/QCPUFreq
-       chmod a+x /opt/usr/bin/set_scalingmaxfreq
-       chmod a+x /opt/usr/bin/set_sr
 fi
-
+if [ -e /opt/usr/bin/QCPUFreq.helper ]; then
+       chmod a+x /opt/usr/bin/QCPUFreq.helper
+fi
 
 
 # dh_installdeb will replace this with shell code automatically
diff --git a/src/data/scripts/QCPUFreq.helper b/src/data/scripts/QCPUFreq.helper
new file mode 100755 (executable)
index 0000000..e5e98cb
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# This script is part of QCPUFreq
+# (c)2010 Daniel Klaffenbach
+
+if [[ -z $1 || -z $2 ]]; then
+       echo "$0 should only be called by QCPUFreq"
+       exit 1
+fi
+
+case "$1" in
+       "set_maxfreq")
+               #set scaling_max_freq
+               echo $2 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
+               exit 0
+               ;;
+       "set_sr")
+               #enable or disable SmartReflex
+               if [ "$2" == "on" ]; then
+                       echo 1 > /sys/power/sr_vdd1_autocomp
+                       echo 1 > /sys/power/sr_vdd2_autocomp
+               else
+                       echo 0 > /sys/power/sr_vdd1_autocomp
+                       echo 0 > /sys/power/sr_vdd2_autocomp
+
+               fi
+               exit 0
+               ;;
+       *)
+               echo "$0 should only be called by QCPUFreq"
+               exit 1
+               ;;
+esac
+
diff --git a/src/data/scripts/set_scalingmaxfreq b/src/data/scripts/set_scalingmaxfreq
deleted file mode 100755 (executable)
index 3bd645b..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh
-#
-# This script is part of QCPUFreq
-# (c)2010 Daniel Klaffenbach
-
-if [ -z $1 ]; then
-       echo "Usage: $0 maxfreq"
-       exit
-fi
-
-echo $1 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
diff --git a/src/data/scripts/set_sr b/src/data/scripts/set_sr
deleted file mode 100755 (executable)
index 55e9b7e..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/sh
-#
-# This script is part of QCPUFreq
-# (c)2010 Daniel Klaffenbach
-
-if [ -z $1 ]; then
-       echo "Usage: $0 on|off"
-       exit
-fi
-
-if [ $1 == 'on' ]; then
-       echo 1 > /sys/power/sr_vdd1_autocomp
-       echo 1 > /sys/power/sr_vdd2_autocomp
-else
-       echo 0 > /sys/power/sr_vdd1_autocomp
-       echo 0 > /sys/power/sr_vdd2_autocomp
-fi
index cd047d6..ec1b047 100755 (executable)
@@ -1,2 +1 @@
-user ALL = NOPASSWD: /opt/usr/bin/set_scalingmaxfreq
-user ALL = NOPASSWD: /opt/usr/bin/set_sr
+user ALL = NOPASSWD: /opt/usr/bin/QCPUFreq.helper
index 1ae4294..afffeef 100755 (executable)
@@ -80,23 +80,40 @@ void MainWindow::adjustFreq()
     int newmax = getScalingFreq( ui->freq_adjust->sliderPosition() );
     QString max;
     max.setNum( newmax );
+
+    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)
+{
+    QProcess helper;
     QStringList arguments;
     //run sudo in non-interactive mode
     arguments.append( "-n" );
 
-#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 );
+    #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
 
-    //execute the scaling script
-    QProcess script;
-    script.execute( "sudo", arguments );
+    arguments.append( action );
+    arguments.append( param );
 
-    refresh();
+    helper.execute( "sudo", arguments );
+
+    return helper.exitCode();
 }
 
 
@@ -320,24 +337,15 @@ void MainWindow::setAutoRotaion()
   */
 void MainWindow::setSmartReflex()
 {
-//SmartReflex is only supprted on Maemo5
+//SmartReflex is only supported on Maemo5
 #if defined(Q_WS_MAEMO_5)
-    QStringList arguments;
-    //run sudo in non-interactive mode
-    arguments.append( "-n" );
-    arguments.append( "/opt/usr/bin/set_sr" );
-
     if ( getSmartReflexState() == 1 )
-       arguments.append( "off" );
+       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);
-       arguments.append( "on" );
+       callHelper( "set_sr", "on");
     }
 
-    //execute the sr script script
-    QProcess script;
-    script.execute( "sudo", arguments );
-
 #endif
     //refresh the UI
     refresh();
index 448aa53..4b77db0 100755 (executable)
@@ -45,6 +45,7 @@ public slots:
 
 private:
     Ui::MainWindow *ui;
+    int callHelper( QString action, QString param );
     QString getCPUTemp();
     int getMaxFreq();
     int getMinFreq();
index bbfe174..555090f 100755 (executable)
@@ -41,7 +41,7 @@ unix {
 
        #MAKE INSTALL
 
-       INSTALLS += target helper sudoers desktop icon48 icon64 helper_sr locale_de
+       INSTALLS += target helper sudoers desktop icon48 icon64 locale_de
        target.path =$$BINDIR
 
        desktop.path = /usr/share/applications/hildon
@@ -54,13 +54,9 @@ unix {
        icon64.files += data/48x48/qcpufreq.png
 
        helper.path = $$BINDIR/
-       helper.files += data/scripts/set_scalingmaxfreq
+       helper.files += data/scripts/QCPUFreq.helper
        helper.permissions = 755
 
-       helper_sr.path = $$BINDIR/
-       helper_sr.files += data/scripts/set_sr
-       helper_sr.permissions = 755
-
        sudoers.path = /etc/sudoers.d/
        sudoers.files += data/sudoers/qcpufreq.sudoers