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 | |
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.
-rw-r--r-- | include/client/buffer.h | 8 | ||||
-rw-r--r-- | include/client/client.h (renamed from include/client.h) | 15 | ||||
-rw-r--r-- | swaybg/main.c | 37 | ||||
-rw-r--r-- | wayland/buffers.c | 116 | ||||
-rw-r--r-- | wayland/client.c | 113 |
5 files changed, 168 insertions, 121 deletions
diff --git a/include/client/buffer.h b/include/client/buffer.h new file mode 100644 index 00000000..aa8d68a1 --- /dev/null +++ b/include/client/buffer.h | |||
@@ -0,0 +1,8 @@ | |||
1 | #ifndef _BUFFER_H | ||
2 | #define _BUFFER_H | ||
3 | |||
4 | #include "client/client.h" | ||
5 | |||
6 | struct buffer *get_next_buffer(struct client_state *state); | ||
7 | |||
8 | #endif | ||
diff --git a/include/client.h b/include/client/client.h index f10e6b1a..bac2c1a6 100644 --- a/include/client.h +++ b/include/client/client.h | |||
@@ -10,13 +10,16 @@ | |||
10 | struct output_state { | 10 | struct output_state { |
11 | struct wl_output *output; | 11 | struct wl_output *output; |
12 | uint32_t flags; | 12 | uint32_t flags; |
13 | int width, height; | 13 | uint32_t width, height; |
14 | }; | 14 | }; |
15 | 15 | ||
16 | struct buffer { | 16 | struct buffer { |
17 | struct wl_buffer *buffer; | 17 | struct wl_buffer *buffer; |
18 | struct wl_shm_pool *pool; | 18 | cairo_surface_t *surface; |
19 | cairo_t *cairo; | ||
20 | PangoContext *pango; | ||
19 | uint32_t width, height; | 21 | uint32_t width, height; |
22 | bool busy; | ||
20 | }; | 23 | }; |
21 | 24 | ||
22 | struct client_state { | 25 | struct client_state { |
@@ -26,20 +29,18 @@ struct client_state { | |||
26 | struct wl_seat *seat; | 29 | struct wl_seat *seat; |
27 | struct wl_shell *shell; | 30 | struct wl_shell *shell; |
28 | struct wl_shm *shm; | 31 | struct wl_shm *shm; |
32 | struct buffer buffers[2]; | ||
29 | struct buffer *buffer; | 33 | struct buffer *buffer; |
30 | struct wl_surface *surface; | 34 | struct wl_surface *surface; |
31 | struct wl_shell_surface *shell_surface; | 35 | struct wl_shell_surface *shell_surface; |
32 | struct wl_callback *frame_cb; | 36 | struct wl_callback *frame_cb; |
33 | bool busy; | 37 | uint32_t width, height; |
34 | cairo_t *cairo; | 38 | cairo_t *cairo; |
35 | cairo_surface_t *cairo_surface; | ||
36 | PangoContext *pango; | ||
37 | list_t *outputs; | 39 | list_t *outputs; |
38 | }; | 40 | }; |
39 | 41 | ||
40 | struct client_state *client_setup(void); | 42 | struct client_state *client_setup(uint32_t width, uint32_t height); |
41 | void client_teardown(struct client_state *state); | 43 | void client_teardown(struct client_state *state); |
42 | struct buffer *create_memory_pool(struct client_state *state, int32_t width, int32_t height, uint32_t format); | ||
43 | int client_prerender(struct client_state *state); | 44 | int client_prerender(struct client_state *state); |
44 | int client_render(struct client_state *state); | 45 | int client_render(struct client_state *state); |
45 | 46 | ||
diff --git a/swaybg/main.c b/swaybg/main.c index db5af375..e8f400c0 100644 --- a/swaybg/main.c +++ b/swaybg/main.c | |||
@@ -2,7 +2,7 @@ | |||
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <wayland-client.h> | 3 | #include <wayland-client.h> |
4 | #include <time.h> | 4 | #include <time.h> |
5 | #include "client.h" | 5 | #include "client/client.h" |
6 | #include "log.h" | 6 | #include "log.h" |
7 | 7 | ||
8 | struct client_state *state; | 8 | struct client_state *state; |
@@ -14,36 +14,23 @@ void sway_terminate(void) { | |||
14 | 14 | ||
15 | int main(int argc, char **argv) { | 15 | int main(int argc, char **argv) { |
16 | init_log(L_INFO); | 16 | init_log(L_INFO); |
17 | if (!(state = client_setup())) { | 17 | if (!(state = client_setup(100, 100))) { |
18 | return -1; | 18 | return -1; |
19 | } | 19 | } |
20 | 20 | ||
21 | uint8_t r = 0, g = 0, b = 0; | 21 | uint8_t r = 100, g = 100, b = 100; |
22 | 22 | ||
23 | long last_ms = 0; | ||
24 | int rs; | ||
25 | do { | 23 | do { |
26 | struct timespec spec; | 24 | if (client_prerender(state)) { |
27 | clock_gettime(CLOCK_MONOTONIC, &spec); | 25 | cairo_set_source_rgb(state->cairo, r / 256.0, g / 256.0, b / 256.0); |
28 | long ms = round(spec.tv_nsec / 1.0e6); | 26 | cairo_rectangle(state->cairo, 0, 0, state->width, state->height); |
29 | 27 | cairo_fill(state->cairo); | |
30 | cairo_set_source_rgb(state->cairo, r, g, b); | 28 | |
31 | cairo_rectangle(state->cairo, 0, 0, 100, 100); | 29 | client_render(state); |
32 | cairo_fill(state->cairo); | 30 | |
33 | 31 | r++; if (r == 0) { g++; if (g == 0) { b++; } } | |
34 | rs = client_render(state); | ||
35 | |||
36 | if (ms - last_ms > 100) { | ||
37 | r++; | ||
38 | if (r == 0) { | ||
39 | g++; | ||
40 | if (g == 0) { | ||
41 | b++; | ||
42 | } | ||
43 | } | ||
44 | ms = last_ms; | ||
45 | } | 32 | } |
46 | } while (rs); | 33 | } while (wl_display_dispatch(state->display) != -1); |
47 | 34 | ||
48 | client_teardown(state); | 35 | client_teardown(state); |
49 | return 0; | 36 | return 0; |
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) { |