Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/usbgadget/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type gadgetConfigItem struct {
order uint
device string
path []string
attrs gadgetAttributes
configAttrs gadgetAttributes
attrs gadgetAttributes
optionalAttrs gadgetAttributes // written with IgnoreErrors; for kernel features that may not exist
configAttrs gadgetAttributes
configPath []string
reportDesc []byte
}
Expand All @@ -34,7 +35,7 @@ var defaultGadgetConfig = map[string]gadgetConfigItem{
"bcdDevice": "0x0100", // USB2
},
configAttrs: gadgetAttributes{
"MaxPower": "250", // in unit of 2mA
"MaxPower": "250", // in unit of 2mA
"bmAttributes": "0xa0", // bus-powered + remote wakeup
},
},
Expand Down
28 changes: 28 additions & 0 deletions internal/usbgadget/config_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ func (tx *UsbGadgetTransaction) writeGadgetItemConfig(item gadgetConfigItem, dep
)...)
}

if len(item.optionalAttrs) > 0 {
// write optional attributes (IgnoreErrors=true for backward compat with older kernels)
files = append(files, tx.writeGadgetAttrsOptional(
gadgetItemPath,
item.optionalAttrs,
component,
beforeChange,
)...)
}

// write report descriptor if available
reportDescPath := path.Join(gadgetItemPath, "report_desc")
if item.reportDesc != nil {
Expand Down Expand Up @@ -295,6 +305,24 @@ func (tx *UsbGadgetTransaction) writeGadgetAttrs(basePath string, attrs gadgetAt
return files
}

func (tx *UsbGadgetTransaction) writeGadgetAttrsOptional(basePath string, attrs gadgetAttributes, component string, beforeChange []string) (files []string) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead wakeup_on_write check after refactoring to optionalAttrs

Low Severity

The IgnoreErrors: key == "wakeup_on_write" check in writeGadgetAttrs is now dead code. This PR moved wakeup_on_write from attrs to optionalAttrs across all three HID configs, so writeGadgetAttrs will never see that key. The leftover condition is misleading and could confuse future maintainers about where optional attribute handling lives.

Fix in Cursor Fix in Web

files = make([]string, 0)
for key, val := range attrs {
filePath := filepath.Join(basePath, key)
tx.addFileChange(component, RequestedFileChange{
Path: filePath,
ExpectedState: FileStateFileContentMatch,
ExpectedContent: []byte(val),
Description: "write optional gadget attribute",
DependsOn: []string{basePath},
BeforeChange: beforeChange,
IgnoreErrors: true,
})
files = append(files, filePath)
}
return files
}

func (tx *UsbGadgetTransaction) addReorderSymlinkChange(path string, target string, deps []string) {
tx.log.Trace().Str("path", path).Str("target", target).Msg("add reorder symlink change")

Expand Down
2 changes: 2 additions & 0 deletions internal/usbgadget/hid_keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var keyboardConfig = gadgetConfigItem{
"subclass": "1",
"report_length": "8",
"no_out_endpoint": "0",
},
optionalAttrs: gadgetAttributes{
"wakeup_on_write": "1",
},
reportDesc: keyboardReportDesc,
Expand Down
2 changes: 2 additions & 0 deletions internal/usbgadget/hid_mouse_absolute.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var absoluteMouseConfig = gadgetConfigItem{
"subclass": "0",
"report_length": "6",
"no_out_endpoint": "1",
},
optionalAttrs: gadgetAttributes{
"wakeup_on_write": "1",
},
reportDesc: absoluteMouseCombinedReportDesc,
Expand Down
2 changes: 2 additions & 0 deletions internal/usbgadget/hid_mouse_relative.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var relativeMouseConfig = gadgetConfigItem{
"subclass": "1",
"report_length": "5",
"no_out_endpoint": "1",
},
optionalAttrs: gadgetAttributes{
"wakeup_on_write": "1",
},
reportDesc: relativeMouseCombinedReportDesc,
Expand Down
5 changes: 4 additions & 1 deletion ui/localization/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1093,5 +1093,8 @@
"wake_on_lan_invalid_mac": "Invalid MAC address",
"wake_on_lan_magic_sent_success": "Magic Packet sent successfully",
"welcome_to_jetkvm": "Welcome to JetKVM",
"welcome_to_jetkvm_description": "Control any computer remotely"
"welcome_to_jetkvm_description": "Control any computer remotely",
"video_overlay_no_hdmi_try_wake": "If the target computer is sleeping, you can try waking it with a simulated keyboard press",
"video_overlay_no_hdmi_wake_host": "Try Wake Host",
"video_overlay_no_hdmi_wake_host_sending": "Sending wake signal..."
}
20 changes: 18 additions & 2 deletions ui/src/components/VideoOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,11 @@ export function PeerConnectionDisconnectedOverlay({ show }: PeerConnectionDiscon
interface HDMIErrorOverlayProps {
readonly show: boolean;
readonly hdmiState: string;
readonly onWakeHost?: () => void;
readonly isWaking?: boolean;
}

export function HDMIErrorOverlay({ show, hdmiState }: HDMIErrorOverlayProps) {
export function HDMIErrorOverlay({ show, hdmiState, onWakeHost, isWaking }: HDMIErrorOverlayProps) {
const isNoSignal = hdmiState === "no_signal";
const isOtherError = hdmiState === "no_lock" || hdmiState === "out_of_range";

Expand Down Expand Up @@ -247,16 +249,30 @@ export function HDMIErrorOverlay({ show, hdmiState }: HDMIErrorOverlayProps) {
<li>{m.video_overlay_no_hdmi_ensure_cable()}</li>
<li>{m.video_overlay_no_hdmi_ensure_power()}</li>
<li>{m.video_overlay_no_hdmi_adapter_compat()}</li>
<li>{m.video_overlay_no_hdmi_try_wake()}</li>
</ul>
</div>
<div>
<div className="flex items-center gap-x-2">
<LinkButton
to={"https://jetkvm.com/docs/getting-started/troubleshooting"}
theme="light"
text={m.video_overlay_learn_more()}
TrailingIcon={ArrowRightIcon}
size="SM"
/>
{onWakeHost && (
<Button
onClick={onWakeHost}
text={
isWaking
? m.video_overlay_no_hdmi_wake_host_sending()
: m.video_overlay_no_hdmi_wake_host()
}
size="SM"
theme="primary"
disabled={isWaking}
/>
)}
</div>
</div>
</div>
Expand Down
22 changes: 21 additions & 1 deletion ui/src/components/WebRTCVideo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { isWindows } from "@/utils";
import useKeyboard from "@hooks/useKeyboard";
import useMouse from "@hooks/useMouse";
import { useRTCStore, useSettingsStore, useUiStore, useVideoStore } from "@hooks/stores";
import { useJsonRpc } from "@hooks/useJsonRpc";
import VirtualKeyboard from "@components/VirtualKeyboard";
import Actionbar from "@components/ActionBar";
import MacroBar from "@components/MacroBar";
Expand All @@ -30,6 +31,8 @@ export default function WebRTCVideo({
}) {
// Video and stream related refs and states
const videoElm = useRef<HTMLVideoElement>(null);
const [isWaking, setIsWaking] = useState(false);
const { send } = useJsonRpc();
const fullscreenContainerRef = useRef<HTMLDivElement>(null);
const { mediaStream, peerConnectionState } = useRTCStore();
const [isPlaying, setIsPlaying] = useState(false);
Expand All @@ -39,6 +42,18 @@ export default function WebRTCVideo({
const isPointerLockPossible =
window.location.protocol === "https:" || window.location.hostname === "localhost";

// Wake host handler - sends a spacebar press+release to wake sleeping host
const handleWakeHost = useCallback(() => {
setIsWaking(true);
// Send spacebar (HID usage 0x2C) press
send("keyboardReport", { keys: [0x2c, 0, 0, 0, 0, 0], modifier: 0 }, () => {
// Send key release
send("keyboardReport", { keys: [0, 0, 0, 0, 0, 0], modifier: 0 }, () => {
setTimeout(() => setIsWaking(false), 3000);
});
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wake button stuck forever if RPC send silently fails

Medium Severity

The handleWakeHost callback sets isWaking to true then relies on nested send callbacks to eventually reset it to false. However, the send function in useJsonRpc returns immediately without invoking the callback when rpcDataChannel?.readyState !== "open". If the data channel drops between button click and RPC completion, setIsWaking(false) is never called, leaving the button permanently disabled with "Sending wake signal..." text and no way to recover short of a page refresh.

Fix in Cursor Fix in Web

}, [send]);

// Store hooks
const settings = useSettingsStore();
const { handleKeyPress, resetKeyboardState } = useKeyboard();
Expand Down Expand Up @@ -675,7 +690,12 @@ export default function WebRTCVideo({
>
<div className="relative h-full w-full rounded-md">
<LoadingVideoOverlay show={isVideoLoading} />
<HDMIErrorOverlay show={hdmiError} hdmiState={hdmiState} />
<HDMIErrorOverlay
show={hdmiError}
hdmiState={hdmiState}
onWakeHost={handleWakeHost}
isWaking={isWaking}
/>
<NoAutoplayPermissionsOverlay
show={hasNoAutoPlayPermissions}
onPlayClick={() => {
Expand Down