Skip to content

Commit fdaedca

Browse files
authored
Remove VLA and imlib2's deprecated DATA32 type
@N-R-K pointed out the deprecated type and revived interest in removing the only usage of a VLA in this project (see the discussion: #193). Author: Guilherme Janczak <[email protected]> Reviewed-By: NRK <[email protected]> Reviewed-By: Daniel T. Borelli <[email protected]> Closes: #198
1 parent 1a55e8a commit fdaedca

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/scrot.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -398,42 +398,50 @@ Window scrotGetWindow(Display *display, Window window, int x, int y)
398398
return target;
399399
}
400400

401-
402401
void scrotGrabMousePointer(const Imlib_Image image, const int xOffset,
403402
const int yOffset)
404403
{
405-
XFixesCursorImage *xcim = XFixesGetCursorImage(disp);
406-
407-
if (!xcim) {
408-
warnx("Failed to get mouse cursor image.");
409-
return;
404+
XFixesCursorImage *xcim = NULL;
405+
uint32_t *pixels = NULL;
406+
/* Get the X cursor and the information we want. */
407+
if ((xcim = XFixesGetCursorImage(disp)) == NULL) {
408+
warnx("Can't get the cursor from X");
409+
goto end;
410410
}
411-
412411
const unsigned short width = xcim->width;
413412
const unsigned short height = xcim->height;
414-
const int x = (xcim->x - xcim->xhot) - xOffset;
415-
const int y = (xcim->y - xcim->yhot) - yOffset;
416-
DATA32 data[width * height];
417-
418-
for (size_t i = 0; i < width*height; i++)
419-
data[i] = xcim->pixels[i];
420-
421-
Imlib_Image imcursor = imlib_create_image_using_data(width, height, data);
413+
const size_t pixcnt = (size_t)width*height;
422414

423-
XFree(xcim);
415+
/* xcim->pixels wouldn't be valid if sizeof(*pixels)*pixcnt wrapped around.
416+
*/
417+
if ((pixels = malloc(sizeof(*pixels)*pixcnt)) == NULL) {
418+
warn("malloc()");
419+
goto end;
420+
}
424421

422+
/* Convert an X cursor into the format imlib wants. */
423+
for (size_t i = 0; i < pixcnt; i++)
424+
pixels[i] = xcim->pixels[i];
425+
Imlib_Image imcursor = imlib_create_image_using_data(width, height, pixels);
425426
if (!imcursor) {
426-
errx(EXIT_FAILURE,
427-
"scrotGrabMousePointer: Failed create image using data.");
427+
warnx("Can't create cursor image");
428+
goto end;
428429
}
429430

431+
/* Overlay the cursor into `image`. */
432+
const int x = (xcim->x - xcim->xhot) - xOffset;
433+
const int y = (xcim->y - xcim->yhot) - yOffset;
430434
imlib_context_set_image(imcursor);
431435
imlib_image_set_has_alpha(1);
432436
imlib_context_set_image(image);
433437
imlib_blend_image_onto_image(imcursor, 0, 0, 0, width, height, x, y, width,
434438
height);
435439
imlib_context_set_image(imcursor);
436440
imlib_free_image();
441+
442+
end:
443+
free(pixels);
444+
XFree(xcim);
437445
}
438446

439447
// It assumes that the local variable 'scrot.c:Imlib_Image image' is in context

0 commit comments

Comments
 (0)