1 //
2 // Copyright 2015 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 // X11Window.cpp: Implementation of OSWindow for X11
8
9 #include "util/x11/X11Window.h"
10
11 #include "common/debug.h"
12 #include "util/Timer.h"
13 #include "util/test_utils.h"
14
15 namespace
16 {
17
WaitForMapNotify(Display * dpy,XEvent * event,XPointer window)18 Bool WaitForMapNotify(Display *dpy, XEvent *event, XPointer window)
19 {
20 return event->type == MapNotify && event->xmap.window == reinterpret_cast<Window>(window);
21 }
22
X11CodeToKey(Display * display,unsigned int scancode)23 static Key X11CodeToKey(Display *display, unsigned int scancode)
24 {
25 int temp;
26 KeySym *keySymbols;
27 keySymbols = XGetKeyboardMapping(display, scancode, 1, &temp);
28
29 KeySym keySymbol = keySymbols[0];
30 XFree(keySymbols);
31
32 switch (keySymbol)
33 {
34 case XK_Shift_L:
35 return KEY_LSHIFT;
36 case XK_Shift_R:
37 return KEY_RSHIFT;
38 case XK_Alt_L:
39 return KEY_LALT;
40 case XK_Alt_R:
41 return KEY_RALT;
42 case XK_Control_L:
43 return KEY_LCONTROL;
44 case XK_Control_R:
45 return KEY_RCONTROL;
46 case XK_Super_L:
47 return KEY_LSYSTEM;
48 case XK_Super_R:
49 return KEY_RSYSTEM;
50 case XK_Menu:
51 return KEY_MENU;
52
53 case XK_semicolon:
54 return KEY_SEMICOLON;
55 case XK_slash:
56 return KEY_SLASH;
57 case XK_equal:
58 return KEY_EQUAL;
59 case XK_minus:
60 return KEY_DASH;
61 case XK_bracketleft:
62 return KEY_LBRACKET;
63 case XK_bracketright:
64 return KEY_RBRACKET;
65 case XK_comma:
66 return KEY_COMMA;
67 case XK_period:
68 return KEY_PERIOD;
69 case XK_backslash:
70 return KEY_BACKSLASH;
71 case XK_asciitilde:
72 return KEY_TILDE;
73 case XK_Escape:
74 return KEY_ESCAPE;
75 case XK_space:
76 return KEY_SPACE;
77 case XK_Return:
78 return KEY_RETURN;
79 case XK_BackSpace:
80 return KEY_BACK;
81 case XK_Tab:
82 return KEY_TAB;
83 case XK_Page_Up:
84 return KEY_PAGEUP;
85 case XK_Page_Down:
86 return KEY_PAGEDOWN;
87 case XK_End:
88 return KEY_END;
89 case XK_Home:
90 return KEY_HOME;
91 case XK_Insert:
92 return KEY_INSERT;
93 case XK_Delete:
94 return KEY_DELETE;
95 case XK_KP_Add:
96 return KEY_ADD;
97 case XK_KP_Subtract:
98 return KEY_SUBTRACT;
99 case XK_KP_Multiply:
100 return KEY_MULTIPLY;
101 case XK_KP_Divide:
102 return KEY_DIVIDE;
103 case XK_Pause:
104 return KEY_PAUSE;
105
106 case XK_F1:
107 return KEY_F1;
108 case XK_F2:
109 return KEY_F2;
110 case XK_F3:
111 return KEY_F3;
112 case XK_F4:
113 return KEY_F4;
114 case XK_F5:
115 return KEY_F5;
116 case XK_F6:
117 return KEY_F6;
118 case XK_F7:
119 return KEY_F7;
120 case XK_F8:
121 return KEY_F8;
122 case XK_F9:
123 return KEY_F9;
124 case XK_F10:
125 return KEY_F10;
126 case XK_F11:
127 return KEY_F11;
128 case XK_F12:
129 return KEY_F12;
130 case XK_F13:
131 return KEY_F13;
132 case XK_F14:
133 return KEY_F14;
134 case XK_F15:
135 return KEY_F15;
136
137 case XK_Left:
138 return KEY_LEFT;
139 case XK_Right:
140 return KEY_RIGHT;
141 case XK_Down:
142 return KEY_DOWN;
143 case XK_Up:
144 return KEY_UP;
145
146 case XK_KP_Insert:
147 return KEY_NUMPAD0;
148 case XK_KP_End:
149 return KEY_NUMPAD1;
150 case XK_KP_Down:
151 return KEY_NUMPAD2;
152 case XK_KP_Page_Down:
153 return KEY_NUMPAD3;
154 case XK_KP_Left:
155 return KEY_NUMPAD4;
156 case XK_KP_5:
157 return KEY_NUMPAD5;
158 case XK_KP_Right:
159 return KEY_NUMPAD6;
160 case XK_KP_Home:
161 return KEY_NUMPAD7;
162 case XK_KP_Up:
163 return KEY_NUMPAD8;
164 case XK_KP_Page_Up:
165 return KEY_NUMPAD9;
166
167 case XK_a:
168 return KEY_A;
169 case XK_b:
170 return KEY_B;
171 case XK_c:
172 return KEY_C;
173 case XK_d:
174 return KEY_D;
175 case XK_e:
176 return KEY_E;
177 case XK_f:
178 return KEY_F;
179 case XK_g:
180 return KEY_G;
181 case XK_h:
182 return KEY_H;
183 case XK_i:
184 return KEY_I;
185 case XK_j:
186 return KEY_J;
187 case XK_k:
188 return KEY_K;
189 case XK_l:
190 return KEY_L;
191 case XK_m:
192 return KEY_M;
193 case XK_n:
194 return KEY_N;
195 case XK_o:
196 return KEY_O;
197 case XK_p:
198 return KEY_P;
199 case XK_q:
200 return KEY_Q;
201 case XK_r:
202 return KEY_R;
203 case XK_s:
204 return KEY_S;
205 case XK_t:
206 return KEY_T;
207 case XK_u:
208 return KEY_U;
209 case XK_v:
210 return KEY_V;
211 case XK_w:
212 return KEY_W;
213 case XK_x:
214 return KEY_X;
215 case XK_y:
216 return KEY_Y;
217 case XK_z:
218 return KEY_Z;
219
220 case XK_1:
221 return KEY_NUM1;
222 case XK_2:
223 return KEY_NUM2;
224 case XK_3:
225 return KEY_NUM3;
226 case XK_4:
227 return KEY_NUM4;
228 case XK_5:
229 return KEY_NUM5;
230 case XK_6:
231 return KEY_NUM6;
232 case XK_7:
233 return KEY_NUM7;
234 case XK_8:
235 return KEY_NUM8;
236 case XK_9:
237 return KEY_NUM9;
238 case XK_0:
239 return KEY_NUM0;
240 }
241
242 return Key(0);
243 }
244
AddX11KeyStateToEvent(Event * event,unsigned int state)245 static void AddX11KeyStateToEvent(Event *event, unsigned int state)
246 {
247 event->Key.Shift = state & ShiftMask;
248 event->Key.Control = state & ControlMask;
249 event->Key.Alt = state & Mod1Mask;
250 event->Key.System = state & Mod4Mask;
251 }
252
253 } // namespace
254
X11Window()255 X11Window::X11Window()
256 : WM_DELETE_WINDOW(None),
257 WM_PROTOCOLS(None),
258 TEST_EVENT(None),
259 mDisplay(nullptr),
260 mWindow(0),
261 mRequestedVisualId(-1),
262 mVisible(false)
263 {}
264
X11Window(int visualId)265 X11Window::X11Window(int visualId)
266 : WM_DELETE_WINDOW(None),
267 WM_PROTOCOLS(None),
268 TEST_EVENT(None),
269 mDisplay(nullptr),
270 mWindow(0),
271 mRequestedVisualId(visualId),
272 mVisible(false)
273 {}
274
~X11Window()275 X11Window::~X11Window()
276 {
277 destroy();
278 }
279
initializeImpl(const std::string & name,int width,int height)280 bool X11Window::initializeImpl(const std::string &name, int width, int height)
281 {
282 destroy();
283
284 mDisplay = XOpenDisplay(nullptr);
285 if (!mDisplay)
286 {
287 return false;
288 }
289
290 {
291 int screen = DefaultScreen(mDisplay);
292 Window root = RootWindow(mDisplay, screen);
293
294 Visual *visual;
295 if (mRequestedVisualId == -1)
296 {
297 visual = DefaultVisual(mDisplay, screen);
298 }
299 else
300 {
301 XVisualInfo visualTemplate;
302 visualTemplate.visualid = mRequestedVisualId;
303
304 int numVisuals = 0;
305 XVisualInfo *visuals =
306 XGetVisualInfo(mDisplay, VisualIDMask, &visualTemplate, &numVisuals);
307 if (numVisuals <= 0)
308 {
309 return false;
310 }
311 ASSERT(numVisuals == 1);
312
313 visual = visuals[0].visual;
314 XFree(visuals);
315 }
316
317 int depth = DefaultDepth(mDisplay, screen);
318 Colormap colormap = XCreateColormap(mDisplay, root, visual, AllocNone);
319
320 XSetWindowAttributes attributes;
321 unsigned long attributeMask = CWBorderPixel | CWColormap | CWEventMask;
322
323 attributes.event_mask = StructureNotifyMask | PointerMotionMask | ButtonPressMask |
324 ButtonReleaseMask | FocusChangeMask | EnterWindowMask |
325 LeaveWindowMask | KeyPressMask | KeyReleaseMask;
326 attributes.border_pixel = 0;
327 attributes.colormap = colormap;
328
329 mWindow = XCreateWindow(mDisplay, root, 0, 0, width, height, 0, depth, InputOutput, visual,
330 attributeMask, &attributes);
331 XFreeColormap(mDisplay, colormap);
332 }
333
334 if (!mWindow)
335 {
336 destroy();
337 return false;
338 }
339
340 // Tell the window manager to notify us when the user wants to close the
341 // window so we can do it ourselves.
342 WM_DELETE_WINDOW = XInternAtom(mDisplay, "WM_DELETE_WINDOW", False);
343 WM_PROTOCOLS = XInternAtom(mDisplay, "WM_PROTOCOLS", False);
344 if (WM_DELETE_WINDOW == None || WM_PROTOCOLS == None)
345 {
346 destroy();
347 return false;
348 }
349
350 if (XSetWMProtocols(mDisplay, mWindow, &WM_DELETE_WINDOW, 1) == 0)
351 {
352 destroy();
353 return false;
354 }
355
356 // Create an atom to identify our test event
357 TEST_EVENT = XInternAtom(mDisplay, "ANGLE_TEST_EVENT", False);
358 if (TEST_EVENT == None)
359 {
360 destroy();
361 return false;
362 }
363
364 XFlush(mDisplay);
365
366 mX = 0;
367 mY = 0;
368 mWidth = width;
369 mHeight = height;
370
371 return true;
372 }
373
disableErrorMessageDialog()374 void X11Window::disableErrorMessageDialog() {}
375
destroy()376 void X11Window::destroy()
377 {
378 if (mWindow)
379 {
380 XDestroyWindow(mDisplay, mWindow);
381 mWindow = 0;
382 }
383 if (mDisplay)
384 {
385 XCloseDisplay(mDisplay);
386 mDisplay = nullptr;
387 }
388 WM_DELETE_WINDOW = None;
389 WM_PROTOCOLS = None;
390 }
391
resetNativeWindow()392 void X11Window::resetNativeWindow() {}
393
getNativeWindow() const394 EGLNativeWindowType X11Window::getNativeWindow() const
395 {
396 return mWindow;
397 }
398
getNativeDisplay() const399 EGLNativeDisplayType X11Window::getNativeDisplay() const
400 {
401 return reinterpret_cast<EGLNativeDisplayType>(mDisplay);
402 }
403
messageLoop()404 void X11Window::messageLoop()
405 {
406 int eventCount = XPending(mDisplay);
407 while (eventCount--)
408 {
409 XEvent event;
410 XNextEvent(mDisplay, &event);
411 processEvent(event);
412 }
413 }
414
setMousePosition(int x,int y)415 void X11Window::setMousePosition(int x, int y)
416 {
417 XWarpPointer(mDisplay, None, mWindow, 0, 0, 0, 0, x, y);
418 }
419
setOrientation(int width,int height)420 bool X11Window::setOrientation(int width, int height)
421 {
422 UNIMPLEMENTED();
423 return false;
424 }
425
setPosition(int x,int y)426 bool X11Window::setPosition(int x, int y)
427 {
428 XMoveWindow(mDisplay, mWindow, x, y);
429 XFlush(mDisplay);
430 return true;
431 }
432
resize(int width,int height)433 bool X11Window::resize(int width, int height)
434 {
435 XResizeWindow(mDisplay, mWindow, width, height);
436 XFlush(mDisplay);
437
438 Timer timer;
439 timer.start();
440
441 // Wait until the window has actually been resized so that the code calling resize
442 // can assume the window has been resized.
443 const double kResizeWaitDelay = 0.2;
444 while ((mHeight != height || mWidth != width) &&
445 timer.getElapsedWallClockTime() < kResizeWaitDelay)
446 {
447 messageLoop();
448 angle::Sleep(10);
449 }
450
451 return true;
452 }
453
setVisible(bool isVisible)454 void X11Window::setVisible(bool isVisible)
455 {
456 if (mVisible == isVisible)
457 {
458 return;
459 }
460
461 if (isVisible)
462 {
463 XMapWindow(mDisplay, mWindow);
464
465 // Wait until we get an event saying this window is mapped so that the
466 // code calling setVisible can assume the window is visible.
467 // This is important when creating a framebuffer as the framebuffer content
468 // is undefined when the window is not visible.
469 XEvent placeholderEvent;
470 XIfEvent(mDisplay, &placeholderEvent, WaitForMapNotify,
471 reinterpret_cast<XPointer>(mWindow));
472 }
473 else
474 {
475 XUnmapWindow(mDisplay, mWindow);
476 XFlush(mDisplay);
477 }
478 mVisible = isVisible;
479 }
480
signalTestEvent()481 void X11Window::signalTestEvent()
482 {
483 XEvent event;
484 event.type = ClientMessage;
485 event.xclient.message_type = TEST_EVENT;
486 // Format needs to be valid or a BadValue is generated
487 event.xclient.format = 32;
488
489 // Hijack StructureNotifyMask as we know we will be listening for it.
490 XSendEvent(mDisplay, mWindow, False, StructureNotifyMask, &event);
491
492 // For test events, the tests want to check that it really did arrive, and they don't wait
493 // long. XSync here makes sure the event is sent by the time the messageLoop() is called.
494 XSync(mDisplay, false);
495 }
496
processEvent(const XEvent & xEvent)497 void X11Window::processEvent(const XEvent &xEvent)
498 {
499 // TODO(cwallez) text events
500 switch (xEvent.type)
501 {
502 case ButtonPress:
503 {
504 Event event;
505 MouseButton button = MOUSEBUTTON_UNKNOWN;
506 int wheelY = 0;
507
508 // The mouse wheel updates are sent via button events.
509 switch (xEvent.xbutton.button)
510 {
511 case Button4:
512 wheelY = 1;
513 break;
514 case Button5:
515 wheelY = -1;
516 break;
517 case 6:
518 break;
519 case 7:
520 break;
521
522 case Button1:
523 button = MOUSEBUTTON_LEFT;
524 break;
525 case Button2:
526 button = MOUSEBUTTON_MIDDLE;
527 break;
528 case Button3:
529 button = MOUSEBUTTON_RIGHT;
530 break;
531 case 8:
532 button = MOUSEBUTTON_BUTTON4;
533 break;
534 case 9:
535 button = MOUSEBUTTON_BUTTON5;
536 break;
537
538 default:
539 break;
540 }
541
542 if (wheelY != 0)
543 {
544 event.Type = Event::EVENT_MOUSE_WHEEL_MOVED;
545 event.MouseWheel.Delta = wheelY;
546 pushEvent(event);
547 }
548
549 if (button != MOUSEBUTTON_UNKNOWN)
550 {
551 event.Type = Event::EVENT_MOUSE_BUTTON_RELEASED;
552 event.MouseButton.Button = button;
553 event.MouseButton.X = xEvent.xbutton.x;
554 event.MouseButton.Y = xEvent.xbutton.y;
555 pushEvent(event);
556 }
557 }
558 break;
559
560 case ButtonRelease:
561 {
562 Event event;
563 MouseButton button = MOUSEBUTTON_UNKNOWN;
564
565 switch (xEvent.xbutton.button)
566 {
567 case Button1:
568 button = MOUSEBUTTON_LEFT;
569 break;
570 case Button2:
571 button = MOUSEBUTTON_MIDDLE;
572 break;
573 case Button3:
574 button = MOUSEBUTTON_RIGHT;
575 break;
576 case 8:
577 button = MOUSEBUTTON_BUTTON4;
578 break;
579 case 9:
580 button = MOUSEBUTTON_BUTTON5;
581 break;
582
583 default:
584 break;
585 }
586
587 if (button != MOUSEBUTTON_UNKNOWN)
588 {
589 event.Type = Event::EVENT_MOUSE_BUTTON_RELEASED;
590 event.MouseButton.Button = button;
591 event.MouseButton.X = xEvent.xbutton.x;
592 event.MouseButton.Y = xEvent.xbutton.y;
593 pushEvent(event);
594 }
595 }
596 break;
597
598 case KeyPress:
599 {
600 Event event;
601 event.Type = Event::EVENT_KEY_PRESSED;
602 event.Key.Code = X11CodeToKey(mDisplay, xEvent.xkey.keycode);
603 AddX11KeyStateToEvent(&event, xEvent.xkey.state);
604 pushEvent(event);
605 }
606 break;
607
608 case KeyRelease:
609 {
610 Event event;
611 event.Type = Event::EVENT_KEY_RELEASED;
612 event.Key.Code = X11CodeToKey(mDisplay, xEvent.xkey.keycode);
613 AddX11KeyStateToEvent(&event, xEvent.xkey.state);
614 pushEvent(event);
615 }
616 break;
617
618 case EnterNotify:
619 {
620 Event event;
621 event.Type = Event::EVENT_MOUSE_ENTERED;
622 pushEvent(event);
623 }
624 break;
625
626 case LeaveNotify:
627 {
628 Event event;
629 event.Type = Event::EVENT_MOUSE_LEFT;
630 pushEvent(event);
631 }
632 break;
633
634 case MotionNotify:
635 {
636 Event event;
637 event.Type = Event::EVENT_MOUSE_MOVED;
638 event.MouseMove.X = xEvent.xmotion.x;
639 event.MouseMove.Y = xEvent.xmotion.y;
640 pushEvent(event);
641 }
642 break;
643
644 case ConfigureNotify:
645 {
646 if (xEvent.xconfigure.width != mWidth || xEvent.xconfigure.height != mHeight)
647 {
648 Event event;
649 event.Type = Event::EVENT_RESIZED;
650 event.Size.Width = xEvent.xconfigure.width;
651 event.Size.Height = xEvent.xconfigure.height;
652 pushEvent(event);
653 }
654 if (xEvent.xconfigure.x != mX || xEvent.xconfigure.y != mY)
655 {
656 // Sometimes, the window manager reparents our window (for example
657 // when resizing) then the X and Y coordinates will be with respect to
658 // the new parent and not what the user wants to know. Use
659 // XTranslateCoordinates to get the coordinates on the screen.
660 int screen = DefaultScreen(mDisplay);
661 Window root = RootWindow(mDisplay, screen);
662
663 int x, y;
664 Window child;
665 XTranslateCoordinates(mDisplay, mWindow, root, 0, 0, &x, &y, &child);
666
667 if (x != mX || y != mY)
668 {
669 Event event;
670 event.Type = Event::EVENT_MOVED;
671 event.Move.X = x;
672 event.Move.Y = y;
673 pushEvent(event);
674 }
675 }
676 }
677 break;
678
679 case FocusIn:
680 if (xEvent.xfocus.mode == NotifyNormal || xEvent.xfocus.mode == NotifyWhileGrabbed)
681 {
682 Event event;
683 event.Type = Event::EVENT_GAINED_FOCUS;
684 pushEvent(event);
685 }
686 break;
687
688 case FocusOut:
689 if (xEvent.xfocus.mode == NotifyNormal || xEvent.xfocus.mode == NotifyWhileGrabbed)
690 {
691 Event event;
692 event.Type = Event::EVENT_LOST_FOCUS;
693 pushEvent(event);
694 }
695 break;
696
697 case DestroyNotify:
698 // We already received WM_DELETE_WINDOW
699 break;
700
701 case ClientMessage:
702 if (xEvent.xclient.message_type == WM_PROTOCOLS &&
703 static_cast<Atom>(xEvent.xclient.data.l[0]) == WM_DELETE_WINDOW)
704 {
705 Event event;
706 event.Type = Event::EVENT_CLOSED;
707 pushEvent(event);
708 }
709 else if (xEvent.xclient.message_type == TEST_EVENT)
710 {
711 Event event;
712 event.Type = Event::EVENT_TEST;
713 pushEvent(event);
714 }
715 break;
716 }
717 }
718
719 // static
New()720 OSWindow *OSWindow::New()
721 {
722 return new X11Window();
723 }
724