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