• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 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
MouseButtonName(MouseButton button)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 
KeyName(Key key)41 static const char *KeyName(Key key)
42 {
43     switch (key)
44     {
45         case KEY_UNKNOWN:
46             return "Unknown";
47         case KEY_A:
48             return "A";
49         case KEY_B:
50             return "B";
51         case KEY_C:
52             return "C";
53         case KEY_D:
54             return "D";
55         case KEY_E:
56             return "E";
57         case KEY_F:
58             return "F";
59         case KEY_G:
60             return "G";
61         case KEY_H:
62             return "H";
63         case KEY_I:
64             return "I";
65         case KEY_J:
66             return "J";
67         case KEY_K:
68             return "K";
69         case KEY_L:
70             return "L";
71         case KEY_M:
72             return "M";
73         case KEY_N:
74             return "N";
75         case KEY_O:
76             return "O";
77         case KEY_P:
78             return "P";
79         case KEY_Q:
80             return "Q";
81         case KEY_R:
82             return "R";
83         case KEY_S:
84             return "S";
85         case KEY_T:
86             return "T";
87         case KEY_U:
88             return "U";
89         case KEY_V:
90             return "V";
91         case KEY_W:
92             return "W";
93         case KEY_X:
94             return "X";
95         case KEY_Y:
96             return "Y";
97         case KEY_Z:
98             return "Z";
99         case KEY_NUM0:
100             return "Num0";
101         case KEY_NUM1:
102             return "Num1";
103         case KEY_NUM2:
104             return "Num2";
105         case KEY_NUM3:
106             return "Num3";
107         case KEY_NUM4:
108             return "Num4";
109         case KEY_NUM5:
110             return "Num5";
111         case KEY_NUM6:
112             return "Num6";
113         case KEY_NUM7:
114             return "Num7";
115         case KEY_NUM8:
116             return "Num8";
117         case KEY_NUM9:
118             return "Num9";
119         case KEY_ESCAPE:
120             return "Escape";
121         case KEY_LCONTROL:
122             return "Left Control";
123         case KEY_LSHIFT:
124             return "Left Shift";
125         case KEY_LALT:
126             return "Left Alt";
127         case KEY_LSYSTEM:
128             return "Left System";
129         case KEY_RCONTROL:
130             return "Right Control";
131         case KEY_RSHIFT:
132             return "Right Shift";
133         case KEY_RALT:
134             return "Right Alt";
135         case KEY_RSYSTEM:
136             return "Right System";
137         case KEY_MENU:
138             return "Menu";
139         case KEY_LBRACKET:
140             return "Left Bracket";
141         case KEY_RBRACKET:
142             return "Right Bracket";
143         case KEY_SEMICOLON:
144             return "Semicolon";
145         case KEY_COMMA:
146             return "Comma";
147         case KEY_PERIOD:
148             return "Period";
149         case KEY_QUOTE:
150             return "Quote";
151         case KEY_SLASH:
152             return "Slash";
153         case KEY_BACKSLASH:
154             return "Backslash";
155         case KEY_TILDE:
156             return "Tilde";
157         case KEY_EQUAL:
158             return "Equal";
159         case KEY_DASH:
160             return "Dash";
161         case KEY_SPACE:
162             return "Space";
163         case KEY_RETURN:
164             return "Return";
165         case KEY_BACK:
166             return "Back";
167         case KEY_TAB:
168             return "Tab";
169         case KEY_PAGEUP:
170             return "Page Up";
171         case KEY_PAGEDOWN:
172             return "Page Down";
173         case KEY_END:
174             return "End";
175         case KEY_HOME:
176             return "Home";
177         case KEY_INSERT:
178             return "Insert";
179         case KEY_DELETE:
180             return "Delete";
181         case KEY_ADD:
182             return "Add";
183         case KEY_SUBTRACT:
184             return "Substract";
185         case KEY_MULTIPLY:
186             return "Multiply";
187         case KEY_DIVIDE:
188             return "Divide";
189         case KEY_LEFT:
190             return "Left";
191         case KEY_RIGHT:
192             return "Right";
193         case KEY_UP:
194             return "Up";
195         case KEY_DOWN:
196             return "Down";
197         case KEY_NUMPAD0:
198             return "Numpad 0";
199         case KEY_NUMPAD1:
200             return "Numpad 1";
201         case KEY_NUMPAD2:
202             return "Numpad 2";
203         case KEY_NUMPAD3:
204             return "Numpad 3";
205         case KEY_NUMPAD4:
206             return "Numpad 4";
207         case KEY_NUMPAD5:
208             return "Numpad 5";
209         case KEY_NUMPAD6:
210             return "Numpad 6";
211         case KEY_NUMPAD7:
212             return "Numpad 7";
213         case KEY_NUMPAD8:
214             return "Numpad 8";
215         case KEY_NUMPAD9:
216             return "Numpad 9";
217         case KEY_F1:
218             return "F1";
219         case KEY_F2:
220             return "F2";
221         case KEY_F3:
222             return "F3";
223         case KEY_F4:
224             return "F4";
225         case KEY_F5:
226             return "F5";
227         case KEY_F6:
228             return "F6";
229         case KEY_F7:
230             return "F7";
231         case KEY_F8:
232             return "F8";
233         case KEY_F9:
234             return "F9";
235         case KEY_F10:
236             return "F10";
237         case KEY_F11:
238             return "F11";
239         case KEY_F12:
240             return "F12";
241         case KEY_F13:
242             return "F13";
243         case KEY_F14:
244             return "F14";
245         case KEY_F15:
246             return "F15";
247         case KEY_PAUSE:
248             return "Pause";
249         default:
250             return "Unknown Key";
251     }
252 }
253 
KeyState(const Event::KeyEvent & event)254 static std::string KeyState(const Event::KeyEvent &event)
255 {
256     if (event.Shift || event.Control || event.Alt || event.System)
257     {
258         std::ostringstream buffer;
259         buffer << " [";
260 
261         if (event.Shift)
262         {
263             buffer << "Shift";
264         }
265         if (event.Control)
266         {
267             buffer << "Control";
268         }
269         if (event.Alt)
270         {
271             buffer << "Alt";
272         }
273         if (event.System)
274         {
275             buffer << "System";
276         }
277 
278         buffer << "]";
279         return buffer.str();
280     }
281     return "";
282 }
283 
PrintEvent(const Event & event)284 static void PrintEvent(const Event &event)
285 {
286     switch (event.Type)
287     {
288         case Event::EVENT_CLOSED:
289             std::cout << "Event: Window Closed" << std::endl;
290             break;
291         case Event::EVENT_MOVED:
292             std::cout << "Event: Window Moved (" << event.Move.X << ", " << event.Move.Y << ")"
293                       << std::endl;
294             break;
295         case Event::EVENT_RESIZED:
296             std::cout << "Event: Window Resized (" << event.Size.Width << ", " << event.Size.Height
297                       << ")" << std::endl;
298             break;
299         case Event::EVENT_LOST_FOCUS:
300             std::cout << "Event: Window Lost Focus" << std::endl;
301             break;
302         case Event::EVENT_GAINED_FOCUS:
303             std::cout << "Event: Window Gained Focus" << std::endl;
304             break;
305         case Event::EVENT_TEXT_ENTERED:
306             // TODO(cwallez) show the character
307             std::cout << "Event: Text Entered" << std::endl;
308             break;
309         case Event::EVENT_KEY_PRESSED:
310             std::cout << "Event: Key Pressed (" << KeyName(event.Key.Code) << KeyState(event.Key)
311                       << ")" << std::endl;
312             break;
313         case Event::EVENT_KEY_RELEASED:
314             std::cout << "Event: Key Released (" << KeyName(event.Key.Code) << KeyState(event.Key)
315                       << ")" << std::endl;
316             break;
317         case Event::EVENT_MOUSE_WHEEL_MOVED:
318             std::cout << "Event: Mouse Wheel (" << event.MouseWheel.Delta << ")" << std::endl;
319             break;
320         case Event::EVENT_MOUSE_BUTTON_PRESSED:
321             std::cout << "Event: Mouse Button Pressed " << MouseButtonName(event.MouseButton.Button)
322                       << " at (" << event.MouseButton.X << ", " << event.MouseButton.Y << ")"
323                       << std::endl;
324             break;
325         case Event::EVENT_MOUSE_BUTTON_RELEASED:
326             std::cout << "Event: Mouse Button Released "
327                       << MouseButtonName(event.MouseButton.Button) << " at (" << event.MouseButton.X
328                       << ", " << event.MouseButton.Y << ")" << std::endl;
329             break;
330         case Event::EVENT_MOUSE_MOVED:
331             std::cout << "Event: Mouse Moved (" << event.MouseMove.X << ", " << event.MouseMove.Y
332                       << ")" << std::endl;
333             break;
334         case Event::EVENT_MOUSE_ENTERED:
335             std::cout << "Event: Mouse Entered Window" << std::endl;
336             break;
337         case Event::EVENT_MOUSE_LEFT:
338             std::cout << "Event: Mouse Left Window" << std::endl;
339             break;
340         case Event::EVENT_TEST:
341             std::cout << "Event: Test" << std::endl;
342             break;
343         default:
344             UNREACHABLE();
345             break;
346     }
347 }
348 #endif
349 
OSWindow()350 OSWindow::OSWindow() : mX(0), mY(0), mWidth(0), mHeight(0) {}
351 
~OSWindow()352 OSWindow::~OSWindow() {}
353 
getX() const354 int OSWindow::getX() const
355 {
356     return mX;
357 }
358 
getY() const359 int OSWindow::getY() const
360 {
361     return mY;
362 }
363 
getWidth() const364 int OSWindow::getWidth() const
365 {
366     return mWidth;
367 }
368 
getHeight() const369 int OSWindow::getHeight() const
370 {
371     return mHeight;
372 }
373 
takeScreenshot(uint8_t * pixelData)374 bool OSWindow::takeScreenshot(uint8_t *pixelData)
375 {
376     return false;
377 }
378 
popEvent(Event * event)379 bool OSWindow::popEvent(Event *event)
380 {
381     if (mEvents.size() > 0 && event)
382     {
383         *event = mEvents.front();
384         mEvents.pop_front();
385         return true;
386     }
387     else
388     {
389         return false;
390     }
391 }
392 
pushEvent(Event event)393 void OSWindow::pushEvent(Event event)
394 {
395     switch (event.Type)
396     {
397         case Event::EVENT_MOVED:
398             mX = event.Move.X;
399             mY = event.Move.Y;
400             break;
401         case Event::EVENT_RESIZED:
402             mWidth  = event.Size.Width;
403             mHeight = event.Size.Height;
404             break;
405         default:
406             break;
407     }
408 
409     mEvents.push_back(event);
410 
411 #if DEBUG_EVENTS
412     PrintEvent(event);
413 #endif
414 }
415 
didTestEventFire()416 bool OSWindow::didTestEventFire()
417 {
418     Event topEvent;
419     while (popEvent(&topEvent))
420     {
421         if (topEvent.Type == Event::EVENT_TEST)
422         {
423             return true;
424         }
425     }
426 
427     return false;
428 }
429 
430 // static
Delete(OSWindow ** window)431 void OSWindow::Delete(OSWindow **window)
432 {
433     delete *window;
434     *window = nullptr;
435 }
436