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 17namespace ui { 18 19namespace { 20 21// Size of initial cnavas (in characters). 22const int kDefaultCanvasWidth = 160; 23const 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. 29CacaWindow::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 42CacaWindow::~CacaWindow() { 43 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this); 44 manager_->RemoveWindow(widget_, this); 45} 46 47bool 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 73void 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 86void 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 94void CacaWindow::OnCacaResize() { 95 UpdateDisplaySize(); 96 97 delegate_->OnBoundsChanged(gfx::Rect(bitmap_size_)); 98} 99 100void CacaWindow::OnCacaQuit() { 101 delegate_->OnCloseRequest(); 102} 103 104 105void CacaWindow::OnCacaEvent(ui::Event* event) { 106 DispatchEventFromNativeUiEvent( 107 event, 108 base::Bind(&PlatformWindowDelegate::DispatchEvent, 109 base::Unretained(delegate_))); 110} 111 112gfx::Rect CacaWindow::GetBounds() { return gfx::Rect(bitmap_size_); } 113 114void CacaWindow::SetBounds(const gfx::Rect& bounds) {} 115 116void CacaWindow::Show() {} 117 118void CacaWindow::Hide() {} 119 120void CacaWindow::Close() {} 121 122void CacaWindow::SetCapture() {} 123 124void CacaWindow::ReleaseCapture() {} 125 126void CacaWindow::ToggleFullscreen() {} 127 128void CacaWindow::Maximize() {} 129 130void CacaWindow::Minimize() {} 131 132void CacaWindow::Restore() {} 133 134void CacaWindow::SetCursor(PlatformCursor cursor) {} 135 136void CacaWindow::MoveCursorTo(const gfx::Point& location) {} 137 138bool CacaWindow::CanDispatchEvent(const PlatformEvent& event) { return true; } 139 140uint32_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