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 "ui/ozone/platform/caca/caca_window.h"
6
7 #include "base/bind.h"
8 #include "base/debug/trace_event.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.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"
16
17 namespace ui {
18
19 namespace {
20
21 // Size of initial cnavas (in characters).
22 const int kDefaultCanvasWidth = 160;
23 const int kDefaultCanvasHeight = 48;
24
25 } // namespace
26
27 // TODO(dnicoara) As an optimization, |bitmap_size_| should be scaled based on
28 // |physical_size_| and font size.
CacaWindow(PlatformWindowDelegate * delegate,CacaWindowManager * manager,CacaEventSource * event_source,const gfx::Rect & bounds)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_);
40 }
41
~CacaWindow()42 CacaWindow::~CacaWindow() {
43 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
44 manager_->RemoveWindow(widget_, this);
45 }
46
Initialize()47 bool CacaWindow::Initialize() {
48 if (display_)
49 return true;
50
51 canvas_.reset(caca_create_canvas(kDefaultCanvasWidth, kDefaultCanvasHeight));
52 if (!canvas_) {
53 PLOG(ERROR) << "failed to create libcaca canvas";
54 return false;
55 }
56
57 display_.reset(caca_create_display(canvas_.get()));
58 if (!display_) {
59 PLOG(ERROR) << "failed to initialize libcaca display";
60 return false;
61 }
62
63 caca_set_cursor(display_.get(), 1);
64 caca_set_display_title(display_.get(), "Ozone Content Shell");
65
66 UpdateDisplaySize();
67
68 TryProcessingEvent();
69
70 return true;
71 }
72
TryProcessingEvent()73 void CacaWindow::TryProcessingEvent() {
74 event_source_->TryProcessingEvent(this);
75
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));
84 }
85
UpdateDisplaySize()86 void CacaWindow::UpdateDisplaySize() {
87 physical_size_.SetSize(caca_get_canvas_width(canvas_.get()),
88 caca_get_canvas_height(canvas_.get()));
89
90 bitmap_size_.SetSize(caca_get_display_width(display_.get()),
91 caca_get_display_height(display_.get()));
92 }
93
OnCacaResize()94 void CacaWindow::OnCacaResize() {
95 UpdateDisplaySize();
96
97 delegate_->OnBoundsChanged(gfx::Rect(bitmap_size_));
98 }
99
OnCacaQuit()100 void CacaWindow::OnCacaQuit() {
101 delegate_->OnCloseRequest();
102 }
103
104
OnCacaEvent(ui::Event * event)105 void CacaWindow::OnCacaEvent(ui::Event* event) {
106 DispatchEventFromNativeUiEvent(
107 event,
108 base::Bind(&PlatformWindowDelegate::DispatchEvent,
109 base::Unretained(delegate_)));
110 }
111
GetBounds()112 gfx::Rect CacaWindow::GetBounds() { return gfx::Rect(bitmap_size_); }
113
SetBounds(const gfx::Rect & bounds)114 void CacaWindow::SetBounds(const gfx::Rect& bounds) {}
115
Show()116 void CacaWindow::Show() {}
117
Hide()118 void CacaWindow::Hide() {}
119
Close()120 void CacaWindow::Close() {}
121
SetCapture()122 void CacaWindow::SetCapture() {}
123
ReleaseCapture()124 void CacaWindow::ReleaseCapture() {}
125
ToggleFullscreen()126 void CacaWindow::ToggleFullscreen() {}
127
Maximize()128 void CacaWindow::Maximize() {}
129
Minimize()130 void CacaWindow::Minimize() {}
131
Restore()132 void CacaWindow::Restore() {}
133
SetCursor(PlatformCursor cursor)134 void CacaWindow::SetCursor(PlatformCursor cursor) {}
135
MoveCursorTo(const gfx::Point & location)136 void CacaWindow::MoveCursorTo(const gfx::Point& location) {}
137
CanDispatchEvent(const PlatformEvent & event)138 bool CacaWindow::CanDispatchEvent(const PlatformEvent& event) { return true; }
139
DispatchEvent(const PlatformEvent & ne)140 uint32_t CacaWindow::DispatchEvent(const PlatformEvent& ne) {
141 // We don't dispatch events via PlatformEventSource.
142 NOTREACHED();
143 return ui::POST_DISPATCH_STOP_PROPAGATION;
144 }
145
146 } // namespace ui
147