Skip to content

Commit

Permalink
- Added PowerManagement plugin for Qt port of PhoneGap
Browse files Browse the repository at this point in the history
  • Loading branch information
Viras- committed Sep 27, 2011
1 parent 4415249 commit bf3fb19
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Qt/PowerManagement/plugins/powermanagement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "powermanagement.h"

PowerManagement::PowerManagement(QWebFrame *p_webFrame) :
PGPlugin(p_webFrame)
{
m_systemScreenSaver = new QSystemScreenSaver();
}

void PowerManagement::release( int scId, int ecId ) {
m_systemScreenSaver->setScreenSaverInhibited(false);

// Check if everything went fine
if( !m_systemScreenSaver->screenSaverInhibited() ) {
this->callback( scId, "" );
}
else {
this->callback( ecId, "" );
}
}

void PowerManagement::acquire( int scId, int ecId ) {
m_systemScreenSaver->setScreenSaverInhibited(true);

// Check if everything went fine
if( m_systemScreenSaver->screenSaverInhibited() ) {
this->callback( scId, "" );
}
else {
this->callback( ecId, "" );
}
}
26 changes: 26 additions & 0 deletions Qt/PowerManagement/plugins/powermanagement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef POWERMANAGEMENT_H
#define POWERMANAGEMENT_H

#include "../pgplugin.h"

#include <QSystemScreenSaver>

QTM_USE_NAMESPACE

class PowerManagement : public PGPlugin
{
Q_OBJECT
public:
explicit PowerManagement(QWebFrame *p_webFrame);

signals:

public slots:
void release( int scId, int ecId );
void acquire( int scId, int ecId );

private:
QSystemScreenSaver *m_systemScreenSaver;
};

#endif // POWERMANAGEMENT_H
89 changes: 89 additions & 0 deletions Qt/PowerManagement/www/js/powermanagement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2011, Wolfgang Koller - http://www.gofg.at/
*/

function PowermanagementError() {
};

PowermanagementError.cast = function( p_code, p_message ) {
var powerManagementError = new PowermanagementError();
powerManagementError.code = p_code;
powerManagementError.message = p_message;

return powerManagementError;
};

PowermanagementError.ALREADY_ACTIVE = 1;
PowermanagementError.NOT_SUPPORTED = 2;
PowermanagementError.NOT_ACTIVE = 3;

PowermanagementError.prototype.code = 0;
PowermanagementError.prototype.message = "";

function Powermanagement() {
};

Powermanagement.prototype.acquired = false;

Powermanagement.prototype.acquire = function( successCallback, errorCallback ) {
if( this.acquired ) {
errorCallback( PowermanagementError.cast( PowermanagementError.ALREADY_ACTIVE, "Powermanagement lock already active." ) );
return;
}

var me = this;
PhoneGap.exec( function() {
me.acquired = true;
successCallback();
}, function() {
me.acquired = false;
errorCallback( PowermanagementError.cast( PowermanagementError.NOT_SUPPORTED, "Powermanagement not supported." ) );
}, "com.phonegap.plugin.Powermanagement", "acquire", [] );
};

Powermanagement.prototype.release = function( successCallback, errorCallback ) {
if( !this.acquired ) {
errorCallback( PowermanagementError.cast( PowermanagementError.NOT_ACTIVE, "Powermanagement lock not active." ) );
return;
}

var me = this;
PhoneGap.exec( function() {
me.acquired = false;
successCallback();
}, function() {
me.acquired = false;
errorCallback( PowermanagementError.cast( PowermanagementError.NOT_SUPPORTED, "Powermanagement not supported." ) );
}, "com.phonegap.plugin.Powermanagement", "release", [] );
};

Powermanagement.prototype.dim = function( successCallback, errorCallback ) {
if( this.acquired ) {
errorCallback( PowermanagementError.cast( PowermanagementError.ALREADY_ACTIVE, "Powermanagement lock already active." ) );
return;
}

var me = this;
if( !PhoneGap.exec( function() {
me.acquired = true;
successCallback();
}, function() {
me.acquired = false;
errorCallback( PowermanagementError.cast( PowermanagementError.NOT_SUPPORTED, "Powermanagement not supported." ) );
}, "com.phonegap.plugin.Powermanagement", "dim", [] ) ) {

// If dimming doesn't work, try to acquire a full wake lock
this.acquire( successCallback, errorCallback );
}
};

/**
* Create Powermanagement instance
*/
PhoneGap.addConstructor( "com.phonegap.plugin.Powermanagement", function () {
window.Powermanagement = new Powermanagement();
window.PowerManagement = window.Powermanagement; // Alias for backwards compatibility
} );

0 comments on commit bf3fb19

Please sign in to comment.