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