401 lines
11 KiB
JavaScript
Raw Normal View History

2016-09-07 00:41:13 -05:00
/**
* RetroArch Web Player
*
* This provides the basic JavaScript for the RetroArch web player.
*/
var client = new Dropbox.Client({ key: "il6e10mfd7pgf8r" });
2016-09-29 08:49:31 -05:00
var BrowserFS = BrowserFS;
2016-09-17 02:00:55 -05:00
var afs;
2016-09-07 00:41:13 -05:00
var showError = function(error) {
switch (error.status) {
case Dropbox.ApiError.INVALID_TOKEN:
// If you're using dropbox.js, the only cause behind this error is that
// the user token expired.
// Get the user through the authentication flow again.
break;
case Dropbox.ApiError.NOT_FOUND:
// The file or folder you tried to access is not in the user's Dropbox.
// Handling this error is specific to your application.
break;
case Dropbox.ApiError.OVER_QUOTA:
// The user is over their Dropbox quota.
// Tell them their Dropbox is full. Refreshing the page won't help.
break;
case Dropbox.ApiError.RATE_LIMITED:
// Too many API requests. Tell the user to try again later.
// Long-term, optimize your code to use fewer API calls.
break;
case Dropbox.ApiError.NETWORK_ERROR:
// An error occurred at the XMLHttpRequest layer.
// Most likely, the user's network connection is down.
// API calls will not succeed until the user gets back online.
break;
case Dropbox.ApiError.INVALID_PARAM:
case Dropbox.ApiError.OAUTH_ERROR:
case Dropbox.ApiError.INVALID_METHOD:
default:
// Caused by a bug in dropbox.js, in your application, or in Dropbox.
// Tell the user an error occurred, ask them to refresh the page.
}
};
function cleanupStorage()
{
localStorage.clear();
if (BrowserFS.FileSystem.IndexedDB.isAvailable())
2016-11-21 20:58:45 -05:00
{
var req = indexedDB.deleteDatabase("RetroArch");
req.onsuccess = function () {
console.log("Deleted database successfully");
};
req.onerror = function () {
console.log("Couldn't delete database");
};
req.onblocked = function () {
console.log("Couldn't delete database due to the operation being blocked");
};
}
2016-09-17 15:11:54 -05:00
document.getElementById("btnClean").disabled = true;
}
2016-09-07 00:41:13 -05:00
function dropboxInit()
{
document.getElementById("btnDrop").disabled = true;
2016-09-08 00:49:36 -05:00
$('#icnDrop').removeClass('fa-dropbox');
2016-09-11 00:19:18 -04:00
$('#icnDrop').addClass('fa-spinner fa-spin');
2016-09-07 00:41:13 -05:00
client.authDriver(new Dropbox.AuthDriver.Redirect());
client.authenticate({ rememberUser: true }, function(error, client)
{
if (error)
2016-09-07 00:41:13 -05:00
{
return showError(error);
}
dropboxSync(client, dropboxSyncComplete);
2016-09-07 00:41:13 -05:00
});
}
2016-09-07 00:41:13 -05:00
function dropboxSync(dropboxClient, cb)
{
var dbfs = new BrowserFS.FileSystem.Dropbox(dropboxClient);
2016-09-08 00:49:36 -05:00
// Wrap in afsFS.
afs = new BrowserFS.FileSystem.AsyncMirror(
2016-09-07 00:41:13 -05:00
new BrowserFS.FileSystem.InMemory(), dbfs);
2016-09-08 00:49:36 -05:00
afs.initialize(function(err)
2016-09-07 00:41:13 -05:00
{
// Initialize it as the root file system.
2016-09-08 00:49:36 -05:00
//BrowserFS.initialize(afs);
cb();
2016-09-07 00:41:13 -05:00
});
}
2016-09-24 17:30:03 -05:00
function dropboxSyncComplete()
{
$('#icnDrop').removeClass('fa-spinner').removeClass('fa-spin');
$('#icnDrop').addClass('fa-check');
console.log("WEBPLAYER: Dropbox sync successful");
setupFileSystem("dropbox");
preLoadingComplete();
}
function idbfsInit()
{
document.getElementById("btnLocal").disabled = true;
$('#icnLocal').removeClass('fa-globe');
$('#icnLocal').addClass('fa-spinner fa-spin');
2016-09-24 17:30:03 -05:00
var imfs = new BrowserFS.FileSystem.InMemory();
if (BrowserFS.FileSystem.IndexedDB.isAvailable())
2016-09-24 17:30:03 -05:00
{
afs = new BrowserFS.FileSystem.AsyncMirror(imfs,
new BrowserFS.FileSystem.IndexedDB(function(e, fs)
2016-09-24 17:30:03 -05:00
{
if (e)
{
//fallback to imfs
afs = new BrowserFS.FileSystem.InMemory();
console.log("WEBPLAYER: error: " + e + " falling back to in-memory filesystem");
2016-09-24 17:30:03 -05:00
setupFileSystem("browser");
preLoadingComplete();
}
else
2016-09-24 17:30:03 -05:00
{
// initialize afs by copying files from async storage to sync storage.
afs.initialize(function (e)
2016-09-24 17:30:03 -05:00
{
if (e)
2016-09-24 17:30:03 -05:00
{
afs = new BrowserFS.FileSystem.InMemory();
console.log("WEBPLAYER: error: " + e + " falling back to in-memory filesystem");
2016-09-24 17:30:03 -05:00
setupFileSystem("browser");
preLoadingComplete();
}
else
2016-09-24 17:30:03 -05:00
{
idbfsSyncComplete();
2016-09-24 17:30:03 -05:00
}
});
}
},
"RetroArch"));
}
}
function idbfsSyncComplete()
{
$('#icnLocal').removeClass('fa-spinner').removeClass('fa-spin');
$('#icnLocal').addClass('fa-check');
console.log("WEBPLAYER: idbfs setup successful");
setupFileSystem("browser");
preLoadingComplete();
}
function preLoadingComplete()
{
/* Make the Preview image clickable to start RetroArch. */
$('.webplayer-preview').addClass('loaded').click(function () {
startRetroArch();
return false;
});
document.getElementById("btnRun").disabled = false;
$('#btnRun').removeClass('disabled');
}
2016-09-08 00:49:36 -05:00
function setupFileSystem(backend)
2016-09-07 00:41:13 -05:00
{
2016-09-09 00:27:24 -05:00
/* create a mountable filesystem that will server as a root
2016-09-09 00:25:27 -05:00
mountpoint for browserfs */
var mfs = new BrowserFS.FileSystem.MountableFileSystem();
/* create an XmlHttpRequest filesystem for the bundled data */
var xfs1 = new BrowserFS.FileSystem.XmlHttpRequest
(".index-xhr", "/assets/frontend/bundle/");
2016-09-09 00:25:27 -05:00
/* create an XmlHttpRequest filesystem for core assets */
var xfs2 = new BrowserFS.FileSystem.XmlHttpRequest
(".index-xhr", "/assets/cores/");
2016-09-24 17:30:03 -05:00
console.log("WEBPLAYER: initializing filesystem: " + backend);
mfs.mount('/home/web_user/retroarch/userdata', afs);
2016-09-09 00:25:27 -05:00
mfs.mount('/home/web_user/retroarch/bundle', xfs1);
mfs.mount('/home/web_user/retroarch/userdata/content/downloads', xfs2);
2016-09-09 00:25:27 -05:00
BrowserFS.initialize(mfs);
var BFS = new BrowserFS.EmscriptenFS();
FS.mount(BFS, {root: '/home'}, '/home');
2016-09-24 17:30:03 -05:00
console.log("WEBPLAYER: " + backend + " filesystem initialization successful");
2016-09-07 00:41:13 -05:00
}
/**
* Retrieve the value of the given GET parameter.
*/
function getParam(name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results) {
return results[1] || null;
}
}
function startRetroArch()
{
$('.webplayer').show();
$('.webplayer-preview').hide();
document.getElementById("btnDrop").disabled = true;
document.getElementById("btnRun").disabled = true;
2016-09-08 00:49:36 -05:00
$('#btnFullscreen').removeClass('disabled');
$('#btnMenu').removeClass('disabled');
2016-09-08 00:49:36 -05:00
$('#btnAdd').removeClass('disabled');
$('#btnRom').removeClass('disabled');
2016-09-07 00:41:13 -05:00
document.getElementById("btnAdd").disabled = false;
document.getElementById("btnRom").disabled = false;
document.getElementById("btnMenu").disabled = false;
document.getElementById("btnFullscreen").disabled = false;
2016-09-08 00:49:36 -05:00
Module['callMain'](Module['arguments']);
document.getElementById('canvas').focus();
2016-09-07 00:41:13 -05:00
}
function selectFiles(files)
{
2016-09-07 12:50:01 -05:00
$('#btnAdd').addClass('disabled');
$('#icnAdd').removeClass('fa-plus');
$('#icnAdd').addClass('fa-spinner spinning');
2016-09-07 16:04:18 -05:00
var count = files.length;
2016-09-07 12:50:01 -05:00
for (var i = 0; i < files.length; i++)
2016-09-07 12:50:01 -05:00
{
filereader = new FileReader();
filereader.file_name = files[i].name;
filereader.readAsArrayBuffer(files[i]);
filereader.onload = function(){uploadData(this.result, this.file_name)};
filereader.onloadend = function(evt)
2016-09-07 12:50:01 -05:00
{
2016-09-24 17:30:03 -05:00
console.log("WEBPLAYER: file: " + this.file_name + " upload complete");
2016-09-07 12:50:01 -05:00
if (evt.target.readyState == FileReader.DONE)
{
$('#btnAdd').removeClass('disabled');
$('#icnAdd').removeClass('fa-spinner spinning');
$('#icnAdd').addClass('fa-plus');
}
}
}
2016-09-07 00:41:13 -05:00
}
function uploadData(data,name)
{
2016-09-08 00:49:36 -05:00
var dataView = new Uint8Array(data);
FS.createDataFile('/', name, dataView, true, false);
var data = FS.readFile(name,{ encoding: 'binary' });
FS.writeFile('/home/web_user/retroarch/userdata/content/' + name, data ,{ encoding: 'binary' });
2016-09-08 00:49:36 -05:00
FS.unlink(name);
2016-09-07 00:41:13 -05:00
}
var Module =
2016-09-07 00:41:13 -05:00
{
noInitialRun: true,
arguments: ["-v", "--menu"],
preRun: [],
postRun: [],
print: (function()
{
var element = document.getElementById('output');
element.value = ''; // clear browser cache
return function(text)
{
text = Array.prototype.slice.call(arguments).join(' ');
element.value += text + "\n";
element.scrollTop = 99999; // focus on bottom
};
})(),
printErr: function(text)
{
var text = Array.prototype.slice.call(arguments).join(' ');
var element = document.getElementById('output');
element.value += text + "\n";
element.scrollTop = 99999; // focus on bottom
},
2016-09-07 00:41:13 -05:00
canvas: document.getElementById('canvas'),
totalDependencies: 0,
monitorRunDependencies: function(left)
2016-09-07 00:41:13 -05:00
{
this.totalDependencies = Math.max(this.totalDependencies, left);
2016-09-08 00:49:36 -05:00
}
2016-09-07 00:41:13 -05:00
};
function switchCore(corename) {
localStorage.setItem("core", corename);
}
2016-09-08 00:49:36 -05:00
function switchStorage(backend) {
if (backend != localStorage.getItem("backend"))
{
localStorage.setItem("backend", backend);
location.reload();
}
}
2016-09-07 00:41:13 -05:00
// When the browser has loaded everything.
$(function() {
// Enable all available ToolTips.
$('.tooltip-enable').tooltip({
placement: 'right'
});
/**
* Attempt to disable some default browser keys.
*/
var keys = {
9: "tab",
13: "enter",
16: "shift",
18: "alt",
27: "esc",
33: "rePag",
34: "avPag",
35: "end",
36: "home",
37: "left",
38: "up",
39: "right",
40: "down",
112: "F1",
113: "F2",
114: "F3",
115: "F4",
116: "F5",
117: "F6",
118: "F7",
119: "F8",
120: "F9",
121: "F10",
122: "F11",
123: "F12"
};
window.addEventListener('keydown', function (e) {
if (keys[e.which]) {
e.preventDefault();
}
});
// Switch the core when selecting one.
$('#core-selector a').click(function () {
var coreChoice = $(this).data('core');
switchCore(coreChoice);
});
// Find which core to load.
var core = localStorage.getItem("core", core);
if (!core) {
core = 'gambatte';
}
// Make the core the selected core in the UI.
var coreTitle = $('#core-selector a[data-core="' + core + '"]').addClass('active').text();
$('#dropdownMenu1').text(coreTitle);
2016-09-07 00:41:13 -05:00
// Load the Core's related JavaScript.
$.getScript(core + '_libretro.js', function ()
{
$('#icnRun').removeClass('fa-spinner').removeClass('fa-spin');
$('#icnRun').addClass('fa-play');
if (localStorage.getItem("backend") == "dropbox")
{
$('#lblDrop').addClass('active');
$('#lblLocal').removeClass('active');
dropboxInit();
}
else
{
$('#lblDrop').removeClass('active');
$('#lblLocal').addClass('active');
2016-09-24 17:30:03 -05:00
idbfsInit();
}
});
});
2016-09-11 02:37:56 -05:00
function keyPress(k)
{
kp(k, "keydown");
setTimeout(function(){kp(k, "keyup")}, 50);
2016-09-11 02:37:56 -05:00
}
kp = function(k, event) {
2018-01-14 21:02:59 -06:00
var oEvent = new KeyboardEvent(event, { code: k });
2016-09-11 02:37:56 -05:00
document.dispatchEvent(oEvent);
document.getElementById('canvas').focus();
2016-09-14 10:42:15 -05:00
}