82 lines
2.7 KiB
C
82 lines
2.7 KiB
C
#ifndef SOCKET_PROTOCOL_H
|
||
#define SOCKET_PROTOCOL_H
|
||
|
||
#include <stdint.h>
|
||
#include <sys/ioctl.h>
|
||
#include <termios.h>
|
||
|
||
// 消息类型枚举
|
||
typedef enum {
|
||
MSG_TYPE_INIT = 1, // 初始化连接,发送命令信息
|
||
MSG_TYPE_WINDOW_SIZE_UPDATE = 2, // 终端窗口大小更新
|
||
MSG_TYPE_SERVER_RESPONSE = 3, // 服务器响应消息
|
||
MSG_TYPE_CLOSE = 4, // 关闭连接
|
||
MSG_TYPE_TERMINAL_INPUT = 5, // 终端输入数据(键盘、鼠标等)
|
||
MSG_TYPE_TERMINAL_OUTPUT = 6, // 终端输出数据
|
||
MSG_TYPE_MOUSE_EVENT = 7, // 鼠标事件
|
||
MSG_TYPE_KEY_EVENT = 8 // 键盘事件
|
||
} MessageType;
|
||
|
||
// 消息头结构(固定大小)
|
||
typedef struct {
|
||
uint32_t magic; // 魔数,用于验证 0x42534D54 ("BSMT")
|
||
uint32_t type; // 消息类型
|
||
uint32_t payload_len; // 载荷长度
|
||
uint32_t reserved; // 保留字段,用于对齐
|
||
} __attribute__((packed)) MessageHeader;
|
||
|
||
// 终端信息结构
|
||
typedef struct {
|
||
uint32_t is_tty; // 是否为TTY
|
||
uint16_t rows; // 行数
|
||
uint16_t cols; // 列数
|
||
uint16_t x_pixel; // X像素
|
||
uint16_t y_pixel; // Y像素
|
||
uint32_t has_termios; // 是否有termios属性
|
||
uint32_t input_flags; // termios输入标志
|
||
uint32_t output_flags; // termios输出标志
|
||
uint32_t control_flags; // termios控制标志
|
||
uint32_t local_flags; // termios本地标志
|
||
} __attribute__((packed)) TerminalInfoFixed;
|
||
|
||
// 鼠标事件类型
|
||
typedef enum {
|
||
MOUSE_BUTTON_PRESS = 1,
|
||
MOUSE_BUTTON_RELEASE = 2,
|
||
MOUSE_MOVE = 3,
|
||
MOUSE_SCROLL_UP = 4,
|
||
MOUSE_SCROLL_DOWN = 5
|
||
} MouseEventType;
|
||
|
||
// 鼠标事件结构
|
||
typedef struct {
|
||
uint32_t event_type; // MouseEventType
|
||
uint32_t button; // 鼠标按钮(1=左键,2=中键,3=右键)
|
||
uint32_t x; // X坐标
|
||
uint32_t y; // Y坐标
|
||
uint32_t modifiers; // 修饰键(Shift, Ctrl, Alt等)
|
||
} __attribute__((packed)) MouseEvent;
|
||
|
||
// 键盘事件结构
|
||
typedef struct {
|
||
uint32_t key_code; // 键码
|
||
uint32_t modifiers; // 修饰键
|
||
uint32_t is_press; // 1=按下,0=释放
|
||
} __attribute__((packed)) KeyEvent;
|
||
|
||
// 魔数定义
|
||
#define MESSAGE_MAGIC 0x42534D54 // "BSMT"
|
||
|
||
// 函数声明
|
||
int write_message(int sock, MessageType type, const void* payload, uint32_t payload_len);
|
||
int read_message(int sock, MessageType* type, void** payload, uint32_t* payload_len);
|
||
void free_message_payload(void* payload);
|
||
|
||
// 终端输入捕获相关函数
|
||
int setup_terminal_raw_mode(int fd);
|
||
int restore_terminal_mode(int fd);
|
||
int enable_mouse_tracking(int fd);
|
||
int disable_mouse_tracking(int fd);
|
||
|
||
#endif // SOCKET_PROTOCOL_H
|