diff options
Diffstat (limited to 'swaybg')
-rw-r--r-- | swaybg/main.c | 139 |
1 files changed, 13 insertions, 126 deletions
diff --git a/swaybg/main.c b/swaybg/main.c index c282a707..679b8c20 100644 --- a/swaybg/main.c +++ b/swaybg/main.c | |||
@@ -7,20 +7,12 @@ | |||
7 | #include <time.h> | 7 | #include <time.h> |
8 | #include <wayland-client.h> | 8 | #include <wayland-client.h> |
9 | #include <wlr/util/log.h> | 9 | #include <wlr/util/log.h> |
10 | #include "background-image.h" | ||
10 | #include "pool-buffer.h" | 11 | #include "pool-buffer.h" |
11 | #include "cairo.h" | 12 | #include "cairo.h" |
12 | #include "util.h" | 13 | #include "util.h" |
13 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" | 14 | #include "wlr-layer-shell-unstable-v1-client-protocol.h" |
14 | 15 | ||
15 | enum background_mode { | ||
16 | BACKGROUND_MODE_STRETCH, | ||
17 | BACKGROUND_MODE_FILL, | ||
18 | BACKGROUND_MODE_FIT, | ||
19 | BACKGROUND_MODE_CENTER, | ||
20 | BACKGROUND_MODE_TILE, | ||
21 | BACKGROUND_MODE_SOLID_COLOR, | ||
22 | }; | ||
23 | |||
24 | struct swaybg_args { | 16 | struct swaybg_args { |
25 | int output_idx; | 17 | int output_idx; |
26 | const char *path; | 18 | const char *path; |
@@ -71,85 +63,18 @@ bool is_valid_color(const char *color) { | |||
71 | return true; | 63 | return true; |
72 | } | 64 | } |
73 | 65 | ||
74 | static void render_image(struct swaybg_state *state) { | ||
75 | cairo_t *cairo = state->current_buffer->cairo; | ||
76 | cairo_surface_t *image = state->context.image; | ||
77 | double width = cairo_image_surface_get_width(image); | ||
78 | double height = cairo_image_surface_get_height(image); | ||
79 | int buffer_width = state->width * state->scale; | ||
80 | int buffer_height = state->height * state->scale; | ||
81 | |||
82 | switch (state->args->mode) { | ||
83 | case BACKGROUND_MODE_STRETCH: | ||
84 | cairo_scale(cairo, (double)buffer_width / width, | ||
85 | (double)buffer_height / height); | ||
86 | cairo_set_source_surface(cairo, image, 0, 0); | ||
87 | break; | ||
88 | case BACKGROUND_MODE_FILL: { | ||
89 | double window_ratio = (double)buffer_width / buffer_height; | ||
90 | double bg_ratio = width / height; | ||
91 | |||
92 | if (window_ratio > bg_ratio) { | ||
93 | double scale = (double)buffer_width / width; | ||
94 | cairo_scale(cairo, scale, scale); | ||
95 | cairo_set_source_surface(cairo, image, | ||
96 | 0, (double)buffer_height / 2 / scale - height / 2); | ||
97 | } else { | ||
98 | double scale = (double)buffer_height / height; | ||
99 | cairo_scale(cairo, scale, scale); | ||
100 | cairo_set_source_surface(cairo, image, | ||
101 | (double)buffer_width / 2 / scale - width / 2, 0); | ||
102 | } | ||
103 | break; | ||
104 | } | ||
105 | case BACKGROUND_MODE_FIT: { | ||
106 | double window_ratio = (double)buffer_width / buffer_height; | ||
107 | double bg_ratio = width / height; | ||
108 | |||
109 | if (window_ratio > bg_ratio) { | ||
110 | double scale = (double)buffer_height / height; | ||
111 | cairo_scale(cairo, scale, scale); | ||
112 | cairo_set_source_surface(cairo, image, | ||
113 | (double)buffer_width / 2 / scale - width / 2, 0); | ||
114 | } else { | ||
115 | double scale = (double)buffer_width / width; | ||
116 | cairo_scale(cairo, scale, scale); | ||
117 | cairo_set_source_surface(cairo, image, | ||
118 | 0, (double)buffer_height / 2 / scale - height / 2); | ||
119 | } | ||
120 | break; | ||
121 | } | ||
122 | case BACKGROUND_MODE_CENTER: | ||
123 | cairo_set_source_surface(cairo, image, | ||
124 | (double)buffer_width / 2 - width / 2, | ||
125 | (double)buffer_height / 2 - height / 2); | ||
126 | break; | ||
127 | case BACKGROUND_MODE_TILE: { | ||
128 | cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); | ||
129 | cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); | ||
130 | cairo_set_source(cairo, pattern); | ||
131 | break; | ||
132 | } | ||
133 | case BACKGROUND_MODE_SOLID_COLOR: | ||
134 | assert(0); | ||
135 | break; | ||
136 | } | ||
137 | cairo_paint(cairo); | ||
138 | } | ||
139 | |||
140 | static void render_frame(struct swaybg_state *state) { | 66 | static void render_frame(struct swaybg_state *state) { |
141 | state->current_buffer = get_next_buffer(state->shm, state->buffers, | 67 | int buffer_width = state->width * state->scale, |
142 | state->width * state->scale, state->height * state->scale); | 68 | buffer_height = state->height * state->scale; |
69 | state->current_buffer = get_next_buffer(state->shm, | ||
70 | state->buffers, buffer_width, buffer_height); | ||
143 | cairo_t *cairo = state->current_buffer->cairo; | 71 | cairo_t *cairo = state->current_buffer->cairo; |
144 | 72 | if (state->args->mode == BACKGROUND_MODE_SOLID_COLOR) { | |
145 | switch (state->args->mode) { | ||
146 | case BACKGROUND_MODE_SOLID_COLOR: | ||
147 | cairo_set_source_u32(cairo, state->context.color); | 73 | cairo_set_source_u32(cairo, state->context.color); |
148 | cairo_paint(cairo); | 74 | cairo_paint(cairo); |
149 | break; | 75 | } else { |
150 | default: | 76 | render_background_image(cairo, state->context.image, |
151 | render_image(state); | 77 | state->args->mode, buffer_width, buffer_height); |
152 | break; | ||
153 | } | 78 | } |
154 | 79 | ||
155 | wl_surface_set_buffer_scale(state->surface, state->scale); | 80 | wl_surface_set_buffer_scale(state->surface, state->scale); |
@@ -163,31 +88,7 @@ static bool prepare_context(struct swaybg_state *state) { | |||
163 | state->context.color = parse_color(state->args->path); | 88 | state->context.color = parse_color(state->args->path); |
164 | return is_valid_color(state->args->path); | 89 | return is_valid_color(state->args->path); |
165 | } | 90 | } |
166 | #ifdef HAVE_GDK_PIXBUF | 91 | if (!(state->context.image = load_background_image(state->args->path))) { |
167 | GError *err = NULL; | ||
168 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(state->args->path, &err); | ||
169 | if (!pixbuf) { | ||
170 | wlr_log(L_ERROR, "Failed to load background image."); | ||
171 | return false; | ||
172 | } | ||
173 | state->context.image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); | ||
174 | g_object_unref(pixbuf); | ||
175 | #else | ||
176 | state->context.image = cairo_image_surface_create_from_png( | ||
177 | state->args->path); | ||
178 | #endif //HAVE_GDK_PIXBUF | ||
179 | if (!state->context.image) { | ||
180 | wlr_log(L_ERROR, "Failed to read background image."); | ||
181 | return false; | ||
182 | } | ||
183 | if (cairo_surface_status(state->context.image) != CAIRO_STATUS_SUCCESS) { | ||
184 | wlr_log(L_ERROR, "Failed to read background image: %s." | ||
185 | #ifndef HAVE_GDK_PIXBUF | ||
186 | "\nSway was compiled without gdk_pixbuf support, so only" | ||
187 | "\nPNG images can be loaded. This is the likely cause." | ||
188 | #endif //HAVE_GDK_PIXBUF | ||
189 | , cairo_status_to_string( | ||
190 | cairo_surface_status(state->context.image))); | ||
191 | return false; | 92 | return false; |
192 | } | 93 | } |
193 | return true; | 94 | return true; |
@@ -294,24 +195,10 @@ int main(int argc, const char **argv) { | |||
294 | args.output_idx = atoi(argv[1]); | 195 | args.output_idx = atoi(argv[1]); |
295 | args.path = argv[2]; | 196 | args.path = argv[2]; |
296 | 197 | ||
297 | args.mode = BACKGROUND_MODE_STRETCH; | 198 | args.mode = parse_background_mode(argv[3]); |
298 | if (strcmp(argv[3], "stretch") == 0) { | 199 | if (args.mode == BACKGROUND_MODE_INVALID) { |
299 | args.mode = BACKGROUND_MODE_STRETCH; | ||
300 | } else if (strcmp(argv[3], "fill") == 0) { | ||
301 | args.mode = BACKGROUND_MODE_FILL; | ||
302 | } else if (strcmp(argv[3], "fit") == 0) { | ||
303 | args.mode = BACKGROUND_MODE_FIT; | ||
304 | } else if (strcmp(argv[3], "center") == 0) { | ||
305 | args.mode = BACKGROUND_MODE_CENTER; | ||
306 | } else if (strcmp(argv[3], "tile") == 0) { | ||
307 | args.mode = BACKGROUND_MODE_TILE; | ||
308 | } else if (strcmp(argv[3], "solid_color") == 0) { | ||
309 | args.mode = BACKGROUND_MODE_SOLID_COLOR; | ||
310 | } else { | ||
311 | wlr_log(L_ERROR, "Unsupported background mode: %s", argv[3]); | ||
312 | return 1; | 200 | return 1; |
313 | } | 201 | } |
314 | |||
315 | if (!prepare_context(&state)) { | 202 | if (!prepare_context(&state)) { |
316 | return 1; | 203 | return 1; |
317 | } | 204 | } |
@@ -345,10 +232,10 @@ int main(int argc, const char **argv) { | |||
345 | zwlr_layer_surface_v1_set_exclusive_zone(state.layer_surface, -1); | 232 | zwlr_layer_surface_v1_set_exclusive_zone(state.layer_surface, -1); |
346 | zwlr_layer_surface_v1_add_listener(state.layer_surface, | 233 | zwlr_layer_surface_v1_add_listener(state.layer_surface, |
347 | &layer_surface_listener, &state); | 234 | &layer_surface_listener, &state); |
348 | state.run_display = true; | ||
349 | wl_surface_commit(state.surface); | 235 | wl_surface_commit(state.surface); |
350 | wl_display_roundtrip(state.display); | 236 | wl_display_roundtrip(state.display); |
351 | 237 | ||
238 | state.run_display = true; | ||
352 | while (wl_display_dispatch(state.display) != -1 && state.run_display) { | 239 | while (wl_display_dispatch(state.display) != -1 && state.run_display) { |
353 | // This space intentionally left blank | 240 | // This space intentionally left blank |
354 | } | 241 | } |