From 8c881feb2ea05a927c12f9c07be2b1f385af8a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahsan=20David=20P=C3=A9rez?= Date: Sat, 18 Dec 2021 21:59:58 -0500 Subject: [PATCH 1/4] Implementation of different image formats --- Makefile | 7 ++++-- xseticon.c | 67 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 35ad8e9..f8a1099 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,16 @@ GLIB_CFLAGS=$(shell pkg-config --cflags glib-2.0) GLIB_LIBS=$(shell pkg-config --libs glib-2.0) +MAGICKWAND_CFLAGS=$(shell pkg-config --cflags MagickWand) + MAGICKWAND_LIBS=$(shell pkg-config --libs MagickWand) + XLIB_CFLAGS=-I /usr/X11R6/include XLIB_LIBS=-L /usr/X11R6/lib -lX11 -lXmu GD_LIBS=-lgd -LIBS=${GLIB_LIBS} ${XLIB_LIBS} ${GD_LIBS} +LIBS=${GLIB_LIBS} ${MAGICKWAND_LIBS} ${XLIB_LIBS} ${GD_LIBS} PREFIX=/usr/local BINDIR=$(PREFIX)/bin @@ -15,7 +18,7 @@ BINDIR=$(PREFIX)/bin all: xseticon xseticon.o: xseticon.c - gcc ${GLIB_CFLAGS} ${XLIB_CFLAGS} -c $^ -o $@ + gcc ${GLIB_CFLAGS} ${MAGICKWAND_CFLAGS} ${XLIB_CFLAGS} -c $^ -o $@ xseticon: xseticon.o gcc $^ ${LIBS} -o $@ diff --git a/xseticon.c b/xseticon.c index 28167ae..6e3f853 100644 --- a/xseticon.c +++ b/xseticon.c @@ -24,10 +24,10 @@ #include #include -#include #include #include #include +#include #include @@ -134,11 +134,8 @@ Window Select_Window_Args(Display* dpy, int screen, int* rargc, char* argv[]) NXTOPT; if (verbose) printf("Selecting window by ID %s\n", OPTION); - w=0; - sscanf(OPTION, "0x%lx", &w); - if (!w) - sscanf(OPTION, "%ld", &w); - if (!w) + w = strtoul(OPTION, NULL, 0); + if (!w || (w == LONG_MIN || w == LONG_MAX)) Fatal_Error("Invalid window id format: %s.", OPTION); continue; } @@ -201,25 +198,44 @@ void abortprog(gchar* fname) exit(1); } +/* + * Loads the filename with MagickWand a gdImagePtr from it if the file is valid if not returns NULL + */ +gdImagePtr get_png_image_from_file(gchar *filename) +{ + MagickWandGenesis(); + gdImagePtr imagePtr = NULL; // Default value, if the image can't be loaded then the result is going to be NULL + MagickWand *wand = NewMagickWand(); + PixelWand *pixelWand = NewPixelWand(); + PixelSetColor(pixelWand, "transparent"); // Color for the background + MagickSetBackgroundColor(wand, pixelWand); // The background has to be assigned before loading the file image + // to avoid white backgrounds (happens with svg files) + MagickReadImage(wand, filename); // Reads the file + int success = MagickSetImageFormat(wand, "png"); // Converts the file to PNG format + if (success) { // If the conversion is successful then it creates an imagePtr from the content of the file + size_t length; + unsigned char *info = MagickGetImageBlob(wand, &length); // Gets image data and size to create an imagePtr + imagePtr = gdImageCreateFromPngPtr((int) length, info); // Creates imagePtr from png data + } + DestroyMagickWand(wand); + MagickWandTerminus(); + return imagePtr; +} + /* Note: * dispite the fact this routine specifically loads 32bit data, it needs to * load it into an unsigned long int array, not a guint32 array. The * XChangeProperty() call wants to see a native size array when format == 32, * not necessarily a 32bit one. */ - void load_icon(gchar* filename, int* ndata, CARD32** data) { - FILE* iconfile = fopen(filename, "r"); + gdImagePtr icon = get_png_image_from_file(filename); - if (!iconfile) { - abortprog("fopen()"); + if (icon == NULL) { + abortprog("get_png_image_from_file(filename)"); } - gdImagePtr icon = gdImageCreateFromPng(iconfile); - - fclose(iconfile); - int width, height; width = gdImageSX(icon); @@ -297,16 +313,13 @@ int main(int argc, char* argv[]) if (verbose) printf("Selecting window by mouse...\n"); window = Select_Window_Mouse(display, screen); - if (window != None) { - Window root; - int dummyi; - unsigned int dummy; - - if (XGetGeometry (display, window, &root, &dummyi, &dummyi, - &dummy, &dummy, &dummy, &dummy) - && window != root) - window = XmuClientWindow (display, window); - } + Window root; + int dummyi; + unsigned int dummy; + if (XGetGeometry(display, window, &root, &dummyi, &dummyi, + &dummy, &dummy, &dummy, &dummy) + && window != root) + window = XmuClientWindow(display, window); } if (verbose) @@ -320,10 +333,10 @@ int main(int argc, char* argv[]) guint nelements; CARD32* data; - load_icon(argv[argindex], &nelements, &data); + load_icon(argv[argindex], (int *) &nelements, &data); - int result = XChangeProperty(display, window, property, XA_CARDINAL, 32, PropModeReplace, - (gchar*)data, nelements); + int result = XChangeProperty(display, window, property, XA_CARDINAL, 32, PropModeReplace, + (guchar*) data, (int) nelements); if(!result) abortprog("XChangeProperty"); From a272c36352903253edf756601f82f0d354032b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahsan=20David=20P=C3=A9rez?= Date: Sat, 18 Dec 2021 22:16:49 -0500 Subject: [PATCH 2/4] Implementation of different image formats --- xseticon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xseticon.c b/xseticon.c index 6e3f853..1ad96c9 100644 --- a/xseticon.c +++ b/xseticon.c @@ -199,7 +199,7 @@ void abortprog(gchar* fname) } /* - * Loads the filename with MagickWand a gdImagePtr from it if the file is valid if not returns NULL + * Loads the filename with MagickWand and creates a gdImagePtr from it if the file is valid if not returns NULL */ gdImagePtr get_png_image_from_file(gchar *filename) { From 7a7f1e6c63623b858118b1a6bd7467f90ccb7401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahsan=20David=20P=C3=A9rez?= Date: Sun, 19 Dec 2021 11:39:16 -0500 Subject: [PATCH 3/4] Fixing comments and dependencies --- README.md | 8 ++++---- xseticon.c | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index aa5b9d0..4cba305 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Xterm, and likely many other X11 programs, do not set themselves window icons, w Usage ===== - usage: xseticon [options] path/to/icon.png + usage: xseticon [options] path/to/icon options: -name : apply icon to the window of the name supplied -id : apply icon to the window id supplied @@ -15,7 +15,7 @@ Usage selected using the cursor. Hints: - xseticon -id "$WINDOWID" path/to/icon.png + xseticon -id "$WINDOWID" path/to/icon Will set the icon for an xterm.xseticon from Paul Evans (http://www.leonerd.org.uk/). Build and install instructions @@ -25,7 +25,7 @@ On Ubuntu / Debian: ``` bash # Install dependencies -sudo apt install libxmu-headers libgd-dev libxmu-dev libglib2.0-dev +sudo apt install libxmu-headers libgd-dev libxmu-dev libglib2.0-dev libmagickwand-dev # Build make # Install @@ -35,7 +35,7 @@ On EL7: ```bash # Install dependencies -sudo yum install libXmu-devel gd gd-devel glib2-devel +sudo yum install libXmu-devel gd gd-devel glib2-devel ImageMagick-devel # Build make # Install diff --git a/xseticon.c b/xseticon.c index 1ad96c9..2216a13 100644 --- a/xseticon.c +++ b/xseticon.c @@ -203,7 +203,7 @@ void abortprog(gchar* fname) */ gdImagePtr get_png_image_from_file(gchar *filename) { - MagickWandGenesis(); + MagickWandGenesis(); // Starts MagickWand gdImagePtr imagePtr = NULL; // Default value, if the image can't be loaded then the result is going to be NULL MagickWand *wand = NewMagickWand(); PixelWand *pixelWand = NewPixelWand(); @@ -211,14 +211,14 @@ gdImagePtr get_png_image_from_file(gchar *filename) MagickSetBackgroundColor(wand, pixelWand); // The background has to be assigned before loading the file image // to avoid white backgrounds (happens with svg files) MagickReadImage(wand, filename); // Reads the file - int success = MagickSetImageFormat(wand, "png"); // Converts the file to PNG format - if (success) { // If the conversion is successful then it creates an imagePtr from the content of the file + int success = MagickSetImageFormat(wand, "png"); // Converts the wand to PNG format + if (success) { // If the conversion is successful then it creates an imagePtr from the content of the wand size_t length; - unsigned char *info = MagickGetImageBlob(wand, &length); // Gets image data and size to create an imagePtr + unsigned char *info = MagickGetImageBlob(wand, &length); // Gets image data and size from the wand imagePtr = gdImageCreateFromPngPtr((int) length, info); // Creates imagePtr from png data } DestroyMagickWand(wand); - MagickWandTerminus(); + MagickWandTerminus(); // Ends MagickWand return imagePtr; } From 17062f845a754f15face3da9d7a462dcede4ba7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahsan=20David=20P=C3=A9rez=20Berm=C3=BAdez?= <32344641+ahsand97@users.noreply.github.com> Date: Thu, 20 Jan 2022 12:55:21 -0500 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4cba305..a1691fc 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Usage -name : apply icon to the window of the name supplied -id : apply icon to the window id supplied - Sets the window icon to the specified .png image. The image is loaded from + Sets the window icon to the specified image. The image is loaded from the file at runtime and sent to the X server; thereafter the file does not need to exist, and can be deleted/renamed/modified without the X server or window manager noticing.