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