Compare commits

...

170 Commits

Author SHA1 Message Date
1f632db6b4 Bump version to 4.6.2 2024-05-30 17:24:50 +07:00
c07e3297ca Update better-xcloud.user.js 2024-05-30 17:22:19 +07:00
5e43915ff7 Add a Disable button in the MKB dialog 2024-05-30 17:22:06 +07:00
e21375821d Update better-xcloud.user.js 2024-05-30 16:46:56 +07:00
6438e533d6 Hide rocket animation in Smart TV profile 2024-05-30 16:46:48 +07:00
e9671cbe5d Fix video not being full screen (#415) 2024-05-30 16:28:05 +07:00
b99ec65cc9 Update better-xcloud.user.js 2024-05-30 09:34:47 +07:00
addcf56abf Minor fix 2024-05-30 09:22:04 +07:00
db17bda673 Bump version to 4.6.1 2024-05-30 07:09:39 +07:00
0a60119c3b Update better-xcloud.user.js 2024-05-30 07:09:14 +07:00
ef14c78941 Fix settings being reset after refreshing page 2024-05-30 07:04:01 +07:00
f2dc102996 Update better-xcloud.user.js 2024-05-29 20:19:43 +07:00
02db103a72 Fix pink border when using Clarity feature in Logitech G Cloud 2024-05-29 20:19:36 +07:00
f291047b64 Update better-xcloud.user.js 2024-05-29 20:09:22 +07:00
5866644673 Clear TABs when disabling touch control 2024-05-29 20:09:20 +07:00
5baad2d89a Bump version to 4.6.0 2024-05-29 17:41:27 +07:00
381f3fb679 Update better-xcloud.user.js 2024-05-29 17:29:12 +07:00
0f48cb891f Support emulated MKB in Android app
commit ad365d4ee854971122f0e8cb9157ed44b3aac0d8
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 17:19:57 2024 +0700

    Fix not able to reconnect to WebSocket server when switching game

commit ca9369318d4cbb831650e8ca631e7997dc7706cb
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 17:19:23 2024 +0700

    Stop emulated MKB when losing pointer capture

commit 8cca1a0554c46b8f61455e79d5b16f1dff9a8014
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 17:17:42 2024 +0700

    Allow fine-tuning maximum video bitrate

commit 763d414d560d9d2aa6710fd60e3f80bf43a534d6
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 08:13:56 2024 +0700

    Update mouse settings

commit d65c5ab4e4a33ed8ad13acf0a15c4bb5ace870eb
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 08:10:49 2024 +0700

    Increase MKB dialog's bg opacity

commit 3e72f2ad2700737c8148ef47629528954a606578
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 08:02:57 2024 +0700

    Show/hide MKB dialog properly

commit e7786f36508e3aa843604d9886861930bada5d60
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 07:47:21 2024 +0700

    Fix connecting to WebSocket server when it's not ready

commit 512d8c227a057e5c0399bf128bc1c52a88fcf853
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Wed May 29 07:18:06 2024 +0700

    Fix arrow keys not working in Android app

commit 0ce90f47f37d057d5a4fab0003e2bec8960d1eee
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Tue May 28 17:36:56 2024 +0700

    Set mouse's default sensitivities to 50

commit 16eb48660dd44497e16ca22343a880d9a2e53a30
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Tue May 28 17:33:37 2024 +0700

    Allow emulated MKB feature in Android app

commit c3d0e64f8502e19cd4f167fea4cdbdfc2e14b65e
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Tue May 28 17:32:49 2024 +0700

    Remove stick decay settings

commit d289d2a0dea61a440c1bc6b9392920b8e6ab6298
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Tue May 28 17:21:39 2024 +0700

    Remove stick decaying feature

commit 76bd001d98bac53f757f4ae793b2850aad055007
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Tue May 28 17:21:14 2024 +0700

    Update data structure

commit c5d3c87da9e6624ebefb288f6d7c8d06dc00916b
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Tue May 28 08:14:27 2024 +0700

    Fix not toggling the MKB feature correctly

commit 9615535cf0e4d4372e201aefb6f1231ddbc22536
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Mon May 27 20:51:57 2024 +0700

    Handle mouse data from the app
2024-05-29 17:28:39 +07:00
228c2ad008 Update better-xcloud.user.js 2024-05-28 17:48:07 +07:00
5604664b66 Bump version to 4.5.0 2024-05-26 15:45:29 +07:00
beb02796b3 Update better-xcloud.user.js 2024-05-26 15:18:06 +07:00
9041f70dbd Bug fixes 2024-05-26 15:18:04 +07:00
c13845ffe1 Update better-xcloud.user.js 2024-05-26 15:10:11 +07:00
0d0ecca155 Update translations when version changed 2024-05-26 15:05:30 +07:00
c09bd9be83 Update better-xcloud.user.js 2024-05-26 11:59:53 +07:00
15a2c67703 Observe root dialog 2024-05-26 11:59:38 +07:00
9166761780 Rename "Quick Bar" to "Stream Settings dialog" 2024-05-26 11:42:19 +07:00
ac37fe05bc Show note for Video Ratio setting 2024-05-26 11:16:52 +07:00
030791d9c4 Format 2024-05-26 10:54:32 +07:00
5523be1b7f Update better-xcloud.user.js 2024-05-26 07:46:13 +07:00
2a9b070373 Minor optimization for the shortcuts feature 2024-05-26 07:46:09 +07:00
8ba305af2b Rearrange shortcut buttons 2024-05-26 07:40:30 +07:00
29813fbaf2 Update better-xcloud.user.js 2024-05-25 18:55:44 +07:00
02f33875e4 Add L3 & R3 buttons and rearrange buttons 2024-05-25 18:55:33 +07:00
474f655707 Update better-xcloud.user.js 2024-05-25 18:10:40 +07:00
78021020ce Support device shortcuts 2024-05-25 18:10:22 +07:00
7c206bd079 Rearrange shortcuts 2024-05-25 15:09:51 +07:00
298a40d156 Update better-xcloud.user.js 2024-05-25 14:55:46 +07:00
498123af85 Add notes to Shortcuts UI 2024-05-25 14:55:24 +07:00
579dc6bf40 Update better-xcloud.user.js 2024-05-25 11:41:10 +07:00
17e02e5b32 Improve shortcut actions selection box 2024-05-25 11:41:02 +07:00
bf135d34d1 Update better-xcloud.user.js 2024-05-25 10:29:14 +07:00
9fec033173 Add shortcut to mute/unmute sound 2024-05-25 10:28:59 +07:00
78d74cfd23 Set audioGainNode to null when couldn't setup GainNode 2024-05-25 10:09:33 +07:00
3418cdd666 Update better-xcloud.user.js 2024-05-25 09:55:24 +07:00
567770c86e Fix crashing with GainNode when the stream has no sound 2024-05-25 09:55:20 +07:00
18027ed1c5 Update better-xcloud.user.js 2024-05-25 09:52:25 +07:00
dcbae39042 Add shortcuts to control stream's volume 2024-05-25 09:50:41 +07:00
90df5d655f Move MicrophoneState to shortcut-microphone 2024-05-25 07:51:51 +07:00
774a822e69 Clean up 2024-05-25 07:44:25 +07:00
5623f3f02f Use different arrow symbol in action selection box 2024-05-25 07:43:55 +07:00
4eda413da6 Update translations 2024-05-25 07:43:32 +07:00
f5b4bd2f40 Update better-xcloud.user.js 2024-05-24 20:10:29 +07:00
a702d29f22 Try to fix problem with Dualsense controller 2024-05-24 20:10:23 +07:00
71576439fd Update better-xcloud.user.js 2024-05-24 18:11:02 +07:00
07c1757237 Squashed commit of the following:
commit 2faed50e5c2165647e389d794de673038d56241e
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Fri May 24 18:09:25 2024 +0700

    Make shortcuts work with controller

commit b8f6c503ba7969de3a232644d3f6b53532a4b7bb
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Fri May 24 17:01:15 2024 +0700

    Update translations

commit 6f6c0899e5a09cd5534e06a9e272bf78c74536dc
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Fri May 24 17:00:50 2024 +0700

    Preload PrompFont

commit 1bf0f2b9dae77890d35091bed970b942c4d61fbc
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Fri May 24 07:08:05 2024 +0700

    Render Controller shortcuts settings

commit 2f24965c73a941be2ebc8a3509dc540a47b4e38d
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Thu May 23 17:21:55 2024 +0700

    Fix not able to capture screenshot after switching games

commit 6ac791e2dfb17215ee82d449047d0cd11d185c42
Author: redphx <96280+redphx@users.noreply.github.com>
Date:   Thu May 23 17:11:19 2024 +0700

    Hijack the Home button
2024-05-24 18:10:38 +07:00
22e29e1d92 Move patchPollGamepads code to external file 2024-05-23 06:55:41 +07:00
e18e05589a Move some patch code to external files 2024-05-23 06:22:25 +07:00
88df490c50 Update better-xcloud.user.js 2024-05-22 18:38:55 +07:00
e2e2322d94 Minor fixes 2024-05-22 18:38:51 +07:00
a4a1743062 Update better-xcloud.user.js 2024-05-22 18:23:08 +07:00
a3600dfd75 Fix not downloading translations when needed 2024-05-22 18:19:27 +07:00
c4ad50906e Move foreign translations to external files 2024-05-22 18:10:53 +07:00
a87b26b077 Create config.yml 2024-05-22 06:50:59 +07:00
6874d64ceb Update better-xcloud.user.js 2024-05-21 16:52:06 +07:00
a376f443ef Map the Share button on Xbox Series controller with the capturing screenshot feature 2024-05-21 16:51:55 +07:00
3bfe11280e Update 01-bug-report.yml 2024-05-21 06:28:44 +07:00
b6a3e56d9f Bump version to 4.4.0 2024-05-19 17:58:06 +07:00
a6f06fe0f1 Update better-xcloud.user.js 2024-05-19 17:49:17 +07:00
229df61f53 Update translations 2024-05-19 17:49:08 +07:00
0c712b6a31 Update better-xcloud.user.js 2024-05-19 12:14:32 +07:00
f06e36e46b Use PWA version in Android app 2024-05-19 12:14:30 +07:00
1db19f69ac Update better-xcloud.user.js 2024-05-19 11:42:44 +07:00
dc62c13c21 Optimize video player 2024-05-19 11:42:40 +07:00
d5f02550c7 Update better-xcloud.user.js 2024-05-19 10:44:35 +07:00
88b63a5518 App option to disable context menu in Home page 2024-05-19 10:43:44 +07:00
afd851861a Update translations 2024-05-19 07:54:29 +07:00
378f186ee2 Update better-xcloud.user.js 2024-05-18 19:54:49 +07:00
423b171964 Improve emulated mouse's responsiveness 2024-05-18 19:54:43 +07:00
4acf9eba11 Update better-xcloud.user.js 2024-05-18 17:53:39 +07:00
0f5c4f004b Fix button text's vertical alignment in WebView 2024-05-18 17:52:35 +07:00
c7dfacf5c4 Update default MKB preset 2024-05-18 16:06:02 +07:00
0e724b0e4f Update better-xcloud.user.js 2024-05-18 16:00:18 +07:00
47078da413 Fix not fully disable native MKB 2024-05-18 16:00:13 +07:00
e52a296872 Minor fixes 2024-05-18 15:38:03 +07:00
4c593a298e Update better-xcloud.user.js 2024-05-17 18:20:49 +07:00
962b57f0a6 Add option to disable native MKB 2024-05-17 18:19:45 +07:00
22fc730fa1 Update translations 2024-05-17 18:07:08 +07:00
5bd25bf31c Put back the Reload page button into the global settings UI 2024-05-17 17:57:41 +07:00
aba9340e91 Fix HTML issues 2024-05-17 17:24:10 +07:00
d07d6127df Update stylings of global settings UI 2024-05-17 16:57:46 +07:00
e45ed6f9ea Update better-xcloud.user.js 2024-05-15 17:46:45 +07:00
07b477a738 Add "Android app settings" button 2024-05-15 17:46:36 +07:00
fcaab4ce77 Update translations 2024-05-15 17:43:04 +07:00
3954a5d934 Update 01-bug-report.yml 2024-05-13 08:20:14 +07:00
2ef1d17901 Bump version to 4.3.0 2024-05-12 22:10:54 +07:00
8334a79f5d Update better-xcloud.user.js 2024-05-12 21:50:36 +07:00
a80da85098 Misc 2024-05-12 21:50:18 +07:00
0ffa6b55b2 Change the max value of video bitrate to 14 to discourage people from using it 2024-05-12 21:49:54 +07:00
8f8b7c6f22 Update translations 2024-05-12 21:48:58 +07:00
31804ea8cc Update better-xcloud.user.js 2024-05-12 19:00:17 +07:00
99c81cfb90 Change "Default" to "Unlimited" in maximum video bitrate setting 2024-05-12 18:59:50 +07:00
761e58254a Update better-xcloud.user.js 2024-05-12 18:06:40 +07:00
1dee720f77 Add "Maximum video bitrate" option 2024-05-12 18:05:21 +07:00
c1b41663db Update better-xcloud.user.js 2024-05-12 15:51:20 +07:00
5e1c5c5420 Remove "exposeEventTarget" patch 2024-05-12 15:51:13 +07:00
99a9396d5b Update better-xcloud.user.js 2024-05-12 15:20:44 +07:00
bd3f8c9f50 Reorder settings 2024-05-12 15:20:41 +07:00
5e8db626c5 Update better-xcloud.user.js 2024-05-12 15:19:23 +07:00
5d9319b831 Remove "experimental" flag from AUDIO_ENABLE_VOLUME_CONTROL 2024-05-12 15:18:15 +07:00
e867f156e8 Trying to fix custom touch control sometimes not showing 2024-05-12 15:07:18 +07:00
4068930db7 Fix bug with Game Bar when showing it on the right side 2024-05-12 14:40:15 +07:00
8a1dff3372 Update better-xcloud.user.js 2024-05-12 12:26:40 +07:00
41effff226 Show gyroscope settings if the custom layout supports it 2024-05-12 12:26:34 +07:00
be897848fe Cache screenshot's canvas context 2024-05-12 11:13:32 +07:00
453a45a995 Update better-xcloud.user.js 2024-05-12 08:28:26 +07:00
30e2193fe7 Update caret icons 2024-05-12 08:28:22 +07:00
f06346457a Update better-xcloud.user.js 2024-05-12 08:09:05 +07:00
cec2bdf807 Fix emulated MKB not being disabled on native MKB games (#391) 2024-05-12 08:07:35 +07:00
1be9bd8ee1 Add option for Game Bar's position 2024-05-12 08:01:49 +07:00
84adf9989e Remove empty translations at the end of arrays 2024-05-12 07:39:33 +07:00
bc429088ca Ignore translations that are the same in English 2024-05-12 07:35:42 +07:00
7d79b12d4d Update translations 2024-05-12 07:32:54 +07:00
952af5c274 Update better-xcloud.user.js 2024-05-11 21:23:42 +07:00
362c5386d1 Add microphone action for Game Bar 2024-05-11 21:15:22 +07:00
5c9202119b Update style of the show/hide touch control button 2024-05-11 21:12:14 +07:00
0092417a6e Add microphone icons 2024-05-11 20:56:00 +07:00
328372878e Update better-xcloud.user.js 2024-05-11 15:45:57 +07:00
ae37c0660f Add taking screenshot animation 2024-05-11 15:45:46 +07:00
e9b0d900b0 Update better-xcloud.user.js 2024-05-11 12:18:47 +07:00
85eac4be14 Move screenshot functions to a separate file 2024-05-11 12:18:36 +07:00
40b61b173f Use singleton in GameBar 2024-05-11 11:48:07 +07:00
b3033089ed Fix unexpected behavior with Stream bar when using Quest VR profile 2024-05-11 10:47:29 +07:00
6b88f73e34 Add setting to enable/disable Game Bar feature 2024-05-11 10:42:30 +07:00
72579249b1 Update translations 2024-05-11 10:20:11 +07:00
b866cc95a3 Detect hasTouchSupport based on spoofed User-Agent 2024-05-11 10:09:10 +07:00
8bee5b2073 Init UserAgent before STATES 2024-05-11 09:39:50 +07:00
011b75057a Refactor UserAgent class 2024-05-11 09:35:38 +07:00
daaaea1f16 Minor fixes 2024-05-11 09:16:01 +07:00
84182ffe77 Update User-Agent values 2024-05-11 09:13:21 +07:00
9ce906c0b2 Move User-Agent values to a separate localStorage item 2024-05-11 09:08:08 +07:00
77f7b647da Improve ready() in Preferences 2024-05-11 07:58:27 +07:00
9988a55601 Prevent clicking when hiding game bar & toast 2024-05-10 20:34:54 +07:00
49af04a3e0 Update better-xcloud.user.js 2024-05-10 18:38:35 +07:00
b2e932cc4c Game bar (#392)
* Fix games with custom touch control sometimes not showing touch icon

* Create game-bar with screenshot button

* Disable Game bar when opening the Guide

* Remove SCREENSHOT_BUTTON_POSITION pref

* Make the touch control action functional

* Show game bar when the game starts

* Fix 720p/High not working (#387)

* Update icons

* Update game bar's animations

* Reset states of Game bar actions before playing

* Don't show Touch control action on non-touch-supported devices

* Clean up

* Update translations

* Update actions' texts

* Clean up
2024-05-10 18:35:40 +07:00
b66ca192b2 Bump version to 4.2.0 2024-05-08 17:38:45 +07:00
660aac4e8c Update better-xcloud.user.js 2024-05-08 17:18:42 +07:00
3b1f5155c6 Update translations 2024-05-08 17:18:23 +07:00
500f6671c6 Rename "touchLayoutManager" and "testTouchLayout" 2024-05-08 17:11:15 +07:00
26bf14eda6 Show custom touch layout's author name in toast message 2024-05-08 17:04:14 +07:00
d8fada8f5d Fix not able to get Chromium version in WebView 2024-05-08 16:48:42 +07:00
4e8848d2fb Update better-xcloud.user.js 2024-05-08 08:55:10 +07:00
8e23ca51de Remove debuggers 2024-05-08 08:55:05 +07:00
9ac988e894 Update dist 2024-05-08 08:04:09 +07:00
c2efbd9c1d Remove non-cloud games from touch games list 2024-05-08 08:03:58 +07:00
7eda0b61cc Update dist 2024-05-07 21:40:28 +07:00
c948b63b8d Show touch icon on games with custom layouts 2024-05-07 21:40:12 +07:00
fc56d486a7 Update dist 2024-05-07 18:03:00 +07:00
7dacc8f23a Fix problems when holding NumberStepper's buttons 2024-05-07 18:01:36 +07:00
2df3bb4611 Add "Default opacity" setting for touch controller 2024-05-07 17:37:49 +07:00
b9355d5c01 Optimize touch control's canvas, use low-power profile and disable antialiasing 2024-05-07 17:07:15 +07:00
d1b99705e6 Bump version to 4.1.2 2024-05-05 21:32:41 +07:00
52896c94ae Update dist 2024-05-05 18:29:14 +07:00
cadc7987b7 Update "disableIndexDbLogging" patch 2024-05-05 18:29:04 +07:00
8fb1787222 Disable telemetry flags in meversion.js 2024-05-05 18:04:20 +07:00
4231d7e9c6 Update logs 2024-05-05 11:54:59 +07:00
ba05eab47b Update dist 2024-05-05 09:37:06 +07:00
e852b246d3 Rewrite volume control feature 2024-05-05 09:36:53 +07:00
23fb50cb6f Update dist 2024-05-04 17:32:17 +07:00
443bf93c9a Fix the refresh button not focusable using gamepad 2024-05-04 17:32:08 +07:00
75 changed files with 6593 additions and 8674 deletions

View File

@ -4,12 +4,19 @@ title: "[Bug] "
labels: labels:
- bug - bug
body: body:
- type: markdown - type: checkboxes
id: checklist
attributes: attributes:
value: | label: Checklist
Please fill out the following information to help us resolve the issue. options:
> [!warning] - label: I will only use English in my report.
> Only use English. Any other languages will be deleted. required: true
- label: "The bug doesn't happen when I disable Better xCloud script."
required: true
- label: I have used the search function for [**open and closed issues**](https://github.com/redphx/better-xcloud/issues?q=is%3Aissue) to see if someone else has already submitted the same bug report.
required: true
- label: I will describe the problem with as much detail as possible.
required: true
- type: dropdown - type: dropdown
id: device_type id: device_type
attributes: attributes:
@ -24,40 +31,28 @@ body:
multiple: false multiple: false
validations: validations:
required: true required: true
- type: dropdown - type: input
id: device_name
attributes:
label: "Device"
description: "Name of the device"
placeholder: "e.g., Google Pixel 8"
validations:
required: true
- type: input
id: os id: os
attributes: attributes:
label: "Operating System" label: "Operating System"
description: "Which operating system is it running?" description: "Which operating system is it running?"
options: placeholder: "e.g., Android 14"
- Windows
- macOS
- Linux
- Android
- iOS/iPadOS
- Other
multiple: false
validations:
required: true
- type: dropdown
id: browser
attributes:
label: "Browser"
description: "Which browser are you using?"
options:
- Chrome/Edge/Chromium
- Kiwi Browser
- Safari
- Other
multiple: false
validations: validations:
required: true required: true
- type: input - type: input
id: browser_version id: browser_version
attributes: attributes:
label: "Browser Version" label: "Browser Version"
description: "What is the version of the browser?" description: "What is the name and version of the browser?"
placeholder: "e.g., 122.0" placeholder: "e.g., Chrome 124.0"
validations: validations:
required: true required: true
- type: input - type: input
@ -68,12 +63,20 @@ body:
placeholder: "e.g., 3.5.0" placeholder: "e.g., 3.5.0"
validations: validations:
required: true required: true
- type: input
id: game_list
attributes:
label: "Game list"
description: "Name the game(s) where you saw this bug"
placeholder: "e.g., Halo"
validations:
required: false
- type: textarea - type: textarea
id: repro id: reproduction
attributes: attributes:
label: "Reproduction Steps" label: "Reproduction Steps"
description: | description: |
How did you trigger this bug? Please provide screenshot/video if possible. How did you trigger this bug?
placeholder: | placeholder: |
Example: Example:
1. Open game X 1. Open game X
@ -81,3 +84,11 @@ body:
3. Error 3. Error
validations: validations:
required: true required: true
- type: textarea
id: media
attributes:
label: "Screenshot/video"
description: |
Please provide screenshot/video if possible.
validations:
required: false

1
.github/ISSUE_TEMPLATE/config.yml vendored Normal file
View File

@ -0,0 +1 @@
blank_issues_enabled: false

View File

@ -1,5 +1,5 @@
// ==UserScript== // ==UserScript==
// @name Better xCloud // @name Better xCloud
// @namespace https://github.com/redphx // @namespace https://github.com/redphx
// @version 4.1.1 // @version 4.6.2
// ==/UserScript== // ==/UserScript==

File diff suppressed because it is too large Load Diff

View File

@ -71,9 +71,10 @@
span { span {
display: inline-block; display: inline-block;
height: calc(var(--bx-button-height) - 2px); height: var(--bx-button-height);
line-height: var(--bx-button-height); line-height: var(--bx-button-height);
vertical-align: middle; vertical-align: middle;
/* vertical-align: -webkit-baseline-middle; */
color: #fff; color: #fff;
overflow: hidden; overflow: hidden;
white-space: nowrap; white-space: nowrap;

View File

@ -0,0 +1,118 @@
#bx-game-bar {
z-index: var(--bx-game-bar-z-index);
position: fixed;
bottom: 0;
width: 40px;
height: 90px;
overflow: visible;
cursor: pointer;
> svg {
display: none;
pointer-events: none;
position: absolute;
height: 28px;
margin-top: 16px;
}
@media (hover: hover) {
&:hover {
> svg {
display: block;
}
}
}
.bx-game-bar-container {
opacity: 0;
position absolute;
display: flex;
overflow: hidden;
background: #1a1b1ee8;
box-shadow: 0px 0px 6px #1c1c1c;
transition: opacity 0.1s ease-in;
&.bx-show {
opacity: 0.9;
+ svg {
display: none !important;
}
}
&.bx-hide {
opacity: 0;
pointer-events: none;
}
button {
width: 60px;
height: 60px;
border-radius: 0;
svg {
width: 28px;
height: 28px;
transition: transform 0.08s ease 0s;
}
&:hover {
border-radius: 0;
}
&:active {
svg {
transform: scale(0.75);
}
}
&.bx-activated {
background-color: white;
svg {
filter: invert(1);
}
}
}
/* Touch controller buttons */
div[data-enabled] {
button {
display: none;
}
}
/* Show enabled button */
div[data-enabled='true'] {
button:first-of-type {
display: block;
}
}
/* Show enable button */
div[data-enabled='false'] {
button:last-of-type {
display: block;
}
}
}
&[data-position="bottom-left"] {
left: 0;
direction: ltr;
.bx-game-bar-container {
border-radius: 0 10px 10px 0;
}
}
&[data-position="bottom-right"] {
right: 0;
direction: rtl;
.bx-game-bar-container {
direction: ltr;
border-radius: 10px 0 0 10px;
}
}
}

View File

@ -1,17 +1,6 @@
.bx-settings-reload-button-wrapper { .bx-settings-reload-button {
z-index: var(--bx-reload-button-z-index); margin-top: 10px;
position: fixed; height: calc(var(--bx-button-height) * 1.5);
bottom: 0;
left: 0;
right: 0;
text-align: center;
background: #000000cf;
padding: 10px;
button {
max-width: 450px;
margin: 0 !important;
}
} }
.bx-settings-container { .bx-settings-container {
@ -98,35 +87,57 @@
.bx-settings-row { .bx-settings-row {
display: flex; display: flex;
margin-bottom: 8px; padding: 6px 12px;
padding: 2px 4px; position: relative;
label { label {
flex: 1; flex: 1;
align-self: center; align-self: center;
margin-bottom: 0; margin-bottom: 0;
padding-left: 10px;
} }
&:focus-within { &:hover, &:focus-within {
@media (hover: none) {
background-color: #242424; background-color: #242424;
} }
}
input { input {
align-self: center; align-self: center;
accent-color: var(--bx-primary-button-color); accent-color: var(--bx-primary-button-color);
&:focus {
accent-color: var(--bx-danger-button-color);
}
} }
select:disabled { select {
&:disabled {
-webkit-appearance: none; -webkit-appearance: none;
background: transparent; background: transparent;
text-align-last: right; text-align-last: right;
border: none; border: none;
color: #fff; color: #fff;
} }
}
input[type=checkbox], select {
&:focus {
filter: drop-shadow(1px 0 0 #fff) drop-shadow(-1px 0 0 #fff) drop-shadow(0 1px 0 #fff) drop-shadow(0 -1px 0 #fff);
}
}
&:has(input:focus), &:has(select:focus) {
&::before {
content: ' ';
border-radius: 4px;
border: 2px solid #fff;
position: absolute;
top: 0;
left: 0;
bottom: 0;
}
}
} }
.bx-settings-group-label b, .bx-settings-row label b { .bx-settings-group-label b, .bx-settings-row label b {
@ -161,6 +172,10 @@
&:hover { &:hover {
color: #6dd72b; color: #6dd72b;
} }
&:focus {
text-decoration: underline;
}
} }
.bx-settings-custom-user-agent { .bx-settings-custom-user-agent {

View File

@ -16,7 +16,6 @@
} }
.bx-mkb-pointer-lock-msg { .bx-mkb-pointer-lock-msg {
display: flex;
cursor: pointer; cursor: pointer;
user-select: none; user-select: none;
-webkit-user-select: none; -webkit-user-select: none;
@ -25,7 +24,7 @@
top: 50%; top: 50%;
transform: translateX(-50%) translateY(-50%); transform: translateX(-50%) translateY(-50%);
margin: auto; margin: auto;
background: #000000e5; background: #000000b3;
z-index: var(--bx-mkb-pointer-lock-msg-z-index); z-index: var(--bx-mkb-pointer-lock-msg-z-index);
color: #fff; color: #fff;
text-align: center; text-align: center;
@ -41,16 +40,7 @@
background: #151515; background: #151515;
} }
button { > div:first-of-type {
margin-right: 12px;
height: 60px;
}
svg {
width: 32px;
}
div {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
text-align: left; text-align: left;
@ -69,6 +59,26 @@
font-style: italic; font-style: italic;
} }
} }
> div:last-of-type {
display: flex;
flex-flow: row;
margin-top: 10px;
button {
flex: 1;
&:first-of-type {
margin-right: 5px;
}
&:last-of-type {
margin-left: 5px;
}
}
button
}
} }
.bx-mkb-preset-tools { .bx-mkb-preset-tools {

View File

@ -1,7 +1,9 @@
.bx-number-stepper { .bx-number-stepper {
text-align: center;
span { span {
display: inline-block; display: inline-block;
width: 40px; min-width: 40px;
font-family: var(--bx-monospaced-font); font-family: var(--bx-monospaced-font);
font-size: 14px; font-size: 14px;
} }
@ -35,6 +37,13 @@
} }
} }
input[type="range"] {
display: block;
margin: 12px auto 2px;
width: 180px;
color: #959595 !important;
}
input[type=range]:disabled, button:disabled { input[type=range]:disabled, button:disabled {
display: none; display: none;
} }

View File

@ -20,16 +20,15 @@
--bx-danger-button-disabled-color: #a26c6c; --bx-danger-button-disabled-color: #a26c6c;
--bx-toast-z-index: 9999; --bx-toast-z-index: 9999;
--bx-reload-button-z-index: 9200;
--bx-dialog-z-index: 9101; --bx-dialog-z-index: 9101;
--bx-dialog-overlay-z-index: 9100; --bx-dialog-overlay-z-index: 9100;
--bx-remote-play-popup-z-index: 9090; --bx-remote-play-popup-z-index: 9090;
--bx-stats-bar-z-index: 9001; --bx-stats-bar-z-index: 9001;
--bx-stream-settings-z-index: 9000; --bx-stream-settings-z-index: 9000;
--bx-mkb-pointer-lock-msg-z-index: 8999; --bx-mkb-pointer-lock-msg-z-index: 8999;
--bx-screenshot-z-index: 8888; --bx-game-bar-z-index: 8888;
--bx-touch-controller-bar-z-index: 5555;
--bx-wait-time-box-z-index: 100; --bx-wait-time-box-z-index: 100;
--bx-screenshot-animation-z-index: 1;
} }
@font-face { @font-face {
@ -88,6 +87,10 @@ div[class^=HUDButton-module__hiddenContainer] ~ div:not([class^=HUDButton-module
padding: 0 !important; padding: 0 !important;
} }
.bx-prompt {
font-family: var(--bx-promptfont-font);
}
/* Hide UI elements */ /* Hide UI elements */
#headerArea, #uhfSkipToMain, .uhf-footer { #headerArea, #uhfSkipToMain, .uhf-footer {
display: none; display: none;

View File

@ -1,46 +0,0 @@
.bx-screenshot-button {
display: none;
opacity: 0;
position: fixed;
bottom: 0;
box-sizing: border-box;
width: 60px;
height: 90px;
padding: 16px 16px 46px 16px;
background-size: cover;
background-repeat: no-repeat;
background-origin: content-box;
filter: drop-shadow(0 0 2px #000000B0);
transition: opacity 0.1s ease-in-out 0s, padding 0.1s ease-in 0s;
z-index: var(--bx-screenshot-z-index);
/* Credit: https://phosphoricons.com */
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDMyIDMyIiBmaWxsPSIjZmZmIj48cGF0aCBkPSJNMjguMzA4IDUuMDM4aC00LjI2NWwtMi4wOTctMy4xNDVhMS4yMyAxLjIzIDAgMCAwLTEuMDIzLS41NDhoLTkuODQ2YTEuMjMgMS4yMyAwIDAgMC0xLjAyMy41NDhMNy45NTYgNS4wMzhIMy42OTJBMy43MSAzLjcxIDAgMCAwIDAgOC43MzF2MTcuMjMxYTMuNzEgMy43MSAwIDAgMCAzLjY5MiAzLjY5MmgyNC42MTVBMy43MSAzLjcxIDAgMCAwIDMyIDI1Ljk2MlY4LjczMWEzLjcxIDMuNzEgMCAwIDAtMy42OTItMy42OTJ6bS02Ljc2OSAxMS42OTJjMCAzLjAzOS0yLjUgNS41MzgtNS41MzggNS41MzhzLTUuNTM4LTIuNS01LjUzOC01LjUzOCAyLjUtNS41MzggNS41MzgtNS41MzggNS41MzggMi41IDUuNTM4IDUuNTM4eiIvPjwvc3ZnPgo=');
&[data-showing=true] {
opacity: 0.9;
}
&[data-capturing=true] {
padding: 8px 8px 38px 8px;
}
}
.bx-screenshot-canvas {
display: none;
}
#bx-touch-controller-bar {
display: none;
opacity: 0;
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 6vh;
z-index: var(--bx-touch-controller-bar-z-index);
&[data-showing=true] {
display: block;
}
}

View File

@ -1,4 +1,4 @@
.bx-quick-settings-bar { .bx-stream-settings-dialog {
display: flex; display: flex;
position: fixed; position: fixed;
z-index: var(--bx-stream-settings-z-index); z-index: var(--bx-stream-settings-z-index);
@ -7,7 +7,7 @@
-webkit-user-select: none; -webkit-user-select: none;
} }
.bx-quick-settings-tabs { .bx-stream-settings-tabs {
position: fixed; position: fixed;
top: 0; top: 0;
right: 420px; right: 420px;
@ -39,7 +39,7 @@
} }
.bx-quick-settings-tab-contents { .bx-stream-settings-tab-contents {
flex-direction: column; flex-direction: column;
position: fixed; position: fixed;
right: 0; right: 0;
@ -86,17 +86,10 @@
white-space: nowrap; white-space: nowrap;
} }
} }
input[type="range"] {
display: block;
margin: 12px auto 2px;
width: 180px;
color: #959595 !important;
}
} }
.bx-quick-settings-row { .bx-stream-settings-row {
display: flex; display: flex;
border-bottom: 1px solid #40404080; border-bottom: 1px solid #40404080;
margin-bottom: 16px; margin-bottom: 16px;
@ -123,11 +116,74 @@
} }
} }
.bx-quick-settings-bar-note { .bx-stream-settings-dialog-note {
display: block; display: block;
text-align: center;
font-size: 12px; font-size: 12px;
font-weight: lighter; font-weight: lighter;
font-style: italic; font-style: italic;
padding-top: 16px; }
.bx-stream-settings-tab-contents {
div[data-group="shortcuts"] {
> div {
&[data-has-gamepad=true] {
> div:first-of-type {
display: none;
}
> div:last-of-type {
display: block;
}
}
&[data-has-gamepad=false] {
> div:first-of-type {
display: block;
}
> div:last-of-type {
display: none;
}
}
}
.bx-shortcut-profile {
width: 100%;
height: 36px;
display: block;
margin-bottom: 10px;
}
.bx-shortcut-note {
font-size: 14px;
}
.bx-shortcut-row {
display: flex;
margin-bottom: 10px;
label.bx-prompt {
flex: 1;
font-size: 26px;
margin-bottom: 0;
}
.bx-shortcut-actions {
flex: 2;
position: relative;
select {
position: absolute;
width: 100%;
height: 100%;
display: block;
&:last-of-type {
opacity: 0;
z-index: calc(var(--bx-stream-settings-z-index) + 1);
}
}
}
}
}
} }

View File

@ -12,6 +12,44 @@ div[class*=StreamMenu-module__menuContainer] > div[class*=Menu-module] {
top: calc(env(safe-area-inset-top, 0px) + 10px + 50px) !important; top: calc(env(safe-area-inset-top, 0px) + 10px + 50px) !important;
} }
body[data-media-type=default] .bx-stream-refresh-button {
left: calc(env(safe-area-inset-left, 0px) + 11px) !important;
}
body[data-media-type=tv] .bx-stream-refresh-button { body[data-media-type=tv] .bx-stream-refresh-button {
top: calc(var(--gds-focus-borderSize) + 80px) !important; top: calc(var(--gds-focus-borderSize) + 80px) !important;
} }
@keyframes bx-anim-taking-screenshot {
0% {
border: 0px solid #ffffff80;
}
50% {
border: 8px solid #ffffff80;
}
100% {
border: 0px solid #ffffff80;
}
}
div[data-testid=media-container] {
display: flex;
&.bx-taking-screenshot:before {
animation: bx-anim-taking-screenshot 0.5s ease;
content: ' ';
position: absolute;
width: 100%;
height: 100%;
z-index: var(--bx-screenshot-animation-z-index);
}
}
#game-stream video {
margin: auto;
align-self: center;
background: #000;
}

View File

@ -10,7 +10,7 @@
@import 'stream.styl'; @import 'stream.styl';
@import 'number-stepper.styl'; @import 'number-stepper.styl';
@import 'stream-actions.styl'; @import 'game-bar.styl';
@import 'stream-stats.styl'; @import 'stream-stats.styl';
@import 'stream-settings.styl'; @import 'stream-settings.styl';
@import 'mkb.styl'; @import 'mkb.styl';

View File

@ -23,6 +23,7 @@
&.bx-hide { &.bx-hide {
opacity: 0; opacity: 0;
pointer-events: none;
} }
} }

View File

@ -0,0 +1,6 @@
<svg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='#fff' fill-rule='evenodd' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' viewBox='0 0 32 32'>
<g transform="matrix(.150985 0 0 .150985 -3.32603 -2.72209)" fill="none" stroke="#fff" stroke-width="16">
<path d="M208 208H48c-8.777 0-16-7.223-16-16V80c0-8.777 7.223-16 16-16h32l16-24h64l16 24h32c8.777 0 16 7.223 16 16v112c0 8.777-7.223 16-16 16z"/>
<circle cx="128" cy="132" r="36"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 494 B

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="100%" stroke="#fff" fill="#fff" height="100%" viewBox="0 0 32 32" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2">
<path d="M6.755 1.924l-6 13.649c-.119.27-.119.578 0 .849l6 13.649c.234.533.857.775 1.389.541s.775-.857.541-1.389L2.871 15.997 8.685 2.773c.234-.533-.008-1.155-.541-1.389s-1.155.008-1.389.541z"/>
</svg>

After

Width:  |  Height:  |  Size: 386 B

View File

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="100%" stroke="#fff" fill="#fff" height="100%" viewBox="0 0 32 32" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2">
<path d="M2.685 1.924l6 13.649c.119.27.119.578 0 .849l-6 13.649c-.234.533-.857.775-1.389.541s-.775-.857-.541-1.389l5.813-13.225L.755 2.773c-.234-.533.008-1.155.541-1.389s1.155.008 1.389.541z"/>
</svg>

After

Width:  |  Height:  |  Size: 385 B

View File

@ -0,0 +1,4 @@
<svg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='#fff' fill-rule='evenodd' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' viewBox='0 0 32 32'>
<path d="M25.425 1.5c2.784 0 5.075 2.291 5.075 5.075s-2.291 5.075-5.075 5.075H20.35V6.575c0-2.784 2.291-5.075 5.075-5.075zM11.65 11.65H6.575C3.791 11.65 1.5 9.359 1.5 6.575S3.791 1.5 6.575 1.5s5.075 2.291 5.075 5.075v5.075zm8.7 8.7h5.075c2.784 0 5.075 2.291 5.075 5.075S28.209 30.5 25.425 30.5s-5.075-2.291-5.075-5.075V20.35zM6.575 30.5c-2.784 0-5.075-2.291-5.075-5.075s2.291-5.075 5.075-5.075h5.075v5.075c0 2.784-2.291 5.075-5.075 5.075z"/>
<path d="M11.65 11.65h8.7v8.7h-8.7z"/>
</svg>

After

Width:  |  Height:  |  Size: 667 B

View File

@ -0,0 +1,4 @@
<svg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='#fff' fill-rule='evenodd' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' viewBox='0 0 32 32'>
<path d="M16 25.125v5.368M5.265 4.728l21.471 23.618m-4.789-5.267c-1.698 1.326-3.793 2.047-5.947 2.047-5.3 0-9.662-4.362-9.662-9.662"/>
<path d="M25.662 15.463a9.62 9.62 0 0 1-.978 4.242m-5.64.187c-.895.616-1.957.943-3.043.939-2.945 0-5.368-2.423-5.368-5.368v-4.831m.442-5.896A5.38 5.38 0 0 1 16 1.507c2.945 0 5.368 2.423 5.368 5.368v8.588c0 .188-.01.375-.03.562"/>
</svg>

After

Width:  |  Height:  |  Size: 551 B

View File

@ -0,0 +1,3 @@
<svg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='#fff' fill-rule='evenodd' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' viewBox='0 0 32 32'>
<path d="M21.368 6.875A5.37 5.37 0 0 0 16 1.507a5.37 5.37 0 0 0-5.368 5.368v8.588A5.37 5.37 0 0 0 16 20.831a5.37 5.37 0 0 0 5.368-5.368V6.875zM16 25.125v5.368m9.662-15.03c0 5.3-4.362 9.662-9.662 9.662s-9.662-4.362-9.662-9.662"/>
</svg>

After

Width:  |  Height:  |  Size: 411 B

View File

@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="#fff" viewBox="0 0 32 32" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2">
<g fill="none" stroke="#fff">
<path d="M6.021 5.021l20 22" stroke-width="2"/>
<path d="M8.735 8.559H2.909a.89.89 0 0 0-.889.889v13.146a.89.89 0 0 0 .889.888h19.34m4.289 0h2.594a.89.89 0 0 0 .889-.888V9.448a.89.89 0 0 0-.889-.889H12.971" stroke-miterlimit="1.5" stroke-width="2.083"/>
</g>
<path d="M8.147 11.981l-.053-.001-.054.001c-.55.028-.988.483-.988 1.04v6c0 .575.467 1.042 1.042 1.042l.053-.001c.55-.028.988-.484.988-1.04v-6a1.04 1.04 0 0 0-.988-1.04z"/>
<path d="M11.147 14.981l-.054-.001h-6a1.04 1.04 0 1 0 0 2.083h6c.575 0 1.042-.467 1.042-1.042a1.04 1.04 0 0 0-.988-1.04z"/>
<circle cx="25.345" cy="18.582" r="2.561" fill="none" stroke="#fff" stroke-width="1.78" transform="matrix(1.17131 0 0 1.17131 -5.74235 -5.74456)"/>
</svg>

After

Width:  |  Height:  |  Size: 915 B

View File

@ -0,0 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="#fff" viewBox="0 0 32 32" fill-rule="evenodd" stroke-linejoin="round" stroke-miterlimit="2">
<path d="M30.021 9.448a.89.89 0 0 0-.889-.889H2.909a.89.89 0 0 0-.889.889v13.146a.89.89 0 0 0 .889.888h26.223a.89.89 0 0 0 .889-.888V9.448z" fill="none" stroke="#fff" stroke-width="2.083"/>
<path d="M8.147 11.981l-.053-.001-.054.001c-.55.028-.988.483-.988 1.04v6c0 .575.467 1.042 1.042 1.042l.053-.001c.55-.028.988-.484.988-1.04v-6a1.04 1.04 0 0 0-.988-1.04z"/>
<path d="M11.147 14.981l-.054-.001h-6a1.04 1.04 0 1 0 0 2.083h6c.575 0 1.042-.467 1.042-1.042a1.04 1.04 0 0 0-.988-1.04z"/>
<circle cx="25.345" cy="18.582" r="2.561" fill="none" stroke="#fff" stroke-width="1.78" transform="matrix(1.17131 0 0 1.17131 -5.74235 -5.74456)"/>
</svg>

After

Width:  |  Height:  |  Size: 796 B

View File

@ -11,7 +11,7 @@ import { StreamBadges } from "@modules/stream/stream-badges";
import { StreamStats } from "@modules/stream/stream-stats"; import { StreamStats } from "@modules/stream/stream-stats";
import { addCss } from "@utils/css"; import { addCss } from "@utils/css";
import { Toast } from "@utils/toast"; import { Toast } from "@utils/toast";
import { setupBxUi, updateVideoPlayerCss } from "@modules/ui/ui"; import { setupStreamUi, updateVideoPlayerCss } from "@modules/ui/ui";
import { PrefKey, getPref } from "@utils/preferences"; import { PrefKey, getPref } from "@utils/preferences";
import { LoadingScreen } from "@modules/loading-screen"; import { LoadingScreen } from "@modules/loading-screen";
import { MouseCursorHider } from "@modules/mkb/mouse-cursor-hider"; import { MouseCursorHider } from "@modules/mkb/mouse-cursor-hider";
@ -22,11 +22,14 @@ import { Patcher } from "@modules/patcher";
import { RemotePlay } from "@modules/remote-play"; import { RemotePlay } from "@modules/remote-play";
import { onHistoryChanged, patchHistoryMethod } from "@utils/history"; import { onHistoryChanged, patchHistoryMethod } from "@utils/history";
import { VibrationManager } from "@modules/vibration-manager"; import { VibrationManager } from "@modules/vibration-manager";
import { PreloadedState } from "@utils/titles-info"; import { overridePreloadState } from "@utils/preload-state";
import { patchAudioContext, patchRtcCodecs, patchRtcPeerConnection, patchVideoApi } from "@utils/monkey-patches"; import { patchAudioContext, patchCanvasContext, patchMeControl, patchRtcCodecs, patchRtcPeerConnection, patchVideoApi } from "@utils/monkey-patches";
import { STATES } from "@utils/global"; import { AppInterface, STATES } from "@utils/global";
import { injectStreamMenuButtons } from "@modules/stream/stream-ui"; import { injectStreamMenuButtons } from "@modules/stream/stream-ui";
import { BxLogger } from "@utils/bx-logger"; import { BxLogger } from "@utils/bx-logger";
import { GameBar } from "./modules/game-bar/game-bar";
import { Screenshot } from "./utils/screenshot";
// Handle login page // Handle login page
if (window.location.pathname.includes('/auth/msa')) { if (window.location.pathname.includes('/auth/msa')) {
@ -123,9 +126,7 @@ window.addEventListener(BxEvent.STREAM_LOADING, e => {
} }
// Setup UI // Setup UI
setupBxUi(); setupStreamUi();
}); });
// Setup loading screen // Setup loading screen
@ -143,37 +144,22 @@ window.addEventListener(BxEvent.STREAM_STARTING, e => {
}); });
window.addEventListener(BxEvent.STREAM_PLAYING, e => { window.addEventListener(BxEvent.STREAM_PLAYING, e => {
const $video = (e as any).$video; const $video = (e as any).$video as HTMLVideoElement;
STATES.currentStream.$video = $video; STATES.currentStream.$video = $video;
STATES.isPlaying = true; STATES.isPlaying = true;
injectStreamMenuButtons(); injectStreamMenuButtons();
/*
if (getPref(Preferences.CONTROLLER_ENABLE_SHORTCUTS)) {
GamepadHandler.startPolling();
}
*/
const PREF_SCREENSHOT_BUTTON_POSITION = getPref(PrefKey.SCREENSHOT_BUTTON_POSITION); if (getPref(PrefKey.GAME_BAR_POSITION) !== 'off') {
STATES.currentStream.$screenshotCanvas!.width = $video.videoWidth; const gameBar = GameBar.getInstance();
STATES.currentStream.$screenshotCanvas!.height = $video.videoHeight; gameBar.reset();
gameBar.enable();
gameBar.showBar();
}
Screenshot.updateCanvasSize($video.videoWidth, $video.videoHeight);
updateVideoPlayerCss(); updateVideoPlayerCss();
// Setup screenshot button
if (PREF_SCREENSHOT_BUTTON_POSITION !== 'none') {
const $btn = document.querySelector('.bx-screenshot-button')! as HTMLElement;
$btn.classList.remove('bx-gone');
$btn.style.display = 'block';
if (PREF_SCREENSHOT_BUTTON_POSITION === 'bottom-right') {
$btn.style.right = '0';
} else {
$btn.style.left = '0';
}
}
const $touchControllerBar = document.getElementById('bx-touch-controller-bar');
$touchControllerBar && $touchControllerBar.classList.remove('bx-gone');
}); });
window.addEventListener(BxEvent.STREAM_ERROR_PAGE, e => { window.addEventListener(BxEvent.STREAM_ERROR_PAGE, e => {
@ -186,41 +172,84 @@ window.addEventListener(BxEvent.STREAM_STOPPED, e => {
} }
STATES.isPlaying = false; STATES.isPlaying = false;
STATES.currentStream = {};
window.BX_EXPOSED.shouldShowSensorControls = false;
// Stop MKB listeners // Stop MKB listeners
getPref(PrefKey.MKB_ENABLED) && MkbHandler.INSTANCE.destroy(); getPref(PrefKey.MKB_ENABLED) && MkbHandler.INSTANCE.destroy();
const $quickBar = document.querySelector('.bx-quick-settings-bar'); const $streamSettingsDialog = document.querySelector('.bx-stream-settings-dialog');
if ($quickBar) { if ($streamSettingsDialog) {
$quickBar.classList.add('bx-gone'); $streamSettingsDialog.classList.add('bx-gone');
} }
STATES.currentStream.audioGainNode = null; STATES.currentStream.audioGainNode = null;
STATES.currentStream.$video = null; STATES.currentStream.$video = null;
StreamStats.onStoppedPlaying(); StreamStats.onStoppedPlaying();
const $screenshotBtn = document.querySelector('.bx-screenshot-button');
if ($screenshotBtn) {
$screenshotBtn.removeAttribute('style');
}
MouseCursorHider.stop(); MouseCursorHider.stop();
TouchController.reset(); TouchController.reset();
GameBar.getInstance().disable();
});
window.addEventListener(BxEvent.CAPTURE_SCREENSHOT, e => {
Screenshot.takeScreenshot();
}); });
function observeRootDialog($root: HTMLElement) {
let currentShown = false;
const observer = new MutationObserver(mutationList => {
for (const mutation of mutationList) {
if (mutation.type !== 'childList') {
continue;
}
const shown = ($root.firstElementChild && $root.firstElementChild.childElementCount > 0) || false;
if (shown !== currentShown) {
currentShown = shown;
BxEvent.dispatch(window, shown ? BxEvent.XCLOUD_DIALOG_SHOWN : BxEvent.XCLOUD_DIALOG_DISMISSED);
}
}
});
observer.observe($root, {subtree: true, childList: true});
}
function waitForRootDialog() {
const observer = new MutationObserver(mutationList => {
for (const mutation of mutationList) {
if (mutation.type !== 'childList') {
continue;
}
const $target = mutation.target as HTMLElement;
if ($target.id && $target.id === 'gamepass-dialog-root') {
observer.disconnect();
observeRootDialog($target);
break;
}
};
});
observer.observe(document.documentElement, {subtree: true, childList: true});
}
function main() { function main() {
waitForRootDialog();
// Monkey patches // Monkey patches
patchRtcPeerConnection(); patchRtcPeerConnection();
patchRtcCodecs(); patchRtcCodecs();
interceptHttpRequests(); interceptHttpRequests();
patchVideoApi(); patchVideoApi();
patchCanvasContext();
if (getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL)) { getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && patchAudioContext();
patchAudioContext(); getPref(PrefKey.BLOCK_TRACKING) && patchMeControl();
}
PreloadedState.override(); STATES.hasTouchSupport && TouchController.updateCustomList();
overridePreloadState();
VibrationManager.initialSetup(); VibrationManager.initialSetup();
@ -230,7 +259,8 @@ function main() {
// Setup UI // Setup UI
addCss(); addCss();
Toast.setup(); Toast.setup();
BX_FLAGS.PreloadUi && setupBxUi(); (getPref(PrefKey.GAME_BAR_POSITION) !== 'off') && GameBar.getInstance();
BX_FLAGS.PreloadUi && setupStreamUi();
StreamBadges.setupEvents(); StreamBadges.setupEvents();
StreamStats.setupEvents(); StreamStats.setupEvents();
@ -252,6 +282,9 @@ function main() {
if (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all') { if (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all') {
TouchController.setup(); TouchController.setup();
} }
// Start PointerProviderServer
(getPref(PrefKey.MKB_ENABLED)) && AppInterface && AppInterface.startPointerServer();
} }
main(); main();

View File

@ -0,0 +1,369 @@
import { Screenshot } from "@utils/screenshot";
import { GamepadKey } from "./mkb/definitions";
import { PrompFont } from "@utils/prompt-font";
import { CE } from "@utils/html";
import { t } from "@utils/translation";
import { MkbHandler } from "./mkb/mkb-handler";
import { StreamStats } from "./stream/stream-stats";
import { MicrophoneShortcut } from "./shortcuts/shortcut-microphone";
import { StreamUiShortcut } from "./shortcuts/shortcut-stream-ui";
import { PrefKey, getPref } from "@utils/preferences";
import { SoundShortcut } from "./shortcuts/shortcut-sound";
import { BxEvent } from "@/utils/bx-event";
import { AppInterface } from "@/utils/global";
enum ShortcutAction {
STREAM_SCREENSHOT_CAPTURE = 'stream-screenshot-capture',
STREAM_MENU_SHOW = 'stream-menu-show',
STREAM_STATS_TOGGLE = 'stream-stats-toggle',
STREAM_SOUND_TOGGLE = 'stream-sound-toggle',
STREAM_MICROPHONE_TOGGLE = 'stream-microphone-toggle',
STREAM_VOLUME_INC = 'stream-volume-inc',
STREAM_VOLUME_DEC = 'stream-volume-dec',
DEVICE_SOUND_TOGGLE = 'device-sound-toggle',
DEVICE_VOLUME_INC = 'device-volume-inc',
DEVICE_VOLUME_DEC = 'device-volume-dec',
DEVICE_BRIGHTNESS_INC = 'device-brightness-inc',
DEVICE_BRIGHTNESS_DEC = 'device-brightness-dec',
}
export class ControllerShortcut {
static readonly #STORAGE_KEY = 'better_xcloud_controller_shortcuts';
static #buttonsCache: {[key: string]: boolean[]} = {};
static #buttonsStatus: {[key: string]: boolean[]} = {};
static #$selectProfile: HTMLSelectElement;
static #$selectActions: Partial<{[key in GamepadKey]: HTMLSelectElement}> = {};
static #$container: HTMLElement;
static #ACTIONS: {[key: string]: (ShortcutAction | null)[]} = {};
static reset(index: number) {
ControllerShortcut.#buttonsCache[index] = [];
ControllerShortcut.#buttonsStatus[index] = [];
}
static handle(gamepad: Gamepad): boolean {
const gamepadIndex = gamepad.index;
const actions = ControllerShortcut.#ACTIONS[gamepad.id];
if (!actions) {
return false;
}
// Move the buttons status from the previous frame to the cache
ControllerShortcut.#buttonsCache[gamepadIndex] = ControllerShortcut.#buttonsStatus[gamepadIndex].slice(0);
// Clear the buttons status
ControllerShortcut.#buttonsStatus[gamepadIndex] = [];
const pressed: boolean[] = [];
let otherButtonPressed = false;
gamepad.buttons.forEach((button, index) => {
// Only add the newly pressed button to the array (holding doesn't count)
if (button.pressed && index !== GamepadKey.HOME) {
otherButtonPressed = true;
pressed[index] = true;
// If this is newly pressed button -> run action
if (actions[index] && !ControllerShortcut.#buttonsCache[gamepadIndex][index]) {
setTimeout(() => ControllerShortcut.#runAction(actions[index]!), 0);
}
}
});
ControllerShortcut.#buttonsStatus[gamepadIndex] = pressed;
return otherButtonPressed;
}
static #runAction(action: ShortcutAction) {
switch (action) {
case ShortcutAction.STREAM_SCREENSHOT_CAPTURE:
Screenshot.takeScreenshot();
break;
case ShortcutAction.STREAM_STATS_TOGGLE:
StreamStats.toggle();
break;
case ShortcutAction.STREAM_MICROPHONE_TOGGLE:
MicrophoneShortcut.toggle();
break;
case ShortcutAction.STREAM_MENU_SHOW:
StreamUiShortcut.showHideStreamMenu();
break;
case ShortcutAction.STREAM_SOUND_TOGGLE:
SoundShortcut.muteUnmute();
break;
case ShortcutAction.STREAM_VOLUME_INC:
SoundShortcut.adjustGainNodeVolume(10);
break;
case ShortcutAction.STREAM_VOLUME_DEC:
SoundShortcut.adjustGainNodeVolume(-10);
break;
case ShortcutAction.DEVICE_BRIGHTNESS_INC:
case ShortcutAction.DEVICE_BRIGHTNESS_DEC:
case ShortcutAction.DEVICE_SOUND_TOGGLE:
case ShortcutAction.DEVICE_VOLUME_INC:
case ShortcutAction.DEVICE_VOLUME_DEC:
AppInterface && AppInterface.runShortcut && AppInterface.runShortcut(action);
break;
}
}
static #updateAction(profile: string, button: GamepadKey, action: ShortcutAction | null) {
if (!(profile in ControllerShortcut.#ACTIONS)) {
ControllerShortcut.#ACTIONS[profile] = [];
}
if (!action) {
action = null;
}
ControllerShortcut.#ACTIONS[profile][button] = action;
// Remove empty profiles
for (const key in ControllerShortcut.#ACTIONS) {
let empty = true;
for (const value of ControllerShortcut.#ACTIONS[key]) {
if (!!value) {
empty = false;
break;
}
}
if (empty) {
delete ControllerShortcut.#ACTIONS[key];
}
}
// Save to storage
window.localStorage.setItem(ControllerShortcut.#STORAGE_KEY, JSON.stringify(ControllerShortcut.#ACTIONS));
console.log(ControllerShortcut.#ACTIONS);
}
static #updateProfileList(e?: GamepadEvent) {
const $select = ControllerShortcut.#$selectProfile;
const $container = ControllerShortcut.#$container;
const $fragment = document.createDocumentFragment();
// Remove old profiles
while ($select.firstElementChild) {
$select.firstElementChild.remove();
}
const gamepads = navigator.getGamepads();
let hasGamepad = false;
for (const gamepad of gamepads) {
if (!gamepad || !gamepad.connected) {
continue;
}
// Ignore emulated gamepad
if (gamepad.id === MkbHandler.VIRTUAL_GAMEPAD_ID) {
continue;
}
hasGamepad = true;
const $option = CE<HTMLOptionElement>('option', {value: gamepad.id}, gamepad.id);
$fragment.appendChild($option);
}
if (hasGamepad) {
$select.appendChild($fragment);
$select.selectedIndex = 0;
$select.dispatchEvent(new Event('change'));
}
$container.dataset.hasGamepad = hasGamepad.toString();
}
static #switchProfile(profile: string) {
let actions = ControllerShortcut.#ACTIONS[profile];
if (!actions) {
actions = [];
}
// Reset selects' values
let button: any;
for (button in ControllerShortcut.#$selectActions) {
const $select = ControllerShortcut.#$selectActions[button as GamepadKey]!;
$select.value = actions[button] || '';
BxEvent.dispatch($select, 'change', {
ignoreOnChange: true,
});
}
}
static renderSettings() {
// Read actions from localStorage
ControllerShortcut.#ACTIONS = JSON.parse(window.localStorage.getItem(ControllerShortcut.#STORAGE_KEY) || '{}');
const buttons: Map<GamepadKey, PrompFont> = new Map();
buttons.set(GamepadKey.Y, PrompFont.Y);
buttons.set(GamepadKey.A, PrompFont.A);
buttons.set(GamepadKey.B, PrompFont.B);
buttons.set(GamepadKey.X, PrompFont.X);
buttons.set(GamepadKey.UP, PrompFont.UP);
buttons.set(GamepadKey.DOWN, PrompFont.DOWN);
buttons.set(GamepadKey.LEFT, PrompFont.LEFT);
buttons.set(GamepadKey.RIGHT, PrompFont.RIGHT);
buttons.set(GamepadKey.SELECT, PrompFont.SELECT);
buttons.set(GamepadKey.START, PrompFont.START);
buttons.set(GamepadKey.LB, PrompFont.LB);
buttons.set(GamepadKey.RB, PrompFont.RB);
buttons.set(GamepadKey.LT, PrompFont.LT);
buttons.set(GamepadKey.RT, PrompFont.RT);
buttons.set(GamepadKey.L3, PrompFont.L3);
buttons.set(GamepadKey.R3, PrompFont.R3);
const actions: {[key: string]: Partial<{[key in ShortcutAction]: string | string[]}>} = {
[t('device')]: AppInterface && {
[ShortcutAction.DEVICE_SOUND_TOGGLE]: [t('sound'), t('toggle')],
[ShortcutAction.DEVICE_VOLUME_INC]: [t('volume'), t('increase')],
[ShortcutAction.DEVICE_VOLUME_DEC]: [t('volume'), t('decrease')],
[ShortcutAction.DEVICE_BRIGHTNESS_INC]: [t('brightness'), t('increase')],
[ShortcutAction.DEVICE_BRIGHTNESS_DEC]: [t('brightness'), t('decrease')],
},
[t('stream')]: {
[ShortcutAction.STREAM_SCREENSHOT_CAPTURE]: t('take-screenshot'),
[ShortcutAction.STREAM_SOUND_TOGGLE]: [t('sound'), t('toggle')],
[ShortcutAction.STREAM_VOLUME_INC]: getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && [t('volume'), t('increase')],
[ShortcutAction.STREAM_VOLUME_DEC]: getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && [t('volume'), t('decrease')],
[ShortcutAction.STREAM_MENU_SHOW]: [t('menu'), t('show')],
[ShortcutAction.STREAM_STATS_TOGGLE]: [t('stats'), t('show-hide')],
[ShortcutAction.STREAM_MICROPHONE_TOGGLE]: [t('microphone'), t('toggle')],
}
};
const $baseSelect = CE<HTMLSelectElement>('select', {autocomplete: 'off'}, CE('option', {value: ''}, '---'));
for (const groupLabel in actions) {
const items = actions[groupLabel];
if (!items) {
continue;
}
const $optGroup = CE<HTMLOptGroupElement>('optgroup', {'label': groupLabel});
for (const action in items) {
let label = items[action as keyof typeof items];
if (!label) {
continue;
}
if (Array.isArray(label)) {
label = label.join(' ');
}
const $option = CE<HTMLOptionElement>('option', {value: action}, label);
$optGroup.appendChild($option);
}
$baseSelect.appendChild($optGroup);
}
let $remap: HTMLElement;
let $selectProfile: HTMLSelectElement;
const $container = CE('div', {'data-has-gamepad': 'false'},
CE('div', {},
CE('p', {'class': 'bx-shortcut-note'}, t('controller-shortcuts-connect-note')),
),
$remap = CE('div', {},
$selectProfile = CE('select', {'class': 'bx-shortcut-profile', autocomplete: 'off'}),
CE('p', {'class': 'bx-shortcut-note'},
CE('span', {'class': 'bx-prompt'}, PrompFont.HOME),
': ' + t('controller-shortcuts-xbox-note'),
),
),
);
$selectProfile.addEventListener('change', e => {
ControllerShortcut.#switchProfile($selectProfile.value);
});
const onActionChanged = (e: Event) => {
const $target = e.target as HTMLSelectElement;
const profile = $selectProfile.value;
const button: unknown = $target.dataset.button;
const action = $target.value as ShortcutAction;
const $fakeSelect = $target.previousElementSibling! as HTMLSelectElement;
let fakeText = '---';
if (action) {
const $selectedOption = $target.options[$target.selectedIndex];
const $optGroup = $selectedOption.parentElement as HTMLOptGroupElement;
fakeText = $optGroup.label + ' ' + $selectedOption.text;
}
($fakeSelect.firstElementChild as HTMLOptionElement).text = fakeText;
!(e as any).ignoreOnChange && ControllerShortcut.#updateAction(profile, button as GamepadKey, action);
};
// @ts-ignore
for (const [button, prompt] of buttons) {
const $row = CE('div', {'class': 'bx-shortcut-row'});
const $label = CE('label', {'class': 'bx-prompt'}, `${PrompFont.HOME} + ${prompt}`);
const $div = CE('div', {'class': 'bx-shortcut-actions'});
const $fakeSelect = CE<HTMLSelectElement>('select', {autocomplete: 'off'},
CE('option', {}, '---'),
);
$div.appendChild($fakeSelect);
const $select = $baseSelect.cloneNode(true) as HTMLSelectElement;
$select.dataset.button = button.toString();
$select.addEventListener('change', onActionChanged);
ControllerShortcut.#$selectActions[button] = $select;
$div.appendChild($select);
$row.appendChild($label);
$row.appendChild($div);
$remap.appendChild($row);
}
$container.appendChild($remap);
ControllerShortcut.#$selectProfile = $selectProfile;
ControllerShortcut.#$container = $container;
// Detect when gamepad connected/disconnect
window.addEventListener('gamepadconnected', ControllerShortcut.#updateProfileList);
window.addEventListener('gamepaddisconnected', ControllerShortcut.#updateProfileList);
ControllerShortcut.#updateProfileList();
return $container;
}
}

View File

@ -55,7 +55,7 @@ export class Dialog {
}), }),
), ),
this.$content = CE('div', {'class': 'bx-dialog-content'}, content), this.$content = CE('div', {'class': 'bx-dialog-content'}, content),
!hideCloseButton && ($close = CE('button', {}, t('close'))), !hideCloseButton && ($close = CE('button', {type: 'button'}, t('close'))),
); );
$close && $close.addEventListener('click', e => { $close && $close.addEventListener('click', e => {

View File

@ -0,0 +1,6 @@
export abstract class BaseGameBarAction {
constructor() {}
reset() {}
abstract render(): HTMLElement;
}

View File

@ -0,0 +1,66 @@
import { BxEvent } from "@utils/bx-event";
import { BxIcon } from "@utils/bx-icon";
import { createButton, ButtonStyle, CE } from "@utils/html";
import { t } from "@utils/translation";
import { BaseGameBarAction } from "./action-base";
import { MicrophoneShortcut, MicrophoneState } from "../shortcuts/shortcut-microphone";
export class MicrophoneAction extends BaseGameBarAction {
$content: HTMLElement;
visible: boolean = false;
constructor() {
super();
const onClick = (e: Event) => {
BxEvent.dispatch(window, BxEvent.GAME_BAR_ACTION_ACTIVATED);
const enabled = MicrophoneShortcut.toggle(false);
this.$content.setAttribute('data-enabled', enabled.toString());
};
const $btnDefault = createButton({
style: ButtonStyle.GHOST,
icon: BxIcon.MICROPHONE,
title: t('show-touch-controller'),
onClick: onClick,
classes: ['bx-activated'],
});
const $btnMuted = createButton({
style: ButtonStyle.GHOST,
icon: BxIcon.MICROPHONE_MUTED,
title: t('hide-touch-controller'),
onClick: onClick,
});
this.$content = CE('div', {},
$btnDefault,
$btnMuted,
);
this.reset();
window.addEventListener(BxEvent.MICROPHONE_STATE_CHANGED, e => {
const microphoneState = (e as any).microphoneState;
const enabled = microphoneState === MicrophoneState.ENABLED;
this.$content.setAttribute('data-enabled', enabled.toString());
// Show the button in Game Bar if the mic is enabled
this.$content.classList.remove('bx-gone');
});
}
render(): HTMLElement {
return this.$content;
}
reset(): void {
this.visible = false;
this.$content.classList.add('bx-gone');
this.$content.setAttribute('data-enabled', 'false');
}
}

View File

@ -0,0 +1,30 @@
import { BxEvent } from "@utils/bx-event";
import { BxIcon } from "@utils/bx-icon";
import { createButton, ButtonStyle } from "@utils/html";
import { BaseGameBarAction } from "./action-base";
import { t } from "@utils/translation";
import { Screenshot } from "@/utils/screenshot";
export class ScreenshotAction extends BaseGameBarAction {
$content: HTMLElement;
constructor() {
super();
const onClick = (e: Event) => {
BxEvent.dispatch(window, BxEvent.GAME_BAR_ACTION_ACTIVATED);
Screenshot.takeScreenshot();
};
this.$content = createButton({
style: ButtonStyle.GHOST,
icon: BxIcon.SCREENSHOT,
title: t('take-screenshot'),
onClick: onClick,
});
}
render(): HTMLElement {
return this.$content;
}
}

View File

@ -0,0 +1,54 @@
import { BxEvent } from "@utils/bx-event";
import { BxIcon } from "@utils/bx-icon";
import { createButton, ButtonStyle, CE } from "@utils/html";
import { TouchController } from "@modules/touch-controller";
import { BaseGameBarAction } from "./action-base";
import { t } from "@utils/translation";
export class TouchControlAction extends BaseGameBarAction {
$content: HTMLElement;
constructor() {
super();
const onClick = (e: Event) => {
BxEvent.dispatch(window, BxEvent.GAME_BAR_ACTION_ACTIVATED);
const $parent = (e as any).target.closest('div[data-enabled]');
let enabled = $parent.getAttribute('data-enabled', 'true') === 'true';
$parent.setAttribute('data-enabled', (!enabled).toString());
TouchController.toggleVisibility(enabled);
};
const $btnEnable = createButton({
style: ButtonStyle.GHOST,
icon: BxIcon.TOUCH_CONTROL_ENABLE,
title: t('show-touch-controller'),
onClick: onClick,
classes: ['bx-activated'],
});
const $btnDisable = createButton({
style: ButtonStyle.GHOST,
icon: BxIcon.TOUCH_CONTROL_DISABLE,
title: t('hide-touch-controller'),
onClick: onClick,
});
this.$content = CE('div', {},
$btnEnable,
$btnDisable,
);
this.reset();
}
render(): HTMLElement {
return this.$content;
}
reset(): void {
this.$content.setAttribute('data-enabled', 'true');
}
}

View File

@ -0,0 +1,148 @@
import { CE, createSvgIcon } from "@utils/html";
import { ScreenshotAction } from "./action-screenshot";
import { TouchControlAction } from "./action-touch-control";
import { BxEvent } from "@utils/bx-event";
import { BxIcon } from "@utils/bx-icon";
import type { BaseGameBarAction } from "./action-base";
import { STATES } from "@utils/global";
import { PrefKey, getPref } from "@utils/preferences";
import { MicrophoneAction } from "./action-microphone";
export class GameBar {
private static instance: GameBar;
public static getInstance(): GameBar {
if (!GameBar.instance) {
GameBar.instance = new GameBar();
}
return GameBar.instance;
}
private static readonly VISIBLE_DURATION = 2000;
private $gameBar: HTMLElement;
private $container: HTMLElement;
private timeout: number | null = null;
private actions: BaseGameBarAction[] = [];
private constructor() {
let $container;
const position = getPref(PrefKey.GAME_BAR_POSITION);
const $gameBar = CE('div', {id: 'bx-game-bar', class: 'bx-gone', 'data-position': position},
$container = CE('div', {class: 'bx-game-bar-container bx-offscreen'}),
createSvgIcon(position === 'bottom-left' ? BxIcon.CARET_RIGHT : BxIcon.CARET_LEFT),
);
this.actions = [
new ScreenshotAction(),
...(STATES.hasTouchSupport && (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) !== 'off') ? [new TouchControlAction()] : []),
new MicrophoneAction(),
];
// Reverse the action list if Game Bar's position is on the right side
if (position === 'bottom-right') {
this.actions.reverse();
}
// Render actions
for (const action of this.actions) {
$container.appendChild(action.render());
}
// Toggle game bar when clicking on the game bar box
$gameBar.addEventListener('click', e => {
if (e.target !== $gameBar) {
return;
}
$container.classList.contains('bx-show') ? this.hideBar() : this.showBar();
});
// Hide game bar after clicking on an action
window.addEventListener(BxEvent.GAME_BAR_ACTION_ACTIVATED, this.hideBar.bind(this));
$container.addEventListener('pointerover', this.clearHideTimeout.bind(this));
$container.addEventListener('pointerout', this.beginHideTimeout.bind(this));
// Add animation when hiding game bar
$container.addEventListener('transitionend', e => {
const classList = $container.classList;
if (classList.contains('bx-hide')) {
classList.remove('bx-offscreen', 'bx-hide');
classList.add('bx-offscreen');
}
});
document.documentElement.appendChild($gameBar);
this.$gameBar = $gameBar;
this.$container = $container;
// Enable/disable Game Bar when playing/pausing
getPref(PrefKey.GAME_BAR_POSITION) !== 'off' && window.addEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, ((e: Event) => {
if (!STATES.isPlaying) {
this.disable();
return;
}
// Toggle Game bar
const mode = (e as any).mode;
mode !== 'None' ? this.disable() : this.enable();
}).bind(this));
}
private beginHideTimeout() {
this.clearHideTimeout();
this.timeout = window.setTimeout(() => {
this.timeout = null;
this.hideBar();
}, GameBar.VISIBLE_DURATION);
}
private clearHideTimeout() {
this.timeout && clearTimeout(this.timeout);
this.timeout = null;
}
enable() {
this.$gameBar && this.$gameBar.classList.remove('bx-gone');
}
disable() {
this.hideBar();
this.$gameBar && this.$gameBar.classList.add('bx-gone');
}
showBar() {
if (!this.$container) {
return;
}
this.$container.classList.remove('bx-offscreen', 'bx-hide');
this.$container.classList.add('bx-show');
this.beginHideTimeout();
}
hideBar() {
if (!this.$container) {
return;
}
this.$container.classList.remove('bx-show');
this.$container.classList.add('bx-hide');
}
// Reset all states
reset() {
for (const action of this.actions) {
action.reset();
}
}
}

View File

@ -46,6 +46,10 @@ export class LoadingScreen {
#game-stream div[class*=RocketAnimation-module__container] > svg { #game-stream div[class*=RocketAnimation-module__container] > svg {
display: none; display: none;
} }
#game-stream video[class*=RocketAnimationVideo-module__video] {
display: none;
}
`; `;
$bgStyle.textContent += css; $bgStyle.textContent += css;
} }
@ -163,9 +167,9 @@ export class LoadingScreen {
} }
static reset() { static reset() {
LoadingScreen.#$waitTimeBox && LoadingScreen.#$waitTimeBox.classList.add('bx-gone'); LoadingScreen.#$bgStyle && setTimeout(() => LoadingScreen.#$bgStyle.textContent = '', 2000);
LoadingScreen.#$bgStyle && (LoadingScreen.#$bgStyle.textContent = '');
LoadingScreen.#$waitTimeBox && LoadingScreen.#$waitTimeBox.classList.add('bx-gone');
LoadingScreen.#waitTimeInterval && clearInterval(LoadingScreen.#waitTimeInterval); LoadingScreen.#waitTimeInterval && clearInterval(LoadingScreen.#waitTimeInterval);
LoadingScreen.#waitTimeInterval = null; LoadingScreen.#waitTimeInterval = null;
} }

View File

@ -1,4 +1,5 @@
import type { GamepadKeyNameType } from "@/types/mkb"; import type { GamepadKeyNameType } from "@/types/mkb";
import { PrompFont } from "@/utils/prompt-font";
export enum GamepadKey { export enum GamepadKey {
A = 0, A = 0,
@ -18,6 +19,7 @@ export enum GamepadKey {
LEFT = 14, LEFT = 14,
RIGHT = 15, RIGHT = 15,
HOME = 16, HOME = 16,
SHARE = 17,
LS_UP = 100, LS_UP = 100,
LS_DOWN = 101, LS_DOWN = 101,
@ -32,36 +34,36 @@ export enum GamepadKey {
export const GamepadKeyName: GamepadKeyNameType = { export const GamepadKeyName: GamepadKeyNameType = {
[GamepadKey.A]: ['A', '⇓'], [GamepadKey.A]: ['A', PrompFont.A],
[GamepadKey.B]: ['B', '⇒'], [GamepadKey.B]: ['B', PrompFont.B],
[GamepadKey.X]: ['X', '⇐'], [GamepadKey.X]: ['X', PrompFont.X],
[GamepadKey.Y]: ['Y', '⇑'], [GamepadKey.Y]: ['Y', PrompFont.Y],
[GamepadKey.LB]: ['LB', '↘'], [GamepadKey.LB]: ['LB', PrompFont.LB],
[GamepadKey.RB]: ['RB', '↙'], [GamepadKey.RB]: ['RB', PrompFont.RB],
[GamepadKey.LT]: ['LT', '↖'], [GamepadKey.LT]: ['LT', PrompFont.LT],
[GamepadKey.RT]: ['RT', '↗'], [GamepadKey.RT]: ['RT', PrompFont.RT],
[GamepadKey.SELECT]: ['Select', '⇺'], [GamepadKey.SELECT]: ['Select', PrompFont.SELECT],
[GamepadKey.START]: ['Start', '⇻'], [GamepadKey.START]: ['Start', PrompFont.START],
[GamepadKey.HOME]: ['Home', ''], [GamepadKey.HOME]: ['Home', PrompFont.HOME],
[GamepadKey.UP]: ['D-Pad Up', '≻'], [GamepadKey.UP]: ['D-Pad Up', PrompFont.UP],
[GamepadKey.DOWN]: ['D-Pad Down', '≽'], [GamepadKey.DOWN]: ['D-Pad Down', PrompFont.DOWN],
[GamepadKey.LEFT]: ['D-Pad Left', '≺'], [GamepadKey.LEFT]: ['D-Pad Left', PrompFont.LEFT],
[GamepadKey.RIGHT]: ['D-Pad Right', '≼'], [GamepadKey.RIGHT]: ['D-Pad Right', PrompFont.RIGHT],
[GamepadKey.L3]: ['L3', '↺'], [GamepadKey.L3]: ['L3', PrompFont.L3],
[GamepadKey.LS_UP]: ['Left Stick Up', '↾'], [GamepadKey.LS_UP]: ['Left Stick Up', PrompFont.LS_UP],
[GamepadKey.LS_DOWN]: ['Left Stick Down', '⇂'], [GamepadKey.LS_DOWN]: ['Left Stick Down', PrompFont.LS_DOWN],
[GamepadKey.LS_LEFT]: ['Left Stick Left', '↼'], [GamepadKey.LS_LEFT]: ['Left Stick Left', PrompFont.LS_LEFT],
[GamepadKey.LS_RIGHT]: ['Left Stick Right', '⇀'], [GamepadKey.LS_RIGHT]: ['Left Stick Right', PrompFont.LS_RIGHT],
[GamepadKey.R3]: ['R3', '↻'], [GamepadKey.R3]: ['R3', PrompFont.R3],
[GamepadKey.RS_UP]: ['Right Stick Up', '↿'], [GamepadKey.RS_UP]: ['Right Stick Up', PrompFont.RS_UP],
[GamepadKey.RS_DOWN]: ['Right Stick Down', '⇃'], [GamepadKey.RS_DOWN]: ['Right Stick Down', PrompFont.RS_DOWN],
[GamepadKey.RS_LEFT]: ['Right Stick Left', '↽'], [GamepadKey.RS_LEFT]: ['Right Stick Left', PrompFont.RS_LEFT],
[GamepadKey.RS_RIGHT]: ['Right Stick Right', '⇁'], [GamepadKey.RS_RIGHT]: ['Right Stick Right', PrompFont.RS_RIGHT],
}; };
@ -97,7 +99,4 @@ export enum MkbPresetKey {
MOUSE_SENSITIVITY_Y = 'sensitivity_y', MOUSE_SENSITIVITY_Y = 'sensitivity_y',
MOUSE_DEADZONE_COUNTERWEIGHT = 'deadzone_counterweight', MOUSE_DEADZONE_COUNTERWEIGHT = 'deadzone_counterweight',
MOUSE_STICK_DECAY_STRENGTH = 'stick_decay_strength',
MOUSE_STICK_DECAY_MIN = 'stick_decay_min',
} }

View File

@ -20,7 +20,7 @@ export class KeyHelper {
let name; let name;
if (e instanceof KeyboardEvent) { if (e instanceof KeyboardEvent) {
code = e.code; code = e.code || e.key;
} else if (e instanceof WheelEvent) { } else if (e instanceof WheelEvent) {
if (e.deltaY < 0) { if (e.deltaY < 0) {
code = WheelCode.SCROLL_UP; code = WheelCode.SCROLL_UP;
@ -28,7 +28,7 @@ export class KeyHelper {
code = WheelCode.SCROLL_DOWN; code = WheelCode.SCROLL_DOWN;
} else if (e.deltaX < 0) { } else if (e.deltaX < 0) {
code = WheelCode.SCROLL_LEFT; code = WheelCode.SCROLL_LEFT;
} else { } else if (e.deltaX > 0) {
code = WheelCode.SCROLL_RIGHT; code = WheelCode.SCROLL_RIGHT;
} }
} else if (e instanceof MouseEvent) { } else if (e instanceof MouseEvent) {

View File

@ -9,13 +9,152 @@ import { LocalDb } from "@utils/local-db";
import { KeyHelper } from "./key-helper"; import { KeyHelper } from "./key-helper";
import type { MkbStoredPreset } from "@/types/mkb"; import type { MkbStoredPreset } from "@/types/mkb";
import { showStreamSettings } from "@modules/stream/stream-ui"; import { showStreamSettings } from "@modules/stream/stream-ui";
import { STATES } from "@utils/global"; import { AppInterface, STATES } from "@utils/global";
import { UserAgent } from "@utils/user-agent"; import { UserAgent } from "@utils/user-agent";
import { BxLogger } from "@utils/bx-logger"; import { BxLogger } from "@utils/bx-logger";
import { BxIcon } from "@utils/bx-icon"; import { BxIcon } from "@utils/bx-icon";
import { PointerClient } from "./pointer-client";
const LOG_TAG = 'MkbHandler'; const LOG_TAG = 'MkbHandler';
abstract class MouseDataProvider {
protected mkbHandler: MkbHandler;
constructor(handler: MkbHandler) {
this.mkbHandler = handler;
}
abstract init(): void;
abstract start(): void;
abstract stop(): void;
abstract destroy(): void;
abstract toggle(enabled: boolean): void;
}
class WebSocketMouseDataProvider extends MouseDataProvider {
#pointerClient: PointerClient | undefined
#connected = false
init(): void {
this.#pointerClient = PointerClient.getInstance();
this.#connected = false;
try {
this.#pointerClient.start(this.mkbHandler);
this.#connected = true;
} catch (e) {
Toast.show('Cannot enable Mouse & Keyboard feature');
}
}
start(): void {
this.#connected && AppInterface.requestPointerCapture();
}
stop(): void {
this.#connected && AppInterface.releasePointerCapture();
}
destroy(): void {
this.#connected && this.#pointerClient?.stop();
}
toggle(enabled: boolean): void {
if (!this.#connected) {
enabled = false;
}
enabled ? this.mkbHandler.start() : this.mkbHandler.stop();
this.mkbHandler.waitForMouseData(!enabled);
}
}
class PointerLockMouseDataProvider extends MouseDataProvider {
init(): void {
document.addEventListener('pointerlockchange', this.#onPointerLockChange);
document.addEventListener('pointerlockerror', this.#onPointerLockError);
}
start(): void {
if (!document.pointerLockElement) {
document.body.requestPointerLock();
}
window.addEventListener('mousemove', this.#onMouseMoveEvent);
window.addEventListener('mousedown', this.#onMouseEvent);
window.addEventListener('mouseup', this.#onMouseEvent);
window.addEventListener('wheel', this.#onWheelEvent);
window.addEventListener('contextmenu', this.#disableContextMenu);
}
stop(): void {
window.removeEventListener('mousemove', this.#onMouseMoveEvent);
window.removeEventListener('mousedown', this.#onMouseEvent);
window.removeEventListener('mouseup', this.#onMouseEvent);
window.removeEventListener('wheel', this.#onWheelEvent);
window.removeEventListener('contextmenu', this.#disableContextMenu);
}
destroy(): void {
document.removeEventListener('pointerlockchange', this.#onPointerLockChange);
document.removeEventListener('pointerlockerror', this.#onPointerLockError);
}
toggle(enabled: boolean): void {
enabled ? document.pointerLockElement && this.mkbHandler.start() : this.mkbHandler.stop();
if (enabled) {
!document.pointerLockElement && this.mkbHandler.waitForMouseData(true);
} else {
this.mkbHandler.waitForMouseData(false);
document.pointerLockElement && document.exitPointerLock();
}
}
#onPointerLockChange = () => {
if (this.mkbHandler.isEnabled() && !document.pointerLockElement) {
this.mkbHandler.stop();
}
}
#onPointerLockError = (e: Event) => {
console.log(e);
this.stop();
}
#onMouseMoveEvent = (e: MouseEvent) => {
this.mkbHandler.handleMouseMove({
movementX: e.movementX,
movementY: e.movementY,
});
}
#onMouseEvent = (e: MouseEvent) => {
e.preventDefault();
const isMouseDown = e.type === 'mousedown';
const key = KeyHelper.getKeyFromEvent(e);
const data: MkbMouseClick = {
key: key,
pressed: isMouseDown
};
this.mkbHandler.handleMouseClick(data);
}
#onWheelEvent = (e: WheelEvent) => {
const key = KeyHelper.getKeyFromEvent(e);
if (!key) {
return;
}
if (this.mkbHandler.handleMouseWheel({key})) {
e.preventDefault();
}
}
#disableContextMenu = (e: Event) => e.preventDefault();
}
/* /*
This class uses some code from Yuzu emulator to handle mouse's movements This class uses some code from Yuzu emulator to handle mouse's movements
Source: https://github.com/yuzu-emu/yuzu-mainline/blob/master/src/input_common/drivers/mouse.cpp Source: https://github.com/yuzu-emu/yuzu-mainline/blob/master/src/input_common/drivers/mouse.cpp
@ -33,7 +172,6 @@ export class MkbHandler {
#CURRENT_PRESET_DATA = MkbPreset.convert(MkbPreset.DEFAULT_PRESET); #CURRENT_PRESET_DATA = MkbPreset.convert(MkbPreset.DEFAULT_PRESET);
static readonly DEFAULT_PANNING_SENSITIVITY = 0.0010; static readonly DEFAULT_PANNING_SENSITIVITY = 0.0010;
static readonly DEFAULT_STICK_SENSITIVITY = 0.0006;
static readonly DEFAULT_DEADZONE_COUNTERWEIGHT = 0.01; static readonly DEFAULT_DEADZONE_COUNTERWEIGHT = 0.01;
static readonly MAXIMUM_STICK_RANGE = 1.1; static readonly MAXIMUM_STICK_RANGE = 1.1;
@ -55,13 +193,13 @@ export class MkbHandler {
#nativeGetGamepads = window.navigator.getGamepads.bind(window.navigator); #nativeGetGamepads = window.navigator.getGamepads.bind(window.navigator);
#enabled = false; #enabled = false;
#mouseDataProvider: MouseDataProvider | undefined;
#isPolling = false; #isPolling = false;
#prevWheelCode = null; #prevWheelCode = null;
#wheelStoppedTimeout?: number | null; #wheelStoppedTimeout?: number | null;
#detectMouseStoppedTimeout?: number | null; #detectMouseStoppedTimeout?: number | null;
#allowStickDecaying = false;
#$message?: HTMLElement; #$message?: HTMLElement;
@ -85,6 +223,8 @@ export class MkbHandler {
}; };
} }
isEnabled = () => this.#enabled;
#patchedGetGamepads = () => { #patchedGetGamepads = () => {
const gamepads = this.#nativeGetGamepads() || []; const gamepads = this.#nativeGetGamepads() || [];
(gamepads as any)[this.#VIRTUAL_GAMEPAD.index] = this.#VIRTUAL_GAMEPAD; (gamepads as any)[this.#VIRTUAL_GAMEPAD.index] = this.#VIRTUAL_GAMEPAD;
@ -102,6 +242,7 @@ export class MkbHandler {
virtualGamepad.timestamp = performance.now(); virtualGamepad.timestamp = performance.now();
} }
/*
#getStickAxes(stick: GamepadStick) { #getStickAxes(stick: GamepadStick) {
const virtualGamepad = this.#getVirtualGamepad(); const virtualGamepad = this.#getVirtualGamepad();
return { return {
@ -109,11 +250,10 @@ export class MkbHandler {
y: virtualGamepad.axes[stick * 2 + 1], y: virtualGamepad.axes[stick * 2 + 1],
}; };
} }
*/
#vectorLength = (x: number, y: number): number => Math.sqrt(x ** 2 + y ** 2); #vectorLength = (x: number, y: number): number => Math.sqrt(x ** 2 + y ** 2);
#disableContextMenu = (e: Event) => e.preventDefault();
#resetGamepad = () => { #resetGamepad = () => {
const gamepad = this.#getVirtualGamepad(); const gamepad = this.#getVirtualGamepad();
@ -172,6 +312,10 @@ export class MkbHandler {
e.preventDefault(); e.preventDefault();
this.toggle(); this.toggle();
return; return;
} else if (e.code === 'Escape') {
e.preventDefault();
this.#enabled && this.stop();
return;
} }
if (!this.#isPolling) { if (!this.#isPolling) {
@ -179,7 +323,7 @@ export class MkbHandler {
} }
} }
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[e.code]!; const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[e.code || e.key]!;
if (typeof buttonIndex === 'undefined') { if (typeof buttonIndex === 'undefined') {
return; return;
} }
@ -193,89 +337,29 @@ export class MkbHandler {
this.#pressButton(buttonIndex, isKeyDown); this.#pressButton(buttonIndex, isKeyDown);
} }
#onMouseEvent = (e: MouseEvent) => { #onMouseStopped = () => {
const isMouseDown = e.type === 'mousedown'; // Reset stick position
const key = KeyHelper.getKeyFromEvent(e); this.#detectMouseStoppedTimeout = null;
if (!key) {
return;
}
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[key.code]!;
if (typeof buttonIndex === 'undefined') {
return;
}
e.preventDefault();
this.#pressButton(buttonIndex, isMouseDown);
}
#onWheelEvent = (e: WheelEvent) => {
const key = KeyHelper.getKeyFromEvent(e);
if (!key) {
return;
}
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[key.code]!;
if (typeof buttonIndex === 'undefined') {
return;
}
e.preventDefault();
if (this.#prevWheelCode === null || this.#prevWheelCode === key.code) {
this.#wheelStoppedTimeout && clearTimeout(this.#wheelStoppedTimeout);
this.#pressButton(buttonIndex, true);
}
this.#wheelStoppedTimeout = window.setTimeout(() => {
this.#prevWheelCode = null;
this.#pressButton(buttonIndex, false);
}, 20);
}
#decayStick = () => {
if (!this.#allowStickDecaying) {
return;
}
const mouseMapTo = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_MAP_TO]; const mouseMapTo = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_MAP_TO];
if (mouseMapTo === MouseMapTo.OFF) { const analog = mouseMapTo === MouseMapTo.LS ? GamepadStick.LEFT : GamepadStick.RIGHT;
this.#updateStick(analog, 0, 0);
}
handleMouseClick = (data: MkbMouseClick) => {
if (!data || !data.key) {
return; return;
} }
const analog = mouseMapTo === MouseMapTo.LS ? GamepadStick.LEFT : GamepadStick.RIGHT; const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[data.key.code]!;
if (typeof buttonIndex === 'undefined') {
let { x, y } = this.#getStickAxes(analog); return;
const length = this.#vectorLength(x, y);
const clampedLength = Math.min(1.0, length);
const decayStrength = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_STICK_DECAY_STRENGTH];
const decay = 1 - clampedLength * clampedLength * decayStrength;
const minDecay = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_STICK_DECAY_MIN];
const clampedDecay = Math.min(1 - minDecay, decay);
x *= clampedDecay;
y *= clampedDecay;
const deadzoneCounterweight = 20 * MkbHandler.DEFAULT_DEADZONE_COUNTERWEIGHT;
if (Math.abs(x) <= deadzoneCounterweight && Math.abs(y) <= deadzoneCounterweight) {
x = 0;
y = 0;
} }
if (this.#allowStickDecaying) { this.#pressButton(buttonIndex, data.pressed);
this.#updateStick(analog, x, y);
(x !== 0 || y !== 0) && requestAnimationFrame(this.#decayStick);
}
} }
#onMouseStopped = () => { handleMouseMove = (data: MkbMouseMove) => {
this.#allowStickDecaying = true;
requestAnimationFrame(this.#decayStick);
}
#onMouseMoveEvent = (e: MouseEvent) => {
// TODO: optimize this // TODO: optimize this
const mouseMapTo = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_MAP_TO]; const mouseMapTo = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_MAP_TO];
if (mouseMapTo === MouseMapTo.OFF) { if (mouseMapTo === MouseMapTo.OFF) {
@ -283,17 +367,13 @@ export class MkbHandler {
return; return;
} }
this.#allowStickDecaying = false;
this.#detectMouseStoppedTimeout && clearTimeout(this.#detectMouseStoppedTimeout); this.#detectMouseStoppedTimeout && clearTimeout(this.#detectMouseStoppedTimeout);
this.#detectMouseStoppedTimeout = window.setTimeout(this.#onMouseStopped.bind(this), 100); this.#detectMouseStoppedTimeout = window.setTimeout(this.#onMouseStopped.bind(this), 50);
const deltaX = e.movementX;
const deltaY = e.movementY;
const deadzoneCounterweight = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT]; const deadzoneCounterweight = this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT];
let x = deltaX * this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_SENSITIVITY_X]; let x = data.movementX * this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_SENSITIVITY_X];
let y = deltaY * this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_SENSITIVITY_Y]; let y = data.movementY * this.#CURRENT_PRESET_DATA.mouse[MkbPresetKey.MOUSE_SENSITIVITY_Y];
let length = this.#vectorLength(x, y); let length = this.#vectorLength(x, y);
if (length !== 0 && length < deadzoneCounterweight) { if (length !== 0 && length < deadzoneCounterweight) {
@ -308,18 +388,33 @@ export class MkbHandler {
this.#updateStick(analog, x, y); this.#updateStick(analog, x, y);
} }
handleMouseWheel = (data: MkbMouseWheel): boolean => {
if (!data || !data.key) {
return false;
}
const buttonIndex = this.#CURRENT_PRESET_DATA.mapping[data.key.code]!;
if (typeof buttonIndex === 'undefined') {
return false;
}
if (this.#prevWheelCode === null || this.#prevWheelCode === data.key.code) {
this.#wheelStoppedTimeout && clearTimeout(this.#wheelStoppedTimeout);
this.#pressButton(buttonIndex, true);
}
this.#wheelStoppedTimeout = window.setTimeout(() => {
this.#prevWheelCode = null;
this.#pressButton(buttonIndex, false);
}, 20);
return true;
}
toggle = () => { toggle = () => {
this.#enabled = !this.#enabled; this.#enabled = !this.#enabled;
this.#enabled ? document.pointerLockElement && this.start() : this.stop();
Toast.show(t('mouse-and-keyboard'), t(this.#enabled ? 'enabled' : 'disabled'), {instant: true}); Toast.show(t('mouse-and-keyboard'), t(this.#enabled ? 'enabled' : 'disabled'), {instant: true});
this.#mouseDataProvider?.toggle(this.#enabled);
if (this.#enabled) {
!document.pointerLockElement && this.#waitForPointerLock(true);
} else {
this.#waitForPointerLock(false);
document.pointerLockElement && document.exitPointerLock();
}
} }
#getCurrentPreset = (): Promise<MkbStoredPreset> => { #getCurrentPreset = (): Promise<MkbStoredPreset> => {
@ -338,51 +433,46 @@ export class MkbHandler {
}); });
} }
#onPointerLockChange = () => { waitForMouseData = (wait: boolean) => {
if (this.#enabled && !document.pointerLockElement) {
this.stop();
this.#waitForPointerLock(true);
}
}
#onPointerLockError = (e: Event) => {
console.log(e);
this.stop();
}
#onActivatePointerLock = () => {
if (!document.pointerLockElement) {
document.body.requestPointerLock();
}
this.#waitForPointerLock(false);
this.start();
}
#waitForPointerLock = (wait: boolean) => {
this.#$message && this.#$message.classList.toggle('bx-gone', !wait); this.#$message && this.#$message.classList.toggle('bx-gone', !wait);
} }
#onStreamMenuShown = () => { #onPollingModeChanged = (e: Event) => {
this.#enabled && this.#waitForPointerLock(false); if (!this.#$message) {
return;
} }
#onStreamMenuHidden = () => { const mode = (e as any).mode;
this.#enabled && this.#waitForPointerLock(true); if (mode === 'None') {
this.#$message.classList.remove('bx-offscreen');
} else {
this.#$message.classList.add('bx-offscreen');
}
} }
init = () => { init = () => {
this.refreshPresetData(); this.refreshPresetData();
this.#enabled = true; this.#enabled = true;
if (AppInterface) {
this.#mouseDataProvider = new WebSocketMouseDataProvider(this);
} else {
this.#mouseDataProvider = new PointerLockMouseDataProvider(this);
}
this.#mouseDataProvider.init();
window.addEventListener('keydown', this.#onKeyboardEvent); window.addEventListener('keydown', this.#onKeyboardEvent);
document.addEventListener('pointerlockchange', this.#onPointerLockChange);
document.addEventListener('pointerlockerror', this.#onPointerLockError);
this.#$message = CE('div', {'class': 'bx-mkb-pointer-lock-msg bx-gone'}, this.#$message = CE('div', {'class': 'bx-mkb-pointer-lock-msg bx-gone'},
CE('div', {},
CE('p', {}, t('mkb-click-to-activate')),
CE('p', {}, t('press-key-to-toggle-mkb', {key: 'F8'})),
),
CE('div', {},
createButton({ createButton({
icon: BxIcon.MOUSE_SETTINGS, icon: BxIcon.MOUSE_SETTINGS,
label: t('edit'),
style: ButtonStyle.PRIMARY, style: ButtonStyle.PRIMARY,
onClick: e => { onClick: e => {
e.preventDefault(); e.preventDefault();
@ -391,19 +481,25 @@ export class MkbHandler {
showStreamSettings('mkb'); showStreamSettings('mkb');
}, },
}), }),
CE('div', {},
CE('p', {}, t('mkb-click-to-activate')), createButton({
CE('p', {}, t<any>('press-key-to-toggle-mkb')({key: 'F8'})), label: t('disable'),
onClick: e => {
e.preventDefault();
e.stopPropagation();
this.toggle();
},
}),
), ),
); );
this.#$message.addEventListener('click', this.#onActivatePointerLock); this.#$message.addEventListener('click', this.start.bind(this));
document.documentElement.appendChild(this.#$message); document.documentElement.appendChild(this.#$message);
window.addEventListener(BxEvent.STREAM_MENU_SHOWN, this.#onStreamMenuShown); window.addEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.#onPollingModeChanged);
window.addEventListener(BxEvent.STREAM_MENU_HIDDEN, this.#onStreamMenuHidden);
this.#waitForPointerLock(true); this.waitForMouseData(true);
} }
destroy = () => { destroy = () => {
@ -411,31 +507,31 @@ export class MkbHandler {
this.#enabled = false; this.#enabled = false;
this.stop(); this.stop();
this.#waitForPointerLock(false); this.waitForMouseData(false);
document.pointerLockElement && document.exitPointerLock(); document.pointerLockElement && document.exitPointerLock();
window.removeEventListener('keydown', this.#onKeyboardEvent); window.removeEventListener('keydown', this.#onKeyboardEvent);
document.removeEventListener('pointerlockchange', this.#onPointerLockChange); this.#mouseDataProvider?.destroy();
document.removeEventListener('pointerlockerror', this.#onPointerLockError);
window.removeEventListener(BxEvent.STREAM_MENU_SHOWN, this.#onStreamMenuShown); window.removeEventListener(BxEvent.XCLOUD_POLLING_MODE_CHANGED, this.#onPollingModeChanged);
window.removeEventListener(BxEvent.STREAM_MENU_HIDDEN, this.#onStreamMenuHidden);
} }
start = () => { start = () => {
if (!this.#enabled) {
this.#enabled = true;
Toast.show(t('mouse-and-keyboard'), t('enabled'), {instant: true});
}
this.#isPolling = true; this.#isPolling = true;
window.navigator.getGamepads = this.#patchedGetGamepads;
this.#resetGamepad(); this.#resetGamepad();
window.navigator.getGamepads = this.#patchedGetGamepads;
this.waitForMouseData(false);
window.addEventListener('keyup', this.#onKeyboardEvent); window.addEventListener('keyup', this.#onKeyboardEvent);
this.#mouseDataProvider?.start();
window.addEventListener('mousemove', this.#onMouseMoveEvent);
window.addEventListener('mousedown', this.#onMouseEvent);
window.addEventListener('mouseup', this.#onMouseEvent);
window.addEventListener('wheel', this.#onWheelEvent);
window.addEventListener('contextmenu', this.#disableContextMenu);
// Dispatch "gamepadconnected" event // Dispatch "gamepadconnected" event
const virtualGamepad = this.#getVirtualGamepad(); const virtualGamepad = this.#getVirtualGamepad();
@ -451,6 +547,8 @@ export class MkbHandler {
this.#isPolling = false; this.#isPolling = false;
// Dispatch "gamepaddisconnected" event // Dispatch "gamepaddisconnected" event
this.#resetGamepad();
const virtualGamepad = this.#getVirtualGamepad(); const virtualGamepad = this.#getVirtualGamepad();
virtualGamepad.connected = false; virtualGamepad.connected = false;
virtualGamepad.timestamp = performance.now(); virtualGamepad.timestamp = performance.now();
@ -461,19 +559,14 @@ export class MkbHandler {
window.navigator.getGamepads = this.#nativeGetGamepads; window.navigator.getGamepads = this.#nativeGetGamepads;
this.#resetGamepad();
window.removeEventListener('keyup', this.#onKeyboardEvent); window.removeEventListener('keyup', this.#onKeyboardEvent);
window.removeEventListener('mousemove', this.#onMouseMoveEvent); this.waitForMouseData(true);
window.removeEventListener('mousedown', this.#onMouseEvent); this.#mouseDataProvider?.stop();
window.removeEventListener('mouseup', this.#onMouseEvent);
window.removeEventListener('wheel', this.#onWheelEvent);
window.removeEventListener('contextmenu', this.#disableContextMenu);
} }
static setupEvents() { static setupEvents() {
getPref(PrefKey.MKB_ENABLED) && !UserAgent.isMobile() && window.addEventListener(BxEvent.STREAM_PLAYING, () => { getPref(PrefKey.MKB_ENABLED) && (AppInterface || !UserAgent.isMobile()) && window.addEventListener(BxEvent.STREAM_PLAYING, () => {
// Enable MKB // Enable MKB
if (!STATES.currentStream.titleInfo?.details.hasMkbSupport) { if (!STATES.currentStream.titleInfo?.details.hasMkbSupport) {
BxLogger.info(LOG_TAG, 'Emulate MKB'); BxLogger.info(LOG_TAG, 'Emulate MKB');

View File

@ -24,11 +24,11 @@ export class MkbPreset {
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 50, default: 50,
min: 1, min: 1,
max: 200, max: 300,
params: { params: {
suffix: '%', suffix: '%',
exactTicks: 20, exactTicks: 50,
}, },
}, },
@ -37,11 +37,11 @@ export class MkbPreset {
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 50, default: 50,
min: 1, min: 1,
max: 200, max: 300,
params: { params: {
suffix: '%', suffix: '%',
exactTicks: 20, exactTicks: 50,
}, },
}, },
@ -50,38 +50,13 @@ export class MkbPreset {
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 20, default: 20,
min: 1, min: 1,
max: 100, max: 50,
params: { params: {
suffix: '%', suffix: '%',
exactTicks: 10, exactTicks: 10,
}, },
}, },
[MkbPresetKey.MOUSE_STICK_DECAY_STRENGTH]: {
label: t('stick-decay-strength'),
type: SettingElementType.NUMBER_STEPPER,
default: 100,
min: 10,
max: 100,
params: {
suffix: '%',
exactTicks: 10,
},
},
[MkbPresetKey.MOUSE_STICK_DECAY_MIN]: {
label: t('stick-decay-minimum'),
type: SettingElementType.NUMBER_STEPPER,
default: 10,
min: 1,
max: 10,
params: {
suffix: '%',
},
},
}; };
static DEFAULT_PRESET: MkbPresetData = { static DEFAULT_PRESET: MkbPresetData = {
@ -124,11 +99,9 @@ export class MkbPreset {
'mouse': { 'mouse': {
[MkbPresetKey.MOUSE_MAP_TO]: MouseMapTo[MouseMapTo.RS], [MkbPresetKey.MOUSE_MAP_TO]: MouseMapTo[MouseMapTo.RS],
[MkbPresetKey.MOUSE_SENSITIVITY_X]: 50, [MkbPresetKey.MOUSE_SENSITIVITY_X]: 100,
[MkbPresetKey.MOUSE_SENSITIVITY_Y]: 50, [MkbPresetKey.MOUSE_SENSITIVITY_Y]: 100,
[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT]: 20, [MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT]: 20,
[MkbPresetKey.MOUSE_STICK_DECAY_STRENGTH]: 18,
[MkbPresetKey.MOUSE_STICK_DECAY_MIN]: 6,
}, },
}; };
@ -149,8 +122,6 @@ export class MkbPreset {
mouse[MkbPresetKey.MOUSE_SENSITIVITY_X] *= MkbHandler.DEFAULT_PANNING_SENSITIVITY; mouse[MkbPresetKey.MOUSE_SENSITIVITY_X] *= MkbHandler.DEFAULT_PANNING_SENSITIVITY;
mouse[MkbPresetKey.MOUSE_SENSITIVITY_Y] *= MkbHandler.DEFAULT_PANNING_SENSITIVITY; mouse[MkbPresetKey.MOUSE_SENSITIVITY_Y] *= MkbHandler.DEFAULT_PANNING_SENSITIVITY;
mouse[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT] *= MkbHandler.DEFAULT_DEADZONE_COUNTERWEIGHT; mouse[MkbPresetKey.MOUSE_DEADZONE_COUNTERWEIGHT] *= MkbHandler.DEFAULT_DEADZONE_COUNTERWEIGHT;
mouse[MkbPresetKey.MOUSE_STICK_DECAY_STRENGTH] *= 0.01;
mouse[MkbPresetKey.MOUSE_STICK_DECAY_MIN] *= 0.01;
const mouseMapTo = MouseMapTo[mouse[MkbPresetKey.MOUSE_MAP_TO]!]; const mouseMapTo = MouseMapTo[mouse[MkbPresetKey.MOUSE_MAP_TO]!];
if (typeof mouseMapTo !== 'undefined') { if (typeof mouseMapTo !== 'undefined') {

View File

@ -426,6 +426,7 @@ export class MkbRemapper {
const $fragment = document.createDocumentFragment(); const $fragment = document.createDocumentFragment();
for (let i = 0; i < keysPerButton; i++) { for (let i = 0; i < keysPerButton; i++) {
$elm = CE('button', { $elm = CE('button', {
type: 'button',
'data-prompt': buttonPrompt, 'data-prompt': buttonPrompt,
'data-button-index': buttonIndex, 'data-button-index': buttonIndex,
'data-key-slot': i, 'data-key-slot': i,
@ -459,7 +460,7 @@ export class MkbRemapper {
const onChange = (e: Event, value: any) => { const onChange = (e: Event, value: any) => {
(this.#STATE.editingPresetData!.mouse as any)[key] = value; (this.#STATE.editingPresetData!.mouse as any)[key] = value;
}; };
const $row = CE('div', {'class': 'bx-quick-settings-row'}, const $row = CE('div', {'class': 'bx-stream-settings-row'},
CE('label', {'for': `bx_setting_${key}`}, setting.label), CE('label', {'for': `bx_setting_${key}`}, setting.label),
$elm = SettingElement.render(setting.type, key, setting, value, onChange, setting.params), $elm = SettingElement.render(setting.type, key, setting, value, onChange, setting.params),
); );

View File

@ -0,0 +1,152 @@
import { BxLogger } from "@/utils/bx-logger";
import type { MkbHandler } from "./mkb-handler";
import { KeyHelper } from "./key-helper";
import { WheelCode } from "./definitions";
import { Toast } from "@/utils/toast";
const LOG_TAG = 'PointerClient';
enum PointerAction {
MOVE = 1,
BUTTON_PRESS = 2,
BUTTON_RELEASE = 3,
SCROLL = 4,
POINTER_CAPTURE_CHANGED = 5,
}
const FixedMouseIndex = {
1: 0,
2: 2,
4: 1,
}
export class PointerClient {
static #PORT = 9269;
private static instance: PointerClient;
public static getInstance(): PointerClient {
if (!PointerClient.instance) {
PointerClient.instance = new PointerClient();
}
return PointerClient.instance;
}
#socket: WebSocket | undefined | null;
#mkbHandler: MkbHandler | undefined;
start(mkbHandler: MkbHandler) {
this.#mkbHandler = mkbHandler;
// Create WebSocket connection.
this.#socket = new WebSocket(`ws://localhost:${PointerClient.#PORT}`);
this.#socket.binaryType = 'arraybuffer';
// Connection opened
this.#socket.addEventListener('open', (event) => {
BxLogger.info(LOG_TAG, 'connected')
});
// Error
this.#socket.addEventListener('error', (event) => {
BxLogger.error(LOG_TAG, event);
Toast.show('Cannot setup mouse');
});
this.#socket.addEventListener('close', (event) => {
this.#socket = null;
});
// Listen for messages
this.#socket.addEventListener('message', (event) => {
const dataView = new DataView(event.data);
let messageType = dataView.getInt8(0);
let offset = Int8Array.BYTES_PER_ELEMENT;
switch (messageType) {
case PointerAction.MOVE:
this.onMove(dataView, offset);
break;
case PointerAction.BUTTON_PRESS:
case PointerAction.BUTTON_RELEASE:
this.onPress(messageType, dataView, offset);
break;
case PointerAction.SCROLL:
this.onScroll(dataView, offset);
break;
case PointerAction.POINTER_CAPTURE_CHANGED:
this.onPointerCaptureChanged(dataView, offset);
}
});
}
onMove(dataView: DataView, offset: number) {
// [X, Y]
const x = dataView.getInt16(offset);
offset += Int16Array.BYTES_PER_ELEMENT;
const y = dataView.getInt16(offset);
this.#mkbHandler?.handleMouseMove({
movementX: x,
movementY: y,
});
// BxLogger.info(LOG_TAG, 'move', x, y);
}
onPress(messageType: PointerAction, dataView: DataView, offset: number) {
const buttonIndex = dataView.getInt8(offset);
const fixedIndex = FixedMouseIndex[buttonIndex as keyof typeof FixedMouseIndex];
const keyCode = 'Mouse' + fixedIndex;
this.#mkbHandler?.handleMouseClick({
key: {
code: keyCode,
name: KeyHelper.codeToKeyName(keyCode),
},
pressed: messageType === PointerAction.BUTTON_PRESS,
});
// BxLogger.info(LOG_TAG, 'press', buttonIndex);
}
onScroll(dataView: DataView, offset: number) {
// [V_SCROLL, H_SCROLL]
const vScroll = dataView.getInt8(offset);
offset += Int8Array.BYTES_PER_ELEMENT;
const hScroll = dataView.getInt8(offset);
let code = '';
if (vScroll < 0) {
code = WheelCode.SCROLL_UP;
} else if (vScroll > 0) {
code = WheelCode.SCROLL_DOWN;
} else if (hScroll < 0) {
code = WheelCode.SCROLL_LEFT;
} else if (hScroll > 0) {
code = WheelCode.SCROLL_RIGHT;
}
code && this.#mkbHandler?.handleMouseWheel({
key: {
code: code,
name: KeyHelper.codeToKeyName(code),
},
});
// BxLogger.info(LOG_TAG, 'scroll', vScroll, hScroll);
}
onPointerCaptureChanged(dataView: DataView, offset: number) {
const hasCapture = dataView.getInt8(offset) === 1;
!hasCapture && this.#mkbHandler?.stop();
}
stop() {
try {
this.#socket?.close();
} catch (e) {}
}
}

View File

@ -3,7 +3,14 @@ import { BX_FLAGS } from "@utils/bx-flags";
import { getPref, PrefKey } from "@utils/preferences"; import { getPref, PrefKey } from "@utils/preferences";
import { VibrationManager } from "@modules/vibration-manager"; import { VibrationManager } from "@modules/vibration-manager";
import { BxLogger } from "@utils/bx-logger"; import { BxLogger } from "@utils/bx-logger";
import { hashCode } from "@/utils/utils"; import { hashCode, renderString } from "@utils/utils";
import { BxEvent } from "@/utils/bx-event";
import codeControllerShortcuts from "./patches/controller-shortcuts.js" with { type: "text" };
import codeLocalCoOpEnable from "./patches/local-co-op-enable.js" with { type: "text" };
import codeRemotePlayEnable from "./patches/remote-play-enable.js" with { type: "text" };
import codeRemotePlayKeepAlive from "./patches/remote-play-keep-alive.js" with { type: "text" };
import codeVibrationAdjust from "./patches/vibration-adjust.js" with { type: "text" };
type PatchArray = (keyof typeof PATCHES)[]; type PatchArray = (keyof typeof PATCHES)[];
@ -59,12 +66,14 @@ const PATCHES = {
// Disable IndexDB logging // Disable IndexDB logging
disableIndexDbLogging(str: string) { disableIndexDbLogging(str: string) {
const text = 'async addLog(e,t=1e4){'; const text = ',this.logsDb=new';
if (!str.includes(text)) { if (!str.includes(text)) {
return false; return false;
} }
return str.replace(text, text + 'return;'); // Replace log() with an empty function
let newCode = ',this.log=()=>{}';
return str.replace(text, newCode + text);
}, },
// Set custom website layout // Set custom website layout
@ -89,31 +98,24 @@ const PATCHES = {
}, },
remotePlayKeepAlive(str: string) { remotePlayKeepAlive(str: string) {
if (!str.includes('onServerDisconnectMessage(e){')) { const text = 'onServerDisconnectMessage(e){';
if (!str.includes(text)) {
return false; return false;
} }
str = str.replace('onServerDisconnectMessage(e){', `onServerDisconnectMessage(e) { str = str.replace(text, text + codeRemotePlayKeepAlive);
const msg = JSON.parse(e);
if (msg.reason === 'WarningForBeingIdle' && !window.location.pathname.includes('/launch/')) {
try {
this.sendKeepAlive();
return;
} catch (ex) { console.log(ex); }
}
`);
return str; return str;
}, },
// Enable Remote Play feature // Enable Remote Play feature
remotePlayConnectMode(str: string) { remotePlayConnectMode(str: string) {
const text = 'connectMode:"cloud-connect"'; const text = 'connectMode:"cloud-connect",';
if (!str.includes(text)) { if (!str.includes(text)) {
return false; return false;
} }
return str.replace(text, `connectMode:window.BX_REMOTE_PLAY_CONFIG?"xhome-connect":"cloud-connect",remotePlayServerId:(window.BX_REMOTE_PLAY_CONFIG&&window.BX_REMOTE_PLAY_CONFIG.serverId)||''`); return str.replace(text, codeRemotePlayEnable);
}, },
// Disable achievement toast in Remote Play // Disable achievement toast in Remote Play
@ -152,15 +154,36 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) {
return str.replace(text, 'this.shouldCollectStats=!1'); return str.replace(text, 'this.shouldCollectStats=!1');
}, },
blockGamepadStatsCollector(str: string) { patchPollGamepads(str: string) {
const text = 'this.inputPollingIntervalStats.addValue'; const index = str.indexOf('},this.pollGamepads=()=>{');
if (!str.includes(text)) { if (index === -1) {
return false; return false;
} }
str = str.replace('this.inputPollingIntervalStats.addValue', ''); const nextIndex = str.indexOf('setTimeout(this.pollGamepads', index);
str = str.replace('this.inputPollingDurationStats.addValue', ''); if (nextIndex === -1) {
return str; return false;
}
let codeBlock = str.substring(index, nextIndex);
// Block gamepad stats collecting
if (getPref(PrefKey.BLOCK_TRACKING)) {
codeBlock = codeBlock.replaceAll('this.inputPollingIntervalStats.addValue', '');
}
// Map the Share button on Xbox Series controller with the capturing screenshot feature
const match = codeBlock.match(/this\.gamepadTimestamps\.set\((\w+)\.index/);
if (match) {
const gamepadVar = match[1];
const newCode = renderString(codeControllerShortcuts, {
gamepadVar,
});
codeBlock = codeBlock.replace('this.gamepadTimestamps.set', newCode + 'this.gamepadTimestamps.set');
}
return str.substring(0, index) + codeBlock + str.substring(nextIndex);
}, },
enableXcloudLogger(str: string) { enableXcloudLogger(str: string) {
@ -190,20 +213,8 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) {
return false; return false;
} }
const newCode = `
if (!window.BX_ENABLE_CONTROLLER_VIBRATION) {
return void(0);
}
if (window.BX_VIBRATION_INTENSITY && window.BX_VIBRATION_INTENSITY < 1) {
e.leftMotorPercent = e.leftMotorPercent * window.BX_VIBRATION_INTENSITY;
e.rightMotorPercent = e.rightMotorPercent * window.BX_VIBRATION_INTENSITY;
e.leftTriggerMotorPercent = e.leftTriggerMotorPercent * window.BX_VIBRATION_INTENSITY;
e.rightTriggerMotorPercent = e.rightTriggerMotorPercent * window.BX_VIBRATION_INTENSITY;
}
`;
VibrationManager.updateGlobalVars(); VibrationManager.updateGlobalVars();
str = str.replaceAll(text, text + newCode); str = str.replaceAll(text, text + codeVibrationAdjust);
return str; return str;
}, },
@ -283,7 +294,13 @@ if (window.BX_VIBRATION_INTENSITY && window.BX_VIBRATION_INTENSITY < 1) {
return false; return false;
} }
str = str.replace(text, 'window.BX_EXPOSED["touch_layout_manager"] = this,' + text); const newCode = `
true;
window.BX_EXPOSED["touchLayoutManager"] = this;
window.dispatchEvent(new Event("${BxEvent.TOUCH_LAYOUT_MANAGER_READY}"));
`;
str = str.replace(text, newCode + text);
return str; return str;
}, },
@ -293,27 +310,7 @@ if (window.BX_VIBRATION_INTENSITY && window.BX_VIBRATION_INTENSITY < 1) {
return false; return false;
} }
let patchstr = ` const newCode = `true; ${codeLocalCoOpEnable}; true,`;
let match;
let onGamepadChangedStr = this.onGamepadChanged.toString();
onGamepadChangedStr = onGamepadChangedStr.replaceAll('0', 'arguments[1]');
eval(\`this.onGamepadChanged = function \${onGamepadChangedStr}\`);
let onGamepadInputStr = this.onGamepadInput.toString();
match = onGamepadInputStr.match(/(\\w+\\.GamepadIndex)/);
if (match) {
const gamepadIndexVar = match[0];
onGamepadInputStr = onGamepadInputStr.replace('this.gamepadStates.get(', \`this.gamepadStates.get(\${gamepadIndexVar},\`);
eval(\`this.onGamepadInput = function \${onGamepadInputStr}\`);
BxLogger.info('supportLocalCoOp', '✅ Successfully patched local co-op support');
} else {
BxLogger.error('supportLocalCoOp', '❌ Unable to patch local co-op support');
}
`;
const newCode = `true; ${patchstr}; true,`;
str = str.replace(text, text + newCode); str = str.replace(text, text + newCode);
return str; return str;
@ -387,13 +384,19 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) {
return false; return false;
} }
// Restore the "..." button let newCode = `
str = str.replace(text, 'e.guideUI = null;' + text); // Expose onShowStreamMenu
window.BX_EXPOSED.showStreamMenu = e.onShowStreamMenu;
// Restore the "..." button
e.guideUI = null;
`;
// Remove the TAK Edit button when the touch controller is disabled // Remove the TAK Edit button when the touch controller is disabled
if (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'off') { if (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'off') {
str = str.replace(text, 'e.canShowTakHUD = false;' + text); newCode += 'e.canShowTakHUD = false;';
} }
str = str.replace(text, newCode + text);
return str; return str;
}, },
@ -404,7 +407,7 @@ if (!!window.BX_REMOTE_PLAY_CONFIG) {
} }
const newCode = ` const newCode = `
window.BX_EXPOSED.onPollingModeChanged && window.BX_EXPOSED.onPollingModeChanged(e); BxEvent.dispatch(window, BxEvent.XCLOUD_POLLING_MODE_CHANGED, {mode: e});
`; `;
str = str.replace(text, text + newCode); str = str.replace(text, text + newCode);
return str; return str;
@ -459,6 +462,98 @@ BxLogger.info('patchRemotePlayMkb', ${configsVar});
return str; return str;
}, },
patchAudioMediaStream(str: string) {
const text = '.srcObject=this.audioMediaStream,';
if (!str.includes(text)) {
return false;
}
const newCode = `window.BX_EXPOSED.setupGainNode(arguments[1], this.audioMediaStream),`;
str = str.replace(text, text + newCode);
return str;
},
patchCombinedAudioVideoMediaStream(str: string) {
const text = '.srcObject=this.combinedAudioVideoStream';
if (!str.includes(text)) {
return false;
}
const newCode = `,window.BX_EXPOSED.setupGainNode(arguments[0], this.combinedAudioVideoStream)`;
str = str.replace(text, text + newCode);
return str;
},
patchTouchControlDefaultOpacity(str: string) {
const text = 'opacityMultiplier:1';
if (!str.includes(text)) {
return false;
}
const opacity = (getPref(PrefKey.STREAM_TOUCH_CONTROLLER_DEFAULT_OPACITY) / 100).toFixed(1);
const newCode = `opacityMultiplier: ${opacity}`;
str = str.replace(text, newCode);
return str;
},
patchShowSensorControls(str: string) {
const text = '{shouldShowSensorControls:';
if (!str.includes(text)) {
return false;
}
const newCode = `{shouldShowSensorControls: (window.BX_EXPOSED && window.BX_EXPOSED.shouldShowSensorControls) ||`;
str = str.replace(text, newCode);
return str;
},
/*
exposeEventTarget(str: string) {
const text ='this._eventTarget=new EventTarget';
if (!str.includes(text)) {
return false;
}
const newCode = `
window.BX_EXPOSED.eventTarget = ${text},
window.dispatchEvent(new Event('${BxEvent.STREAM_EVENT_TARGET_READY}'))
`;
str = str.replace(text, newCode);
return str;
},
//*/
// Class with: connectAsync(), doConnectAsync(), setPlayClient()
exposeStreamSession(str: string) {
const text =',this._connectionType=';
if (!str.includes(text)) {
return false;
}
const newCode = `;
window.BX_EXPOSED.streamSession = this;
const orgSetMicrophoneState = this.setMicrophoneState.bind(this);
this.setMicrophoneState = state => {
orgSetMicrophoneState(state);
const evt = new Event('${BxEvent.MICROPHONE_STATE_CHANGED}');
evt.microphoneState = state;
window.dispatchEvent(evt);
};
window.dispatchEvent(new Event('${BxEvent.STREAM_SESSION_READY}'))
true` + text;
str = str.replace(text, newCode);
return str;
},
}; };
let PATCH_ORDERS: PatchArray = [ let PATCH_ORDERS: PatchArray = [
@ -466,6 +561,8 @@ let PATCH_ORDERS: PatchArray = [
'overrideSettings', 'overrideSettings',
'broadcastPollingMode', 'broadcastPollingMode',
'exposeStreamSession',
getPref(PrefKey.UI_LAYOUT) !== 'default' && 'websiteLayout', getPref(PrefKey.UI_LAYOUT) !== 'default' && 'websiteLayout',
getPref(PrefKey.LOCAL_CO_OP_ENABLED) && 'supportLocalCoOp', getPref(PrefKey.LOCAL_CO_OP_ENABLED) && 'supportLocalCoOp',
getPref(PrefKey.GAME_FORTNITE_FORCE_CONSOLE) && 'forceFortniteConsole', getPref(PrefKey.GAME_FORTNITE_FORCE_CONSOLE) && 'forceFortniteConsole',
@ -501,12 +598,22 @@ let PLAYING_PATCH_ORDERS: PatchArray = [
'patchStreamHud', 'patchStreamHud',
'playVibration', 'playVibration',
// 'exposeEventTarget',
// Patch volume control for normal stream
getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && !getPref(PrefKey.STREAM_COMBINE_SOURCES) && 'patchAudioMediaStream',
// Patch volume control for combined audio+video stream
getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && getPref(PrefKey.STREAM_COMBINE_SOURCES) && 'patchCombinedAudioVideoMediaStream',
STATES.hasTouchSupport && getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all' && 'patchShowSensorControls',
STATES.hasTouchSupport && getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all' && 'exposeTouchLayoutManager', STATES.hasTouchSupport && getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'all' && 'exposeTouchLayoutManager',
STATES.hasTouchSupport && (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'off' || getPref(PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF)) && 'disableTakRenderer', STATES.hasTouchSupport && (getPref(PrefKey.STREAM_TOUCH_CONTROLLER) === 'off' || getPref(PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF)) && 'disableTakRenderer',
STATES.hasTouchSupport && getPref(PrefKey.STREAM_TOUCH_CONTROLLER_DEFAULT_OPACITY) !== 100 && 'patchTouchControlDefaultOpacity',
BX_FLAGS.EnableXcloudLogging && 'enableConsoleLogging', BX_FLAGS.EnableXcloudLogging && 'enableConsoleLogging',
getPref(PrefKey.BLOCK_TRACKING) && 'blockGamepadStatsCollector', 'patchPollGamepads',
getPref(PrefKey.STREAM_COMBINE_SOURCES) && 'streamCombineSources', getPref(PrefKey.STREAM_COMBINE_SOURCES) && 'streamCombineSources',
@ -564,7 +671,7 @@ export class Patcher {
let patchesToCheck: PatchArray; let patchesToCheck: PatchArray;
let appliedPatches: PatchArray; let appliedPatches: PatchArray;
const patchesMap: { [key: string]: PatchArray } = {}; const patchesMap: Record<string, PatchArray> = {};
for (let id in item[1]) { for (let id in item[1]) {
appliedPatches = []; appliedPatches = [];
@ -608,7 +715,7 @@ export class Patcher {
modified = true; modified = true;
str = patchedStr; str = patchedStr;
BxLogger.info(LOG_TAG, `Applied "${patchName}" patch`); BxLogger.info(LOG_TAG, `${patchName}`);
appliedPatches.push(patchName); appliedPatches.push(patchName);
// Remove patch // Remove patch
@ -698,7 +805,7 @@ export class PatcherCache {
return PatcherCache.#CACHE[id]; return PatcherCache.#CACHE[id];
} }
static saveToCache(subCache: { [key: string]: PatchArray }) { static saveToCache(subCache: Record<string, PatchArray>) {
for (const id in subCache) { for (const id in subCache) {
const patchNames = subCache[id]; const patchNames = subCache[id];

View File

@ -0,0 +1,87 @@
const currentGamepad = ${gamepadVar};
// Share button on XS controller
if (currentGamepad.buttons[17] && currentGamepad.buttons[17].pressed) {
window.dispatchEvent(new Event(BxEvent.CAPTURE_SCREENSHOT));
}
const btnHome = currentGamepad.buttons[16];
if (btnHome) {
if (!this.bxHomeStates) {
this.bxHomeStates = {};
}
if (btnHome.pressed) {
this.gamepadIsIdle.set(currentGamepad.index, false);
if (this.bxHomeStates[currentGamepad.index]) {
const lastTimestamp = this.bxHomeStates[currentGamepad.index].timestamp;
if (currentGamepad.timestamp !== lastTimestamp) {
this.bxHomeStates[currentGamepad.index].timestamp = currentGamepad.timestamp;
const handled = window.BX_EXPOSED.handleControllerShortcut(currentGamepad);
if (handled) {
this.bxHomeStates[currentGamepad.index].shortcutPressed += 1;
}
}
} else {
// First time pressing > save current timestamp
window.BX_EXPOSED.resetControllerShortcut(currentGamepad.index);
this.bxHomeStates[currentGamepad.index] = {
shortcutPressed: 0,
timestamp: currentGamepad.timestamp,
};
}
// Listen to next button press
const intervalMs = 16;
this.inputConfiguration.useIntervalWorkerThreadForInput && this.intervalWorker ? this.intervalWorker.scheduleTimer(intervalMs) : this.pollGamepadssetTimeoutTimerID = setTimeout(this.pollGamepads, intervalMs);
// Hijack this button
return;
} else if (this.bxHomeStates[currentGamepad.index]) {
const info = structuredClone(this.bxHomeStates[currentGamepad.index]);
// Home button released
this.bxHomeStates[currentGamepad.index] = null;
if (info.shortcutPressed === 0) {
const fakeGamepadMappings = [{
GamepadIndex: currentGamepad.index,
A: 0,
B: 0,
X: 0,
Y: 0,
LeftShoulder: 0,
RightShoulder: 0,
LeftTrigger: 0,
RightTrigger: 0,
View: 0,
Menu: 0,
LeftThumb: 0,
RightThumb: 0,
DPadUp: 0,
DPadDown: 0,
DPadLeft: 0,
DPadRight: 0,
Nexus: 1,
LeftThumbXAxis: 0,
LeftThumbYAxis: 0,
RightThumbXAxis: 0,
RightThumbYAxis: 0,
PhysicalPhysicality: 0,
VirtualPhysicality: 0,
Dirty: true,
Virtual: false,
}];
const isLongPress = (currentGamepad.timestamp - info.timestamp) >= 500;
const intervalMs = isLongPress ? 500 : 100;
this.inputSink.onGamepadInput(performance.now() - intervalMs, fakeGamepadMappings);
this.inputConfiguration.useIntervalWorkerThreadForInput && this.intervalWorker ? this.intervalWorker.scheduleTimer(intervalMs) : this.pollGamepadssetTimeoutTimerID = setTimeout(this.pollGamepads, intervalMs);
return;
}
}
}

View File

@ -0,0 +1,17 @@
let match;
let onGamepadChangedStr = this.onGamepadChanged.toString();
onGamepadChangedStr = onGamepadChangedStr.replaceAll('0', 'arguments[1]');
eval(`this.onGamepadChanged = function ${onGamepadChangedStr}`);
let onGamepadInputStr = this.onGamepadInput.toString();
match = onGamepadInputStr.match(/(\w+\.GamepadIndex)/);
if (match) {
const gamepadIndexVar = match[0];
onGamepadInputStr = onGamepadInputStr.replace('this.gamepadStates.get(', `this.gamepadStates.get(${gamepadIndexVar},`);
eval(`this.onGamepadInput = function ${onGamepadInputStr}`);
BxLogger.info('supportLocalCoOp', '✅ Successfully patched local co-op support');
} else {
BxLogger.error('supportLocalCoOp', '❌ Unable to patch local co-op support');
}

View File

@ -0,0 +1,2 @@
connectMode: window.BX_REMOTE_PLAY_CONFIG ? "xhome-connect" : "cloud-connect",
remotePlayServerId: (window.BX_REMOTE_PLAY_CONFIG && window.BX_REMOTE_PLAY_CONFIG.serverId) || '',

View File

@ -0,0 +1,7 @@
const msg = JSON.parse(e);
if (msg.reason === 'WarningForBeingIdle' && !window.location.pathname.includes('/launch/')) {
try {
this.sendKeepAlive();
return;
} catch (ex) { console.log(ex); }
}

View File

@ -0,0 +1,11 @@
if (!window.BX_ENABLE_CONTROLLER_VIBRATION) {
return void(0);
}
const intensity = window.BX_VIBRATION_INTENSITY;
if (intensity && intensity < 1) {
e.leftMotorPercent *= intensity;
e.rightMotorPercent *= intensity;
e.leftTriggerMotorPercent *= intensity;
e.rightTriggerMotorPercent *= intensity;
}

View File

@ -1,97 +0,0 @@
import { STATES, AppInterface } from "@utils/global";
import { CE } from "@utils/html";
export function takeScreenshot(callback: any) {
const currentStream = STATES.currentStream!;
const $video = currentStream.$video;
const $canvas = currentStream.$screenshotCanvas;
if (!$video || !$canvas) {
return;
}
const $canvasContext = $canvas.getContext('2d')!;
$canvasContext.drawImage($video, 0, 0, $canvas.width, $canvas.height);
// Get data URL and pass to parent app
if (AppInterface) {
const data = $canvas.toDataURL('image/png').split(';base64,')[1];
AppInterface.saveScreenshot(currentStream.titleId, data);
// Free screenshot from memory
$canvasContext.clearRect(0, 0, $canvas.width, $canvas.height);
callback && callback();
return;
}
$canvas && $canvas.toBlob(blob => {
// Download screenshot
const now = +new Date;
const $anchor = CE<HTMLAnchorElement>('a', {
'download': `${currentStream.titleId}-${now}.png`,
'href': URL.createObjectURL(blob!),
});
$anchor.click();
// Free screenshot from memory
URL.revokeObjectURL($anchor.href);
$canvasContext.clearRect(0, 0, $canvas.width, $canvas.height);
callback && callback();
}, 'image/png');
}
export function setupScreenshotButton() {
const currentStream = STATES.currentStream!
currentStream.$screenshotCanvas = CE('canvas', {'class': 'bx-screenshot-canvas'});
document.documentElement.appendChild(currentStream.$screenshotCanvas!);
const delay = 2000;
const $btn = CE('div', {'class': 'bx-screenshot-button', 'data-showing': false});
let timeout: number | null;
const detectDbClick = (e: MouseEvent) => {
if (!currentStream.$video) {
timeout = null;
$btn.style.display = 'none';
return;
}
if (timeout) {
clearTimeout(timeout);
timeout = null;
$btn.setAttribute('data-capturing', 'true');
takeScreenshot(() => {
// Hide button
$btn.setAttribute('data-showing', 'false');
window.setTimeout(() => {
if (!timeout) {
$btn.setAttribute('data-capturing', 'false');
}
}, 100);
});
return;
}
const isShowing = $btn.getAttribute('data-showing') === 'true';
if (!isShowing) {
// Show button
$btn.setAttribute('data-showing', 'true');
$btn.setAttribute('data-capturing', 'false');
timeout && clearTimeout(timeout);
timeout = window.setTimeout(() => {
timeout = null;
$btn.setAttribute('data-showing', 'false');
$btn.setAttribute('data-capturing', 'false');
}, delay);
}
}
$btn.addEventListener('mousedown', detectDbClick);
document.documentElement.appendChild($btn);
}

View File

@ -0,0 +1,33 @@
import { t } from "@utils/translation";
import { Toast } from "@utils/toast";
export enum MicrophoneState {
REQUESTED = 'Requested',
ENABLED = 'Enabled',
MUTED = 'Muted',
NOT_ALLOWED = 'NotAllowed',
NOT_FOUND = 'NotFound',
}
export class MicrophoneShortcut {
static toggle(showToast: boolean = true): boolean {
if (!window.BX_EXPOSED.streamSession) {
return false;
}
const state = window.BX_EXPOSED.streamSession._microphoneState;
const enableMic = state === MicrophoneState.ENABLED ? false : true;
try {
window.BX_EXPOSED.streamSession.tryEnableChatAsync(enableMic);
showToast && Toast.show(t('microphone'), t(enableMic ? 'unmuted': 'muted'), {instant: true});
return enableMic;
} catch (e) {
console.log(e);
}
return false;
}
}

View File

@ -0,0 +1,90 @@
import { t } from "@utils/translation";
import { STATES } from "@utils/global";
import { PrefKey, getPref, setPref } from "@utils/preferences";
import { Toast } from "@utils/toast";
import { BxEvent } from "@/utils/bx-event";
import { ceilToNearest, floorToNearest } from "@/utils/utils";
export class SoundShortcut {
static adjustGainNodeVolume(amount: number): number {
if (!getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL)) {
return 0;
}
const currentValue = getPref(PrefKey.AUDIO_VOLUME);
let nearestValue: number;
if (amount > 0) { // Increase
nearestValue = ceilToNearest(currentValue, amount);
} else { // Decrease
nearestValue = floorToNearest(currentValue, -1 * amount);
}
let newValue: number;
if (currentValue !== nearestValue) {
newValue = nearestValue;
} else {
newValue = currentValue + amount;
}
newValue = setPref(PrefKey.AUDIO_VOLUME, newValue);
SoundShortcut.setGainNodeVolume(newValue);
// Show toast
Toast.show(`${t('stream')} ${t('volume')}`, newValue + '%', {instant: true});
BxEvent.dispatch(window, BxEvent.GAINNODE_VOLUME_CHANGED, {
volume: newValue,
});
return newValue;
}
static setGainNodeVolume(value: number) {
STATES.currentStream.audioGainNode && (STATES.currentStream.audioGainNode.gain.value = value / 100);
}
static muteUnmute() {
if (getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL) && STATES.currentStream.audioGainNode) {
const gainValue = STATES.currentStream.audioGainNode.gain.value;
const settingValue = getPref(PrefKey.AUDIO_VOLUME);
let targetValue: number;
if (settingValue === 0) { // settingValue is 0 => set to 100
targetValue = 100;
setPref(PrefKey.AUDIO_VOLUME, targetValue);
BxEvent.dispatch(window, BxEvent.GAINNODE_VOLUME_CHANGED, {
volume: targetValue,
});
} else if (gainValue === 0) { // is being muted => set to settingValue
targetValue = settingValue;
} else { // not being muted => mute
targetValue = 0;
}
let status: string;
if (targetValue === 0) {
status = t('muted');
} else {
status = targetValue + '%';
}
SoundShortcut.setGainNodeVolume(targetValue);
Toast.show(`${t('stream')} ${t('volume')}`, status, {instant: true});
return;
}
let $media: HTMLMediaElement;
$media = document.querySelector('div[data-testid=media-container] audio') as HTMLAudioElement;
if (!$media) {
$media = document.querySelector('div[data-testid=media-container] video') as HTMLAudioElement;
}
if ($media) {
$media.muted = !$media.muted;
const status = $media.muted ? t('muted') : t('unmuted');
Toast.show(`${t('stream')} ${t('volume')}`, status, {instant: true});
}
}
}

View File

@ -0,0 +1,6 @@
export class StreamUiShortcut {
static showHideStreamMenu() {
// Show menu
window.BX_EXPOSED.showStreamMenu && window.BX_EXPOSED.showStreamMenu();
}
}

View File

@ -35,7 +35,7 @@ function cloneStreamHudButton($orgButton: HTMLElement, label: string, svgIcon: t
} }
}; };
if (STATES.hasTouchSupport) { if (STATES.browserHasTouchSupport) {
$container.addEventListener('transitionstart', onTransitionStart); $container.addEventListener('transitionstart', onTransitionStart);
$container.addEventListener('transitionend', onTransitionEnd); $container.addEventListener('transitionend', onTransitionEnd);
} }
@ -66,9 +66,9 @@ export function injectStreamMenuButtons() {
($screen as any).xObserving = true; ($screen as any).xObserving = true;
const $quickBar = document.querySelector('.bx-quick-settings-bar')!; const $settingsDialog = document.querySelector('.bx-stream-settings-dialog')!;
const $parent = $screen.parentElement; const $parent = $screen.parentElement;
const hideQuickBarFunc = (e?: MouseEvent | TouchEvent) => { const hideSettingsFunc = (e?: MouseEvent | TouchEvent) => {
if (e) { if (e) {
const $target = e.target as HTMLElement; const $target = e.target as HTMLElement;
e.stopPropagation(); e.stopPropagation();
@ -76,15 +76,15 @@ export function injectStreamMenuButtons() {
return; return;
} }
if ($target.id === 'MultiTouchSurface') { if ($target.id === 'MultiTouchSurface') {
$target.removeEventListener('touchstart', hideQuickBarFunc); $target.removeEventListener('touchstart', hideSettingsFunc);
} }
} }
// Hide Quick settings bar // Hide Stream settings dialog
$quickBar.classList.add('bx-gone'); $settingsDialog.classList.add('bx-gone');
$parent?.removeEventListener('click', hideQuickBarFunc); $parent?.removeEventListener('click', hideSettingsFunc);
// $parent.removeEventListener('touchstart', hideQuickBarFunc); // $parent.removeEventListener('touchstart', hideSettingsFunc);
} }
let $btnStreamSettings: HTMLElement; let $btnStreamSettings: HTMLElement;
@ -105,12 +105,6 @@ export function injectStreamMenuButtons() {
if (!($node as HTMLElement).className || !($node as HTMLElement).className.startsWith) { if (!($node as HTMLElement).className || !($node as HTMLElement).className.startsWith) {
return; return;
} }
if (($node as HTMLElement).className.startsWith('StreamMenu')) {
if (!document.querySelector('div[class^=PureInStreamConfirmationModal]')) {
BxEvent.dispatch(window, BxEvent.STREAM_MENU_HIDDEN);
}
}
}); });
item.addedNodes.forEach(async $node => { item.addedNodes.forEach(async $node => {
@ -120,8 +114,13 @@ export function injectStreamMenuButtons() {
let $elm: HTMLElement | null = $node as HTMLElement; let $elm: HTMLElement | null = $node as HTMLElement;
// Ignore SVG elements
if ($elm instanceof SVGSVGElement) {
return;
}
// Error Page: .PureErrorPage.ErrorScreen // Error Page: .PureErrorPage.ErrorScreen
if ($elm.className.includes('PureErrorPage')) { if ($elm.className?.includes('PureErrorPage')) {
BxEvent.dispatch(window, BxEvent.STREAM_ERROR_PAGE); BxEvent.dispatch(window, BxEvent.STREAM_ERROR_PAGE);
return; return;
} }
@ -133,17 +132,15 @@ export function injectStreamMenuButtons() {
} }
// Render badges // Render badges
if ($elm.className.startsWith('StreamMenu-module__container')) { if ($elm.className?.startsWith('StreamMenu-module__container')) {
BxEvent.dispatch(window, BxEvent.STREAM_MENU_SHOWN);
const $btnCloseHud = document.querySelector('button[class*=StreamMenu-module__backButton]'); const $btnCloseHud = document.querySelector('button[class*=StreamMenu-module__backButton]');
if (!$btnCloseHud) { if (!$btnCloseHud) {
return; return;
} }
// Hide Quick bar when closing HUD // Hide Stream Settings dialog when closing HUD
$btnCloseHud && $btnCloseHud.addEventListener('click', e => { $btnCloseHud && $btnCloseHud.addEventListener('click', e => {
$quickBar.classList.add('bx-gone'); $settingsDialog.classList.add('bx-gone');
}); });
// Create Refresh button from the Close button // Create Refresh button from the Close button
@ -171,11 +168,11 @@ export function injectStreamMenuButtons() {
const $menu = document.querySelector('div[class*=StreamMenu-module__menuContainer] > div[class*=Menu-module]'); const $menu = document.querySelector('div[class*=StreamMenu-module__menuContainer] > div[class*=Menu-module]');
$menu?.appendChild(await StreamBadges.render()); $menu?.appendChild(await StreamBadges.render());
hideQuickBarFunc(); hideSettingsFunc();
return; return;
} }
if ($elm.className.startsWith('Overlay-module_') || $elm.className.startsWith('InProgressScreen')) { if ($elm.className?.startsWith('Overlay-module_') || $elm.className?.startsWith('InProgressScreen')) {
$elm = $elm.querySelector('#StreamHud'); $elm = $elm.querySelector('#StreamHud');
} }
@ -205,25 +202,25 @@ export function injectStreamMenuButtons() {
// Create Stream Settings button // Create Stream Settings button
if (!$btnStreamSettings) { if (!$btnStreamSettings) {
$btnStreamSettings = cloneStreamHudButton($orgButton, t('menu-stream-settings'), BxIcon.STREAM_SETTINGS); $btnStreamSettings = cloneStreamHudButton($orgButton, t('stream-settings'), BxIcon.STREAM_SETTINGS);
$btnStreamSettings.addEventListener('click', e => { $btnStreamSettings.addEventListener('click', e => {
hideGripHandle(); hideGripHandle();
e.preventDefault(); e.preventDefault();
// Show Quick settings bar // Show Stream Settings dialog
$quickBar.classList.remove('bx-gone'); $settingsDialog.classList.remove('bx-gone');
$parent?.addEventListener('click', hideQuickBarFunc); $parent?.addEventListener('click', hideSettingsFunc);
//$parent.addEventListener('touchstart', hideQuickBarFunc); //$parent.addEventListener('touchstart', hideSettingsFunc);
const $touchSurface = document.getElementById('MultiTouchSurface'); const $touchSurface = document.getElementById('MultiTouchSurface');
$touchSurface && $touchSurface.style.display != 'none' && $touchSurface.addEventListener('touchstart', hideQuickBarFunc); $touchSurface && $touchSurface.style.display != 'none' && $touchSurface.addEventListener('touchstart', hideSettingsFunc);
}); });
} }
// Create Stream Stats button // Create Stream Stats button
if (!$btnStreamStats) { if (!$btnStreamStats) {
$btnStreamStats = cloneStreamHudButton($orgButton, t('menu-stream-stats'), BxIcon.STREAM_STATS); $btnStreamStats = cloneStreamHudButton($orgButton, t('stream-stats'), BxIcon.STREAM_STATS);
$btnStreamStats.addEventListener('click', e => { $btnStreamStats.addEventListener('click', e => {
hideGripHandle(); hideGripHandle();
e.preventDefault(); e.preventDefault();
@ -258,14 +255,14 @@ export function injectStreamMenuButtons() {
export function showStreamSettings(tabId: string) { export function showStreamSettings(tabId: string) {
const $wrapper = document.querySelector('.bx-quick-settings-bar'); const $wrapper = document.querySelector('.bx-stream-settings-dialog');
if (!$wrapper) { if (!$wrapper) {
return; return;
} }
// Select tab // Select tab
if (tabId) { if (tabId) {
const $tab = $wrapper.querySelector(`.bx-quick-settings-tabs svg[data-group=${tabId}]`); const $tab = $wrapper.querySelector(`.bx-stream-settings-tabs svg[data-group=${tabId}]`);
$tab && $tab.dispatchEvent(new Event('click')); $tab && $tab.dispatchEvent(new Event('click'));
} }

View File

@ -1,18 +1,21 @@
import { STATES } from "@utils/global"; import { STATES } from "@utils/global";
import { CE } from "@utils/html"; import { escapeHtml } from "@utils/html";
import { Toast } from "@utils/toast"; import { Toast } from "@utils/toast";
import { BxEvent } from "@utils/bx-event"; import { BxEvent } from "@utils/bx-event";
import { BX_FLAGS } from "@utils/bx-flags"; import { BX_FLAGS, NATIVE_FETCH } from "@utils/bx-flags";
import { getPref, PrefKey } from "@utils/preferences"; import { getPref, PrefKey } from "@utils/preferences";
import { t } from "@utils/translation"; import { t } from "@utils/translation";
import { NATIVE_FETCH } from "@utils/network";
import { BxLogger } from "@utils/bx-logger"; import { BxLogger } from "@utils/bx-logger";
const LOG_TAG = 'TouchController'; const LOG_TAG = 'TouchController';
export class TouchController { export class TouchController {
static readonly #EVENT_SHOW_DEFAULT_CONTROLLER = new MessageEvent('message', { static readonly #EVENT_SHOW_DEFAULT_CONTROLLER = new MessageEvent('message', {
data: '{"content":"{\\"layoutId\\":\\"\\"}","target":"/streaming/touchcontrols/showlayoutv2","type":"Message"}', data: JSON.stringify({
content: '{"layoutId":""}',
target: '/streaming/touchcontrols/showlayoutv2',
type: 'Message',
}),
origin: 'better-xcloud', origin: 'better-xcloud',
}); });
@ -23,17 +26,17 @@ export class TouchController {
}); });
*/ */
static #$bar: HTMLElement;
static #$style: HTMLStyleElement; static #$style: HTMLStyleElement;
static #enable = false; static #enable = false;
static #showing = false;
static #dataChannel: RTCDataChannel | null; static #dataChannel: RTCDataChannel | null;
static #customLayouts: {[index: string]: any} = {}; static #customLayouts: {[index: string]: any} = {};
static #baseCustomLayouts: {[index: string]: any} = {}; static #baseCustomLayouts: {[index: string]: any} = {};
static #currentLayoutId: string; static #currentLayoutId: string;
static #customList: string[];
static enable() { static enable() {
TouchController.#enable = true; TouchController.#enable = true;
} }
@ -48,37 +51,28 @@ export class TouchController {
static #showDefault() { static #showDefault() {
TouchController.#dispatchMessage(TouchController.#EVENT_SHOW_DEFAULT_CONTROLLER); TouchController.#dispatchMessage(TouchController.#EVENT_SHOW_DEFAULT_CONTROLLER);
TouchController.#showing = true;
} }
static #show() { static #show() {
document.querySelector('#BabylonCanvasContainer-main')?.parentElement?.classList.remove('bx-offscreen'); document.querySelector('#BabylonCanvasContainer-main')?.parentElement?.classList.remove('bx-offscreen');
TouchController.#showing = true;
} }
static #hide() { static #hide() {
document.querySelector('#BabylonCanvasContainer-main')?.parentElement?.classList.add('bx-offscreen'); document.querySelector('#BabylonCanvasContainer-main')?.parentElement?.classList.add('bx-offscreen');
TouchController.#showing = false;
} }
static #toggleVisibility() { static toggleVisibility(status: boolean) {
if (!TouchController.#dataChannel) { if (!TouchController.#dataChannel) {
return; return;
} }
TouchController.#showing ? TouchController.#hide() : TouchController.#show(); status ? TouchController.#hide() : TouchController.#show();
}
static #toggleBar(value: boolean) {
TouchController.#$bar && TouchController.#$bar.setAttribute('data-showing', value.toString());
} }
static reset() { static reset() {
TouchController.#enable = false; TouchController.#enable = false;
TouchController.#showing = false;
TouchController.#dataChannel = null; TouchController.#dataChannel = null;
TouchController.#$bar && TouchController.#$bar.removeAttribute('data-showing');
TouchController.#$style && (TouchController.#$style.textContent = ''); TouchController.#$style && (TouchController.#$style.textContent = '');
} }
@ -103,7 +97,7 @@ export class TouchController {
retries = retries || 1; retries = retries || 1;
if (retries > 2) { if (retries > 2) {
TouchController.#customLayouts[xboxTitleId] = null; TouchController.#customLayouts[xboxTitleId] = null;
// Wait for BX_EXPOSED.touch_layout_manager // Wait for BX_EXPOSED.touchLayoutManager
window.setTimeout(() => TouchController.#dispatchLayouts(null), 1000); window.setTimeout(() => TouchController.#dispatchLayouts(null), 1000);
return; return;
} }
@ -139,7 +133,7 @@ export class TouchController {
json.layouts = layouts; json.layouts = layouts;
TouchController.#customLayouts[xboxTitleId] = json; TouchController.#customLayouts[xboxTitleId] = json;
// Wait for BX_EXPOSED.touch_layout_manager // Wait for BX_EXPOSED.touchLayoutManager
window.setTimeout(() => TouchController.#dispatchLayouts(json), 1000); window.setTimeout(() => TouchController.#dispatchLayouts(json), 1000);
} catch (e) { } catch (e) {
// Retry // Retry
@ -148,7 +142,16 @@ export class TouchController {
} }
static loadCustomLayout(xboxTitleId: string, layoutId: string, delay: number=0) { static loadCustomLayout(xboxTitleId: string, layoutId: string, delay: number=0) {
if (!window.BX_EXPOSED.touch_layout_manager) { // TODO: fix this
if (!window.BX_EXPOSED.touchLayoutManager) {
const listener = (e: Event) => {
window.removeEventListener(BxEvent.TOUCH_LAYOUT_MANAGER_READY, listener);
if (TouchController.#enable) {
TouchController.loadCustomLayout(xboxTitleId, layoutId, 0);
}
};
window.addEventListener(BxEvent.TOUCH_LAYOUT_MANAGER_READY, listener);
return; return;
} }
@ -168,10 +171,23 @@ export class TouchController {
} }
// Show a toast with layout's name // Show a toast with layout's name
layoutChanged && Toast.show(t('touch-control-layout'), layout.name); let msg: string;
let html = false;
if (layout.author) {
const author = `<b>${escapeHtml(layout.author)}</b>`;
msg = t('touch-control-layout-by', {name: author});
html = true;
} else {
msg = t('touch-control-layout');
}
layoutChanged && Toast.show(msg, layout.name, {html: html});
window.setTimeout(() => { window.setTimeout(() => {
window.BX_EXPOSED.touch_layout_manager.changeLayoutForScope({ // Show gyroscope control in the "More options" dialog if this layout has gyroscope
window.BX_EXPOSED.shouldShowSensorControls = JSON.stringify(layout).includes('gyroscope');
window.BX_EXPOSED.touchLayoutManager.changeLayoutForScope({
type: 'showLayout', type: 'showLayout',
scope: xboxTitleId, scope: xboxTitleId,
subscope: 'base', subscope: 'base',
@ -184,12 +200,28 @@ export class TouchController {
}, delay); }, delay);
} }
static updateCustomList() {
const key = 'better_xcloud_custom_touch_layouts';
TouchController.#customList = JSON.parse(window.localStorage.getItem(key) || '[]');
NATIVE_FETCH('https://raw.githubusercontent.com/redphx/better-xcloud/gh-pages/touch-layouts/ids.json')
.then(response => response.json())
.then(json => {
TouchController.#customList = json;
window.localStorage.setItem(key, JSON.stringify(json));
});
}
static getCustomList(): string[] {
return TouchController.#customList;
}
static setup() { static setup() {
// Function for testing touch control // Function for testing touch control
window.BX_EXPOSED.test_touch_control = (layout: any) => { (window as any).testTouchLayout = (layout: any) => {
const { touch_layout_manager } = window.BX_EXPOSED; const { touchLayoutManager } = window.BX_EXPOSED;
touch_layout_manager && touch_layout_manager.changeLayoutForScope({ touchLayoutManager && touchLayoutManager.changeLayoutForScope({
type: 'showLayout', type: 'showLayout',
scope: '' + STATES.currentStream?.xboxTitleId, scope: '' + STATES.currentStream?.xboxTitleId,
subscope: 'base', subscope: 'base',
@ -201,32 +233,9 @@ export class TouchController {
}); });
}; };
const $fragment = document.createDocumentFragment();
const $style = document.createElement('style'); const $style = document.createElement('style');
$fragment.appendChild($style); document.documentElement.appendChild($style);
const $bar = CE('div', {'id': 'bx-touch-controller-bar'});
$fragment.appendChild($bar);
document.documentElement.appendChild($fragment);
// Setup double-tap event
let clickTimeout: number | null;
$bar.addEventListener('mousedown', (e: MouseEvent) => {
clickTimeout && clearTimeout(clickTimeout);
if (clickTimeout) {
// Double-clicked
clickTimeout = null;
TouchController.#toggleVisibility();
return;
}
clickTimeout = window.setTimeout(() => {
clickTimeout = null;
}, 400);
});
TouchController.#$bar = $bar;
TouchController.#$style = $style; TouchController.#$style = $style;
const PREF_STYLE_STANDARD = getPref(PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD); const PREF_STYLE_STANDARD = getPref(PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD);
@ -285,7 +294,6 @@ export class TouchController {
try { try {
if (msg.data.includes('/titleinfo')) { if (msg.data.includes('/titleinfo')) {
const json = JSON.parse(JSON.parse(msg.data).content); const json = JSON.parse(JSON.parse(msg.data).content);
TouchController.#toggleBar(json.focused);
focused = json.focused; focused = json.focused;
if (!json.focused) { if (!json.focused) {

View File

@ -4,7 +4,7 @@ import { BxIcon } from "@utils/bx-icon";
import { getPreferredServerRegion } from "@utils/region"; import { getPreferredServerRegion } from "@utils/region";
import { UserAgent, UserAgentProfile } from "@utils/user-agent"; import { UserAgent, UserAgentProfile } from "@utils/user-agent";
import { getPref, Preferences, PrefKey, setPref, toPrefElement } from "@utils/preferences"; import { getPref, Preferences, PrefKey, setPref, toPrefElement } from "@utils/preferences";
import { t, refreshCurrentLocale } from "@utils/translation"; import { t, Translations } from "@utils/translation";
import { PatcherCache } from "../patcher"; import { PatcherCache } from "../patcher";
const SETTINGS_UI = { const SETTINGS_UI = {
@ -27,18 +27,26 @@ const SETTINGS_UI = {
items: [ items: [
PrefKey.STREAM_TARGET_RESOLUTION, PrefKey.STREAM_TARGET_RESOLUTION,
PrefKey.STREAM_CODEC_PROFILE, PrefKey.STREAM_CODEC_PROFILE,
PrefKey.GAME_FORTNITE_FORCE_CONSOLE,
PrefKey.BITRATE_VIDEO_MAX,
PrefKey.AUDIO_ENABLE_VOLUME_CONTROL,
PrefKey.AUDIO_MIC_ON_PLAYING, PrefKey.AUDIO_MIC_ON_PLAYING,
PrefKey.STREAM_DISABLE_FEEDBACK_DIALOG, PrefKey.STREAM_DISABLE_FEEDBACK_DIALOG,
PrefKey.SCREENSHOT_BUTTON_POSITION,
PrefKey.SCREENSHOT_APPLY_FILTERS, PrefKey.SCREENSHOT_APPLY_FILTERS,
PrefKey.AUDIO_ENABLE_VOLUME_CONTROL, PrefKey.GAME_FORTNITE_FORCE_CONSOLE,
PrefKey.STREAM_COMBINE_SOURCES, PrefKey.STREAM_COMBINE_SOURCES,
], ],
}, },
[t('game-bar')]: {
items: [
PrefKey.GAME_BAR_POSITION,
],
},
[t('local-co-op')]: { [t('local-co-op')]: {
items: [ items: [
PrefKey.LOCAL_CO_OP_ENABLED, PrefKey.LOCAL_CO_OP_ENABLED,
@ -47,6 +55,7 @@ const SETTINGS_UI = {
[t('mouse-and-keyboard')]: { [t('mouse-and-keyboard')]: {
items: [ items: [
PrefKey.NATIVE_MKB_DISABLED,
PrefKey.MKB_ENABLED, PrefKey.MKB_ENABLED,
PrefKey.MKB_HIDE_IDLE_CURSOR, PrefKey.MKB_HIDE_IDLE_CURSOR,
], ],
@ -58,6 +67,7 @@ const SETTINGS_UI = {
items: [ items: [
PrefKey.STREAM_TOUCH_CONTROLLER, PrefKey.STREAM_TOUCH_CONTROLLER,
PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF, PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF,
PrefKey.STREAM_TOUCH_CONTROLLER_DEFAULT_OPACITY,
PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD, PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD,
PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_CUSTOM, PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_CUSTOM,
], ],
@ -74,6 +84,7 @@ const SETTINGS_UI = {
[t('ui')]: { [t('ui')]: {
items: [ items: [
PrefKey.UI_LAYOUT, PrefKey.UI_LAYOUT,
PrefKey.UI_HOME_CONTEXT_MENU_DISABLED,
PrefKey.STREAM_SIMPLIFY_MENU, PrefKey.STREAM_SIMPLIFY_MENU,
PrefKey.SKIP_SPLASH_VIDEO, PrefKey.SKIP_SPLASH_VIDEO,
!AppInterface && PrefKey.UI_SCROLLBAR_HIDE, !AppInterface && PrefKey.UI_SCROLLBAR_HIDE,
@ -106,7 +117,7 @@ export function setupSettingsUi() {
const PREF_PREFERRED_REGION = getPreferredServerRegion(); const PREF_PREFERRED_REGION = getPreferredServerRegion();
const PREF_LATEST_VERSION = getPref(PrefKey.LATEST_VERSION); const PREF_LATEST_VERSION = getPref(PrefKey.LATEST_VERSION);
let $reloadBtnWrapper: HTMLButtonElement; let $btnReload: HTMLButtonElement;
// Setup Settings UI // Setup Settings UI
const $container = CE<HTMLElement>('div', { const $container = CE<HTMLElement>('div', {
@ -122,7 +133,12 @@ export function setupSettingsUi() {
'href': SCRIPT_HOME, 'href': SCRIPT_HOME,
'target': '_blank', 'target': '_blank',
}, 'Better xCloud ' + SCRIPT_VERSION), }, 'Better xCloud ' + SCRIPT_VERSION),
createButton({icon: BxIcon.QUESTION, label: t('help'), url: 'https://better-xcloud.github.io/features/'}), createButton({
icon: BxIcon.QUESTION,
style: ButtonStyle.FOCUSABLE,
label: t('help'),
url: 'https://better-xcloud.github.io/features/',
}),
) )
); );
$updateAvailable = CE('a', { $updateAvailable = CE('a', {
@ -139,8 +155,20 @@ export function setupSettingsUi() {
$updateAvailable.classList.remove('bx-gone'); $updateAvailable.classList.remove('bx-gone');
} }
if (AppInterface) {
// Show Android app settings button
const $btn = createButton({
label: t('android-app-settings'),
icon: BxIcon.STREAM_SETTINGS,
style: ButtonStyle.FULL_WIDTH | ButtonStyle.FOCUSABLE,
onClick: e => {
AppInterface.openAppSettings && AppInterface.openAppSettings();
},
});
$wrapper.appendChild($btn);
} else {
// Show link to Android app // Show link to Android app
if (!AppInterface) {
const userAgent = UserAgent.getDefault().toLowerCase(); const userAgent = UserAgent.getDefault().toLowerCase();
if (userAgent.includes('android')) { if (userAgent.includes('android')) {
const $btn = createButton({ const $btn = createButton({
@ -153,23 +181,23 @@ export function setupSettingsUi() {
} }
} }
const onChange = (e: Event) => { const onChange = async (e: Event) => {
if (!$reloadBtnWrapper) {
return;
}
$reloadBtnWrapper.classList.remove('bx-gone');
// Clear PatcherCache; // Clear PatcherCache;
PatcherCache.clear(); PatcherCache.clear();
$btnReload.classList.add('bx-danger');
// Highlight the Settings button in the Header to remind user to reload the page
const $btnHeaderSettings = document.querySelector('.bx-header-settings-button');
$btnHeaderSettings && $btnHeaderSettings.classList.add('bx-danger');
if ((e.target as HTMLElement).id === 'bx_setting_' + PrefKey.BETTER_XCLOUD_LOCALE) { if ((e.target as HTMLElement).id === 'bx_setting_' + PrefKey.BETTER_XCLOUD_LOCALE) {
// Update locale // Update locale
refreshCurrentLocale(); Translations.refreshCurrentLocale();
await Translations.updateTranslations();
const $btn = $reloadBtnWrapper.firstElementChild! as HTMLButtonElement; $btnReload.textContent = t('settings-reloading');
$btn.textContent = t('settings-reloading'); $btnReload.click();
$btn.click();
} }
}; };
@ -215,40 +243,52 @@ export function setupSettingsUi() {
} }
} }
let $control; let $control: any;
let $inpCustomUserAgent: HTMLInputElement; let $inpCustomUserAgent: HTMLInputElement;
let labelAttrs = {}; let labelAttrs: any = {
tabindex: '-1',
};
if (settingId === PrefKey.USER_AGENT_PROFILE) { if (settingId === PrefKey.USER_AGENT_PROFILE) {
let defaultUserAgent = (window.navigator as any).orgUserAgent || window.navigator.userAgent; let defaultUserAgent = (window.navigator as any).orgUserAgent || window.navigator.userAgent;
$inpCustomUserAgent = CE('input', { $inpCustomUserAgent = CE('input', {
'type': 'text', id: `bx_setting_inp_${settingId}`,
'placeholder': defaultUserAgent, type: 'text',
placeholder: defaultUserAgent,
'class': 'bx-settings-custom-user-agent', 'class': 'bx-settings-custom-user-agent',
}); });
$inpCustomUserAgent.addEventListener('change', e => { $inpCustomUserAgent.addEventListener('change', e => {
setPref(PrefKey.USER_AGENT_CUSTOM, (e.target as HTMLInputElement).value.trim()); const profile = $control.value;
const custom = (e.target as HTMLInputElement).value.trim();
UserAgent.updateStorage(profile, custom);
onChange(e); onChange(e);
}); });
$control = toPrefElement(PrefKey.USER_AGENT_PROFILE, (e: Event) => { $control = toPrefElement(PrefKey.USER_AGENT_PROFILE, (e: Event) => {
const value = (e.target as HTMLInputElement).value; const value = (e.target as HTMLInputElement).value as UserAgentProfile;
let isCustom = value === UserAgentProfile.CUSTOM; let isCustom = value === UserAgentProfile.CUSTOM;
let userAgent = UserAgent.get(value as UserAgentProfile); let userAgent = UserAgent.get(value as UserAgentProfile);
UserAgent.updateStorage(value);
$inpCustomUserAgent.value = userAgent; $inpCustomUserAgent.value = userAgent;
$inpCustomUserAgent.readOnly = !isCustom; $inpCustomUserAgent.readOnly = !isCustom;
$inpCustomUserAgent.disabled = !isCustom; $inpCustomUserAgent.disabled = !isCustom;
onChange(e); !(e.target as HTMLInputElement).disabled && onChange(e);
}); });
} else if (settingId === PrefKey.SERVER_REGION) { } else if (settingId === PrefKey.SERVER_REGION) {
let selectedValue; let selectedValue;
$control = CE<HTMLSelectElement>('select', {id: `bx_setting_${settingId}`}); $control = CE<HTMLSelectElement>('select', {
id: `bx_setting_${settingId}`,
title: settingLabel,
tabindex: 0,
});
$control.name = $control.id; $control.name = $control.id;
$control.addEventListener('change', e => { $control.addEventListener('change', (e: Event) => {
setPref(settingId, (e.target as HTMLSelectElement).value); setPref(settingId, (e.target as HTMLSelectElement).value);
onChange(e); onChange(e);
}); });
@ -291,7 +331,12 @@ export function setupSettingsUi() {
} else { } else {
$control = toPrefElement(settingId, onChange); $control = toPrefElement(settingId, onChange);
} }
labelAttrs = {'for': $control.id, 'tabindex': 0}; }
if (!!$control.id) {
labelAttrs['for'] = $control.id;
} else {
labelAttrs['for'] = `bx_setting_${settingId}`;
} }
// Disable unsupported settings // Disable unsupported settings
@ -299,13 +344,18 @@ export function setupSettingsUi() {
($control as HTMLInputElement).disabled = true; ($control as HTMLInputElement).disabled = true;
} }
// Make disabled control elements un-focusable
if ($control.disabled && !!$control.getAttribute('tabindex')) {
$control.setAttribute('tabindex', -1);
}
const $label = CE('label', labelAttrs, settingLabel); const $label = CE('label', labelAttrs, settingLabel);
if (settingNote) { if (settingNote) {
$label.appendChild(CE('b', {}, settingNote)); $label.appendChild(CE('b', {}, settingNote));
} }
const $elm = CE<HTMLElement>('div', {'class': 'bx-settings-row'}, const $elm = CE<HTMLElement>('div', {'class': 'bx-settings-row'},
$label, $label,
$control $control,
); );
$wrapper.appendChild($elm); $wrapper.appendChild($elm);
@ -314,28 +364,35 @@ export function setupSettingsUi() {
if (settingId === PrefKey.USER_AGENT_PROFILE) { if (settingId === PrefKey.USER_AGENT_PROFILE) {
$wrapper.appendChild($inpCustomUserAgent!); $wrapper.appendChild($inpCustomUserAgent!);
// Trigger 'change' event // Trigger 'change' event
$control.disabled = true;
$control.dispatchEvent(new Event('change')); $control.dispatchEvent(new Event('change'));
$control.disabled = false;
} }
} }
} }
// Setup Reload button // Setup Reload button
const $reloadBtn = createButton({ $btnReload = createButton({
label: t('settings-reload'), label: t('settings-reload'),
style: ButtonStyle.DANGER | ButtonStyle.FOCUSABLE | ButtonStyle.FULL_WIDTH, classes: ['bx-settings-reload-button'],
style: ButtonStyle.FOCUSABLE | ButtonStyle.FULL_WIDTH,
onClick: e => { onClick: e => {
window.location.reload(); window.location.reload();
$reloadBtn.disabled = true; $btnReload.disabled = true;
$reloadBtn.textContent = t('settings-reloading'); $btnReload.textContent = t('settings-reloading');
}, },
}); });
$reloadBtn.setAttribute('tabindex', '0'); $btnReload.setAttribute('tabindex', '0');
$reloadBtnWrapper = CE<HTMLButtonElement>('div', {'class': 'bx-settings-reload-button-wrapper bx-gone'}, $reloadBtn); $wrapper.appendChild($btnReload);
$wrapper.appendChild($reloadBtnWrapper);
// Donation link // Donation link
const $donationLink = CE('a', {'class': 'bx-donation-link', href: 'https://ko-fi.com/redphx', target: '_blank'}, `❤️ ${t('support-better-xcloud')}`); const $donationLink = CE('a', {
'class': 'bx-donation-link',
href: 'https://ko-fi.com/redphx',
target: '_blank',
tabindex: 0,
}, `❤️ ${t('support-better-xcloud')}`);
$wrapper.appendChild($donationLink); $wrapper.appendChild($donationLink);
// Show Game Pass app version // Show Game Pass app version

View File

@ -4,12 +4,14 @@ import { BxIcon } from "@utils/bx-icon";
import { UserAgent } from "@utils/user-agent"; import { UserAgent } from "@utils/user-agent";
import { BxEvent } from "@utils/bx-event"; import { BxEvent } from "@utils/bx-event";
import { MkbRemapper } from "@modules/mkb/mkb-remapper"; import { MkbRemapper } from "@modules/mkb/mkb-remapper";
import { getPref, PrefKey, toPrefElement } from "@utils/preferences"; import { getPref, Preferences, PrefKey, toPrefElement } from "@utils/preferences";
import { setupScreenshotButton } from "@modules/screenshot";
import { StreamStats } from "@modules/stream/stream-stats"; import { StreamStats } from "@modules/stream/stream-stats";
import { TouchController } from "@modules/touch-controller"; import { TouchController } from "@modules/touch-controller";
import { t } from "@utils/translation"; import { t } from "@utils/translation";
import { VibrationManager } from "@modules/vibration-manager"; import { VibrationManager } from "@modules/vibration-manager";
import { Screenshot } from "@/utils/screenshot";
import { ControllerShortcut } from "../controller-shortcut";
import { SoundShortcut } from "../shortcuts/shortcut-sound";
export function localRedirect(path: string) { export function localRedirect(path: string) {
@ -66,7 +68,7 @@ function getVideoPlayerFilterStyle() {
return filters.join(' '); return filters.join(' ');
} }
function setupQuickSettingsBar() { function setupStreamSettingsDialog() {
const isSafari = UserAgent.isSafari(); const isSafari = UserAgent.isSafari();
const SETTINGS_UI = [ const SETTINGS_UI = [
@ -94,13 +96,21 @@ function setupQuickSettingsBar() {
items: [ items: [
{ {
pref: PrefKey.AUDIO_VOLUME, pref: PrefKey.AUDIO_VOLUME,
label: t('volume'),
onChange: (e: any, value: number) => { onChange: (e: any, value: number) => {
STATES.currentStream.audioGainNode && (STATES.currentStream.audioGainNode.gain.value = value / 100); SoundShortcut.setGainNodeVolume(value);
}, },
params: { params: {
disabled: !getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL), disabled: !getPref(PrefKey.AUDIO_ENABLE_VOLUME_CONTROL),
}, },
onMounted: ($elm: HTMLElement) => {
const $range = $elm.querySelector('input[type=range') as HTMLInputElement;
window.addEventListener(BxEvent.GAINNODE_VOLUME_CHANGED, e => {
$range.value = (e as any).volume;
BxEvent.dispatch($range, 'input', {
ignoreOnChange: true,
});
});
},
}, },
], ],
}, },
@ -112,32 +122,27 @@ function setupQuickSettingsBar() {
items: [ items: [
{ {
pref: PrefKey.VIDEO_RATIO, pref: PrefKey.VIDEO_RATIO,
label: t('ratio'),
onChange: updateVideoPlayerCss, onChange: updateVideoPlayerCss,
}, },
{ {
pref: PrefKey.VIDEO_CLARITY, pref: PrefKey.VIDEO_CLARITY,
label: t('clarity'),
onChange: updateVideoPlayerCss, onChange: updateVideoPlayerCss,
unsupported: isSafari, unsupported: isSafari,
}, },
{ {
pref: PrefKey.VIDEO_SATURATION, pref: PrefKey.VIDEO_SATURATION,
label: t('saturation'),
onChange: updateVideoPlayerCss, onChange: updateVideoPlayerCss,
}, },
{ {
pref: PrefKey.VIDEO_CONTRAST, pref: PrefKey.VIDEO_CONTRAST,
label: t('contrast'),
onChange: updateVideoPlayerCss, onChange: updateVideoPlayerCss,
}, },
{ {
pref: PrefKey.VIDEO_BRIGHTNESS, pref: PrefKey.VIDEO_BRIGHTNESS,
label: t('brightness'),
onChange: updateVideoPlayerCss, onChange: updateVideoPlayerCss,
}, },
], ],
@ -156,21 +161,18 @@ function setupQuickSettingsBar() {
items: [ items: [
{ {
pref: PrefKey.CONTROLLER_ENABLE_VIBRATION, pref: PrefKey.CONTROLLER_ENABLE_VIBRATION,
label: t('controller-vibration'),
unsupported: !VibrationManager.supportControllerVibration(), unsupported: !VibrationManager.supportControllerVibration(),
onChange: VibrationManager.updateGlobalVars, onChange: VibrationManager.updateGlobalVars,
}, },
{ {
pref: PrefKey.CONTROLLER_DEVICE_VIBRATION, pref: PrefKey.CONTROLLER_DEVICE_VIBRATION,
label: t('device-vibration'),
unsupported: !VibrationManager.supportDeviceVibration(), unsupported: !VibrationManager.supportDeviceVibration(),
onChange: VibrationManager.updateGlobalVars, onChange: VibrationManager.updateGlobalVars,
}, },
(VibrationManager.supportControllerVibration() || VibrationManager.supportDeviceVibration()) && { (VibrationManager.supportControllerVibration() || VibrationManager.supportDeviceVibration()) && {
pref: PrefKey.CONTROLLER_VIBRATION_INTENSITY, pref: PrefKey.CONTROLLER_VIBRATION_INTENSITY,
label: t('vibration-intensity'),
unsupported: !VibrationManager.supportDeviceVibration(), unsupported: !VibrationManager.supportDeviceVibration(),
onChange: VibrationManager.updateGlobalVars, onChange: VibrationManager.updateGlobalVars,
}, },
@ -217,7 +219,14 @@ function setupQuickSettingsBar() {
for (const key in data.layouts) { for (const key in data.layouts) {
const layout = data.layouts[key]; const layout = data.layouts[key];
const $option = CE('option', {value: key}, layout.name); let name;
if (layout.author) {
name = `${layout.name} (${layout.author})`;
} else {
name = layout.name;
}
const $option = CE('option', {value: key}, name);
$fragment.appendChild($option); $fragment.appendChild($option);
} }
@ -232,54 +241,58 @@ function setupQuickSettingsBar() {
], ],
}, },
{
icon: BxIcon.COMMAND,
group: 'shortcuts',
items: [
{
group: 'shortcuts_controller',
label: t('controller-shortcuts'),
content: ControllerShortcut.renderSettings(),
},
],
},
{ {
icon: BxIcon.STREAM_STATS, icon: BxIcon.STREAM_STATS,
group: 'stats', group: 'stats',
items: [ items: [
{ {
group: 'stats', group: 'stats',
label: t('menu-stream-stats'), label: t('stream-stats'),
help_url: 'https://better-xcloud.github.io/stream-stats/', help_url: 'https://better-xcloud.github.io/stream-stats/',
items: [ items: [
{ {
pref: PrefKey.STATS_SHOW_WHEN_PLAYING, pref: PrefKey.STATS_SHOW_WHEN_PLAYING,
label: t('show-stats-on-startup'),
}, },
{ {
pref: PrefKey.STATS_QUICK_GLANCE, pref: PrefKey.STATS_QUICK_GLANCE,
label: '👀 ' + t('enable-quick-glance-mode'),
onChange: (e: InputEvent) => { onChange: (e: InputEvent) => {
(e.target! as HTMLInputElement).checked ? StreamStats.quickGlanceSetup() : StreamStats.quickGlanceStop(); (e.target! as HTMLInputElement).checked ? StreamStats.quickGlanceSetup() : StreamStats.quickGlanceStop();
}, },
}, },
{ {
pref: PrefKey.STATS_ITEMS, pref: PrefKey.STATS_ITEMS,
label: t('stats'),
onChange: StreamStats.refreshStyles, onChange: StreamStats.refreshStyles,
}, },
{ {
pref: PrefKey.STATS_POSITION, pref: PrefKey.STATS_POSITION,
label: t('position'),
onChange: StreamStats.refreshStyles, onChange: StreamStats.refreshStyles,
}, },
{ {
pref: PrefKey.STATS_TEXT_SIZE, pref: PrefKey.STATS_TEXT_SIZE,
label: t('text-size'),
onChange: StreamStats.refreshStyles, onChange: StreamStats.refreshStyles,
}, },
{ {
pref: PrefKey.STATS_OPACITY, pref: PrefKey.STATS_OPACITY,
label: t('opacity'),
onChange: StreamStats.refreshStyles, onChange: StreamStats.refreshStyles,
}, },
{ {
pref: PrefKey.STATS_TRANSPARENT, pref: PrefKey.STATS_TRANSPARENT,
label: t('transparent-background'),
onChange: StreamStats.refreshStyles, onChange: StreamStats.refreshStyles,
}, },
{ {
pref: PrefKey.STATS_CONDITIONAL_FORMATTING, pref: PrefKey.STATS_CONDITIONAL_FORMATTING,
label: t('conditional-formatting'),
onChange: StreamStats.refreshStyles, onChange: StreamStats.refreshStyles,
}, },
], ],
@ -291,9 +304,9 @@ function setupQuickSettingsBar() {
let $tabs: HTMLElement; let $tabs: HTMLElement;
let $settings: HTMLElement; let $settings: HTMLElement;
const $wrapper = CE<HTMLElement>('div', {'class': 'bx-quick-settings-bar bx-gone'}, const $wrapper = CE<HTMLElement>('div', {'class': 'bx-stream-settings-dialog bx-gone'},
$tabs = CE<HTMLElement>('div', {'class': 'bx-quick-settings-tabs'}), $tabs = CE<HTMLElement>('div', {'class': 'bx-stream-settings-tabs'}),
$settings = CE<HTMLElement>('div', {'class': 'bx-quick-settings-tab-contents'}), $settings = CE<HTMLElement>('div', {'class': 'bx-stream-settings-tab-contents'}),
); );
for (const settingTab of SETTINGS_UI) { for (const settingTab of SETTINGS_UI) {
@ -368,10 +381,14 @@ function setupQuickSettingsBar() {
$control = toPrefElement(pref, setting.onChange, setting.params); $control = toPrefElement(pref, setting.onChange, setting.params);
} }
const $content = CE<HTMLElement>('div', {'class': 'bx-quick-settings-row', 'data-type': settingGroup.group}, const label = Preferences.SETTINGS[pref as PrefKey]?.label || setting.label;
const note = Preferences.SETTINGS[pref as PrefKey]?.note || setting.note;
const $content = CE('div', {'class': 'bx-stream-settings-row', 'data-type': settingGroup.group},
CE('label', {for: `bx_setting_${pref}`}, CE('label', {for: `bx_setting_${pref}`},
setting.label, label,
setting.unsupported && CE<HTMLElement>('div', {'class': 'bx-quick-settings-bar-note'}, t('browser-unsupported-feature')), note && CE('div', {'class': 'bx-stream-settings-dialog-note'}, note),
setting.unsupported && CE('div', {'class': 'bx-stream-settings-dialog-note'}, t('browser-unsupported-feature')),
), ),
!setting.unsupported && $control, !setting.unsupported && $control,
); );
@ -421,53 +438,91 @@ export function updateVideoPlayerCss() {
// Apply video filters to screenshots // Apply video filters to screenshots
if (getPref(PrefKey.SCREENSHOT_APPLY_FILTERS)) { if (getPref(PrefKey.SCREENSHOT_APPLY_FILTERS)) {
STATES.currentStream.$screenshotCanvas!.getContext('2d')!.filter = filters; Screenshot.updateCanvasFilters(filters);
}
const PREF_RATIO = getPref(PrefKey.VIDEO_RATIO);
if (PREF_RATIO && PREF_RATIO !== '16:9') {
if (PREF_RATIO.includes(':')) {
videoCss += `aspect-ratio: ${PREF_RATIO.replace(':', '/')}; object-fit: unset !important;`;
const tmp = PREF_RATIO.split(':');
const ratio = parseFloat(tmp[0]) / parseFloat(tmp[1]);
const maxRatio = window.innerWidth / window.innerHeight;
if (ratio < maxRatio) {
videoCss += 'width: fit-content !important;'
} else {
videoCss += 'height: fit-content !important;'
}
} else {
videoCss += `object-fit: ${PREF_RATIO} !important;`;
}
} }
let css = ''; let css = '';
if (videoCss) { if (videoCss) {
css = ` css = `
div[data-testid="media-container"] {
display: flex;
}
#game-stream video { #game-stream video {
margin: 0 auto;
align-self: center;
background: #000;
${videoCss} ${videoCss}
} }
`; `;
} }
$elm.textContent = css; $elm.textContent = css;
resizeVideoPlayer();
} }
export function setupBxUi() { function resizeVideoPlayer() {
const $video = STATES.currentStream.$video;
if (!$video || !$video.parentElement) {
return;
}
const PREF_RATIO = getPref(PrefKey.VIDEO_RATIO);
if (PREF_RATIO.includes(':')) {
const tmp = PREF_RATIO.split(':');
// Get preferred ratio
const videoRatio = parseFloat(tmp[0]) / parseFloat(tmp[1]);
let width = 0;
let height = 0;
// Get parent's ratio
const parentRect = $video.parentElement.getBoundingClientRect();
const parentRatio = parentRect.width / parentRect.height;
// Get target width & height
if (parentRatio > videoRatio) {
height = parentRect.height;
width = height * videoRatio;
} else {
width = parentRect.width;
height = width / videoRatio;
}
// Prevent floating points
width = Math.min(parentRect.width, Math.ceil(width));
height = Math.min(parentRect.height, Math.ceil(height));
// Update size
$video.style.width = `${width}px`;
$video.style.height = `${height}px`;
$video.style.objectFit = 'fill';
} else {
$video.style.width = '100%';
$video.style.height = '100%';
$video.style.objectFit = PREF_RATIO;
}
}
function preloadFonts() {
const $link = CE<HTMLLinkElement>('link', {
rel: 'preload',
href: 'https://redphx.github.io/better-xcloud/fonts/promptfont.otf',
as: 'font',
type: 'font/otf',
crossorigin: '',
});
document.querySelector('head')?.appendChild($link);
}
export function setupStreamUi() {
// Prevent initializing multiple times // Prevent initializing multiple times
if (!document.querySelector('.bx-quick-settings-bar')) { if (!document.querySelector('.bx-stream-settings-dialog')) {
preloadFonts();
window.addEventListener('resize', updateVideoPlayerCss); window.addEventListener('resize', updateVideoPlayerCss);
setupQuickSettingsBar(); setupStreamSettingsDialog();
setupScreenshotButton();
StreamStats.render(); StreamStats.render();
Screenshot.setup();
} }
updateVideoPlayerCss(); updateVideoPlayerCss();

31
src/types/index.d.ts vendored
View File

@ -1,6 +1,8 @@
// Get type of an array's element // Get type of an array's element
type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never; type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
type PartialRecord<K extends keyof any, T> = Partial<Record<K, T>>
interface Window { interface Window {
AppInterface: any; AppInterface: any;
BX_FLAGS?: BxFlags; BX_FLAGS?: BxFlags;
@ -25,7 +27,9 @@ type BxStates = {
isPlaying: boolean; isPlaying: boolean;
appContext: any | null; appContext: any | null;
serverRegions: any; serverRegions: any;
hasTouchSupport: boolean; hasTouchSupport: boolean;
browserHasTouchSupport: boolean;
currentStream: Partial<{ currentStream: Partial<{
titleId: string; titleId: string;
@ -34,7 +38,6 @@ type BxStates = {
titleInfo: XcloudTitleInfo; titleInfo: XcloudTitleInfo;
$video: HTMLVideoElement | null; $video: HTMLVideoElement | null;
$screenshotCanvas: HTMLCanvasElement | null;
peerConnection: RTCPeerConnection; peerConnection: RTCPeerConnection;
audioContext: AudioContext | null; audioContext: AudioContext | null;
@ -56,6 +59,7 @@ type XcloudTitleInfo = {
details: { details: {
productId: string; productId: string;
supportedInputTypes: InputType[]; supportedInputTypes: InputType[];
supportedTabs: any[];
hasTouchSupport: boolean; hasTouchSupport: boolean;
hasFakeTouchSupport: boolean; hasFakeTouchSupport: boolean;
hasMkbSupport: boolean; hasMkbSupport: boolean;
@ -68,5 +72,26 @@ type XcloudTitleInfo = {
}; };
}; };
declare module "*.svg"; declare module '*.js';
declare module "*.styl"; declare module '*.svg';
declare module '*.styl';
type MkbMouseMove = {
movementX: number;
movementY: number;
}
type MkbMouseClick = {
key: {
code: string;
name: string;
} | null;
pressed: boolean;
}
type MkbMouseWheel = {
key: {
code: string;
name: string;
} | null;
}

View File

@ -5,8 +5,8 @@ export type PreferenceSetting = {
unsupported?: string | boolean; unsupported?: string | boolean;
note?: string | HTMLElement; note?: string | HTMLElement;
type?: SettingElementType; type?: SettingElementType;
ready?: () => void; ready?: (setting: PreferenceSetting) => void;
migrate?: (savedPrefs: any, value: any) => {}; migrate?: (this: Preferences, savedPrefs: any, value: any) => void;
min?: number; min?: number;
max?: number; max?: number;
steps?: number; steps?: number;

View File

@ -13,13 +13,14 @@ export enum BxEvent {
STREAM_STOPPED = 'bx-stream-stopped', STREAM_STOPPED = 'bx-stream-stopped',
STREAM_ERROR_PAGE = 'bx-stream-error-page', STREAM_ERROR_PAGE = 'bx-stream-error-page',
STREAM_MENU_SHOWN = 'bx-stream-menu-shown',
STREAM_MENU_HIDDEN = 'bx-stream-menu-hidden',
STREAM_WEBRTC_CONNECTED = 'bx-stream-webrtc-connected', STREAM_WEBRTC_CONNECTED = 'bx-stream-webrtc-connected',
STREAM_WEBRTC_DISCONNECTED = 'bx-stream-webrtc-disconnected', STREAM_WEBRTC_DISCONNECTED = 'bx-stream-webrtc-disconnected',
// STREAM_EVENT_TARGET_READY = 'bx-stream-event-target-ready',
STREAM_SESSION_READY = 'bx-stream-session-ready',
CUSTOM_TOUCH_LAYOUTS_LOADED = 'bx-custom-touch-layouts-loaded', CUSTOM_TOUCH_LAYOUTS_LOADED = 'bx-custom-touch-layouts-loaded',
TOUCH_LAYOUT_MANAGER_READY = 'bx-touch-layout-manager-ready',
REMOTE_PLAY_READY = 'bx-remote-play-ready', REMOTE_PLAY_READY = 'bx-remote-play-ready',
REMOTE_PLAY_FAILED = 'bx-remote-play-failed', REMOTE_PLAY_FAILED = 'bx-remote-play-failed',
@ -27,6 +28,22 @@ export enum BxEvent {
XCLOUD_SERVERS_READY = 'bx-servers-ready', XCLOUD_SERVERS_READY = 'bx-servers-ready',
DATA_CHANNEL_CREATED = 'bx-data-channel-created', DATA_CHANNEL_CREATED = 'bx-data-channel-created',
GAME_BAR_ACTION_ACTIVATED = 'bx-game-bar-action-activated',
MICROPHONE_STATE_CHANGED = 'bx-microphone-state-changed',
CAPTURE_SCREENSHOT = 'bx-capture-screenshot',
GAINNODE_VOLUME_CHANGED = 'bx-gainnode-volume-changed',
// xCloud Dialog events
XCLOUD_DIALOG_SHOWN = 'bx-xcloud-dialog-shown',
XCLOUD_DIALOG_DISMISSED = 'bx-xcloud-dialog-dismissed',
XCLOUD_POLLING_MODE_CHANGED = 'bx-xcloud-polling-mode-changed',
}
export enum XcloudEvent {
MICROPHONE_STATE_CHANGED = 'microphoneStateChanged',
} }
export namespace BxEvent { export namespace BxEvent {
@ -48,3 +65,5 @@ export namespace BxEvent {
target.dispatchEvent(event); target.dispatchEvent(event);
} }
} }
(window as any).BxEvent = BxEvent;

View File

@ -1,9 +1,11 @@
import { ControllerShortcut } from "@/modules/controller-shortcut";
import { BxEvent } from "@utils/bx-event"; import { BxEvent } from "@utils/bx-event";
import { STATES } from "@utils/global"; import { STATES } from "@utils/global";
import { getPref, PrefKey } from "@utils/preferences"; import { getPref, PrefKey } from "@utils/preferences";
import { UserAgent } from "@utils/user-agent"; import { UserAgent } from "@utils/user-agent";
import { BxLogger } from "./bx-logger";
enum InputType { export enum InputType {
CONTROLLER = 'Controller', CONTROLLER = 'Controller',
MKB = 'MKB', MKB = 'MKB',
CUSTOM_TOUCH_OVERLAY = 'CustomTouchOverlay', CUSTOM_TOUCH_OVERLAY = 'CustomTouchOverlay',
@ -13,38 +15,23 @@ enum InputType {
} }
export const BxExposed = { export const BxExposed = {
onPollingModeChanged: (mode: 'All' | 'None') => {
if (!STATES.isPlaying) {
return false;
}
const $screenshotBtn = document.querySelector('.bx-screenshot-button');
const $touchControllerBar = document.getElementById('bx-touch-controller-bar');
if (mode !== 'None') {
// Hide screenshot button
$screenshotBtn && $screenshotBtn.classList.add('bx-gone');
// Hide touch controller bar
$touchControllerBar && $touchControllerBar.classList.add('bx-gone');
} else {
// Show screenshot button
$screenshotBtn && $screenshotBtn.classList.remove('bx-gone');
// Show touch controller bar
$touchControllerBar && $touchControllerBar.classList.remove('bx-gone');
}
},
getTitleInfo: () => STATES.currentStream.titleInfo, getTitleInfo: () => STATES.currentStream.titleInfo,
modifyTitleInfo: (titleInfo: XcloudTitleInfo): XcloudTitleInfo => { modifyTitleInfo: (titleInfo: XcloudTitleInfo): XcloudTitleInfo => {
// Clone the object since the original is read-only // Clone the object since the original is read-only
titleInfo = structuredClone(titleInfo); titleInfo = structuredClone(titleInfo);
let supportedInputTypes = titleInfo.details.supportedInputTypes;
// Remove native MKB support on mobile browsers or by user's choice
if (getPref(PrefKey.NATIVE_MKB_DISABLED) || UserAgent.isMobile()) {
supportedInputTypes = supportedInputTypes.filter(i => i !== InputType.MKB);
}
titleInfo.details.hasMkbSupport = supportedInputTypes.includes(InputType.MKB);
if (STATES.hasTouchSupport) { if (STATES.hasTouchSupport) {
let touchControllerAvailability = getPref(PrefKey.STREAM_TOUCH_CONTROLLER); let touchControllerAvailability = getPref(PrefKey.STREAM_TOUCH_CONTROLLER);
let supportedInputTypes = titleInfo.details.supportedInputTypes;
// Disable touch control when gamepad found // Disable touch control when gamepad found
if (touchControllerAvailability !== 'off' && getPref(PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF)) { if (touchControllerAvailability !== 'off' && getPref(PrefKey.STREAM_TOUCH_CONTROLLER_AUTO_OFF)) {
@ -61,18 +48,14 @@ export const BxExposed = {
gamepadFound && (touchControllerAvailability = 'off'); gamepadFound && (touchControllerAvailability = 'off');
} }
// Remove MKB support on mobile browsers
if (UserAgent.isMobile()) {
supportedInputTypes = supportedInputTypes.filter(i => i !== InputType.MKB);
}
if (touchControllerAvailability === 'off') { if (touchControllerAvailability === 'off') {
// Disable touch on all games (not native touch) // Disable touch on all games (not native touch)
supportedInputTypes = supportedInputTypes.filter(i => i !== InputType.CUSTOM_TOUCH_OVERLAY && i !== InputType.GENERIC_TOUCH); supportedInputTypes = supportedInputTypes.filter(i => i !== InputType.CUSTOM_TOUCH_OVERLAY && i !== InputType.GENERIC_TOUCH);
// Empty TABs
titleInfo.details.supportedTabs = [];
} }
// Pre-check supported input types // Pre-check supported input types
titleInfo.details.hasMkbSupport = supportedInputTypes.includes(InputType.MKB);
titleInfo.details.hasTouchSupport = supportedInputTypes.includes(InputType.NATIVE_TOUCH) || titleInfo.details.hasTouchSupport = supportedInputTypes.includes(InputType.NATIVE_TOUCH) ||
supportedInputTypes.includes(InputType.CUSTOM_TOUCH_OVERLAY) || supportedInputTypes.includes(InputType.CUSTOM_TOUCH_OVERLAY) ||
supportedInputTypes.includes(InputType.GENERIC_TOUCH); supportedInputTypes.includes(InputType.GENERIC_TOUCH);
@ -82,14 +65,43 @@ export const BxExposed = {
titleInfo.details.hasFakeTouchSupport = true; titleInfo.details.hasFakeTouchSupport = true;
supportedInputTypes.push(InputType.GENERIC_TOUCH); supportedInputTypes.push(InputType.GENERIC_TOUCH);
} }
}
titleInfo.details.supportedInputTypes = supportedInputTypes; titleInfo.details.supportedInputTypes = supportedInputTypes;
}
// Save this info in STATES // Save this info in STATES
STATES.currentStream.titleInfo = titleInfo; STATES.currentStream.titleInfo = titleInfo;
BxEvent.dispatch(window, BxEvent.TITLE_INFO_READY); BxEvent.dispatch(window, BxEvent.TITLE_INFO_READY);
return titleInfo; return titleInfo;
},
setupGainNode: ($media: HTMLMediaElement, audioStream: MediaStream) => {
if ($media instanceof HTMLAudioElement) {
$media.muted = true;
$media.addEventListener('playing', e => {
$media.muted = true;
$media.pause();
});
} else {
$media.muted = true;
$media.addEventListener('playing', e => {
$media.muted = true;
});
} }
try {
const audioCtx = STATES.currentStream.audioContext!;
const source = audioCtx.createMediaStreamSource(audioStream);
const gainNode = audioCtx.createGain(); // call monkey-patched createGain() in BxAudioContext
source.connect(gainNode).connect(audioCtx.destination);
} catch (e) {
BxLogger.error('setupGainNode', e);
STATES.currentStream.audioGainNode = null;
}
},
handleControllerShortcut: ControllerShortcut.handle,
resetControllerShortcut: ControllerShortcut.reset,
}; };

View File

@ -23,3 +23,5 @@ export const BX_FLAGS = Object.assign(DEFAULT_FLAGS, window.BX_FLAGS || {});
try { try {
delete window.BX_FLAGS; delete window.BX_FLAGS;
} catch (e) {} } catch (e) {}
export const NATIVE_FETCH = window.fetch;

View File

@ -1,3 +1,4 @@
import iconCommand from "@assets/svg/command.svg" with { type: "text" };
import iconController from "@assets/svg/controller.svg" with { type: "text" }; import iconController from "@assets/svg/controller.svg" with { type: "text" };
import iconCopy from "@assets/svg/copy.svg" with { type: "text" }; import iconCopy from "@assets/svg/copy.svg" with { type: "text" };
import iconCursorText from "@assets/svg/cursor-text.svg" with { type: "text" }; import iconCursorText from "@assets/svg/cursor-text.svg" with { type: "text" };
@ -11,10 +12,20 @@ import iconRemotePlay from "@assets/svg/remote-play.svg" with { type: "text" };
import iconStreamSettings from "@assets/svg/stream-settings.svg" with { type: "text" }; import iconStreamSettings from "@assets/svg/stream-settings.svg" with { type: "text" };
import iconStreamStats from "@assets/svg/stream-stats.svg" with { type: "text" }; import iconStreamStats from "@assets/svg/stream-stats.svg" with { type: "text" };
import iconTrash from "@assets/svg/trash.svg" with { type: "text" }; import iconTrash from "@assets/svg/trash.svg" with { type: "text" };
import iconTouchControlEnable from "@assets/svg/touch-control-enable.svg" with { type: "text" };
import iconTouchControlDisable from "@assets/svg/touch-control-disable.svg" with { type: "text" };
// Game Bar
import iconCaretLeft from "@assets/svg/caret-left.svg" with { type: "text" };
import iconCaretRight from "@assets/svg/caret-right.svg" with { type: "text" };
import iconCamera from "@assets/svg/camera.svg" with { type: "text" };
import iconMicrophone from "@assets/svg/microphone.svg" with { type: "text" };
import iconMicrophoneMuted from "@assets/svg/microphone-slash.svg" with { type: "text" };
export const BxIcon = { export const BxIcon = {
STREAM_SETTINGS: iconStreamSettings, STREAM_SETTINGS: iconStreamSettings,
STREAM_STATS: iconStreamStats, STREAM_STATS: iconStreamStats,
COMMAND: iconCommand,
CONTROLLER: iconController, CONTROLLER: iconController,
DISPLAY: iconDisplay, DISPLAY: iconDisplay,
MOUSE: iconMouse, MOUSE: iconMouse,
@ -28,5 +39,13 @@ export const BxIcon = {
REMOTE_PLAY: iconRemotePlay, REMOTE_PLAY: iconRemotePlay,
// HAND_TAP = '<path d="M6.537 8.906c0-4.216 3.469-7.685 7.685-7.685s7.685 3.469 7.685 7.685M7.719 30.778l-4.333-7.389C3.133 22.944 3 22.44 3 21.928a2.97 2.97 0 0 1 2.956-2.956 2.96 2.96 0 0 1 2.55 1.461l2.761 4.433V8.906a2.97 2.97 0 0 1 2.956-2.956 2.97 2.97 0 0 1 2.956 2.956v8.276a2.97 2.97 0 0 1 2.956-2.956 2.97 2.97 0 0 1 2.956 2.956v2.365a2.97 2.97 0 0 1 2.956-2.956A2.97 2.97 0 0 1 29 19.547v5.32c0 3.547-1.182 5.911-1.182 5.911"/>', // Game Bar
CARET_LEFT: iconCaretLeft,
CARET_RIGHT: iconCaretRight,
SCREENSHOT: iconCamera,
TOUCH_CONTROL_ENABLE: iconTouchControlEnable,
TOUCH_CONTROL_DISABLE: iconTouchControlDisable,
MICROPHONE: iconMicrophone,
MICROPHONE_MUTED: iconMicrophoneMuted,
} as const; } as const;

View File

@ -20,7 +20,7 @@ export class BxLogger {
} }
static #log(color: TextColor, tag: string, ...args: any) { static #log(color: TextColor, tag: string, ...args: any) {
console.log('%c' + BxLogger.#PREFIX, 'color:' + color + ';font-weight:bold;', tag, '-', ...args); console.log(`%c${BxLogger.#PREFIX}`, `color:${color};font-weight:bold;`, tag, '//', ...args);
} }
} }

View File

@ -0,0 +1,4 @@
export enum GamePassCloudGallery {
TOUCH = '9c86f07a-f3e8-45ad-82a0-a1f759597059',
ALL = '29a81209-df6f-41fd-a528-2ae6b91f719c',
}

View File

@ -1,13 +1,24 @@
import { UserAgent } from "./user-agent";
export const SCRIPT_VERSION = Bun.env.SCRIPT_VERSION; export const SCRIPT_VERSION = Bun.env.SCRIPT_VERSION;
export const SCRIPT_HOME = 'https://github.com/redphx/better-xcloud'; export const SCRIPT_HOME = 'https://github.com/redphx/better-xcloud';
export const AppInterface = window.AppInterface; export const AppInterface = window.AppInterface;
UserAgent.init();
const userAgent = window.navigator.userAgent.toLowerCase();
const isTv = userAgent.includes('smart-tv') || userAgent.includes('smarttv') || /\baft.*\b/.test(userAgent);
const isVr = window.navigator.userAgent.includes('VR') && window.navigator.userAgent.includes('OculusBrowser');
const browserHasTouchSupport = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
const hasTouchSupport = !isTv && !isVr && browserHasTouchSupport;
export const STATES: BxStates = { export const STATES: BxStates = {
isPlaying: false, isPlaying: false,
appContext: {}, appContext: {},
serverRegions: {}, serverRegions: {},
hasTouchSupport: ('ontouchstart' in window || navigator.maxTouchPoints > 0), hasTouchSupport: hasTouchSupport,
browserHasTouchSupport: browserHasTouchSupport,
currentStream: {}, currentStream: {},
remotePlay: {}, remotePlay: {},

View File

@ -77,7 +77,7 @@ export const createButton = <T=HTMLButtonElement>(options: BxButton): T => {
$btn.href = options.url; $btn.href = options.url;
$btn.target = '_blank'; $btn.target = '_blank';
} else { } else {
$btn = CE('button', {'class': 'bx-button'}) as HTMLButtonElement; $btn = CE('button', {'class': 'bx-button', type: 'button'}) as HTMLButtonElement;
} }
const style = (options.style || 0) as number; const style = (options.style || 0) as number;
@ -96,5 +96,13 @@ export const createButton = <T=HTMLButtonElement>(options: BxButton): T => {
return $btn as T; return $btn as T;
} }
export function escapeHtml(html: string): string {
const text = document.createTextNode(html);
const $span = document.createElement('span');
$span.appendChild(text);
return $span.innerHTML;
}
export const CTN = document.createTextNode.bind(document); export const CTN = document.createTextNode.bind(document);
window.BX_CE = createElement; window.BX_CE = createElement;

View File

@ -1,8 +1,8 @@
import { BxEvent } from "@utils/bx-event"; import { BxEvent } from "@utils/bx-event";
import { getPref, PrefKey } from "@utils/preferences"; import { getPref, PrefKey } from "@utils/preferences";
import { STATES } from "@utils/global"; import { STATES } from "@utils/global";
import { UserAgent } from "@utils/user-agent";
import { BxLogger } from "@utils/bx-logger"; import { BxLogger } from "@utils/bx-logger";
import { patchSdpBitrate } from "./sdp";
export function patchVideoApi() { export function patchVideoApi() {
const PREF_SKIP_SPLASH_VIDEO = getPref(PrefKey.SKIP_SPLASH_VIDEO); const PREF_SKIP_SPLASH_VIDEO = getPref(PrefKey.SKIP_SPLASH_VIDEO);
@ -97,6 +97,22 @@ export function patchRtcPeerConnection() {
return dataChannel; return dataChannel;
} }
const nativeSetLocalDescription = RTCPeerConnection.prototype.setLocalDescription;
RTCPeerConnection.prototype.setLocalDescription = function(description?: RTCLocalSessionDescriptionInit): Promise<void> {
// set maximum bitrate
try {
const maxVideoBitrate = getPref(PrefKey.BITRATE_VIDEO_MAX);
if (maxVideoBitrate > 0) {
arguments[0].sdp = patchSdpBitrate(arguments[0].sdp, Math.round(maxVideoBitrate / 1000));
}
} catch (e) {
BxLogger.error('setLocalDescription', e);
}
// @ts-ignore
return nativeSetLocalDescription.apply(this, arguments);
};
const OrgRTCPeerConnection = window.RTCPeerConnection; const OrgRTCPeerConnection = window.RTCPeerConnection;
// @ts-ignore // @ts-ignore
window.RTCPeerConnection = function() { window.RTCPeerConnection = function() {
@ -104,10 +120,6 @@ export function patchRtcPeerConnection() {
STATES.currentStream.peerConnection = conn; STATES.currentStream.peerConnection = conn;
conn.addEventListener('connectionstatechange', e => { conn.addEventListener('connectionstatechange', e => {
if (conn.connectionState === 'connecting') {
STATES.currentStream.audioGainNode = null;
}
BxLogger.info('connectionstatechange', conn.connectionState); BxLogger.info('connectionstatechange', conn.connectionState);
}); });
return conn; return conn;
@ -115,46 +127,95 @@ export function patchRtcPeerConnection() {
} }
export function patchAudioContext() { export function patchAudioContext() {
if (UserAgent.isSafari(true)) { const OrgAudioContext = window.AudioContext;
const nativeCreateGain = window.AudioContext.prototype.createGain; const nativeCreateGain = OrgAudioContext.prototype.createGain;
window.AudioContext.prototype.createGain = function() {
// @ts-ignore
window.AudioContext = function(options?: AudioContextOptions | undefined): AudioContext {
const ctx = new OrgAudioContext(options);
BxLogger.info('patchAudioContext', ctx, options);
ctx.createGain = function() {
const gainNode = nativeCreateGain.apply(this); const gainNode = nativeCreateGain.apply(this);
gainNode.gain.value = getPref(PrefKey.AUDIO_VOLUME) / 100; gainNode.gain.value = getPref(PrefKey.AUDIO_VOLUME) / 100;
STATES.currentStream.audioGainNode = gainNode; STATES.currentStream.audioGainNode = gainNode;
return gainNode; return gainNode;
} }
}
const OrgAudioContext = window.AudioContext;
// @ts-ignore
window.AudioContext = function() {
const ctx = new OrgAudioContext();
STATES.currentStream.audioContext = ctx; STATES.currentStream.audioContext = ctx;
STATES.currentStream.audioGainNode = null;
return ctx; return ctx;
} }
}
const nativePlay = HTMLAudioElement.prototype.play; /**
HTMLAudioElement.prototype.play = function() { * Disable telemetry flags in meversion.js
this.muted = true; */
export function patchMeControl() {
const overrideConfigs = {
enableAADTelemetry: false,
enableTelemetry: false,
telEvs: '',
oneDSUrl: '',
};
const promise = nativePlay.apply(this); const MSA = {
if (STATES.currentStream.audioGainNode) { MeControl: {},
return promise; };
const MeControl = {};
const MsaHandler: ProxyHandler<any> = {
get(target, prop, receiver) {
return target[prop];
},
set(obj, prop, value) {
if (prop === 'MeControl' && value.Config) {
value.Config = Object.assign(value.Config, overrideConfigs);
} }
this.addEventListener('playing', e => (e.target as HTMLAudioElement).pause()); obj[prop] = value;
return true;
},
};
const audioCtx = STATES.currentStream.audioContext!; const MeControlHandler: ProxyHandler<any> = {
// TOOD: check srcObject get(target, prop, receiver) {
const audioStream = audioCtx.createMediaStreamSource(this.srcObject as any); return target[prop];
const gainNode = audioCtx.createGain(); },
audioStream.connect(gainNode); set(obj, prop, value) {
gainNode.connect(audioCtx.destination); if (prop === 'Config') {
gainNode.gain.value = getPref(PrefKey.AUDIO_VOLUME) / 100; value = Object.assign(value, overrideConfigs);
STATES.currentStream.audioGainNode = gainNode; }
return promise; obj[prop] = value;
return true;
},
};
(window as any).MSA = new Proxy(MSA, MsaHandler);
(window as any).MeControl = new Proxy(MeControl, MeControlHandler);
}
/**
* Use power-saving flags for touch control
*/
export function patchCanvasContext() {
const nativeGetContext = HTMLCanvasElement.prototype.getContext;
// @ts-ignore
HTMLCanvasElement.prototype.getContext = function(contextType: string, contextAttributes?: any) {
if (contextType.includes('webgl')) {
contextAttributes = contextAttributes || {};
contextAttributes.antialias = false;
// Use low-power profile for touch controller
if (contextAttributes.powerPreference === 'high-performance') {
contextAttributes.powerPreference = 'low-power';
}
}
return nativeGetContext.apply(this, [contextType, contextAttributes]);
} }
} }

View File

@ -1,5 +1,5 @@
import { BxEvent } from "@utils/bx-event"; import { BxEvent } from "@utils/bx-event";
import { BX_FLAGS } from "@utils/bx-flags"; import { BX_FLAGS, NATIVE_FETCH } from "@utils/bx-flags";
import { LoadingScreen } from "@modules/loading-screen"; import { LoadingScreen } from "@modules/loading-screen";
import { PrefKey, getPref } from "@utils/preferences"; import { PrefKey, getPref } from "@utils/preferences";
import { RemotePlay } from "@modules/remote-play"; import { RemotePlay } from "@modules/remote-play";
@ -7,8 +7,9 @@ import { StreamBadges } from "@modules/stream/stream-badges";
import { TouchController } from "@modules/touch-controller"; import { TouchController } from "@modules/touch-controller";
import { STATES } from "@utils/global"; import { STATES } from "@utils/global";
import { getPreferredServerRegion } from "@utils/region"; import { getPreferredServerRegion } from "@utils/region";
import { GamePassCloudGallery } from "./gamepass-gallery";
export const NATIVE_FETCH = window.fetch; import { InputType } from "./bx-exposed";
import { UserAgent } from "./user-agent";
enum RequestType { enum RequestType {
XCLOUD = 'xcloud', XCLOUD = 'xcloud',
@ -187,7 +188,7 @@ class XhomeInterceptor {
let hasTouchSupport = inputConfigs.supportedTabs.length > 0; let hasTouchSupport = inputConfigs.supportedTabs.length > 0;
if (!hasTouchSupport) { if (!hasTouchSupport) {
const supportedInputTypes = inputConfigs.supportedInputTypes; const supportedInputTypes = inputConfigs.supportedInputTypes;
hasTouchSupport = supportedInputTypes.includes('NativeTouch'); hasTouchSupport = supportedInputTypes.includes(InputType.NATIVE_TOUCH) || supportedInputTypes.includes(InputType.CUSTOM_TOUCH_OVERLAY);
} }
if (hasTouchSupport) { if (hasTouchSupport) {
@ -437,6 +438,17 @@ class XcloudInterceptor {
overrides.inputConfiguration = overrides.inputConfiguration || {}; overrides.inputConfiguration = overrides.inputConfiguration || {};
overrides.inputConfiguration.enableVibration = true; overrides.inputConfiguration.enableVibration = true;
if (getPref(PrefKey.NATIVE_MKB_DISABLED) || UserAgent.isMobile()) {
overrides.inputConfiguration = Object.assign(overrides.inputConfiguration, {
enableMouseInput: false,
enableAbsoluteMouse: false,
enableKeyboardInput: false,
});
}
overrides.videoConfiguration = overrides.videoConfiguration || {};
overrides.videoConfiguration.setCodecPreferences = true;
// Enable touch controller // Enable touch controller
if (TouchController.isEnabled()) { if (TouchController.isEnabled()) {
overrides.inputConfiguration.enableTouchInput = true; overrides.inputConfiguration.enableTouchInput = true;
@ -526,6 +538,8 @@ export function interceptHttpRequests() {
return nativeXhrSend.apply(this, arguments); return nativeXhrSend.apply(this, arguments);
}; };
let gamepassAllGames: string[] = [];
(window as any).BX_FETCH = window.fetch = async (request: RequestInfo | URL, init?: RequestInit): Promise<Response> => { (window as any).BX_FETCH = window.fetch = async (request: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
let url = (typeof request === 'string') ? request : (request as Request).url; let url = (typeof request === 'string') ? request : (request as Request).url;
@ -549,6 +563,56 @@ export function interceptHttpRequests() {
BxEvent.dispatch(window, BxEvent.STREAM_STARTING); BxEvent.dispatch(window, BxEvent.STREAM_STARTING);
} }
// Override experimentals
if (url.startsWith('https://emerald.xboxservices.com/xboxcomfd/experimentation')) {
try {
const response = await NATIVE_FETCH(request, init);
const json = await response.json();
const overrideTreatments: {[key: string]: boolean} = {};
if (getPref(PrefKey.UI_HOME_CONTEXT_MENU_DISABLED)) {
overrideTreatments['EnableHomeContextMenu'] = false;
}
for (const key in overrideTreatments) {
json.exp.treatments[key] = overrideTreatments[key]
}
response.json = () => Promise.resolve(json);
return response;
} catch (e) {
console.log(e);
}
}
// Add list of games with custom layouts to the official list
if (STATES.hasTouchSupport && url.includes('catalog.gamepass.com/sigls/')) {
const response = await NATIVE_FETCH(request, init);
const obj = await response.clone().json();
if (url.includes(GamePassCloudGallery.ALL)) {
for (let i = 1; i < obj.length; i++) {
gamepassAllGames.push(obj[i].id);
}
} else if (url.includes(GamePassCloudGallery.TOUCH)) {
try {
let customList = TouchController.getCustomList();
// Remove non-cloud games from the list
customList = customList.filter(id => gamepassAllGames.includes(id));
const newCustomList = customList.map(item => ({ id: item }));
obj.push(...newCustomList);
} catch (e) {
console.log(e);
}
}
response.json = () => Promise.resolve(obj);
return response;
}
let requestType: RequestType; let requestType: RequestType;
if (url.includes('/sessions/home') || url.includes('xhome.') || (STATES.remotePlay.isPlaying && url.endsWith('/inputconfigs'))) { if (url.includes('/sessions/home') || url.includes('xhome.') || (STATES.remotePlay.isPlaying && url.endsWith('/inputconfigs'))) {
requestType = RequestType.XHOME; requestType = RequestType.XHOME;

View File

@ -3,8 +3,8 @@ import { SUPPORTED_LANGUAGES, t } from "@utils/translation";
import { SettingElement, SettingElementType } from "@utils/settings"; import { SettingElement, SettingElementType } from "@utils/settings";
import { UserAgentProfile } from "@utils/user-agent"; import { UserAgentProfile } from "@utils/user-agent";
import { StreamStat } from "@modules/stream/stream-stats"; import { StreamStat } from "@modules/stream/stream-stats";
import type { PreferenceSettings } from "@/types/preferences"; import type { PreferenceSetting, PreferenceSettings } from "@/types/preferences";
import { STATES } from "@utils/global"; import { AppInterface, STATES } from "@utils/global";
export enum PrefKey { export enum PrefKey {
LAST_UPDATE_CHECK = 'version_last_check', LAST_UPDATE_CHECK = 'version_last_check',
@ -20,18 +20,22 @@ export enum PrefKey {
STREAM_CODEC_PROFILE = 'stream_codec_profile', STREAM_CODEC_PROFILE = 'stream_codec_profile',
USER_AGENT_PROFILE = 'user_agent_profile', USER_AGENT_PROFILE = 'user_agent_profile',
USER_AGENT_CUSTOM = 'user_agent_custom',
STREAM_SIMPLIFY_MENU = 'stream_simplify_menu', STREAM_SIMPLIFY_MENU = 'stream_simplify_menu',
STREAM_COMBINE_SOURCES = 'stream_combine_sources', STREAM_COMBINE_SOURCES = 'stream_combine_sources',
STREAM_TOUCH_CONTROLLER = 'stream_touch_controller', STREAM_TOUCH_CONTROLLER = 'stream_touch_controller',
STREAM_TOUCH_CONTROLLER_AUTO_OFF = 'stream_touch_controller_auto_off', STREAM_TOUCH_CONTROLLER_AUTO_OFF = 'stream_touch_controller_auto_off',
STREAM_TOUCH_CONTROLLER_DEFAULT_OPACITY = 'stream_touch_controller_default_opacity',
STREAM_TOUCH_CONTROLLER_STYLE_STANDARD = 'stream_touch_controller_style_standard', STREAM_TOUCH_CONTROLLER_STYLE_STANDARD = 'stream_touch_controller_style_standard',
STREAM_TOUCH_CONTROLLER_STYLE_CUSTOM = 'stream_touch_controller_style_custom', STREAM_TOUCH_CONTROLLER_STYLE_CUSTOM = 'stream_touch_controller_style_custom',
STREAM_DISABLE_FEEDBACK_DIALOG = 'stream_disable_feedback_dialog', STREAM_DISABLE_FEEDBACK_DIALOG = 'stream_disable_feedback_dialog',
BITRATE_VIDEO_MAX = 'bitrate_video_max',
GAME_BAR_POSITION = 'game_bar_position',
LOCAL_CO_OP_ENABLED = 'local_co_op_enabled', LOCAL_CO_OP_ENABLED = 'local_co_op_enabled',
// LOCAL_CO_OP_SEPARATE_TOUCH_CONTROLLER = 'local_co_op_separate_touch_controller', // LOCAL_CO_OP_SEPARATE_TOUCH_CONTROLLER = 'local_co_op_separate_touch_controller',
@ -40,12 +44,12 @@ export enum PrefKey {
CONTROLLER_DEVICE_VIBRATION = 'controller_device_vibration', CONTROLLER_DEVICE_VIBRATION = 'controller_device_vibration',
CONTROLLER_VIBRATION_INTENSITY = 'controller_vibration_intensity', CONTROLLER_VIBRATION_INTENSITY = 'controller_vibration_intensity',
NATIVE_MKB_DISABLED = 'native_mkb_disabled',
MKB_ENABLED = 'mkb_enabled', MKB_ENABLED = 'mkb_enabled',
MKB_HIDE_IDLE_CURSOR = 'mkb_hide_idle_cursor', MKB_HIDE_IDLE_CURSOR = 'mkb_hide_idle_cursor',
MKB_ABSOLUTE_MOUSE = 'mkb_absolute_mouse', MKB_ABSOLUTE_MOUSE = 'mkb_absolute_mouse',
MKB_DEFAULT_PRESET_ID = 'mkb_default_preset_id', MKB_DEFAULT_PRESET_ID = 'mkb_default_preset_id',
SCREENSHOT_BUTTON_POSITION = 'screenshot_button_position',
SCREENSHOT_APPLY_FILTERS = 'screenshot_apply_filters', SCREENSHOT_APPLY_FILTERS = 'screenshot_apply_filters',
BLOCK_TRACKING = 'block_tracking', BLOCK_TRACKING = 'block_tracking',
@ -61,6 +65,8 @@ export enum PrefKey {
UI_LAYOUT = 'ui_layout', UI_LAYOUT = 'ui_layout',
UI_SCROLLBAR_HIDE = 'ui_scrollbar_hide', UI_SCROLLBAR_HIDE = 'ui_scrollbar_hide',
UI_HOME_CONTEXT_MENU_DISABLED = 'ui_home_context_menu_disabled',
VIDEO_CLARITY = 'video_clarity', VIDEO_CLARITY = 'video_clarity',
VIDEO_RATIO = 'video_ratio', VIDEO_RATIO = 'video_ratio',
VIDEO_BRIGHTNESS = 'video_brightness', VIDEO_BRIGHTNESS = 'video_brightness',
@ -207,8 +213,7 @@ export class Preferences {
return options; return options;
})(), })(),
ready: () => { ready: (setting: PreferenceSetting) => {
const setting = Preferences.SETTINGS[PrefKey.STREAM_CODEC_PROFILE]
const options: any = setting.options; const options: any = setting.options;
const keys = Object.keys(options); const keys = Object.keys(options);
@ -226,15 +231,6 @@ export class Preferences {
default: false, default: false,
}, },
[PrefKey.SCREENSHOT_BUTTON_POSITION]: {
label: t('screenshot-button-position'),
default: 'bottom-left',
options: {
'bottom-left': t('bottom-left'),
'bottom-right': t('bottom-right'),
'none': t('disable'),
},
},
[PrefKey.SCREENSHOT_APPLY_FILTERS]: { [PrefKey.SCREENSHOT_APPLY_FILTERS]: {
label: t('screenshot-apply-filters'), label: t('screenshot-apply-filters'),
default: false, default: false,
@ -265,8 +261,7 @@ export class Preferences {
off: t('off'), off: t('off'),
}, },
unsupported: !STATES.hasTouchSupport, unsupported: !STATES.hasTouchSupport,
ready: () => { ready: (setting: PreferenceSetting) => {
const setting = Preferences.SETTINGS[PrefKey.STREAM_TOUCH_CONTROLLER];
if (setting.unsupported) { if (setting.unsupported) {
setting.default = 'default'; setting.default = 'default';
} }
@ -277,6 +272,20 @@ export class Preferences {
default: false, default: false,
unsupported: !STATES.hasTouchSupport, unsupported: !STATES.hasTouchSupport,
}, },
[PrefKey.STREAM_TOUCH_CONTROLLER_DEFAULT_OPACITY]: {
type: SettingElementType.NUMBER_STEPPER,
label: t('tc-default-opacity'),
default: 100,
min: 10,
max: 100,
steps: 10,
params: {
suffix: '%',
ticks: 10,
hideSlider: true,
},
unsupported: !STATES.hasTouchSupport,
},
[PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD]: { [PrefKey.STREAM_TOUCH_CONTROLLER_STYLE_STANDARD]: {
label: t('tc-standard-layout-style'), label: t('tc-standard-layout-style'),
default: 'default', default: 'default',
@ -310,6 +319,50 @@ export class Preferences {
default: false, default: false,
}, },
[PrefKey.BITRATE_VIDEO_MAX]: {
type: SettingElementType.NUMBER_STEPPER,
label: t('bitrate-video-maximum'),
note: '⚠️ ' + t('unexpected-behavior'),
default: 0,
min: 0,
max: 14 * 1024 * 1000,
steps: 100 * 1024,
params: {
exactTicks: 5 * 1024 * 1000,
customTextValue: (value: any) => {
value = parseInt(value);
if (value === 0) {
return t('unlimited');
} else {
return (value / (1024 * 1000)).toFixed(1) + ' Mb/s';
}
return null;
},
},
migrate: function(savedPrefs: any, value: any) {
try {
value = parseInt(value);
if (value !== 0 && value < 100) {
value *= 1024 * 1000;
}
this.set(PrefKey.BITRATE_VIDEO_MAX, value, true);
savedPrefs[PrefKey.BITRATE_VIDEO_MAX] = value;
} catch (e) {}
},
},
[PrefKey.GAME_BAR_POSITION]: {
label: t('position'),
default: 'bottom-left',
options: {
'bottom-left': t('bottom-left'),
'bottom-right': t('bottom-right'),
'off': t('off'),
},
},
[PrefKey.LOCAL_CO_OP_ENABLED]: { [PrefKey.LOCAL_CO_OP_ENABLED]: {
label: t('enable-local-co-op-support'), label: t('enable-local-co-op-support'),
default: false, default: false,
@ -331,10 +384,12 @@ export class Preferences {
}, },
[PrefKey.CONTROLLER_ENABLE_VIBRATION]: { [PrefKey.CONTROLLER_ENABLE_VIBRATION]: {
label: t('controller-vibration'),
default: true, default: true,
}, },
[PrefKey.CONTROLLER_DEVICE_VIBRATION]: { [PrefKey.CONTROLLER_DEVICE_VIBRATION]: {
label: t('device-vibration'),
default: 'off', default: 'off',
options: { options: {
on: t('on'), on: t('on'),
@ -344,6 +399,7 @@ export class Preferences {
}, },
[PrefKey.CONTROLLER_VIBRATION_INTENSITY]: { [PrefKey.CONTROLLER_VIBRATION_INTENSITY]: {
label: t('vibration-intensity'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 100, default: 100,
min: 0, min: 0,
@ -360,14 +416,12 @@ export class Preferences {
default: false, default: false,
unsupported: ((): string | boolean => { unsupported: ((): string | boolean => {
const userAgent = ((window.navigator as any).orgUserAgent || window.navigator.userAgent || '').toLowerCase(); const userAgent = ((window.navigator as any).orgUserAgent || window.navigator.userAgent || '').toLowerCase();
return userAgent.match(/(android|iphone|ipad)/) ? t('browser-unsupported-feature') : false; return !AppInterface && userAgent.match(/(android|iphone|ipad)/) ? t('browser-unsupported-feature') : false;
})(), })(),
ready: () => { ready: (setting: PreferenceSetting) => {
const pref = Preferences.SETTINGS[PrefKey.MKB_ENABLED];
let note; let note;
let url; let url;
if (pref.unsupported) { if (setting.unsupported) {
note = t('browser-unsupported-feature'); note = t('browser-unsupported-feature');
url = 'https://github.com/redphx/better-xcloud/issues/206#issuecomment-1920475657'; url = 'https://github.com/redphx/better-xcloud/issues/206#issuecomment-1920475657';
} else { } else {
@ -375,13 +429,18 @@ export class Preferences {
url = 'https://better-xcloud.github.io/mouse-and-keyboard/#disclaimer'; url = 'https://better-xcloud.github.io/mouse-and-keyboard/#disclaimer';
} }
Preferences.SETTINGS[PrefKey.MKB_ENABLED].note = CE('a', { setting.note = CE('a', {
href: url, href: url,
target: '_blank', target: '_blank',
}, '⚠️ ' + note); }, '⚠️ ' + note);
}, },
}, },
[PrefKey.NATIVE_MKB_DISABLED]: {
label: t('disable-native-mkb'),
default: false,
},
[PrefKey.MKB_DEFAULT_PRESET_ID]: { [PrefKey.MKB_DEFAULT_PRESET_ID]: {
default: 0, default: 0,
}, },
@ -427,6 +486,11 @@ export class Preferences {
default: false, default: false,
}, },
[PrefKey.UI_HOME_CONTEXT_MENU_DISABLED]: {
label: t('disable-home-context-menu'),
default: false,
},
[PrefKey.BLOCK_SOCIAL_FEATURES]: { [PrefKey.BLOCK_SOCIAL_FEATURES]: {
label: t('disable-social-features'), label: t('disable-social-features'),
default: false, default: false,
@ -437,22 +501,21 @@ export class Preferences {
}, },
[PrefKey.USER_AGENT_PROFILE]: { [PrefKey.USER_AGENT_PROFILE]: {
label: t('user-agent-profile'), label: t('user-agent-profile'),
note: '⚠️ ' + t('unexpected-behavior'),
default: 'default', default: 'default',
options: { options: {
[UserAgentProfile.DEFAULT]: t('default'), [UserAgentProfile.DEFAULT]: t('default'),
[UserAgentProfile.EDGE_WINDOWS]: 'Edge + Windows', [UserAgentProfile.WINDOWS_EDGE]: 'Edge + Windows',
[UserAgentProfile.SAFARI_MACOS]: 'Safari + macOS', [UserAgentProfile.MACOS_SAFARI]: 'Safari + macOS',
[UserAgentProfile.SMARTTV]: 'Smart TV', [UserAgentProfile.SMARTTV_GENERIC]: 'Smart TV',
[UserAgentProfile.SMARTTV_TIZEN]: 'Samsung Smart TV', [UserAgentProfile.SMARTTV_TIZEN]: 'Samsung Smart TV',
[UserAgentProfile.VR_OCULUS]: 'Meta Quest VR', [UserAgentProfile.VR_OCULUS]: 'Meta Quest VR',
[UserAgentProfile.KIWI_V123]: 'Kiwi Browser v123', [UserAgentProfile.ANDROID_KIWI_V123]: 'Kiwi Browser v123',
[UserAgentProfile.CUSTOM]: t('custom'), [UserAgentProfile.CUSTOM]: t('custom'),
}, },
}, },
[PrefKey.USER_AGENT_CUSTOM]: {
default: '',
},
[PrefKey.VIDEO_CLARITY]: { [PrefKey.VIDEO_CLARITY]: {
label: t('clarity'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 0, default: 0,
min: 0, min: 0,
@ -462,6 +525,8 @@ export class Preferences {
}, },
}, },
[PrefKey.VIDEO_RATIO]: { [PrefKey.VIDEO_RATIO]: {
label: t('ratio'),
note: t('stretch-note'),
default: '16:9', default: '16:9',
options: { options: {
'16:9': '16:9', '16:9': '16:9',
@ -475,6 +540,7 @@ export class Preferences {
}, },
}, },
[PrefKey.VIDEO_SATURATION]: { [PrefKey.VIDEO_SATURATION]: {
label: t('saturation'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 100, default: 100,
min: 50, min: 50,
@ -485,6 +551,7 @@ export class Preferences {
}, },
}, },
[PrefKey.VIDEO_CONTRAST]: { [PrefKey.VIDEO_CONTRAST]: {
label: t('contrast'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 100, default: 100,
min: 50, min: 50,
@ -495,6 +562,7 @@ export class Preferences {
}, },
}, },
[PrefKey.VIDEO_BRIGHTNESS]: { [PrefKey.VIDEO_BRIGHTNESS]: {
label: t('brightness'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 100, default: 100,
min: 50, min: 50,
@ -512,9 +580,9 @@ export class Preferences {
[PrefKey.AUDIO_ENABLE_VOLUME_CONTROL]: { [PrefKey.AUDIO_ENABLE_VOLUME_CONTROL]: {
label: t('enable-volume-control'), label: t('enable-volume-control'),
default: false, default: false,
experimental: true,
}, },
[PrefKey.AUDIO_VOLUME]: { [PrefKey.AUDIO_VOLUME]: {
label: t('volume'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 100, default: 100,
min: 0, min: 0,
@ -527,6 +595,7 @@ export class Preferences {
[PrefKey.STATS_ITEMS]: { [PrefKey.STATS_ITEMS]: {
label: t('stats'),
default: [StreamStat.PING, StreamStat.FPS, StreamStat.BITRATE, StreamStat.DECODE_TIME, StreamStat.PACKETS_LOST, StreamStat.FRAMES_LOST], default: [StreamStat.PING, StreamStat.FPS, StreamStat.BITRATE, StreamStat.DECODE_TIME, StreamStat.PACKETS_LOST, StreamStat.FRAMES_LOST],
multipleOptions: { multipleOptions: {
[StreamStat.PING]: `${StreamStat.PING.toUpperCase()}: ${t('stat-ping')}`, [StreamStat.PING]: `${StreamStat.PING.toUpperCase()}: ${t('stat-ping')}`,
@ -541,12 +610,15 @@ export class Preferences {
}, },
}, },
[PrefKey.STATS_SHOW_WHEN_PLAYING]: { [PrefKey.STATS_SHOW_WHEN_PLAYING]: {
label: t('show-stats-on-startup'),
default: false, default: false,
}, },
[PrefKey.STATS_QUICK_GLANCE]: { [PrefKey.STATS_QUICK_GLANCE]: {
label: '👀 ' + t('enable-quick-glance-mode'),
default: true, default: true,
}, },
[PrefKey.STATS_POSITION]: { [PrefKey.STATS_POSITION]: {
label: t('position'),
default: 'top-right', default: 'top-right',
options: { options: {
'top-left': t('top-left'), 'top-left': t('top-left'),
@ -555,6 +627,7 @@ export class Preferences {
}, },
}, },
[PrefKey.STATS_TEXT_SIZE]: { [PrefKey.STATS_TEXT_SIZE]: {
label: t('text-size'),
default: '0.9rem', default: '0.9rem',
options: { options: {
'0.9rem': t('small'), '0.9rem': t('small'),
@ -563,9 +636,11 @@ export class Preferences {
}, },
}, },
[PrefKey.STATS_TRANSPARENT]: { [PrefKey.STATS_TRANSPARENT]: {
label: t('transparent-background'),
default: false, default: false,
}, },
[PrefKey.STATS_OPACITY]: { [PrefKey.STATS_OPACITY]: {
label: t('opacity'),
type: SettingElementType.NUMBER_STEPPER, type: SettingElementType.NUMBER_STEPPER,
default: 80, default: 80,
min: 50, min: 50,
@ -576,6 +651,7 @@ export class Preferences {
}, },
}, },
[PrefKey.STATS_CONDITIONAL_FORMATTING]: { [PrefKey.STATS_CONDITIONAL_FORMATTING]: {
label: t('conditional-formatting'),
default: false, default: false,
}, },
@ -624,11 +700,12 @@ export class Preferences {
for (let settingId in Preferences.SETTINGS) { for (let settingId in Preferences.SETTINGS) {
const setting = Preferences.SETTINGS[settingId]; const setting = Preferences.SETTINGS[settingId];
setting.ready && setting.ready.call(this);
if (setting.migrate && settingId in savedPrefs) { if (setting.migrate && settingId in savedPrefs) {
setting.migrate.call(this, savedPrefs, savedPrefs[settingId]); setting.migrate.call(this, savedPrefs, savedPrefs[settingId]);
} }
setting.ready && setting.ready.call(this, setting);
} }
for (let settingId in Preferences.SETTINGS) { for (let settingId in Preferences.SETTINGS) {
@ -639,7 +716,7 @@ export class Preferences {
continue; continue;
} }
// Ignore deprecated settings // Ignore deprecated/migrated settings
if (setting.migrate) { if (setting.migrate) {
continue; continue;
} }
@ -706,11 +783,13 @@ export class Preferences {
return this.#prefs[key]; return this.#prefs[key];
} }
set(key: PrefKey, value: any) { set(key: PrefKey, value: any, skipSave?: boolean): any {
value = this.#validateValue(key, value); value = this.#validateValue(key, value);
this.#prefs[key] = value; this.#prefs[key] = value;
this.#updateStorage(); !skipSave && this.#updateStorage();
return value;
} }
#updateStorage() { #updateStorage() {
@ -721,7 +800,6 @@ export class Preferences {
const setting = Preferences.SETTINGS[key]; const setting = Preferences.SETTINGS[key];
let currentValue = this.get(key); let currentValue = this.get(key);
let $control;
let type; let type;
if ('type' in setting) { if ('type' in setting) {
type = setting.type; type = setting.type;
@ -740,7 +818,7 @@ export class Preferences {
currentValue = Preferences.SETTINGS[key].default; currentValue = Preferences.SETTINGS[key].default;
} }
$control = SettingElement.render(type!, key as string, setting, currentValue, (e: any, value: any) => { const $control = SettingElement.render(type!, key as string, setting, currentValue, (e: any, value: any) => {
this.set(key, value); this.set(key, value);
onChange && onChange(e, value); onChange && onChange(e, value);
}, params); }, params);

View File

@ -0,0 +1,58 @@
import { STATES } from "@utils/global";
import { BxLogger } from "./bx-logger";
import { TouchController } from "@modules/touch-controller";
import { GamePassCloudGallery } from "./gamepass-gallery";
import { getPref, PrefKey } from "./preferences";
const LOG_TAG = 'PreloadState';
export function overridePreloadState() {
let _state: any;
Object.defineProperty(window, '__PRELOADED_STATE__', {
configurable: true,
get: () => {
return _state;
},
set: state => {
// Override User-Agent
try {
state.appContext.requestInfo.userAgent = window.navigator.userAgent;
} catch (e) {
BxLogger.error(LOG_TAG, e);
}
// Add list of games with custom layouts to the official list
if (STATES.hasTouchSupport) {
try {
const sigls = state.xcloud.sigls;
if (GamePassCloudGallery.TOUCH in sigls) {
let customList = TouchController.getCustomList();
const allGames = sigls[GamePassCloudGallery.ALL].data.products;
// Remove non-cloud games from the list
customList = customList.filter(id => allGames.includes(id));
// Add to the official list
sigls[GamePassCloudGallery.TOUCH]?.data.products.push(...customList);
}
} catch (e) {
BxLogger.error(LOG_TAG, e);
}
}
if (getPref(PrefKey.UI_HOME_CONTEXT_MENU_DISABLED)) {
try {
state.experiments.experimentationInfo.data.treatments.EnableHomeContextMenu = false;
} catch (e) {
BxLogger.error(LOG_TAG, e);
}
}
// @ts-ignore
_state = state;
STATES.appContext = structuredClone(state.appContext);
}
});
}

32
src/utils/prompt-font.ts Normal file
View File

@ -0,0 +1,32 @@
export enum PrompFont {
A = '⇓',
B = '⇒',
X = '⇐',
Y = '⇑',
LB = '↘',
RB = '↙',
LT = '↖',
RT = '↗',
SELECT = '⇺',
START = '⇻',
HOME = '',
UP = '≻',
DOWN = '≽',
LEFT = '≺',
RIGHT = '≼',
L3 = '↺',
LS_UP = '↾',
LS_DOWN = '⇂',
LS_LEFT = '↼',
LS_RIGHT = '⇀',
R3 = '↻',
RS_UP = '↿',
RS_DOWN = '⇃',
RS_LEFT = '↽',
RS_RIGHT = '⇁',
}

80
src/utils/screenshot.ts Normal file
View File

@ -0,0 +1,80 @@
import { AppInterface, STATES } from "./global";
import { CE } from "./html";
export class Screenshot {
static #$canvas: HTMLCanvasElement;
static #canvasContext: CanvasRenderingContext2D;
static setup() {
if (Screenshot.#$canvas) {
return;
}
Screenshot.#$canvas = CE<HTMLCanvasElement>('canvas', {'class': 'bx-gone'});
Screenshot.#canvasContext = Screenshot.#$canvas.getContext('2d', {
alpha: false,
willReadFrequently: false,
})!;
}
static updateCanvasSize(width: number, height: number) {
const $canvas = Screenshot.#$canvas;
if ($canvas) {
$canvas.width = width;
$canvas.height = height;
}
}
static updateCanvasFilters(filters: string) {
Screenshot.#canvasContext.filter = filters;
}
private static onAnimationEnd(e: Event) {
(e.target as any).classList.remove('bx-taking-screenshot');
}
static takeScreenshot(callback?: any) {
const currentStream = STATES.currentStream;
const $video = currentStream.$video;
const $canvas = Screenshot.#$canvas;
if (!$video || !$canvas) {
return;
}
$video.parentElement?.addEventListener('animationend', this.onAnimationEnd);
$video.parentElement?.classList.add('bx-taking-screenshot');
const canvasContext = Screenshot.#canvasContext;
canvasContext.drawImage($video, 0, 0, $canvas.width, $canvas.height);
// Get data URL and pass to parent app
if (AppInterface) {
const data = $canvas.toDataURL('image/png').split(';base64,')[1];
AppInterface.saveScreenshot(currentStream.titleId, data);
// Free screenshot from memory
canvasContext.clearRect(0, 0, $canvas.width, $canvas.height);
callback && callback();
return;
}
$canvas && $canvas.toBlob(blob => {
// Download screenshot
const now = +new Date;
const $anchor = CE<HTMLAnchorElement>('a', {
'download': `${currentStream.titleId}-${now}.png`,
'href': URL.createObjectURL(blob!),
});
$anchor.click();
// Free screenshot from memory
URL.revokeObjectURL($anchor.href);
canvasContext.clearRect(0, 0, $canvas.width, $canvas.height);
callback && callback();
}, 'image/png');
}
}

61
src/utils/sdp.ts Normal file
View File

@ -0,0 +1,61 @@
export function patchSdpBitrate(sdp: string, video?: number, audio?: number) {
const lines = sdp.split('\n');
const mediaSet: Set<string> = new Set();
!!video && mediaSet.add('video');
!!audio && mediaSet.add('audio');
const bitrate = {
video,
audio,
};
for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
let media: string = '';
let line = lines[lineNumber];
if (!line.startsWith('m=')) {
continue;
}
for (const m of mediaSet) {
if (line.startsWith(`m=${m}`)) {
media = m;
// Remove matched media from set
mediaSet.delete(media);
break;
}
}
// Invalid media, continue looking
if (!media) {
continue;
}
const bLine = `b=AS:${bitrate[media as keyof typeof bitrate]}`;
while (lineNumber++, lineNumber < lines.length) {
line = lines[lineNumber];
// Ignore lines that start with "i=" or "c="
if (line.startsWith('i=') || line.startsWith('c=')) {
continue;
}
if (line.startsWith('b=AS:')) {
// Replace bitrate
lines[lineNumber] = bLine;
// Stop lookine for "b=AS:" line
break;
}
if (line.startsWith('m=')) {
// "b=AS:" line not found, add "b" line before "m="
lines.splice(lineNumber, 0, bLine);
// Stop
break;
}
}
}
return lines.join('\n');
}

View File

@ -12,6 +12,8 @@ type NumberStepperParams = {
ticks?: number; ticks?: number;
exactTicks?: number; exactTicks?: number;
customTextValue?: (value: any) => string | null;
} }
export enum SettingElementType { export enum SettingElementType {
@ -24,7 +26,10 @@ export enum SettingElementType {
export class SettingElement { export class SettingElement {
static #renderOptions(key: string, setting: PreferenceSetting, currentValue: any, onChange: any) { static #renderOptions(key: string, setting: PreferenceSetting, currentValue: any, onChange: any) {
const $control = CE<HTMLSelectElement>('select') as HTMLSelectElement; const $control = CE<HTMLSelectElement>('select', {
title: setting.label,
tabindex: 0,
}) as HTMLSelectElement;
for (let value in setting.options) { for (let value in setting.options) {
const label = setting.options[value]; const label = setting.options[value];
@ -48,7 +53,11 @@ export class SettingElement {
} }
static #renderMultipleOptions(key: string, setting: PreferenceSetting, currentValue: any, onChange: any, params: MultipleOptionsParams={}) { static #renderMultipleOptions(key: string, setting: PreferenceSetting, currentValue: any, onChange: any, params: MultipleOptionsParams={}) {
const $control = CE<HTMLSelectElement>('select', {'multiple': true}); const $control = CE<HTMLSelectElement>('select', {
title: setting.label,
multiple: true,
tabindex: 0,
});
if (params && params.size) { if (params && params.size) {
$control.setAttribute('size', params.size.toString()); $control.setAttribute('size', params.size.toString());
} }
@ -91,7 +100,7 @@ export class SettingElement {
} }
static #renderNumber(key: string, setting: PreferenceSetting, currentValue: any, onChange: any) { static #renderNumber(key: string, setting: PreferenceSetting, currentValue: any, onChange: any) {
const $control = CE('input', {'type': 'number', 'min': setting.min, 'max': setting.max}) as HTMLInputElement; const $control = CE('input', {'tabindex': 0, 'type': 'number', 'min': setting.min, 'max': setting.max}) as HTMLInputElement;
$control.value = currentValue; $control.value = currentValue;
onChange && $control.addEventListener('change', (e: Event) => { onChange && $control.addEventListener('change', (e: Event) => {
const target = e.target as HTMLInputElement; const target = e.target as HTMLInputElement;
@ -106,7 +115,7 @@ export class SettingElement {
} }
static #renderCheckbox(key: string, setting: PreferenceSetting, currentValue: any, onChange: any) { static #renderCheckbox(key: string, setting: PreferenceSetting, currentValue: any, onChange: any) {
const $control = CE('input', {'type': 'checkbox'}) as HTMLInputElement; const $control = CE('input', {'type': 'checkbox', 'tabindex': 0}) as HTMLInputElement;
$control.checked = currentValue; $control.checked = currentValue;
onChange && $control.addEventListener('change', e => { onChange && $control.addEventListener('change', e => {
@ -131,19 +140,50 @@ export class SettingElement {
const MAX = setting.max!; const MAX = setting.max!;
const STEPS = Math.max(setting.steps || 1, 1); const STEPS = Math.max(setting.steps || 1, 1);
const renderTextValue = (value: any) => {
value = parseInt(value as string);
let textContent = null;
if (options.customTextValue) {
textContent = options.customTextValue(value);
}
if (textContent === null) {
textContent = value.toString() + options.suffix;
}
return textContent;
};
const $wrapper = CE('div', {'class': 'bx-number-stepper'}, const $wrapper = CE('div', {'class': 'bx-number-stepper'},
$decBtn = CE('button', {'data-type': 'dec'}, '-') as HTMLButtonElement, $decBtn = CE('button', {
$text = CE('span', {}, value + options.suffix) as HTMLSpanElement, 'data-type': 'dec',
$incBtn = CE('button', {'data-type': 'inc'}, '+') as HTMLButtonElement, type: 'button',
tabindex: -1,
}, '-') as HTMLButtonElement,
$text = CE('span', {}, renderTextValue(value)) as HTMLSpanElement,
$incBtn = CE('button', {
'data-type': 'inc',
type: 'button',
tabindex: -1,
}, '+') as HTMLButtonElement,
); );
if (!options.disabled && !options.hideSlider) { if (!options.disabled && !options.hideSlider) {
$range = CE('input', {'type': 'range', 'min': MIN, 'max': MAX, 'value': value, 'step': STEPS}) as HTMLInputElement; $range = CE('input', {
id: `bx_setting_${key}`,
type: 'range',
min: MIN,
max: MAX,
value: value,
step: STEPS,
tabindex: 0,
}) as HTMLInputElement;
$range.addEventListener('input', e => { $range.addEventListener('input', e => {
value = parseInt((e.target as HTMLInputElement).value); value = parseInt((e.target as HTMLInputElement).value);
$text.textContent = renderTextValue(value);
$text.textContent = value + options.suffix; !(e as any).ignoreOnChange && onChange && onChange(e, value);
onChange && onChange(e, value);
}); });
$wrapper.appendChild($range); $wrapper.appendChild($range);
@ -204,17 +244,20 @@ export class SettingElement {
value = Math.min(MAX, value + STEPS); value = Math.min(MAX, value + STEPS);
} }
$text.textContent = value.toString() + options.suffix; $text.textContent = renderTextValue(value);
$range && ($range.value = value.toString()); $range && ($range.value = value.toString());
isHolding = false; isHolding = false;
onChange && onChange(e, value); onChange && onChange(e, value);
} }
const onMouseDown = (e: MouseEvent | TouchEvent) => { const onMouseDown = (e: PointerEvent) => {
e.preventDefault();
isHolding = true; isHolding = true;
const args = arguments; const args = arguments;
interval && clearInterval(interval);
interval = window.setInterval(() => { interval = window.setInterval(() => {
const event = new Event('click'); const event = new Event('click');
(event as any).arguments = args; (event as any).arguments = args;
@ -223,28 +266,30 @@ export class SettingElement {
}, 200); }, 200);
}; };
const onMouseUp = (e: MouseEvent | TouchEvent) => { const onMouseUp = (e: PointerEvent) => {
clearInterval(interval); e.preventDefault();
interval && clearInterval(interval);
isHolding = false; isHolding = false;
}; };
const onContextMenu = (e: Event) => e.preventDefault();
// Custom method // Custom method
($wrapper as any).setValue = (value: any) => { ($wrapper as any).setValue = (value: any) => {
$text.textContent = value + options.suffix; $text.textContent = renderTextValue(value);
$range && ($range.value = value); $range && ($range.value = value);
}; };
$decBtn.addEventListener('click', onClick); $decBtn.addEventListener('click', onClick);
$decBtn.addEventListener('mousedown', onMouseDown); $decBtn.addEventListener('pointerdown', onMouseDown);
$decBtn.addEventListener('mouseup', onMouseUp); $decBtn.addEventListener('pointerup', onMouseUp);
$decBtn.addEventListener('touchstart', onMouseDown); $decBtn.addEventListener('contextmenu', onContextMenu);
$decBtn.addEventListener('touchend', onMouseUp);
$incBtn.addEventListener('click', onClick); $incBtn.addEventListener('click', onClick);
$incBtn.addEventListener('mousedown', onMouseDown); $incBtn.addEventListener('pointerdown', onMouseDown);
$incBtn.addEventListener('mouseup', onMouseUp); $incBtn.addEventListener('pointerup', onMouseUp);
$incBtn.addEventListener('touchstart', onMouseDown); $incBtn.addEventListener('contextmenu', onContextMenu);
$incBtn.addEventListener('touchend', onMouseUp);
return $wrapper; return $wrapper;
} }
@ -261,7 +306,10 @@ export class SettingElement {
const method = SettingElement.#METHOD_MAP[type]; const method = SettingElement.#METHOD_MAP[type];
// @ts-ignore // @ts-ignore
const $control = method(...Array.from(arguments).slice(1)) as HTMLElement; const $control = method(...Array.from(arguments).slice(1)) as HTMLElement;
if (type !== SettingElementType.NUMBER_STEPPER) {
$control.id = `bx_setting_${key}`; $control.id = `bx_setting_${key}`;
}
// Add "name" property to "select" elements // Add "name" property to "select" elements
if (type === SettingElementType.OPTIONS || type === SettingElementType.MULTIPLE_OPTIONS) { if (type === SettingElementType.OPTIONS || type === SettingElementType.MULTIPLE_OPTIONS) {

View File

@ -1,24 +0,0 @@
import { STATES } from "@utils/global";
import { UserAgent } from "@utils/user-agent";
export class PreloadedState {
static override() {
Object.defineProperty(window, '__PRELOADED_STATE__', {
configurable: true,
get: () => {
// Override User-Agent
const userAgent = UserAgent.spoof();
if (userAgent) {
(this as any)._state.appContext.requestInfo.userAgent = userAgent;
}
return (this as any)._state;
},
set: state => {
(this as any)._state = state;
STATES.appContext = structuredClone(state.appContext);
}
});
}
}

View File

@ -2,6 +2,7 @@ import { CE } from "@utils/html";
type ToastOptions = { type ToastOptions = {
instant?: boolean; instant?: boolean;
html?: boolean;
} }
export class Toast { export class Toast {
@ -14,7 +15,7 @@ export class Toast {
static #timeout?: number | null; static #timeout?: number | null;
static #DURATION = 3000; static #DURATION = 3000;
static show(msg: string, status?: string, options: Partial<ToastOptions>={}) { static show(msg: string, status?: string, options: Partial<ToastOptions> = {}) {
options = options || {}; options = options || {};
const args = Array.from(arguments) as [string, string, ToastOptions]; const args = Array.from(arguments) as [string, string, ToastOptions];
@ -40,9 +41,13 @@ export class Toast {
Toast.#timeout = window.setTimeout(Toast.#hide, Toast.#DURATION); Toast.#timeout = window.setTimeout(Toast.#hide, Toast.#DURATION);
// Get values from item // Get values from item
const [msg, status, _] = Toast.#stack.shift()!; const [msg, status, options] = Toast.#stack.shift()!;
if (options && options.html) {
Toast.#$msg.innerHTML = msg;
} else {
Toast.#$msg.textContent = msg; Toast.#$msg.textContent = msg;
}
if (status) { if (status) {
Toast.#$status.classList.remove('bx-gone'); Toast.#$status.classList.remove('bx-gone');

File diff suppressed because it is too large Load Diff

View File

@ -1,18 +1,21 @@
import { PrefKey, getPref } from "@utils/preferences"; type UserAgentConfig = {
profile: UserAgentProfile,
custom?: string,
};
export enum UserAgentProfile { export enum UserAgentProfile {
EDGE_WINDOWS = 'edge-windows', WINDOWS_EDGE = 'windows-edge',
SAFARI_MACOS = 'safari-macos', MACOS_SAFARI = 'macos-safari',
SMARTTV = 'smarttv', SMARTTV_GENERIC = 'smarttv-generic',
SMARTTV_TIZEN = 'smarttv-tizen', SMARTTV_TIZEN = 'smarttv-tizen',
VR_OCULUS = 'vr-oculus', VR_OCULUS = 'vr-oculus',
KIWI_V123 = 'kiwi-v123', ANDROID_KIWI_V123 = 'android-kiwi-v123',
DEFAULT = 'default', DEFAULT = 'default',
CUSTOM = 'custom', CUSTOM = 'custom',
} }
let CHROMIUM_VERSION = '123.0.0.0'; let CHROMIUM_VERSION = '123.0.0.0';
if (!!(window as any).chrome) { if (!!(window as any).chrome || window.navigator.userAgent.includes('Chrome')) {
// Get Chromium version in the original User-Agent value // Get Chromium version in the original User-Agent value
const match = window.navigator.userAgent.match(/\s(?:Chrome|Edg)\/([\d\.]+)/); const match = window.navigator.userAgent.match(/\s(?:Chrome|Edg)\/([\d\.]+)/);
if (match) { if (match) {
@ -20,18 +23,41 @@ if (!!(window as any).chrome) {
} }
} }
// Repace Chromium version
let EDGE_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/[[VERSION]] Safari/537.36 Edg/[[VERSION]]';
EDGE_USER_AGENT = EDGE_USER_AGENT.replaceAll('[[VERSION]]', CHROMIUM_VERSION);
export class UserAgent { export class UserAgent {
static #USER_AGENTS = { static readonly STORAGE_KEY = 'better_xcloud_user_agent';
[UserAgentProfile.EDGE_WINDOWS]: EDGE_USER_AGENT, static #config: UserAgentConfig;
[UserAgentProfile.SAFARI_MACOS]: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.1',
[UserAgentProfile.SMARTTV]: window.navigator.userAgent + ' SmartTV', static #USER_AGENTS: PartialRecord<UserAgentProfile, string> = {
[UserAgentProfile.SMARTTV_TIZEN]: 'Mozilla/5.0 (SMART-TV; LINUX; Tizen 7.0) AppleWebKit/537.36 (KHTML, like Gecko) 94.0.4606.31/7.0 TV Safari/537.36', [UserAgentProfile.WINDOWS_EDGE]: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${CHROMIUM_VERSION} Safari/537.36 Edg/${CHROMIUM_VERSION}`,
[UserAgentProfile.MACOS_SAFARI]: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.1',
[UserAgentProfile.SMARTTV_GENERIC]: window.navigator.userAgent + ' SmartTV',
[UserAgentProfile.SMARTTV_TIZEN]: `Mozilla/5.0 (SMART-TV; LINUX; Tizen 7.0) AppleWebKit/537.36 (KHTML, like Gecko) ${CHROMIUM_VERSION}/7.0 TV Safari/537.36`,
[UserAgentProfile.VR_OCULUS]: window.navigator.userAgent + ' OculusBrowser VR', [UserAgentProfile.VR_OCULUS]: window.navigator.userAgent + ' OculusBrowser VR',
[UserAgentProfile.KIWI_V123]: 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.118 Mobile Safari/537.36', [UserAgentProfile.ANDROID_KIWI_V123]: 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.118 Mobile Safari/537.36',
}
static init() {
UserAgent.#config = JSON.parse(window.localStorage.getItem(UserAgent.STORAGE_KEY) || '{}') as UserAgentConfig;
if (!UserAgent.#config.profile) {
UserAgent.#config.profile = UserAgentProfile.DEFAULT;
}
if (!UserAgent.#config.custom) {
UserAgent.#config.custom = '';
}
UserAgent.spoof();
}
static updateStorage(profile: UserAgentProfile, custom?: string) {
const clonedConfig = structuredClone(UserAgent.#config);
clonedConfig.profile = profile;
if (typeof custom !== 'undefined') {
clonedConfig.custom = custom;
}
window.localStorage.setItem(UserAgent.STORAGE_KEY, JSON.stringify(clonedConfig));
} }
static getDefault(): string { static getDefault(): string {
@ -39,16 +65,22 @@ export class UserAgent {
} }
static get(profile: UserAgentProfile): string { static get(profile: UserAgentProfile): string {
const defaultUserAgent = UserAgent.getDefault(); const defaultUserAgent = window.navigator.userAgent;
if (profile === UserAgentProfile.CUSTOM) {
return getPref(PrefKey.USER_AGENT_CUSTOM);
}
return (UserAgent.#USER_AGENTS as any)[profile] || defaultUserAgent; switch (profile) {
case UserAgentProfile.DEFAULT:
return defaultUserAgent;
case UserAgentProfile.CUSTOM:
return UserAgent.#config.custom || defaultUserAgent;
default:
return UserAgent.#USER_AGENTS[profile] || defaultUserAgent;
}
} }
static isSafari(mobile=false): boolean { static isSafari(mobile=false): boolean {
const userAgent = (UserAgent.getDefault() || '').toLowerCase(); const userAgent = UserAgent.getDefault().toLowerCase();
let result = userAgent.includes('safari') && !userAgent.includes('chrom'); let result = userAgent.includes('safari') && !userAgent.includes('chrom');
if (result && mobile) { if (result && mobile) {
@ -59,21 +91,17 @@ export class UserAgent {
} }
static isMobile(): boolean { static isMobile(): boolean {
const userAgent = (UserAgent.getDefault() || '').toLowerCase(); const userAgent = UserAgent.getDefault().toLowerCase();
return /iphone|ipad|android/.test(userAgent); return /iphone|ipad|android/.test(userAgent);
} }
static spoof() { static spoof() {
let newUserAgent; const profile = UserAgent.#config.profile;
const profile = getPref(PrefKey.USER_AGENT_PROFILE);
if (profile === UserAgentProfile.DEFAULT) { if (profile === UserAgentProfile.DEFAULT) {
return; return;
} }
if (!newUserAgent) { const newUserAgent = UserAgent.get(profile);
newUserAgent = UserAgent.get(profile);
}
// Clear data of navigator.userAgentData, force xCloud to detect browser based on navigator.userAgent // Clear data of navigator.userAgentData, force xCloud to detect browser based on navigator.userAgent
(window.navigator as any).orgUserAgentData = (window.navigator as any).userAgentData; (window.navigator as any).orgUserAgentData = (window.navigator as any).userAgentData;
@ -84,7 +112,5 @@ export class UserAgent {
Object.defineProperty(window.navigator, 'userAgent', { Object.defineProperty(window.navigator, 'userAgent', {
value: newUserAgent, value: newUserAgent,
}); });
return newUserAgent;
} }
} }

View File

@ -1,6 +1,7 @@
import { PrefKey, getPref, setPref } from "@utils/preferences"; import { PrefKey, getPref, setPref } from "@utils/preferences";
import { SCRIPT_VERSION } from "@utils/global"; import { AppInterface, SCRIPT_VERSION } from "@utils/global";
import { UserAgent } from "@utils/user-agent"; import { UserAgent } from "@utils/user-agent";
import { Translations } from "./translation";
/** /**
* Check for update * Check for update
@ -25,6 +26,9 @@ export function checkForUpdate() {
setPref(PrefKey.LATEST_VERSION, json.tag_name.substring(1)); setPref(PrefKey.LATEST_VERSION, json.tag_name.substring(1));
setPref(PrefKey.CURRENT_VERSION, SCRIPT_VERSION); setPref(PrefKey.CURRENT_VERSION, SCRIPT_VERSION);
}); });
// Update translations
Translations.updateTranslations(currentVersion === SCRIPT_VERSION);
} }
@ -38,7 +42,7 @@ export function disablePwa() {
} }
// Check if it's Safari on mobile // Check if it's Safari on mobile
if (UserAgent.isSafari(true)) { if (!!AppInterface || UserAgent.isSafari(true)) {
// Disable the PWA prompt // Disable the PWA prompt
Object.defineProperty(window.navigator, 'standalone', { Object.defineProperty(window.navigator, 'standalone', {
value: true, value: true,
@ -61,3 +65,28 @@ export function hashCode(str: string): number {
return hash; return hash;
} }
export function renderString(str: string, obj: any){
return str.replace(/\$\{.+?\}/g, match => {
const key = match.substring(2, match.length - 1);
if (key in obj) {
return obj[key];
}
return match;
});
}
export function ceilToNearest(value: number, interval: number): number {
return Math.ceil(value / interval) * interval;
}
export function floorToNearest(value: number, interval: number): number {
return Math.floor(value / interval) * interval;
}
export function roundToNearest(value: number, interval: number): number {
return Math.round(value / interval) * interval;
}