Subversion Repositories Kolibri OS

Rev

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

  1. /* TIMERS PaulCodeman */
  2. /**
  3.         void Timers::revise(void) -> This function revising all timers.
  4.         void Timers::getTime(void) -> This function updating current time for timers.
  5.         dword set(dword,dword,byte); -> This function seting timer for function Timers::revise.
  6.         dword clear(dword); -> This function clearning anything timer.
  7.         ---------
  8.         The functions setTimeout,setInterval,clearInterval,clearTimeout implementing functional JavaScript.
  9. */
  10. #define offsetSizeTimers 4*3+1
  11. #define defaultMaxTimers 1000
  12. :struct Timers
  13. {
  14.         dword time;
  15.     dword alloc;
  16.     dword count;
  17.     dword size;
  18.     void revise(void);
  19.     void getTime(void);
  20.     dword set(dword,dword,byte);
  21.     dword clear(dword);
  22. };
  23. void Timers::getTime(void)
  24. {
  25.         EAX = 26;
  26.         EBX = 9;
  27.         $int 0x40
  28.         time = EAX;
  29. }
  30. void Timers::revise(void)
  31. {
  32.         dword position = 0;
  33.         dword i = 0;
  34.         IF (!alloc) RETURN;
  35.         getTime();
  36.         i = count;
  37.         position = alloc;
  38.         WHILE(i)
  39.         {
  40.                 IF (DSDWORD[position])
  41.                 {
  42.                         IF (DSDWORD[position+4]<=time)
  43.                         {
  44.                                 $call DSDWORD[position];
  45.                                 IF (DSBYTE[position+12]) DSDWORD[position+4] = time+DSDWORD[position+8];
  46.                                 ELSE
  47.                                 {
  48.                                         DSDWORD[position] = 0;
  49.                                         count--;
  50.                                 }
  51.                         }
  52.                         i--;
  53.                 }
  54.                 position+=offsetSizeTimers;
  55.         }
  56. }
  57. dword Timers::set(dword function, newTime, byte repeat)
  58. {
  59.         dword position = 0;
  60.         dword i = 0;
  61.         IF (!alloc)
  62.         {
  63.                 size = defaultMaxTimers*offsetSizeTimers;
  64.                 alloc = malloc(size);
  65.         }
  66.         i = count;
  67.         position = alloc;
  68.         WHILE(i)
  69.         {
  70.                 IF (!DSDWORD[position]) BREAK;
  71.                 position+=offsetSizeTimers;
  72.                 i--;
  73.         }
  74.         count++;
  75.         getTime();
  76.         DSDWORD[position] = function;
  77.         DSDWORD[position+4] = time+newTime;
  78.         DSBYTE[position+8] = newTime;
  79.         DSBYTE[position+12] = repeat;
  80.         RETURN position;
  81. }
  82. dword Timers::clear(dword id)
  83. {
  84.         IF (!alloc) || (!id) || (!DSDWORD[id]) RETURN 0;
  85.         count--;
  86.         DSDWORD[id] = 0;
  87.         RETURN id;
  88. }
  89.  
  90. // Analogs JS Functions
  91. :Timers Time = {0};
  92. inline dword setTimeout(dword function, time)
  93. {
  94.         RETURN Time.set(function, time, 0);
  95. }
  96. inline dword setInterval(dword function, time)
  97. {
  98.         RETURN Time.set(function, time, 1);
  99. }
  100. inline dword clearTimeout(dword id)
  101. {
  102.         RETURN Time.clear(id);
  103. }
  104. inline dword clearInterval(dword id)
  105. {
  106.         RETURN Time.clear(id);
  107. }
  108.