xcursor: adjust style, use calloc()

Adjust the code style for wlroots, and use calloc() for structs.
This commit is contained in:
Simon Ser 2023-04-11 19:57:31 +02:00
parent b1e38fc7ea
commit 9affcaa93c
1 changed files with 19 additions and 47 deletions

View File

@ -47,17 +47,13 @@ static void xcursor_destroy(struct wlr_xcursor *cursor) {
static struct wlr_xcursor *xcursor_create_from_data( static struct wlr_xcursor *xcursor_create_from_data(
const struct cursor_metadata *metadata, struct wlr_xcursor_theme *theme) { const struct cursor_metadata *metadata, struct wlr_xcursor_theme *theme) {
struct wlr_xcursor *cursor; struct wlr_xcursor *cursor = calloc(1, sizeof(*cursor));
struct wlr_xcursor_image *image;
int size;
cursor = malloc(sizeof(*cursor));
if (!cursor) { if (!cursor) {
return NULL; return NULL;
} }
cursor->image_count = 1; cursor->image_count = 1;
cursor->images = malloc(sizeof(*cursor->images)); cursor->images = calloc(1, sizeof(*cursor->images));
if (!cursor->images) { if (!cursor->images) {
goto err_free_cursor; goto err_free_cursor;
} }
@ -65,7 +61,7 @@ static struct wlr_xcursor *xcursor_create_from_data(
cursor->name = strdup(metadata->name); cursor->name = strdup(metadata->name);
cursor->total_delay = 0; cursor->total_delay = 0;
image = malloc(sizeof(*image)); struct wlr_xcursor_image *image = calloc(1, sizeof(*image));
if (!image) { if (!image) {
goto err_free_images; goto err_free_images;
} }
@ -78,9 +74,8 @@ static struct wlr_xcursor *xcursor_create_from_data(
image->hotspot_y = metadata->hotspot_y; image->hotspot_y = metadata->hotspot_y;
image->delay = 0; image->delay = 0;
size = metadata->width * metadata->height * sizeof(uint32_t); int size = metadata->width * metadata->height * sizeof(uint32_t);
image->buffer = malloc(size); image->buffer = malloc(size);
if (!image->buffer) { if (!image->buffer) {
goto err_free_image; goto err_free_image;
} }
@ -102,42 +97,33 @@ err_free_cursor:
} }
static void load_default_theme(struct wlr_xcursor_theme *theme) { static void load_default_theme(struct wlr_xcursor_theme *theme) {
uint32_t i;
free(theme->name); free(theme->name);
theme->name = strdup("default"); theme->name = strdup("default");
theme->cursor_count = sizeof(cursor_metadata) / sizeof(cursor_metadata[0]); theme->cursor_count = sizeof(cursor_metadata) / sizeof(cursor_metadata[0]);
theme->cursors = malloc(theme->cursor_count * sizeof(*theme->cursors)); theme->cursors = malloc(theme->cursor_count * sizeof(*theme->cursors));
if (theme->cursors == NULL) { if (theme->cursors == NULL) {
theme->cursor_count = 0; theme->cursor_count = 0;
return; return;
} }
for (i = 0; i < theme->cursor_count; ++i) { for (uint32_t i = 0; i < theme->cursor_count; ++i) {
theme->cursors[i] = theme->cursors[i] =
xcursor_create_from_data(&cursor_metadata[i], theme); xcursor_create_from_data(&cursor_metadata[i], theme);
if (theme->cursors[i] == NULL) { if (theme->cursors[i] == NULL) {
break; break;
} }
} }
theme->cursor_count = i;
} }
static struct wlr_xcursor *xcursor_create_from_xcursor_images( static struct wlr_xcursor *xcursor_create_from_xcursor_images(
XcursorImages *images, struct wlr_xcursor_theme *theme) { XcursorImages *images, struct wlr_xcursor_theme *theme) {
struct wlr_xcursor *cursor; struct wlr_xcursor *cursor = calloc(1, sizeof(*cursor));
struct wlr_xcursor_image *image;
int i, size;
cursor = malloc(sizeof(*cursor));
if (!cursor) { if (!cursor) {
return NULL; return NULL;
} }
cursor->images = malloc(images->nimage * sizeof(cursor->images[0])); cursor->images = calloc(images->nimage, sizeof(cursor->images[0]));
if (!cursor->images) { if (!cursor->images) {
free(cursor); free(cursor);
return NULL; return NULL;
@ -146,8 +132,8 @@ static struct wlr_xcursor *xcursor_create_from_xcursor_images(
cursor->name = strdup(images->name); cursor->name = strdup(images->name);
cursor->total_delay = 0; cursor->total_delay = 0;
for (i = 0; i < images->nimage; i++) { for (int i = 0; i < images->nimage; i++) {
image = malloc(sizeof(*image)); struct wlr_xcursor_image *image = calloc(1, sizeof(*image));
if (image == NULL) { if (image == NULL) {
break; break;
} }
@ -160,7 +146,7 @@ static struct wlr_xcursor *xcursor_create_from_xcursor_images(
image->hotspot_y = images->images[i]->yhot; image->hotspot_y = images->images[i]->yhot;
image->delay = images->images[i]->delay; image->delay = images->images[i]->delay;
size = image->width * image->height * 4; size_t size = image->width * image->height * 4;
image->buffer = malloc(size); image->buffer = malloc(size);
if (!image->buffer) { if (!image->buffer) {
free(image); free(image);
@ -171,8 +157,8 @@ static struct wlr_xcursor *xcursor_create_from_xcursor_images(
memcpy(image->buffer, images->images[i]->pixels, size); memcpy(image->buffer, images->images[i]->pixels, size);
cursor->total_delay += image->delay; cursor->total_delay += image->delay;
cursor->images[i] = image; cursor->images[i] = image;
cursor->image_count++;
} }
cursor->image_count = i;
if (cursor->image_count == 0) { if (cursor->image_count == 0) {
free(cursor->name); free(cursor->name);
@ -186,22 +172,17 @@ static struct wlr_xcursor *xcursor_create_from_xcursor_images(
static void load_callback(XcursorImages *images, void *data) { static void load_callback(XcursorImages *images, void *data) {
struct wlr_xcursor_theme *theme = data; struct wlr_xcursor_theme *theme = data;
struct wlr_xcursor *cursor;
if (wlr_xcursor_theme_get_cursor(theme, images->name)) { if (wlr_xcursor_theme_get_cursor(theme, images->name)) {
XcursorImagesDestroy(images); XcursorImagesDestroy(images);
return; return;
} }
cursor = xcursor_create_from_xcursor_images(images, theme); struct wlr_xcursor *cursor = xcursor_create_from_xcursor_images(images, theme);
if (cursor) { if (cursor) {
struct wlr_xcursor **cursors;
theme->cursor_count++; theme->cursor_count++;
cursors = struct wlr_xcursor **cursors = realloc(theme->cursors,
realloc(theme->cursors, theme->cursor_count * sizeof(theme->cursors[0]));
theme->cursor_count * sizeof(theme->cursors[0]));
if (cursors == NULL) { if (cursors == NULL) {
theme->cursor_count--; theme->cursor_count--;
free(cursor); free(cursor);
@ -215,9 +196,7 @@ static void load_callback(XcursorImages *images, void *data) {
} }
struct wlr_xcursor_theme *wlr_xcursor_theme_load(const char *name, int size) { struct wlr_xcursor_theme *wlr_xcursor_theme_load(const char *name, int size) {
struct wlr_xcursor_theme *theme; struct wlr_xcursor_theme *theme = calloc(1, sizeof(*theme));
theme = malloc(sizeof(*theme));
if (!theme) { if (!theme) {
return NULL; return NULL;
} }
@ -251,9 +230,7 @@ out_error_name:
} }
void wlr_xcursor_theme_destroy(struct wlr_xcursor_theme *theme) { void wlr_xcursor_theme_destroy(struct wlr_xcursor_theme *theme) {
unsigned int i; for (unsigned int i = 0; i < theme->cursor_count; i++) {
for (i = 0; i < theme->cursor_count; i++) {
xcursor_destroy(theme->cursors[i]); xcursor_destroy(theme->cursors[i]);
} }
@ -264,9 +241,7 @@ void wlr_xcursor_theme_destroy(struct wlr_xcursor_theme *theme) {
struct wlr_xcursor *wlr_xcursor_theme_get_cursor(struct wlr_xcursor_theme *theme, struct wlr_xcursor *wlr_xcursor_theme_get_cursor(struct wlr_xcursor_theme *theme,
const char *name) { const char *name) {
unsigned int i; for (unsigned int i = 0; i < theme->cursor_count; i++) {
for (i = 0; i < theme->cursor_count; i++) {
if (strcmp(name, theme->cursors[i]->name) == 0) { if (strcmp(name, theme->cursors[i]->name) == 0) {
return theme->cursors[i]; return theme->cursors[i];
} }
@ -277,9 +252,6 @@ struct wlr_xcursor *wlr_xcursor_theme_get_cursor(struct wlr_xcursor_theme *theme
static int xcursor_frame_and_duration(struct wlr_xcursor *cursor, static int xcursor_frame_and_duration(struct wlr_xcursor *cursor,
uint32_t time, uint32_t *duration) { uint32_t time, uint32_t *duration) {
uint32_t t;
int i;
if (cursor->image_count == 1) { if (cursor->image_count == 1) {
if (duration) { if (duration) {
*duration = 0; *duration = 0;
@ -287,8 +259,8 @@ static int xcursor_frame_and_duration(struct wlr_xcursor *cursor,
return 0; return 0;
} }
i = 0; int i = 0;
t = time % cursor->total_delay; uint32_t t = time % cursor->total_delay;
/* If there is a 0 delay in the image set then this /* If there is a 0 delay in the image set then this
* loop breaks on it and we display that cursor until * loop breaks on it and we display that cursor until