taskswitcher/client/s60/src/tssession.cpp
author hgs
Mon, 13 Sep 2010 13:26:33 +0300
changeset 116 305818acdca4
child 119 50e220be30d1
permissions -rw-r--r--
201036
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
116
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
#include <s32mem.h>
hgs
parents:
diff changeset
    18
#include <fbs.h>
hgs
parents:
diff changeset
    19
hgs
parents:
diff changeset
    20
#include "tssession.h"
hgs
parents:
diff changeset
    21
#include "tsscreenshotmsg.h"
hgs
parents:
diff changeset
    22
#include "tsunregscreenshotmsg.h"
hgs
parents:
diff changeset
    23
#include "tsvisibilitymsg.h"
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
TInt RTsSession::Connect()
hgs
parents:
diff changeset
    26
{
hgs
parents:
diff changeset
    27
    TInt retry = 4;
hgs
parents:
diff changeset
    28
    TInt error(KErrNone);
hgs
parents:
diff changeset
    29
    do {
hgs
parents:
diff changeset
    30
        error = CreateSession(KRunningAppServerName, TVersion(0, 0, 0));
hgs
parents:
diff changeset
    31
        if (KErrNotFound == error || KErrServerTerminated == error) {
hgs
parents:
diff changeset
    32
            error = StartServer();
hgs
parents:
diff changeset
    33
            if (KErrNone == error || KErrAlreadyExists == error) {
hgs
parents:
diff changeset
    34
                retry = 0;
hgs
parents:
diff changeset
    35
                error = CreateSession(KRunningAppServerName, TVersion(0, 0, 0));
hgs
parents:
diff changeset
    36
            }
hgs
parents:
diff changeset
    37
        } else {
hgs
parents:
diff changeset
    38
            retry = 0;
hgs
parents:
diff changeset
    39
        }
hgs
parents:
diff changeset
    40
    } while (--retry > 0);
hgs
parents:
diff changeset
    41
    return error;
hgs
parents:
diff changeset
    42
}
hgs
parents:
diff changeset
    43
hgs
parents:
diff changeset
    44
TInt RTsSession::StartServer()
hgs
parents:
diff changeset
    45
{
hgs
parents:
diff changeset
    46
    TInt error;
hgs
parents:
diff changeset
    47
hgs
parents:
diff changeset
    48
    RSemaphore semaphore;
hgs
parents:
diff changeset
    49
    error = semaphore.CreateGlobal(KTsServerReadySemaphore, 0);
hgs
parents:
diff changeset
    50
    
hgs
parents:
diff changeset
    51
    if (KErrNone == error || KErrAlreadyExists == error) {
hgs
parents:
diff changeset
    52
        RProcess server;
hgs
parents:
diff changeset
    53
        error = server.Create(KRunningAppServerName, KNullDesC, TUidType(KNullUid, KNullUid, KRunningAppServerUid));
hgs
parents:
diff changeset
    54
        if (KErrNone == error) {
hgs
parents:
diff changeset
    55
            TRequestStatus status;
hgs
parents:
diff changeset
    56
            server.Rendezvous(status);
hgs
parents:
diff changeset
    57
            
hgs
parents:
diff changeset
    58
            if (status != KRequestPending) {
hgs
parents:
diff changeset
    59
                server.Kill(0);
hgs
parents:
diff changeset
    60
                User::WaitForRequest(status);
hgs
parents:
diff changeset
    61
            } else {
hgs
parents:
diff changeset
    62
                server.Resume();
hgs
parents:
diff changeset
    63
                User::WaitForRequest(status);
hgs
parents:
diff changeset
    64
hgs
parents:
diff changeset
    65
                // wait for server
hgs
parents:
diff changeset
    66
                semaphore.Wait();
hgs
parents:
diff changeset
    67
            }
hgs
parents:
diff changeset
    68
hgs
parents:
diff changeset
    69
            if (KErrCancel == status.Int()) {
hgs
parents:
diff changeset
    70
                error = KErrNone;
hgs
parents:
diff changeset
    71
            } else {
hgs
parents:
diff changeset
    72
                error = (EExitPanic == server.ExitType()) ? KErrGeneral : status.Int();
hgs
parents:
diff changeset
    73
            }
hgs
parents:
diff changeset
    74
        }
hgs
parents:
diff changeset
    75
        server.Close();
hgs
parents:
diff changeset
    76
    }
hgs
parents:
diff changeset
    77
    semaphore.Close();
hgs
parents:
diff changeset
    78
hgs
parents:
diff changeset
    79
    return error;
hgs
parents:
diff changeset
    80
}
hgs
parents:
diff changeset
    81
hgs
parents:
diff changeset
    82
hgs
parents:
diff changeset
    83
HBufC8* RTsSession::TasksContentLC()
hgs
parents:
diff changeset
    84
{
hgs
parents:
diff changeset
    85
    for (;;) {
hgs
parents:
diff changeset
    86
        TPckgBuf<TInt> dataSize;
hgs
parents:
diff changeset
    87
        User::LeaveIfError(SendReceive(GetRunningAppInfo, TIpcArgs(&dataSize)));        
hgs
parents:
diff changeset
    88
        HBufC8* data = HBufC8::NewLC(dataSize());
hgs
parents:
diff changeset
    89
        TPtr8 dataPointer(data->Des());
hgs
parents:
diff changeset
    90
        TInt err = SendReceive(FlushData, TIpcArgs(&dataPointer, dataSize()));
hgs
parents:
diff changeset
    91
        if (err == KErrNone) {
hgs
parents:
diff changeset
    92
            return data;
hgs
parents:
diff changeset
    93
        } else if (err != KErrCorrupt) {
hgs
parents:
diff changeset
    94
            User::Leave(err);
hgs
parents:
diff changeset
    95
        }
hgs
parents:
diff changeset
    96
        CleanupStack::PopAndDestroy(data);
hgs
parents:
diff changeset
    97
        // If result was KErrCorrupt then the size received from GetRunningAppInfo is
hgs
parents:
diff changeset
    98
        // not valid anymore so restart the whole procedure.
hgs
parents:
diff changeset
    99
    }
hgs
parents:
diff changeset
   100
}
hgs
parents:
diff changeset
   101
hgs
parents:
diff changeset
   102
void RTsSession::Subscribe(TRequestStatus& aStatus)
hgs
parents:
diff changeset
   103
{
hgs
parents:
diff changeset
   104
    SendReceive(SubscribeRunningAppChanges, aStatus);
hgs
parents:
diff changeset
   105
}
hgs
parents:
diff changeset
   106
hgs
parents:
diff changeset
   107
void RTsSession::CancelSubscribe()
hgs
parents:
diff changeset
   108
{
hgs
parents:
diff changeset
   109
    SendReceive(CancelSubscribeRunningAppChanges);
hgs
parents:
diff changeset
   110
}
hgs
parents:
diff changeset
   111
hgs
parents:
diff changeset
   112
void RTsSession::OpenTask(const TDesC8 &key)
hgs
parents:
diff changeset
   113
{
hgs
parents:
diff changeset
   114
    SendReceive(OpenTaskMessage, TIpcArgs(&key));
hgs
parents:
diff changeset
   115
}
hgs
parents:
diff changeset
   116
hgs
parents:
diff changeset
   117
void RTsSession::CloseTask(const TDesC8 &key)
hgs
parents:
diff changeset
   118
{
hgs
parents:
diff changeset
   119
    SendReceive(CloseTaskMessage, TIpcArgs(&key));
hgs
parents:
diff changeset
   120
}
hgs
parents:
diff changeset
   121
hgs
parents:
diff changeset
   122
TInt RTsSession::RegisterScreenshot(CFbsBitmap *screenshot, TInt wgId, UpdatePriority priority)
hgs
parents:
diff changeset
   123
{
hgs
parents:
diff changeset
   124
   TRAPD(errNo, RegisterScreenshotL(screenshot, wgId, priority));
hgs
parents:
diff changeset
   125
   return errNo;
hgs
parents:
diff changeset
   126
}
hgs
parents:
diff changeset
   127
hgs
parents:
diff changeset
   128
TInt RTsSession::UnregisterScreenshot(TInt wgId)
hgs
parents:
diff changeset
   129
{
hgs
parents:
diff changeset
   130
    TRAPD(errNo, UnregisterScreenshotL(wgId));
hgs
parents:
diff changeset
   131
   return errNo;
hgs
parents:
diff changeset
   132
}
hgs
parents:
diff changeset
   133
hgs
parents:
diff changeset
   134
TInt RTsSession::SetVisibility(TInt wgId, Visibility value)
hgs
parents:
diff changeset
   135
{
hgs
parents:
diff changeset
   136
    TRAPD(errNo, SetVisibilityL(wgId, value));
hgs
parents:
diff changeset
   137
    return errNo;
hgs
parents:
diff changeset
   138
}
hgs
parents:
diff changeset
   139
hgs
parents:
diff changeset
   140
void RTsSession::RegisterScreenshotL(CFbsBitmap *screenshot, TInt wgId, UpdatePriority priority)
hgs
parents:
diff changeset
   141
{
hgs
parents:
diff changeset
   142
    CTsScreenshotMsg *message = CTsScreenshotMsg::NewLC(wgId, *screenshot, priority, 0);
hgs
parents:
diff changeset
   143
    HBufC8* serializedData = message->ExternalizeLC();
hgs
parents:
diff changeset
   144
    TIpcArgs arguments(serializedData);
hgs
parents:
diff changeset
   145
    User::LeaveIfError(SendReceive(RegisterScreenshotMessage, arguments));
hgs
parents:
diff changeset
   146
    CleanupStack::PopAndDestroy(serializedData);
hgs
parents:
diff changeset
   147
    CleanupStack::PopAndDestroy(message);
hgs
parents:
diff changeset
   148
}
hgs
parents:
diff changeset
   149
hgs
parents:
diff changeset
   150
void RTsSession::UnregisterScreenshotL(TInt wgId)
hgs
parents:
diff changeset
   151
{
hgs
parents:
diff changeset
   152
    CTsUnregisterScreenshotMsg *message = CTsUnregisterScreenshotMsg::NewLC(wgId);
hgs
parents:
diff changeset
   153
    HBufC8* serializedData = message->ExternalizeLC();
hgs
parents:
diff changeset
   154
    TIpcArgs arguments(serializedData);
hgs
parents:
diff changeset
   155
    User::LeaveIfError(SendReceive(UnregisterScreenshotMessage, arguments));
hgs
parents:
diff changeset
   156
    CleanupStack::PopAndDestroy(serializedData);
hgs
parents:
diff changeset
   157
    CleanupStack::PopAndDestroy(message);
hgs
parents:
diff changeset
   158
}
hgs
parents:
diff changeset
   159
hgs
parents:
diff changeset
   160
void RTsSession::SetVisibilityL(TInt wgId, Visibility value)
hgs
parents:
diff changeset
   161
{
hgs
parents:
diff changeset
   162
    CTsVisibilitMsg *message = CTsVisibilitMsg::NewLC(wgId, value);
hgs
parents:
diff changeset
   163
    HBufC8* serializedData = message->ExternalizeLC();
hgs
parents:
diff changeset
   164
    TIpcArgs arguments(serializedData);
hgs
parents:
diff changeset
   165
    User::LeaveIfError(SendReceive(VisibilityChange, arguments));
hgs
parents:
diff changeset
   166
    CleanupStack::PopAndDestroy(serializedData);
hgs
parents:
diff changeset
   167
    CleanupStack::PopAndDestroy(message);
hgs
parents:
diff changeset
   168
}