1 // Copyright 2013 Intel Corporation. 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.
5 #include "ozone/wayland/input/cursor.h"
6 #include "ozone/wayland/surface.h"
8 namespace ozonewayland {
10 class WaylandCursorData {
12 explicit WaylandCursorData(wl_shm* shm);
13 virtual ~WaylandCursorData();
15 static WaylandCursorData* GetInstance() {
19 static void InitializeCursorData(wl_shm* shm) {
21 impl_ = new WaylandCursorData(shm);
24 static void DestroyCursorData() {
31 struct wl_cursor_image* GetCursorImage(int index);
34 wl_cursor_theme *cursor_theme_;
36 static WaylandCursorData* impl_;
39 WaylandCursorData* WaylandCursorData::impl_ = NULL;
41 WaylandCursorData::WaylandCursorData(wl_shm* shm) {
42 const char *cursor_names[] = {
44 "bottom_right_corner",
57 // (kalyan) We should be able to configure the size of cursor and theme name.
58 unsigned int i, array_size =
59 (sizeof(cursor_names) / sizeof(cursor_names[0]));
60 cursor_theme_ = wl_cursor_theme_load(NULL, 24, shm);
61 cursors_ = new wl_cursor*[array_size];
62 memset(cursors_, 0, sizeof(cursors_) * array_size);
64 for (i = 0; i < array_size; i++)
65 cursors_[i] = wl_cursor_theme_get_cursor(cursor_theme_, cursor_names[i]);
68 struct wl_cursor_image* WaylandCursorData::GetCursorImage(int index) {
69 struct wl_cursor *cursor = cursors_[index];
73 return cursor->images[0];
76 WaylandCursorData::~WaylandCursorData() {
78 wl_cursor_theme_destroy(cursor_theme_);
88 WaylandCursor::WaylandCursor(wl_shm* shm) : input_pointer_(NULL),
93 pointer_surface_ = new WaylandSurface();
94 WaylandCursorData::InitializeCursorData(shm);
97 WaylandCursor::~WaylandCursor() {
98 if (pointer_surface_) {
99 delete pointer_surface_;
100 pointer_surface_ = NULL;
104 void WaylandCursor::Update(CursorType type, uint32_t serial) {
108 ValidateBuffer(type, serial);
109 struct wl_surface* surface = pointer_surface_->wlSurface();
110 wl_surface_attach(surface, buffer_, 0, 0);
111 wl_surface_damage(surface, 0, 0, width_, height_);
112 wl_surface_commit(surface);
115 void WaylandCursor::SetInputPointer(wl_pointer* pointer) {
116 input_pointer_ = pointer;
119 void WaylandCursor::ValidateBuffer(CursorType type, uint32_t serial) {
123 struct wl_cursor_image* image = WaylandCursorData::GetInstance()->
124 GetCursorImage(type - 1);
125 buffer_ = wl_cursor_image_get_buffer(image);
126 width_ = image->width;
127 height_ = image->height;
128 wl_pointer_set_cursor(input_pointer_,
130 pointer_surface_->wlSurface(),
135 void WaylandCursor::Clear() {
136 WaylandCursorData::DestroyCursorData();
139 } // namespace ozonewayland