//  Copyright (c) 2007, Monju System Architects
//
//  Released under the Monju-Public copyright license:
//
//  Permission is hereby granted, free of charge, to any person or entity
//  obtaining a copy of this software and associated documentation files
//  (the "Software"), to deal in the Software without restriction,
//  including without limitation the rights to use, copy, modify, merge,
//  publish, distribute, sublicense, and/or sell copies and modifications
//  of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of published, sold, or
//  ownership-transferred versions of the Software. The notices are not
//  required for binary-only releases or binary-only derivatives of the
//  Software, or documentation provided with binary-only releases and
//  binary-only derivatives.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
//  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
//  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
//  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
//  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
//  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#ifndef _PUBLIC_SIGNAL_HANDLING_H
#define _PUBLIC_SIGNAL_HANDLING_H



//  INCLUDES.
#ifdef _WIN32
#include <Windows.h>
#else
#include <signal.h>
#endif



//  DEFINITIONS.
#define SYSTEM_SIGNAL__IGNORE  1000

#ifdef _WIN32

	#define MAX_SYSTEM_SIGNAL 7

	//  Common system signals.
	#define SIGNAL__KEYBOARD_INTERRUPT          CTRL_C_EVENT
	#define SIGNAL__CLOSE_TERMINAL              CTRL_CLOSE_EVENT

	//  Windows only signals.
	#define SIGNAL__BREAK                       CTRL_BREAK_EVENT
	#define SIGNAL__LOGOFF                      CTRL_LOGOFF_EVENT    // ANY USER.  ONLY RECEIVED BY SERVICES.
	#define SIGNAL__SHUTDOWN                    CTRL_SHUTDOWN_EVENT  // ONLY RECEIVED BY SERVICES.

	//  Unix only signals.
	#define SIGNAL__ABORT                       SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__ALARM                       SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__BROKEN_PIPE                 SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__CHILD_PROCESS               SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__CONTINUE                    SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__FLOATING_POINT_EXCEPTION    SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__ILLEGAL_INSTRUCTION         SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__KILL                        SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__KEYBOARD_QUIT               SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__SEGMENTATION_VIOLATION      SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__STOP                        SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__STOP_TTY                    SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__TERMINATE                   SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__TTY_INPUT                   SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__TTY_OUTPUT                  SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__USER_1                      SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__USER_2                      SYSTEM_SIGNAL__IGNORE

	typedef DWORD SYSTEM_SIGNAL_TYPE;
	typedef bool SYSTEM_SIGNAL_RETURN_TYPE;


#else  //  Unix...

	#define MAX_SYSTEM_SIGNAL 40

	//  Common system signals.
	#define SIGNAL__KEYBOARD_INTERRUPT          SIGINT
	#define SIGNAL__CLOSE_TERMINAL              SIGHUP

	//  Windows only signals.
	#define SIGNAL__BREAK                       SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__LOGOFF                      SYSTEM_SIGNAL__IGNORE
	#define SIGNAL__SHUTDOWN                    SYSTEM_SIGNAL__IGNORE

	//  Unix only signals.
	#define SIGNAL__ABORT                       SIGABRT
	#define SIGNAL__ALARM                       SIGALARM
	#define SIGNAL__BROKEN_PIPE                 SIGPIPE
	#define SIGNAL__CHILD_PROCESS               SIGCHILD
	#define SIGNAL__CONTINUE                    SIGCONT
	#define SIGNAL__FLOATING_POINT_EXCEPTION    SIGFPE
	#define SIGNAL__ILLEGAL_INSTRUCTION         SIGILL
	#define SIGNAL__KILL                        SIGKILL
	#define SIGNAL__KEYBOARD_QUIT               SIGQUIT
	#define SIGNAL__SEGMENTATION_VIOLATION      SIGSEGV
	#define SIGNAL__STOP                        SIGSTOP
	#define SIGNAL__STOP_TTY                    SIGSTP
	#define SIGNAL__TERMINATE                   SIGTERM
	#define SIGNAL__TTY_INPUT                   SIGTTIN
	#define SIGNAL__TTY_OUTPUT                  SIGTTOU
	#define SIGNAL__USER_1                      SIGUSR1
	#define SIGNAL__USER_2                      SIGUSR2

	typedef int SYSTEM_SIGNAL_TYPE;
	typedef void SYSTEM_SIGNAL_RETURN_TYPE;

#endif // #ifdef _WIN32



//  THE SIGNAL HANDLER FUNCTION DEFINITION.
//
//  A signal handler should return true if it handles the signal and false otherwise.
typedef bool (*SYSTEM_SIGNAL_HANDLER) (SYSTEM_SIGNAL_TYPE);




//  setSystemSignalHandler()
void setSystemSignalHandler(SYSTEM_SIGNAL_TYPE signal, SYSTEM_SIGNAL_HANDLER handlerFunction);



#endif //  #ifndef _PUBLIC_SIGNAL_HANDLING_H


