Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slideshow images loaded when needed (#241) #242

Merged
merged 3 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions i3lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <err.h>
#include <errno.h>
#include <assert.h>
#include <regex.h>
#ifdef __OpenBSD__
#include <bsd_auth.h>
#else
Expand Down Expand Up @@ -244,8 +245,12 @@ static uint8_t xkb_base_event;
static uint8_t xkb_base_error;
static int randr_base = -1;

char *image_path = NULL;
char *image_raw_format = NULL;
char *slideshow_path = NULL;

cairo_surface_t *img = NULL;
cairo_surface_t *img_slideshow[256];
char *img_slideshow[256];
cairo_surface_t *blur_bg_img = NULL;
int slideshow_image_count = 0;
int slideshow_interval = 10;
Expand Down Expand Up @@ -1368,7 +1373,7 @@ static void raise_loop(xcb_window_t window) {
/*
* Loads an image from the given path. Handles JPEG and PNG. Returns NULL in case of error.
*/
static cairo_surface_t* load_image(char* image_path, char* image_raw_format) {
cairo_surface_t* load_image(char* image_path) {
cairo_surface_t *img = NULL;
JPEG_INFO jpg_info;

Expand Down Expand Up @@ -1400,48 +1405,57 @@ static cairo_surface_t* load_image(char* image_path, char* image_raw_format) {
}

/*
* Loads the images from the provided directory and stores them in the pointer array
* Reads the provided directory and stores the images path in the pointer array
* img_slideshow
*/
static void load_slideshow_images(const char *path, char *image_raw_format) {
bool load_slideshow_images(const char *path) {
slideshow_enabled = true;
DIR *d;
struct dirent *dir;
int file_count = 0;
slideshow_image_count = 0;

DEBUG("Loading slideshow images at \"%s\"\n", path);

d = opendir(path);
if (d == NULL) {
printf("Could not open directory: %s\n", path);
exit(EXIT_SUCCESS);
return false;
}

regex_t reg;

if (regcomp(&reg, ".*\\.(jpe?g|png)", REG_EXTENDED)) {
printf("Could not compile regex\n");
return false;
}

while ((dir = readdir(d)) != NULL) {
if (file_count >= 256) {
break;
}
if (file_count >= 256) break;
int result = regexec(&reg, dir->d_name, 0, NULL, 0);
if (result) continue;

char path_to_image[256];
strcpy(path_to_image, path);
strcat(path_to_image, "/");
strcat(path_to_image, dir->d_name);

img_slideshow[file_count] = load_image(path_to_image, image_raw_format);
img_slideshow[file_count] = strdup(path_to_image);

if (img_slideshow[file_count] != NULL) {
++file_count;
}
}

slideshow_image_count = file_count;

regfree(&reg);
closedir(d);
return true;
}

int main(int argc, char *argv[]) {
struct passwd *pw;
char *username;
char *image_path = NULL;
char *image_raw_format = NULL;
#ifndef __OpenBSD__
int ret;
struct pam_conv conv = {conv_callback, NULL};
Expand Down Expand Up @@ -2359,10 +2373,12 @@ int main(int argc, char *argv[]) {
init_colors_once();
if (image_path != NULL) {
if (!is_directory(image_path)) {
img = load_image(image_path, image_raw_format);
img = load_image(image_path);
} else {
/* Path to a directory is provided -> use slideshow mode */
load_slideshow_images(image_path, image_raw_format);
slideshow_path = strdup(image_path);
if (!load_slideshow_images(slideshow_path)) exit(EXIT_FAILURE);
img = load_image(img_slideshow[0]);
}

free(image_path);
Expand Down
21 changes: 14 additions & 7 deletions unlock_indicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ extern char *modifier_string;

/* A Cairo surface containing the specified image (-i), if any. */
extern cairo_surface_t *img;
extern cairo_surface_t *img_slideshow[256];
extern char *image_path;
extern char *slideshow_path;
extern char *img_slideshow[256];
extern cairo_surface_t *blur_bg_img;
extern int slideshow_image_count;
extern int slideshow_interval;
extern bool slideshow_random_selection;
int slideshow_image_now = 0;

unsigned long lastCheck;

Expand Down Expand Up @@ -161,6 +164,9 @@ extern char *lock_failed_text;
extern char *layout_text;
extern char *greeter_text;

bool load_slideshow_images(const char *path);
cairo_surface_t* load_image(char* image_path);

/* Whether the failed attempts should be displayed. */
extern bool show_failed_attempts;
/* Number of failed unlock attempts. */
Expand Down Expand Up @@ -686,13 +692,14 @@ void render_lock(uint32_t *resolution, xcb_drawable_t drawable) {
unsigned long now = (unsigned long)time(NULL);
if (img == NULL || now - lastCheck >= slideshow_interval) {
if (slideshow_random_selection) {
img = img_slideshow[rand() % slideshow_image_count];
img = load_image(img_slideshow[rand() % slideshow_image_count]);
} else {
img = img_slideshow[current_slideshow_index++];

if (current_slideshow_index >= slideshow_image_count) {
current_slideshow_index = 0;
}
img = load_image(img_slideshow[current_slideshow_index]);
}
current_slideshow_index++;
if (current_slideshow_index >= slideshow_image_count) {
current_slideshow_index = 0;
load_slideshow_images(slideshow_path);
}
lastCheck = now;
}
Expand Down