Subversion Repositories Kolibri OS

Rev

Rev 5668 | Rev 5677 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2010-2015. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  VNC client for KolibriOS                                       ;;
  7. ;;                                                                 ;;
  8. ;;  Written by hidnplayr@kolibrios.org                             ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. thread_start:
  16.  
  17.         mcall   40, 0                   ; disable all events for this thread
  18.  
  19. ; resolve name
  20.         push    esp                     ; reserve stack place
  21.         invoke  getaddrinfo, serveraddr, 0, 0, esp
  22.         pop     esi
  23. ; test for error
  24.         test    eax, eax
  25.         jnz     err_dns
  26.  
  27.         mov     eax, [esi+addrinfo.ai_addr]
  28.         mov     eax, [eax+sockaddr_in.sin_addr]
  29.         mov     [sockaddr1.ip], eax
  30.  
  31.         DEBUGF  1, "Connecting to %u.%u.%u.%u:%u\n", \
  32.                 [sockaddr1.ip]:1, [sockaddr1.ip+1]:1, \
  33.                 [sockaddr1.ip+2]:1, [sockaddr1.ip+3]:1, \
  34.                 [sockaddr1.port]:2
  35.  
  36.         invoke  freeaddrinfo, esi
  37.  
  38.         mcall   socket, AF_INET4, SOCK_STREAM, 0
  39.         cmp     eax, -1
  40.         je      err_sock
  41.  
  42.         mov     [socketnum], eax
  43.         mcall   connect, [socketnum], sockaddr1, 18
  44.         cmp     eax, -1
  45.         je      err_connect
  46.  
  47.         ; TODO: implement timeout
  48.         call    wait_for_data
  49.  
  50.         cmp     dword[receive_buffer], "RFB "
  51.         jne     err_proto
  52.         DEBUGF  1, "received: %s\n", receive_buffer
  53.         mcall   send, [socketnum], HandShake, 12, 0
  54.         DEBUGF  1, "Sending handshake: protocol version\n"
  55.  
  56.         call    wait_for_data
  57.  
  58.         cmp     dword[receive_buffer], 0x01000000
  59.         je      no_security
  60.         cmp     dword[receive_buffer], 0x02000000
  61.         je      vnc_security
  62.         jmp     err_security
  63.  
  64. vnc_security:
  65.         mov     [status], STATUS_LOGIN
  66.         call    draw_gui
  67.  
  68. no_security:
  69.         mcall   send, [socketnum], ClientInit, 1, 0
  70.         DEBUGF  1, "ClientInit sent\n"
  71.  
  72.         call    wait_for_data       ; now the server should send init message
  73.  
  74.         DEBUGF  1, "Serverinit: bpp: %u depth: %u bigendian: %u truecolor: %u\n", \
  75.         [receive_buffer+framebuffer.pixelformat.bpp]:1, \
  76.         [receive_buffer+framebuffer.pixelformat.depth]:1, \
  77.         [receive_buffer+framebuffer.pixelformat.big_endian]:1, \
  78.         [receive_buffer+framebuffer.pixelformat.true_color]:1
  79.  
  80.         mov     eax, dword[receive_buffer+framebuffer.width]
  81.         mov     dword[FramebufferUpdateRequest.width], eax
  82.         bswap   eax
  83.         mov     dword[screen], eax
  84.         DEBUGF  1, "Screen width=%u, height=%u\n", [screen.width]:2, [screen.height]:2
  85.  
  86.         mcall   send, [socketnum], SetPixelFormat8, 20, 0
  87.         DEBUGF  1, "Sending pixel format\n"
  88.  
  89.         mcall   send, [socketnum], SetEncodings, 12, 0
  90.         DEBUGF  1, "Sending encoding info\n"
  91.  
  92. ; Set main window caption to servername
  93.         mov     ecx, dword[receive_buffer+framebuffer.name_length]
  94.         bswap   ecx
  95.         cmp     ecx, 64
  96.         jbe     @f
  97.         mov     ecx, 64
  98.   @@:
  99.         lea     esi, [receive_buffer+framebuffer.name]
  100.         mov     edi, servername
  101.         rep movsb
  102.         mov     byte[edi], 0
  103.         mov     [name.dash], "-"
  104.  
  105. ; Tell the main thread we are ready for business!
  106.         mov     [status], STATUS_CONNECTED
  107.  
  108. ; Request initial update
  109.         mov     [FramebufferUpdateRequest.inc], 0
  110.  
  111. request_fbu:
  112.         DEBUGF  1, "Requesting framebuffer update\n"
  113.         mcall   send, [socketnum], FramebufferUpdateRequest, 10, 0
  114.         mov     [FramebufferUpdateRequest.inc], 1
  115.  
  116. thread_loop:
  117.         call    read_data              ; Read the data into the buffer
  118.  
  119.         lodsb
  120.         cmp     al, 0
  121.         je      framebufferupdate
  122.         cmp     al, 1
  123.         je      setcolourmapentries
  124.         cmp     al, 2
  125.         je      bell
  126.         cmp     al, 3
  127.         je      servercuttext
  128.  
  129.  
  130.         DEBUGF  2, "Unknown server command: %u\n", al
  131.         jmp     thread_loop
  132.  
  133. framebufferupdate:
  134.  
  135.   @@:
  136.         lea     eax, [esi+6]
  137.         cmp     [datapointer], eax
  138.         jae     @f
  139.         call    read_data.more
  140.         jmp     @b
  141.   @@:
  142.  
  143.         inc     esi     ; padding
  144.         lodsw
  145.         xchg    al, ah
  146.         mov     [rectangles], ax
  147.         DEBUGF  1, "Framebufferupdate: %u rectangles\n", ax
  148.  
  149. rectangle_loop:
  150.  
  151.   @@:
  152.         lea     eax, [esi+12]
  153.         cmp     [datapointer], eax
  154.         jae     @f
  155.         call    read_data.more
  156.         jmp     @b
  157.   @@:
  158.  
  159.         xor     eax, eax
  160.         lodsw
  161.         xchg    al, ah
  162.         mov     [rectangle.x], eax
  163.         lodsw
  164.         xchg    al, ah
  165.         mov     [rectangle.y], eax
  166.         lodsw
  167.         xchg    al, ah
  168.         mov     [rectangle.width], eax
  169.         lodsw
  170.         xchg    al, ah
  171.         mov     [rectangle.height], eax
  172.  
  173.         lodsd                           ; encoding
  174.         DEBUGF  1, "rectangle: width=%u height=%u x=%u y=%u encoding: ",\
  175.         [rectangle.width]:2, [rectangle.height]:2, [rectangle.x]:2, [rectangle.y]:2
  176.  
  177.         cmp     eax, 0
  178.         je      encoding_raw
  179.         cmp     eax, 1
  180.         je      encoding_CopyRect
  181.         cmp     eax, 2
  182.         je      encoding_RRE
  183. ;        cmp     eax, 5
  184. ;        je      encoding_hextile
  185. ;        cmp     eax, 15
  186. ;        je      encoding_TRLE
  187. ;        cmp     eax, 16
  188. ;        je      encoding_ZRLE
  189.  
  190.         DEBUGF  2, "unknown encoding: %u\n", eax
  191.         jmp     thread_loop
  192.  
  193. next_rectangle:
  194.         inc     [update_framebuffer]
  195.         dec     [rectangles]
  196.         jnz     rectangle_loop
  197.         jmp     request_fbu
  198.  
  199.  
  200. setcolourmapentries:
  201.  
  202.         DEBUGF  1, "Server sent SetColourMapEntries message\n"
  203.  
  204.         ; TODO
  205.  
  206.         jmp     thread_loop
  207.  
  208.  
  209. bell:
  210.         mcall   55, 55, , , beep
  211.         jmp     thread_loop
  212.  
  213.  
  214. servercuttext:
  215.  
  216.         DEBUGF  1, "Server cut text\n"
  217.  
  218.   @@:
  219.         lea     eax, [esi+7]
  220.         cmp     [datapointer], eax
  221.         jae     @f
  222.         call    read_data.more
  223.         jmp     @b
  224.   @@:
  225.  
  226.         add     esi, 3
  227.         lodsd
  228.         bswap   eax
  229.         mov     ecx, eax
  230.  
  231.   @@:
  232.         lea     eax, [esi+ecx]
  233.         cmp     [datapointer], eax
  234.         jae     @f
  235.         call    read_data.more
  236.         jmp     @b
  237.   @@:
  238.  
  239.         ; TODO: paste text to clipboard
  240.  
  241.         DEBUGF  1, "%u bytes of text\n", ecx
  242.         add     esi, ecx
  243.         jmp     thread_loop
  244.  
  245.  
  246. read_data:
  247.         mov     [datapointer], receive_buffer
  248.         mov     esi, receive_buffer
  249.   .more:
  250.         push    ebx ecx edx esi edi
  251.         mcall   recv, [socketnum], [datapointer], 4096, 0
  252.         pop     edi esi edx ecx ebx
  253.         cmp     eax, -1
  254.         je      err_disconnected
  255.         add     [datapointer], eax
  256. ; Check for buffer overflow
  257.         cmp     [datapointer], receive_buffer + RECEIVE_BUFFER_SIZE
  258.         jbe     @f
  259.         ; Buffer is full, first needed data by program is pointed to by esi.
  260.         ; Move all remaining data, starting from esi, to begin of buffer
  261.         cmp     esi, receive_buffer
  262.         je      err_proto
  263.         mov     ecx, [datapointer]
  264.         sub     ecx, esi
  265.         mov     edi, receive_buffer
  266.         rep movsb
  267.         mov     [datapointer], edi
  268.         mov     esi, receive_buffer
  269.   @@:
  270.         cmp     eax, 4096
  271.         je      .more
  272.         ret
  273.  
  274.  
  275. wait_for_data:  ; FIXME: add timeout
  276.         mcall   recv, [socketnum], receive_buffer, 4096, 0
  277.         cmp     eax, -1
  278.         je      err_disconnected
  279.         test    eax, eax
  280.         jz      wait_for_data
  281.         ret
  282.  
  283.  
  284. err_disconnected:
  285.         mov     [status], STATUS_DISCONNECTED
  286.         inc     [update_gui]
  287.         mcall   -1
  288.  
  289. err_dns:
  290.         mov     [status], STATUS_DNS_ERR
  291.         inc     [update_gui]
  292.         mcall   -1
  293.  
  294. err_sock:
  295.         mov     [status], STATUS_SOCK_ERR
  296.         inc     [update_gui]
  297.         mcall   -1
  298.  
  299. err_connect:
  300.         mov     [status], STATUS_CONNECT_ERR
  301.         inc     [update_gui]
  302.         mcall   -1
  303.         ret
  304.  
  305. err_proto:
  306.         mov     [status], STATUS_PROTO_ERR
  307.         inc     [update_gui]
  308.         mcall   -1
  309.         ret
  310.  
  311. err_security:
  312.         mov     [status], STATUS_SECURITY_ERR
  313.         inc     [update_gui]
  314.         mcall   -1
  315.         ret
  316.