diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-12-10 08:18:48 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-12-10 08:18:48 -0500 |
commit | d2e9c6864005d716a2d66f5bff5b8bd87b180574 (patch) | |
tree | e1d9f82364e65ac2206a5afed4294491daf7a071 | |
parent | Discover swaylock extension in registry (diff) | |
download | sway-d2e9c6864005d716a2d66f5bff5b8bd87b180574.tar.gz sway-d2e9c6864005d716a2d66f5bff5b8bd87b180574.tar.zst sway-d2e9c6864005d716a2d66f5bff5b8bd87b180574.zip |
Copy+paste swaybg code to swaylock for testing
-rw-r--r-- | swaybg/main.c | 4 | ||||
-rw-r--r-- | swaylock/main.c | 124 |
2 files changed, 125 insertions, 3 deletions
diff --git a/swaybg/main.c b/swaybg/main.c index 0bb83396..2ae06c6f 100644 --- a/swaybg/main.c +++ b/swaybg/main.c | |||
@@ -56,8 +56,8 @@ int main(int argc, const char **argv) { | |||
56 | desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); | 56 | desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); |
57 | list_add(surfaces, window); | 57 | list_add(surfaces, window); |
58 | 58 | ||
59 | GError *err=NULL; | 59 | GError *err = NULL; |
60 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(argv[2],&err); | 60 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(argv[2], &err); |
61 | if (!pixbuf) { | 61 | if (!pixbuf) { |
62 | sway_abort("Failed to load background image."); | 62 | sway_abort("Failed to load background image."); |
63 | } | 63 | } |
diff --git a/swaylock/main.c b/swaylock/main.c index a7a15533..0e96afc7 100644 --- a/swaylock/main.c +++ b/swaylock/main.c | |||
@@ -1,8 +1,10 @@ | |||
1 | #include "wayland-swaylock-client-protocol.h" | 1 | #include "wayland-swaylock-client-protocol.h" |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <string.h> | ||
4 | #include "client/window.h" | 5 | #include "client/window.h" |
5 | #include "client/registry.h" | 6 | #include "client/registry.h" |
7 | #include "client/cairo.h" | ||
6 | #include "log.h" | 8 | #include "log.h" |
7 | 9 | ||
8 | list_t *surfaces; | 10 | list_t *surfaces; |
@@ -17,6 +19,13 @@ enum scaling_mode { | |||
17 | }; | 19 | }; |
18 | 20 | ||
19 | void sway_terminate(void) { | 21 | void sway_terminate(void) { |
22 | int i; | ||
23 | for (i = 0; i < surfaces->length; ++i) { | ||
24 | struct window *window = surfaces->items[i]; | ||
25 | window_teardown(window); | ||
26 | } | ||
27 | list_free(surfaces); | ||
28 | registry_teardown(registry); | ||
20 | exit(EXIT_FAILURE); | 29 | exit(EXIT_FAILURE); |
21 | } | 30 | } |
22 | 31 | ||
@@ -32,12 +41,125 @@ int main(int argc, char **argv) { | |||
32 | int i; | 41 | int i; |
33 | for (i = 0; i < registry->outputs->length; ++i) { | 42 | for (i = 0; i < registry->outputs->length; ++i) { |
34 | struct output_state *output = registry->outputs->items[i]; | 43 | struct output_state *output = registry->outputs->items[i]; |
35 | struct window *window = window_setup(registry, output->width, output->height, false); | 44 | struct window *window = window_setup(registry, output->width, output->height, true); |
36 | if (!window) { | 45 | if (!window) { |
37 | sway_abort("Failed to create surfaces."); | 46 | sway_abort("Failed to create surfaces."); |
38 | } | 47 | } |
39 | lock_set_lock_surface(registry->swaylock, output->output, window->surface); | 48 | lock_set_lock_surface(registry->swaylock, output->output, window->surface); |
40 | list_add(surfaces, window); | 49 | list_add(surfaces, window); |
41 | } | 50 | } |
51 | |||
52 | GError *err = NULL; | ||
53 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(argv[1], &err); // TODO: Parse i3lock arguments | ||
54 | if (!pixbuf) { | ||
55 | sway_abort("Failed to load background image."); | ||
56 | } | ||
57 | cairo_surface_t *image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); | ||
58 | g_object_unref(pixbuf); | ||
59 | if (!image) { | ||
60 | sway_abort("Failed to read background image."); | ||
61 | } | ||
62 | double width = cairo_image_surface_get_width(image); | ||
63 | double height = cairo_image_surface_get_height(image); | ||
64 | |||
65 | const char *scaling_mode_str = argv[2]; | ||
66 | enum scaling_mode scaling_mode = SCALING_MODE_STRETCH; | ||
67 | if (strcmp(scaling_mode_str, "stretch") == 0) { | ||
68 | scaling_mode = SCALING_MODE_STRETCH; | ||
69 | } else if (strcmp(scaling_mode_str, "fill") == 0) { | ||
70 | scaling_mode = SCALING_MODE_FILL; | ||
71 | } else if (strcmp(scaling_mode_str, "fit") == 0) { | ||
72 | scaling_mode = SCALING_MODE_FIT; | ||
73 | } else if (strcmp(scaling_mode_str, "center") == 0) { | ||
74 | scaling_mode = SCALING_MODE_CENTER; | ||
75 | } else if (strcmp(scaling_mode_str, "tile") == 0) { | ||
76 | scaling_mode = SCALING_MODE_TILE; | ||
77 | } else { | ||
78 | sway_abort("Unsupported scaling mode: %s", scaling_mode_str); | ||
79 | } | ||
80 | |||
81 | for (i = 0; i < surfaces->length; ++i) { | ||
82 | struct window *window = surfaces->items[i]; | ||
83 | if (window_prerender(window) && window->cairo) { | ||
84 | switch (scaling_mode) { | ||
85 | case SCALING_MODE_STRETCH: | ||
86 | cairo_scale(window->cairo, | ||
87 | (double) window->width / width, | ||
88 | (double) window->height / height); | ||
89 | cairo_set_source_surface(window->cairo, image, 0, 0); | ||
90 | break; | ||
91 | case SCALING_MODE_FILL: | ||
92 | { | ||
93 | double window_ratio = (double) window->width / window->height; | ||
94 | double bg_ratio = width / height; | ||
95 | |||
96 | if (window_ratio > bg_ratio) { | ||
97 | double scale = (double) window->width / width; | ||
98 | cairo_scale(window->cairo, scale, scale); | ||
99 | cairo_set_source_surface(window->cairo, image, | ||
100 | 0, | ||
101 | (double) window->height/2 / scale - height/2); | ||
102 | } else { | ||
103 | double scale = (double) window->height / height; | ||
104 | cairo_scale(window->cairo, scale, scale); | ||
105 | cairo_set_source_surface(window->cairo, image, | ||
106 | (double) window->width/2 / scale - width/2, | ||
107 | 0); | ||
108 | } | ||
109 | break; | ||
110 | } | ||
111 | case SCALING_MODE_FIT: | ||
112 | { | ||
113 | double window_ratio = (double) window->width / window->height; | ||
114 | double bg_ratio = width / height; | ||
115 | |||
116 | if (window_ratio > bg_ratio) { | ||
117 | double scale = (double) window->height / height; | ||
118 | cairo_scale(window->cairo, scale, scale); | ||
119 | cairo_set_source_surface(window->cairo, image, | ||
120 | (double) window->width/2 / scale - width/2, | ||
121 | 0); | ||
122 | } else { | ||
123 | double scale = (double) window->width / width; | ||
124 | cairo_scale(window->cairo, scale, scale); | ||
125 | cairo_set_source_surface(window->cairo, image, | ||
126 | 0, | ||
127 | (double) window->height/2 / scale - height/2); | ||
128 | } | ||
129 | break; | ||
130 | } | ||
131 | case SCALING_MODE_CENTER: | ||
132 | cairo_set_source_surface(window->cairo, image, | ||
133 | (double) window->width/2 - width/2, | ||
134 | (double) window->height/2 - height/2); | ||
135 | break; | ||
136 | case SCALING_MODE_TILE: | ||
137 | { | ||
138 | cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); | ||
139 | cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); | ||
140 | cairo_set_source(window->cairo, pattern); | ||
141 | break; | ||
142 | } | ||
143 | default: | ||
144 | sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); | ||
145 | } | ||
146 | |||
147 | cairo_paint(window->cairo); | ||
148 | |||
149 | window_render(window); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | cairo_surface_destroy(image); | ||
154 | |||
155 | while (wl_display_dispatch(registry->display) != -1); | ||
156 | |||
157 | for (i = 0; i < surfaces->length; ++i) { | ||
158 | struct window *window = surfaces->items[i]; | ||
159 | window_teardown(window); | ||
160 | } | ||
161 | list_free(surfaces); | ||
162 | registry_teardown(registry); | ||
163 | |||
42 | return 0; | 164 | return 0; |
43 | } | 165 | } |