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_display.h" |
| 6 |
| 7 #include <string.h> |
| 8 #include <wayland-client.h> |
| 9 |
| 10 #include "ui/wayland/wayland_buffer.h" |
| 11 #include "ui/wayland/wayland_input_device.h" |
| 12 #include "ui/wayland/wayland_screen.h" |
| 13 #include "ui/wayland/wayland_window.h" |
| 14 |
| 15 // static |
| 16 WaylandDisplay* WaylandDisplay::Connect(char* name) { |
| 17 WaylandDisplay* display = new WaylandDisplay(name); |
| 18 if (!display->display_) { |
| 19 delete display; |
| 20 display = NULL; |
| 21 return NULL; |
| 22 } |
| 23 |
| 24 wl_display_set_user_data(display->display_, display); |
| 25 // Register the display initialization handler and iterate over the initial |
| 26 // connection events sent by the server. This is required since the display |
| 27 // will send registration events needed to initialize everything else. This |
| 28 // will create the compositor, visuals, etc.., which are required in creating |
| 29 // a drawing context. |
| 30 wl_display_add_global_listener(display->display_, |
| 31 WaylandDisplay::DisplayHandleGlobal, |
| 32 display); |
| 33 wl_display_iterate(display->display_, WL_DISPLAY_READABLE); |
| 34 |
| 35 return display; |
| 36 } |
| 37 |
| 38 // static |
| 39 WaylandDisplay* WaylandDisplay::GetDisplay(wl_display* display) { |
| 40 return static_cast<WaylandDisplay*>(wl_display_get_user_data(display)); |
| 41 } |
| 42 |
| 43 WaylandDisplay::WaylandDisplay(char* name) : display_(NULL), |
| 44 compositor_(NULL), |
| 45 shell_(NULL), |
| 46 shm_(NULL), |
| 47 visual_(NULL) { |
| 48 display_ = wl_display_connect(name); |
| 49 } |
| 50 |
| 51 WaylandDisplay::~WaylandDisplay() { |
| 52 if (display_) |
| 53 wl_display_destroy(display_); |
| 54 if (compositor_) |
| 55 wl_compositor_destroy(compositor_); |
| 56 if (visual_) |
| 57 wl_visual_destroy(visual_); |
| 58 if (shell_) |
| 59 wl_shell_destroy(shell_); |
| 60 if (shm_) |
| 61 wl_shm_destroy(shm_); |
| 62 for (std::list<WaylandInputDevice*>::iterator it = input_list_.begin(); |
| 63 it != input_list_.end(); it++) { |
| 64 delete *it; |
| 65 } |
| 66 for (std::list<WaylandScreen*>::iterator it = screen_list_.begin(); |
| 67 it != screen_list_.end(); it++) { |
| 68 delete *it; |
| 69 } |
| 70 } |
| 71 |
| 72 wl_surface* WaylandDisplay::CreateSurface() { |
| 73 return wl_compositor_create_surface(compositor_); |
| 74 } |
| 75 |
| 76 void WaylandDisplay::SetCursor(WaylandBuffer* buffer, |
| 77 int32_t x, int32_t y) { |
| 78 // Currently there is no way of knowing which input device should have the |
| 79 // buffer attached, so we just attach to every input device. |
| 80 for (std::list<WaylandInputDevice*>::iterator it = input_list_.begin(); |
| 81 it != input_list_.end(); it++) { |
| 82 (*it)->Attach(buffer->GetBuffer(), x, y); |
| 83 } |
| 84 } |
| 85 |
| 86 wl_display* WaylandDisplay::GetNativeDisplay() const { |
| 87 return display_; |
| 88 } |
| 89 |
| 90 std::list<WaylandScreen*> WaylandDisplay::GetScreenList() const { |
| 91 return screen_list_; |
| 92 } |
| 93 |
| 94 wl_shell* WaylandDisplay::GetShell() const { |
| 95 return shell_; |
| 96 } |
| 97 |
| 98 wl_shm* WaylandDisplay::GetShm() const { |
| 99 return shm_; |
| 100 } |
| 101 |
| 102 wl_visual* WaylandDisplay::GetVisual() const { |
| 103 return visual_; |
| 104 } |
| 105 |
| 106 // static |
| 107 void WaylandDisplay::DisplayHandleGlobal(wl_display* display, |
| 108 uint32_t id, |
| 109 const char* interface, |
| 110 uint32_t version, |
| 111 void* data) { |
| 112 WaylandDisplay* disp = static_cast<WaylandDisplay*>(data); |
| 113 |
| 114 static const wl_compositor_listener compositor_listener = { |
| 115 WaylandDisplay::CompositorHandleVisual, |
| 116 }; |
| 117 static const wl_shell_listener shell_listener = { |
| 118 WaylandDisplay::ShellHandleConfigure, |
| 119 }; |
| 120 |
| 121 if (strcmp(interface, "wl_compositor") == 0) { |
| 122 disp->compositor_ = wl_compositor_create(display, id, 1); |
| 123 wl_compositor_add_listener(disp->compositor_, |
| 124 &compositor_listener, |
| 125 disp); |
| 126 } else if (strcmp(interface, "wl_output") == 0) { |
| 127 WaylandScreen* screen = new WaylandScreen(disp, id); |
| 128 disp->screen_list_.push_back(screen); |
| 129 } else if (strcmp(interface, "wl_input_device") == 0) { |
| 130 WaylandInputDevice *input_device = new WaylandInputDevice(display, id); |
| 131 disp->input_list_.push_back(input_device); |
| 132 } else if (strcmp(interface, "wl_shell") == 0) { |
| 133 disp->shell_ = wl_shell_create(display, id, 1); |
| 134 wl_shell_add_listener(disp->shell_, &shell_listener, disp); |
| 135 } else if (strcmp(interface, "wl_shm") == 0) { |
| 136 disp->shm_ = wl_shm_create(display, id, 1); |
| 137 } |
| 138 } |
| 139 |
| 140 // static |
| 141 void WaylandDisplay::CompositorHandleVisual(void* data, |
| 142 wl_compositor* compositor, |
| 143 uint32_t id, |
| 144 uint32_t token) { |
| 145 WaylandDisplay* display = static_cast<WaylandDisplay*>(data); |
| 146 |
| 147 // The compositor may support multiple types of visuals but we really only |
| 148 // need one. |
| 149 switch (token) { |
| 150 case WL_COMPOSITOR_VISUAL_ARGB32: |
| 151 break; |
| 152 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32: |
| 153 display->visual_ = wl_visual_create(display->display_, id, 1); |
| 154 break; |
| 155 case WL_COMPOSITOR_VISUAL_XRGB32: |
| 156 break; |
| 157 } |
| 158 } |
| 159 |
| 160 // static |
| 161 void WaylandDisplay::ShellHandleConfigure(void* data, |
| 162 wl_shell* shell, |
| 163 uint32_t time, |
| 164 uint32_t edges, |
| 165 wl_surface* surface, |
| 166 int32_t width, |
| 167 int32_t height) { |
| 168 WaylandWindow* window = static_cast<WaylandWindow*>( |
| 169 wl_surface_get_user_data(surface)); |
| 170 window->Configure(time, edges, 0, 0, width, height); |
| 171 } |
OLD | NEW |