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 "wayland_display.h" |
| 6 |
| 7 #include "wayland_buffer.h" |
| 8 #include "wayland_input_device.h" |
| 9 #include "wayland_screen.h" |
| 10 #include "wayland_window.h" |
| 11 |
| 12 #include <stdio.h> |
| 13 #include <string.h> |
| 14 #include <wayland-client.h> |
| 15 |
| 16 // static |
| 17 WaylandDisplay* WaylandDisplay::Connect(char* name) { |
| 18 static WaylandDisplay *display = NULL; |
| 19 if (display) { |
| 20 return display; |
| 21 } |
| 22 |
| 23 display = new WaylandDisplay(name); |
| 24 if (!display->display_) { |
| 25 delete display; |
| 26 display = NULL; |
| 27 return NULL; |
| 28 } |
| 29 |
| 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 WaylandDisplay::WaylandDisplay(char* name) : display_(NULL), |
| 39 compositor_(NULL), |
| 40 shell_(NULL), |
| 41 shm_(NULL), |
| 42 visual_(NULL) { |
| 43 display_ = wl_display_connect(name); |
| 44 } |
| 45 |
| 46 WaylandDisplay::~WaylandDisplay() { |
| 47 if (display_) { |
| 48 wl_display_destroy(display_); |
| 49 } |
| 50 if (compositor_) { |
| 51 wl_compositor_destroy(compositor_); |
| 52 } |
| 53 if (visual_) { |
| 54 wl_visual_destroy(visual_); |
| 55 } |
| 56 if (shell_) { |
| 57 wl_shell_destroy(shell_); |
| 58 } |
| 59 if (shm_) { |
| 60 wl_shm_destroy(shm_); |
| 61 } |
| 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 struct 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 for (std::list<WaylandInputDevice*>::iterator it = input_list_.begin(); |
| 79 it != input_list_.end(); it++) { |
| 80 (*it)->Attach(buffer->GetBuffer(), x, y); |
| 81 } |
| 82 } |
| 83 |
| 84 struct wl_display* WaylandDisplay::GetNativeDisplay() const { |
| 85 return display_; |
| 86 } |
| 87 |
| 88 std::list<WaylandScreen*> WaylandDisplay::GetScreenList() const { |
| 89 return screen_list_; |
| 90 } |
| 91 |
| 92 struct wl_shell* WaylandDisplay::GetShell() const { |
| 93 return shell_; |
| 94 } |
| 95 |
| 96 struct wl_shm* WaylandDisplay::GetShm() const { |
| 97 return shm_; |
| 98 } |
| 99 |
| 100 struct wl_visual* WaylandDisplay::GetVisual() const { |
| 101 return visual_; |
| 102 } |
| 103 |
| 104 // static |
| 105 void WaylandDisplay::DisplayHandleGlobal(struct wl_display* display, |
| 106 uint32_t id, |
| 107 const char* interface, |
| 108 uint32_t version, |
| 109 void* data) { |
| 110 WaylandDisplay* disp = static_cast<WaylandDisplay*>(data); |
| 111 |
| 112 static const struct wl_compositor_listener compositor_listener = { |
| 113 WaylandDisplay::CompositorHandleVisual, |
| 114 }; |
| 115 static const struct wl_shell_listener shell_listener = { |
| 116 WaylandDisplay::ShellHandleConfigure, |
| 117 }; |
| 118 |
| 119 //fprintf(stderr, "Processing: %s\n", interface); |
| 120 if (strcmp(interface, "wl_compositor") == 0) { |
| 121 disp->compositor_ = wl_compositor_create(display, id, 1); |
| 122 wl_compositor_add_listener(disp->compositor_, |
| 123 &compositor_listener, |
| 124 disp); |
| 125 } else if (strcmp(interface, "wl_output") == 0) { |
| 126 WaylandScreen* screen = new WaylandScreen(disp, id); |
| 127 disp->screen_list_.push_back(screen); |
| 128 } else if (strcmp(interface, "wl_input_device") == 0) { |
| 129 WaylandInputDevice *input_device = new WaylandInputDevice(display, id); |
| 130 disp->input_list_.push_back(input_device); |
| 131 } else if (strcmp(interface, "wl_shell") == 0) { |
| 132 disp->shell_ = wl_shell_create(display, id, 1); |
| 133 wl_shell_add_listener(disp->shell_, &shell_listener, disp); |
| 134 } else if (strcmp(interface, "wl_shm") == 0) { |
| 135 disp->shm_ = wl_shm_create(display, id, 1); |
| 136 } |
| 137 } |
| 138 |
| 139 // static |
| 140 void WaylandDisplay::CompositorHandleVisual(void* data, |
| 141 struct wl_compositor* compositor, |
| 142 uint32_t id, |
| 143 uint32_t token) { |
| 144 WaylandDisplay* display = static_cast<WaylandDisplay*>(data); |
| 145 |
| 146 switch (token) { |
| 147 case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32: |
| 148 display->visual_ = wl_visual_create(display->display_, id, 1); |
| 149 break; |
| 150 } |
| 151 } |
| 152 |
| 153 // static |
| 154 void WaylandDisplay::ShellHandleConfigure(void* data, |
| 155 struct wl_shell* shell, |
| 156 uint32_t time, |
| 157 uint32_t edges, |
| 158 struct wl_surface* surface, |
| 159 int32_t width, |
| 160 int32_t height) { |
| 161 WaylandWindow* window = static_cast<WaylandWindow*>( |
| 162 wl_surface_get_user_data(surface)); |
| 163 window->Configure(time, edges, 0, 0, width, height); |
| 164 } |
OLD | NEW |