///////////////////////////////////////////////////////////////////////////////
//                            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     :="Unix - Timer management.";

  - version := 1;  

  - date    :="2003/04";
  
  - external := 
`
#include <signal.h>
#define __BEGIN_INTERRUPT__
#define __END_INTERRUPT__
XEvent timer_ev;
`;
  
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 Interrupt  
  
  - 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;
    };
    // ((timer_count % 20)=0).if {
    //   CLOCK.rtc;
    // };
    
    (`is_sleep`:INTEGER = 1).if {
      `timer_ev.type           = ClientMessage`;
      `timer_ev.xclient.format = 32`;
      (`XSendEvent(display,window,0,ClientMessage,&timer_ev)`:INTEGER != 0).if {
	`XFlush(display)`;
      };
    };
    `ualarm(50000,0)`;    
  );
  
Section Public

  - make <-
  ( + hdle:POINTER;
    + 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;
    `signal(SIGALRM,@hdle)`;
    `ualarm(500000,0)`;
  );

  - 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;
    };    
  );


/* 
//Other solution :
void catcher( int sig ) {

    time_count ++;
}

int main( int argc, char *argv[] ) {

  int old_time;

    struct itimerval value;

    signal(SIGALRM,catcher);

    value.it_interval.tv_sec = 1;   
    value.it_interval.tv_usec = 0;  
    value.it_value.tv_sec = 1;      
    value.it_value.tv_usec = 0;     

    setitimer(ITIMER_REAL, &value, NULL); 

    while (1) {
      if (old_time != time_count) {
	printf("Time %ld\n",time_count);
	old_time = time_count;
      };
    };

    return(0);
}
*/