[PATCH] Common backlight for kernel 2.6

Slavek Banko <[email protected]>
Newsgroups gmane.comp.handhelds.opie.devel
Organization axis, spol. s r. o.
Message-ID <[email protected]>
Hi.

Here is first version of patch, that prepare common code for backlight on 
devices with kernels 2.6. I don't know, that code belongs to odevice.cpp 
and that to odevice_abstractmobiledevice.cpp. I put common code to 
odevice.cpp. In odevice_ipaq.cpp is corresponding code removed and common 
methods is used. Method iPAQ::setDisplayStatus will be completely 
removed. Some cases from iPAQ::displayBrightnessResolution can be too 
removed (which models use kernel 2.6?).

If common code will be accepted, code for other devices than iPAQs can be 
modified to use this common code.

I expect suggestions and comments.

Slavek

_______________________________________________

http://opie.handhelds.org/cgi-bin/moin.cgi/DeveloperWikiIndex

Opie-devel mailing list
[email protected]
https://handhelds.org/mailman/listinfo/opie-devel
backlight-ipaq.patch (text/x-diff, 6.3 KB)
diff -aruN libopie2.orig/opie/libopie2/opiecore/device/odevice.cpp libopie2/opie/libopie2/opiecore/device/odevice.cpp
--- libopie2.orig/opie/libopie2/opiecore/device/odevice.cpp	2005-08-10 21:43:26.000000000 +0200
+++ libopie2/opie/libopie2/opiecore/device/odevice.cpp	2006-06-06 01:13:45.000000000 +0200
@@ -40,6 +40,7 @@
 
 /* QT */
 #include <qapplication.h>
+#include <qdir.h>
 #include <qfile.h>
 #include <qtextstream.h>
 #include <qwindowsystem_qws.h>
@@ -278,7 +279,22 @@
 */
 bool ODevice::setDisplayStatus( bool on )
 {
-    qDebug( "ODevice::setDisplayStatus( %d ) - please override me.", on );
+    int res = false;
+    QDir sysClass( "/sys/class/lcd/" );
+    sysClass.setFilter(QDir::Dirs);
+    if ( sysClass.exists() && sysClass.count() > 2 ) {
+	QString sysClassPath = sysClass.absFilePath( sysClass[2] + "/power" );
+        int fd = ::open( sysClassPath, O_WRONLY|O_NONBLOCK );
+        if ( fd ) {
+            char buf[10];
+            buf[0] = on ? 0 : 4;
+            buf[1] = '\0';
+            res = ( ::write( fd, &buf[0], 2 ) == 0 );
+            ::close( fd );
+        }
+    } else {
+        qDebug( "ODevice::setDisplayStatus( %d ) - please override me.", on );
+    }
     return false; // don't do anything for unknown models
 }
 
@@ -290,8 +306,23 @@
 */
 bool ODevice::setDisplayBrightness( int b )
 {
-    qDebug( "ODevice::setDisplayBrightness( %d ) - please override me.", b );
-    return false;
+    int res = false;
+    QDir sysClass( "/sys/class/backlight/" );
+    sysClass.setFilter(QDir::Dirs);
+    if ( sysClass.exists() && sysClass.count() > 2 ) {
+	QString sysClassPath = sysClass.absFilePath( sysClass[2] + "/brightness" );
+        int fd = ::open( sysClassPath, O_WRONLY|O_NONBLOCK );
+        if ( fd ) {
+            char buf[100];
+            int val = b * displayBrightnessResolution() / 255;
+            int len = ::snprintf( &buf[0], sizeof buf, "%d", val );
+            res = ( ::write( fd, &buf[0], len ) == 0 );
+            ::close( fd );
+        }
+    } else {
+	qDebug( "ODevice::setDisplayBrightness( %d ) - please override me.", b );
+    }
+    return res;
 }
 
 /**
@@ -305,8 +336,22 @@
  */
 int ODevice::displayBrightnessResolution() const
 {
-    qDebug( "ODevice::displayBrightnessResolution() - please override me." );
-    return 16;
+    int res = 16;
+    QDir sysClass( "/sys/class/backlight/" );
+    sysClass.setFilter(QDir::Dirs);
+    if ( sysClass.exists() && sysClass.count() > 2 ) {
+	QString sysClassPath = sysClass.absFilePath( sysClass[2] + "/max_brightness" );
+        int fd = ::open( sysClassPath, O_RDONLY|O_NONBLOCK );
+        if ( fd ) {
+            char buf[100];
+            if ( ::read( fd, &buf[0], sizeof buf ) )
+                ::sscanf( &buf[0], "%d", &res );
+            ::close( fd );
+        }
+    } else {
+        qDebug( "ODevice::displayBrightnessResolution() - please override me." );
+    }
+    return res;
 }
 
 /**
diff -aruN libopie2.orig/opie/libopie2/opiecore/device/odevice_abstractmobiledevice.cpp libopie2/opie/libopie2/opiecore/device/odevice_abstractmobiledevice.cpp
--- libopie2.orig/opie/libopie2/opiecore/device/odevice_abstractmobiledevice.cpp	2005-08-28 00:40:41.000000000 +0200
+++ libopie2/opie/libopie2/opiecore/device/odevice_abstractmobiledevice.cpp	2006-06-06 01:16:13.000000000 +0200
@@ -31,6 +31,7 @@
 
 /* QT */
 #include <qcopchannel_qws.h>
+#include <qdir.h>
 
 /* STD */
 #include <sys/time.h>
@@ -105,6 +106,11 @@
     bool res = false;
     int fd;
 
+    QDir sysClass( "/sys/class" );
+    if ( sysClass.exists() ) {
+        return ODevice::setDisplayStatus ( on );
+    }
+
 #ifdef QT_QWS_DEVFS
     if (( fd = ::open ( "/dev/fb/0", O_RDWR )) >= 0 ) {
 #else
diff -aruN libopie2.orig/opie/libopie2/opiecore/device/odevice_ipaq.cpp libopie2/opie/libopie2/opiecore/device/odevice_ipaq.cpp
--- libopie2.orig/opie/libopie2/opiecore/device/odevice_ipaq.cpp	2006-05-27 19:29:31.000000000 +0200
+++ libopie2/opie/libopie2/opiecore/device/odevice_ipaq.cpp	2006-06-06 01:12:09.000000000 +0200
@@ -31,6 +31,7 @@
 
 /* QT */
 #include <qapplication.h>
+#include <qdir.h>
 #include <qfile.h>
 #include <qtextstream.h>
 #include <qwindowsystem_qws.h>
@@ -375,26 +376,10 @@
     if ( bright < 0 )
         bright = 0;
 
-    QString cmdline;
-
-    switch ( model()) {
-    case Model_iPAQ_H191x:
-        if ( !bright )
-            cmdline = QString::fromLatin1( "echo 4 > /sys/class/backlight/pxafb/power");
-        else
-            cmdline = QString::fromLatin1( "echo 0 > /sys/class/backlight/pxafb/power; echo %1 > /sys/class/backlight/pxafb/brightness" ).arg( bright );
-        // No Global::shellQuote as we gurantee it to be sane
-        res = ( ::system( QFile::encodeName(cmdline) ) == 0 );
-        break; 
-	
-    case Model_iPAQ_HX4700:
-            cmdline = QString::fromLatin1( "echo %1 > /sys/class/backlight/w100fb/brightness" ).arg( bright );
-        // No Global::shellQuote as we gurantee it to be sane
-        res = ( ::system( QFile::encodeName(cmdline) ) == 0 );
-        break; 
-	
-
-    default:
+    QDir sysClass( "/sys/class" );
+    if ( sysClass.exists() ) {
+	OAbstractMobileDevice::setDisplayBrightness( bright );
+    } else {
         if (( fd = ::open ( "/dev/touchscreen/0", O_WRONLY )) >= 0 ) {
             FLITE_IN bl;
             bl. mode = 1;
@@ -409,6 +394,13 @@
 
 int iPAQ::displayBrightnessResolution() const
 {
+    int res = 1;
+
+    QDir sysClass( "/sys/class" );
+    if ( sysClass.exists() ) {
+	return OAbstractMobileDevice::displayBrightnessResolution();
+    }
+
     switch ( model()) {
         case Model_iPAQ_H31xx:
         case Model_iPAQ_H36xx:
@@ -432,24 +424,18 @@
 
 bool iPAQ::setDisplayStatus ( bool on )
 {
-    bool res = false;
-
-    QString cmdline;
-
-    if ( model() == Model_iPAQ_H191x ) {
-        cmdline = QString::fromLatin1( "echo %1 > /sys/class/lcd/pxafb/power; echo %2 > /sys/class/backlight/pxafb/power").arg( on ? "0" : "4" ).arg( on ? "0" : "4" );
-    } else {
-        return OAbstractMobileDevice::setDisplayStatus(on);
-    }
-
-    res = ( ::system( QFile::encodeName(cmdline) ) == 0 );
-
-    return res;
+    return OAbstractMobileDevice::setDisplayStatus(on);
 }
 
 bool iPAQ::hasLightSensor() const
 {
-    return true;
+    switch ( model()) {
+        case Model_iPAQ_H22xx:
+	    return false;
+
+	default:
+	    return true;
+    }
 }
 
 int iPAQ::readLightSensor()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.