[dali_1.2.51] Merge branch '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 #include <dali/devel-api/common/dali-vector-devel.h>
24
25 namespace Dali
26 {
27
28 namespace
29 {
30 /**
31  * This container stores a mapping between public key event and impl as we cannot add data members in public one.
32  * In practice this keeps the impl "alive" in between KeyEvent constructor and destructor calls so that getter
33  * methods can be called to access the new data members. There is a 1:1 mapping between KeyEvent and KeyEventImpl.
34  */
35 struct KeyImplMapping
36 {
37   KeyEvent* keyEvent;
38   Internal::KeyEventImpl* impl;
39 };
40 Vector< KeyImplMapping > gKeyEventToImplMapping;
41
42 }
43
44 namespace Internal
45 {
46
47 KeyEventImpl::KeyEventImpl( KeyEvent* keyEvent )
48 : mDeviceName( "" ),
49   mDeviceClass( DevelDevice::Class::NONE ),
50   mDeviceSubclass( DevelDevice::Subclass::NONE )
51 {
52   gKeyEventToImplMapping.PushBack( { keyEvent, this } );
53 }
54
55 KeyEventImpl::~KeyEventImpl()
56 {
57   for( auto&& iter : gKeyEventToImplMapping )
58   {
59     if( this == iter.impl )
60     {
61       gKeyEventToImplMapping.Erase( &iter ); // iter is reference to KeyImplMapping, take address of it for Erase
62       return;
63     }
64   }
65 }
66
67 KeyEventImpl& KeyEventImpl::operator=( const KeyEventImpl& rhs )
68 {
69   if( this != &rhs )
70   {
71     mDeviceName = rhs.mDeviceName;
72     mDeviceClass = rhs.mDeviceClass;
73     mDeviceSubclass = rhs.mDeviceSubclass;
74   }
75
76   return *this;
77 }
78
79 std::string KeyEventImpl::GetDeviceName() const
80 {
81   return mDeviceName;
82 }
83
84 void KeyEventImpl::SetDeviceName( const std::string& deviceName )
85 {
86   mDeviceName = deviceName;
87 }
88
89 DevelDevice::Class::Type KeyEventImpl::GetDeviceClass() const
90 {
91   return mDeviceClass;
92 }
93
94 void KeyEventImpl::SetDeviceClass( DevelDevice::Class::Type deviceClass )
95 {
96   mDeviceClass = deviceClass;
97 }
98
99 DevelDevice::Subclass::Type KeyEventImpl::GetDeviceSubclass() const
100 {
101   return mDeviceSubclass;
102 }
103
104 void KeyEventImpl::SetDeviceSubclass( DevelDevice::Subclass::Type deviceSubclass )
105 {
106   mDeviceSubclass = deviceSubclass;
107 }
108
109 } // namsespace Internal
110
111 Internal::KeyEventImpl* GetImplementation( KeyEvent* keyEvent )
112 {
113   Internal::KeyEventImpl* impl( NULL );
114   for( auto&& iter : gKeyEventToImplMapping )
115   {
116     if( iter.keyEvent == keyEvent )
117     {
118       impl = iter.impl;
119     }
120   }
121   return impl;
122 }
123
124 const Internal::KeyEventImpl* GetImplementation( const KeyEvent* keyEvent )
125 {
126   Internal::KeyEventImpl* impl( NULL );
127   for( auto&& iter : gKeyEventToImplMapping )
128   {
129     if( iter.keyEvent == keyEvent )
130     {
131       impl = iter.impl;
132     }
133   }
134   return impl;
135 }
136
137 } // namespace Dali