Add the logical key to Integration::KeyEvent
[platform/core/uifw/dali-core.git] / dali / internal / event / events / key-event-impl.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/key-event-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23
24 namespace Dali
25 {
26
27 namespace
28 {
29 /**
30  * This container stores a mapping between public key event and impl as we cannot add data members in public one.
31  * In practice this keeps the impl "alive" in between KeyEvent constructor and destructor calls so that getter
32  * methods can be called to access the new data members. There is a 1:1 mapping between KeyEvent and KeyEventImpl.
33  */
34 struct KeyImplMapping
35 {
36   KeyEvent* keyEvent;
37   Internal::KeyEventImpl* impl;
38 };
39 Vector< KeyImplMapping > gKeyEventToImplMapping;
40
41 }
42
43 namespace Internal
44 {
45
46 KeyEventImpl::KeyEventImpl( KeyEvent* keyEvent )
47 : mLogicalKey( "" ),
48   mCompose( "" ),
49   mDeviceName( "" ),
50   mDeviceClass( Device::Class::NONE ),
51   mDeviceSubclass( Device::Subclass::NONE )
52 {
53   gKeyEventToImplMapping.PushBack( { keyEvent, this } );
54 }
55
56 KeyEventImpl::~KeyEventImpl()
57 {
58   for( auto&& iter : gKeyEventToImplMapping )
59   {
60     if( this == iter.impl )
61     {
62       gKeyEventToImplMapping.Erase( &iter ); // iter is reference to KeyImplMapping, take address of it for Erase
63       return;
64     }
65   }
66 }
67
68 KeyEventImpl& KeyEventImpl::operator=( const KeyEventImpl& rhs )
69 {
70   if( this != &rhs )
71   {
72     mLogicalKey = rhs.mLogicalKey;
73     mCompose = rhs.mCompose;
74     mDeviceName = rhs.mDeviceName;
75     mDeviceClass = rhs.mDeviceClass;
76     mDeviceSubclass = rhs.mDeviceSubclass;
77   }
78
79   return *this;
80 }
81
82 std::string KeyEventImpl::GetLogicalKey() const
83 {
84   return mLogicalKey;
85 }
86
87 void KeyEventImpl::SetLogicalKey( const std::string& logicalKey )
88 {
89   mLogicalKey = logicalKey;
90 }
91
92 std::string KeyEventImpl::GetCompose() const
93 {
94   return mCompose;
95 }
96
97 void KeyEventImpl::SetCompose( const std::string& compose )
98 {
99   mCompose = compose;
100 }
101
102 std::string KeyEventImpl::GetDeviceName() const
103 {
104   return mDeviceName;
105 }
106
107 void KeyEventImpl::SetDeviceName( const std::string& deviceName )
108 {
109   mDeviceName = deviceName;
110 }
111
112 Device::Class::Type KeyEventImpl::GetDeviceClass() const
113 {
114   return mDeviceClass;
115 }
116
117 void KeyEventImpl::SetDeviceClass( Device::Class::Type deviceClass )
118 {
119   mDeviceClass = deviceClass;
120 }
121
122 Device::Subclass::Type KeyEventImpl::GetDeviceSubclass() const
123 {
124   return mDeviceSubclass;
125 }
126
127 void KeyEventImpl::SetDeviceSubclass( Device::Subclass::Type deviceSubclass )
128 {
129   mDeviceSubclass = deviceSubclass;
130 }
131
132 } // namsespace Internal
133
134 Internal::KeyEventImpl* GetImplementation( KeyEvent* keyEvent )
135 {
136   Internal::KeyEventImpl* impl( NULL );
137   for( auto&& iter : gKeyEventToImplMapping )
138   {
139     if( iter.keyEvent == keyEvent )
140     {
141       impl = iter.impl;
142     }
143   }
144   return impl;
145 }
146
147 const Internal::KeyEventImpl* GetImplementation( const KeyEvent* keyEvent )
148 {
149   Internal::KeyEventImpl* impl( NULL );
150   for( auto&& iter : gKeyEventToImplMapping )
151   {
152     if( iter.keyEvent == keyEvent )
153     {
154       impl = iter.impl;
155     }
156   }
157   return impl;
158 }
159
160 } // namespace Dali