Merge pull request #475 from christophgysin/swaylock_color

swaylock: implement --color
This commit is contained in:
Drew DeVault 2016-01-25 15:28:38 -05:00
commit d2af1f3395

View file

@ -115,14 +115,100 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod
} }
} }
static void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
cairo_set_source_rgba(cairo,
(color >> (3*8) & 0xFF) / 255.0,
(color >> (2*8) & 0xFF) / 255.0,
(color >> (1*8) & 0xFF) / 255.0,
(color >> (0*8) & 0xFF) / 255.0);
}
void render_color(struct window *window, uint32_t color) {
cairo_set_source_u32(window->cairo, color);
cairo_paint(window->cairo);
window_render(window);
}
void render_image(struct window *window, cairo_surface_t *image, enum scaling_mode scaling_mode) {
double width = cairo_image_surface_get_width(image);
double height = cairo_image_surface_get_height(image);
switch (scaling_mode) {
case SCALING_MODE_STRETCH:
cairo_scale(window->cairo,
(double) window->width / width,
(double) window->height / height);
cairo_set_source_surface(window->cairo, image, 0, 0);
break;
case SCALING_MODE_FILL:
{
double window_ratio = (double) window->width / window->height;
double bg_ratio = width / height;
if (window_ratio > bg_ratio) {
double scale = (double) window->width / width;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
0,
(double) window->height/2 / scale - height/2);
} else {
double scale = (double) window->height / height;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
(double) window->width/2 / scale - width/2,
0);
}
break;
}
case SCALING_MODE_FIT:
{
double window_ratio = (double) window->width / window->height;
double bg_ratio = width / height;
if (window_ratio > bg_ratio) {
double scale = (double) window->height / height;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
(double) window->width/2 / scale - width/2,
0);
} else {
double scale = (double) window->width / width;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
0,
(double) window->height/2 / scale - height/2);
}
break;
}
case SCALING_MODE_CENTER:
cairo_set_source_surface(window->cairo, image,
(double) window->width/2 - width/2,
(double) window->height/2 - height/2);
break;
case SCALING_MODE_TILE:
{
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
cairo_set_source(window->cairo, pattern);
break;
}
}
cairo_paint(window->cairo);
window_render(window);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
char *image_path = NULL; char *image_path = NULL;
char *scaling_mode_str = "fit"; char *scaling_mode_str = "fit";
uint32_t color = 0xFFFFFFFF;
init_log(L_INFO); init_log(L_INFO);
static struct option long_options[] = { static struct option long_options[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"color", required_argument, NULL, 'c'},
{"image", required_argument, NULL, 'i'}, {"image", required_argument, NULL, 'i'},
{"scaling", required_argument, NULL, 's'}, {"scaling", required_argument, NULL, 's'},
{"tiling", no_argument, NULL, 't'}, {"tiling", no_argument, NULL, 't'},
@ -134,6 +220,7 @@ int main(int argc, char **argv) {
"Usage: swaylock [options...]\n" "Usage: swaylock [options...]\n"
"\n" "\n"
" -h, --help Show help message and quit.\n" " -h, --help Show help message and quit.\n"
" -c, --color <rrggbb> Turn the screen into the given color instead of white.\n"
" -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n"
" -t, --tiling Same as --scaling=tile.\n" " -t, --tiling Same as --scaling=tile.\n"
" -v, --version Show the version number and quit.\n" " -v, --version Show the version number and quit.\n"
@ -142,11 +229,21 @@ int main(int argc, char **argv) {
int c; int c;
while (1) { while (1) {
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "hi:s:tv", long_options, &option_index); c = getopt_long(argc, argv, "hc:i:s:tv", long_options, &option_index);
if (c == -1) { if (c == -1) {
break; break;
} }
switch (c) { switch (c) {
case 'c':
if (strlen(optarg) < 6) {
fprintf(stderr, "color must be specified in 3 byte format, e.g. ff0000\n");
exit(EXIT_FAILURE);
}
color = strtol(optarg, NULL, 16);
color <<= 8;
color |= 0xFF;
sway_log(L_DEBUG, "color: 0x%x", color);
break;
case 'i': case 'i':
image_path = optarg; image_path = optarg;
break; break;
@ -170,10 +267,19 @@ int main(int argc, char **argv) {
} }
} }
// TODO: support locking without image enum scaling_mode scaling_mode = SCALING_MODE_STRETCH;
if (!image_path) { if (strcmp(scaling_mode_str, "stretch") == 0) {
fprintf(stderr, "No image specified!\n"); scaling_mode = SCALING_MODE_STRETCH;
exit(EXIT_FAILURE); } else if (strcmp(scaling_mode_str, "fill") == 0) {
scaling_mode = SCALING_MODE_FILL;
} else if (strcmp(scaling_mode_str, "fit") == 0) {
scaling_mode = SCALING_MODE_FIT;
} else if (strcmp(scaling_mode_str, "center") == 0) {
scaling_mode = SCALING_MODE_CENTER;
} else if (strcmp(scaling_mode_str, "tile") == 0) {
scaling_mode = SCALING_MODE_TILE;
} else {
sway_abort("Unsupported scaling mode: %s", scaling_mode_str);
} }
password = malloc(1024); // TODO: Let this grow password = malloc(1024); // TODO: Let this grow
@ -197,111 +303,40 @@ int main(int argc, char **argv) {
registry->input->notify = notify_key; registry->input->notify = notify_key;
#ifdef WITH_GDK_PIXBUF cairo_surface_t *image = NULL;
GError *err = NULL;
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err);
if (!pixbuf) {
sway_abort("Failed to load background image.");
}
cairo_surface_t *image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf);
g_object_unref(pixbuf);
#else
cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]);
#endif //WITH_GDK_PIXBUF
if (!image) {
sway_abort("Failed to read background image.");
}
double width = cairo_image_surface_get_width(image);
double height = cairo_image_surface_get_height(image);
enum scaling_mode scaling_mode = SCALING_MODE_STRETCH; if (image_path) {
if (strcmp(scaling_mode_str, "stretch") == 0) { #ifdef WITH_GDK_PIXBUF
scaling_mode = SCALING_MODE_STRETCH; GError *err = NULL;
} else if (strcmp(scaling_mode_str, "fill") == 0) { GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err);
scaling_mode = SCALING_MODE_FILL; if (!pixbuf) {
} else if (strcmp(scaling_mode_str, "fit") == 0) { sway_abort("Failed to load background image.");
scaling_mode = SCALING_MODE_FIT; }
} else if (strcmp(scaling_mode_str, "center") == 0) { image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf);
scaling_mode = SCALING_MODE_CENTER; g_object_unref(pixbuf);
} else if (strcmp(scaling_mode_str, "tile") == 0) { #else
scaling_mode = SCALING_MODE_TILE; cairo_surface_t *image = cairo_image_surface_create_from_png(argv[1]);
} else { #endif //WITH_GDK_PIXBUF
sway_abort("Unsupported scaling mode: %s", scaling_mode_str); if (!image) {
sway_abort("Failed to read background image.");
}
} }
for (i = 0; i < surfaces->length; ++i) { for (i = 0; i < surfaces->length; ++i) {
struct window *window = surfaces->items[i]; struct window *window = surfaces->items[i];
if (window_prerender(window) && window->cairo) { if (!window_prerender(window) || !window->cairo) {
switch (scaling_mode) { continue;
case SCALING_MODE_STRETCH: }
cairo_scale(window->cairo, if (image) {
(double) window->width / width, render_image(window, image, scaling_mode);
(double) window->height / height); } else {
cairo_set_source_surface(window->cairo, image, 0, 0); render_color(window, color);
break;
case SCALING_MODE_FILL:
{
double window_ratio = (double) window->width / window->height;
double bg_ratio = width / height;
if (window_ratio > bg_ratio) {
double scale = (double) window->width / width;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
0,
(double) window->height/2 / scale - height/2);
} else {
double scale = (double) window->height / height;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
(double) window->width/2 / scale - width/2,
0);
}
break;
}
case SCALING_MODE_FIT:
{
double window_ratio = (double) window->width / window->height;
double bg_ratio = width / height;
if (window_ratio > bg_ratio) {
double scale = (double) window->height / height;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
(double) window->width/2 / scale - width/2,
0);
} else {
double scale = (double) window->width / width;
cairo_scale(window->cairo, scale, scale);
cairo_set_source_surface(window->cairo, image,
0,
(double) window->height/2 / scale - height/2);
}
break;
}
case SCALING_MODE_CENTER:
cairo_set_source_surface(window->cairo, image,
(double) window->width/2 - width/2,
(double) window->height/2 - height/2);
break;
case SCALING_MODE_TILE:
{
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
cairo_set_source(window->cairo, pattern);
break;
}
default:
sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str);
}
cairo_paint(window->cairo);
window_render(window);
} }
} }
cairo_surface_destroy(image); if (image) {
cairo_surface_destroy(image);
}
bool locked = false; bool locked = false;
while (wl_display_dispatch(registry->display) != -1) { while (wl_display_dispatch(registry->display) != -1) {