]> git.saurik.com Git - wxWidgets.git/blame - src/unix/appunix.cpp
Call wxThread::Create() from Run() automatically.
[wxWidgets.git] / src / unix / appunix.cpp
CommitLineData
b46b1d59 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/unix/appunix.cpp
b46b1d59
VZ
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
26844c72
VZ
27#ifndef SA_RESTART
28 // don't use for systems which don't define it (at least VMS and QNX)
e1cf09d9 29 #define SA_RESTART 0
26844c72
VZ
30#endif
31
3bc8edd5
VZ
32// use unusual names for arg[cv] to avoid clashes with wxApp members with the
33// same names
34bool wxAppConsole::Initialize(int& argc_, wxChar** argv_)
b46b1d59 35{
3bc8edd5 36 if ( !wxAppConsoleBase::Initialize(argc_, argv_) )
b46b1d59
VZ
37 return false;
38
39 sigemptyset(&m_signalsCaught);
40
41 return true;
42}
43
e0954e72 44void wxAppConsole::HandleSignal(int signal)
b46b1d59 45{
e0954e72 46 wxAppConsole * const app = wxTheApp;
b46b1d59
VZ
47 if ( !app )
48 return;
49
50 sigaddset(&(app->m_signalsCaught), signal);
51 app->WakeUpIdle();
52}
53
e0954e72 54void wxAppConsole::CheckSignal()
b46b1d59
VZ
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
7d10ec93
VZ
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
73extern "C"
74{
75 typedef void (*SignalHandler_t)(int);
76}
77
e0954e72 78bool wxAppConsole::SetSignalHandler(int signal, SignalHandler handler)
b46b1d59 79{
7d10ec93
VZ
80 const bool install = (SignalHandler_t)handler != SIG_DFL &&
81 (SignalHandler_t)handler != SIG_IGN;
b46b1d59
VZ
82
83 struct sigaction sa;
84 memset(&sa, 0, sizeof(sa));
7d10ec93 85 sa.sa_handler = (SignalHandler_t)&wxAppConsole::HandleSignal;
26844c72
VZ
86 sa.sa_flags = SA_RESTART;
87 int res = sigaction(signal, &sa, 0);
b46b1d59
VZ
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}
f6342fb5 101