/* $Id: Application,v 1.8 2008/12/30 19:49:57 kiesling Exp $ -*-c-*-*/

/*
  This file is part of Ctalk.
  Copyright  2005 - 2009  Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/*
 *    Application class.
 */

/*
 *  The require statement is still needed here for the 
 *  instance variable's class.
 */
require SignalHandler;

Event class Application;
Application instanceVariable exitHandler SignalHandler 0x0;

#if defined(__sparc__) && defined(__svr4__)
#define	SIGINT	2
#define	SIG_DFL	(void(*)())0
#undef sa_handler
#else  
# ifdef DJGPP
#define SIGINT  295
#define SIG_DFL ((void (*)(int))(0))
# else /* Linux */
# define SIG_DFL	((__sighandler_t) 0)
# define	SIGINT		2
# undef sa_handler
# endif  /* DJGPP */
#endif

/*
 *  TO DO - Make sure that eval_expr doesn't execute this method
 *  when evaluating installExitHandler....
 */
Application instanceMethod __handleAppExit (__c_arg__ int signo) {
  noMethodInit;
  struct sigaction s, s_old;
  __ctalk_exitFn(1);
  /*
   *  Exit and clean up UNIX-ly.
   *
   *  Don't use the ISO-ized #defined sigaction members 
   *  here.
   */
#if defined(__sparc__) && defined(__svr4__)
  s._funcptr._handler = SIG_DFL;
#else
# ifdef DJGPP
  s.sa_handler = SIG_DFL;
# else  /* Linux */
  s.__sigaction_handler.sa_handler = SIG_DFL;
# endif /* DJGPP */
#endif
  sigaction (SIGINT, &s, &s_old);
  kill (getpid (), SIGINT);
}

Application instanceMethod installExitHandler (void) {
  self exitHandler setSigInt;
  self exitHandler installHandler __handleAppExit;
  methodReturnSelf
}

Application instanceMethod uSleep (long long int __uSec) {
  __ctalkSleep (__uSec);
  methodReturnNULL;
}
