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"
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"
21 // Size of initial cnavas (in characters).
22 const int kDefaultCanvasWidth
= 160;
23 const int kDefaultCanvasHeight
= 48;
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
),
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() {
51 canvas_
.reset(caca_create_canvas(kDefaultCanvasWidth
, kDefaultCanvasHeight
));
53 PLOG(ERROR
) << "failed to create libcaca canvas";
57 display_
.reset(caca_create_display(canvas_
.get()));
59 PLOG(ERROR
) << "failed to initialize libcaca display";
63 caca_set_cursor(display_
.get(), 1);
64 caca_set_display_title(display_
.get(), "Ozone Content Shell");
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
79 base::MessageLoop::current()->PostDelayedTask(
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() {
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.
144 return ui::POST_DISPATCH_STOP_PROPAGATION
;