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