]>
Commit | Line | Data |
---|---|---|
b46b1d59 VZ |
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 | ||
74ab5f5b VZ |
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 | ||
b46b1d59 VZ |
22 | #include "wx/evtloop.h" |
23 | ||
24 | #include <signal.h> | |
25 | #include <unistd.h> | |
26 | ||
3bc8edd5 VZ |
27 | // use unusual names for arg[cv] to avoid clashes with wxApp members with the |
28 | // same names | |
29 | bool wxAppConsole::Initialize(int& argc_, wxChar** argv_) | |
b46b1d59 | 30 | { |
3bc8edd5 | 31 | if ( !wxAppConsoleBase::Initialize(argc_, argv_) ) |
b46b1d59 VZ |
32 | return false; |
33 | ||
34 | sigemptyset(&m_signalsCaught); | |
35 | ||
36 | return true; | |
37 | } | |
38 | ||
e0954e72 | 39 | void wxAppConsole::HandleSignal(int signal) |
b46b1d59 | 40 | { |
e0954e72 | 41 | wxAppConsole * const app = wxTheApp; |
b46b1d59 VZ |
42 | if ( !app ) |
43 | return; | |
44 | ||
45 | sigaddset(&(app->m_signalsCaught), signal); | |
46 | app->WakeUpIdle(); | |
47 | } | |
48 | ||
e0954e72 | 49 | void wxAppConsole::CheckSignal() |
b46b1d59 VZ |
50 | { |
51 | for ( SignalHandlerHash::iterator it = m_signalHandlerHash.begin(); | |
52 | it != m_signalHandlerHash.end(); | |
53 | ++it ) | |
54 | { | |
55 | int sig = it->first; | |
56 | if ( sigismember(&m_signalsCaught, sig) ) | |
57 | { | |
58 | sigdelset(&m_signalsCaught, sig); | |
59 | (it->second)(sig); | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
7d10ec93 VZ |
64 | // the type of the signal handlers we use is "void(*)(int)" while the real |
65 | // signal handlers are extern "C" and so have incompatible type and at least | |
66 | // Sun CC warns about it, so use explicit casts to suppress these warnings as | |
67 | // they should be harmless | |
68 | extern "C" | |
69 | { | |
70 | typedef void (*SignalHandler_t)(int); | |
71 | } | |
72 | ||
e0954e72 | 73 | bool wxAppConsole::SetSignalHandler(int signal, SignalHandler handler) |
b46b1d59 | 74 | { |
7d10ec93 VZ |
75 | const bool install = (SignalHandler_t)handler != SIG_DFL && |
76 | (SignalHandler_t)handler != SIG_IGN; | |
b46b1d59 VZ |
77 | |
78 | struct sigaction sa; | |
79 | memset(&sa, 0, sizeof(sa)); | |
7d10ec93 | 80 | sa.sa_handler = (SignalHandler_t)&wxAppConsole::HandleSignal; |
934960d1 JJ |
81 | #ifdef __VMS |
82 | sa.sa_flags = 0; | |
83 | #else | |
84 | sa.sa_flags = SA_RESTART; | |
85 | #endif | |
86 | int res = sigaction(signal, &sa, 0); | |
b46b1d59 VZ |
87 | if ( res != 0 ) |
88 | { | |
89 | wxLogSysError(_("Failed to install signal handler")); | |
90 | return false; | |
91 | } | |
92 | ||
93 | if ( install ) | |
94 | m_signalHandlerHash[signal] = handler; | |
95 | else | |
96 | m_signalHandlerHash.erase(signal); | |
97 | ||
98 | return true; | |
99 | } | |
f6342fb5 | 100 |