diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-11-12 19:04:01 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-11-12 19:04:01 -0500 |
commit | bfcabe48ef3fc7a0388de007504fc232f826fb84 (patch) | |
tree | 8bef61a10259765dbafed49c9a2a76b4bf9ced2d /wayland | |
parent | Merge branch 'master' of github.com:SirCmpwn/sway (diff) | |
download | sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.tar.gz sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.tar.zst sway-bfcabe48ef3fc7a0388de007504fc232f826fb84.zip |
Start fleshing out wayland client implementation
This introduces a basic shared framework for making wayland clients
within sway itself.
Diffstat (limited to 'wayland')
-rw-r--r-- | wayland/client.c | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/wayland/client.c b/wayland/client.c new file mode 100644 index 00000000..c62f92bd --- /dev/null +++ b/wayland/client.c | |||
@@ -0,0 +1,192 @@ | |||
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.h" | ||
11 | #include "list.h" | ||
12 | #include "log.h" | ||
13 | |||
14 | static void display_handle_mode(void *data, struct wl_output *wl_output, | ||
15 | uint32_t flags, int32_t width, int32_t height, int32_t refresh) { | ||
16 | struct output_state *state = data; | ||
17 | if (flags & WL_OUTPUT_MODE_CURRENT) { | ||
18 | state->flags = flags; | ||
19 | state->width = width; | ||
20 | state->height = height; | ||
21 | } | ||
22 | } | ||
23 | |||
24 | static void display_handle_geometry(void *data, struct wl_output *wl_output, | ||
25 | int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, | ||
26 | int32_t subpixel, const char *make, const char *model, int32_t transform) { | ||
27 | // this space intentionally left blank | ||
28 | } | ||
29 | |||
30 | static void display_handle_done(void *data, struct wl_output *wl_output) { | ||
31 | // this space intentionally left blank | ||
32 | } | ||
33 | |||
34 | static void display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) { | ||
35 | // this space intentionally left blank | ||
36 | } | ||
37 | |||
38 | static const struct wl_output_listener output_listener = { | ||
39 | .mode = display_handle_mode, | ||
40 | .geometry = display_handle_geometry, | ||
41 | .done = display_handle_done, | ||
42 | .scale = display_handle_scale | ||
43 | }; | ||
44 | |||
45 | static void registry_global(void *data, struct wl_registry *registry, | ||
46 | uint32_t name, const char *interface, uint32_t version) { | ||
47 | struct client_state *state = data; | ||
48 | |||
49 | if (strcmp(interface, wl_compositor_interface.name) == 0) { | ||
50 | state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version); | ||
51 | } else if (strcmp(interface, wl_shm_interface.name) == 0) { | ||
52 | state->shm = wl_registry_bind(registry, name, &wl_shm_interface, version); | ||
53 | } else if (strcmp(interface, wl_shell_interface.name) == 0) { | ||
54 | state->shell = wl_registry_bind(registry, name, &wl_shell_interface, version); | ||
55 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { | ||
56 | state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version); | ||
57 | state->pointer = wl_seat_get_pointer(state->seat); | ||
58 | } else if (strcmp(interface, wl_output_interface.name) == 0) { | ||
59 | struct wl_output *output = wl_registry_bind(registry, name, &wl_output_interface, version); | ||
60 | struct output_state *ostate = malloc(sizeof(struct output_state)); | ||
61 | ostate->output = output; | ||
62 | wl_output_add_listener(output, &output_listener, ostate); | ||
63 | list_add(state->outputs, ostate); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) { | ||
68 | // this space intentionally left blank | ||
69 | } | ||
70 | |||
71 | static const struct wl_registry_listener registry_listener = { | ||
72 | .global = registry_global, | ||
73 | .global_remove = registry_global_remove | ||
74 | }; | ||
75 | |||
76 | static int create_pool_file(size_t size) { | ||
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 | struct buffer *create_buffer(struct client_state *state, | ||
107 | int32_t width, int32_t height, uint32_t format) { | ||
108 | |||
109 | struct buffer *buf = malloc(sizeof(struct buffer)); | ||
110 | memset(buf, 0, sizeof(struct buffer)); | ||
111 | uint32_t stride = width * 4; | ||
112 | uint32_t size = stride * height; | ||
113 | |||
114 | int fd = create_pool_file(size); | ||
115 | void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
116 | buf->pool = wl_shm_create_pool(state->shm, fd, size); | ||
117 | buf->buffer = wl_shm_pool_create_buffer(buf->pool, 0, width, height, stride, format); | ||
118 | |||
119 | state->cairo_surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, width, height, stride); | ||
120 | state->cairo = cairo_create(state->cairo_surface); | ||
121 | state->pango = pango_cairo_create_context(state->cairo); | ||
122 | |||
123 | sway_log(L_INFO, "%p %p", buf->pool, buf->buffer); | ||
124 | return buf; | ||
125 | } | ||
126 | |||
127 | struct client_state *client_setup(void) { | ||
128 | struct client_state *state = malloc(sizeof(struct client_state)); | ||
129 | memset(state, 0, sizeof(struct client_state)); | ||
130 | state->outputs = create_list(); | ||
131 | |||
132 | state->display = wl_display_connect(NULL); | ||
133 | if (!state->display) { | ||
134 | sway_log(L_ERROR, "Error opening display"); | ||
135 | client_teardown(state); | ||
136 | return NULL; | ||
137 | } | ||
138 | |||
139 | struct wl_registry *registry = wl_display_get_registry(state->display); | ||
140 | wl_registry_add_listener(registry, ®istry_listener, state); | ||
141 | wl_display_roundtrip(state->display); // globals | ||
142 | wl_display_roundtrip(state->display); // listeners | ||
143 | wl_registry_destroy(registry); | ||
144 | |||
145 | state->buffer = create_buffer(state, 100, 100, WL_SHM_FORMAT_ARGB8888); | ||
146 | state->surface = wl_compositor_create_surface(state->compositor); | ||
147 | state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface); | ||
148 | wl_shell_surface_set_toplevel(state->shell_surface); | ||
149 | |||
150 | wl_surface_damage(state->surface, 0, 0, 100, 100); | ||
151 | wl_surface_attach(state->surface, state->buffer->buffer, 0, 0); | ||
152 | wl_surface_commit(state->surface); | ||
153 | |||
154 | return state; | ||
155 | } | ||
156 | |||
157 | int client_prerender(struct client_state *state) { | ||
158 | wl_display_dispatch_pending(state->display); | ||
159 | if (wl_display_flush(state->display) < 0 && errno != EAGAIN) { | ||
160 | return 0; | ||
161 | } | ||
162 | return 1; | ||
163 | } | ||
164 | |||
165 | int client_render(struct client_state *state) { | ||
166 | return wl_display_dispatch(state->display) != -1; | ||
167 | } | ||
168 | |||
169 | void client_teardown(struct client_state *state) { | ||
170 | if (state->pointer) { | ||
171 | wl_pointer_destroy(state->pointer); | ||
172 | } | ||
173 | if (state->seat) { | ||
174 | wl_seat_destroy(state->seat); | ||
175 | } | ||
176 | if (state->shell) { | ||
177 | wl_shell_destroy(state->shell); | ||
178 | } | ||
179 | if (state->shm) { | ||
180 | wl_shm_destroy(state->shm); | ||
181 | } | ||
182 | if (state->compositor) { | ||
183 | wl_compositor_destroy(state->compositor); | ||
184 | } | ||
185 | if (state->display) { | ||
186 | wl_display_disconnect(state->display); | ||
187 | } | ||
188 | if (state->outputs) { | ||
189 | // TODO: Free the outputs themselves | ||
190 | list_free(state->outputs); | ||
191 | } | ||
192 | } | ||