Merge "Implemented the Handle assignment operators properly" into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / key-impl-wl.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
27 #include <dali/integration-api/debug.h>
28
29
30 namespace Dali
31 {
32
33 const KEY DALI_KEY_INVALID          = -1;
34 const KEY DALI_KEY_ESCAPE           = 9;
35 const KEY DALI_KEY_BACK             = 166;
36 const KEY DALI_KEY_CAMERA           = 167;
37 const KEY DALI_KEY_CONFIG           = 168;
38 const KEY DALI_KEY_POWER            = 169;
39 const KEY DALI_KEY_PAUSE            = 170;
40 const KEY DALI_KEY_CANCEL           = 171;
41 const KEY DALI_KEY_PLAY_CD          = 172;
42 const KEY DALI_KEY_STOP_CD          = 173;
43 const KEY DALI_KEY_PAUSE_CD         = 174;
44 const KEY DALI_KEY_NEXT_SONG        = 175;
45 const KEY DALI_KEY_PREVIOUS_SONG    = 176;
46 const KEY DALI_KEY_REWIND           = 177;
47 const KEY DALI_KEY_FASTFORWARD      = 178;
48 const KEY DALI_KEY_MEDIA            = 179;
49 const KEY DALI_KEY_PLAY_PAUSE       = 180;
50 const KEY DALI_KEY_MUTE             = 181;
51 const KEY DALI_KEY_SEND             = 182;
52 const KEY DALI_KEY_SELECT           = 183;
53 const KEY DALI_KEY_END              = DALI_KEY_BACK;
54 const KEY DALI_KEY_MENU             = DALI_KEY_SEND;
55 const KEY DALI_KEY_HOME             = DALI_KEY_SELECT;
56 const KEY DALI_KEY_HOMEPAGE         = 187;
57 const KEY DALI_KEY_WEBPAGE          = 188;
58 const KEY DALI_KEY_MAIL             = 189;
59 const KEY DALI_KEY_SCREENSAVER      = 190;
60 const KEY DALI_KEY_BRIGHTNESS_UP    = 191;
61 const KEY DALI_KEY_BRIGHTNESS_DOWN  = 192;
62 const KEY DALI_KEY_SOFT_KBD         = 193;
63 const KEY DALI_KEY_QUICK_PANEL      = 194;
64 const KEY DALI_KEY_TASK_SWITCH      = 195;
65 const KEY DALI_KEY_APPS             = 196;
66 const KEY DALI_KEY_SEARCH           = 197;
67 const KEY DALI_KEY_VOICE            = 198;
68 const KEY DALI_KEY_LANGUAGE         = 199;
69 const KEY DALI_KEY_VOLUME_UP        = 200;
70 const KEY DALI_KEY_VOLUME_DOWN      = 201;
71
72 namespace Internal
73 {
74
75 namespace Adaptor
76 {
77
78 namespace KeyLookup
79 {
80
81 namespace
82 {
83
84 struct KeyLookup
85 {
86   const char* keyName;      ///< X string representation
87   const int   daliKeyCode;  ///< Dali Enum Representation
88   const bool  deviceButton; ///< Whether the key is from a button on the device
89 };
90
91 // matches a DALI_KEY enum, to a X key name
92 KeyLookup KeyLookupTable[]=
93 {
94   // more than one key name can be assigned to a single dali-key code
95   // e.g. Menu and KEY_MENU("FS86KeyMenu") are both assigned to  DALI_KEY_MENU
96
97   { "Escape",               DALI_KEY_ESCAPE,          false },  // item not defined in utilX
98   { "Menu",                 DALI_KEY_MENU,            false },  // item not defined in utilX
99 };
100
101 const std::size_t KEY_LOOKUP_COUNT = (sizeof( KeyLookupTable))/ (sizeof(KeyLookup));
102
103 class KeyMap
104 {
105   public:
106
107   KeyMap():
108   mLookup( cmpString )
109   {
110     // create the lookup
111     for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
112     {
113       const KeyLookup&  keyLookup( KeyLookupTable[i] );
114       mLookup[ keyLookup.keyName  ] = DaliKeyType( keyLookup.daliKeyCode, keyLookup.deviceButton );
115     }
116   }
117
118   int GetDaliKeyEnum( const char* keyName ) const
119   {
120     Lookup::const_iterator i = mLookup.find( keyName );
121     if( i == mLookup.end() )
122     {
123       return -1;
124     }
125     else
126     {
127       return (*i).second.first;
128     }
129   }
130
131   bool IsDeviceButton( const char* keyName ) const
132   {
133     Lookup::const_iterator i = mLookup.find( keyName );
134     if ( i != mLookup.end() )
135     {
136       return (*i).second.second;
137     }
138     return false;
139   }
140
141   private:
142
143   /**
144    * compare function, to compare string by pointer
145    */
146   static bool cmpString( const char* a, const char* b)
147   {
148     return strcmp(a, b) < 0;
149   }
150
151   typedef std::pair< int, bool > DaliKeyType;
152   typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool(*) ( char const* a, char const* b) > Lookup;
153   Lookup mLookup;
154
155 };
156 const KeyMap globalKeyLookup;
157
158 } // un-named name space
159
160 bool IsKey( const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
161 {
162   int key = globalKeyLookup.GetDaliKeyEnum( keyEvent.keyPressedName.c_str() );
163   return daliKey == key;
164 }
165
166 bool IsDeviceButton( const char* keyName )
167 {
168   return globalKeyLookup.IsDeviceButton( keyName );
169 }
170
171 } // namespace KeyLookup
172
173 } // namespace Adaptor
174
175 } // namespace Internal
176
177 } // namespace Dali