1// Copyright 2014 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 "athena/screen/screen_accelerator_handler.h"
6
7#include "athena/input/public/accelerator_manager.h"
8#include "athena/screen/public/screen_manager.h"
9#include "ui/aura/window.h"
10#include "ui/aura/window_event_dispatcher.h"
11#include "ui/aura/window_tree_host.h"
12#include "ui/compositor/debug_utils.h"
13#include "ui/gfx/display.h"
14#include "ui/gfx/screen.h"
15#include "ui/wm/public/activation_client.h"
16
17namespace athena {
18namespace {
19
20enum Command {
21  CMD_PRINT_LAYER_HIERARCHY,
22  CMD_PRINT_WINDOW_HIERARCHY,
23  CMD_ROTATE_SCREEN,
24};
25
26const int EF_ALL_DOWN =
27    ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN;
28
29const AcceleratorData accelerator_data[] = {
30    {TRIGGER_ON_PRESS, ui::VKEY_L, EF_ALL_DOWN, CMD_PRINT_LAYER_HIERARCHY,
31     AF_DEBUG},
32    {TRIGGER_ON_PRESS, ui::VKEY_W, EF_ALL_DOWN, CMD_PRINT_WINDOW_HIERARCHY,
33     AF_DEBUG},
34    {TRIGGER_ON_PRESS, ui::VKEY_F3,
35     ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN,
36     CMD_ROTATE_SCREEN, AF_NONE},
37};
38
39void PrintLayerHierarchy(aura::Window* root_window) {
40  ui::PrintLayerHierarchy(
41      root_window->layer(),
42      root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
43}
44
45void PrintWindowHierarchy(aura::Window* window,
46                          aura::Window* active,
47                          int indent,
48                          std::ostringstream* out) {
49  std::string indent_str(indent, ' ');
50  std::string name(window->name());
51  if (name.empty())
52    name = "\"\"";
53  *out << indent_str << name << " (" << window << ")"
54       << " type=" << window->type()
55       << ((window == active) ? " [active] " : " ")
56       << (window->IsVisible() ? " visible " : " ")
57       << window->bounds().ToString() << '\n';
58
59  for (size_t i = 0; i < window->children().size(); ++i)
60    PrintWindowHierarchy(window->children()[i], active, indent + 3, out);
61}
62
63void HandlePrintWindowHierarchy(aura::Window* root_window) {
64  aura::Window* active =
65      aura::client::GetActivationClient(root_window)->GetActiveWindow();
66  std::ostringstream out;
67  out << "RootWindow :\n";
68  PrintWindowHierarchy(root_window, active, 0, &out);
69  // Error so logs can be collected from end-users.
70  LOG(ERROR) << out.str();
71}
72
73void HandleRotateScreen() {
74  ScreenManager* screen_manager = ScreenManager::Get();
75  gfx::Display::Rotation current_rotation =
76      gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().rotation();
77  if (current_rotation == gfx::Display::ROTATE_0)
78    screen_manager->SetRotation(gfx::Display::ROTATE_90);
79  else if (current_rotation == gfx::Display::ROTATE_90)
80    screen_manager->SetRotation(gfx::Display::ROTATE_180);
81  else if (current_rotation == gfx::Display::ROTATE_180)
82    screen_manager->SetRotation(gfx::Display::ROTATE_270);
83  else if (current_rotation == gfx::Display::ROTATE_270)
84    screen_manager->SetRotation(gfx::Display::ROTATE_0);
85}
86
87}  // namespace
88
89// static
90ScreenAcceleratorHandler::ScreenAcceleratorHandler(aura::Window* root_window)
91    : root_window_(root_window) {
92  AcceleratorManager::Get()->RegisterAccelerators(
93      accelerator_data, arraysize(accelerator_data), this);
94}
95
96ScreenAcceleratorHandler::~ScreenAcceleratorHandler() {
97}
98
99bool ScreenAcceleratorHandler::IsCommandEnabled(int command_id) const {
100  return true;
101}
102
103bool ScreenAcceleratorHandler::OnAcceleratorFired(
104    int command_id,
105    const ui::Accelerator& accelerator) {
106  switch (command_id) {
107    case CMD_PRINT_LAYER_HIERARCHY:
108      PrintLayerHierarchy(root_window_);
109      return true;
110    case CMD_PRINT_WINDOW_HIERARCHY:
111      HandlePrintWindowHierarchy(root_window_);
112      return true;
113    case CMD_ROTATE_SCREEN:
114      HandleRotateScreen();
115      return true;
116  }
117  return false;
118}
119
120}  // namesapce athena
121