diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-11-18 08:22:37 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-11-18 08:22:53 -0500 |
commit | 399220f14bc60581490936d9f1a0fd353bfc9ef5 (patch) | |
tree | ec086840b4f340029e775d324279730fda64a9ad /wayland | |
parent | Track pid of child process from exec (diff) | |
download | sway-399220f14bc60581490936d9f1a0fd353bfc9ef5.tar.gz sway-399220f14bc60581490936d9f1a0fd353bfc9ef5.tar.zst sway-399220f14bc60581490936d9f1a0fd353bfc9ef5.zip |
Fix up wayland client implementation
Now it receives frame callbacks and renders properly, and is double
buffered and such.
Diffstat (limited to 'wayland')
-rw-r--r-- | wayland/buffers.c | 116 | ||||
-rw-r--r-- | wayland/client.c | 113 |
2 files changed, 140 insertions, 89 deletions
diff --git a/wayland/buffers.c b/wayland/buffers.c new file mode 100644 index 00000000..13193423 --- /dev/null +++ b/wayland/buffers.c | |||
@@ -0,0 +1,116 @@ | |||
1 | #include <wayland-client.h> | ||
2 | #include <cairo/cairo.h> | ||
3 | #include <pango/pangocairo.h> | ||
4 | #include <stdlib.h> | ||
5 | #include <string.h> | ||
6 | #include <stdio.h> | ||
7 | #include <unistd.h> | ||
8 | #include <errno.h> | ||
9 | #include <sys/mman.h> | ||
10 | #include "client/buffer.h" | ||
11 | #include "list.h" | ||
12 | #include "log.h" | ||
13 | |||
14 | static int create_pool_file(size_t size) { | ||
15 | static const char template[] = "/sway-client-XXXXXX"; | ||
16 | const char *path = getenv("XDG_RUNTIME_DIR"); | ||
17 | if (!path) { | ||
18 | return -1; | ||
19 | } | ||
20 | |||
21 | int ts = (path[strlen(path) - 1] == '/'); | ||
22 | |||
23 | char *name = malloc( | ||
24 | strlen(template) + | ||
25 | strlen(path) + | ||
26 | (ts ? 1 : 0) + 1); | ||
27 | sprintf(name, "%s%s%s", path, ts ? "" : "/", template); | ||
28 | |||
29 | int fd = mkstemp(name); | ||
30 | free(name); | ||
31 | |||
32 | if (fd < 0) { | ||
33 | return -1; | ||
34 | } | ||
35 | |||
36 | if (ftruncate(fd, size) < 0) { | ||
37 | close(fd); | ||
38 | return -1; | ||
39 | } | ||
40 | |||
41 | return fd; | ||
42 | } | ||
43 | |||
44 | static void buffer_release(void *data, struct wl_buffer *wl_buffer) { | ||
45 | struct buffer *buffer = data; | ||
46 | buffer->busy = false; | ||
47 | } | ||
48 | |||
49 | static const struct wl_buffer_listener buffer_listener = { | ||
50 | .release = buffer_release | ||
51 | }; | ||
52 | |||
53 | static struct buffer *create_buffer(struct client_state *state, struct buffer *buf, | ||
54 | int32_t width, int32_t height, uint32_t format) { | ||
55 | |||
56 | uint32_t stride = width * 4; | ||
57 | uint32_t size = stride * height; | ||
58 | |||
59 | int fd = create_pool_file(size); | ||
60 | void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
61 | struct wl_shm_pool *pool = wl_shm_create_pool(state->shm, fd, size); | ||
62 | buf->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format); | ||
63 | wl_shm_pool_destroy(pool); | ||
64 | close(fd); | ||
65 | fd = -1; | ||
66 | |||
67 | buf->surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); | ||
68 | buf->cairo = cairo_create(buf->surface); | ||
69 | buf->pango = pango_cairo_create_context(buf->cairo); | ||
70 | |||
71 | wl_buffer_add_listener(buf->buffer, &buffer_listener, state); | ||
72 | return buf; | ||
73 | } | ||
74 | |||
75 | static void destroy_buffer(struct buffer *buffer) { | ||
76 | if (buffer->buffer) { | ||
77 | wl_buffer_destroy(buffer->buffer); | ||
78 | } | ||
79 | if (buffer->cairo) { | ||
80 | cairo_destroy(buffer->cairo); | ||
81 | } | ||
82 | if (buffer->surface) { | ||
83 | cairo_surface_destroy(buffer->surface); | ||
84 | } | ||
85 | memset(buffer, 0, sizeof(struct buffer)); | ||
86 | } | ||
87 | |||
88 | struct buffer *get_next_buffer(struct client_state *state) { | ||
89 | struct buffer *buffer = NULL; | ||
90 | |||
91 | int i; | ||
92 | for (i = 0; i < 2; ++i) { | ||
93 | if (state->buffers[i].busy) { | ||
94 | continue; | ||
95 | } | ||
96 | buffer = &state->buffers[i]; | ||
97 | } | ||
98 | |||
99 | if (!buffer) { | ||
100 | return NULL; | ||
101 | } | ||
102 | |||
103 | if (buffer->width != state->width || buffer->height != state->height) { | ||
104 | destroy_buffer(buffer); | ||
105 | } | ||
106 | |||
107 | if (!buffer->buffer) { | ||
108 | if (!create_buffer(state, buffer, state->width, state->height, WL_SHM_FORMAT_ARGB8888)) { | ||
109 | return NULL; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | state->cairo = buffer->cairo; | ||
114 | state->buffer = buffer; | ||
115 | return buffer; | ||
116 | } | ||
diff --git a/wayland/client.c b/wayland/client.c index fcbba885..4d4a6bbb 100644 --- a/wayland/client.c +++ b/wayland/client.c | |||
@@ -7,7 +7,8 @@ | |||
7 | #include <unistd.h> | 7 | #include <unistd.h> |
8 | #include <errno.h> | 8 | #include <errno.h> |
9 | #include <sys/mman.h> | 9 | #include <sys/mman.h> |
10 | #include "client.h" | 10 | #include "client/client.h" |
11 | #include "client/buffer.h" | ||
11 | #include "list.h" | 12 | #include "list.h" |
12 | #include "log.h" | 13 | #include "log.h" |
13 | 14 | ||
@@ -73,87 +74,12 @@ static const struct wl_registry_listener registry_listener = { | |||
73 | .global_remove = registry_global_remove | 74 | .global_remove = registry_global_remove |
74 | }; | 75 | }; |
75 | 76 | ||
76 | static int create_pool_file(size_t size) { | 77 | struct client_state *client_setup(uint32_t width, uint32_t height) { |
77 | static const char template[] = "/swaybg-XXXXXX"; | ||
78 | const char *path = getenv("XDG_RUNTIME_DIR"); | ||
79 | if (!path) { | ||
80 | return -1; | ||
81 | } | ||
82 | |||
83 | int ts = (path[strlen(path) - 1] == '/'); | ||
84 | |||
85 | char *name = malloc( | ||
86 | strlen(template) + | ||
87 | strlen(path) + | ||
88 | (ts ? 1 : 0) + 1); | ||
89 | sprintf(name, "%s%s%s", path, ts ? "" : "/", template); | ||
90 | |||
91 | int fd = mkstemp(name); | ||
92 | free(name); | ||
93 | |||
94 | if (fd < 0) { | ||
95 | return -1; | ||
96 | } | ||
97 | |||
98 | if (ftruncate(fd, size) < 0) { | ||
99 | close(fd); | ||
100 | return -1; | ||
101 | } | ||
102 | |||
103 | return fd; | ||
104 | } | ||
105 | |||
106 | static void buffer_release(void *data, struct wl_buffer *buffer) { | ||
107 | struct client_state *state = data; | ||
108 | state->busy = false; | ||
109 | sway_log(L_INFO, "buffer release"); | ||
110 | } | ||
111 | |||
112 | static const struct wl_buffer_listener buffer_listener = { | ||
113 | .release = buffer_release | ||
114 | }; | ||
115 | |||
116 | struct buffer *create_buffer(struct client_state *state, | ||
117 | int32_t width, int32_t height, uint32_t format) { | ||
118 | |||
119 | struct buffer *buf = malloc(sizeof(struct buffer)); | ||
120 | memset(buf, 0, sizeof(struct buffer)); | ||
121 | uint32_t stride = width * 4; | ||
122 | uint32_t size = stride * height; | ||
123 | |||
124 | int fd = create_pool_file(size); | ||
125 | void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
126 | buf->pool = wl_shm_create_pool(state->shm, fd, size); | ||
127 | buf->buffer = wl_shm_pool_create_buffer(buf->pool, 0, width, height, stride, format); | ||
128 | wl_shm_pool_destroy(buf->pool); | ||
129 | close(fd); | ||
130 | fd = -1; | ||
131 | |||
132 | state->cairo_surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); | ||
133 | state->cairo = cairo_create(state->cairo_surface); | ||
134 | state->pango = pango_cairo_create_context(state->cairo); | ||
135 | |||
136 | wl_buffer_add_listener(buf->buffer, &buffer_listener, state); | ||
137 | |||
138 | sway_log(L_INFO, "%p %p", buf->pool, buf->buffer); | ||
139 | return buf; | ||
140 | } | ||
141 | |||
142 | static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) { | ||
143 | sway_log(L_INFO, "frame callback"); | ||
144 | struct client_state *state = data; | ||
145 | wl_callback_destroy(callback); | ||
146 | state->frame_cb = NULL; | ||
147 | } | ||
148 | |||
149 | static const struct wl_callback_listener listener = { | ||
150 | frame_callback | ||
151 | }; | ||
152 | |||
153 | struct client_state *client_setup(void) { | ||
154 | struct client_state *state = malloc(sizeof(struct client_state)); | 78 | struct client_state *state = malloc(sizeof(struct client_state)); |
155 | memset(state, 0, sizeof(struct client_state)); | 79 | memset(state, 0, sizeof(struct client_state)); |
156 | state->outputs = create_list(); | 80 | state->outputs = create_list(); |
81 | state->width = width; | ||
82 | state->height = height; | ||
157 | 83 | ||
158 | state->display = wl_display_connect(NULL); | 84 | state->display = wl_display_connect(NULL); |
159 | if (!state->display) { | 85 | if (!state->display) { |
@@ -168,22 +94,33 @@ struct client_state *client_setup(void) { | |||
168 | wl_display_roundtrip(state->display); | 94 | wl_display_roundtrip(state->display); |
169 | wl_registry_destroy(registry); | 95 | wl_registry_destroy(registry); |
170 | 96 | ||
171 | state->buffer = create_buffer(state, 100, 100, WL_SHM_FORMAT_ARGB8888); | ||
172 | state->surface = wl_compositor_create_surface(state->compositor); | 97 | state->surface = wl_compositor_create_surface(state->compositor); |
173 | state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface); | 98 | state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface); |
174 | wl_shell_surface_set_toplevel(state->shell_surface); | 99 | wl_shell_surface_set_toplevel(state->shell_surface); |
175 | 100 | ||
176 | wl_surface_damage(state->surface, 0, 0, 100, 100); | ||
177 | |||
178 | return state; | 101 | return state; |
179 | } | 102 | } |
180 | 103 | ||
181 | int client_render(struct client_state *state) { | 104 | static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) { |
182 | if (state->frame_cb || state->busy) { | 105 | struct client_state *state = data; |
183 | return 2; | 106 | wl_callback_destroy(callback); |
107 | state->frame_cb = NULL; | ||
108 | } | ||
109 | |||
110 | static const struct wl_callback_listener listener = { | ||
111 | frame_callback | ||
112 | }; | ||
113 | |||
114 | int client_prerender(struct client_state *state) { | ||
115 | if (state->frame_cb) { | ||
116 | return 0; | ||
184 | } | 117 | } |
185 | sway_log(L_INFO, "Rendering"); | ||
186 | 118 | ||
119 | get_next_buffer(state); | ||
120 | return 1; | ||
121 | } | ||
122 | |||
123 | int client_render(struct client_state *state) { | ||
187 | state->frame_cb = wl_surface_frame(state->surface); | 124 | state->frame_cb = wl_surface_frame(state->surface); |
188 | wl_callback_add_listener(state->frame_cb, &listener, state); | 125 | wl_callback_add_listener(state->frame_cb, &listener, state); |
189 | 126 | ||
@@ -191,9 +128,7 @@ int client_render(struct client_state *state) { | |||
191 | wl_surface_attach(state->surface, state->buffer->buffer, 0, 0); | 128 | wl_surface_attach(state->surface, state->buffer->buffer, 0, 0); |
192 | wl_surface_commit(state->surface); | 129 | wl_surface_commit(state->surface); |
193 | 130 | ||
194 | state->busy = true; | 131 | return 1; |
195 | |||
196 | return wl_display_dispatch(state->display) != -1; | ||
197 | } | 132 | } |
198 | 133 | ||
199 | void client_teardown(struct client_state *state) { | 134 | void client_teardown(struct client_state *state) { |