Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / third_party / angle / util / OSWindow.cpp
1 //
2 // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include "OSWindow.h"
8
9 #include <iostream>
10 #include <sstream>
11
12 #include "common/debug.h"
13
14 #ifndef DEBUG_EVENTS
15 #define DEBUG_EVENTS 0
16 #endif
17
18 #if DEBUG_EVENTS
19 static const char *MouseButtonName(MouseButton button)
20 {
21     switch (button)
22     {
23       case MOUSEBUTTON_UNKNOWN:
24         return "Unknown";
25       case MOUSEBUTTON_LEFT:
26         return "Left";
27       case MOUSEBUTTON_RIGHT:
28         return "Right";
29       case MOUSEBUTTON_MIDDLE:
30         return "Middle";
31       case MOUSEBUTTON_BUTTON4:
32         return "Button4";
33       case MOUSEBUTTON_BUTTON5:
34         return "Button5";
35       default:
36         UNREACHABLE();
37         return nullptr;
38     }
39 }
40
41 static const char *KeyName(Key key)
42 {
43     switch (key)
44     {
45       case KEY_UNKNOWN:   return "Unknown";
46       case KEY_A:         return "A";
47       case KEY_B:         return "B";
48       case KEY_C:         return "C";
49       case KEY_D:         return "D";
50       case KEY_E:         return "E";
51       case KEY_F:         return "F";
52       case KEY_G:         return "G";
53       case KEY_H:         return "H";
54       case KEY_I:         return "I";
55       case KEY_J:         return "J";
56       case KEY_K:         return "K";
57       case KEY_L:         return "L";
58       case KEY_M:         return "M";
59       case KEY_N:         return "N";
60       case KEY_O:         return "O";
61       case KEY_P:         return "P";
62       case KEY_Q:         return "Q";
63       case KEY_R:         return "R";
64       case KEY_S:         return "S";
65       case KEY_T:         return "T";
66       case KEY_U:         return "U";
67       case KEY_V:         return "V";
68       case KEY_W:         return "W";
69       case KEY_X:         return "X";
70       case KEY_Y:         return "Y";
71       case KEY_Z:         return "Z";
72       case KEY_NUM0:      return "Num0";
73       case KEY_NUM1:      return "Num1";
74       case KEY_NUM2:      return "Num2";
75       case KEY_NUM3:      return "Num3";
76       case KEY_NUM4:      return "Num4";
77       case KEY_NUM5:      return "Num5";
78       case KEY_NUM6:      return "Num6";
79       case KEY_NUM7:      return "Num7";
80       case KEY_NUM8:      return "Num8";
81       case KEY_NUM9:      return "Num9";
82       case KEY_ESCAPE:    return "Escape";
83       case KEY_LCONTROL:  return "Left Control";
84       case KEY_LSHIFT:    return "Left Shift";
85       case KEY_LALT:      return "Left Alt";
86       case KEY_LSYSTEM:   return "Left System";
87       case KEY_RCONTROL:  return "Right Control";
88       case KEY_RSHIFT:    return "Right Shift";
89       case KEY_RALT:      return "Right Alt";
90       case KEY_RSYSTEM:   return "Right System";
91       case KEY_MENU:      return "Menu";
92       case KEY_LBRACKET:  return "Left Bracket";
93       case KEY_RBRACKET:  return "Right Bracket";
94       case KEY_SEMICOLON: return "Semicolon";
95       case KEY_COMMA:     return "Comma";
96       case KEY_PERIOD:    return "Period";
97       case KEY_QUOTE:     return "Quote";
98       case KEY_SLASH:     return "Slash";
99       case KEY_BACKSLASH: return "Backslash";
100       case KEY_TILDE:     return "Tilde";
101       case KEY_EQUAL:     return "Equal";
102       case KEY_DASH:      return "Dash";
103       case KEY_SPACE:     return "Space";
104       case KEY_RETURN:    return "Return";
105       case KEY_BACK:      return "Back";
106       case KEY_TAB:       return "Tab";
107       case KEY_PAGEUP:    return "Page Up";
108       case KEY_PAGEDOWN:  return "Page Down";
109       case KEY_END:       return "End";
110       case KEY_HOME:      return "Home";
111       case KEY_INSERT:    return "Insert";
112       case KEY_DELETE:    return "Delete";
113       case KEY_ADD:       return "Add";
114       case KEY_SUBTRACT:  return "Substract";
115       case KEY_MULTIPLY:  return "Multiply";
116       case KEY_DIVIDE:    return "Divide";
117       case KEY_LEFT:      return "Left";
118       case KEY_RIGHT:     return "Right";
119       case KEY_UP:        return "Up";
120       case KEY_DOWN:      return "Down";
121       case KEY_NUMPAD0:   return "Numpad 0";
122       case KEY_NUMPAD1:   return "Numpad 1";
123       case KEY_NUMPAD2:   return "Numpad 2";
124       case KEY_NUMPAD3:   return "Numpad 3";
125       case KEY_NUMPAD4:   return "Numpad 4";
126       case KEY_NUMPAD5:   return "Numpad 5";
127       case KEY_NUMPAD6:   return "Numpad 6";
128       case KEY_NUMPAD7:   return "Numpad 7";
129       case KEY_NUMPAD8:   return "Numpad 8";
130       case KEY_NUMPAD9:   return "Numpad 9";
131       case KEY_F1:        return "F1";
132       case KEY_F2:        return "F2";
133       case KEY_F3:        return "F3";
134       case KEY_F4:        return "F4";
135       case KEY_F5:        return "F5";
136       case KEY_F6:        return "F6";
137       case KEY_F7:        return "F7";
138       case KEY_F8:        return "F8";
139       case KEY_F9:        return "F9";
140       case KEY_F10:       return "F10";
141       case KEY_F11:       return "F11";
142       case KEY_F12:       return "F12";
143       case KEY_F13:       return "F13";
144       case KEY_F14:       return "F14";
145       case KEY_F15:       return "F15";
146       case KEY_PAUSE:     return "Pause";
147       default:            return "Unknown Key";
148     }
149 }
150
151 static std::string KeyState(const Event::KeyEvent &event)
152 {
153     if (event.Shift || event.Control || event.Alt || event.System)
154     {
155         std::ostringstream buffer;
156         buffer << " [";
157
158         if (event.Shift)
159         {
160             buffer << "Shift";
161         }
162         if (event.Control)
163         {
164             buffer << "Control";
165         }
166         if (event.Alt)
167         {
168             buffer << "Alt";
169         }
170         if (event.System)
171         {
172             buffer << "System";
173         }
174
175         buffer << "]";
176         return buffer.str();
177     }
178     return "";
179 }
180
181 static void PrintEvent(const Event& event)
182 {
183     switch (event.Type)
184     {
185       case Event::EVENT_CLOSED:
186         std::cout << "Event: Window Closed" << std::endl;
187         break;
188       case Event::EVENT_MOVED:
189         std::cout << "Event: Window Moved (" << event.Move.X
190                   << ", " << event.Move.Y << ")" << std::endl;
191         break;
192       case Event::EVENT_RESIZED:
193         std::cout << "Event: Window Resized (" << event.Size.Width
194                   << ", " << event.Size.Height << ")" << std::endl;
195         break;
196       case Event::EVENT_LOST_FOCUS:
197         std::cout << "Event: Window Lost Focus" << std::endl;
198         break;
199       case Event::EVENT_GAINED_FOCUS:
200         std::cout << "Event: Window Gained Focus" << std::endl;
201         break;
202       case Event::EVENT_TEXT_ENTERED:
203         // TODO(cwallez) show the character
204         std::cout << "Event: Text Entered" << std::endl;
205         break;
206       case Event::EVENT_KEY_PRESSED:
207         std::cout << "Event: Key Pressed (" << KeyName(event.Key.Code) << KeyState(event.Key) << ")" << std::endl;
208         break;
209       case Event::EVENT_KEY_RELEASED:
210         std::cout << "Event: Key Released (" << KeyName(event.Key.Code) << KeyState(event.Key) << ")" << std::endl;
211         break;
212       case Event::EVENT_MOUSE_WHEEL_MOVED:
213         std::cout << "Event: Mouse Wheel (" << event.MouseWheel.Delta << ")" << std::endl;
214         break;
215       case Event::EVENT_MOUSE_BUTTON_PRESSED:
216         std::cout << "Event: Mouse Button Pressed " << MouseButtonName(event.MouseButton.Button) <<
217                   " at (" << event.MouseButton.X << ", " << event.MouseButton.Y << ")" << std::endl;
218         break;
219       case Event::EVENT_MOUSE_BUTTON_RELEASED:
220         std::cout << "Event: Mouse Button Released " << MouseButtonName(event.MouseButton.Button) <<
221                   " at (" << event.MouseButton.X << ", " << event.MouseButton.Y << ")" << std::endl;
222         break;
223       case Event::EVENT_MOUSE_MOVED:
224         std::cout << "Event: Mouse Moved (" << event.MouseMove.X
225                   << ", " << event.MouseMove.Y << ")" << std::endl;
226         break;
227       case Event::EVENT_MOUSE_ENTERED:
228         std::cout << "Event: Mouse Entered Window" << std::endl;
229         break;
230       case Event::EVENT_MOUSE_LEFT:
231         std::cout << "Event: Mouse Left Window" << std::endl;
232         break;
233       case Event::EVENT_TEST:
234         std::cout << "Event: Test" << std::endl;
235         break;
236       default:
237         UNREACHABLE();
238         break;
239     }
240 }
241 #endif
242
243 OSWindow::OSWindow()
244     : mX(0),
245       mY(0),
246       mWidth(0),
247       mHeight(0)
248 {
249 }
250
251 OSWindow::~OSWindow()
252 {}
253
254 int OSWindow::getX() const
255 {
256     return mX;
257 }
258
259 int OSWindow::getY() const
260 {
261     return mY;
262 }
263
264 int OSWindow::getWidth() const
265 {
266     return mWidth;
267 }
268
269 int OSWindow::getHeight() const
270 {
271     return mHeight;
272 }
273
274 bool OSWindow::takeScreenshot(uint8_t *pixelData)
275 {
276     return false;
277 }
278
279 bool OSWindow::popEvent(Event *event)
280 {
281     if (mEvents.size() > 0 && event)
282     {
283         *event = mEvents.front();
284         mEvents.pop_front();
285         return true;
286     }
287     else
288     {
289         return false;
290     }
291 }
292
293 void OSWindow::pushEvent(Event event)
294 {
295     switch (event.Type)
296     {
297       case Event::EVENT_MOVED:
298         mX = event.Move.X;
299         mY = event.Move.Y;
300         break;
301       case Event::EVENT_RESIZED:
302         mWidth = event.Size.Width;
303         mHeight = event.Size.Height;
304         break;
305       default:
306         break;
307     }
308
309     mEvents.push_back(event);
310
311 #if DEBUG_EVENTS
312     PrintEvent(event);
313 #endif
314 }
315
316 bool OSWindow::didTestEventFire()
317 {
318     Event topEvent;
319     while (popEvent(&topEvent))
320     {
321         if (topEvent.Type == Event::EVENT_TEST)
322         {
323             return true;
324         }
325     }
326
327     return false;
328 }