Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. ; clipboard client module by barsuk.
  3. ; version 0.2
  4. ; see readme.txt
  5.  
  6. macro CLIP_BUFFER buf_name, buf_size
  7. {
  8. #buf_name:
  9. .size   dd      buf_size
  10. .sys1   dd      ?
  11. .sys2   dd      ?
  12. .data   db      buf_size dup(?)
  13. }
  14.  
  15. _ipc_send:
  16. ; internally used routine
  17. ; esi->buffer, edx = byte count
  18.  
  19.         push    ebx
  20.         push    ecx
  21.         push    ebp
  22.  
  23.         mov     ebp, ATTEMPT    ; number of attempts to send
  24.         xchg    edx, esi
  25. .send_again:
  26.         mov     eax, 5
  27.         mov     ebx, SEND_DELAY                
  28.         int     0x40                   
  29.  
  30.         mov     eax, 60
  31.         mov     ebx, 2
  32.         mov     ecx, [clipboard_init.clipserv_pid]
  33.         int     0x40
  34. print "send result eax"
  35. pregs
  36.  
  37.         or      eax, eax
  38.         jz      .ok
  39.         cmp     eax, 2
  40.         jz      .wait
  41.         jmp     .err
  42. .wait:
  43.         dec     ebp
  44.         jz      .err
  45.         jmp     .send_again
  46. .ok:
  47.         mov     eax, 1
  48.         jmp     .exit
  49. .err:
  50.         xor     eax, eax
  51. .exit:
  52.         pop     ebp
  53.         pop     ecx
  54.         pop     ebx
  55.         xchg    edx, esi
  56.         ret
  57.  
  58. _ipc_recv:
  59. ; used internally.
  60. ; edx = default event mask of thread(!!!)
  61. ; esi -> CLIP_buffer
  62. ; result: eax == 1 - suc, 0 - err
  63.  
  64.         push    ebx
  65.         push    ecx
  66.  
  67.         mov     dword [esi + 4], 0      ; unlock buffer
  68.         mov     dword [esi + 8], 0      ;
  69.  
  70.         push    edx
  71.         mov     eax, 60                 ; register buffer
  72.         mov     ebx, 1
  73.         lea     ecx, [esi + 4]
  74.         mov     edx, [esi]
  75.         add     edx, 8
  76. ;print "register buffer"
  77. ;pregs
  78.         int     0x40
  79.         pop     edx
  80.  
  81. .wait:
  82.         mov     eax, 40                 ; listen for IPC
  83.         mov     ebx, 01000000b
  84.         int     0x40
  85.  
  86.         mov     eax, 23                 ; wait for IPC event
  87.         mov     ebx, RECV_DELAY         ; not too long
  88.         int     0x40
  89.         cmp     eax, 7
  90.         jnz     .err
  91.  
  92.         mov     eax, [clipboard_init.clipserv_pid]      ; not message from
  93.         cmp     eax, [esi + 4]                          ; daemon, so ignore
  94.         jnz     .wait
  95.  
  96.         mov     dword [esi + 4], 1              ; lock so it will not be spoiled
  97.  
  98. print "cli recv"
  99. dph1    [esi]
  100. dph1    [esi+4]
  101. dph1    [esi+8]
  102. dph1    [esi+12]
  103. dph1    [esi+16]
  104. dph1    [esi+20]
  105. print ""
  106.  
  107.         mov     eax, 40                 ; restore event mask
  108.         mov     ebx, edx
  109.         int     0x40
  110.         mov     eax, 1
  111.         jmp     .exit
  112. .err:
  113.         xor     eax, eax
  114. .exit:
  115.         pop     ecx
  116.         pop     ebx
  117.         ret
  118.  
  119.  
  120. clipboard_init:
  121. ; action: define IPC buffer and find clipserv process
  122. ; output: eax == 0 - error, eax == 1 - success
  123.  
  124.         push    ebx
  125.         push    ecx
  126.         push    edx
  127.  
  128. ;       mov     eax, 60                 ; define buffer for IPC
  129. ;       mov     ebx, 1
  130. ;       mov     ecx, .IPC_real_buffer
  131. ;       mov     edx, IPC_buffer_size
  132. ;       int     0x40
  133.  
  134. ;print "buffer defined"
  135.  
  136. ;       mov     [.IPC_buffer], ecx
  137.  
  138.         push    ebp
  139.         mov     ebp, 1    
  140. .next_process:
  141.         mov     eax, 9
  142.         mov     ebx, .process_info
  143.         mov     ecx, ebp
  144.         int     0x40
  145.         mov     ecx, eax    
  146.         mov     ebx, .process_info + 10
  147.         mov     eax, [ebx]
  148.         cmp     eax, '@CLI'
  149.         jnz     .differ
  150.         mov     al, [ebx + 4]
  151.         cmp     al, 'P'
  152.         jnz     .differ
  153.         jmp     .similar
  154.  
  155. ;       mov     edx, .clipserv_name
  156. ;.compare:          
  157. ;       mov     al, [edx]
  158. ;       cmp     al, 0
  159. ;       jz      .similar
  160. ;       cmp     al, [ebx]
  161. ;       jnz     .differ
  162. ;       inc     edx    
  163. ;       inc     ebx
  164. ;       jmp     .compare
  165. .differ:
  166.         inc     ebp
  167.         cmp     ebp, ecx
  168.         jae     .err            ; process not found
  169.         jmp     .next_process
  170. .similar:
  171. ;print "found server"
  172.         mov     eax, dword [.process_info + 30]
  173.         mov     [.clipserv_pid], eax
  174.  
  175.         mov     eax, 1
  176.         jmp     .exit
  177. .err:
  178. ;print "no server"
  179.         xor     eax, eax
  180. .exit:
  181.         pop     ebp
  182.         pop     edx
  183.         pop     ecx
  184.         pop     ebx
  185.         ret
  186. .clipserv_pid   dd      0
  187. .process_info   db      1024 dup(0)
  188. ;;.clipserv_name        db      '@clip',0
  189. ;.IPC_buffer    dd      .IPC_real_buffer        ; sorry
  190. ;.IPC_real_buffer       db      IPC_buffer_size dup(0)
  191.  
  192. clipboard_write:
  193. ; action: put to clipboard data in format (ax) from buffer
  194. ; input: esi -> CLIP_buffer, ax = format id
  195. ; output: eax == 0 - error, eax == 1 - success
  196.  
  197.         push    ecx
  198.         push    edx
  199.         push    edi
  200.  
  201.         mov     edx, [esi]              ; CLIP_buffer.size
  202.  
  203.         push    edx
  204.         push    esi
  205.         mov     [.msg_set_size + 2], ax
  206.         mov     word [esi + 4 + 2], ax  ; format id to low word of sys1
  207.         mov     dword [.msg_set_size + 8], edx
  208.         mov     esi, .msg_set_size
  209.         mov     edx, 12
  210.         call    _ipc_send
  211.         pop     esi
  212.         pop     edx
  213.         or      eax, eax
  214.         jz      .err
  215.  
  216.         mov     word [esi + 4], 2       ; COMMAND_SET to high word of sys1
  217.         add     esi, 4                  ; esi->buffer.sys1
  218.         add     edx, 8
  219.         call    _ipc_send
  220.         sub     esi, 4
  221.         or      eax, eax
  222.         jz      .err
  223.  
  224.         mov     eax, 1
  225. print "write success"
  226.         jmp     .exit
  227. .err:
  228. print "write failed"
  229.         xor     eax, eax
  230. .exit:
  231.         pop     edi
  232.         pop     edx
  233.         pop     ecx
  234.         ret    
  235.  
  236. .msg_set_size   dw      1
  237.                 dw      1
  238.                 dd      0
  239.                 dd      0
  240.  
  241. clipboard_read:
  242. ; esi -> CLIP_buffer, ax = format id
  243. ; edx - ¬ áª  ᮡë⨩ ¯® 㬮«ç ­¨î
  244. ; result: eax = 1 - success, 0 - general failure,
  245. ; -1 - buffer too small
  246. ; edx = size of data
  247.  
  248.         push    ebx
  249.         push    ecx
  250.         push    ebp
  251.         push    esi
  252.         push    edi
  253.  
  254.         mov     ebp, edx
  255.  
  256.         ; get size
  257.  
  258.         mov     edi, esi
  259.         mov     esi, .msg_get_size
  260.         mov     word [esi], 3
  261.         mov     [esi + 2], ax
  262.         mov     [.msg_get_buf + 2], ax
  263.         mov     edx, 8
  264.         call    _ipc_send
  265.         or      eax, eax       
  266.         jz      .err
  267.  
  268.         ;;mov   edx, DEFAULT_MASK
  269.         mov     edx, ebp
  270.         mov     esi, .buf_for_size
  271.         mov     dword [esi], 4
  272.         call    _ipc_recv
  273.         or      eax, eax
  274.         jz      .err
  275.  
  276.         mov     eax, [esi + 12]         ; space we need
  277.         mov     edx, [edi]              ; space we have
  278. print "read size eax"
  279. pregs
  280.         mov     ecx, eax
  281.         cmp     eax, edx
  282.         ja      .size
  283.         or      eax, eax
  284.         jz      .err
  285.         mov     ebx, eax
  286.  
  287.         mov     esi, .msg_get_buf
  288.         mov     word [esi], 4
  289.         mov     edx, 8
  290.         call    _ipc_send
  291.         or      eax, eax
  292.         jz      .err
  293.  
  294. ;print "send fuck"
  295.  
  296.         mov     edx, ebp
  297.         mov     esi, edi
  298.         call    _ipc_recv
  299.         or      eax, eax
  300.         jz      .err
  301.  
  302. print "read get data"
  303.  
  304.         mov     edx, ebx
  305.         mov     eax, 1
  306. print "read ok"
  307.         jmp     .exit           ; i'm an idiot. Never will I code at night again
  308.                                 ; i put jz instead of jmp.
  309.  
  310. .size:
  311. print "buffer small"
  312.         mov     edx, eax
  313.         or      eax, -1
  314.         jmp     .exit
  315. .err:
  316. print "read error"
  317.         xor     eax, eax
  318. .exit:
  319.         pop     edi
  320.         pop     esi
  321.         pop     ebp
  322.         pop     ecx
  323.         pop     ebx
  324.         ret
  325.  
  326. .msg_get_size   dw      0
  327.                 dw      1
  328.                 dd      0
  329. .msg_get_buf    dw      0
  330.                 dw      1
  331.                 dd      0
  332. .buf_for_size   dd      0
  333.                 dd      0
  334.                 dd      0
  335.                 dd      0
  336.  
  337.  
  338. clipboard_delete:
  339. ; ax = format id to delete
  340. ; result: eax = 0 error, = 1 ok
  341.  
  342.         push    edx
  343.         push    esi
  344.  
  345.         mov     esi, .msg_del
  346.         mov     word [esi], 5
  347.         mov     [esi + 2], ax
  348.         mov     edx, 8
  349.  
  350.         call    _ipc_send
  351.  
  352.         pop     esi
  353.         pop     edx
  354.  
  355.         ret
  356. .msg_del        dw      0
  357.                 dw      1
  358.                 dd      0
  359.  
  360.