qtmobileextensions/src/keycapture/targetwrapper.cpp
branchRCL_3
changeset 10 cd2778e5acfe
parent 9 5d007b20cfd0
child 11 19a54be74e5e
equal deleted inserted replaced
9:5d007b20cfd0 10:cd2778e5acfe
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU Lesser General Public License as published by
       
     7  * the Free Software Foundation, version 2.1 of the License.
       
     8  * 
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU Lesser General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Lesser General Public License
       
    15  * along with this program.  If not, 
       
    16  * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17  *
       
    18  * Description:
       
    19  *
       
    20  */
       
    21 
       
    22 #include "targetwrapper.h"
       
    23 
       
    24 #include <QMenuBar>
       
    25 #include <QApplication>
       
    26 #include <QVBoxLayout>
       
    27 #include <QPlainTextEdit>
       
    28 #include <QTimer>
       
    29 
       
    30 #include <QDebug>
       
    31 #include <QtGlobal>
       
    32 
       
    33 #include "responsehandler.h"
       
    34 #include "responsehandlerex.h"
       
    35 
       
    36 #include "txlogger.h"
       
    37 
       
    38 TargetWrapper::TargetWrapper()
       
    39 :
       
    40 selector(0),
       
    41 target(0),
       
    42 targetEx(0),
       
    43 handler(0),
       
    44 handlerEx(0)
       
    45 {
       
    46     initMapping();
       
    47 }
       
    48 
       
    49 TargetWrapper::~TargetWrapper()
       
    50 {
       
    51     delete selector;
       
    52 }
       
    53 
       
    54 void TargetWrapper::close(XQKeyCapture::CapturingFlags flags)
       
    55 {
       
    56     captureFlags &= ~flags;
       
    57     reset();
       
    58 }
       
    59 
       
    60 void TargetWrapper::init(XQKeyCapture::CapturingFlags flags)
       
    61 {
       
    62     captureFlags |= flags;
       
    63     reset();
       
    64 }
       
    65 
       
    66 void TargetWrapper::reset()
       
    67 {
       
    68     try {
       
    69         delete selector;
       
    70     
       
    71         QT_TRAP_THROWING(selector = CRemConInterfaceSelector::NewL());
       
    72         
       
    73         if (captureFlags & XQKeyCapture::CaptureBasic) {
       
    74             QT_TRAP_THROWING(target = CRemConCoreApiTarget::NewL(*selector, *this));
       
    75             QT_TRAP_THROWING(handler = CResponseHandler::NewL(*target));
       
    76         }
       
    77          
       
    78         if (captureFlags & XQKeyCapture::CaptureCallHandlingExt) {
       
    79             QT_TRAP_THROWING(targetEx = CRemConCallHandlingTarget::NewL(*selector, *this));
       
    80             QT_TRAP_THROWING(handlerEx = CResponseHandlerEx::NewL(*targetEx));
       
    81         }
       
    82         
       
    83         QT_TRAP_THROWING(selector->OpenTargetL());
       
    84     
       
    85     } catch (const std::exception &e) {
       
    86         delete selector;
       
    87         selector = 0;
       
    88         target = 0;
       
    89         targetEx = 0;
       
    90         handler = 0;
       
    91         handlerEx = 0;
       
    92         qDebug() << "TargetWrapper::init - exception: " << e.what();
       
    93         throw;
       
    94     }    
       
    95 }
       
    96 
       
    97 Qt::Key TargetWrapper::mapKey(TRemConCoreApiOperationId aOperationId)
       
    98 {
       
    99     Qt::Key key = keyMapping.value(aOperationId);
       
   100     
       
   101     if (key != Qt::Key())
       
   102         return key;
       
   103     else {
       
   104         return Qt::Key_unknown;
       
   105     }
       
   106     
       
   107 }
       
   108 
       
   109 void TargetWrapper::MrccatoCommand(TRemConCoreApiOperationId aOperationId, TRemConCoreApiButtonAction aButtonAct)
       
   110 {
       
   111     Qt::Key key = mapKey(aOperationId); 
       
   112 
       
   113     switch (aButtonAct) {
       
   114         case ERemConCoreApiButtonPress:
       
   115             TX_LOG_ARGS("ERemConCoreApiButtonPress");
       
   116             sendKey(QEvent::KeyPress, key, Qt::NoModifier, aOperationId);
       
   117             break;
       
   118         case ERemConCoreApiButtonRelease:
       
   119             TX_LOG_ARGS("ERemConCoreApiButtonRelease");
       
   120             sendKey(QEvent::KeyRelease, key, Qt::NoModifier, aOperationId);
       
   121             break;
       
   122         case ERemConCoreApiButtonClick:
       
   123             TX_LOG_ARGS("ERemConCoreApiButtonClick");
       
   124             sendKey(QEvent::KeyPress, key, Qt::NoModifier, aOperationId);
       
   125             sendKey(QEvent::KeyRelease, key, Qt::NoModifier, aOperationId);
       
   126             break;
       
   127         default:
       
   128             return;
       
   129     }
       
   130 
       
   131     handler->CompleteAnyKey(aOperationId);
       
   132 }
       
   133 
       
   134 void TargetWrapper::AnswerCall()
       
   135 {
       
   136     sendKey(QEvent::KeyPress, Qt::Key_Call, Qt::NoModifier);
       
   137     sendKey(QEvent::KeyRelease, Qt::Key_Call, Qt::NoModifier);
       
   138     handlerEx->CompleteAnyKey(0);
       
   139 }
       
   140 
       
   141 void TargetWrapper::EndCall()
       
   142 {
       
   143     sendKey(QEvent::KeyPress, Qt::Key_Hangup, Qt::NoModifier);
       
   144     sendKey(QEvent::KeyRelease, Qt::Key_Hangup, Qt::NoModifier);
       
   145     handlerEx->CompleteAnyKey(0);
       
   146 }
       
   147 
       
   148 void TargetWrapper::AnswerEndCall()
       
   149 {
       
   150     sendKey(QEvent::KeyPress, Qt::Key_Hangup, Qt::NoModifier);  //TODO: Qt::Key_ToggleCallHangup
       
   151     sendKey(QEvent::KeyRelease, Qt::Key_Hangup, Qt::NoModifier); 
       
   152     handlerEx->CompleteAnyKey(0);
       
   153 }
       
   154 
       
   155 void TargetWrapper::VoiceDial( const TBool aActivate )
       
   156 {
       
   157     Q_UNUSED(aActivate)
       
   158 }
       
   159 
       
   160 void TargetWrapper::LastNumberRedial()
       
   161 {
       
   162 }
       
   163 
       
   164 void TargetWrapper::DialCall( const TDesC8& aTelNumber )
       
   165 {
       
   166     Q_UNUSED(aTelNumber)
       
   167 }
       
   168 
       
   169 void TargetWrapper::MultipartyCalling( const TDesC8& aData )
       
   170 {
       
   171     Q_UNUSED(aData)
       
   172 }
       
   173 
       
   174 void TargetWrapper::GenerateDTMF( const TChar aChar )
       
   175 {
       
   176     Q_UNUSED(aChar)
       
   177 }
       
   178 
       
   179 void TargetWrapper::SpeedDial( const TInt aIndex )    
       
   180 {
       
   181     Q_UNUSED(aIndex)
       
   182 }
       
   183 
       
   184 void TargetWrapper::sendKey(QEvent::Type eventType, Qt::Key key, Qt::KeyboardModifiers modFlags, 
       
   185                     TRemConCoreApiOperationId aOperationId)
       
   186 {
       
   187     QWidget *widget = getTargetWidget();
       
   188     if (widget) {
       
   189         QKeyEvent *event = NULL;
       
   190         if (captureFlags & XQKeyCapture::CaptureEnableRemoteExtEvents){
       
   191             if (eventType == QEvent::KeyPress){
       
   192                 event = QKeyEvent::createExtendedKeyEvent(XQKeyCapture::remoteEventType_KeyPress(), 
       
   193                         key, modFlags, 0, aOperationId, 0);
       
   194             } else if (eventType == QEvent::KeyRelease){
       
   195                 event = QKeyEvent::createExtendedKeyEvent(XQKeyCapture::remoteEventType_KeyRelease(), 
       
   196                         key, modFlags, 0, aOperationId, 0);
       
   197             }
       
   198         } else {
       
   199             event = new QKeyEvent(eventType, key, modFlags);
       
   200         }
       
   201         
       
   202         if (event){
       
   203             QApplication::sendEvent(widget, event);
       
   204             qDebug("sending key event!");
       
   205             delete event;
       
   206         }
       
   207     }
       
   208 }
       
   209 
       
   210 QWidget *TargetWrapper::getTargetWidget()
       
   211 {
       
   212     QWidget *widget;
       
   213     widget = QWidget::keyboardGrabber();
       
   214     
       
   215     if (!widget) {
       
   216         widget = QApplication::focusWidget();
       
   217     }
       
   218     
       
   219     if (!widget) {
       
   220         if (QApplication::activePopupWidget()) {
       
   221             widget = QApplication::activePopupWidget()->focusWidget();
       
   222             if (!widget) {
       
   223                 widget = QApplication::activePopupWidget();
       
   224             }
       
   225         }
       
   226     }
       
   227 
       
   228     return widget;
       
   229 }
       
   230 
       
   231 
       
   232 
       
   233 void TargetWrapper::initMapping()
       
   234 {
       
   235     keyMapping.insert(ERemConCoreApiSelect, Qt::Key_Select);
       
   236     keyMapping.insert(ERemConCoreApiUp, Qt::Key_Up);
       
   237     keyMapping.insert(ERemConCoreApiDown, Qt::Key_Down);
       
   238     keyMapping.insert(ERemConCoreApiLeft, Qt::Key_Left); // Qt::Key_Direction_L
       
   239     keyMapping.insert(ERemConCoreApiRight, Qt::Key_Right); // Qt::Key_Direction_R
       
   240     keyMapping.insert(ERemConCoreApiRightUp, Qt::Key_unknown);
       
   241     keyMapping.insert(ERemConCoreApiRightDown, Qt::Key_unknown);
       
   242     keyMapping.insert(ERemConCoreApiLeftUp, Qt::Key_unknown);
       
   243     keyMapping.insert(ERemConCoreApiLeftDown, Qt::Key_unknown);
       
   244     keyMapping.insert(ERemConCoreApiRootMenu, Qt::Key_TopMenu); // Qt::Key_Menu
       
   245     keyMapping.insert(ERemConCoreApiSetupMenu, Qt::Key_unknown); 
       
   246     keyMapping.insert(ERemConCoreApiContentsMenu, Qt::Key_unknown);
       
   247     keyMapping.insert(ERemConCoreApiFavoriteMenu, Qt::Key_Favorites); 
       
   248     keyMapping.insert(ERemConCoreApiExit, Qt::Key_unknown); // Qt::Key_Escape, Qt::Key_Cancel, Qt::Key_No
       
   249     keyMapping.insert(ERemConCoreApi0, Qt::Key_0);
       
   250     keyMapping.insert(ERemConCoreApi1, Qt::Key_1);
       
   251     keyMapping.insert(ERemConCoreApi2, Qt::Key_2);
       
   252     keyMapping.insert(ERemConCoreApi3, Qt::Key_3);
       
   253     keyMapping.insert(ERemConCoreApi4, Qt::Key_4);
       
   254     keyMapping.insert(ERemConCoreApi5, Qt::Key_5);
       
   255     keyMapping.insert(ERemConCoreApi6, Qt::Key_6);
       
   256     keyMapping.insert(ERemConCoreApi7, Qt::Key_7);
       
   257     keyMapping.insert(ERemConCoreApi8, Qt::Key_8);
       
   258     keyMapping.insert(ERemConCoreApi9, Qt::Key_9);
       
   259     keyMapping.insert(ERemConCoreApiDot, Qt::Key_Period);
       
   260     keyMapping.insert(ERemConCoreApiEnter, Qt::Key_Enter);
       
   261     keyMapping.insert(ERemConCoreApiClear, Qt::Key_Clear);
       
   262     keyMapping.insert(ERemConCoreApiChannelUp, Qt::Key_unknown);
       
   263     keyMapping.insert(ERemConCoreApiChannelDown, Qt::Key_unknown);
       
   264     keyMapping.insert(ERemConCoreApiPreviousChannel, Qt::Key_unknown);
       
   265     keyMapping.insert(ERemConCoreApiSoundSelect, Qt::Key_unknown);
       
   266     keyMapping.insert(ERemConCoreApiInputSelect, Qt::Key_unknown);
       
   267     keyMapping.insert(ERemConCoreApiDisplayInformation, Qt::Key_unknown);   // Qt::Key_Time ???
       
   268     keyMapping.insert(ERemConCoreApiHelp, Qt::Key_unknown);
       
   269     keyMapping.insert(ERemConCoreApiPageUp, Qt::Key_unknown);
       
   270     keyMapping.insert(ERemConCoreApiPageDown, Qt::Key_unknown);
       
   271     keyMapping.insert(ERemConCoreApiPower, Qt::Key_unknown); // Qt::Key_PowerOff, Qt::Key_WakeUp, Qt::Key_PowerDown, Qt::Key_Suspend
       
   272     keyMapping.insert(ERemConCoreApiVolumeUp, Qt::Key_VolumeUp);
       
   273     keyMapping.insert(ERemConCoreApiVolumeDown, Qt::Key_VolumeDown);
       
   274     keyMapping.insert(ERemConCoreApiMute, Qt::Key_unknown);
       
   275     keyMapping.insert(ERemConCoreApiPlay, Qt::Key_MediaPlay);
       
   276     keyMapping.insert(ERemConCoreApiStop, Qt::Key_MediaStop);
       
   277     keyMapping.insert(ERemConCoreApiPause, Qt::Key_unknown); // NEW: Qt::Key_MediaPause
       
   278     keyMapping.insert(ERemConCoreApiRecord, Qt::Key_MediaRecord);
       
   279     keyMapping.insert(ERemConCoreApiRewind, Qt::Key_AudioRewind);
       
   280     keyMapping.insert(ERemConCoreApiFastForward, Qt::Key_AudioForward);
       
   281     keyMapping.insert(ERemConCoreApiEject, Qt::Key_Eject);
       
   282     keyMapping.insert(ERemConCoreApiForward, Qt::Key_MediaNext);
       
   283     keyMapping.insert(ERemConCoreApiBackward, Qt::Key_MediaPrevious);
       
   284     keyMapping.insert(ERemConCoreApiAngle, Qt::Key_unknown);
       
   285     keyMapping.insert(ERemConCoreApiSubpicture, Qt::Key_unknown);  // Qt::Key_SplitScreen ???
       
   286     keyMapping.insert(ERemConCoreApiPausePlayFunction, Qt::Key_MediaPlay); // NEW: Media_PausePlay
       
   287     keyMapping.insert(ERemConCoreApiRestoreVolumeFunction, Qt::Key_unknown);
       
   288     keyMapping.insert(ERemConCoreApiTuneFunction, Qt::Key_unknown);
       
   289     keyMapping.insert(ERemConCoreApiSelectDiskFunction, Qt::Key_unknown);
       
   290     keyMapping.insert(ERemConCoreApiSelectAvInputFunction, Qt::Key_unknown);
       
   291     keyMapping.insert(ERemConCoreApiSelectAudioInputFunction, Qt::Key_unknown);
       
   292     keyMapping.insert(ERemConCoreApiF1, Qt::Key_F1);
       
   293     keyMapping.insert(ERemConCoreApiF2, Qt::Key_F2);
       
   294     keyMapping.insert(ERemConCoreApiF3, Qt::Key_F3);
       
   295     keyMapping.insert(ERemConCoreApiF4, Qt::Key_F4);
       
   296     keyMapping.insert(ERemConCoreApiF5, Qt::Key_F5);
       
   297     keyMapping.insert(ENop, Qt::Key_unknown);
       
   298 }