mirror_window_controller.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/display/mirror_window_controller.h"
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_X11)
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <X11/Xlib.h>
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Xlib.h defines RootWindow.
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#undef RootWindow
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/display/cursor_window_controller.h"
15eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ash/display/display_controller.h"
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/display/display_info.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/display/display_manager.h"
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/display/root_window_transformers.h"
19a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ash/host/ash_window_tree_host.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ash/host/ash_window_tree_host_init_params.h"
21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ash/host/root_window_transformer.h"
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ash/root_window_settings.h"
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ash/shell.h"
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/stringprintf.h"
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/aura/client/capture_client.h"
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/aura/window_delegate.h"
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
28a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "ui/aura/window_tree_host.h"
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/layout.h"
30558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "ui/compositor/reflector.h"
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/gfx/canvas.h"
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/gfx/native_widget_types.h"
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#if defined(USE_X11)
3568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/gfx/x/x11_types.h"
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace ash {
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace {
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_X11)
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Mirror window shouldn't handle input events.
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void DisableInput(XID window) {
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  long event_mask = ExposureMask | VisibilityChangeMask |
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      StructureNotifyMask | PropertyChangeMask;
4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  XSelectInput(gfx::GetXDisplay(), window, event_mask);
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class NoneCaptureClient : public aura::client::CaptureClient {
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  NoneCaptureClient() {}
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual ~NoneCaptureClient() {}
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Does a capture on the |window|.
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void SetCapture(aura::Window* window) OVERRIDE {}
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Releases a capture from the |window|.
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void ReleaseCapture(aura::Window* window) OVERRIDE {}
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Returns the current capture window.
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual aura::Window* GetCaptureWindow() OVERRIDE {
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return NULL;
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual aura::Window* GetGlobalCaptureWindow() OVERRIDE {
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return NULL;
68f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NoneCaptureClient);
7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)MirrorWindowController::MirrorWindowController() {}
7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)MirrorWindowController::~MirrorWindowController() {
7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Make sure the root window gets deleted before cursor_window_delegate.
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Close();
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void MirrorWindowController::UpdateWindow(const DisplayInfo& display_info) {
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static int mirror_host_count = 0;
84a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (!ash_host_.get()) {
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    AshWindowTreeHostInitParams init_params;
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    init_params.initial_bounds = display_info.bounds_in_native();
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ash_host_.reset(AshWindowTreeHost::Create(init_params));
88a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
89a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->window()->SetName(
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::StringPrintf("MirrorRootWindow-%d", mirror_host_count++));
91a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->compositor()->SetBackgroundColor(SK_ColorBLACK);
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // No need to remove the observer because the DisplayController outlives the
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // host.
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->AddObserver(Shell::GetInstance()->display_controller());
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->AddObserver(this);
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // TODO(oshima): TouchHUD is using idkey.
97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    InitRootWindowSettings(host->window())->display_id = display_info.id();
98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->InitHost();
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if defined(USE_X11)
100a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    DisableInput(host->GetAcceleratedWidget());
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::client::SetCaptureClient(host->window(), new NoneCaptureClient());
104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->Show();
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
106868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // TODO(oshima): Start mirroring.
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    aura::Window* mirror_window = new aura::Window(NULL);
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    mirror_window->Init(aura::WINDOW_LAYER_TEXTURED);
109a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->window()->AddChild(mirror_window);
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    mirror_window->SetBounds(host->window()->bounds());
111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    mirror_window->Show();
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    reflector_ = ui::ContextFactory::GetInstance()->CreateReflector(
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        Shell::GetPrimaryRootWindow()->GetHost()->compositor(),
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        mirror_window->layer());
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  } else {
116a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    GetRootWindowSettings(host->window())->display_id = display_info.id();
118a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->SetBounds(display_info.bounds_in_native());
119868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      Shell::GetScreen()->GetPrimaryDisplay().id());
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(display_manager->IsMirrored());
125a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<RootWindowTransformer> transformer(
126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      CreateRootWindowTransformerForMirroredDisplay(source_display_info,
127c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                    display_info));
128a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ash_host_->SetRootWindowTransformer(transformer.Pass());
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void MirrorWindowController::UpdateWindow() {
132a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (ash_host_.get()) {
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    DisplayManager* display_manager = Shell::GetInstance()->display_manager();
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        display_manager->mirrored_display_id());
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    UpdateWindow(mirror_display_info);
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
13990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
14090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void MirrorWindowController::Close() {
141a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (ash_host_.get()) {
142a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::WindowTreeHost* host = ash_host_->AsWindowTreeHost();
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ui::ContextFactory::GetInstance()->RemoveReflector(reflector_);
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    reflector_ = NULL;
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    NoneCaptureClient* capture_client = static_cast<NoneCaptureClient*>(
146a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch        aura::client::GetCaptureClient(host->window()));
147a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    aura::client::SetCaptureClient(host->window(), NULL);
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    delete capture_client;
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
150a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->RemoveObserver(Shell::GetInstance()->display_controller());
151a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    host->RemoveObserver(this);
152a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    ash_host_.reset();
15390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
15490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
15590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void MirrorWindowController::OnHostResized(const aura::WindowTreeHost* host) {
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (mirror_window_host_size_ == host->GetBounds().size())
158868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  mirror_window_host_size_ = host->GetBounds().size();
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  reflector_->OnMirroringCompositorResized();
161a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  ash_host_->SetRootWindowTransformer(CreateRootWindowTransformer().Pass());
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Shell::GetInstance()->display_controller()->cursor_window_controller()->
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      UpdateLocation();
1647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
1657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
166a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochaura::Window* MirrorWindowController::GetWindow() {
167a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return ash_host_.get() ? ash_host_->AsWindowTreeHost()->window() : NULL;
168a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
170a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochscoped_ptr<RootWindowTransformer>
1717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)MirrorWindowController::CreateRootWindowTransformer() const {
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
173868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DisplayInfo& mirror_display_info = display_manager->GetDisplayInfo(
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      display_manager->mirrored_display_id());
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  const DisplayInfo& source_display_info = display_manager->GetDisplayInfo(
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      Shell::GetScreen()->GetPrimaryDisplay().id());
1771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(display_manager->IsMirrored());
178a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return scoped_ptr<RootWindowTransformer>(
179c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      CreateRootWindowTransformerForMirroredDisplay(source_display_info,
180c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                                                    mirror_display_info));
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace ash
184