ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / aura / demo / demo_main.cc
blobe7ed3680c17ea8293161d63e8d3ce7202432afca
1 // Copyright (c) 2012 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.
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/i18n/icu_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_device_source.h"
12 #include "third_party/skia/include/core/SkXfermode.h"
13 #include "ui/aura/client/default_capture_client.h"
14 #include "ui/aura/client/window_tree_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/test/test_focus_client.h"
17 #include "ui/aura/test/test_screen.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_delegate.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/base/hit_test.h"
22 #include "ui/compositor/test/in_process_context_factory.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/skia_util.h"
27 #include "ui/gl/gl_surface.h"
29 #if defined(USE_X11)
30 #include "ui/gfx/x/x11_connection.h"
31 #endif
33 #if defined(OS_WIN)
34 #include "ui/gfx/win/dpi.h"
35 #endif
37 namespace {
39 // Trivial WindowDelegate implementation that draws a colored background.
40 class DemoWindowDelegate : public aura::WindowDelegate {
41 public:
42 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
44 // Overridden from WindowDelegate:
45 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
47 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
49 void OnBoundsChanged(const gfx::Rect& old_bounds,
50 const gfx::Rect& new_bounds) override {}
51 ui::TextInputClient* GetFocusedTextInputClient() override { return nullptr; }
52 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
53 return gfx::kNullCursor;
55 int GetNonClientComponent(const gfx::Point& point) const override {
56 return HTCAPTION;
58 bool ShouldDescendIntoChildForEventHandling(
59 aura::Window* child,
60 const gfx::Point& location) override {
61 return true;
63 bool CanFocus() override { return true; }
64 void OnCaptureLost() override {}
65 void OnPaint(gfx::Canvas* canvas) override {
66 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
67 gfx::Rect r;
68 canvas->GetClipBounds(&r);
69 // Fill with a non-solid color so that the compositor will exercise its
70 // texture upload path.
71 while (!r.IsEmpty()) {
72 r.Inset(2, 2);
73 canvas->FillRect(r, color_, SkXfermode::kXor_Mode);
76 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
77 void OnWindowDestroying(aura::Window* window) override {}
78 void OnWindowDestroyed(aura::Window* window) override {}
79 void OnWindowTargetVisibilityChanged(bool visible) override {}
80 bool HasHitTestMask() const override { return false; }
81 void GetHitTestMask(gfx::Path* mask) const override {}
83 private:
84 SkColor color_;
86 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
89 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
90 public:
91 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
92 aura::client::SetWindowTreeClient(window_, this);
95 ~DemoWindowTreeClient() override {
96 aura::client::SetWindowTreeClient(window_, nullptr);
99 // Overridden from aura::client::WindowTreeClient:
100 aura::Window* GetDefaultParent(aura::Window* context,
101 aura::Window* window,
102 const gfx::Rect& bounds) override {
103 if (!capture_client_) {
104 capture_client_.reset(
105 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
107 return window_;
110 private:
111 aura::Window* window_;
113 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
115 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
118 int DemoMain() {
119 #if defined(USE_X11)
120 // This demo uses InProcessContextFactory which uses X on a separate Gpu
121 // thread.
122 gfx::InitializeThreadedX11();
123 #endif
125 gfx::GLSurface::InitializeOneOff();
127 #if defined(OS_WIN)
128 gfx::InitDeviceScaleFactor(1.0f);
129 #endif
131 // The ContextFactory must exist before any Compositors are created.
132 bool context_factory_for_test = false;
133 scoped_ptr<ui::InProcessContextFactory> context_factory(
134 new ui::InProcessContextFactory(context_factory_for_test, nullptr));
135 context_factory->set_use_test_surface(false);
137 // Create the message-loop here before creating the root window.
138 base::MessageLoopForUI message_loop;
140 base::PowerMonitor power_monitor(make_scoped_ptr(
141 new base::PowerMonitorDeviceSource));
143 aura::Env::CreateInstance(true);
144 aura::Env::GetInstance()->set_context_factory(context_factory.get());
145 scoped_ptr<aura::TestScreen> test_screen(
146 aura::TestScreen::Create(gfx::Size()));
147 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
148 scoped_ptr<aura::WindowTreeHost> host(
149 test_screen->CreateHostForPrimaryDisplay());
150 scoped_ptr<DemoWindowTreeClient> window_tree_client(
151 new DemoWindowTreeClient(host->window()));
152 aura::test::TestFocusClient focus_client;
153 aura::client::SetFocusClient(host->window(), &focus_client);
155 // Create a hierarchy of test windows.
156 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
157 aura::Window window1(&window_delegate1);
158 window1.set_id(1);
159 window1.Init(aura::WINDOW_LAYER_TEXTURED);
160 window1.SetBounds(gfx::Rect(100, 100, 400, 400));
161 window1.Show();
162 aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
164 DemoWindowDelegate window_delegate2(SK_ColorRED);
165 aura::Window window2(&window_delegate2);
166 window2.set_id(2);
167 window2.Init(aura::WINDOW_LAYER_TEXTURED);
168 window2.SetBounds(gfx::Rect(200, 200, 350, 350));
169 window2.Show();
170 aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
172 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
173 aura::Window window3(&window_delegate3);
174 window3.set_id(3);
175 window3.Init(aura::WINDOW_LAYER_TEXTURED);
176 window3.SetBounds(gfx::Rect(10, 10, 50, 50));
177 window3.Show();
178 window2.AddChild(&window3);
180 host->Show();
181 base::MessageLoopForUI::current()->Run();
183 return 0;
186 } // namespace
188 int main(int argc, char** argv) {
189 base::CommandLine::Init(argc, argv);
191 // The exit manager is in charge of calling the dtors of singleton objects.
192 base::AtExitManager exit_manager;
194 base::i18n::InitializeICU();
196 return DemoMain();