suppress Sun CC warnings about casting between C++ and extern C function pointers
[wxWidgets.git] / src / unix / appunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/appunix.cpp
3 // Purpose: wxAppConsole with wxMainLoop implementation
4 // Author: Lukasz Michalski
5 // Created: 28/01/2005
6 // RCS-ID: $Id$
7 // Copyright: (c) Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/app.h"
12 #include "wx/log.h"
13 #include "wx/evtloop.h"
14
15 #include <signal.h>
16 #include <unistd.h>
17
18 // use unusual names for arg[cv] to avoid clashes with wxApp members with the
19 // same names
20 bool wxAppConsole::Initialize(int& argc_, wxChar** argv_)
21 {
22 if ( !wxAppConsoleBase::Initialize(argc_, argv_) )
23 return false;
24
25 sigemptyset(&m_signalsCaught);
26
27 return true;
28 }
29
30 void wxAppConsole::HandleSignal(int signal)
31 {
32 wxAppConsole * const app = wxTheApp;
33 if ( !app )
34 return;
35
36 sigaddset(&(app->m_signalsCaught), signal);
37 app->WakeUpIdle();
38 }
39
40 void wxAppConsole::CheckSignal()
41 {
42 for ( SignalHandlerHash::iterator it = m_signalHandlerHash.begin();
43 it != m_signalHandlerHash.end();
44 ++it )
45 {
46 int sig = it->first;
47 if ( sigismember(&m_signalsCaught, sig) )
48 {
49 sigdelset(&m_signalsCaught, sig);
50 (it->second)(sig);
51 }
52 }
53 }
54
55 // the type of the signal handlers we use is "void(*)(int)" while the real
56 // signal handlers are extern "C" and so have incompatible type and at least
57 // Sun CC warns about it, so use explicit casts to suppress these warnings as
58 // they should be harmless
59 extern "C"
60 {
61 typedef void (*SignalHandler_t)(int);
62 }
63
64 bool wxAppConsole::SetSignalHandler(int signal, SignalHandler handler)
65 {
66 const bool install = (SignalHandler_t)handler != SIG_DFL &&
67 (SignalHandler_t)handler != SIG_IGN;
68
69 struct sigaction sa;
70 memset(&sa, 0, sizeof(sa));
71 sa.sa_handler = (SignalHandler_t)&wxAppConsole::HandleSignal;
72 sa.sa_flags = SA_RESTART;
73 int res = sigaction(signal, &sa, 0);
74 if ( res != 0 )
75 {
76 wxLogSysError(_("Failed to install signal handler"));
77 return false;
78 }
79
80 if ( install )
81 m_signalHandlerHash[signal] = handler;
82 else
83 m_signalHandlerHash.erase(signal);
84
85 return true;
86 }
87