简单总结下在嵌入式arm板中设置屏保的方法:
在Qt中有二个QWSScreenSaver类
这个类的定义如下
定义路径qwindowsystem_qws.h
class
Q_GUI_EXPORT QWSScreenSaver
086
{
087
public
:
088
virtual
~QWSScreenSaver();
089
virtual
void
restore()=0;
090
virtual
bool
save(
int
level)=0;
091
};
是类class QWSServerPrivate 的成员,在成类中与屏保设置相关的类成员还有
QTime screensavertime;
097
QTimer* screensavertimer;
098
int
* screensaverintervals;
099
int
screensavereventblocklevel;
100
bool
screensaverblockevents;
101
bool
screensaverblockevent(
int
index,
int
*screensaverinterval,
bool
isDown );
102
QWSScreenSaver* saver;
void screenSave(
int
level);
在设置屏保的时候,Qt程序要作为服务端来运行,QWSScreenSaver中的虚函数virtualvoid
restore()和virtualbool
save(
int
level)都是用来重载,
设置在屏保状态的处理在后面的函数中重载,设置恢复屏保的处理在前面的虚函数中执行,为了节省功耗,我在屏保状态下关掉屏幕的背光,在恢复状态下开启背光。
我用这个方法做个简单的测试,能实现屏保时间设置的动态更。说下我这个测试的调试过程
定义了个SCreenSaver类,继承自QWSScreenSaver
类的定义和实现代码如下
#ifndef SCREENSAVER_H
#define SCREENSAVER_H
#include
#include
class ScreenSaver : public QWSScreenSaver
{
public:
ScreenSaver();
virtual void restore();
virtual bool save(int level);
private:
int freq;
int dutycycle;
public slots:
void ScreenSaver_parameter_changed(int);
};
#endif // SCREENSAVER_H
实现文件如下
#include "screensaver.h"
#include
#include
#include
#include
#include
#include
#include
#include
#define IOCTL_FREQUENCY 0
#define IOCTL_DUTYCYCLE 1
static int fd;
ScreenSaver::ScreenSaver()
:QWSScreenSaver()
{
// window=new QMainWindow();
qDebug()<<"Inlize";
QSettings *readini = new QSettings("/home/config.ini", QSettings::IniFormat, 0); //初始化读取当前背光值,并存储
readini->beginGroup("backlight");
dutycycle=readini->value("backlight_duty").toInt();
qDebug("dutycycle=%d",dutycycle);
readini->endGroup();
delete readini;
freq= 6500;
}
void ScreenSaver::restore()
{
qDebug("enter screen restore");
//open pwm
if((fd=open("/dev/pwm9",O_RDWR)) < 0)
{
printf("open device gptimer8 error
");
exit(1);// close();
}
printf("The frequecy is %d Hz
",freq);
printf("The duty cycle is %d percent
",dutycycle);
ioctl(fd,IOCTL_FREQUENCY,freq);
ioctl(fd,IOCTL_DUTYCYCLE,dutycycle);
::close(fd);
}
bool ScreenSaver::save(int level)
{
//close pwm 反逻辑
if((fd=open("/dev/pwm9",O_RDWR)) < 0)
{
printf("open device gptimer8 error
");
exit(1);// close();
}
printf("The frequecy is %d Hz
",freq);
printf("The duty cycle is 90
");
ioctl(fd,IOCTL_FREQUENCY,freq);
ioctl(fd,IOCTL_DUTYCYCLE,100);
::close(fd);
qDebug()<<"window show";
return true;
}
void ScreenSaver::ScreenSaver_parameter_changed(int interval)
{
QWSServer::setScreenSaverInterval(interval*1000);
// ignore the key/mouse event that turns on the screen
}
屏保设置还需要做一些其他的设置。
我创建一个另外两个类,Input和mainwindow,在mainwindow的构造函数中对屏保进行了设置
saver = new ScreenSaver;
QWSServer::setScreenSaver((QWSScreenSaver *)saver);
QWSServer::setScreenSaverInterval(10000);//读配置文件得出初始值
QWSServer::screenSaverActivate(true);
// int blocklevel = 1;
// QWSServer::setScreenSaverBlockLevel( blocklevel );