|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "uieventsender.h" |
|
19 #include "UiEvent.h" |
|
20 |
|
21 #include "filelogger.h" |
|
22 |
|
23 using namespace stmUiEventEngine; |
|
24 |
|
25 |
|
26 CUiEventSender* CUiEventSender::NewL() |
|
27 { |
|
28 CUiEventSender* self = new (ELeave) CUiEventSender(); |
|
29 |
|
30 return self; |
|
31 } |
|
32 |
|
33 /*! |
|
34 * An active object for sending the UI events to the observers. |
|
35 * Depending on the m_directsending the asynchronous method is not |
|
36 * used but instead the observers will be called immediately. |
|
37 */ |
|
38 CUiEventSender::CUiEventSender() |
|
39 { |
|
40 m_loggingenabled = false ; |
|
41 for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++) |
|
42 { |
|
43 iEvents[i] = NULL ; |
|
44 } |
|
45 } |
|
46 |
|
47 CUiEventSender::~CUiEventSender() |
|
48 { |
|
49 // remove the possible events from the buffers if release was missed |
|
50 for (int i = 0; i < stmUiEventEngine::KMaxNumberOfPointers; i++) |
|
51 { |
|
52 if (iEvents[i]) delete iEvents[i] ; |
|
53 } |
|
54 iObserver.Reset() ; |
|
55 } |
|
56 |
|
57 /*! |
|
58 * Add new UI event to the list or send it directly to the observers |
|
59 * depending on the m_directsending flag. |
|
60 * \param aUiEvent the new UI event to be sent to the observers. |
|
61 */ |
|
62 TInt CUiEventSender::AddEvent(CUiEvent* aUiEvent) |
|
63 { |
|
64 int pointerIndex = aUiEvent->Index() ; |
|
65 // Store the new UI event. Check what kind of event it is and compress the set of events |
|
66 // stored so far if possible |
|
67 compressStack(aUiEvent) ; |
|
68 aUiEvent->setPrevious(iEvents[pointerIndex]) ; |
|
69 iEvents[pointerIndex] = aUiEvent ; // Store the new event |
|
70 TRAPD(err, EmitEventL(*aUiEvent)); |
|
71 if(err) |
|
72 return err; |
|
73 |
|
74 if (m_loggingenabled) |
|
75 { |
|
76 LOGARG("Sent event: %s: (ptr %d) (%d,%d)", |
|
77 stmUiEventEngine::EventName(aUiEvent->Code()), pointerIndex, |
|
78 aUiEvent->CurrentXY().iX, aUiEvent->CurrentXY().iY); |
|
79 } |
|
80 // If this was release event, then the chain can be removed |
|
81 if (aUiEvent->Code() == stmUiEventEngine::ERelease) |
|
82 { |
|
83 delete aUiEvent; // This will delete the whole chain |
|
84 iEvents[pointerIndex] = NULL ; |
|
85 } |
|
86 return KErrNone; |
|
87 } |
|
88 /*! |
|
89 * Call each observer with the event |
|
90 */ |
|
91 void CUiEventSender::EmitEventL(const CUiEvent& aEvent) |
|
92 { |
|
93 for (TInt i = 0; i < iObserver.Count(); i++) |
|
94 { |
|
95 iObserver[i]->HandleUiEventL(aEvent); |
|
96 } |
|
97 } |
|
98 |
|
99 /*! |
|
100 * Add a new observer. Note that current implementation is very rude: |
|
101 * max 5 observers in a simple array. |
|
102 */ |
|
103 bool CUiEventSender::addObserver(MUiEventObserver* aObserver ) |
|
104 { |
|
105 iObserver.Append(aObserver) ; |
|
106 return true ; |
|
107 } |
|
108 |
|
109 /* |
|
110 * remove observer from list |
|
111 */ |
|
112 bool CUiEventSender::removeObserver(MUiEventObserver* aObserver ) |
|
113 { |
|
114 int x = iObserver.Find(aObserver) ; |
|
115 if (x != -1) |
|
116 { |
|
117 iObserver.Remove(x) ; |
|
118 return true ; |
|
119 |
|
120 } |
|
121 return EFalse ; // Could not find observer |
|
122 } |
|
123 void CUiEventSender::compressStack(CUiEvent* aUiEvent) |
|
124 { |
|
125 int pointerIndex = aUiEvent->Index() ; |
|
126 CUiEvent*& top = iEvents[pointerIndex] ; |
|
127 if(!top) |
|
128 { |
|
129 return; |
|
130 } |
|
131 if (aUiEvent->Code() == stmUiEventEngine::EHold) |
|
132 { |
|
133 // assumption: in case of hold, we can discard all previous messages |
|
134 delete top ; |
|
135 top = NULL ; |
|
136 } |
|
137 else |
|
138 { |
|
139 // Check if there would too many moves |
|
140 CUiEvent* next = dynamic_cast<CUiEvent*>(top->previousEvent()) ; |
|
141 if (next != 0 && next->Code() == stmUiEventEngine::EMove) |
|
142 { |
|
143 // leave only the topmost to the stack |
|
144 top->setPrevious(0) ; |
|
145 delete next ; |
|
146 } |
|
147 } |
|
148 |
|
149 } |