summaryrefslogtreecommitdiffstats
path: root/swaylock
diff options
context:
space:
mode:
authorLibravatar Christoph Gysin <christoph.gysin@gmail.com>2016-01-25 21:17:36 +0200
committerLibravatar Christoph Gysin <christoph.gysin@gmail.com>2016-01-25 21:46:26 +0200
commit42bd8c53eea0b8f4e5c0a63f87e4fa3474317869 (patch)
treed6324c6b73108c84d6609cd7eb79aad3a0172473 /swaylock
parentswaylock: extract render_image (diff)
downloadsway-42bd8c53eea0b8f4e5c0a63f87e4fa3474317869.tar.gz
sway-42bd8c53eea0b8f4e5c0a63f87e4fa3474317869.tar.zst
sway-42bd8c53eea0b8f4e5c0a63f87e4fa3474317869.zip
swaylock: support --color
Diffstat (limited to 'swaylock')
-rw-r--r--swaylock/main.c64
1 files changed, 46 insertions, 18 deletions
diff --git a/swaylock/main.c b/swaylock/main.c
index d58fc868..98e26839 100644
--- a/swaylock/main.c
+++ b/swaylock/main.c
@@ -115,6 +115,20 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod
115 } 115 }
116} 116}
117 117
118static void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
119 cairo_set_source_rgba(cairo,
120 (color >> (3*8) & 0xFF) / 255.0,
121 (color >> (2*8) & 0xFF) / 255.0,
122 (color >> (1*8) & 0xFF) / 255.0,
123 (color >> (0*8) & 0xFF) / 255.0);
124}
125
126void render_color(struct window *window, uint32_t color) {
127 cairo_set_source_u32(window->cairo, color);
128 cairo_paint(window->cairo);
129 window_render(window);
130}
131
118void render_image(struct window *window, cairo_surface_t *image, enum scaling_mode scaling_mode) { 132void render_image(struct window *window, cairo_surface_t *image, enum scaling_mode scaling_mode) {
119 double width = cairo_image_surface_get_width(image); 133 double width = cairo_image_surface_get_width(image);
120 double height = cairo_image_surface_get_height(image); 134 double height = cairo_image_surface_get_height(image);
@@ -188,11 +202,13 @@ void render_image(struct window *window, cairo_surface_t *image, enum scaling_mo
188int main(int argc, char **argv) { 202int main(int argc, char **argv) {
189 char *image_path = NULL; 203 char *image_path = NULL;
190 char *scaling_mode_str = "fit"; 204 char *scaling_mode_str = "fit";
205 uint32_t color = 0xFFFFFFFF;
191 206
192 init_log(L_INFO); 207 init_log(L_INFO);
193 208
194 static struct option long_options[] = { 209 static struct option long_options[] = {
195 {"help", no_argument, NULL, 'h'}, 210 {"help", no_argument, NULL, 'h'},
211 {"color", required_argument, NULL, 'c'},
196 {"image", required_argument, NULL, 'i'}, 212 {"image", required_argument, NULL, 'i'},
197 {"scaling", required_argument, NULL, 's'}, 213 {"scaling", required_argument, NULL, 's'},
198 {"tiling", no_argument, NULL, 't'}, 214 {"tiling", no_argument, NULL, 't'},
@@ -204,6 +220,7 @@ int main(int argc, char **argv) {
204 "Usage: swaylock [options...]\n" 220 "Usage: swaylock [options...]\n"
205 "\n" 221 "\n"
206 " -h, --help Show help message and quit.\n" 222 " -h, --help Show help message and quit.\n"
223 " -c, --color <rrggbb> Turn the screen into the given color instead of white.\n"
207 " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" 224 " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n"
208 " -t, --tiling Same as --scaling=tile.\n" 225 " -t, --tiling Same as --scaling=tile.\n"
209 " -v, --version Show the version number and quit.\n" 226 " -v, --version Show the version number and quit.\n"
@@ -212,11 +229,21 @@ int main(int argc, char **argv) {
212 int c; 229 int c;
213 while (1) { 230 while (1) {
214 int option_index = 0; 231 int option_index = 0;
215 c = getopt_long(argc, argv, "hi:s:tv", long_options, &option_index); 232 c = getopt_long(argc, argv, "hc:i:s:tv", long_options, &option_index);
216 if (c == -1) { 233 if (c == -1) {
217 break; 234 break;
218 } 235 }
219 switch (c) { 236 switch (c) {
237 case 'c':
238 if (strlen(optarg) < 6) {
239 fprintf(stderr, "color must be specified in 3 byte format, e.g. ff0000\n");
240 exit(EXIT_FAILURE);
241 }
242 color = strtol(optarg, NULL, 16);
243 color <<= 8;
244 color |= 0xFF;
245 sway_log(L_DEBUG, "color: 0x%x", color);
246 break;
220 case 'i': 247 case 'i':
221 image_path = optarg; 248 image_path = optarg;
222 break; 249 break;
@@ -240,12 +267,6 @@ int main(int argc, char **argv) {
240 } 267 }
241 } 268 }
242 269
243 // TODO: support locking without image
244 if (!image_path) {
245 fprintf(stderr, "No image specified!\n");
246 exit(EXIT_FAILURE);
247 }
248
249 enum scaling_mode scaling_mode = SCALING_MODE_STRETCH; 270 enum scaling_mode scaling_mode = SCALING_MODE_STRETCH;
250 if (strcmp(scaling_mode_str, "stretch") == 0) { 271 if (strcmp(scaling_mode_str, "stretch") == 0) {
251 scaling_mode = SCALING_MODE_STRETCH; 272 scaling_mode = SCALING_MODE_STRETCH;
@@ -283,19 +304,22 @@ int main(int argc, char **argv) {
283 registry->input->notify = notify_key; 304 registry->input->notify = notify_key;
284 305
285 cairo_surface_t *image = NULL; 306 cairo_surface_t *image = NULL;
307
308 if (image_path) {
286#ifdef WITH_GDK_PIXBUF 309#ifdef WITH_GDK_PIXBUF
287 GError *err = NULL; 310 GError *err = NULL;
288 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err); 311 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err);
289 if (!pixbuf) { 312 if (!pixbuf) {
290 sway_abort("Failed to load background image."); 313 sway_abort("Failed to load background image.");
291 } 314 }
292 image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); 315 image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf);
293 g_object_unref(pixbuf); 316 g_object_unref(pixbuf);
294#else 317#else
295 cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]); 318 cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]);
296#endif //WITH_GDK_PIXBUF 319#endif //WITH_GDK_PIXBUF
297 if (!image) { 320 if (!image) {
298 sway_abort("Failed to read background image."); 321 sway_abort("Failed to read background image.");
322 }
299 } 323 }
300 324
301 for (i = 0; i < surfaces->length; ++i) { 325 for (i = 0; i < surfaces->length; ++i) {
@@ -305,10 +329,14 @@ int main(int argc, char **argv) {
305 } 329 }
306 if (image) { 330 if (image) {
307 render_image(window, image, scaling_mode); 331 render_image(window, image, scaling_mode);
332 } else {
333 render_color(window, color);
308 } 334 }
309 } 335 }
310 336
311 cairo_surface_destroy(image); 337 if (image) {
338 cairo_surface_destroy(image);
339 }
312 340
313 bool locked = false; 341 bool locked = false;
314 while (wl_display_dispatch(registry->display) != -1) { 342 while (wl_display_dispatch(registry->display) != -1) {