[3.0] Fix build error
[platform/core/uifw/dali-adaptor.git] / adaptors / common / key-impl.cpp
1 /*
2  * Copyright (c) 2014 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 "key-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <map>
23 #include <string.h>
24 #include <iostream>
25
26 #include <dali/integration-api/debug.h>
27
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace KeyLookup
39 {
40
41 namespace
42 {
43
44 class KeyMap
45 {
46   public:
47
48   KeyMap():
49   mLookup( cmpString )
50   {
51     // create the lookup
52     for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
53     {
54       const KeyLookup&  keyLookup( KeyLookupTable[i] );
55       mLookup[ keyLookup.keyName  ] = DaliKeyType( keyLookup.daliKeyCode, keyLookup.deviceButton );
56     }
57   }
58
59   int GetDaliKeyEnum( const char* keyName ) const
60   {
61     Lookup::const_iterator i = mLookup.find( keyName );
62     if( i == mLookup.end() )
63     {
64       return -1;
65     }
66     else
67     {
68       return (*i).second.first;
69     }
70   }
71
72   const char* GetKeyName( int daliKeyCode ) const
73   {
74     for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
75     {
76       const KeyLookup& keyLookup( KeyLookupTable[i] );
77       if( keyLookup.daliKeyCode == daliKeyCode )
78       {
79         return keyLookup.keyName;
80       }
81     }
82     return NULL;
83   }
84
85   bool IsDeviceButton( const char* keyName ) const
86   {
87     Lookup::const_iterator i = mLookup.find( keyName );
88     if ( i != mLookup.end() )
89     {
90       return (*i).second.second;
91     }
92     return false;
93   }
94
95   private:
96
97   /**
98    * compare function, to compare string by pointer
99    */
100   static bool cmpString( const char* a, const char* b)
101   {
102     return strcmp(a, b) < 0;
103   }
104
105   typedef std::pair< int, bool > DaliKeyType;
106   typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool(*) ( char const* a, char const* b) > Lookup;
107   Lookup mLookup;
108
109 };
110 const KeyMap globalKeyLookup;
111
112 } // un-named name space
113
114 bool IsKey( const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
115 {
116   int key = globalKeyLookup.GetDaliKeyEnum( keyEvent.keyPressedName.c_str() );
117   return daliKey == key;
118 }
119
120 bool IsDeviceButton( const char* keyName )
121 {
122   return globalKeyLookup.IsDeviceButton( keyName );
123 }
124
125 const char* GetKeyName( Dali::KEY daliKey )
126 {
127   return globalKeyLookup.GetKeyName( daliKey );
128 }
129
130 int GetDaliKeyCode( const char* keyName )
131 {
132   return globalKeyLookup.GetDaliKeyEnum( keyName );
133 }
134
135 } // namespace KeyLookup
136
137 } // namespace Adaptor
138
139 } // namespace Internal
140
141 } // namespace Dali