Read PNG from stdin when no argument is given

Change -i to optional_argument
When image_path is the empty string assume stdin
pull/76/head
Mart Lubbers 2016-05-19 12:35:13 +02:00
parent 9a7604bb35
commit de70369f91
1 changed files with 18 additions and 4 deletions

View File

@ -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[]) { int main(int argc, char *argv[]) {
struct passwd *pw; struct passwd *pw;
char *username; char *username;
@ -762,7 +769,7 @@ int main(int argc, char *argv[]) {
{"debug", no_argument, NULL, 0}, {"debug", no_argument, NULL, 0},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"no-unlock-indicator", no_argument, NULL, 'u'}, {"no-unlock-indicator", no_argument, NULL, 'u'},
{"image", required_argument, NULL, 'i'}, {"image", optional_argument, NULL, 'i'},
{"tiling", no_argument, NULL, 't'}, {"tiling", no_argument, NULL, 't'},
{"ignore-empty-password", no_argument, NULL, 'e'}, {"ignore-empty-password", no_argument, NULL, 'e'},
{"inactivity-timeout", required_argument, NULL, 'I'}, {"inactivity-timeout", required_argument, NULL, 'I'},
@ -774,7 +781,7 @@ int main(int argc, char *argv[]) {
if ((username = pw->pw_name) == NULL) if ((username = pw->pw_name) == NULL)
errx(EXIT_FAILURE, "pw->pw_name is NULL.\n"); 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) { while ((o = getopt_long(argc, argv, optstring, longopts, &optind)) != -1) {
switch (o) { switch (o) {
case 'v': case 'v':
@ -811,6 +818,9 @@ int main(int argc, char *argv[]) {
unlock_indicator = false; unlock_indicator = false;
break; break;
case 'i': case 'i':
if (optarg == NULL)
image_path = "";
else
image_path = strdup(optarg); image_path = strdup(optarg);
break; break;
case 't': case 't':
@ -933,7 +943,11 @@ int main(int argc, char *argv[]) {
if (image_path) { if (image_path) {
/* Create a pixmap to render on, fill it with the background color */ /* Create a pixmap to render on, fill it with the background color */
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); img = cairo_image_surface_create_from_png(image_path);
/* In case loading failed, we just pretend no -i was specified. */ /* In case loading failed, we just pretend no -i was specified. */
if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) { if (cairo_surface_status(img) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "Could not load image \"%s\": %s\n", fprintf(stderr, "Could not load image \"%s\": %s\n",