Skip to content

Instantly share code, notes, and snippets.

@CardealRusso
Forked from AlecsFerra/animated_wallpaper.c
Last active March 2, 2024 13:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CardealRusso/51d2e2db3dbbc7645c9898d8ab1a6812 to your computer and use it in GitHub Desktop.
Save CardealRusso/51d2e2db3dbbc7645c9898d8ab1a6812 to your computer and use it in GitHub Desktop.
basic animated wallpapers in Xorg https://youtu.be/guchbe-gKis?t=257
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <X11/Xlib.h>
#include <Imlib2.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc < 4 || argc > 5) {
fprintf(stderr, "Usage: %s <image_folder> <frame_delay> <screen_number> [loop]\n", argv[0]);
return 1;
}
char *image_folder = argv[1];
int frame_delay = atoi(argv[2]);
int screen_number = atoi(argv[3]);
int should_loop = (argc == 5 && atoi(argv[4]) == 1);
Display *display = XOpenDisplay(NULL);
Window root_window = RootWindow(display, screen_number);
imlib_context_set_display(display);
DIR *dir = opendir(image_folder);
while (should_loop || dir) {
rewinddir(dir);
struct dirent *entry;
while ((entry = readdir(dir))) {
if (entry->d_type == DT_REG) {
char image_path[256];
snprintf(image_path, sizeof(image_path), "%s%s", image_folder, entry->d_name);
imlib_context_set_image(imlib_load_image(image_path));
Pixmap pixmap = XCreatePixmap(display, root_window, imlib_image_get_width(), imlib_image_get_height(), DefaultDepth(display, screen_number));
imlib_context_set_drawable(pixmap);
imlib_render_image_on_drawable(0, 0);
XSetWindowBackgroundPixmap(display, root_window, pixmap);
XClearWindow(display, root_window);
XFlush(display);
XFreePixmap(display, pixmap);
imlib_free_image();
usleep(frame_delay * 1000);
}
}
if (!should_loop) break;
}
closedir(dir);
XCloseDisplay(display);
return 0;
}
@narukeh
Copy link

narukeh commented Nov 6, 2023

I have compiled this, but am unable to run it.

$ ls -A ; ~D/ba . 8 0 8
t-0.bmp  t-10.bmp  t-11.bmp  t-12.bmp  t-13.bmp  t-14.bmp  t-15.bmp  t-1.bmp  t-2.bmp  t-3.bmp  t-4.bmp  t-5.bmp  t-6.bmp  t-7.bmp  t-8.bmp  t-9.bmp
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_image_get_height();

        With the parameter:

        image

        being NULL. Please fix your program.
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_image_get_width();

        With the parameter:

        image

        being NULL. Please fix your program.
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_render_image_on_drawable();

        With the parameter:

        image

        being NULL. Please fix your program.
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_free_image();

        With the parameter:

        image

        being NULL. Please fix your program.
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_image_get_height();

        With the parameter:

        image

        being NULL. Please fix your program.
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_image_get_width();

        With the parameter:

        image

        being NULL. Please fix your program.
***** Imlib2 Developer Warning ***** :
        This program is calling the Imlib call:

        imlib_render_image_on_drawable();

        With the parameter:

        image

        being NULL. Please fix your program.
X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  53 (X_CreatePixmap)
  Value in failed request:  0x0
  Serial number of failed request:  7
  Current serial number in output stream:  13

( i got this ***** Imlib2 Developer Warning ***** : when i did not edit the code of https://gist.github.com/AlecsFerra/ef1cc008990319f3b676eb2d8aa89903 to add the list of my .bmp )

@CardealRusso
Copy link
Author

@narukeh give the full path instead of "."

@FeintDoxx
Copy link

I got this compiled but get
"Segmentation Fault"

sudo ./a.out /home/user/pictures/wallpapers/test 10 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment