• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/system_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 
initialize(const std::string & name,int width,int height)280 bool X11Window::initialize(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 
destroy()374 void X11Window::destroy()
375 {
376     if (mWindow)
377     {
378         XDestroyWindow(mDisplay, mWindow);
379         mWindow = 0;
380     }
381     if (mDisplay)
382     {
383         XCloseDisplay(mDisplay);
384         mDisplay = nullptr;
385     }
386     WM_DELETE_WINDOW = None;
387     WM_PROTOCOLS     = None;
388 }
389 
resetNativeWindow()390 void X11Window::resetNativeWindow() {}
391 
getNativeWindow() const392 EGLNativeWindowType X11Window::getNativeWindow() const
393 {
394     return mWindow;
395 }
396 
getNativeDisplay() const397 EGLNativeDisplayType X11Window::getNativeDisplay() const
398 {
399     return mDisplay;
400 }
401 
messageLoop()402 void X11Window::messageLoop()
403 {
404     int eventCount = XPending(mDisplay);
405     while (eventCount--)
406     {
407         XEvent event;
408         XNextEvent(mDisplay, &event);
409         processEvent(event);
410     }
411 }
412 
setMousePosition(int x,int y)413 void X11Window::setMousePosition(int x, int y)
414 {
415     XWarpPointer(mDisplay, None, mWindow, 0, 0, 0, 0, x, y);
416 }
417 
setPosition(int x,int y)418 bool X11Window::setPosition(int x, int y)
419 {
420     XMoveWindow(mDisplay, mWindow, x, y);
421     XFlush(mDisplay);
422     return true;
423 }
424 
resize(int width,int height)425 bool X11Window::resize(int width, int height)
426 {
427     XResizeWindow(mDisplay, mWindow, width, height);
428     XFlush(mDisplay);
429 
430     Timer *timer = CreateTimer();
431     timer->start();
432 
433     // Wait until the window as actually been resized so that the code calling resize
434     // can assume the window has been resized.
435     const double kResizeWaitDelay = 0.2;
436     while ((mHeight != height || mWidth != width) && timer->getElapsedTime() < kResizeWaitDelay)
437     {
438         messageLoop();
439         angle::Sleep(10);
440     }
441 
442     delete timer;
443 
444     return true;
445 }
446 
setVisible(bool isVisible)447 void X11Window::setVisible(bool isVisible)
448 {
449     if (mVisible == isVisible)
450     {
451         return;
452     }
453 
454     if (isVisible)
455     {
456         XMapWindow(mDisplay, mWindow);
457 
458         // Wait until we get an event saying this window is mapped so that the
459         // code calling setVisible can assume the window is visible.
460         // This is important when creating a framebuffer as the framebuffer content
461         // is undefined when the window is not visible.
462         XEvent dummyEvent;
463         XIfEvent(mDisplay, &dummyEvent, WaitForMapNotify, reinterpret_cast<XPointer>(mWindow));
464     }
465     else
466     {
467         XUnmapWindow(mDisplay, mWindow);
468         XFlush(mDisplay);
469     }
470     mVisible = isVisible;
471 }
472 
signalTestEvent()473 void X11Window::signalTestEvent()
474 {
475     XEvent event;
476     event.type                 = ClientMessage;
477     event.xclient.message_type = TEST_EVENT;
478     // Format needs to be valid or a BadValue is generated
479     event.xclient.format = 32;
480 
481     // Hijack StructureNotifyMask as we know we will be listening for it.
482     XSendEvent(mDisplay, mWindow, False, StructureNotifyMask, &event);
483 
484     // For test events, the tests want to check that it really did arrive, and they don't wait
485     // long.  XSync here makes sure the event is sent by the time the messageLoop() is called.
486     XSync(mDisplay, false);
487 }
488 
processEvent(const XEvent & xEvent)489 void X11Window::processEvent(const XEvent &xEvent)
490 {
491     // TODO(cwallez) text events
492     switch (xEvent.type)
493     {
494         case ButtonPress:
495         {
496             Event event;
497             MouseButton button = MOUSEBUTTON_UNKNOWN;
498             int wheelY         = 0;
499 
500             // The mouse wheel updates are sent via button events.
501             switch (xEvent.xbutton.button)
502             {
503                 case Button4:
504                     wheelY = 1;
505                     break;
506                 case Button5:
507                     wheelY = -1;
508                     break;
509                 case 6:
510                     break;
511                 case 7:
512                     break;
513 
514                 case Button1:
515                     button = MOUSEBUTTON_LEFT;
516                     break;
517                 case Button2:
518                     button = MOUSEBUTTON_MIDDLE;
519                     break;
520                 case Button3:
521                     button = MOUSEBUTTON_RIGHT;
522                     break;
523                 case 8:
524                     button = MOUSEBUTTON_BUTTON4;
525                     break;
526                 case 9:
527                     button = MOUSEBUTTON_BUTTON5;
528                     break;
529 
530                 default:
531                     break;
532             }
533 
534             if (wheelY != 0)
535             {
536                 event.Type             = Event::EVENT_MOUSE_WHEEL_MOVED;
537                 event.MouseWheel.Delta = wheelY;
538                 pushEvent(event);
539             }
540 
541             if (button != MOUSEBUTTON_UNKNOWN)
542             {
543                 event.Type               = Event::EVENT_MOUSE_BUTTON_RELEASED;
544                 event.MouseButton.Button = button;
545                 event.MouseButton.X      = xEvent.xbutton.x;
546                 event.MouseButton.Y      = xEvent.xbutton.y;
547                 pushEvent(event);
548             }
549         }
550         break;
551 
552         case ButtonRelease:
553         {
554             Event event;
555             MouseButton button = MOUSEBUTTON_UNKNOWN;
556 
557             switch (xEvent.xbutton.button)
558             {
559                 case Button1:
560                     button = MOUSEBUTTON_LEFT;
561                     break;
562                 case Button2:
563                     button = MOUSEBUTTON_MIDDLE;
564                     break;
565                 case Button3:
566                     button = MOUSEBUTTON_RIGHT;
567                     break;
568                 case 8:
569                     button = MOUSEBUTTON_BUTTON4;
570                     break;
571                 case 9:
572                     button = MOUSEBUTTON_BUTTON5;
573                     break;
574 
575                 default:
576                     break;
577             }
578 
579             if (button != MOUSEBUTTON_UNKNOWN)
580             {
581                 event.Type               = Event::EVENT_MOUSE_BUTTON_RELEASED;
582                 event.MouseButton.Button = button;
583                 event.MouseButton.X      = xEvent.xbutton.x;
584                 event.MouseButton.Y      = xEvent.xbutton.y;
585                 pushEvent(event);
586             }
587         }
588         break;
589 
590         case KeyPress:
591         {
592             Event event;
593             event.Type     = Event::EVENT_KEY_PRESSED;
594             event.Key.Code = X11CodeToKey(mDisplay, xEvent.xkey.keycode);
595             AddX11KeyStateToEvent(&event, xEvent.xkey.state);
596             pushEvent(event);
597         }
598         break;
599 
600         case KeyRelease:
601         {
602             Event event;
603             event.Type     = Event::EVENT_KEY_RELEASED;
604             event.Key.Code = X11CodeToKey(mDisplay, xEvent.xkey.keycode);
605             AddX11KeyStateToEvent(&event, xEvent.xkey.state);
606             pushEvent(event);
607         }
608         break;
609 
610         case EnterNotify:
611         {
612             Event event;
613             event.Type = Event::EVENT_MOUSE_ENTERED;
614             pushEvent(event);
615         }
616         break;
617 
618         case LeaveNotify:
619         {
620             Event event;
621             event.Type = Event::EVENT_MOUSE_LEFT;
622             pushEvent(event);
623         }
624         break;
625 
626         case MotionNotify:
627         {
628             Event event;
629             event.Type        = Event::EVENT_MOUSE_MOVED;
630             event.MouseMove.X = xEvent.xmotion.x;
631             event.MouseMove.Y = xEvent.xmotion.y;
632             pushEvent(event);
633         }
634         break;
635 
636         case ConfigureNotify:
637         {
638             if (xEvent.xconfigure.width != mWidth || xEvent.xconfigure.height != mHeight)
639             {
640                 Event event;
641                 event.Type        = Event::EVENT_RESIZED;
642                 event.Size.Width  = xEvent.xconfigure.width;
643                 event.Size.Height = xEvent.xconfigure.height;
644                 pushEvent(event);
645             }
646             if (xEvent.xconfigure.x != mX || xEvent.xconfigure.y != mY)
647             {
648                 // Sometimes, the window manager reparents our window (for example
649                 // when resizing) then the X and Y coordinates will be with respect to
650                 // the new parent and not what the user wants to know. Use
651                 // XTranslateCoordinates to get the coordinates on the screen.
652                 int screen  = DefaultScreen(mDisplay);
653                 Window root = RootWindow(mDisplay, screen);
654 
655                 int x, y;
656                 Window child;
657                 XTranslateCoordinates(mDisplay, mWindow, root, 0, 0, &x, &y, &child);
658 
659                 if (x != mX || y != mY)
660                 {
661                     Event event;
662                     event.Type   = Event::EVENT_MOVED;
663                     event.Move.X = x;
664                     event.Move.Y = y;
665                     pushEvent(event);
666                 }
667             }
668         }
669         break;
670 
671         case FocusIn:
672             if (xEvent.xfocus.mode == NotifyNormal || xEvent.xfocus.mode == NotifyWhileGrabbed)
673             {
674                 Event event;
675                 event.Type = Event::EVENT_GAINED_FOCUS;
676                 pushEvent(event);
677             }
678             break;
679 
680         case FocusOut:
681             if (xEvent.xfocus.mode == NotifyNormal || xEvent.xfocus.mode == NotifyWhileGrabbed)
682             {
683                 Event event;
684                 event.Type = Event::EVENT_LOST_FOCUS;
685                 pushEvent(event);
686             }
687             break;
688 
689         case DestroyNotify:
690             // We already received WM_DELETE_WINDOW
691             break;
692 
693         case ClientMessage:
694             if (xEvent.xclient.message_type == WM_PROTOCOLS &&
695                 static_cast<Atom>(xEvent.xclient.data.l[0]) == WM_DELETE_WINDOW)
696             {
697                 Event event;
698                 event.Type = Event::EVENT_CLOSED;
699                 pushEvent(event);
700             }
701             else if (xEvent.xclient.message_type == TEST_EVENT)
702             {
703                 Event event;
704                 event.Type = Event::EVENT_TEST;
705                 pushEvent(event);
706             }
707             break;
708     }
709 }
710 
711 // static
New()712 OSWindow *OSWindow::New()
713 {
714     return new X11Window();
715 }
716