| 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/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 | // 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_) |
| 30 | { |
| 31 | if ( !wxAppConsoleBase::Initialize(argc_, argv_) ) |
| 32 | return false; |
| 33 | |
| 34 | sigemptyset(&m_signalsCaught); |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | void wxAppConsole::HandleSignal(int signal) |
| 40 | { |
| 41 | wxAppConsole * const app = wxTheApp; |
| 42 | if ( !app ) |
| 43 | return; |
| 44 | |
| 45 | sigaddset(&(app->m_signalsCaught), signal); |
| 46 | app->WakeUpIdle(); |
| 47 | } |
| 48 | |
| 49 | void wxAppConsole::CheckSignal() |
| 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 | |
| 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 | |
| 73 | bool wxAppConsole::SetSignalHandler(int signal, SignalHandler handler) |
| 74 | { |
| 75 | const bool install = (SignalHandler_t)handler != SIG_DFL && |
| 76 | (SignalHandler_t)handler != SIG_IGN; |
| 77 | |
| 78 | struct sigaction sa; |
| 79 | memset(&sa, 0, sizeof(sa)); |
| 80 | sa.sa_handler = (SignalHandler_t)&wxAppConsole::HandleSignal; |
| 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); |
| 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 | } |
| 100 | |