Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa      ;;
  5. ;; Distributed under terms of the GNU General Public License    ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 593 $
  9.  
  10.  
  11. align 4
  12. init_events:
  13.            stdcall kernel_alloc, 512*EVENT_SIZE
  14.            mov [events], eax
  15.            xor eax, eax
  16.            mov [event_uid], eax
  17.            not eax
  18.            mov edi, event_map
  19.            mov [event_start], edi
  20.            mov ecx, 64/4
  21.            cld
  22.            rep stosd
  23.            mov [event_end], edi
  24.            ret
  25.  
  26. align 4
  27. proc alloc_event
  28.  
  29.            pushfd
  30.            cli
  31.            mov ebx, [event_start]
  32.            mov ecx, [event_end]
  33. .l1:
  34.            bsf eax,[ebx]
  35.            jnz .found
  36.            add ebx,4
  37.            cmp ebx, ecx
  38.            jb .l1
  39.            popfd
  40.            xor eax,eax
  41.            ret
  42. .found:
  43.            btr [ebx], eax
  44.            mov [event_start],ebx
  45.            inc [event_uid]
  46.  
  47.            sub ebx, event_map
  48.            lea eax,[eax+ebx*8]
  49.  
  50.            lea ebx, [eax+eax*4]
  51.            shl eax,5
  52.            lea eax,[eax+ebx*4]   ;eax*=52 (EVENT_SIZE)
  53.            add eax, [events]
  54.            mov ebx, [event_uid]
  55.            popfd
  56.            ret
  57. endp
  58.  
  59. align 4
  60. free_event:
  61.            sub eax, [events]
  62.            mov ecx, EVENT_SIZE
  63.            mov ebx, event_map
  64.            cdq
  65.            div ecx
  66.  
  67.            pushfd
  68.            cli
  69.            bts [ebx], eax
  70.            shr eax, 3
  71.            and eax, not 3
  72.            add eax, ebx
  73.            cmp [event_start], eax
  74.            ja @f
  75.            popfd
  76.            ret
  77. @@:
  78.            mov [event_start], eax
  79.            popfd
  80.            ret
  81.  
  82. EVENT_WATCHED    equ 0x10000000
  83. EVENT_SIGNALED   equ 0x20000000
  84. MANUAL_RESET     equ 0x40000000
  85. MANUAL_DESTROY   equ 0x80000000
  86.  
  87.  
  88. ; param
  89. ;  eax= event data
  90. ;  ebx= flags
  91. ;
  92. ; retval
  93. ;  eax= event
  94. ;  edx= id
  95.  
  96. create_event:
  97.            .flags  equ  esp+4
  98.            .data   equ  esp
  99.  
  100.            push ebx
  101.            push eax
  102.  
  103.            call alloc_event
  104.            test eax, eax
  105.            jz .fail
  106.  
  107.            mov [eax+APPOBJ.magic], 'EVNT'
  108.            mov [eax+APPOBJ.destroy], destroy_event.internal
  109.            mov [eax+EVENT.id], ebx
  110.  
  111.            mov ebx, [CURRENT_TASK]
  112.            shl ebx, 5
  113.            mov ebx, [CURRENT_TASK+ebx+4]
  114.            mov [eax+APPOBJ.pid], ebx
  115.            mov edx, [.flags]
  116.            mov [eax+EVENT.state], edx
  117.  
  118.            mov esi, [.data]
  119.            test esi, esi
  120.            jz @F
  121.            lea edi, [eax+EVENT.code]
  122.            mov ecx, 6
  123.            cld
  124.            rep movsd
  125. @@:
  126.            mov ecx, [current_slot]
  127.            add ecx, APP_OBJ_OFFSET
  128.  
  129.            pushfd
  130.            cli
  131.            mov edx, [ecx+APPOBJ.fd]
  132.            mov [eax+APPOBJ.fd], edx
  133.            mov [eax+APPOBJ.bk], ecx
  134.            mov [ecx+APPOBJ.fd], eax
  135.            mov [edx+APPOBJ.bk], eax
  136.            popfd
  137.            mov edx, [eax+EVENT.id]
  138. .fail:
  139.            add esp, 8
  140.            ret
  141.  
  142. restore .flags
  143. restore .data
  144.  
  145. ; param
  146. ;  eax= event
  147. ;  ebx= id
  148.  
  149. destroy_event:
  150.  
  151.            cmp [eax+APPOBJ.magic], 'EVNT'
  152.            jne .fail
  153.            cmp [eax+EVENT.id], ebx
  154.            jne .fail
  155. .internal:
  156.            mov ebx, [eax+APPOBJ.fd]
  157.            mov ecx, [eax+APPOBJ.bk]
  158.            mov [ebx+APPOBJ.bk], ecx
  159.            mov [ecx+APPOBJ.fd], ebx
  160. .force:
  161.            xor edx, edx             ;clear common header
  162.            mov [eax], edx
  163.            mov [eax+4], edx
  164.            mov [eax+8], edx
  165.            mov [eax+12], edx
  166.            mov [eax+16], edx
  167.  
  168.            call free_event          ;release object memory
  169. .fail:
  170.            ret
  171.  
  172. align 4
  173. proc send_event stdcall pid:dword, event:dword
  174.            locals
  175.              slot     dd ?
  176.            endl
  177.  
  178.            mov eax, [pid]
  179.            call pid_to_slot
  180.            test eax, eax
  181.            jz .fail
  182.  
  183.            shl eax, 8
  184.            cmp [SLOT_BASE+eax+APPDATA.ev_count], 32
  185.            ja .fail
  186.  
  187.            mov [slot], eax
  188.  
  189.            call alloc_event
  190.            test eax, eax
  191.            jz .fail
  192.  
  193.            lea edi, [eax+EVENT.code]
  194.            mov ecx, 6
  195.            mov esi, [event]
  196.            cld
  197.            rep movsd
  198.  
  199.            mov ecx, [slot]
  200.            add ecx, SLOT_BASE+APP_EV_OFFSET
  201.  
  202.            mov [eax+APPOBJ.magic], 'EVNT'
  203.            mov [eax+APPOBJ.destroy], destroy_event
  204.            mov ebx, [pid]
  205.            mov [eax+APPOBJ.pid], ebx
  206.            mov [eax+EVENT.state], EVENT_SIGNALED
  207.  
  208.            pushfd
  209.            cli                         ;insert event into
  210.            mov edx, [ecx+APPOBJ.fd]    ;events list
  211.            mov [eax+APPOBJ.fd], edx    ;and set events flag
  212.            mov [eax+APPOBJ.bk], ecx
  213.            mov [ecx+APPOBJ.fd], eax
  214.            mov [edx+APPOBJ.bk], eax
  215.            inc [ecx+APPDATA.ev_count-APP_EV_OFFSET]
  216.            or  [ecx+APPDATA.event_mask-APP_EV_OFFSET], EVENT_EXTENDED
  217.            popfd
  218. .fail:
  219.            ret
  220. endp
  221.  
  222. ; timeout ignored
  223.  
  224. align 4
  225. proc get_event_ex stdcall, p_ev:dword, timeout:dword
  226.  
  227. .wait:
  228.            mov edx,[current_slot]
  229. ;           cmp [SLOT_BASE+edx+APPDATA.ev_count], 0
  230. ;           je .switch
  231.  
  232.            add edx, APP_EV_OFFSET
  233.  
  234.            mov eax, [edx+APPOBJ.fd]
  235.            cmp eax, edx
  236.            je .switch
  237.  
  238.            lea esi, [eax+EVENT.code]
  239.            mov edi, [p_ev]                ;copy event data
  240.            mov ecx, 6
  241.            cld
  242.            rep movsd
  243.  
  244.            and dword [edi-24], 0xFF00FFFF ;clear priority field
  245.                                          ;
  246.            test [eax+EVENT.state], MANUAL_RESET
  247.            jnz .done
  248.  
  249.            pushfd
  250.            cli                         ;remove event from events
  251.            mov ebx, [eax+APPOBJ.fd]    ;list (reset event)
  252.            mov ecx, [eax+APPOBJ.bk]    ;and clear events flag
  253.            mov [ebx+APPOBJ.bk], ecx    ;if no active events
  254.            mov [ecx+APPOBJ.fd], ebx
  255.  
  256.            and [eax+EVENT.state], not (EVENT_SIGNALED+EVENT_WATCHED)
  257.  
  258.            dec [edx+APPDATA.ev_count-APP_EV_OFFSET]
  259.            jnz @F
  260.            and [edx+APPDATA.event_mask-APP_EV_OFFSET], not EVENT_EXTENDED
  261. @@:
  262.            popfd
  263.  
  264.            test [eax+EVENT.state], MANUAL_DESTROY
  265.            jz .destroy
  266.  
  267.            add edx, (APP_OBJ_OFFSET-APP_EV_OFFSET)
  268.  
  269.            pushfd
  270.            cli
  271.            mov ebx, [edx+APPOBJ.fd]  ;insert event into
  272.            mov [eax+APPOBJ.fd], ebx  ;objects list
  273.            mov [eax+APPOBJ.bk], edx
  274.            mov [edx+APPOBJ.fd], eax
  275.            mov [ebx+APPOBJ.bk], eax
  276.            popfd
  277. .done:
  278.            ret
  279.  
  280. .destroy:
  281.            call destroy_event.force
  282.            ret
  283. .switch:
  284.            mov eax, [TASK_BASE]
  285.            mov [eax+TASKDATA.state], byte 5
  286.            call change_task
  287.            jmp .wait
  288. endp
  289.  
  290. ; param
  291. ;  eax= event
  292. ;  ebx= id
  293.  
  294. align 4
  295. wait_event:
  296.            .event equ esp
  297.            push eax
  298. .wait:
  299.            cmp [eax+APPOBJ.magic], 'EVNT'
  300.            jne .done
  301.            cmp [eax+EVENT.id], ebx
  302.            jne .done
  303.  
  304.            test [eax+EVENT.state], EVENT_SIGNALED
  305.            jz .switch
  306.  
  307.            test [eax+EVENT.state], MANUAL_RESET
  308.            jnz .done
  309.  
  310.            mov edx,[current_slot]
  311.  
  312.            pushfd
  313.            cli                         ;remove event from events
  314.            mov ebx, [eax+APPOBJ.fd]    ;list (reset event)
  315.            mov ecx, [eax+APPOBJ.bk]    ;and clear events flag
  316.            mov [ebx+APPOBJ.bk], ecx    ;if no active events
  317.            mov [ecx+APPOBJ.fd], ebx
  318.            dec [edx+APPDATA.ev_count]
  319.            jnz @F
  320.            and [edx+APPDATA.event_mask], not EVENT_EXTENDED
  321. @@:
  322.            and [eax+EVENT.state], not (EVENT_SIGNALED+EVENT_WATCHED)
  323.            popfd
  324.  
  325.            test [eax+EVENT.state], MANUAL_DESTROY
  326.            jz .destroy
  327.  
  328.            add edx, APP_OBJ_OFFSET
  329.  
  330.            pushfd
  331.            cli
  332.            mov ecx, [edx+APPOBJ.fd]  ;insert event into
  333.            mov [eax+APPOBJ.fd], ecx  ;objects list
  334.            mov [eax+APPOBJ.bk], edx
  335.            mov [edx+APPOBJ.fd], eax
  336.            mov [ecx+APPOBJ.bk], eax
  337.            popfd
  338. .done:
  339.            add esp, 4
  340.            ret
  341. .destroy:
  342.            call destroy_event.force
  343.            add esp, 4
  344.            ret
  345. .switch:
  346.            or [eax+EVENT.state], EVENT_WATCHED
  347.            mov eax, [TASK_BASE]
  348.            mov [eax+TASKDATA.state], byte 5
  349.            call change_task
  350.            mov eax, [.event]
  351.            jmp .wait
  352. restore .event
  353.  
  354. ; param
  355. ;  eax= event
  356. ;  ebx= id
  357. ;  ecx= flags
  358. ;  edx= event data
  359.  
  360. raise_event:
  361.            .event equ esp
  362.            push eax
  363.  
  364.            cmp [eax+APPOBJ.magic], 'EVNT'
  365.            jne .fail
  366.            cmp [eax+EVENT.id], ebx
  367.            jne .fail
  368.  
  369.            mov eax, [eax+APPOBJ.pid]
  370.            call pid_to_slot
  371.            test eax, eax
  372.            jz .fail
  373.  
  374.            mov esi, edx
  375.            test esi, esi
  376.            mov edx, [.event]
  377.            jz @F
  378.  
  379.            push ecx
  380.            lea edi, [edx+EVENT.code]
  381.            mov ecx, 6
  382.            cld
  383.            rep movsd
  384.            pop ecx
  385. @@:
  386.            test [edx+EVENT.state], EVENT_SIGNALED
  387.            jnz .done
  388.  
  389.            test ecx, EVENT_WATCHED
  390.            jz @F
  391.            test [edx+EVENT.state], EVENT_WATCHED
  392.            jz .done
  393. @@:
  394.            shl eax, 8
  395.            add eax, SLOT_BASE+APP_EV_OFFSET
  396.  
  397.            pushfd
  398.            cli
  399.            mov ebx, [edx+APPOBJ.fd]
  400.            mov ecx, [edx+APPOBJ.bk]
  401.            mov [ebx+APPOBJ.bk], ecx
  402.            mov [ecx+APPOBJ.fd], ebx
  403.  
  404.            mov ecx, [eax+APPOBJ.fd]
  405.            mov [edx+APPOBJ.fd], ecx
  406.            mov [edx+APPOBJ.bk], eax
  407.            mov [eax+APPOBJ.fd], edx
  408.            mov [ecx+APPOBJ.bk], edx
  409.            or [edx+EVENT.state], EVENT_SIGNALED
  410.  
  411.            inc [eax+APPDATA.ev_count-APP_EV_OFFSET]
  412.            or  [eax+APPDATA.event_mask-APP_EV_OFFSET], EVENT_EXTENDED
  413.            popfd
  414. .fail:
  415. .done:
  416.            add esp, 4
  417.            ret
  418. restore .event
  419.  
  420. ; param
  421. ;  eax= event
  422. ;  ebx= id
  423. align 4
  424. clear_event:
  425.            .event equ esp
  426.            push eax
  427.  
  428.            cmp [eax+APPOBJ.magic], 'EVNT'
  429.            jne .fail
  430.            cmp [eax+EVENT.id], ebx
  431.            jne .fail
  432.  
  433.            mov eax, [eax+APPOBJ.pid]
  434.            call pid_to_slot
  435.            test eax, eax
  436.            jz .fail
  437.  
  438.            shl eax, 8
  439.            add eax, SLOT_BASE+APP_EV_OFFSET
  440.            mov edx, [.event]
  441.            pushfd
  442.            cli                         ;remove event from events
  443.            mov ebx, [edx+APPOBJ.fd]    ;list (reset event)
  444.            mov ecx, [edx+APPOBJ.bk]    ;and clear events flag
  445.            mov [ebx+APPOBJ.bk], ecx    ;if no active events
  446.            mov [ecx+APPOBJ.fd], ebx
  447.  
  448.            and [edx+EVENT.state], not (EVENT_SIGNALED+EVENT_WATCHED)
  449.  
  450.            dec [eax+APPDATA.ev_count-APP_EV_OFFSET]
  451.            jnz @F
  452.            and [eax+APPDATA.event_mask-APP_EV_OFFSET], not EVENT_EXTENDED
  453. @@:
  454.            add eax, (APP_OBJ_OFFSET-APP_EV_OFFSET)
  455.  
  456.            mov ecx, [eax+APPOBJ.fd]  ;insert event into
  457.            mov [edx+APPOBJ.fd], ecx  ;objects list
  458.            mov [edx+APPOBJ.bk], eax
  459.            mov [eax+APPOBJ.fd], edx
  460.            mov [ecx+APPOBJ.bk], edx
  461.            popfd
  462. .fail:
  463. .done:
  464.            add esp, 4
  465.            ret
  466. restore .event
  467.  
  468. sys_getevent:
  469.  
  470.      call   get_event_for_app
  471.      mov    [esp+36],eax
  472.      ret
  473.  
  474. sys_waitforevent:
  475.         or      eax, 0xFFFFFFFF         ; infinite timeout
  476.         jmp     @f
  477.  
  478. sys_wait_event_timeout:
  479.         add     eax, [timer_ticks]
  480. @@:
  481.         mov     ebx, [current_slot]
  482.         mov     [ebx + APPDATA.wait_timeout], eax
  483.         call    get_event_for_app
  484.         test    eax, eax
  485.         jnz     eventoccur
  486.  
  487.         mov     eax, [TASK_BASE]
  488.         mov     [eax+TASKDATA.state], byte 5
  489.         call    change_task
  490.  
  491.         mov     eax, [event_sched]
  492. eventoccur:
  493.         mov     [esp+36], eax
  494.         ret
  495.  
  496. get_event_for_app:
  497.  
  498.      pushad
  499.  
  500.      mov   edi,[TASK_BASE]              ; WINDOW REDRAW
  501.      test  [edi+TASKDATA.event_mask], 1
  502.      jz    no_eventoccur1
  503.      ;mov   edi,[TASK_BASE]
  504.      cmp   [edi-twdw+WDATA.fl_redraw],byte 0
  505.      je    no_eventoccur1
  506.      popad
  507.      mov   eax,1
  508.      ret
  509.    no_eventoccur1:
  510.  
  511.      ;mov   edi,[TASK_BASE]              ; KEY IN BUFFER
  512.      test  [edi+TASKDATA.event_mask],dword 2
  513.      jz    no_eventoccur2
  514.      mov   ecx, [CURRENT_TASK]
  515.      movzx edx,word [WIN_STACK+ecx*2]
  516.      mov   eax, [TASK_COUNT]
  517.      cmp   eax,edx
  518.      jne   no_eventoccur2x
  519.      cmp   [KEY_COUNT],byte 0
  520.      je    no_eventoccur2x
  521.    eventoccur2:
  522.      popad
  523.      mov   eax,2
  524.      ret
  525.    no_eventoccur2x:
  526.         mov     eax, hotkey_buffer
  527. @@:
  528.         cmp     [eax], ecx
  529.         jz      eventoccur2
  530.         add     eax, 8
  531.         cmp     eax, hotkey_buffer+120*8
  532.         jb      @b
  533.    no_eventoccur2:
  534.  
  535.      ;mov   edi,[TASK_BASE]              ; BUTTON IN BUFFER
  536.      test  [edi+TASKDATA.event_mask],dword 4
  537.      jz    no_eventoccur3
  538.      cmp   [BTN_COUNT],byte 0
  539.      je    no_eventoccur3
  540.      mov   ecx, [CURRENT_TASK]
  541.      movzx edx, word [WIN_STACK+ecx*2]
  542.      mov   eax, [TASK_COUNT]
  543.      cmp   eax,edx
  544.      jnz   no_eventoccur3
  545.      popad
  546.      mov   eax,[BTN_BUFF]
  547.      cmp   eax,65535
  548.      je    no_event_1
  549.      mov   eax,3
  550.      ret
  551.  
  552.     no_event_1:
  553.      mov   [window_minimize],1
  554.      mov   [BTN_COUNT],byte 0
  555.      xor   eax, eax
  556.      ret
  557.  
  558. no_eventoccur3:
  559.  
  560.      ;mov   edi,[TASK_BASE]              ; mouse event
  561.      mov eax, [CURRENT_TASK]
  562.      shl eax, 8
  563.      add eax, SLOT_BASE
  564.      test  [edi+TASKDATA.event_mask],dword 00100000b
  565.      jz    no_mouse_event
  566.  
  567.      test  [eax+APPDATA.event_mask],dword 00100000b
  568.      jz    no_mouse_event
  569.      and   [eax+APPDATA.event_mask],dword (not 00100000b)
  570.      popad
  571.      mov   eax,6
  572.      ret
  573. no_mouse_event:
  574.  
  575.      ;mov   edi,[TASK_BASE]              ; DESKTOP BACKGROUND REDRAW
  576.      test  [edi+TASKDATA.event_mask], 16
  577.      jz    no_eventoccur5
  578. ;     cmp   [REDRAW_BACKGROUND],byte 2
  579. ;     jnz   no_eventoccur5
  580.      test  [eax+APPDATA.event_mask], 16
  581.      jz    no_eventoccur5
  582.      and   [eax+APPDATA.event_mask], not 16
  583.      popad
  584.      mov   eax,5
  585.      ret
  586. no_eventoccur5:
  587.  
  588.      ;mov   edi,[TASK_BASE]              ; IPC
  589.      test  [edi+TASKDATA.event_mask],dword 01000000b
  590.      jz    no_ipc
  591.      test  [eax+APPDATA.event_mask],dword 01000000b
  592.      jz    no_ipc
  593.      and   [eax+APPDATA.event_mask],dword 0xffffffff-01000000b
  594.      popad
  595.      mov   eax,7
  596.      ret
  597. no_ipc:
  598.  
  599.      ;mov   edi,[TASK_BASE]              ; STACK
  600.      test  [edi+TASKDATA.event_mask],dword 10000000b
  601.      jz    no_stack_event
  602.      test  [eax+APPDATA.event_mask],dword 10000000b
  603.      jz    no_stack_event
  604.      and   [eax+APPDATA.event_mask],dword 0xffffffff-10000000b
  605.      popad
  606.      mov   eax,8
  607.      ret
  608. no_stack_event:
  609.  
  610.      test  byte [edi+TASKDATA.event_mask+1], 1          ; DEBUG
  611.      jz    .test_IRQ
  612.      test  byte [eax+APPDATA.event_mask+1], byte 1
  613.      jz    .test_IRQ
  614.      and   byte [eax+APPDATA.event_mask+1], not 1
  615.      popad
  616.      mov   eax, 9
  617.      ret
  618.  
  619. ;.test_ext:
  620. ;     mov   eax, [CURRENT_TASK]
  621. ;     shl   eax, 8
  622. ;     test  dword [eax+SLOT_BASE+APPDATA.event_mask], EVENT_EXTENDED
  623. ;     jz .test_IRQ
  624. ;     popad
  625. ;     mov eax, 10
  626. ;     ret
  627.  
  628. .test_IRQ:
  629.      cmp   dword [edi+TASKDATA.event_mask], 0xFFFF
  630.      jbe   no_events
  631.  
  632.      mov   esi,IRQ_SAVE              ; IRQ'S AND DATA
  633.      mov   ebx,0x00010000
  634.      xor   ecx, ecx
  635.    irq_event_test:
  636.      mov   edi,[TASK_BASE]
  637.      test  [edi+TASKDATA.event_mask],ebx
  638.      jz    no_irq_event
  639.      mov   edi,ecx
  640.      shl   edi,2
  641.      add   edi,irq_owner
  642.      mov   edx,[edi]
  643.      mov   eax,[TASK_BASE]
  644.      mov   eax,[eax+TASKDATA.pid]
  645.      cmp   edx,eax
  646.      jne   no_irq_event
  647.      cmp   [esi],dword 0
  648.      jz    no_irq_event
  649.      mov   eax,ecx
  650.      add   eax,16
  651.      mov   [esp+28],eax
  652.      popad
  653.      ret
  654.     no_irq_event:
  655.      add   esi,0x1000
  656.      shl   ebx,1
  657.      inc   ecx
  658.      cmp   ecx,16
  659.      jb    irq_event_test
  660.  
  661.    no_events:
  662.      popad
  663.      xor   eax, eax
  664.      ret
  665.  
  666.  
  667.  
  668.