OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_AURAX11_H | |
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_AURAX11_H | |
7 | |
8 #include <bitset> | |
9 #include <map> | |
10 | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/message_loop/message_pump.h" | |
13 #include "base/message_loop/message_pump_dispatcher.h" | |
14 #include "base/message_loop/message_pump_glib.h" | |
15 #include "base/message_loop/message_pump_observer.h" | |
16 #include "base/observer_list.h" | |
17 | |
18 // It would be nice to include the X11 headers here so that we use Window | |
19 // instead of its typedef of unsigned long, but we can't because everything in | |
20 // chrome includes us through base/message_loop/message_loop.h, and X11's crappy | |
21 // #define heavy headers muck up half of chrome. | |
22 | |
23 typedef struct _GPollFD GPollFD; | |
24 typedef struct _GSource GSource; | |
25 typedef struct _XDisplay Display; | |
26 | |
27 namespace base { | |
28 | |
29 // This class implements a message-pump for dispatching X events. | |
30 // | |
31 // If there's a current dispatcher given through RunWithDispatcher(), that | |
32 // dispatcher receives events. Otherwise, we route to messages to dispatchers | |
33 // who have subscribed to messages from a specific X11 window. | |
34 class BASE_EXPORT MessagePumpAuraX11 : public MessagePumpGlib, | |
35 public MessagePumpDispatcher { | |
36 public: | |
37 MessagePumpAuraX11(); | |
38 virtual ~MessagePumpAuraX11(); | |
39 | |
40 // Returns default X Display. | |
41 static Display* GetDefaultXDisplay(); | |
42 | |
43 // Returns true if the system supports XINPUT2. | |
44 static bool HasXInput2(); | |
45 | |
46 // Returns the UI message pump. | |
47 static MessagePumpAuraX11* Current(); | |
48 | |
49 // Adds/Removes |dispatcher| for the |xid|. This will route all messages from | |
50 // the window |xid| to |dispatcher. | |
51 void AddDispatcherForWindow(MessagePumpDispatcher* dispatcher, | |
52 unsigned long xid); | |
53 void RemoveDispatcherForWindow(unsigned long xid); | |
54 | |
55 // Adds/Removes |dispatcher| to receive all events sent to the X root | |
56 // window. A root window can have multiple dispatchers, and events on root | |
57 // windows will be dispatched to all. | |
58 void AddDispatcherForRootWindow(MessagePumpDispatcher* dispatcher); | |
59 void RemoveDispatcherForRootWindow(MessagePumpDispatcher* dispatcher); | |
60 | |
61 // Adds an Observer, which will start receiving notifications immediately. | |
62 void AddObserver(MessagePumpObserver* observer); | |
63 | |
64 // Removes an Observer. It is safe to call this method while an Observer is | |
65 // receiving a notification callback. | |
66 void RemoveObserver(MessagePumpObserver* observer); | |
67 | |
68 // Internal function. Called by the glib source dispatch function. Processes | |
69 // all available X events. | |
70 bool DispatchXEvents(); | |
71 | |
72 // Blocks on the X11 event queue until we receive notification from the | |
73 // xserver that |w| has been mapped; StructureNotifyMask events on |w| are | |
74 // pulled out from the queue and dispatched out of order. | |
75 // | |
76 // For those that know X11, this is really a wrapper around XWindowEvent | |
77 // which still makes sure the preempted event is dispatched instead of | |
78 // dropped on the floor. This method exists because mapping a window is | |
79 // asynchronous (and we receive an XEvent when mapped), while there are also | |
80 // functions which require a mapped window. | |
81 void BlockUntilWindowMapped(unsigned long xid); | |
82 | |
83 private: | |
84 typedef std::map<unsigned long, MessagePumpDispatcher*> DispatchersMap; | |
85 | |
86 // Initializes the glib event source for X. | |
87 void InitXSource(); | |
88 | |
89 // Dispatches the XEvent and returns true if we should exit the current loop | |
90 // of message processing. | |
91 bool ProcessXEvent(MessagePumpDispatcher* dispatcher, XEvent* event); | |
92 | |
93 // Sends the event to the observers. If an observer returns true, then it does | |
94 // not send the event to any other observers and returns true. Returns false | |
95 // if no observer returns true. | |
96 bool WillProcessXEvent(XEvent* xevent); | |
97 void DidProcessXEvent(XEvent* xevent); | |
98 | |
99 // Returns the Dispatcher based on the event's target window. | |
100 MessagePumpDispatcher* GetDispatcherForXEvent(const NativeEvent& xev) const; | |
101 | |
102 ObserverList<MessagePumpObserver>& observers() { return observers_; } | |
103 | |
104 // Overridden from MessagePumpDispatcher: | |
105 virtual bool Dispatch(const NativeEvent& event) OVERRIDE; | |
106 | |
107 // The event source for X events. | |
108 GSource* x_source_; | |
109 | |
110 // The poll attached to |x_source_|. | |
111 scoped_ptr<GPollFD> x_poll_; | |
112 | |
113 DispatchersMap dispatchers_; | |
114 | |
115 // Dispatch calls can cause addition of new dispatchers as we iterate | |
116 // through them. Use ObserverList to ensure the iterator remains valid across | |
117 // additions. | |
118 ObserverList<MessagePumpDispatcher> root_window_dispatchers_; | |
119 | |
120 // List of observers. | |
121 ObserverList<MessagePumpObserver> observers_; | |
122 | |
123 unsigned long x_root_window_; | |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(MessagePumpAuraX11); | |
126 }; | |
127 | |
128 typedef MessagePumpAuraX11 MessagePumpForUI; | |
129 | |
130 } // namespace base | |
131 | |
132 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_AURAX11_H | |
OLD | NEW |