Apply parts of patch #1719888 to fix compilation on Mac and with
[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 //this code should not be compiled when GUI is defined
16 //(monolithic build issue)
17 #if !wxUSE_GUI
18
19 #include <signal.h>
20 #include <unistd.h>
21
22 bool wxAppConsoleUnix::Initialize(int& argc, wxChar** argv)
23 {
24 if ( !wxAppConsole::Initialize(argc,argv) )
25 return false;
26
27 if ( !m_mainLoop->IsOk() )
28 return false;
29
30 sigemptyset(&m_signalsCaught);
31
32 return true;
33 }
34
35 void wxAppConsoleUnix::HandleSignal(int signal)
36 {
37 wxAppConsoleUnix * const app = wxTheApp;
38 if ( !app )
39 return;
40
41 sigaddset(&(app->m_signalsCaught), signal);
42 app->WakeUpIdle();
43 }
44
45 void wxAppConsoleUnix::CheckSignal()
46 {
47 for ( SignalHandlerHash::iterator it = m_signalHandlerHash.begin();
48 it != m_signalHandlerHash.end();
49 ++it )
50 {
51 int sig = it->first;
52 if ( sigismember(&m_signalsCaught, sig) )
53 {
54 sigdelset(&m_signalsCaught, sig);
55 (it->second)(sig);
56 }
57 }
58 }
59
60 bool wxAppConsoleUnix::SetSignalHandler(int signal, SignalHandler handler)
61 {
62 const bool install = handler != SIG_DFL && handler != SIG_IGN;
63
64 struct sigaction sa;
65 memset(&sa, 0, sizeof(sa));
66 sa.sa_handler = &wxAppConsoleUnix::HandleSignal;
67 sa.sa_flags = SA_RESTART;
68 int res = sigaction(signal, &sa, 0);
69 if ( res != 0 )
70 {
71 wxLogSysError(_("Failed to install signal handler"));
72 return false;
73 }
74
75 if ( install )
76 m_signalHandlerHash[signal] = handler;
77 else
78 m_signalHandlerHash.erase(signal);
79
80 return true;
81 }
82
83 #endif // !wxUSE_GUI