不带桌面管理的linux平台 

通过重写QApplication的notify监听触摸事件,来控制定时器重新计时

Application.h

#ifndef APPLICATION_H
#define APPLICATION_H


#include <QApplication>
#include <QTimer>
#include <QWidget>
#include <QMutex>

class Application:public QApplication
{
    Q_OBJECT
  public:
    explicit Application(int& argc, char** argv);
    bool notify(QObject *, QEvent *);

    void Backlight(bool status);
    void setInterval(int sec);



  public slots:
    //操作超时
    void OperationTimeOut();

    //进入屏保
    void EnterScreenSaver();
    //退出屏保
    void ExitScreenSaver();


  private:

    QTimer* _timer;
    bool _isScreenSaver;
    QWidget* _screen;
    QMutex _mutex;

};


#endif // APPLICATION_H

Application.cpp

#include "Application.h"
#include <QDebug>

#if defined(Q_OS_LINUX)
#include <unistd.h>
#include <time.h>
#endif

Application::Application(int &argc, char **argv)
    :QApplication(argc,argv, QApplication::GuiServer),_screen(NULL),
      _isScreenSaver(false)
{
    //打开背光
    Backlight(true);

    //设置定时器
    _timer = new QTimer();
    _timer->setInterval(3*1000);
    _timer->start();
    QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(OperationTimeOut()));
}

void Application::Backlight(bool status)
{
    if(status)
        system("echo \"7\" > /sys/devices/platform/backlight/backlight/backlight/brightness ");
    else
      system("echo \"0\" > /sys/devices/platform/backlight/backlight/backlight/brightness ");
}

void Application::setInterval(int sec)
{
  _timer->stop();
  _timer->setInterval(sec*1000);
  _timer->start();
}



void Application::OperationTimeOut()
{
    EnterScreenSaver();
}

//进入屏保
void Application::EnterScreenSaver()
{
    if(_mutex.tryLock())
    {
        if(!_isScreenSaver)
        {
            Backlight(false);

            if(_screen == NULL)
            {//实例化一个QWidget,可做屏保 和 接收唤醒时触摸事件
                _screen = new QWidget;
                _screen->showFullScreen();
            }
            _isScreenSaver = true;
            qDebug()<<__FUNCTION__;
        }
        _mutex.unlock();
    }
}

//退出屏保
void Application::ExitScreenSaver()
{
    _mutex.lock();
    if(_isScreenSaver)
    {

        if(_screen != NULL)
        {
#if defined(Q_OS_LINUX)
            usleep(1000*300);
#endif
            _screen->close();
            _screen = NULL;
        }
        _isScreenSaver = false;
        Backlight(true);
        qDebug()<<__FUNCTION__;
    }
    _mutex.unlock();
}


bool Application::notify(QObject *obj, QEvent *e)
{
    if(e->type() == QEvent::MouseMove)
    {
        _timer->stop();
        ExitScreenSaver();
    } else {
        if(!_timer->isActive())
            _timer->start();
    }
    return QApplication::notify(obj,e);
}



Logo

更多推荐