Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / platform / caca / caca_window.cc
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.
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
42 CacaWindow::~CacaWindow() {
43   ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
44   manager_->RemoveWindow(widget_, this);
45 }
46
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
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
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
94 void CacaWindow::OnCacaResize() {
95   UpdateDisplaySize();
96
97   delegate_->OnBoundsChanged(gfx::Rect(bitmap_size_));
98 }
99
100 void CacaWindow::OnCacaQuit() {
101   delegate_->OnCloseRequest();
102 }
103
104
105 void CacaWindow::OnCacaEvent(ui::Event* event) {
106   DispatchEventFromNativeUiEvent(
107       event, base::Bind(&PlatformWindowDelegate::DispatchEvent,
108                         base::Unretained(delegate_)));
109 }
110
111 gfx::Rect CacaWindow::GetBounds() { return gfx::Rect(bitmap_size_); }
112
113 void CacaWindow::SetBounds(const gfx::Rect& bounds) {}
114
115 void CacaWindow::Show() {}
116
117 void CacaWindow::Hide() {}
118
119 void CacaWindow::Close() {}
120
121 void CacaWindow::SetCapture() {}
122
123 void CacaWindow::ReleaseCapture() {}
124
125 void CacaWindow::ToggleFullscreen() {}
126
127 void CacaWindow::Maximize() {}
128
129 void CacaWindow::Minimize() {}
130
131 void CacaWindow::Restore() {}
132
133 void CacaWindow::SetCursor(PlatformCursor cursor) {}
134
135 void CacaWindow::MoveCursorTo(const gfx::Point& location) {}
136
137 bool CacaWindow::CanDispatchEvent(const PlatformEvent& event) { return true; }
138
139 uint32_t CacaWindow::DispatchEvent(const PlatformEvent& ne) {
140   // We don't dispatch events via PlatformEventSource.
141   NOTREACHED();
142   return ui::POST_DISPATCH_STOP_PROPAGATION;
143 }
144
145 }  // namespace ui