kmilo
asus.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 #include <kpluginfactory.h>
00025 #include <kpluginloader.h>
00026 #include <kconfig.h>
00027 #include <krun.h>
00028 #include <kurl.h>
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031
00032 #include <QFile>
00033 #include <QDir>
00034
00035 #include "kmilointerface.h"
00036
00037 #include "asus.h"
00038
00039 #ifdef Q_OS_FREEBSD
00040 #include <sys/types.h>
00041 #include <sys/sysctl.h>
00042 #endif
00043
00044 #define ROUND(x) (int(x + 0.5))
00045
00046 namespace KMilo {
00047
00048 AsusMonitor::AsusMonitor(QObject* parent, const QVariantList& args): Monitor(parent, args)
00049 {
00050 }
00051
00052 AsusMonitor::~AsusMonitor()
00053 {
00054 }
00055
00056 bool AsusMonitor::init()
00057 {
00058 kDebug() << "loading asus kmilo plugin" ;
00059
00060 #ifdef Q_OS_FREEBSD
00061
00062
00063
00064
00065 size_t len = 4;
00066 if ( sysctlnametomib("hw.acpi.asus.lcd_brightness", brightness_mib, &len) == -1 ||
00067 sysctlnametomib("hw.acpi.asus.lcd_backlight", backlight_mib, &len) == -1 ||
00068 sysctlnametomib("hw.acpi.asus.video_output", video_mib, &len) == -1 )
00069 {
00070 kDebug() << "The driver's sysctls don't seem to exist, check that the acpi_asus module is loaded" ;
00071 return false;
00072 }
00073 #else
00074
00075 QDir dir("/proc/acpi/asus");
00076 if (!dir.exists())
00077 {
00078 kDebug() << "/proc/acpi/asus doesn't exist, check that the asus_acpi module is loaded" ;
00079 return false;
00080 }
00081 #endif
00082 else
00083 {
00084 clearStruct(asus_state);
00085 clearStruct(last_asus_state);
00086 if (!readProc(&asus_state))
00087 {
00088 return false;
00089 }
00090 }
00091
00092 return true;
00093 }
00094
00095 Monitor::DisplayType AsusMonitor::poll()
00096 {
00097
00098
00099 memcpy(&last_asus_state, &asus_state, sizeof(asus_state_struct));
00100 readProc(&asus_state);
00101
00102 Monitor::DisplayType pollResult = None;
00103
00104 if (last_asus_state.brightness != asus_state.brightness)
00105 {
00106 pollResult = Brightness;
00107 float value = (float)asus_state.brightness / 15;
00108 m_progress = ROUND(value * 100);
00109 }
00110
00111 if (last_asus_state.lcd != asus_state.lcd)
00112 {
00113 if (asus_state.lcd == 0)
00114 {
00115 _interface->displayText(i18n("Display changed: LCD off"));
00116 }
00117 else
00118 {
00119 _interface->displayText(i18n("Display changed: LCD on"));
00120 }
00121 }
00122
00123 if (last_asus_state.display != asus_state.display)
00124 {
00125 switch (asus_state.display)
00126 {
00127 case 0:
00128 _interface->displayText(i18n("Display changed: off"));
00129 break;
00130 case 1:
00131 _interface->displayText(i18n("Display changed: LCD on"));
00132 break;
00133 case 2:
00134 _interface->displayText(i18n("Display changed: CRT on"));
00135 break;
00136 case 3:
00137 _interface->displayText(i18n("Display changed: LCD and CRT on"));
00138 break;
00139 case 4:
00140 _interface->displayText(i18n("Display changed: TV out on"));
00141 break;
00142 case 5:
00143 _interface->displayText(i18n("Display changed: LCD and TV out on"));
00144 break;
00145 case 6:
00146 _interface->displayText(i18n("Display changed: CRT and TV out on"));
00147 break;
00148 case 7:
00149 _interface->displayText(i18n("Display changed: LCD, CRT and TV out on"));
00150 break;
00151 }
00152 }
00153
00154 return pollResult;
00155 }
00156
00157
00158 int AsusMonitor::progress() const
00159 {
00160 return m_progress;
00161 }
00162
00163 bool AsusMonitor::readProc(asus_state_struct* asus_state)
00164 {
00165 #ifdef Q_OS_FREEBSD
00166 int value;
00167 size_t value_len = sizeof(value);
00168
00169 if ( sysctl(brightness_mib, 4, &value, &value_len, NULL, 0) != -1 )
00170 asus_state->brightness = value;
00171
00172 if ( sysctl(backlight_mib, 4, &value, &value_len, NULL, 0) != -1 )
00173 asus_state->lcd = value;
00174
00175 if ( sysctl(video_mib, 4, &value, &value_len, NULL, 0) != -1 )
00176 asus_state->display = value;
00177 #else
00178 asus_state->brightness = readProcEntry(QString("brn"));
00179 asus_state->lcd = readProcEntry(QString("lcd"));
00180
00181
00182
00183 #endif
00184 return true;
00185 }
00186
00187 int AsusMonitor::readProcEntry(const QString &name)
00188 {
00189 QFile f(QString("/proc/acpi/asus/%1").arg(name));
00190
00191 if (f.open(QIODevice::ReadOnly))
00192 {
00193 QString line;
00194 if ( !(line = f.readLine()).isNull())
00195 {
00196 line = line.trimmed();
00197 int value = line.section(' ', 0, 0).toInt();
00198 if (value > 0)
00199 {
00200 return value;
00201 }
00202 }
00203 }
00204 return 0;
00205 }
00206
00207 void AsusMonitor::clearStruct(asus_state_struct& asus_state)
00208 {
00209 asus_state.brightness = 0;
00210 asus_state.lcd = 0;
00211 asus_state.display = 0;
00212 }
00213
00214 }
00215
00216 K_PLUGIN_FACTORY(KMiloAsusFactory, registerPlugin<KMilo::AsusMonitor>();)
00217 K_EXPORT_PLUGIN(KMiloAsusFactory("kmilo_asus"))
00218