Subversion Repositories Kolibri OS

Rev

Rev 5666 | Rev 5670 | 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  1, "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  1, "\nunknown encoding: %u\n", eax
  191.         jmp     thread_loop
  192.  
  193. next_rectangle:
  194.         push    esi
  195.         call    drawbuffer
  196.         pop     esi
  197.  
  198.         dec     [rectangles]
  199.         jnz     rectangle_loop
  200.         jmp     request_fbu
  201.  
  202.  
  203. setcolourmapentries:
  204.  
  205.         DEBUGF  1, "Server sent SetColourMapEntries message\n"
  206.  
  207.         ; TODO
  208.  
  209.         jmp     thread_loop
  210.  
  211.  
  212. bell:
  213.         mcall   55, 55, , , beep
  214.         jmp     thread_loop
  215.  
  216.  
  217. servercuttext:
  218.  
  219.         DEBUGF  1, "Server cut text\n"
  220.  
  221.   @@:
  222.         lea     eax, [esi+7]
  223.         cmp     [datapointer], eax
  224.         jae     @f
  225.         call    read_data.more
  226.         jmp     @b
  227.   @@:
  228.  
  229.         add     esi, 3
  230.         lodsd
  231.         bswap   eax
  232.         mov     ecx, eax
  233.  
  234.   @@:
  235.         lea     eax, [esi+ecx]
  236.         cmp     [datapointer], eax
  237.         jae     @f
  238.         call    read_data.more
  239.         jmp     @b
  240.   @@:
  241.  
  242.         ; TODO: paste text to clipboard
  243.  
  244.         DEBUGF  1, "%u bytes of text\n", ecx
  245.         add     esi, ecx
  246.         jmp     thread_loop
  247.  
  248.  
  249. read_data:
  250.         mov     [datapointer], receive_buffer
  251.         mov     esi, receive_buffer
  252.   .more:
  253.         push    ebx ecx edx esi edi
  254.         mcall   recv, [socketnum], [datapointer], 4096, 0
  255.         pop     edi esi edx ecx ebx
  256.         cmp     eax, -1
  257.         je      err_disconnected
  258.         add     [datapointer], eax
  259. ; Check for buffer overflow
  260.         cmp     [datapointer], receive_buffer + RECEIVE_BUFFER_SIZE
  261.         jbe     @f
  262.         ; Buffer is full, first needed data by program is pointed to by esi.
  263.         ; Move all remaining data, starting from esi, to begin of buffer
  264.         cmp     esi, receive_buffer
  265.         je      err_proto
  266.         mov     ecx, [datapointer]
  267.         sub     ecx, esi
  268.         mov     edi, receive_buffer
  269.         rep movsb
  270.         mov     [datapointer], edi
  271.         mov     esi, receive_buffer
  272.   @@:
  273.         cmp     eax, 4096
  274.         je      .more
  275.         ret
  276.  
  277.  
  278. wait_for_data:  ; FIXME: add timeout
  279.         mcall   recv, [socketnum], receive_buffer, 4096, 0
  280.         cmp     eax, -1
  281.         je      err_disconnected
  282.         test    eax, eax
  283.         jz      wait_for_data
  284.         ret
  285.  
  286.  
  287. err_disconnected:
  288.         mov     [status], STATUS_DISCONNECTED
  289.         inc     [update_gui]
  290.         mcall   -1
  291.  
  292. err_dns:
  293.         mov     [status], STATUS_DNS_ERR
  294.         inc     [update_gui]
  295.         mcall   -1
  296.  
  297. err_sock:
  298.         mov     [status], STATUS_SOCK_ERR
  299.         inc     [update_gui]
  300.         mcall   -1
  301.  
  302. err_connect:
  303.         mov     [status], STATUS_CONNECT_ERR
  304.         inc     [update_gui]
  305.         mcall   -1
  306.         ret
  307.  
  308. err_proto:
  309.         mov     [status], STATUS_PROTO_ERR
  310.         inc     [update_gui]
  311.         mcall   -1
  312.         ret
  313.  
  314. err_security:
  315.         mov     [status], STATUS_SECURITY_ERR
  316.         inc     [update_gui]
  317.         mcall   -1
  318.         ret
  319.