tools/runonphone/ossignalconverter.cpp
changeset 37 758a864f9613
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "ossignalconverter_p.h"
       
    43 #include <signal.h>
       
    44 #include <QTimer>
       
    45 
       
    46 Q_GLOBAL_STATIC(OsSignalConverter, osSignalConverter);
       
    47 
       
    48 OsSignalConverter* OsSignalConverter::instance()
       
    49 {
       
    50     return osSignalConverter();
       
    51 }
       
    52 
       
    53 OsSignalConverter::OsSignalConverter()
       
    54 : d(new OsSignalConverterPrivate(this))
       
    55 {
       
    56 };
       
    57 
       
    58 OsSignalConverter::~OsSignalConverter()
       
    59 {
       
    60 }
       
    61 
       
    62 OsSignalConverterPrivate::OsSignalConverterPrivate(OsSignalConverter* owner)
       
    63 : QObject(owner), q(owner), poller(new QTimer(this))
       
    64 {
       
    65     trap();
       
    66     connect(poller, SIGNAL(timeout()), this, SLOT(poll()));
       
    67     poller->start(1000);
       
    68 }
       
    69 
       
    70 OsSignalConverterPrivate::~OsSignalConverterPrivate()
       
    71 {
       
    72     untrap();
       
    73 }
       
    74 
       
    75 void OsSignalConverterPrivate::trap()
       
    76 {
       
    77     signal(SIGINT, handler);
       
    78     signal(SIGTERM, handler);
       
    79 #ifdef SIGBREAK
       
    80     signal(SIGBREAK, handler);
       
    81 #endif
       
    82 #ifdef SIGHUP
       
    83     signal(SIGHUP, handler);
       
    84 #endif
       
    85 #ifdef SIGQUIT
       
    86     signal(SIGQUIT, handler);
       
    87 #endif
       
    88 }
       
    89 
       
    90 void OsSignalConverterPrivate::untrap()
       
    91 {
       
    92     signal(SIGINT, SIG_DFL);
       
    93     signal(SIGTERM, SIG_DFL);
       
    94 #ifdef SIGBREAK
       
    95     signal(SIGBREAK, SIG_DFL);
       
    96 #endif
       
    97 #ifdef SIGHUP
       
    98     signal(SIGHUP, SIG_DFL);
       
    99 #endif
       
   100 #ifdef SIGQUIT
       
   101     signal(SIGQUIT, SIG_DFL);
       
   102 #endif
       
   103 }
       
   104 
       
   105 void OsSignalConverterPrivate::handler(int sig)
       
   106 {
       
   107     untrap(); //allow 2nd ctrl-c to really kill us
       
   108     terminateRequest = sig;
       
   109 }
       
   110 
       
   111 void OsSignalConverterPrivate::poll()
       
   112 {
       
   113     if (terminateRequest) {
       
   114         fprintf(stderr, "\n*** caught signal %d, terminating ***\n", terminateRequest);
       
   115         poller->stop();
       
   116         emit q->terminate();
       
   117     }
       
   118 }
       
   119 
       
   120 sig_atomic_t OsSignalConverterPrivate::terminateRequest;