Files

68 lines
2.0 KiB
Go

// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
package tentaclewire
import (
"encoding/json"
"git.stormux.org/storm/cthulhu/internal/input"
)
const RemoteKeyQueueSize = 256
const RemoteKeyQueueBytes = 256 * 1024
// RemoteKey identifies one logical, unshifted key, never an X keycode, Windows
// VK, scan code, text to type or command to execute. Native ownership and
// destination-layout resolution belong to a reader input owner.
type RemoteKey struct {
Name string `json:"name"`
Pressed bool `json:"pressed"`
}
type RemoteKeyRequest struct {
RemoteOperation
Key RemoteKey `json:"key"`
}
func ValidRemoteKey(key RemoteKey) bool { return input.ValidRemoteKeyName(key.Name) }
func ValidRemoteKeyRequest(request RemoteKeyRequest) bool {
return len(request.Lease) == 32 && request.Epoch != 0 && request.Sequence != 0 && ValidRemoteKey(request.Key)
}
func (key *RemoteKey) UnmarshalJSON(data []byte) error {
var fields map[string]json.RawMessage
if DecodeStrict(data, &fields) != nil || len(fields) != 2 || fields["name"] == nil || fields["pressed"] == nil {
return ErrProtocol
}
var value struct {
Name string `json:"name"`
Pressed *bool `json:"pressed"`
}
if DecodeStrict(data, &value) != nil || value.Pressed == nil {
return ErrProtocol
}
result := RemoteKey{Name: value.Name, Pressed: *value.Pressed}
if !ValidRemoteKey(result) {
return ErrProtocol
}
*key = result
return nil
}
func (request *RemoteKeyRequest) UnmarshalJSON(data []byte) error {
var fields map[string]json.RawMessage
if DecodeStrict(data, &fields) != nil || len(fields) != 4 || fields["lease"] == nil || fields["epoch"] == nil || fields["sequence"] == nil || fields["key"] == nil {
return ErrProtocol
}
type plain RemoteKeyRequest
var value plain
if DecodeStrict(data, &value) != nil || !ValidRemoteKeyRequest(RemoteKeyRequest(value)) {
return ErrProtocol
}
*request = RemoteKeyRequest(value)
return nil
}