OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/legacy_window_manager/wm_ipc.h" | 5 #include "chrome/browser/chromeos/legacy_window_manager/wm_ipc.h" |
6 | 6 |
7 #include <gdk/gdkx.h> | 7 #include <gdk/gdkx.h> |
8 extern "C" { | 8 extern "C" { |
9 #include <X11/Xatom.h> | 9 #include <X11/Xatom.h> |
10 #include <X11/Xlib.h> | 10 #include <X11/Xlib.h> |
11 } | 11 } |
12 | 12 |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
17 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
18 #include "ui/base/x/x11_util.h" | 18 #include "ui/base/x/x11_util.h" |
19 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 20 #include "views/widget/wm_ipc.h" |
20 | 21 |
21 using std::map; | 22 using std::map; |
22 using std::string; | 23 using std::string; |
23 | 24 |
24 namespace chromeos { | 25 namespace chromeos { |
25 | 26 |
26 namespace { | |
27 | |
28 // A value from the Atom enum and the actual name that should be used to | |
29 // look up its ID on the X server. | |
30 struct AtomInfo { | |
31 WmIpc::AtomType atom; | |
32 const char* name; | |
33 }; | |
34 | |
35 // Each value from the Atom enum must be present here. | |
36 static const AtomInfo kAtomInfos[] = { | |
37 { WmIpc::ATOM_CHROME_LAYOUT_MODE, "_CHROME_LAYOUT_MODE" }, | |
38 { WmIpc::ATOM_CHROME_LOGGED_IN, "_CHROME_LOGGED_IN" }, | |
39 { WmIpc::ATOM_CHROME_STATE, "_CHROME_STATE" }, | |
40 { WmIpc::ATOM_CHROME_STATE_COLLAPSED_PANEL, "_CHROME_STATE_COLLAPSED_PANEL" }, | |
41 { WmIpc::ATOM_CHROME_STATE_STATUS_HIDDEN, "_CHROME_STATE_STATUS_HIDDEN" }, | |
42 { WmIpc::ATOM_CHROME_STATUS_BOUNDS, "_CHROME_STATUS_BOUNDS" }, | |
43 { WmIpc::ATOM_CHROME_WINDOW_TYPE, "_CHROME_WINDOW_TYPE" }, | |
44 { WmIpc::ATOM_CHROME_WM_MESSAGE, "_CHROME_WM_MESSAGE" }, | |
45 { WmIpc::ATOM_MANAGER, "MANAGER" }, | |
46 { WmIpc::ATOM_STRING, "STRING" }, | |
47 { WmIpc::ATOM_UTF8_STRING, "UTF8_STRING" }, | |
48 { WmIpc::ATOM_WM_S0, "WM_S0" }, | |
49 }; | |
50 | |
51 bool SetIntArrayProperty(XID xid, | |
52 Atom property, | |
53 Atom property_type, | |
54 const std::vector<int>& values) { | |
55 DCHECK(!values.empty()); | |
56 | |
57 // XChangeProperty expects values of type 32 to be longs. | |
58 scoped_array<long> data(new long[values.size()]); | |
59 for (size_t i = 0; i < values.size(); ++i) | |
60 data[i] = values[i]; | |
61 | |
62 // TODO: Trap errors and return false on failure. | |
63 XChangeProperty(ui::GetXDisplay(), | |
64 xid, | |
65 property, | |
66 property_type, | |
67 32, // size in bits of items in 'value' | |
68 PropModeReplace, | |
69 reinterpret_cast<const unsigned char*>(data.get()), | |
70 values.size()); // num items | |
71 XFlush(ui::GetXDisplay()); | |
72 return true; | |
73 } | |
74 | |
75 } // namespace | |
76 | |
77 static base::LazyInstance<WmIpc> g_wm_ipc = LAZY_INSTANCE_INITIALIZER; | 27 static base::LazyInstance<WmIpc> g_wm_ipc = LAZY_INSTANCE_INITIALIZER; |
78 | 28 |
79 // static | 29 // static |
80 WmIpc* WmIpc::instance() { | 30 WmIpc* WmIpc::instance() { |
81 return g_wm_ipc.Pointer(); | 31 return g_wm_ipc.Pointer(); |
82 } | 32 } |
83 | 33 |
84 string WmIpc::GetAtomName(AtomType type) const { | 34 string WmIpc::GetAtomName(views::chromeos::WmIpc::AtomType type) const { |
85 map<AtomType, Atom>::const_iterator atom_it = type_to_atom_.find(type); | 35 return views::chromeos::WmIpc::instance()->GetAtomName(type); |
86 DCHECK(atom_it != type_to_atom_.end()) << "Unknown AtomType " << type; | |
87 if (atom_it == type_to_atom_.end()) | |
88 return ""; | |
89 | |
90 map<Atom, string>::const_iterator name_it = | |
91 atom_to_string_.find(atom_it->second); | |
92 DCHECK(name_it != atom_to_string_.end()) | |
93 << "Unknown atom " << atom_it->second << " for AtomType " << type; | |
94 if (name_it == atom_to_string_.end()) | |
95 return ""; | |
96 | |
97 return name_it->second; | |
98 } | 36 } |
99 | 37 |
100 bool WmIpc::SetWindowType(GtkWidget* widget, | 38 bool WmIpc::SetWindowType(GtkWidget* widget, |
101 WmIpcWindowType type, | 39 WmIpcWindowType type, |
102 const std::vector<int>* params) { | 40 const std::vector<int>* params) { |
103 std::vector<int> values; | 41 return views::chromeos::WmIpc::instance()->SetWindowType( |
104 values.push_back(type); | 42 widget, type, params); |
105 if (params) | |
106 values.insert(values.end(), params->begin(), params->end()); | |
107 const Atom atom = type_to_atom_[ATOM_CHROME_WINDOW_TYPE]; | |
108 return SetIntArrayProperty(ui::GetX11WindowFromGtkWidget(widget), | |
109 atom, atom, values); | |
110 } | 43 } |
111 | 44 |
112 WmIpcWindowType WmIpc::GetWindowType(GtkWidget* widget, | 45 WmIpcWindowType WmIpc::GetWindowType(GtkWidget* widget, |
113 std::vector<int>* params) { | 46 std::vector<int>* params) { |
114 std::vector<int> properties; | 47 return views::chromeos::WmIpc::instance()->GetWindowType( |
115 if (ui::GetIntArrayProperty( | 48 widget, params); |
116 ui::GetX11WindowFromGtkWidget(widget), | |
117 GetAtomName(ATOM_CHROME_WINDOW_TYPE), | |
118 &properties)) { | |
119 int type = properties.front(); | |
120 if (params) { | |
121 params->clear(); | |
122 params->insert(params->begin(), properties.begin() + 1, properties.end()); | |
123 } | |
124 return static_cast<WmIpcWindowType>(type); | |
125 } else { | |
126 return WM_IPC_WINDOW_UNKNOWN; | |
127 } | |
128 } | 49 } |
129 | 50 |
130 bool WmIpc::GetWindowState(GtkWidget* widget, | 51 bool WmIpc::GetWindowState( |
131 std::set<WmIpc::AtomType>* atom_types) { | 52 GtkWidget* widget, |
132 atom_types->clear(); | 53 std::set<views::chromeos::WmIpc::AtomType>* atom_types) { |
133 | 54 return views::chromeos::WmIpc::instance()->GetWindowState( |
134 std::vector<Atom> atoms; | 55 widget, atom_types); |
135 if (!ui::GetAtomArrayProperty( | |
136 ui::GetX11WindowFromGtkWidget(widget), | |
137 GetAtomName(ATOM_CHROME_STATE), | |
138 &atoms)) { | |
139 return false; | |
140 } | |
141 | |
142 for (std::vector<Atom>::const_iterator it = atoms.begin(); | |
143 it != atoms.end(); ++it) { | |
144 std::map<Atom, AtomType>::const_iterator type_it = atom_to_type_.find(*it); | |
145 if (type_it != atom_to_type_.end()) | |
146 atom_types->insert(type_it->second); | |
147 else | |
148 LOG(WARNING) << "Unknown state atom " << *it; | |
149 } | |
150 return true; | |
151 } | 56 } |
152 | 57 |
153 void WmIpc::SendMessage(const Message& msg) { | 58 void WmIpc::SendMessage(const Message& msg) { |
154 XEvent e; | 59 views::chromeos::WmIpc::instance()->SendMessage(msg); |
155 e.xclient.type = ClientMessage; | |
156 e.xclient.window = wm_; | |
157 e.xclient.message_type = type_to_atom_[ATOM_CHROME_WM_MESSAGE]; | |
158 e.xclient.format = 32; // 32-bit values | |
159 e.xclient.data.l[0] = msg.type(); | |
160 | |
161 // XClientMessageEvent only gives us five 32-bit items, and we're using | |
162 // the first one for our message type. | |
163 DCHECK_LE(msg.max_params(), 4); | |
164 for (int i = 0; i < msg.max_params(); ++i) | |
165 e.xclient.data.l[i+1] = msg.param(i); | |
166 | |
167 XSendEvent(ui::GetXDisplay(), | |
168 wm_, | |
169 False, // propagate | |
170 0, // empty event mask | |
171 &e); | |
172 } | 60 } |
173 | 61 |
174 bool WmIpc::DecodeMessage(const GdkEventClient& event, Message* msg) { | 62 bool WmIpc::DecodeMessage(const GdkEventClient& event, Message* msg) { |
175 if (wm_message_atom_ != gdk_x11_atom_to_xatom(event.message_type)) | 63 return views::chromeos::WmIpc::instance()->DecodeMessage(event, msg); |
176 return false; | |
177 | |
178 if (event.data_format != 32) { | |
179 DLOG(WARNING) << "Ignoring ClientEventMessage with invalid bit " | |
180 << "format " << event.data_format | |
181 << " (expected 32-bit values)"; | |
182 return false; | |
183 } | |
184 | |
185 msg->set_type(static_cast<WmIpcMessageType>(event.data.l[0])); | |
186 if (msg->type() < 0) { | |
187 DLOG(WARNING) << "Ignoring ClientEventMessage with invalid message " | |
188 << "type " << msg->type(); | |
189 return false; | |
190 } | |
191 | |
192 // XClientMessageEvent only gives us five 32-bit items, and we're using | |
193 // the first one for our message type. | |
194 DCHECK_LE(msg->max_params(), 4); | |
195 for (int i = 0; i < msg->max_params(); ++i) | |
196 msg->set_param(i, event.data.l[i+1]); // l[0] contains message type | |
197 | |
198 return true; | |
199 } | 64 } |
200 | 65 |
201 void WmIpc::HandleNonChromeClientMessageEvent(const GdkEventClient& event) { | 66 void WmIpc::HandleNonChromeClientMessageEvent(const GdkEventClient& event) { |
202 // Only do these lookups once; they should never change. | 67 views::chromeos::WmIpc::instance()->HandleNonChromeClientMessageEvent(event); |
203 static GdkAtom manager_gdk_atom = | |
204 gdk_x11_xatom_to_atom(type_to_atom_[ATOM_MANAGER]); | |
205 static Atom wm_s0_atom = type_to_atom_[ATOM_WM_S0]; | |
206 | |
207 if (event.message_type == manager_gdk_atom && | |
208 static_cast<Atom>(event.data.l[1]) == wm_s0_atom) { | |
209 InitWmInfo(); | |
210 } | |
211 } | 68 } |
212 | 69 |
213 void WmIpc::HandleRootWindowPropertyEvent(const GdkEventProperty& event) { | 70 void WmIpc::HandleRootWindowPropertyEvent(const GdkEventProperty& event) { |
214 static GdkAtom layout_mode_gdk_atom = | 71 static GdkAtom layout_mode_gdk_atom = |
215 gdk_x11_xatom_to_atom(type_to_atom_[ATOM_CHROME_LAYOUT_MODE]); | 72 gdk_x11_xatom_to_atom( |
| 73 (*views::chromeos::WmIpc::instance()->type_to_atom())[ |
| 74 views::chromeos::WmIpc::ATOM_CHROME_LAYOUT_MODE]); |
216 | 75 |
217 if (event.atom == layout_mode_gdk_atom) | 76 if (event.atom == layout_mode_gdk_atom) |
218 FetchLayoutModeProperty(); | 77 FetchLayoutModeProperty(); |
219 } | 78 } |
220 | 79 |
221 void WmIpc::SetLoggedInProperty(bool logged_in) { | 80 void WmIpc::SetLoggedInProperty(bool logged_in) { |
222 std::vector<int> values; | 81 views::chromeos::WmIpc::instance()->SetLoggedInProperty(logged_in); |
223 values.push_back(static_cast<int>(logged_in)); | |
224 const Atom atom = type_to_atom_[ATOM_CHROME_LOGGED_IN]; | |
225 SetIntArrayProperty(gdk_x11_get_default_root_xwindow(), atom, atom, values); | |
226 } | 82 } |
227 | 83 |
228 void WmIpc::SetStatusBoundsProperty(GtkWidget* widget, | 84 void WmIpc::SetStatusBoundsProperty(GtkWidget* widget, |
229 const gfx::Rect& bounds) { | 85 const gfx::Rect& bounds) { |
230 std::vector<int> values; | 86 views::chromeos::WmIpc::instance()->SetStatusBoundsProperty( |
231 values.push_back(bounds.x()); | 87 widget, bounds); |
232 values.push_back(bounds.y()); | |
233 values.push_back(bounds.width()); | |
234 values.push_back(bounds.height()); | |
235 SetIntArrayProperty(ui::GetX11WindowFromGtkWidget(widget), | |
236 type_to_atom_[ATOM_CHROME_STATUS_BOUNDS], | |
237 XA_CARDINAL, | |
238 values); | |
239 } | 88 } |
240 | 89 |
241 void WmIpc::NotifyAboutSignout() { | 90 void WmIpc::NotifyAboutSignout() { |
242 Message msg(chromeos::WM_IPC_MESSAGE_WM_NOTIFY_SIGNING_OUT); | 91 views::chromeos::WmIpc::instance()->NotifyAboutSignout(); |
243 SendMessage(msg); | |
244 XFlush(ui::GetXDisplay()); | |
245 } | 92 } |
246 | 93 |
247 WmIpc::WmIpc() | 94 WmIpc::WmIpc() |
248 : wm_message_atom_(0), | 95 : layout_mode_(::chromeos::WM_IPC_LAYOUT_MAXIMIZED) { |
249 wm_(0), | 96 views::chromeos::WmIpc::instance(); |
250 layout_mode_(WM_IPC_LAYOUT_MAXIMIZED) { | |
251 scoped_array<char*> names(new char*[kNumAtoms]); | |
252 scoped_array<Atom> atoms(new Atom[kNumAtoms]); | |
253 | |
254 for (int i = 0; i < kNumAtoms; ++i) { | |
255 // Need to const_cast because XInternAtoms() wants a char**. | |
256 names[i] = const_cast<char*>(kAtomInfos[i].name); | |
257 } | |
258 | |
259 XInternAtoms(ui::GetXDisplay(), names.get(), kNumAtoms, | |
260 False, // only_if_exists | |
261 atoms.get()); | |
262 | |
263 for (int i = 0; i < kNumAtoms; ++i) { | |
264 type_to_atom_[kAtomInfos[i].atom] = atoms[i]; | |
265 atom_to_type_[atoms[i]] = kAtomInfos[i].atom; | |
266 atom_to_string_[atoms[i]] = kAtomInfos[i].name; | |
267 } | |
268 | |
269 wm_message_atom_ = type_to_atom_[ATOM_CHROME_WM_MESSAGE]; | |
270 | |
271 // Make sure that we're selecting structure changes on the root window; | |
272 // the window manager uses StructureNotifyMask when sending the ClientMessage | |
273 // event to announce that it's taken the manager selection. | |
274 GdkWindow* root = gdk_get_default_root_window(); | |
275 GdkEventMask event_mask = gdk_window_get_events(root); | |
276 gdk_window_set_events( | |
277 root, | |
278 static_cast<GdkEventMask>( | |
279 event_mask | GDK_STRUCTURE_MASK | GDK_PROPERTY_CHANGE_MASK)); | |
280 | |
281 InitWmInfo(); | |
282 FetchLayoutModeProperty(); | 97 FetchLayoutModeProperty(); |
283 } | 98 } |
284 | 99 |
285 WmIpc::~WmIpc() {} | 100 WmIpc::~WmIpc() {} |
286 | 101 |
287 void WmIpc::InitWmInfo() { | |
288 wm_ = XGetSelectionOwner(ui::GetXDisplay(), type_to_atom_[ATOM_WM_S0]); | |
289 | |
290 // Let the window manager know which version of the IPC messages we support. | |
291 Message msg(chromeos::WM_IPC_MESSAGE_WM_NOTIFY_IPC_VERSION); | |
292 // TODO: The version number is the latest listed in wm_ipc.h -- | |
293 // ideally, once this header is shared between Chrome and the Chrome OS window | |
294 // manager, we'll just define the version statically in the header. | |
295 msg.set_param(0, 1); | |
296 SendMessage(msg); | |
297 } | |
298 | |
299 void WmIpc::FetchLayoutModeProperty() { | 102 void WmIpc::FetchLayoutModeProperty() { |
300 int value = 0; | 103 int value = 0; |
301 if (ui::GetIntProperty( | 104 if (ui::GetIntProperty( |
302 gdk_x11_get_default_root_xwindow(), | 105 gdk_x11_get_default_root_xwindow(), |
303 GetAtomName(ATOM_CHROME_LAYOUT_MODE), | 106 GetAtomName(views::chromeos::WmIpc::ATOM_CHROME_LAYOUT_MODE), |
304 &value)) { | 107 &value)) { |
305 layout_mode_ = static_cast<WmIpcLayoutMode>(value); | 108 layout_mode_ = static_cast<WmIpcLayoutMode>(value); |
306 content::NotificationService::current()->Notify( | 109 content::NotificationService::current()->Notify( |
307 chrome::NOTIFICATION_LAYOUT_MODE_CHANGED, | 110 chrome::NOTIFICATION_LAYOUT_MODE_CHANGED, |
308 content::Source<WmIpc>(this), | 111 content::Source<WmIpc>(this), |
309 content::Details<WmIpcLayoutMode>(&layout_mode_)); | 112 content::Details<WmIpcLayoutMode>(&layout_mode_)); |
310 } else { | 113 } else { |
311 DLOG(WARNING) << "Missing _CHROME_LAYOUT_MODE property on root window"; | 114 DLOG(WARNING) << "Missing _CHROME_LAYOUT_MODE property on root window"; |
312 } | 115 } |
313 } | 116 } |
314 | 117 |
315 } // namespace chromeos | 118 } // namespace chromeos |
OLD | NEW |