From de70369f919388ec3f131e8a80effe4c3b16b9f5 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Thu, 19 May 2016 12:35:13 +0200 Subject: [PATCH] Read PNG from stdin when no argument is given Change -i to optional_argument When image_path is the empty string assume stdin --- i3lock.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/i3lock.c b/i3lock.c index 928953b..f712e52 100644 --- a/i3lock.c +++ b/i3lock.c @@ -743,6 +743,13 @@ static void raise_loop(xcb_window_t window) { } } +cairo_status_t img_read(void *closure, unsigned char *data, + unsigned int length) { + return fread(data, 1, length, stdin) == length ? CAIRO_STATUS_SUCCESS + : CAIRO_STATUS_READ_ERROR; + (void)closure; +} + int main(int argc, char *argv[]) { struct passwd *pw; char *username; @@ -762,7 +769,7 @@ int main(int argc, char *argv[]) { {"debug", no_argument, NULL, 0}, {"help", no_argument, NULL, 'h'}, {"no-unlock-indicator", no_argument, NULL, 'u'}, - {"image", required_argument, NULL, 'i'}, + {"image", optional_argument, NULL, 'i'}, {"tiling", no_argument, NULL, 't'}, {"ignore-empty-password", no_argument, NULL, 'e'}, {"inactivity-timeout", required_argument, NULL, 'I'}, @@ -774,7 +781,7 @@ int main(int argc, char *argv[]) { if ((username = pw->pw_name) == NULL) errx(EXIT_FAILURE, "pw->pw_name is NULL.\n"); - char *optstring = "hvnbdc:p:ui:teI:f"; + char *optstring = "hvnbdc:p:ui::teI:f"; while ((o = getopt_long(argc, argv, optstring, longopts, &optind)) != -1) { switch (o) { case 'v': @@ -811,7 +818,10 @@ int main(int argc, char *argv[]) { unlock_indicator = false; break; case 'i': - image_path = strdup(optarg); + if (optarg == NULL) + image_path = ""; + else + image_path = strdup(optarg); break; case 't': tile = true; @@ -933,7 +943,11 @@ int main(int argc, char *argv[]) { if (image_path) { /* Create a pixmap to render on, fill it with the background color */ - img = cairo_image_surface_create_from_png(image_path); + if (strlen(image_path) == 0) + img = cairo_image_surface_create_from_png_stream(img_read, NULL); + else + img = cairo_image_surface_create_from_png(image_path); + /* In case loading failed, we just pretend no -i was specified. */ if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "Could not load image \"%s\": %s\n",