Revert "[3.0] Change key names according to changes of utilX"
[platform/core/uifw/dali-adaptor.git] / adaptors / ubuntu / key-impl-ubuntu.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 #define KEY_VOLUMEUP            "XF86AudioRaiseVolume"  /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Volume Up' key */
23 #define KEY_VOLUMEDOWN          "XF86AudioLowerVolume"  /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Volume Down' key */
24
25 #define KEY_CAMERA              "XF86WebCam"    /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Half-Press of Camera' key */
26 #define KEY_CONFIG              "XF86Pictures"  /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Full-Press of Camera' key */
27
28 #define KEY_POWER               "XF86PowerOff"  /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Power' key */
29 #define KEY_PAUSE               "XF86Standby"   /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Pause' key */
30 #define KEY_CANCEL              "Cancel"        /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Cancel' key */
31
32 // Earjack/BT Headset/Multimedia keys
33 #define KEY_PLAYCD              "XF86AudioPlay" /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Play Audio' key */
34 #define KEY_STOPCD              "XF86AudioStop" /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Stop Audio' key */
35 #define KEY_PAUSECD             "XF86AudioPause"        /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Pause Audio' key */
36 #define KEY_NEXTSONG            "XF86AudioNext" /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Next Song' key */
37 #define KEY_PREVIOUSSONG        "XF86AudioPrev" /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Previous Song' key */
38 #define KEY_REWIND              "XF86AudioRewind"       /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Rewind Song' key */
39 #define KEY_FASTFORWARD         "XF86AudioForward"      /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Forward Song' key */
40 #define KEY_MEDIA               "XF86AudioMedia"        /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Media' key */
41 #define KEY_PLAYPAUSE           "XF86AudioPlayPause"    /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'PlayPause' key */
42 #define KEY_MUTE                        "XF86AudioMute" /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Mute' key */
43
44 // 3-Touch key
45 #define KEY_SEND                "XF86Send"      /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Send' key */
46 #define KEY_SELECT              "XF86Phone"     /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Home' key */
47 #define KEY_END                 "XF86Stop"      /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'End' key */
48
49 // Renamed 3-Touch key
50 #define KEY_MENU                "XF86Send"      /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Send' key */
51 #define KEY_HOME                "XF86Phone"     /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Home' key */
52 #define KEY_BACK                "XF86Stop"      /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'End' key */
53
54 //Other functions keys
55 #define KEY_HOMEPAGE            "XF86HomePage"  /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'HomePage' key */
56 #define KEY_WEBPAGE             "XF86WWW"       /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'WWW' key */
57 #define KEY_MAIL                        "XF86Mail"      /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Mail' key */
58 #define KEY_SCREENSAVER "XF86ScreenSaver"       /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'ScreenSaver' key */
59 #define KEY_BRIGHTNESSUP        "XF86MonBrightnessUp"   /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'BrightnessUp' key */
60 #define KEY_BRIGHTNESSDOWN      "XF86MonBrightnessDown" /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'BrightnessDown' key */
61 #define KEY_SOFTKBD                     "XF86MenuKB"    /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Soft keyboard(toggle)' key */
62 #define KEY_QUICKPANEL          "XF86Tools"     /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Quick panel(toggle)' key */
63 #define KEY_TASKSWITCH          "XF86TaskPane"  /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Task switcher(toggle)' key */
64 #define KEY_APPS                "XF86Launch0"   /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Apptray(toggle)' key */
65 #define KEY_SEARCH              "XF86Search"            /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Search(toggle)' key */
66 #define KEY_VOICE               "XF86Launch2"           /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Voice(toggle)' key */
67 #define KEY_LANGUAGE            "Hangul"                /**< this macro means the XKeySym (XServer Key Symbol) corresponds to 'Language(toggle)' key */
68
69 #include <map>
70 #include <string.h>
71 #include <iostream>
72
73
74 #include <dali/integration-api/debug.h>
75
76
77 namespace Dali
78 {
79
80 const KEY DALI_KEY_INVALID          = -1;
81 const KEY DALI_KEY_ESCAPE           = 9;
82 const KEY DALI_KEY_BACKSPACE        = 22;
83 const KEY DALI_KEY_CURSOR_UP        = 111;
84 const KEY DALI_KEY_CURSOR_LEFT      = 113;
85 const KEY DALI_KEY_CURSOR_RIGHT     = 114;
86 const KEY DALI_KEY_CURSOR_DOWN      = 116;
87 const KEY DALI_KEY_BACK             = 166;
88 const KEY DALI_KEY_CAMERA           = 167;
89 const KEY DALI_KEY_CONFIG           = 168;
90 const KEY DALI_KEY_POWER            = 169;
91 const KEY DALI_KEY_PAUSE            = 170;
92 const KEY DALI_KEY_CANCEL           = 171;
93 const KEY DALI_KEY_PLAY_CD          = 172;
94 const KEY DALI_KEY_STOP_CD          = 173;
95 const KEY DALI_KEY_PAUSE_CD         = 174;
96 const KEY DALI_KEY_NEXT_SONG        = 175;
97 const KEY DALI_KEY_PREVIOUS_SONG    = 176;
98 const KEY DALI_KEY_REWIND           = 177;
99 const KEY DALI_KEY_FASTFORWARD      = 178;
100 const KEY DALI_KEY_MEDIA            = 179;
101 const KEY DALI_KEY_PLAY_PAUSE       = 180;
102 const KEY DALI_KEY_MUTE             = 181;
103 const KEY DALI_KEY_SEND             = 182;
104 const KEY DALI_KEY_SELECT           = 183;
105 const KEY DALI_KEY_END              = DALI_KEY_BACK;
106 const KEY DALI_KEY_MENU             = DALI_KEY_SEND;
107 const KEY DALI_KEY_HOME             = DALI_KEY_SELECT;
108 const KEY DALI_KEY_HOMEPAGE         = 187;
109 const KEY DALI_KEY_WEBPAGE          = 188;
110 const KEY DALI_KEY_MAIL             = 189;
111 const KEY DALI_KEY_SCREENSAVER      = 190;
112 const KEY DALI_KEY_BRIGHTNESS_UP    = 191;
113 const KEY DALI_KEY_BRIGHTNESS_DOWN  = 192;
114 const KEY DALI_KEY_SOFT_KBD         = 193;
115 const KEY DALI_KEY_QUICK_PANEL      = 194;
116 const KEY DALI_KEY_TASK_SWITCH      = 195;
117 const KEY DALI_KEY_APPS             = 196;
118 const KEY DALI_KEY_SEARCH           = 197;
119 const KEY DALI_KEY_VOICE            = 198;
120 const KEY DALI_KEY_LANGUAGE         = 199;
121 const KEY DALI_KEY_VOLUME_UP        = 200;
122 const KEY DALI_KEY_VOLUME_DOWN      = 201;
123
124 namespace Internal
125 {
126
127 namespace Adaptor
128 {
129
130 namespace KeyLookup
131 {
132
133 namespace
134 {
135
136 struct KeyLookup
137 {
138   const char* keyName;      ///< X string representation
139   const int   daliKeyCode;  ///< Dali Enum Representation
140   const bool  deviceButton; ///< Whether the key is from a button on the device
141 };
142
143 // matches a DALI_KEY enum, to a X key name
144 KeyLookup KeyLookupTable[]=
145 {
146   // more than one key name can be assigned to a single dali-key code
147   // e.g. Menu and KEY_MENU("FS86KeyMenu") are both assigned to  DALI_KEY_MENU
148
149   { "Escape",               DALI_KEY_ESCAPE,          false },  // item not defined in utilX
150   { "Menu",                 DALI_KEY_MENU,            false },  // item not defined in utilX
151   { KEY_CAMERA,             DALI_KEY_CAMERA,          false },
152   { KEY_CONFIG,             DALI_KEY_CONFIG,          false },
153   { KEY_POWER,              DALI_KEY_POWER,           true  },
154   { KEY_PAUSE,              DALI_KEY_PAUSE,           false },
155   { KEY_CANCEL,             DALI_KEY_CANCEL,          false },
156   { KEY_PLAYCD,             DALI_KEY_PLAY_CD,         false },
157   { KEY_STOPCD,             DALI_KEY_STOP_CD,         false },
158   { KEY_PAUSECD,            DALI_KEY_PAUSE_CD,        false },
159   { KEY_NEXTSONG,           DALI_KEY_NEXT_SONG,       false },
160   { KEY_PREVIOUSSONG,       DALI_KEY_PREVIOUS_SONG,   false },
161   { KEY_REWIND,             DALI_KEY_REWIND,          false },
162   { KEY_FASTFORWARD,        DALI_KEY_FASTFORWARD,     false },
163   { KEY_MEDIA,              DALI_KEY_MEDIA,           false },
164   { KEY_PLAYPAUSE,          DALI_KEY_PLAY_PAUSE,      false },
165   { KEY_MUTE,               DALI_KEY_MUTE,            false },
166   { KEY_SEND,               DALI_KEY_SEND,            true  },
167   { KEY_SELECT,             DALI_KEY_SELECT,          true  },
168   { KEY_END,                DALI_KEY_END,             true  },
169   { KEY_MENU,               DALI_KEY_MENU,            true  },
170   { KEY_HOME,               DALI_KEY_HOME,            true  },
171   { KEY_BACK,               DALI_KEY_BACK,            true  },
172   { KEY_HOMEPAGE,           DALI_KEY_HOMEPAGE,        false },
173   { KEY_WEBPAGE,            DALI_KEY_WEBPAGE,         false },
174   { KEY_MAIL,               DALI_KEY_MAIL,            false },
175   { KEY_SCREENSAVER,        DALI_KEY_SCREENSAVER,     false },
176   { KEY_BRIGHTNESSUP,       DALI_KEY_BRIGHTNESS_UP,   false },
177   { KEY_BRIGHTNESSDOWN,     DALI_KEY_BRIGHTNESS_DOWN, false },
178   { KEY_SOFTKBD,            DALI_KEY_SOFT_KBD,        false },
179   { KEY_QUICKPANEL,         DALI_KEY_QUICK_PANEL,     false },
180   { KEY_TASKSWITCH,         DALI_KEY_TASK_SWITCH,     false },
181   { KEY_APPS,               DALI_KEY_APPS,            false },
182   { KEY_SEARCH,             DALI_KEY_SEARCH,          false },
183   { KEY_VOICE,              DALI_KEY_VOICE,           false },
184   { KEY_LANGUAGE,           DALI_KEY_LANGUAGE,        false },
185   { KEY_VOLUMEUP,           DALI_KEY_VOLUME_UP,       true  },
186   { KEY_VOLUMEDOWN,         DALI_KEY_VOLUME_DOWN,     true  },
187 };
188
189 const std::size_t KEY_LOOKUP_COUNT = (sizeof( KeyLookupTable))/ (sizeof(KeyLookup));
190
191 class KeyMap
192 {
193   public:
194
195   KeyMap():
196   mLookup( cmpString )
197   {
198     // create the lookup
199     for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
200     {
201       const KeyLookup&  keyLookup( KeyLookupTable[i] );
202       mLookup[ keyLookup.keyName  ] = DaliKeyType( keyLookup.daliKeyCode, keyLookup.deviceButton );
203     }
204   }
205
206   int GetDaliKeyEnum( const char* keyName ) const
207   {
208     Lookup::const_iterator i = mLookup.find( keyName );
209     if( i == mLookup.end() )
210     {
211       return -1;
212     }
213     else
214     {
215       return (*i).second.first;
216     }
217   }
218
219   const char* GetKeyName( int daliKeyCode ) const
220   {
221     for( size_t i = 0; i < KEY_LOOKUP_COUNT ; ++i )
222     {
223       const KeyLookup& keyLookup( KeyLookupTable[i] );
224       if( keyLookup.daliKeyCode == daliKeyCode )
225       {
226         return keyLookup.keyName;
227       }
228     }
229     return "";
230   }
231
232   bool IsDeviceButton( const char* keyName ) const
233   {
234     Lookup::const_iterator i = mLookup.find( keyName );
235     if ( i != mLookup.end() )
236     {
237       return (*i).second.second;
238     }
239     return false;
240   }
241
242   private:
243
244   /**
245    * compare function, to compare string by pointer
246    */
247   static bool cmpString( const char* a, const char* b)
248   {
249     return strcmp(a, b) < 0;
250   }
251
252   typedef std::pair< int, bool > DaliKeyType;
253   typedef std::map<const char* /* key name */, DaliKeyType /* key code */, bool(*) ( char const* a, char const* b) > Lookup;
254   Lookup mLookup;
255
256 };
257 const KeyMap globalKeyLookup;
258
259 } // un-named name space
260
261 bool IsKey( const Dali::KeyEvent& keyEvent, Dali::KEY daliKey)
262 {
263   int key = globalKeyLookup.GetDaliKeyEnum( keyEvent.keyPressedName.c_str() );
264   return daliKey == key;
265 }
266
267 bool IsDeviceButton( const char* keyName )
268 {
269   return globalKeyLookup.IsDeviceButton( keyName );
270 }
271
272 const char* GetKeyName( Dali::KEY daliKey )
273 {
274   return globalKeyLookup.GetKeyName( daliKey );
275 }
276
277 } // namespace KeyLookup
278
279 } // namespace Adaptor
280
281 } // namespace Internal
282
283 } // namespace Dali