]> git.saurik.com Git - wxWidgets.git/blob - src/unix/appunix.cpp
adapting to init pattern
[wxWidgets.git] / src / unix / appunix.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #ifndef WX_PRECOMP
18 #include "wx/app.h"
19 #include "wx/log.h"
20 #endif
21
22 #include "wx/evtloop.h"
23
24 #include <signal.h>
25 #include <unistd.h>
26
27 #ifndef SA_RESTART
28 // don't use for systems which don't define it (at least VMS and QNX)
29 #define SA_RESTART 0
30 #endif
31
32 // use unusual names for arg[cv] to avoid clashes with wxApp members with the
33 // same names
34 bool wxAppConsole::Initialize(int& argc_, wxChar** argv_)
35 {
36 if ( !wxAppConsoleBase::Initialize(argc_, argv_) )
37 return false;
38
39 sigemptyset(&m_signalsCaught);
40
41 return true;
42 }
43
44 void wxAppConsole::HandleSignal(int signal)
45 {
46 wxAppConsole * const app = wxTheApp;
47 if ( !app )
48 return;
49
50 sigaddset(&(app->m_signalsCaught), signal);
51 app->WakeUpIdle();
52 }
53
54 void wxAppConsole::CheckSignal()
55 {
56 for ( SignalHandlerHash::iterator it = m_signalHandlerHash.begin();
57 it != m_signalHandlerHash.end();
58 ++it )
59 {
60 int sig = it->first;
61 if ( sigismember(&m_signalsCaught, sig) )
62 {
63 sigdelset(&m_signalsCaught, sig);
64 (it->second)(sig);
65 }
66 }
67 }
68
69 // the type of the signal handlers we use is "void(*)(int)" while the real
70 // signal handlers are extern "C" and so have incompatible type and at least
71 // Sun CC warns about it, so use explicit casts to suppress these warnings as
72 // they should be harmless
73 extern "C"
74 {
75 typedef void (*SignalHandler_t)(int);
76 }
77
78 bool wxAppConsole::SetSignalHandler(int signal, SignalHandler handler)
79 {
80 const bool install = (SignalHandler_t)handler != SIG_DFL &&
81 (SignalHandler_t)handler != SIG_IGN;
82
83 struct sigaction sa;
84 memset(&sa, 0, sizeof(sa));
85 sa.sa_handler = (SignalHandler_t)&wxAppConsole::HandleSignal;
86 sa.sa_flags = SA_RESTART;
87 int res = sigaction(signal, &sa, 0);
88 if ( res != 0 )
89 {
90 wxLogSysError(_("Failed to install signal handler"));
91 return false;
92 }
93
94 if ( install )
95 m_signalHandlerHash[signal] = handler;
96 else
97 m_signalHandlerHash.erase(signal);
98
99 return true;
100 }
101