diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-11-19 07:58:57 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-11-19 07:58:57 -0500 |
commit | b4e5e1381f909b173a171fb3941610aec989df48 (patch) | |
tree | f7dcf63dc6f4ac05c8fbb86da37d52766f210130 /wayland/client.c | |
parent | Fix background extensions (diff) | |
download | sway-b4e5e1381f909b173a171fb3941610aec989df48.tar.gz sway-b4e5e1381f909b173a171fb3941610aec989df48.tar.zst sway-b4e5e1381f909b173a171fb3941610aec989df48.zip |
Refactor the crap out of wayland clients
And create a background surface on every output when invoking swaybg.
Diffstat (limited to 'wayland/client.c')
-rw-r--r-- | wayland/client.c | 218 |
1 files changed, 0 insertions, 218 deletions
diff --git a/wayland/client.c b/wayland/client.c deleted file mode 100644 index ab8adc47..00000000 --- a/wayland/client.c +++ /dev/null | |||
@@ -1,218 +0,0 @@ | |||
1 | #include <wayland-client.h> | ||
2 | #include <wayland-cursor.h> | ||
3 | #include "wayland-xdg-shell-client-protocol.h" | ||
4 | #include "wayland-desktop-shell-client-protocol.h" | ||
5 | #include <cairo/cairo.h> | ||
6 | #include <pango/pangocairo.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <string.h> | ||
9 | #include <stdio.h> | ||
10 | #include <unistd.h> | ||
11 | #include <errno.h> | ||
12 | #include <sys/mman.h> | ||
13 | #include "client/client.h" | ||
14 | #include "client/buffer.h" | ||
15 | #include "list.h" | ||
16 | #include "log.h" | ||
17 | |||
18 | static void display_handle_mode(void *data, struct wl_output *wl_output, | ||
19 | uint32_t flags, int32_t width, int32_t height, int32_t refresh) { | ||
20 | struct output_state *state = data; | ||
21 | if (flags & WL_OUTPUT_MODE_CURRENT) { | ||
22 | state->flags = flags; | ||
23 | state->width = width; | ||
24 | state->height = height; | ||
25 | } | ||
26 | } | ||
27 | |||
28 | static void display_handle_geometry(void *data, struct wl_output *wl_output, | ||
29 | int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, | ||
30 | int32_t subpixel, const char *make, const char *model, int32_t transform) { | ||
31 | // this space intentionally left blank | ||
32 | } | ||
33 | |||
34 | static void display_handle_done(void *data, struct wl_output *wl_output) { | ||
35 | // this space intentionally left blank | ||
36 | } | ||
37 | |||
38 | static void display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) { | ||
39 | // this space intentionally left blank | ||
40 | } | ||
41 | |||
42 | static const struct wl_output_listener output_listener = { | ||
43 | .mode = display_handle_mode, | ||
44 | .geometry = display_handle_geometry, | ||
45 | .done = display_handle_done, | ||
46 | .scale = display_handle_scale | ||
47 | }; | ||
48 | |||
49 | static void pointer_handle_enter(void *data, struct wl_pointer *pointer, | ||
50 | uint32_t serial, struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w) { | ||
51 | struct client_state *state = data; | ||
52 | struct wl_cursor_image *image = state->cursor.cursor->images[0]; | ||
53 | wl_pointer_set_cursor(pointer, serial, state->cursor.surface, image->hotspot_x, image->hotspot_y); | ||
54 | } | ||
55 | |||
56 | static void pointer_handle_leave(void *data, struct wl_pointer *pointer, | ||
57 | uint32_t serial, struct wl_surface *surface) { | ||
58 | } | ||
59 | |||
60 | static void pointer_handle_motion(void *data, struct wl_pointer *pointer, | ||
61 | uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) { | ||
62 | } | ||
63 | |||
64 | static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial, | ||
65 | uint32_t time, uint32_t button, uint32_t state_w) { | ||
66 | } | ||
67 | |||
68 | static void pointer_handle_axis(void *data, struct wl_pointer *pointer, | ||
69 | uint32_t time, uint32_t axis, wl_fixed_t value) { | ||
70 | } | ||
71 | |||
72 | static const struct wl_pointer_listener pointer_listener = { | ||
73 | .enter = pointer_handle_enter, | ||
74 | .leave = pointer_handle_leave, | ||
75 | .motion = pointer_handle_motion, | ||
76 | .button = pointer_handle_button, | ||
77 | .axis = pointer_handle_axis | ||
78 | }; | ||
79 | |||
80 | static void registry_global(void *data, struct wl_registry *registry, | ||
81 | uint32_t name, const char *interface, uint32_t version) { | ||
82 | struct client_state *state = data; | ||
83 | |||
84 | if (strcmp(interface, wl_compositor_interface.name) == 0) { | ||
85 | state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version); | ||
86 | } else if (strcmp(interface, wl_shm_interface.name) == 0) { | ||
87 | state->shm = wl_registry_bind(registry, name, &wl_shm_interface, version); | ||
88 | } else if (strcmp(interface, wl_shell_interface.name) == 0) { | ||
89 | state->shell = wl_registry_bind(registry, name, &wl_shell_interface, version); | ||
90 | } else if (strcmp(interface, wl_seat_interface.name) == 0) { | ||
91 | state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version); | ||
92 | state->pointer = wl_seat_get_pointer(state->seat); | ||
93 | wl_pointer_add_listener(state->pointer, &pointer_listener, state); | ||
94 | } else if (strcmp(interface, wl_output_interface.name) == 0) { | ||
95 | struct wl_output *output = wl_registry_bind(registry, name, &wl_output_interface, version); | ||
96 | struct output_state *ostate = malloc(sizeof(struct output_state)); | ||
97 | ostate->output = output; | ||
98 | wl_output_add_listener(output, &output_listener, ostate); | ||
99 | list_add(state->outputs, ostate); | ||
100 | } else if (strcmp(interface, desktop_shell_interface.name) == 0) { | ||
101 | state->desktop_shell = wl_registry_bind(registry, name, &desktop_shell_interface, version); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) { | ||
106 | // this space intentionally left blank | ||
107 | } | ||
108 | |||
109 | static const struct wl_registry_listener registry_listener = { | ||
110 | .global = registry_global, | ||
111 | .global_remove = registry_global_remove | ||
112 | }; | ||
113 | |||
114 | void shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface, | ||
115 | uint32_t edges, int32_t width, int32_t height) { | ||
116 | struct client_state *state = data; | ||
117 | state->width = width; | ||
118 | state->height = height; | ||
119 | } | ||
120 | |||
121 | static const struct wl_shell_surface_listener surface_listener = { | ||
122 | .configure = shell_surface_configure | ||
123 | }; | ||
124 | |||
125 | struct client_state *client_setup(uint32_t width, uint32_t height, bool shell_surface) { | ||
126 | struct client_state *state = malloc(sizeof(struct client_state)); | ||
127 | memset(state, 0, sizeof(struct client_state)); | ||
128 | state->outputs = create_list(); | ||
129 | state->width = width; | ||
130 | state->height = height; | ||
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_dispatch(state->display); | ||
142 | wl_display_roundtrip(state->display); | ||
143 | wl_registry_destroy(registry); | ||
144 | |||
145 | state->surface = wl_compositor_create_surface(state->compositor); | ||
146 | if (shell_surface) { | ||
147 | state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface); | ||
148 | wl_shell_surface_add_listener(state->shell_surface, &surface_listener, state); | ||
149 | wl_shell_surface_set_toplevel(state->shell_surface); | ||
150 | } | ||
151 | |||
152 | state->cursor.cursor_theme = wl_cursor_theme_load("default", 32, state->shm); // TODO: let you customize this | ||
153 | state->cursor.cursor = wl_cursor_theme_get_cursor(state->cursor.cursor_theme, "left_ptr"); | ||
154 | state->cursor.surface = wl_compositor_create_surface(state->compositor); | ||
155 | |||
156 | struct wl_cursor_image *image = state->cursor.cursor->images[0]; | ||
157 | struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image); | ||
158 | wl_surface_attach(state->cursor.surface, cursor_buf, 0, 0); | ||
159 | wl_surface_damage(state->cursor.surface, 0, 0, image->width, image->height); | ||
160 | wl_surface_commit(state->cursor.surface); | ||
161 | |||
162 | return state; | ||
163 | } | ||
164 | |||
165 | static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) { | ||
166 | struct client_state *state = data; | ||
167 | wl_callback_destroy(callback); | ||
168 | state->frame_cb = NULL; | ||
169 | } | ||
170 | |||
171 | static const struct wl_callback_listener listener = { | ||
172 | frame_callback | ||
173 | }; | ||
174 | |||
175 | int client_prerender(struct client_state *state) { | ||
176 | if (state->frame_cb) { | ||
177 | return 0; | ||
178 | } | ||
179 | |||
180 | get_next_buffer(state); | ||
181 | return 1; | ||
182 | } | ||
183 | |||
184 | int client_render(struct client_state *state) { | ||
185 | state->frame_cb = wl_surface_frame(state->surface); | ||
186 | wl_callback_add_listener(state->frame_cb, &listener, state); | ||
187 | |||
188 | wl_surface_damage(state->surface, 0, 0, state->buffer->width, state->buffer->height); | ||
189 | wl_surface_attach(state->surface, state->buffer->buffer, 0, 0); | ||
190 | wl_surface_commit(state->surface); | ||
191 | |||
192 | return 1; | ||
193 | } | ||
194 | |||
195 | void client_teardown(struct client_state *state) { | ||
196 | if (state->pointer) { | ||
197 | wl_pointer_destroy(state->pointer); | ||
198 | } | ||
199 | if (state->seat) { | ||
200 | wl_seat_destroy(state->seat); | ||
201 | } | ||
202 | if (state->shell) { | ||
203 | wl_shell_destroy(state->shell); | ||
204 | } | ||
205 | if (state->shm) { | ||
206 | wl_shm_destroy(state->shm); | ||
207 | } | ||
208 | if (state->compositor) { | ||
209 | wl_compositor_destroy(state->compositor); | ||
210 | } | ||
211 | if (state->display) { | ||
212 | wl_display_disconnect(state->display); | ||
213 | } | ||
214 | if (state->outputs) { | ||
215 | // TODO: Free the outputs themselves | ||
216 | list_free(state->outputs); | ||
217 | } | ||
218 | } | ||