OpcUaCanOpen
CANopen OPC-UA server
shutdown.cxx
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (C) 2006-2008 Unified Automation GmbH. All Rights Reserved.
3 ** Web: http://www.unifiedautomation.com
4 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
5 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
6 **
7 ** Project: Utility functions.
8 ** Description: Portable shutdown flag using CTRL-C (SIGINT)
9 ** Notes: Visual Studio catches CTRL-C when it's thrown by default,
10 ** which is not wanted normally. You can disable this by opening the
11  exceptions dialog via the menu Debug->Exceptions. Then deactivate
12  the "CTRL-C" exception in the "Win32 Exceptions" category.
13 ******************************************************************************/
14 #include "shutdown.h"
15 
16 /* shutdown flag */
17 static volatile unsigned int g_ShutDown = 0;
18 
19 #ifdef _WIN32_WCE
20 # include <windows.h>
21 #elif defined(_WIN32) && !defined(USE_CTRLC_ON_WINDOWS)
22 # include <conio.h> /* DOS header for _kbhit() and _getch() */
23 #endif
24 
25 /* Return shutdown flag. */
26 unsigned int ShutDownFlag()
27 {
28 #if defined(_WIN32) && !defined(USE_CTRLC_ON_WINDOWS)
29  #ifndef _WIN32_WCE
30  if (_kbhit() && _getch() == 'x') return 1;
31  else return 0;
32  #else
33  if (GetAsyncKeyState(VK_ESCAPE)) return 1;
34  else return 0;
35  #endif
36 #else
37  return g_ShutDown;
38 #endif
39 }
40 
41 /****************************************
42  * Linux SIGINT Handler implementation. *
43  ****************************************/
44 #ifdef __linux__
45 #include <signal.h>
46 
48 void sig_int(int signo)
49 {
50  SHUTDOWN_TRACE("Received SIG_INT(%i) signal.\n", signo);
51  g_ShutDown = 1;
52 }
53 
55 {
56  struct sigaction new_action, old_action;
57  /* Set up the structure to specify the new action. */
58  new_action.sa_handler = sig_int;
59  sigemptyset (&new_action.sa_mask);
60  new_action.sa_flags = 0;
61  /* install new signal handler */
62  sigaction(SIGINT, NULL, &old_action);
63  if (old_action.sa_handler != SIG_IGN)
64  {
65  sigaction(SIGINT, &new_action, NULL);
66  }
67 }
68 #elif defined(_WIN32)
69 /******************************************
70  * Windows CTRL-C Handler implementation. *
71  ******************************************/
72 # ifdef USE_CTRLC_ON_WINDOWS
73 # include <windows.h>
74 # include <conio.h>
75 BOOL CtrlHandler(DWORD fdwCtrlType)
76 {
77  switch( fdwCtrlType )
78  {
79  // Handle the CTRL-C signal.
80  case CTRL_C_EVENT:
81  SHUTDOWN_TRACE("Received CTRL-C signal.\n");
82  g_ShutDown = 1;
83  return( TRUE );
84  default:
85  break;
86  }
87  return FALSE;
88 }
90 {
91  SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );
92 }
93 # else /* USE_CTRLC_ON_WINDOWS */
95 {
96 
97 }
98 # endif /* USE_CTRLC_ON_WINDOWS */
99 #else /* __linux__ */
100 /********************************************************************
101  * Dummy implementation for embedded systems. They never shut down. *
102  ********************************************************************/
104 {
105 }
106 #endif