summaryrefslogtreecommitdiffstats
path: root/views/widget/native_widget_wayland.cc
blob: ea1a1c312d0c352d55049d84d35f736e85e8c736 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// Copyright (c) 2011 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 "views/widget/native_widget_wayland.h"

#include <cairo.h>
#include <cairo-gl.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include <wayland-egl.h>

#include <algorithm>
#include <list>

#include "base/memory/scoped_ptr.h"
#include "ui/base/view_prop.h"
#include "ui/gfx/canvas_skia_paint.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/gl/gl_surface.h"
#include "ui/gfx/gl/gl_surface_egl.h"
#include "ui/wayland/events/wayland_event.h"
#include "ui/wayland/wayland_display.h"
#include "ui/wayland/wayland_input_device.h"
#include "ui/wayland/wayland_screen.h"
#include "ui/wayland/wayland_window.h"
#include "views/ime/input_method_wayland.h"
#include "views/views_delegate.h"
#include "views/widget/native_widget_views.h"
#include "views/widget/root_view.h"
#include "views/widget/tooltip_manager_views.h"

using ui::ViewProp;

namespace views {

namespace {

// Links the WaylandWidget to its NativeWidget.
const char* const kNativeWidgetKey = "__VIEWS_NATIVE_WIDGET__";

}  // namespace

NativeWidgetWayland::NativeWidgetWayland(
                      internal::NativeWidgetDelegate* delegate)
    : delegate_(delegate),
      ALLOW_THIS_IN_INITIALIZER_LIST(close_widget_factory_(this)),
      ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET),
      has_mouse_capture_(false),
      wayland_display_(
       ui:: WaylandDisplay::GetDisplay(gfx::GLSurfaceEGL::GetNativeDisplay())),
      wayland_window_(new ui::WaylandWindow(this, wayland_display_)),
      surface_data_key_(),
      damage_area_() {
}

NativeWidgetWayland::~NativeWidgetWayland() {
  if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
    delete delegate_;

  if (!View::get_use_acceleration_when_possible()) {
    cairo_surface_destroy(cairo_surface_);
    cairo_device_destroy(device_);
  }

  if (egl_window_)
    wl_egl_window_destroy(egl_window_);

  if (wayland_window_)
    delete wayland_window_;
}

void NativeWidgetWayland::InitNativeWidget(const Widget::InitParams& params) {
  // Cannot create a window with a size smaller than 1x1
  allocation_.set_width(std::max(params.bounds.width(), 1));
  allocation_.set_height(std::max(params.bounds.height(), 1));

  egl_window_ = wl_egl_window_create(wayland_window_->surface(),
                                     allocation_.width(),
                                     allocation_.height(),
                                     wayland_display_->visual());

  SetNativeWindowProperty(kNativeWidgetKey, this);

  if (View::get_use_acceleration_when_possible()) {
    if (Widget::compositor_factory()) {
      compositor_ = (*Widget::compositor_factory())();
    } else {
      compositor_ = ui::Compositor::Create(egl_window_, allocation_.size());
    }
    if (compositor_.get())
      delegate_->AsWidget()->GetRootView()->SetPaintToLayer(true);
  } else {
    surface_ = gfx::GLSurface::CreateViewGLSurface(false, egl_window_);
    context_ = gfx::GLContext::CreateGLContext(NULL, surface_.get());

    if (!context_->MakeCurrent(surface_.get()))
      DLOG(ERROR) << "Failed to make surface current";

    device_ = cairo_egl_device_create(gfx::GLSurfaceEGL::GetHardwareDisplay(),
                                      context_->GetHandle());
    if (cairo_device_status(device_) != CAIRO_STATUS_SUCCESS)
      DLOG(ERROR) << "Failed to create cairo egl device";

    cairo_surface_ = cairo_gl_surface_create_for_egl(device_,
                                                     surface_->GetHandle(),
                                                     allocation_.width(),
                                                     allocation_.height());
    cairo_surface_set_user_data(cairo_surface_,
                                &surface_data_key_,
                                this,
                                NULL);
  }

  delegate_->OnNativeWidgetCreated();

  if (params.type != Widget::InitParams::TYPE_TOOLTIP) {
    // TODO(dnicoara) Enable this once it works with Wayland
    /*
    views::TooltipManagerViews* manager = new views::TooltipManagerViews(
        static_cast<internal::RootView*>(GetWidget()->GetRootView()));
    tooltip_manager_.reset(manager);
    */
  }

  // TODO(dnicoara) This should be removed when we can specify the (x, y)
  // coordinates for a window. We use fullscreen since it will center the
  // window rather than give it random (x, y) coordinates.
  wayland_window_->set_fullscreen(true);
  Show();
  OnPaint(allocation_);
}

NonClientFrameView* NativeWidgetWayland::CreateNonClientFrameView() {
  return NULL;
}

void NativeWidgetWayland::UpdateFrameAfterFrameChange() {
  NOTIMPLEMENTED();
}

bool NativeWidgetWayland::ShouldUseNativeFrame() const {
  NOTIMPLEMENTED();
  return false;
}

void NativeWidgetWayland::FrameTypeChanged() {
  // Called when the Theme has changed, so forward the event to the root
  // widget
  GetWidget()->ThemeChanged();
  GetWidget()->GetRootView()->SchedulePaint();
}

Widget* NativeWidgetWayland::GetWidget() {
  return delegate_->AsWidget();
}

const Widget* NativeWidgetWayland::GetWidget() const {
  return delegate_->AsWidget();
}

gfx::NativeView NativeWidgetWayland::GetNativeView() const {
  return wayland_window_;
}

gfx::NativeWindow NativeWidgetWayland::GetNativeWindow() const {
  return wayland_window_;
}

Widget* NativeWidgetWayland::GetTopLevelWidget() {
  NativeWidgetPrivate* native_widget = GetTopLevelNativeWidget(GetNativeView());
  return native_widget ? native_widget->GetWidget() : NULL;
}

const ui::Compositor* NativeWidgetWayland::GetCompositor() const {
  return compositor_.get();
}

ui::Compositor* NativeWidgetWayland::GetCompositor() {
  return compositor_.get();
}

void NativeWidgetWayland::CalculateOffsetToAncestorWithLayer(
    gfx::Point* offset,
    ui::Layer** layer_parent) {
}

void NativeWidgetWayland::ViewRemoved(View* view) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::SetNativeWindowProperty(const char* name,
                                                  void* value) {
  // Remove the existing property (if any).
  for (ViewProps::iterator i = props_.begin(); i != props_.end(); ++i) {
    if ((*i)->Key() == name) {
      props_.erase(i);
      break;
    }
  }

  if (value)
    props_.push_back(new ViewProp(wayland_window_, name, value));
}

void* NativeWidgetWayland::GetNativeWindowProperty(const char* name) const {
  return ViewProp::GetValue(wayland_window_, name);
}

TooltipManager* NativeWidgetWayland::GetTooltipManager() const {
  return tooltip_manager_.get();
}

bool NativeWidgetWayland::IsScreenReaderActive() const {
  return false;
}

void NativeWidgetWayland::SendNativeAccessibilityEvent(
    View* view,
    ui::AccessibilityTypes::Event event_type) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::SetMouseCapture() {
  NOTIMPLEMENTED();
  has_mouse_capture_ = true;
}

void NativeWidgetWayland::ReleaseMouseCapture() {
  NOTIMPLEMENTED();
  has_mouse_capture_ = false;
}

bool NativeWidgetWayland::HasMouseCapture() const {
  NOTIMPLEMENTED();
  return has_mouse_capture_;
}

InputMethod* NativeWidgetWayland::CreateInputMethod() {
  return new InputMethodWayland(this);
}

void NativeWidgetWayland::CenterWindow(const gfx::Size& size) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::GetWindowBoundsAndMaximizedState(
    gfx::Rect* bounds,
    bool* maximized) const {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::SetWindowTitle(const std::wstring& title) {
}

void NativeWidgetWayland::SetWindowIcons(const SkBitmap& window_icon,
                                         const SkBitmap& app_icon) {
}

void NativeWidgetWayland::SetAccessibleName(const std::wstring& name) {
}

void NativeWidgetWayland::SetAccessibleRole(
    ui::AccessibilityTypes::Role role) {
}

void NativeWidgetWayland::SetAccessibleState(
    ui::AccessibilityTypes::State state) {
}

void NativeWidgetWayland::BecomeModal() {
  NOTIMPLEMENTED();
}

gfx::Rect NativeWidgetWayland::GetWindowScreenBounds() const {
  return GetClientAreaScreenBounds();
}

gfx::Rect NativeWidgetWayland::GetClientAreaScreenBounds() const {
  return allocation_;
}

gfx::Rect NativeWidgetWayland::GetRestoredBounds() const {
  return GetWindowScreenBounds();
}

void NativeWidgetWayland::SetBounds(const gfx::Rect& bounds) {
  saved_allocation_ = allocation_;
  allocation_ = bounds;

  // TODO(dnicoara) This needs to be updated to include (x, y).
  wl_egl_window_resize(egl_window_,
                       allocation_.width(),
                       allocation_.height(),
                       0, 0);
  if (!View::get_use_acceleration_when_possible()) {
    cairo_gl_surface_set_size(cairo_surface_,
                              allocation_.width(),
                              allocation_.height());
  }

  if (compositor_.get())
    compositor_->WidgetSizeChanged(allocation_.size());
  delegate_->OnNativeWidgetSizeChanged(allocation_.size());
}

void NativeWidgetWayland::SetSize(const gfx::Size& size) {
  gfx::Rect new_alloc = allocation_;
  new_alloc.set_size(size);

  SetBounds(new_alloc);
}

void NativeWidgetWayland::SetBoundsConstrained(const gfx::Rect& bounds,
                                               Widget* other_widget) {
  // TODO(dnicoara) Need to take into account |other_widget|.
  SetBounds(bounds);
}

void NativeWidgetWayland::MoveAbove(gfx::NativeView native_view) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::MoveToTop() {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::SetShape(gfx::NativeRegion shape) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::Close() {
  Hide();
  if (close_widget_factory_.empty()) {
    MessageLoop::current()->PostTask(FROM_HERE,
        close_widget_factory_.NewRunnableMethod(
            &NativeWidgetWayland::CloseNow));
  }
}

void NativeWidgetWayland::CloseNow() {
  delegate_->OnNativeWidgetDestroying();
  delegate_->OnNativeWidgetDestroyed();
  if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
    delete this;
}

void NativeWidgetWayland::EnableClose(bool enable) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::Show() {
  wayland_window_->SetVisible(true);
  delegate_->OnNativeWidgetVisibilityChanged(true);
}

void NativeWidgetWayland::Hide() {
  wayland_window_->SetVisible(false);
  delegate_->OnNativeWidgetVisibilityChanged(false);
}

void NativeWidgetWayland::ShowMaximizedWithBounds(
    const gfx::Rect& restored_bounds) {
  Show();
  Maximize();
  saved_allocation_ = restored_bounds;
}

void NativeWidgetWayland::ShowWithState(ShowState state) {
  NOTIMPLEMENTED();
}

bool NativeWidgetWayland::IsVisible() const {
  return wayland_window_->IsVisible();
}

void NativeWidgetWayland::Activate() {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::Deactivate() {
  NOTIMPLEMENTED();
}

bool NativeWidgetWayland::IsActive() const {
  NOTIMPLEMENTED();
  return true;
}

void NativeWidgetWayland::SetAlwaysOnTop(bool always_on_top) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::Maximize() {
  std::list<ui::WaylandScreen*> screens = wayland_display_->GetScreenList();

  if (screens.empty())
    return;

  // TODO(dnicoara) We need to intersect the current coordinates with the
  // screen ones and decide the correct screen to fullscreen on.
  ui::WaylandScreen* screen = screens.front();

  SetBounds(screen->GetAllocation());
}

void NativeWidgetWayland::Minimize() {
  NOTIMPLEMENTED();
}

bool NativeWidgetWayland::IsMaximized() const {
  NOTIMPLEMENTED();
  return true;
}

bool NativeWidgetWayland::IsMinimized() const {
  NOTIMPLEMENTED();
  return false;
}

void NativeWidgetWayland::Restore() {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::SetFullscreen(bool fullscreen) {
  gfx::Rect new_allocation = allocation_;

  if (fullscreen) {
    std::list<ui::WaylandScreen*> screens = wayland_display_->GetScreenList();

    if (screens.empty())
      return;

    // TODO(dnicoara) What does it mean to be fullscreen when having multiple
    // monitors? If we're going fullscreen only on one screen then we need to
    // intersect the current coordinates with the screen ones and decide the
    // correct screen to fullscreen on.
    ui::WaylandScreen* screen = screens.front();
    new_allocation = screen->GetAllocation();
  } else {
    new_allocation = saved_allocation_;
  }

  wayland_window_->set_fullscreen(fullscreen);
  SetBounds(new_allocation);
}

bool NativeWidgetWayland::IsFullscreen() const {
  return wayland_window_->fullscreen();
}

void NativeWidgetWayland::SetOpacity(unsigned char opacity) {
  NOTIMPLEMENTED();
}

void NativeWidgetWayland::SetUseDragFrame(bool use_drag_frame) {
  NOTIMPLEMENTED();
}

bool NativeWidgetWayland::IsAccessibleWidget() const {
  NOTIMPLEMENTED();
  return true;
}

void NativeWidgetWayland::RunShellDrag(View* view,
                                       const ui::OSExchangeData& data,
                                       int operation) {
  NOTIMPLEMENTED();
}

gboolean NativeWidgetWayland::IdleRedraw(void* ptr) {
  NativeWidgetWayland* widget = static_cast<NativeWidgetWayland*>(ptr);
  gfx::Rect damage_area = widget->damage_area_;
  widget->damage_area_ = gfx::Rect();

  widget->OnPaint(damage_area);

  return FALSE;
}

void NativeWidgetWayland::SchedulePaintInRect(const gfx::Rect& rect) {
  if (damage_area_.IsEmpty())
    g_idle_add(NativeWidgetWayland::IdleRedraw, this);

  damage_area_ = damage_area_.Union(rect);
}

void NativeWidgetWayland::SetCursor(gfx::NativeCursor cursor) {
  NOTIMPLEMENTED();
}


void NativeWidgetWayland::ClearNativeFocus() {
  NOTIMPLEMENTED();
}


void NativeWidgetWayland::FocusNativeView(gfx::NativeView native_view) {
  NOTIMPLEMENTED();
}

bool NativeWidgetWayland::ConvertPointFromAncestor(
    const Widget* ancestor, gfx::Point* point) const {
  NOTREACHED();
  return false;
}

// Overridden from NativeWidget
gfx::AcceleratedWidget NativeWidgetWayland::GetAcceleratedWidget() {
  return egl_window_;
}


// Overridden from internal::InputMethodDelegate
void NativeWidgetWayland::DispatchKeyEventPostIME(const KeyEvent& key) {
  NOTIMPLEMENTED();
  delegate_->OnKeyEvent(key);
}

/////////////////////////////////////////////////////////////////////////////
// NativeWidgetWayland, private, event handlers

void NativeWidgetWayland::OnPaint(gfx::Rect damage_area) {
  if (!delegate_->OnNativeWidgetPaintAccelerated(damage_area)) {
    // This is required since the CanvasSkiaPaint damages the surface
    // in the destructor so we need to have this done before calling
    // swapbuffers.
    {
      cairo_rectangle_int_t region = damage_area.ToCairoRectangle();
      gfx::CanvasSkiaPaint canvas(cairo_surface_, &region);
      if (!canvas.is_empty()) {
        canvas.set_composite_alpha(false);
        delegate_->OnNativeWidgetPaint(&canvas);
      }
    }

    // Have cairo swap buffers, then let Wayland know of the damaged area.
    cairo_gl_surface_swapbuffers(cairo_surface_);
    wl_surface_damage(wayland_window_->surface(),
                      damage_area.x(), damage_area.y(),
                      damage_area.width(), damage_area.height());
  }
}

void NativeWidgetWayland::OnMotionNotify(ui::WaylandEvent event) {
  MouseEvent mouse_event(&event);
  delegate_->OnMouseEvent(mouse_event);
}

void NativeWidgetWayland::OnButtonNotify(ui::WaylandEvent event) {
  if (event.button.button == ui::SCROLL_UP ||
      event.button.button == ui::SCROLL_DOWN) {
    MouseWheelEvent mouse_event(&event);
    delegate_->OnMouseEvent(mouse_event);
  } else {
    MouseEvent mouse_event(&event);
    delegate_->OnMouseEvent(mouse_event);
  }
}

void NativeWidgetWayland::OnKeyNotify(ui::WaylandEvent event) {
  KeyEvent key_event(&event);
  InputMethod* input_method = GetWidget()->GetInputMethodDirect();
  if (input_method)
    input_method->DispatchKeyEvent(key_event);
  else
    DispatchKeyEventPostIME(key_event);
}

void NativeWidgetWayland::OnPointerFocus(ui::WaylandEvent event) {
  MouseEvent mouse_event(&event);
  delegate_->OnMouseEvent(mouse_event);
}

void NativeWidgetWayland::OnKeyboardFocus(ui::WaylandEvent event) {
  InputMethod* input_method = GetWidget()->GetInputMethodDirect();
  if (input_method) {
    if (event.keyboard_focus.state)
      input_method->OnFocus();
    else
      input_method->OnBlur();
  }
}

void NativeWidgetWayland::OnGeometryChange(ui::WaylandEvent event) {
  SetSize(gfx::Size(event.geometry_change.width,
                    event.geometry_change.height));
}

/////////////////////////////////////////////////////////////////////////////
// Widget

// static
bool Widget::ConvertRect(const Widget* source,
                         const Widget* target,
                         gfx::Rect* rect) {
  DCHECK(source);
  DCHECK(target);
  DCHECK(rect);

  gfx::NativeView source_widget = source->GetNativeView();
  gfx::NativeView target_widget = target->GetNativeView();
  if (source_widget == target_widget)
    return true;

  if (!source_widget || !target_widget)
    return false;

  NOTIMPLEMENTED();
  return false;
}

namespace internal {

/////////////////////////////////////////////////////////////////////////////
// NativeWidget

// static
NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget(
    internal::NativeWidgetDelegate* delegate) {
  if (Widget::IsPureViews() &&
      ViewsDelegate::views_delegate->GetDefaultParentView()) {
    return new NativeWidgetViews(delegate);
  }
  return new NativeWidgetWayland(delegate);
}

// static
NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView(
    gfx::NativeView native_view) {
  return reinterpret_cast<NativeWidgetWayland*>(
      ViewProp::GetValue(native_view, kNativeWidgetKey));
}

// static
NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
    gfx::NativeWindow native_window) {
  return GetNativeWidgetForNativeView(native_window);
}

// static
NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget(
    gfx::NativeView native_view) {
  // TODO(dnicoara) What would be the best way to implement this?
  // Since there isn't any actual parenting concept in Wayland, we could
  // implement it using WaylandWindow->SetParent/GetParent calls.
  return GetNativeWidgetForNativeView(native_view);
}

// static
void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view,
                                             Widget::Widgets* children) {
  NOTIMPLEMENTED();
  if (!native_view)
    return;
}

// static
void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view,
                                             gfx::NativeView new_parent) {
  NOTIMPLEMENTED();
  if (!native_view)
    return;
}

// static
bool NativeWidgetPrivate::IsMouseButtonDown() {
  NOTIMPLEMENTED();
  return false;
}

}  // namespace internal

}  // namespace views