OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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_display.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 #include <stdio.h> |
| 9 #include <string.h> |
| 10 #include <wayland-client.h> |
| 11 #include <wayland-cursor.h> |
| 12 #include <wayland-egl.h> |
| 13 |
| 14 #include "ui/wayland/wayland_input_device.h" |
| 15 #include "ui/wayland/wayland_screen.h" |
| 16 #include "ui/wayland/wayland_window.h" |
| 17 #include "ui/wayland/wayland_input_method_event_filter.h" |
| 18 #include "base/message_loop.h" |
| 19 |
| 20 #ifdef USE_CAIRO_GLESV2 |
| 21 #include <GLES2/gl2.h> |
| 22 #include <GLES2/gl2ext.h> |
| 23 #else |
| 24 #include <GL/gl.h> |
| 25 #endif |
| 26 |
| 27 #include <EGL/egl.h> |
| 28 #include <EGL/eglext.h> |
| 29 |
| 30 namespace ui { |
| 31 |
| 32 WaylandDisplay* g_display = NULL; |
| 33 |
| 34 WaylandDisplay* WaylandDisplay::GetDisplay() |
| 35 { |
| 36 if (!g_display) |
| 37 g_display = WaylandDisplay::Connect(NULL); |
| 38 return g_display; |
| 39 } |
| 40 |
| 41 void WaylandDisplay::DestroyDisplay() |
| 42 { |
| 43 if(g_display) |
| 44 delete g_display; |
| 45 g_display = NULL; |
| 46 } |
| 47 |
| 48 // static |
| 49 WaylandDisplay* WaylandDisplay::Connect(char* name) |
| 50 { |
| 51 static const struct wl_registry_listener registry_listener = { |
| 52 WaylandDisplay::DisplayHandleGlobal |
| 53 }; |
| 54 |
| 55 WaylandDisplay* display = new WaylandDisplay(name); |
| 56 if (!display->display_) { |
| 57 delete display; |
| 58 return NULL; |
| 59 } |
| 60 |
| 61 wl_display_set_user_data(display->display_, display); |
| 62 |
| 63 display->registry_ = wl_display_get_registry(display->display_); |
| 64 wl_registry_add_listener(display->registry_, ®istry_listener, display); |
| 65 |
| 66 wl_display_dispatch(display->display_); |
| 67 |
| 68 display->CreateCursors(); |
| 69 |
| 70 return display; |
| 71 } |
| 72 |
| 73 void WaylandDisplay::AddWindow(WaylandWindow* window) |
| 74 { |
| 75 if(window) |
| 76 window_list_.push_back(window); |
| 77 } |
| 78 |
| 79 void WaylandDisplay::AddTask(WaylandTask* task) |
| 80 { |
| 81 if(task) |
| 82 task_list_.push_back(task); |
| 83 } |
| 84 |
| 85 void WaylandDisplay::ProcessTasks() |
| 86 { |
| 87 WaylandTask *task = NULL; |
| 88 |
| 89 while(!task_list_.empty()) |
| 90 { |
| 91 task = task_list_.front(); |
| 92 task->Run(); |
| 93 task_list_.pop_front(); |
| 94 delete task; |
| 95 } |
| 96 } |
| 97 |
| 98 void WaylandDisplay::RemoveWindow(WaylandWindow* window) |
| 99 { |
| 100 if(!window) |
| 101 return; |
| 102 |
| 103 WaylandTask *task = NULL; |
| 104 |
| 105 for (std::list<WaylandTask*>::iterator i = task_list_.begin(); |
| 106 i != task_list_.end(); ++i) { |
| 107 if((*i)->GetWindow() == window) |
| 108 { |
| 109 delete *i; |
| 110 i = task_list_.erase(i); |
| 111 } |
| 112 } |
| 113 |
| 114 for (std::list<WaylandWindow*>::iterator i = window_list_.begin(); |
| 115 i != window_list_.end(); ++i) { |
| 116 if((*i) == window) |
| 117 { |
| 118 i = window_list_.erase(i); |
| 119 break; |
| 120 } |
| 121 } |
| 122 |
| 123 if(window_list_.size() < 1) |
| 124 { |
| 125 base::MessageLoop::current()->PostTask(FROM_HERE, base::MessageLoop::QuitClo
sure()); |
| 126 } |
| 127 } |
| 128 |
| 129 bool WaylandDisplay::IsWindow(WaylandWindow* window) |
| 130 { |
| 131 for (std::list<WaylandWindow*>::iterator i = window_list_.begin(); |
| 132 i != window_list_.end(); ++i) { |
| 133 if((*i) == window) |
| 134 { |
| 135 return true; |
| 136 } |
| 137 } |
| 138 |
| 139 return false; |
| 140 } |
| 141 |
| 142 InputMethod* WaylandDisplay::GetInputMethod() const |
| 143 { |
| 144 return input_method_filter_ ? input_method_filter_->GetInputMethod(): NULL; |
| 145 } |
| 146 |
| 147 // static |
| 148 WaylandDisplay* WaylandDisplay::GetDisplay(wl_display* display) |
| 149 { |
| 150 return static_cast<WaylandDisplay*>(wl_display_get_user_data(display)); |
| 151 } |
| 152 |
| 153 WaylandDisplay::WaylandDisplay(char* name) : display_(NULL), |
| 154 cursor_theme_(NULL), |
| 155 cursors_(NULL), |
| 156 compositor_(NULL), |
| 157 shell_(NULL), |
| 158 shm_(NULL) |
| 159 { |
| 160 display_ = wl_display_connect(name); |
| 161 input_method_filter_ = new WaylandInputMethodEventFilter; |
| 162 } |
| 163 |
| 164 WaylandDisplay::~WaylandDisplay() |
| 165 { |
| 166 if (window_list_.size() > 0) |
| 167 fprintf(stderr, "toytoolkit warning: windows exist.\n"); |
| 168 |
| 169 if (task_list_.size() > 0) |
| 170 fprintf(stderr, "toytoolkit warning: deferred tasks exist.\n"); |
| 171 |
| 172 for (std::list<WaylandInputDevice*>::iterator i = input_list_.begin(); |
| 173 i != input_list_.end(); ++i) { |
| 174 delete *i; |
| 175 } |
| 176 |
| 177 for (std::list<WaylandScreen*>::iterator i = screen_list_.begin(); |
| 178 i != screen_list_.end(); ++i) { |
| 179 delete *i; |
| 180 } |
| 181 |
| 182 DestroyCursors(); |
| 183 |
| 184 if (compositor_) |
| 185 wl_compositor_destroy(compositor_); |
| 186 if (shell_) |
| 187 wl_shell_destroy(shell_); |
| 188 if (shm_) |
| 189 wl_shm_destroy(shm_); |
| 190 if (display_) |
| 191 wl_display_disconnect(display_); |
| 192 |
| 193 delete input_method_filter_; |
| 194 } |
| 195 |
| 196 wl_surface* WaylandDisplay::CreateSurface() |
| 197 { |
| 198 return wl_compositor_create_surface(compositor_); |
| 199 } |
| 200 |
| 201 std::list<WaylandScreen*> WaylandDisplay::GetScreenList() const { |
| 202 return screen_list_; |
| 203 } |
| 204 |
| 205 // static |
| 206 void WaylandDisplay::DisplayHandleGlobal(void *data, |
| 207 struct wl_registry *registry, |
| 208 uint32_t name, |
| 209 const char *interface, |
| 210 uint32_t version) |
| 211 { |
| 212 |
| 213 WaylandDisplay* disp = static_cast<WaylandDisplay*>(data); |
| 214 |
| 215 if (strcmp(interface, "wl_compositor") == 0) { |
| 216 disp->compositor_ = static_cast<wl_compositor*>( |
| 217 wl_registry_bind(registry, name, &wl_compositor_interface, 1)); |
| 218 } else if (strcmp(interface, "wl_output") == 0) { |
| 219 WaylandScreen* screen = new WaylandScreen(disp, name); |
| 220 disp->screen_list_.push_back(screen); |
| 221 } else if (strcmp(interface, "wl_seat") == 0) { |
| 222 WaylandInputDevice *input_device = new WaylandInputDevice(disp, name); |
| 223 disp->input_list_.push_back(input_device); |
| 224 } else if (strcmp(interface, "wl_shell") == 0) { |
| 225 disp->shell_ = static_cast<wl_shell*>( |
| 226 wl_registry_bind(registry, name, &wl_shell_interface, 1)); |
| 227 } else if (strcmp(interface, "wl_shm") == 0) { |
| 228 disp->shm_ = static_cast<wl_shm*>( |
| 229 wl_registry_bind(registry, name, &wl_shm_interface, 1)); |
| 230 } |
| 231 } |
| 232 |
| 233 static const char *cursor_names[] = { |
| 234 "bottom_left_corner", |
| 235 "bottom_right_corner", |
| 236 "bottom_side", |
| 237 "grabbing", |
| 238 "left_ptr", |
| 239 "left_side", |
| 240 "right_side", |
| 241 "top_left_corner", |
| 242 "top_right_corner", |
| 243 "top_side", |
| 244 "xterm", |
| 245 "hand1", |
| 246 }; |
| 247 |
| 248 void WaylandDisplay::CreateCursors() |
| 249 { |
| 250 unsigned int i, array_size = sizeof(cursor_names) / sizeof(cursor_names[0]); |
| 251 cursor_theme_ = wl_cursor_theme_load(NULL, 32, shm_); |
| 252 cursors_ = new wl_cursor*[array_size]; |
| 253 memset(cursors_, 0, sizeof(wl_cursor*) * array_size); |
| 254 |
| 255 for(i = 0; i < array_size; i++) |
| 256 cursors_[i] = wl_cursor_theme_get_cursor(cursor_theme_, cursor_names[i]); |
| 257 } |
| 258 |
| 259 void WaylandDisplay::DestroyCursors() |
| 260 { |
| 261 if(cursor_theme_) |
| 262 { |
| 263 wl_cursor_theme_destroy(cursor_theme_); |
| 264 cursor_theme_ = NULL; |
| 265 } |
| 266 |
| 267 if(cursors_) |
| 268 { |
| 269 delete[] cursors_; |
| 270 cursors_ = NULL; |
| 271 } |
| 272 } |
| 273 |
| 274 void WaylandDisplay::SetPointerImage(WaylandInputDevice *device, uint32_t time,
int index) |
| 275 { |
| 276 struct wl_buffer *buffer; |
| 277 struct wl_cursor *cursor; |
| 278 struct wl_cursor_image *image; |
| 279 |
| 280 if (index == device->GetCurrentPointerImage()) |
| 281 return; |
| 282 |
| 283 cursor = cursors_[index]; |
| 284 if (!cursor) |
| 285 return; |
| 286 |
| 287 image = cursor->images[0]; |
| 288 buffer = wl_cursor_image_get_buffer(image); |
| 289 if (!buffer) |
| 290 return; |
| 291 |
| 292 device->SetCurrentPointerImage(index); |
| 293 // TODO: |
| 294 // wl_pointer_set_cursor(device->GetPointer(), time, buffer, |
| 295 // image->hotspot_x, image->hotspot_y); |
| 296 } |
| 297 |
| 298 cairo_surface_t* WaylandDisplay::CreateSurfaceFromFile(const char *filename, gfx
::Rect *rect) |
| 299 { |
| 300 return NULL; |
| 301 } |
| 302 |
| 303 void WaylandDisplay::AddWindowCallback(const wl_callback_listener *listener, Way
landWindow *window) |
| 304 { |
| 305 wl_callback *cb; |
| 306 cb = wl_display_sync(display_); |
| 307 wl_callback_add_listener(cb, listener, window); |
| 308 } |
| 309 |
| 310 cairo_surface_t* WaylandDisplay::CreateSurface(wl_surface *surface, gfx::Rect *r
ect, uint32_t flags) |
| 311 { |
| 312 return NULL; |
| 313 } |
| 314 |
| 315 } // namespace ui |
OLD | NEW |