OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/wayland/wayland_cursor.h" |
| 6 |
| 7 #include <cairo.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/wayland/wayland_display.h" |
| 11 #include "ui/wayland/wayland_shm_buffer.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 struct PointerImage { |
| 16 const char* filename; |
| 17 int hotspot_x, hotspot_y; |
| 18 }; |
| 19 |
| 20 // TODO(dnicoara) Add more pointer images and fix the path |
| 21 static const struct PointerImage kPointerImages[] = { |
| 22 {"left_ptr.png", 10, 5}, |
| 23 {"hand.png", 10, 5}, |
| 24 }; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 WaylandCursor::WaylandCursor(WaylandDisplay* display) |
| 29 : display_(display), |
| 30 buffer_(NULL) { |
| 31 } |
| 32 |
| 33 WaylandCursor::~WaylandCursor() { |
| 34 if (buffer_) { |
| 35 delete buffer_; |
| 36 buffer_ = NULL; |
| 37 } |
| 38 } |
| 39 |
| 40 void WaylandCursor::ChangeCursor(WaylandCursorType type) { |
| 41 const struct PointerImage *ptr = &kPointerImages[type]; |
| 42 |
| 43 cairo_surface_t* ptr_surface = cairo_image_surface_create_from_png( |
| 44 ptr->filename); |
| 45 |
| 46 if (!ptr_surface) { |
| 47 LOG(ERROR) << "Failed to create cursor surface"; |
| 48 return; |
| 49 } |
| 50 |
| 51 int width = cairo_image_surface_get_width(ptr_surface); |
| 52 int height = cairo_image_surface_get_height(ptr_surface); |
| 53 |
| 54 if (buffer_) { |
| 55 delete buffer_; |
| 56 buffer_ = NULL; |
| 57 } |
| 58 |
| 59 // TODO(dnicoara) There should be a simpler way to create a wl_buffer for |
| 60 // a cairo surface. Meantime we just copy the contents of the cairo surface |
| 61 // created from file to the cairo surface created using a shm file. |
| 62 buffer_ = new WaylandShmBuffer(display_, width, height); |
| 63 cairo_surface_t* shared_surface = buffer_->GetDataSurface(); |
| 64 |
| 65 cairo_t* cr = cairo_create(shared_surface); |
| 66 cairo_set_source_surface(cr, ptr_surface, 0, 0); |
| 67 cairo_rectangle(cr, 0, 0, width, height); |
| 68 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 69 cairo_fill(cr); |
| 70 |
| 71 display_->SetCursor(buffer_, ptr->hotspot_x, ptr->hotspot_y); |
| 72 } |
OLD | NEW |