Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / ui / ozone / platform / caca / caca_window.cc
blobf0dc0a7a81640400cb3b880bf4fc9c735413b907
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.
5 #include "ui/ozone/platform/caca/caca_window.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/trace_event/trace_event.h"
11 #include "ui/events/ozone/events_ozone.h"
12 #include "ui/events/platform/platform_event_source.h"
13 #include "ui/ozone/platform/caca/caca_event_source.h"
14 #include "ui/ozone/platform/caca/caca_window_manager.h"
15 #include "ui/platform_window/platform_window_delegate.h"
17 namespace ui {
19 namespace {
21 // Size of initial cnavas (in characters).
22 const int kDefaultCanvasWidth = 160;
23 const int kDefaultCanvasHeight = 48;
25 } // namespace
27 // TODO(dnicoara) As an optimization, |bitmap_size_| should be scaled based on
28 // |physical_size_| and font size.
29 CacaWindow::CacaWindow(PlatformWindowDelegate* delegate,
30 CacaWindowManager* manager,
31 CacaEventSource* event_source,
32 const gfx::Rect& bounds)
33 : delegate_(delegate),
34 manager_(manager),
35 event_source_(event_source),
36 weak_ptr_factory_(this) {
37 widget_ = manager_->AddWindow(this);
38 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
39 delegate_->OnAcceleratedWidgetAvailable(widget_);
42 CacaWindow::~CacaWindow() {
43 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
44 manager_->RemoveWindow(widget_, this);
47 bool CacaWindow::Initialize() {
48 if (display_)
49 return true;
51 canvas_.reset(caca_create_canvas(kDefaultCanvasWidth, kDefaultCanvasHeight));
52 if (!canvas_) {
53 PLOG(ERROR) << "failed to create libcaca canvas";
54 return false;
57 display_.reset(caca_create_display(canvas_.get()));
58 if (!display_) {
59 PLOG(ERROR) << "failed to initialize libcaca display";
60 return false;
63 caca_set_cursor(display_.get(), 1);
64 caca_set_display_title(display_.get(), "Ozone Content Shell");
66 UpdateDisplaySize();
68 TryProcessingEvent();
70 return true;
73 void CacaWindow::TryProcessingEvent() {
74 event_source_->TryProcessingEvent(this);
76 // Caca uses a poll based event retrieval. Since we don't want to block we'd
77 // either need to spin up a new thread or just poll. For simplicity just poll
78 // for messages.
79 base::MessageLoop::current()->PostDelayedTask(
80 FROM_HERE,
81 base::Bind(&CacaWindow::TryProcessingEvent,
82 weak_ptr_factory_.GetWeakPtr()),
83 base::TimeDelta::FromMilliseconds(10));
86 void CacaWindow::UpdateDisplaySize() {
87 physical_size_.SetSize(caca_get_canvas_width(canvas_.get()),
88 caca_get_canvas_height(canvas_.get()));
90 bitmap_size_.SetSize(caca_get_display_width(display_.get()),
91 caca_get_display_height(display_.get()));
94 void CacaWindow::OnCacaResize() {
95 UpdateDisplaySize();
97 delegate_->OnBoundsChanged(gfx::Rect(bitmap_size_));
100 void CacaWindow::OnCacaQuit() {
101 delegate_->OnCloseRequest();
105 void CacaWindow::OnCacaEvent(ui::Event* event) {
106 DispatchEventFromNativeUiEvent(
107 event, base::Bind(&PlatformWindowDelegate::DispatchEvent,
108 base::Unretained(delegate_)));
111 gfx::Rect CacaWindow::GetBounds() { return gfx::Rect(bitmap_size_); }
113 void CacaWindow::SetBounds(const gfx::Rect& bounds) {}
115 void CacaWindow::Show() {}
117 void CacaWindow::Hide() {}
119 void CacaWindow::Close() {}
121 void CacaWindow::SetCapture() {}
123 void CacaWindow::ReleaseCapture() {}
125 void CacaWindow::ToggleFullscreen() {}
127 void CacaWindow::Maximize() {}
129 void CacaWindow::Minimize() {}
131 void CacaWindow::Restore() {}
133 void CacaWindow::SetCursor(PlatformCursor cursor) {}
135 void CacaWindow::MoveCursorTo(const gfx::Point& location) {}
137 void CacaWindow::ConfineCursorToBounds(const gfx::Rect& bounds) {}
139 bool CacaWindow::CanDispatchEvent(const PlatformEvent& event) { return true; }
141 uint32_t CacaWindow::DispatchEvent(const PlatformEvent& ne) {
142 // We don't dispatch events via PlatformEventSource.
143 NOTREACHED();
144 return ui::POST_DISPATCH_STOP_PROPAGATION;
147 } // namespace ui