1pub mod hextile;
2pub mod raw;
3pub mod zlib;
4
5use crate::error::Result;
6
7pub trait Encoder {
8 fn encoding_id(&self) -> i32;
9 fn encode_rect(
10 &mut self,
11 pixels: &[u8],
12 stride: usize,
13 x: u16,
14 y: u16,
15 w: u16,
16 h: u16,
17 swap_rb: bool,
18 ) -> Result<Vec<u8>>;
19}
20
21pub struct EncoderSet {
22 raw: raw::RawEncoder,
23 hextile: hextile::HextileEncoder,
24 zlib: zlib::ZlibCompressor,
25 active: i32,
26}
27
28impl EncoderSet {
29 pub fn new() -> Self {
30 Self {
31 raw: raw::RawEncoder,
32 hextile: hextile::HextileEncoder,
33 zlib: zlib::ZlibCompressor::new(),
34 active: 0,
35 }
36 }
37
38 pub fn negotiate(&mut self, client_encodings: &[i32]) {
39 let our_priority = [5, 6, 0];
40
41 for &our_enc in &our_priority {
42 if client_encodings.contains(&our_enc) {
43 let name = match our_enc {
44 0 => "Raw",
45 5 => "Hextile",
46 6 => "Zlib",
47 _ => "Unknown",
48 };
49 println!("🎨 Negotiated encoding: {}", name);
50 self.active = our_enc;
51 return;
52 }
53 }
54 println!("🎨 Negotiated encoding: Raw (fallback)");
55 self.active = 0;
56 }
57
58 pub fn encoding_id(&self) -> i32 {
59 self.active
60 }
61
62 pub fn encode_rect(
63 &mut self,
64 pixels: &[u8],
65 stride: usize,
66 x: u16,
67 y: u16,
68 w: u16,
69 h: u16,
70 swap_rb: bool,
71 ) -> Result<Vec<u8>> {
72 match self.active {
73 5 => self
74 .hextile
75 .encode_rect(pixels, stride, x, y, w, h, swap_rb),
76 6 => self.zlib.encode_rect(pixels, stride, x, y, w, h, swap_rb),
77 _ => self.raw.encode_rect(pixels, stride, x, y, w, h, swap_rb),
78 }
79 }
80}