acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

VNC.h (7641B)

      1 /*
      2  * @file VNC.h
      3  * @date 11.05.2015
      4  * @author Markus Sattler
      5  *
      6  * Copyright (c) 2015 Markus Sattler. All rights reserved.
      7  * This file is part of the VNC client for Arduino.
      8  *
      9  * This program is free software; you can redistribute it and/or modify
     10  * it under the terms of the GNU General Public License as published by
     11  * the Free Software Foundation; version 2 of the License
     12  *
     13  * This program is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  * GNU General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU General Public License
     19  * along with this program; if not, a copy can be downloaded from
     20  * http://www.gnu.org/licenses/gpl.html, or obtained by writing to the
     21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     22  * Boston, MA 02110-1301, USA.
     23  *
     24  * Thanks to all that worked on the original VNC implementation
     25  *
     26  */
     27 
     28 #ifndef VNC_H_
     29 #define VNC_H_
     30 
     31 #include "VNC_config.h"
     32 
     33 /// more save free
     34 #define freeSec(ptr) \
     35   free(ptr);         \
     36   ptr = 0
     37 
     38 #include "Arduino.h"
     39 
     40 #ifdef VNC_ZRLE
     41 #if defined(ESP32)
     42 #include "esp32/rom/miniz.h"
     43 #else
     44 #include "miniz.h"
     45 #endif
     46 #endif // #ifdef VNC_ZRLE
     47 
     48 #ifdef USE_ARDUINO_TCP
     49 
     50 #if defined(ESP32)
     51 #include <WiFi.h>
     52 #elif defined(ESP8266)
     53 #include <ESP8266WiFi.h>
     54 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
     55 #include <WiFi.h>
     56 #elif defined(RTL8722DM)
     57 #include <WiFi.h>
     58 #else // default platform
     59 
     60 #include <UIPEthernet.h>
     61 #ifndef UIPETHERNET_H
     62 #include <Ethernet.h>
     63 #include <SPI.h>
     64 #endif // #ifndef UIPETHERNET_H
     65 
     66 #endif // default platform
     67 
     68 #endif // #ifdef USE_ARDUINO_TCP
     69 
     70 #define MAXPWLEN 8
     71 #define CHALLENGESIZE 16
     72 
     73 #define MAX_ENCODINGS 20
     74 
     75 #ifdef WORDS_BIGENDIAN
     76 #define Swap16IfLE(s) (s)
     77 #define Swap32IfLE(l) (l)
     78 #else
     79 #define Swap16IfLE(s) __builtin_bswap16(s)
     80 #define Swap32IfLE(l) __builtin_bswap32(l)
     81 #endif /* WORDS_BIGENDIAN */
     82 
     83 typedef uint8_t CARD8;
     84 typedef int8_t INT8;
     85 typedef uint16_t CARD16;
     86 typedef int16_t INT16;
     87 typedef uint32_t CARD32;
     88 typedef int32_t INT32;
     89 
     90 typedef struct serversettings
     91 {
     92   char *name;
     93   int width;
     94   int height;
     95   int bpp;
     96   int depth;
     97   int bigendian;
     98   int truecolour;
     99   int redmax;
    100   int greenmax;
    101   int bluemax;
    102   int redshift;
    103   int greenshift;
    104   int blueshift;
    105 } serversettings_t;
    106 
    107 typedef struct clientsettings
    108 {
    109   int width;
    110   int height;
    111   int bpp;
    112   int depth;
    113   int bigendian;
    114   int truecolour;
    115   int redmax;
    116   int greenmax;
    117   int bluemax;
    118   int redshift;
    119   int greenshift;
    120   int blueshift;
    121   int compresslevel;
    122   int quality;
    123 } clientsettings_t;
    124 
    125 //__dfb_vnc_options
    126 typedef struct
    127 {
    128   // char *servername;
    129   // int port;
    130   char *password;
    131   // char *passwordfile;
    132   // char *encodings;
    133   // char *modmapfile;
    134   serversettings_t server;
    135   clientsettings_t client;
    136   int shared;
    137   int stretch;
    138   int localcursor;
    139   // int poll_freq;
    140   /* not really options, but hey ;) */
    141   double h_ratio;
    142   double v_ratio;
    143   int h_offset;
    144   int v_offset;
    145 } dfb_vnc_options;
    146 
    147 typedef struct
    148 {
    149   int x;
    150   int y;
    151   unsigned int buttonmask;
    152 } mousestate_t;
    153 
    154 #include "rfbproto.h"
    155 
    156 class VNCdisplay
    157 {
    158 protected:
    159   VNCdisplay() {}
    160 
    161 public:
    162   virtual ~VNCdisplay() {}
    163 
    164   virtual uint32_t getHeight(void) = 0;
    165   virtual uint32_t getWidth(void) = 0;
    166 
    167   virtual bool hasCopyRect(void) = 0;
    168   virtual void copy_rect(uint32_t src_x, uint32_t src_y, uint32_t dest_x, uint32_t dest_y, uint32_t w, uint32_t h){};
    169 
    170   virtual void draw_area(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t *data) = 0;
    171 
    172   virtual void draw_rect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t color) = 0;
    173 
    174   virtual void flush() {};
    175 
    176   virtual void vnc_options_override(dfb_vnc_options *opt){};
    177 };
    178 
    179 class arduinoVNC
    180 {
    181 public:
    182   arduinoVNC(VNCdisplay *display);
    183   ~arduinoVNC(void);
    184 
    185   void begin(char *host, uint16_t port = 5900, bool onlyFullUpdate = false);
    186   void begin(const char *host, uint16_t port = 5900, bool onlyFullUpdate = false);
    187   void begin(String host, uint16_t port = 5900, bool onlyFullUpdate = false);
    188 
    189   void setPassword(char *pass);
    190   void setPassword(const char *pass);
    191   void setPassword(String pass);
    192 
    193   bool connected(void);
    194   void reconnect(void);
    195 
    196   void loop(void);
    197 
    198   int forceFullUpdate(void);
    199 
    200   void mouseEvent(uint16_t x, uint16_t y, uint8_t buttonMask);
    201   void keyEvent(int key, int down_flag);
    202 
    203 private:
    204   bool onlyFullUpdate;
    205   int port;
    206   String host;
    207   String password;
    208 
    209   VNCdisplay *display;
    210 
    211   dfb_vnc_options opt;
    212 
    213   uint8_t protocolMinorVersion;
    214 
    215   int sock;
    216   mousestate_t mousestate;
    217 
    218   /// TCP handling
    219   void disconnect(void);
    220   bool read_from_rfb_server(int sock, char *out, size_t n);
    221 
    222 #ifdef VNC_ZRLE
    223   bool read_from_z(uint8_t *out, size_t n);
    224 #endif // #ifdef VNC_ZRLE
    225 
    226   bool write_exact(int sock, char *buf, size_t n);
    227   bool set_non_blocking(int sock);
    228 
    229   /// Connect to Server
    230   bool rfb_connect_to_server(const char *server, int display);
    231   bool rfb_initialise_connection();
    232 
    233   bool _read_conn_failed_reason(void);
    234   bool _read_authentication_result(void);
    235 
    236   bool _rfb_negotiate_protocol(void);
    237   bool _rfb_authenticate(void);
    238   bool _rfb_initialise_client(void);
    239   bool _rfb_initialise_server(void);
    240 
    241   bool rfb_set_format_and_encodings();
    242   bool rfb_set_desktop_size();
    243   bool rfb_send_update_request(int incremental);
    244   bool rfb_set_continuous_updates(bool enable);
    245   bool rfb_handle_server_message();
    246   bool rfb_update_mouse();
    247   bool rfb_send_key_event(int key, int down_flag);
    248 
    249   // void rfb_get_rgb_from_data(int *r, int *g, int *b, char *data);
    250 
    251   /// Encode handling
    252   bool _handle_server_cut_text_message(rfbServerToClientMsg *msg);
    253 
    254   bool _handle_raw_encoded_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    255   bool _handle_raw_encoded_message_core(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    256   bool _handle_copyrect_encoded_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    257 #ifdef VNC_RRE
    258   bool _handle_rre_encoded_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    259 #endif
    260 #ifdef VNC_CORRE
    261   bool _handle_corre_encoded_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    262 #endif
    263 #ifdef VNC_HEXTILE
    264   bool _handle_hextile_encoded_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    265 #endif
    266 #ifdef VNC_ZRLE
    267   bool _handle_zrle_encoded_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    268 #endif
    269   bool _handle_cursor_pos_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    270 
    271   bool _handle_server_continuous_updates_message(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
    272 
    273   /// Encryption
    274   void vncRandomBytes(unsigned char *bytes);
    275   void vncEncryptBytes(unsigned char *bytes, char *passwd);
    276 
    277 #ifdef USE_ARDUINO_TCP
    278 
    279 #if defined(ESP32)
    280   WiFiClient TCPclient;
    281 #elif defined(ARDUINO_RASPBERRY_PI_PICO_W)
    282   WiFiClient TCPclient;
    283 #elif defined(ESP8266)
    284   WiFiClient TCPclient;
    285 #elif defined(RTL8722DM)
    286   WiFiClient TCPclient;
    287 #else // default platform
    288 
    289 #ifdef UIPETHERNET_H
    290   UIPClient TCPclient;
    291 #else
    292   EthernetClient TCPclient;
    293 #endif
    294 
    295 #endif // default platform
    296 
    297 #endif // #ifdef USE_ARDUINO_TCP
    298 
    299 #ifdef TCP_BUFFER_SIZE
    300   uint8_t tcp_buffer[TCP_BUFFER_SIZE];
    301   size_t buf_idx = 0;
    302   size_t buf_remain = 0;
    303 #endif
    304 
    305   uint32_t maxSize = VNC_RAW_BUFFER;
    306   char raw_buffer[VNC_RAW_BUFFER];
    307   uint16_t framebuffer[FB_SIZE];
    308 
    309 #ifdef VNC_ZRLE
    310   tinfl_decompressor inflator;
    311   bool header_inited = false;
    312 
    313   uint8_t *zin_buffer;
    314   size_t allocated_zin = 0, zin_buf_size = 0, zin_buf_ofs = 0;
    315 
    316   uint8_t *zdict;
    317   size_t zdict_ofs = 0;
    318 
    319   uint8_t *zout_buffer;
    320   size_t zout_buf_idx = 0, zout_buf_remain = 0;
    321 
    322   uint16_t palette[127];
    323 #endif
    324 };
    325 
    326 #endif /* MARKUS_VNC_H_ */