Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wakelock changes #1

Merged
merged 1 commit into from
Dec 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add MOBILE_WAKE_LOCK option to disable wakelock when not necessary (w…
…ebvr-polyfill/#169), and to disable when on desktop (webvr-polyfill/#275) using NoSleep module (webvr-polyfill/#217)
jsantell committed Dec 28, 2017
commit 0faac5b05dc7b02be7e903436bf1356df660b6d6
24 changes: 24 additions & 0 deletions licenses.txt
Original file line number Diff line number Diff line change
@@ -55,3 +55,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @license
* nosleep.js
* Copyright (c) 2017, Rich Tibbett
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
},
"dependencies": {
"gl-preserve-state": "^1.0.0",
"nosleep.js": "^0.7.0",
"webvr-polyfill-dpdb": "^1.0.3"
}
}
35 changes: 27 additions & 8 deletions src/base.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
*/

var Util = require('./util.js');
var WakeLock = require('./wakelock.js');
var NoSleep = require('nosleep.js');

// Start at a higher number to reduce chance of conflict.
var nextDisplayId = 1000;
@@ -38,7 +38,10 @@ function VRFrameData() {
/**
* The base class for all VR displays.
*/
function VRDisplay() {
function VRDisplay(config) {
config = config || {};
var USE_WAKELOCK = 'wakelock' in config ? config.wakelock : true;

this.isPolyfilled = true;
this.displayId = nextDisplayId++;
this.displayName = '';
@@ -73,7 +76,11 @@ function VRDisplay() {
this.fullscreenChangeHandler_ = null;
this.fullscreenErrorHandler_ = null;

this.wakelock_ = new WakeLock();
// Get an appropriate wakelock for Android or iOS if MOBILE_WAKE_LOCK
// is true.
if (USE_WAKELOCK && Util.isMobile()) {
this.wakelock_ = new NoSleep();
}
}

VRDisplay.prototype.getFrameData = function(frameData) {
@@ -272,7 +279,7 @@ VRDisplay.prototype.requestPresent = function(layers) {
screen.orientation.unlock();
}
self.removeFullscreenWrapper();
self.wakelock_.release();
self.disableWakeLock();
self.endPresent_();
self.removeFullscreenListeners_();
}
@@ -286,7 +293,7 @@ VRDisplay.prototype.requestPresent = function(layers) {
self.removeFullscreenWrapper();
self.removeFullscreenListeners_();

self.wakelock_.release();
self.disableWakeLock();
self.waitingForPresent_ = false;
self.isPresenting = false;

@@ -297,11 +304,11 @@ VRDisplay.prototype.requestPresent = function(layers) {
onFullscreenChange, onFullscreenError);

if (Util.requestFullscreen(fullscreenElement)) {
self.wakelock_.request();
self.enableWakeLock();
self.waitingForPresent_ = true;
} else if (Util.isIOS() || Util.isWebViewAndroid()) {
// *sigh* Just fake it.
self.wakelock_.request();
self.enableWakeLock();
self.isPresenting = true;
self.beginPresent_();
self.fireVRDisplayPresentChange_();
@@ -321,7 +328,7 @@ VRDisplay.prototype.exitPresent = function() {
var self = this;
this.isPresenting = false;
this.layer_ = null;
this.wakelock_.release();
this.disableWakeLock();

return new Promise(function(resolve, reject) {
if (wasPresenting) {
@@ -426,6 +433,18 @@ VRDisplay.prototype.removeFullscreenListeners_ = function() {
this.fullscreenErrorHandler_ = null;
};

VRDisplay.prototype.enableWakeLock = function() {
if (this.wakelock_) {
this.wakelock_.enable();
}
}

VRDisplay.prototype.disableWakeLock = function() {
if (this.wakelock_) {
this.wakelock_.disable();
}
}

VRDisplay.prototype.beginPresent_ = function() {
// Override to add custom behavior when presentation begins.
};
10 changes: 8 additions & 2 deletions src/cardboard-vr-display.js
Original file line number Diff line number Diff line change
@@ -34,7 +34,13 @@ var Eye = {
*/
function CardboardVRDisplay(config) {
var defaults = Util.extend({}, Options);
this.config = Util.extend(defaults, config || {});
config = Util.extend(defaults, config || {});

VRDisplay.call(this, {
wakelock: config.MOBILE_WAKE_LOCK,
});

this.config = config;

this.displayName = 'Cardboard VRDisplay';

@@ -69,7 +75,7 @@ function CardboardVRDisplay(config) {
window.addEventListener('resize', this.onResize_.bind(this));
}
}
CardboardVRDisplay.prototype = new VRDisplay();
CardboardVRDisplay.prototype = Object.create(VRDisplay.prototype);

CardboardVRDisplay.prototype.getImmediatePose = function() {
return {
6 changes: 6 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -14,6 +14,12 @@
*/

module.exports = {
// By default, on mobile, a wakelock is necessary to prevent the device's screen
// from turning off without user input. Disable if you're keeping the screen awake through
// other means on mobile. A wakelock is never used on desktop.
// Added in 1.0.3.
MOBILE_WAKE_LOCK: true,

// Whether or not CardboardVRDisplay is in debug mode. Logs extra
// messages. Added in 1.0.2.
DEBUG: false,
78 changes: 0 additions & 78 deletions src/wakelock.js

This file was deleted.