Merge changes Iebb3261e,I67abf9f4 into devel/master
[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 : mDeviceName( "" ),
48   mDeviceClass( DevelDevice::Class::NONE ),
49   mDeviceSubclass( DevelDevice::Subclass::NONE )
50 {
51   gKeyEventToImplMapping.PushBack( { keyEvent, this } );
52 }
53
54 KeyEventImpl::~KeyEventImpl()
55 {
56   for( auto&& iter : gKeyEventToImplMapping )
57   {
58     if( this == iter.impl )
59     {
60       gKeyEventToImplMapping.Erase( &iter ); // iter is reference to KeyImplMapping, take address of it for Erase
61       return;
62     }
63   }
64 }
65
66 KeyEventImpl& KeyEventImpl::operator=( const KeyEventImpl& rhs )
67 {
68   if( this != &rhs )
69   {
70     mDeviceName = rhs.mDeviceName;
71     mDeviceClass = rhs.mDeviceClass;
72     mDeviceSubclass = rhs.mDeviceSubclass;
73   }
74
75   return *this;
76 }
77
78 std::string KeyEventImpl::GetDeviceName() const
79 {
80   return mDeviceName;
81 }
82
83 void KeyEventImpl::SetDeviceName( const std::string& deviceName )
84 {
85   mDeviceName = deviceName;
86 }
87
88 DevelDevice::Class::Type KeyEventImpl::GetDeviceClass() const
89 {
90   return mDeviceClass;
91 }
92
93 void KeyEventImpl::SetDeviceClass( DevelDevice::Class::Type deviceClass )
94 {
95   mDeviceClass = deviceClass;
96 }
97
98 DevelDevice::Subclass::Type KeyEventImpl::GetDeviceSubclass() const
99 {
100   return mDeviceSubclass;
101 }
102
103 void KeyEventImpl::SetDeviceSubclass( DevelDevice::Subclass::Type deviceSubclass )
104 {
105   mDeviceSubclass = deviceSubclass;
106 }
107
108 } // namsespace Internal
109
110 Internal::KeyEventImpl* GetImplementation( KeyEvent* keyEvent )
111 {
112   Internal::KeyEventImpl* impl( NULL );
113   for( auto&& iter : gKeyEventToImplMapping )
114   {
115     if( iter.keyEvent == keyEvent )
116     {
117       impl = iter.impl;
118     }
119   }
120   return impl;
121 }
122
123 const Internal::KeyEventImpl* GetImplementation( const KeyEvent* keyEvent )
124 {
125   Internal::KeyEventImpl* impl( NULL );
126   for( auto&& iter : gKeyEventToImplMapping )
127   {
128     if( iter.keyEvent == keyEvent )
129     {
130       impl = iter.impl;
131     }
132   }
133   return impl;
134 }
135
136 } // namespace Dali