From 1ca54536784917738d1be595e0e9bc1b07f3c364 Mon Sep 17 00:00:00 2001 From: Nuew Date: Tue, 22 Mar 2016 01:20:11 -0400 Subject: [PATCH 1/5] Added --window-image option to swaylock This allows setting window specific images. Also Changed: - The colored background is _always_ drawn, to prevent non-fullscreen images from leaking information. - Added function list_arbitrary_insert, which allows insertion to a list at an arbitrary location less than the capacity. It silently fails otherwise. --- common/list.c | 8 +++ include/list.h | 1 + swaylock/main.c | 132 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 108 insertions(+), 33 deletions(-) diff --git a/common/list.c b/common/list.c index a7585a311..0e7c9e91b 100644 --- a/common/list.c +++ b/common/list.c @@ -47,6 +47,14 @@ void list_insert(list_t *list, int index, void *item) { list->items[index] = item; } +// Added because IDK if it's safe to remove that memmove +void list_arbitrary_insert(list_t *list, int index, void *item) { + list_resize(list); + if(index > list->capacity) return; + if(list->length < index) list->length = index; + list->items[index] = item; +} + void list_del(list_t *list, int index) { list->length--; memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); diff --git a/include/list.h b/include/list.h index b2e26f958..6343ca27c 100644 --- a/include/list.h +++ b/include/list.h @@ -12,6 +12,7 @@ void list_free(list_t *list); void list_foreach(list_t *list, void (*callback)(void* item)); void list_add(list_t *list, void *item); void list_insert(list_t *list, int index, void *item); +void list_arbitrary_insert(list_t *list, int index, void *item); void list_del(list_t *list, int index); void list_cat(list_t *list, list_t *source); // See qsort. Remember to use *_qsort functions as compare functions, diff --git a/swaylock/main.c b/swaylock/main.c index f2015a051..32e6086f5 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -12,6 +12,7 @@ #include "client/registry.h" #include "client/cairo.h" #include "log.h" +#include "list.h" list_t *surfaces; struct registry *registry; @@ -24,6 +25,17 @@ enum scaling_mode { SCALING_MODE_TILE, }; +enum none_one_list_choice { + NOL_NONE, + NOL_ONE, + NOL_LIST +}; + +struct none_one_list { + enum none_one_list_choice type; + void *a, *b; +}; + void sway_terminate(int exit_code) { int i; for (i = 0; i < surfaces->length; ++i) { @@ -124,7 +136,6 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod 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) { @@ -193,14 +204,35 @@ void render_image(struct window *window, cairo_surface_t *image, enum scaling_mo } cairo_paint(window->cairo); +} - window_render(window); +cairo_surface_t *load_image(char *image_path) { + cairo_surface_t *image = NULL; + +#ifdef WITH_GDK_PIXBUF + GError *err = NULL; + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err); + if (!pixbuf) { + fprintf(stderr, "GDK Error: %s\n", err->message); + sway_abort("Failed to load background image."); + } + image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); + g_object_unref(pixbuf); +#else + image = cairo_image_surface_create_from_png(image_path); +#endif //WITH_GDK_PIXBUF + if (!image) { + sway_abort("Failed to read background image."); + } + + return image; } int main(int argc, char **argv) { - char *image_path = NULL; + struct none_one_list images = { NOL_NONE, NULL, NULL }; char *scaling_mode_str = "fit"; uint32_t color = 0xFFFFFFFF; + int i; init_log(L_INFO); @@ -211,18 +243,20 @@ int main(int argc, char **argv) { {"scaling", required_argument, NULL, 's'}, {"tiling", no_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, + {"window-image", required_argument, NULL, 'w'}, {0, 0, 0, 0} }; const char *usage = "Usage: swaylock [options...]\n" "\n" - " -h, --help Show help message and quit.\n" - " -c, --color Turn the screen into the given color instead of white.\n" - " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" - " -t, --tiling Same as --scaling=tile.\n" - " -v, --version Show the version number and quit.\n" - " -i, --image Display the given image.\n"; + " -h, --help Show help message and quit.\n" + " -c, --color Turn the screen into the given color instead of white.\n" + " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" + " -t, --tiling Same as --scaling=tile.\n" + " -v, --version Show the version number and quit.\n" + " -i, --image Display the given image.\n" + " --window-image : Display the given image on the given window.\n"; int c; while (1) { @@ -249,7 +283,36 @@ int main(int argc, char **argv) { break; } case 'i': - image_path = optarg; + if(images.type == NOL_NONE) { + images = (struct none_one_list) { NOL_ONE, optarg, NULL }; + } else { + fprintf(stderr, "only one of --image/--window-image is allowed\n"); + exit(EXIT_FAILURE); + } + break; + case 'w': + if(images.type == NOL_NONE) { + images = (struct none_one_list) { NOL_LIST, create_list(), create_list() }; + for(i = 0; i <= ((list_t*) images.a)->capacity; ++i) { + // this isn't perfect - if you hav>e more than 10 monitors there's a small chance of + // a segfault from uninitialized data. I honestly don't think enough people have more than + // 10 monitors to care about that though. + ((list_t*) images.a)->items[i] = NULL; + ((list_t*) images.b)->items[i] = NULL; + } + } else if(images.type == NOL_ONE) { + fprintf(stderr, "only one of --image/--window-image is allowed\n"); + exit(EXIT_FAILURE); + } + + char *image_path; + int index = strtol(optarg, &image_path, 0); + if(*image_path != ':') { + fprintf(stderr, "--window-image should be in form ':'\n"); + exit(EXIT_FAILURE); + } else image_path++; + + list_arbitrary_insert(images.a, index, image_path); break; case 's': scaling_mode_str = optarg; @@ -302,7 +365,6 @@ int main(int argc, char **argv) { registry->pointer = NULL; } - int i; for (i = 0; i < registry->outputs->length; ++i) { struct output_state *output = registry->outputs->items[i]; struct window *window = window_setup(registry, output->width, output->height, true); @@ -314,22 +376,13 @@ int main(int argc, char **argv) { registry->input->notify = notify_key; - cairo_surface_t *image = NULL; - - if (image_path) { -#ifdef WITH_GDK_PIXBUF - GError *err = NULL; - GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err); - if (!pixbuf) { - sway_abort("Failed to load background image."); - } - 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."); + if(images.type == NOL_ONE) { + images.b = load_image(images.a); + } else if(images.type == NOL_LIST) { + for(i = 0; i <= ((list_t*) images.a)->length; ++i) { + if(((list_t*) images.a)->items[i] != NULL) { + list_arbitrary_insert(images.b, i, load_image(((list_t*) images.a)->items[i])); + } } } @@ -338,15 +391,28 @@ int main(int argc, char **argv) { if (!window_prerender(window) || !window->cairo) { continue; } - if (image) { - render_image(window, image, scaling_mode); - } else { - render_color(window, color); + // always render the color in case the image doesn't fully cover the screen + render_color(window, color); + + if (images.type == NOL_ONE) { + render_image(window, images.b, scaling_mode); + } else if(images.type == NOL_LIST) { + if(((list_t*) images.b)->items[i] != NULL) { + render_image(window, ((list_t*) images.b)->items[i], scaling_mode); + } } + + window_render(window); } - if (image) { - cairo_surface_destroy(image); + if(images.type == NOL_ONE) { + cairo_surface_destroy(images.b); + } else if(images.type == NOL_LIST) { + for(i = 0; i <= ((list_t*) images.a)->length; ++i) { + cairo_surface_destroy(((list_t*) images.b)->items[i]); + } + list_free(images.a); + list_free(images.b); } bool locked = false; From 34cc909efbf2c1b2005eac1e7938d72be6eaea24 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 22 Mar 2016 01:38:52 -0400 Subject: [PATCH 2/5] Fix small coding style issue --- swaylock/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/swaylock/main.c b/swaylock/main.c index 32e6086f5..65673a118 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -310,7 +310,9 @@ int main(int argc, char **argv) { if(*image_path != ':') { fprintf(stderr, "--window-image should be in form ':'\n"); exit(EXIT_FAILURE); - } else image_path++; + } else { + image_path++; + } list_arbitrary_insert(images.a, index, image_path); break; From 4c8343f1eecc021d559ea9b39d1cda3c7d707084 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 22 Mar 2016 01:40:01 -0400 Subject: [PATCH 3/5] Tabs and more style. --- common/list.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/common/list.c b/common/list.c index 0e7c9e91b..49e049527 100644 --- a/common/list.c +++ b/common/list.c @@ -50,8 +50,12 @@ void list_insert(list_t *list, int index, void *item) { // Added because IDK if it's safe to remove that memmove void list_arbitrary_insert(list_t *list, int index, void *item) { list_resize(list); - if(index > list->capacity) return; - if(list->length < index) list->length = index; + if(index > list->capacity) { + return; + } + if(list->length < index) { + list->length = index; + } list->items[index] = item; } From 0aa4383c9a1b3b693ee771e7829ed84582b4577c Mon Sep 17 00:00:00 2001 From: Nuew Date: Thu, 24 Mar 2016 18:02:34 -0400 Subject: [PATCH 4/5] Add choice of display to --image. Additionally - Made background colors display when an image is enabled if a --color is specified. - Link CJson to swaylock. - Add the --socket option to swaylock. --- swaylock/CMakeLists.txt | 2 + swaylock/main.c | 153 +++++++++++++++++++++++++++++++--------- swaylock/swaylock.1.txt | 12 +++- 3 files changed, 131 insertions(+), 36 deletions(-) diff --git a/swaylock/CMakeLists.txt b/swaylock/CMakeLists.txt index 08c893ca0..b290cd2fb 100644 --- a/swaylock/CMakeLists.txt +++ b/swaylock/CMakeLists.txt @@ -4,6 +4,7 @@ include_directories( ${CAIRO_INCLUDE_DIRS} ${PANGO_INCLUDE_DIRS} ${PAM_INCLUDE_DIRS} + ${JSONC_INCLUDE_DIRS} ) add_executable(swaylock @@ -18,6 +19,7 @@ target_link_libraries(swaylock ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES} ${PAM_LIBRARIES} + ${JSONC_LIBRARIES} m ) diff --git a/swaylock/main.c b/swaylock/main.c index feb64975d..7f4c424d1 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -11,6 +12,7 @@ #include "client/window.h" #include "client/registry.h" #include "client/cairo.h" +#include "ipc-client.h" #include "log.h" list_t *surfaces; @@ -134,7 +136,6 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod 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) { @@ -203,14 +204,35 @@ void render_image(struct window *window, cairo_surface_t *image, enum scaling_mo } cairo_paint(window->cairo); +} - window_render(window); +cairo_surface_t *load_image(char *image_path) { + cairo_surface_t *image = NULL; + +#ifdef WITH_GDK_PIXBUF + GError *err = NULL; + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err); + if (!pixbuf) { + fprintf(stderr, "GDK Error: %s\n", err->message); + sway_abort("Failed to load background image."); + } + image = gdk_cairo_image_surface_create_from_pixbuf(pixbuf); + g_object_unref(pixbuf); +#else + image = cairo_image_surface_create_from_png(image_path); +#endif //WITH_GDK_PIXBUF + if (!image) { + sway_abort("Failed to read background image."); + } + + return image; } int main(int argc, char **argv) { - char *image_path = NULL; - char *scaling_mode_str = "fit"; + char *scaling_mode_str = "fit", *socket_path = NULL; + int i, num_images = 0, color_set = 0; uint32_t color = 0xFFFFFFFF; + void *images; init_log(L_INFO); @@ -221,18 +243,22 @@ int main(int argc, char **argv) { {"scaling", required_argument, NULL, 's'}, {"tiling", no_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, + {"socket", required_argument, NULL, 'p'}, {0, 0, 0, 0} }; const char *usage = "Usage: swaylock [options...]\n" "\n" - " -h, --help Show help message and quit.\n" - " -c, --color Turn the screen into the given color instead of white.\n" - " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" - " -t, --tiling Same as --scaling=tile.\n" - " -v, --version Show the version number and quit.\n" - " -i, --image Display the given image.\n"; + " -h, --help Show help message and quit.\n" + " -c, --color Turn the screen into the given color instead of white.\n" + " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" + " -t, --tiling Same as --scaling=tile.\n" + " -v, --version Show the version number and quit.\n" + " -i, --image [:] Display the given image.\n" + " --socket Use the specified socket.\n"; + + registry = registry_poll(); int c; while (1) { @@ -250,6 +276,7 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } color = strtol(optarg, NULL, 16); + color_set = 1; if (colorlen == 6) { color <<= 8; @@ -259,14 +286,39 @@ int main(int argc, char **argv) { break; } case 'i': - image_path = optarg; + { + char *image_path = strchr(optarg, ':'); + if (image_path == NULL) { + if (num_images == 0) { + images = load_image(optarg); + num_images = -1; + } else { + fprintf(stderr, "display must be defined for all --images or no --images.\n"); + exit(EXIT_FAILURE); + } + } else { + if (num_images == 0) { + images = calloc(registry->outputs->length, sizeof(char*) * 2); + } else if (num_images == -1) { + fprintf(stderr, "display must be defined for all --images or no --images.\n"); + exit(EXIT_FAILURE); + } + + image_path[0] = '\0'; + ((char**) images)[num_images * 2] = optarg; + ((char**) images)[num_images++ * 2 + 1] = ++image_path; + } break; + } case 's': scaling_mode_str = optarg; break; case 't': scaling_mode_str = "tile"; break; + case 'p': + socket_path = optarg; + break; case 'v': #if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE fprintf(stdout, "swaylock version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH); @@ -300,7 +352,12 @@ int main(int argc, char **argv) { password = malloc(password_size); password[0] = '\0'; surfaces = create_list(); - registry = registry_poll(); + if (!socket_path) { + socket_path = get_socketpath(); + if (!socket_path) { + sway_abort("Unable to retrieve socket path"); + } + } if (!registry) { sway_abort("Unable to connect to wayland compositor"); @@ -316,7 +373,6 @@ int main(int argc, char **argv) { registry->pointer = NULL; } - int i; for (i = 0; i < registry->outputs->length; ++i) { struct output_state *output = registry->outputs->items[i]; struct window *window = window_setup(registry, output->width, output->height, true); @@ -328,23 +384,38 @@ int main(int argc, char **argv) { registry->input->notify = notify_key; - cairo_surface_t *image = NULL; + if (num_images >= 1) { + char **displays_paths = images; + images = calloc(registry->outputs->length, sizeof(cairo_surface_t*)); - if (image_path) { -#ifdef WITH_GDK_PIXBUF - GError *err = NULL; - GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(image_path, &err); - if (!pixbuf) { - sway_abort("Failed to load background image."); - } - 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."); + int socketfd = ipc_open_socket(socket_path); + uint32_t len = 0; + char *outputs = ipc_single_command(socketfd, IPC_GET_OUTPUTS, "", &len); + struct json_object *json_outputs = json_tokener_parse(outputs); + + for (i = 0; i < registry->outputs->length; ++i) { + if (displays_paths[i * 2] != NULL) { + for (int j = 0;; ++j) { + if (j >= json_object_array_length(json_outputs)) { + fprintf(stderr, "%s is not an extant display\n", displays_paths[i * 2]); + exit(EXIT_FAILURE); + } + + struct json_object *dsp_name, *at_j = json_object_array_get_idx(json_outputs, j); + if (!json_object_object_get_ex(at_j, "name", &dsp_name)) { + sway_abort("display doesn't have a name field"); + } + if (!strcmp(displays_paths[i * 2], json_object_get_string(dsp_name))) { + ((cairo_surface_t**) images)[j] = load_image(displays_paths[i * 2 + 1]); + break; + } + } + } } + + json_object_put(json_outputs); + close(socketfd); + free(displays_paths); } for (i = 0; i < surfaces->length; ++i) { @@ -352,15 +423,31 @@ int main(int argc, char **argv) { if (!window_prerender(window) || !window->cairo) { continue; } - if (image) { - render_image(window, image, scaling_mode); - } else { + + if (num_images == 0 || color_set) { render_color(window, color); } + + if (num_images == -1) { + render_image(window, images, scaling_mode); + } else if (num_images >= 1) { + if (((cairo_surface_t**) images)[i] != NULL) { + render_image(window, ((cairo_surface_t**) images)[i], scaling_mode); + } + } + + window_render(window); } - if (image) { - cairo_surface_destroy(image); + if (num_images == -1) { + cairo_surface_destroy((cairo_surface_t*) images); + } else if (num_images >= 1) { + for (i = 0; i < registry->outputs->length; ++i) { + if (((cairo_surface_t**) images)[i] != NULL) { + cairo_surface_destroy(((cairo_surface_t**) images)[i]); + } + } + free(images); } bool locked = false; diff --git a/swaylock/swaylock.1.txt b/swaylock/swaylock.1.txt index 04594fd09..724573cee 100644 --- a/swaylock/swaylock.1.txt +++ b/swaylock/swaylock.1.txt @@ -23,10 +23,11 @@ Options Show help message and quit. *-c, \--color* :: - Turn the screen into the given color instead of white. + Turn the screen into the given color instead of white. If an image is in use, + also enables color display. -*-i, \--image* :: - Display the given image. +*-i, \--image* [:]:: + Display the given image, optionally only on the given display. *-s, \--scaling*:: Scaling mode for images: stretch, fill, fit, center, or tile. @@ -37,6 +38,11 @@ Options *-v, \--version*:: Show the version number and quit. +*--socket *:: + Use the specified socket path. Otherwise, swaymsg will ask sway where the + socket is (which is the value of $SWAYSOCK, then of $I3SOCK). + + Authors ------- From 382cd18367124f627aaba4983cfe4a884b6d00ae Mon Sep 17 00:00:00 2001 From: Nuew Date: Thu, 24 Mar 2016 18:18:46 -0400 Subject: [PATCH 5/5] Remove list_arbitrary_insert --- common/list.c | 12 ------------ include/list.h | 1 - 2 files changed, 13 deletions(-) diff --git a/common/list.c b/common/list.c index 49e049527..a7585a311 100644 --- a/common/list.c +++ b/common/list.c @@ -47,18 +47,6 @@ void list_insert(list_t *list, int index, void *item) { list->items[index] = item; } -// Added because IDK if it's safe to remove that memmove -void list_arbitrary_insert(list_t *list, int index, void *item) { - list_resize(list); - if(index > list->capacity) { - return; - } - if(list->length < index) { - list->length = index; - } - list->items[index] = item; -} - void list_del(list_t *list, int index) { list->length--; memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index)); diff --git a/include/list.h b/include/list.h index 6343ca27c..b2e26f958 100644 --- a/include/list.h +++ b/include/list.h @@ -12,7 +12,6 @@ void list_free(list_t *list); void list_foreach(list_t *list, void (*callback)(void* item)); void list_add(list_t *list, void *item); void list_insert(list_t *list, int index, void *item); -void list_arbitrary_insert(list_t *list, int index, void *item); void list_del(list_t *list, int index); void list_cat(list_t *list, list_t *source); // See qsort. Remember to use *_qsort functions as compare functions,