-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathennuiboard.min.js
21 lines (21 loc) · 3.58 KB
/
ennuiboard.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* Copyright (c) 2020 Yahweasel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
Ennuiboard=function(){function k(a,b){var f=new KeyboardEvent("key"+a,{key:b,bubbles:!0});(document.activeElement||document.body).dispatchEvent(f)}var c={supported:{any:!1},enabled:{any:!1},enabling:{},requiresPermissions:{},enable:function(a,b){return!this.supported[a]?Promise.resolve(!1):this.enabling[a]?this.enabling[a]:this.enabling[a]=this[a].enable(b).then(function(f){if(f&&b&&b.auto&&"undefined"!==typeof localStorage){var d=localStorage.getItem("eb-auto"),d=d?JSON.parse(d):{};d[a]=b;localStorage.setItem("eb-auto",
JSON.stringify(d))}delete c.enabling[a];return f})},disable:function(a){if("undefined"!==typeof localStorage){var b=localStorage.getItem("eb-auto");b&&(b=JSON.parse(b),delete b[a],0===Object.keys(b).length?localStorage.removeItem("eb-auto"):localStorage.setItem("eb-auto",JSON.stringify(b)))}},gamepad:{requiresPermissions:!1,supported:function(){return!(!navigator.getGamepads&&!navigator.webkitGetGamepads)},enable:function(a){if(c.enabled.gamepad)return Promise.resolve(!0);a=a||{};navigator.getGamepads?
(this._getGamepads=navigator.getGamepads.bind(navigator),this._queryButton=this._standardQueryButton):(this._getGamepads=navigator.webkitGetGamepads.bind(navigator),this._queryButton=this._webkitQueryButton);a.manualPoll||(this._interval=setInterval(this.poll.bind(this),50));c.enabled.gamepad=c.enabled.any=!0;return Promise.resolve(!0)},state:{},poll:function(){for(var a=this._getGamepads(),b=0;b<a.length;b++){var f=a[b];if(f){f.id in this.state||(this.state[f.id]={});for(var d=this.state[f.id],c=
0;c<f.buttons.length;c++){var e="b"+c;e in d||(d[e]=!1);var g=this._queryButton(f.buttons[c]);g!==d[e]&&(k(g?"down":"up","eb:gamepad:"+f.id+":"+e),d[e]=g)}for(c=0;c<f.axes.length;c++)if(e="a"+c,e in d||(d[e]=0),g=~~Math.round(f.axes[c]),g!==d[e]){var l="eb:gamepad:"+f.id+":"+e;d[e]&&k("up",l+(0<d[e]?"+":"-"));g&&k("down",l+(0<g?"+":"-"));d[e]=g}}}},_standardQueryButton:function(a){return 0<a.value||a.pressed},_webkitQueryButtons:function(a){return 0<a}},midi:{requiresPermissions:!0,supported:function(){return!!navigator.requestMIDIAccess},
state:{},enable:function(a){if(c.enabled.midi)return Promise.resolve(!0);var b=this;return navigator.requestMIDIAccess().then(function(a){a.inputs.forEach(function(a,c){var e=b.state[c]={};a.addEventListener("midimessage",function(a){if(!(3>a.data.length)){var b=null,d=a.data[0]&240;144===d?b=a.data[2]?"down":"up":128===d&&(b="up");b&&(a=(a.data[0]&15)+":"+a.data[1],a in e||(e[a]="up"),b!==e[a]&&(k(b,"eb:midi:"+c+":"+a),e[a]=b))}})});return!0}).catch(function(){return!1})}}};["gamepad","midi"].forEach(function(a){if(c.supported[a]=
c[a].supported())c.supported.any=!0;c.requiresPermissions[a]=c[a].requiresPermission});if("undefined"!==typeof localStorage){var h=localStorage.getItem("eb-auto");h||(h="{}");h=JSON.parse(h);Object.keys(h).sort().forEach(function(a){h[a]&&c.enable(a,h[a])})}return c}();