Files
chandra/remote/remote_internal.h
nandiandClaude Opus 5.5 589d167f79
CI / test (pull_request) Successful in 1m22s
Remote: copy from the phone to this device's clipboard
- Web UI: Ctrl+C, Ctrl+X, Ctrl+Shift+C (Cmd+C on Mac) also put what they copied, text or PNG, on this device's clipboard; Copy, Copy Selection and Copy Clipboard buttons
- chandra-remote: GET /api/clipboard/data, with the same authorization as the upload
- Compositor: clipboard-get-fd has the clipboard's owner write into a pipe, refused while locked; input-copy presses Ctrl+Shift+C in terminals, Ctrl+C elsewhere
- Pending clipboard transfers keep the main loop awake

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:26:53 -08:00

135 lines
4.5 KiB
C

#ifndef CHANDRA_REMOTE_INTERNAL_H
#define CHANDRA_REMOTE_INTERNAL_H
#define _POSIX_C_SOURCE 200809L
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <sys/socket.h>
#include "chandra_fb_shm.h"
#define REMOTE_CLIPBOARD_MAX (16u * 1024u * 1024u)
#define REMOTE_AUTH_NONE 0
#define REMOTE_AUTH_PASSWORD 1
typedef struct {
int enabled;
char bind[64]; /* "tailscale", "127.0.0.1", "0.0.0.0", or IP */
int bind_loopback; /* 1 to also bind 127.0.0.1 when bind == "tailscale" */
int auth_mode; /* REMOTE_AUTH_NONE or REMOTE_AUTH_PASSWORD */
int vnc_port;
int web_port;
int jpeg_quality;
int max_fps;
char password[64];
int allow_insecure_open;
} RemoteConfig;
typedef struct {
pthread_mutex_t mu;
int fd;
char sock_path[512];
uint64_t last_try_ms;
} CtlConn;
typedef struct {
int fd;
ChandraFbHeader *hdr;
char shm_path[512];
uint64_t last_open_try_ms;
} FbReader;
typedef struct {
RemoteConfig cfg;
pthread_mutex_t cfg_mu;
char config_path[512];
char web_dir[512];
CtlConn ctl;
uint8_t hmac_key[32];
volatile sig_atomic_t *running;
pthread_mutex_t ping_mu;
pthread_mutex_t osk_mu;
int osk_suppress_refs;
int osk_restore_on_end;
char shm_path[512];
} RemoteApp;
/* Time helper */
uint64_t remote_now_ms(void);
void remote_sleep_ms(int ms);
/* Config & auth (auth.c) */
void remote_default_config(RemoteConfig *cfg);
int remote_bind_is_loopback(const char *bind_str);
int remote_bind_is_tailscale(const char *bind_str);
int remote_load_or_init_config(const char *path, RemoteConfig *cfg);
int remote_save_config(const char *path, const RemoteConfig *cfg);
void remote_generate_password(char *out, size_t len);
int remote_ct_equal(const uint8_t hmac_key[32], const char *a, const char *b);
void vnc_des_encrypt_challenge(const char *password, const uint8_t challenge[16], uint8_t out[16]);
void web_token_sign(const uint8_t key[32], uint64_t now_sec, char *out, size_t cap);
int web_token_verify(const uint8_t key[32], const char *tok, uint64_t now_sec);
int ws_compute_accept(const char *client_key, char *out, size_t cap);
/* Control socket & compositor I/O (chandra-remote.c) */
void ctl_init(CtlConn *c, const char *sock_path);
void ctl_close(CtlConn *c);
int ctl_cmd(CtlConn *c, const char *line, char *reply, size_t reply_cap);
int ctl_fire(CtlConn *c, const char *line);
/* Put `len` bytes on the phone's clipboard as `mime` ("text/plain" or "image/png"); the new
clipboard serial goes to `serial` (may be NULL). 0, or -1 when the compositor refused. */
int remote_clipboard_set(CtlConn *c, const char *mime, const void *data, size_t len,
unsigned long *serial);
enum {
REMOTE_CLIP_OK, REMOTE_CLIP_FAILED, REMOTE_CLIP_EMPTY, REMOTE_CLIP_LOCKED, REMOTE_CLIP_OLD,
REMOTE_CLIP_TOO_LARGE
};
/* The phone's clipboard: its Content-Type (static), bytes (the caller frees) and serial. */
int remote_clipboard_get(CtlConn *c, const char **mime, uint8_t **data, size_t *len,
unsigned long *serial);
/* Current clipboard serial: it changes whenever something is put on the clipboard. */
int remote_clipboard_serial(CtlConn *c, unsigned long *serial);
void remote_osk_suppress_acquire(RemoteApp *app, int *client_flag);
void remote_osk_suppress_release(RemoteApp *app, int *client_flag);
void remote_osk_manual_override(RemoteApp *app);
/* Shared framebuffer reader (chandra-remote.c) */
void fb_reader_init(FbReader *r, const char *shm_path);
void fb_reader_close(FbReader *r);
void fb_reader_ping(FbReader *r);
int fb_reader_grab(
FbReader *r,
uint64_t last_seq,
uint32_t last_w,
uint32_t last_h,
int *out_full,
uint32_t *out_w,
uint32_t *out_h,
int *out_x0,
int *out_y0,
int *out_x1,
int *out_y1,
uint64_t *out_seq,
uint8_t **scratch,
size_t *scratch_cap);
/* Peer & socket helpers (chandra-remote.c) */
int remote_peer_allowed(const RemoteConfig *cfg, const struct sockaddr_storage *ss, socklen_t slen);
int remote_find_tailscale_ipv4(char *out, size_t cap);
/* Keysym & UTF-8 translation (keysym.c) */
int keysym_to_evdev(uint32_t keysym, uint32_t *out_evdev, int *out_shift, int *out_mod_bit, uint32_t *out_unicode);
void remote_send_codepoint(CtlConn *ctl, uint32_t uc);
void remote_send_utf8_text(CtlConn *ctl, const char *utf8);
void hex_encode(const uint8_t *in, size_t len, char *out);
/* RFB & Web servers (rfb.c, web.c) */
void rfb_handle_client(RemoteApp *app, int fd);
void web_handle_client(RemoteApp *app, int fd);
#endif /* CHANDRA_REMOTE_INTERNAL_H */