///////////////////////////////////////////////////////////////////////////////
//                            Lisaac OS Library                              //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program is distributed in the hope that it will be useful,         //
//   but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//   GNU General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header

  + name    := TIMER;

  - copyright   := "2003-2005 Jérome Boutet, 2003-2007 Benoit Sonntag";

  - comment :="Windows - Timer management.";

  - version := 1;  
    
Section Inherit  
  
  + parent_input:Expanded INPUT;
  
Section Private
  
  - timer_count:UINTEGER_32;
  
  + buffer_event:FAST_ARRAY[EVENT_TIMER];
  
  - p_beg:UINTEGER_8;  // Pointer on the buffer (beginning)
  
  - p_end:UINTEGER_8;  // Pointer on the buffer (end)
    
Section Public  
  
  - timer_interrupt <-  
  ( + tmp:UINTEGER_8;     
    
    timer_count := timer_count + 1;
    
    tmp:=(p_end+1)&003h;
    buffer_event.item p_end.make timer_count; 
    (((tmp+2)&3)!=p_beg).if {
      p_end:=tmp;
    };    
  );
  
Section Public

  - make <-
  ( + new_event:EVENT_TIMER;
    
    is_actif := TRUE;
    //
    // Software configuration.
    //
    buffer_event := FAST_ARRAY[EVENT_TIMER].create 4;
    0.to 3 do { j:INTEGER;
      new_event := EVENT_TIMER.clone;
      buffer_event.put new_event to j;
      (j != 0).if {
	new_event.set_prev (buffer_event.item (j-1));
      };
    };
    buffer_event.first.set_prev new_event;
    
    //hdle := timer_interrupt;    
        
    `SetTimer(hwnd_glob, 0, 50, NULL)`;
  );

  - acknowledge <-
  (
    p_beg := (p_beg+1) & 03h;
  );

  - get_event <-
  ( + p:INTEGER;
    + evt:EVENT_TIMER;
    
    p := p_beg;
    { p != p_end }.while_do {
      evt := buffer_event.item p;
      (list_client.lower).to (list_client.upper) do { j:INTEGER;
	list_client.item j.receive (buffer_event.item p);
      };      
      p := (p + 1) & 03h;
    };    
  );
