Subversion Repositories Kolibri OS

Rev

Rev 5670 | Rev 5680 | 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.         DEBUGF  1, "Sending pixel format\n"
  87. if BITS_PER_PIXEL = 8
  88.         mcall   send, [socketnum], SetPixelFormat8, 20, 0
  89. else if BITS_PER_PIXEL = 16
  90.         mcall   send, [socketnum], SetPixelFormat16, 20, 0
  91. else
  92.         mcall   send, [socketnum], SetPixelFormat24, 20, 0
  93. end if
  94.  
  95.         DEBUGF  1, "Sending encoding info\n"
  96.         mcall   send, [socketnum], SetEncodings, 12, 0
  97.  
  98. ; Set main window caption to servername
  99.         mov     ecx, dword[receive_buffer+framebuffer.name_length]
  100.         bswap   ecx
  101.         cmp     ecx, 64
  102.         jbe     @f
  103.         mov     ecx, 64
  104.   @@:
  105.         lea     esi, [receive_buffer+framebuffer.name]
  106.         mov     edi, servername
  107.         rep movsb
  108.         mov     byte[edi], 0
  109.         mov     [name.dash], "-"
  110.  
  111. ; Tell the main thread we are ready for business!
  112.         mov     [status], STATUS_CONNECTED
  113.  
  114. ; Request initial update
  115.         mov     [FramebufferUpdateRequest.inc], 0
  116.  
  117. request_fbu:
  118.         DEBUGF  1, "Requesting framebuffer update\n"
  119.         mcall   send, [socketnum], FramebufferUpdateRequest, 10, 0
  120.         mov     [FramebufferUpdateRequest.inc], 1
  121.  
  122. thread_loop:
  123.         call    read_data              ; Read the data into the buffer
  124.  
  125.         lodsb
  126.         cmp     al, 0
  127.         je      framebufferupdate
  128.         cmp     al, 1
  129.         je      setcolourmapentries
  130.         cmp     al, 2
  131.         je      bell
  132.         cmp     al, 3
  133.         je      servercuttext
  134.  
  135.         DEBUGF  2, "Unknown server command: %u\n", al
  136.         jmp     thread_loop
  137.  
  138. framebufferupdate:
  139.  
  140.   @@:
  141.         lea     eax, [esi+6]
  142.         cmp     [datapointer], eax
  143.         jae     @f
  144.         call    read_data.more
  145.         jmp     @b
  146.   @@:
  147.  
  148.         inc     esi     ; padding
  149.         lodsw
  150.         xchg    al, ah
  151.         mov     [rectangles], ax
  152.         DEBUGF  1, "Framebufferupdate: %u rectangles\n", ax
  153.  
  154. rectangle_loop:
  155.  
  156.   @@:
  157.         lea     eax, [esi+12]
  158.         cmp     [datapointer], eax
  159.         jae     @f
  160.         call    read_data.more
  161.         jmp     @b
  162.   @@:
  163.  
  164.         xor     eax, eax
  165.         lodsw
  166.         xchg    al, ah
  167.         mov     [rectangle.x], eax
  168.         lodsw
  169.         xchg    al, ah
  170.         mov     [rectangle.y], eax
  171.         lodsw
  172.         xchg    al, ah
  173.         mov     [rectangle.width], eax
  174.         lodsw
  175.         xchg    al, ah
  176.         mov     [rectangle.height], eax
  177.  
  178.         lodsd                           ; encoding
  179.         DEBUGF  1, "rectangle: width=%u height=%u x=%u y=%u encoding: ",\
  180.         [rectangle.width]:2, [rectangle.height]:2, [rectangle.x]:2, [rectangle.y]:2
  181.  
  182.         cmp     eax, 0
  183.         je      encoding_raw
  184.         cmp     eax, 1
  185.         je      encoding_CopyRect
  186.         cmp     eax, 2
  187.         je      encoding_RRE
  188. ;        cmp     eax, 5
  189. ;        je      encoding_hextile
  190. ;        cmp     eax, 15
  191. ;        je      encoding_TRLE
  192. ;        cmp     eax, 16
  193. ;        je      encoding_ZRLE
  194.  
  195.         DEBUGF  2, "unknown encoding: %u\n", eax
  196.         jmp     thread_loop
  197.  
  198. next_rectangle:
  199.         inc     [update_framebuffer]
  200.         dec     [rectangles]
  201.         jnz     rectangle_loop
  202.         jmp     request_fbu
  203.  
  204.  
  205. setcolourmapentries:
  206.  
  207.         DEBUGF  1, "Server sent SetColourMapEntries message\n"
  208.  
  209.   @@:
  210.         lea     eax, [esi+5]
  211.         cmp     [datapointer], eax
  212.         jae     @f
  213.         call    read_data.more
  214.         jmp     @b
  215.   @@:
  216.  
  217.         inc     esi             ; padding
  218.  
  219.         xor     eax, eax
  220.         lodsw                   ; first color (just ignore for now)
  221.  
  222.         lodsw                   ; number of colors (use to find end of message)
  223.         xchg    al, ah
  224.         lea     eax, [eax*2+eax]
  225.         shl     eax, 1
  226.   @@:
  227.         push    eax
  228.         add     eax, esi
  229.         cmp     [datapointer], eax
  230.         jae     @f
  231.         call    read_data.more
  232.         pop     eax
  233.         jmp     @b
  234.   @@:
  235.         pop     eax
  236.  
  237.         add     esi, eax        ; Just skip it for now.
  238.         jmp     thread_loop
  239.  
  240.  
  241. bell:
  242.         mcall   55, 55, , , beep
  243.         jmp     thread_loop
  244.  
  245.  
  246. servercuttext:
  247.  
  248.         DEBUGF  1, "Server cut text\n"
  249.  
  250.   @@:
  251.         lea     eax, [esi+7]
  252.         cmp     [datapointer], eax
  253.         jae     @f
  254.         call    read_data.more
  255.         jmp     @b
  256.   @@:
  257.  
  258.         add     esi, 3
  259.         lodsd
  260.         bswap   eax
  261.         mov     ecx, eax
  262.  
  263.   @@:
  264.         lea     eax, [esi+ecx]
  265.         cmp     [datapointer], eax
  266.         jae     @f
  267.         call    read_data.more
  268.         jmp     @b
  269.   @@:
  270.  
  271.         ; TODO: paste text to clipboard
  272.  
  273.         DEBUGF  1, "%u bytes of text\n", ecx
  274.         add     esi, ecx
  275.         jmp     thread_loop
  276.  
  277.  
  278. read_data:
  279.         mov     [datapointer], receive_buffer
  280.         mov     esi, receive_buffer
  281.   .more:
  282.         push    ebx ecx edx esi edi
  283.         neg     esi
  284.         add     esi, receive_buffer + RECEIVE_BUFFER_SIZE
  285.         jz      .buffer_end_reached
  286.         xor     edi, edi
  287.         mcall   recv, [socketnum], [datapointer]
  288.         pop     edi esi edx ecx ebx
  289.         cmp     eax, -1
  290.         je      err_sock
  291.         test    eax, eax
  292.         jz      err_disconnected
  293.         add     [datapointer], eax
  294.         ret
  295.  
  296.   .buffer_end_reached:
  297.         ; Buffer is full, first needed data by program is pointed to by esi.
  298.         ; Move all usefull data to begin of buffer
  299.         cmp     esi, receive_buffer
  300.         je      err_proto
  301.         mov     ecx, [datapointer]
  302.         sub     ecx, esi
  303.         mov     edi, receive_buffer
  304.         rep movsb
  305.         mov     [datapointer], edi      ; new end of data
  306.         mov     esi, receive_buffer     ; new start of data
  307.         jmp     .more
  308.  
  309.  
  310.  
  311. wait_for_data:  ; FIXME: add timeout
  312.         mcall   recv, [socketnum], receive_buffer, 4096, 0      ; MSG_DONTWAIT
  313.         cmp     eax, -1
  314.         je      err_sock
  315.         test    eax, eax
  316.         jz      err_disconnected
  317.         ret
  318.  
  319.  
  320. err_disconnected:
  321.         mov     [status], STATUS_DISCONNECTED
  322.         inc     [update_gui]
  323.         mcall   -1
  324.  
  325. err_dns:
  326.         mov     [status], STATUS_DNS_ERR
  327.         inc     [update_gui]
  328.         mcall   -1
  329.  
  330. err_sock:
  331.         mov     [status], STATUS_SOCK_ERR
  332.         inc     [update_gui]
  333.         mcall   -1
  334.  
  335. err_connect:
  336.         mov     [status], STATUS_CONNECT_ERR
  337.         inc     [update_gui]
  338.         mcall   -1
  339.         ret
  340.  
  341. err_proto:
  342.         mov     [status], STATUS_PROTO_ERR
  343.         inc     [update_gui]
  344.         mcall   -1
  345.         ret
  346.  
  347. err_security:
  348.         mov     [status], STATUS_SECURITY_ERR
  349.         inc     [update_gui]
  350.         mcall   -1
  351.         ret
  352.