major code refactorings to lower the confusing branching. This is mostly tested, but bugs may have been introduced. Progress on #32

This commit is contained in:
R. Aidan Campbell 2016-09-03 12:04:46 -04:00
parent 225db4eaaa
commit 2df5527cc0
2 changed files with 198 additions and 228 deletions

View File

@ -2,9 +2,7 @@ var hosts = {}; // hosts is an associative array of NvHTTP objects, keyed by se
var activePolls = {}; // hosts currently being polled. An associated array of polling IDs, keyed by server UID var activePolls = {}; // hosts currently being polled. An associated array of polling IDs, keyed by server UID
var pairingCert; var pairingCert;
var myUniqueid; var myUniqueid;
var api; var api; // `api` should only be set if we're in a host-specific screen. on the initial screen it should always be null.
var relaunchSourceEvent;
var unpairHostSourceEvent;
// Called by the common.js module. // Called by the common.js module.
function attachListeners() { function attachListeners() {
@ -15,16 +13,8 @@ function attachListeners() {
$('#bitrateSlider').on('input', updateBitrateField); // input occurs every notch you slide $('#bitrateSlider').on('input', updateBitrateField); // input occurs every notch you slide
$('#bitrateSlider').on('change', saveBitrate); // change occurs once the mouse lets go. $('#bitrateSlider').on('change', saveBitrate); // change occurs once the mouse lets go.
$("#remoteAudioEnabledSwitch").on('click', saveRemoteAudio); $("#remoteAudioEnabledSwitch").on('click', saveRemoteAudio);
$('#hostChosen').on('click', hostChosen);
$('#addHostCell').on('click', addHost); $('#addHostCell').on('click', addHost);
$('#cancelAddHost').on('click', cancelAddHost);
$('#continueAddHost').on('click', continueAddHost);
$('#continueUnpairHost').on('click', unpairHost);
$('#cancelUnpairHost').on('click', cancelUnpairHost);
$('#cancelPairingDialog').on('click', pairingPopupCanceled);
$('#cancelQuitApp').on('click', cancelQuitApp);
$('#backIcon').on('click', showHostsAndSettingsMode); $('#backIcon').on('click', showHostsAndSettingsMode);
$('#continueQuitApp').on('click', continueQuitApp);
$('#quitCurrentApp').on('click', stopGameWithConfirmation); $('#quitCurrentApp').on('click', stopGameWithConfirmation);
$(window).resize(fullscreenNaclModule); $(window).resize(fullscreenNaclModule);
chrome.app.window.current().onMaximized.addListener(fullscreenChromeWindow); chrome.app.window.current().onMaximized.addListener(fullscreenChromeWindow);
@ -89,10 +79,6 @@ function beginBackgroundPollingOfHost(host) {
// The host was already online. Just start polling in the background now. // The host was already online. Just start polling in the background now.
activePolls[host.serverUid] = window.setInterval(function() { activePolls[host.serverUid] = window.setInterval(function() {
if (api && activePolls[api.serverUid] != null) {
stopBackgroundPollingOfHost(api);
return;
}
// every 5 seconds, poll at the address we know it was live at // every 5 seconds, poll at the address we know it was live at
host.pollServer(function () { host.pollServer(function () {
if (host.online) { if (host.online) {
@ -115,10 +101,6 @@ function beginBackgroundPollingOfHost(host) {
// Now start background polling // Now start background polling
activePolls[host.serverUid] = window.setInterval(function() { activePolls[host.serverUid] = window.setInterval(function() {
if (api && activePolls[api.serverUid] != null) {
stopBackgroundPollingOfHost(api);
return;
}
// every 5 seconds, poll at the address we know it was live at // every 5 seconds, poll at the address we know it was live at
host.pollServer(function () { host.pollServer(function () {
if (host.online) { if (host.online) {
@ -195,16 +177,15 @@ function pairTo(nvhttpHost, onSuccess, onFailure) {
return; return;
} }
var _api = nvhttpHost; nvhttpHost.pollServer(function (ret) {
_api.pollServer(function (ret) { if (!nvhttpHost.online) {
if (!_api.online) { snackbarLog('Failed to connect to ' + nvhttpHost.address + '! Are you sure the host is on?');
snackbarLog('Failed to connect to ' + _api.address + '! Are you sure the host is on?'); console.log(nvhttpHost.toString());
console.log(_api.toString());
onFailure(); onFailure();
return; return;
} }
if (_api.paired) { if (nvhttpHost.paired) {
onSuccess(); onSuccess();
return; return;
} }
@ -213,17 +194,22 @@ function pairTo(nvhttpHost, onSuccess, onFailure) {
var pairingDialog = document.querySelector('#pairingDialog'); var pairingDialog = document.querySelector('#pairingDialog');
$('#pairingDialogText').html('Please enter the number ' + randomNumber + ' on the GFE dialog on the computer. This dialog will be dismissed once complete'); $('#pairingDialogText').html('Please enter the number ' + randomNumber + ' on the GFE dialog on the computer. This dialog will be dismissed once complete');
pairingDialog.showModal(); pairingDialog.showModal();
console.log('sending pairing request to ' + _api.address + ' with random number ' + randomNumber);
_api.pair(randomNumber).then(function (paired) { $('#cancelPairingDialog').off('click');
$('#cancelPairingDialog').on('click', function () {
pairingDialog.close();
});
console.log('sending pairing request to ' + nvhttpHost.address + ' with random number ' + randomNumber);
nvhttpHost.pair(randomNumber).then(function (paired) {
if (!paired) { if (!paired) {
if (_api.currentGame != 0) { if (nvhttpHost.currentGame != 0) {
$('#pairingDialogText').html('Error: ' + _api.address + ' is in app. Cannot pair until the app is stopped.'); $('#pairingDialogText').html('Error: ' + nvhttpHost.address + ' is in app. Cannot pair until the app is stopped.');
} else { } else {
$('#pairingDialogText').html('Error: failed to pair with ' + _api.address + '. failure reason unknown.'); $('#pairingDialogText').html('Error: failed to pair with ' + nvhttpHost.address + '. failure reason unknown.');
} }
console.log('failed API object: '); console.log('failed API object: ');
console.log(_api.toString()); console.log(nvhttpHost.toString());
onFailure(); onFailure();
return; return;
} }
@ -232,62 +218,66 @@ function pairTo(nvhttpHost, onSuccess, onFailure) {
pairingDialog.close(); pairingDialog.close();
onSuccess(); onSuccess();
}, function (failedPairing) { }, function (failedPairing) {
snackbarLog('Failed pairing to: ' + _api.address); snackbarLog('Failed pairing to: ' + nvhttpHost.address);
console.log('pairing failed, and returned ' + failedPairing); console.log('pairing failed, and returned ' + failedPairing);
console.log('failed API object: '); console.log('failed API object: ');
console.log(_api.toString()); console.log(nvhttpHost.toString());
onFailure(); onFailure();
}); });
}); });
} }
function hostChosen(sourceEvent) { function hostChosen(host) {
// if(sourceEvent && sourceEvent.srcElement) { if (!host.online) {
// if (sourceEvent.srcElement.innerText == "") {
// console.log('user clicked image. we gotta hack to parse out the host.');
// var serverUid = sourceEvent.currentTarget.id.substring("hostgrid-".length);
// } else {
// console.log('parsing host from grid element.');
// var serverUid = sourceEvent.srcElement.id.substring("hostgrid-".length);
// }
// } else
if (sourceEvent && sourceEvent.target && sourceEvent.target.id ) {
console.log('hacking out the host');
var serverUid = sourceEvent.target.id.substring("hostgrid-".length);
} else if (sourceEvent.target.parentElement && sourceEvent.target.parentElement.id) {
var serverUid = sourceEvent.target.parentElement.id.substring("hostgrid-".length);
} else {
console.log('Failed to find host! This should never happen!');
console.log(sourceEvent);
}
api = hosts[serverUid];
if (!api.online) {
return; return;
} }
stopBackgroundPollingOfHost(api); stopBackgroundPollingOfHost(host);
api = host;
if (!api.paired) { if (!host.paired) {
// Still not paired; go to the pairing flow // Still not paired; go to the pairing flow
pairTo(api, function(){ showApps(api); saveHosts();}, function(){}); pairTo(host, function() {
showApps(host);
saveHosts();
},
function(){
});
} else { } else {
// When we queried again, it was paired, so show apps. // When we queried again, it was paired, so show apps.
showApps(api); showApps(host);
} }
} }
// the `+` was selected on the host grid. // the `+` was selected on the host grid.
// give the user a dialog to input connection details for the PC // give the user a dialog to input connection details for the PC
function addHost() { function addHost() {
document.querySelector('#addHostDialog').showModal(); var modal = document.querySelector('#addHostDialog');
modal.showModal();
// drop the dialog if they cancel
$('#cancelAddHost').off('click');
$('#cancelAddHost').on('click', function() {
modal.close();
});
// try to pair if they continue
$('#continueAddHost').off('click');
$('#continueAddHost').on('click', function () {
var inputHost = $('#dialogInputHost').val();
var _nvhttpHost = new NvHTTP(inputHost, myUniqueid, inputHost);
pairTo(_nvhttpHost, function() {
beginBackgroundPollingOfHost(_nvhttpHost);
addHostToGrid(_nvhttpHost);
saveHosts();
}, function() {
snackbarLog('pairing to ' + inputHost + ' failed!');
});
modal.close();
});
} }
// user canceled the dialog for adding a new PC
function cancelAddHost() {
document.querySelector('#addHostDialog').close();
}
// host is an NvHTTP object // host is an NvHTTP object
function addHostToGrid(host, ismDNSDiscovered=false) { function addHostToGrid(host, ismDNSDiscovered=false) {
@ -295,8 +285,14 @@ function addHostToGrid(host, ismDNSDiscovered=false) {
var cell = $("<div>", {class: 'mdl-cell mdl-cell--3-col host-cell mdl-button mdl-js-button mdl-js-ripple-effect', id: 'hostgrid-' + host.serverUid, html:host.hostname }); var cell = $("<div>", {class: 'mdl-cell mdl-cell--3-col host-cell mdl-button mdl-js-button mdl-js-ripple-effect', id: 'hostgrid-' + host.serverUid, html:host.hostname });
$(cell).prepend($("<img>", {src: "static/res/ic_desktop_windows_white_24px.svg"})); $(cell).prepend($("<img>", {src: "static/res/ic_desktop_windows_white_24px.svg"}));
var removalButton = $("<div>", {class: "remove-host", id: "removeHostButton-" + host.serverUid}); var removalButton = $("<div>", {class: "remove-host", id: "removeHostButton-" + host.serverUid});
removalButton.click(confirmUnpairHost); removalButton.off('click');
cell.click(hostChosen); removalButton.click(function () {
unpairClicked(host);
});
cell.off('click');
cell.click(function () {
hostChosen(host);
});
$(outerDiv).append(cell); $(outerDiv).append(cell);
if (!ismDNSDiscovered) { if (!ismDNSDiscovered) {
// we don't have the option to unpair from mDNS hosts. So don't show it to the user. // we don't have the option to unpair from mDNS hosts. So don't show it to the user.
@ -306,43 +302,22 @@ function addHostToGrid(host, ismDNSDiscovered=false) {
hosts[host.serverUid] = host; hosts[host.serverUid] = host;
} }
function continueAddHost() { function unpairClicked(host) {
var inputHost = $('#dialogInputHost').val();
var _nvhttpHost = new NvHTTP(inputHost, myUniqueid, inputHost);
pairTo(_nvhttpHost,
function() {
beginBackgroundPollingOfHost(_nvhttpHost);
addHostToGrid(_nvhttpHost);
saveHosts();
document.querySelector('#addHostDialog').close();
},
function() {
snackbarLog('pairing to ' + inputHost + ' failed!');
});
}
function confirmUnpairHost(sourceEvent) {
snackbarLog('Need to parse host from event: ' + sourceEvent);
var unpairHostDialog = document.querySelector('#unpairHostDialog'); var unpairHostDialog = document.querySelector('#unpairHostDialog');
document.getElementById('unpairHostDialogText').innerHTML = document.getElementById('unpairHostDialogText').innerHTML =
' Are you sure you want like to unpair from ' + hosts[sourceEvent.target.id.substring("removeHostButton-".length)].hostname + '?'; ' Are you sure you want like to unpair from ' + host.hostname + '?';
unpairHostDialog.showModal(); unpairHostDialog.showModal();
unpairHostSourceEvent = sourceEvent;
}
function cancelUnpairHost() { $('#cancelUnpairHost').off('click');
var unpairHostDialog = document.querySelector('#unpairHostDialog'); $('#cancelUnpairHost').on('click', function () {
unpairHostDialog.close(); unpairHostDialog.close();
} });
// locally remove the hostname/ip from the saved `hosts` array. // locally remove the hostname/ip from the saved `hosts` array.
// note: this does not make the host forget the pairing to us. // note: this does not make the host forget the pairing to us.
// this means we can re-add the host, and will still be paired. // this means we can re-add the host, and will still be paired.
function unpairHost() { $('#continueUnpairHost').off('click');
var sourceEvent = unpairHostSourceEvent; $('#continueUnpairHost').on('click', function () {
unpairHostSourceEvent = null;
host = hosts[sourceEvent.target.id.substring("removeHostButton-".length)];
host.unpair().then(function (onSuccess) { host.unpair().then(function (onSuccess) {
var unpairHostDialog = document.querySelector('#unpairHostDialog'); var unpairHostDialog = document.querySelector('#unpairHostDialog');
unpairHostDialog.close(); unpairHostDialog.close();
@ -353,10 +328,9 @@ function unpairHost() {
}, function (onFailure) { }, function (onFailure) {
snackbarLog('Failed to unpair from host!'); snackbarLog('Failed to unpair from host!');
}); });
} unpairHostDialog.close();
});
function pairingPopupCanceled() {
document.querySelector('#pairingDialog').close();
} }
// puts the CSS style for current app on the app that's currently running // puts the CSS style for current app on the app that's currently running
@ -379,9 +353,9 @@ function stylizeBoxArt(freshApi, appIdToStylize) {
} }
// show the app list // show the app list
function showApps() { function showApps(host) {
if(!api || !api.paired) { // safety checking. shouldn't happen. if(!host || !host.paired) { // safety checking. shouldn't happen.
console.log('Moved into showApps, but `api` did not initialize properly! Failing.'); console.log('Moved into showApps, but `host` did not initialize properly! Failing.');
return; return;
} }
$('#quitCurrentApp').show(); $('#quitCurrentApp').show();
@ -391,12 +365,12 @@ function showApps() {
$('#naclSpinnerMessage').text('Loading apps...'); $('#naclSpinnerMessage').text('Loading apps...');
$('#naclSpinner').css('display', 'inline-block'); $('#naclSpinner').css('display', 'inline-block');
api.getAppList().then(function (appList) { host.getAppList().then(function (appList) {
$('#naclSpinner').hide(); $('#naclSpinner').hide();
// if game grid is populated, empty it // if game grid is populated, empty it
appList.forEach(function (app) { appList.forEach(function (app) {
api.getBoxArt(app.id).then(function (resolvedPromise) { host.getBoxArt(app.id).then(function (resolvedPromise) {
// put the box art into the image holder // put the box art into the image holder
if ($('#game-' + app.id).length === 0) { if ($('#game-' + app.id).length === 0) {
// double clicking the button will cause multiple box arts to appear. // double clicking the button will cause multiple box arts to appear.
@ -404,24 +378,26 @@ function showApps() {
// This isn't perfect: there's lots of RTTs before the logic prevents anything // This isn't perfect: there's lots of RTTs before the logic prevents anything
var imageBlob = new Blob([resolvedPromise], {type: "image/png"}); var imageBlob = new Blob([resolvedPromise], {type: "image/png"});
$("#game-grid").append($("<div>", {html:$("<img \>", {src: URL.createObjectURL(imageBlob), id: 'game-'+app.id, name: app.title }), class: 'box-art mdl-cell mdl-cell--3-col'}).append($("<span>", {html: app.title, class:"game-title"}))); $("#game-grid").append($("<div>", {html:$("<img \>", {src: URL.createObjectURL(imageBlob), id: 'game-'+app.id, name: app.title }), class: 'box-art mdl-cell mdl-cell--3-col'}).append($("<span>", {html: app.title, class:"game-title"})));
$('#game-'+app.id).on('click', startGame); $('#game-'+app.id).on('click', function () {
startGame(host, app.id);
});
// apply CSS stylization to indicate whether the app is active // apply CSS stylization to indicate whether the app is active
stylizeBoxArt(api, app.id); stylizeBoxArt(host, app.id);
} }
}, function (failedPromise) { }, function (failedPromise) {
console.log('Error! Failed to retrieve box art for app ID: ' + app.id + '. Returned value was: ' + failedPromise) console.log('Error! Failed to retrieve box art for app ID: ' + app.id + '. Returned value was: ' + failedPromise)
console.log('failed API object: '); console.log('failed host object: ');
console.log(api.toString()); console.log(host.toString());
}); });
}); });
}, function (failedAppList) { }, function (failedAppList) {
$('#naclSpinner').hide(); $('#naclSpinner').hide();
console.log('Failed to get applist from host: ' + api.address); console.log('Failed to get applist from host: ' + host.address);
console.log('failed API object: '); console.log('failed host object: ');
console.log(api.toString()); console.log(host.toString());
}); });
showAppsMode(); showAppsMode();
@ -438,9 +414,10 @@ function showHostsAndSettingsMode() {
$("#main-content").removeClass("fullscreen"); $("#main-content").removeClass("fullscreen");
$("#listener").removeClass("fullscreen"); $("#listener").removeClass("fullscreen");
$("body").css('backgroundColor', 'white'); $("body").css('backgroundColor', 'white');
if(api && !activePolls[api.serverUid]) { // We're no longer in a host-specific screen. Null host, and add it back to the polling list
if(api) {
beginBackgroundPollingOfHost(api); beginBackgroundPollingOfHost(api);
api = null; api = null; // and null api
} }
} }
@ -454,48 +431,49 @@ function showAppsMode() {
$("#main-content").removeClass("fullscreen"); $("#main-content").removeClass("fullscreen");
$("#listener").removeClass("fullscreen"); $("#listener").removeClass("fullscreen");
$("body").css('backgroundColor', 'white'); $("body").css('backgroundColor', 'white');
} }
// start the given appID. if another app is running, offer to quit it. // start the given appID. if another app is running, offer to quit it.
// if the given app is already running, just resume it. // if the given app is already running, just resume it.
function startGame(sourceEvent) { function startGame(host, appID) {
if(!api || !api.paired) { if(!host || !host.paired) {
console.log('attempted to start a game, but `api` did not initialize properly. Failing!'); console.log('attempted to start a game, but `host` did not initialize properly. Failing!');
return; return;
} }
if(sourceEvent && sourceEvent.target) {
appID = parseInt(sourceEvent.target.id.substring('game-'.length)); // parse the AppID from the ID of the grid icon.
appName = sourceEvent.target.name;
} else {
console.log('Error! failed to parse appID from grid icon! Failing...');
snackbarLog('An error occurred while parsing the appID from the grid icon.')
return;
}
var host = api.address;
// refresh the server info, because the user might have quit the game. // refresh the server info, because the user might have quit the game.
api.refreshServerInfo().then(function (ret) { host.refreshServerInfo().then(function (ret) {
if(api.currentGame != 0 && api.currentGame != appID) { host.getAppById(appID).then(function (appToStart) {
api.getAppById(api.currentGame).then(function (currentApp) {
// This event gets saved and passed back to this callback
// after the game is quit
relaunchSourceEvent = sourceEvent;
if(host.currentGame != 0 && host.currentGame != appID) {
host.getAppById(host.currentGame).then(function (currentApp) {
var quitAppDialog = document.querySelector('#quitAppDialog'); var quitAppDialog = document.querySelector('#quitAppDialog');
document.getElementById('quitAppDialogText').innerHTML = document.getElementById('quitAppDialogText').innerHTML =
currentApp.title + ' is already running. Would you like to quit ' + currentApp.title + ' is already running. Would you like to quit ' +
currentApp.title + '?'; currentApp.title + '?';
quitAppDialog.showModal(); quitAppDialog.showModal();
$('#cancelQuitApp').off('click');
$('#cancelQuitApp').on('click', function () {
quitAppDialog.close();
console.log('closing app dialog, and returning');
});
$('#continueQuitApp').off('click');
$('#continueQuitApp').on('click', function () {
console.log('stopping game, and closing app dialog, and returning');
stopGame(host, function () {
// please oh please don't infinite loop with recursion
startGame(host, appID);
});
quitAppDialog.close();
});
return; return;
}, function (failedCurrentApp) { }, function (failedCurrentApp) {
console.log('ERROR: failed to get the current running app from host!'); console.log('ERROR: failed to get the current running app from host!');
console.log('Returned error was: ' + failedCurrentApp); console.log('Returned error was: ' + failedCurrentApp);
console.log('failed API object: '); console.log('failed host object: ');
console.log(api.toString()); console.log(host.toString());
return; return;
}); });
return; return;
@ -506,18 +484,18 @@ function startGame(sourceEvent) {
var streamHeight = $('#selectResolution option:selected').val().split(':')[1]; var streamHeight = $('#selectResolution option:selected').val().split(':')[1];
// we told the user it was in Mbps. We're dirty liars and use Kbps behind their back. // we told the user it was in Mbps. We're dirty liars and use Kbps behind their back.
var bitrate = parseInt($("#bitrateSlider").val()) * 1000; var bitrate = parseInt($("#bitrateSlider").val()) * 1000;
console.log('startRequest:' + host + ":" + streamWidth + ":" + streamHeight + ":" + frameRate + ":" + bitrate); console.log('startRequest:' + host.address + ":" + streamWidth + ":" + streamHeight + ":" + frameRate + ":" + bitrate);
var rikey = generateRemoteInputKey(); var rikey = generateRemoteInputKey();
var rikeyid = generateRemoteInputKeyId(); var rikeyid = generateRemoteInputKeyId();
$('#loadingMessage').text('Starting ' + appName + '...'); $('#loadingMessage').text('Starting ' + appToStart.title + '...');
playGameMode(); playGameMode();
if(api.currentGame == appID) { // if user wants to launch the already-running app, then we resume it. if(host.currentGame == appID) { // if user wants to launch the already-running app, then we resume it.
return api.resumeApp(rikey, rikeyid).then(function (ret) { return host.resumeApp(rikey, rikeyid).then(function (ret) {
sendMessage('startRequest', [host, streamWidth, streamHeight, frameRate, sendMessage('startRequest', [host.address, streamWidth, streamHeight, frameRate,
bitrate.toString(), api.serverMajorVersion.toString(), rikey, rikeyid.toString()]); bitrate.toString(), host.serverMajorVersion.toString(), rikey, rikeyid.toString()]);
}, function (failedResumeApp) { }, function (failedResumeApp) {
console.log('ERROR: failed to resume the app!'); console.log('ERROR: failed to resume the app!');
console.log('Returned error was: ' + failedResumeApp); console.log('Returned error was: ' + failedResumeApp);
@ -527,43 +505,23 @@ function startGame(sourceEvent) {
remote_audio_enabled = $("#remoteAudioEnabledSwitch").parent().hasClass('is-checked') ? 1 : 0; remote_audio_enabled = $("#remoteAudioEnabledSwitch").parent().hasClass('is-checked') ? 1 : 0;
api.launchApp(appID, host.launchApp(appID,
streamWidth + "x" + streamHeight + "x" + frameRate, streamWidth + "x" + streamHeight + "x" + frameRate,
1, // Allow GFE to optimize game settings 1, // Allow GFE to optimize game settings
rikey, rikeyid, rikey, rikeyid,
remote_audio_enabled, // Play audio locally too? remote_audio_enabled, // Play audio locally too?
0x030002 // Surround channel mask << 16 | Surround channel count 0x030002 // Surround channel mask << 16 | Surround channel count
).then(function (ret) { ).then(function (ret) {
sendMessage('startRequest', [host, streamWidth, streamHeight, frameRate, sendMessage('startRequest', [host.address, streamWidth, streamHeight, frameRate,
bitrate.toString(), api.serverMajorVersion.toString(), rikey, rikeyid.toString()]); bitrate.toString(), host.serverMajorVersion.toString(), rikey, rikeyid.toString()]);
}, function (failedLaunchApp) { }, function (failedLaunchApp) {
console.log('ERROR: failed to launch app with appID: ' + appID); console.log('ERROR: failed to launch app with appID: ' + appID);
console.log('Returned error was: ' + failedLaunchApp); console.log('Returned error was: ' + failedLaunchApp);
return; return;
}); });
});
}); });
}
function cancelQuitApp() {
relaunchSourceEvent = null;
document.querySelector('#quitAppDialog').close();
console.log('closing app dialog, and returning');
}
function continueQuitApp(sourceEvent) {
console.log('stopping game, and closing app dialog, and returning');
stopGame(
function() {
if (relaunchSourceEvent != null) {
// Save and null relaunchSourceEvent just in case startGame()
// wants to set it again.
var event = relaunchSourceEvent;
relaunchSourceEvent = null;
startGame(event);
}
}
);
document.querySelector('#quitAppDialog').close();
} }
function playGameMode() { function playGameMode() {
@ -607,32 +565,44 @@ function stopGameWithConfirmation() {
' Are you sure you would like to quit ' + ' Are you sure you would like to quit ' +
currentGame.title + '? Unsaved progress will be lost.'; currentGame.title + '? Unsaved progress will be lost.';
quitAppDialog.showModal(); quitAppDialog.showModal();
$('#cancelQuitApp').off('click');
$('#cancelQuitApp').on('click', function () {
console.log('closing app dialog, and returning');
quitAppDialog.close();
});
$('#continueQuitApp').off('click');
$('#continueQuitApp').on('click', function () {
console.log('stopping game, and closing app dialog, and returning');
stopGame(api);
quitAppDialog.close();
});
}); });
} }
} }
function stopGame(callbackFunction) { function stopGame(host, callbackFunction) {
if (!api.paired) { if (!host.paired) {
return; return;
} }
api.refreshServerInfo().then(function (ret) { host.refreshServerInfo().then(function (ret) {
api.getAppById(api.currentGame).then(function (runningApp) { host.getAppById(host.currentGame).then(function (runningApp) {
if (!runningApp) { if (!runningApp) {
snackbarLog('Nothing was running'); snackbarLog('Nothing was running');
return; return;
} }
var appName = runningApp.title; var appName = runningApp.title;
snackbarLog('Stopping ' + appName); snackbarLog('Stopping ' + appName);
api.quitApp().then(function (ret2) { host.quitApp().then(function (ret2) {
api.refreshServerInfo().then(function (ret3) { // refresh to show no app is currently running. host.refreshServerInfo().then(function (ret3) { // refresh to show no app is currently running.
showAppsMode(); showAppsMode();
stylizeBoxArt(api, runningApp.id); stylizeBoxArt(host, runningApp.id);
if (typeof(callbackFunction) === "function") callbackFunction(); if (typeof(callbackFunction) === "function") callbackFunction();
}, function (failedRefreshInfo2) { }, function (failedRefreshInfo2) {
console.log('ERROR: failed to refresh server info!'); console.log('ERROR: failed to refresh server info!');
console.log('Returned error was: ' + failedRefreshInfo2); console.log('Returned error was: ' + failedRefreshInfo2);
console.log('failed server was: ' + api.toString()); console.log('failed server was: ' + host.toString());
}); });
}, function (failedQuitApp) { }, function (failedQuitApp) {
console.log('ERROR: failed to quit app!'); console.log('ERROR: failed to quit app!');

View File

@ -29,7 +29,7 @@ function handleMessage(msg) {
stylizeBoxArt(api, app.id); // and reapply stylization to indicate what's currently running stylizeBoxArt(api, app.id); // and reapply stylization to indicate what's currently running
}); });
}); });
showApps(); showApps(api);
chrome.app.window.current().restore(); chrome.app.window.current().restore();
}); });