OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include "wm/host/foreign_test_window_host_linux.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "ui/aura/env.h" |
| 9 #include "ui/aura/root_window.h" |
| 10 |
| 11 namespace wm { |
| 12 |
| 13 ForeignTestWindowHostLinux::ForeignTestWindowHostLinux( |
| 14 aura::RootWindow* root_window) |
| 15 : display_(NULL), |
| 16 parent_(root_window->GetAcceleratedWidget()), |
| 17 window_(0), |
| 18 font_info_(NULL), |
| 19 gc_(0) { |
| 20 } |
| 21 |
| 22 ForeignTestWindowHostLinux::~ForeignTestWindowHostLinux() { |
| 23 } |
| 24 |
| 25 void ForeignTestWindowHostLinux::Initialize() { |
| 26 DCHECK(parent_); |
| 27 display_ = XOpenDisplay(NULL); |
| 28 size_ = gfx::Size(300, 300); |
| 29 window_ = XCreateSimpleWindow( |
| 30 display_, |
| 31 parent_, |
| 32 30, 30, size_.width(), size_.height(), 0, |
| 33 BlackPixel(display_, DefaultScreen(display_)), |
| 34 WhitePixel(display_, DefaultScreen(display_))); |
| 35 XSelectInput(display_, window_, ExposureMask | StructureNotifyMask); |
| 36 font_info_ = XLoadQueryFont(display_, "9x15"); |
| 37 DCHECK(font_info_); |
| 38 gc_ = XCreateGC(display_, window_, 0, NULL); |
| 39 XSetFont(display_, gc_, font_info_->fid); |
| 40 XSetForeground(display_, gc_, BlackPixel(display_, DefaultScreen(display_))); |
| 41 |
| 42 MessageLoopForIO::current()->WatchFileDescriptor( |
| 43 ConnectionNumber(display_), true, MessageLoopForIO::WATCH_READ, |
| 44 &connection_watcher_, this); |
| 45 |
| 46 PumpXEvents(); |
| 47 } |
| 48 |
| 49 void ForeignTestWindowHostLinux::Delete() { |
| 50 connection_watcher_.StopWatchingFileDescriptor(); |
| 51 XFreeGC(display_, gc_); |
| 52 XFreeFont(display_, font_info_); |
| 53 XCloseDisplay(display_); |
| 54 delete this; |
| 55 } |
| 56 |
| 57 void ForeignTestWindowHostLinux::Show() { |
| 58 DCHECK(window_); |
| 59 XMapWindow(display_, window_); |
| 60 } |
| 61 |
| 62 void ForeignTestWindowHostLinux::Hide() { |
| 63 DCHECK(window_); |
| 64 XUnmapWindow(display_, window_); |
| 65 } |
| 66 |
| 67 void ForeignTestWindowHostLinux::Destroy() { |
| 68 XDestroyWindow(display_, window_); |
| 69 window_ = 0; |
| 70 } |
| 71 |
| 72 void ForeignTestWindowHostLinux::Sync() { |
| 73 XSync(display_, False); |
| 74 } |
| 75 |
| 76 void ForeignTestWindowHostLinux::ProcessXEvent(XEvent *event) { |
| 77 switch (event->type) { |
| 78 case Expose: { |
| 79 if (event->xexpose.count != 0) |
| 80 break; |
| 81 |
| 82 std::string message("Hello, X Window System!"); |
| 83 |
| 84 // Center text. |
| 85 int width = XTextWidth(font_info_, message.c_str(), message.length()); |
| 86 int msg_x = (size_.width() - width) / 2; |
| 87 |
| 88 int font_height = font_info_->ascent + font_info_->descent; |
| 89 int msg_y = (size_.height() + font_height) / 2; |
| 90 |
| 91 XDrawString(display_, |
| 92 window_, |
| 93 gc_, |
| 94 msg_x, msg_y, |
| 95 message.c_str(), message.length()); |
| 96 } break; |
| 97 case ConfigureNotify: { |
| 98 size_ = gfx::Size(event->xconfigure.width, event->xconfigure.height); |
| 99 } break; |
| 100 } |
| 101 } |
| 102 |
| 103 void ForeignTestWindowHostLinux::PumpXEvents() { |
| 104 while (XPending(display_)) { |
| 105 XEvent event; |
| 106 XNextEvent(display_, &event); |
| 107 ProcessXEvent(&event); |
| 108 } |
| 109 } |
| 110 |
| 111 void ForeignTestWindowHostLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 112 PumpXEvents(); |
| 113 } |
| 114 |
| 115 void ForeignTestWindowHostLinux::OnFileCanWriteWithoutBlocking(int fd) { |
| 116 } |
| 117 |
| 118 // static |
| 119 ForeignTestWindowHost* ForeignTestWindowHost::Create( |
| 120 aura::RootWindow* root_window) { |
| 121 return new ForeignTestWindowHostLinux(root_window); |
| 122 } |
| 123 |
| 124 // static |
| 125 void ForeignTestWindowHost::RunAllPendingInMessageLoop() { |
| 126 Display* display = base::MessagePumpAuraX11::GetDefaultXDisplay(); |
| 127 XSync(display, False); |
| 128 base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher()); |
| 129 run_loop.RunUntilIdle(); |
| 130 } |
| 131 |
| 132 } // namespace wm |
OLD | NEW |