summaryrefslogtreecommitdiffstats
path: root/ui/ozone/platform/caca/caca_window.cc
blob: f0dc0a7a81640400cb3b880bf4fc9c735413b907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/caca/caca_window.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/trace_event/trace_event.h"
#include "ui/events/ozone/events_ozone.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/ozone/platform/caca/caca_event_source.h"
#include "ui/ozone/platform/caca/caca_window_manager.h"
#include "ui/platform_window/platform_window_delegate.h"

namespace ui {

namespace {

// Size of initial cnavas (in characters).
const int kDefaultCanvasWidth = 160;
const int kDefaultCanvasHeight = 48;

}  // namespace

// TODO(dnicoara) As an optimization, |bitmap_size_| should be scaled based on
// |physical_size_| and font size.
CacaWindow::CacaWindow(PlatformWindowDelegate* delegate,
                       CacaWindowManager* manager,
                       CacaEventSource* event_source,
                       const gfx::Rect& bounds)
    : delegate_(delegate),
      manager_(manager),
      event_source_(event_source),
      weak_ptr_factory_(this) {
  widget_ = manager_->AddWindow(this);
  ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
  delegate_->OnAcceleratedWidgetAvailable(widget_);
}

CacaWindow::~CacaWindow() {
  ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
  manager_->RemoveWindow(widget_, this);
}

bool CacaWindow::Initialize() {
  if (display_)
    return true;

  canvas_.reset(caca_create_canvas(kDefaultCanvasWidth, kDefaultCanvasHeight));
  if (!canvas_) {
    PLOG(ERROR) << "failed to create libcaca canvas";
    return false;
  }

  display_.reset(caca_create_display(canvas_.get()));
  if (!display_) {
    PLOG(ERROR) << "failed to initialize libcaca display";
    return false;
  }

  caca_set_cursor(display_.get(), 1);
  caca_set_display_title(display_.get(), "Ozone Content Shell");

  UpdateDisplaySize();

  TryProcessingEvent();

  return true;
}

void CacaWindow::TryProcessingEvent() {
  event_source_->TryProcessingEvent(this);

  // Caca uses a poll based event retrieval. Since we don't want to block we'd
  // either need to spin up a new thread or just poll. For simplicity just poll
  // for messages.
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&CacaWindow::TryProcessingEvent,
                 weak_ptr_factory_.GetWeakPtr()),
      base::TimeDelta::FromMilliseconds(10));
}

void CacaWindow::UpdateDisplaySize() {
  physical_size_.SetSize(caca_get_canvas_width(canvas_.get()),
                         caca_get_canvas_height(canvas_.get()));

  bitmap_size_.SetSize(caca_get_display_width(display_.get()),
                       caca_get_display_height(display_.get()));
}

void CacaWindow::OnCacaResize() {
  UpdateDisplaySize();

  delegate_->OnBoundsChanged(gfx::Rect(bitmap_size_));
}

void CacaWindow::OnCacaQuit() {
  delegate_->OnCloseRequest();
}


void CacaWindow::OnCacaEvent(ui::Event* event) {
  DispatchEventFromNativeUiEvent(
      event, base::Bind(&PlatformWindowDelegate::DispatchEvent,
                        base::Unretained(delegate_)));
}

gfx::Rect CacaWindow::GetBounds() { return gfx::Rect(bitmap_size_); }

void CacaWindow::SetBounds(const gfx::Rect& bounds) {}

void CacaWindow::Show() {}

void CacaWindow::Hide() {}

void CacaWindow::Close() {}

void CacaWindow::SetCapture() {}

void CacaWindow::ReleaseCapture() {}

void CacaWindow::ToggleFullscreen() {}

void CacaWindow::Maximize() {}

void CacaWindow::Minimize() {}

void CacaWindow::Restore() {}

void CacaWindow::SetCursor(PlatformCursor cursor) {}

void CacaWindow::MoveCursorTo(const gfx::Point& location) {}

void CacaWindow::ConfineCursorToBounds(const gfx::Rect& bounds) {}

bool CacaWindow::CanDispatchEvent(const PlatformEvent& event) { return true; }

uint32_t CacaWindow::DispatchEvent(const PlatformEvent& ne) {
  // We don't dispatch events via PlatformEventSource.
  NOTREACHED();
  return ui::POST_DISPATCH_STOP_PROPAGATION;
}

}  // namespace ui