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
5 changes: 5 additions & 0 deletions components/control_protocol/control_protocol_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ bool rpc_get_usb_emulation_state(const jetkvm_control_context_t *context,
bool rpc_set_usb_emulation_state(const jetkvm_control_context_t *context,
const cJSON *params,
rpc_dispatch_result_t *dispatch);
extern jetkvm_pointer_context_t s_default_pointer;
bool rpc_get_pointer_type(const jetkvm_control_context_t *context,
const cJSON *params, rpc_dispatch_result_t *dispatch);
bool rpc_set_pointer_type(const jetkvm_control_context_t *context,
const cJSON *params, rpc_dispatch_result_t *dispatch);
extern jetkvm_usb_debugging_context_t s_default_usb_debugging;
bool rpc_get_usb_debugging_state(const jetkvm_control_context_t *context,
const cJSON *params,
Expand Down
16 changes: 16 additions & 0 deletions components/control_protocol/include/jetkvm_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,20 @@ typedef struct {
} jetkvm_usb_debugging_context_t;
void jetkvm_control_set_default_usb_debugging_context(const jetkvm_usb_debugging_context_t *context);

/* The pointer type (components/jetkvm_pointer): "mouse" or "pen". set stores
* and applies it and returns NULL, or the reason it could not; a change
* presents USB again. A registered context adds the "pointer_types"
* capability. setPointerType uses the request's context (pointer), which the
* RPC worker fills with the default, because set writes NVS and waits for
* USB. */
typedef struct {
const char *(*get)(void *ctx);
const char *(*set)(const char *type, void *ctx);
void *ctx;
} jetkvm_pointer_context_t;
void jetkvm_control_set_default_pointer_context(const jetkvm_pointer_context_t *context);
jetkvm_pointer_context_t jetkvm_control_default_pointer_context(void);

/* Certificates are public; get never exposes private key material. */
typedef struct {
bool (*get)(char *mode, size_t mode_capacity, char *certificate, size_t capacity, void *ctx);
Expand Down Expand Up @@ -1385,6 +1399,8 @@ typedef struct {
jetkvm_guest_clipboard_context_t guest_clipboard;
/** Per request, as guest_clipboard. */
jetkvm_guest_file_context_t guest_file;
/** Per request, as guest_clipboard: only its set is used. */
jetkvm_pointer_context_t pointer;
void *network_ctx;
void *hid_state_ctx;
/** Called for each known method that counts as activity
Expand Down
2 changes: 2 additions & 0 deletions components/control_protocol/jetkvm_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ static const rpc_method_t s_rpc_methods[] = {
{ "setUsbDeviceState", rpc_set_usb_device_state },
{ "getUsbEmulationState", rpc_get_usb_emulation_state },
{ "setUsbEmulationState", rpc_set_usb_emulation_state },
{ "getPointerType", rpc_get_pointer_type },
{ "setPointerType", rpc_set_pointer_type },
{ "getUsbDebuggingState", rpc_get_usb_debugging_state },
{ "setUsbDebuggingState", rpc_set_usb_debugging_state },
{ "wakeHost", rpc_wake_host },
Expand Down
61 changes: 61 additions & 0 deletions components/control_protocol/rpc_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,67 @@ bool rpc_set_usb_emulation_state(
}
}

jetkvm_pointer_context_t s_default_pointer;

void jetkvm_control_set_default_pointer_context(
const jetkvm_pointer_context_t *context)
{
s_default_pointer = context ? *context : (jetkvm_pointer_context_t){ 0 };
}

jetkvm_pointer_context_t jetkvm_control_default_pointer_context(void)
{
return s_default_pointer;
}

static bool pointer_type_state(rpc_dispatch_result_t *dispatch)
{
const char *type = s_default_pointer.get ?
s_default_pointer.get(s_default_pointer.ctx) : NULL;
if (!type) {
dispatch->error_code = -32603;
dispatch->error_message = "Internal error";
dispatch->error_data = "Pointer type unavailable";
return true;
}
dispatch->result = cJSON_CreateString(type);
return dispatch->result != NULL;
}

bool rpc_get_pointer_type(const jetkvm_control_context_t *context,
const cJSON *params, rpc_dispatch_result_t *dispatch)
{
(void)context;
if (!require_no_rpc_params(params, dispatch)) {
return true;
}
return pointer_type_state(dispatch);
}

bool rpc_set_pointer_type(const jetkvm_control_context_t *context,
const cJSON *params, rpc_dispatch_result_t *dispatch)
{
const cJSON *type = rpc_param(params, "type");
if (!cJSON_IsString(type) || !type->valuestring || params->child != type ||
type->next != NULL) {
dispatch->error_code = -32602;
dispatch->error_message = "Invalid params";
dispatch->error_data = "type must be the only parameter and must be a string";
return true;
}
const jetkvm_pointer_context_t *pointer = &context->pointer;
const char *error = pointer->set ?
pointer->set(type->valuestring, pointer->ctx) :
"Pointer type unavailable";
if (error) {
dispatch->error_code = -32603;
dispatch->error_message = "Internal error";
dispatch->error_data = error;
return true;
}
return pointer_type_state(dispatch);
}

jetkvm_usb_debugging_context_t s_default_usb_debugging;

void jetkvm_control_set_default_usb_debugging_context(
Expand Down
4 changes: 4 additions & 0 deletions components/control_protocol/rpc_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ static bool add_capability(cJSON *list, bool present, const char *name)
* orientations instead of upstream's Normal (270) and Inverted (90).
* "usb_debugging": the build registered the USB debugging setting, so the UI
* shows its switch (getUsbDebuggingState, setUsbDebuggingState).
* "pointer_types": the build registered the pointer type, so the UI shows its
* modes (getPointerType, setPointerType).
* "extra_hostnames": the web server takes other names for the device
* (getExtraHostnames, setExtraHostnames), so the UI shows that setting.
* "mdns": the network settings carry mdns_mode, so the UI shows the mDNS
Expand Down Expand Up @@ -85,6 +87,8 @@ static cJSON *device_capabilities_json(const jetkvm_control_context_t *context)
!add_capability(result, ts->get_status && ts->get_config && ts->set_config, "tailscale") ||
!add_capability(result, s_default_usb_debugging.get && s_default_usb_debugging.set,
"usb_debugging") ||
!add_capability(result, s_default_pointer.get && s_default_pointer.set,
"pointer_types") ||
!add_capability(result, s_default_extra_hostnames.get && s_default_extra_hostnames.set,
"extra_hostnames") ||
!add_capability(result, context && context->get_network_settings &&
Expand Down
49 changes: 49 additions & 0 deletions components/control_protocol/tests/test_jetkvm_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,54 @@ static void test_usb_debugging(void)
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":[]}");
}

/* The pointer type: a stored name that applies at once. */
typedef struct {
const char *type, *error;
unsigned sets;
} pointer_test_t;
static const char *pointer_test_get(void *ctx) { return ((pointer_test_t *)ctx)->type; }
static const char *pointer_test_set(const char *type, void *ctx)
{
pointer_test_t *test = ctx;
++test->sets;
if (test->error) return test->error;
test->type = strcmp(type, "pen") == 0 ? "pen" : "mouse";
return NULL;
}
static void test_pointer_type(void)
{
display_test_error("getPointerType", "{}", -32603);
display_test_error("setPointerType", "{\"type\":\"pen\"}", -32603);
pointer_test_t test = {.type = "mouse"};
const jetkvm_pointer_context_t pointer = {
.get = pointer_test_get, .set = pointer_test_set, .ctx = &test};
jetkvm_control_set_default_pointer_context(&pointer);
expect_rpc("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"getDeviceCapabilities\"}",
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":[\"pointer_types\"]}");
expect_rpc("{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"getPointerType\",\"params\":{}}",
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"mouse\"}");
/* A set runs only with the request's context, as the RPC worker gives it. */
display_test_error("setPointerType", "{\"type\":\"pen\"}", -32603);
assert(test.sets == 0U);
const jetkvm_control_context_t worker = {
.pointer = jetkvm_control_default_pointer_context()};
expect_rpc_context(&worker,
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"setPointerType\",\"params\":{\"type\":\"pen\"}}",
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"pen\"}");
/* Bad parameters change nothing. */
display_test_error("setPointerType", "{}", -32602);
display_test_error("setPointerType", "{\"type\":1}", -32602);
display_test_error("setPointerType", "{\"type\":\"pen\",\"x\":1}", -32602);
display_test_error("getPointerType", "{\"type\":\"pen\"}", -32602);
assert(test.sets == 1U && strcmp(test.type, "pen") == 0);
test.error = "Pointer type not saved";
expect_rpc_context(&worker,
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"setPointerType\",\"params\":{\"type\":\"mouse\"}}",
"{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":-32603,\"message\":\"Internal error\",\"data\":\"Pointer type not saved\"}}");
assert(test.sets == 2U && strcmp(test.type, "pen") == 0);
jetkvm_control_set_default_pointer_context(NULL);
}

void test_os_services(void); /* test_os_services.c */


Expand Down Expand Up @@ -2325,6 +2373,7 @@ int main(void)
test_audio_contract();
test_cloud_deregistration();
test_usb_debugging();
test_pointer_type();

expect_rpc(
"{\"jsonrpc\":\"2.0\",\"method\":\"ping\",\"params\":{},\"id\":\"probe-1\"}",
Expand Down
7 changes: 7 additions & 0 deletions components/jetkvm_pointer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
idf_component_register(
SRCS "jetkvm_pointer.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES control_protocol jetkvm_hid_input jetkvm_usb_hid jetkvm_usb_service log nvs_flash
)

target_compile_options(${COMPONENT_LIB} PRIVATE -Werror)
18 changes: 18 additions & 0 deletions components/jetkvm_pointer/include/jetkvm_pointer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2026
* SPDX-License-Identifier: MIT
*/

#pragma once

/*
* The pointer type: what the target computer gets from the page's absolute
* positions. It is in NVS with the other device settings (namespace
* jkvm_settings), so a factory reset erases it. Without a stored value it is
* the absolute mouse.
*
* Reads the setting for the first enumeration and registers the
* getPointerType and setPointerType RPCs. Call once, after NVS is ready and
* before jetkvm_usb_service_start().
*/
void jetkvm_pointer_start(void);
119 changes: 119 additions & 0 deletions components/jetkvm_pointer/jetkvm_pointer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* SPDX-FileCopyrightText: 2026
* SPDX-License-Identifier: MIT
*/

#include "jetkvm_pointer.h"

#include <stdint.h>
#include <string.h>

#include "esp_log.h"
#include "jetkvm_control.h"
#include "jetkvm_hid_input.h"
#include "jetkvm_usb_hid.h"
#include "jetkvm_usb_service.h"
#include "nvs.h"

#define POINTER_NAMESPACE "jkvm_settings"
#define POINTER_KEY "pointer_type"

static const char *TAG = "jetkvm_pointer";

/* The selected type is not on USB yet: its reconnect failed. A request for it
* reconnects again instead of reporting success. Only the RPC worker sets the
* type, one request at a time. */
static bool s_pending;

/* The RPC names, by jetkvm_usb_hid_pointer_type_t, which is the stored value. */
static const char *const s_names[] = {"mouse", "pen"};
#define POINTER_TYPES (sizeof(s_names) / sizeof(s_names[0]))

static esp_err_t load(uint8_t *type)
{
nvs_handle_t handle;
esp_err_t error = nvs_open(POINTER_NAMESPACE, NVS_READONLY, &handle);
if (error != ESP_OK) {
return error;
}
error = nvs_get_u8(handle, POINTER_KEY, type);
nvs_close(handle);
return error == ESP_OK && *type >= POINTER_TYPES ? ESP_ERR_INVALID_STATE : error;
}

static esp_err_t save(uint8_t type)
{
nvs_handle_t handle;
esp_err_t error = nvs_open(POINTER_NAMESPACE, NVS_READWRITE, &handle);
if (error != ESP_OK) {
return error;
}
error = nvs_set_u8(handle, POINTER_KEY, type);
if (error == ESP_OK) {
error = nvs_commit(handle);
}
nvs_close(handle);
return error;
}

static const char *rpc_get(void *ctx)
{
(void)ctx;
return s_names[jetkvm_usb_hid_get_pointer_type()];
}

static const char *rpc_set(const char *name, void *ctx)
{
(void)ctx;
uint8_t type = 0U;
while (type < POINTER_TYPES && strcmp(name, s_names[type]) != 0) {
type++;
}
if (type == POINTER_TYPES) {
return "type must be mouse or pen";
}
const jetkvm_usb_hid_pointer_type_t previous = jetkvm_usb_hid_get_pointer_type();
if (type == (uint8_t)previous && !s_pending) {
return NULL;
}
if (type != (uint8_t)previous) {
esp_err_t error = save(type);
if (error != ESP_OK) {
ESP_LOGW(TAG, "pointer type not saved: %s", esp_err_to_name(error));
return "Pointer type not saved";
}
jetkvm_usb_hid_set_pointer_type((jetkvm_usb_hid_pointer_type_t)type);
}
/* The pen has its own descriptor, so USB has to reconnect. */
s_pending = true;
/* After the switch: a button pressed before it is released, and a report
* after it has the new type. The old descriptor stays until the
* reconnect, so the release reaches the interface that pressed. */
const esp_err_t release = jetkvm_hid_input_release_all();
if (release != ESP_OK) {
ESP_LOGW(TAG, "input not released: %s", esp_err_to_name(release));
}
if (jetkvm_usb_service_reenumerate() !=
JETKVM_USB_SERVICE_CONFIGURATION_COMPLETED) {
return "Pointer type saved; USB could not reconnect";
}
s_pending = false;
ESP_LOGI(TAG, "pointer type %s", s_names[type]);
return NULL;
}

void jetkvm_pointer_start(void)
{
uint8_t type = JETKVM_USB_HID_POINTER_MOUSE;
esp_err_t error = load(&type);
if (error == ESP_OK) {
jetkvm_usb_hid_set_pointer_type((jetkvm_usb_hid_pointer_type_t)type);
} else if (error != ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGW(TAG, "pointer type unreadable, using the mouse: %s",
esp_err_to_name(error));
}
jetkvm_control_set_default_pointer_context(&(jetkvm_pointer_context_t){
.get = rpc_get,
.set = rpc_set,
});
}
12 changes: 12 additions & 0 deletions components/jetkvm_usb_hid/include/jetkvm_usb_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ typedef struct {
void *ctx;
} jetkvm_usb_hid_os_services_hooks_t;

/* What the absolute pointer's interface is to the target computer. */
typedef enum {
JETKVM_USB_HID_POINTER_MOUSE,
/* An integrated pen: hover, tip, barrel button and leaving range. */
JETKVM_USB_HID_POINTER_PEN,
} jetkvm_usb_hid_pointer_type_t;

/* A change takes effect at the next enumeration
* (jetkvm_usb_service_reenumerate). */
void jetkvm_usb_hid_set_pointer_type(jetkvm_usb_hid_pointer_type_t type);
jetkvm_usb_hid_pointer_type_t jetkvm_usb_hid_get_pointer_type(void);

/*
* TinyUSB calls this callback from its CPU0 task. The callback must not block
* or call a HID report function synchronously.
Expand Down
Loading